Skip to content

Commit ddcb41b

Browse files
committed
more tests
1 parent 53ce47c commit ddcb41b

File tree

3 files changed

+213
-0
lines changed
  • test
    • Microsoft.TemplateEngine.IDE.IntegrationTests
    • Microsoft.TemplateEngine.TestTemplates/test_templates/TemplateWithStringCoalesce

3 files changed

+213
-0
lines changed

test/Microsoft.TemplateEngine.IDE.IntegrationTests/End2EndTests.cs

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Text.RegularExpressions;
45
using Microsoft.TemplateEngine.TestHelper;
56
using Microsoft.TemplateEngine.Utils;
67

@@ -73,5 +74,183 @@ internal async Task ValueForms_DerivedSymbolFromGeneratedSymbolTest()
7374

7475
await Verify(File.ReadAllText(targetFile));
7576
}
77+
78+
[Fact]
79+
internal async Task PortAndCoalesceTest_WithFallbackInput()
80+
{
81+
using Bootstrapper bootstrapper = GetBootstrapper();
82+
string templateLocation = GetTestTemplateLocation("TemplateWithPortsAndCoalesce");
83+
await InstallTemplateAsync(bootstrapper, templateLocation).ConfigureAwait(false);
84+
85+
string output = TestUtils.CreateTemporaryFolder();
86+
87+
IReadOnlyList<Abstractions.TemplateFiltering.ITemplateMatchInfo> foundTemplates = await bootstrapper.GetTemplatesAsync(new[] { WellKnownSearchFilters.NameFilter("TestAssets.TemplateWithPortsAndCoalesce") }).ConfigureAwait(false);
88+
Edge.Template.ITemplateCreationResult result = await bootstrapper.CreateAsync(foundTemplates[0].Info, "test-template", output, new Dictionary<string, string?>()).ConfigureAwait(false);
89+
90+
Assert.Equal(Edge.Template.CreationResultStatus.Success, result.Status);
91+
92+
string targetFile = Path.Combine(output, "bar.cs");
93+
Assert.True(File.Exists(targetFile));
94+
string fileContent = File.ReadAllText(targetFile);
95+
Assert.True(Regex.Match(
96+
fileContent,
97+
"""
98+
The port is (\d{4,5})
99+
The port is (\d{4,5})
100+
101+
""").Success);
102+
Assert.NotEqual(
103+
"""
104+
The port is 1234
105+
The port is 1235
106+
107+
""",
108+
fileContent);
109+
}
110+
111+
[Fact]
112+
internal async Task PortAndCoalesceTest_WithUserInput()
113+
{
114+
using Bootstrapper bootstrapper = GetBootstrapper();
115+
string templateLocation = GetTestTemplateLocation("TemplateWithPortsAndCoalesce");
116+
await InstallTemplateAsync(bootstrapper, templateLocation).ConfigureAwait(false);
117+
118+
string output = TestUtils.CreateTemporaryFolder();
119+
Dictionary<string, string?> parameters = new()
120+
{
121+
{ "userPort1", "4000" },
122+
{ "userPort2", "3000" },
123+
};
124+
125+
IReadOnlyList<Abstractions.TemplateFiltering.ITemplateMatchInfo> foundTemplates = await bootstrapper.GetTemplatesAsync(new[] { WellKnownSearchFilters.NameFilter("TestAssets.TemplateWithPortsAndCoalesce") }).ConfigureAwait(false);
126+
Edge.Template.ITemplateCreationResult result = await bootstrapper.CreateAsync(foundTemplates[0].Info, "test-template", output, parameters).ConfigureAwait(false);
127+
128+
Assert.Equal(Edge.Template.CreationResultStatus.Success, result.Status);
129+
130+
string targetFile = Path.Combine(output, "bar.cs");
131+
Assert.True(File.Exists(targetFile));
132+
string fileContent = File.ReadAllText(targetFile);
133+
Assert.Equal(
134+
"""
135+
The port is 4000
136+
The port is 3000
137+
138+
""",
139+
fileContent);
140+
}
141+
142+
[Fact]
143+
internal async Task StringCoalesceTest_WithFallbackInput()
144+
{
145+
using Bootstrapper bootstrapper = GetBootstrapper();
146+
string templateLocation = GetTestTemplateLocation("TemplateWithStringCoalesce");
147+
await InstallTemplateAsync(bootstrapper, templateLocation).ConfigureAwait(false);
148+
149+
string output = TestUtils.CreateTemporaryFolder();
150+
151+
IReadOnlyList<Abstractions.TemplateFiltering.ITemplateMatchInfo> foundTemplates = await bootstrapper.GetTemplatesAsync(new[] { WellKnownSearchFilters.NameFilter("TestAssets.TemplateWithStringCoalesce") }).ConfigureAwait(false);
152+
Edge.Template.ITemplateCreationResult result = await bootstrapper.CreateAsync(foundTemplates[0].Info, "test-template", output, new Dictionary<string, string?>()).ConfigureAwait(false);
153+
154+
Assert.Equal(Edge.Template.CreationResultStatus.Success, result.Status);
155+
156+
string targetFile = Path.Combine(output, "bar.cs");
157+
Assert.True(File.Exists(targetFile));
158+
string fileContent = File.ReadAllText(targetFile);
159+
Assert.Equal(
160+
"""
161+
var str = "fallback";
162+
163+
""",
164+
fileContent);
165+
}
166+
167+
[Fact]
168+
internal async Task StringCoalesceTest_WithUserInput()
169+
{
170+
using Bootstrapper bootstrapper = GetBootstrapper();
171+
string templateLocation = GetTestTemplateLocation("TemplateWithStringCoalesce");
172+
await InstallTemplateAsync(bootstrapper, templateLocation).ConfigureAwait(false);
173+
174+
string output = TestUtils.CreateTemporaryFolder();
175+
Dictionary<string, string?> parameters = new()
176+
{
177+
{ "userVal", "myVal" },
178+
};
179+
180+
IReadOnlyList<Abstractions.TemplateFiltering.ITemplateMatchInfo> foundTemplates = await bootstrapper.GetTemplatesAsync(new[] { WellKnownSearchFilters.NameFilter("TestAssets.TemplateWithStringCoalesce") }).ConfigureAwait(false);
181+
Edge.Template.ITemplateCreationResult result = await bootstrapper.CreateAsync(foundTemplates[0].Info, "test-template", output, parameters).ConfigureAwait(false);
182+
183+
Assert.Equal(Edge.Template.CreationResultStatus.Success, result.Status);
184+
185+
string targetFile = Path.Combine(output, "bar.cs");
186+
Assert.True(File.Exists(targetFile));
187+
string fileContent = File.ReadAllText(targetFile);
188+
Assert.Equal(
189+
"""
190+
var str = "myVal";
191+
192+
""",
193+
fileContent);
194+
}
195+
196+
[Fact]
197+
internal async Task StringCoalesceTest_WithEmptyUserInput()
198+
{
199+
using Bootstrapper bootstrapper = GetBootstrapper();
200+
string templateLocation = GetTestTemplateLocation("TemplateWithStringCoalesce");
201+
await InstallTemplateAsync(bootstrapper, templateLocation).ConfigureAwait(false);
202+
203+
string output = TestUtils.CreateTemporaryFolder();
204+
#pragma warning disable SA1122 // Use string.Empty for empty strings
205+
Dictionary<string, string?> parameters = new()
206+
{
207+
{ "userVal", "" },
208+
};
209+
#pragma warning restore SA1122 // Use string.Empty for empty strings
210+
211+
IReadOnlyList<Abstractions.TemplateFiltering.ITemplateMatchInfo> foundTemplates = await bootstrapper.GetTemplatesAsync(new[] { WellKnownSearchFilters.NameFilter("TestAssets.TemplateWithStringCoalesce") }).ConfigureAwait(false);
212+
Edge.Template.ITemplateCreationResult result = await bootstrapper.CreateAsync(foundTemplates[0].Info, "test-template", output, parameters).ConfigureAwait(false);
213+
214+
Assert.Equal(Edge.Template.CreationResultStatus.Success, result.Status);
215+
216+
string targetFile = Path.Combine(output, "bar.cs");
217+
Assert.True(File.Exists(targetFile));
218+
string fileContent = File.ReadAllText(targetFile);
219+
Assert.Equal(
220+
"""
221+
var str = "";
222+
223+
""",
224+
fileContent);
225+
}
226+
227+
[Fact]
228+
internal async Task StringCoalesceTest_WithNullUserInput()
229+
{
230+
using Bootstrapper bootstrapper = GetBootstrapper();
231+
string templateLocation = GetTestTemplateLocation("TemplateWithStringCoalesce");
232+
await InstallTemplateAsync(bootstrapper, templateLocation).ConfigureAwait(false);
233+
234+
string output = TestUtils.CreateTemporaryFolder();
235+
Dictionary<string, string?> parameters = new()
236+
{
237+
{ "userVal", null },
238+
};
239+
240+
IReadOnlyList<Abstractions.TemplateFiltering.ITemplateMatchInfo> foundTemplates = await bootstrapper.GetTemplatesAsync(new[] { WellKnownSearchFilters.NameFilter("TestAssets.TemplateWithStringCoalesce") }).ConfigureAwait(false);
241+
Edge.Template.ITemplateCreationResult result = await bootstrapper.CreateAsync(foundTemplates[0].Info, "test-template", output, parameters).ConfigureAwait(false);
242+
243+
Assert.Equal(Edge.Template.CreationResultStatus.Success, result.Status);
244+
245+
string targetFile = Path.Combine(output, "bar.cs");
246+
Assert.True(File.Exists(targetFile));
247+
string fileContent = File.ReadAllText(targetFile);
248+
Assert.Equal(
249+
"""
250+
var str = "A";
251+
252+
""",
253+
fileContent);
254+
}
76255
}
77256
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"author": "Test Asset",
3+
"classifications": [ "Test Asset" ],
4+
"name": "TemplateWithStringCoalesce",
5+
"generatorVersions": "[1.0.0.0-*)",
6+
"groupIdentity": "TestAssets.TemplateWithStringCoalesce",
7+
"precedence": "100",
8+
"identity": "TestAssets.TemplateWithStringCoalesce",
9+
"shortName": "TestAssets.TemplateWithStringCoalesce",
10+
"symbols": {
11+
"userVal": {
12+
"type": "parameter",
13+
"dataType": "string",
14+
"defaultIfOptionWithoutValue": "A"
15+
},
16+
"generatedVal": {
17+
"type": "generated",
18+
"generator": "constant",
19+
"parameters": {
20+
"value": "fallback"
21+
}
22+
},
23+
"port1": {
24+
"type": "generated",
25+
"generator": "coalesce",
26+
"parameters": {
27+
"sourceVariableName": "userVal",
28+
"fallbackVariableName": "generatedVal"
29+
},
30+
"replaces": "%VAL%"
31+
}
32+
}
33+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var str = "%VAL%";

0 commit comments

Comments
 (0)