|
20 | 20 | using Roslyn.Test.Utilities.TestGenerators; |
21 | 21 | using Roslyn.Utilities; |
22 | 22 | using Xunit; |
| 23 | +using Xunit.Abstractions; |
| 24 | + |
23 | 25 | namespace Microsoft.CodeAnalysis.CSharp.Semantic.UnitTests.SourceGeneration |
24 | 26 | { |
25 | | - public class GeneratorDriverTests |
| 27 | + public class GeneratorDriverTests(ITestOutputHelper output) |
26 | 28 | : CSharpTestBase |
27 | 29 | { |
| 30 | + private readonly ITestOutputHelper _output = output; |
| 31 | + |
28 | 32 | [Fact] |
29 | 33 | public void Running_With_No_Changes_Is_NoOp() |
30 | 34 | { |
@@ -3891,6 +3895,48 @@ public void Diagnostic_SpanOutsideRange_Incremental() |
3891 | 3895 | compilation.VerifyDiagnostics(); |
3892 | 3896 | } |
3893 | 3897 |
|
| 3898 | + [Fact] |
| 3899 | + [WorkItem("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1805836")] |
| 3900 | + [WorkItem("https://github.com/dotnet/roslyn/issues/82032")] |
| 3901 | + public void Diagnostic_SpanOutsideRange_Incremental_Update() |
| 3902 | + { |
| 3903 | + var source = "class SomewhatLongClassName {}"; |
| 3904 | + var parseOptions = TestOptions.RegularPreview; |
| 3905 | + Compilation compilation = CreateCompilation(source, options: TestOptions.DebugDll, parseOptions: parseOptions, sourceFileName: "/original"); |
| 3906 | + compilation.VerifyDiagnostics(); |
| 3907 | + |
| 3908 | + var generator = new PipelineCallbackGenerator(ctx => |
| 3909 | + { |
| 3910 | + var inputs = ctx.SyntaxProvider.CreateSyntaxProvider( |
| 3911 | + static (node, ct) => node.IsKind(SyntaxKind.ClassDeclaration), |
| 3912 | + static (ctx, ct) => 0); |
| 3913 | + ctx.RegisterSourceOutput(inputs, (ctx, input) => |
| 3914 | + { |
| 3915 | + var tree = compilation.SyntaxTrees.Single(); |
| 3916 | + ctx.ReportDiagnostic(CodeAnalysis.Diagnostic.Create( |
| 3917 | + "TEST0001", |
| 3918 | + "Test", |
| 3919 | + "Test diagnostic", |
| 3920 | + DiagnosticSeverity.Warning, |
| 3921 | + DiagnosticSeverity.Warning, |
| 3922 | + isEnabledByDefault: true, |
| 3923 | + warningLevel: 1, |
| 3924 | + location: Location.Create(tree, TextSpan.FromBounds(20, 21)))); |
| 3925 | + }); |
| 3926 | + }).AsSourceGenerator(); |
| 3927 | + |
| 3928 | + GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { generator }, parseOptions: parseOptions); |
| 3929 | + driver = driver.RunGeneratorsAndUpdateCompilation(compilation, out compilation, out var diagnostics); |
| 3930 | + diagnostics.Verify(Diagnostic("TEST0001", "a").WithLocation(1, 21)); |
| 3931 | + |
| 3932 | + source = "class C {}"; |
| 3933 | + compilation = compilation.ReplaceSyntaxTree(compilation.SyntaxTrees.Single(), CSharpSyntaxTree.ParseText(source, parseOptions, path: "/original")); |
| 3934 | + driver = driver.RunGeneratorsAndUpdateCompilation(compilation, out compilation, out diagnostics); |
| 3935 | + |
| 3936 | + VerifyArgumentExceptionDiagnostic(diagnostics.Single(), nameof(PipelineCallbackGenerator), string.Format(CodeAnalysisResources.InvalidDiagnosticLocationReported, "TEST0001", "/original"), "diagnostic"); |
| 3937 | + compilation.VerifyDiagnostics(); |
| 3938 | + } |
| 3939 | + |
3894 | 3940 | [ConditionalFact(typeof(IsEnglishLocal))] |
3895 | 3941 | [WorkItem("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1805836")] |
3896 | 3942 | public void Diagnostic_SpanOutsideRange_Incremental_AdditionalLocations() |
@@ -4237,19 +4283,16 @@ static void replace(ref Compilation compilation, CSharpParseOptions parseOptions |
4237 | 4283 | } |
4238 | 4284 | } |
4239 | 4285 |
|
4240 | | - private static void VerifyArgumentExceptionDiagnostic( |
| 4286 | + private void VerifyArgumentExceptionDiagnostic( |
4241 | 4287 | Diagnostic diagnostic, |
4242 | 4288 | string generatorName, |
4243 | 4289 | string message, |
4244 | 4290 | string parameterName, |
4245 | 4291 | bool initialization = false) |
4246 | 4292 | { |
4247 | | - var expectedMessage = |
4248 | | -#if NET |
4249 | | - $"{message} (Parameter '{parameterName}')"; |
4250 | | -#else |
4251 | | - $"{message}{Environment.NewLine}Parameter name: {parameterName}"; |
4252 | | -#endif |
| 4293 | + _output.WriteLine(diagnostic.ToString()); |
| 4294 | + |
| 4295 | + var expectedMessage = new ArgumentException(message: message, paramName: parameterName).Message; |
4253 | 4296 | VerifyGeneratorExceptionDiagnostic<ArgumentException>(diagnostic, generatorName, expectedMessage, initialization); |
4254 | 4297 | } |
4255 | 4298 |
|
|
0 commit comments