diff --git a/tests/ComputeSharp.Tests.SourceGenerators/Helpers/CSharpGeneratorTest{TGenerator}.cs b/tests/ComputeSharp.Tests.SourceGenerators/Helpers/CSharpGeneratorTest{TGenerator}.cs index 972c51f8..739f5110 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/Helpers/CSharpGeneratorTest{TGenerator}.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/Helpers/CSharpGeneratorTest{TGenerator}.cs @@ -77,13 +77,15 @@ internal static class CSharpGeneratorTest /// The reason for the first "Execute" step. /// The reason for the "Diagnostics" step. /// The reason for the "Output" step. + /// The reason for the output step for the diagnostics. /// The reason for the final output source. public static void VerifyIncrementalSteps( string source, string updatedSource, IncrementalStepRunReason executeReason, - IncrementalStepRunReason diagnosticsReason, + IncrementalStepRunReason? diagnosticsReason, IncrementalStepRunReason outputReason, + IncrementalStepRunReason? diagnosticsSourceReason, IncrementalStepRunReason sourceReason) { Compilation compilation = CreateCompilation(source); @@ -105,19 +107,61 @@ internal static class CSharpGeneratorTest GeneratorRunResult result = driver.GetRunResult().Results.Single(); - // Get the generated sources (only one output file should be produced) - (object Value, IncrementalStepRunReason Reason)[] sourceOuputs = - result.TrackedOutputSteps - .SelectMany(outputStep => outputStep.Value) - .SelectMany(output => output.Outputs) - .ToArray(); + // Get the generated sources and validate them. We have two possible cases: if no diagnostics + // are produced, then just the output source node is triggered. Otherwise, we'll also have one + // output node which is used to emit the gathered diagnostics from the initial transform step. + if (diagnosticsSourceReason is not null) + { + Assert.AreEqual( + expected: 2, + actual: + result.TrackedOutputSteps + .SelectMany(outputStep => outputStep.Value) + .SelectMany(output => output.Outputs) + .Count()); - Assert.AreEqual(1, sourceOuputs.Length); - Assert.AreEqual(sourceReason, sourceOuputs[0].Reason); + // The "Diagnostics" name has one more parent compared to "Output", because it also + // has one extra Where(...) call on the node (used to filter out empty diagnostics). + Assert.AreEqual( + expected: diagnosticsSourceReason, + actual: + result.TrackedOutputSteps + .Single().Value + .Single(run => run.Inputs[0].Source.Inputs[0].Source.Name == "Diagnostics") + .Outputs.Single().Reason); + + Assert.AreEqual( + expected: sourceReason, + actual: + result.TrackedOutputSteps + .Single().Value + .Single(run => run.Inputs[0].Source.Name == "Output") + .Outputs.Single().Reason); + } + else + { + (object Value, IncrementalStepRunReason Reason)[] sourceOuputs = + result.TrackedOutputSteps + .SelectMany(outputStep => outputStep.Value) + .SelectMany(output => output.Outputs) + .ToArray(); + + Assert.AreEqual(1, sourceOuputs.Length); + Assert.AreEqual(sourceReason, sourceOuputs[0].Reason); + } Assert.AreEqual(executeReason, result.TrackedSteps["Execute"].Single().Outputs[0].Reason); - Assert.AreEqual(diagnosticsReason, result.TrackedSteps["Diagnostics"].Single().Outputs[0].Reason); Assert.AreEqual(outputReason, result.TrackedSteps["Output"].Single().Outputs[0].Reason); + + // Check the diagnostics reason, which might not be present + if (diagnosticsReason is not null) + { + Assert.AreEqual(diagnosticsReason, result.TrackedSteps["Diagnostics"].Single().Outputs[0].Reason); + } + else + { + Assert.IsFalse(result.TrackedSteps.ContainsKey("Diagnostics")); + } } ///