Repository navigation
fix: verify an omitted optional parameter against its declared default in classic mode - #1027
Conversation
…t in classic mode
With the classic overloads (C# 14 or `MockolateDisableUnionParameters`, and the members that keep them in union mode: overloaded, generic and `params` methods), setup and verify disagreed about an optional parameter that is left out or given a `null` matcher. The setup maps it to the declared default value, but the verification compared with `default(T)`, so the verification of a call that happened failed and its message showed an empty argument:
```csharp
public interface IOpt { void Opt(string? s, int number = 5); }
sut.Opt("a");
sut.Mock.Verify.Opt(It.IsAny<string?>()).Once();
// before: Expected that mock invoked method Opt(It.IsAny<string>(), ) exactly once, but it never did.
```
The generated verify method now replaces a `null` matcher of an optional parameter with a `LiteralParameterMatch<T>` for the declared default value before it builds the verification (`number ??= new LiteralParameterMatch<int>(5);`). Because this happens up front, the typed fast path, the mixed literal/matcher overloads and the predicate path (generic methods, more than four parameters) all match the declared default, for methods and delegates, and the failure message prints it like a literal value (`Opt(It.IsAny<string>(), 5)`). It allocates one matcher, as the `It.Is<T>(default!)` it replaces did. The union overloads already used the declared default and are unchanged.
Behavior change: in a classic verification an explicit `null` matcher for an optional parameter now also means the declared default value instead of `default(T)`, exactly as in the setup and as the parameter matching page documents. For a parameter whose declared default is `default(T)` only the failure message changes (`0` instead of an empty argument for an `int`).
|
Test Results 47 files 47 suites 59m 16s ⏱️ Results for commit 9b346f3. |
🚀 Benchmark ResultsCompared to CallbackBenchmarks✅ No changes in memory or relative performance (1 benchmarks) All benchmarks compared to main
Raw resultsBenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 9V74 2.87GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10
CompleteEventBenchmarksRelative performance (how many times as long the other library takes as Mockolate, main → PR)
All benchmarks compared to main
Raw resultsBenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) Intel Xeon Platinum 8370C CPU 2.80GHz (Max: 3.39GHz), 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10
CompleteIndexerBenchmarks✅ No changes in memory or relative performance (3 benchmarks) All benchmarks compared to main
Raw resultsBenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 7763 3.24GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10
CompleteMethodBenchmarks✅ No changes in memory or relative performance (3 benchmarks) All benchmarks compared to main
Raw resultsBenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 9V74 3.69GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10
CompletePropertyBenchmarksRelative performance (how many times as long the other library takes as Mockolate, main → PR)
All benchmarks compared to main
Raw resultsBenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 9V45 4.52GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10
CompleteUnconfiguredBenchmarksMemory
Relative performance (how many times as long the other library takes as Mockolate, main → PR)
All benchmarks compared to main
Raw resultsBenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 9V74 3.68GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10
CompleteWorkflowBenchmarks✅ No changes in memory or relative performance (1 benchmarks) All benchmarks compared to main
Raw resultsBenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) Intel Xeon 6975P-C 3.74GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10
MockCreationBenchmarks✅ No changes in memory or relative performance (1 benchmarks) All benchmarks compared to main
Raw resultsBenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 7763 2.59GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10
|
…er against its declared default in classic mode (#1027) by Valentin Breuß
…er against its declared default in classic mode (#1027) by Valentin Breuß
|
This is addressed in release v4.2.0. |



With the classic overloads (C# 14 or
MockolateDisableUnionParameters, and the members that keep them in union mode: overloaded, generic andparamsmethods), setup and verify disagreed about an optional parameter that is left out or given anullmatcher. The setup maps it to the declared default value, but the verification compared withdefault(T), so the verification of a call that happened failed and its message showed an empty argument:The generated verify method now replaces a
nullmatcher of an optional parameter with aLiteralParameterMatch<T>for the declared default value before it builds the verification (number ??= new LiteralParameterMatch<int>(5);). Because this happens up front, the typed fast path, the mixed literal/matcher overloads and the predicate path (generic methods, more than four parameters) all match the declared default, for methods and delegates, and the failure message prints it like a literal value (Opt(It.IsAny<string>(), 5)). It allocates one matcher, as theIt.Is<T>(default!)it replaces did. The union overloads already used the declared default and are unchanged.Behavior change: in a classic verification an explicit
nullmatcher for an optional parameter now also means the declared default value instead ofdefault(T), exactly as in the setup and as the parameter matching page documents. For a parameter whose declared default isdefault(T)only the failure message changes (0instead of an empty argument for anint).