Skip to content

Commit 6ef6a21

Browse files
committed
Just a syntax refactoring
1 parent 3aebb19 commit 6ef6a21

38 files changed

Lines changed: 150 additions & 195 deletions

Roslyn.Testing/Analyzer/CSharpDiagnosticAnalyzerTest.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System.Collections.Generic;
2-
using System.Linq;
2+
using JetBrains.Annotations;
33
using Microsoft.CodeAnalysis;
44
using Microsoft.CodeAnalysis.Diagnostics;
55
using Roslyn.Testing.Model;
66
using Shouldly;
77

88
namespace Roslyn.Testing.Analyzer;
99

10+
[PublicAPI]
1011
public abstract class CSharpDiagnosticAnalyzerTest<T> : FileReaderTest
1112
where T : DiagnosticAnalyzer, new()
1213
{
@@ -18,11 +19,9 @@ public abstract class CSharpDiagnosticAnalyzerTest<T> : FileReaderTest
1819
/// <inheritdoc />
1920
public override string PathToTestData => "./TestData/Analyzer/";
2021

21-
private readonly DiagnosticAnalyzer _diagnosticAnalyzer;
22+
private readonly DiagnosticAnalyzer _diagnosticAnalyzer = new T();
2223

23-
protected CSharpDiagnosticAnalyzerTest() => _diagnosticAnalyzer = new T();
24-
25-
protected virtual IEnumerable<MetadataReference> GetAdditionalReferences() => Enumerable.Empty<MetadataReference>();
24+
protected virtual IEnumerable<MetadataReference> GetAdditionalReferences() => [];
2625

2726
/// <summary>
2827
/// Called to test a C# DiagnosticAnalyzer when applied on the single inputted
@@ -34,23 +33,19 @@ public abstract class CSharpDiagnosticAnalyzerTest<T> : FileReaderTest
3433
/// DiagnosticResults that should appear after the analyzer
3534
/// is run on the source
3635
/// </param>
37-
protected void VerifyDiagnostic(string source, DiagnosticResult[] expected) => VerifyDiagnostic(new[]
38-
{
36+
protected void VerifyDiagnostic(string source, DiagnosticResult[] expected) => VerifyDiagnostic([
3937
source
40-
}, expected);
38+
], expected);
4139

42-
protected void VerifyDiagnostic(string source, DiagnosticResult expected) => VerifyDiagnostic(new[]
43-
{
40+
protected void VerifyDiagnostic(string source, DiagnosticResult expected) => VerifyDiagnostic([
4441
source
45-
}, new[]
46-
{
42+
], [
4743
expected
48-
});
44+
]);
4945

50-
protected void VerifyNoDiagnosticTriggered(string source) => VerifyDiagnostic(new[]
51-
{
46+
protected void VerifyNoDiagnosticTriggered(string source) => VerifyDiagnostic([
5247
source
53-
}, new DiagnosticResult[0]);
48+
], []);
5449

5550
/// <summary>
5651
/// Called to test a C# DiagnosticAnalyzer when applied on the inputted strings as

Roslyn.Testing/Analyzer/DiagnosticVerifier.Helpers.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace Roslyn.Testing.Analyzer;
1515
/// Class for turning strings into documents and getting the diagnostics on them
1616
/// All methods are static
1717
/// </summary>
18+
[PublicAPI]
1819
public abstract class DiagnosticVerifier
1920
{
2021
private static readonly MetadataReference CorlibReference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
@@ -171,10 +172,9 @@ private static Document[] GetDocuments(string[] sources, string language, IEnume
171172
/// <param name="references"></param>
172173
/// <returns> A Document created from the source string </returns>
173174
protected static Document CreateDocument(string source, string language = LanguageNames.CSharp,
174-
IEnumerable<MetadataReference> references = null) => CreateProject(new[]
175-
{
175+
IEnumerable<MetadataReference> references = null) => CreateProject([
176176
source
177-
}, language, references)
177+
], language, references)
178178
.Documents.First();
179179

180180
/// <summary>

Roslyn.Testing/Analyzer/DiagnosticVerifier.cs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Collections.Immutable;
43
using System.Diagnostics;
54
using System.Linq;
65
using System.Text;
6+
using JetBrains.Annotations;
77
using Microsoft.CodeAnalysis;
88
using Microsoft.CodeAnalysis.CSharp;
99
using Microsoft.CodeAnalysis.Diagnostics;
@@ -12,6 +12,7 @@
1212

1313
namespace Roslyn.Testing.Analyzer;
1414

15+
[PublicAPI]
1516
internal static class DiagnosticAnalyzerTestExtensions
1617
{
1718
private static readonly MetadataReference CoreLibraryReference =
@@ -29,13 +30,13 @@ internal static class DiagnosticAnalyzerTestExtensions
2930
private static readonly MetadataReference SystemDiagnosticReference =
3031
MetadataReference.CreateFromFile(typeof(Process).Assembly.Location);
3132

32-
internal static string DefaultFilePathPrefix = "Test";
33+
private const string DefaultFilePathPrefix = "Test";
3334

34-
internal static string CSharpDefaultFileExt = "cs";
35+
private const string CSharpDefaultFileExt = "cs";
3536

36-
internal static string VisualBasicDefaultExt = "vb";
37+
private const string VisualBasicDefaultExt = "vb";
3738

38-
internal static string TestProjectName = "TestProject";
39+
private const string TestProjectName = "TestProject";
3940

4041
#region [Get Diagnostics]
4142

@@ -86,7 +87,7 @@ public static Diagnostic[] GetSortedDiagnosticsFromDocuments(this DiagnosticAnal
8687
project.GetCompilationAsync()
8788
.GetAwaiter()
8889
.GetResult()
89-
.WithAnalyzers(ImmutableArray.Create(analyzer));
90+
.WithAnalyzers([analyzer]);
9091

9192
var diags = compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync()
9293
.GetAwaiter()
@@ -170,10 +171,9 @@ private static Document[] GetDocuments(string[] sources, string language, IEnume
170171
/// <param name="references"></param>
171172
/// <returns> A Document created from the source string </returns>
172173
public static Document CreateDocument(string source, string language, IEnumerable<MetadataReference> references = null) =>
173-
CreateProject(new[]
174-
{
174+
CreateProject([
175175
source
176-
}, language, references)
176+
], language, references)
177177
.Documents.First();
178178

179179
/// <summary>
@@ -375,14 +375,11 @@ private static VerifyDiagnosticAnalyzerResult VerifyDiagnosticLocation(Diagnosti
375375
}
376376

377377
// Only check column position if there is an actual column position in the real diagnostic
378-
if (actualLinePosition.Character > 0)
378+
if (actualLinePosition.Character > 0 && actualLinePosition.Character + 1 != expected.Column)
379379
{
380-
if (actualLinePosition.Character + 1 != expected.Column)
381-
{
382-
var msg = GetNotInExpectedColumn(analyzer, diagnostic, expected, actualLinePosition);
380+
var msg = GetNotInExpectedColumn(analyzer, diagnostic, expected, actualLinePosition);
383381

384-
return VerifyDiagnosticAnalyzerResult.Fail(msg);
385-
}
382+
return VerifyDiagnosticAnalyzerResult.Fail(msg);
386383
}
387384

388385
return VerifyDiagnosticAnalyzerResult.Ok();
@@ -393,46 +390,46 @@ private static string GetMismatchNumberOfDiagnosticsMessage(int expectedCount, i
393390
$"Mismatch between number of diagnostics returned, expected \"{expectedCount}\" actual \"{actualCount}\"{Environment.NewLine}{Environment.NewLine}Diagnostics:{Environment.NewLine}{diagnosticsOutput}{Environment.NewLine}";
394391

395392
private static string GetExpectedDiagnosticWithNoLocation(DiagnosticAnalyzer analyzer, Diagnostic actual) =>
396-
$"Expected:\nA project diagnostic with No location\nActual:\n{FormatDiagnostics(analyzer, new[] { actual })}";
393+
$"Expected:\nA project diagnostic with No location\nActual:\n{FormatDiagnostics(analyzer, [actual])}";
397394

398395
private static string GetNotExpectedLocation(DiagnosticAnalyzer analyzer,
399396
Diagnostic actual,
400397
DiagnosticResult expected,
401398
Location[] additionalLocations) =>
402-
$"Expected {expected.Locations.Length - 1} additional locations but got {additionalLocations.Length} for Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, new[] { actual })}{Environment.NewLine}";
399+
$"Expected {expected.Locations.Length - 1} additional locations but got {additionalLocations.Length} for Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, [actual])}{Environment.NewLine}";
403400

404401
private static string GetNoExpectedDiagnosticId(DiagnosticAnalyzer analyzer,
405402
Diagnostic actual,
406403
DiagnosticResult expected) =>
407-
$"Expected diagnostic id to be \"{expected.Id}\" was \"{actual.Id}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, new[] { actual })}{Environment.NewLine}";
404+
$"Expected diagnostic id to be \"{expected.Id}\" was \"{actual.Id}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, [actual])}{Environment.NewLine}";
408405

409406
private static string GetNotExpectedSeverityMessage(DiagnosticAnalyzer analyzer,
410407
Diagnostic actual,
411408
DiagnosticResult expected) =>
412-
$"Expected diagnostic severity to be \"{expected.Severity}\" was \"{actual.Severity}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, new[] { actual })}{Environment.NewLine}";
409+
$"Expected diagnostic severity to be \"{expected.Severity}\" was \"{actual.Severity}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, [actual])}{Environment.NewLine}";
413410

414411
private static string GetNotExcpectedMessage(DiagnosticAnalyzer analyzer,
415412
Diagnostic actual,
416413
DiagnosticResult expected) =>
417-
$"Expected diagnostic message to be \"{expected.Message}\" was \"{actual.GetMessage()}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, new[] { actual })}{Environment.NewLine}";
414+
$"Expected diagnostic message to be \"{expected.Message}\" was \"{actual.GetMessage()}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, [actual])}{Environment.NewLine}";
418415

419416
private static string GetNotInExpectedColumn(DiagnosticAnalyzer analyzer,
420417
Diagnostic diagnostic,
421418
DiagnosticResultLocation expected,
422419
LinePosition actualLinePosition) =>
423-
$"Expected diagnostic to start at column \"{expected.Column}\" was actually at column \"{actualLinePosition.Character + 1}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, new[] { diagnostic })}{Environment.NewLine}";
420+
$"Expected diagnostic to start at column \"{expected.Column}\" was actually at column \"{actualLinePosition.Character + 1}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, [diagnostic])}{Environment.NewLine}";
424421

425422
private static string GetNotInExpectedLineMessage(DiagnosticAnalyzer analyzer,
426423
Diagnostic diagnostic,
427424
DiagnosticResultLocation expected,
428425
LinePosition actualLinePosition) =>
429-
$"Expected diagnostic to be on line \"{expected.Line}\" was actually on line \"{actualLinePosition.Line + 1}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, new[] { diagnostic })}{Environment.NewLine}";
426+
$"Expected diagnostic to be on line \"{expected.Line}\" was actually on line \"{actualLinePosition.Line + 1}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, [diagnostic])}{Environment.NewLine}";
430427

431428
private static string GetNotInExpectedFileMessage(DiagnosticAnalyzer analyzer,
432429
Diagnostic diagnostic,
433430
DiagnosticResultLocation expected,
434431
FileLinePositionSpan actualSpan) =>
435-
$"Expected diagnostic to be in file \"{expected.Path}\" was actually in file \"{actualSpan.Path}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, new[] { diagnostic })}{Environment.NewLine}";
432+
$"Expected diagnostic to be in file \"{expected.Path}\" was actually in file \"{actualSpan.Path}\"{Environment.NewLine}{Environment.NewLine}Diagnostic:{Environment.NewLine} {FormatDiagnostics(analyzer, [diagnostic])}{Environment.NewLine}";
436433

437434
/// <summary>
438435
/// Helper method to format a Diagnostic into an easily readable string

Roslyn.Testing/CodeFix/CSharpCodeFixProviderTest.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32
using Microsoft.CodeAnalysis;
43
using Microsoft.CodeAnalysis.CodeFixes;
54
using Microsoft.CodeAnalysis.Diagnostics;
@@ -22,19 +21,13 @@ public abstract class CSharpCodeFixProviderTest<TDiagnosticAnalyzer, TCodeFixPro
2221
/// <inheritdoc />
2322
public override string PathToTestData => "./TestData/CodeFix/";
2423

25-
protected virtual IEnumerable<MetadataReference> GetAdditionalReferences() => Enumerable.Empty<MetadataReference>();
24+
protected virtual IEnumerable<MetadataReference> GetAdditionalReferences() => [];
2625

2726
#endregion
2827

29-
private readonly TCodeFixProvider _codeFixProvider;
28+
private readonly TCodeFixProvider _codeFixProvider = new();
3029

31-
private readonly TDiagnosticAnalyzer _diagnosticAnalyzer;
32-
33-
protected CSharpCodeFixProviderTest()
34-
{
35-
_diagnosticAnalyzer = new();
36-
_codeFixProvider = new();
37-
}
30+
private readonly TDiagnosticAnalyzer _diagnosticAnalyzer = new();
3831

3932
/// <summary>
4033
/// Called to test a C# codefix when applied on the inputted string as a source

Roslyn.Testing/CodeFix/CodeFixProviderTestExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading;
5+
using JetBrains.Annotations;
56
using Microsoft.CodeAnalysis;
67
using Microsoft.CodeAnalysis.CodeActions;
78
using Microsoft.CodeAnalysis.CodeFixes;
@@ -12,6 +13,7 @@
1213

1314
namespace Roslyn.Testing.CodeFix;
1415

16+
[PublicAPI]
1517
internal static class CodeFixProviderTestExtensions
1618
{
1719
/// <summary>
@@ -79,10 +81,9 @@ public static VerifyCodeFixProviderResult VerifyFix(this CodeFixProvider codeFix
7981
{
8082
var document = DiagnosticAnalyzerTestExtensions.CreateDocument(oldSource, language, additionalReferences);
8183

82-
var analyzerDiagnostics = analyzer.GetSortedDiagnosticsFromDocuments(new[]
83-
{
84+
var analyzerDiagnostics = analyzer.GetSortedDiagnosticsFromDocuments([
8485
document
85-
});
86+
]);
8687

8788
var compilerDiagnostics = document.GetCompilerDiagnostics();
8889
var attempts = analyzerDiagnostics.Length;
@@ -110,10 +111,9 @@ public static VerifyCodeFixProviderResult VerifyFix(this CodeFixProvider codeFix
110111

111112
document = document.ApplyFix(actions.ElementAt(0));
112113

113-
analyzerDiagnostics = analyzer.GetSortedDiagnosticsFromDocuments(new[]
114-
{
114+
analyzerDiagnostics = analyzer.GetSortedDiagnosticsFromDocuments([
115115
document
116-
});
116+
]);
117117

118118
var newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, document.GetCompilerDiagnostics());
119119

Roslyn.Testing/Model/DiagnosticResult.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ public struct DiagnosticResult
1111

1212
public DiagnosticResultLocation[] Locations
1313
{
14-
get =>
15-
_locations
16-
?? (_locations = new DiagnosticResultLocation[]
17-
{
18-
});
14+
get => _locations ??= [];
1915

2016
set => _locations = value;
2117
}

System.IO.Abstractions.Analyzers.Tests/Analyzers/DirectoryAnalyzerTests.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ public void Analyzer_is_triggered(string filename, int diagnosticLine, int diagn
3131
Id = DirectoryAnalyzer.DiagnosticId,
3232
Message = DirectoryAnalyzer.MessageFormat,
3333
Severity = DiagnosticSeverity.Warning,
34-
Locations = new[]
35-
{
36-
new DiagnosticResultLocation("Test0.cs", diagnosticLine, diagnosticColumn)
37-
}
34+
Locations = [new("Test0.cs", diagnosticLine, diagnosticColumn)]
3835
};
3936

4037
VerifyDiagnostic(source, expectedDiagnostic);
@@ -47,8 +44,8 @@ public void Empty_source_code_does_not_trigger_analyzer()
4744
VerifyNoDiagnosticTriggered(source);
4845
}
4946

50-
protected override IEnumerable<MetadataReference> GetAdditionalReferences() => new[]
51-
{
47+
protected override IEnumerable<MetadataReference> GetAdditionalReferences() =>
48+
[
5249
MetadataReference.CreateFromFile(typeof(IFileSystem).Assembly.Location)
53-
};
50+
];
5451
}

System.IO.Abstractions.Analyzers.Tests/Analyzers/DirectoryInfoAnalyzerTests.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ public void Analyzer_is_triggered(string filename, int diagnosticLine, int diagn
3030
Id = DirectoryInfoAnalyzer.DiagnosticId,
3131
Message = DirectoryInfoAnalyzer.MessageFormat,
3232
Severity = DiagnosticSeverity.Warning,
33-
Locations = new[]
34-
{
35-
new DiagnosticResultLocation("Test0.cs", diagnosticLine, diagnosticColumn)
36-
}
33+
Locations = [new("Test0.cs", diagnosticLine, diagnosticColumn)]
3734
};
3835

3936
VerifyDiagnostic(source, expectedDiagnostic);
@@ -46,8 +43,8 @@ public void Empty_source_code_does_not_trigger_analyzer()
4643
VerifyNoDiagnosticTriggered(source);
4744
}
4845

49-
protected override IEnumerable<MetadataReference> GetAdditionalReferences() => new[]
50-
{
46+
protected override IEnumerable<MetadataReference> GetAdditionalReferences() =>
47+
[
5148
MetadataReference.CreateFromFile(typeof(IFileSystem).Assembly.Location)
52-
};
49+
];
5350
}

System.IO.Abstractions.Analyzers.Tests/Analyzers/FileAnalyzerTests.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ public void Analyzer_is_triggered(string filename, int diagnosticLine, int diagn
2929
Id = Constants.Io0002,
3030
Message = FileAnalyzer.MessageFormat,
3131
Severity = DiagnosticSeverity.Warning,
32-
Locations = new[]
33-
{
34-
new DiagnosticResultLocation("Test0.cs", diagnosticLine, diagnosticColumn)
35-
}
32+
Locations = [new("Test0.cs", diagnosticLine, diagnosticColumn)]
3633
};
3734

3835
VerifyDiagnostic(source, expectedDiagnostic);
@@ -45,8 +42,8 @@ public void Empty_source_code_does_not_trigger_analyzer()
4542
VerifyNoDiagnosticTriggered(source);
4643
}
4744

48-
protected override IEnumerable<MetadataReference> GetAdditionalReferences() => new[]
49-
{
45+
protected override IEnumerable<MetadataReference> GetAdditionalReferences() =>
46+
[
5047
MetadataReference.CreateFromFile(typeof(IFileSystem).Assembly.Location)
51-
};
48+
];
5249
}

System.IO.Abstractions.Analyzers.Tests/Analyzers/FileInfoAnalyzerTests.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ public void Analyzer_is_triggered(string filename, int diagnosticLine, int diagn
2929
Id = FileInfoAnalyzer.DiagnosticId,
3030
Message = FileInfoAnalyzer.MessageFormat,
3131
Severity = DiagnosticSeverity.Warning,
32-
Locations = new[]
33-
{
34-
new DiagnosticResultLocation("Test0.cs", diagnosticLine, diagnosticColumn)
35-
}
32+
Locations = [new("Test0.cs", diagnosticLine, diagnosticColumn)]
3633
};
3734

3835
VerifyDiagnostic(source, expectedDiagnostic);
@@ -45,8 +42,8 @@ public void Empty_source_code_does_not_trigger_analyzer()
4542
VerifyNoDiagnosticTriggered(source);
4643
}
4744

48-
protected override IEnumerable<MetadataReference> GetAdditionalReferences() => new[]
49-
{
45+
protected override IEnumerable<MetadataReference> GetAdditionalReferences() =>
46+
[
5047
MetadataReference.CreateFromFile(typeof(IFileSystem).Assembly.Location)
51-
};
48+
];
5249
}

0 commit comments

Comments
 (0)