Skip to content

Commit

Permalink
added different file modified test
Browse files Browse the repository at this point in the history
  • Loading branch information
jkurdek committed Apr 26, 2024
1 parent bb8c8cf commit a0eac85
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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<IncrementalStepRunReason> 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<string> sources, List<string> modified, Action<IncrementalStepRunReason> assert)
{
var inputCompilation = SourceGenHelpers.CreateCompilation(source1);
var inputCompilation = SourceGenHelpers.CreateCompilation(sources);
var cloneCompilation = inputCompilation.Clone();
var driver = SourceGenHelpers.CreateDriver();

Expand All @@ -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;

Expand All @@ -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));
}

Expand Down
16 changes: 14 additions & 2 deletions src/Controls/tests/BindingSourceGen.UnitTests/SourceGenHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,26 @@ internal static CodeGeneratorResult Run(string source)
Binding: resultBinding);
}

internal static Compilation CreateCompilation(string source)
private static Compilation CreateCompilationFromSyntaxTrees(List<SyntaxTree> 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),
MetadataReference.CreateFromFile(AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName("System.Runtime")).Location),
],
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<string> sources)
{
var syntaxTrees = sources.Select(source => CSharpSyntaxTree.ParseText(source, ParseOptions, path: $@"Path\To\Program{sources.IndexOf(source)}.cs")).ToList();
return CreateCompilationFromSyntaxTrees(syntaxTrees);
}
}

0 comments on commit a0eac85

Please sign in to comment.