From 34084c2e5653e3a29ac2e78e173baed69becc63b Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Tue, 29 Sep 2026 08:57:09 +0100 Subject: [PATCH] perf(mocks): move shared MockCall wrapper plumbing into runtime base classes --- .../Builders/MockMembersBuilder.cs | 200 ++---------- src/TUnit.Mocks/MockMethodCallBase.cs | 173 +++++++++++ ...s_Back_To_Generated_Namespace.verified.txt | 66 +--- ..._Returning_Task_And_ValueTask.verified.txt | 268 ++-------------- ...er_Static_Extension_Discovery.verified.txt | 66 +--- ...Interface_Extension_Discovery.verified.txt | 129 +------- ...face_With_Class_Type_Argument.verified.txt | 63 +--- ...le_Non_Builtin_Type_Arguments.verified.txt | 66 +--- ...ested_Namespace_Type_Argument.verified.txt | 63 +--- ...rectly_Inheriting_IEnumerable.verified.txt | 63 +--- ...entUI_Shape_Nullable_Warnings.verified.txt | 219 ++----------- ...nheriting_Multiple_Interfaces.verified.txt | 63 +--- .../Interface_With_Async_Methods.verified.txt | 268 ++-------------- ...ace_With_Dynamic_Async_Result.verified.txt | 138 +-------- ...ultable_Generic_Async_Results.verified.txt | 288 ++---------------- .../Interface_With_Events.verified.txt | 63 +--- ..._Constraints_On_Explicit_Impl.verified.txt | 68 +---- ...nterface_With_Generic_Methods.verified.txt | 201 +----------- ...ith_IConvertible_Async_Result.verified.txt | 138 +-------- ...ace_With_Keyword_Member_Names.verified.txt | 129 +------- ..._With_Keyword_Parameter_Names.verified.txt | 129 +------- .../Interface_With_Mixed_Members.verified.txt | 132 +------- ..._Native_Integer_Async_Results.verified.txt | 207 ++----------- ..._RefStruct_Out_Ref_Parameters.verified.txt | 129 +------- ...ble_Reference_Type_Parameters.verified.txt | 263 +--------------- ...terface_With_Obsolete_Members.verified.txt | 271 +--------------- ...rface_With_Out_Ref_Parameters.verified.txt | 129 +------- ...rface_With_Overloaded_Methods.verified.txt | 264 +--------------- ...e_With_Params_Array_Parameter.verified.txt | 266 +--------------- ...h_Small_Numeric_Async_Results.verified.txt | 207 ++----------- ...stract_Transitive_Return_Type.verified.txt | 66 +--- ...face_With_Tuple_Async_Results.verified.txt | 276 ++--------------- ...ters_Named_Like_Numeric_Types.verified.txt | 140 +-------- ...nconstrained_Nullable_Generic.verified.txt | 71 +---- ...ed_Constrained_Type_Parameter.verified.txt | 63 +--- ...Params_Than_Func_Action_Arity.verified.txt | 63 +--- ..._With_Secondary_Setup_Surface.verified.txt | 63 +--- .../Multi_Method_Interface.verified.txt | 132 +------- ...embers_From_External_Assembly.verified.txt | 66 +--- ...With_Internal_Signature_Types.verified.txt | 66 +--- ...c_Constrained_Virtual_Methods.verified.txt | 201 +----------- ...ct_Events_Generation_Snapshot.verified.txt | 129 +------- ...Params_Than_Func_Action_Arity.verified.txt | 69 +---- ...lsOf_GetHashCodeOf_ToStringOf.verified.txt | 132 +------- ...ple_Interface_With_One_Method.verified.txt | 66 +--- ...ion_Discovery_Without_Mock_Of.verified.txt | 63 +--- ..._Mocked_Regularly_And_Wrapped.verified.txt | 66 +--- ...c_Constrained_Virtual_Methods.verified.txt | 133 +------- 48 files changed, 528 insertions(+), 6066 deletions(-) create mode 100644 src/TUnit.Mocks/MockMethodCallBase.cs diff --git a/src/TUnit.Mocks.SourceGenerator/Builders/MockMembersBuilder.cs b/src/TUnit.Mocks.SourceGenerator/Builders/MockMembersBuilder.cs index 91d8574ab5..5795a73153 100644 --- a/src/TUnit.Mocks.SourceGenerator/Builders/MockMembersBuilder.cs +++ b/src/TUnit.Mocks.SourceGenerator/Builders/MockMembersBuilder.cs @@ -466,66 +466,30 @@ private static void GenerateReturnUnifiedClass(CodeWriter writer, string wrapper string visibility, MockTypeModel model, MockMemberModel method, bool isAsync = false, bool isValueTask = false, string? fullReturnType = null) { - var builderType = $"global::TUnit.Mocks.Setup.MethodSetupBuilder<{returnType}>"; var hasOutRef = allParameters.Any(p => p.Direction == ParameterDirection.Out || p.Direction == ParameterDirection.Ref); + // The parameter-independent plumbing (lazy setup registration, Returns/Throws/Callback/ + // TransitionsTo/Then and the ICallVerification forwards) lives in the runtime base class; + // only the per-method typed members are emitted here. writer.AppendLine("[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]"); - using (writer.Block($"{visibility} sealed class {wrapperTypeName} : global::TUnit.Mocks.Verification.ICallVerification{typeConstraints}")) + using (writer.Block($"{visibility} sealed class {wrapperTypeName} : global::TUnit.Mocks.MockMethodCallBase<{wrapperTypeName}, {returnType}>{typeConstraints}")) { - // Fields - writer.AppendLine("private readonly global::TUnit.Mocks.IMockEngineAccess _engine;"); - writer.AppendLine("private readonly int _memberId;"); - writer.AppendLine("private readonly string _memberName;"); - writer.AppendLine("private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers;"); - if (method.IsGenericMethod) - { - writer.AppendLine("private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments;"); - } - writer.AppendLine($"private {builderType}? _builder;"); - writer.AppendLine(); + EmitWrapperConstructor(writer, wrapperCtorName, method.IsGenericMethod); - // Constructor - var typeArgumentsCtorParameter = method.IsGenericMethod - ? ", global::System.Collections.Immutable.ImmutableArray typeArguments" - : ""; - writer.AppendLine($"internal {wrapperCtorName}(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers{typeArgumentsCtorParameter})"); - using (writer.Block()) + if (isAsync && fullReturnType is not null) { - writer.AppendLine("_engine = engine;"); - writer.AppendLine("_memberId = memberId;"); - writer.AppendLine("_memberName = memberName;"); - writer.AppendLine("_matchers = matchers;"); - if (method.IsGenericMethod) - { - writer.AppendLine("_typeArguments = typeArguments;"); - } + // The async Returns aliases below compete with Returns(TReturn)/Returns(Func) + // for lambdas such as `() => null`, and that tie is settled by overload resolution + // priority, which only ranks candidates declared in the same type. C# also drops + // base-class candidates whenever a derived-class overload is applicable, so these two + // are redeclared here to keep every Returns overload in one candidate set. + writer.AppendLine(); + writer.AppendLine("/// Configure a fixed return value."); + writer.AppendLine($"public new {wrapperTypeName} Returns({returnType} value) {{ EnsureSetup().Returns(value); return this; }}"); + writer.AppendLine("/// Configure a computed return value, invoked on each call."); + writer.AppendLine($"public new {wrapperTypeName} Returns(global::System.Func<{returnType}> factory) {{ EnsureSetup().Returns(factory); return this; }}"); } - writer.AppendLine(); - - // EnsureSetup — CAS-based lazy init (see EmitEnsureSetup) - EmitEnsureSetup(writer, builderType, method.IsGenericMethod); - - writer.AppendLine(); - - // Public self-returning setup methods - writer.AppendLine($"/// "); - writer.AppendLine($"public {wrapperTypeName} Returns({returnType} value) {{ EnsureSetup().Returns(value); return this; }}"); - writer.AppendLine($"/// "); - writer.AppendLine($"public {wrapperTypeName} Returns(global::System.Func<{returnType}> factory) {{ EnsureSetup().Returns(factory); return this; }}"); - writer.AppendLine($"/// "); - writer.AppendLine($"public {wrapperTypeName} ReturnsSequentially(params {returnType}[] values) {{ EnsureSetup().ReturnsSequentially(values); return this; }}"); - writer.AppendLine($"/// "); - writer.AppendLine($"public {wrapperTypeName} Throws() where TException : global::System.Exception, new() {{ EnsureSetup().Throws(); return this; }}"); - writer.AppendLine($"/// "); - writer.AppendLine($"public {wrapperTypeName} Throws(global::System.Exception exception) {{ EnsureSetup().Throws(exception); return this; }}"); - writer.AppendLine($"/// "); - writer.AppendLine($"public {wrapperTypeName} Callback(global::System.Action callback) {{ EnsureSetup().Callback(callback); return this; }}"); - writer.AppendLine($"/// "); - writer.AppendLine($"public {wrapperTypeName} TransitionsTo(string stateName) {{ EnsureSetup().TransitionsTo(stateName); return this; }}"); - writer.AppendLine($"/// "); - writer.AppendLine($"public {wrapperTypeName} Then() {{ EnsureSetup().Then(); return this; }}"); - var aliasTypeParam = GetAsyncAliasTypeParamName(model, method); if (isAsync && fullReturnType is not null) { @@ -563,25 +527,6 @@ private static void GenerateReturnUnifiedClass(CodeWriter writer, string wrapper writer.AppendLine(); GenerateTypedEventRaises(writer, events, wrapperTypeName); } - - // Verify methods (ICallVerification implementation) - writer.AppendLine(); - writer.AppendLine("// ICallVerification"); - writer.AppendLine("/// "); - var createVerification = method.IsGenericMethod - ? "new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments)" - : "_engine.CreateVerification(_memberId, _memberName, _matchers)"; - writer.AppendLine($"public void WasCalled() => {createVerification}.WasCalled();"); - writer.AppendLine("/// "); - writer.AppendLine($"public void WasCalled(global::TUnit.Mocks.Times times) => {createVerification}.WasCalled(times);"); - writer.AppendLine("/// "); - writer.AppendLine($"public void WasCalled(global::TUnit.Mocks.Times times, string? message) => {createVerification}.WasCalled(times, message);"); - writer.AppendLine("/// "); - writer.AppendLine($"public void WasCalled(string? message) => {createVerification}.WasCalled(message);"); - writer.AppendLine("/// "); - writer.AppendLine($"public void WasNeverCalled() => {createVerification}.WasNeverCalled();"); - writer.AppendLine("/// "); - writer.AppendLine($"public void WasNeverCalled(string? message) => {createVerification}.WasNeverCalled(message);"); } } @@ -592,63 +537,15 @@ private static void GenerateVoidUnifiedClass(CodeWriter writer, string wrapperCt string? spanReturnElementType = null, string? spanReturnType = null, bool isAsync = false, bool isValueTask = false) { - var builderType = "global::TUnit.Mocks.Setup.VoidMethodSetupBuilder"; var hasOutRef = allParameters.Any(p => p.Direction == ParameterDirection.Out || p.Direction == ParameterDirection.Ref); + // Shared plumbing lives in the runtime base class, whose constructor eagerly registers the + // setup because void methods are commonly used without chaining (e.g. + // mock.Log(Arg.Any()) in strict mode). writer.AppendLine($"[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]"); - using (writer.Block($"{visibility} sealed class {wrapperTypeName} : global::TUnit.Mocks.Verification.ICallVerification{typeConstraints}")) + using (writer.Block($"{visibility} sealed class {wrapperTypeName} : global::TUnit.Mocks.VoidMockMethodCallBase<{wrapperTypeName}>{typeConstraints}")) { - // Fields - writer.AppendLine("private readonly global::TUnit.Mocks.IMockEngineAccess _engine;"); - writer.AppendLine("private readonly int _memberId;"); - writer.AppendLine("private readonly string _memberName;"); - writer.AppendLine("private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers;"); - if (method.IsGenericMethod) - { - writer.AppendLine("private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments;"); - } - writer.AppendLine($"private {builderType}? _builder;"); - writer.AppendLine(); - - // Constructor — eagerly registers because void methods - // are commonly used without chaining (e.g., mock.Log(Arg.Any()) in strict mode). - var typeArgumentsCtorParameter = method.IsGenericMethod - ? ", global::System.Collections.Immutable.ImmutableArray typeArguments" - : ""; - writer.AppendLine($"internal {wrapperCtorName}(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers{typeArgumentsCtorParameter})"); - using (writer.Block()) - { - writer.AppendLine("_engine = engine;"); - writer.AppendLine("_memberId = memberId;"); - writer.AppendLine("_memberName = memberName;"); - writer.AppendLine("_matchers = matchers;"); - if (method.IsGenericMethod) - { - writer.AppendLine("_typeArguments = typeArguments;"); - } - writer.AppendLine("_ = EnsureSetup();"); - } - - writer.AppendLine(); - - // EnsureSetup — CAS-based lazy init (see EmitEnsureSetup) - EmitEnsureSetup(writer, builderType, method.IsGenericMethod); - - writer.AppendLine(); - - // Public self-returning setup methods - writer.AppendLine($"/// "); - writer.AppendLine($"public {wrapperTypeName} Returns() {{ EnsureSetup().Returns(); return this; }}"); - writer.AppendLine($"/// "); - writer.AppendLine($"public {wrapperTypeName} Throws() where TException : global::System.Exception, new() {{ EnsureSetup().Throws(); return this; }}"); - writer.AppendLine($"/// "); - writer.AppendLine($"public {wrapperTypeName} Throws(global::System.Exception exception) {{ EnsureSetup().Throws(exception); return this; }}"); - writer.AppendLine($"/// "); - writer.AppendLine($"public {wrapperTypeName} Callback(global::System.Action callback) {{ EnsureSetup().Callback(callback); return this; }}"); - writer.AppendLine($"/// "); - writer.AppendLine($"public {wrapperTypeName} TransitionsTo(string stateName) {{ EnsureSetup().TransitionsTo(stateName); return this; }}"); - writer.AppendLine($"/// "); - writer.AppendLine($"public {wrapperTypeName} Then() {{ EnsureSetup().Then(); return this; }}"); + EmitWrapperConstructor(writer, wrapperCtorName, method.IsGenericMethod); // Span return support: generate Returns(SpanType) that stores via SetsOutParameter(-1, ...) if (spanReturnElementType is not null && spanReturnType is not null) @@ -703,25 +600,6 @@ private static void GenerateVoidUnifiedClass(CodeWriter writer, string wrapperCt writer.AppendLine(); GenerateTypedEventRaises(writer, events, wrapperTypeName); } - - // Verify methods (ICallVerification implementation) - writer.AppendLine(); - writer.AppendLine("// ICallVerification"); - writer.AppendLine("/// "); - var createVerification = method.IsGenericMethod - ? "new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments)" - : "_engine.CreateVerification(_memberId, _memberName, _matchers)"; - writer.AppendLine($"public void WasCalled() => {createVerification}.WasCalled();"); - writer.AppendLine("/// "); - writer.AppendLine($"public void WasCalled(global::TUnit.Mocks.Times times) => {createVerification}.WasCalled(times);"); - writer.AppendLine("/// "); - writer.AppendLine($"public void WasCalled(global::TUnit.Mocks.Times times, string? message) => {createVerification}.WasCalled(times, message);"); - writer.AppendLine("/// "); - writer.AppendLine($"public void WasCalled(string? message) => {createVerification}.WasCalled(message);"); - writer.AppendLine("/// "); - writer.AppendLine($"public void WasNeverCalled() => {createVerification}.WasNeverCalled();"); - writer.AppendLine("/// "); - writer.AppendLine($"public void WasNeverCalled(string? message) => {createVerification}.WasNeverCalled(message);"); } } @@ -2427,36 +2305,14 @@ private static string GetTaskResultType(string taskType) return trimmed.Substring(open + 1, trimmed.Length - open - 2); } - private static void EmitEnsureSetup(CodeWriter writer, string builderType, bool hasTypeArguments) + private static void EmitWrapperConstructor(CodeWriter writer, string wrapperCtorName, bool isGenericMethod) { - // CAS-based lazy init avoids the LazyInitializer Func closure and its two scratch fields. - writer.AppendLine($"private {builderType} EnsureSetup()"); - using (writer.Block()) - { - writer.AppendLine($"var existing = global::System.Threading.Volatile.Read(ref _builder);"); - writer.AppendLine("if (existing is not null) return existing;"); - writer.AppendLine("return EnsureSetupSlow();"); - } - - writer.AppendLine(); - // Race note: if two threads lose/win the CAS, the loser returns before the winner - // calls AddSetup. Mock setup is sequential in normal usage (test setup phase completes - // before invocations), so this window is not observable. If you ever need concurrent - // setup + invocation of the same method-wrapper, reintroduce synchronization here. - writer.AppendLine("[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]"); - writer.AppendLine($"private {builderType} EnsureSetupSlow()"); - using (writer.Block()) - { - var typeArgumentsArgument = hasTypeArguments ? ", _typeArguments" : ""; - writer.AppendLine($"var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName{typeArgumentsArgument});"); - writer.AppendLine($"var fresh = new {builderType}(setup);"); - writer.AppendLine($"var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null);"); - writer.AppendLine("if (prev is not null) return prev;"); - writer.AppendLine("// AddSetup runs only on the CAS winner. Setup is sequential in practice,"); - writer.AppendLine("// so a concurrent loser observing the builder before registration is benign."); - writer.AppendLine("_engine.AddSetup(setup);"); - writer.AppendLine("return fresh;"); - } + var typeArgumentsCtorParameter = isGenericMethod + ? ", global::System.Collections.Immutable.ImmutableArray typeArguments" + : ""; + var typeArgumentsArgument = isGenericMethod ? "typeArguments" : "default"; + writer.AppendLine($"internal {wrapperCtorName}(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers{typeArgumentsCtorParameter})"); + writer.AppendLine($" : base(engine, memberId, memberName, matchers, {typeArgumentsArgument}) {{ }}"); } } diff --git a/src/TUnit.Mocks/MockMethodCallBase.cs b/src/TUnit.Mocks/MockMethodCallBase.cs new file mode 100644 index 0000000000..2e7bee0124 --- /dev/null +++ b/src/TUnit.Mocks/MockMethodCallBase.cs @@ -0,0 +1,173 @@ +using System.Collections.Immutable; +using System.ComponentModel; +using System.Runtime.CompilerServices; +using TUnit.Mocks.Arguments; +using TUnit.Mocks.Setup; +using TUnit.Mocks.Verification; + +namespace TUnit.Mocks; + +/// +/// Shared plumbing for the generated per-method *_MockCall wrappers of non-void methods: +/// lazy setup registration, the parameter-independent setup methods and call verification. +/// The generated wrapper derives from this and adds only its per-method typed overloads. +/// Setup methods return so chaining keeps the generated wrapper type. +/// Public for generated code access. Not intended for direct use. +/// +[EditorBrowsable(EditorBrowsableState.Never)] +public abstract class MockMethodCallBase : ICallVerification + where TSelf : MockMethodCallBase +{ + private readonly IMockEngineAccess _engine; + private readonly int _memberId; + private readonly string _memberName; + private readonly IArgumentMatcher[] _matchers; + private readonly ImmutableArray _typeArguments; + private MethodSetupBuilder? _builder; + + [EditorBrowsable(EditorBrowsableState.Never)] + protected MockMethodCallBase(IMockEngineAccess engine, int memberId, string memberName, IArgumentMatcher[] matchers, ImmutableArray typeArguments) + { + _engine = engine; + _memberId = memberId; + _memberName = memberName; + _matchers = matchers; + _typeArguments = typeArguments; + } + + /// Returns the setup builder, registering the setup with the engine on first use. + [EditorBrowsable(EditorBrowsableState.Never)] + protected MethodSetupBuilder EnsureSetup() + { + var existing = Volatile.Read(ref _builder); + if (existing is not null) return existing; + return EnsureSetupSlow(); + } + + // Race note: if two threads lose/win the CAS, the loser returns before the winner + // calls AddSetup. Mock setup is sequential in normal usage (test setup phase completes + // before invocations), so this window is not observable. If you ever need concurrent + // setup + invocation of the same method-wrapper, reintroduce synchronization here. + [MethodImpl(MethodImplOptions.NoInlining)] + private MethodSetupBuilder EnsureSetupSlow() + { + var setup = new MethodSetup(_memberId, _matchers, _memberName, _typeArguments); + var fresh = new MethodSetupBuilder(setup); + var prev = Interlocked.CompareExchange(ref _builder, fresh, null); + if (prev is not null) return prev; + _engine.AddSetup(setup); + return fresh; + } + + /// Configure a fixed return value. + public TSelf Returns(TReturn value) { EnsureSetup().Returns(value); return (TSelf)this; } + /// Configure a computed return value, invoked on each call. + public TSelf Returns(Func factory) { EnsureSetup().Returns(factory); return (TSelf)this; } + /// Configure sequential return values. + public TSelf ReturnsSequentially(params TReturn[] values) { EnsureSetup().ReturnsSequentially(values); return (TSelf)this; } + /// Configure the method to throw an exception of the specified type. + public TSelf Throws() where TException : Exception, new() { EnsureSetup().Throws(); return (TSelf)this; } + /// Configure the method to throw the specified exception. + public TSelf Throws(Exception exception) { EnsureSetup().Throws(exception); return (TSelf)this; } + /// Execute a callback when the method is called. + public TSelf Callback(Action callback) { EnsureSetup().Callback(callback); return (TSelf)this; } + /// Transition the mock to the specified state after this setup matches. + public TSelf TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return (TSelf)this; } + /// Chain the next sequential behavior. + public TSelf Then() { EnsureSetup().Then(); return (TSelf)this; } + + private ICallVerification CreateVerification() + => MockCallVerification.Create(_engine, _memberId, _memberName, _matchers, _typeArguments); + + /// + public void WasCalled() => CreateVerification().WasCalled(); + /// + public void WasCalled(Times times) => CreateVerification().WasCalled(times); + /// + public void WasCalled(Times times, string? message) => CreateVerification().WasCalled(times, message); + /// + public void WasCalled(string? message) => CreateVerification().WasCalled(message); + /// + public void WasNeverCalled() => CreateVerification().WasNeverCalled(); + /// + public void WasNeverCalled(string? message) => CreateVerification().WasNeverCalled(message); +} + +/// +/// Shared plumbing for the generated per-method *_MockCall wrappers of void (and +/// ref-struct-returning) methods. The setup is registered eagerly in the constructor so that +/// standalone calls like mock.Log(Arg.Any<string>()) work in strict mode without chaining. +/// Public for generated code access. Not intended for direct use. +/// +[EditorBrowsable(EditorBrowsableState.Never)] +public abstract class VoidMockMethodCallBase : ICallVerification + where TSelf : VoidMockMethodCallBase +{ + private readonly IMockEngineAccess _engine; + private readonly int _memberId; + private readonly string _memberName; + private readonly IArgumentMatcher[] _matchers; + private readonly ImmutableArray _typeArguments; + private VoidMethodSetupBuilder? _builder; + + [EditorBrowsable(EditorBrowsableState.Never)] + protected VoidMockMethodCallBase(IMockEngineAccess engine, int memberId, string memberName, IArgumentMatcher[] matchers, ImmutableArray typeArguments) + { + _engine = engine; + _memberId = memberId; + _memberName = memberName; + _matchers = matchers; + _typeArguments = typeArguments; + _ = EnsureSetup(); + } + + /// Returns the setup builder, registering the setup with the engine on first use. + [EditorBrowsable(EditorBrowsableState.Never)] + protected VoidMethodSetupBuilder EnsureSetup() + { + var existing = Volatile.Read(ref _builder); + if (existing is not null) return existing; + return EnsureSetupSlow(); + } + + // See MockMethodCallBase.EnsureSetupSlow for the race note. + [MethodImpl(MethodImplOptions.NoInlining)] + private VoidMethodSetupBuilder EnsureSetupSlow() + { + var setup = new MethodSetup(_memberId, _matchers, _memberName, _typeArguments); + var fresh = new VoidMethodSetupBuilder(setup); + var prev = Interlocked.CompareExchange(ref _builder, fresh, null); + if (prev is not null) return prev; + _engine.AddSetup(setup); + return fresh; + } + + /// Configure the method to complete normally (explicitly allows the call in strict mode). + public TSelf Returns() { EnsureSetup().Returns(); return (TSelf)this; } + /// Configure the method to throw an exception of the specified type. + public TSelf Throws() where TException : Exception, new() { EnsureSetup().Throws(); return (TSelf)this; } + /// Configure the method to throw the specified exception. + public TSelf Throws(Exception exception) { EnsureSetup().Throws(exception); return (TSelf)this; } + /// Execute a callback when the method is called. + public TSelf Callback(Action callback) { EnsureSetup().Callback(callback); return (TSelf)this; } + /// Transition the mock to the specified state after this setup matches. + public TSelf TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return (TSelf)this; } + /// Chain the next sequential behavior. + public TSelf Then() { EnsureSetup().Then(); return (TSelf)this; } + + private ICallVerification CreateVerification() + => MockCallVerification.Create(_engine, _memberId, _memberName, _matchers, _typeArguments); + + /// + public void WasCalled() => CreateVerification().WasCalled(); + /// + public void WasCalled(Times times) => CreateVerification().WasCalled(times); + /// + public void WasCalled(Times times, string? message) => CreateVerification().WasCalled(times, message); + /// + public void WasCalled(string? message) => CreateVerification().WasCalled(message); + /// + public void WasNeverCalled() => CreateVerification().WasNeverCalled(); + /// + public void WasNeverCalled(string? message) => CreateVerification().WasNeverCalled(message); +} diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Conflict_With_Existing_Type_Falls_Back_To_Generated_Namespace.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Conflict_With_Existing_Type_Falls_Back_To_Generated_Namespace.verified.txt index 9bd68ed20d..cada62e658 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Conflict_With_Existing_Type_Falls_Back_To_Generated_Namespace.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Conflict_With_Existing_Type_Falls_Back_To_Generated_Namespace.verified.txt @@ -145,58 +145,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class MyApp_IGreeter_Greet_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class MyApp_IGreeter_Greet_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal MyApp_IGreeter_Greet_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public MyApp_IGreeter_Greet_M0_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public MyApp_IGreeter_Greet_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public MyApp_IGreeter_Greet_M0_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public MyApp_IGreeter_Greet_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public MyApp_IGreeter_Greet_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public MyApp_IGreeter_Greet_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public MyApp_IGreeter_Greet_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public MyApp_IGreeter_Greet_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public MyApp_IGreeter_Greet_M0_MockCall Returns(global::System.Func factory) @@ -218,20 +170,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Delegates_Returning_Task_And_ValueTask.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Delegates_Returning_Task_And_ValueTask.verified.txt index 968a934e32..bc20caeeaf 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Delegates_Returning_Task_And_ValueTask.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Delegates_Returning_Task_And_ValueTask.verified.txt @@ -100,55 +100,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class System_Func_System_Threading_Tasks_ValueTask__Invoke_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class System_Func_System_Threading_Tasks_ValueTask__Invoke_M0_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal System_Func_System_Threading_Tasks_ValueTask__Invoke_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public System_Func_System_Threading_Tasks_ValueTask__Invoke_M0_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public System_Func_System_Threading_Tasks_ValueTask__Invoke_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public System_Func_System_Threading_Tasks_ValueTask__Invoke_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public System_Func_System_Threading_Tasks_ValueTask__Invoke_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public System_Func_System_Threading_Tasks_ValueTask__Invoke_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public System_Func_System_Threading_Tasks_ValueTask__Invoke_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Return a pre-built ValueTask directly (e.g., from a TaskCompletionSource). /// The same ValueTask instance is returned on every call. Since ValueTask may only be awaited once, @@ -158,20 +113,6 @@ namespace TUnit.Mocks.Generated public System_Func_System_Threading_Tasks_ValueTask__Invoke_M0_MockCall ReturnsAsync(global::System.Func taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } /// Return a ValueTask from a factory, invoked on each call. The ValueTask is returned as-is, so an async factory stays pending until it completes. public System_Func_System_Threading_Tasks_ValueTask__Invoke_M0_MockCall Returns(global::System.Func taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } @@ -287,55 +228,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class System_Func_int_System_Threading_Tasks_Task__Invoke_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class System_Func_int_System_Threading_Tasks_Task__Invoke_M0_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal System_Func_int_System_Threading_Tasks_Task__Invoke_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public System_Func_int_System_Threading_Tasks_Task__Invoke_M0_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public System_Func_int_System_Threading_Tasks_Task__Invoke_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public System_Func_int_System_Threading_Tasks_Task__Invoke_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public System_Func_int_System_Threading_Tasks_Task__Invoke_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public System_Func_int_System_Threading_Tasks_Task__Invoke_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public System_Func_int_System_Threading_Tasks_Task__Invoke_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public System_Func_int_System_Threading_Tasks_Task__Invoke_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -357,20 +253,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } @@ -486,58 +368,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class System_Func_int_System_Threading_Tasks_Task_int__Invoke_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class System_Func_int_System_Threading_Tasks_Task_int__Invoke_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal System_Func_int_System_Threading_Tasks_Task_int__Invoke_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public System_Func_int_System_Threading_Tasks_Task_int__Invoke_M0_MockCall Returns(int value) { EnsureSetup().Returns(value); return this; } - /// - public System_Func_int_System_Threading_Tasks_Task_int__Invoke_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public System_Func_int_System_Threading_Tasks_Task_int__Invoke_M0_MockCall ReturnsSequentially(params int[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public System_Func_int_System_Threading_Tasks_Task_int__Invoke_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public System_Func_int_System_Threading_Tasks_Task_int__Invoke_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public System_Func_int_System_Threading_Tasks_Task_int__Invoke_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public System_Func_int_System_Threading_Tasks_Task_int__Invoke_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public System_Func_int_System_Threading_Tasks_Task_int__Invoke_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } + + /// Configure a fixed return value. + public new System_Func_int_System_Threading_Tasks_Task_int__Invoke_M0_MockCall Returns(int value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new System_Func_int_System_Threading_Tasks_Task_int__Invoke_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public System_Func_int_System_Threading_Tasks_Task_int__Invoke_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -614,20 +453,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } @@ -743,58 +568,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class System_Func_string_System_Threading_Tasks_ValueTask_string__Invoke_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class System_Func_string_System_Threading_Tasks_ValueTask_string__Invoke_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal System_Func_string_System_Threading_Tasks_ValueTask_string__Invoke_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public System_Func_string_System_Threading_Tasks_ValueTask_string__Invoke_M0_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public System_Func_string_System_Threading_Tasks_ValueTask_string__Invoke_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public System_Func_string_System_Threading_Tasks_ValueTask_string__Invoke_M0_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public System_Func_string_System_Threading_Tasks_ValueTask_string__Invoke_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public System_Func_string_System_Threading_Tasks_ValueTask_string__Invoke_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public System_Func_string_System_Threading_Tasks_ValueTask_string__Invoke_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public System_Func_string_System_Threading_Tasks_ValueTask_string__Invoke_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public System_Func_string_System_Threading_Tasks_ValueTask_string__Invoke_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } + + /// Configure a fixed return value. + public new System_Func_string_System_Threading_Tasks_ValueTask_string__Invoke_M0_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new System_Func_string_System_Threading_Tasks_ValueTask_string__Invoke_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built ValueTask directly (e.g., from a TaskCompletionSource). /// The same ValueTask instance is returned on every call. Since ValueTask may only be awaited once, @@ -867,20 +649,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/External_Interface_With_Indexer_Static_Extension_Discovery.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/External_Interface_With_Indexer_Static_Extension_Discovery.verified.txt index f49e3d8de4..e82cc5ff7e 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/External_Interface_With_Indexer_Static_Extension_Discovery.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/External_Interface_With_Indexer_Static_Extension_Discovery.verified.txt @@ -165,58 +165,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class Microsoft_Extensions_Configuration_IConfiguration_GetSectionValue_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class Microsoft_Extensions_Configuration_IConfiguration_GetSectionValue_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal Microsoft_Extensions_Configuration_IConfiguration_GetSectionValue_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public Microsoft_Extensions_Configuration_IConfiguration_GetSectionValue_M0_MockCall Returns(string? value) { EnsureSetup().Returns(value); return this; } - /// - public Microsoft_Extensions_Configuration_IConfiguration_GetSectionValue_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public Microsoft_Extensions_Configuration_IConfiguration_GetSectionValue_M0_MockCall ReturnsSequentially(params string?[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public Microsoft_Extensions_Configuration_IConfiguration_GetSectionValue_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public Microsoft_Extensions_Configuration_IConfiguration_GetSectionValue_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public Microsoft_Extensions_Configuration_IConfiguration_GetSectionValue_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public Microsoft_Extensions_Configuration_IConfiguration_GetSectionValue_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public Microsoft_Extensions_Configuration_IConfiguration_GetSectionValue_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public Microsoft_Extensions_Configuration_IConfiguration_GetSectionValue_M0_MockCall Returns(global::System.Func factory) @@ -238,20 +190,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_Extension_Discovery.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_Extension_Discovery.verified.txt index 8e6c17a171..4afdb8faf3 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_Extension_Discovery.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_Extension_Discovery.verified.txt @@ -156,58 +156,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IRepository_T__GetById_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IRepository_T__GetById_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase, T> { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IRepository_T__GetById_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IRepository_T__GetById_M0_MockCall Returns(T value) { EnsureSetup().Returns(value); return this; } - /// - public IRepository_T__GetById_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IRepository_T__GetById_M0_MockCall ReturnsSequentially(params T[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IRepository_T__GetById_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IRepository_T__GetById_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IRepository_T__GetById_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IRepository_T__GetById_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IRepository_T__GetById_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public IRepository_T__GetById_M0_MockCall Returns(global::System.Func factory) @@ -229,72 +181,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IRepository_T__Save_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IRepository_T__Save_M1_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase> { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal IRepository_T__Save_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IRepository_T__Save_M1_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public IRepository_T__Save_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IRepository_T__Save_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IRepository_T__Save_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IRepository_T__Save_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IRepository_T__Save_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public IRepository_T__Save_M1_MockCall Callback(global::System.Action callback) @@ -309,20 +202,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((T)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Class_Type_Argument.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Class_Type_Argument.verified.txt index 60943450da..0784c315a0 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Class_Type_Argument.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Class_Type_Argument.verified.txt @@ -155,55 +155,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class Sandbox_IFoo_T__Process_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class Sandbox_IFoo_T__Process_M1_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase> { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal Sandbox_IFoo_T__Process_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public Sandbox_IFoo_T__Process_M1_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public Sandbox_IFoo_T__Process_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public Sandbox_IFoo_T__Process_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public Sandbox_IFoo_T__Process_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public Sandbox_IFoo_T__Process_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public Sandbox_IFoo_T__Process_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public Sandbox_IFoo_T__Process_M1_MockCall Callback(global::System.Action callback) @@ -218,20 +173,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((T)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Multiple_Non_Builtin_Type_Arguments.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Multiple_Non_Builtin_Type_Arguments.verified.txt index a32d843244..61d297155a 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Multiple_Non_Builtin_Type_Arguments.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Multiple_Non_Builtin_Type_Arguments.verified.txt @@ -139,58 +139,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class Sandbox_IMapper_TIn_TOut__Map_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class Sandbox_IMapper_TIn_TOut__Map_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase, TOut> { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal Sandbox_IMapper_TIn_TOut__Map_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public Sandbox_IMapper_TIn_TOut__Map_M0_MockCall Returns(TOut value) { EnsureSetup().Returns(value); return this; } - /// - public Sandbox_IMapper_TIn_TOut__Map_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public Sandbox_IMapper_TIn_TOut__Map_M0_MockCall ReturnsSequentially(params TOut[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public Sandbox_IMapper_TIn_TOut__Map_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public Sandbox_IMapper_TIn_TOut__Map_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public Sandbox_IMapper_TIn_TOut__Map_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public Sandbox_IMapper_TIn_TOut__Map_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public Sandbox_IMapper_TIn_TOut__Map_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public Sandbox_IMapper_TIn_TOut__Map_M0_MockCall Returns(global::System.Func factory) @@ -212,20 +164,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((TIn)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Nested_Namespace_Type_Argument.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Nested_Namespace_Type_Argument.verified.txt index c8825b4ff7..c5a1b521be 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Nested_Namespace_Type_Argument.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Nested_Namespace_Type_Argument.verified.txt @@ -155,55 +155,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class Sandbox_IService_T__Apply_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class Sandbox_IService_T__Apply_M1_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase> { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal Sandbox_IService_T__Apply_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public Sandbox_IService_T__Apply_M1_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public Sandbox_IService_T__Apply_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public Sandbox_IService_T__Apply_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public Sandbox_IService_T__Apply_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public Sandbox_IService_T__Apply_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public Sandbox_IService_T__Apply_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public Sandbox_IService_T__Apply_M1_MockCall Callback(global::System.Action callback) @@ -218,20 +173,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((T)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Directly_Inheriting_IEnumerable.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Directly_Inheriting_IEnumerable.verified.txt index e06f27e931..1631a1d87f 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Directly_Inheriting_IEnumerable.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Directly_Inheriting_IEnumerable.verified.txt @@ -172,55 +172,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ICustomCollection_Add_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ICustomCollection_Add_M1_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal ICustomCollection_Add_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ICustomCollection_Add_M1_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public ICustomCollection_Add_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ICustomCollection_Add_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ICustomCollection_Add_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ICustomCollection_Add_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ICustomCollection_Add_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public ICustomCollection_Add_M1_MockCall Callback(global::System.Action callback) @@ -235,20 +190,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_FluentUI_Shape_Nullable_Warnings.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_FluentUI_Shape_Nullable_Warnings.verified.txt index ddfbbc0d35..d04d8f804f 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_FluentUI_Shape_Nullable_Warnings.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_FluentUI_Shape_Nullable_Warnings.verified.txt @@ -145,60 +145,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IDialogReference_GetReturnValueAsync_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IDialogReference_GetReturnValueAsync_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase, T?> { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IDialogReference_GetReturnValueAsync_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IDialogReference_GetReturnValueAsync_M0_MockCall Returns(T? value) { EnsureSetup().Returns(value); return this; } - /// - public IDialogReference_GetReturnValueAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IDialogReference_GetReturnValueAsync_M0_MockCall ReturnsSequentially(params T?[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IDialogReference_GetReturnValueAsync_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IDialogReference_GetReturnValueAsync_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IDialogReference_GetReturnValueAsync_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IDialogReference_GetReturnValueAsync_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IDialogReference_GetReturnValueAsync_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } + + /// Configure a fixed return value. + public new IDialogReference_GetReturnValueAsync_M0_MockCall Returns(T? value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IDialogReference_GetReturnValueAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public IDialogReference_GetReturnValueAsync_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -227,20 +182,6 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public IDialogReference_GetReturnValueAsync_M0_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } } @@ -559,60 +500,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IDialogService_UpdateDialogAsync_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification where TData : class + public sealed class IDialogService_UpdateDialogAsync_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase, global::IDialogReference?> where TData : class { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IDialogService_UpdateDialogAsync_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IDialogService_UpdateDialogAsync_M0_MockCall Returns(global::IDialogReference? value) { EnsureSetup().Returns(value); return this; } - /// - public IDialogService_UpdateDialogAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IDialogService_UpdateDialogAsync_M0_MockCall ReturnsSequentially(params global::IDialogReference?[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IDialogService_UpdateDialogAsync_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IDialogService_UpdateDialogAsync_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IDialogService_UpdateDialogAsync_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IDialogService_UpdateDialogAsync_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IDialogService_UpdateDialogAsync_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } + + /// Configure a fixed return value. + public new IDialogService_UpdateDialogAsync_M0_MockCall Returns(global::IDialogReference? value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IDialogService_UpdateDialogAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public IDialogService_UpdateDialogAsync_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -687,77 +583,18 @@ namespace TUnit.Mocks.Generated /// Auto-raise the OnShow event when this method is called. public IDialogService_UpdateDialogAsync_M0_MockCall RaisesOnShow(global::IDialogReference arg1, global::System.Type? arg2, global::DialogParameters arg3, object arg4) { EnsureSetup().Raises("OnShow", (object?)new object?[] { arg1, arg2, arg3, arg4 }); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IDialogService_ShowDialogAsync_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification where TDialog : global::IDialogContentComponent + public sealed class IDialogService_ShowDialogAsync_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase, global::IDialogReference> where TDialog : global::IDialogContentComponent { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IDialogService_ShowDialogAsync_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IDialogService_ShowDialogAsync_M1_MockCall Returns(global::IDialogReference value) { EnsureSetup().Returns(value); return this; } - /// - public IDialogService_ShowDialogAsync_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IDialogService_ShowDialogAsync_M1_MockCall ReturnsSequentially(params global::IDialogReference[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IDialogService_ShowDialogAsync_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IDialogService_ShowDialogAsync_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IDialogService_ShowDialogAsync_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IDialogService_ShowDialogAsync_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IDialogService_ShowDialogAsync_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } + + /// Configure a fixed return value. + public new IDialogService_ShowDialogAsync_M1_MockCall Returns(global::IDialogReference value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IDialogService_ShowDialogAsync_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public IDialogService_ShowDialogAsync_M1_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -833,20 +670,6 @@ namespace TUnit.Mocks.Generated /// Auto-raise the OnShow event when this method is called. public IDialogService_ShowDialogAsync_M1_MockCall RaisesOnShow(global::IDialogReference arg1, global::System.Type? arg2, global::DialogParameters arg3, object arg4) { EnsureSetup().Raises("OnShow", (object?)new object?[] { arg1, arg2, arg3, arg4 }); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_Multiple_Interfaces.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_Multiple_Interfaces.verified.txt index ebf761d206..39293bffc5 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_Multiple_Interfaces.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_Multiple_Interfaces.verified.txt @@ -171,55 +171,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IReadWriter_Write_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IReadWriter_Write_M2_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal IReadWriter_Write_M2_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IReadWriter_Write_M2_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public IReadWriter_Write_M2_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IReadWriter_Write_M2_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IReadWriter_Write_M2_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IReadWriter_Write_M2_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IReadWriter_Write_M2_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public IReadWriter_Write_M2_MockCall Callback(global::System.Action callback) @@ -234,20 +189,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Async_Methods.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Async_Methods.verified.txt index d2d032acea..987634109e 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Async_Methods.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Async_Methods.verified.txt @@ -244,58 +244,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IAsyncService_GetValueAsync_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IAsyncService_GetValueAsync_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IAsyncService_GetValueAsync_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IAsyncService_GetValueAsync_M0_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public IAsyncService_GetValueAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IAsyncService_GetValueAsync_M0_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IAsyncService_GetValueAsync_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IAsyncService_GetValueAsync_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IAsyncService_GetValueAsync_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IAsyncService_GetValueAsync_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IAsyncService_GetValueAsync_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } + + /// Configure a fixed return value. + public new IAsyncService_GetValueAsync_M0_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IAsyncService_GetValueAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public IAsyncService_GetValueAsync_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -366,72 +323,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IAsyncService_DoWorkAsync_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IAsyncService_DoWorkAsync_M1_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal IAsyncService_DoWorkAsync_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IAsyncService_DoWorkAsync_M1_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public IAsyncService_DoWorkAsync_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IAsyncService_DoWorkAsync_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IAsyncService_DoWorkAsync_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IAsyncService_DoWorkAsync_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IAsyncService_DoWorkAsync_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public IAsyncService_DoWorkAsync_M1_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -439,75 +337,18 @@ namespace TUnit.Mocks.Generated public IAsyncService_DoWorkAsync_M1_MockCall ReturnsAsync(global::System.Func taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } /// Return a Task from a factory, invoked on each call. The Task is returned as-is, so an async factory stays pending until it completes. public IAsyncService_DoWorkAsync_M1_MockCall Returns(global::System.Func taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IAsyncService_ComputeAsync_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IAsyncService_ComputeAsync_M2_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IAsyncService_ComputeAsync_M2_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IAsyncService_ComputeAsync_M2_MockCall Returns(int value) { EnsureSetup().Returns(value); return this; } - /// - public IAsyncService_ComputeAsync_M2_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IAsyncService_ComputeAsync_M2_MockCall ReturnsSequentially(params int[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IAsyncService_ComputeAsync_M2_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IAsyncService_ComputeAsync_M2_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IAsyncService_ComputeAsync_M2_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IAsyncService_ComputeAsync_M2_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IAsyncService_ComputeAsync_M2_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } + + /// Configure a fixed return value. + public new IAsyncService_ComputeAsync_M2_MockCall Returns(int value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IAsyncService_ComputeAsync_M2_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built ValueTask directly (e.g., from a TaskCompletionSource). /// The same ValueTask instance is returned on every call. Since ValueTask may only be awaited once, @@ -586,72 +427,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IAsyncService_InitializeAsync_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IAsyncService_InitializeAsync_M3_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal IAsyncService_InitializeAsync_M3_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IAsyncService_InitializeAsync_M3_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public IAsyncService_InitializeAsync_M3_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IAsyncService_InitializeAsync_M3_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IAsyncService_InitializeAsync_M3_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IAsyncService_InitializeAsync_M3_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IAsyncService_InitializeAsync_M3_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Return a pre-built ValueTask directly (e.g., from a TaskCompletionSource). /// The same ValueTask instance is returned on every call. Since ValueTask may only be awaited once, @@ -675,20 +457,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((global::System.Threading.CancellationToken)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Dynamic_Async_Result.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Dynamic_Async_Result.verified.txt index 8e52895ebd..7e6252b87f 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Dynamic_Async_Result.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Dynamic_Async_Result.verified.txt @@ -171,58 +171,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IDynamicService_GetAsync_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IDynamicService_GetAsync_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IDynamicService_GetAsync_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } + : base(engine, memberId, memberName, matchers, default) { } - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IDynamicService_GetAsync_M0_MockCall Returns(dynamic value) { EnsureSetup().Returns(value); return this; } - /// - public IDynamicService_GetAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IDynamicService_GetAsync_M0_MockCall ReturnsSequentially(params dynamic[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IDynamicService_GetAsync_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IDynamicService_GetAsync_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IDynamicService_GetAsync_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IDynamicService_GetAsync_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IDynamicService_GetAsync_M0_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new IDynamicService_GetAsync_M0_MockCall Returns(dynamic value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IDynamicService_GetAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public IDynamicService_GetAsync_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -249,75 +206,18 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public IDynamicService_GetAsync_M0_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IDynamicService_ComputeAsync_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IDynamicService_ComputeAsync_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IDynamicService_ComputeAsync_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } + : base(engine, memberId, memberName, matchers, default) { } - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IDynamicService_ComputeAsync_M1_MockCall Returns(dynamic value) { EnsureSetup().Returns(value); return this; } - /// - public IDynamicService_ComputeAsync_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IDynamicService_ComputeAsync_M1_MockCall ReturnsSequentially(params dynamic[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IDynamicService_ComputeAsync_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IDynamicService_ComputeAsync_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IDynamicService_ComputeAsync_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IDynamicService_ComputeAsync_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IDynamicService_ComputeAsync_M1_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new IDynamicService_ComputeAsync_M1_MockCall Returns(dynamic value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IDynamicService_ComputeAsync_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built ValueTask directly (e.g., from a TaskCompletionSource). /// The same ValueTask instance is returned on every call. Since ValueTask may only be awaited once, @@ -346,20 +246,6 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public IDynamicService_ComputeAsync_M1_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Enum_And_Defaultable_Generic_Async_Results.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Enum_And_Defaultable_Generic_Async_Results.verified.txt index a08f83441c..c7895aca92 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Enum_And_Defaultable_Generic_Async_Results.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Enum_And_Defaultable_Generic_Async_Results.verified.txt @@ -237,58 +237,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IEnumAndGenericService_GetColorAsync_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IEnumAndGenericService_GetColorAsync_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IEnumAndGenericService_GetColorAsync_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IEnumAndGenericService_GetColorAsync_M0_MockCall Returns(global::Color value) { EnsureSetup().Returns(value); return this; } - /// - public IEnumAndGenericService_GetColorAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IEnumAndGenericService_GetColorAsync_M0_MockCall ReturnsSequentially(params global::Color[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IEnumAndGenericService_GetColorAsync_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IEnumAndGenericService_GetColorAsync_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IEnumAndGenericService_GetColorAsync_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IEnumAndGenericService_GetColorAsync_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IEnumAndGenericService_GetColorAsync_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } + + /// Configure a fixed return value. + public new IEnumAndGenericService_GetColorAsync_M0_MockCall Returns(global::Color value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IEnumAndGenericService_GetColorAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public IEnumAndGenericService_GetColorAsync_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -317,75 +274,18 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public IEnumAndGenericService_GetColorAsync_M0_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IEnumAndGenericService_GetColorValueAsync_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IEnumAndGenericService_GetColorValueAsync_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IEnumAndGenericService_GetColorValueAsync_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IEnumAndGenericService_GetColorValueAsync_M1_MockCall Returns(global::Color value) { EnsureSetup().Returns(value); return this; } - /// - public IEnumAndGenericService_GetColorValueAsync_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IEnumAndGenericService_GetColorValueAsync_M1_MockCall ReturnsSequentially(params global::Color[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IEnumAndGenericService_GetColorValueAsync_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IEnumAndGenericService_GetColorValueAsync_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IEnumAndGenericService_GetColorValueAsync_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IEnumAndGenericService_GetColorValueAsync_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IEnumAndGenericService_GetColorValueAsync_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } + + /// Configure a fixed return value. + public new IEnumAndGenericService_GetColorValueAsync_M1_MockCall Returns(global::Color value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IEnumAndGenericService_GetColorValueAsync_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built ValueTask directly (e.g., from a TaskCompletionSource). /// The same ValueTask instance is returned on every call. Since ValueTask may only be awaited once, @@ -416,77 +316,18 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public IEnumAndGenericService_GetColorValueAsync_M1_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IEnumAndGenericService_FindAsync_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IEnumAndGenericService_FindAsync_M2_MockCall : global::TUnit.Mocks.MockMethodCallBase, T?> { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IEnumAndGenericService_FindAsync_M2_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IEnumAndGenericService_FindAsync_M2_MockCall Returns(T? value) { EnsureSetup().Returns(value); return this; } - /// - public IEnumAndGenericService_FindAsync_M2_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IEnumAndGenericService_FindAsync_M2_MockCall ReturnsSequentially(params T?[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IEnumAndGenericService_FindAsync_M2_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IEnumAndGenericService_FindAsync_M2_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IEnumAndGenericService_FindAsync_M2_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IEnumAndGenericService_FindAsync_M2_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IEnumAndGenericService_FindAsync_M2_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } + + /// Configure a fixed return value. + public new IEnumAndGenericService_FindAsync_M2_MockCall Returns(T? value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IEnumAndGenericService_FindAsync_M2_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public IEnumAndGenericService_FindAsync_M2_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -559,77 +400,18 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IEnumAndGenericService_FindStructAsync_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification where T : struct + public sealed class IEnumAndGenericService_FindStructAsync_M3_MockCall : global::TUnit.Mocks.MockMethodCallBase, T?> where T : struct { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IEnumAndGenericService_FindStructAsync_M3_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IEnumAndGenericService_FindStructAsync_M3_MockCall Returns(T? value) { EnsureSetup().Returns(value); return this; } - /// - public IEnumAndGenericService_FindStructAsync_M3_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IEnumAndGenericService_FindStructAsync_M3_MockCall ReturnsSequentially(params T?[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IEnumAndGenericService_FindStructAsync_M3_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IEnumAndGenericService_FindStructAsync_M3_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IEnumAndGenericService_FindStructAsync_M3_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IEnumAndGenericService_FindStructAsync_M3_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IEnumAndGenericService_FindStructAsync_M3_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } + + /// Configure a fixed return value. + public new IEnumAndGenericService_FindStructAsync_M3_MockCall Returns(T? value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IEnumAndGenericService_FindStructAsync_M3_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public IEnumAndGenericService_FindStructAsync_M3_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -701,20 +483,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Events.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Events.verified.txt index 1049c2fcfa..785d482fa1 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Events.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Events.verified.txt @@ -204,55 +204,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class INotifier_Notify_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class INotifier_Notify_M0_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal INotifier_Notify_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public INotifier_Notify_M0_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public INotifier_Notify_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public INotifier_Notify_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public INotifier_Notify_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public INotifier_Notify_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public INotifier_Notify_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public INotifier_Notify_M0_MockCall Callback(global::System.Action callback) @@ -270,20 +225,6 @@ namespace TUnit.Mocks.Generated /// Auto-raise the ItemAdded event when this method is called. public INotifier_Notify_M0_MockCall RaisesItemAdded(string e) { EnsureSetup().Raises("ItemAdded", (object?)e); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Method_Constraints_On_Explicit_Impl.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Method_Constraints_On_Explicit_Impl.verified.txt index 8beff540a3..68c98add66 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Method_Constraints_On_Explicit_Impl.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Method_Constraints_On_Explicit_Impl.verified.txt @@ -204,60 +204,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IConstrained_GetNotnull_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification where T : notnull + public sealed class IConstrained_GetNotnull_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase, T> where T : notnull { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IConstrained_GetNotnull_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IConstrained_GetNotnull_M0_MockCall Returns(T value) { EnsureSetup().Returns(value); return this; } - /// - public IConstrained_GetNotnull_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IConstrained_GetNotnull_M0_MockCall ReturnsSequentially(params T[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IConstrained_GetNotnull_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IConstrained_GetNotnull_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IConstrained_GetNotnull_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IConstrained_GetNotnull_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IConstrained_GetNotnull_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } /// Configure a typed computed return value using the actual method parameters. public IConstrained_GetNotnull_M0_MockCall Returns(global::System.Func factory) @@ -279,20 +229,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Methods.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Methods.verified.txt index 16cd00c99e..7790ffdc0a 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Methods.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Methods.verified.txt @@ -182,60 +182,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IRepository_GetById_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification where T : class + public sealed class IRepository_GetById_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase, T> where T : class { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IRepository_GetById_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IRepository_GetById_M0_MockCall Returns(T value) { EnsureSetup().Returns(value); return this; } - /// - public IRepository_GetById_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IRepository_GetById_M0_MockCall ReturnsSequentially(params T[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IRepository_GetById_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IRepository_GetById_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IRepository_GetById_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IRepository_GetById_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IRepository_GetById_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } /// Configure a typed computed return value using the actual method parameters. public IRepository_GetById_M0_MockCall Returns(global::System.Func factory) @@ -257,74 +207,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IRepository_Save_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification where T : class, new() + public sealed class IRepository_Save_M1_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase> where T : class, new() { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal IRepository_Save_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IRepository_Save_M1_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public IRepository_Save_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IRepository_Save_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IRepository_Save_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IRepository_Save_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IRepository_Save_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } /// Execute a typed callback using the actual method parameters. public IRepository_Save_M1_MockCall Callback(global::System.Action callback) @@ -339,77 +228,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((T)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IRepository_Transform_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification where TInput : notnull where TResult : struct + public sealed class IRepository_Transform_M2_MockCall : global::TUnit.Mocks.MockMethodCallBase, TResult> where TInput : notnull where TResult : struct { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IRepository_Transform_M2_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IRepository_Transform_M2_MockCall Returns(TResult value) { EnsureSetup().Returns(value); return this; } - /// - public IRepository_Transform_M2_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IRepository_Transform_M2_MockCall ReturnsSequentially(params TResult[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IRepository_Transform_M2_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IRepository_Transform_M2_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IRepository_Transform_M2_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IRepository_Transform_M2_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IRepository_Transform_M2_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } /// Configure a typed computed return value using the actual method parameters. public IRepository_Transform_M2_MockCall Returns(global::System.Func factory) @@ -431,20 +256,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((TInput)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_IConvertible_Async_Result.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_IConvertible_Async_Result.verified.txt index 54a7041680..bfc5eb8fa3 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_IConvertible_Async_Result.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_IConvertible_Async_Result.verified.txt @@ -171,58 +171,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IConvertibleService_GetAsync_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IConvertibleService_GetAsync_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IConvertibleService_GetAsync_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } + : base(engine, memberId, memberName, matchers, default) { } - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IConvertibleService_GetAsync_M0_MockCall Returns(global::System.IConvertible value) { EnsureSetup().Returns(value); return this; } - /// - public IConvertibleService_GetAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IConvertibleService_GetAsync_M0_MockCall ReturnsSequentially(params global::System.IConvertible[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IConvertibleService_GetAsync_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IConvertibleService_GetAsync_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IConvertibleService_GetAsync_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IConvertibleService_GetAsync_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IConvertibleService_GetAsync_M0_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new IConvertibleService_GetAsync_M0_MockCall Returns(global::System.IConvertible value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IConvertibleService_GetAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public IConvertibleService_GetAsync_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -250,75 +207,18 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public IConvertibleService_GetAsync_M0_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IConvertibleService_ComputeAsync_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IConvertibleService_ComputeAsync_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IConvertibleService_ComputeAsync_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } + : base(engine, memberId, memberName, matchers, default) { } - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IConvertibleService_ComputeAsync_M1_MockCall Returns(global::System.IConvertible value) { EnsureSetup().Returns(value); return this; } - /// - public IConvertibleService_ComputeAsync_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IConvertibleService_ComputeAsync_M1_MockCall ReturnsSequentially(params global::System.IConvertible[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IConvertibleService_ComputeAsync_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IConvertibleService_ComputeAsync_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IConvertibleService_ComputeAsync_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IConvertibleService_ComputeAsync_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IConvertibleService_ComputeAsync_M1_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new IConvertibleService_ComputeAsync_M1_MockCall Returns(global::System.IConvertible value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IConvertibleService_ComputeAsync_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built ValueTask directly (e.g., from a TaskCompletionSource). /// The same ValueTask instance is returned on every call. Since ValueTask may only be awaited once, @@ -348,20 +248,6 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public IConvertibleService_ComputeAsync_M1_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Member_Names.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Member_Names.verified.txt index 856ba03510..afa58a2a97 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Member_Names.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Member_Names.verified.txt @@ -210,55 +210,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IEscapedNames_event_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IEscapedNames_event_M2_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal IEscapedNames_event_M2_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IEscapedNames_event_M2_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public IEscapedNames_event_M2_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IEscapedNames_event_M2_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IEscapedNames_event_M2_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IEscapedNames_event_M2_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IEscapedNames_event_M2_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public IEscapedNames_event_M2_MockCall Callback(global::System.Action callback) @@ -273,75 +228,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IEscapedNames_namespace_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IEscapedNames_namespace_M3_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IEscapedNames_namespace_M3_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IEscapedNames_namespace_M3_MockCall Returns(int value) { EnsureSetup().Returns(value); return this; } - /// - public IEscapedNames_namespace_M3_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IEscapedNames_namespace_M3_MockCall ReturnsSequentially(params int[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IEscapedNames_namespace_M3_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IEscapedNames_namespace_M3_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IEscapedNames_namespace_M3_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IEscapedNames_namespace_M3_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IEscapedNames_namespace_M3_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public IEscapedNames_namespace_M3_MockCall Returns(global::System.Func factory) @@ -363,20 +256,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!, (int)args[1]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Parameter_Names.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Parameter_Names.verified.txt index 1dc7b55441..9bcbbbf342 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Parameter_Names.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Parameter_Names.verified.txt @@ -184,55 +184,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ITest_Test_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ITest_Test_M0_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal ITest_Test_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ITest_Test_M0_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public ITest_Test_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ITest_Test_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ITest_Test_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ITest_Test_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ITest_Test_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public ITest_Test_M0_MockCall Callback(global::System.Action callback) @@ -247,75 +202,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ITest_Get_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ITest_Get_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal ITest_Get_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ITest_Get_M1_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public ITest_Get_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public ITest_Get_M1_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public ITest_Get_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ITest_Get_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ITest_Get_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ITest_Get_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ITest_Get_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public ITest_Get_M1_MockCall Returns(global::System.Func factory) @@ -337,20 +230,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!, (string)args[1]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Mixed_Members.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Mixed_Members.verified.txt index 80cbe2c140..255d153e98 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Mixed_Members.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Mixed_Members.verified.txt @@ -261,58 +261,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IService_GetAsync_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IService_GetAsync_M3_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IService_GetAsync_M3_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } + : base(engine, memberId, memberName, matchers, default) { } - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IService_GetAsync_M3_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public IService_GetAsync_M3_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IService_GetAsync_M3_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IService_GetAsync_M3_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IService_GetAsync_M3_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IService_GetAsync_M3_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IService_GetAsync_M3_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IService_GetAsync_M3_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new IService_GetAsync_M3_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IService_GetAsync_M3_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public IService_GetAsync_M3_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -386,72 +343,13 @@ namespace TUnit.Mocks.Generated /// Auto-raise the StatusChanged event when this method is called. public IService_GetAsync_M3_MockCall RaisesStatusChanged(string e) { EnsureSetup().Raises("StatusChanged", (object?)e); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IService_Process_M4_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IService_Process_M4_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal IService_Process_M4_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IService_Process_M4_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public IService_Process_M4_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IService_Process_M4_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IService_Process_M4_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IService_Process_M4_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IService_Process_M4_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public IService_Process_M4_MockCall Callback(global::System.Action callback) @@ -469,20 +367,6 @@ namespace TUnit.Mocks.Generated /// Auto-raise the StatusChanged event when this method is called. public IService_Process_M4_MockCall RaisesStatusChanged(string e) { EnsureSetup().Raises("StatusChanged", (object?)e); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Native_Integer_Async_Results.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Native_Integer_Async_Results.verified.txt index 378158392c..b02bdd457e 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Native_Integer_Async_Results.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Native_Integer_Async_Results.verified.txt @@ -197,58 +197,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class INativeIntService_GetLongAsync_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class INativeIntService_GetLongAsync_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal INativeIntService_GetLongAsync_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } + : base(engine, memberId, memberName, matchers, default) { } - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public INativeIntService_GetLongAsync_M0_MockCall Returns(long value) { EnsureSetup().Returns(value); return this; } - /// - public INativeIntService_GetLongAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public INativeIntService_GetLongAsync_M0_MockCall ReturnsSequentially(params long[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public INativeIntService_GetLongAsync_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public INativeIntService_GetLongAsync_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public INativeIntService_GetLongAsync_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public INativeIntService_GetLongAsync_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public INativeIntService_GetLongAsync_M0_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new INativeIntService_GetLongAsync_M0_MockCall Returns(long value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new INativeIntService_GetLongAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public INativeIntService_GetLongAsync_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -284,75 +241,18 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public INativeIntService_GetLongAsync_M0_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class INativeIntService_GetNativeAsync_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class INativeIntService_GetNativeAsync_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal INativeIntService_GetNativeAsync_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } + : base(engine, memberId, memberName, matchers, default) { } - /// - public INativeIntService_GetNativeAsync_M1_MockCall Returns(nint value) { EnsureSetup().Returns(value); return this; } - /// - public INativeIntService_GetNativeAsync_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public INativeIntService_GetNativeAsync_M1_MockCall ReturnsSequentially(params nint[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public INativeIntService_GetNativeAsync_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public INativeIntService_GetNativeAsync_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public INativeIntService_GetNativeAsync_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public INativeIntService_GetNativeAsync_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public INativeIntService_GetNativeAsync_M1_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new INativeIntService_GetNativeAsync_M1_MockCall Returns(nint value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new INativeIntService_GetNativeAsync_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public INativeIntService_GetNativeAsync_M1_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -386,75 +286,18 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public INativeIntService_GetNativeAsync_M1_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class INativeIntService_GetUnsignedNativeAsync_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class INativeIntService_GetUnsignedNativeAsync_M2_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal INativeIntService_GetUnsignedNativeAsync_M2_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } + : base(engine, memberId, memberName, matchers, default) { } - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public INativeIntService_GetUnsignedNativeAsync_M2_MockCall Returns(nuint value) { EnsureSetup().Returns(value); return this; } - /// - public INativeIntService_GetUnsignedNativeAsync_M2_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public INativeIntService_GetUnsignedNativeAsync_M2_MockCall ReturnsSequentially(params nuint[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public INativeIntService_GetUnsignedNativeAsync_M2_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public INativeIntService_GetUnsignedNativeAsync_M2_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public INativeIntService_GetUnsignedNativeAsync_M2_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public INativeIntService_GetUnsignedNativeAsync_M2_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public INativeIntService_GetUnsignedNativeAsync_M2_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new INativeIntService_GetUnsignedNativeAsync_M2_MockCall Returns(nuint value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new INativeIntService_GetUnsignedNativeAsync_M2_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built ValueTask directly (e.g., from a TaskCompletionSource). /// The same ValueTask instance is returned on every call. Since ValueTask may only be awaited once, @@ -489,20 +332,6 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public INativeIntService_GetUnsignedNativeAsync_M2_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_NonSpan_RefStruct_Out_Ref_Parameters.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_NonSpan_RefStruct_Out_Ref_Parameters.verified.txt index 74ee05f7ce..3abab522f5 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_NonSpan_RefStruct_Out_Ref_Parameters.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_NonSpan_RefStruct_Out_Ref_Parameters.verified.txt @@ -186,58 +186,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class Sandbox_ICursorParser_TryRead_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class Sandbox_ICursorParser_TryRead_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal Sandbox_ICursorParser_TryRead_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public Sandbox_ICursorParser_TryRead_M0_MockCall Returns(bool value) { EnsureSetup().Returns(value); return this; } - /// - public Sandbox_ICursorParser_TryRead_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public Sandbox_ICursorParser_TryRead_M0_MockCall ReturnsSequentially(params bool[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public Sandbox_ICursorParser_TryRead_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public Sandbox_ICursorParser_TryRead_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public Sandbox_ICursorParser_TryRead_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public Sandbox_ICursorParser_TryRead_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public Sandbox_ICursorParser_TryRead_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public Sandbox_ICursorParser_TryRead_M0_MockCall Returns(global::System.Func factory) @@ -262,89 +214,16 @@ namespace TUnit.Mocks.Generated /// Sets the 'cursor' out parameter to the specified value when this setup matches. public Sandbox_ICursorParser_TryRead_M0_MockCall SetsOutCursor(global::Sandbox.ICursorParser_TryRead_M0_Cursor_OutSetter setter) { EnsureSetup().SetsOutParameter(1, setter); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class Sandbox_ICursorParser_Advance_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class Sandbox_ICursorParser_Advance_M1_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal Sandbox_ICursorParser_Advance_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public Sandbox_ICursorParser_Advance_M1_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public Sandbox_ICursorParser_Advance_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public Sandbox_ICursorParser_Advance_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public Sandbox_ICursorParser_Advance_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public Sandbox_ICursorParser_Advance_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public Sandbox_ICursorParser_Advance_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Sets the 'cursor' ref parameter to the specified value when this setup matches. public Sandbox_ICursorParser_Advance_M1_MockCall SetsRefCursor(global::Sandbox.ICursorParser_Advance_M1_Cursor_RefSetter setter) { EnsureSetup().SetsOutParameter(0, setter); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Reference_Type_Parameters.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Reference_Type_Parameters.verified.txt index 5f142df970..c00663ec1f 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Reference_Type_Parameters.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Reference_Type_Parameters.verified.txt @@ -308,55 +308,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFoo_Bar_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IFoo_Bar_M0_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal IFoo_Bar_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IFoo_Bar_M0_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public IFoo_Bar_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IFoo_Bar_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IFoo_Bar_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IFoo_Bar_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IFoo_Bar_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public IFoo_Bar_M0_MockCall Callback(global::System.Action callback) @@ -371,75 +326,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((object?)args[0])); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFoo_GetValue_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IFoo_GetValue_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IFoo_GetValue_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IFoo_GetValue_M1_MockCall Returns(string? value) { EnsureSetup().Returns(value); return this; } - /// - public IFoo_GetValue_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IFoo_GetValue_M1_MockCall ReturnsSequentially(params string?[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IFoo_GetValue_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IFoo_GetValue_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IFoo_GetValue_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IFoo_GetValue_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IFoo_GetValue_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public IFoo_GetValue_M1_MockCall Returns(global::System.Func factory) @@ -461,72 +354,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string?)args[0], (int)args[1]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFoo_Process_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IFoo_Process_M2_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal IFoo_Process_M2_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IFoo_Process_M2_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public IFoo_Process_M2_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IFoo_Process_M2_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IFoo_Process_M2_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IFoo_Process_M2_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IFoo_Process_M2_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public IFoo_Process_M2_MockCall Callback(global::System.Action callback) @@ -541,75 +375,18 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!, (string?)args[1], (object?)args[2])); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFoo_GetAsync_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IFoo_GetAsync_M3_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IFoo_GetAsync_M3_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IFoo_GetAsync_M3_MockCall Returns(string? value) { EnsureSetup().Returns(value); return this; } - /// - public IFoo_GetAsync_M3_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IFoo_GetAsync_M3_MockCall ReturnsSequentially(params string?[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IFoo_GetAsync_M3_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IFoo_GetAsync_M3_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IFoo_GetAsync_M3_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IFoo_GetAsync_M3_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IFoo_GetAsync_M3_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } + + /// Configure a fixed return value. + public new IFoo_GetAsync_M3_MockCall Returns(string? value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IFoo_GetAsync_M3_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public IFoo_GetAsync_M3_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -680,20 +457,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string?)args[0])); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Obsolete_Members.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Obsolete_Members.verified.txt index 9bd7ab45b2..94a58b4d82 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Obsolete_Members.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Obsolete_Members.verified.txt @@ -133,58 +133,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class BaseDialog_Compute_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class BaseDialog_Compute_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal BaseDialog_Compute_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public BaseDialog_Compute_M0_MockCall Returns(string? value) { EnsureSetup().Returns(value); return this; } - /// - public BaseDialog_Compute_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public BaseDialog_Compute_M0_MockCall ReturnsSequentially(params string?[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public BaseDialog_Compute_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public BaseDialog_Compute_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public BaseDialog_Compute_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public BaseDialog_Compute_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public BaseDialog_Compute_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public BaseDialog_Compute_M0_MockCall Returns(global::System.Func factory) @@ -206,20 +158,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string?)args[0])); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } @@ -571,58 +509,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IDialogService_Show_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IDialogService_Show_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IDialogService_Show_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IDialogService_Show_M0_MockCall Returns(string? value) { EnsureSetup().Returns(value); return this; } - /// - public IDialogService_Show_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IDialogService_Show_M0_MockCall ReturnsSequentially(params string?[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IDialogService_Show_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IDialogService_Show_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IDialogService_Show_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IDialogService_Show_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IDialogService_Show_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public IDialogService_Show_M0_MockCall Returns(global::System.Func factory) @@ -647,77 +537,18 @@ namespace TUnit.Mocks.Generated /// Auto-raise the Opened event when this method is called. public IDialogService_Show_M0_MockCall RaisesOpened(string? e) { EnsureSetup().Raises("Opened", (object?)e); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IDialogService_ShowPanel_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification where TData : class + public sealed class IDialogService_ShowPanel_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase, string?> where TData : class { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IDialogService_ShowPanel_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IDialogService_ShowPanel_M1_MockCall Returns(string? value) { EnsureSetup().Returns(value); return this; } - /// - public IDialogService_ShowPanel_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IDialogService_ShowPanel_M1_MockCall ReturnsSequentially(params string?[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IDialogService_ShowPanel_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IDialogService_ShowPanel_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IDialogService_ShowPanel_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IDialogService_ShowPanel_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IDialogService_ShowPanel_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } + + /// Configure a fixed return value. + public new IDialogService_ShowPanel_M1_MockCall Returns(string? value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IDialogService_ShowPanel_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public IDialogService_ShowPanel_M1_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -791,92 +622,16 @@ namespace TUnit.Mocks.Generated /// Auto-raise the Opened event when this method is called. public IDialogService_ShowPanel_M1_MockCall RaisesOpened(string? e) { EnsureSetup().Raises("Opened", (object?)e); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IDialogService_WithTrickyChars_M8_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IDialogService_WithTrickyChars_M8_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IDialogService_WithTrickyChars_M8_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IDialogService_WithTrickyChars_M8_MockCall Returns(string? value) { EnsureSetup().Returns(value); return this; } - /// - public IDialogService_WithTrickyChars_M8_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IDialogService_WithTrickyChars_M8_MockCall ReturnsSequentially(params string?[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IDialogService_WithTrickyChars_M8_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IDialogService_WithTrickyChars_M8_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IDialogService_WithTrickyChars_M8_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IDialogService_WithTrickyChars_M8_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IDialogService_WithTrickyChars_M8_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Auto-raise the Opened event when this method is called. public IDialogService_WithTrickyChars_M8_MockCall RaisesOpened(string? e) { EnsureSetup().Raises("Opened", (object?)e); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Out_Ref_Parameters.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Out_Ref_Parameters.verified.txt index 2de9a00d2b..7e53614771 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Out_Ref_Parameters.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Out_Ref_Parameters.verified.txt @@ -168,58 +168,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IDictionary_TryGetValue_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IDictionary_TryGetValue_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IDictionary_TryGetValue_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IDictionary_TryGetValue_M0_MockCall Returns(bool value) { EnsureSetup().Returns(value); return this; } - /// - public IDictionary_TryGetValue_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IDictionary_TryGetValue_M0_MockCall ReturnsSequentially(params bool[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IDictionary_TryGetValue_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IDictionary_TryGetValue_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IDictionary_TryGetValue_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IDictionary_TryGetValue_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IDictionary_TryGetValue_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public IDictionary_TryGetValue_M0_MockCall Returns(global::System.Func factory) @@ -244,72 +196,13 @@ namespace TUnit.Mocks.Generated /// Sets the 'value' out parameter to the specified value when this setup matches. public IDictionary_TryGetValue_M0_MockCall SetsOutValue(string value) { EnsureSetup().SetsOutParameter(1, value); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IDictionary_Swap_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IDictionary_Swap_M1_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal IDictionary_Swap_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IDictionary_Swap_M1_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public IDictionary_Swap_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IDictionary_Swap_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IDictionary_Swap_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IDictionary_Swap_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IDictionary_Swap_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public IDictionary_Swap_M1_MockCall Callback(global::System.Action callback) @@ -329,20 +222,6 @@ namespace TUnit.Mocks.Generated public IDictionary_Swap_M1_MockCall SetsRefA(int a) { EnsureSetup().SetsOutParameter(0, a); return this; } /// Sets the 'b' ref parameter to the specified value when this setup matches. public IDictionary_Swap_M1_MockCall SetsRefB(int b) { EnsureSetup().SetsOutParameter(1, b); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Overloaded_Methods.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Overloaded_Methods.verified.txt index 18719db6f4..bac55b0d8e 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Overloaded_Methods.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Overloaded_Methods.verified.txt @@ -261,58 +261,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFormatter_Format_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IFormatter_Format_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IFormatter_Format_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IFormatter_Format_M0_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public IFormatter_Format_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IFormatter_Format_M0_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IFormatter_Format_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IFormatter_Format_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IFormatter_Format_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IFormatter_Format_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IFormatter_Format_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public IFormatter_Format_M0_MockCall Returns(global::System.Func factory) @@ -334,75 +286,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFormatter_Format_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IFormatter_Format_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IFormatter_Format_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IFormatter_Format_M1_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public IFormatter_Format_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IFormatter_Format_M1_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IFormatter_Format_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IFormatter_Format_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IFormatter_Format_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IFormatter_Format_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IFormatter_Format_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public IFormatter_Format_M1_MockCall Returns(global::System.Func factory) @@ -424,75 +314,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFormatter_Format_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IFormatter_Format_M2_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IFormatter_Format_M2_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IFormatter_Format_M2_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public IFormatter_Format_M2_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IFormatter_Format_M2_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IFormatter_Format_M2_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IFormatter_Format_M2_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IFormatter_Format_M2_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IFormatter_Format_M2_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IFormatter_Format_M2_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public IFormatter_Format_M2_MockCall Returns(global::System.Func factory) @@ -514,75 +342,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!, (string)args[1]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFormatter_Format_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IFormatter_Format_M3_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IFormatter_Format_M3_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IFormatter_Format_M3_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public IFormatter_Format_M3_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IFormatter_Format_M3_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IFormatter_Format_M3_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IFormatter_Format_M3_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IFormatter_Format_M3_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IFormatter_Format_M3_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IFormatter_Format_M3_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public IFormatter_Format_M3_MockCall Returns(global::System.Func factory) @@ -604,20 +370,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!, (string)args[1]!, (string)args[2]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Params_Array_Parameter.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Params_Array_Parameter.verified.txt index 6f4d17da6c..8b0771249e 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Params_Array_Parameter.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Params_Array_Parameter.verified.txt @@ -289,58 +289,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IParamsSink_Sum_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IParamsSink_Sum_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IParamsSink_Sum_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IParamsSink_Sum_M0_MockCall Returns(int value) { EnsureSetup().Returns(value); return this; } - /// - public IParamsSink_Sum_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IParamsSink_Sum_M0_MockCall ReturnsSequentially(params int[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IParamsSink_Sum_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IParamsSink_Sum_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IParamsSink_Sum_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IParamsSink_Sum_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IParamsSink_Sum_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public IParamsSink_Sum_M0_MockCall Returns(global::System.Func factory) @@ -362,75 +314,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int[])args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IParamsSink_Render_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IParamsSink_Render_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IParamsSink_Render_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IParamsSink_Render_M1_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public IParamsSink_Render_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IParamsSink_Render_M1_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IParamsSink_Render_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IParamsSink_Render_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IParamsSink_Render_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IParamsSink_Render_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IParamsSink_Render_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public IParamsSink_Render_M1_MockCall Returns(global::System.Func factory) @@ -452,75 +342,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((object[])args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IParamsSink_Combine_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IParamsSink_Combine_M2_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IParamsSink_Combine_M2_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IParamsSink_Combine_M2_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public IParamsSink_Combine_M2_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IParamsSink_Combine_M2_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IParamsSink_Combine_M2_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IParamsSink_Combine_M2_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IParamsSink_Combine_M2_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IParamsSink_Combine_M2_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IParamsSink_Combine_M2_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public IParamsSink_Combine_M2_MockCall Returns(global::System.Func factory) @@ -542,77 +370,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!, (string[])args[1]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IParamsSink_First_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IParamsSink_First_M3_MockCall : global::TUnit.Mocks.MockMethodCallBase, T> { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IParamsSink_First_M3_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IParamsSink_First_M3_MockCall Returns(T value) { EnsureSetup().Returns(value); return this; } - /// - public IParamsSink_First_M3_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IParamsSink_First_M3_MockCall ReturnsSequentially(params T[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IParamsSink_First_M3_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IParamsSink_First_M3_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IParamsSink_First_M3_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IParamsSink_First_M3_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IParamsSink_First_M3_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } /// Configure a typed computed return value using the actual method parameters. public IParamsSink_First_M3_MockCall Returns(global::System.Func factory) @@ -634,20 +398,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((T[])args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Small_Numeric_Async_Results.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Small_Numeric_Async_Results.verified.txt index 0d46ff0565..7514cb8e5b 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Small_Numeric_Async_Results.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Small_Numeric_Async_Results.verified.txt @@ -197,58 +197,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ISmallNumericService_GetByteAsync_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ISmallNumericService_GetByteAsync_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal ISmallNumericService_GetByteAsync_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } + : base(engine, memberId, memberName, matchers, default) { } - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ISmallNumericService_GetByteAsync_M0_MockCall Returns(byte value) { EnsureSetup().Returns(value); return this; } - /// - public ISmallNumericService_GetByteAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public ISmallNumericService_GetByteAsync_M0_MockCall ReturnsSequentially(params byte[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public ISmallNumericService_GetByteAsync_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ISmallNumericService_GetByteAsync_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ISmallNumericService_GetByteAsync_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ISmallNumericService_GetByteAsync_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ISmallNumericService_GetByteAsync_M0_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new ISmallNumericService_GetByteAsync_M0_MockCall Returns(byte value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new ISmallNumericService_GetByteAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public ISmallNumericService_GetByteAsync_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -277,75 +234,18 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public ISmallNumericService_GetByteAsync_M0_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ISmallNumericService_GetShortAsync_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ISmallNumericService_GetShortAsync_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal ISmallNumericService_GetShortAsync_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } + : base(engine, memberId, memberName, matchers, default) { } - /// - public ISmallNumericService_GetShortAsync_M1_MockCall Returns(short value) { EnsureSetup().Returns(value); return this; } - /// - public ISmallNumericService_GetShortAsync_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public ISmallNumericService_GetShortAsync_M1_MockCall ReturnsSequentially(params short[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public ISmallNumericService_GetShortAsync_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ISmallNumericService_GetShortAsync_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ISmallNumericService_GetShortAsync_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ISmallNumericService_GetShortAsync_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ISmallNumericService_GetShortAsync_M1_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new ISmallNumericService_GetShortAsync_M1_MockCall Returns(short value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new ISmallNumericService_GetShortAsync_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built ValueTask directly (e.g., from a TaskCompletionSource). /// The same ValueTask instance is returned on every call. Since ValueTask may only be awaited once, @@ -378,75 +278,18 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public ISmallNumericService_GetShortAsync_M1_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ISmallNumericService_GetUnsignedAsync_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ISmallNumericService_GetUnsignedAsync_M2_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal ISmallNumericService_GetUnsignedAsync_M2_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } + : base(engine, memberId, memberName, matchers, default) { } - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ISmallNumericService_GetUnsignedAsync_M2_MockCall Returns(ulong value) { EnsureSetup().Returns(value); return this; } - /// - public ISmallNumericService_GetUnsignedAsync_M2_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public ISmallNumericService_GetUnsignedAsync_M2_MockCall ReturnsSequentially(params ulong[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public ISmallNumericService_GetUnsignedAsync_M2_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ISmallNumericService_GetUnsignedAsync_M2_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ISmallNumericService_GetUnsignedAsync_M2_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ISmallNumericService_GetUnsignedAsync_M2_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ISmallNumericService_GetUnsignedAsync_M2_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new ISmallNumericService_GetUnsignedAsync_M2_MockCall Returns(ulong value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new ISmallNumericService_GetUnsignedAsync_M2_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public ISmallNumericService_GetUnsignedAsync_M2_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -481,20 +324,6 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public ISmallNumericService_GetUnsignedAsync_M2_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Static_Abstract_Transitive_Return_Type.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Static_Abstract_Transitive_Return_Type.verified.txt index 75dcddb028..8cb608391d 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Static_Abstract_Transitive_Return_Type.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Static_Abstract_Transitive_Return_Type.verified.txt @@ -158,58 +158,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IMyService_GetValue_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IMyService_GetValue_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IMyService_GetValue_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IMyService_GetValue_M0_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public IMyService_GetValue_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IMyService_GetValue_M0_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IMyService_GetValue_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IMyService_GetValue_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IMyService_GetValue_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IMyService_GetValue_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IMyService_GetValue_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public IMyService_GetValue_M0_MockCall Returns(global::System.Func factory) @@ -231,20 +183,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Tuple_Async_Results.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Tuple_Async_Results.verified.txt index fccf6cdb97..0464a5fa28 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Tuple_Async_Results.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Tuple_Async_Results.verified.txt @@ -223,58 +223,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ITupleService_GetPairAsync_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ITupleService_GetPairAsync_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder<(object, object)>? _builder; - internal ITupleService_GetPairAsync_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder<(object, object)> EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } + : base(engine, memberId, memberName, matchers, default) { } - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder<(object, object)> EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder<(object, object)>(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ITupleService_GetPairAsync_M0_MockCall Returns((object, object) value) { EnsureSetup().Returns(value); return this; } - /// - public ITupleService_GetPairAsync_M0_MockCall Returns(global::System.Func<(object, object)> factory) { EnsureSetup().Returns(factory); return this; } - /// - public ITupleService_GetPairAsync_M0_MockCall ReturnsSequentially(params (object, object)[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public ITupleService_GetPairAsync_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ITupleService_GetPairAsync_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ITupleService_GetPairAsync_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ITupleService_GetPairAsync_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ITupleService_GetPairAsync_M0_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new ITupleService_GetPairAsync_M0_MockCall Returns((object, object) value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new ITupleService_GetPairAsync_M0_MockCall Returns(global::System.Func<(object, object)> factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public ITupleService_GetPairAsync_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task<(object, object)> task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -321,75 +278,18 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public ITupleService_GetPairAsync_M0_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ITupleService_GetNamedAsync_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ITupleService_GetNamedAsync_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder<(int Id, string Name)>? _builder; - internal ITupleService_GetNamedAsync_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder<(int Id, string Name)> EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } + : base(engine, memberId, memberName, matchers, default) { } - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder<(int Id, string Name)> EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder<(int Id, string Name)>(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ITupleService_GetNamedAsync_M1_MockCall Returns((int Id, string Name) value) { EnsureSetup().Returns(value); return this; } - /// - public ITupleService_GetNamedAsync_M1_MockCall Returns(global::System.Func<(int Id, string Name)> factory) { EnsureSetup().Returns(factory); return this; } - /// - public ITupleService_GetNamedAsync_M1_MockCall ReturnsSequentially(params (int Id, string Name)[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public ITupleService_GetNamedAsync_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ITupleService_GetNamedAsync_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ITupleService_GetNamedAsync_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ITupleService_GetNamedAsync_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ITupleService_GetNamedAsync_M1_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new ITupleService_GetNamedAsync_M1_MockCall Returns((int Id, string Name) value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new ITupleService_GetNamedAsync_M1_MockCall Returns(global::System.Func<(int Id, string Name)> factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built ValueTask directly (e.g., from a TaskCompletionSource). /// The same ValueTask instance is returned on every call. Since ValueTask may only be awaited once, @@ -444,75 +344,18 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public ITupleService_GetNamedAsync_M1_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ITupleService_GetNestedAsync_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ITupleService_GetNestedAsync_M2_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder<(long, (object, object))>? _builder; - internal ITupleService_GetNestedAsync_M2_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder<(long, (object, object))> EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } + : base(engine, memberId, memberName, matchers, default) { } - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder<(long, (object, object))> EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder<(long, (object, object))>(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ITupleService_GetNestedAsync_M2_MockCall Returns((long, (object, object)) value) { EnsureSetup().Returns(value); return this; } - /// - public ITupleService_GetNestedAsync_M2_MockCall Returns(global::System.Func<(long, (object, object))> factory) { EnsureSetup().Returns(factory); return this; } - /// - public ITupleService_GetNestedAsync_M2_MockCall ReturnsSequentially(params (long, (object, object))[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public ITupleService_GetNestedAsync_M2_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ITupleService_GetNestedAsync_M2_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ITupleService_GetNestedAsync_M2_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ITupleService_GetNestedAsync_M2_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ITupleService_GetNestedAsync_M2_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new ITupleService_GetNestedAsync_M2_MockCall Returns((long, (object, object)) value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new ITupleService_GetNestedAsync_M2_MockCall Returns(global::System.Func<(long, (object, object))> factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public ITupleService_GetNestedAsync_M2_MockCall ReturnsAsync(global::System.Threading.Tasks.Task<(long, (object, object))> task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -588,75 +431,18 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public ITupleService_GetNestedAsync_M2_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ITupleService_GetOptionalPairAsync_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ITupleService_GetOptionalPairAsync_M3_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder<(int, string)?>? _builder; - internal ITupleService_GetOptionalPairAsync_M3_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder<(int, string)?> EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } + : base(engine, memberId, memberName, matchers, default) { } - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder<(int, string)?> EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder<(int, string)?>(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ITupleService_GetOptionalPairAsync_M3_MockCall Returns((int, string)? value) { EnsureSetup().Returns(value); return this; } - /// - public ITupleService_GetOptionalPairAsync_M3_MockCall Returns(global::System.Func<(int, string)?> factory) { EnsureSetup().Returns(factory); return this; } - /// - public ITupleService_GetOptionalPairAsync_M3_MockCall ReturnsSequentially(params (int, string)?[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public ITupleService_GetOptionalPairAsync_M3_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ITupleService_GetOptionalPairAsync_M3_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ITupleService_GetOptionalPairAsync_M3_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ITupleService_GetOptionalPairAsync_M3_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ITupleService_GetOptionalPairAsync_M3_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new ITupleService_GetOptionalPairAsync_M3_MockCall Returns((int, string)? value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new ITupleService_GetOptionalPairAsync_M3_MockCall Returns(global::System.Func<(int, string)?> factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public ITupleService_GetOptionalPairAsync_M3_MockCall ReturnsAsync(global::System.Threading.Tasks.Task<(int, string)?> task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -709,20 +495,6 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public ITupleService_GetOptionalPairAsync_M3_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Type_Parameters_Named_Like_Numeric_Types.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Type_Parameters_Named_Like_Numeric_Types.verified.txt index 254274de22..cdd1002747 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Type_Parameters_Named_Like_Numeric_Types.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Type_Parameters_Named_Like_Numeric_Types.verified.txt @@ -205,58 +205,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class INumericNamed_Int32__GetAsync_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class INumericNamed_Int32__GetAsync_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase, Int32> { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal INumericNamed_Int32__GetAsync_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } + : base(engine, memberId, memberName, matchers, default) { } - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public INumericNamed_Int32__GetAsync_M0_MockCall Returns(Int32 value) { EnsureSetup().Returns(value); return this; } - /// - public INumericNamed_Int32__GetAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public INumericNamed_Int32__GetAsync_M0_MockCall ReturnsSequentially(params Int32[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public INumericNamed_Int32__GetAsync_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public INumericNamed_Int32__GetAsync_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public INumericNamed_Int32__GetAsync_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public INumericNamed_Int32__GetAsync_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public INumericNamed_Int32__GetAsync_M0_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new INumericNamed_Int32__GetAsync_M0_MockCall Returns(Int32 value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new INumericNamed_Int32__GetAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public INumericNamed_Int32__GetAsync_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -285,77 +242,18 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public INumericNamed_Int32__GetAsync_M0_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class INumericNamed_Int32__RoundtripAsync_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class INumericNamed_Int32__RoundtripAsync_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase, Int64> { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal INumericNamed_Int32__RoundtripAsync_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } + : base(engine, memberId, memberName, matchers, typeArguments) { } - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public INumericNamed_Int32__RoundtripAsync_M1_MockCall Returns(Int64 value) { EnsureSetup().Returns(value); return this; } - /// - public INumericNamed_Int32__RoundtripAsync_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public INumericNamed_Int32__RoundtripAsync_M1_MockCall ReturnsSequentially(params Int64[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public INumericNamed_Int32__RoundtripAsync_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public INumericNamed_Int32__RoundtripAsync_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public INumericNamed_Int32__RoundtripAsync_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public INumericNamed_Int32__RoundtripAsync_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public INumericNamed_Int32__RoundtripAsync_M1_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new INumericNamed_Int32__RoundtripAsync_M1_MockCall Returns(Int64 value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new INumericNamed_Int32__RoundtripAsync_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public INumericNamed_Int32__RoundtripAsync_M1_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -428,20 +326,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((Int64)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Unconstrained_Nullable_Generic.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Unconstrained_Nullable_Generic.verified.txt index 6622381a8e..6bba86c8bc 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Unconstrained_Nullable_Generic.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Unconstrained_Nullable_Generic.verified.txt @@ -171,60 +171,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFoo_DoSomethingAsync_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IFoo_DoSomethingAsync_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase, T?> { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IFoo_DoSomethingAsync_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } + : base(engine, memberId, memberName, matchers, typeArguments) { } - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IFoo_DoSomethingAsync_M0_MockCall Returns(T? value) { EnsureSetup().Returns(value); return this; } - /// - public IFoo_DoSomethingAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IFoo_DoSomethingAsync_M0_MockCall ReturnsSequentially(params T?[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IFoo_DoSomethingAsync_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IFoo_DoSomethingAsync_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IFoo_DoSomethingAsync_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IFoo_DoSomethingAsync_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IFoo_DoSomethingAsync_M0_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new IFoo_DoSomethingAsync_M0_MockCall Returns(T? value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new IFoo_DoSomethingAsync_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public IFoo_DoSomethingAsync_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -253,20 +208,6 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public IFoo_DoSomethingAsync_M0_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Unmanaged_Constrained_Type_Parameter.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Unmanaged_Constrained_Type_Parameter.verified.txt index ad373183f2..7dfc70c2a9 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Unmanaged_Constrained_Type_Parameter.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Unmanaged_Constrained_Type_Parameter.verified.txt @@ -291,55 +291,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ISnapshotSource_T__Fill_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification where T : unmanaged + public sealed class ISnapshotSource_T__Fill_M0_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase> where T : unmanaged { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal ISnapshotSource_T__Fill_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ISnapshotSource_T__Fill_M0_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public ISnapshotSource_T__Fill_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ISnapshotSource_T__Fill_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ISnapshotSource_T__Fill_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ISnapshotSource_T__Fill_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ISnapshotSource_T__Fill_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public ISnapshotSource_T__Fill_M0_MockCall Callback(global::System.Action callback) @@ -357,20 +312,6 @@ namespace TUnit.Mocks.Generated /// Sets the 'snapshot' ref parameter to the specified value when this setup matches. public ISnapshotSource_T__Fill_M0_MockCall SetsRefSnapshot(T snapshot) { EnsureSetup().SetsOutParameter(0, snapshot); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Method_With_More_Params_Than_Func_Action_Arity.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Method_With_More_Params_Than_Func_Action_Arity.verified.txt index 96911be75a..aaa8197d97 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Method_With_More_Params_Than_Func_Action_Arity.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Method_With_More_Params_Than_Func_Action_Arity.verified.txt @@ -152,55 +152,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ILongMethodSignatures_SomeMethod_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ILongMethodSignatures_SomeMethod_M0_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal ILongMethodSignatures_SomeMethod_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ILongMethodSignatures_SomeMethod_M0_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public ILongMethodSignatures_SomeMethod_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ILongMethodSignatures_SomeMethod_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ILongMethodSignatures_SomeMethod_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ILongMethodSignatures_SomeMethod_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ILongMethodSignatures_SomeMethod_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public ILongMethodSignatures_SomeMethod_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -208,20 +163,6 @@ namespace TUnit.Mocks.Generated public ILongMethodSignatures_SomeMethod_M0_MockCall ReturnsAsync(global::System.Func taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } /// Return a Task from a factory, invoked on each call. The Task is returned as-is, so an async factory stays pending until it completes. public ILongMethodSignatures_SomeMethod_M0_MockCall Returns(global::System.Func taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Multi_Interface_Mock_With_Secondary_Setup_Surface.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Multi_Interface_Mock_With_Secondary_Setup_Surface.verified.txt index f692751ae4..52e31cc278 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Multi_Interface_Mock_With_Secondary_Setup_Surface.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Multi_Interface_Mock_With_Secondary_Setup_Surface.verified.txt @@ -155,55 +155,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IMultiLogger_Log_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IMultiLogger_Log_M0_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal IMultiLogger_Log_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IMultiLogger_Log_M0_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public IMultiLogger_Log_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IMultiLogger_Log_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IMultiLogger_Log_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IMultiLogger_Log_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IMultiLogger_Log_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public IMultiLogger_Log_M0_MockCall Callback(global::System.Action callback) @@ -218,20 +173,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Multi_Method_Interface.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Multi_Method_Interface.verified.txt index d0cdc12ce7..1fd0bf48f3 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Multi_Method_Interface.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Multi_Method_Interface.verified.txt @@ -215,58 +215,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ICalculator_Add_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ICalculator_Add_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal ICalculator_Add_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ICalculator_Add_M0_MockCall Returns(int value) { EnsureSetup().Returns(value); return this; } - /// - public ICalculator_Add_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public ICalculator_Add_M0_MockCall ReturnsSequentially(params int[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public ICalculator_Add_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ICalculator_Add_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ICalculator_Add_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ICalculator_Add_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ICalculator_Add_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public ICalculator_Add_M0_MockCall Returns(global::System.Func factory) @@ -288,75 +240,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!, (int)args[1]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ICalculator_Subtract_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ICalculator_Subtract_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal ICalculator_Subtract_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ICalculator_Subtract_M1_MockCall Returns(int value) { EnsureSetup().Returns(value); return this; } - /// - public ICalculator_Subtract_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public ICalculator_Subtract_M1_MockCall ReturnsSequentially(params int[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public ICalculator_Subtract_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ICalculator_Subtract_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ICalculator_Subtract_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ICalculator_Subtract_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ICalculator_Subtract_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public ICalculator_Subtract_M1_MockCall Returns(global::System.Func factory) @@ -378,20 +268,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!, (int)args[1]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Internal_Virtual_Members_From_External_Assembly.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Internal_Virtual_Members_From_External_Assembly.verified.txt index 89a01a897d..ef28acd54a 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Internal_Virtual_Members_From_External_Assembly.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Internal_Virtual_Members_From_External_Assembly.verified.txt @@ -175,58 +175,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ExternalLib_ExternalClient_PublicMethod_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ExternalLib_ExternalClient_PublicMethod_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal ExternalLib_ExternalClient_PublicMethod_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ExternalLib_ExternalClient_PublicMethod_M0_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public ExternalLib_ExternalClient_PublicMethod_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public ExternalLib_ExternalClient_PublicMethod_M0_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public ExternalLib_ExternalClient_PublicMethod_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ExternalLib_ExternalClient_PublicMethod_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ExternalLib_ExternalClient_PublicMethod_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ExternalLib_ExternalClient_PublicMethod_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ExternalLib_ExternalClient_PublicMethod_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public ExternalLib_ExternalClient_PublicMethod_M0_MockCall Returns(global::System.Func factory) @@ -248,20 +200,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Members_With_Internal_Signature_Types.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Members_With_Internal_Signature_Types.verified.txt index 77d0d5aa2b..be3c2514bf 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Members_With_Internal_Signature_Types.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Members_With_Internal_Signature_Types.verified.txt @@ -135,58 +135,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ExternalLib_ServiceClient_GetValue_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ExternalLib_ServiceClient_GetValue_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal ExternalLib_ServiceClient_GetValue_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ExternalLib_ServiceClient_GetValue_M0_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public ExternalLib_ServiceClient_GetValue_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public ExternalLib_ServiceClient_GetValue_M0_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public ExternalLib_ServiceClient_GetValue_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ExternalLib_ServiceClient_GetValue_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ExternalLib_ServiceClient_GetValue_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ExternalLib_ServiceClient_GetValue_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ExternalLib_ServiceClient_GetValue_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public ExternalLib_ServiceClient_GetValue_M0_MockCall Returns(global::System.Func factory) @@ -208,20 +160,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt index ca57c3adfd..cfc6665e29 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt @@ -187,60 +187,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class BaseService_GetById_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification where T : class + public sealed class BaseService_GetById_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase, T> where T : class { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal BaseService_GetById_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public BaseService_GetById_M0_MockCall Returns(T value) { EnsureSetup().Returns(value); return this; } - /// - public BaseService_GetById_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public BaseService_GetById_M0_MockCall ReturnsSequentially(params T[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public BaseService_GetById_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public BaseService_GetById_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public BaseService_GetById_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public BaseService_GetById_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public BaseService_GetById_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } /// Configure a typed computed return value using the actual method parameters. public BaseService_GetById_M0_MockCall Returns(global::System.Func factory) @@ -262,74 +212,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class BaseService_Save_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification where T : class, new() + public sealed class BaseService_Save_M1_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase> where T : class, new() { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal BaseService_Save_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public BaseService_Save_M1_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public BaseService_Save_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public BaseService_Save_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public BaseService_Save_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public BaseService_Save_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public BaseService_Save_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } /// Execute a typed callback using the actual method parameters. public BaseService_Save_M1_MockCall Callback(global::System.Action callback) @@ -344,77 +233,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((T)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class BaseService_Transform_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification where TInput : notnull where TResult : struct + public sealed class BaseService_Transform_M2_MockCall : global::TUnit.Mocks.MockMethodCallBase, TResult> where TInput : notnull where TResult : struct { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal BaseService_Transform_M2_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public BaseService_Transform_M2_MockCall Returns(TResult value) { EnsureSetup().Returns(value); return this; } - /// - public BaseService_Transform_M2_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public BaseService_Transform_M2_MockCall ReturnsSequentially(params TResult[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public BaseService_Transform_M2_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public BaseService_Transform_M2_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public BaseService_Transform_M2_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public BaseService_Transform_M2_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public BaseService_Transform_M2_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } /// Configure a typed computed return value using the actual method parameters. public BaseService_Transform_M2_MockCall Returns(global::System.Func factory) @@ -436,20 +261,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((TInput)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/RefStruct_Events_Generation_Snapshot.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/RefStruct_Events_Generation_Snapshot.verified.txt index 7c2e4a7ba4..2b8d0247c8 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/RefStruct_Events_Generation_Snapshot.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/RefStruct_Events_Generation_Snapshot.verified.txt @@ -316,144 +316,23 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IEvents_Execute_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IEvents_Execute_M0_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal IEvents_Execute_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IEvents_Execute_M0_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public IEvents_Execute_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IEvents_Execute_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IEvents_Execute_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IEvents_Execute_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IEvents_Execute_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Auto-raise the Ordinary event when this method is called. public IEvents_Execute_M0_MockCall RaisesOrdinary(string e) { EnsureSetup().Raises("Ordinary", (object?)e); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IEvents_Query_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IEvents_Query_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IEvents_Query_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IEvents_Query_M1_MockCall Returns(int value) { EnsureSetup().Returns(value); return this; } - /// - public IEvents_Query_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IEvents_Query_M1_MockCall ReturnsSequentially(params int[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IEvents_Query_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IEvents_Query_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IEvents_Query_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IEvents_Query_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IEvents_Query_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Auto-raise the Ordinary event when this method is called. public IEvents_Query_M1_MockCall RaisesOrdinary(string e) { EnsureSetup().Raises("Ordinary", (object?)e); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Returning_Async_Method_With_More_Params_Than_Func_Action_Arity.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Returning_Async_Method_With_More_Params_Than_Func_Action_Arity.verified.txt index 0bcc9c467f..21f3e3f3ff 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Returning_Async_Method_With_More_Params_Than_Func_Action_Arity.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Returning_Async_Method_With_More_Params_Than_Func_Action_Arity.verified.txt @@ -152,58 +152,15 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ILongReturningSignature_Sum_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class ILongReturningSignature_Sum_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal ILongReturningSignature_Sum_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } + : base(engine, memberId, memberName, matchers, default) { } - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public ILongReturningSignature_Sum_M0_MockCall Returns(int value) { EnsureSetup().Returns(value); return this; } - /// - public ILongReturningSignature_Sum_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public ILongReturningSignature_Sum_M0_MockCall ReturnsSequentially(params int[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public ILongReturningSignature_Sum_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public ILongReturningSignature_Sum_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public ILongReturningSignature_Sum_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public ILongReturningSignature_Sum_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public ILongReturningSignature_Sum_M0_MockCall Then() { EnsureSetup().Then(); return this; } + /// Configure a fixed return value. + public new ILongReturningSignature_Sum_M0_MockCall Returns(int value) { EnsureSetup().Returns(value); return this; } + /// Configure a computed return value, invoked on each call. + public new ILongReturningSignature_Sum_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } /// Return a pre-built Task directly (e.g., from a TaskCompletionSource). public ILongReturningSignature_Sum_M0_MockCall ReturnsAsync(global::System.Threading.Tasks.Task task) { EnsureSetup().ReturnsRaw(task); return this; } @@ -236,20 +193,6 @@ namespace TUnit.Mocks.Generated [global::System.Runtime.CompilerServices.OverloadResolutionPriority(-1)] public ILongReturningSignature_Sum_M0_MockCall Returns(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } #endif - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/SelfEquatable_Generates_EqualsOf_GetHashCodeOf_ToStringOf.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/SelfEquatable_Generates_EqualsOf_GetHashCodeOf_ToStringOf.verified.txt index bfb67d5fc3..51430ba33a 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/SelfEquatable_Generates_EqualsOf_GetHashCodeOf_ToStringOf.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/SelfEquatable_Generates_EqualsOf_GetHashCodeOf_ToStringOf.verified.txt @@ -184,58 +184,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class SelfEquatableSnapshot_Equals_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class SelfEquatableSnapshot_Equals_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal SelfEquatableSnapshot_Equals_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public SelfEquatableSnapshot_Equals_M0_MockCall Returns(bool value) { EnsureSetup().Returns(value); return this; } - /// - public SelfEquatableSnapshot_Equals_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public SelfEquatableSnapshot_Equals_M0_MockCall ReturnsSequentially(params bool[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public SelfEquatableSnapshot_Equals_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public SelfEquatableSnapshot_Equals_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public SelfEquatableSnapshot_Equals_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public SelfEquatableSnapshot_Equals_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public SelfEquatableSnapshot_Equals_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public SelfEquatableSnapshot_Equals_M0_MockCall Returns(global::System.Func factory) @@ -257,75 +209,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((global::SelfEquatableSnapshot?)args[0])); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class SelfEquatableSnapshot_Equals_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class SelfEquatableSnapshot_Equals_M1_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal SelfEquatableSnapshot_Equals_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public SelfEquatableSnapshot_Equals_M1_MockCall Returns(bool value) { EnsureSetup().Returns(value); return this; } - /// - public SelfEquatableSnapshot_Equals_M1_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public SelfEquatableSnapshot_Equals_M1_MockCall ReturnsSequentially(params bool[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public SelfEquatableSnapshot_Equals_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public SelfEquatableSnapshot_Equals_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public SelfEquatableSnapshot_Equals_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public SelfEquatableSnapshot_Equals_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public SelfEquatableSnapshot_Equals_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public SelfEquatableSnapshot_Equals_M1_MockCall Returns(global::System.Func factory) @@ -347,20 +237,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((object?)args[0])); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Simple_Interface_With_One_Method.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Simple_Interface_With_One_Method.verified.txt index 366d1aa33e..368ae1eb06 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Simple_Interface_With_One_Method.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Simple_Interface_With_One_Method.verified.txt @@ -139,58 +139,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IGreeter_Greet_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class IGreeter_Greet_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal IGreeter_Greet_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public IGreeter_Greet_M0_MockCall Returns(string value) { EnsureSetup().Returns(value); return this; } - /// - public IGreeter_Greet_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public IGreeter_Greet_M0_MockCall ReturnsSequentially(params string[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public IGreeter_Greet_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public IGreeter_Greet_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public IGreeter_Greet_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public IGreeter_Greet_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public IGreeter_Greet_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public IGreeter_Greet_M0_MockCall Returns(global::System.Func factory) @@ -212,20 +164,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Static_Extension_Discovery_Without_Mock_Of.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Static_Extension_Discovery_Without_Mock_Of.verified.txt index 2a8bf982de..57f29f9000 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Static_Extension_Discovery_Without_Mock_Of.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Static_Extension_Discovery_Without_Mock_Of.verified.txt @@ -155,55 +155,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class INotifier_Notify_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class INotifier_Notify_M0_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal INotifier_Notify_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public INotifier_Notify_M0_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public INotifier_Notify_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public INotifier_Notify_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public INotifier_Notify_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public INotifier_Notify_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public INotifier_Notify_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Execute a typed callback using the actual method parameters. public INotifier_Notify_M0_MockCall Callback(global::System.Action callback) @@ -218,20 +173,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Type_Mocked_Regularly_And_Wrapped.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Type_Mocked_Regularly_And_Wrapped.verified.txt index 8ca8f5ec71..f9235ad942 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Type_Mocked_Regularly_And_Wrapped.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Type_Mocked_Regularly_And_Wrapped.verified.txt @@ -190,58 +190,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class TestNamespace_Calculator_Add_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public sealed class TestNamespace_Calculator_Add_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal TestNamespace_Calculator_Add_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public TestNamespace_Calculator_Add_M0_MockCall Returns(int value) { EnsureSetup().Returns(value); return this; } - /// - public TestNamespace_Calculator_Add_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public TestNamespace_Calculator_Add_M0_MockCall ReturnsSequentially(params int[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public TestNamespace_Calculator_Add_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public TestNamespace_Calculator_Add_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public TestNamespace_Calculator_Add_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public TestNamespace_Calculator_Add_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public TestNamespace_Calculator_Add_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, default) { } /// Configure a typed computed return value using the actual method parameters. public TestNamespace_Calculator_Add_M0_MockCall Returns(global::System.Func factory) @@ -263,20 +215,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!, (int)args[1]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(times, message); - /// - public void WasCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasCalled(message); - /// - public void WasNeverCalled() => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => _engine.CreateVerification(_memberId, _memberName, _matchers).WasNeverCalled(message); } } diff --git a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Wrap_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Wrap_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt index 4b07843a6b..2177ec4f44 100644 --- a/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Wrap_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt +++ b/tests/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Wrap_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt @@ -77,60 +77,10 @@ namespace TUnit.Mocks.Generated } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class Repository_Get_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification where T : class + public sealed class Repository_Get_M0_MockCall : global::TUnit.Mocks.MockMethodCallBase, T> where T : class { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.MethodSetupBuilder? _builder; - internal Repository_Get_M0_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - } - - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.MethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.MethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public Repository_Get_M0_MockCall Returns(T value) { EnsureSetup().Returns(value); return this; } - /// - public Repository_Get_M0_MockCall Returns(global::System.Func factory) { EnsureSetup().Returns(factory); return this; } - /// - public Repository_Get_M0_MockCall ReturnsSequentially(params T[] values) { EnsureSetup().ReturnsSequentially(values); return this; } - /// - public Repository_Get_M0_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public Repository_Get_M0_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public Repository_Get_M0_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public Repository_Get_M0_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public Repository_Get_M0_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } /// Configure a typed computed return value using the actual method parameters. public Repository_Get_M0_MockCall Returns(global::System.Func factory) @@ -152,74 +102,13 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class Repository_Store_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification where T : class, new() + public sealed class Repository_Store_M1_MockCall : global::TUnit.Mocks.VoidMockMethodCallBase> where T : class, new() { - private readonly global::TUnit.Mocks.IMockEngineAccess _engine; - private readonly int _memberId; - private readonly string _memberName; - private readonly global::TUnit.Mocks.Arguments.IArgumentMatcher[] _matchers; - private readonly global::System.Collections.Immutable.ImmutableArray _typeArguments; - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder? _builder; - internal Repository_Store_M1_MockCall(global::TUnit.Mocks.IMockEngineAccess engine, int memberId, string memberName, global::TUnit.Mocks.Arguments.IArgumentMatcher[] matchers, global::System.Collections.Immutable.ImmutableArray typeArguments) - { - _engine = engine; - _memberId = memberId; - _memberName = memberName; - _matchers = matchers; - _typeArguments = typeArguments; - _ = EnsureSetup(); - } - - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetup() - { - var existing = global::System.Threading.Volatile.Read(ref _builder); - if (existing is not null) return existing; - return EnsureSetupSlow(); - } - - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() - { - var setup = new global::TUnit.Mocks.Setup.MethodSetup(_memberId, _matchers, _memberName, _typeArguments); - var fresh = new global::TUnit.Mocks.Setup.VoidMethodSetupBuilder(setup); - var prev = global::System.Threading.Interlocked.CompareExchange(ref _builder, fresh, null); - if (prev is not null) return prev; - // AddSetup runs only on the CAS winner. Setup is sequential in practice, - // so a concurrent loser observing the builder before registration is benign. - _engine.AddSetup(setup); - return fresh; - } - - /// - public Repository_Store_M1_MockCall Returns() { EnsureSetup().Returns(); return this; } - /// - public Repository_Store_M1_MockCall Throws() where TException : global::System.Exception, new() { EnsureSetup().Throws(); return this; } - /// - public Repository_Store_M1_MockCall Throws(global::System.Exception exception) { EnsureSetup().Throws(exception); return this; } - /// - public Repository_Store_M1_MockCall Callback(global::System.Action callback) { EnsureSetup().Callback(callback); return this; } - /// - public Repository_Store_M1_MockCall TransitionsTo(string stateName) { EnsureSetup().TransitionsTo(stateName); return this; } - /// - public Repository_Store_M1_MockCall Then() { EnsureSetup().Then(); return this; } + : base(engine, memberId, memberName, matchers, typeArguments) { } /// Execute a typed callback using the actual method parameters. public Repository_Store_M1_MockCall Callback(global::System.Action callback) @@ -234,20 +123,6 @@ namespace TUnit.Mocks.Generated EnsureSetup().Throws(args => exceptionFactory((T)args[0]!)); return this; } - - // ICallVerification - /// - public void WasCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(); - /// - public void WasCalled(global::TUnit.Mocks.Times times) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times); - /// - public void WasCalled(global::TUnit.Mocks.Times times, string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(times, message); - /// - public void WasCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasCalled(message); - /// - public void WasNeverCalled() => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(); - /// - public void WasNeverCalled(string? message) => new global::TUnit.Mocks.MockMethodCall(_engine, _memberId, _memberName, _matchers, _typeArguments).WasNeverCalled(message); } }