Skip to content

Commit

Permalink
Allow DiagnosticAnalyzer with duplicate id.
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred-brands authored and JohanLarsson committed Apr 3, 2022
1 parent 5dfd98f commit acd358d
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 76 deletions.
9 changes: 0 additions & 9 deletions Gu.Roslyn.Asserts.Tests/RoslynAssertTests/Diagnostics.Fail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,15 +538,6 @@ public static void WhenMoreThanOne()
Assert.AreEqual(expected, exception.Message);
}

[Test]
public static void DuplicateId()
{
var expected = "SyntaxNodeAnalyzer.SupportedDiagnostics has more than one descriptor with ID 'ID1'.";
var analyzer = new SyntaxNodeAnalyzer(Descriptors.Id1, Descriptors.Id1Duplicate);
var exception = Assert.Throws<AssertException>(() => RoslynAssert.Diagnostics(analyzer, ExpectedDiagnostic.Create(Descriptors.Id1), string.Empty));
Assert.AreEqual(expected, exception.Message);
}

[Test]
public static void SingleDocumentTwoErrors()
{
Expand Down
11 changes: 0 additions & 11 deletions Gu.Roslyn.Asserts.Tests/RoslynAssertTests/FixAll.Fail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,17 +425,6 @@ class C
";
CodeAssert.AreEqual(expected, exception.Message);
}

[Test]
public static void DuplicateId()
{
var expected = "SyntaxNodeAnalyzer.SupportedDiagnostics has more than one descriptor with ID 'ID1'.";
var analyzer = new SyntaxNodeAnalyzer(Descriptors.Id1, Descriptors.Id1Duplicate);
var fix = new DuplicateIdFix();
var expectedDiagnostic = ExpectedDiagnostic.Create(Descriptors.Id1);
var exception = Assert.Throws<AssertException>(() => RoslynAssert.FixAll(analyzer, fix, expectedDiagnostic, string.Empty, string.Empty));
Assert.AreEqual(expected, exception.Message);
}
}
}
}
11 changes: 0 additions & 11 deletions Gu.Roslyn.Asserts.Tests/RoslynAssertTests/NoFix.Fail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,6 @@ class C2
CodeAssert.AreEqual(expected, exception.Message);
}

[Test]
public static void DuplicateId()
{
var expected = "SyntaxNodeAnalyzer.SupportedDiagnostics has more than one descriptor with ID 'ID1'.";
var analyzer = new SyntaxNodeAnalyzer(Descriptors.Id1, Descriptors.Id1Duplicate);
var fix = new DuplicateIdFix();
var expectedDiagnostic = ExpectedDiagnostic.Create(Descriptors.Id1);
var exception = Assert.Throws<AssertException>(() => RoslynAssert.NoFix(analyzer, fix, expectedDiagnostic, string.Empty));
Assert.AreEqual(expected, exception.Message);
}

[Test]
public static void WhenNoAnalyzerDiagnosticsButCompilerError()
{
Expand Down
10 changes: 0 additions & 10 deletions Gu.Roslyn.Asserts.Tests/RoslynAssertTests/Valid.Fail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,6 @@ class C
Assert.AreEqual(expected, exception.Message);
}

[Test]
public static void DuplicateId()
{
var expected = "SyntaxNodeAnalyzer.SupportedDiagnostics has more than one descriptor with ID 'ID1'.";
var analyzer = new SyntaxNodeAnalyzer(Descriptors.Id1, Descriptors.Id1Duplicate);
var descriptor = Descriptors.Id1;
var exception = Assert.Throws<AssertException>(() => RoslynAssert.Valid(analyzer, descriptor, string.Empty));
Assert.AreEqual(expected, exception.Message);
}

[Test]
public static void WhenCompilerWarnings()
{
Expand Down
18 changes: 0 additions & 18 deletions Gu.Roslyn.Asserts.Tests/TestHelpers/CodeFixes/DuplicateIdFix.cs

This file was deleted.

18 changes: 1 addition & 17 deletions Gu.Roslyn.Asserts/RoslynAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,29 +158,13 @@ internal static void VerifySingleSupportedSuppression(DiagnosticSuppressor suppr
/// <param name="expectedId">The descriptor of the supported diagnostic.</param>
internal static void VerifyAnalyzerSupportsDiagnostic(DiagnosticAnalyzer analyzer, string expectedId)
{
if (analyzer.SupportedDiagnostics.Length > 0 &&
analyzer.SupportedDiagnostics.Length != analyzer.SupportedDiagnostics.Select(x => x.Id).Distinct().Count())
{
var message = $"{analyzer.GetType().Name}.SupportedDiagnostics has more than one descriptor with ID '{analyzer.SupportedDiagnostics.ToLookup(x => x.Id).First(x => x.Count() > 1).Key}'.";
throw new AssertException(message);
}

var descriptors = analyzer.SupportedDiagnostics.Count(x => x.Id == expectedId);
if (descriptors == 0)
if (!analyzer.SupportedDiagnostics.Any(x => x.Id == expectedId))
{
var message = $"{analyzer.GetType().Name} does not produce a diagnostic with ID '{expectedId}'.{Environment.NewLine}" +
$"{analyzer.GetType().Name}.{nameof(analyzer.SupportedDiagnostics)}: {Format(analyzer.SupportedDiagnostics)}.{Environment.NewLine}" +
$"The expected diagnostic is: '{expectedId}'.";
throw new AssertException(message);
}

if (descriptors > 1)
{
var message = $"{analyzer.GetType().Name} supports multiple diagnostics with ID '{expectedId}'.{Environment.NewLine}" +
$"{analyzer.GetType().Name}.{nameof(analyzer.SupportedDiagnostics)}: {Format(analyzer.SupportedDiagnostics)}.{Environment.NewLine}" +
$"The expected diagnostic is: {expectedId}.";
throw new AssertException(message);
}
}

/// <summary>
Expand Down

0 comments on commit acd358d

Please sign in to comment.