From a0eac85b5a6c4a3d035617b6db2466cf5391c905 Mon Sep 17 00:00:00 2001 From: Jeremi Kurdek Date: Fri, 26 Apr 2024 15:05:42 +0200 Subject: [PATCH] added different file modified test --- .../IncrementalGenerationTests.cs | 35 ++++++++++++++----- .../SourceGenHelpers.cs | 16 +++++++-- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/Controls/tests/BindingSourceGen.UnitTests/IncrementalGenerationTests.cs b/src/Controls/tests/BindingSourceGen.UnitTests/IncrementalGenerationTests.cs index 729d093d3774..f0d12f7c81e1 100644 --- a/src/Controls/tests/BindingSourceGen.UnitTests/IncrementalGenerationTests.cs +++ b/src/Controls/tests/BindingSourceGen.UnitTests/IncrementalGenerationTests.cs @@ -36,7 +36,7 @@ public void DoesNotRegenerateCodeWhenNoChanges() label.SetBinding(Label.RotationProperty, static (string s) => s.Length); """; - RunGeneratorOnTwoSourcesAndVerifyResults(source, source, reason => Assert.True(reason == IncrementalStepRunReason.Unchanged || reason == IncrementalStepRunReason.Cached)); + RunGeneratorOnTwoSourcesAndVerifyResults([source], [source], reason => Assert.True(reason == IncrementalStepRunReason.Unchanged || reason == IncrementalStepRunReason.Cached)); } [Fact] @@ -54,7 +54,7 @@ public void DoesRegenerateCodeWhenSourceChanged() label.SetBinding(Label.RotationProperty, static (string s) => s); """; - RunGeneratorOnTwoSourcesAndVerifyResults(source, newSource, reason => Assert.True(reason == IncrementalStepRunReason.Modified)); + RunGeneratorOnTwoSourcesAndVerifyResults([source], [newSource], reason => Assert.True(reason == IncrementalStepRunReason.Modified)); } [Fact] @@ -73,7 +73,7 @@ public void DoesRegenerateCodeWhenNewCodeInsertedAbove() label.SetBinding(Label.RotationProperty, static (string s) => s.Length); """; - RunGeneratorOnTwoSourcesAndVerifyResults(source, newSource, reason => Assert.True(reason == IncrementalStepRunReason.Modified)); + RunGeneratorOnTwoSourcesAndVerifyResults([source], [newSource], reason => Assert.True(reason == IncrementalStepRunReason.Modified)); } [Fact] @@ -93,12 +93,32 @@ public void DoesNotRegenerateCodeWhenNewCodeInsertedBelow() var x = 42; """; - RunGeneratorOnTwoSourcesAndVerifyResults(source, newSource, reason => Assert.True(reason == IncrementalStepRunReason.Unchanged || reason == IncrementalStepRunReason.Cached)); + RunGeneratorOnTwoSourcesAndVerifyResults([source], [newSource], reason => Assert.True(reason == IncrementalStepRunReason.Unchanged || reason == IncrementalStepRunReason.Cached)); } - private static void RunGeneratorOnTwoSourcesAndVerifyResults(string source1, string source2, Action assert) + [Fact] + public void DoesNotRegerateCodeWhenDifferentFileEdited() + { + var fileASource = """ + using Microsoft.Maui.Controls; + var label = new Label(); + label.SetBinding(Label.RotationProperty, static (string s) => s.Length); + """; + + var fileBSource = """ + var x = 42; + """; + + var fileBModified = """ + var x = 43; + """; + + RunGeneratorOnTwoSourcesAndVerifyResults([fileASource, fileBSource], [fileASource, fileBModified], reason => Assert.True(reason == IncrementalStepRunReason.Unchanged || reason == IncrementalStepRunReason.Cached)); + } + + private static void RunGeneratorOnTwoSourcesAndVerifyResults(List sources, List modified, Action assert) { - var inputCompilation = SourceGenHelpers.CreateCompilation(source1); + var inputCompilation = SourceGenHelpers.CreateCompilation(sources); var cloneCompilation = inputCompilation.Clone(); var driver = SourceGenHelpers.CreateDriver(); @@ -110,7 +130,7 @@ private static void RunGeneratorOnTwoSourcesAndVerifyResults(string source1, str var reasons = steps.SelectMany(step => step.Value).SelectMany(x => x.Outputs).Select(x => x.Reason); Assert.All(reasons, reason => Assert.Equal(IncrementalStepRunReason.New, reason)); - var newCompilation = SourceGenHelpers.CreateCompilation(source2); + var newCompilation = SourceGenHelpers.CreateCompilation(modified); var newResult = driverWithCachedInfo.RunGenerators(newCompilation).GetRunResult().Results.Single(); var newSteps = newResult.TrackedSteps; @@ -120,7 +140,6 @@ private static void RunGeneratorOnTwoSourcesAndVerifyResults(string source1, str .SelectMany(x => x.Outputs) .Select(x => x.Reason); - Console.WriteLine(string.Join(", ", newReasons)); Assert.All(newReasons, reason => assert(reason)); } diff --git a/src/Controls/tests/BindingSourceGen.UnitTests/SourceGenHelpers.cs b/src/Controls/tests/BindingSourceGen.UnitTests/SourceGenHelpers.cs index 878af0c3c27f..3c1b7ab74a2d 100644 --- a/src/Controls/tests/BindingSourceGen.UnitTests/SourceGenHelpers.cs +++ b/src/Controls/tests/BindingSourceGen.UnitTests/SourceGenHelpers.cs @@ -54,9 +54,9 @@ internal static CodeGeneratorResult Run(string source) Binding: resultBinding); } - internal static Compilation CreateCompilation(string source) + private static Compilation CreateCompilationFromSyntaxTrees(List syntaxTrees) => CSharpCompilation.Create("compilation", - [CSharpSyntaxTree.ParseText(source, ParseOptions, path: @"Path\To\Program.cs")], + syntaxTrees, [ MetadataReference.CreateFromFile(typeof(Microsoft.Maui.Controls.BindableObject).GetTypeInfo().Assembly.Location), MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location), @@ -64,4 +64,16 @@ internal static Compilation CreateCompilation(string source) ], new CSharpCompilationOptions(OutputKind.ConsoleApplication) .WithNullableContextOptions(NullableContextOptions.Enable)); + + + internal static Compilation CreateCompilation(string source) + { + return CreateCompilationFromSyntaxTrees([CSharpSyntaxTree.ParseText(source, ParseOptions, path: @"Path\To\Program.cs")]); + } + + internal static Compilation CreateCompilation(List sources) + { + var syntaxTrees = sources.Select(source => CSharpSyntaxTree.ParseText(source, ParseOptions, path: $@"Path\To\Program{sources.IndexOf(source)}.cs")).ToList(); + return CreateCompilationFromSyntaxTrees(syntaxTrees); + } }