From cd472d0fe3b9a400867fcab073f34d0b39061be1 Mon Sep 17 00:00:00 2001 From: JerrettDavis Date: Sat, 30 May 2026 21:40:27 -0500 Subject: [PATCH] test: cover prototype and template generator branches --- .../PrototypeGeneratorTests.cs | 39 +++++++++++++ .../TemplateGeneratorTests.cs | 56 +++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/test/PatternKit.Generators.Tests/PrototypeGeneratorTests.cs b/test/PatternKit.Generators.Tests/PrototypeGeneratorTests.cs index 1cb318cd..8cd5954a 100644 --- a/test/PatternKit.Generators.Tests/PrototypeGeneratorTests.cs +++ b/test/PatternKit.Generators.Tests/PrototypeGeneratorTests.cs @@ -1035,4 +1035,43 @@ public partial class Container ScenarioExpect.Contains("Cloneable.Clone()", generatedSource); ScenarioExpect.Contains("this.NonCloneable", generatedSource); } + + [Scenario("ShallowPrototype CopiesArraysAndCollections")] + [Fact] + public void ShallowPrototype_CopiesArraysAndCollections() + { + const string source = """ + using PatternKit.Generators.Prototype; + using System.Collections.Generic; + + namespace TestNamespace; + + [Prototype(Mode = PrototypeMode.Shallow)] + public partial class Snapshot + { + [PrototypeStrategy(PrototypeCloneStrategy.ShallowCopy)] + public int[] Scores { get; set; } = []; + + [PrototypeStrategy(PrototypeCloneStrategy.ShallowCopy)] + public List Tags { get; set; } = []; + } + """; + + var comp = RoslynTestHelpers.CreateCompilation(source, nameof(ShallowPrototype_CopiesArraysAndCollections)); + var gen = new PrototypeGenerator(); + _ = RoslynTestHelpers.Run(comp, gen, out var result, out var updated); + + ScenarioExpect.All(result.Results, r => ScenarioExpect.DoesNotContain(r.Diagnostics, d => d.Severity == DiagnosticSeverity.Error)); + + var generatedSource = result.Results + .SelectMany(r => r.GeneratedSources) + .First(gs => gs.HintName == "Snapshot.Prototype.g.cs") + .SourceText.ToString(); + + ScenarioExpect.Contains("((int[])this.Scores.Clone())", generatedSource); + ScenarioExpect.Contains("new global::System.Collections.Generic.List(this.Tags)", generatedSource); + + var emit = updated.Emit(Stream.Null); + ScenarioExpect.True(emit.Success, string.Join("\n", emit.Diagnostics)); + } } diff --git a/test/PatternKit.Generators.Tests/TemplateGeneratorTests.cs b/test/PatternKit.Generators.Tests/TemplateGeneratorTests.cs index ec73935c..9a52754a 100644 --- a/test/PatternKit.Generators.Tests/TemplateGeneratorTests.cs +++ b/test/PatternKit.Generators.Tests/TemplateGeneratorTests.cs @@ -1012,5 +1012,61 @@ private void TraceError(ImportContext ctx, Exception ex) { } ScenarioExpect.True(emit.Success, string.Join("\n", emit.Diagnostics)); } + [Scenario("ForceAsync Template GeneratesAsyncPathForSynchronousSteps")] + [Fact] + public void ForceAsync_Template_GeneratesAsyncPathForSynchronousSteps() + { + var source = """ + using PatternKit.Generators.Template; + using System.Collections.Generic; + + namespace PatternKit.Examples; + + public sealed class ImportContext + { + public List Log { get; } = new(); + } + + [Template(GenerateAsync = true, ForceAsync = true, ErrorPolicy = TemplateErrorPolicy.HandleAndContinue)] + public partial class ImportWorkflow + { + [TemplateHook(HookPoint.BeforeAll)] + private void Open(ImportContext ctx) => ctx.Log.Add("open"); + + [TemplateStep(0, Optional = true)] + private void Validate(ImportContext ctx) => ctx.Log.Add("validate"); + + [TemplateHook(HookPoint.AfterAll)] + private void Close(ImportContext ctx) => ctx.Log.Add("close"); + + [TemplateHook(HookPoint.OnError)] + private void CaptureError(ImportContext ctx, System.Exception ex) => ctx.Log.Add(ex.Message); + } + """; + + var comp = RoslynTestHelpers.CreateCompilation( + source, + assemblyName: nameof(ForceAsync_Template_GeneratesAsyncPathForSynchronousSteps)); + + var gen = new TemplateGenerator(); + _ = RoslynTestHelpers.Run(comp, gen, out var run, out var updated); + + ScenarioExpect.All(run.Results, r => ScenarioExpect.Empty(r.Diagnostics)); + + var generated = run.Results + .SelectMany(r => r.GeneratedSources) + .Single(gs => gs.HintName == "ImportWorkflow.Template.g.cs") + .SourceText.ToString(); + + ScenarioExpect.Contains("public async System.Threading.Tasks.ValueTask ExecuteAsync", generated); + ScenarioExpect.Contains("Open(ctx);", generated); + ScenarioExpect.Contains("Validate(ctx);", generated); + ScenarioExpect.Contains("Close(ctx);", generated); + ScenarioExpect.Contains("catch (System.Exception ex)", generated); + + var emit = updated.Emit(Stream.Null); + ScenarioExpect.True(emit.Success, string.Join("\n", emit.Diagnostics)); + } + #endregion }