From 935bce8183af52e74bc9b7aa3ec94a8847c3bb74 Mon Sep 17 00:00:00 2001 From: Justin Baur <19896123+justindbaur@users.noreply.github.com> Date: Sun, 19 Nov 2023 20:54:11 -0500 Subject: [PATCH] Setup Baseline Compare --- .../{Example.csproj => Example.Tests.csproj} | 0 pretender.sln | 7 +- src/Pretender/Pretend.cs | 2 +- ...Creates_PretendISimpleInterface8199A3_g.cs | 29 +++++++ .../ReturningMethod/Pretender_Setups_g.cs | 36 ++++++++ ...pe_PretendISimpleInterface2AADE68_g_cs.txt | 77 ----------------- ...er_Type_PretendISimpleInterface8199A3_g.cs | 84 +++++++++++++++++++ test/SourceGeneratorTests/MainTests.cs | 35 +------- ...retender.SourceGenerator.SpecTests.csproj} | 6 +- test/SourceGeneratorTests/TestBase.cs | 34 +++++++- 10 files changed, 195 insertions(+), 115 deletions(-) rename example/{Example.csproj => Example.Tests.csproj} (100%) create mode 100644 test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Creates_PretendISimpleInterface8199A3_g.cs create mode 100644 test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Setups_g.cs delete mode 100644 test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Type_PretendISimpleInterface2AADE68_g_cs.txt create mode 100644 test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Type_PretendISimpleInterface8199A3_g.cs rename test/SourceGeneratorTests/{SourceGeneratorTests.csproj => Pretender.SourceGenerator.SpecTests.csproj} (92%) diff --git a/example/Example.csproj b/example/Example.Tests.csproj similarity index 100% rename from example/Example.csproj rename to example/Example.Tests.csproj diff --git a/pretender.sln b/pretender.sln index 0bba829..630fa41 100644 --- a/pretender.sln +++ b/pretender.sln @@ -11,7 +11,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pretender.SourceGenerator", EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{2667A3D7-30CA-4DF5-B2F4-A7554C6D3ADD}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGeneratorTests", "test\SourceGeneratorTests\SourceGeneratorTests.csproj", "{09EB76D6-C82D-48A9-A0F7-B9BCC10B7621}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pretender.SourceGenerator.SpecTests", "test\SourceGeneratorTests\Pretender.SourceGenerator.SpecTests.csproj", "{09EB76D6-C82D-48A9-A0F7-B9BCC10B7621}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9E33F6C1-8032-4CCA-A485-685B730A6EAE}" ProjectSection(SolutionItems) = preProject @@ -19,7 +19,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md EndProjectSection EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example", "example\Example.csproj", "{6D387D31-B80A-4E0C-A0F8-066185964A44}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.Tests", "example\Example.Tests.csproj", "{6D387D31-B80A-4E0C-A0F8-066185964A44}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "perf", "perf", "{DA78B66F-EE75-46B5-8EC8-6F498A8AFC64}" EndProject @@ -27,6 +27,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Comparison", "perf\Comparis EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pretender.Tests", "test\Pretender.Tests\Pretender.Tests.csproj", "{17552274-CC28-438A-80BB-4A161F95AB11}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "example", "example", "{3BF89FF8-E743-46DF-BC57-F4B617DB8D92}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -117,6 +119,7 @@ Global {F22B60A6-3E45-42EE-842D-321232A90968} = {DFB5E6EE-B017-40FD-BC67-CE0471060A68} {90CC0802-39BB-4390-8A06-C7FD0C14D5C7} = {DFB5E6EE-B017-40FD-BC67-CE0471060A68} {09EB76D6-C82D-48A9-A0F7-B9BCC10B7621} = {2667A3D7-30CA-4DF5-B2F4-A7554C6D3ADD} + {6D387D31-B80A-4E0C-A0F8-066185964A44} = {3BF89FF8-E743-46DF-BC57-F4B617DB8D92} {96C653E3-D10B-47A0-8E42-0B93119AE145} = {DA78B66F-EE75-46B5-8EC8-6F498A8AFC64} {17552274-CC28-438A-80BB-4A161F95AB11} = {2667A3D7-30CA-4DF5-B2F4-A7554C6D3ADD} EndGlobalSection diff --git a/src/Pretender/Pretend.cs b/src/Pretender/Pretend.cs index 5052100..10d9404 100644 --- a/src/Pretender/Pretend.cs +++ b/src/Pretender/Pretend.cs @@ -66,7 +66,7 @@ public void Verify(IPretendSetup pretendSetup, Called called) // TODO: Make this obsolete public void Handle(CallInfo callInfo) { - _calls = []; + _calls ??= []; _calls.Add(callInfo); if (_setups != null) diff --git a/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Creates_PretendISimpleInterface8199A3_g.cs b/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Creates_PretendISimpleInterface8199A3_g.cs new file mode 100644 index 0000000..d85c642 --- /dev/null +++ b/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Creates_PretendISimpleInterface8199A3_g.cs @@ -0,0 +1,29 @@ +namespace System.Runtime.CompilerServices +{ + using System; + using System.CodeDom.Compiler; + + [GeneratedCode("Pretender.SourceGenerator", "1.0.0.0")] + [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] + file sealed class InterceptsLocationAttribute : Attribute + { + public InterceptsLocationAttribute(string filePath, int line, int column) + { + } + } +} + +namespace Pretender.SourceGeneration +{ + using System.Runtime.CompilerServices; + using Pretender; + + file static class CreateInterceptors + { + [InterceptsLocation("MyTest.cs", 16, 38)] + internal static global::ISimpleInterface Create0(this Pretend pretend) + { + return new PretendISimpleInterface8199A3(pretend); + } + } +} \ No newline at end of file diff --git a/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Setups_g.cs b/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Setups_g.cs new file mode 100644 index 0000000..5235e48 --- /dev/null +++ b/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Setups_g.cs @@ -0,0 +1,36 @@ +namespace System.Runtime.CompilerServices +{ + using System; + using System.CodeDom.Compiler; + + [GeneratedCode("Pretender.SourceGenerator", "1.0.0.0")] + [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] + file sealed class InterceptsLocationAttribute : Attribute + { + public InterceptsLocationAttribute(string filePath, int line, int column) + { + } + } +} + +namespace Pretender.SourceGeneration +{ + using System; + using System.Runtime.CompilerServices; + using System.Linq.Expressions; + using System.Threading.Tasks; + using Pretender; + using Pretender.Internals; + + file static class SetupInterceptors + { + [InterceptsLocation("MyTest.cs", 13, 6)] + internal static IPretendSetup Setup0(this Pretend pretend, Func setupExpression) + { + return pretend.GetOrCreateSetup(0, static (pretend, expr) => + { + return new ReturningCompiledSetup(pretend, PretendISimpleInterface8199A3.MethodInfo_get_Bar_3685A65, matcher: Cache.NoOpMatcher, expr.Target, defaultValue: default); + }, setupExpression); + } + } +} \ No newline at end of file diff --git a/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Type_PretendISimpleInterface2AADE68_g_cs.txt b/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Type_PretendISimpleInterface2AADE68_g_cs.txt deleted file mode 100644 index 051fe36..0000000 --- a/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Type_PretendISimpleInterface2AADE68_g_cs.txt +++ /dev/null @@ -1,77 +0,0 @@ -// -#nullable enable -/// -internal class PretendISimpleInterface2AADE68 : global::ISimpleInterface -{ - public static readonly global::System.Reflection.MethodInfo MethodInfo_Foo_30A5A51 = typeof(global::ISimpleInterface).GetMethod(nameof(Foo))!; - public static readonly global::System.Reflection.MethodInfo MethodInfo_VoidMethod_369C2F4 = typeof(global::ISimpleInterface).GetMethod(nameof(VoidMethod))!; - public static readonly global::System.Reflection.MethodInfo MethodInfo_AsyncMethod_30D4D3B = typeof(global::ISimpleInterface).GetMethod(nameof(AsyncMethod))!; - public static readonly global::System.Reflection.MethodInfo MethodInfo_AsyncReturningMethod_24A71EA = typeof(global::ISimpleInterface).GetMethod(nameof(AsyncReturningMethod))!; - public static readonly global::System.Reflection.MethodInfo MethodInfo_TryParse_85E202 = typeof(global::ISimpleInterface).GetMethod(nameof(TryParse))!; - public static readonly global::System.Reflection.MethodInfo MethodInfo_get_Bar_3177B39 = typeof(global::ISimpleInterface).GetProperty(nameof(Bar)).GetMethod!; - public static readonly global::System.Reflection.MethodInfo MethodInfo_set_Bar_392D46 = typeof(global::ISimpleInterface).GetProperty(nameof(Bar)).SetMethod!; - private readonly global::Pretender.Pretend _pretend; - /// - public PretendISimpleInterface2AADE68(global::Pretender.Pretend pretend) - { - _pretend = pretend; - } - - public string? Foo(string? bar, int baz) - { - Span arguments = [bar, baz]; - var callInfo = new global::Pretender.CallInfo(MethodInfo_Foo_30A5A51, arguments); - _pretend.Handle(ref callInfo); - return (string)callInfo.ReturnValue; - } - - public void VoidMethod(bool baz) - { - Span arguments = [baz]; - var callInfo = new global::Pretender.CallInfo(MethodInfo_VoidMethod_369C2F4, arguments); - _pretend.Handle(ref callInfo); - } - - public global::System.Threading.Tasks.Task AsyncMethod() - { - Span arguments = []; - var callInfo = new global::Pretender.CallInfo(MethodInfo_AsyncMethod_30D4D3B, arguments); - _pretend.Handle(ref callInfo); - return (global::System.Threading.Tasks.Task)callInfo.ReturnValue; - } - - public global::System.Threading.Tasks.Task AsyncReturningMethod(string bar) - { - Span arguments = [bar]; - var callInfo = new global::Pretender.CallInfo(MethodInfo_AsyncReturningMethod_24A71EA, arguments); - _pretend.Handle(ref callInfo); - return (global::System.Threading.Tasks.Task)callInfo.ReturnValue; - } - - public bool TryParse(string thing, out bool myValue) - { - Span arguments = [thing, myValue]; - var callInfo = new global::Pretender.CallInfo(MethodInfo_TryParse_85E202, arguments); - _pretend.Handle(ref callInfo); - myValue = arguments[1]; - return (bool)callInfo.ReturnValue; - } - - public string Bar - { - get - { - Span arguments = []; - var callInfo = new global::Pretender.CallInfo(MethodInfo_get_Bar_3177B39, arguments); - _pretend.Handle(ref callInfo); - return (string)callInfo.ReturnValue; - } - - set - { - Span arguments = [value]; - var callInfo = new global::Pretender.CallInfo(MethodInfo_set_Bar_392D46, arguments); - _pretend.Handle(ref callInfo); - } - } -} \ No newline at end of file diff --git a/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Type_PretendISimpleInterface8199A3_g.cs b/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Type_PretendISimpleInterface8199A3_g.cs new file mode 100644 index 0000000..be8eb69 --- /dev/null +++ b/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_Type_PretendISimpleInterface8199A3_g.cs @@ -0,0 +1,84 @@ +// +#nullable enable +/// +namespace Pretender.SourceGeneration +{ + using System.Reflection; + using Pretender; + + /// + internal class PretendISimpleInterface8199A3 : global::ISimpleInterface + { + public static readonly MethodInfo MethodInfo_Foo_3EB6F56 = typeof(global::ISimpleInterface).GetMethod(nameof(Foo))!; + public static readonly MethodInfo MethodInfo_VoidMethod_4CB5F7 = typeof(global::ISimpleInterface).GetMethod(nameof(VoidMethod))!; + public static readonly MethodInfo MethodInfo_AsyncMethod_3177B39 = typeof(global::ISimpleInterface).GetMethod(nameof(AsyncMethod))!; + public static readonly MethodInfo MethodInfo_AsyncReturningMethod_35E1C1E = typeof(global::ISimpleInterface).GetMethod(nameof(AsyncReturningMethod))!; + public static readonly MethodInfo MethodInfo_TryParse_3859BF9 = typeof(global::ISimpleInterface).GetMethod(nameof(TryParse))!; + public static readonly MethodInfo MethodInfo_get_Bar_3685A65 = typeof(global::ISimpleInterface).GetProperty(nameof(Bar)).GetMethod!; + public static readonly MethodInfo MethodInfo_set_Bar_2D53694 = typeof(global::ISimpleInterface).GetProperty(nameof(Bar)).SetMethod!; + private readonly Pretend _pretend; + /// + public PretendISimpleInterface8199A3(Pretend pretend) + { + _pretend = pretend; + } + + public string? Foo(string? bar, int baz) + { + object? [] __arguments = [bar, baz]; + var __callInfo = new CallInfo(MethodInfo_Foo_3EB6F56, __arguments); + _pretend.Handle(__callInfo); + return (string? )__callInfo.ReturnValue; + } + + public void VoidMethod(bool baz) + { + object? [] __arguments = [baz]; + var __callInfo = new CallInfo(MethodInfo_VoidMethod_4CB5F7, __arguments); + _pretend.Handle(__callInfo); + } + + public global::System.Threading.Tasks.Task AsyncMethod() + { + object? [] __arguments = []; + var __callInfo = new CallInfo(MethodInfo_AsyncMethod_3177B39, __arguments); + _pretend.Handle(__callInfo); + return (global::System.Threading.Tasks.Task)__callInfo.ReturnValue; + } + + public global::System.Threading.Tasks.Task AsyncReturningMethod(string bar) + { + object? [] __arguments = [bar]; + var __callInfo = new CallInfo(MethodInfo_AsyncReturningMethod_35E1C1E, __arguments); + _pretend.Handle(__callInfo); + return (global::System.Threading.Tasks.Task)__callInfo.ReturnValue; + } + + public bool TryParse(string thing, out bool myValue) + { + object? [] __arguments = [thing, myValue]; + var __callInfo = new CallInfo(MethodInfo_TryParse_3859BF9, __arguments); + _pretend.Handle(__callInfo); + myValue = __arguments[1]; + return (bool)__callInfo.ReturnValue; + } + + public string Bar + { + get + { + object? [] __arguments = []; + var __callInfo = new CallInfo(MethodInfo_get_Bar_3685A65, __arguments); + _pretend.Handle(__callInfo); + return (string)__callInfo.ReturnValue; + } + + set + { + object? [] __arguments = [value]; + var __callInfo = new CallInfo(MethodInfo_set_Bar_2D53694, __arguments); + _pretend.Handle(__callInfo); + } + } + } +} \ No newline at end of file diff --git a/test/SourceGeneratorTests/MainTests.cs b/test/SourceGeneratorTests/MainTests.cs index 5b03519..450d664 100644 --- a/test/SourceGeneratorTests/MainTests.cs +++ b/test/SourceGeneratorTests/MainTests.cs @@ -5,32 +5,15 @@ public partial class MainTests : TestBase [Fact] public async Task ReturningMethod() { - var (result, compilation) = await RunGeneratorAsync($$""" + await RunAndCompareAsync($$""" var pretendSimpleInterface = Pretend.That(); - + pretendSimpleInterface .Setup(i => i.Bar) .Returns("Hi"); - + var pretend = pretendSimpleInterface.Create(); - - pretendSimpleInterface.Verify(i => i.Bar, 2); """); - - Assert.Equal(4, result.GeneratedSources.Length); - - //Assert.All(result.GeneratedSources, (result) => - //{ - // CompareAgainstBaseline(result); - //}); - var source1 = result.GeneratedSources[0]; - var text1 = source1.SourceText.ToString(); - var source2 = result.GeneratedSources[1]; - var text2 = source2.SourceText.ToString(); - var source3 = result.GeneratedSources[2]; - var text3 = source3.SourceText.ToString(); - var source4 = result.GeneratedSources[3]; - var text4 = source4.SourceText.ToString(); } @@ -40,18 +23,6 @@ public async Task Test2() var (result, compilation) = await RunGeneratorAsync($$""" var pretendSimpleInterface = Pretend.That(); - pretendSimpleInterface - .Setup(i => i.Foo("1", 2)) - .Returns("Hello"); - - pretendSimpleInterface - .Setup(i => i.Foo("1", 2)) - .Returns("Hello"); - - pretendSimpleInterface - .Setup(i => i.Foo("2", 3)) - .Returns("Bye!"); - var simpleInterface = pretendSimpleInterface.Create(); """); diff --git a/test/SourceGeneratorTests/SourceGeneratorTests.csproj b/test/SourceGeneratorTests/Pretender.SourceGenerator.SpecTests.csproj similarity index 92% rename from test/SourceGeneratorTests/SourceGeneratorTests.csproj rename to test/SourceGeneratorTests/Pretender.SourceGenerator.SpecTests.csproj index f9f574b..6010a8a 100644 --- a/test/SourceGeneratorTests/SourceGeneratorTests.csproj +++ b/test/SourceGeneratorTests/Pretender.SourceGenerator.SpecTests.csproj @@ -34,7 +34,11 @@ - + + + + + diff --git a/test/SourceGeneratorTests/TestBase.cs b/test/SourceGeneratorTests/TestBase.cs index ba1c323..ef09273 100644 --- a/test/SourceGeneratorTests/TestBase.cs +++ b/test/SourceGeneratorTests/TestBase.cs @@ -3,6 +3,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Text; +using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions; using Pretender; using Pretender.SourceGenerator; @@ -82,8 +83,20 @@ public SimpleAbstractClass(string arg) return (Assert.Single(runResult.Results), updateCompilation); } - public void CompareAgainstBaseline(GeneratedSourceResult result, [CallerMemberName] string testMethodName = null!) + public async Task RunAndCompareAsync(string source, [CallerMemberName] string testMethodName = null!) { + var (result, _) = await RunGeneratorAsync(source); + Assert.All(result.GeneratedSources, s => + { + CompareAgainstBaseline(s, testMethodName); + }); + } + + private void CompareAgainstBaseline(GeneratedSourceResult result, string testMethodName = null!) + { + var normalizedName = result.HintName[..^3].Replace('.', '_') + ".cs"; +#if !GENERATE_SOURCE + var fileInfo = new FileInfo(result.HintName); var resultFileName = result.HintName.Replace('.', '_'); var baseLineName = $"{GetType().Name}.{testMethodName}.{resultFileName}.txt"; var resourceName = typeof(TestBase).Assembly.GetManifestResourceNames() @@ -92,7 +105,24 @@ public void CompareAgainstBaseline(GeneratedSourceResult result, [CallerMemberNa using var stream = typeof(TestBase).Assembly.GetManifestResourceStream(resourceName)!; using var reader = new StreamReader(stream); Assert.Equal(reader.ReadToEnd(), result.SourceText.ToString()); - +#else + var baseDirectory = new DirectoryInfo(typeof(TestBase).Assembly.GetAssemblyLocation()) + .Parent?.Parent?.Parent?.Parent; + + if (baseDirectory == null || !baseDirectory.Exists) + { + throw new Exception("Could not find directory."); + } + + var baselinePath = Path.Combine( + baseDirectory.FullName, + "Baselines", + GetType().Name, + testMethodName, + normalizedName); + + File.WriteAllText(baselinePath, result.SourceText.ToString()); +#endif } private Task CreateCompilationAsync(string source)