diff --git a/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_g.cs b/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_g.cs index 1cca5be..34858aa 100644 --- a/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_g.cs +++ b/test/SourceGeneratorTests/Baselines/MainTests/ReturningMethod/Pretender_g.cs @@ -40,18 +40,18 @@ namespace Pretender.SourceGeneration public static readonly MethodInfo get_Bar_MethodInfo = typeof(PretendISimpleInterface).GetProperty(nameof(Bar))!.GetMethod; public static readonly MethodInfo set_Bar_MethodInfo = typeof(PretendISimpleInterface).GetProperty(nameof(Bar))!.SetMethod; - private readonly Pretend _pretend; + private readonly ICallHandler _callHandler; - public PretendISimpleInterface(Pretend pretend) + public PretendISimpleInterface(ICallHandler callHandler) { - _pretend = pretend; + _callHandler = callHandler; } public string? Foo(string? bar, int baz) { object?[] __arguments__ = [bar, baz]; var __callInfo__ = new CallInfo(Foo_MethodInfo, __arguments__); - _pretend.Handle(__callInfo__); + _callHandler.Handle(__callInfo__); return (string?)__callInfo__.ReturnValue; } @@ -59,14 +59,14 @@ public void VoidMethod(bool baz) { object?[] __arguments__ = [baz]; var __callInfo__ = new CallInfo(VoidMethod_MethodInfo, __arguments__); - _pretend.Handle(__callInfo__); + _callHandler.Handle(__callInfo__); } public global::System.Threading.Tasks.Task AsyncMethod() { object?[] __arguments__ = []; var __callInfo__ = new CallInfo(AsyncMethod_MethodInfo, __arguments__); - _pretend.Handle(__callInfo__); + _callHandler.Handle(__callInfo__); return (global::System.Threading.Tasks.Task)__callInfo__.ReturnValue; } @@ -74,7 +74,7 @@ public void VoidMethod(bool baz) { object?[] __arguments__ = [bar]; var __callInfo__ = new CallInfo(AsyncReturningMethod_MethodInfo, __arguments__); - _pretend.Handle(__callInfo__); + _callHandler.Handle(__callInfo__); return (global::System.Threading.Tasks.Task)__callInfo__.ReturnValue; } @@ -82,7 +82,7 @@ public bool TryParse(string thing, out bool myValue) { object?[] __arguments__ = [thing, myValue]; var __callInfo__ = new CallInfo(TryParse_MethodInfo, __arguments__); - _pretend.Handle(__callInfo__); + _callHandler.Handle(__callInfo__); myValue = __arguments__[1]; return (bool)__callInfo__.ReturnValue; } @@ -93,14 +93,14 @@ public string Bar { object?[] __arguments__ = []; var __callInfo__ = new CallInfo(get_Bar_MethodInfo, __arguments__); - _pretend.Handle(__callInfo__); + _callHandler.Handle(__callInfo__); return (string)__callInfo__.ReturnValue; } set { object?[] __arguments__ = [value]; var __callInfo__ = new CallInfo(set_Bar_MethodInfo, __arguments__); - _pretend.Handle(__callInfo__); + _callHandler.Handle(__callInfo__); } } } diff --git a/test/SourceGeneratorTests/Baselines/MainTests/TaskOfTMethod/Pretender_g.cs b/test/SourceGeneratorTests/Baselines/MainTests/TaskOfTMethod/Pretender_g.cs new file mode 100644 index 0000000..f3f5a67 --- /dev/null +++ b/test/SourceGeneratorTests/Baselines/MainTests/TaskOfTMethod/Pretender_g.cs @@ -0,0 +1,89 @@ +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0612, CS0618 + +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.Reflection; + using System.Runtime.CompilerServices; + using System.Threading.Tasks; + using Pretender; + using Pretender.Internals; + + file class PretendIMyInterface : global::TaskOfTMethodNamespace.IMyInterface + { + public static readonly MethodInfo MethodAsync_MethodInfo = typeof(PretendIMyInterface).GetMethod(nameof(MethodAsync))!; + + private readonly ICallHandler _callHandler; + + public PretendIMyInterface(ICallHandler callHandler) + { + _callHandler = callHandler; + } + + public global::System.Threading.Tasks.Task MethodAsync(string str) + { + object?[] __arguments__ = [str]; + var __callInfo__ = new CallInfo(MethodAsync_MethodInfo, __arguments__); + _callHandler.Handle(__callInfo__); + return (global::System.Threading.Tasks.Task)__callInfo__.ReturnValue; + } + } + + file static class SetupInterceptors + { + [InterceptsLocation(@"MyTest.cs", 21, 17)] + internal static IPretendSetup> Setup0(this Pretend pretend, Func> setupExpression) + { + return pretend.GetOrCreateSetup>(0, static (pretend, expr) => + { + Matcher matchCall = (callInfo, setup) => + { + var singleUseCallHandler = new SingleUseCallHandler(); + var fake = new PretendIMyInterface(singleUseCallHandler); + var listener = MatcherListener.StartListening(); + setup.Method.Invoke(setup.Target, [fake]); + listener.Dispose(); + var capturedArguments = singleUseCallHandler.Arguments; + + var str_capturedMatcher = listener.Matchers[0]; + var str_arg = (string)callInfo.Arguments[0]; + if (!str_capturedMatcher.Matches(str_arg)) + { + return false; + } + return true; + }; + return new ReturningCompiledSetup>(pretend, PretendIMyInterface.MethodAsync_MethodInfo, matchCall, expr, defaultValue: default); + }, setupExpression); + } + } + + file static class VerifyInterceptors + { + } + + file static class CreateInterceptors + { + } +} \ No newline at end of file diff --git a/test/SourceGeneratorTests/Baselines/MainTests/Test3/Pretender_g.cs b/test/SourceGeneratorTests/Baselines/MainTests/Test3/Pretender_g.cs new file mode 100644 index 0000000..edcca1d --- /dev/null +++ b/test/SourceGeneratorTests/Baselines/MainTests/Test3/Pretender_g.cs @@ -0,0 +1,169 @@ +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0612, CS0618 + +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.Reflection; + using System.Runtime.CompilerServices; + using System.Threading.Tasks; + using Pretender; + using Pretender.Internals; + + file class PretendISimpleInterface : global::ISimpleInterface + { + public static readonly MethodInfo Foo_MethodInfo = typeof(PretendISimpleInterface).GetMethod(nameof(Foo))!; + public static readonly MethodInfo VoidMethod_MethodInfo = typeof(PretendISimpleInterface).GetMethod(nameof(VoidMethod))!; + public static readonly MethodInfo AsyncMethod_MethodInfo = typeof(PretendISimpleInterface).GetMethod(nameof(AsyncMethod))!; + public static readonly MethodInfo AsyncReturningMethod_MethodInfo = typeof(PretendISimpleInterface).GetMethod(nameof(AsyncReturningMethod))!; + public static readonly MethodInfo TryParse_MethodInfo = typeof(PretendISimpleInterface).GetMethod(nameof(TryParse))!; + public static readonly MethodInfo get_Bar_MethodInfo = typeof(PretendISimpleInterface).GetProperty(nameof(Bar))!.GetMethod; + public static readonly MethodInfo set_Bar_MethodInfo = typeof(PretendISimpleInterface).GetProperty(nameof(Bar))!.SetMethod; + + private readonly ICallHandler _callHandler; + + public PretendISimpleInterface(ICallHandler callHandler) + { + _callHandler = callHandler; + } + + public string? Foo(string? bar, int baz) + { + object?[] __arguments__ = [bar, baz]; + var __callInfo__ = new CallInfo(Foo_MethodInfo, __arguments__); + _callHandler.Handle(__callInfo__); + return (string?)__callInfo__.ReturnValue; + } + + public void VoidMethod(bool baz) + { + object?[] __arguments__ = [baz]; + var __callInfo__ = new CallInfo(VoidMethod_MethodInfo, __arguments__); + _callHandler.Handle(__callInfo__); + } + + public global::System.Threading.Tasks.Task AsyncMethod() + { + object?[] __arguments__ = []; + var __callInfo__ = new CallInfo(AsyncMethod_MethodInfo, __arguments__); + _callHandler.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(AsyncReturningMethod_MethodInfo, __arguments__); + _callHandler.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(TryParse_MethodInfo, __arguments__); + _callHandler.Handle(__callInfo__); + myValue = __arguments__[1]; + return (bool)__callInfo__.ReturnValue; + } + + public string Bar + { + get + { + object?[] __arguments__ = []; + var __callInfo__ = new CallInfo(get_Bar_MethodInfo, __arguments__); + _callHandler.Handle(__callInfo__); + return (string)__callInfo__.ReturnValue; + } + set + { + object?[] __arguments__ = [value]; + var __callInfo__ = new CallInfo(set_Bar_MethodInfo, __arguments__); + _callHandler.Handle(__callInfo__); + } + } + } + + 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) => + { + Matcher matchCall = (callInfo, setup) => + { + var bar_arg = (string?)callInfo.Arguments[0]; + if (bar_arg != "1") + { + return false; + } + var baz_arg = (int)callInfo.Arguments[1]; + if (baz_arg != (int)(1)) + { + return false; + } + return true; + }; + return new ReturningCompiledSetup(pretend, PretendISimpleInterface.Foo_MethodInfo, matchCall, expr, defaultValue: default); + }, setupExpression); + } + } + + file static class VerifyInterceptors + { + [InterceptsLocation(@"MyTest.cs", 18, 24)] + internal static void Verify0(this Pretend pretend, Func setupExpression, Called called) + { + var setup = pretend.GetOrCreateSetup(0, static (pretend, expr) => + { + Matcher matchCall = (callInfo, setup) => + { + var bar_arg = (string?)callInfo.Arguments[0]; + if (bar_arg != "1") + { + return false; + } + var baz_arg = (int)callInfo.Arguments[1]; + if (baz_arg != (int)(1)) + { + return false; + } + return true; + }; + return new ReturningCompiledSetup(pretend, PretendISimpleInterface.Foo_MethodInfo, matchCall, expr, defaultValue: default); + }, setupExpression); + setup.Verify(called); + } + } + + file static class CreateInterceptors + { + [InterceptsLocation(@"MyTest.cs", 16, 38)] + internal static global::ISimpleInterface Create0(this Pretend pretend) + { + return new PretendISimpleInterface(pretend); + } + } +} \ No newline at end of file diff --git a/test/SourceGeneratorTests/MainTests.cs b/test/SourceGeneratorTests/MainTests.cs index 288ec62..79434aa 100644 --- a/test/SourceGeneratorTests/MainTests.cs +++ b/test/SourceGeneratorTests/MainTests.cs @@ -19,7 +19,7 @@ await RunAndComparePartialAsync($$""" [Fact] public async Task TaskOfTMethod() { - var (result, c) = await RunGeneratorAsync($$""" + await RunAndCompareAsync($$""" using System; using System.Threading.Tasks; using Pretender; @@ -44,51 +44,42 @@ public TestClass() } } """); - - var source = Assert.Single(result.GeneratedSources); - - var text1 = source.SourceText.ToString(); - //var text2 = result.GeneratedSources[1].SourceText.ToString(); } - [Fact] - public async Task AbstractClass() - { - var (result, c) = await RunPartialGeneratorAsync($$""" - #nullable enable - using System; - using System.Threading.Tasks; - using Pretender; + //[Fact] + //public async Task AbstractClass() + //{ + // await RunAndCompareAsync($$""" + // #nullable enable + // using System; + // using System.Threading.Tasks; + // using Pretender; - namespace AbstractClass; + // namespace AbstractClass; - public abstract class MyAbstractClass - { - abstract Task MethodAsync(string str); - abstract string Name { get; set; } - } + // public abstract class MyAbstractClass + // { + // abstract Task MethodAsync(string str); + // abstract string Name { get; set; } + // } - public class TestClass - { - public TestClass() - { - var pretend = Pretend.That(); - - pretend.Setup(c => c.MethodAsync("Hi")); - } - } - """); - - var source = Assert.Single(result.GeneratedSources); + // public class TestClass + // { + // public TestClass() + // { + // var pretend = Pretend.That(); - var sourceText = source.SourceText.ToString(); - } + // pretend.Setup(c => c.MethodAsync("Hi")); + // } + // } + // """); + //} [Fact] public async Task Test3() { - var (result, compilation) = await RunPartialGeneratorAsync($$""" + await RunAndComparePartialAsync($$""" var pretendSimpleInterface = Pretend.That(); pretendSimpleInterface @@ -99,8 +90,5 @@ public async Task Test3() pretendSimpleInterface.Verify(i => i.Foo("1", 1), 2); """); - - var source = Assert.Single(result.GeneratedSources); - var text = source.SourceText.ToString(); } } \ No newline at end of file diff --git a/test/SourceGeneratorTests/TestBase.cs b/test/SourceGeneratorTests/TestBase.cs index b033df2..8651dbf 100644 --- a/test/SourceGeneratorTests/TestBase.cs +++ b/test/SourceGeneratorTests/TestBase.cs @@ -109,7 +109,7 @@ public async Task RunAndComparePartialAsync(string source, [CallerMemberName] st }); } - public async Task RunAndCompareAsync(string source, [CallerMemberName] string? testMethodName = null) + public async Task RunAndCompareAsync(string source, [CallerMemberName] string testMethodName = null!) { var (result, _) = await RunGeneratorAsync(source); Assert.All(result.GeneratedSources, s => @@ -118,10 +118,10 @@ public async Task RunAndCompareAsync(string source, [CallerMemberName] string? t }); } - private void CompareAgainstBaseline(GeneratedSourceResult result, string? testMethodName = null) + private void CompareAgainstBaseline(GeneratedSourceResult result, string testMethodName) { var normalizedName = result.HintName[..^3].Replace('.', '_') + ".cs"; -#if GENERATE_SOURCE +#if !GENERATE_SOURCE var resultFileName = result.HintName.Replace('.', '_'); var baseLineName = $"{GetType().Name}.{testMethodName}.{normalizedName}"; var resourceName = Assert.Single(typeof(TestBase).Assembly.GetManifestResourceNames() @@ -139,12 +139,14 @@ private void CompareAgainstBaseline(GeneratedSourceResult result, string? testMe throw new Exception("Could not find directory."); } - var baselinePath = Path.Combine( - baseDirectory.FullName, - "Baselines", - GetType().Name, - testMethodName, - normalizedName); + var directory = Path.Combine(baseDirectory.FullName, "Baselines", GetType().Name, testMethodName); + + if (!Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + } + + var baselinePath = Path.Combine(directory, normalizedName); File.WriteAllText(baselinePath, result.SourceText.ToString()); #endif