/
SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs
483 lines (424 loc) · 23.2 KB
/
SonarAnalysisContextBaseTest.ShouldAnalyzeTree.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2023 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using System.Text;
using Microsoft.CodeAnalysis.Text;
using Moq;
using SonarAnalyzer.Common;
using CS = SonarAnalyzer.Rules.CSharp;
using VB = SonarAnalyzer.Rules.VisualBasic;
namespace SonarAnalyzer.UnitTest.AnalysisContext;
public partial class SonarAnalysisContextBaseTest
{
private const string GeneratedFileName = "ExtraEmptyFile.g.";
private const string OtherFileName = "OtherFile";
[TestMethod]
public void ShouldAnalyzeTree_SonarLint()
{
var options = AnalysisScaffolding.CreateOptions(); // No SonarProjectConfig.xml
ShouldAnalyzeTree(options).Should().BeTrue();
}
[TestMethod]
public void ShouldAnalyzeTree_Scanner_UnchangedFiles_NotAvailable()
{
var options = AnalysisScaffolding.CreateOptions(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product)); // SonarProjectConfig.xml without UnchangedFiles.txt
ShouldAnalyzeTree(options).Should().BeTrue();
}
[TestMethod]
public void ShouldAnalyzeTree_Scanner_UnchangedFiles_Empty()
{
var options = CreateOptions(Array.Empty<string>());
ShouldAnalyzeTree(options).Should().BeTrue();
}
[TestMethod]
public void ShouldAnalyzeTree_Scanner_UnchangedFiles_ContainsTreeFile()
{
var options = CreateOptions(new[] { OtherFileName + ".cs" });
ShouldAnalyzeTree(options).Should().BeFalse("File is known to be Unchanged in Incremental PR analysis");
}
[TestMethod]
public void ShouldAnalyzeTree_Scanner_UnchangedFiles_ContainsOtherFile()
{
var options = CreateOptions(new[] { "ThisIsNotInCompilation.cs", "SomethingElse.cs" });
ShouldAnalyzeTree(options).Should().BeTrue();
}
[DataTestMethod]
[DataRow(GeneratedFileName, false)]
[DataRow(OtherFileName, true)]
public void ShouldAnalyzeTree_GeneratedFile_NoSonarLintXml(string fileName, bool expected)
{
var sonarLintXml = new DummySourceText(AnalysisScaffolding.GenerateSonarLintXmlContent(analyzeGeneratedCode: true));
var (compilation, tree) = CreateDummyCompilation(AnalyzerLanguage.CSharp, fileName);
var sut = CreateSut(compilation, CreateOptions(sonarLintXml, @"ResourceTests\Foo.xml"));
sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().Be(expected);
sonarLintXml.ToStringCallCount.Should().Be(0, "this file doesn't have 'SonarLint.xml' name");
}
[TestMethod]
public void ShouldAnalyzeTree_GeneratedFile_ShouldAnalyzeGeneratedProvider_IsCached()
{
var sonarLintXml = new DummySourceText(AnalysisScaffolding.GenerateSonarLintXmlContent(analyzeGeneratedCode: true));
var additionalText = new Mock<AdditionalText>();
additionalText.Setup(x => x.Path).Returns("SonarLint.xml");
additionalText.Setup(x => x.GetText(default)).Returns(sonarLintXml);
var tree = CreateDummyCompilation(AnalyzerLanguage.CSharp, OtherFileName).Tree;
var sut = CreateSut(new AnalyzerOptions(ImmutableArray.Create(additionalText.Object)));
// Call ShouldAnalyzeGenerated multiple times...
sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().BeTrue();
sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().BeTrue();
sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().BeTrue();
// GetText should be called every time ShouldAnalyzeGenerated is called...
additionalText.Verify(x => x.GetText(It.IsAny<CancellationToken>()), Times.Exactly(3));
sonarLintXml.ToStringCallCount.Should().Be(1); // ... but we should only try to read the file once
}
[DataTestMethod]
[DataRow(GeneratedFileName, false)]
[DataRow(OtherFileName, true)]
public void ShouldAnalyzeTree_GeneratedFile_InvalidSonarLintXml(string fileName, bool expected)
{
var sonarLintXml = new DummySourceText("Not valid xml");
var (compilation, tree) = CreateDummyCompilation(AnalyzerLanguage.CSharp, fileName);
var sut = CreateSut(compilation, CreateOptions(sonarLintXml));
// 1. Read -> no error
sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().Be(expected);
sonarLintXml.ToStringCallCount.Should().Be(1); // should have attempted to read the file
// 2. Read again to check that the load error doesn't prevent caching from working
sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().Be(expected);
sonarLintXml.ToStringCallCount.Should().Be(1); // should not have attempted to read the file again
}
[DataTestMethod]
[DataRow(GeneratedFileName)]
[DataRow(OtherFileName)]
public void ShouldAnalyzeTree_GeneratedFile_AnalyzeGenerated_AnalyzeAllFiles(string fileName)
{
var sonarLintXml = new DummySourceText(AnalysisScaffolding.GenerateSonarLintXmlContent(analyzeGeneratedCode: true));
var (compilation, tree) = CreateDummyCompilation(AnalyzerLanguage.CSharp, fileName);
var sut = CreateSut(compilation, CreateOptions(sonarLintXml));
sut.ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance).Should().BeTrue();
}
[DataTestMethod]
[DataRow(GeneratedFileName, LanguageNames.CSharp, false)]
[DataRow(OtherFileName, LanguageNames.CSharp, true)]
[DataRow(GeneratedFileName, LanguageNames.VisualBasic, false)]
[DataRow(OtherFileName, LanguageNames.VisualBasic, true)]
public void ShouldAnalyzeTree_CorrectSettingUsed(string fileName, string language, bool expected)
{
var sonarLintXml = new DummySourceText(AnalysisScaffolding.GenerateSonarLintXmlContent(language: language, analyzeGeneratedCode: false));
var analyzerLanguage = language == LanguageNames.CSharp ? AnalyzerLanguage.CSharp : AnalyzerLanguage.VisualBasic;
var (compilation, tree) = CreateDummyCompilation(analyzerLanguage, fileName);
var sut = CreateSut(compilation, CreateOptions(sonarLintXml));
GeneratedCodeRecognizer generatedCodeRecognizer = language == LanguageNames.CSharp ? CSharpGeneratedCodeRecognizer.Instance : VisualBasicGeneratedCodeRecognizer.Instance;
sut.ShouldAnalyzeTree(tree, generatedCodeRecognizer).Should().Be(expected);
sonarLintXml.ToStringCallCount.Should().Be(1, "file should be read once per language");
// Read again to check caching
sut.ShouldAnalyzeTree(tree, generatedCodeRecognizer).Should().Be(expected);
sonarLintXml.ToStringCallCount.Should().Be(1, "file should not have been read again");
}
// Until https://github.com/SonarSource/sonar-dotnet/issues/2228, we were considering a file as generated if the word "generated" was contained inside a region.
[DataTestMethod]
[DataRow("generated stuff")]
[DataRow("Contains FooGenerated methods")]
[DataRow("Windows Form Designer generated code")] // legacy Windows Forms used to include generated code in dev files, surrounded by such a region
[DataRow("Windows Form Designer GeNeRaTed code")] // legacy Windows Forms used to include generated code in dev files, surrounded by such a region
public void ShouldAnalyzeTree_IssuesRaisedOnPartiallyGenerated_LegacyWinFormsFile(string regionName)
{
var content = $$"""
class Sample
{
void HandWrittenEventHandler()
{
; // Noncompliant
}
#region {{regionName}}
void GeneratedStuff()
{
; // Noncompliant
}
#endregion
}
""";
var compilation = SolutionBuilder
.Create()
.AddProject(AnalyzerLanguage.CSharp, createExtraEmptyFile: false)
.AddSnippet(content, "Foo.cs")
.GetCompilation();
DiagnosticVerifier.Verify(compilation, new CS.EmptyStatement(), CompilationErrorBehavior.FailTest, compilation.SyntaxTrees.First());
}
[TestMethod]
public void ShouldAnalyzeTree_NoIssue_OnGeneratedFile_WithGeneratedName()
{
const string sourceCS = """
class Sample
{
void MethodWithEmptyStatement()
{
; // Noncompliant
}
}
""";
const string sourceVB = """
Module Sample
Sub Main()
Dim Foo() As String ' Noncompliant
End Sub
End Module
""";
VerifyEmpty("test.g.cs", sourceCS, new CS.EmptyStatement());
VerifyEmpty("test.g.vb", sourceVB, new VB.ArrayDesignatorOnVariable());
}
[TestMethod]
public void ShouldAnalyzeTree_NoIssueOnGeneratedFile_WithAutoGeneratedComment()
{
const string autogeneratedExpandedTagCS = """
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.3053
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
""";
const string autogeneratedCollapsedTagCS = """
// <autogenerated />
""";
const string autogeneratedGeneratedBySwaggerCS = """
/*
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
*
* OpenAPI spec version: 1.0.0
*
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*/
""";
const string autogeneratedGeneratedByProtobufCS = "// Generated by the protocol buffer compiler. DO NOT EDIT!";
const string sourceCS = """
// Extra line for concatenation
class Sample
{
void MethodWithEmptyStatement()
{
; // Noncompliant
}
}
""";
const string sourceVB = """
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:2.0.50727.4927
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Module Module1
Sub Main()
Dim Foo() As String ' Noncompliant
End Sub
End Module
""";
VerifyEmpty("test.cs", autogeneratedExpandedTagCS + sourceCS, new CS.EmptyStatement());
VerifyEmpty("test.cs", autogeneratedCollapsedTagCS + sourceCS, new CS.EmptyStatement());
VerifyEmpty("test.cs", autogeneratedGeneratedBySwaggerCS + sourceCS, new CS.EmptyStatement());
VerifyEmpty("test.cs", autogeneratedGeneratedByProtobufCS + sourceCS, new CS.EmptyStatement());
VerifyEmpty("test.vb", sourceVB, new VB.ArrayDesignatorOnVariable());
}
[TestMethod]
public void ShouldAnalyzeTree_NoIssueOnGeneratedFile_WithExcludedAttribute()
{
const string sourceCS = """
class Sample
{
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
void M()
{
; // Noncompliant
}
}
""";
const string sourceVB = """
Module Module1
<System.Diagnostics.DebuggerNonUserCodeAttribute()>
Sub Main()
Dim Foo() As String ' Noncompliant
End Sub
End Module
""";
VerifyEmpty("test.cs", sourceCS, new CS.EmptyStatement());
VerifyEmpty("test.vb", sourceVB, new VB.ArrayDesignatorOnVariable());
}
[TestMethod]
public void ShouldAnalyzeTree_NoIssueOnGeneratedAnnotatedLambda_WithExcludedAttribute()
{
const string sourceCs = """
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
class Sample
{
public void Bar()
{
[DebuggerNonUserCodeAttribute()] int Do() => 1;
Action a = [CompilerGenerated] () => { ;;; };
Action x = true
? ([DebuggerNonUserCodeAttribute] () => { ;;; })
: [GenericAttribute<int>] () => { ;;; }; // FN? Empty statement in lambda
Call([DebuggerNonUserCodeAttribute] (x) => { ;;; });
}
private void Call(Action<int> action) => action(1);
}
public class GenericAttribute<T> : Attribute { }
""";
VerifyEmpty("test.cs", sourceCs, new CS.EmptyStatement());
}
[DataTestMethod]
[DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, false)]
[DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)]
[DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)]
[DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)]
public void ShouldAnalyzeTree_Exclusions_ReturnExpected(string filePath, string[] exclusions, ProjectType projectType, bool expectedResult) =>
ShouldAnalyzeTree_WithExclusionInclusionParametersSet_ReturnsTrueForIncludedFilesOnly(filePath, projectType, expectedResult, exclusions: exclusions);
[DataTestMethod]
[DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, false)]
[DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)]
[DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)]
[DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)]
public void ShouldAnalyzeTree_GlobalExclusions_ReturnExpected(string filePath, string[] globalExclusions, ProjectType projectType, bool expectedResult) =>
ShouldAnalyzeTree_WithExclusionInclusionParametersSet_ReturnsTrueForIncludedFilesOnly(filePath, projectType, expectedResult, globalExclusions: globalExclusions);
[DataTestMethod]
[DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)]
[DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)]
[DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, false)]
[DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)]
public void ShouldAnalyzeTree_TestExclusions_ReturnExpected(string filePath, string[] testExclusions, ProjectType projectType, bool expectedResult) =>
ShouldAnalyzeTree_WithExclusionInclusionParametersSet_ReturnsTrueForIncludedFilesOnly(filePath, projectType, expectedResult, testExclusions: testExclusions);
[DataTestMethod]
[DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)]
[DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)]
[DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, false)]
[DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)]
public void ShouldAnalyzeTree_GlobalTestExclusions_ReturnExpected(string filePath, string[] globalTestExclusions, ProjectType projectType, bool expectedResult) =>
ShouldAnalyzeTree_WithExclusionInclusionParametersSet_ReturnsTrueForIncludedFilesOnly(filePath, projectType, expectedResult, globalTestExclusions: globalTestExclusions);
[DataTestMethod]
[DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)]
[DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, false)]
[DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)]
[DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, true)]
public void ShouldAnalyzeTree_Inclusions_ReturnExpected(string filePath, string[] inclusions, ProjectType projectType, bool expectedResult) =>
ShouldAnalyzeTree_WithExclusionInclusionParametersSet_ReturnsTrueForIncludedFilesOnly(filePath, projectType, expectedResult, inclusions: inclusions);
[DataTestMethod]
[DataRow("Foo", new string[] { "Foo" }, ProjectType.Product, true)]
[DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Product, true)]
[DataRow("Foo", new string[] { "Foo" }, ProjectType.Test, true)]
[DataRow("Foo", new string[] { "NotFoo" }, ProjectType.Test, false)]
public void ShouldAnalyzeTree_TestInclusions_ReturnExpected(string filePath, string[] testInclusions, ProjectType projectType, bool expectedResult) =>
ShouldAnalyzeTree_WithExclusionInclusionParametersSet_ReturnsTrueForIncludedFilesOnly(filePath, projectType, expectedResult, testInclusions: testInclusions);
[DataTestMethod]
[DataRow("Foo", new string[] { "Foo" }, new string[] { "Foo" }, false)]
[DataRow("Foo", new string[] { "NotFoo" }, new string[] { "Foo" }, false)]
[DataRow("Foo", new string[] { "Foo" }, new string[] { "NotFoo" }, true)]
[DataRow("Foo", new string[] { "NotFoo" }, new string[] { "NotFoo" }, false)]
public void ShouldAnalyzeTree_MixedInput_ProductProject_ReturnExpected(string filePath, string[] inclusions, string[] exclusions, bool expectedResult) =>
ShouldAnalyzeTree_WithExclusionInclusionParametersSet_ReturnsTrueForIncludedFilesOnly(filePath, ProjectType.Product, expectedResult, inclusions: inclusions, exclusions: exclusions);
[DataTestMethod]
[DataRow("Foo", new string[] { "Foo" }, new string[] { "Foo" }, false)]
[DataRow("Foo", new string[] { "NotFoo" }, new string[] { "Foo" }, false)]
[DataRow("Foo", new string[] { "Foo" }, new string[] { "NotFoo" }, true)]
[DataRow("Foo", new string[] { "NotFoo" }, new string[] { "NotFoo" }, false)]
public void ShouldAnalyzeTree_MixedInput_TestProject_ReturnExpected(string filePath, string[] testInclusions, string[] testExclusions, bool expectedResult) =>
ShouldAnalyzeTree_WithExclusionInclusionParametersSet_ReturnsTrueForIncludedFilesOnly(filePath, ProjectType.Test, expectedResult, testInclusions: testInclusions, testExclusions: testExclusions);
private void ShouldAnalyzeTree_WithExclusionInclusionParametersSet_ReturnsTrueForIncludedFilesOnly(
string fileName,
ProjectType projectType,
bool shouldAnalyze,
string language = LanguageNames.CSharp,
string[] exclusions = null,
string[] inclusions = null,
string[] globalExclusions = null,
string[] testExclusions = null,
string[] testInclusions = null,
string[] globalTestExclusions = null)
{
var analyzerLanguage = language == LanguageNames.CSharp ? AnalyzerLanguage.CSharp : AnalyzerLanguage.VisualBasic;
var sonarLintXml = AnalysisScaffolding.CreateSonarLintXml(
TestContext,
language: language,
exclusions: exclusions,
inclusions: inclusions,
globalExclusions: globalExclusions,
testExclusions: testExclusions,
testInclusions: testInclusions,
globalTestExclusions: globalTestExclusions);
var options = AnalysisScaffolding.CreateOptions(sonarLintXml);
var compilation = SolutionBuilder
.Create()
.AddProject(analyzerLanguage, createExtraEmptyFile: false)
.AddReferences(TestHelper.ProjectTypeReference(projectType))
.AddSnippet(string.Empty, fileName)
.GetCompilation();
var tree = compilation.SyntaxTrees.Single(x => x.FilePath.Contains(fileName));
var sut = CreateSut(compilation, options);
GeneratedCodeRecognizer codeRecognizer = language == LanguageNames.CSharp ? CSharpGeneratedCodeRecognizer.Instance : VisualBasicGeneratedCodeRecognizer.Instance;
sut.ShouldAnalyzeTree(tree, codeRecognizer).Should().Be(shouldAnalyze);
}
private AnalyzerOptions CreateOptions(string[] unchangedFiles) =>
AnalysisScaffolding.CreateOptions(AnalysisScaffolding.CreateSonarProjectConfigWithUnchangedFiles(TestContext, unchangedFiles));
private static AnalyzerOptions CreateOptions(SourceText sourceText, string path = @"ResourceTests\SonarLint.xml") =>
AnalysisScaffolding.CreateOptions(path, sourceText);
private static (Compilation Compilation, SyntaxTree Tree) CreateDummyCompilation(AnalyzerLanguage language, string treeFileName)
{
var compilation = SolutionBuilder.Create().AddProject(language).AddSnippet(string.Empty, OtherFileName + language.FileExtension).GetCompilation();
return (compilation, compilation.SyntaxTrees.Single(x => x.FilePath.Contains(treeFileName)));
}
private static bool ShouldAnalyzeTree(AnalyzerOptions options)
{
var tree = CreateDummyCompilation(AnalyzerLanguage.CSharp, OtherFileName).Tree;
return CreateSut(options).ShouldAnalyzeTree(tree, CSharpGeneratedCodeRecognizer.Instance);
}
private static void VerifyEmpty(string fileName, string snippet, DiagnosticAnalyzer analyzer)
{
var builder = new VerifierBuilder().AddAnalyzer(() => analyzer).AddSnippet(snippet, fileName);
var language = AnalyzerLanguage.FromPath(fileName);
builder = language.LanguageName switch
{
LanguageNames.CSharp => builder.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.Latest),
LanguageNames.VisualBasic => builder.WithLanguageVersion(Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.Latest),
_ => throw new UnexpectedLanguageException(language.LanguageName)
};
builder.VerifyNoIssueReported();
}
private sealed class DummySourceText : SourceText
{
private readonly string textToReturn;
public int ToStringCallCount { get; private set; }
public override char this[int position] => throw new NotImplementedException();
public override Encoding Encoding => throw new NotImplementedException();
public override int Length => throw new NotImplementedException();
public DummySourceText(string textToReturn) =>
this.textToReturn = textToReturn;
public override string ToString()
{
ToStringCallCount++;
return textToReturn;
}
public override void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count) =>
throw new NotImplementedException();
}
}