Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IRepository_string__GetById_M0_MockCall Callback(global::System.Action<int> callback)
{
EnsureSetup().Callback(args => callback((int)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down Expand Up @@ -236,7 +236,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IRepository_string__Save_M1_MockCall Callback(global::System.Action<string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public Sandbox_IFoo_Sandbox_Bar__Process_M1_MockCall Callback(global::System.Action<global::Sandbox.Bar> callback)
{
EnsureSetup().Callback(args => callback((global::Sandbox.Bar)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public Sandbox_IMapper_Sandbox_Entity_Sandbox_Status__Map_M0_MockCall Callback(global::System.Action<global::Sandbox.Entity> callback)
{
EnsureSetup().Callback(args => callback((global::Sandbox.Entity)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public Sandbox_IService_Outer_Inner_Config__Apply_M1_MockCall Callback(global::System.Action<global::Outer.Inner.Config> callback)
{
EnsureSetup().Callback(args => callback((global::Outer.Inner.Config)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IReadWriter_Write_M2_MockCall Callback(global::System.Action<string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IAsyncService_GetValueAsync_M0_MockCall Callback(global::System.Action<string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down Expand Up @@ -415,7 +415,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IAsyncService_ComputeAsync_M2_MockCall Callback(global::System.Action<int> callback)
{
EnsureSetup().Callback(args => callback((int)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down Expand Up @@ -492,7 +492,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IAsyncService_InitializeAsync_M3_MockCall Callback(global::System.Action<global::System.Threading.CancellationToken> callback)
{
EnsureSetup().Callback(args => callback((global::System.Threading.CancellationToken)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public INotifier_Notify_M0_MockCall Callback(global::System.Action<string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public ITest_Test_M0_MockCall Callback(global::System.Action<string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down Expand Up @@ -251,7 +251,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public ITest_Get_M1_MockCall Callback(global::System.Action<int, string> callback)
{
EnsureSetup().Callback(args => callback((int)args[0]!, (string)args[1]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IService_GetAsync_M3_MockCall Callback(global::System.Action<int> callback)
{
EnsureSetup().Callback(args => callback((int)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down Expand Up @@ -347,7 +347,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IService_Process_M4_MockCall Callback(global::System.Action<string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IFoo_Bar_M0_MockCall Callback(global::System.Action<object?> callback)
{
EnsureSetup().Callback(args => callback((object?)args[0]));
EnsureSetup().Callback(callback);
return this;
}

Expand Down Expand Up @@ -368,7 +368,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IFoo_GetValue_M1_MockCall Callback(global::System.Action<string?, int> callback)
{
EnsureSetup().Callback(args => callback((string?)args[0], (int)args[1]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down Expand Up @@ -438,7 +438,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IFoo_Process_M2_MockCall Callback(global::System.Action<string, string?, object?> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!, (string?)args[1], (object?)args[2]));
EnsureSetup().Callback(callback);
return this;
}

Expand Down Expand Up @@ -530,7 +530,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IFoo_GetAsync_M3_MockCall Callback(global::System.Action<string?> callback)
{
EnsureSetup().Callback(args => callback((string?)args[0]));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IDictionary_TryGetValue_M0_MockCall Callback(global::System.Action<string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down Expand Up @@ -245,7 +245,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IDictionary_Swap_M1_MockCall Callback(global::System.Action<int, int> callback)
{
EnsureSetup().Callback(args => callback((int)args[0]!, (int)args[1]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IFormatter_Format_M0_MockCall Callback(global::System.Action<string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down Expand Up @@ -345,7 +345,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IFormatter_Format_M1_MockCall Callback(global::System.Action<int> callback)
{
EnsureSetup().Callback(args => callback((int)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down Expand Up @@ -425,7 +425,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IFormatter_Format_M2_MockCall Callback(global::System.Action<string, string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!, (string)args[1]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down Expand Up @@ -505,7 +505,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IFormatter_Format_M3_MockCall Callback(global::System.Action<string, string, string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!, (string)args[1]!, (string)args[2]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IMyService_GetValue_M0_MockCall Callback(global::System.Action<string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public ICalculator_Add_M0_MockCall Callback(global::System.Action<int, int> callback)
{
EnsureSetup().Callback(args => callback((int)args[0]!, (int)args[1]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down Expand Up @@ -289,7 +289,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public ICalculator_Subtract_M1_MockCall Callback(global::System.Action<int, int> callback)
{
EnsureSetup().Callback(args => callback((int)args[0]!, (int)args[1]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public ExternalLib_ExternalClient_PublicMethod_M0_MockCall Callback(global::System.Action<string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public ExternalLib_ServiceClient_GetValue_M0_MockCall Callback(global::System.Action<string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public IGreeter_Greet_M0_MockCall Callback(global::System.Action<string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ namespace TUnit.Mocks.Generated
/// <summary>Execute a typed callback using the actual method parameters.</summary>
public INotifier_Notify_M0_MockCall Callback(global::System.Action<string> callback)
{
EnsureSetup().Callback(args => callback((string)args[0]!));
EnsureSetup().Callback(callback);
return this;
}

Expand Down
14 changes: 12 additions & 2 deletions TUnit.Mocks.SourceGenerator/Builders/MockMembersBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,22 @@ private static void GenerateTypedCallbackOverload(CodeWriter writer, List<MockPa
{
var typeList = string.Join(", ", nonOutParams.Select(p => p.FullyQualifiedType));
var actionType = $"global::System.Action<{typeList}>";
var castArgs = BuildCastArgs(nonOutParams, allNonOutParams);

writer.AppendLine("/// <summary>Execute a typed callback using the actual method parameters.</summary>");
using (writer.Block($"public {wrapperName} Callback({actionType} callback)"))
{
writer.AppendLine($"EnsureSetup().Callback(args => callback({castArgs}));");
// allNonOutParams is null when this is the primary overload (no out/ref struct subset remapping).
// In that case the callback's parameter types match the typed Callback<T1,...> overload directly,
// so we can register it without a wrapping closure — avoiding the object?[] allocation.
if (allNonOutParams is null && nonOutParams.Count <= MaxTypedParams)
{
writer.AppendLine("EnsureSetup().Callback(callback);");
}
else
{
var castArgs = BuildCastArgs(nonOutParams, allNonOutParams);
writer.AppendLine($"EnsureSetup().Callback(args => callback({castArgs}));");
}
writer.AppendLine("return this;");
}
}
Expand Down
Loading
Loading