| 
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.  | 
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.  | 
3 | 3 | 
 
  | 
 | 4 | +using System.Text.RegularExpressions;  | 
4 | 5 | using Microsoft.TemplateEngine.TestHelper;  | 
5 | 6 | using Microsoft.TemplateEngine.Utils;  | 
6 | 7 | 
 
  | 
@@ -73,5 +74,183 @@ internal async Task ValueForms_DerivedSymbolFromGeneratedSymbolTest()  | 
73 | 74 | 
 
  | 
74 | 75 |             await Verify(File.ReadAllText(targetFile));  | 
75 | 76 |         }  | 
 | 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 | +        }  | 
76 | 255 |     }  | 
77 | 256 | }  | 
0 commit comments