|
12 | 12 | using System.Collections.Immutable;
|
13 | 13 | using System.Linq;
|
14 | 14 | using System.Reflection;
|
| 15 | +using Bicep.Core.Syntax; |
| 16 | +using Bicep.Core.UnitTests.Assertions; |
| 17 | +using Bicep.Core.UnitTests.Utils; |
15 | 18 |
|
16 | 19 | namespace Bicep.Core.UnitTests.Diagnostics
|
17 | 20 | {
|
@@ -155,7 +158,121 @@ private static object CreateMockParameter(ParameterInfo parameter, int index)
|
155 | 158 | return $"<param_{index}>";
|
156 | 159 | }
|
157 | 160 |
|
| 161 | + if (parameter.ParameterType == typeof(ObjectSyntax)) |
| 162 | + { |
| 163 | + return TestSyntaxFactory.CreateObject(Array.Empty<ObjectPropertySyntax>()); |
| 164 | + } |
| 165 | + |
158 | 166 | throw new AssertFailedException($"Unable to generate mock parameter value of type '{parameter.ParameterType}' for the diagnostic builder method.");
|
159 | 167 | }
|
| 168 | + |
| 169 | + private void ExpectDiagnosticWithFixedText(string text, string expectedText) |
| 170 | + { |
| 171 | + var result = CompilationHelper.Compile(text); |
| 172 | + result.Diagnostics.Should().HaveCount(1); |
| 173 | + |
| 174 | + FixableDiagnostic diagnostic = (FixableDiagnostic)result.Diagnostics.Single(); |
| 175 | + diagnostic.Code.Should().Be("BCP035"); |
| 176 | + diagnostic.Fixes.Should().HaveCount(1); |
| 177 | + |
| 178 | + var fix = diagnostic.Fixes.Single(); |
| 179 | + fix.Replacements.Should().HaveCount(1); |
| 180 | + |
| 181 | + var replacement = fix.Replacements.Single(); |
| 182 | + |
| 183 | + var actualText = text.Remove(replacement.Span.Position, replacement.Span.Length); |
| 184 | + actualText = actualText.Insert(replacement.Span.Position, replacement.Text); |
| 185 | + |
| 186 | + // Normalize line endings |
| 187 | + expectedText = expectedText.Replace("\r\n", "\n").Replace("\n", Environment.NewLine); |
| 188 | + actualText = actualText.Replace("\r\n", "\n").Replace("\n", Environment.NewLine); |
| 189 | + |
| 190 | + actualText.Should().Be(expectedText); |
| 191 | + } |
| 192 | + |
| 193 | + [DataRow(@" |
| 194 | + resource vnet 'Microsoft.Network/virtualNetworks@2018-10-01' = { |
| 195 | + }", |
| 196 | + @" |
| 197 | + resource vnet 'Microsoft.Network/virtualNetworks@2018-10-01' = { |
| 198 | + name: |
| 199 | + }" |
| 200 | + )] |
| 201 | + [DataRow(@" |
| 202 | + resource vnet 'Microsoft.Network/virtualNetworks@2018-10-01' = { |
| 203 | +
|
| 204 | + }", |
| 205 | + @" |
| 206 | + resource vnet 'Microsoft.Network/virtualNetworks@2018-10-01' = { |
| 207 | + name: |
| 208 | + }" |
| 209 | + )] |
| 210 | + // There is leading whitespace in this one |
| 211 | + [DataRow(@" |
| 212 | + resource vnet 'Microsoft.Network/virtualNetworks@2018-10-01' = { |
| 213 | + |
| 214 | + }", |
| 215 | + @" |
| 216 | + resource vnet 'Microsoft.Network/virtualNetworks@2018-10-01' = { |
| 217 | + name: |
| 218 | + }" |
| 219 | + )] |
| 220 | + [DataRow(@" |
| 221 | + resource vnet 'Microsoft.Network/virtualNetworks@2018-10-01' = { |
| 222 | + location: 'westus2' |
| 223 | + }", |
| 224 | + @" |
| 225 | + resource vnet 'Microsoft.Network/virtualNetworks@2018-10-01' = { |
| 226 | + location: 'westus2' |
| 227 | + name: |
| 228 | + }" |
| 229 | + )] |
| 230 | + [DataRow(@" |
| 231 | + resource vnet 'Microsoft.Network/virtualNetworks@2018-10-01' = { |
| 232 | + location: 'westus2' |
| 233 | + }", |
| 234 | + @" |
| 235 | + resource vnet 'Microsoft.Network/virtualNetworks@2018-10-01' = { |
| 236 | + location: 'westus2' |
| 237 | + name: |
| 238 | + }" |
| 239 | + )] |
| 240 | + [DataRow(@" |
| 241 | + resource appService 'Microsoft.Web/serverFarms@2020-06-01' = { |
| 242 | + sku: { |
| 243 | +
|
| 244 | + name: 'D1' |
| 245 | +
|
| 246 | + } |
| 247 | + // comment |
| 248 | + }", |
| 249 | + @" |
| 250 | + resource appService 'Microsoft.Web/serverFarms@2020-06-01' = { |
| 251 | + sku: { |
| 252 | +
|
| 253 | + name: 'D1' |
| 254 | +
|
| 255 | + } |
| 256 | + // comment |
| 257 | + location: |
| 258 | + name: |
| 259 | + }" |
| 260 | + )] |
| 261 | + [DataRow(@" |
| 262 | + resource appService 'Microsoft.Web/serverFarms@2020-06-01' = { |
| 263 | + sku: {} |
| 264 | + }", |
| 265 | + @" |
| 266 | + resource appService 'Microsoft.Web/serverFarms@2020-06-01' = { |
| 267 | + sku: {} |
| 268 | + location: |
| 269 | + name: |
| 270 | + }" |
| 271 | + )] |
| 272 | + [DataTestMethod] |
| 273 | + public void MissingTypePropertiesHasFix(string text, string expectedFix) |
| 274 | + { |
| 275 | + ExpectDiagnosticWithFixedText(text, expectedFix); |
| 276 | + } |
160 | 277 | }
|
161 | 278 | }
|
0 commit comments