diff --git a/TUnit.Mocks.SourceGenerator.Tests/MockGeneratorTests.cs b/TUnit.Mocks.SourceGenerator.Tests/MockGeneratorTests.cs index adbb42c728..458d8114a4 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/MockGeneratorTests.cs +++ b/TUnit.Mocks.SourceGenerator.Tests/MockGeneratorTests.cs @@ -1498,4 +1498,79 @@ void M() return VerifyGeneratorOutput(source); } + + [Test] + public void Global_Namespace_Mock_Does_Not_Emit_Empty_Namespace_Prefix() + { + // Regression: when an interface lives in the global namespace, the + // builders previously emitted `global::.IGreeterMock` because they + // unconditionally interpolated `$"global::{mockNamespace}.{TypeName}"` + // and `mockNamespace` is "" for global-namespace types in + // non-fallback mode. That syntax fails to compile (CS1001). + // + // We assert the textual form rather than calling AssertGeneratedCodeCompiles + // because the generated `extension(...)` blocks are not parseable by the + // test-pinned Roslyn (see SnapshotTestBase.AssertGeneratedCodeHasNoNullableWarnings + // remarks). Searching for the broken `global::.` substring directly catches + // the regression no matter where it surfaces (MockStaticExtensionBuilder, + // MockMembersBuilder, GetMockableTypeName, etc.). + var source = """ + using TUnit.Mocks; + + public interface IGreeter + { + string Greet(string name); + } + + public class TestUsage + { + void M() + { + var mock = Mock.Of(); + _ = mock.Greet("hi"); + } + } + """; + + var generated = RunGenerator(source); + var combined = string.Join("\n", generated); + + if (combined.Contains("global::.")) + { + var lines = combined.Split('\n') + .Select((line, idx) => (line, idx)) + .Where(t => t.line.Contains("global::.")) + .Select(t => $" line {t.idx + 1}: {t.line.Trim()}") + .ToList(); + + throw new InvalidOperationException( + "Generated code contains invalid 'global::.' (empty-namespace) prefix. " + + "A builder is concatenating `$\"global::{ns}.{Name}\"` without guarding " + + "for empty `ns`. Use `MockImplBuilder.GetGlobalMockNamespacePrefix(model)` " + + "instead.\nOffending lines:\n" + + string.Join("\n", lines)); + } + } + + [Test] + public Task Conflict_With_Existing_Type_Falls_Back_To_Generated_Namespace() + { + var source = """ + using TUnit.Mocks; + + namespace MyApp; + + public interface IGreeter { string Greet(string name); } + + // User already has a class named IGreeterMock — generator must fall back. + public class IGreeterMock { } + + public class TestUsage + { + void M() { var mock = Mock.Of(); } + } + """; + + return VerifyGeneratorOutput(source); + } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/MockNamespaceConflictTests.cs b/TUnit.Mocks.SourceGenerator.Tests/MockNamespaceConflictTests.cs new file mode 100644 index 0000000000..3efb142e0f --- /dev/null +++ b/TUnit.Mocks.SourceGenerator.Tests/MockNamespaceConflictTests.cs @@ -0,0 +1,162 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using TUnit.Mocks.SourceGenerator.Discovery; + +namespace TUnit.Mocks.SourceGenerator.Tests; + +/// +/// Tests for — the helper that decides +/// whether placing a generated mock alongside its target type would collide with an +/// existing user-declared type in the same namespace. +/// +public class MockNamespaceConflictTests : SnapshotTestBase +{ + [Test] + public async Task NoConflict_ReturnsFalse() + { + var source = """ + namespace MyApp + { + public interface IGreeter + { + string Greet(string name); + } + } + """; + + var (compilation, target) = CompileAndGetType(source, "MyApp.IGreeter"); + + var result = MockNamespaceConflictDetector.HasConflict(compilation, target); + + await Assert.That(result).IsFalse(); + } + + [Test] + public async Task TypeNamedFooMockExists_ReturnsTrue() + { + var source = """ + namespace MyApp + { + public interface IGreeter + { + string Greet(string name); + } + + public class IGreeterMock + { + } + } + """; + + var (compilation, target) = CompileAndGetType(source, "MyApp.IGreeter"); + + var result = MockNamespaceConflictDetector.HasConflict(compilation, target); + + await Assert.That(result).IsTrue(); + } + + [Test] + public async Task EventsExtensionsCollision_AlwaysFlagged() + { + // The detector treats every emitted-suffix collision as a conflict, regardless + // of whether the target type actually has events. Keeping the rule symmetric + // ensures both call sites — model construction and auto-mock factory references + // — reach the same fallback decision without re-deriving event lists. + var source = """ + namespace MyApp + { + public interface IGreeter + { + string Greet(string name); + } + + public static class IGreeter_MockEventsExtensions + { + } + } + """; + + var (compilation, target) = CompileAndGetType(source, "MyApp.IGreeter"); + + var result = MockNamespaceConflictDetector.HasConflict(compilation, target); + + await Assert.That(result).IsTrue(); + } + + [Test] + public async Task GlobalNamespace_ChecksGlobalTypes() + { + var source = """ + public interface IGreeter + { + string Greet(string name); + } + + public class IGreeterMock + { + } + """; + + var (compilation, target) = CompileAndGetType(source, "IGreeter"); + + var result = MockNamespaceConflictDetector.HasConflict(compilation, target); + + await Assert.That(result).IsTrue(); + } + + [Test] + public async Task NestedNamespace_ChecksDeepPath() + { + var (compilation, type) = CompileAndGetType(""" + namespace A.B.C; + public interface IGreeter { string Greet(string name); } + public class IGreeterMock { } + """, "A.B.C.IGreeter"); + + await Assert.That(MockNamespaceConflictDetector.HasConflict(compilation, type)).IsTrue(); + } + + [Test] + public async Task GenericType_DetectsCollisionByBareName() + { + var (compilation, type) = CompileAndGetType(""" + namespace MyApp; + public interface IGreeter { T Greet(string name); } + public class IGreeterMock { } + """, "MyApp.IGreeter`1"); + + await Assert.That(MockNamespaceConflictDetector.HasConflict(compilation, type)).IsTrue(); + } + + [Test] + public async Task ExternalAssemblyType_WithoutCollisionInConsumer_ReturnsFalse() + { + // Documents the cross-assembly contract: types referenced from another assembly + // are checked against the consumer's compilation. Absence of a colliding type in + // the consumer's view of that namespace returns false (no conflict). + var externalRef = CreateExternalAssemblyReference(""" + namespace ExternalLib; + public interface IGreeter { string Greet(string name); } + """, assemblyName: "ExtLib"); + + // Consumer source declares NO ExternalLib namespace at all. + var tree = CSharpSyntaxTree.ParseText("class C {}", + CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Preview)); + var compilation = CSharpCompilation.Create( + "Consumer", [tree], GetCachedReferences().Append(externalRef)); + + var type = compilation.GetTypeByMetadataName("ExternalLib.IGreeter")!; + + await Assert.That(MockNamespaceConflictDetector.HasConflict(compilation, type)).IsFalse(); + } + + private static (Compilation, INamedTypeSymbol) CompileAndGetType(string source, string fullyQualifiedName) + { + var tree = CSharpSyntaxTree.ParseText(source, + CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Preview)); + var compilation = CSharpCompilation.Create("Test", [tree], GetCachedReferences()); + var type = compilation.GetTypeByMetadataName(fullyQualifiedName) + ?? throw new InvalidOperationException($"Type not found: {fullyQualifiedName}"); + return (compilation, type); + } +} diff --git a/TUnit.Mocks.SourceGenerator.Tests/SnapshotTestBase.cs b/TUnit.Mocks.SourceGenerator.Tests/SnapshotTestBase.cs index 5bc99bd07e..99fc91e002 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/SnapshotTestBase.cs +++ b/TUnit.Mocks.SourceGenerator.Tests/SnapshotTestBase.cs @@ -14,6 +14,13 @@ public abstract class SnapshotTestBase private static readonly Lazy> _references = new(LoadReferences); private static readonly UTF8Encoding SnapshotEncoding = new(encoderShouldEmitUTF8Identifier: false); + /// + /// Returns the shared, lazily-loaded set of metadata references used by the test compilations + /// (current AppDomain assemblies plus the ref/TUnit.Mocks.dll if present). Derived + /// test classes should reuse this instead of re-discovering references per test invocation. + /// + protected static IEnumerable GetCachedReferences() => _references.Value; + private static List LoadReferences() { var refs = AppDomain.CurrentDomain.GetAssemblies() diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_Implementing_Static_Abstract_Interface.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_Implementing_Static_Abstract_Interface.verified.txt index 88d3e7a9a9..4dad239b74 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_Implementing_Static_Abstract_Interface.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_Implementing_Static_Abstract_Interface.verified.txt @@ -2,56 +2,53 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class StaticAbstractImplMockImpl : global::StaticAbstractImpl, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class StaticAbstractImplMockImpl : global::StaticAbstractImpl, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] - internal StaticAbstractImplMockImpl(global::TUnit.Mocks.MockEngine engine) : base() - { - _engine = engine; - } + [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] + internal StaticAbstractImplMockImpl(global::TUnit.Mocks.MockEngine engine) : base() + { + _engine = engine; + } - public override int InstanceValue + public override int InstanceValue + { + get { - get + if (_engine.TryHandleCallWithReturn(0, "get_InstanceValue", global::System.Array.Empty(), default, out var __result)) { - if (_engine.TryHandleCallWithReturn(0, "get_InstanceValue", global::System.Array.Empty(), default, out var __result)) - { - return __result; - } - return base.InstanceValue; + return __result; } + return base.InstanceValue; } + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - file static class StaticAbstractImplPartialMockFactory +file static class StaticAbstractImplPartialMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new StaticAbstractImplMockImpl(engine); - engine.Raisable = impl; - var mock = new global::TUnit.Mocks.Mock(impl, engine); - return mock; - } + private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new StaticAbstractImplMockImpl(engine); + engine.Raisable = impl; + var mock = new global::TUnit.Mocks.Mock(impl, engine); + return mock; } } @@ -62,15 +59,12 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class StaticAbstractImpl_MockMemberExtensions { - public static class StaticAbstractImpl_MockMemberExtensions + extension(global::TUnit.Mocks.Mock mock) { - extension(global::TUnit.Mocks.Mock mock) - { - public global::TUnit.Mocks.PropertyMockCall InstanceValue - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, 0, "InstanceValue", true, false); - } + public global::TUnit.Mocks.PropertyMockCall InstanceValue + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, 0, "InstanceValue", true, false); } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_With_Constructor_Parameters_Extension_Discovery.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_With_Constructor_Parameters_Extension_Discovery.verified.txt index 917d0a40a8..f275e44e04 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_With_Constructor_Parameters_Extension_Discovery.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_With_Constructor_Parameters_Extension_Discovery.verified.txt @@ -2,93 +2,90 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class MyServiceMockImpl : global::MyService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class MyServiceMockImpl : global::MyService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] - internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine) : base() - { - _engine = engine; - } - [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] - internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine, string connectionString, int timeout) : base(connectionString, timeout) - { - _engine = engine; - } - [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] - internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine, string connectionString, int timeout, bool verbose) : base(connectionString, timeout, verbose) - { - _engine = engine; - } + [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] + internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine) : base() + { + _engine = engine; + } + [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] + internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine, string connectionString, int timeout) : base(connectionString, timeout) + { + _engine = engine; + } + [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] + internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine, string connectionString, int timeout, bool verbose) : base(connectionString, timeout, verbose) + { + _engine = engine; + } - public override string GetValue() + public override string GetValue() + { + if (_engine.TryHandleCallWithReturn(0, "GetValue", global::System.Array.Empty(), "", out var __result)) { - if (_engine.TryHandleCallWithReturn(0, "GetValue", global::System.Array.Empty(), "", out var __result)) - { - return __result; - } - return base.GetValue(); + return __result; } + return base.GetValue(); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); + } +} + +file static class MyServicePartialMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() + { + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); } - file static class MyServicePartialMockFactory + private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() + var engine = new global::TUnit.Mocks.MockEngine(behavior); + MyServiceMockImpl impl; + if (constructorArgs.Length == 0) { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + impl = new MyServiceMockImpl(engine); } - - private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + else if (constructorArgs.Length == 2) { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - MyServiceMockImpl impl; - if (constructorArgs.Length == 0) + if ((constructorArgs[0] is null or string) && constructorArgs[1] is int) { - impl = new MyServiceMockImpl(engine); + impl = new MyServiceMockImpl(engine, (string)constructorArgs[0], (int)constructorArgs[1]); } - else if (constructorArgs.Length == 2) + else { - if ((constructorArgs[0] is null or string) && constructorArgs[1] is int) - { - impl = new MyServiceMockImpl(engine, (string)constructorArgs[0], (int)constructorArgs[1]); - } - else - { - throw new global::System.ArgumentException($"No matching constructor found for type 'global::MyService' with the provided argument types."); - } + throw new global::System.ArgumentException($"No matching constructor found for type 'global::MyService' with the provided argument types."); } - else if (constructorArgs.Length == 3) + } + else if (constructorArgs.Length == 3) + { + if ((constructorArgs[0] is null or string) && constructorArgs[1] is int && constructorArgs[2] is bool) { - if ((constructorArgs[0] is null or string) && constructorArgs[1] is int && constructorArgs[2] is bool) - { - impl = new MyServiceMockImpl(engine, (string)constructorArgs[0], (int)constructorArgs[1], (bool)constructorArgs[2]); - } - else - { - throw new global::System.ArgumentException($"No matching constructor found for type 'global::MyService' with the provided argument types."); - } + impl = new MyServiceMockImpl(engine, (string)constructorArgs[0], (int)constructorArgs[1], (bool)constructorArgs[2]); } else { - throw new global::System.ArgumentException($"No matching constructor found for type 'global::MyService' with {constructorArgs.Length} argument(s)."); + throw new global::System.ArgumentException($"No matching constructor found for type 'global::MyService' with the provided argument types."); } - engine.Raisable = impl; - var mock = new global::TUnit.Mocks.Mock(impl, engine); - return mock; } + else + { + throw new global::System.ArgumentException($"No matching constructor found for type 'global::MyService' with {constructorArgs.Length} argument(s)."); + } + engine.Raisable = impl; + var mock = new global::TUnit.Mocks.Mock(impl, engine); + return mock; } } @@ -99,15 +96,12 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class MyService_MockMemberExtensions { - public static class MyService_MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall GetValue(this global::TUnit.Mocks.Mock mock) { - public static global::TUnit.Mocks.MockMethodCall GetValue(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetValue", matchers); - } + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetValue", matchers); } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_With_Required_Members.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_With_Required_Members.verified.txt index 3f99453bdd..d4a51f01a5 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_With_Required_Members.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_With_Required_Members.verified.txt @@ -2,44 +2,41 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class ConfigBaseMockImpl : global::ConfigBase, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class ConfigBaseMockImpl : global::ConfigBase, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] - internal ConfigBaseMockImpl(global::TUnit.Mocks.MockEngine engine) : base() - { - _engine = engine; - } + [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] + internal ConfigBaseMockImpl(global::TUnit.Mocks.MockEngine engine) : base() + { + _engine = engine; + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - file static class ConfigBasePartialMockFactory +file static class ConfigBasePartialMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new ConfigBaseMockImpl(engine); - engine.Raisable = impl; - var mock = new global::TUnit.Mocks.Mock(impl, engine); - return mock; - } + private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new ConfigBaseMockImpl(engine); + engine.Raisable = impl; + var mock = new global::TUnit.Mocks.Mock(impl, engine); + return mock; } } @@ -50,11 +47,8 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class ConfigBase_MockMemberExtensions { - public static class ConfigBase_MockMemberExtensions - { - } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_With_Same_Arity_Constructor_Overloads.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_With_Same_Arity_Constructor_Overloads.verified.txt index 751421a6db..ff1c3dbe24 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_With_Same_Arity_Constructor_Overloads.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Class_With_Same_Arity_Constructor_Overloads.verified.txt @@ -2,102 +2,99 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class MyServiceMockImpl : global::MyService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class MyServiceMockImpl : global::MyService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] - internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine, string name) : base(name) - { - _engine = engine; - } - [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] - internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine, int id) : base(id) - { - _engine = engine; - } - [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] - internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine, string host, int port) : base(host, port) - { - _engine = engine; - } - [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] - internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine, int timeout, bool verbose) : base(timeout, verbose) - { - _engine = engine; - } + [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] + internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine, string name) : base(name) + { + _engine = engine; + } + [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] + internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine, int id) : base(id) + { + _engine = engine; + } + [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] + internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine, string host, int port) : base(host, port) + { + _engine = engine; + } + [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] + internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine, int timeout, bool verbose) : base(timeout, verbose) + { + _engine = engine; + } - public override string GetValue() + public override string GetValue() + { + if (_engine.TryHandleCallWithReturn(0, "GetValue", global::System.Array.Empty(), "", out var __result)) { - if (_engine.TryHandleCallWithReturn(0, "GetValue", global::System.Array.Empty(), "", out var __result)) - { - return __result; - } - return base.GetValue(); + return __result; } + return base.GetValue(); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); + } +} + +file static class MyServicePartialMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() + { + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); } - file static class MyServicePartialMockFactory + private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() + var engine = new global::TUnit.Mocks.MockEngine(behavior); + MyServiceMockImpl impl; + if (constructorArgs.Length == 1) { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + if ((constructorArgs[0] is null or string)) + { + impl = new MyServiceMockImpl(engine, (string)constructorArgs[0]); + } + else if (constructorArgs[0] is int) + { + impl = new MyServiceMockImpl(engine, (int)constructorArgs[0]); + } + else + { + throw new global::System.ArgumentException($"No matching constructor found for type 'global::MyService' with the provided argument types."); + } } - - private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + else if (constructorArgs.Length == 2) { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - MyServiceMockImpl impl; - if (constructorArgs.Length == 1) + if ((constructorArgs[0] is null or string) && constructorArgs[1] is int) { - if ((constructorArgs[0] is null or string)) - { - impl = new MyServiceMockImpl(engine, (string)constructorArgs[0]); - } - else if (constructorArgs[0] is int) - { - impl = new MyServiceMockImpl(engine, (int)constructorArgs[0]); - } - else - { - throw new global::System.ArgumentException($"No matching constructor found for type 'global::MyService' with the provided argument types."); - } + impl = new MyServiceMockImpl(engine, (string)constructorArgs[0], (int)constructorArgs[1]); } - else if (constructorArgs.Length == 2) + else if (constructorArgs[0] is int && constructorArgs[1] is bool) { - if ((constructorArgs[0] is null or string) && constructorArgs[1] is int) - { - impl = new MyServiceMockImpl(engine, (string)constructorArgs[0], (int)constructorArgs[1]); - } - else if (constructorArgs[0] is int && constructorArgs[1] is bool) - { - impl = new MyServiceMockImpl(engine, (int)constructorArgs[0], (bool)constructorArgs[1]); - } - else - { - throw new global::System.ArgumentException($"No matching constructor found for type 'global::MyService' with the provided argument types."); - } + impl = new MyServiceMockImpl(engine, (int)constructorArgs[0], (bool)constructorArgs[1]); } else { - throw new global::System.ArgumentException($"No matching constructor found for type 'global::MyService' with {constructorArgs.Length} argument(s)."); + throw new global::System.ArgumentException($"No matching constructor found for type 'global::MyService' with the provided argument types."); } - engine.Raisable = impl; - var mock = new global::TUnit.Mocks.Mock(impl, engine); - return mock; } + else + { + throw new global::System.ArgumentException($"No matching constructor found for type 'global::MyService' with {constructorArgs.Length} argument(s)."); + } + engine.Raisable = impl; + var mock = new global::TUnit.Mocks.Mock(impl, engine); + return mock; } } @@ -108,15 +105,12 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class MyService_MockMemberExtensions { - public static class MyService_MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall GetValue(this global::TUnit.Mocks.Mock mock) { - public static global::TUnit.Mocks.MockMethodCall GetValue(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetValue", matchers); - } + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetValue", matchers); } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Conflict_With_Existing_Type_Falls_Back_To_Generated_Namespace.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Conflict_With_Existing_Type_Falls_Back_To_Generated_Namespace.verified.txt new file mode 100644 index 0000000000..90bfeea9b4 --- /dev/null +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Conflict_With_Existing_Type_Falls_Back_To_Generated_Namespace.verified.txt @@ -0,0 +1,223 @@ +// +#pragma warning disable +#nullable enable + +namespace TUnit.Mocks.Generated.MyApp +{ + public sealed class IGreeterMock : global::TUnit.Mocks.Mock, global::MyApp.IGreeter + { + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IGreeterMock(global::MyApp.IGreeter mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } + + string global::MyApp.IGreeter.Greet(string name) => Object.Greet(name); + } +} + + +// ===== FILE SEPARATOR ===== + +// +#pragma warning disable +#nullable enable + +namespace TUnit.Mocks.Generated.MyApp +{ + file sealed class IGreeterMockImpl : global::MyApp.IGreeter, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject + { + private readonly global::TUnit.Mocks.MockEngine _engine; + + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + + internal IGreeterMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } + + public string Greet(string name) + { + return _engine.HandleCallWithReturn(0, "Greet", name, ""); + } + + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); + } + } + + internal static class IGreeterMockFactory + { + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() + { + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } + + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IGreeterMockImpl(engine); + engine.Raisable = impl; + var mock = new IGreeterMock(impl, engine); + return mock; + } + + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::MyApp.IGreeter' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IGreeterMockImpl(engine); + engine.Raisable = impl; + var mock = new IGreeterMock(impl, engine); + return mock; + } + } +} + + +// ===== FILE SEPARATOR ===== + +// +#pragma warning disable +#nullable enable + +namespace TUnit.Mocks.Generated.MyApp +{ + public static class MyApp_IGreeter_MockMemberExtensions + { + public static MyApp_IGreeter_Greet_M0_MockCall Greet(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg name) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { name.Matcher }; + return new MyApp_IGreeter_Greet_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Greet", matchers); + } + + public static MyApp_IGreeter_Greet_M0_MockCall Greet(this global::TUnit.Mocks.Mock mock, global::System.Func name) + { + global::TUnit.Mocks.Arguments.Arg __fa_name = name; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_name.Matcher }; + return new MyApp_IGreeter_Greet_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Greet", matchers); + } + } + + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public sealed class MyApp_IGreeter_Greet_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + { + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public MyApp_IGreeter_Greet_M0_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((string)args[0]!)); + return this; + } + + /// Execute a typed callback using the actual method parameters. + public MyApp_IGreeter_Greet_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; + } + + /// Configure a typed computed exception using the actual method parameters. + public MyApp_IGreeter_Greet_M0_MockCall Throws(global::System.Func exceptionFactory) + { + 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); + } +} + + +// ===== FILE SEPARATOR ===== + +// +#pragma warning disable +#nullable enable + +namespace TUnit.Mocks +{ + public static class MyApp_IGreeter_MockStaticExtension + { + extension(global::MyApp.IGreeter _) + { + public static global::TUnit.Mocks.Generated.MyApp.IGreeterMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + { + return (global::TUnit.Mocks.Generated.MyApp.IGreeterMock)global::TUnit.Mocks.Generated.MyApp.IGreeterMockFactory.CreateAutoMock(behavior); + } + } + } +} + + +// ===== FILE SEPARATOR ===== + +// +#pragma warning disable +#nullable enable + +namespace TUnit.Mocks.Generated; \ No newline at end of file diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/GenerateMock_Attribute_With_Concrete_Class.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/GenerateMock_Attribute_With_Concrete_Class.verified.txt index 499fccd908..17415160da 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/GenerateMock_Attribute_With_Concrete_Class.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/GenerateMock_Attribute_With_Concrete_Class.verified.txt @@ -2,62 +2,59 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class MyServiceMockImpl : global::MyService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class MyServiceMockImpl : global::MyService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] - internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine) : base() - { - _engine = engine; - } + [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] + internal MyServiceMockImpl(global::TUnit.Mocks.MockEngine engine) : base() + { + _engine = engine; + } - public override string GetValue() + public override string GetValue() + { + if (_engine.TryHandleCallWithReturn(0, "GetValue", global::System.Array.Empty(), "", out var __result)) { - if (_engine.TryHandleCallWithReturn(0, "GetValue", global::System.Array.Empty(), "", out var __result)) - { - return __result; - } - return base.GetValue(); + return __result; } + return base.GetValue(); + } - public override void DoWork() + public override void DoWork() + { + if (_engine.TryHandleCall(1, "DoWork", global::System.Array.Empty())) { - if (_engine.TryHandleCall(1, "DoWork", global::System.Array.Empty())) - { - return; - } - base.DoWork(); + return; } + base.DoWork(); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - file static class MyServicePartialMockFactory +file static class MyServicePartialMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new MyServiceMockImpl(engine); - engine.Raisable = impl; - var mock = new global::TUnit.Mocks.Mock(impl, engine); - return mock; - } + private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new MyServiceMockImpl(engine); + engine.Raisable = impl; + var mock = new global::TUnit.Mocks.Mock(impl, engine); + return mock; } } @@ -68,21 +65,18 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class MyService_MockMemberExtensions { - public static class MyService_MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall GetValue(this global::TUnit.Mocks.Mock mock) { - public static global::TUnit.Mocks.MockMethodCall GetValue(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetValue", matchers); - } + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetValue", matchers); + } - public static global::TUnit.Mocks.VoidMockMethodCall DoWork(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "DoWork", matchers); - } + public static global::TUnit.Mocks.VoidMockMethodCall DoWork(this global::TUnit.Mocks.Mock mock) + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "DoWork", matchers); } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_Extension_Discovery.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_Extension_Discovery.verified.txt index 2290973ecf..4fdd5c35f5 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_Extension_Discovery.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_Extension_Discovery.verified.txt @@ -2,20 +2,17 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IRepository_T_Mock : global::TUnit.Mocks.Mock>, global::IRepository { - public sealed class IRepository_T_Mock : global::TUnit.Mocks.Mock>, global::IRepository - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IRepository_T_Mock(global::IRepository mockObject, global::TUnit.Mocks.MockEngine> engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IRepository_T_Mock(global::IRepository mockObject, global::TUnit.Mocks.MockEngine> engine) + : base(mockObject, engine) { } - T global::IRepository.GetById(int id) => Object.GetById(id); + T global::IRepository.GetById(int id) => Object.GetById(id); - void global::IRepository.Save(T entity) - { - Object.Save(entity); - } + void global::IRepository.Save(T entity) + { + Object.Save(entity); } } @@ -26,58 +23,55 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IRepository_T_MockImpl : global::IRepository, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IRepository_T_MockImpl : global::IRepository, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine> _engine; - - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + private readonly global::TUnit.Mocks.MockEngine> _engine; - internal IRepository_T_MockImpl(global::TUnit.Mocks.MockEngine> engine) - { - _engine = engine; - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - public T GetById(int id) - { - return _engine.HandleCallWithReturn(0, "GetById", id, default!); - } + internal IRepository_T_MockImpl(global::TUnit.Mocks.MockEngine> engine) + { + _engine = engine; + } - public void Save(T entity) - { - _engine.HandleCall(1, "Save", entity); - } + public T GetById(int id) + { + return _engine.HandleCallWithReturn(0, "GetById", id, default!); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + public void Save(T entity) + { + _engine.HandleCall(1, "Save", entity); } - internal static class IRepository_T_MockFactory + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterOpenGenericFactory( - typeof(global::IRepository<>), - typeof(IRepository_T_MockImpl<>), - typeof(IRepository_T_Mock<>)); - } + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); + } +} - internal static global::TUnit.Mocks.Mock> CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine>(behavior); - var impl = new IRepository_T_MockImpl(engine); - engine.Raisable = impl; - var mock = new IRepository_T_Mock(impl, engine); - return mock; - } +internal static class IRepository_T_MockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() + { + global::TUnit.Mocks.MockRegistry.RegisterOpenGenericFactory( + typeof(global::IRepository<>), + typeof(IRepository_T_MockImpl<>), + typeof(IRepository_T_Mock<>)); + } + internal static global::TUnit.Mocks.Mock> CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine>(behavior); + var impl = new IRepository_T_MockImpl(engine); + engine.Raisable = impl; + var mock = new IRepository_T_Mock(impl, engine); + return mock; } + } @@ -87,206 +81,203 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IRepository_T__MockMemberExtensions { - public static class IRepository_T__MockMemberExtensions + public static IRepository_T__GetById_M0_MockCall GetById(this global::TUnit.Mocks.Mock> mock, global::TUnit.Mocks.Arguments.Arg id) { - public static IRepository_T__GetById_M0_MockCall GetById(this global::TUnit.Mocks.Mock> mock, global::TUnit.Mocks.Arguments.Arg id) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { id.Matcher }; - return new IRepository_T__GetById_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetById", matchers); - } - - public static IRepository_T__GetById_M0_MockCall GetById(this global::TUnit.Mocks.Mock> mock, global::System.Func id) - { - global::TUnit.Mocks.Arguments.Arg __fa_id = id; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_id.Matcher }; - return new IRepository_T__GetById_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetById", matchers); - } - - public static IRepository_T__Save_M1_MockCall Save(this global::TUnit.Mocks.Mock> mock, global::TUnit.Mocks.Arguments.Arg entity) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { entity.Matcher }; - return new IRepository_T__Save_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Save", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { id.Matcher }; + return new IRepository_T__GetById_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetById", matchers); + } - public static IRepository_T__Save_M1_MockCall Save(this global::TUnit.Mocks.Mock> mock, global::System.Func entity) - { - global::TUnit.Mocks.Arguments.Arg __fa_entity = entity; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_entity.Matcher }; - return new IRepository_T__Save_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Save", matchers); - } + public static IRepository_T__GetById_M0_MockCall GetById(this global::TUnit.Mocks.Mock> mock, global::System.Func id) + { + global::TUnit.Mocks.Arguments.Arg __fa_id = id; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_id.Matcher }; + return new IRepository_T__GetById_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetById", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IRepository_T__GetById_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public static IRepository_T__Save_M1_MockCall Save(this global::TUnit.Mocks.Mock> mock, global::TUnit.Mocks.Arguments.Arg entity) { - 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; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { entity.Matcher }; + return new IRepository_T__Save_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Save", matchers); + } - 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; - } + public static IRepository_T__Save_M1_MockCall Save(this global::TUnit.Mocks.Mock> mock, global::System.Func entity) + { + global::TUnit.Mocks.Arguments.Arg __fa_entity = entity; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_entity.Matcher }; + return new IRepository_T__Save_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Save", 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IRepository_T__GetById_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public IRepository_T__GetById_M0_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((int)args[0]!)); - return this; - } + 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(); + } - /// Execute a typed callback using the actual method parameters. - public IRepository_T__GetById_M0_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public IRepository_T__GetById_M0_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public IRepository_T__GetById_M0_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((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); + /// Execute a typed callback using the actual method parameters. + public IRepository_T__GetById_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IRepository_T__Save_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public IRepository_T__GetById_M0_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); + return this; + } - 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(); - } + // 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IRepository_T__Save_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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(); + } - /// - 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; } - - /// Execute a typed callback using the actual method parameters. - public IRepository_T__Save_M1_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + 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(); + } - /// Configure a typed computed exception using the actual method parameters. - public IRepository_T__Save_M1_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((T)args[0]!)); - return this; - } + [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; } + + /// Execute a typed callback using the actual method parameters. + public IRepository_T__Save_M1_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + 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); + /// Configure a typed computed exception using the actual method parameters. + public IRepository_T__Save_M1_MockCall Throws(global::System.Func exceptionFactory) + { + 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); } @@ -302,9 +293,9 @@ namespace TUnit.Mocks { extension(global::IRepository _) { - public static global::TUnit.Mocks.Generated.IRepository_T_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IRepository_T_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IRepository_T_Mock)global::TUnit.Mocks.Generated.IRepository_T_MockFactory.CreateAutoMock(behavior); + return (global::IRepository_T_Mock)global::IRepository_T_MockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Class_Type_Argument.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Class_Type_Argument.verified.txt index ae4a321a6b..a1f9d4e143 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Class_Type_Argument.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Class_Type_Argument.verified.txt @@ -2,7 +2,7 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated.Sandbox +namespace Sandbox { public sealed class IFoo_T_Mock : global::TUnit.Mocks.Mock>, global::Sandbox.IFoo { @@ -26,7 +26,7 @@ namespace TUnit.Mocks.Generated.Sandbox #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated.Sandbox +namespace Sandbox { file sealed class IFoo_T_MockImpl : global::Sandbox.IFoo, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { @@ -87,7 +87,7 @@ namespace TUnit.Mocks.Generated.Sandbox #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +namespace Sandbox { public static class Sandbox_IFoo_T__MockMemberExtensions { @@ -205,9 +205,9 @@ namespace TUnit.Mocks { extension(global::Sandbox.IFoo _) { - public static global::TUnit.Mocks.Generated.Sandbox.IFoo_T_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::Sandbox.IFoo_T_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.Sandbox.IFoo_T_Mock)global::TUnit.Mocks.Generated.Sandbox.IFoo_T_MockFactory.CreateAutoMock(behavior); + return (global::Sandbox.IFoo_T_Mock)global::Sandbox.IFoo_T_MockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Enum_Type_Argument.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Enum_Type_Argument.verified.txt index dd614a4b3b..4a38d47577 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Enum_Type_Argument.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Enum_Type_Argument.verified.txt @@ -2,7 +2,7 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated.Sandbox +namespace Sandbox { public sealed class IFoo_T_Mock : global::TUnit.Mocks.Mock>, global::Sandbox.IFoo { @@ -21,7 +21,7 @@ namespace TUnit.Mocks.Generated.Sandbox #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated.Sandbox +namespace Sandbox { file sealed class IFoo_T_MockImpl : global::Sandbox.IFoo, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { @@ -77,7 +77,7 @@ namespace TUnit.Mocks.Generated.Sandbox #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +namespace Sandbox { public static class Sandbox_IFoo_T__MockMemberExtensions { @@ -102,9 +102,9 @@ namespace TUnit.Mocks { extension(global::Sandbox.IFoo _) { - public static global::TUnit.Mocks.Generated.Sandbox.IFoo_T_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::Sandbox.IFoo_T_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.Sandbox.IFoo_T_Mock)global::TUnit.Mocks.Generated.Sandbox.IFoo_T_MockFactory.CreateAutoMock(behavior); + return (global::Sandbox.IFoo_T_Mock)global::Sandbox.IFoo_T_MockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Multiple_Non_Builtin_Type_Arguments.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Multiple_Non_Builtin_Type_Arguments.verified.txt index 19d12f1a61..f05cf9a4bf 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Multiple_Non_Builtin_Type_Arguments.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Multiple_Non_Builtin_Type_Arguments.verified.txt @@ -2,7 +2,7 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated.Sandbox +namespace Sandbox { public sealed class IMapper_TIn_TOut_Mock : global::TUnit.Mocks.Mock>, global::Sandbox.IMapper { @@ -21,7 +21,7 @@ namespace TUnit.Mocks.Generated.Sandbox #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated.Sandbox +namespace Sandbox { file sealed class IMapper_TIn_TOut_MockImpl : global::Sandbox.IMapper, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { @@ -77,7 +77,7 @@ namespace TUnit.Mocks.Generated.Sandbox #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +namespace Sandbox { public static class Sandbox_IMapper_TIn_TOut__MockMemberExtensions { @@ -199,9 +199,9 @@ namespace TUnit.Mocks { extension(global::Sandbox.IMapper _) { - public static global::TUnit.Mocks.Generated.Sandbox.IMapper_TIn_TOut_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::Sandbox.IMapper_TIn_TOut_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.Sandbox.IMapper_TIn_TOut_Mock)global::TUnit.Mocks.Generated.Sandbox.IMapper_TIn_TOut_MockFactory.CreateAutoMock(behavior); + return (global::Sandbox.IMapper_TIn_TOut_Mock)global::Sandbox.IMapper_TIn_TOut_MockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Nested_Generic_Type_Argument.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Nested_Generic_Type_Argument.verified.txt index b94ad71414..133ad10e61 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Nested_Generic_Type_Argument.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Nested_Generic_Type_Argument.verified.txt @@ -2,7 +2,7 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated.Sandbox +namespace Sandbox { public sealed class IProvider_T_Mock : global::TUnit.Mocks.Mock>, global::Sandbox.IProvider { @@ -21,7 +21,7 @@ namespace TUnit.Mocks.Generated.Sandbox #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated.Sandbox +namespace Sandbox { file sealed class IProvider_T_MockImpl : global::Sandbox.IProvider, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { @@ -77,7 +77,7 @@ namespace TUnit.Mocks.Generated.Sandbox #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +namespace Sandbox { public static class Sandbox_IProvider_T__MockMemberExtensions { @@ -102,9 +102,9 @@ namespace TUnit.Mocks { extension(global::Sandbox.IProvider _) { - public static global::TUnit.Mocks.Generated.Sandbox.IProvider_T_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::Sandbox.IProvider_T_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.Sandbox.IProvider_T_Mock)global::TUnit.Mocks.Generated.Sandbox.IProvider_T_MockFactory.CreateAutoMock(behavior); + return (global::Sandbox.IProvider_T_Mock)global::Sandbox.IProvider_T_MockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Nested_Namespace_Type_Argument.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Nested_Namespace_Type_Argument.verified.txt index 7df96c44ac..016ba92e5b 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Nested_Namespace_Type_Argument.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Generic_Interface_With_Nested_Namespace_Type_Argument.verified.txt @@ -2,7 +2,7 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated.Sandbox +namespace Sandbox { public sealed class IService_T_Mock : global::TUnit.Mocks.Mock>, global::Sandbox.IService { @@ -26,7 +26,7 @@ namespace TUnit.Mocks.Generated.Sandbox #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated.Sandbox +namespace Sandbox { file sealed class IService_T_MockImpl : global::Sandbox.IService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { @@ -87,7 +87,7 @@ namespace TUnit.Mocks.Generated.Sandbox #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +namespace Sandbox { public static class Sandbox_IService_T__MockMemberExtensions { @@ -205,9 +205,9 @@ namespace TUnit.Mocks { extension(global::Sandbox.IService _) { - public static global::TUnit.Mocks.Generated.Sandbox.IService_T_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::Sandbox.IService_T_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.Sandbox.IService_T_Mock)global::TUnit.Mocks.Generated.Sandbox.IService_T_MockFactory.CreateAutoMock(behavior); + return (global::Sandbox.IService_T_Mock)global::Sandbox.IService_T_MockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Directly_Inheriting_IEnumerable.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Directly_Inheriting_IEnumerable.verified.txt index a693ded5b9..bfcf4730e0 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Directly_Inheriting_IEnumerable.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Directly_Inheriting_IEnumerable.verified.txt @@ -2,25 +2,22 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class ICustomCollectionMock : global::TUnit.Mocks.Mock, global::ICustomCollection { - public sealed class ICustomCollectionMock : global::TUnit.Mocks.Mock, global::ICustomCollection - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal ICustomCollectionMock(global::ICustomCollection mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal ICustomCollectionMock(global::ICustomCollection mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - void global::ICustomCollection.Add(int item) - { - Object.Add(item); - } + void global::ICustomCollection.Add(int item) + { + Object.Add(item); + } - global::System.Collections.Generic.IEnumerator global::System.Collections.Generic.IEnumerable.GetEnumerator() => Object.GetEnumerator(); + global::System.Collections.Generic.IEnumerator global::System.Collections.Generic.IEnumerable.GetEnumerator() => Object.GetEnumerator(); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => ((global::System.Collections.IEnumerable)Object).GetEnumerator(); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => ((global::System.Collections.IEnumerable)Object).GetEnumerator(); - int global::ICustomCollection.Count { get => Object.Count; } - } + int global::ICustomCollection.Count { get => Object.Count; } } @@ -30,70 +27,67 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class ICustomCollectionMockImpl : global::ICustomCollection, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class ICustomCollectionMockImpl : global::ICustomCollection, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal ICustomCollectionMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal ICustomCollectionMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public void Add(int item) - { - _engine.HandleCall(1, "Add", item); - } + public void Add(int item) + { + _engine.HandleCall(1, "Add", item); + } - public global::System.Collections.Generic.IEnumerator GetEnumerator() - { - return _engine.HandleCallWithReturn>(2, "GetEnumerator", global::System.Array.Empty(), default!); - } + public global::System.Collections.Generic.IEnumerator GetEnumerator() + { + return _engine.HandleCallWithReturn>(2, "GetEnumerator", global::System.Array.Empty(), default!); + } - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - public int Count - { - get => _engine.HandleCallWithReturn(0, "get_Count", global::System.Array.Empty(), default); - } + public int Count + { + get => _engine.HandleCallWithReturn(0, "get_Count", global::System.Array.Empty(), default); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class ICustomCollectionMockFactory +internal static class ICustomCollectionMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new ICustomCollectionMockImpl(engine); - engine.Raisable = impl; - var mock = new ICustomCollectionMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new ICustomCollectionMockImpl(engine); + engine.Raisable = impl; + var mock = new ICustomCollectionMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::ICustomCollection' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new ICustomCollectionMockImpl(engine); - engine.Raisable = impl; - var mock = new ICustomCollectionMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::ICustomCollection' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new ICustomCollectionMockImpl(engine); + engine.Raisable = impl; + var mock = new ICustomCollectionMock(impl, engine); + return mock; } } @@ -104,115 +98,112 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class ICustomCollection_MockMemberExtensions { - public static class ICustomCollection_MockMemberExtensions + public static ICustomCollection_Add_M1_MockCall Add(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg item) { - public static ICustomCollection_Add_M1_MockCall Add(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg item) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { item.Matcher }; - return new ICustomCollection_Add_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Add", matchers); - } - - public static ICustomCollection_Add_M1_MockCall Add(this global::TUnit.Mocks.Mock mock, global::System.Func item) - { - global::TUnit.Mocks.Arguments.Arg __fa_item = item; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_item.Matcher }; - return new ICustomCollection_Add_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Add", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { item.Matcher }; + return new ICustomCollection_Add_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Add", matchers); + } - public static global::TUnit.Mocks.MockMethodCall> GetEnumerator(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall>(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "GetEnumerator", matchers); - } + public static ICustomCollection_Add_M1_MockCall Add(this global::TUnit.Mocks.Mock mock, global::System.Func item) + { + global::TUnit.Mocks.Arguments.Arg __fa_item = item; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_item.Matcher }; + return new ICustomCollection_Add_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Add", matchers); + } - extension(global::TUnit.Mocks.Mock mock) - { - public global::TUnit.Mocks.PropertyMockCall Count - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, 0, "Count", true, false); - } + public static global::TUnit.Mocks.MockMethodCall> GetEnumerator(this global::TUnit.Mocks.Mock mock) + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall>(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "GetEnumerator", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ICustomCollection_Add_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + extension(global::TUnit.Mocks.Mock mock) { - 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; + public global::TUnit.Mocks.PropertyMockCall Count + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, 0, "Count", true, false); + } +} - 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(); - } +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class ICustomCollection_Add_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - 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(); - } + 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(); + } - [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; - } + 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(); + } - /// - 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; } - - /// Execute a typed callback using the actual method parameters. - public ICustomCollection_Add_M1_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public ICustomCollection_Add_M1_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); - return this; - } + /// + 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; } + + /// Execute a typed callback using the actual method parameters. + public ICustomCollection_Add_M1_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + 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); + /// Configure a typed computed exception using the actual method parameters. + public ICustomCollection_Add_M1_MockCall Throws(global::System.Func exceptionFactory) + { + 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); } @@ -228,9 +219,9 @@ namespace TUnit.Mocks { extension(global::ICustomCollection _) { - public static global::TUnit.Mocks.Generated.ICustomCollectionMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::ICustomCollectionMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.ICustomCollectionMock)global::TUnit.Mocks.Generated.ICustomCollectionMockFactory.CreateAutoMock(behavior); + return (global::ICustomCollectionMock)global::ICustomCollectionMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_FluentUI_Shape_Nullable_Warnings.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_FluentUI_Shape_Nullable_Warnings.verified.txt index 799d390a67..000504d5c7 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_FluentUI_Shape_Nullable_Warnings.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_FluentUI_Shape_Nullable_Warnings.verified.txt @@ -2,16 +2,13 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IDialogReferenceMock : global::TUnit.Mocks.Mock, global::IDialogReference { - public sealed class IDialogReferenceMock : global::TUnit.Mocks.Mock, global::IDialogReference - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IDialogReferenceMock(global::IDialogReference mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IDialogReferenceMock(global::IDialogReference mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - global::System.Threading.Tasks.Task global::IDialogReference.GetReturnValueAsync() where T : default => Object.GetReturnValueAsync(); - } + global::System.Threading.Tasks.Task global::IDialogReference.GetReturnValueAsync() where T : default => Object.GetReturnValueAsync(); } @@ -21,71 +18,68 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IDialogReferenceMockImpl : global::IDialogReference, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IDialogReferenceMockImpl : global::IDialogReference, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IDialogReferenceMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IDialogReferenceMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public global::System.Threading.Tasks.Task GetReturnValueAsync() + public global::System.Threading.Tasks.Task GetReturnValueAsync() + { + try { - try - { - var __result = _engine.HandleCallWithReturn(0, "GetReturnValueAsync", global::System.Array.Empty(), default); - if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) - { - if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; - throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); - } - return global::System.Threading.Tasks.Task.FromResult(__result); - } - catch (global::System.Exception __ex) + var __result = _engine.HandleCallWithReturn(0, "GetReturnValueAsync", global::System.Array.Empty(), default); + if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) { - return global::System.Threading.Tasks.Task.FromException(__ex); + if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; + throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); } + return global::System.Threading.Tasks.Task.FromResult(__result); } - - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) + catch (global::System.Exception __ex) { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); + return global::System.Threading.Tasks.Task.FromException(__ex); } } - internal static class IDialogReferenceMockFactory + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); + } +} - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IDialogReferenceMockImpl(engine); - engine.Raisable = impl; - var mock = new IDialogReferenceMock(impl, engine); - return mock; - } +internal static class IDialogReferenceMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() + { + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IDialogReference' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IDialogReferenceMockImpl(engine); - engine.Raisable = impl; - var mock = new IDialogReferenceMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IDialogReferenceMockImpl(engine); + engine.Raisable = impl; + var mock = new IDialogReferenceMock(impl, engine); + return mock; + } + + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IDialogReference' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IDialogReferenceMockImpl(engine); + engine.Raisable = impl; + var mock = new IDialogReferenceMock(impl, engine); + return mock; } } @@ -96,15 +90,12 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IDialogReference_MockMemberExtensions { - public static class IDialogReference_MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall GetReturnValueAsync(this global::TUnit.Mocks.Mock mock) { - public static global::TUnit.Mocks.MockMethodCall GetReturnValueAsync(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetReturnValueAsync", matchers); - } + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetReturnValueAsync", matchers); } } @@ -121,9 +112,9 @@ namespace TUnit.Mocks { extension(global::IDialogReference _) { - public static global::TUnit.Mocks.Generated.IDialogReferenceMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IDialogReferenceMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IDialogReferenceMock)global::TUnit.Mocks.Generated.IDialogReferenceMockFactory.CreateAutoMock(behavior); + return (global::IDialogReferenceMock)global::IDialogReferenceMockFactory.CreateAutoMock(behavior); } } } @@ -136,20 +127,17 @@ namespace TUnit.Mocks #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IDialogServiceMock : global::TUnit.Mocks.Mock, global::IDialogService { - public sealed class IDialogServiceMock : global::TUnit.Mocks.Mock, global::IDialogService - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IDialogServiceMock(global::IDialogService mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IDialogServiceMock(global::IDialogService mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - global::System.Threading.Tasks.Task global::IDialogService.UpdateDialogAsync(string id, global::DialogParameters parameters) where TData : class => Object.UpdateDialogAsync(id, parameters); + global::System.Threading.Tasks.Task global::IDialogService.UpdateDialogAsync(string id, global::DialogParameters parameters) where TData : class => Object.UpdateDialogAsync(id, parameters); - global::System.Threading.Tasks.Task global::IDialogService.ShowDialogAsync(object data, global::DialogParameters parameters) => Object.ShowDialogAsync(data, parameters); + global::System.Threading.Tasks.Task global::IDialogService.ShowDialogAsync(object data, global::DialogParameters parameters) => Object.ShowDialogAsync(data, parameters); - event global::System.Action? global::IDialogService.OnShow { add => Object.OnShow += value; remove => Object.OnShow -= value; } - } + event global::System.Action? global::IDialogService.OnShow { add => Object.OnShow += value; remove => Object.OnShow -= value; } } @@ -159,28 +147,25 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public readonly struct IDialogService_MockEvents { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public readonly struct IDialogService_MockEvents - { - internal readonly global::TUnit.Mocks.MockEngine Engine; + internal readonly global::TUnit.Mocks.MockEngine Engine; - internal IDialogService_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; - } + internal IDialogService_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; +} - public static class IDialogService_MockEventsExtensions +public static class IDialogService_MockEventsExtensions +{ + extension(global::TUnit.Mocks.Mock mock) { - extension(global::TUnit.Mocks.Mock mock) - { - public IDialogService_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); - } + public IDialogService_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); + } - extension(IDialogService_MockEvents events) - { - public global::TUnit.Mocks.EventSubscriptionAccessor OnShow - => new(events.Engine, "OnShow"); - } + extension(IDialogService_MockEvents events) + { + public global::TUnit.Mocks.EventSubscriptionAccessor OnShow + => new(events.Engine, "OnShow"); } } @@ -191,119 +176,116 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IDialogServiceMockImpl : global::IDialogService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IDialogServiceMockImpl : global::IDialogService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IDialogServiceMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IDialogServiceMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public global::System.Threading.Tasks.Task UpdateDialogAsync(string id, global::DialogParameters parameters) where TData : class + public global::System.Threading.Tasks.Task UpdateDialogAsync(string id, global::DialogParameters parameters) where TData : class + { + try { - try - { - var __result = _engine.HandleCallWithReturn>(0, "UpdateDialogAsync", id, parameters, default, static __behavior => global::TUnit.Mocks.Generated.IDialogReferenceMockFactory.CreateAutoMock(__behavior)); - if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) - { - if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; - throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); - } - return global::System.Threading.Tasks.Task.FromResult(__result); - } - catch (global::System.Exception __ex) + var __result = _engine.HandleCallWithReturn>(0, "UpdateDialogAsync", id, parameters, default, static __behavior => global::IDialogReferenceMockFactory.CreateAutoMock(__behavior)); + if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) { - return global::System.Threading.Tasks.Task.FromException(__ex); + if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; + throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); } + return global::System.Threading.Tasks.Task.FromResult(__result); } + catch (global::System.Exception __ex) + { + return global::System.Threading.Tasks.Task.FromException(__ex); + } + } - public global::System.Threading.Tasks.Task ShowDialogAsync(object data, global::DialogParameters parameters) where TDialog : global::IDialogContentComponent + public global::System.Threading.Tasks.Task ShowDialogAsync(object data, global::DialogParameters parameters) where TDialog : global::IDialogContentComponent + { + try { - try - { - var __result = _engine.HandleCallWithReturn(1, "ShowDialogAsync", data, parameters, default!, static __behavior => global::TUnit.Mocks.Generated.IDialogReferenceMockFactory.CreateAutoMock(__behavior)); - if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) - { - if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; - throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); - } - return global::System.Threading.Tasks.Task.FromResult(__result); - } - catch (global::System.Exception __ex) + var __result = _engine.HandleCallWithReturn(1, "ShowDialogAsync", data, parameters, default!, static __behavior => global::IDialogReferenceMockFactory.CreateAutoMock(__behavior)); + if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) { - return global::System.Threading.Tasks.Task.FromException(__ex); + if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; + throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); } + return global::System.Threading.Tasks.Task.FromResult(__result); } - - private global::System.Action? _backing_OnShow; - - public event global::System.Action? OnShow + catch (global::System.Exception __ex) { - add { _backing_OnShow += value; _engine.RecordEventSubscription("OnShow", true); } - remove { _backing_OnShow -= value; _engine.RecordEventSubscription("OnShow", false); } + return global::System.Threading.Tasks.Task.FromException(__ex); } + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal void Raise_OnShow(global::IDialogReference arg1, global::System.Type? arg2, global::DialogParameters arg3, object arg4) - { - _backing_OnShow?.Invoke(arg1, arg2, arg3, arg4); - } + private global::System.Action? _backing_OnShow; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) + public event global::System.Action? OnShow + { + add { _backing_OnShow += value; _engine.RecordEventSubscription("OnShow", true); } + remove { _backing_OnShow -= value; _engine.RecordEventSubscription("OnShow", false); } + } + + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal void Raise_OnShow(global::IDialogReference arg1, global::System.Type? arg2, global::DialogParameters arg3, object arg4) + { + _backing_OnShow?.Invoke(arg1, arg2, arg3, arg4); + } + + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + switch (eventName) { - switch (eventName) - { - case "OnShow": + case "OnShow": + { + if (args is object?[] __argArray) { - if (args is object?[] __argArray) - { - Raise_OnShow((global::IDialogReference)__argArray[0]!, (global::System.Type?)__argArray[1]!, (global::DialogParameters)__argArray[2]!, (object)__argArray[3]!); - } - else - { - throw new global::System.ArgumentException($"Event 'OnShow' requires an object[] of arguments."); - } - break; + Raise_OnShow((global::IDialogReference)__argArray[0]!, (global::System.Type?)__argArray[1]!, (global::DialogParameters)__argArray[2]!, (object)__argArray[3]!); } - default: - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + else + { + throw new global::System.ArgumentException($"Event 'OnShow' requires an object[] of arguments."); + } + break; + } + default: + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } } +} - internal static class IDialogServiceMockFactory +internal static class IDialogServiceMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IDialogServiceMockImpl(engine); - engine.Raisable = impl; - var mock = new IDialogServiceMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IDialogServiceMockImpl(engine); + engine.Raisable = impl; + var mock = new IDialogServiceMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IDialogService' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IDialogServiceMockImpl(engine); - engine.Raisable = impl; - var mock = new IDialogServiceMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IDialogService' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IDialogServiceMockImpl(engine); + engine.Raisable = impl; + var mock = new IDialogServiceMock(impl, engine); + return mock; } } @@ -314,70 +296,67 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IDialogService_MockMemberExtensions { - public static class IDialogService_MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall UpdateDialogAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg id, global::TUnit.Mocks.Arguments.Arg> parameters) where TData : class { - public static global::TUnit.Mocks.MockMethodCall UpdateDialogAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg id, global::TUnit.Mocks.Arguments.Arg> parameters) where TData : class - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { id.Matcher, parameters.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "UpdateDialogAsync", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { id.Matcher, parameters.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "UpdateDialogAsync", matchers); + } - public static global::TUnit.Mocks.MockMethodCall UpdateDialogAsync(this global::TUnit.Mocks.Mock mock, global::System.Func id, global::TUnit.Mocks.Arguments.Arg> parameters) where TData : class - { - global::TUnit.Mocks.Arguments.Arg __fa_id = id; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_id.Matcher, parameters.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "UpdateDialogAsync", matchers); - } + public static global::TUnit.Mocks.MockMethodCall UpdateDialogAsync(this global::TUnit.Mocks.Mock mock, global::System.Func id, global::TUnit.Mocks.Arguments.Arg> parameters) where TData : class + { + global::TUnit.Mocks.Arguments.Arg __fa_id = id; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_id.Matcher, parameters.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "UpdateDialogAsync", matchers); + } - public static global::TUnit.Mocks.MockMethodCall UpdateDialogAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg id, global::System.Func, bool> parameters) where TData : class - { - global::TUnit.Mocks.Arguments.Arg> __fa_parameters = parameters; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { id.Matcher, __fa_parameters.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "UpdateDialogAsync", matchers); - } + public static global::TUnit.Mocks.MockMethodCall UpdateDialogAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg id, global::System.Func, bool> parameters) where TData : class + { + global::TUnit.Mocks.Arguments.Arg> __fa_parameters = parameters; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { id.Matcher, __fa_parameters.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "UpdateDialogAsync", matchers); + } - public static global::TUnit.Mocks.MockMethodCall UpdateDialogAsync(this global::TUnit.Mocks.Mock mock, global::System.Func id, global::System.Func, bool> parameters) where TData : class - { - global::TUnit.Mocks.Arguments.Arg __fa_id = id; - global::TUnit.Mocks.Arguments.Arg> __fa_parameters = parameters; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_id.Matcher, __fa_parameters.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "UpdateDialogAsync", matchers); - } + public static global::TUnit.Mocks.MockMethodCall UpdateDialogAsync(this global::TUnit.Mocks.Mock mock, global::System.Func id, global::System.Func, bool> parameters) where TData : class + { + global::TUnit.Mocks.Arguments.Arg __fa_id = id; + global::TUnit.Mocks.Arguments.Arg> __fa_parameters = parameters; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_id.Matcher, __fa_parameters.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "UpdateDialogAsync", matchers); + } - public static global::TUnit.Mocks.MockMethodCall ShowDialogAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg data, global::TUnit.Mocks.Arguments.Arg parameters) where TDialog : global::IDialogContentComponent - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { data.Matcher, parameters.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "ShowDialogAsync", matchers); - } + public static global::TUnit.Mocks.MockMethodCall ShowDialogAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg data, global::TUnit.Mocks.Arguments.Arg parameters) where TDialog : global::IDialogContentComponent + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { data.Matcher, parameters.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "ShowDialogAsync", matchers); + } - public static global::TUnit.Mocks.MockMethodCall ShowDialogAsync(this global::TUnit.Mocks.Mock mock, global::System.Func data, global::TUnit.Mocks.Arguments.Arg parameters) where TDialog : global::IDialogContentComponent - { - global::TUnit.Mocks.Arguments.Arg __fa_data = data; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_data.Matcher, parameters.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "ShowDialogAsync", matchers); - } + public static global::TUnit.Mocks.MockMethodCall ShowDialogAsync(this global::TUnit.Mocks.Mock mock, global::System.Func data, global::TUnit.Mocks.Arguments.Arg parameters) where TDialog : global::IDialogContentComponent + { + global::TUnit.Mocks.Arguments.Arg __fa_data = data; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_data.Matcher, parameters.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "ShowDialogAsync", matchers); + } - public static global::TUnit.Mocks.MockMethodCall ShowDialogAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg data, global::System.Func parameters) where TDialog : global::IDialogContentComponent - { - global::TUnit.Mocks.Arguments.Arg __fa_parameters = parameters; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { data.Matcher, __fa_parameters.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "ShowDialogAsync", matchers); - } + public static global::TUnit.Mocks.MockMethodCall ShowDialogAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg data, global::System.Func parameters) where TDialog : global::IDialogContentComponent + { + global::TUnit.Mocks.Arguments.Arg __fa_parameters = parameters; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { data.Matcher, __fa_parameters.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "ShowDialogAsync", matchers); + } - public static global::TUnit.Mocks.MockMethodCall ShowDialogAsync(this global::TUnit.Mocks.Mock mock, global::System.Func data, global::System.Func parameters) where TDialog : global::IDialogContentComponent - { - global::TUnit.Mocks.Arguments.Arg __fa_data = data; - global::TUnit.Mocks.Arguments.Arg __fa_parameters = parameters; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_data.Matcher, __fa_parameters.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "ShowDialogAsync", matchers); - } + public static global::TUnit.Mocks.MockMethodCall ShowDialogAsync(this global::TUnit.Mocks.Mock mock, global::System.Func data, global::System.Func parameters) where TDialog : global::IDialogContentComponent + { + global::TUnit.Mocks.Arguments.Arg __fa_data = data; + global::TUnit.Mocks.Arguments.Arg __fa_parameters = parameters; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_data.Matcher, __fa_parameters.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "ShowDialogAsync", matchers); + } - public static void RaiseOnShow(this global::TUnit.Mocks.Mock mock, global::IDialogReference arg1, global::System.Type? arg2, global::DialogParameters arg3, object arg4) - { - ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("OnShow", (object?)new object?[] { arg1, arg2, arg3, arg4 }); - } + public static void RaiseOnShow(this global::TUnit.Mocks.Mock mock, global::IDialogReference arg1, global::System.Type? arg2, global::DialogParameters arg3, object arg4) + { + ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("OnShow", (object?)new object?[] { arg1, arg2, arg3, arg4 }); } } @@ -394,9 +373,9 @@ namespace TUnit.Mocks { extension(global::IDialogService _) { - public static global::TUnit.Mocks.Generated.IDialogServiceMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IDialogServiceMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IDialogServiceMock)global::TUnit.Mocks.Generated.IDialogServiceMockFactory.CreateAutoMock(behavior); + return (global::IDialogServiceMock)global::IDialogServiceMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_IEnumerable_Generic.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_IEnumerable_Generic.verified.txt index 80be6d3926..59169fea9a 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_IEnumerable_Generic.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_IEnumerable_Generic.verified.txt @@ -2,18 +2,15 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class ITestEnum_T_Mock : global::TUnit.Mocks.Mock>, global::ITestEnum { - public sealed class ITestEnum_T_Mock : global::TUnit.Mocks.Mock>, global::ITestEnum - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal ITestEnum_T_Mock(global::ITestEnum mockObject, global::TUnit.Mocks.MockEngine> engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal ITestEnum_T_Mock(global::ITestEnum mockObject, global::TUnit.Mocks.MockEngine> engine) + : base(mockObject, engine) { } - global::System.Collections.Generic.IEnumerator global::System.Collections.Generic.IEnumerable.GetEnumerator() => Object.GetEnumerator(); + global::System.Collections.Generic.IEnumerator global::System.Collections.Generic.IEnumerable.GetEnumerator() => Object.GetEnumerator(); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => ((global::System.Collections.IEnumerable)Object).GetEnumerator(); - } + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => ((global::System.Collections.IEnumerable)Object).GetEnumerator(); } @@ -23,55 +20,52 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class ITestEnum_T_MockImpl : global::ITestEnum, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class ITestEnum_T_MockImpl : global::ITestEnum, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine> _engine; + private readonly global::TUnit.Mocks.MockEngine> _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal ITestEnum_T_MockImpl(global::TUnit.Mocks.MockEngine> engine) - { - _engine = engine; - } + internal ITestEnum_T_MockImpl(global::TUnit.Mocks.MockEngine> engine) + { + _engine = engine; + } - public global::System.Collections.Generic.IEnumerator GetEnumerator() - { - return _engine.HandleCallWithReturn>(0, "GetEnumerator", global::System.Array.Empty(), default!); - } + public global::System.Collections.Generic.IEnumerator GetEnumerator() + { + return _engine.HandleCallWithReturn>(0, "GetEnumerator", global::System.Array.Empty(), default!); + } - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class ITestEnum_T_MockFactory +internal static class ITestEnum_T_MockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterOpenGenericFactory( - typeof(global::ITestEnum<>), - typeof(ITestEnum_T_MockImpl<>), - typeof(ITestEnum_T_Mock<>)); - } - - internal static global::TUnit.Mocks.Mock> CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine>(behavior); - var impl = new ITestEnum_T_MockImpl(engine); - engine.Raisable = impl; - var mock = new ITestEnum_T_Mock(impl, engine); - return mock; - } + global::TUnit.Mocks.MockRegistry.RegisterOpenGenericFactory( + typeof(global::ITestEnum<>), + typeof(ITestEnum_T_MockImpl<>), + typeof(ITestEnum_T_Mock<>)); + } + internal static global::TUnit.Mocks.Mock> CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine>(behavior); + var impl = new ITestEnum_T_MockImpl(engine); + engine.Raisable = impl; + var mock = new ITestEnum_T_Mock(impl, engine); + return mock; } + } @@ -81,15 +75,12 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class ITestEnum_T__MockMemberExtensions { - public static class ITestEnum_T__MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall> GetEnumerator(this global::TUnit.Mocks.Mock> mock) { - public static global::TUnit.Mocks.MockMethodCall> GetEnumerator(this global::TUnit.Mocks.Mock> mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall>(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetEnumerator", matchers); - } + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall>(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetEnumerator", matchers); } } @@ -106,9 +97,9 @@ namespace TUnit.Mocks { extension(global::ITestEnum _) { - public static global::TUnit.Mocks.Generated.ITestEnum_T_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::ITestEnum_T_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.ITestEnum_T_Mock)global::TUnit.Mocks.Generated.ITestEnum_T_MockFactory.CreateAutoMock(behavior); + return (global::ITestEnum_T_Mock)global::ITestEnum_T_MockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_Multiple_Interfaces.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_Multiple_Interfaces.verified.txt index a703d102b4..1323d592c6 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_Multiple_Interfaces.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_Multiple_Interfaces.verified.txt @@ -2,25 +2,22 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IReadWriterMock : global::TUnit.Mocks.Mock, global::IReadWriter { - public sealed class IReadWriterMock : global::TUnit.Mocks.Mock, global::IReadWriter - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IReadWriterMock(global::IReadWriter mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IReadWriterMock(global::IReadWriter mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - void global::IReadWriter.Flush() - { - Object.Flush(); - } + void global::IReadWriter.Flush() + { + Object.Flush(); + } - string global::IReader.Read() => Object.Read(); + string global::IReader.Read() => Object.Read(); - void global::IWriter.Write(string data) - { - Object.Write(data); - } + void global::IWriter.Write(string data) + { + Object.Write(data); } } @@ -31,68 +28,65 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IReadWriterMockImpl : global::IReadWriter, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IReadWriterMockImpl : global::IReadWriter, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IReadWriterMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IReadWriterMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public void Flush() - { - _engine.HandleCall(0, "Flush", global::System.Array.Empty()); - } + public void Flush() + { + _engine.HandleCall(0, "Flush", global::System.Array.Empty()); + } - public string Read() - { - return _engine.HandleCallWithReturn(1, "Read", global::System.Array.Empty(), ""); - } + public string Read() + { + return _engine.HandleCallWithReturn(1, "Read", global::System.Array.Empty(), ""); + } - public void Write(string data) - { - _engine.HandleCall(2, "Write", data); - } + public void Write(string data) + { + _engine.HandleCall(2, "Write", data); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class IReadWriterMockFactory +internal static class IReadWriterMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IReadWriterMockImpl(engine); - engine.Raisable = impl; - var mock = new IReadWriterMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IReadWriterMockImpl(engine); + engine.Raisable = impl; + var mock = new IReadWriterMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IReadWriter' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IReadWriterMockImpl(engine); - engine.Raisable = impl; - var mock = new IReadWriterMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IReadWriter' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IReadWriterMockImpl(engine); + engine.Raisable = impl; + var mock = new IReadWriterMock(impl, engine); + return mock; } } @@ -103,115 +97,112 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IReadWriter_MockMemberExtensions { - public static class IReadWriter_MockMemberExtensions + public static global::TUnit.Mocks.VoidMockMethodCall Flush(this global::TUnit.Mocks.Mock mock) { - public static global::TUnit.Mocks.VoidMockMethodCall Flush(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Flush", matchers); - } - - public static global::TUnit.Mocks.MockMethodCall Read(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Read", matchers); - } + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Flush", matchers); + } - public static IReadWriter_Write_M2_MockCall Write(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg data) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { data.Matcher }; - return new IReadWriter_Write_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Write", matchers); - } + public static global::TUnit.Mocks.MockMethodCall Read(this global::TUnit.Mocks.Mock mock) + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Read", matchers); + } - public static IReadWriter_Write_M2_MockCall Write(this global::TUnit.Mocks.Mock mock, global::System.Func data) - { - global::TUnit.Mocks.Arguments.Arg __fa_data = data; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_data.Matcher }; - return new IReadWriter_Write_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Write", matchers); - } + public static IReadWriter_Write_M2_MockCall Write(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg data) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { data.Matcher }; + return new IReadWriter_Write_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Write", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IReadWriter_Write_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public static IReadWriter_Write_M2_MockCall Write(this global::TUnit.Mocks.Mock mock, global::System.Func data) { - 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; + global::TUnit.Mocks.Arguments.Arg __fa_data = data; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_data.Matcher }; + return new IReadWriter_Write_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Write", matchers); + } +} - 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(); - } +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IReadWriter_Write_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - 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(); - } + 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(); + } - [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; - } + 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(); + } - /// - 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; } - - /// Execute a typed callback using the actual method parameters. - public IReadWriter_Write_M2_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public IReadWriter_Write_M2_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); - return this; - } + /// + 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; } + + /// Execute a typed callback using the actual method parameters. + public IReadWriter_Write_M2_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + 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); + /// Configure a typed computed exception using the actual method parameters. + public IReadWriter_Write_M2_MockCall Throws(global::System.Func exceptionFactory) + { + 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); } @@ -227,9 +218,9 @@ namespace TUnit.Mocks { extension(global::IReadWriter _) { - public static global::TUnit.Mocks.Generated.IReadWriterMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IReadWriterMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IReadWriterMock)global::TUnit.Mocks.Generated.IReadWriterMockFactory.CreateAutoMock(behavior); + return (global::IReadWriterMock)global::IReadWriterMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_Nested_Generic_IEnumerable.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_Nested_Generic_IEnumerable.verified.txt index 30e2dbaab7..d9c5c1b8b9 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_Nested_Generic_IEnumerable.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_Inheriting_Nested_Generic_IEnumerable.verified.txt @@ -2,22 +2,19 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IPagedResult_T_Mock : global::TUnit.Mocks.Mock>, global::IPagedResult { - public sealed class IPagedResult_T_Mock : global::TUnit.Mocks.Mock>, global::IPagedResult - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IPagedResult_T_Mock(global::IPagedResult mockObject, global::TUnit.Mocks.MockEngine> engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IPagedResult_T_Mock(global::IPagedResult mockObject, global::TUnit.Mocks.MockEngine> engine) + : base(mockObject, engine) { } - global::System.Collections.Generic.IEnumerator global::System.Collections.Generic.IEnumerable.GetEnumerator() => Object.GetEnumerator(); + global::System.Collections.Generic.IEnumerator global::System.Collections.Generic.IEnumerable.GetEnumerator() => Object.GetEnumerator(); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => ((global::System.Collections.IEnumerable)Object).GetEnumerator(); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => ((global::System.Collections.IEnumerable)Object).GetEnumerator(); - int global::IPagedResult.TotalCount { get => Object.TotalCount; } + int global::IPagedResult.TotalCount { get => Object.TotalCount; } - int global::IPagedResult.PageSize { get => Object.PageSize; } - } + int global::IPagedResult.PageSize { get => Object.PageSize; } } @@ -27,65 +24,62 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IPagedResult_T_MockImpl : global::IPagedResult, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IPagedResult_T_MockImpl : global::IPagedResult, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine> _engine; + private readonly global::TUnit.Mocks.MockEngine> _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - - internal IPagedResult_T_MockImpl(global::TUnit.Mocks.MockEngine> engine) - { - _engine = engine; - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - public global::System.Collections.Generic.IEnumerator GetEnumerator() - { - return _engine.HandleCallWithReturn>(2, "GetEnumerator", global::System.Array.Empty(), default!); - } + internal IPagedResult_T_MockImpl(global::TUnit.Mocks.MockEngine> engine) + { + _engine = engine; + } - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + public global::System.Collections.Generic.IEnumerator GetEnumerator() + { + return _engine.HandleCallWithReturn>(2, "GetEnumerator", global::System.Array.Empty(), default!); + } - public int TotalCount - { - get => _engine.HandleCallWithReturn(0, "get_TotalCount", global::System.Array.Empty(), default); - } + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - public int PageSize - { - get => _engine.HandleCallWithReturn(1, "get_PageSize", global::System.Array.Empty(), default); - } + public int TotalCount + { + get => _engine.HandleCallWithReturn(0, "get_TotalCount", global::System.Array.Empty(), default); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + public int PageSize + { + get => _engine.HandleCallWithReturn(1, "get_PageSize", global::System.Array.Empty(), default); } - internal static class IPagedResult_T_MockFactory + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterOpenGenericFactory( - typeof(global::IPagedResult<>), - typeof(IPagedResult_T_MockImpl<>), - typeof(IPagedResult_T_Mock<>)); - } + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); + } +} - internal static global::TUnit.Mocks.Mock> CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine>(behavior); - var impl = new IPagedResult_T_MockImpl(engine); - engine.Raisable = impl; - var mock = new IPagedResult_T_Mock(impl, engine); - return mock; - } +internal static class IPagedResult_T_MockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() + { + global::TUnit.Mocks.MockRegistry.RegisterOpenGenericFactory( + typeof(global::IPagedResult<>), + typeof(IPagedResult_T_MockImpl<>), + typeof(IPagedResult_T_Mock<>)); + } + internal static global::TUnit.Mocks.Mock> CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine>(behavior); + var impl = new IPagedResult_T_MockImpl(engine); + engine.Raisable = impl; + var mock = new IPagedResult_T_Mock(impl, engine); + return mock; } + } @@ -95,24 +89,21 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IPagedResult_T__MockMemberExtensions { - public static class IPagedResult_T__MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall> GetEnumerator(this global::TUnit.Mocks.Mock> mock) { - public static global::TUnit.Mocks.MockMethodCall> GetEnumerator(this global::TUnit.Mocks.Mock> mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall>(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "GetEnumerator", matchers); - } + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall>(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "GetEnumerator", matchers); + } - extension(global::TUnit.Mocks.Mock> mock) - { - public global::TUnit.Mocks.PropertyMockCall TotalCount - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, 0, "TotalCount", true, false); + extension(global::TUnit.Mocks.Mock> mock) + { + public global::TUnit.Mocks.PropertyMockCall TotalCount + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, 0, "TotalCount", true, false); - public global::TUnit.Mocks.PropertyMockCall PageSize - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, 0, "PageSize", true, false); - } + public global::TUnit.Mocks.PropertyMockCall PageSize + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, 0, "PageSize", true, false); } } @@ -129,9 +120,9 @@ namespace TUnit.Mocks { extension(global::IPagedResult _) { - public static global::TUnit.Mocks.Generated.IPagedResult_T_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IPagedResult_T_Mock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IPagedResult_T_Mock)global::TUnit.Mocks.Generated.IPagedResult_T_MockFactory.CreateAutoMock(behavior); + return (global::IPagedResult_T_Mock)global::IPagedResult_T_MockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Async_Methods.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Async_Methods.verified.txt index 47e00bfe8e..2dcaf4859b 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Async_Methods.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Async_Methods.verified.txt @@ -2,22 +2,19 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IAsyncServiceMock : global::TUnit.Mocks.Mock, global::IAsyncService { - public sealed class IAsyncServiceMock : global::TUnit.Mocks.Mock, global::IAsyncService - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IAsyncServiceMock(global::IAsyncService mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IAsyncServiceMock(global::IAsyncService mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - global::System.Threading.Tasks.Task global::IAsyncService.GetValueAsync(string key) => Object.GetValueAsync(key); + global::System.Threading.Tasks.Task global::IAsyncService.GetValueAsync(string key) => Object.GetValueAsync(key); - global::System.Threading.Tasks.Task global::IAsyncService.DoWorkAsync() => Object.DoWorkAsync(); + global::System.Threading.Tasks.Task global::IAsyncService.DoWorkAsync() => Object.DoWorkAsync(); - global::System.Threading.Tasks.ValueTask global::IAsyncService.ComputeAsync(int input) => Object.ComputeAsync(input); + global::System.Threading.Tasks.ValueTask global::IAsyncService.ComputeAsync(int input) => Object.ComputeAsync(input); - global::System.Threading.Tasks.ValueTask global::IAsyncService.InitializeAsync(global::System.Threading.CancellationToken ct) => Object.InitializeAsync(ct); - } + global::System.Threading.Tasks.ValueTask global::IAsyncService.InitializeAsync(global::System.Threading.CancellationToken ct) => Object.InitializeAsync(ct); } @@ -27,125 +24,122 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IAsyncServiceMockImpl : global::IAsyncService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IAsyncServiceMockImpl : global::IAsyncService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IAsyncServiceMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IAsyncServiceMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public global::System.Threading.Tasks.Task GetValueAsync(string key) + public global::System.Threading.Tasks.Task GetValueAsync(string key) + { + try { - try - { - var __result = _engine.HandleCallWithReturn(0, "GetValueAsync", key, ""); - if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) - { - if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; - throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); - } - return global::System.Threading.Tasks.Task.FromResult(__result); - } - catch (global::System.Exception __ex) + var __result = _engine.HandleCallWithReturn(0, "GetValueAsync", key, ""); + if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) { - return global::System.Threading.Tasks.Task.FromException(__ex); + if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; + throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); } + return global::System.Threading.Tasks.Task.FromResult(__result); } - - public global::System.Threading.Tasks.Task DoWorkAsync() + catch (global::System.Exception __ex) { - try - { - _engine.HandleCall(1, "DoWorkAsync", global::System.Array.Empty()); - if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) - { - if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; - throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); - } - return global::System.Threading.Tasks.Task.CompletedTask; - } - catch (global::System.Exception __ex) - { - return global::System.Threading.Tasks.Task.FromException(__ex); - } + return global::System.Threading.Tasks.Task.FromException(__ex); } + } - public global::System.Threading.Tasks.ValueTask ComputeAsync(int input) + public global::System.Threading.Tasks.Task DoWorkAsync() + { + try { - try + _engine.HandleCall(1, "DoWorkAsync", global::System.Array.Empty()); + if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) { - var __result = _engine.HandleCallWithReturn(2, "ComputeAsync", input, default); - if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) - { - if (__rawAsync is global::System.Threading.Tasks.ValueTask __typedAsync) return __typedAsync; - throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.ValueTask but got {__rawAsync?.GetType().Name ?? "null"}"); - } - return new global::System.Threading.Tasks.ValueTask(__result); - } - catch (global::System.Exception __ex) - { - return new global::System.Threading.Tasks.ValueTask(global::System.Threading.Tasks.Task.FromException(__ex)); + if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; + throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); } + return global::System.Threading.Tasks.Task.CompletedTask; } + catch (global::System.Exception __ex) + { + return global::System.Threading.Tasks.Task.FromException(__ex); + } + } - public global::System.Threading.Tasks.ValueTask InitializeAsync(global::System.Threading.CancellationToken ct) + public global::System.Threading.Tasks.ValueTask ComputeAsync(int input) + { + try { - try + var __result = _engine.HandleCallWithReturn(2, "ComputeAsync", input, default); + if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) { - _engine.HandleCall(3, "InitializeAsync", ct); - if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) - { - if (__rawAsync is global::System.Threading.Tasks.ValueTask __typedAsync) return __typedAsync; - throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.ValueTask but got {__rawAsync?.GetType().Name ?? "null"}"); - } - return default(global::System.Threading.Tasks.ValueTask); - } - catch (global::System.Exception __ex) - { - return new global::System.Threading.Tasks.ValueTask(global::System.Threading.Tasks.Task.FromException(__ex)); + if (__rawAsync is global::System.Threading.Tasks.ValueTask __typedAsync) return __typedAsync; + throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.ValueTask but got {__rawAsync?.GetType().Name ?? "null"}"); } + return new global::System.Threading.Tasks.ValueTask(__result); } - - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) + catch (global::System.Exception __ex) { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); + return new global::System.Threading.Tasks.ValueTask(global::System.Threading.Tasks.Task.FromException(__ex)); } } - internal static class IAsyncServiceMockFactory + public global::System.Threading.Tasks.ValueTask InitializeAsync(global::System.Threading.CancellationToken ct) { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() + try { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + _engine.HandleCall(3, "InitializeAsync", ct); + if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) + { + if (__rawAsync is global::System.Threading.Tasks.ValueTask __typedAsync) return __typedAsync; + throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.ValueTask but got {__rawAsync?.GetType().Name ?? "null"}"); + } + return default(global::System.Threading.Tasks.ValueTask); } - - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + catch (global::System.Exception __ex) { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IAsyncServiceMockImpl(engine); - engine.Raisable = impl; - var mock = new IAsyncServiceMock(impl, engine); - return mock; + return new global::System.Threading.Tasks.ValueTask(global::System.Threading.Tasks.Task.FromException(__ex)); } + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IAsyncService' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IAsyncServiceMockImpl(engine); - engine.Raisable = impl; - var mock = new IAsyncServiceMock(impl, engine); - return mock; - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); + } +} + +internal static class IAsyncServiceMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() + { + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } + + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IAsyncServiceMockImpl(engine); + engine.Raisable = impl; + var mock = new IAsyncServiceMock(impl, engine); + return mock; + } + + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IAsyncService' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IAsyncServiceMockImpl(engine); + engine.Raisable = impl; + var mock = new IAsyncServiceMock(impl, engine); + return mock; } } @@ -156,419 +150,416 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IAsyncService_MockMemberExtensions { - public static class IAsyncService_MockMemberExtensions + public static IAsyncService_GetValueAsync_M0_MockCall GetValueAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg key) { - public static IAsyncService_GetValueAsync_M0_MockCall GetValueAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg key) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { key.Matcher }; - return new IAsyncService_GetValueAsync_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetValueAsync", matchers); - } - - public static IAsyncService_GetValueAsync_M0_MockCall GetValueAsync(this global::TUnit.Mocks.Mock mock, global::System.Func key) - { - global::TUnit.Mocks.Arguments.Arg __fa_key = key; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_key.Matcher }; - return new IAsyncService_GetValueAsync_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetValueAsync", matchers); - } - - public static IAsyncService_DoWorkAsync_M1_MockCall DoWorkAsync(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new IAsyncService_DoWorkAsync_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "DoWorkAsync", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { key.Matcher }; + return new IAsyncService_GetValueAsync_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetValueAsync", matchers); + } - public static IAsyncService_ComputeAsync_M2_MockCall ComputeAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg input) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { input.Matcher }; - return new IAsyncService_ComputeAsync_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "ComputeAsync", matchers); - } + public static IAsyncService_GetValueAsync_M0_MockCall GetValueAsync(this global::TUnit.Mocks.Mock mock, global::System.Func key) + { + global::TUnit.Mocks.Arguments.Arg __fa_key = key; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_key.Matcher }; + return new IAsyncService_GetValueAsync_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetValueAsync", matchers); + } - public static IAsyncService_ComputeAsync_M2_MockCall ComputeAsync(this global::TUnit.Mocks.Mock mock, global::System.Func input) - { - global::TUnit.Mocks.Arguments.Arg __fa_input = input; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_input.Matcher }; - return new IAsyncService_ComputeAsync_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "ComputeAsync", matchers); - } + public static IAsyncService_DoWorkAsync_M1_MockCall DoWorkAsync(this global::TUnit.Mocks.Mock mock) + { + var matchers = global::System.Array.Empty(); + return new IAsyncService_DoWorkAsync_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "DoWorkAsync", matchers); + } - public static IAsyncService_InitializeAsync_M3_MockCall InitializeAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg ct) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { ct.Matcher }; - return new IAsyncService_InitializeAsync_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "InitializeAsync", matchers); - } + public static IAsyncService_ComputeAsync_M2_MockCall ComputeAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg input) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { input.Matcher }; + return new IAsyncService_ComputeAsync_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "ComputeAsync", matchers); + } - public static IAsyncService_InitializeAsync_M3_MockCall InitializeAsync(this global::TUnit.Mocks.Mock mock, global::System.Func ct) - { - global::TUnit.Mocks.Arguments.Arg __fa_ct = ct; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_ct.Matcher }; - return new IAsyncService_InitializeAsync_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "InitializeAsync", matchers); - } + public static IAsyncService_ComputeAsync_M2_MockCall ComputeAsync(this global::TUnit.Mocks.Mock mock, global::System.Func input) + { + global::TUnit.Mocks.Arguments.Arg __fa_input = input; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_input.Matcher }; + return new IAsyncService_ComputeAsync_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "ComputeAsync", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IAsyncService_GetValueAsync_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public static IAsyncService_InitializeAsync_M3_MockCall InitializeAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg ct) { - 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; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { ct.Matcher }; + return new IAsyncService_InitializeAsync_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "InitializeAsync", matchers); + } - 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; - } + public static IAsyncService_InitializeAsync_M3_MockCall InitializeAsync(this global::TUnit.Mocks.Mock mock, global::System.Func ct) + { + global::TUnit.Mocks.Arguments.Arg __fa_ct = ct; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_ct.Matcher }; + return new IAsyncService_InitializeAsync_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "InitializeAsync", 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IAsyncService_GetValueAsync_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// 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; } - /// Return a pre-built Task from a factory, invoked on each call. - public IAsyncService_GetValueAsync_M0_MockCall ReturnsAsync(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } - - /// Configure a typed computed return value using the actual method parameters. - public IAsyncService_GetValueAsync_M0_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((string)args[0]!)); - return this; - } + 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(); + } - /// Configure a typed computed async return value using the actual method parameters. - public IAsyncService_GetValueAsync_M0_MockCall ReturnsAsync(global::System.Func> factory) - { - EnsureSetup().ReturnsRaw(args => (object?)factory((string)args[0]!)); - return this; - } + [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; + } - /// Execute a typed callback using the actual method parameters. - public IAsyncService_GetValueAsync_M0_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + /// + 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; } + + /// 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; } + /// Return a pre-built Task from a factory, invoked on each call. + public IAsyncService_GetValueAsync_M0_MockCall ReturnsAsync(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } + + /// Configure a typed computed return value using the actual method parameters. + public IAsyncService_GetValueAsync_M0_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((string)args[0]!)); + return this; + } - /// Configure a typed computed exception using the actual method parameters. - public IAsyncService_GetValueAsync_M0_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); - return this; - } + /// Configure a typed computed async return value using the actual method parameters. + public IAsyncService_GetValueAsync_M0_MockCall ReturnsAsync(global::System.Func> factory) + { + EnsureSetup().ReturnsRaw(args => (object?)factory((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); + /// Execute a typed callback using the actual method parameters. + public IAsyncService_GetValueAsync_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IAsyncService_DoWorkAsync_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public IAsyncService_GetValueAsync_M0_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); + return this; + } - 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(); - } + // 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IAsyncService_DoWorkAsync_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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(); + } - /// - 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; } - - /// 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; } - /// Return a pre-built Task from a factory, invoked on each call. - public IAsyncService_DoWorkAsync_M1_MockCall ReturnsAsync(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); + 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IAsyncService_ComputeAsync_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] + private global::TUnit.Mocks.Setup.VoidMethodSetupBuilder EnsureSetupSlow() { - 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; + 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; + } - 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; - } + /// + 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; } + + /// 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; } + /// Return a pre-built Task from a factory, invoked on each call. + public IAsyncService_DoWorkAsync_M1_MockCall ReturnsAsync(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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IAsyncService_ComputeAsync_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// 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, - /// use the factory overload if the mock will be called multiple times, or ensure the ValueTask is backed by a Task. - public IAsyncService_ComputeAsync_M2_MockCall ReturnsAsync(global::System.Threading.Tasks.ValueTask task) { EnsureSetup().ReturnsRaw(task); return this; } - /// Return a pre-built ValueTask from a factory, invoked on each call. - public IAsyncService_ComputeAsync_M2_MockCall ReturnsAsync(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } - - /// Configure a typed computed return value using the actual method parameters. - public IAsyncService_ComputeAsync_M2_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((int)args[0]!)); - return this; - } + 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(); + } - /// Configure a typed computed async return value using the actual method parameters. - public IAsyncService_ComputeAsync_M2_MockCall ReturnsAsync(global::System.Func> factory) - { - EnsureSetup().ReturnsRaw(args => (object?)factory((int)args[0]!)); - return this; - } + [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; + } - /// Execute a typed callback using the actual method parameters. - public IAsyncService_ComputeAsync_M2_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + /// + 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; } + + /// 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, + /// use the factory overload if the mock will be called multiple times, or ensure the ValueTask is backed by a Task. + public IAsyncService_ComputeAsync_M2_MockCall ReturnsAsync(global::System.Threading.Tasks.ValueTask task) { EnsureSetup().ReturnsRaw(task); return this; } + /// Return a pre-built ValueTask from a factory, invoked on each call. + public IAsyncService_ComputeAsync_M2_MockCall ReturnsAsync(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } + + /// Configure a typed computed return value using the actual method parameters. + public IAsyncService_ComputeAsync_M2_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((int)args[0]!)); + return this; + } - /// Configure a typed computed exception using the actual method parameters. - public IAsyncService_ComputeAsync_M2_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); - return this; - } + /// Configure a typed computed async return value using the actual method parameters. + public IAsyncService_ComputeAsync_M2_MockCall ReturnsAsync(global::System.Func> factory) + { + EnsureSetup().ReturnsRaw(args => (object?)factory((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); + /// Execute a typed callback using the actual method parameters. + public IAsyncService_ComputeAsync_M2_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IAsyncService_InitializeAsync_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public IAsyncService_ComputeAsync_M2_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); + return this; + } - 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(); - } + // 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IAsyncService_InitializeAsync_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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(); + } - /// - 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; } - - /// 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, - /// use the factory overload if the mock will be called multiple times, or ensure the ValueTask is backed by a Task. - public IAsyncService_InitializeAsync_M3_MockCall ReturnsAsync(global::System.Threading.Tasks.ValueTask task) { EnsureSetup().ReturnsRaw(task); return this; } - /// Return a pre-built ValueTask from a factory, invoked on each call. - public IAsyncService_InitializeAsync_M3_MockCall ReturnsAsync(global::System.Func taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } - - /// Execute a typed callback using the actual method parameters. - public IAsyncService_InitializeAsync_M3_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + 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(); + } - /// Configure a typed computed exception using the actual method parameters. - public IAsyncService_InitializeAsync_M3_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((global::System.Threading.CancellationToken)args[0]!)); - return this; - } + [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; + } - // 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); + /// + 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; } + + /// 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, + /// use the factory overload if the mock will be called multiple times, or ensure the ValueTask is backed by a Task. + public IAsyncService_InitializeAsync_M3_MockCall ReturnsAsync(global::System.Threading.Tasks.ValueTask task) { EnsureSetup().ReturnsRaw(task); return this; } + /// Return a pre-built ValueTask from a factory, invoked on each call. + public IAsyncService_InitializeAsync_M3_MockCall ReturnsAsync(global::System.Func taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } + + /// Execute a typed callback using the actual method parameters. + public IAsyncService_InitializeAsync_M3_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } + + /// Configure a typed computed exception using the actual method parameters. + public IAsyncService_InitializeAsync_M3_MockCall Throws(global::System.Func exceptionFactory) + { + 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); } @@ -584,9 +575,9 @@ namespace TUnit.Mocks { extension(global::IAsyncService _) { - public static global::TUnit.Mocks.Generated.IAsyncServiceMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IAsyncServiceMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IAsyncServiceMock)global::TUnit.Mocks.Generated.IAsyncServiceMockFactory.CreateAutoMock(behavior); + return (global::IAsyncServiceMock)global::IAsyncServiceMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Events.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Events.verified.txt index 5b85cf06c2..f0d8ea3e51 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Events.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Events.verified.txt @@ -2,21 +2,18 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class INotifierMock : global::TUnit.Mocks.Mock, global::INotifier { - public sealed class INotifierMock : global::TUnit.Mocks.Mock, global::INotifier - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal INotifierMock(global::INotifier mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } - - void global::INotifier.Notify(string message) - { - Object.Notify(message); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal INotifierMock(global::INotifier mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - event global::System.EventHandler global::INotifier.ItemAdded { add => Object.ItemAdded += value; remove => Object.ItemAdded -= value; } + void global::INotifier.Notify(string message) + { + Object.Notify(message); } + + event global::System.EventHandler global::INotifier.ItemAdded { add => Object.ItemAdded += value; remove => Object.ItemAdded -= value; } } @@ -26,28 +23,25 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public readonly struct INotifier_MockEvents { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public readonly struct INotifier_MockEvents - { - internal readonly global::TUnit.Mocks.MockEngine Engine; + internal readonly global::TUnit.Mocks.MockEngine Engine; - internal INotifier_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; - } + internal INotifier_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; +} - public static class INotifier_MockEventsExtensions +public static class INotifier_MockEventsExtensions +{ + extension(global::TUnit.Mocks.Mock mock) { - extension(global::TUnit.Mocks.Mock mock) - { - public INotifier_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); - } + public INotifier_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); + } - extension(INotifier_MockEvents events) - { - public global::TUnit.Mocks.EventSubscriptionAccessor ItemAdded - => new(events.Engine, "ItemAdded"); - } + extension(INotifier_MockEvents events) + { + public global::TUnit.Mocks.EventSubscriptionAccessor ItemAdded + => new(events.Engine, "ItemAdded"); } } @@ -58,81 +52,78 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class INotifierMockImpl : global::INotifier, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class INotifierMockImpl : global::INotifier, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal INotifierMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal INotifierMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public void Notify(string message) - { - _engine.HandleCall(0, "Notify", message); - } + public void Notify(string message) + { + _engine.HandleCall(0, "Notify", message); + } - private global::System.EventHandler? _backing_ItemAdded; + private global::System.EventHandler? _backing_ItemAdded; - public event global::System.EventHandler? ItemAdded - { - add { _backing_ItemAdded += value; _engine.RecordEventSubscription("ItemAdded", true); } - remove { _backing_ItemAdded -= value; _engine.RecordEventSubscription("ItemAdded", false); } - } + public event global::System.EventHandler? ItemAdded + { + add { _backing_ItemAdded += value; _engine.RecordEventSubscription("ItemAdded", true); } + remove { _backing_ItemAdded -= value; _engine.RecordEventSubscription("ItemAdded", false); } + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal void Raise_ItemAdded(string e) - { - _backing_ItemAdded?.Invoke(this, e); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal void Raise_ItemAdded(string e) + { + _backing_ItemAdded?.Invoke(this, e); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + switch (eventName) { - switch (eventName) - { - case "ItemAdded": - { - Raise_ItemAdded((string)args!); - break; - } - default: - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + case "ItemAdded": + { + Raise_ItemAdded((string)args!); + break; + } + default: + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } } +} - internal static class INotifierMockFactory +internal static class INotifierMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new INotifierMockImpl(engine); - engine.Raisable = impl; - var mock = new INotifierMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new INotifierMockImpl(engine); + engine.Raisable = impl; + var mock = new INotifierMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::INotifier' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new INotifierMockImpl(engine); - engine.Raisable = impl; - var mock = new INotifierMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::INotifier' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new INotifierMockImpl(engine); + engine.Raisable = impl; + var mock = new INotifierMock(impl, engine); + return mock; } } @@ -143,111 +134,108 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class INotifier_MockMemberExtensions { - public static class INotifier_MockMemberExtensions + public static INotifier_Notify_M0_MockCall Notify(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg message) { - public static INotifier_Notify_M0_MockCall Notify(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg message) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { message.Matcher }; - return new INotifier_Notify_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Notify", matchers); - } - - public static INotifier_Notify_M0_MockCall Notify(this global::TUnit.Mocks.Mock mock, global::System.Func message) - { - global::TUnit.Mocks.Arguments.Arg __fa_message = message; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_message.Matcher }; - return new INotifier_Notify_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Notify", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { message.Matcher }; + return new INotifier_Notify_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Notify", matchers); + } - public static void RaiseItemAdded(this global::TUnit.Mocks.Mock mock, string e) - { - ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("ItemAdded", (object?)e); - } + public static INotifier_Notify_M0_MockCall Notify(this global::TUnit.Mocks.Mock mock, global::System.Func message) + { + global::TUnit.Mocks.Arguments.Arg __fa_message = message; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_message.Matcher }; + return new INotifier_Notify_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Notify", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class INotifier_Notify_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public static void RaiseItemAdded(this global::TUnit.Mocks.Mock mock, string e) { - 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; + ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("ItemAdded", (object?)e); + } +} - 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(); - } +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class INotifier_Notify_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - 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(); - } + 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(); + } - [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; - } + 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(); + } - /// - 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; } - - /// Execute a typed callback using the actual method parameters. - public INotifier_Notify_M0_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public INotifier_Notify_M0_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); - return this; - } + /// + 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; } + + /// Execute a typed callback using the actual method parameters. + public INotifier_Notify_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; + } - /// 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); + /// Configure a typed computed exception using the actual method parameters. + public INotifier_Notify_M0_MockCall Throws(global::System.Func exceptionFactory) + { + EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); + return this; } + + /// 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); } @@ -263,9 +251,9 @@ namespace TUnit.Mocks { extension(global::INotifier _) { - public static global::TUnit.Mocks.Generated.INotifierMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::INotifierMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.INotifierMock)global::TUnit.Mocks.Generated.INotifierMockFactory.CreateAutoMock(behavior); + return (global::INotifierMock)global::INotifierMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Method_Constraints_On_Explicit_Impl.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Method_Constraints_On_Explicit_Impl.verified.txt index db09467453..a8b59cf085 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Method_Constraints_On_Explicit_Impl.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Method_Constraints_On_Explicit_Impl.verified.txt @@ -2,26 +2,23 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IConstrainedMock : global::TUnit.Mocks.Mock, global::IConstrained { - public sealed class IConstrainedMock : global::TUnit.Mocks.Mock, global::IConstrained - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IConstrainedMock(global::IConstrained mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IConstrainedMock(global::IConstrained mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - T global::IConstrained.GetNotnull(string key) => Object.GetNotnull(key); + T global::IConstrained.GetNotnull(string key) => Object.GetNotnull(key); - T global::IConstrained.GetNew() => Object.GetNew(); + T global::IConstrained.GetNew() => Object.GetNew(); - T global::IConstrained.GetUnmanaged() where T : struct => Object.GetUnmanaged(); + T global::IConstrained.GetUnmanaged() where T : struct => Object.GetUnmanaged(); - T global::IConstrained.GetDisposable() => Object.GetDisposable(); + T global::IConstrained.GetDisposable() => Object.GetDisposable(); - T global::IConstrained.GetClassNew() where T : class => Object.GetClassNew(); + T global::IConstrained.GetClassNew() where T : class => Object.GetClassNew(); - T global::IConstrained.GetStructDisposable() where T : struct => Object.GetStructDisposable(); - } + T global::IConstrained.GetStructDisposable() where T : struct => Object.GetStructDisposable(); } @@ -31,83 +28,80 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IConstrainedMockImpl : global::IConstrained, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IConstrainedMockImpl : global::IConstrained, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IConstrainedMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IConstrainedMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public T GetNotnull(string key) where T : notnull - { - return _engine.HandleCallWithReturn(0, "GetNotnull", key, default!); - } + public T GetNotnull(string key) where T : notnull + { + return _engine.HandleCallWithReturn(0, "GetNotnull", key, default!); + } - public T GetNew() where T : new() - { - return _engine.HandleCallWithReturn(1, "GetNew", global::System.Array.Empty(), default!); - } + public T GetNew() where T : new() + { + return _engine.HandleCallWithReturn(1, "GetNew", global::System.Array.Empty(), default!); + } - public T GetUnmanaged() where T : struct, unmanaged - { - return _engine.HandleCallWithReturn(2, "GetUnmanaged", global::System.Array.Empty(), default); - } + public T GetUnmanaged() where T : struct, unmanaged + { + return _engine.HandleCallWithReturn(2, "GetUnmanaged", global::System.Array.Empty(), default); + } - public T GetDisposable() where T : global::System.IDisposable - { - return _engine.HandleCallWithReturn(3, "GetDisposable", global::System.Array.Empty(), default!); - } + public T GetDisposable() where T : global::System.IDisposable + { + return _engine.HandleCallWithReturn(3, "GetDisposable", global::System.Array.Empty(), default!); + } - public T GetClassNew() where T : class, global::System.IDisposable, new() - { - return _engine.HandleCallWithReturn(4, "GetClassNew", global::System.Array.Empty(), default!); - } + public T GetClassNew() where T : class, global::System.IDisposable, new() + { + return _engine.HandleCallWithReturn(4, "GetClassNew", global::System.Array.Empty(), default!); + } - public T GetStructDisposable() where T : struct, global::System.IDisposable - { - return _engine.HandleCallWithReturn(5, "GetStructDisposable", global::System.Array.Empty(), default); - } + public T GetStructDisposable() where T : struct, global::System.IDisposable + { + return _engine.HandleCallWithReturn(5, "GetStructDisposable", global::System.Array.Empty(), default); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class IConstrainedMockFactory +internal static class IConstrainedMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IConstrainedMockImpl(engine); - engine.Raisable = impl; - var mock = new IConstrainedMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IConstrainedMockImpl(engine); + engine.Raisable = impl; + var mock = new IConstrainedMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IConstrained' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IConstrainedMockImpl(engine); - engine.Raisable = impl; - var mock = new IConstrainedMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IConstrained' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IConstrainedMockImpl(engine); + engine.Raisable = impl; + var mock = new IConstrainedMock(impl, engine); + return mock; } } @@ -118,52 +112,49 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IConstrained_MockMemberExtensions { - public static class IConstrained_MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall GetNotnull(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg key) where T : notnull { - public static global::TUnit.Mocks.MockMethodCall GetNotnull(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg key) where T : notnull - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { key.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetNotnull", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { key.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetNotnull", matchers); + } - public static global::TUnit.Mocks.MockMethodCall GetNotnull(this global::TUnit.Mocks.Mock mock, global::System.Func key) where T : notnull - { - global::TUnit.Mocks.Arguments.Arg __fa_key = key; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_key.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetNotnull", matchers); - } + public static global::TUnit.Mocks.MockMethodCall GetNotnull(this global::TUnit.Mocks.Mock mock, global::System.Func key) where T : notnull + { + global::TUnit.Mocks.Arguments.Arg __fa_key = key; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_key.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetNotnull", matchers); + } - public static global::TUnit.Mocks.MockMethodCall GetNew(this global::TUnit.Mocks.Mock mock) where T : new() - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetNew", matchers); - } + public static global::TUnit.Mocks.MockMethodCall GetNew(this global::TUnit.Mocks.Mock mock) where T : new() + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetNew", matchers); + } - public static global::TUnit.Mocks.MockMethodCall GetUnmanaged(this global::TUnit.Mocks.Mock mock) where T : struct, unmanaged - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "GetUnmanaged", matchers); - } + public static global::TUnit.Mocks.MockMethodCall GetUnmanaged(this global::TUnit.Mocks.Mock mock) where T : struct, unmanaged + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "GetUnmanaged", matchers); + } - public static global::TUnit.Mocks.MockMethodCall GetDisposable(this global::TUnit.Mocks.Mock mock) where T : global::System.IDisposable - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "GetDisposable", matchers); - } + public static global::TUnit.Mocks.MockMethodCall GetDisposable(this global::TUnit.Mocks.Mock mock) where T : global::System.IDisposable + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "GetDisposable", matchers); + } - public static global::TUnit.Mocks.MockMethodCall GetClassNew(this global::TUnit.Mocks.Mock mock) where T : class, global::System.IDisposable, new() - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 4, "GetClassNew", matchers); - } + public static global::TUnit.Mocks.MockMethodCall GetClassNew(this global::TUnit.Mocks.Mock mock) where T : class, global::System.IDisposable, new() + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 4, "GetClassNew", matchers); + } - public static global::TUnit.Mocks.MockMethodCall GetStructDisposable(this global::TUnit.Mocks.Mock mock) where T : struct, global::System.IDisposable - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 5, "GetStructDisposable", matchers); - } + public static global::TUnit.Mocks.MockMethodCall GetStructDisposable(this global::TUnit.Mocks.Mock mock) where T : struct, global::System.IDisposable + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 5, "GetStructDisposable", matchers); } } @@ -180,9 +171,9 @@ namespace TUnit.Mocks { extension(global::IConstrained _) { - public static global::TUnit.Mocks.Generated.IConstrainedMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IConstrainedMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IConstrainedMock)global::TUnit.Mocks.Generated.IConstrainedMockFactory.CreateAutoMock(behavior); + return (global::IConstrainedMock)global::IConstrainedMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Methods.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Methods.verified.txt index 4caba67245..3d450283af 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Methods.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Generic_Methods.verified.txt @@ -2,23 +2,20 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IRepositoryMock : global::TUnit.Mocks.Mock, global::IRepository { - public sealed class IRepositoryMock : global::TUnit.Mocks.Mock, global::IRepository - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IRepositoryMock(global::IRepository mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } - - T global::IRepository.GetById(int id) where T : class => Object.GetById(id); + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IRepositoryMock(global::IRepository mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - void global::IRepository.Save(T entity) where T : class - { - Object.Save(entity); - } + T global::IRepository.GetById(int id) where T : class => Object.GetById(id); - TResult global::IRepository.Transform(TInput input) where TResult : struct => Object.Transform(input); + void global::IRepository.Save(T entity) where T : class + { + Object.Save(entity); } + + TResult global::IRepository.Transform(TInput input) where TResult : struct => Object.Transform(input); } @@ -28,68 +25,65 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IRepositoryMockImpl : global::IRepository, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IRepositoryMockImpl : global::IRepository, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IRepositoryMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IRepositoryMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public T GetById(int id) where T : class - { - return _engine.HandleCallWithReturn(0, "GetById", id, default!); - } + public T GetById(int id) where T : class + { + return _engine.HandleCallWithReturn(0, "GetById", id, default!); + } - public void Save(T entity) where T : class, new() - { - _engine.HandleCall(1, "Save", entity); - } + public void Save(T entity) where T : class, new() + { + _engine.HandleCall(1, "Save", entity); + } - public TResult Transform(TInput input) where TInput : notnull where TResult : struct - { - return _engine.HandleCallWithReturn(2, "Transform", input, default); - } + public TResult Transform(TInput input) where TInput : notnull where TResult : struct + { + return _engine.HandleCallWithReturn(2, "Transform", input, default); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class IRepositoryMockFactory +internal static class IRepositoryMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IRepositoryMockImpl(engine); - engine.Raisable = impl; - var mock = new IRepositoryMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IRepositoryMockImpl(engine); + engine.Raisable = impl; + var mock = new IRepositoryMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IRepository' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IRepositoryMockImpl(engine); - engine.Raisable = impl; - var mock = new IRepositoryMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IRepository' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IRepositoryMockImpl(engine); + engine.Raisable = impl; + var mock = new IRepositoryMock(impl, engine); + return mock; } } @@ -100,48 +94,45 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IRepository_MockMemberExtensions { - public static class IRepository_MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall GetById(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg id) where T : class { - public static global::TUnit.Mocks.MockMethodCall GetById(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg id) where T : class - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { id.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetById", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { id.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetById", matchers); + } - public static global::TUnit.Mocks.MockMethodCall GetById(this global::TUnit.Mocks.Mock mock, global::System.Func id) where T : class - { - global::TUnit.Mocks.Arguments.Arg __fa_id = id; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_id.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetById", matchers); - } + public static global::TUnit.Mocks.MockMethodCall GetById(this global::TUnit.Mocks.Mock mock, global::System.Func id) where T : class + { + global::TUnit.Mocks.Arguments.Arg __fa_id = id; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_id.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetById", matchers); + } - public static global::TUnit.Mocks.VoidMockMethodCall Save(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg entity) where T : class, new() - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { entity.Matcher }; - return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Save", matchers); - } + public static global::TUnit.Mocks.VoidMockMethodCall Save(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg entity) where T : class, new() + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { entity.Matcher }; + return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Save", matchers); + } - public static global::TUnit.Mocks.VoidMockMethodCall Save(this global::TUnit.Mocks.Mock mock, global::System.Func entity) where T : class, new() - { - global::TUnit.Mocks.Arguments.Arg __fa_entity = entity; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_entity.Matcher }; - return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Save", matchers); - } + public static global::TUnit.Mocks.VoidMockMethodCall Save(this global::TUnit.Mocks.Mock mock, global::System.Func entity) where T : class, new() + { + global::TUnit.Mocks.Arguments.Arg __fa_entity = entity; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_entity.Matcher }; + return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Save", matchers); + } - public static global::TUnit.Mocks.MockMethodCall Transform(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg input) where TInput : notnull where TResult : struct - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { input.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Transform", matchers); - } + public static global::TUnit.Mocks.MockMethodCall Transform(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg input) where TInput : notnull where TResult : struct + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { input.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Transform", matchers); + } - public static global::TUnit.Mocks.MockMethodCall Transform(this global::TUnit.Mocks.Mock mock, global::System.Func input) where TInput : notnull where TResult : struct - { - global::TUnit.Mocks.Arguments.Arg __fa_input = input; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_input.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Transform", matchers); - } + public static global::TUnit.Mocks.MockMethodCall Transform(this global::TUnit.Mocks.Mock mock, global::System.Func input) where TInput : notnull where TResult : struct + { + global::TUnit.Mocks.Arguments.Arg __fa_input = input; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_input.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Transform", matchers); } } @@ -158,9 +149,9 @@ namespace TUnit.Mocks { extension(global::IRepository _) { - public static global::TUnit.Mocks.Generated.IRepositoryMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IRepositoryMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IRepositoryMock)global::TUnit.Mocks.Generated.IRepositoryMockFactory.CreateAutoMock(behavior); + return (global::IRepositoryMock)global::IRepositoryMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Inherited_Static_Abstract_Members.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Inherited_Static_Abstract_Members.verified.txt index 8f54ed193a..a20849b6dc 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Inherited_Static_Abstract_Members.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Inherited_Static_Abstract_Members.verified.txt @@ -2,30 +2,27 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public interface IMyServiceMockable : global::IMyService { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public interface IMyServiceMockable : global::IMyService + static global::ClientConfig global::IServiceBase.CreateDefaultConfig() { - static global::ClientConfig global::IServiceBase.CreateDefaultConfig() - { - var __engine = IMyServiceStaticEngine.Engine; - if (__engine is null) return default!; - var __result = __engine.HandleCallWithReturn(1, "CreateDefaultConfig", global::System.Array.Empty(), default!); - return __result; - } + var __engine = IMyServiceStaticEngine.Engine; + if (__engine is null) return default!; + var __result = __engine.HandleCallWithReturn(1, "CreateDefaultConfig", global::System.Array.Empty(), default!); + return __result; } +} - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal static class IMyServiceStaticEngine - { - private static readonly global::System.Threading.AsyncLocal _engine = new(); +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +internal static class IMyServiceStaticEngine +{ + private static readonly global::System.Threading.AsyncLocal _engine = new(); - internal static global::TUnit.Mocks.IMockEngineAccess? Engine - { - get => _engine.Value; - set => _engine.Value = value; - } + internal static global::TUnit.Mocks.IMockEngineAccess? Engine + { + get => _engine.Value; + set => _engine.Value = value; } } @@ -36,65 +33,62 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IMyServiceMockImpl : global::IMyServiceMockable, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IMyServiceMockImpl : global::TUnit.Mocks.Generated.IMyServiceMockable, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IMyServiceMockImpl(global::TUnit.Mocks.MockEngine engine) + internal IMyServiceMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + if (IMyServiceStaticEngine.Engine is not null) { - _engine = engine; - if (IMyServiceStaticEngine.Engine is not null) - { - throw new global::System.InvalidOperationException( - "Multiple mocks of an interface with static abstract members cannot be created in the same test context. " + - "Static member calls are routed via a shared AsyncLocal engine, so only one mock instance per type is supported per test."); - } - IMyServiceStaticEngine.Engine = engine; + throw new global::System.InvalidOperationException( + "Multiple mocks of an interface with static abstract members cannot be created in the same test context. " + + "Static member calls are routed via a shared AsyncLocal engine, so only one mock instance per type is supported per test."); } + IMyServiceStaticEngine.Engine = engine; + } - public string GetName() - { - return _engine.HandleCallWithReturn(0, "GetName", global::System.Array.Empty(), ""); - } + public string GetName() + { + return _engine.HandleCallWithReturn(0, "GetName", global::System.Array.Empty(), ""); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class IMyServiceMockFactory +internal static class IMyServiceMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IMyServiceMockImpl(engine); - engine.Raisable = impl; - var mock = new global::TUnit.Mocks.Mock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IMyServiceMockImpl(engine); + engine.Raisable = impl; + var mock = new global::TUnit.Mocks.Mock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::TUnit.Mocks.Generated.IMyServiceMockable' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IMyServiceMockImpl(engine); - engine.Raisable = impl; - var mock = new global::TUnit.Mocks.Mock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IMyServiceMockable' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IMyServiceMockImpl(engine); + engine.Raisable = impl; + var mock = new global::TUnit.Mocks.Mock(impl, engine); + return mock; } } @@ -105,21 +99,18 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IMyService_MockMemberExtensions { - public static class IMyService_MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall GetName(this global::TUnit.Mocks.Mock mock) { - public static global::TUnit.Mocks.MockMethodCall GetName(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetName", matchers); - } + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetName", matchers); + } - public static global::TUnit.Mocks.MockMethodCall CreateDefaultConfig(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "CreateDefaultConfig", matchers); - } + public static global::TUnit.Mocks.MockMethodCall CreateDefaultConfig(this global::TUnit.Mocks.Mock mock) + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "CreateDefaultConfig", matchers); } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Member_Names.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Member_Names.verified.txt index 23438e4b3e..dacbf5c1b8 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Member_Names.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Member_Names.verified.txt @@ -2,25 +2,22 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IEscapedNamesMock : global::TUnit.Mocks.Mock, global::IEscapedNames { - public sealed class IEscapedNamesMock : global::TUnit.Mocks.Mock, global::IEscapedNames - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IEscapedNamesMock(global::IEscapedNames mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IEscapedNamesMock(global::IEscapedNames mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - string global::IEscapedNames.record() => Object.record(); + string global::IEscapedNames.record() => Object.record(); - void global::IEscapedNames.@event(int @params) - { - Object.@event(@params); - } + void global::IEscapedNames.@event(int @params) + { + Object.@event(@params); + } - int global::IEscapedNames.@namespace(int @new, int @static) => Object.@namespace(@new, @static); + int global::IEscapedNames.@namespace(int @new, int @static) => Object.@namespace(@new, @static); - int global::IEscapedNames.@class { get => Object.@class; } - } + int global::IEscapedNames.@class { get => Object.@class; } } @@ -30,73 +27,70 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IEscapedNamesMockImpl : global::IEscapedNames, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IEscapedNamesMockImpl : global::IEscapedNames, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IEscapedNamesMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IEscapedNamesMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public string record() - { - return _engine.HandleCallWithReturn(1, "record", global::System.Array.Empty(), ""); - } + public string record() + { + return _engine.HandleCallWithReturn(1, "record", global::System.Array.Empty(), ""); + } - public void @event(int @params) - { - _engine.HandleCall(2, "event", @params); - } + public void @event(int @params) + { + _engine.HandleCall(2, "event", @params); + } - public int @namespace(int @new, int @static) - { - return _engine.HandleCallWithReturn(3, "namespace", @new, @static, default); - } + public int @namespace(int @new, int @static) + { + return _engine.HandleCallWithReturn(3, "namespace", @new, @static, default); + } - public int @class - { - get => _engine.HandleCallWithReturn(0, "get_class", global::System.Array.Empty(), default); - } + public int @class + { + get => _engine.HandleCallWithReturn(0, "get_class", global::System.Array.Empty(), default); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class IEscapedNamesMockFactory +internal static class IEscapedNamesMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IEscapedNamesMockImpl(engine); - engine.Raisable = impl; - var mock = new IEscapedNamesMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IEscapedNamesMockImpl(engine); + engine.Raisable = impl; + var mock = new IEscapedNamesMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IEscapedNames' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IEscapedNamesMockImpl(engine); - engine.Raisable = impl; - var mock = new IEscapedNamesMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IEscapedNames' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IEscapedNamesMockImpl(engine); + engine.Raisable = impl; + var mock = new IEscapedNamesMock(impl, engine); + return mock; } } @@ -107,240 +101,237 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IEscapedNames_MockMemberExtensions { - public static class IEscapedNames_MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall record(this global::TUnit.Mocks.Mock mock) { - public static global::TUnit.Mocks.MockMethodCall record(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "record", matchers); - } - - public static IEscapedNames_event_M2_MockCall @event(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg @params) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { @params.Matcher }; - return new IEscapedNames_event_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "event", matchers); - } - - public static IEscapedNames_event_M2_MockCall @event(this global::TUnit.Mocks.Mock mock, global::System.Func @params) - { - global::TUnit.Mocks.Arguments.Arg __fa_params = @params; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_params.Matcher }; - return new IEscapedNames_event_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "event", matchers); - } + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "record", matchers); + } - public static IEscapedNames_namespace_M3_MockCall @namespace(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg @new, global::TUnit.Mocks.Arguments.Arg @static) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { @new.Matcher, @static.Matcher }; - return new IEscapedNames_namespace_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "namespace", matchers); - } + public static IEscapedNames_event_M2_MockCall @event(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg @params) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { @params.Matcher }; + return new IEscapedNames_event_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "event", matchers); + } - public static IEscapedNames_namespace_M3_MockCall @namespace(this global::TUnit.Mocks.Mock mock, global::System.Func @new, global::TUnit.Mocks.Arguments.Arg @static) - { - global::TUnit.Mocks.Arguments.Arg __fa_new = @new; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_new.Matcher, @static.Matcher }; - return new IEscapedNames_namespace_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "namespace", matchers); - } + public static IEscapedNames_event_M2_MockCall @event(this global::TUnit.Mocks.Mock mock, global::System.Func @params) + { + global::TUnit.Mocks.Arguments.Arg __fa_params = @params; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_params.Matcher }; + return new IEscapedNames_event_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "event", matchers); + } - public static IEscapedNames_namespace_M3_MockCall @namespace(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg @new, global::System.Func @static) - { - global::TUnit.Mocks.Arguments.Arg __fa_static = @static; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { @new.Matcher, __fa_static.Matcher }; - return new IEscapedNames_namespace_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "namespace", matchers); - } + public static IEscapedNames_namespace_M3_MockCall @namespace(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg @new, global::TUnit.Mocks.Arguments.Arg @static) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { @new.Matcher, @static.Matcher }; + return new IEscapedNames_namespace_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "namespace", matchers); + } - public static IEscapedNames_namespace_M3_MockCall @namespace(this global::TUnit.Mocks.Mock mock, global::System.Func @new, global::System.Func @static) - { - global::TUnit.Mocks.Arguments.Arg __fa_new = @new; - global::TUnit.Mocks.Arguments.Arg __fa_static = @static; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_new.Matcher, __fa_static.Matcher }; - return new IEscapedNames_namespace_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "namespace", matchers); - } + public static IEscapedNames_namespace_M3_MockCall @namespace(this global::TUnit.Mocks.Mock mock, global::System.Func @new, global::TUnit.Mocks.Arguments.Arg @static) + { + global::TUnit.Mocks.Arguments.Arg __fa_new = @new; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_new.Matcher, @static.Matcher }; + return new IEscapedNames_namespace_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "namespace", matchers); + } - /// Configure the mock setup for namespace with every argument matched as Any<T>(). - public static IEscapedNames_namespace_M3_MockCall @namespace(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.AnyArgs _) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance }; - return new IEscapedNames_namespace_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "namespace", matchers); - } + public static IEscapedNames_namespace_M3_MockCall @namespace(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg @new, global::System.Func @static) + { + global::TUnit.Mocks.Arguments.Arg __fa_static = @static; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { @new.Matcher, __fa_static.Matcher }; + return new IEscapedNames_namespace_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "namespace", matchers); + } - extension(global::TUnit.Mocks.Mock mock) - { - public global::TUnit.Mocks.PropertyMockCall @class - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, 0, "class", true, false); - } + public static IEscapedNames_namespace_M3_MockCall @namespace(this global::TUnit.Mocks.Mock mock, global::System.Func @new, global::System.Func @static) + { + global::TUnit.Mocks.Arguments.Arg __fa_new = @new; + global::TUnit.Mocks.Arguments.Arg __fa_static = @static; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_new.Matcher, __fa_static.Matcher }; + return new IEscapedNames_namespace_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "namespace", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IEscapedNames_event_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure the mock setup for namespace with every argument matched as Any<T>(). + public static IEscapedNames_namespace_M3_MockCall @namespace(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.AnyArgs _) { - 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; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance }; + return new IEscapedNames_namespace_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "namespace", matchers); + } - 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(); - } + extension(global::TUnit.Mocks.Mock mock) + { + public global::TUnit.Mocks.PropertyMockCall @class + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, 0, "class", true, false); + } +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IEscapedNames_event_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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(); + } - /// - 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; } - - /// Execute a typed callback using the actual method parameters. - public IEscapedNames_event_M2_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + 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(); + } - /// Configure a typed computed exception using the actual method parameters. - public IEscapedNames_event_M2_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); - return this; - } + [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; + } - // 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); + /// + 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; } + + /// Execute a typed callback using the actual method parameters. + public IEscapedNames_event_M2_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IEscapedNames_namespace_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public IEscapedNames_event_M2_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); + return this; + } - 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; - } + // 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IEscapedNames_namespace_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public IEscapedNames_namespace_M3_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((int)args[0]!, (int)args[1]!)); - return this; - } + 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(); + } - /// Execute a typed callback using the actual method parameters. - public IEscapedNames_namespace_M3_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public IEscapedNames_namespace_M3_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((int)args[0]!, (int)args[1]!)); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public IEscapedNames_namespace_M3_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((int)args[0]!, (int)args[1]!)); + return this; + } + + /// Execute a typed callback using the actual method parameters. + public IEscapedNames_namespace_M3_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + 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); + /// Configure a typed computed exception using the actual method parameters. + public IEscapedNames_namespace_M3_MockCall Throws(global::System.Func exceptionFactory) + { + 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); } @@ -356,9 +347,9 @@ namespace TUnit.Mocks { extension(global::IEscapedNames _) { - public static global::TUnit.Mocks.Generated.IEscapedNamesMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IEscapedNamesMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IEscapedNamesMock)global::TUnit.Mocks.Generated.IEscapedNamesMockFactory.CreateAutoMock(behavior); + return (global::IEscapedNamesMock)global::IEscapedNamesMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Parameter_Names.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Parameter_Names.verified.txt index 019a00dc8b..d2604ec92a 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Parameter_Names.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Keyword_Parameter_Names.verified.txt @@ -2,21 +2,18 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class ITestMock : global::TUnit.Mocks.Mock, global::ITest { - public sealed class ITestMock : global::TUnit.Mocks.Mock, global::ITest - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal ITestMock(global::ITest mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } - - void global::ITest.Test(string @event) - { - Object.Test(@event); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal ITestMock(global::ITest mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - string global::ITest.Get(int @class, string @return) => Object.Get(@class, @return); + void global::ITest.Test(string @event) + { + Object.Test(@event); } + + string global::ITest.Get(int @class, string @return) => Object.Get(@class, @return); } @@ -26,63 +23,60 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class ITestMockImpl : global::ITest, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class ITestMockImpl : global::ITest, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal ITestMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal ITestMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public void Test(string @event) - { - _engine.HandleCall(0, "Test", @event); - } + public void Test(string @event) + { + _engine.HandleCall(0, "Test", @event); + } - public string Get(int @class, string @return) - { - return _engine.HandleCallWithReturn(1, "Get", @class, @return, ""); - } + public string Get(int @class, string @return) + { + return _engine.HandleCallWithReturn(1, "Get", @class, @return, ""); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class ITestMockFactory +internal static class ITestMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new ITestMockImpl(engine); - engine.Raisable = impl; - var mock = new ITestMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new ITestMockImpl(engine); + engine.Raisable = impl; + var mock = new ITestMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::ITest' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new ITestMockImpl(engine); - engine.Raisable = impl; - var mock = new ITestMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::ITest' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new ITestMockImpl(engine); + engine.Raisable = impl; + var mock = new ITestMock(impl, engine); + return mock; } } @@ -93,228 +87,225 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class ITest_MockMemberExtensions { - public static class ITest_MockMemberExtensions + public static ITest_Test_M0_MockCall Test(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg @event) { - public static ITest_Test_M0_MockCall Test(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg @event) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { @event.Matcher }; - return new ITest_Test_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Test", matchers); - } - - public static ITest_Test_M0_MockCall Test(this global::TUnit.Mocks.Mock mock, global::System.Func @event) - { - global::TUnit.Mocks.Arguments.Arg __fa_event = @event; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_event.Matcher }; - return new ITest_Test_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Test", matchers); - } - - public static ITest_Get_M1_MockCall Get(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg @class, global::TUnit.Mocks.Arguments.Arg @return) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { @class.Matcher, @return.Matcher }; - return new ITest_Get_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Get", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { @event.Matcher }; + return new ITest_Test_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Test", matchers); + } - public static ITest_Get_M1_MockCall Get(this global::TUnit.Mocks.Mock mock, global::System.Func @class, global::TUnit.Mocks.Arguments.Arg @return) - { - global::TUnit.Mocks.Arguments.Arg __fa_class = @class; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_class.Matcher, @return.Matcher }; - return new ITest_Get_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Get", matchers); - } + public static ITest_Test_M0_MockCall Test(this global::TUnit.Mocks.Mock mock, global::System.Func @event) + { + global::TUnit.Mocks.Arguments.Arg __fa_event = @event; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_event.Matcher }; + return new ITest_Test_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Test", matchers); + } - public static ITest_Get_M1_MockCall Get(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg @class, global::System.Func @return) - { - global::TUnit.Mocks.Arguments.Arg __fa_return = @return; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { @class.Matcher, __fa_return.Matcher }; - return new ITest_Get_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Get", matchers); - } + public static ITest_Get_M1_MockCall Get(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg @class, global::TUnit.Mocks.Arguments.Arg @return) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { @class.Matcher, @return.Matcher }; + return new ITest_Get_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Get", matchers); + } - public static ITest_Get_M1_MockCall Get(this global::TUnit.Mocks.Mock mock, global::System.Func @class, global::System.Func @return) - { - global::TUnit.Mocks.Arguments.Arg __fa_class = @class; - global::TUnit.Mocks.Arguments.Arg __fa_return = @return; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_class.Matcher, __fa_return.Matcher }; - return new ITest_Get_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Get", matchers); - } + public static ITest_Get_M1_MockCall Get(this global::TUnit.Mocks.Mock mock, global::System.Func @class, global::TUnit.Mocks.Arguments.Arg @return) + { + global::TUnit.Mocks.Arguments.Arg __fa_class = @class; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_class.Matcher, @return.Matcher }; + return new ITest_Get_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Get", matchers); + } - /// Configure the mock setup for Get with every argument matched as Any<T>(). - public static ITest_Get_M1_MockCall Get(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.AnyArgs _) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance }; - return new ITest_Get_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Get", matchers); - } + public static ITest_Get_M1_MockCall Get(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg @class, global::System.Func @return) + { + global::TUnit.Mocks.Arguments.Arg __fa_return = @return; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { @class.Matcher, __fa_return.Matcher }; + return new ITest_Get_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Get", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ITest_Test_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public static ITest_Get_M1_MockCall Get(this global::TUnit.Mocks.Mock mock, global::System.Func @class, global::System.Func @return) { - 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; + global::TUnit.Mocks.Arguments.Arg __fa_class = @class; + global::TUnit.Mocks.Arguments.Arg __fa_return = @return; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_class.Matcher, __fa_return.Matcher }; + return new ITest_Get_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Get", matchers); + } - 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(); - } + /// Configure the mock setup for Get with every argument matched as Any<T>(). + public static ITest_Get_M1_MockCall Get(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.AnyArgs _) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance }; + return new ITest_Get_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Get", matchers); + } +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class ITest_Test_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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(); + } - /// - 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; } - - /// Execute a typed callback using the actual method parameters. - public ITest_Test_M0_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + 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(); + } - /// Configure a typed computed exception using the actual method parameters. - public ITest_Test_M0_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); - return this; - } + [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; + } - // 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); + /// + 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; } + + /// Execute a typed callback using the actual method parameters. + public ITest_Test_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ITest_Get_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public ITest_Test_M0_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); + return this; + } - 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; - } + // 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class ITest_Get_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public ITest_Get_M1_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((int)args[0]!, (string)args[1]!)); - return this; - } + 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(); + } - /// Execute a typed callback using the actual method parameters. - public ITest_Get_M1_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public ITest_Get_M1_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((int)args[0]!, (string)args[1]!)); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public ITest_Get_M1_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((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); + /// Execute a typed callback using the actual method parameters. + public ITest_Get_M1_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } + + /// Configure a typed computed exception using the actual method parameters. + public ITest_Get_M1_MockCall Throws(global::System.Func exceptionFactory) + { + 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); } @@ -330,9 +321,9 @@ namespace TUnit.Mocks { extension(global::ITest _) { - public static global::TUnit.Mocks.Generated.ITestMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::ITestMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.ITestMock)global::TUnit.Mocks.Generated.ITestMockFactory.CreateAutoMock(behavior); + return (global::ITestMock)global::ITestMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Mixed_Members.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Mixed_Members.verified.txt index 72b9535752..7ebb9926e6 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Mixed_Members.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Mixed_Members.verified.txt @@ -2,27 +2,24 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IServiceMock : global::TUnit.Mocks.Mock, global::IService { - public sealed class IServiceMock : global::TUnit.Mocks.Mock, global::IService - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IServiceMock(global::IService mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IServiceMock(global::IService mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - global::System.Threading.Tasks.Task global::IService.GetAsync(int id) => Object.GetAsync(id); + global::System.Threading.Tasks.Task global::IService.GetAsync(int id) => Object.GetAsync(id); - void global::IService.Process(string data) - { - Object.Process(data); - } + void global::IService.Process(string data) + { + Object.Process(data); + } - string global::IService.Name { get => Object.Name; set => Object.Name = value; } + string global::IService.Name { get => Object.Name; set => Object.Name = value; } - int global::IService.Count { get => Object.Count; } + int global::IService.Count { get => Object.Count; } - event global::System.EventHandler global::IService.StatusChanged { add => Object.StatusChanged += value; remove => Object.StatusChanged -= value; } - } + event global::System.EventHandler global::IService.StatusChanged { add => Object.StatusChanged += value; remove => Object.StatusChanged -= value; } } @@ -32,28 +29,25 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public readonly struct IService_MockEvents { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public readonly struct IService_MockEvents - { - internal readonly global::TUnit.Mocks.MockEngine Engine; + internal readonly global::TUnit.Mocks.MockEngine Engine; - internal IService_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; - } + internal IService_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; +} - public static class IService_MockEventsExtensions +public static class IService_MockEventsExtensions +{ + extension(global::TUnit.Mocks.Mock mock) { - extension(global::TUnit.Mocks.Mock mock) - { - public IService_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); - } + public IService_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); + } - extension(IService_MockEvents events) - { - public global::TUnit.Mocks.EventSubscriptionAccessor StatusChanged - => new(events.Engine, "StatusChanged"); - } + extension(IService_MockEvents events) + { + public global::TUnit.Mocks.EventSubscriptionAccessor StatusChanged + => new(events.Engine, "StatusChanged"); } } @@ -64,110 +58,107 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IServiceMockImpl : global::IService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IServiceMockImpl : global::IService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IServiceMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IServiceMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public global::System.Threading.Tasks.Task GetAsync(int id) + public global::System.Threading.Tasks.Task GetAsync(int id) + { + try { - try - { - var __result = _engine.HandleCallWithReturn(3, "GetAsync", id, ""); - if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) - { - if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; - throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); - } - return global::System.Threading.Tasks.Task.FromResult(__result); - } - catch (global::System.Exception __ex) + var __result = _engine.HandleCallWithReturn(3, "GetAsync", id, ""); + if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) { - return global::System.Threading.Tasks.Task.FromException(__ex); + if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; + throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); } + return global::System.Threading.Tasks.Task.FromResult(__result); } - - public void Process(string data) + catch (global::System.Exception __ex) { - _engine.HandleCall(4, "Process", data); + return global::System.Threading.Tasks.Task.FromException(__ex); } + } - public string Name - { - get => _engine.HandleCallWithReturn(0, "get_Name", global::System.Array.Empty(), ""); - set => _engine.HandleCall(1, "set_Name", new object?[] { value }); - } + public void Process(string data) + { + _engine.HandleCall(4, "Process", data); + } - public int Count - { - get => _engine.HandleCallWithReturn(2, "get_Count", global::System.Array.Empty(), default); - } + public string Name + { + get => _engine.HandleCallWithReturn(0, "get_Name", global::System.Array.Empty(), ""); + set => _engine.HandleCall(1, "set_Name", new object?[] { value }); + } - private global::System.EventHandler? _backing_StatusChanged; + public int Count + { + get => _engine.HandleCallWithReturn(2, "get_Count", global::System.Array.Empty(), default); + } - public event global::System.EventHandler? StatusChanged - { - add { _backing_StatusChanged += value; _engine.RecordEventSubscription("StatusChanged", true); } - remove { _backing_StatusChanged -= value; _engine.RecordEventSubscription("StatusChanged", false); } - } + private global::System.EventHandler? _backing_StatusChanged; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal void Raise_StatusChanged(string e) - { - _backing_StatusChanged?.Invoke(this, e); - } + public event global::System.EventHandler? StatusChanged + { + add { _backing_StatusChanged += value; _engine.RecordEventSubscription("StatusChanged", true); } + remove { _backing_StatusChanged -= value; _engine.RecordEventSubscription("StatusChanged", false); } + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - switch (eventName) - { - case "StatusChanged": - { - Raise_StatusChanged((string)args!); - break; - } - default: - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal void Raise_StatusChanged(string e) + { + _backing_StatusChanged?.Invoke(this, e); } - internal static class IServiceMockFactory + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() + switch (eventName) { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + case "StatusChanged": + { + Raise_StatusChanged((string)args!); + break; + } + default: + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } + } +} - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IServiceMockImpl(engine); - engine.Raisable = impl; - var mock = new IServiceMock(impl, engine); - return mock; - } +internal static class IServiceMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() + { + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IService' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IServiceMockImpl(engine); - engine.Raisable = impl; - var mock = new IServiceMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IServiceMockImpl(engine); + engine.Raisable = impl; + var mock = new IServiceMock(impl, engine); + return mock; + } + + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IService' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IServiceMockImpl(engine); + engine.Raisable = impl; + var mock = new IServiceMock(impl, engine); + return mock; } } @@ -178,238 +169,235 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IService_MockMemberExtensions { - public static class IService_MockMemberExtensions + public static IService_GetAsync_M3_MockCall GetAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg id) { - public static IService_GetAsync_M3_MockCall GetAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg id) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { id.Matcher }; - return new IService_GetAsync_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "GetAsync", matchers); - } - - public static IService_GetAsync_M3_MockCall GetAsync(this global::TUnit.Mocks.Mock mock, global::System.Func id) - { - global::TUnit.Mocks.Arguments.Arg __fa_id = id; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_id.Matcher }; - return new IService_GetAsync_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "GetAsync", matchers); - } - - public static IService_Process_M4_MockCall Process(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg data) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { data.Matcher }; - return new IService_Process_M4_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 4, "Process", matchers); - } - - public static IService_Process_M4_MockCall Process(this global::TUnit.Mocks.Mock mock, global::System.Func data) - { - global::TUnit.Mocks.Arguments.Arg __fa_data = data; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_data.Matcher }; - return new IService_Process_M4_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 4, "Process", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { id.Matcher }; + return new IService_GetAsync_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "GetAsync", matchers); + } - extension(global::TUnit.Mocks.Mock mock) - { - public global::TUnit.Mocks.PropertyMockCall Name - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, 1, "Name", true, true); + public static IService_GetAsync_M3_MockCall GetAsync(this global::TUnit.Mocks.Mock mock, global::System.Func id) + { + global::TUnit.Mocks.Arguments.Arg __fa_id = id; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_id.Matcher }; + return new IService_GetAsync_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "GetAsync", matchers); + } - public global::TUnit.Mocks.PropertyMockCall Count - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, 0, "Count", true, false); - } + public static IService_Process_M4_MockCall Process(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg data) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { data.Matcher }; + return new IService_Process_M4_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 4, "Process", matchers); + } - public static void RaiseStatusChanged(this global::TUnit.Mocks.Mock mock, string e) - { - ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("StatusChanged", (object?)e); - } + public static IService_Process_M4_MockCall Process(this global::TUnit.Mocks.Mock mock, global::System.Func data) + { + global::TUnit.Mocks.Arguments.Arg __fa_data = data; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_data.Matcher }; + return new IService_Process_M4_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 4, "Process", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IService_GetAsync_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification + extension(global::TUnit.Mocks.Mock mock) { - 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; + public global::TUnit.Mocks.PropertyMockCall Name + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, 1, "Name", true, true); - 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; - } + public global::TUnit.Mocks.PropertyMockCall Count + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, 0, "Count", true, false); + } - 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(); - } + public static void RaiseStatusChanged(this global::TUnit.Mocks.Mock mock, string e) + { + ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("StatusChanged", (object?)e); + } +} - [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; - } +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IService_GetAsync_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - /// - 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; } - - /// 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; } - /// Return a pre-built Task from a factory, invoked on each call. - public IService_GetAsync_M3_MockCall ReturnsAsync(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } - - /// Configure a typed computed return value using the actual method parameters. - public IService_GetAsync_M3_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((int)args[0]!)); - return this; - } + 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; + } - /// Configure a typed computed async return value using the actual method parameters. - public IService_GetAsync_M3_MockCall ReturnsAsync(global::System.Func> factory) - { - EnsureSetup().ReturnsRaw(args => (object?)factory((int)args[0]!)); - return this; - } + 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(); + } - /// Execute a typed callback using the actual method parameters. - public IService_GetAsync_M3_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public IService_GetAsync_M3_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); - return this; - } + /// + 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; } + + /// 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; } + /// Return a pre-built Task from a factory, invoked on each call. + public IService_GetAsync_M3_MockCall ReturnsAsync(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } + + /// Configure a typed computed return value using the actual method parameters. + public IService_GetAsync_M3_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((int)args[0]!)); + return this; + } - /// 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; } + /// Configure a typed computed async return value using the actual method parameters. + public IService_GetAsync_M3_MockCall ReturnsAsync(global::System.Func> factory) + { + EnsureSetup().ReturnsRaw(args => (object?)factory((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); + /// Execute a typed callback using the actual method parameters. + public IService_GetAsync_M3_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IService_Process_M4_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public IService_GetAsync_M3_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); + return this; + } - 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(); - } + /// 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IService_Process_M4_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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(); + } - /// - 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; } - - /// Execute a typed callback using the actual method parameters. - public IService_Process_M4_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + 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(); + } - /// Configure a typed computed exception using the actual method parameters. - public IService_Process_M4_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); - return this; - } + [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; + } - /// 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); + /// + 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; } + + /// Execute a typed callback using the actual method parameters. + public IService_Process_M4_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } + + /// Configure a typed computed exception using the actual method parameters. + public IService_Process_M4_MockCall Throws(global::System.Func exceptionFactory) + { + EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); + return this; + } + + /// 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); } @@ -425,9 +413,9 @@ namespace TUnit.Mocks { extension(global::IService _) { - public static global::TUnit.Mocks.Generated.IServiceMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IServiceMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IServiceMock)global::TUnit.Mocks.Generated.IServiceMockFactory.CreateAutoMock(behavior); + return (global::IServiceMock)global::IServiceMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Multiple_Multi_Parameter_Events.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Multiple_Multi_Parameter_Events.verified.txt index 4bc92a35c9..2ad6f87070 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Multiple_Multi_Parameter_Events.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Multiple_Multi_Parameter_Events.verified.txt @@ -2,18 +2,15 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IDualEventsMock : global::TUnit.Mocks.Mock, global::IDualEvents { - public sealed class IDualEventsMock : global::TUnit.Mocks.Mock, global::IDualEvents - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IDualEventsMock(global::IDualEvents mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IDualEventsMock(global::IDualEvents mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - event global::FirstHandler global::IDualEvents.First { add => Object.First += value; remove => Object.First -= value; } + event global::FirstHandler global::IDualEvents.First { add => Object.First += value; remove => Object.First -= value; } - event global::SecondHandler global::IDualEvents.Second { add => Object.Second += value; remove => Object.Second -= value; } - } + event global::SecondHandler global::IDualEvents.Second { add => Object.Second += value; remove => Object.Second -= value; } } @@ -23,31 +20,28 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public readonly struct IDualEvents_MockEvents { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public readonly struct IDualEvents_MockEvents - { - internal readonly global::TUnit.Mocks.MockEngine Engine; + internal readonly global::TUnit.Mocks.MockEngine Engine; - internal IDualEvents_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; - } + internal IDualEvents_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; +} - public static class IDualEvents_MockEventsExtensions +public static class IDualEvents_MockEventsExtensions +{ + extension(global::TUnit.Mocks.Mock mock) { - extension(global::TUnit.Mocks.Mock mock) - { - public IDualEvents_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); - } + public IDualEvents_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); + } - extension(IDualEvents_MockEvents events) - { - public global::TUnit.Mocks.EventSubscriptionAccessor First - => new(events.Engine, "First"); + extension(IDualEvents_MockEvents events) + { + public global::TUnit.Mocks.EventSubscriptionAccessor First + => new(events.Engine, "First"); - public global::TUnit.Mocks.EventSubscriptionAccessor Second - => new(events.Engine, "Second"); - } + public global::TUnit.Mocks.EventSubscriptionAccessor Second + => new(events.Engine, "Second"); } } @@ -58,109 +52,106 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IDualEventsMockImpl : global::IDualEvents, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IDualEventsMockImpl : global::IDualEvents, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IDualEventsMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IDualEventsMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - private global::FirstHandler? _backing_First; + private global::FirstHandler? _backing_First; - public event global::FirstHandler? First - { - add { _backing_First += value; _engine.RecordEventSubscription("First", true); } - remove { _backing_First -= value; _engine.RecordEventSubscription("First", false); } - } + public event global::FirstHandler? First + { + add { _backing_First += value; _engine.RecordEventSubscription("First", true); } + remove { _backing_First -= value; _engine.RecordEventSubscription("First", false); } + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal void Raise_First(object sender, string value) - { - _backing_First?.Invoke(sender, value); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal void Raise_First(object sender, string value) + { + _backing_First?.Invoke(sender, value); + } - private global::SecondHandler? _backing_Second; + private global::SecondHandler? _backing_Second; - public event global::SecondHandler? Second - { - add { _backing_Second += value; _engine.RecordEventSubscription("Second", true); } - remove { _backing_Second -= value; _engine.RecordEventSubscription("Second", false); } - } + public event global::SecondHandler? Second + { + add { _backing_Second += value; _engine.RecordEventSubscription("Second", true); } + remove { _backing_Second -= value; _engine.RecordEventSubscription("Second", false); } + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal void Raise_Second(object sender, int value) - { - _backing_Second?.Invoke(sender, value); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal void Raise_Second(object sender, int value) + { + _backing_Second?.Invoke(sender, value); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + switch (eventName) { - switch (eventName) - { - case "First": + case "First": + { + if (args is object?[] __argArray) { - if (args is object?[] __argArray) - { - Raise_First((object)__argArray[0]!, (string)__argArray[1]!); - } - else - { - throw new global::System.ArgumentException($"Event 'First' requires an object[] of arguments."); - } - break; + Raise_First((object)__argArray[0]!, (string)__argArray[1]!); } - case "Second": + else { - if (args is object?[] __argArray) - { - Raise_Second((object)__argArray[0]!, (int)__argArray[1]!); - } - else - { - throw new global::System.ArgumentException($"Event 'Second' requires an object[] of arguments."); - } - break; + throw new global::System.ArgumentException($"Event 'First' requires an object[] of arguments."); } - default: - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + break; + } + case "Second": + { + if (args is object?[] __argArray) + { + Raise_Second((object)__argArray[0]!, (int)__argArray[1]!); + } + else + { + throw new global::System.ArgumentException($"Event 'Second' requires an object[] of arguments."); + } + break; + } + default: + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } } +} - internal static class IDualEventsMockFactory +internal static class IDualEventsMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IDualEventsMockImpl(engine); - engine.Raisable = impl; - var mock = new IDualEventsMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IDualEventsMockImpl(engine); + engine.Raisable = impl; + var mock = new IDualEventsMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IDualEvents' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IDualEventsMockImpl(engine); - engine.Raisable = impl; - var mock = new IDualEventsMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IDualEvents' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IDualEventsMockImpl(engine); + engine.Raisable = impl; + var mock = new IDualEventsMock(impl, engine); + return mock; } } @@ -171,19 +162,16 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IDualEvents_MockMemberExtensions { - public static class IDualEvents_MockMemberExtensions + public static void RaiseFirst(this global::TUnit.Mocks.Mock mock, object sender, string value) { - public static void RaiseFirst(this global::TUnit.Mocks.Mock mock, object sender, string value) - { - ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("First", (object?)new object?[] { sender, value }); - } + ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("First", (object?)new object?[] { sender, value }); + } - public static void RaiseSecond(this global::TUnit.Mocks.Mock mock, object sender, int value) - { - ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("Second", (object?)new object?[] { sender, value }); - } + public static void RaiseSecond(this global::TUnit.Mocks.Mock mock, object sender, int value) + { + ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("Second", (object?)new object?[] { sender, value }); } } @@ -200,9 +188,9 @@ namespace TUnit.Mocks { extension(global::IDualEvents _) { - public static global::TUnit.Mocks.Generated.IDualEventsMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IDualEventsMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IDualEventsMock)global::TUnit.Mocks.Generated.IDualEventsMockFactory.CreateAutoMock(behavior); + return (global::IDualEventsMock)global::IDualEventsMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Event.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Event.verified.txt index 7f7bd8e982..6179e96e15 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Event.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Event.verified.txt @@ -2,16 +2,13 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IFooMock : global::TUnit.Mocks.Mock, global::IFoo { - public sealed class IFooMock : global::TUnit.Mocks.Mock, global::IFoo - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IFooMock(global::IFoo mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IFooMock(global::IFoo mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - event global::System.EventHandler? global::IFoo.Something { add => Object.Something += value; remove => Object.Something -= value; } - } + event global::System.EventHandler? global::IFoo.Something { add => Object.Something += value; remove => Object.Something -= value; } } @@ -21,28 +18,25 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public readonly struct IFoo_MockEvents { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public readonly struct IFoo_MockEvents - { - internal readonly global::TUnit.Mocks.MockEngine Engine; + internal readonly global::TUnit.Mocks.MockEngine Engine; - internal IFoo_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; - } + internal IFoo_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; +} - public static class IFoo_MockEventsExtensions +public static class IFoo_MockEventsExtensions +{ + extension(global::TUnit.Mocks.Mock mock) { - extension(global::TUnit.Mocks.Mock mock) - { - public IFoo_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); - } + public IFoo_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); + } - extension(IFoo_MockEvents events) - { - public global::TUnit.Mocks.EventSubscriptionAccessor Something - => new(events.Engine, "Something"); - } + extension(IFoo_MockEvents events) + { + public global::TUnit.Mocks.EventSubscriptionAccessor Something + => new(events.Engine, "Something"); } } @@ -53,76 +47,73 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IFooMockImpl : global::IFoo, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IFooMockImpl : global::IFoo, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IFooMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IFooMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - private global::System.EventHandler? _backing_Something; + private global::System.EventHandler? _backing_Something; - public event global::System.EventHandler? Something - { - add { _backing_Something += value; _engine.RecordEventSubscription("Something", true); } - remove { _backing_Something -= value; _engine.RecordEventSubscription("Something", false); } - } + public event global::System.EventHandler? Something + { + add { _backing_Something += value; _engine.RecordEventSubscription("Something", true); } + remove { _backing_Something -= value; _engine.RecordEventSubscription("Something", false); } + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal void Raise_Something(string e) - { - _backing_Something?.Invoke(this, e); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal void Raise_Something(string e) + { + _backing_Something?.Invoke(this, e); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + switch (eventName) { - switch (eventName) - { - case "Something": - { - Raise_Something((string)args!); - break; - } - default: - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + case "Something": + { + Raise_Something((string)args!); + break; + } + default: + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } } +} - internal static class IFooMockFactory +internal static class IFooMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IFooMockImpl(engine); - engine.Raisable = impl; - var mock = new IFooMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IFooMockImpl(engine); + engine.Raisable = impl; + var mock = new IFooMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IFoo' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IFooMockImpl(engine); - engine.Raisable = impl; - var mock = new IFooMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IFoo' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IFooMockImpl(engine); + engine.Raisable = impl; + var mock = new IFooMock(impl, engine); + return mock; } } @@ -133,14 +124,11 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IFoo_MockMemberExtensions { - public static class IFoo_MockMemberExtensions + public static void RaiseSomething(this global::TUnit.Mocks.Mock mock, string e) { - public static void RaiseSomething(this global::TUnit.Mocks.Mock mock, string e) - { - ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("Something", (object?)e); - } + ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("Something", (object?)e); } } @@ -157,9 +145,9 @@ namespace TUnit.Mocks { extension(global::IFoo _) { - public static global::TUnit.Mocks.Generated.IFooMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IFooMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IFooMock)global::TUnit.Mocks.Generated.IFooMockFactory.CreateAutoMock(behavior); + return (global::IFooMock)global::IFooMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Event_And_Nullable_Args.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Event_And_Nullable_Args.verified.txt index d8a1a29413..f5fa0fef3d 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Event_And_Nullable_Args.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Event_And_Nullable_Args.verified.txt @@ -2,16 +2,13 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IFooMock : global::TUnit.Mocks.Mock, global::IFoo { - public sealed class IFooMock : global::TUnit.Mocks.Mock, global::IFoo - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IFooMock(global::IFoo mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IFooMock(global::IFoo mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - event global::System.EventHandler? global::IFoo.Something { add => Object.Something += value; remove => Object.Something -= value; } - } + event global::System.EventHandler? global::IFoo.Something { add => Object.Something += value; remove => Object.Something -= value; } } @@ -21,28 +18,25 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public readonly struct IFoo_MockEvents { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public readonly struct IFoo_MockEvents - { - internal readonly global::TUnit.Mocks.MockEngine Engine; + internal readonly global::TUnit.Mocks.MockEngine Engine; - internal IFoo_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; - } + internal IFoo_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; +} - public static class IFoo_MockEventsExtensions +public static class IFoo_MockEventsExtensions +{ + extension(global::TUnit.Mocks.Mock mock) { - extension(global::TUnit.Mocks.Mock mock) - { - public IFoo_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); - } + public IFoo_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); + } - extension(IFoo_MockEvents events) - { - public global::TUnit.Mocks.EventSubscriptionAccessor Something - => new(events.Engine, "Something"); - } + extension(IFoo_MockEvents events) + { + public global::TUnit.Mocks.EventSubscriptionAccessor Something + => new(events.Engine, "Something"); } } @@ -53,76 +47,73 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IFooMockImpl : global::IFoo, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IFooMockImpl : global::IFoo, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IFooMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IFooMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - private global::System.EventHandler? _backing_Something; + private global::System.EventHandler? _backing_Something; - public event global::System.EventHandler? Something - { - add { _backing_Something += value; _engine.RecordEventSubscription("Something", true); } - remove { _backing_Something -= value; _engine.RecordEventSubscription("Something", false); } - } + public event global::System.EventHandler? Something + { + add { _backing_Something += value; _engine.RecordEventSubscription("Something", true); } + remove { _backing_Something -= value; _engine.RecordEventSubscription("Something", false); } + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal void Raise_Something(string? e) - { - _backing_Something?.Invoke(this, e); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal void Raise_Something(string? e) + { + _backing_Something?.Invoke(this, e); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + switch (eventName) { - switch (eventName) - { - case "Something": - { - Raise_Something((string?)args!); - break; - } - default: - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + case "Something": + { + Raise_Something((string?)args!); + break; + } + default: + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } } +} - internal static class IFooMockFactory +internal static class IFooMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IFooMockImpl(engine); - engine.Raisable = impl; - var mock = new IFooMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IFooMockImpl(engine); + engine.Raisable = impl; + var mock = new IFooMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IFoo' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IFooMockImpl(engine); - engine.Raisable = impl; - var mock = new IFooMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IFoo' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IFooMockImpl(engine); + engine.Raisable = impl; + var mock = new IFooMock(impl, engine); + return mock; } } @@ -133,14 +124,11 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IFoo_MockMemberExtensions { - public static class IFoo_MockMemberExtensions + public static void RaiseSomething(this global::TUnit.Mocks.Mock mock, string? e) { - public static void RaiseSomething(this global::TUnit.Mocks.Mock mock, string? e) - { - ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("Something", (object?)e); - } + ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("Something", (object?)e); } } @@ -157,9 +145,9 @@ namespace TUnit.Mocks { extension(global::IFoo _) { - public static global::TUnit.Mocks.Generated.IFooMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IFooMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IFooMock)global::TUnit.Mocks.Generated.IFooMockFactory.CreateAutoMock(behavior); + return (global::IFooMock)global::IFooMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Event_Args.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Event_Args.verified.txt index 10b1fa7cc6..78104c6342 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Event_Args.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Event_Args.verified.txt @@ -2,16 +2,13 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IFooMock : global::TUnit.Mocks.Mock, global::IFoo { - public sealed class IFooMock : global::TUnit.Mocks.Mock, global::IFoo - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IFooMock(global::IFoo mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IFooMock(global::IFoo mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - event global::System.EventHandler global::IFoo.Something { add => Object.Something += value; remove => Object.Something -= value; } - } + event global::System.EventHandler global::IFoo.Something { add => Object.Something += value; remove => Object.Something -= value; } } @@ -21,28 +18,25 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public readonly struct IFoo_MockEvents { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public readonly struct IFoo_MockEvents - { - internal readonly global::TUnit.Mocks.MockEngine Engine; + internal readonly global::TUnit.Mocks.MockEngine Engine; - internal IFoo_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; - } + internal IFoo_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; +} - public static class IFoo_MockEventsExtensions +public static class IFoo_MockEventsExtensions +{ + extension(global::TUnit.Mocks.Mock mock) { - extension(global::TUnit.Mocks.Mock mock) - { - public IFoo_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); - } + public IFoo_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); + } - extension(IFoo_MockEvents events) - { - public global::TUnit.Mocks.EventSubscriptionAccessor Something - => new(events.Engine, "Something"); - } + extension(IFoo_MockEvents events) + { + public global::TUnit.Mocks.EventSubscriptionAccessor Something + => new(events.Engine, "Something"); } } @@ -53,76 +47,73 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IFooMockImpl : global::IFoo, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IFooMockImpl : global::IFoo, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IFooMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IFooMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - private global::System.EventHandler? _backing_Something; + private global::System.EventHandler? _backing_Something; - public event global::System.EventHandler? Something - { - add { _backing_Something += value; _engine.RecordEventSubscription("Something", true); } - remove { _backing_Something -= value; _engine.RecordEventSubscription("Something", false); } - } + public event global::System.EventHandler? Something + { + add { _backing_Something += value; _engine.RecordEventSubscription("Something", true); } + remove { _backing_Something -= value; _engine.RecordEventSubscription("Something", false); } + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal void Raise_Something(string? e) - { - _backing_Something?.Invoke(this, e); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal void Raise_Something(string? e) + { + _backing_Something?.Invoke(this, e); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + switch (eventName) { - switch (eventName) - { - case "Something": - { - Raise_Something((string?)args!); - break; - } - default: - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + case "Something": + { + Raise_Something((string?)args!); + break; + } + default: + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } } +} - internal static class IFooMockFactory +internal static class IFooMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IFooMockImpl(engine); - engine.Raisable = impl; - var mock = new IFooMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IFooMockImpl(engine); + engine.Raisable = impl; + var mock = new IFooMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IFoo' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IFooMockImpl(engine); - engine.Raisable = impl; - var mock = new IFooMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IFoo' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IFooMockImpl(engine); + engine.Raisable = impl; + var mock = new IFooMock(impl, engine); + return mock; } } @@ -133,14 +124,11 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IFoo_MockMemberExtensions { - public static class IFoo_MockMemberExtensions + public static void RaiseSomething(this global::TUnit.Mocks.Mock mock, string? e) { - public static void RaiseSomething(this global::TUnit.Mocks.Mock mock, string? e) - { - ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("Something", (object?)e); - } + ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("Something", (object?)e); } } @@ -157,9 +145,9 @@ namespace TUnit.Mocks { extension(global::IFoo _) { - public static global::TUnit.Mocks.Generated.IFooMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IFooMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IFooMock)global::TUnit.Mocks.Generated.IFooMockFactory.CreateAutoMock(behavior); + return (global::IFooMock)global::IFooMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Reference_Type_Parameters.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Reference_Type_Parameters.verified.txt index 35417093fc..5d0c9ffe42 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Reference_Type_Parameters.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Nullable_Reference_Type_Parameters.verified.txt @@ -2,30 +2,27 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IFooMock : global::TUnit.Mocks.Mock, global::IFoo { - public sealed class IFooMock : global::TUnit.Mocks.Mock, global::IFoo - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IFooMock(global::IFoo mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IFooMock(global::IFoo mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - void global::IFoo.Bar(object? baz) - { - Object.Bar(baz); - } + void global::IFoo.Bar(object? baz) + { + Object.Bar(baz); + } - string? global::IFoo.GetValue(string? key, int count) => Object.GetValue(key, count); + string? global::IFoo.GetValue(string? key, int count) => Object.GetValue(key, count); - void global::IFoo.Process(string nonNull, string? nullable, object? obj) - { - Object.Process(nonNull, nullable, obj); - } + void global::IFoo.Process(string nonNull, string? nullable, object? obj) + { + Object.Process(nonNull, nullable, obj); + } - global::System.Threading.Tasks.Task global::IFoo.GetAsync(string? key) => Object.GetAsync(key); + global::System.Threading.Tasks.Task global::IFoo.GetAsync(string? key) => Object.GetAsync(key); - string? global::IFoo.NullableProp { get => Object.NullableProp; set => Object.NullableProp = value; } - } + string? global::IFoo.NullableProp { get => Object.NullableProp; set => Object.NullableProp = value; } } @@ -35,92 +32,89 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IFooMockImpl : global::IFoo, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IFooMockImpl : global::IFoo, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IFooMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IFooMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public void Bar(object? baz) - { - _engine.HandleCall(0, "Bar", baz); - } + public void Bar(object? baz) + { + _engine.HandleCall(0, "Bar", baz); + } - public string? GetValue(string? key, int count) - { - return _engine.HandleCallWithReturn(1, "GetValue", key, count, default); - } + public string? GetValue(string? key, int count) + { + return _engine.HandleCallWithReturn(1, "GetValue", key, count, default); + } - public void Process(string nonNull, string? nullable, object? obj) - { - _engine.HandleCall(2, "Process", nonNull, nullable, obj); - } + public void Process(string nonNull, string? nullable, object? obj) + { + _engine.HandleCall(2, "Process", nonNull, nullable, obj); + } - public global::System.Threading.Tasks.Task GetAsync(string? key) + public global::System.Threading.Tasks.Task GetAsync(string? key) + { + try { - try - { - var __result = _engine.HandleCallWithReturn(3, "GetAsync", key, default); - if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) - { - if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; - throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); - } - return global::System.Threading.Tasks.Task.FromResult(__result); - } - catch (global::System.Exception __ex) + var __result = _engine.HandleCallWithReturn(3, "GetAsync", key, default); + if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) { - return global::System.Threading.Tasks.Task.FromException(__ex); + if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; + throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); } + return global::System.Threading.Tasks.Task.FromResult(__result); } - - public string? NullableProp + catch (global::System.Exception __ex) { - get => _engine.HandleCallWithReturn(4, "get_NullableProp", global::System.Array.Empty(), default); - set => _engine.HandleCall(5, "set_NullableProp", new object?[] { value }); + return global::System.Threading.Tasks.Task.FromException(__ex); } + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + public string? NullableProp + { + get => _engine.HandleCallWithReturn(4, "get_NullableProp", global::System.Array.Empty(), default); + set => _engine.HandleCall(5, "set_NullableProp", new object?[] { value }); } - internal static class IFooMockFactory + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); + } +} - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IFooMockImpl(engine); - engine.Raisable = impl; - var mock = new IFooMock(impl, engine); - return mock; - } +internal static class IFooMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() + { + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IFoo' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IFooMockImpl(engine); - engine.Raisable = impl; - var mock = new IFooMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IFooMockImpl(engine); + engine.Raisable = impl; + var mock = new IFooMock(impl, engine); + return mock; + } + + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IFoo' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IFooMockImpl(engine); + engine.Raisable = impl; + var mock = new IFooMock(impl, engine); + return mock; } } @@ -131,496 +125,493 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IFoo_MockMemberExtensions { - public static class IFoo_MockMemberExtensions + public static IFoo_Bar_M0_MockCall Bar(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg baz) { - public static IFoo_Bar_M0_MockCall Bar(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg baz) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { baz.Matcher }; - return new IFoo_Bar_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Bar", matchers); - } - - public static IFoo_Bar_M0_MockCall Bar(this global::TUnit.Mocks.Mock mock, global::System.Func baz) - { - global::TUnit.Mocks.Arguments.Arg __fa_baz = baz; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_baz.Matcher }; - return new IFoo_Bar_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Bar", matchers); - } - - public static IFoo_GetValue_M1_MockCall GetValue(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg key, global::TUnit.Mocks.Arguments.Arg count) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { key.Matcher, count.Matcher }; - return new IFoo_GetValue_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetValue", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { baz.Matcher }; + return new IFoo_Bar_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Bar", matchers); + } - public static IFoo_GetValue_M1_MockCall GetValue(this global::TUnit.Mocks.Mock mock, global::System.Func key, global::TUnit.Mocks.Arguments.Arg count) - { - global::TUnit.Mocks.Arguments.Arg __fa_key = key; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_key.Matcher, count.Matcher }; - return new IFoo_GetValue_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetValue", matchers); - } + public static IFoo_Bar_M0_MockCall Bar(this global::TUnit.Mocks.Mock mock, global::System.Func baz) + { + global::TUnit.Mocks.Arguments.Arg __fa_baz = baz; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_baz.Matcher }; + return new IFoo_Bar_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Bar", matchers); + } - public static IFoo_GetValue_M1_MockCall GetValue(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg key, global::System.Func count) - { - global::TUnit.Mocks.Arguments.Arg __fa_count = count; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { key.Matcher, __fa_count.Matcher }; - return new IFoo_GetValue_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetValue", matchers); - } + public static IFoo_GetValue_M1_MockCall GetValue(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg key, global::TUnit.Mocks.Arguments.Arg count) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { key.Matcher, count.Matcher }; + return new IFoo_GetValue_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetValue", matchers); + } - public static IFoo_GetValue_M1_MockCall GetValue(this global::TUnit.Mocks.Mock mock, global::System.Func key, global::System.Func count) - { - global::TUnit.Mocks.Arguments.Arg __fa_key = key; - global::TUnit.Mocks.Arguments.Arg __fa_count = count; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_key.Matcher, __fa_count.Matcher }; - return new IFoo_GetValue_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetValue", matchers); - } + public static IFoo_GetValue_M1_MockCall GetValue(this global::TUnit.Mocks.Mock mock, global::System.Func key, global::TUnit.Mocks.Arguments.Arg count) + { + global::TUnit.Mocks.Arguments.Arg __fa_key = key; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_key.Matcher, count.Matcher }; + return new IFoo_GetValue_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetValue", matchers); + } - /// Configure the mock setup for GetValue with every argument matched as Any<T>(). - public static IFoo_GetValue_M1_MockCall GetValue(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.AnyArgs _) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance }; - return new IFoo_GetValue_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetValue", matchers); - } + public static IFoo_GetValue_M1_MockCall GetValue(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg key, global::System.Func count) + { + global::TUnit.Mocks.Arguments.Arg __fa_count = count; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { key.Matcher, __fa_count.Matcher }; + return new IFoo_GetValue_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetValue", matchers); + } - public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg nonNull, global::TUnit.Mocks.Arguments.Arg nullable, global::TUnit.Mocks.Arguments.Arg obj) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { nonNull.Matcher, nullable.Matcher, obj.Matcher }; - return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); - } + public static IFoo_GetValue_M1_MockCall GetValue(this global::TUnit.Mocks.Mock mock, global::System.Func key, global::System.Func count) + { + global::TUnit.Mocks.Arguments.Arg __fa_key = key; + global::TUnit.Mocks.Arguments.Arg __fa_count = count; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_key.Matcher, __fa_count.Matcher }; + return new IFoo_GetValue_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetValue", matchers); + } - public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::System.Func nonNull, global::TUnit.Mocks.Arguments.Arg nullable, global::TUnit.Mocks.Arguments.Arg obj) - { - global::TUnit.Mocks.Arguments.Arg __fa_nonNull = nonNull; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_nonNull.Matcher, nullable.Matcher, obj.Matcher }; - return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); - } + /// Configure the mock setup for GetValue with every argument matched as Any<T>(). + public static IFoo_GetValue_M1_MockCall GetValue(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.AnyArgs _) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance }; + return new IFoo_GetValue_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetValue", matchers); + } - public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg nonNull, global::System.Func nullable, global::TUnit.Mocks.Arguments.Arg obj) - { - global::TUnit.Mocks.Arguments.Arg __fa_nullable = nullable; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { nonNull.Matcher, __fa_nullable.Matcher, obj.Matcher }; - return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); - } + public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg nonNull, global::TUnit.Mocks.Arguments.Arg nullable, global::TUnit.Mocks.Arguments.Arg obj) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { nonNull.Matcher, nullable.Matcher, obj.Matcher }; + return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); + } - public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::System.Func nonNull, global::System.Func nullable, global::TUnit.Mocks.Arguments.Arg obj) - { - global::TUnit.Mocks.Arguments.Arg __fa_nonNull = nonNull; - global::TUnit.Mocks.Arguments.Arg __fa_nullable = nullable; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_nonNull.Matcher, __fa_nullable.Matcher, obj.Matcher }; - return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); - } + public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::System.Func nonNull, global::TUnit.Mocks.Arguments.Arg nullable, global::TUnit.Mocks.Arguments.Arg obj) + { + global::TUnit.Mocks.Arguments.Arg __fa_nonNull = nonNull; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_nonNull.Matcher, nullable.Matcher, obj.Matcher }; + return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); + } - public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg nonNull, global::TUnit.Mocks.Arguments.Arg nullable, global::System.Func obj) - { - global::TUnit.Mocks.Arguments.Arg __fa_obj = obj; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { nonNull.Matcher, nullable.Matcher, __fa_obj.Matcher }; - return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); - } + public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg nonNull, global::System.Func nullable, global::TUnit.Mocks.Arguments.Arg obj) + { + global::TUnit.Mocks.Arguments.Arg __fa_nullable = nullable; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { nonNull.Matcher, __fa_nullable.Matcher, obj.Matcher }; + return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); + } - public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::System.Func nonNull, global::TUnit.Mocks.Arguments.Arg nullable, global::System.Func obj) - { - global::TUnit.Mocks.Arguments.Arg __fa_nonNull = nonNull; - global::TUnit.Mocks.Arguments.Arg __fa_obj = obj; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_nonNull.Matcher, nullable.Matcher, __fa_obj.Matcher }; - return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); - } + public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::System.Func nonNull, global::System.Func nullable, global::TUnit.Mocks.Arguments.Arg obj) + { + global::TUnit.Mocks.Arguments.Arg __fa_nonNull = nonNull; + global::TUnit.Mocks.Arguments.Arg __fa_nullable = nullable; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_nonNull.Matcher, __fa_nullable.Matcher, obj.Matcher }; + return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); + } - public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg nonNull, global::System.Func nullable, global::System.Func obj) - { - global::TUnit.Mocks.Arguments.Arg __fa_nullable = nullable; - global::TUnit.Mocks.Arguments.Arg __fa_obj = obj; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { nonNull.Matcher, __fa_nullable.Matcher, __fa_obj.Matcher }; - return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); - } + public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg nonNull, global::TUnit.Mocks.Arguments.Arg nullable, global::System.Func obj) + { + global::TUnit.Mocks.Arguments.Arg __fa_obj = obj; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { nonNull.Matcher, nullable.Matcher, __fa_obj.Matcher }; + return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); + } - public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::System.Func nonNull, global::System.Func nullable, global::System.Func obj) - { - global::TUnit.Mocks.Arguments.Arg __fa_nonNull = nonNull; - global::TUnit.Mocks.Arguments.Arg __fa_nullable = nullable; - global::TUnit.Mocks.Arguments.Arg __fa_obj = obj; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_nonNull.Matcher, __fa_nullable.Matcher, __fa_obj.Matcher }; - return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); - } + public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::System.Func nonNull, global::TUnit.Mocks.Arguments.Arg nullable, global::System.Func obj) + { + global::TUnit.Mocks.Arguments.Arg __fa_nonNull = nonNull; + global::TUnit.Mocks.Arguments.Arg __fa_obj = obj; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_nonNull.Matcher, nullable.Matcher, __fa_obj.Matcher }; + return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); + } - /// Configure the mock setup for Process with every argument matched as Any<T>(). - public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.AnyArgs _) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance }; - return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); - } + public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg nonNull, global::System.Func nullable, global::System.Func obj) + { + global::TUnit.Mocks.Arguments.Arg __fa_nullable = nullable; + global::TUnit.Mocks.Arguments.Arg __fa_obj = obj; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { nonNull.Matcher, __fa_nullable.Matcher, __fa_obj.Matcher }; + return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); + } - public static IFoo_GetAsync_M3_MockCall GetAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg key) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { key.Matcher }; - return new IFoo_GetAsync_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "GetAsync", matchers); - } + public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::System.Func nonNull, global::System.Func nullable, global::System.Func obj) + { + global::TUnit.Mocks.Arguments.Arg __fa_nonNull = nonNull; + global::TUnit.Mocks.Arguments.Arg __fa_nullable = nullable; + global::TUnit.Mocks.Arguments.Arg __fa_obj = obj; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_nonNull.Matcher, __fa_nullable.Matcher, __fa_obj.Matcher }; + return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); + } - public static IFoo_GetAsync_M3_MockCall GetAsync(this global::TUnit.Mocks.Mock mock, global::System.Func key) - { - global::TUnit.Mocks.Arguments.Arg __fa_key = key; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_key.Matcher }; - return new IFoo_GetAsync_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "GetAsync", matchers); - } + /// Configure the mock setup for Process with every argument matched as Any<T>(). + public static IFoo_Process_M2_MockCall Process(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.AnyArgs _) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance }; + return new IFoo_Process_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Process", matchers); + } - extension(global::TUnit.Mocks.Mock mock) - { - public global::TUnit.Mocks.PropertyMockCall NullableProp - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 4, 5, "NullableProp", true, true); - } + public static IFoo_GetAsync_M3_MockCall GetAsync(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg key) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { key.Matcher }; + return new IFoo_GetAsync_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "GetAsync", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFoo_Bar_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public static IFoo_GetAsync_M3_MockCall GetAsync(this global::TUnit.Mocks.Mock mock, global::System.Func key) { - 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; + global::TUnit.Mocks.Arguments.Arg __fa_key = key; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_key.Matcher }; + return new IFoo_GetAsync_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "GetAsync", matchers); + } - 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(); - } + extension(global::TUnit.Mocks.Mock mock) + { + public global::TUnit.Mocks.PropertyMockCall NullableProp + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 4, 5, "NullableProp", true, true); + } +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IFoo_Bar_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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(); + } - /// - 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; } - - /// Execute a typed callback using the actual method parameters. - public IFoo_Bar_M0_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + 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(); + } - /// Configure a typed computed exception using the actual method parameters. - public IFoo_Bar_M0_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((object?)args[0])); - return this; - } + [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; + } - // 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); + /// + 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; } + + /// Execute a typed callback using the actual method parameters. + public IFoo_Bar_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFoo_GetValue_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public IFoo_Bar_M0_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((object?)args[0])); + return this; + } - 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; - } + // 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IFoo_GetValue_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public IFoo_GetValue_M1_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((string?)args[0], (int)args[1]!)); - return this; - } + 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(); + } - /// Execute a typed callback using the actual method parameters. - public IFoo_GetValue_M1_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public IFoo_GetValue_M1_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string?)args[0], (int)args[1]!)); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public IFoo_GetValue_M1_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((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); + /// Execute a typed callback using the actual method parameters. + public IFoo_GetValue_M1_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFoo_Process_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public IFoo_GetValue_M1_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((string?)args[0], (int)args[1]!)); + return this; + } - 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(); - } + // 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IFoo_Process_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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(); + } - /// - 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; } - - /// Execute a typed callback using the actual method parameters. - public IFoo_Process_M2_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + 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(); + } - /// Configure a typed computed exception using the actual method parameters. - public IFoo_Process_M2_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string)args[0]!, (string?)args[1], (object?)args[2])); - return this; - } + [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; + } - // 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); + /// + 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; } + + /// Execute a typed callback using the actual method parameters. + public IFoo_Process_M2_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFoo_GetAsync_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public IFoo_Process_M2_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((string)args[0]!, (string?)args[1], (object?)args[2])); + return this; + } - 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; - } + // 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IFoo_GetAsync_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// 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; } - /// Return a pre-built Task from a factory, invoked on each call. - public IFoo_GetAsync_M3_MockCall ReturnsAsync(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } - - /// Configure a typed computed return value using the actual method parameters. - public IFoo_GetAsync_M3_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((string?)args[0])); - return this; - } + 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(); + } - /// Configure a typed computed async return value using the actual method parameters. - public IFoo_GetAsync_M3_MockCall ReturnsAsync(global::System.Func> factory) - { - EnsureSetup().ReturnsRaw(args => (object?)factory((string?)args[0])); - return this; - } + [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; + } - /// Execute a typed callback using the actual method parameters. - public IFoo_GetAsync_M3_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + /// + 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; } + + /// 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; } + /// Return a pre-built Task from a factory, invoked on each call. + public IFoo_GetAsync_M3_MockCall ReturnsAsync(global::System.Func> taskFactory) { EnsureSetup().ReturnsRaw(() => (object?)taskFactory()); return this; } + + /// Configure a typed computed return value using the actual method parameters. + public IFoo_GetAsync_M3_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((string?)args[0])); + return this; + } - /// Configure a typed computed exception using the actual method parameters. - public IFoo_GetAsync_M3_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string?)args[0])); - return this; - } + /// Configure a typed computed async return value using the actual method parameters. + public IFoo_GetAsync_M3_MockCall ReturnsAsync(global::System.Func> factory) + { + EnsureSetup().ReturnsRaw(args => (object?)factory((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); + /// Execute a typed callback using the actual method parameters. + public IFoo_GetAsync_M3_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } + + /// Configure a typed computed exception using the actual method parameters. + public IFoo_GetAsync_M3_MockCall Throws(global::System.Func exceptionFactory) + { + 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); } @@ -636,9 +627,9 @@ namespace TUnit.Mocks { extension(global::IFoo _) { - public static global::TUnit.Mocks.Generated.IFooMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IFooMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IFooMock)global::TUnit.Mocks.Generated.IFooMockFactory.CreateAutoMock(behavior); + return (global::IFooMock)global::IFooMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Obsolete_DiagnosticId_NamedArgs.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Obsolete_DiagnosticId_NamedArgs.verified.txt index 911b486ce6..3d9294c4f2 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Obsolete_DiagnosticId_NamedArgs.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Obsolete_DiagnosticId_NamedArgs.verified.txt @@ -2,17 +2,14 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IDeprecatedApiMock : global::TUnit.Mocks.Mock, global::IDeprecatedApi { - public sealed class IDeprecatedApiMock : global::TUnit.Mocks.Mock, global::IDeprecatedApi - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IDeprecatedApiMock(global::IDeprecatedApi mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IDeprecatedApiMock(global::IDeprecatedApi mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - [global::System.Obsolete("Replaced", DiagnosticId = "CUSTOM001", UrlFormat = "https://example.test/{0}")] - string? global::IDeprecatedApi.WithDiagnosticId() => Object.WithDiagnosticId(); - } + [global::System.Obsolete("Replaced", DiagnosticId = "CUSTOM001", UrlFormat = "https://example.test/{0}")] + string? global::IDeprecatedApi.WithDiagnosticId() => Object.WithDiagnosticId(); } @@ -22,59 +19,56 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IDeprecatedApiMockImpl : global::IDeprecatedApi, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IDeprecatedApiMockImpl : global::IDeprecatedApi, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IDeprecatedApiMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IDeprecatedApiMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - [global::System.Obsolete("Replaced", DiagnosticId = "CUSTOM001", UrlFormat = "https://example.test/{0}")] - public string? WithDiagnosticId() - { - return _engine.HandleCallWithReturn(0, "WithDiagnosticId", global::System.Array.Empty(), default); - } + [global::System.Obsolete("Replaced", DiagnosticId = "CUSTOM001", UrlFormat = "https://example.test/{0}")] + public string? WithDiagnosticId() + { + return _engine.HandleCallWithReturn(0, "WithDiagnosticId", global::System.Array.Empty(), default); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class IDeprecatedApiMockFactory +internal static class IDeprecatedApiMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IDeprecatedApiMockImpl(engine); - engine.Raisable = impl; - var mock = new IDeprecatedApiMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IDeprecatedApiMockImpl(engine); + engine.Raisable = impl; + var mock = new IDeprecatedApiMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IDeprecatedApi' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IDeprecatedApiMockImpl(engine); - engine.Raisable = impl; - var mock = new IDeprecatedApiMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IDeprecatedApi' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IDeprecatedApiMockImpl(engine); + engine.Raisable = impl; + var mock = new IDeprecatedApiMock(impl, engine); + return mock; } } @@ -85,15 +79,12 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IDeprecatedApi_MockMemberExtensions { - public static class IDeprecatedApi_MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall WithDiagnosticId(this global::TUnit.Mocks.Mock mock) { - public static global::TUnit.Mocks.MockMethodCall WithDiagnosticId(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "WithDiagnosticId", matchers); - } + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "WithDiagnosticId", matchers); } } @@ -110,9 +101,9 @@ namespace TUnit.Mocks { extension(global::IDeprecatedApi _) { - public static global::TUnit.Mocks.Generated.IDeprecatedApiMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IDeprecatedApiMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IDeprecatedApiMock)global::TUnit.Mocks.Generated.IDeprecatedApiMockFactory.CreateAutoMock(behavior); + return (global::IDeprecatedApiMock)global::IDeprecatedApiMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Obsolete_Members.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Obsolete_Members.verified.txt index 998c0655e0..3485eb7b1e 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Obsolete_Members.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Obsolete_Members.verified.txt @@ -2,54 +2,51 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class BaseDialogMockImpl : global::BaseDialog, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class BaseDialogMockImpl : global::BaseDialog, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] - internal BaseDialogMockImpl(global::TUnit.Mocks.MockEngine engine) : base() - { - _engine = engine; - } + [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] + internal BaseDialogMockImpl(global::TUnit.Mocks.MockEngine engine) : base() + { + _engine = engine; + } - [global::System.Obsolete] - public override string? Compute(string? input) + [global::System.Obsolete] + public override string? Compute(string? input) + { + if (_engine.TryHandleCallWithReturn(0, "Compute", input, default, out var __result)) { - if (_engine.TryHandleCallWithReturn(0, "Compute", input, default, out var __result)) - { - return __result; - } - return base.Compute(input); + return __result; } + return base.Compute(input); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - file static class BaseDialogPartialMockFactory +file static class BaseDialogPartialMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new BaseDialogMockImpl(engine); - engine.Raisable = impl; - var mock = new global::TUnit.Mocks.Mock(impl, engine); - return mock; - } + private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new BaseDialogMockImpl(engine); + engine.Raisable = impl; + var mock = new global::TUnit.Mocks.Mock(impl, engine); + return mock; } } @@ -60,113 +57,110 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class BaseDialog_MockMemberExtensions { - public static class BaseDialog_MockMemberExtensions + public static BaseDialog_Compute_M0_MockCall Compute(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg input) { - public static BaseDialog_Compute_M0_MockCall Compute(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg input) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { input.Matcher }; - return new BaseDialog_Compute_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Compute", matchers); - } - - public static BaseDialog_Compute_M0_MockCall Compute(this global::TUnit.Mocks.Mock mock, global::System.Func input) - { - global::TUnit.Mocks.Arguments.Arg __fa_input = input; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_input.Matcher }; - return new BaseDialog_Compute_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Compute", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { input.Matcher }; + return new BaseDialog_Compute_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Compute", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class BaseDialog_Compute_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public static BaseDialog_Compute_M0_MockCall Compute(this global::TUnit.Mocks.Mock mock, global::System.Func input) { - 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; + global::TUnit.Mocks.Arguments.Arg __fa_input = input; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_input.Matcher }; + return new BaseDialog_Compute_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Compute", matchers); + } +} - 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; - } +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class BaseDialog_Compute_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - 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(); - } + 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; + } - [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; - } + 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(); + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public BaseDialog_Compute_M0_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((string?)args[0])); - return this; - } + [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; + } - /// Execute a typed callback using the actual method parameters. - public BaseDialog_Compute_M0_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public BaseDialog_Compute_M0_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((string?)args[0])); + return this; + } - /// Configure a typed computed exception using the actual method parameters. - public BaseDialog_Compute_M0_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string?)args[0])); - return this; - } + /// Execute a typed callback using the actual method parameters. + public BaseDialog_Compute_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + 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); + /// Configure a typed computed exception using the actual method parameters. + public BaseDialog_Compute_M0_MockCall Throws(global::System.Func exceptionFactory) + { + 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); } @@ -197,32 +191,29 @@ namespace TUnit.Mocks #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IDialogServiceMock : global::TUnit.Mocks.Mock, global::IDialogService { - public sealed class IDialogServiceMock : global::TUnit.Mocks.Mock, global::IDialogService - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IDialogServiceMock(global::IDialogService mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IDialogServiceMock(global::IDialogService mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - [global::System.Obsolete("Use ShowAsync(options) instead")] - string? global::IDialogService.Show(string? title, string? message) => Object.Show(title, message); + [global::System.Obsolete("Use ShowAsync(options) instead")] + string? global::IDialogService.Show(string? title, string? message) => Object.Show(title, message); - [global::System.Obsolete] - global::System.Threading.Tasks.Task global::IDialogService.ShowPanel(TData? data) where TData : class => Object.ShowPanel(data); + [global::System.Obsolete] + global::System.Threading.Tasks.Task global::IDialogService.ShowPanel(TData? data) where TData : class => Object.ShowPanel(data); - [global::System.Obsolete("Use \"NewMethod\" in C:\\New\\Path")] - string? global::IDialogService.WithTrickyChars() => Object.WithTrickyChars(); + [global::System.Obsolete("Use \"NewMethod\" in C:\\New\\Path")] + string? global::IDialogService.WithTrickyChars() => Object.WithTrickyChars(); - string? global::IDialogService.Greeting { [global::System.Obsolete] get => Object.Greeting; [global::System.Obsolete] set => Object.Greeting = value; } + string? global::IDialogService.Greeting { [global::System.Obsolete] get => Object.Greeting; [global::System.Obsolete] set => Object.Greeting = value; } - string? global::IDialogService.Headline { [global::System.Obsolete] get => Object.Headline; set => Object.Headline = value; } + string? global::IDialogService.Headline { [global::System.Obsolete] get => Object.Headline; set => Object.Headline = value; } - string? global::IDialogService.Subtitle { get => Object.Subtitle; [global::System.Obsolete] set => Object.Subtitle = value; } + string? global::IDialogService.Subtitle { get => Object.Subtitle; [global::System.Obsolete] set => Object.Subtitle = value; } - [global::System.Obsolete("Removed", true)] - event global::System.EventHandler? global::IDialogService.Opened { add => Object.Opened += value; remove => Object.Opened -= value; } - } + [global::System.Obsolete("Removed", true)] + event global::System.EventHandler? global::IDialogService.Opened { add => Object.Opened += value; remove => Object.Opened -= value; } } @@ -232,28 +223,25 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public readonly struct IDialogService_MockEvents { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public readonly struct IDialogService_MockEvents - { - internal readonly global::TUnit.Mocks.MockEngine Engine; + internal readonly global::TUnit.Mocks.MockEngine Engine; - internal IDialogService_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; - } + internal IDialogService_MockEvents(global::TUnit.Mocks.MockEngine engine) => Engine = engine; +} - public static class IDialogService_MockEventsExtensions +public static class IDialogService_MockEventsExtensions +{ + extension(global::TUnit.Mocks.Mock mock) { - extension(global::TUnit.Mocks.Mock mock) - { - public IDialogService_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); - } + public IDialogService_MockEvents Events => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock)); + } - extension(IDialogService_MockEvents events) - { - public global::TUnit.Mocks.EventSubscriptionAccessor Opened - => new(events.Engine, "Opened"); - } + extension(IDialogService_MockEvents events) + { + public global::TUnit.Mocks.EventSubscriptionAccessor Opened + => new(events.Engine, "Opened"); } } @@ -264,130 +252,127 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IDialogServiceMockImpl : global::IDialogService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IDialogServiceMockImpl : global::IDialogService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IDialogServiceMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IDialogServiceMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - [global::System.Obsolete("Use ShowAsync(options) instead")] - public string? Show(string? title, string? message) - { - return _engine.HandleCallWithReturn(0, "Show", title, message, default); - } + [global::System.Obsolete("Use ShowAsync(options) instead")] + public string? Show(string? title, string? message) + { + return _engine.HandleCallWithReturn(0, "Show", title, message, default); + } - [global::System.Obsolete] - public global::System.Threading.Tasks.Task ShowPanel(TData? data) where TData : class + [global::System.Obsolete] + public global::System.Threading.Tasks.Task ShowPanel(TData? data) where TData : class + { + try { - try + var __result = _engine.HandleCallWithReturn(1, "ShowPanel", data, default); + if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) { - var __result = _engine.HandleCallWithReturn(1, "ShowPanel", data, default); - if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) - { - if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; - throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); - } - return global::System.Threading.Tasks.Task.FromResult(__result); - } - catch (global::System.Exception __ex) - { - return global::System.Threading.Tasks.Task.FromException(__ex); + if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; + throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); } + return global::System.Threading.Tasks.Task.FromResult(__result); } - - [global::System.Obsolete("Use \"NewMethod\" in C:\\New\\Path")] - public string? WithTrickyChars() + catch (global::System.Exception __ex) { - return _engine.HandleCallWithReturn(8, "WithTrickyChars", global::System.Array.Empty(), default); + return global::System.Threading.Tasks.Task.FromException(__ex); } + } - public string? Greeting - { - [global::System.Obsolete] - get => _engine.HandleCallWithReturn(2, "get_Greeting", global::System.Array.Empty(), default); - [global::System.Obsolete] - set => _engine.HandleCall(3, "set_Greeting", new object?[] { value }); - } + [global::System.Obsolete("Use \"NewMethod\" in C:\\New\\Path")] + public string? WithTrickyChars() + { + return _engine.HandleCallWithReturn(8, "WithTrickyChars", global::System.Array.Empty(), default); + } - public string? Headline - { - [global::System.Obsolete] - get => _engine.HandleCallWithReturn(4, "get_Headline", global::System.Array.Empty(), default); - set => _engine.HandleCall(5, "set_Headline", new object?[] { value }); - } + public string? Greeting + { + [global::System.Obsolete] + get => _engine.HandleCallWithReturn(2, "get_Greeting", global::System.Array.Empty(), default); + [global::System.Obsolete] + set => _engine.HandleCall(3, "set_Greeting", new object?[] { value }); + } - public string? Subtitle - { - get => _engine.HandleCallWithReturn(6, "get_Subtitle", global::System.Array.Empty(), default); - [global::System.Obsolete] - set => _engine.HandleCall(7, "set_Subtitle", new object?[] { value }); - } + public string? Headline + { + [global::System.Obsolete] + get => _engine.HandleCallWithReturn(4, "get_Headline", global::System.Array.Empty(), default); + set => _engine.HandleCall(5, "set_Headline", new object?[] { value }); + } - private global::System.EventHandler? _backing_Opened; + public string? Subtitle + { + get => _engine.HandleCallWithReturn(6, "get_Subtitle", global::System.Array.Empty(), default); + [global::System.Obsolete] + set => _engine.HandleCall(7, "set_Subtitle", new object?[] { value }); + } - [global::System.Obsolete("Removed", true)] - public event global::System.EventHandler? Opened - { - add { _backing_Opened += value; _engine.RecordEventSubscription("Opened", true); } - remove { _backing_Opened -= value; _engine.RecordEventSubscription("Opened", false); } - } + private global::System.EventHandler? _backing_Opened; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal void Raise_Opened(string? e) - { - _backing_Opened?.Invoke(this, e); - } + [global::System.Obsolete("Removed", true)] + public event global::System.EventHandler? Opened + { + add { _backing_Opened += value; _engine.RecordEventSubscription("Opened", true); } + remove { _backing_Opened -= value; _engine.RecordEventSubscription("Opened", false); } + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - switch (eventName) - { - case "Opened": - { - Raise_Opened((string?)args!); - break; - } - default: - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal void Raise_Opened(string? e) + { + _backing_Opened?.Invoke(this, e); } - internal static class IDialogServiceMockFactory + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() + switch (eventName) { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + case "Opened": + { + Raise_Opened((string?)args!); + break; + } + default: + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } + } +} - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IDialogServiceMockImpl(engine); - engine.Raisable = impl; - var mock = new IDialogServiceMock(impl, engine); - return mock; - } +internal static class IDialogServiceMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() + { + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IDialogService' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IDialogServiceMockImpl(engine); - engine.Raisable = impl; - var mock = new IDialogServiceMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IDialogServiceMockImpl(engine); + engine.Raisable = impl; + var mock = new IDialogServiceMock(impl, engine); + return mock; + } + + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IDialogService' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IDialogServiceMockImpl(engine); + engine.Raisable = impl; + var mock = new IDialogServiceMock(impl, engine); + return mock; } } @@ -398,246 +383,243 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IDialogService_MockMemberExtensions { - public static class IDialogService_MockMemberExtensions + public static IDialogService_Show_M0_MockCall Show(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg title, global::TUnit.Mocks.Arguments.Arg message) { - public static IDialogService_Show_M0_MockCall Show(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg title, global::TUnit.Mocks.Arguments.Arg message) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { title.Matcher, message.Matcher }; - return new IDialogService_Show_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Show", matchers); - } - - public static IDialogService_Show_M0_MockCall Show(this global::TUnit.Mocks.Mock mock, global::System.Func title, global::TUnit.Mocks.Arguments.Arg message) - { - global::TUnit.Mocks.Arguments.Arg __fa_title = title; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_title.Matcher, message.Matcher }; - return new IDialogService_Show_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Show", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { title.Matcher, message.Matcher }; + return new IDialogService_Show_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Show", matchers); + } - public static IDialogService_Show_M0_MockCall Show(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg title, global::System.Func message) - { - global::TUnit.Mocks.Arguments.Arg __fa_message = message; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { title.Matcher, __fa_message.Matcher }; - return new IDialogService_Show_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Show", matchers); - } + public static IDialogService_Show_M0_MockCall Show(this global::TUnit.Mocks.Mock mock, global::System.Func title, global::TUnit.Mocks.Arguments.Arg message) + { + global::TUnit.Mocks.Arguments.Arg __fa_title = title; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_title.Matcher, message.Matcher }; + return new IDialogService_Show_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Show", matchers); + } - public static IDialogService_Show_M0_MockCall Show(this global::TUnit.Mocks.Mock mock, global::System.Func title, global::System.Func message) - { - global::TUnit.Mocks.Arguments.Arg __fa_title = title; - global::TUnit.Mocks.Arguments.Arg __fa_message = message; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_title.Matcher, __fa_message.Matcher }; - return new IDialogService_Show_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Show", matchers); - } + public static IDialogService_Show_M0_MockCall Show(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg title, global::System.Func message) + { + global::TUnit.Mocks.Arguments.Arg __fa_message = message; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { title.Matcher, __fa_message.Matcher }; + return new IDialogService_Show_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Show", matchers); + } - /// Configure the mock setup for Show with every argument matched as Any<T>(). - public static IDialogService_Show_M0_MockCall Show(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.AnyArgs _) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance }; - return new IDialogService_Show_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Show", matchers); - } + public static IDialogService_Show_M0_MockCall Show(this global::TUnit.Mocks.Mock mock, global::System.Func title, global::System.Func message) + { + global::TUnit.Mocks.Arguments.Arg __fa_title = title; + global::TUnit.Mocks.Arguments.Arg __fa_message = message; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_title.Matcher, __fa_message.Matcher }; + return new IDialogService_Show_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Show", matchers); + } - public static global::TUnit.Mocks.MockMethodCall ShowPanel(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg data) where TData : class - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { data.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "ShowPanel", matchers); - } + /// Configure the mock setup for Show with every argument matched as Any<T>(). + public static IDialogService_Show_M0_MockCall Show(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.AnyArgs _) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance }; + return new IDialogService_Show_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Show", matchers); + } - public static global::TUnit.Mocks.MockMethodCall ShowPanel(this global::TUnit.Mocks.Mock mock, global::System.Func data) where TData : class - { - global::TUnit.Mocks.Arguments.Arg __fa_data = data; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_data.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "ShowPanel", matchers); - } + public static global::TUnit.Mocks.MockMethodCall ShowPanel(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg data) where TData : class + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { data.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "ShowPanel", matchers); + } - public static IDialogService_WithTrickyChars_M8_MockCall WithTrickyChars(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new IDialogService_WithTrickyChars_M8_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 8, "WithTrickyChars", matchers); - } + public static global::TUnit.Mocks.MockMethodCall ShowPanel(this global::TUnit.Mocks.Mock mock, global::System.Func data) where TData : class + { + global::TUnit.Mocks.Arguments.Arg __fa_data = data; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_data.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "ShowPanel", matchers); + } - extension(global::TUnit.Mocks.Mock mock) - { - public global::TUnit.Mocks.PropertyMockCall Greeting - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, 3, "Greeting", true, true); + public static IDialogService_WithTrickyChars_M8_MockCall WithTrickyChars(this global::TUnit.Mocks.Mock mock) + { + var matchers = global::System.Array.Empty(); + return new IDialogService_WithTrickyChars_M8_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 8, "WithTrickyChars", matchers); + } - public global::TUnit.Mocks.PropertyMockCall Headline - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 4, 5, "Headline", true, true); + extension(global::TUnit.Mocks.Mock mock) + { + public global::TUnit.Mocks.PropertyMockCall Greeting + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, 3, "Greeting", true, true); - public global::TUnit.Mocks.PropertyMockCall Subtitle - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 6, 7, "Subtitle", true, true); - } + public global::TUnit.Mocks.PropertyMockCall Headline + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 4, 5, "Headline", true, true); - public static void RaiseOpened(this global::TUnit.Mocks.Mock mock, string? e) - { - ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("Opened", (object?)e); - } + public global::TUnit.Mocks.PropertyMockCall Subtitle + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 6, 7, "Subtitle", true, true); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IDialogService_Show_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public static void RaiseOpened(this global::TUnit.Mocks.Mock mock, string? e) { - 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::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("Opened", (object?)e); + } +} - [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; - } +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IDialogService_Show_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public IDialogService_Show_M0_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((string?)args[0], (string?)args[1])); - return this; - } + 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; + } - /// Execute a typed callback using the actual method parameters. - public IDialogService_Show_M0_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + 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(); + } - /// Configure a typed computed exception using the actual method parameters. - public IDialogService_Show_M0_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string?)args[0], (string?)args[1])); - return this; - } + [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; + } - /// 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; } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public IDialogService_Show_M0_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((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); + /// Execute a typed callback using the actual method parameters. + public IDialogService_Show_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IDialogService_WithTrickyChars_M8_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public IDialogService_Show_M0_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((string?)args[0], (string?)args[1])); + return this; + } - 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; - } + /// 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IDialogService_WithTrickyChars_M8_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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(); + } - /// - 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; } - - /// 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); + [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; } + + /// 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); } @@ -653,9 +635,9 @@ namespace TUnit.Mocks { extension(global::IDialogService _) { - public static global::TUnit.Mocks.Generated.IDialogServiceMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IDialogServiceMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IDialogServiceMock)global::TUnit.Mocks.Generated.IDialogServiceMockFactory.CreateAutoMock(behavior); + return (global::IDialogServiceMock)global::IDialogServiceMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Out_Ref_Parameters.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Out_Ref_Parameters.verified.txt index 01124abc66..7e318a2a91 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Out_Ref_Parameters.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Out_Ref_Parameters.verified.txt @@ -2,20 +2,17 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IDictionaryMock : global::TUnit.Mocks.Mock, global::IDictionary { - public sealed class IDictionaryMock : global::TUnit.Mocks.Mock, global::IDictionary - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IDictionaryMock(global::IDictionary mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IDictionaryMock(global::IDictionary mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - bool global::IDictionary.TryGetValue(string key, out string value) => Object.TryGetValue(key, out value); + bool global::IDictionary.TryGetValue(string key, out string value) => Object.TryGetValue(key, out value); - void global::IDictionary.Swap(ref int a, ref int b) - { - Object.Swap(ref a, ref b); - } + void global::IDictionary.Swap(ref int a, ref int b) + { + Object.Swap(ref a, ref b); } } @@ -26,76 +23,73 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IDictionaryMockImpl : global::IDictionary, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IDictionaryMockImpl : global::IDictionary, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IDictionaryMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IDictionaryMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public bool TryGetValue(string key, out string value) + public bool TryGetValue(string key, out string value) + { + value = default!; + var __result = _engine.HandleCallWithReturn(0, "TryGetValue", key, default); + var __outRef = global::TUnit.Mocks.Setup.OutRefContext.Consume(); + if (__outRef is not null) { - value = default!; - var __result = _engine.HandleCallWithReturn(0, "TryGetValue", key, default); - var __outRef = global::TUnit.Mocks.Setup.OutRefContext.Consume(); - if (__outRef is not null) - { - if (__outRef.TryGetValue(1, out var __v1)) value = (string)__v1!; - } - return __result; + if (__outRef.TryGetValue(1, out var __v1)) value = (string)__v1!; } + return __result; + } - public void Swap(ref int a, ref int b) + public void Swap(ref int a, ref int b) + { + _engine.HandleCall(1, "Swap", a, b); + var __outRef = global::TUnit.Mocks.Setup.OutRefContext.Consume(); + if (__outRef is not null) { - _engine.HandleCall(1, "Swap", a, b); - var __outRef = global::TUnit.Mocks.Setup.OutRefContext.Consume(); - if (__outRef is not null) - { - if (__outRef.TryGetValue(0, out var __v0)) a = (int)__v0!; - if (__outRef.TryGetValue(1, out var __v1)) b = (int)__v1!; - } + if (__outRef.TryGetValue(0, out var __v0)) a = (int)__v0!; + if (__outRef.TryGetValue(1, out var __v1)) b = (int)__v1!; } + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class IDictionaryMockFactory +internal static class IDictionaryMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IDictionaryMockImpl(engine); - engine.Raisable = impl; - var mock = new IDictionaryMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IDictionaryMockImpl(engine); + engine.Raisable = impl; + var mock = new IDictionaryMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IDictionary' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IDictionaryMockImpl(engine); - engine.Raisable = impl; - var mock = new IDictionaryMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IDictionary' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IDictionaryMockImpl(engine); + engine.Raisable = impl; + var mock = new IDictionaryMock(impl, engine); + return mock; } } @@ -106,207 +100,204 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IDictionary_MockMemberExtensions { - public static class IDictionary_MockMemberExtensions + public static IDictionary_TryGetValue_M0_MockCall TryGetValue(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg key) { - public static IDictionary_TryGetValue_M0_MockCall TryGetValue(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg key) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { key.Matcher }; - return new IDictionary_TryGetValue_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "TryGetValue", matchers); - } - - public static IDictionary_TryGetValue_M0_MockCall TryGetValue(this global::TUnit.Mocks.Mock mock, global::System.Func key) - { - global::TUnit.Mocks.Arguments.Arg __fa_key = key; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_key.Matcher }; - return new IDictionary_TryGetValue_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "TryGetValue", matchers); - } - - public static IDictionary_Swap_M1_MockCall Swap(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg a, global::TUnit.Mocks.Arguments.Arg b) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { a.Matcher, b.Matcher }; - return new IDictionary_Swap_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Swap", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { key.Matcher }; + return new IDictionary_TryGetValue_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "TryGetValue", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IDictionary_TryGetValue_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public static IDictionary_TryGetValue_M0_MockCall TryGetValue(this global::TUnit.Mocks.Mock mock, global::System.Func key) { - 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; + global::TUnit.Mocks.Arguments.Arg __fa_key = key; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_key.Matcher }; + return new IDictionary_TryGetValue_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "TryGetValue", matchers); + } - 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; - } + public static IDictionary_Swap_M1_MockCall Swap(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg a, global::TUnit.Mocks.Arguments.Arg b) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { a.Matcher, b.Matcher }; + return new IDictionary_Swap_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Swap", 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IDictionary_TryGetValue_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public IDictionary_TryGetValue_M0_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((string)args[0]!)); - return this; - } + 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(); + } - /// Execute a typed callback using the actual method parameters. - public IDictionary_TryGetValue_M0_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public IDictionary_TryGetValue_M0_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public IDictionary_TryGetValue_M0_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((string)args[0]!)); + return this; + } - /// 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); + /// Execute a typed callback using the actual method parameters. + public IDictionary_TryGetValue_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IDictionary_Swap_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public IDictionary_TryGetValue_M0_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); + return this; + } - 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(); - } + /// 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IDictionary_Swap_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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(); + } - /// - 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; } - - /// Execute a typed callback using the actual method parameters. - public IDictionary_Swap_M1_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + 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(); + } - /// Configure a typed computed exception using the actual method parameters. - public IDictionary_Swap_M1_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((int)args[0]!, (int)args[1]!)); - return this; - } + [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; + } - /// Sets the 'a' ref parameter to the specified value when this setup matches. - 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); + /// + 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; } + + /// Execute a typed callback using the actual method parameters. + public IDictionary_Swap_M1_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } + + /// Configure a typed computed exception using the actual method parameters. + public IDictionary_Swap_M1_MockCall Throws(global::System.Func exceptionFactory) + { + EnsureSetup().Throws(args => exceptionFactory((int)args[0]!, (int)args[1]!)); + return this; + } + + /// Sets the 'a' ref parameter to the specified value when this setup matches. + 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); } @@ -322,9 +313,9 @@ namespace TUnit.Mocks { extension(global::IDictionary _) { - public static global::TUnit.Mocks.Generated.IDictionaryMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IDictionaryMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IDictionaryMock)global::TUnit.Mocks.Generated.IDictionaryMockFactory.CreateAutoMock(behavior); + return (global::IDictionaryMock)global::IDictionaryMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Overloaded_Methods.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Overloaded_Methods.verified.txt index c80cd9cf35..4c7a84fa8f 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Overloaded_Methods.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Overloaded_Methods.verified.txt @@ -2,22 +2,19 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IFormatterMock : global::TUnit.Mocks.Mock, global::IFormatter { - public sealed class IFormatterMock : global::TUnit.Mocks.Mock, global::IFormatter - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IFormatterMock(global::IFormatter mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IFormatterMock(global::IFormatter mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - string global::IFormatter.Format(string value) => Object.Format(value); + string global::IFormatter.Format(string value) => Object.Format(value); - string global::IFormatter.Format(int value) => Object.Format(value); + string global::IFormatter.Format(int value) => Object.Format(value); - string global::IFormatter.Format(string template, string arg1) => Object.Format(template, arg1); + string global::IFormatter.Format(string template, string arg1) => Object.Format(template, arg1); - string global::IFormatter.Format(string template, string arg1, string arg2) => Object.Format(template, arg1, arg2); - } + string global::IFormatter.Format(string template, string arg1, string arg2) => Object.Format(template, arg1, arg2); } @@ -27,73 +24,70 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IFormatterMockImpl : global::IFormatter, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IFormatterMockImpl : global::IFormatter, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IFormatterMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IFormatterMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public string Format(string value) - { - return _engine.HandleCallWithReturn(0, "Format", value, ""); - } + public string Format(string value) + { + return _engine.HandleCallWithReturn(0, "Format", value, ""); + } - public string Format(int value) - { - return _engine.HandleCallWithReturn(1, "Format", value, ""); - } + public string Format(int value) + { + return _engine.HandleCallWithReturn(1, "Format", value, ""); + } - public string Format(string template, string arg1) - { - return _engine.HandleCallWithReturn(2, "Format", template, arg1, ""); - } + public string Format(string template, string arg1) + { + return _engine.HandleCallWithReturn(2, "Format", template, arg1, ""); + } - public string Format(string template, string arg1, string arg2) - { - return _engine.HandleCallWithReturn(3, "Format", template, arg1, arg2, ""); - } + public string Format(string template, string arg1, string arg2) + { + return _engine.HandleCallWithReturn(3, "Format", template, arg1, arg2, ""); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class IFormatterMockFactory +internal static class IFormatterMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IFormatterMockImpl(engine); - engine.Raisable = impl; - var mock = new IFormatterMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IFormatterMockImpl(engine); + engine.Raisable = impl; + var mock = new IFormatterMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IFormatter' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IFormatterMockImpl(engine); - engine.Raisable = impl; - var mock = new IFormatterMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IFormatter' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IFormatterMockImpl(engine); + engine.Raisable = impl; + var mock = new IFormatterMock(impl, engine); + return mock; } } @@ -104,484 +98,481 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IFormatter_MockMemberExtensions { - public static class IFormatter_MockMemberExtensions + public static IFormatter_Format_M0_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg value) { - public static IFormatter_Format_M0_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg value) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { value.Matcher }; - return new IFormatter_Format_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Format", matchers); - } - - public static IFormatter_Format_M0_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func value) - { - global::TUnit.Mocks.Arguments.Arg __fa_value = value; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_value.Matcher }; - return new IFormatter_Format_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Format", matchers); - } - - public static IFormatter_Format_M1_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg value) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { value.Matcher }; - return new IFormatter_Format_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Format", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { value.Matcher }; + return new IFormatter_Format_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Format", matchers); + } - public static IFormatter_Format_M1_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func value) - { - global::TUnit.Mocks.Arguments.Arg __fa_value = value; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_value.Matcher }; - return new IFormatter_Format_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Format", matchers); - } + public static IFormatter_Format_M0_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func value) + { + global::TUnit.Mocks.Arguments.Arg __fa_value = value; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_value.Matcher }; + return new IFormatter_Format_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Format", matchers); + } - public static IFormatter_Format_M2_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg template, global::TUnit.Mocks.Arguments.Arg arg1) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { template.Matcher, arg1.Matcher }; - return new IFormatter_Format_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Format", matchers); - } + public static IFormatter_Format_M1_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg value) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { value.Matcher }; + return new IFormatter_Format_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Format", matchers); + } - public static IFormatter_Format_M2_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func template, global::TUnit.Mocks.Arguments.Arg arg1) - { - global::TUnit.Mocks.Arguments.Arg __fa_template = template; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_template.Matcher, arg1.Matcher }; - return new IFormatter_Format_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Format", matchers); - } + public static IFormatter_Format_M1_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func value) + { + global::TUnit.Mocks.Arguments.Arg __fa_value = value; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_value.Matcher }; + return new IFormatter_Format_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Format", matchers); + } - public static IFormatter_Format_M2_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg template, global::System.Func arg1) - { - global::TUnit.Mocks.Arguments.Arg __fa_arg1 = arg1; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { template.Matcher, __fa_arg1.Matcher }; - return new IFormatter_Format_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Format", matchers); - } + public static IFormatter_Format_M2_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg template, global::TUnit.Mocks.Arguments.Arg arg1) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { template.Matcher, arg1.Matcher }; + return new IFormatter_Format_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Format", matchers); + } - public static IFormatter_Format_M2_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func template, global::System.Func arg1) - { - global::TUnit.Mocks.Arguments.Arg __fa_template = template; - global::TUnit.Mocks.Arguments.Arg __fa_arg1 = arg1; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_template.Matcher, __fa_arg1.Matcher }; - return new IFormatter_Format_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Format", matchers); - } + public static IFormatter_Format_M2_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func template, global::TUnit.Mocks.Arguments.Arg arg1) + { + global::TUnit.Mocks.Arguments.Arg __fa_template = template; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_template.Matcher, arg1.Matcher }; + return new IFormatter_Format_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Format", matchers); + } - public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg template, global::TUnit.Mocks.Arguments.Arg arg1, global::TUnit.Mocks.Arguments.Arg arg2) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { template.Matcher, arg1.Matcher, arg2.Matcher }; - return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); - } + public static IFormatter_Format_M2_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg template, global::System.Func arg1) + { + global::TUnit.Mocks.Arguments.Arg __fa_arg1 = arg1; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { template.Matcher, __fa_arg1.Matcher }; + return new IFormatter_Format_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Format", matchers); + } - public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func template, global::TUnit.Mocks.Arguments.Arg arg1, global::TUnit.Mocks.Arguments.Arg arg2) - { - global::TUnit.Mocks.Arguments.Arg __fa_template = template; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_template.Matcher, arg1.Matcher, arg2.Matcher }; - return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); - } + public static IFormatter_Format_M2_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func template, global::System.Func arg1) + { + global::TUnit.Mocks.Arguments.Arg __fa_template = template; + global::TUnit.Mocks.Arguments.Arg __fa_arg1 = arg1; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_template.Matcher, __fa_arg1.Matcher }; + return new IFormatter_Format_M2_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Format", matchers); + } - public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg template, global::System.Func arg1, global::TUnit.Mocks.Arguments.Arg arg2) - { - global::TUnit.Mocks.Arguments.Arg __fa_arg1 = arg1; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { template.Matcher, __fa_arg1.Matcher, arg2.Matcher }; - return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); - } + public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg template, global::TUnit.Mocks.Arguments.Arg arg1, global::TUnit.Mocks.Arguments.Arg arg2) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { template.Matcher, arg1.Matcher, arg2.Matcher }; + return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); + } - public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func template, global::System.Func arg1, global::TUnit.Mocks.Arguments.Arg arg2) - { - global::TUnit.Mocks.Arguments.Arg __fa_template = template; - global::TUnit.Mocks.Arguments.Arg __fa_arg1 = arg1; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_template.Matcher, __fa_arg1.Matcher, arg2.Matcher }; - return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); - } + public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func template, global::TUnit.Mocks.Arguments.Arg arg1, global::TUnit.Mocks.Arguments.Arg arg2) + { + global::TUnit.Mocks.Arguments.Arg __fa_template = template; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_template.Matcher, arg1.Matcher, arg2.Matcher }; + return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); + } - public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg template, global::TUnit.Mocks.Arguments.Arg arg1, global::System.Func arg2) - { - global::TUnit.Mocks.Arguments.Arg __fa_arg2 = arg2; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { template.Matcher, arg1.Matcher, __fa_arg2.Matcher }; - return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); - } + public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg template, global::System.Func arg1, global::TUnit.Mocks.Arguments.Arg arg2) + { + global::TUnit.Mocks.Arguments.Arg __fa_arg1 = arg1; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { template.Matcher, __fa_arg1.Matcher, arg2.Matcher }; + return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); + } - public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func template, global::TUnit.Mocks.Arguments.Arg arg1, global::System.Func arg2) - { - global::TUnit.Mocks.Arguments.Arg __fa_template = template; - global::TUnit.Mocks.Arguments.Arg __fa_arg2 = arg2; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_template.Matcher, arg1.Matcher, __fa_arg2.Matcher }; - return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); - } + public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func template, global::System.Func arg1, global::TUnit.Mocks.Arguments.Arg arg2) + { + global::TUnit.Mocks.Arguments.Arg __fa_template = template; + global::TUnit.Mocks.Arguments.Arg __fa_arg1 = arg1; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_template.Matcher, __fa_arg1.Matcher, arg2.Matcher }; + return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); + } - public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg template, global::System.Func arg1, global::System.Func arg2) - { - global::TUnit.Mocks.Arguments.Arg __fa_arg1 = arg1; - global::TUnit.Mocks.Arguments.Arg __fa_arg2 = arg2; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { template.Matcher, __fa_arg1.Matcher, __fa_arg2.Matcher }; - return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); - } + public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg template, global::TUnit.Mocks.Arguments.Arg arg1, global::System.Func arg2) + { + global::TUnit.Mocks.Arguments.Arg __fa_arg2 = arg2; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { template.Matcher, arg1.Matcher, __fa_arg2.Matcher }; + return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); + } - public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func template, global::System.Func arg1, global::System.Func arg2) - { - global::TUnit.Mocks.Arguments.Arg __fa_template = template; - global::TUnit.Mocks.Arguments.Arg __fa_arg1 = arg1; - global::TUnit.Mocks.Arguments.Arg __fa_arg2 = arg2; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_template.Matcher, __fa_arg1.Matcher, __fa_arg2.Matcher }; - return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); - } + public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func template, global::TUnit.Mocks.Arguments.Arg arg1, global::System.Func arg2) + { + global::TUnit.Mocks.Arguments.Arg __fa_template = template; + global::TUnit.Mocks.Arguments.Arg __fa_arg2 = arg2; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_template.Matcher, arg1.Matcher, __fa_arg2.Matcher }; + return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFormatter_Format_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg template, global::System.Func arg1, global::System.Func arg2) { - 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; + global::TUnit.Mocks.Arguments.Arg __fa_arg1 = arg1; + global::TUnit.Mocks.Arguments.Arg __fa_arg2 = arg2; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { template.Matcher, __fa_arg1.Matcher, __fa_arg2.Matcher }; + return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", matchers); + } - 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; - } + public static IFormatter_Format_M3_MockCall Format(this global::TUnit.Mocks.Mock mock, global::System.Func template, global::System.Func arg1, global::System.Func arg2) + { + global::TUnit.Mocks.Arguments.Arg __fa_template = template; + global::TUnit.Mocks.Arguments.Arg __fa_arg1 = arg1; + global::TUnit.Mocks.Arguments.Arg __fa_arg2 = arg2; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_template.Matcher, __fa_arg1.Matcher, __fa_arg2.Matcher }; + return new IFormatter_Format_M3_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "Format", 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IFormatter_Format_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public IFormatter_Format_M0_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((string)args[0]!)); - return this; - } + 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(); + } - /// Execute a typed callback using the actual method parameters. - public IFormatter_Format_M0_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public IFormatter_Format_M0_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public IFormatter_Format_M0_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((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); + /// Execute a typed callback using the actual method parameters. + public IFormatter_Format_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFormatter_Format_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public IFormatter_Format_M0_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); + return this; + } - 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; - } + // 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IFormatter_Format_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public IFormatter_Format_M1_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((int)args[0]!)); - return this; - } + 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(); + } - /// Execute a typed callback using the actual method parameters. - public IFormatter_Format_M1_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public IFormatter_Format_M1_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public IFormatter_Format_M1_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((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); + /// Execute a typed callback using the actual method parameters. + public IFormatter_Format_M1_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFormatter_Format_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public IFormatter_Format_M1_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((int)args[0]!)); + return this; + } - 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; - } + // 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IFormatter_Format_M2_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public IFormatter_Format_M2_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((string)args[0]!, (string)args[1]!)); - return this; - } + 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(); + } - /// Execute a typed callback using the actual method parameters. - public IFormatter_Format_M2_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public IFormatter_Format_M2_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string)args[0]!, (string)args[1]!)); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public IFormatter_Format_M2_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((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); + /// Execute a typed callback using the actual method parameters. + public IFormatter_Format_M2_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IFormatter_Format_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public IFormatter_Format_M2_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((string)args[0]!, (string)args[1]!)); + return this; + } - 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; - } + // 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IFormatter_Format_M3_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public IFormatter_Format_M3_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((string)args[0]!, (string)args[1]!, (string)args[2]!)); - return this; - } + 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(); + } - /// Execute a typed callback using the actual method parameters. - public IFormatter_Format_M3_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public IFormatter_Format_M3_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string)args[0]!, (string)args[1]!, (string)args[2]!)); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public IFormatter_Format_M3_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((string)args[0]!, (string)args[1]!, (string)args[2]!)); + return this; + } + + /// Execute a typed callback using the actual method parameters. + public IFormatter_Format_M3_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + 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); + /// Configure a typed computed exception using the actual method parameters. + public IFormatter_Format_M3_MockCall Throws(global::System.Func exceptionFactory) + { + 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); } @@ -597,9 +588,9 @@ namespace TUnit.Mocks { extension(global::IFormatter _) { - public static global::TUnit.Mocks.Generated.IFormatterMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IFormatterMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IFormatterMock)global::TUnit.Mocks.Generated.IFormatterMockFactory.CreateAutoMock(behavior); + return (global::IFormatterMock)global::IFormatterMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Properties.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Properties.verified.txt index 970f8963a9..61a687c750 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Properties.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Properties.verified.txt @@ -2,20 +2,17 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IRepositoryMock : global::TUnit.Mocks.Mock, global::IRepository { - public sealed class IRepositoryMock : global::TUnit.Mocks.Mock, global::IRepository - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IRepositoryMock(global::IRepository mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IRepositoryMock(global::IRepository mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - string global::IRepository.Name { get => Object.Name; set => Object.Name = value; } + string global::IRepository.Name { get => Object.Name; set => Object.Name = value; } - int global::IRepository.Count { get => Object.Count; } + int global::IRepository.Count { get => Object.Count; } - bool global::IRepository.IsOpen { set => Object.IsOpen = value; } - } + bool global::IRepository.IsOpen { set => Object.IsOpen = value; } } @@ -25,69 +22,66 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IRepositoryMockImpl : global::IRepository, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IRepositoryMockImpl : global::IRepository, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IRepositoryMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IRepositoryMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public string Name - { - get => _engine.HandleCallWithReturn(0, "get_Name", global::System.Array.Empty(), ""); - set => _engine.HandleCall(1, "set_Name", new object?[] { value }); - } + public string Name + { + get => _engine.HandleCallWithReturn(0, "get_Name", global::System.Array.Empty(), ""); + set => _engine.HandleCall(1, "set_Name", new object?[] { value }); + } - public int Count - { - get => _engine.HandleCallWithReturn(2, "get_Count", global::System.Array.Empty(), default); - } + public int Count + { + get => _engine.HandleCallWithReturn(2, "get_Count", global::System.Array.Empty(), default); + } - public bool IsOpen - { - set => _engine.HandleCall(4, "set_IsOpen", new object?[] { value }); - } + public bool IsOpen + { + set => _engine.HandleCall(4, "set_IsOpen", new object?[] { value }); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class IRepositoryMockFactory +internal static class IRepositoryMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IRepositoryMockImpl(engine); - engine.Raisable = impl; - var mock = new IRepositoryMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IRepositoryMockImpl(engine); + engine.Raisable = impl; + var mock = new IRepositoryMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IRepository' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IRepositoryMockImpl(engine); - engine.Raisable = impl; - var mock = new IRepositoryMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IRepository' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IRepositoryMockImpl(engine); + engine.Raisable = impl; + var mock = new IRepositoryMock(impl, engine); + return mock; } } @@ -98,21 +92,18 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IRepository_MockMemberExtensions { - public static class IRepository_MockMemberExtensions + extension(global::TUnit.Mocks.Mock mock) { - extension(global::TUnit.Mocks.Mock mock) - { - public global::TUnit.Mocks.PropertyMockCall Name - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, 1, "Name", true, true); + public global::TUnit.Mocks.PropertyMockCall Name + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, 1, "Name", true, true); - public global::TUnit.Mocks.PropertyMockCall Count - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, 0, "Count", true, false); + public global::TUnit.Mocks.PropertyMockCall Count + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, 0, "Count", true, false); - public global::TUnit.Mocks.PropertyMockCall IsOpen - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, 4, "IsOpen", false, true); - } + public global::TUnit.Mocks.PropertyMockCall IsOpen + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, 4, "IsOpen", false, true); } } @@ -129,9 +120,9 @@ namespace TUnit.Mocks { extension(global::IRepository _) { - public static global::TUnit.Mocks.Generated.IRepositoryMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IRepositoryMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IRepositoryMock)global::TUnit.Mocks.Generated.IRepositoryMockFactory.CreateAutoMock(behavior); + return (global::IRepositoryMock)global::IRepositoryMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_RefStruct_Parameters.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_RefStruct_Parameters.verified.txt index bf76d6c72a..21e1e6ea60 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_RefStruct_Parameters.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_RefStruct_Parameters.verified.txt @@ -2,23 +2,20 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IBufferProcessorMock : global::TUnit.Mocks.Mock, global::IBufferProcessor { - public sealed class IBufferProcessorMock : global::TUnit.Mocks.Mock, global::IBufferProcessor - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IBufferProcessorMock(global::IBufferProcessor mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IBufferProcessorMock(global::IBufferProcessor mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - void global::IBufferProcessor.Process(global::System.ReadOnlySpan data) - { - Object.Process(data); - } + void global::IBufferProcessor.Process(global::System.ReadOnlySpan data) + { + Object.Process(data); + } - int global::IBufferProcessor.Parse(global::System.ReadOnlySpan text) => Object.Parse(text); + int global::IBufferProcessor.Parse(global::System.ReadOnlySpan text) => Object.Parse(text); - string global::IBufferProcessor.GetName() => Object.GetName(); - } + string global::IBufferProcessor.GetName() => Object.GetName(); } @@ -28,78 +25,75 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IBufferProcessorMockImpl : global::IBufferProcessor, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IBufferProcessorMockImpl : global::IBufferProcessor, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IBufferProcessorMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IBufferProcessorMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public void Process(global::System.ReadOnlySpan data) - { - #if NET9_0_OR_GREATER - var __args = new object?[] { null }; - #else - var __args = global::System.Array.Empty(); - #endif - _engine.HandleCall(0, "Process", __args); - } + public void Process(global::System.ReadOnlySpan data) + { + #if NET9_0_OR_GREATER + var __args = new object?[] { null }; + #else + var __args = global::System.Array.Empty(); + #endif + _engine.HandleCall(0, "Process", __args); + } - public int Parse(global::System.ReadOnlySpan text) - { - #if NET9_0_OR_GREATER - var __args = new object?[] { null }; - #else - var __args = global::System.Array.Empty(); - #endif - return _engine.HandleCallWithReturn(1, "Parse", __args, default); - } + public int Parse(global::System.ReadOnlySpan text) + { + #if NET9_0_OR_GREATER + var __args = new object?[] { null }; + #else + var __args = global::System.Array.Empty(); + #endif + return _engine.HandleCallWithReturn(1, "Parse", __args, default); + } - public string GetName() - { - return _engine.HandleCallWithReturn(2, "GetName", global::System.Array.Empty(), ""); - } + public string GetName() + { + return _engine.HandleCallWithReturn(2, "GetName", global::System.Array.Empty(), ""); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class IBufferProcessorMockFactory +internal static class IBufferProcessorMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IBufferProcessorMockImpl(engine); - engine.Raisable = impl; - var mock = new IBufferProcessorMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IBufferProcessorMockImpl(engine); + engine.Raisable = impl; + var mock = new IBufferProcessorMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IBufferProcessor' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IBufferProcessorMockImpl(engine); - engine.Raisable = impl; - var mock = new IBufferProcessorMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IBufferProcessor' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IBufferProcessorMockImpl(engine); + engine.Raisable = impl; + var mock = new IBufferProcessorMock(impl, engine); + return mock; } } @@ -110,43 +104,40 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IBufferProcessor_MockMemberExtensions { - public static class IBufferProcessor_MockMemberExtensions + #if NET9_0_OR_GREATER + public static global::TUnit.Mocks.VoidMockMethodCall Process(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.RefStructArg> data) { - #if NET9_0_OR_GREATER - public static global::TUnit.Mocks.VoidMockMethodCall Process(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.RefStructArg> data) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { data.Matcher }; - return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Process", matchers); - } - #else - public static global::TUnit.Mocks.VoidMockMethodCall Process(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Process", matchers); - } - #endif + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { data.Matcher }; + return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Process", matchers); + } + #else + public static global::TUnit.Mocks.VoidMockMethodCall Process(this global::TUnit.Mocks.Mock mock) + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Process", matchers); + } + #endif - #if NET9_0_OR_GREATER - public static global::TUnit.Mocks.MockMethodCall Parse(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.RefStructArg> text) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { text.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Parse", matchers); - } - #else - public static global::TUnit.Mocks.MockMethodCall Parse(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Parse", matchers); - } - #endif + #if NET9_0_OR_GREATER + public static global::TUnit.Mocks.MockMethodCall Parse(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.RefStructArg> text) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { text.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Parse", matchers); + } + #else + public static global::TUnit.Mocks.MockMethodCall Parse(this global::TUnit.Mocks.Mock mock) + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Parse", matchers); + } + #endif - public static global::TUnit.Mocks.MockMethodCall GetName(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "GetName", matchers); - } + public static global::TUnit.Mocks.MockMethodCall GetName(this global::TUnit.Mocks.Mock mock) + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "GetName", matchers); } } @@ -163,9 +154,9 @@ namespace TUnit.Mocks { extension(global::IBufferProcessor _) { - public static global::TUnit.Mocks.Generated.IBufferProcessorMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IBufferProcessorMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IBufferProcessorMock)global::TUnit.Mocks.Generated.IBufferProcessorMockFactory.CreateAutoMock(behavior); + return (global::IBufferProcessorMock)global::IBufferProcessorMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Static_Abstract_Members.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Static_Abstract_Members.verified.txt index 3b7f257cab..b29f73196d 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Static_Abstract_Members.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Static_Abstract_Members.verified.txt @@ -2,46 +2,43 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public interface IServiceFactoryMockable : global::IServiceFactory { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public interface IServiceFactoryMockable : global::IServiceFactory + static global::ClientConfig global::IServiceFactory.CreateDefaultConfig() + { + var __engine = IServiceFactoryStaticEngine.Engine; + if (__engine is null) return default!; + var __result = __engine.HandleCallWithReturn(1, "CreateDefaultConfig", global::System.Array.Empty(), default!); + return __result; + } + + static string global::IServiceFactory.ServiceId { - static global::ClientConfig global::IServiceFactory.CreateDefaultConfig() + get { var __engine = IServiceFactoryStaticEngine.Engine; if (__engine is null) return default!; - var __result = __engine.HandleCallWithReturn(1, "CreateDefaultConfig", global::System.Array.Empty(), default!); - return __result; + return __engine.HandleCallWithReturn(2, "get_ServiceId", global::System.Array.Empty(), ""); } - - static string global::IServiceFactory.ServiceId + set { - get - { - var __engine = IServiceFactoryStaticEngine.Engine; - if (__engine is null) return default!; - return __engine.HandleCallWithReturn(2, "get_ServiceId", global::System.Array.Empty(), ""); - } - set - { - var __engine = IServiceFactoryStaticEngine.Engine; - if (__engine is null) return; - __engine.HandleCall(3, "set_ServiceId", new object?[] { value }); - } + var __engine = IServiceFactoryStaticEngine.Engine; + if (__engine is null) return; + __engine.HandleCall(3, "set_ServiceId", new object?[] { value }); } } +} - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal static class IServiceFactoryStaticEngine - { - private static readonly global::System.Threading.AsyncLocal _engine = new(); +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +internal static class IServiceFactoryStaticEngine +{ + private static readonly global::System.Threading.AsyncLocal _engine = new(); - internal static global::TUnit.Mocks.IMockEngineAccess? Engine - { - get => _engine.Value; - set => _engine.Value = value; - } + internal static global::TUnit.Mocks.IMockEngineAccess? Engine + { + get => _engine.Value; + set => _engine.Value = value; } } @@ -52,65 +49,62 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IServiceFactoryMockImpl : global::IServiceFactoryMockable, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IServiceFactoryMockImpl : global::TUnit.Mocks.Generated.IServiceFactoryMockable, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IServiceFactoryMockImpl(global::TUnit.Mocks.MockEngine engine) + internal IServiceFactoryMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + if (IServiceFactoryStaticEngine.Engine is not null) { - _engine = engine; - if (IServiceFactoryStaticEngine.Engine is not null) - { - throw new global::System.InvalidOperationException( - "Multiple mocks of an interface with static abstract members cannot be created in the same test context. " + - "Static member calls are routed via a shared AsyncLocal engine, so only one mock instance per type is supported per test."); - } - IServiceFactoryStaticEngine.Engine = engine; + throw new global::System.InvalidOperationException( + "Multiple mocks of an interface with static abstract members cannot be created in the same test context. " + + "Static member calls are routed via a shared AsyncLocal engine, so only one mock instance per type is supported per test."); } + IServiceFactoryStaticEngine.Engine = engine; + } - public string GetName() - { - return _engine.HandleCallWithReturn(0, "GetName", global::System.Array.Empty(), ""); - } + public string GetName() + { + return _engine.HandleCallWithReturn(0, "GetName", global::System.Array.Empty(), ""); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class IServiceFactoryMockFactory +internal static class IServiceFactoryMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IServiceFactoryMockImpl(engine); - engine.Raisable = impl; - var mock = new global::TUnit.Mocks.Mock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IServiceFactoryMockImpl(engine); + engine.Raisable = impl; + var mock = new global::TUnit.Mocks.Mock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::TUnit.Mocks.Generated.IServiceFactoryMockable' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IServiceFactoryMockImpl(engine); - engine.Raisable = impl; - var mock = new global::TUnit.Mocks.Mock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IServiceFactoryMockable' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IServiceFactoryMockImpl(engine); + engine.Raisable = impl; + var mock = new global::TUnit.Mocks.Mock(impl, engine); + return mock; } } @@ -121,27 +115,24 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IServiceFactory_MockMemberExtensions { - public static class IServiceFactory_MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall GetName(this global::TUnit.Mocks.Mock mock) { - public static global::TUnit.Mocks.MockMethodCall GetName(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetName", matchers); - } + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetName", matchers); + } - public static global::TUnit.Mocks.MockMethodCall CreateDefaultConfig(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "CreateDefaultConfig", matchers); - } + public static global::TUnit.Mocks.MockMethodCall CreateDefaultConfig(this global::TUnit.Mocks.Mock mock) + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "CreateDefaultConfig", matchers); + } - extension(global::TUnit.Mocks.Mock mock) - { - public global::TUnit.Mocks.PropertyMockCall ServiceId - => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, 3, "ServiceId", true, true); - } + extension(global::TUnit.Mocks.Mock mock) + { + public global::TUnit.Mocks.PropertyMockCall ServiceId + => new(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, 3, "ServiceId", true, true); } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Static_Abstract_Transitive_Return_Type.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Static_Abstract_Transitive_Return_Type.verified.txt index 81985574a2..662f151369 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Static_Abstract_Transitive_Return_Type.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Static_Abstract_Transitive_Return_Type.verified.txt @@ -2,18 +2,15 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IMyServiceMock : global::TUnit.Mocks.Mock, global::IMyService { - public sealed class IMyServiceMock : global::TUnit.Mocks.Mock, global::IMyService - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IMyServiceMock(global::IMyService mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IMyServiceMock(global::IMyService mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - string global::IMyService.GetValue(string key) => Object.GetValue(key); + string global::IMyService.GetValue(string key) => Object.GetValue(key); - global::IConfigProvider global::IMyService.GetConfigProvider() => Object.GetConfigProvider(); - } + global::IConfigProvider global::IMyService.GetConfigProvider() => Object.GetConfigProvider(); } @@ -23,63 +20,60 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IMyServiceMockImpl : global::IMyService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IMyServiceMockImpl : global::IMyService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IMyServiceMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IMyServiceMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public string GetValue(string key) - { - return _engine.HandleCallWithReturn(0, "GetValue", key, ""); - } + public string GetValue(string key) + { + return _engine.HandleCallWithReturn(0, "GetValue", key, ""); + } - public global::IConfigProvider GetConfigProvider() - { - return (global::IConfigProvider)_engine.HandleCallWithReturn(1, "GetConfigProvider", global::System.Array.Empty(), null)!; - } + public global::IConfigProvider GetConfigProvider() + { + return (global::IConfigProvider)_engine.HandleCallWithReturn(1, "GetConfigProvider", global::System.Array.Empty(), null)!; + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class IMyServiceMockFactory +internal static class IMyServiceMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IMyServiceMockImpl(engine); - engine.Raisable = impl; - var mock = new IMyServiceMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IMyServiceMockImpl(engine); + engine.Raisable = impl; + var mock = new IMyServiceMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IMyService' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IMyServiceMockImpl(engine); - engine.Raisable = impl; - var mock = new IMyServiceMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IMyService' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IMyServiceMockImpl(engine); + engine.Raisable = impl; + var mock = new IMyServiceMock(impl, engine); + return mock; } } @@ -90,125 +84,122 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IMyService_MockMemberExtensions { - public static class IMyService_MockMemberExtensions + public static IMyService_GetValue_M0_MockCall GetValue(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg key) { - public static IMyService_GetValue_M0_MockCall GetValue(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg key) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { key.Matcher }; - return new IMyService_GetValue_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetValue", matchers); - } - - public static IMyService_GetValue_M0_MockCall GetValue(this global::TUnit.Mocks.Mock mock, global::System.Func key) - { - global::TUnit.Mocks.Arguments.Arg __fa_key = key; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_key.Matcher }; - return new IMyService_GetValue_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetValue", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { key.Matcher }; + return new IMyService_GetValue_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetValue", matchers); + } - /// Configure the mock setup for GetConfigProvider. - /// - /// A typed as object? because the return type - /// contains static abstract members and cannot be used as a generic type argument (CS8920). - /// Pass a value that implements the return interface to .Returns() — incorrect types will cause an at call time. - /// - public static global::TUnit.Mocks.MockMethodCall GetConfigProvider(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetConfigProvider", matchers); - } + public static IMyService_GetValue_M0_MockCall GetValue(this global::TUnit.Mocks.Mock mock, global::System.Func key) + { + global::TUnit.Mocks.Arguments.Arg __fa_key = key; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_key.Matcher }; + return new IMyService_GetValue_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetValue", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IMyService_GetValue_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure the mock setup for GetConfigProvider. + /// + /// A typed as object? because the return type + /// contains static abstract members and cannot be used as a generic type argument (CS8920). + /// Pass a value that implements the return interface to .Returns() — incorrect types will cause an at call time. + /// + public static global::TUnit.Mocks.MockMethodCall GetConfigProvider(this global::TUnit.Mocks.Mock mock) { - 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; + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetConfigProvider", matchers); + } +} - 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; - } +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IMyService_GetValue_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - 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(); - } + 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; + } - [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; - } + 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(); + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public IMyService_GetValue_M0_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((string)args[0]!)); - return this; - } + [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; + } - /// Execute a typed callback using the actual method parameters. - public IMyService_GetValue_M0_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public IMyService_GetValue_M0_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((string)args[0]!)); + return this; + } - /// Configure a typed computed exception using the actual method parameters. - public IMyService_GetValue_M0_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); - return this; - } + /// Execute a typed callback using the actual method parameters. + public IMyService_GetValue_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + 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); + /// Configure a typed computed exception using the actual method parameters. + public IMyService_GetValue_M0_MockCall Throws(global::System.Func exceptionFactory) + { + 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); } @@ -224,9 +215,9 @@ namespace TUnit.Mocks { extension(global::IMyService _) { - public static global::TUnit.Mocks.Generated.IMyServiceMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IMyServiceMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IMyServiceMock)global::TUnit.Mocks.Generated.IMyServiceMockFactory.CreateAutoMock(behavior); + return (global::IMyServiceMock)global::IMyServiceMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Unconstrained_Nullable_Generic.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Unconstrained_Nullable_Generic.verified.txt index 5888b713f6..9403244107 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Unconstrained_Nullable_Generic.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Interface_With_Unconstrained_Nullable_Generic.verified.txt @@ -2,20 +2,17 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IFooMock : global::TUnit.Mocks.Mock, global::IFoo { - public sealed class IFooMock : global::TUnit.Mocks.Mock, global::IFoo - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IFooMock(global::IFoo mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IFooMock(global::IFoo mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - global::System.Threading.Tasks.Task global::IFoo.DoSomethingAsync() where T : default => Object.DoSomethingAsync(); + global::System.Threading.Tasks.Task global::IFoo.DoSomethingAsync() where T : default => Object.DoSomethingAsync(); - T? global::IFoo.GetValue() where T : default => Object.GetValue(); + T? global::IFoo.GetValue() where T : default => Object.GetValue(); - (T?, string) global::IFoo.GetPair() where T : default => Object.GetPair(); - } + (T?, string) global::IFoo.GetPair() where T : default => Object.GetPair(); } @@ -25,81 +22,78 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IFooMockImpl : global::IFoo, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IFooMockImpl : global::IFoo, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IFooMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IFooMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public global::System.Threading.Tasks.Task DoSomethingAsync() + public global::System.Threading.Tasks.Task DoSomethingAsync() + { + try { - try + var __result = _engine.HandleCallWithReturn(0, "DoSomethingAsync", global::System.Array.Empty(), default); + if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) { - var __result = _engine.HandleCallWithReturn(0, "DoSomethingAsync", global::System.Array.Empty(), default); - if (global::TUnit.Mocks.Setup.RawReturnContext.TryConsume(out var __rawAsync)) - { - if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; - throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); - } - return global::System.Threading.Tasks.Task.FromResult(__result); - } - catch (global::System.Exception __ex) - { - return global::System.Threading.Tasks.Task.FromException(__ex); + if (__rawAsync is global::System.Threading.Tasks.Task __typedAsync) return __typedAsync; + throw new global::System.InvalidOperationException($"ReturnsAsync: expected global::System.Threading.Tasks.Task but got {__rawAsync?.GetType().Name ?? "null"}"); } + return global::System.Threading.Tasks.Task.FromResult(__result); } - - public T? GetValue() + catch (global::System.Exception __ex) { - return _engine.HandleCallWithReturn(1, "GetValue", global::System.Array.Empty(), default); + return global::System.Threading.Tasks.Task.FromException(__ex); } + } - public (T?, string) GetPair() - { - return _engine.HandleCallWithReturn<(T?, string)>(2, "GetPair", global::System.Array.Empty(), default); - } + public T? GetValue() + { + return _engine.HandleCallWithReturn(1, "GetValue", global::System.Array.Empty(), default); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + public (T?, string) GetPair() + { + return _engine.HandleCallWithReturn<(T?, string)>(2, "GetPair", global::System.Array.Empty(), default); } - internal static class IFooMockFactory + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); + } +} - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IFooMockImpl(engine); - engine.Raisable = impl; - var mock = new IFooMock(impl, engine); - return mock; - } +internal static class IFooMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() + { + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IFoo' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IFooMockImpl(engine); - engine.Raisable = impl; - var mock = new IFooMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IFooMockImpl(engine); + engine.Raisable = impl; + var mock = new IFooMock(impl, engine); + return mock; + } + + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IFoo' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IFooMockImpl(engine); + engine.Raisable = impl; + var mock = new IFooMock(impl, engine); + return mock; } } @@ -110,27 +104,24 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IFoo_MockMemberExtensions { - public static class IFoo_MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall DoSomethingAsync(this global::TUnit.Mocks.Mock mock) { - public static global::TUnit.Mocks.MockMethodCall DoSomethingAsync(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "DoSomethingAsync", matchers); - } + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "DoSomethingAsync", matchers); + } - public static global::TUnit.Mocks.MockMethodCall GetValue(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetValue", matchers); - } + public static global::TUnit.Mocks.MockMethodCall GetValue(this global::TUnit.Mocks.Mock mock) + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetValue", matchers); + } - public static global::TUnit.Mocks.MockMethodCall<(T?, string)> GetPair(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall<(T?, string)>(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "GetPair", matchers); - } + public static global::TUnit.Mocks.MockMethodCall<(T?, string)> GetPair(this global::TUnit.Mocks.Mock mock) + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall<(T?, string)>(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "GetPair", matchers); } } @@ -147,9 +138,9 @@ namespace TUnit.Mocks { extension(global::IFoo _) { - public static global::TUnit.Mocks.Generated.IFooMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IFooMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IFooMock)global::TUnit.Mocks.Generated.IFooMockFactory.CreateAutoMock(behavior); + return (global::IFooMock)global::IFooMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Multi_Method_Interface.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Multi_Method_Interface.verified.txt index beb8e9a227..b9eb295a5d 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Multi_Method_Interface.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Multi_Method_Interface.verified.txt @@ -2,22 +2,19 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class ICalculatorMock : global::TUnit.Mocks.Mock, global::ICalculator { - public sealed class ICalculatorMock : global::TUnit.Mocks.Mock, global::ICalculator - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal ICalculatorMock(global::ICalculator mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal ICalculatorMock(global::ICalculator mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - int global::ICalculator.Add(int a, int b) => Object.Add(a, b); + int global::ICalculator.Add(int a, int b) => Object.Add(a, b); - int global::ICalculator.Subtract(int a, int b) => Object.Subtract(a, b); + int global::ICalculator.Subtract(int a, int b) => Object.Subtract(a, b); - void global::ICalculator.Reset() - { - Object.Reset(); - } + void global::ICalculator.Reset() + { + Object.Reset(); } } @@ -28,68 +25,65 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class ICalculatorMockImpl : global::ICalculator, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class ICalculatorMockImpl : global::ICalculator, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal ICalculatorMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal ICalculatorMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public int Add(int a, int b) - { - return _engine.HandleCallWithReturn(0, "Add", a, b, default); - } + public int Add(int a, int b) + { + return _engine.HandleCallWithReturn(0, "Add", a, b, default); + } - public int Subtract(int a, int b) - { - return _engine.HandleCallWithReturn(1, "Subtract", a, b, default); - } + public int Subtract(int a, int b) + { + return _engine.HandleCallWithReturn(1, "Subtract", a, b, default); + } - public void Reset() - { - _engine.HandleCall(2, "Reset", global::System.Array.Empty()); - } + public void Reset() + { + _engine.HandleCall(2, "Reset", global::System.Array.Empty()); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class ICalculatorMockFactory +internal static class ICalculatorMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new ICalculatorMockImpl(engine); - engine.Raisable = impl; - var mock = new ICalculatorMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new ICalculatorMockImpl(engine); + engine.Raisable = impl; + var mock = new ICalculatorMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::ICalculator' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new ICalculatorMockImpl(engine); - engine.Raisable = impl; - var mock = new ICalculatorMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::ICalculator' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new ICalculatorMockImpl(engine); + engine.Raisable = impl; + var mock = new ICalculatorMock(impl, engine); + return mock; } } @@ -100,266 +94,263 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class ICalculator_MockMemberExtensions { - public static class ICalculator_MockMemberExtensions + public static ICalculator_Add_M0_MockCall Add(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg a, global::TUnit.Mocks.Arguments.Arg b) { - public static ICalculator_Add_M0_MockCall Add(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg a, global::TUnit.Mocks.Arguments.Arg b) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { a.Matcher, b.Matcher }; - return new ICalculator_Add_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Add", matchers); - } - - public static ICalculator_Add_M0_MockCall Add(this global::TUnit.Mocks.Mock mock, global::System.Func a, global::TUnit.Mocks.Arguments.Arg b) - { - global::TUnit.Mocks.Arguments.Arg __fa_a = a; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_a.Matcher, b.Matcher }; - return new ICalculator_Add_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Add", matchers); - } - - public static ICalculator_Add_M0_MockCall Add(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg a, global::System.Func b) - { - global::TUnit.Mocks.Arguments.Arg __fa_b = b; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { a.Matcher, __fa_b.Matcher }; - return new ICalculator_Add_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Add", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { a.Matcher, b.Matcher }; + return new ICalculator_Add_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Add", matchers); + } - public static ICalculator_Add_M0_MockCall Add(this global::TUnit.Mocks.Mock mock, global::System.Func a, global::System.Func b) - { - global::TUnit.Mocks.Arguments.Arg __fa_a = a; - global::TUnit.Mocks.Arguments.Arg __fa_b = b; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_a.Matcher, __fa_b.Matcher }; - return new ICalculator_Add_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Add", matchers); - } + public static ICalculator_Add_M0_MockCall Add(this global::TUnit.Mocks.Mock mock, global::System.Func a, global::TUnit.Mocks.Arguments.Arg b) + { + global::TUnit.Mocks.Arguments.Arg __fa_a = a; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_a.Matcher, b.Matcher }; + return new ICalculator_Add_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Add", matchers); + } - /// Configure the mock setup for Add with every argument matched as Any<T>(). - public static ICalculator_Add_M0_MockCall Add(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.AnyArgs _) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance }; - return new ICalculator_Add_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Add", matchers); - } + public static ICalculator_Add_M0_MockCall Add(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg a, global::System.Func b) + { + global::TUnit.Mocks.Arguments.Arg __fa_b = b; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { a.Matcher, __fa_b.Matcher }; + return new ICalculator_Add_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Add", matchers); + } - public static ICalculator_Subtract_M1_MockCall Subtract(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg a, global::TUnit.Mocks.Arguments.Arg b) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { a.Matcher, b.Matcher }; - return new ICalculator_Subtract_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Subtract", matchers); - } + public static ICalculator_Add_M0_MockCall Add(this global::TUnit.Mocks.Mock mock, global::System.Func a, global::System.Func b) + { + global::TUnit.Mocks.Arguments.Arg __fa_a = a; + global::TUnit.Mocks.Arguments.Arg __fa_b = b; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_a.Matcher, __fa_b.Matcher }; + return new ICalculator_Add_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Add", matchers); + } - public static ICalculator_Subtract_M1_MockCall Subtract(this global::TUnit.Mocks.Mock mock, global::System.Func a, global::TUnit.Mocks.Arguments.Arg b) - { - global::TUnit.Mocks.Arguments.Arg __fa_a = a; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_a.Matcher, b.Matcher }; - return new ICalculator_Subtract_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Subtract", matchers); - } + /// Configure the mock setup for Add with every argument matched as Any<T>(). + public static ICalculator_Add_M0_MockCall Add(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.AnyArgs _) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance }; + return new ICalculator_Add_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Add", matchers); + } - public static ICalculator_Subtract_M1_MockCall Subtract(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg a, global::System.Func b) - { - global::TUnit.Mocks.Arguments.Arg __fa_b = b; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { a.Matcher, __fa_b.Matcher }; - return new ICalculator_Subtract_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Subtract", matchers); - } + public static ICalculator_Subtract_M1_MockCall Subtract(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg a, global::TUnit.Mocks.Arguments.Arg b) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { a.Matcher, b.Matcher }; + return new ICalculator_Subtract_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Subtract", matchers); + } - public static ICalculator_Subtract_M1_MockCall Subtract(this global::TUnit.Mocks.Mock mock, global::System.Func a, global::System.Func b) - { - global::TUnit.Mocks.Arguments.Arg __fa_a = a; - global::TUnit.Mocks.Arguments.Arg __fa_b = b; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_a.Matcher, __fa_b.Matcher }; - return new ICalculator_Subtract_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Subtract", matchers); - } + public static ICalculator_Subtract_M1_MockCall Subtract(this global::TUnit.Mocks.Mock mock, global::System.Func a, global::TUnit.Mocks.Arguments.Arg b) + { + global::TUnit.Mocks.Arguments.Arg __fa_a = a; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_a.Matcher, b.Matcher }; + return new ICalculator_Subtract_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Subtract", matchers); + } - /// Configure the mock setup for Subtract with every argument matched as Any<T>(). - public static ICalculator_Subtract_M1_MockCall Subtract(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.AnyArgs _) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance }; - return new ICalculator_Subtract_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Subtract", matchers); - } + public static ICalculator_Subtract_M1_MockCall Subtract(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg a, global::System.Func b) + { + global::TUnit.Mocks.Arguments.Arg __fa_b = b; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { a.Matcher, __fa_b.Matcher }; + return new ICalculator_Subtract_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Subtract", matchers); + } - public static global::TUnit.Mocks.VoidMockMethodCall Reset(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Reset", matchers); - } + public static ICalculator_Subtract_M1_MockCall Subtract(this global::TUnit.Mocks.Mock mock, global::System.Func a, global::System.Func b) + { + global::TUnit.Mocks.Arguments.Arg __fa_a = a; + global::TUnit.Mocks.Arguments.Arg __fa_b = b; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_a.Matcher, __fa_b.Matcher }; + return new ICalculator_Subtract_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Subtract", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ICalculator_Add_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure the mock setup for Subtract with every argument matched as Any<T>(). + public static ICalculator_Subtract_M1_MockCall Subtract(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.AnyArgs _) { - 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; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { global::TUnit.Mocks.Matchers.AnyMatcher.Instance, global::TUnit.Mocks.Matchers.AnyMatcher.Instance }; + return new ICalculator_Subtract_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Subtract", matchers); + } - 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; - } + public static global::TUnit.Mocks.VoidMockMethodCall Reset(this global::TUnit.Mocks.Mock mock) + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Reset", 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class ICalculator_Add_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public ICalculator_Add_M0_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((int)args[0]!, (int)args[1]!)); - return this; - } + 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(); + } - /// Execute a typed callback using the actual method parameters. - public ICalculator_Add_M0_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public ICalculator_Add_M0_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((int)args[0]!, (int)args[1]!)); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public ICalculator_Add_M0_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((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); + /// Execute a typed callback using the actual method parameters. + public ICalculator_Add_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class ICalculator_Subtract_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public ICalculator_Add_M0_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((int)args[0]!, (int)args[1]!)); + return this; + } - 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; - } + // 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class ICalculator_Subtract_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public ICalculator_Subtract_M1_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((int)args[0]!, (int)args[1]!)); - return this; - } + 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(); + } - /// Execute a typed callback using the actual method parameters. - public ICalculator_Subtract_M1_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public ICalculator_Subtract_M1_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((int)args[0]!, (int)args[1]!)); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public ICalculator_Subtract_M1_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((int)args[0]!, (int)args[1]!)); + return this; + } + + /// Execute a typed callback using the actual method parameters. + public ICalculator_Subtract_M1_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + 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); + /// Configure a typed computed exception using the actual method parameters. + public ICalculator_Subtract_M1_MockCall Throws(global::System.Func exceptionFactory) + { + 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); } @@ -375,9 +366,9 @@ namespace TUnit.Mocks { extension(global::ICalculator _) { - public static global::TUnit.Mocks.Generated.ICalculatorMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::ICalculatorMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.ICalculatorMock)global::TUnit.Mocks.Generated.ICalculatorMockFactory.CreateAutoMock(behavior); + return (global::ICalculatorMock)global::ICalculatorMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Internal_Virtual_Members_From_External_Assembly.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Internal_Virtual_Members_From_External_Assembly.verified.txt index fe823153f2..0433237990 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Internal_Virtual_Members_From_External_Assembly.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Internal_Virtual_Members_From_External_Assembly.verified.txt @@ -2,7 +2,7 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated.ExternalLib +namespace ExternalLib { file sealed class ExternalClientMockImpl : global::ExternalLib.ExternalClient, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { @@ -87,7 +87,7 @@ namespace TUnit.Mocks.Generated.ExternalLib #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +namespace ExternalLib { public static class ExternalLib_ExternalClient_MockMemberExtensions { diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Members_With_Internal_Signature_Types.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Members_With_Internal_Signature_Types.verified.txt index b0b6c58f3f..1057c815e9 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Members_With_Internal_Signature_Types.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Filters_Members_With_Internal_Signature_Types.verified.txt @@ -2,7 +2,7 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated.ExternalLib +namespace ExternalLib { file sealed class ServiceClientMockImpl : global::ExternalLib.ServiceClient, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { @@ -59,7 +59,7 @@ namespace TUnit.Mocks.Generated.ExternalLib #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +namespace ExternalLib { public static class ExternalLib_ServiceClient_MockMemberExtensions { diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Omits_Inaccessible_Property_Setters.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Omits_Inaccessible_Property_Setters.verified.txt index f6f974e3a2..9b77367aa9 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Omits_Inaccessible_Property_Setters.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_Omits_Inaccessible_Property_Setters.verified.txt @@ -2,7 +2,7 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated.ExternalLib +namespace ExternalLib { file sealed class ExternalResponseMockImpl : global::ExternalLib.ExternalResponse, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { @@ -93,7 +93,7 @@ namespace TUnit.Mocks.Generated.ExternalLib #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +namespace ExternalLib { public static class ExternalLib_ExternalResponse_MockMemberExtensions { diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt index 94eb83ae5f..5cbe270d27 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Partial_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt @@ -2,76 +2,73 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class BaseServiceMockImpl : global::BaseService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class BaseServiceMockImpl : global::BaseService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] - internal BaseServiceMockImpl(global::TUnit.Mocks.MockEngine engine) : base() - { - _engine = engine; - } + [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] + internal BaseServiceMockImpl(global::TUnit.Mocks.MockEngine engine) : base() + { + _engine = engine; + } - public override T GetById(int id) + public override T GetById(int id) + { + if (_engine.TryHandleCallWithReturn(0, "GetById", id, default!, out var __result)) { - if (_engine.TryHandleCallWithReturn(0, "GetById", id, default!, out var __result)) - { - return __result; - } - return base.GetById(id); + return __result; } + return base.GetById(id); + } - public override void Save(T entity) + public override void Save(T entity) + { + if (_engine.TryHandleCall(1, "Save", entity)) { - if (_engine.TryHandleCall(1, "Save", entity)) - { - return; - } - base.Save(entity); + return; } + base.Save(entity); + } - public override TResult Transform(TInput input) + public override TResult Transform(TInput input) + { + if (_engine.TryHandleCallWithReturn(2, "Transform", input, default, out var __result)) { - if (_engine.TryHandleCallWithReturn(2, "Transform", input, default, out var __result)) - { - return __result; - } - return base.Transform(input); + return __result; } + return base.Transform(input); + } - public override global::System.Collections.Generic.IEnumerable GetAll() - { - return _engine.HandleCallWithReturn>(3, "GetAll", global::System.Array.Empty(), global::System.Array.Empty()); - } + public override global::System.Collections.Generic.IEnumerable GetAll() + { + return _engine.HandleCallWithReturn>(3, "GetAll", global::System.Array.Empty(), global::System.Array.Empty()); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - file static class BaseServicePartialMockFactory +file static class BaseServicePartialMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new BaseServiceMockImpl(engine); - engine.Raisable = impl; - var mock = new global::TUnit.Mocks.Mock(impl, engine); - return mock; - } + private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new BaseServiceMockImpl(engine); + engine.Raisable = impl; + var mock = new global::TUnit.Mocks.Mock(impl, engine); + return mock; } } @@ -82,54 +79,51 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class BaseService_MockMemberExtensions { - public static class BaseService_MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall GetById(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg id) where T : class { - public static global::TUnit.Mocks.MockMethodCall GetById(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg id) where T : class - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { id.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetById", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { id.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetById", matchers); + } - public static global::TUnit.Mocks.MockMethodCall GetById(this global::TUnit.Mocks.Mock mock, global::System.Func id) where T : class - { - global::TUnit.Mocks.Arguments.Arg __fa_id = id; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_id.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetById", matchers); - } + public static global::TUnit.Mocks.MockMethodCall GetById(this global::TUnit.Mocks.Mock mock, global::System.Func id) where T : class + { + global::TUnit.Mocks.Arguments.Arg __fa_id = id; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_id.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "GetById", matchers); + } - public static global::TUnit.Mocks.VoidMockMethodCall Save(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg entity) where T : class, new() - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { entity.Matcher }; - return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Save", matchers); - } + public static global::TUnit.Mocks.VoidMockMethodCall Save(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg entity) where T : class, new() + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { entity.Matcher }; + return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Save", matchers); + } - public static global::TUnit.Mocks.VoidMockMethodCall Save(this global::TUnit.Mocks.Mock mock, global::System.Func entity) where T : class, new() - { - global::TUnit.Mocks.Arguments.Arg __fa_entity = entity; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_entity.Matcher }; - return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Save", matchers); - } + public static global::TUnit.Mocks.VoidMockMethodCall Save(this global::TUnit.Mocks.Mock mock, global::System.Func entity) where T : class, new() + { + global::TUnit.Mocks.Arguments.Arg __fa_entity = entity; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_entity.Matcher }; + return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Save", matchers); + } - public static global::TUnit.Mocks.MockMethodCall Transform(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg input) where TInput : notnull where TResult : struct - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { input.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Transform", matchers); - } + public static global::TUnit.Mocks.MockMethodCall Transform(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg input) where TInput : notnull where TResult : struct + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { input.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Transform", matchers); + } - public static global::TUnit.Mocks.MockMethodCall Transform(this global::TUnit.Mocks.Mock mock, global::System.Func input) where TInput : notnull where TResult : struct - { - global::TUnit.Mocks.Arguments.Arg __fa_input = input; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_input.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Transform", matchers); - } + public static global::TUnit.Mocks.MockMethodCall Transform(this global::TUnit.Mocks.Mock mock, global::System.Func input) where TInput : notnull where TResult : struct + { + global::TUnit.Mocks.Arguments.Arg __fa_input = input; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_input.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "Transform", matchers); + } - public static global::TUnit.Mocks.MockMethodCall> GetAll(this global::TUnit.Mocks.Mock mock) where T : class - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall>(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "GetAll", matchers); - } + public static global::TUnit.Mocks.MockMethodCall> GetAll(this global::TUnit.Mocks.Mock mock) where T : class + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall>(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "GetAll", matchers); } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/SelfEquatable_Generates_EqualsOf_GetHashCodeOf_ToStringOf.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/SelfEquatable_Generates_EqualsOf_GetHashCodeOf_ToStringOf.verified.txt index b3efabd6ac..eebcb821f1 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/SelfEquatable_Generates_EqualsOf_GetHashCodeOf_ToStringOf.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/SelfEquatable_Generates_EqualsOf_GetHashCodeOf_ToStringOf.verified.txt @@ -2,80 +2,77 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class SelfEquatableSnapshotMockImpl : global::SelfEquatableSnapshot, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class SelfEquatableSnapshotMockImpl : global::SelfEquatableSnapshot, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] - internal SelfEquatableSnapshotMockImpl(global::TUnit.Mocks.MockEngine engine) : base() - { - _engine = engine; - } + [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] + internal SelfEquatableSnapshotMockImpl(global::TUnit.Mocks.MockEngine engine) : base() + { + _engine = engine; + } - public override bool Equals(global::SelfEquatableSnapshot? other) + public override bool Equals(global::SelfEquatableSnapshot? other) + { + if (_engine.TryHandleCallWithReturn(0, "Equals", other, default, out var __result)) { - if (_engine.TryHandleCallWithReturn(0, "Equals", other, default, out var __result)) - { - return __result; - } - return base.Equals(other); + return __result; } + return base.Equals(other); + } - public override bool Equals(object? obj) + public override bool Equals(object? obj) + { + if (_engine.TryHandleCallWithReturn(1, "Equals", obj, default, out var __result)) { - if (_engine.TryHandleCallWithReturn(1, "Equals", obj, default, out var __result)) - { - return __result; - } - return base.Equals(obj); + return __result; } + return base.Equals(obj); + } - public override int GetHashCode() + public override int GetHashCode() + { + if (_engine.TryHandleCallWithReturn(2, "GetHashCode", global::System.Array.Empty(), default, out var __result)) { - if (_engine.TryHandleCallWithReturn(2, "GetHashCode", global::System.Array.Empty(), default, out var __result)) - { - return __result; - } - return base.GetHashCode(); + return __result; } + return base.GetHashCode(); + } - public override string ToString() + public override string ToString() + { + if (_engine.TryHandleCallWithReturn(3, "ToString", global::System.Array.Empty(), "", out var __result)) { - if (_engine.TryHandleCallWithReturn(3, "ToString", global::System.Array.Empty(), "", out var __result)) - { - return __result; - } - return base.ToString(); + return __result; } + return base.ToString(); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - file static class SelfEquatableSnapshotPartialMockFactory +file static class SelfEquatableSnapshotPartialMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new SelfEquatableSnapshotMockImpl(engine); - engine.Raisable = impl; - var mock = new global::TUnit.Mocks.Mock(impl, engine); - return mock; - } + private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new SelfEquatableSnapshotMockImpl(engine); + engine.Raisable = impl; + var mock = new global::TUnit.Mocks.Mock(impl, engine); + return mock; } } @@ -86,228 +83,225 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class SelfEquatableSnapshot_MockMemberExtensions { - public static class SelfEquatableSnapshot_MockMemberExtensions + public static SelfEquatableSnapshot_Equals_M0_MockCall EqualsOf(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg other) { - public static SelfEquatableSnapshot_Equals_M0_MockCall EqualsOf(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg other) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { other.Matcher }; - return new SelfEquatableSnapshot_Equals_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Equals", matchers); - } - - public static SelfEquatableSnapshot_Equals_M0_MockCall EqualsOf(this global::TUnit.Mocks.Mock mock, global::System.Func other) - { - global::TUnit.Mocks.Arguments.Arg __fa_other = other; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_other.Matcher }; - return new SelfEquatableSnapshot_Equals_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Equals", matchers); - } - - public static SelfEquatableSnapshot_Equals_M1_MockCall EqualsOf(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg obj) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { obj.Matcher }; - return new SelfEquatableSnapshot_Equals_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Equals", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { other.Matcher }; + return new SelfEquatableSnapshot_Equals_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Equals", matchers); + } - public static SelfEquatableSnapshot_Equals_M1_MockCall EqualsOf(this global::TUnit.Mocks.Mock mock, global::System.Func obj) - { - global::TUnit.Mocks.Arguments.Arg __fa_obj = obj; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_obj.Matcher }; - return new SelfEquatableSnapshot_Equals_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Equals", matchers); - } + public static SelfEquatableSnapshot_Equals_M0_MockCall EqualsOf(this global::TUnit.Mocks.Mock mock, global::System.Func other) + { + global::TUnit.Mocks.Arguments.Arg __fa_other = other; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_other.Matcher }; + return new SelfEquatableSnapshot_Equals_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Equals", matchers); + } - public static global::TUnit.Mocks.MockMethodCall GetHashCodeOf(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "GetHashCode", matchers); - } + public static SelfEquatableSnapshot_Equals_M1_MockCall EqualsOf(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg obj) + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { obj.Matcher }; + return new SelfEquatableSnapshot_Equals_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Equals", matchers); + } - public static global::TUnit.Mocks.MockMethodCall ToStringOf(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "ToString", matchers); - } + public static SelfEquatableSnapshot_Equals_M1_MockCall EqualsOf(this global::TUnit.Mocks.Mock mock, global::System.Func obj) + { + global::TUnit.Mocks.Arguments.Arg __fa_obj = obj; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_obj.Matcher }; + return new SelfEquatableSnapshot_Equals_M1_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Equals", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class SelfEquatableSnapshot_Equals_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public static global::TUnit.Mocks.MockMethodCall GetHashCodeOf(this global::TUnit.Mocks.Mock mock) { - 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; + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 2, "GetHashCode", matchers); + } - 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; - } + public static global::TUnit.Mocks.MockMethodCall ToStringOf(this global::TUnit.Mocks.Mock mock) + { + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 3, "ToString", 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class SelfEquatableSnapshot_Equals_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public SelfEquatableSnapshot_Equals_M0_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((global::SelfEquatableSnapshot?)args[0])); - return this; - } + 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(); + } - /// Execute a typed callback using the actual method parameters. - public SelfEquatableSnapshot_Equals_M0_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public SelfEquatableSnapshot_Equals_M0_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((global::SelfEquatableSnapshot?)args[0])); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public SelfEquatableSnapshot_Equals_M0_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((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); + /// Execute a typed callback using the actual method parameters. + public SelfEquatableSnapshot_Equals_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + return this; } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class SelfEquatableSnapshot_Equals_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification + /// Configure a typed computed exception using the actual method parameters. + public SelfEquatableSnapshot_Equals_M0_MockCall Throws(global::System.Func exceptionFactory) { - 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; + EnsureSetup().Throws(args => exceptionFactory((global::SelfEquatableSnapshot?)args[0])); + return this; + } - 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; - } + // 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); +} - 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.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class SelfEquatableSnapshot_Equals_M1_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - [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; - } + 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; + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public SelfEquatableSnapshot_Equals_M1_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((object?)args[0])); - return this; - } + 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(); + } - /// Execute a typed callback using the actual method parameters. - public SelfEquatableSnapshot_Equals_M1_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public SelfEquatableSnapshot_Equals_M1_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((object?)args[0])); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public SelfEquatableSnapshot_Equals_M1_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((object?)args[0])); + return this; + } + + /// Execute a typed callback using the actual method parameters. + public SelfEquatableSnapshot_Equals_M1_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + 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); + /// Configure a typed computed exception using the actual method parameters. + public SelfEquatableSnapshot_Equals_M1_MockCall Throws(global::System.Func exceptionFactory) + { + 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/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Simple_Interface_With_One_Method.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Simple_Interface_With_One_Method.verified.txt index 97c61865c4..d1bc823adc 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Simple_Interface_With_One_Method.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Simple_Interface_With_One_Method.verified.txt @@ -2,16 +2,13 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class IGreeterMock : global::TUnit.Mocks.Mock, global::IGreeter { - public sealed class IGreeterMock : global::TUnit.Mocks.Mock, global::IGreeter - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal IGreeterMock(global::IGreeter mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal IGreeterMock(global::IGreeter mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - string global::IGreeter.Greet(string name) => Object.Greet(name); - } + string global::IGreeter.Greet(string name) => Object.Greet(name); } @@ -21,58 +18,55 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class IGreeterMockImpl : global::IGreeter, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class IGreeterMockImpl : global::IGreeter, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal IGreeterMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal IGreeterMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public string Greet(string name) - { - return _engine.HandleCallWithReturn(0, "Greet", name, ""); - } + public string Greet(string name) + { + return _engine.HandleCallWithReturn(0, "Greet", name, ""); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class IGreeterMockFactory +internal static class IGreeterMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IGreeterMockImpl(engine); - engine.Raisable = impl; - var mock = new IGreeterMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IGreeterMockImpl(engine); + engine.Raisable = impl; + var mock = new IGreeterMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IGreeter' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new IGreeterMockImpl(engine); - engine.Raisable = impl; - var mock = new IGreeterMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::IGreeter' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new IGreeterMockImpl(engine); + engine.Raisable = impl; + var mock = new IGreeterMock(impl, engine); + return mock; } } @@ -83,113 +77,110 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class IGreeter_MockMemberExtensions { - public static class IGreeter_MockMemberExtensions + public static IGreeter_Greet_M0_MockCall Greet(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg name) { - public static IGreeter_Greet_M0_MockCall Greet(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg name) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { name.Matcher }; - return new IGreeter_Greet_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Greet", matchers); - } - - public static IGreeter_Greet_M0_MockCall Greet(this global::TUnit.Mocks.Mock mock, global::System.Func name) - { - global::TUnit.Mocks.Arguments.Arg __fa_name = name; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_name.Matcher }; - return new IGreeter_Greet_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Greet", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { name.Matcher }; + return new IGreeter_Greet_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Greet", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class IGreeter_Greet_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public static IGreeter_Greet_M0_MockCall Greet(this global::TUnit.Mocks.Mock mock, global::System.Func name) { - 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; + global::TUnit.Mocks.Arguments.Arg __fa_name = name; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_name.Matcher }; + return new IGreeter_Greet_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Greet", matchers); + } +} - 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; - } +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class IGreeter_Greet_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - 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(); - } + 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; + } - [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; - } + 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(); + } - /// - 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; } - - /// Configure a typed computed return value using the actual method parameters. - public IGreeter_Greet_M0_MockCall Returns(global::System.Func factory) - { - EnsureSetup().Returns(args => factory((string)args[0]!)); - return this; - } + [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; + } - /// Execute a typed callback using the actual method parameters. - public IGreeter_Greet_M0_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + /// + 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; } + + /// Configure a typed computed return value using the actual method parameters. + public IGreeter_Greet_M0_MockCall Returns(global::System.Func factory) + { + EnsureSetup().Returns(args => factory((string)args[0]!)); + return this; + } - /// Configure a typed computed exception using the actual method parameters. - public IGreeter_Greet_M0_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); - return this; - } + /// Execute a typed callback using the actual method parameters. + public IGreeter_Greet_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + 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); + /// Configure a typed computed exception using the actual method parameters. + public IGreeter_Greet_M0_MockCall Throws(global::System.Func exceptionFactory) + { + 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); } @@ -205,9 +196,9 @@ namespace TUnit.Mocks { extension(global::IGreeter _) { - public static global::TUnit.Mocks.Generated.IGreeterMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::IGreeterMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.IGreeterMock)global::TUnit.Mocks.Generated.IGreeterMockFactory.CreateAutoMock(behavior); + return (global::IGreeterMock)global::IGreeterMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Static_Extension_Discovery_Without_Mock_Of.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Static_Extension_Discovery_Without_Mock_Of.verified.txt index d9240bb070..8a23d7fe0a 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Static_Extension_Discovery_Without_Mock_Of.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Static_Extension_Discovery_Without_Mock_Of.verified.txt @@ -2,21 +2,18 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public sealed class INotifierMock : global::TUnit.Mocks.Mock, global::INotifier { - public sealed class INotifierMock : global::TUnit.Mocks.Mock, global::INotifier - { - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - internal INotifierMock(global::INotifier mockObject, global::TUnit.Mocks.MockEngine engine) - : base(mockObject, engine) { } - - void global::INotifier.Notify(string message) - { - Object.Notify(message); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + internal INotifierMock(global::INotifier mockObject, global::TUnit.Mocks.MockEngine engine) + : base(mockObject, engine) { } - string global::INotifier.GetStatus() => Object.GetStatus(); + void global::INotifier.Notify(string message) + { + Object.Notify(message); } + + string global::INotifier.GetStatus() => Object.GetStatus(); } @@ -26,63 +23,60 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class INotifierMockImpl : global::INotifier, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class INotifierMockImpl : global::INotifier, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::TUnit.Mocks.MockEngine _engine; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - internal INotifierMockImpl(global::TUnit.Mocks.MockEngine engine) - { - _engine = engine; - } + internal INotifierMockImpl(global::TUnit.Mocks.MockEngine engine) + { + _engine = engine; + } - public void Notify(string message) - { - _engine.HandleCall(0, "Notify", message); - } + public void Notify(string message) + { + _engine.HandleCall(0, "Notify", message); + } - public string GetStatus() - { - return _engine.HandleCallWithReturn(1, "GetStatus", global::System.Array.Empty(), ""); - } + public string GetStatus() + { + return _engine.HandleCallWithReturn(1, "GetStatus", global::System.Array.Empty(), ""); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - internal static class INotifierMockFactory +internal static class INotifierMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterFactory(Create); + } - internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new INotifierMockImpl(engine); - engine.Raisable = impl; - var mock = new INotifierMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock CreateAutoMock(global::TUnit.Mocks.MockBehavior behavior) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new INotifierMockImpl(engine); + engine.Raisable = impl; + var mock = new INotifierMock(impl, engine); + return mock; + } - internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) - { - if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::INotifier' does not support constructor arguments, but {constructorArgs.Length} were provided."); - var engine = new global::TUnit.Mocks.MockEngine(behavior); - var impl = new INotifierMockImpl(engine); - engine.Raisable = impl; - var mock = new INotifierMock(impl, engine); - return mock; - } + internal static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, object[] constructorArgs) + { + if (constructorArgs.Length > 0) throw new global::System.ArgumentException($"Interface mock 'global::INotifier' does not support constructor arguments, but {constructorArgs.Length} were provided."); + var engine = new global::TUnit.Mocks.MockEngine(behavior); + var impl = new INotifierMockImpl(engine); + engine.Raisable = impl; + var mock = new INotifierMock(impl, engine); + return mock; } } @@ -93,109 +87,106 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class INotifier_MockMemberExtensions { - public static class INotifier_MockMemberExtensions + public static INotifier_Notify_M0_MockCall Notify(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg message) { - public static INotifier_Notify_M0_MockCall Notify(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg message) - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { message.Matcher }; - return new INotifier_Notify_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Notify", matchers); - } - - public static INotifier_Notify_M0_MockCall Notify(this global::TUnit.Mocks.Mock mock, global::System.Func message) - { - global::TUnit.Mocks.Arguments.Arg __fa_message = message; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_message.Matcher }; - return new INotifier_Notify_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Notify", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { message.Matcher }; + return new INotifier_Notify_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Notify", matchers); + } - public static global::TUnit.Mocks.MockMethodCall GetStatus(this global::TUnit.Mocks.Mock mock) - { - var matchers = global::System.Array.Empty(); - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetStatus", matchers); - } + public static INotifier_Notify_M0_MockCall Notify(this global::TUnit.Mocks.Mock mock, global::System.Func message) + { + global::TUnit.Mocks.Arguments.Arg __fa_message = message; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_message.Matcher }; + return new INotifier_Notify_M0_MockCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Notify", matchers); } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public sealed class INotifier_Notify_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification + public static global::TUnit.Mocks.MockMethodCall GetStatus(this global::TUnit.Mocks.Mock mock) { - 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; + var matchers = global::System.Array.Empty(); + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "GetStatus", matchers); + } +} - 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(); - } +[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] +public sealed class INotifier_Notify_M0_MockCall : global::TUnit.Mocks.Verification.ICallVerification +{ + 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; - 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(); - } + 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(); + } - [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; - } + 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(); + } - /// - 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; } - - /// Execute a typed callback using the actual method parameters. - public INotifier_Notify_M0_MockCall Callback(global::System.Action callback) - { - EnsureSetup().Callback(callback); - return this; - } + [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; + } - /// Configure a typed computed exception using the actual method parameters. - public INotifier_Notify_M0_MockCall Throws(global::System.Func exceptionFactory) - { - EnsureSetup().Throws(args => exceptionFactory((string)args[0]!)); - return this; - } + /// + 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; } + + /// Execute a typed callback using the actual method parameters. + public INotifier_Notify_M0_MockCall Callback(global::System.Action callback) + { + EnsureSetup().Callback(callback); + 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); + /// Configure a typed computed exception using the actual method parameters. + public INotifier_Notify_M0_MockCall Throws(global::System.Func exceptionFactory) + { + 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); } @@ -211,9 +202,9 @@ namespace TUnit.Mocks { extension(global::INotifier _) { - public static global::TUnit.Mocks.Generated.INotifierMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) + public static global::INotifierMock Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose) { - return (global::TUnit.Mocks.Generated.INotifierMock)global::TUnit.Mocks.Generated.INotifierMockFactory.CreateAutoMock(behavior); + return (global::INotifierMock)global::INotifierMockFactory.CreateAutoMock(behavior); } } } diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Wrap_Mock_Filters_Internal_Virtual_Members_From_External_Assembly.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Wrap_Mock_Filters_Internal_Virtual_Members_From_External_Assembly.verified.txt index 3f01582c12..a8915f2e1e 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Wrap_Mock_Filters_Internal_Virtual_Members_From_External_Assembly.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Wrap_Mock_Filters_Internal_Virtual_Members_From_External_Assembly.verified.txt @@ -2,7 +2,7 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated.ExternalLib +namespace ExternalLib { file sealed class ExternalServiceWrapMockImpl : global::ExternalLib.ExternalService, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { @@ -81,7 +81,7 @@ namespace TUnit.Mocks.Generated.ExternalLib #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +namespace ExternalLib { public static class ExternalLib_ExternalService_MockMemberExtensions { diff --git a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Wrap_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Wrap_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt index 1bfb09137b..6854171bca 100644 --- a/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Wrap_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt +++ b/TUnit.Mocks.SourceGenerator.Tests/Snapshots/Wrap_Mock_With_Generic_Constrained_Virtual_Methods.verified.txt @@ -2,65 +2,62 @@ #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +file sealed class RepositoryWrapMockImpl : global::Repository, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject { - file sealed class RepositoryWrapMockImpl : global::Repository, global::TUnit.Mocks.IRaisable, global::TUnit.Mocks.IMockObject - { - private readonly global::TUnit.Mocks.MockEngine _engine; - private readonly global::Repository _wrappedInstance; + private readonly global::TUnit.Mocks.MockEngine _engine; + private readonly global::Repository _wrappedInstance; - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + global::TUnit.Mocks.IMock? global::TUnit.Mocks.IMockObject.MockWrapper { get; set; } - [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] - internal RepositoryWrapMockImpl(global::TUnit.Mocks.MockEngine engine, global::Repository wrappedInstance) : base() - { - _engine = engine; - _wrappedInstance = wrappedInstance; - } + [global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers] + internal RepositoryWrapMockImpl(global::TUnit.Mocks.MockEngine engine, global::Repository wrappedInstance) : base() + { + _engine = engine; + _wrappedInstance = wrappedInstance; + } - public override T Get(int id) + public override T Get(int id) + { + if (_engine.TryHandleCallWithReturn(0, "Get", id, default!, out var __result)) { - if (_engine.TryHandleCallWithReturn(0, "Get", id, default!, out var __result)) - { - return __result; - } - return _wrappedInstance.Get(id); + return __result; } + return _wrappedInstance.Get(id); + } - public override void Store(T item) + public override void Store(T item) + { + if (_engine.TryHandleCall(1, "Store", item)) { - if (_engine.TryHandleCall(1, "Store", item)) - { - return; - } - _wrappedInstance.Store(item); + return; } + _wrappedInstance.Store(item); + } - [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] - public void RaiseEvent(string eventName, object? args) - { - throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); - } + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public void RaiseEvent(string eventName, object? args) + { + throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock."); } +} - file static class RepositoryWrapMockFactory +file static class RepositoryWrapMockFactory +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + internal static void Register() { - [global::System.Runtime.CompilerServices.ModuleInitializer] - internal static void Register() - { - global::TUnit.Mocks.MockRegistry.RegisterWrapFactory(Create); - } + global::TUnit.Mocks.MockRegistry.RegisterWrapFactory(Create); + } - private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, global::Repository instance) - { - var engine = new global::TUnit.Mocks.MockEngine(behavior); - engine.IsWrapMock = true; - var impl = new RepositoryWrapMockImpl(engine, instance); - engine.Raisable = impl; - var mock = new global::TUnit.Mocks.Mock(impl, engine); - return mock; - } + private static global::TUnit.Mocks.Mock Create(global::TUnit.Mocks.MockBehavior behavior, global::Repository instance) + { + var engine = new global::TUnit.Mocks.MockEngine(behavior); + engine.IsWrapMock = true; + var impl = new RepositoryWrapMockImpl(engine, instance); + engine.Raisable = impl; + var mock = new global::TUnit.Mocks.Mock(impl, engine); + return mock; } } @@ -71,35 +68,32 @@ namespace TUnit.Mocks.Generated #pragma warning disable #nullable enable -namespace TUnit.Mocks.Generated +public static class Repository_MockMemberExtensions { - public static class Repository_MockMemberExtensions + public static global::TUnit.Mocks.MockMethodCall Get(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg id) where T : class { - public static global::TUnit.Mocks.MockMethodCall Get(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg id) where T : class - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { id.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Get", matchers); - } + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { id.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Get", matchers); + } - public static global::TUnit.Mocks.MockMethodCall Get(this global::TUnit.Mocks.Mock mock, global::System.Func id) where T : class - { - global::TUnit.Mocks.Arguments.Arg __fa_id = id; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_id.Matcher }; - return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Get", matchers); - } + public static global::TUnit.Mocks.MockMethodCall Get(this global::TUnit.Mocks.Mock mock, global::System.Func id) where T : class + { + global::TUnit.Mocks.Arguments.Arg __fa_id = id; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_id.Matcher }; + return new global::TUnit.Mocks.MockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 0, "Get", matchers); + } - public static global::TUnit.Mocks.VoidMockMethodCall Store(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg item) where T : class, new() - { - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { item.Matcher }; - return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Store", matchers); - } + public static global::TUnit.Mocks.VoidMockMethodCall Store(this global::TUnit.Mocks.Mock mock, global::TUnit.Mocks.Arguments.Arg item) where T : class, new() + { + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { item.Matcher }; + return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Store", matchers); + } - public static global::TUnit.Mocks.VoidMockMethodCall Store(this global::TUnit.Mocks.Mock mock, global::System.Func item) where T : class, new() - { - global::TUnit.Mocks.Arguments.Arg __fa_item = item; - var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_item.Matcher }; - return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Store", matchers); - } + public static global::TUnit.Mocks.VoidMockMethodCall Store(this global::TUnit.Mocks.Mock mock, global::System.Func item) where T : class, new() + { + global::TUnit.Mocks.Arguments.Arg __fa_item = item; + var matchers = new global::TUnit.Mocks.Arguments.IArgumentMatcher[] { __fa_item.Matcher }; + return new global::TUnit.Mocks.VoidMockMethodCall(global::TUnit.Mocks.MockRegistry.GetEngine(mock), 1, "Store", matchers); } } diff --git a/TUnit.Mocks.SourceGenerator/Builders/MockBridgeBuilder.cs b/TUnit.Mocks.SourceGenerator/Builders/MockBridgeBuilder.cs index fe48375195..26b9b69ea5 100644 --- a/TUnit.Mocks.SourceGenerator/Builders/MockBridgeBuilder.cs +++ b/TUnit.Mocks.SourceGenerator/Builders/MockBridgeBuilder.cs @@ -26,7 +26,7 @@ public static string Build(MockTypeModel model) writer.AppendLine("#nullable enable"); writer.AppendLine(); - using (writer.Block($"namespace {mockNamespace}")) + using (writer.OptionalNamespaceBlock(mockNamespace)) { BuildBridgeInterface(writer, model, safeName, staticEngineTypeName); writer.AppendLine(); diff --git a/TUnit.Mocks.SourceGenerator/Builders/MockDelegateFactoryBuilder.cs b/TUnit.Mocks.SourceGenerator/Builders/MockDelegateFactoryBuilder.cs index 7c6c84e971..174998412d 100644 --- a/TUnit.Mocks.SourceGenerator/Builders/MockDelegateFactoryBuilder.cs +++ b/TUnit.Mocks.SourceGenerator/Builders/MockDelegateFactoryBuilder.cs @@ -18,7 +18,7 @@ public static string Build(MockTypeModel model) writer.AppendLine("#nullable enable"); writer.AppendLine(); - using (writer.Block($"namespace {mockNamespace}")) + using (writer.OptionalNamespaceBlock(mockNamespace)) { using (writer.Block($"file static class {safeName}MockDelegateFactory")) { diff --git a/TUnit.Mocks.SourceGenerator/Builders/MockEventsBuilder.cs b/TUnit.Mocks.SourceGenerator/Builders/MockEventsBuilder.cs index dc6935a822..54e2b6fbe3 100644 --- a/TUnit.Mocks.SourceGenerator/Builders/MockEventsBuilder.cs +++ b/TUnit.Mocks.SourceGenerator/Builders/MockEventsBuilder.cs @@ -13,13 +13,14 @@ public static string Build(MockTypeModel model) var typeParams = MockImplBuilder.GetTypeParameterList(model); var constraints = MockImplBuilder.GetConstraintClauses(model); var eventsTypeName = MockImplBuilder.GetGeneratedTypeName($"{safeName}_MockEvents", model); + var mockNamespace = MockImplBuilder.GetMockNamespace(model); writer.AppendLine("// "); writer.AppendLine("#pragma warning disable"); writer.AppendLine("#nullable enable"); writer.AppendLine(); - using (writer.Block("namespace TUnit.Mocks.Generated")) + using (writer.OptionalNamespaceBlock(mockNamespace)) { // Lightweight struct holding engine reference — no allocation writer.AppendLine("[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]"); diff --git a/TUnit.Mocks.SourceGenerator/Builders/MockImplBuilder.cs b/TUnit.Mocks.SourceGenerator/Builders/MockImplBuilder.cs index 6eadb3196f..4850c57a2b 100644 --- a/TUnit.Mocks.SourceGenerator/Builders/MockImplBuilder.cs +++ b/TUnit.Mocks.SourceGenerator/Builders/MockImplBuilder.cs @@ -1648,20 +1648,51 @@ private static string SanitizeIdentifier(string name) return sb.ToString(); } - private static bool IsGlobalNamespace(string ns) + /// + /// Root namespace for fallback-mode mock emission, used when the original namespace + /// already contains a colliding type. Forms either TUnit.Mocks.Generated + /// (global-namespace targets) or TUnit.Mocks.Generated.{Namespace}. + /// + internal const string FallbackNamespaceRoot = "TUnit.Mocks.Generated"; + + internal static bool IsGlobalNamespace(string ns) => string.IsNullOrEmpty(ns) || ns == ""; + internal static string SelectMockNamespace(string originalNamespace, bool useFallback) + { + if (useFallback) + { + return IsGlobalNamespace(originalNamespace) + ? FallbackNamespaceRoot + : $"{FallbackNamespaceRoot}.{originalNamespace}"; + } + + return IsGlobalNamespace(originalNamespace) ? "" : originalNamespace; + } + /// /// Gets the generated namespace for mock types. - /// Types in the global namespace go to TUnit.Mocks.Generated; - /// namespaced types go to TUnit.Mocks.Generated.{OriginalNamespace}. + /// Default: emit into the same namespace as the mocked type. For global-namespace + /// types, returns an empty string — callers must skip the namespace { } block. + /// Fallback (when is true): emit + /// into TUnit.Mocks.Generated or TUnit.Mocks.Generated.{Namespace}. /// public static string GetMockNamespace(MockTypeModel model) - { - return IsGlobalNamespace(model.Namespace) - ? "TUnit.Mocks.Generated" - : $"TUnit.Mocks.Generated.{model.Namespace}"; - } + => SelectMockNamespace(model.Namespace, model.UseFallbackNamespace); + + /// + /// Returns a global::-rooted namespace prefix suitable for prepending to a + /// type name. Yields "global::" when the mock namespace is empty (global + /// namespace) and "global::{ns}." otherwise. Use everywhere a builder + /// concatenates a namespace with a type name — concatenating directly with + /// produces invalid global::.TypeName + /// for globally-namespaced mock targets. + /// + public static string GetGlobalMockNamespacePrefix(MockTypeModel model) + => ToGlobalPrefix(GetMockNamespace(model)); + + internal static string ToGlobalPrefix(string mockNamespace) + => mockNamespace.Length == 0 ? "global::" : $"global::{mockNamespace}."; /// /// Gets the fully qualified type name to use as a generic type argument. @@ -1673,8 +1704,8 @@ public static string GetMockableTypeName(MockTypeModel model) { if (!model.HasStaticAbstractMembers) return model.FullyQualifiedName; var shortName = GetCompositeShortSafeName(model); - var ns = GetMockNamespace(model); - return $"global::{ns}.{GetGeneratedTypeName($"{shortName}Mockable", model)}"; + var globalPrefix = GetGlobalMockNamespacePrefix(model); + return $"{globalPrefix}{GetGeneratedTypeName($"{shortName}Mockable", model)}"; } /// diff --git a/TUnit.Mocks.SourceGenerator/Builders/MockMembersBuilder.cs b/TUnit.Mocks.SourceGenerator/Builders/MockMembersBuilder.cs index 4ae90a943a..ba25d26829 100644 --- a/TUnit.Mocks.SourceGenerator/Builders/MockMembersBuilder.cs +++ b/TUnit.Mocks.SourceGenerator/Builders/MockMembersBuilder.cs @@ -50,13 +50,14 @@ public static string Build(MockTypeModel model) var mockableType = MockImplBuilder.GetMockableTypeName(model); var instanceEvents = model.Events.Where(e => !e.IsStaticAbstract).ToArray(); var hasEvents = instanceEvents.Length > 0; + var mockNamespace = MockImplBuilder.GetMockNamespace(model); writer.AppendLine("// "); writer.AppendLine("#pragma warning disable"); writer.AppendLine("#nullable enable"); writer.AppendLine(); - using (writer.Block("namespace TUnit.Mocks.Generated")) + using (writer.OptionalNamespaceBlock(mockNamespace)) { // Extension methods class using (writer.Block($"{model.Visibility} static class {safeName}_MockMemberExtensions")) @@ -694,7 +695,7 @@ private static void GenerateGenericMethodExtensionBlock(CodeWriter writer, MockM if (MockWrapperTypeBuilder.CanGenerateWrapper(model)) { var wrapperSafeName = MockImplBuilder.GetCompositeShortSafeName(model); - var wrapperTypeName = $"global::{MockImplBuilder.GetMockNamespace(model)}.{MockImplBuilder.GetGeneratedTypeName($"{wrapperSafeName}Mock", model)}"; + var wrapperTypeName = $"{MockImplBuilder.GetGlobalMockNamespacePrefix(model)}{MockImplBuilder.GetGeneratedTypeName($"{wrapperSafeName}Mock", model)}"; using (writer.Block($"extension{modelTypeParams}({wrapperTypeName} mock){modelConstraints}")) { GenerateGenericMethodMembersInCurrentBlock(writer, method, model, safeName); diff --git a/TUnit.Mocks.SourceGenerator/Builders/MockStaticExtensionBuilder.cs b/TUnit.Mocks.SourceGenerator/Builders/MockStaticExtensionBuilder.cs index 2fae51b312..3fcfb15736 100644 --- a/TUnit.Mocks.SourceGenerator/Builders/MockStaticExtensionBuilder.cs +++ b/TUnit.Mocks.SourceGenerator/Builders/MockStaticExtensionBuilder.cs @@ -12,14 +12,14 @@ public static string Build(MockTypeModel model) return string.Empty; var shortName = MockImplBuilder.GetCompositeShortSafeName(model); - var mockNamespace = MockImplBuilder.GetMockNamespace(model); + var globalPrefix = MockImplBuilder.GetGlobalMockNamespacePrefix(model); return BuildCore(model, (writer, mockableType, visibility) => { - using (writer.Block($"{visibility} static global::{mockNamespace}.{MockImplBuilder.GetGeneratedTypeName($"{shortName}Mock", model)} Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose)")) + using (writer.Block($"{visibility} static {globalPrefix}{MockImplBuilder.GetGeneratedTypeName($"{shortName}Mock", model)} Mock(global::TUnit.Mocks.MockBehavior behavior = global::TUnit.Mocks.MockBehavior.Loose)")) { - var generatedMockType = $"global::{mockNamespace}.{MockImplBuilder.GetGeneratedTypeName($"{shortName}Mock", model)}"; - var factoryType = $"global::{mockNamespace}.{shortName}MockFactory"; + var generatedMockType = $"{globalPrefix}{MockImplBuilder.GetGeneratedTypeName($"{shortName}Mock", model)}"; + var factoryType = $"{globalPrefix}{shortName}MockFactory"; var typeArguments = MockImplBuilder.GetTypeParameterList(model); writer.AppendLine($"return ({generatedMockType}){factoryType}.CreateAutoMock{typeArguments}(behavior);"); diff --git a/TUnit.Mocks.SourceGenerator/Builders/MockWrapperTypeBuilder.cs b/TUnit.Mocks.SourceGenerator/Builders/MockWrapperTypeBuilder.cs index 00810f2650..373a38ebb9 100644 --- a/TUnit.Mocks.SourceGenerator/Builders/MockWrapperTypeBuilder.cs +++ b/TUnit.Mocks.SourceGenerator/Builders/MockWrapperTypeBuilder.cs @@ -39,7 +39,7 @@ public static string Build(MockTypeModel model) writer.AppendLine("#nullable enable"); writer.AppendLine(); - using (writer.Block($"namespace {mockNamespace}")) + using (writer.OptionalNamespaceBlock(mockNamespace)) { using (writer.Block($"{model.Visibility} sealed class {safeName}Mock{typeParams} : global::TUnit.Mocks.Mock<{mockableType}>, {model.FullyQualifiedName}{constraints}")) { diff --git a/TUnit.Mocks.SourceGenerator/CodeWriter.cs b/TUnit.Mocks.SourceGenerator/CodeWriter.cs index dc196d0ae4..cf5c1e9603 100644 --- a/TUnit.Mocks.SourceGenerator/CodeWriter.cs +++ b/TUnit.Mocks.SourceGenerator/CodeWriter.cs @@ -57,6 +57,17 @@ public IDisposable Block(string? header = null) return new BlockScope(this); } + /// + /// Opens a namespace {ns} { ... } block when is + /// non-empty, or returns a no-op disposable so the caller's content is emitted at + /// top-level. Used by mock builders to skip the namespace block when targeting the + /// global namespace. + /// + public IDisposable OptionalNamespaceBlock(string namespaceName) + => string.IsNullOrEmpty(namespaceName) + ? NoopDisposable.Instance + : Block($"namespace {namespaceName}"); + public void IncreaseIndent() => _indent++; public void DecreaseIndent() => _indent--; @@ -68,4 +79,10 @@ private sealed class BlockScope : IDisposable public BlockScope(CodeWriter writer) => _writer = writer; public void Dispose() => _writer.CloseBrace(); } + + private sealed class NoopDisposable : IDisposable + { + public static readonly NoopDisposable Instance = new(); + public void Dispose() { } + } } diff --git a/TUnit.Mocks.SourceGenerator/Discovery/MemberDiscovery.cs b/TUnit.Mocks.SourceGenerator/Discovery/MemberDiscovery.cs index a91fad8b9c..720faaf9ab 100644 --- a/TUnit.Mocks.SourceGenerator/Discovery/MemberDiscovery.cs +++ b/TUnit.Mocks.SourceGenerator/Discovery/MemberDiscovery.cs @@ -19,7 +19,7 @@ internal static class MemberDiscovery private static readonly (int Index, ITypeSymbol? ReturnType) NonMockableEntry = (-1, null); public static (EquatableArray Methods, EquatableArray Properties, EquatableArray Events) - DiscoverMembers(ITypeSymbol typeSymbol, IAssemblySymbol? compilationAssembly = null) + DiscoverMembers(ITypeSymbol typeSymbol, IAssemblySymbol? compilationAssembly, Compilation compilation) { var methods = new List(); var properties = new List(); @@ -40,7 +40,7 @@ public static (EquatableArray Methods, EquatableArray Methods, EquatableArray Methods, EquatableArray : IEnumerator). var canDelegate = existing.ReturnType is not null && CanDelegateReturnType(existing.ReturnType, method.ReturnType); - methods.Add(CreateMethodModel(method, ref memberIdCounter, interfaceFqn, interfaceFqn, explicitInterfaceCanDelegate: canDelegate)); + methods.Add(CreateMethodModel(method, ref memberIdCounter, interfaceFqn, interfaceFqn, explicitInterfaceCanDelegate: canDelegate, compilation: compilation)); } else { seenMethods[key] = (methods.Count, method.ReturnType); seenFullMethods.Add(GetFullMethodKey(method)); - methods.Add(CreateMethodModel(method, ref memberIdCounter, explicitInterfaceName, interfaceFqn)); + methods.Add(CreateMethodModel(method, ref memberIdCounter, explicitInterfaceName, interfaceFqn, compilation: compilation)); } break; } @@ -109,7 +109,7 @@ public static (EquatableArray Methods, EquatableArray Methods, EquatableArray Methods, EquatableArray Methods, EquatableArray TypeKind guard is genuinely required. /// public static (EquatableArray Methods, EquatableArray Properties, EquatableArray Events) - DiscoverMembersFromMultipleTypes(INamedTypeSymbol[] typeSymbols, IAssemblySymbol? compilationAssembly = null) + DiscoverMembersFromMultipleTypes(INamedTypeSymbol[] typeSymbols, IAssemblySymbol? compilationAssembly, Compilation compilation) { var methods = new List(); var properties = new List(); @@ -199,7 +199,7 @@ public static (EquatableArray Methods, EquatableArray Methods, EquatableArray Methods, EquatableArray Methods, EquatableArray Methods, EquatableArray Methods, EquatableArray methods, List properties, List events, @@ -362,7 +363,7 @@ private static void ProcessClassMembers( { if (seenMethods.ContainsKey(key)) continue; seenMethods[key] = (methods.Count, method.ReturnType); - methods.Add(CreateMethodModel(method, ref memberIdCounter, null)); + methods.Add(CreateMethodModel(method, ref memberIdCounter, null, compilation: compilation)); } else { @@ -386,7 +387,7 @@ private static void ProcessClassMembers( else { seenProperties[key] = properties.Count; - properties.Add(CreatePropertyModel(property, ref memberIdCounter, null, compilationAssembly: compilationAssembly)); + properties.Add(CreatePropertyModel(property, ref memberIdCounter, null, compilationAssembly: compilationAssembly, compilation: compilation)); } } else if (!seenProperties.ContainsKey(key)) @@ -412,7 +413,7 @@ private static void ProcessClassMembers( else { seenProperties[key] = properties.Count; - properties.Add(CreateIndexerModel(indexer, ref memberIdCounter, null, compilationAssembly: compilationAssembly)); + properties.Add(CreateIndexerModel(indexer, ref memberIdCounter, null, compilationAssembly: compilationAssembly, compilation: compilation)); } } else if (!seenProperties.ContainsKey(key)) @@ -553,12 +554,12 @@ private static bool IsTypeAccessible(ITypeSymbol type, IAssemblySymbol compilati /// /// Creates a MockMemberModel from a delegate type's Invoke method. /// - public static MockMemberModel CreateDelegateInvokeModel(IMethodSymbol invokeMethod, ref int memberIdCounter) + public static MockMemberModel CreateDelegateInvokeModel(IMethodSymbol invokeMethod, ref int memberIdCounter, Compilation compilation) { - return CreateMethodModel(invokeMethod, ref memberIdCounter, null); + return CreateMethodModel(invokeMethod, ref memberIdCounter, null, compilation: compilation); } - private static MockMemberModel CreateMethodModel(IMethodSymbol method, ref int memberIdCounter, string? explicitInterfaceName, string? declaringInterfaceName = null, bool explicitInterfaceCanDelegate = false) + private static MockMemberModel CreateMethodModel(IMethodSymbol method, ref int memberIdCounter, string? explicitInterfaceName, string? declaringInterfaceName = null, bool explicitInterfaceCanDelegate = false, Compilation compilation = null!) { var returnType = method.ReturnType; var isAsync = returnType.IsAsyncReturnType(); @@ -571,7 +572,7 @@ private static MockMemberModel CreateMethodModel(IMethodSymbol method, ref int m var effectiveReturnTypeSymbol = returnType.GetAsyncInnerTypeSymbol() ?? returnType; var returnTypeHasStaticAbstract = !isVoid && IsInterfaceWithStaticAbstractMembers(effectiveReturnTypeSymbol); var autoMockFactoryMethod = !isVoid - ? GetAutoMockFactoryMethod(effectiveReturnTypeSymbol) + ? GetAutoMockFactoryMethod(effectiveReturnTypeSymbol, compilation) : null; return new MockMemberModel @@ -658,7 +659,7 @@ private static void MergePropertyAccessors(List properties, int private static bool IsAccessorAccessible(IMethodSymbol? accessor, IAssemblySymbol? compilationAssembly) => accessor is not null && IsMemberAccessible(accessor, compilationAssembly); - private static MockMemberModel CreatePropertyModel(IPropertySymbol property, ref int memberIdCounter, string? explicitInterfaceName, string? declaringInterfaceName = null, IAssemblySymbol? compilationAssembly = null) + private static MockMemberModel CreatePropertyModel(IPropertySymbol property, ref int memberIdCounter, string? explicitInterfaceName, string? declaringInterfaceName = null, IAssemblySymbol? compilationAssembly = null, Compilation compilation = null!) { var hasGetter = IsAccessorAccessible(property.GetMethod, compilationAssembly); var hasSetter = IsAccessorAccessible(property.SetMethod, compilationAssembly); @@ -687,7 +688,7 @@ private static MockMemberModel CreatePropertyModel(IPropertySymbol property, ref IsProtected = property.DeclaredAccessibility == Accessibility.Protected || property.DeclaredAccessibility == Accessibility.ProtectedOrInternal, IsRefStructReturn = property.Type.IsRefLikeType, - AutoMockFactoryMethod = GetAutoMockFactoryMethod(property.Type), + AutoMockFactoryMethod = GetAutoMockFactoryMethod(property.Type, compilation), IsReturnTypeStaticAbstractInterface = IsInterfaceWithStaticAbstractMembers(property.Type), SpanReturnElementType = property.Type.IsRefLikeType ? GetSpanElementType(property.Type) : null, ObsoleteAttribute = propertyObsolete, @@ -747,7 +748,7 @@ public static EquatableArray DiscoverConstructors(INamedTy return new EquatableArray(constructors.ToImmutableArray()); } - private static MockMemberModel CreateIndexerModel(IPropertySymbol indexer, ref int memberIdCounter, string? explicitInterfaceName, string? declaringInterfaceName = null, IAssemblySymbol? compilationAssembly = null) + private static MockMemberModel CreateIndexerModel(IPropertySymbol indexer, ref int memberIdCounter, string? explicitInterfaceName, string? declaringInterfaceName = null, IAssemblySymbol? compilationAssembly = null, Compilation compilation = null!) { var hasGetter = IsAccessorAccessible(indexer.GetMethod, compilationAssembly); var hasSetter = IsAccessorAccessible(indexer.SetMethod, compilationAssembly); @@ -781,14 +782,14 @@ private static MockMemberModel CreateIndexerModel(IPropertySymbol indexer, ref i DeclaringInterfaceName = declaringInterfaceName, NullableAnnotation = indexer.Type.NullableAnnotation.ToString(), SmartDefault = indexer.Type.GetSmartDefault(indexer.Type.IsNullableAnnotated()), - AutoMockFactoryMethod = GetAutoMockFactoryMethod(indexer.Type), + AutoMockFactoryMethod = GetAutoMockFactoryMethod(indexer.Type, compilation), ObsoleteAttribute = indexerObsolete, GetterObsoleteAttribute = GetAccessorObsoleteAttributeSyntax(indexerObsolete, indexer.GetMethod), SetterObsoleteAttribute = GetAccessorObsoleteAttributeSyntax(indexerObsolete, indexer.SetMethod) }; } - private static string? GetAutoMockFactoryMethod(ITypeSymbol returnType) + private static string? GetAutoMockFactoryMethod(ITypeSymbol returnType, Compilation compilation) { var effectiveReturnType = returnType.GetAsyncInnerTypeSymbol() ?? returnType; if (effectiveReturnType is not INamedTypeSymbol namedType) @@ -809,15 +810,16 @@ private static MockMemberModel CreateIndexerModel(IPropertySymbol indexer, ref i return null; var baseName = namedType.OriginalDefinition.GetGeneratedMockBaseName(); - var factoryNamespace = namedType.OriginalDefinition.GetGeneratedMockNamespace(); + var factoryNamespace = namedType.OriginalDefinition.GetGeneratedMockNamespace(compilation); + var globalPrefix = Builders.MockImplBuilder.ToGlobalPrefix(factoryNamespace); if (!namedType.IsGenericType) { - return $"global::{factoryNamespace}.{baseName}MockFactory.CreateAutoMock"; + return $"{globalPrefix}{baseName}MockFactory.CreateAutoMock"; } var typeArguments = string.Join(", ", namedType.TypeArguments.Select(x => x.GetFullyQualifiedNameWithNullability())); - return $"global::{factoryNamespace}.{baseName}MockFactory.CreateAutoMock<{typeArguments}>"; + return $"{globalPrefix}{baseName}MockFactory.CreateAutoMock<{typeArguments}>"; } private static MockEventModel CreateEventModel(IEventSymbol evt, string? explicitInterfaceName, string? declaringInterfaceName = null) @@ -1087,12 +1089,13 @@ private static void TryCollectStaticAbstractFromInterface( Dictionary seenMethods, Dictionary seenProperties, HashSet seenEvents, - ref int memberIdCounter) + ref int memberIdCounter, + Compilation compilation) { if (!member.IsAbstract) return; if (!ShouldCollectStaticAbstractFromInterfaces(typeSymbol)) return; - CollectStaticAbstractMember(member, interfaceFqn, methods, properties, events, seenMethods, seenProperties, seenEvents, ref memberIdCounter); + CollectStaticAbstractMember(member, interfaceFqn, methods, properties, events, seenMethods, seenProperties, seenEvents, ref memberIdCounter, compilation); } /// @@ -1128,7 +1131,8 @@ private static void CollectStaticAbstractMember( Dictionary seenMethods, Dictionary seenProperties, HashSet seenEvents, - ref int memberIdCounter) + ref int memberIdCounter, + Compilation compilation) { switch (member) { @@ -1138,7 +1142,7 @@ private static void CollectStaticAbstractMember( if (seenMethods.ContainsKey(key)) break; seenMethods[key] = (methods.Count, method.ReturnType); - var model = CreateMethodModel(method, ref memberIdCounter, interfaceFqn) with + var model = CreateMethodModel(method, ref memberIdCounter, interfaceFqn, compilation: compilation) with { IsStaticAbstract = true }; @@ -1152,7 +1156,7 @@ private static void CollectStaticAbstractMember( if (seenProperties.ContainsKey(key)) break; seenProperties[key] = properties.Count; - var model = CreatePropertyModel(property, ref memberIdCounter, interfaceFqn) with + var model = CreatePropertyModel(property, ref memberIdCounter, interfaceFqn, compilation: compilation) with { IsStaticAbstract = true }; diff --git a/TUnit.Mocks.SourceGenerator/Discovery/MockNamespaceConflictDetector.cs b/TUnit.Mocks.SourceGenerator/Discovery/MockNamespaceConflictDetector.cs new file mode 100644 index 0000000000..4b272e3799 --- /dev/null +++ b/TUnit.Mocks.SourceGenerator/Discovery/MockNamespaceConflictDetector.cs @@ -0,0 +1,90 @@ +using Microsoft.CodeAnalysis; + +namespace TUnit.Mocks.SourceGenerator.Discovery; + +/// +/// Detects whether placing a generated mock alongside its target type would produce a +/// name collision with an existing user-declared type in that namespace. Run once per +/// type at transform time; result lives on as +/// UseFallbackNamespace. +/// +/// +/// Resolves the target's namespace by name in the consumer's , +/// rather than walking target.ContainingNamespace directly. This matters when +/// comes from a referenced assembly: the namespace symbol on +/// the target only exposes types from that assembly, but the same namespace name may +/// have additional types declared in the consumer's compilation. +/// All seven generator-emitted suffixes are always checked. The events-extensions pair +/// is included unconditionally even though it only ships when the target has events — +/// keeping the rule symmetric guarantees both call sites (model construction and +/// auto-mock factory references) reach the same fallback decision regardless of how +/// they derive their event lists. +/// +internal static class MockNamespaceConflictDetector +{ + internal static bool HasConflict(Compilation compilation, INamedTypeSymbol target) + { + var nsName = target.ContainingNamespace?.ToDisplayString() ?? ""; + var ns = ResolveNamespaceInCompilation(compilation, nsName); + if (ns is null) + { + return false; + } + + var typeName = target.Name; + foreach (var existing in ns.GetTypeMembers()) + { + if (IsCollidingName(existing.Name, typeName)) + { + return true; + } + } + return false; + } + + private static bool IsCollidingName(string existingName, string typeName) + { + if (existingName.Length <= typeName.Length + || !existingName.StartsWith(typeName, System.StringComparison.Ordinal)) + { + return false; + } + + var suffix = existingName.AsSpan(typeName.Length); + return suffix.SequenceEqual("Mock".AsSpan()) + || suffix.SequenceEqual("Mockable".AsSpan()) + || suffix.SequenceEqual("MockFactory".AsSpan()) + || suffix.SequenceEqual("_MockStaticExtension".AsSpan()) + || suffix.SequenceEqual("_MockMemberExtensions".AsSpan()) + || suffix.SequenceEqual("_MockEvents".AsSpan()) + || suffix.SequenceEqual("_MockEventsExtensions".AsSpan()); + } + + private static INamespaceSymbol? ResolveNamespaceInCompilation(Compilation compilation, string namespaceName) + { + if (string.IsNullOrEmpty(namespaceName) || namespaceName == "") + { + return compilation.GlobalNamespace; + } + + INamespaceSymbol? current = compilation.GlobalNamespace; + foreach (var part in namespaceName.Split('.')) + { + INamespaceSymbol? next = null; + foreach (var child in current!.GetNamespaceMembers()) + { + if (child.Name == part) + { + next = child; + break; + } + } + if (next is null) + { + return null; + } + current = next; + } + return current; + } +} diff --git a/TUnit.Mocks.SourceGenerator/Discovery/MockTypeDiscovery.cs b/TUnit.Mocks.SourceGenerator/Discovery/MockTypeDiscovery.cs index a2365e89e7..a509a14928 100644 --- a/TUnit.Mocks.SourceGenerator/Discovery/MockTypeDiscovery.cs +++ b/TUnit.Mocks.SourceGenerator/Discovery/MockTypeDiscovery.cs @@ -83,8 +83,9 @@ public static ImmutableArray TransformToModels(GeneratorSyntaxCon if (namedType.TypeKind == TypeKind.Error) return ImmutableArray.Empty; - // Get the compilation assembly for accessibility checks on external types - var compilationAssembly = context.SemanticModel.Compilation.Assembly; + // Get the compilation and assembly for accessibility checks and namespace conflict detection + var compilation = context.SemanticModel.Compilation; + var compilationAssembly = compilation.Assembly; // Delegate mocking if (isDelegateMock) @@ -92,7 +93,7 @@ public static ImmutableArray TransformToModels(GeneratorSyntaxCon if (namedType.TypeKind != TypeKind.Delegate) return ImmutableArray.Empty; - var delegateModel = BuildDelegateTypeModel(namedType); + var delegateModel = BuildDelegateTypeModel(namedType, compilation); return delegateModel is not null ? ImmutableArray.Create(delegateModel) : ImmutableArray.Empty; @@ -106,7 +107,7 @@ public static ImmutableArray TransformToModels(GeneratorSyntaxCon return ImmutableArray.Empty; // Wrap uses the same model as partial mock but with IsWrapMock flag - var wrapModel = BuildSingleTypeModel(namedType, isPartialMock: true, compilationAssembly); + var wrapModel = BuildSingleTypeModel(namedType, isPartialMock: true, compilationAssembly, compilation); if (wrapModel is null) return ImmutableArray.Empty; @@ -127,7 +128,7 @@ public static ImmutableArray TransformToModels(GeneratorSyntaxCon if (method.TypeArguments.Length == 1) { - return BuildModelWithTransitiveDependencies(NormalizeSingleMockType(namedType), isPartialMock, compilationAssembly); + return BuildModelWithTransitiveDependencies(NormalizeSingleMockType(namedType), isPartialMock, compilationAssembly, compilation); } // Multi-type mock: validate additional type args are all interfaces @@ -142,13 +143,13 @@ public static ImmutableArray TransformToModels(GeneratorSyntaxCon } // Build single-type model for primary type (generates setup/verify/raise) - var singleTypeModel = BuildSingleTypeModel(namedType, isPartialMock, compilationAssembly); + var singleTypeModel = BuildSingleTypeModel(namedType, isPartialMock, compilationAssembly, compilation); if (singleTypeModel is null) return ImmutableArray.Empty; // Build multi-type model (generates impl + factory only) var allTypes = new[] { namedType }.Concat(additionalTypes).ToArray(); - var (methods, properties, events) = MemberDiscovery.DiscoverMembersFromMultipleTypes(allTypes, compilationAssembly); + var (methods, properties, events) = MemberDiscovery.DiscoverMembersFromMultipleTypes(allTypes, compilationAssembly, compilation); var additionalInterfaceNames = ImmutableArray.CreateBuilder(additionalTypes.Count); foreach (var t in additionalTypes) @@ -175,7 +176,8 @@ public static ImmutableArray TransformToModels(GeneratorSyntaxCon AdditionalInterfaceNames = new EquatableArray(additionalInterfaceNames.MoveToImmutable()), Constructors = singleTypeModel.Constructors, HasStaticAbstractMembers = methods.Any(m => m.IsStaticAbstract) || properties.Any(p => p.IsStaticAbstract) || events.Any(e => e.IsStaticAbstract), - IsPublic = IsEffectivelyPublic(namedType) + IsPublic = IsEffectivelyPublic(namedType), + UseFallbackNamespace = singleTypeModel.UseFallbackNamespace }; return ImmutableArray.Create(singleTypeModel, multiTypeModel); @@ -186,7 +188,7 @@ public static ImmutableArray TransformToModels(GeneratorSyntaxCon /// generated for auto-mocking support. Recurses up to maxDepth levels. /// private static List DiscoverTransitiveInterfaceTypes( - INamedTypeSymbol type, HashSet visited, int maxDepth, IAssemblySymbol? compilationAssembly) + INamedTypeSymbol type, HashSet visited, int maxDepth, IAssemblySymbol? compilationAssembly, Compilation compilation) { var results = new List(); if (maxDepth <= 0) return results; @@ -242,12 +244,12 @@ private static List DiscoverTransitiveInterfaceTypes( if (visited.Contains(returnFqn)) continue; visited.Add(returnFqn); - var model = BuildSingleTypeModel(namedReturn, isPartialMock: false, compilationAssembly); + var model = BuildSingleTypeModel(namedReturn, isPartialMock: false, compilationAssembly, compilation); if (model is null) continue; results.Add(model); // Recurse into the transitive type's members - results.AddRange(DiscoverTransitiveInterfaceTypes(namedReturn, visited, maxDepth - 1, compilationAssembly)); + results.AddRange(DiscoverTransitiveInterfaceTypes(namedReturn, visited, maxDepth - 1, compilationAssembly, compilation)); } return results; @@ -303,14 +305,14 @@ private static bool HasStaticAbstractMembers(INamedTypeSymbol interfaceType) return false; } - private static MockTypeModel? BuildDelegateTypeModel(INamedTypeSymbol delegateType) + private static MockTypeModel? BuildDelegateTypeModel(INamedTypeSymbol delegateType, Compilation compilation) { var invokeMethod = delegateType.DelegateInvokeMethod; if (invokeMethod is null) return null; int memberIdCounter = 0; - var methodModel = MemberDiscovery.CreateDelegateInvokeModel(invokeMethod, ref memberIdCounter); + var methodModel = MemberDiscovery.CreateDelegateInvokeModel(invokeMethod, ref memberIdCounter, compilation); return new MockTypeModel { @@ -328,18 +330,19 @@ private static bool HasStaticAbstractMembers(INamedTypeSymbol interfaceType) Events = EquatableArray.Empty, AllInterfaces = EquatableArray.Empty, IsPublic = IsEffectivelyPublic(delegateType), + UseFallbackNamespace = MockNamespaceConflictDetector.HasConflict(compilation, delegateType), }; } private static ImmutableArray BuildModelWithTransitiveDependencies( - INamedTypeSymbol namedType, bool isPartialMock, IAssemblySymbol? compilationAssembly) + INamedTypeSymbol namedType, bool isPartialMock, IAssemblySymbol? compilationAssembly, Compilation compilation) { - var model = BuildSingleTypeModel(namedType, isPartialMock, compilationAssembly); + var model = BuildSingleTypeModel(namedType, isPartialMock, compilationAssembly, compilation); if (model is null) return ImmutableArray.Empty; var visited = new HashSet(); - var transitiveModels = DiscoverTransitiveInterfaceTypes(namedType, visited, maxDepth: 3, compilationAssembly); + var transitiveModels = DiscoverTransitiveInterfaceTypes(namedType, visited, maxDepth: 3, compilationAssembly, compilation); if (transitiveModels.Count == 0) return ImmutableArray.Create(model); @@ -350,9 +353,9 @@ private static ImmutableArray BuildModelWithTransitiveDependencie return builder.MoveToImmutable(); } - private static MockTypeModel? BuildSingleTypeModel(INamedTypeSymbol namedType, bool isPartialMock, IAssemblySymbol? compilationAssembly) + private static MockTypeModel? BuildSingleTypeModel(INamedTypeSymbol namedType, bool isPartialMock, IAssemblySymbol? compilationAssembly, Compilation compilation) { - var (methods, properties, events) = MemberDiscovery.DiscoverMembers(namedType, compilationAssembly); + var (methods, properties, events) = MemberDiscovery.DiscoverMembers(namedType, compilationAssembly, compilation); // Discover constructors for partial mocks of classes var constructors = isPartialMock && namedType.TypeKind == TypeKind.Class @@ -379,7 +382,8 @@ private static ImmutableArray BuildModelWithTransitiveDependencie ), Constructors = constructors, HasStaticAbstractMembers = methods.Any(m => m.IsStaticAbstract) || properties.Any(p => p.IsStaticAbstract) || events.Any(e => e.IsStaticAbstract), - IsPublic = IsEffectivelyPublic(namedType) + IsPublic = IsEffectivelyPublic(namedType), + UseFallbackNamespace = MockNamespaceConflictDetector.HasConflict(compilation, namedType) }; } @@ -539,8 +543,9 @@ public static ImmutableArray TransformMockExtensionInvocation( return ImmutableArray.Empty; var isPartialMock = namedType.TypeKind == TypeKind.Class; - var compilationAssembly = context.SemanticModel.Compilation.Assembly; - return BuildModelWithTransitiveDependencies(NormalizeSingleMockType(namedType), isPartialMock, compilationAssembly); + var compilation = context.SemanticModel.Compilation; + var compilationAssembly = compilation.Assembly; + return BuildModelWithTransitiveDependencies(NormalizeSingleMockType(namedType), isPartialMock, compilationAssembly, compilation); } // ─── [assembly: GenerateMock(typeof(T))] discovery ──────────────────── @@ -554,7 +559,8 @@ public static ImmutableArray TransformGenerateMockAttribute( { // The target symbol for an assembly attribute is the assembly itself // The attribute constructor argument is typeof(T) - var compilationAssembly = context.SemanticModel.Compilation.Assembly; + var compilation = context.SemanticModel.Compilation; + var compilationAssembly = compilation.Assembly; foreach (var attr in context.Attributes) { if (attr.AttributeClass?.Name is not ("GenerateMockAttribute" or "GenerateMock")) @@ -578,7 +584,8 @@ public static ImmutableArray TransformGenerateMockAttribute( return BuildModelWithTransitiveDependencies( NormalizeSingleMockType(namedType), isPartialMock: namedType.TypeKind == TypeKind.Class, - compilationAssembly); + compilationAssembly, + compilation); } return ImmutableArray.Empty; diff --git a/TUnit.Mocks.SourceGenerator/Extensions/TypeSymbolExtensions.cs b/TUnit.Mocks.SourceGenerator/Extensions/TypeSymbolExtensions.cs index 8fbf827033..e641c05946 100644 --- a/TUnit.Mocks.SourceGenerator/Extensions/TypeSymbolExtensions.cs +++ b/TUnit.Mocks.SourceGenerator/Extensions/TypeSymbolExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using TUnit.Mocks.SourceGenerator.Discovery; namespace TUnit.Mocks.SourceGenerator.Extensions; @@ -86,12 +87,11 @@ public static string GetOpenGenericTypeOfExpression(this INamedTypeSymbol type) return builder.ToString(); } - public static string GetGeneratedMockNamespace(this INamedTypeSymbol type) + public static string GetGeneratedMockNamespace(this INamedTypeSymbol type, Compilation compilation) { var namespaceName = type.ContainingNamespace?.ToDisplayString() ?? ""; - return string.IsNullOrEmpty(namespaceName) || namespaceName == "" - ? "TUnit.Mocks.Generated" - : $"TUnit.Mocks.Generated.{namespaceName}"; + var fallback = MockNamespaceConflictDetector.HasConflict(compilation, type); + return Builders.MockImplBuilder.SelectMockNamespace(namespaceName, fallback); } public static string GetGeneratedMockBaseName(this INamedTypeSymbol type) diff --git a/TUnit.Mocks.SourceGenerator/MockGenerator.cs b/TUnit.Mocks.SourceGenerator/MockGenerator.cs index 73be61e56c..8afe1db518 100644 --- a/TUnit.Mocks.SourceGenerator/MockGenerator.cs +++ b/TUnit.Mocks.SourceGenerator/MockGenerator.cs @@ -175,7 +175,7 @@ private static string BuildCombinedImplAndFactory(MockTypeModel model) writer.AppendLine("#nullable enable"); writer.AppendLine(); - using (writer.Block($"namespace {mockNamespace}")) + using (writer.OptionalNamespaceBlock(mockNamespace)) { MockImplBuilder.BuildInto(writer, model); writer.AppendLine(); diff --git a/TUnit.Mocks.SourceGenerator/Models/MockTypeModel.cs b/TUnit.Mocks.SourceGenerator/Models/MockTypeModel.cs index 86b7949997..887cc9a712 100644 --- a/TUnit.Mocks.SourceGenerator/Models/MockTypeModel.cs +++ b/TUnit.Mocks.SourceGenerator/Models/MockTypeModel.cs @@ -33,6 +33,13 @@ internal sealed record MockTypeModel : IEquatable /// public bool IsPublic { get; init; } = true; + /// + /// True when generation must fall back to TUnit.Mocks.Generated[.{Namespace}] + /// because the original namespace already declares a type whose name would collide + /// with one of the generator's emitted public type names. + /// + public bool UseFallbackNamespace { get; init; } + /// The C# visibility keyword to emit on generated wrapper/extension types. public string Visibility => IsPublic ? "public" : "internal"; @@ -49,6 +56,7 @@ public bool Equals(MockTypeModel? other) && IsDelegateType == other.IsDelegateType && IsWrapMock == other.IsWrapMock && IsPublic == other.IsPublic + && UseFallbackNamespace == other.UseFallbackNamespace && TypeParameters.Equals(other.TypeParameters) && Methods.Equals(other.Methods) && Properties.Equals(other.Properties) @@ -70,6 +78,7 @@ public override int GetHashCode() hash = hash * 31 + IsDelegateType.GetHashCode(); hash = hash * 31 + IsWrapMock.GetHashCode(); hash = hash * 31 + IsPublic.GetHashCode(); + hash = hash * 31 + UseFallbackNamespace.GetHashCode(); hash = hash * 31 + TypeParameters.GetHashCode(); hash = hash * 31 + Methods.GetHashCode(); hash = hash * 31 + Properties.GetHashCode(); diff --git a/TUnit.Mocks.SourceGenerator/TUnit.Mocks.SourceGenerator.csproj b/TUnit.Mocks.SourceGenerator/TUnit.Mocks.SourceGenerator.csproj index 8c42241868..872f39dcbb 100644 --- a/TUnit.Mocks.SourceGenerator/TUnit.Mocks.SourceGenerator.csproj +++ b/TUnit.Mocks.SourceGenerator/TUnit.Mocks.SourceGenerator.csproj @@ -12,6 +12,10 @@ false + + + + all diff --git a/TUnit.Mocks.Tests/StaticAbstractMemberTests.cs b/TUnit.Mocks.Tests/StaticAbstractMemberTests.cs index 42ec870ae7..5a5d26d6d2 100644 --- a/TUnit.Mocks.Tests/StaticAbstractMemberTests.cs +++ b/TUnit.Mocks.Tests/StaticAbstractMemberTests.cs @@ -2,7 +2,6 @@ using System.Diagnostics.CodeAnalysis; using TUnit.Mocks; using TUnit.Mocks.Generated; -using TUnit.Mocks.Generated.TUnit.Mocks.Tests; // Discovery: typeof() does not trigger CS8920, so this is safe for interfaces // with static abstract members. The generator produces a bridge interface