Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
jjonescz committed Feb 20, 2023
1 parent 4190056 commit e34f9b1
Showing 1 changed file with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,95 @@ void verifyDiagnosticsWithSource(string source, (Diagnostic, TextSpan)[] reportD
}
}

[Fact, WorkItem(66337, "https://github.com/dotnet/roslyn/issues/66337")]
public void Diagnostics_Respect_SuppressMessageAttribute()
{
var gen001 = CSDiagnostic.Create("GEN001", "generators", "message", DiagnosticSeverity.Warning, DiagnosticSeverity.Warning, true, 2);

// reported diagnostics can have a location in source
verify("""
class C
{
//comment
}
""",
new[] { (gen001, "com") },
Diagnostic("GEN001", "com").WithLocation(3, 7));

// diagnostics are suppressed via SuppressMessageAttribute
verify("""
[System.Diagnostics.CodeAnalysis.SuppressMessage("", "GEN001")]
class C
{
//comment
}
""",
new[] { (gen001, "com") },
Diagnostic("GEN001", "com", isSuppressed: true).WithLocation(4, 7));

// but not when they don't have a source location
verify("""
[System.Diagnostics.CodeAnalysis.SuppressMessage("", "GEN001")]
class C
{
//comment
}
""",
new[] { (gen001, "") },
Diagnostic("GEN001").WithLocation(1, 1));

static void verify(string source, IReadOnlyList<(Diagnostic Diagnostic, string Location)> reportDiagnostics, params DiagnosticDescription[] expected)
{
var parseOptions = TestOptions.Regular;
source = source.Replace(Environment.NewLine, "\r\n");
var compilation = CreateCompilation(source, sourceFileName: "sourcefile.cs", options: TestOptions.DebugDllThrowing, parseOptions: parseOptions);
compilation.VerifyDiagnostics();
var syntaxTree = compilation.SyntaxTrees.Single();
var actualDiagnostics = reportDiagnostics
.Select(x =>
{
if (string.IsNullOrEmpty(x.Location))
{
return x.Diagnostic;
}
var start = source.IndexOf(x.Location);
Assert.True(start >= 0, $"Not found in source: '{x.Location}'");
var end = start + x.Location.Length;
return x.Diagnostic.WithLocation(Location.Create(syntaxTree, TextSpan.FromBounds(start, end)));
})
.ToImmutableArray();

var gen1 = new CallbackGenerator(c => { }, c =>
{
foreach (var d in actualDiagnostics)
{
c.ReportDiagnostic(d);
}
});

var driver = CSharpGeneratorDriver.Create(new[] { gen1 }, parseOptions: parseOptions);
driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var diagnostics);
outputCompilation.VerifyDiagnostics();
diagnostics.Verify(expected);

var gen2 = new IncrementalGeneratorWrapper(new PipelineCallbackGenerator(c =>
{
c.RegisterSourceOutput(c.CompilationProvider, (c, _) =>
{
foreach (var d in actualDiagnostics)
{
c.ReportDiagnostic(d);
}
});
}));

driver = CSharpGeneratorDriver.Create(new[] { gen2 }, parseOptions: parseOptions);
driver.RunGeneratorsAndUpdateCompilation(compilation, out outputCompilation, out diagnostics);
outputCompilation.VerifyDiagnostics();
diagnostics.Verify(expected);
}
}

[Fact]
public void GeneratorDriver_Prefers_Incremental_Generators()
{
Expand Down

0 comments on commit e34f9b1

Please sign in to comment.