Skip to content

Commit c698a4b

Browse files
committed
Enabled asserting partial string comparisons of generated source.
1 parent b8481d1 commit c698a4b

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

CommunityToolkit.Tooling.SampleGen.Tests/Helpers/TestHelpers.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace CommunityToolkit.Tooling.SampleGen.Tests.Helpers;
66
public static partial class TestHelpers
77
{
88
internal static SourceGeneratorRunResult RunSourceGenerator<TGenerator>(this string source, string assemblyName, string markdown = "") where TGenerator : class, IIncrementalGenerator, new() => RunSourceGenerator<TGenerator>(source.ToSyntaxTree(), assemblyName, markdown);
9-
9+
1010
internal static SourceGeneratorRunResult RunSourceGenerator<TGenerator>(this SyntaxTree syntaxTree, string assemblyName, string markdown = "")
1111
where TGenerator : class, IIncrementalGenerator, new()
1212
{
@@ -42,17 +42,19 @@ internal static void AssertNoCompilationErrors(this Compilation outputCompilatio
4242
Assert.IsTrue(generatedCompilationDiagnostics.All(x => x.Severity != DiagnosticSeverity.Error), $"Expected no generated compilation errors. Got: \n{string.Join("\n", generatedCompilationDiagnostics.Where(x => x.Severity == DiagnosticSeverity.Error).Select(x => $"[{x.Id}: {x.GetMessage()}]"))}");
4343
}
4444

45-
internal static void AssertSourceGenerated(this Compilation compilation, string filename, string expectedContents)
45+
internal static string GetFileContentsByName(this Compilation compilation, string filename)
4646
{
4747
var generatedTree = compilation.SyntaxTrees.SingleOrDefault(tree => Path.GetFileName(tree.FilePath) == filename);
48-
4948
Assert.IsNotNull(generatedTree, $"No file named {filename} was generated");
50-
Assert.AreEqual(expectedContents, generatedTree.ToString(), "Unexpected code generated");
49+
50+
return generatedTree.ToString();
51+
}
52+
53+
internal static void AssertSourceGenerated(this Compilation compilation, string filename, string expectedContents)
54+
{
5155
}
5256

5357
internal static void AssertDiagnosticsAre(this SourceGeneratorRunResult result, params DiagnosticDescriptor[] expectedDiagnosticDescriptors) => AssertDiagnosticsAre(result.Diagnostics, expectedDiagnosticDescriptors);
5458

5559
internal static void AssertNoCompilationErrors(this SourceGeneratorRunResult result) => AssertNoCompilationErrors(result.Compilation);
56-
57-
internal static void AssertSourceGenerated(this SourceGeneratorRunResult result, string filename, string expectedContents) => AssertSourceGenerated(result.Compilation, filename, expectedContents);
5860
}

CommunityToolkit.Tooling.SampleGen.Tests/ToolkitSampleGeneratedPaneTests.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,19 @@ public class UserControl { }
8080
.CreateCompilation("MyApp.Samples")
8181
.ToMetadataReference();
8282

83-
// Get all current referenced assemblies + our generated sample project.
84-
var headReferences = TestHelpers.GetAllReferencedAssemblies().Concat(new[] { sampleProjectAssembly });
85-
var headCompilation = string.Empty.ToSyntaxTree().CreateCompilation("MyApp.Head", headReferences);
83+
// Create application head that references generated sample project
84+
var headCompilation = string.Empty
85+
.ToSyntaxTree()
86+
.CreateCompilation("MyApp.Head", TestHelpers.GetAllReferencedAssemblies())
87+
.AddReferences(sampleProjectAssembly);
8688

8789
// Run source generator
8890
var result = headCompilation.RunSourceGenerator<ToolkitSampleMetadataGenerator>();
8991

9092
result.AssertDiagnosticsAre();
9193
result.AssertNoCompilationErrors();
9294

93-
result.AssertSourceGenerated(filename: "ToolkitSampleRegistry.g.cs", expectedContents: """
95+
Assert.AreEqual(result.Compilation.GetFileContentsByName("ToolkitSampleRegistry.g.cs"), """
9496
#nullable enable
9597
namespace CommunityToolkit.Tooling.SampleGen;
9698
@@ -101,7 +103,7 @@ public static class ToolkitSampleRegistry
101103
["Sample"] = new CommunityToolkit.Tooling.SampleGen.Metadata.ToolkitSampleMetadata("Sample", "Test Sample", "", typeof(MyApp.Sample), () => new MyApp.Sample(), null, null, new CommunityToolkit.Tooling.SampleGen.Metadata.IGeneratedToolkitSampleOptionViewModel[] { new CommunityToolkit.Tooling.SampleGen.Metadata.ToolkitSampleNumericOptionMetadataViewModel(name: "TextSize", initial: 12, min: 8, max: 48, step: 2, showAsNumberBox: false, title: "FontSize") })
102104
};
103105
}
104-
""");
106+
""", "Unexpected code generated");
105107
}
106108

107109
[TestMethod]
@@ -385,7 +387,7 @@ public class UserControl {{ }}
385387
[TestMethod]
386388
public void GeneratedPaneOption_ButtonAction()
387389
{
388-
var syntaxTree = $@"
390+
var sampleProjectAssembly = $@"
389391
using System.ComponentModel;
390392
using CommunityToolkit.Tooling.SampleGen;
391393
using CommunityToolkit.Tooling.SampleGen.Attributes;
@@ -405,19 +407,24 @@ private void RaiseNotification()
405407
namespace Windows.UI.Xaml.Controls
406408
{{
407409
public class UserControl {{ }}
408-
}}".ToSyntaxTree();
409-
410+
}}"
411+
.ToSyntaxTree()
412+
.CreateCompilation("MyApp.Samples")
413+
.ToMetadataReference();
410414

411-
// Create compilation builder with custom assembly name
412-
var compilation = syntaxTree.CreateCompilation("MyApp.Tests");
415+
// Create application head that references generated sample project
416+
var headCompilation = string.Empty
417+
.ToSyntaxTree()
418+
.CreateCompilation("MyApp.Head", TestHelpers.GetAllReferencedAssemblies())
419+
.AddReferences(sampleProjectAssembly);
413420

414421
// Run source generator
415-
var result = compilation.RunSourceGenerator<ToolkitSampleMetadataGenerator>();
422+
var result = headCompilation.RunSourceGenerator<ToolkitSampleMetadataGenerator>();
416423

417424
result.AssertDiagnosticsAre();
418425
result.AssertNoCompilationErrors();
419426

420-
result.AssertSourceGenerated(filename: "ToolkitSampleRegistry.g.cs", expectedContents: """
427+
Assert.AreEqual(result.Compilation.GetFileContentsByName("ToolkitSampleRegistry.g.cs"), """
421428
#nullable enable
422429
namespace CommunityToolkit.Tooling.SampleGen;
423430
@@ -428,6 +435,6 @@ public static class ToolkitSampleRegistry
428435
["Sample"] = new CommunityToolkit.Tooling.SampleGen.Metadata.ToolkitSampleMetadata("Sample", "Test Sample", "", typeof(MyApp.Sample), () => new MyApp.Sample(), null, null, new CommunityToolkit.Tooling.SampleGen.Metadata.IGeneratedToolkitSampleOptionViewModel[] { new CommunityToolkit.Tooling.SampleGen.Metadata.ToolkitSampleNumericOptionMetadataViewModel(name: "TextSize", initial: 12, min: 8, max: 48, step: 2, showAsNumberBox: false, title: "FontSize") })
429436
};
430437
}
431-
""");
438+
""", "Unexpected code generated");
432439
}
433440
}

0 commit comments

Comments
 (0)