Skip to content

Commit af29189

Browse files
committed
Ensure diagnostic order is deterministic in unit tests
The diagnostic order now considers both the location and ID as necessary to produce a deterministic ordering of diagnostics during unit tests. Fixes #1734
1 parent 8831b09 commit af29189

5 files changed

Lines changed: 16 additions & 16 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1600UnitTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public async Task TestRegressionMethodGlobalNamespaceAsync(string code, int colu
3333

3434
var expected = new[]
3535
{
36-
this.CSharpDiagnostic().WithLocation(4, column),
3736
new DiagnosticResult
3837
{
3938
Id = "CS0116",
4039
Severity = DiagnosticSeverity.Error,
4140
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 4, column) },
4241
Message = "A namespace cannot directly contain members such as fields or methods"
43-
}
42+
},
43+
this.CSharpDiagnostic().WithLocation(4, column)
4444
};
4545

4646
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers.Test/Helpers/DiagnosticVerifier.Helper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ protected virtual Project CreateProject(string[] sources, string language = Lang
224224
/// </summary>
225225
/// <param name="diagnostics">A collection of <see cref="Diagnostic"/>s to be sorted.</param>
226226
/// <returns>A collection containing the input <paramref name="diagnostics"/>, sorted by
227-
/// <see cref="Diagnostic.Location"/>.</returns>
227+
/// <see cref="Diagnostic.Location"/> and <see cref="Diagnostic.Id"/>.</returns>
228228
private static Diagnostic[] SortDistinctDiagnostics(IEnumerable<Diagnostic> diagnostics)
229229
{
230-
return diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
230+
return diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ThenBy(d => d.Id).ToArray();
231231
}
232232

233233
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1508UnitTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -853,10 +853,10 @@ public class TestClass /
853853
{
854854
new DiagnosticResult
855855
{
856-
Id = "CS1514",
856+
Id = "CS1022",
857857
Severity = DiagnosticSeverity.Error,
858858
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 3, 28) },
859-
Message = "{ expected"
859+
Message = "Type or namespace definition, or end-of-file expected"
860860
},
861861
new DiagnosticResult
862862
{
@@ -867,10 +867,10 @@ public class TestClass /
867867
},
868868
new DiagnosticResult
869869
{
870-
Id = "CS1022",
870+
Id = "CS1514",
871871
Severity = DiagnosticSeverity.Error,
872872
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 3, 28) },
873-
Message = "Type or namespace definition, or end-of-file expected"
873+
Message = "{ expected"
874874
},
875875
new DiagnosticResult
876876
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1011UnitTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,16 @@ void Method()
306306
{
307307
new DiagnosticResult
308308
{
309-
Id = "CS1003",
309+
Id = "CS0443",
310310
Severity = DiagnosticSeverity.Error,
311-
Message = "Syntax error, ',' expected",
311+
Message = "Syntax error; value expected",
312312
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 6, 28) }
313313
},
314314
new DiagnosticResult
315315
{
316-
Id = "CS0443",
316+
Id = "CS1003",
317317
Severity = DiagnosticSeverity.Error,
318-
Message = "Syntax error; value expected",
318+
Message = "Syntax error, ',' expected",
319319
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 6, 28) }
320320
},
321321
new DiagnosticResult

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1017UnitTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ void MethodName()
135135
{
136136
new DiagnosticResult()
137137
{
138-
Id = "CS1003",
139-
Message = "Syntax error, ',' expected",
138+
Id = "CS0443",
139+
Message = "Syntax error; value expected",
140140
Severity = DiagnosticSeverity.Error,
141141
},
142142
new DiagnosticResult()
143143
{
144-
Id = "CS0443",
145-
Message = "Syntax error; value expected",
144+
Id = "CS1003",
145+
Message = "Syntax error, ',' expected",
146146
Severity = DiagnosticSeverity.Error,
147147
},
148148
new DiagnosticResult()

0 commit comments

Comments
 (0)