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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Mockolate is a source-generated mocking library for .NET. Users write `IFoo.Crea

## SDK and Build

- Use the SDK pinned in `global.json` (currently .NET 11, `rollForward: latestPatch`). Tests also run on .NET 10 and .NET 8, and on .NET Framework 4.8 on Windows only. On .NET 11 the test projects compile with C# 15, so the union-typed setup surface is exercised there.
- Use the SDK pinned in `global.json` (currently .NET 11, `rollForward: latestPatch`). Tests also run on .NET 10 and .NET 8, and on .NET Framework 4.8 on Windows only. The test projects compile with C# 15 on every framework, but `Tests/Directory.Build.props` sets `MockolateDisableUnionParameters` for all except `net11.0`, so the union-typed setup surface is exercised on .NET 11 and the classic one on the others.
- Build from the root with `dotnet build Mockolate.slnx`. Shared settings live in the root `Directory.Build.props` (strong naming, `MinimumRoslynVersion`), `Source/Directory.Build.props` (target frameworks, warnings as errors, AOT and trimming flags) and `Tests/Directory.Build.props` (test frameworks, TUnit, aweXpect).
- Package versions belong only in `Directory.Packages.props`. `VersionOverride` is used only to pin Roslyn packages to `$(MinimumRoslynVersion)`.

Expand Down
6 changes: 4 additions & 2 deletions Docs/agents/workflows.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ Target frameworks per test project:

| Project | Frameworks |
|---|---|
| `Mockolate.Tests`, `Mockolate.Internal.Tests`, `Mockolate.ExampleTests` | `net11.0` (C# 15, union parameters), `net10.0`, `net8.0`, `net48` (Windows only) |
| `Mockolate.Tests`, `Mockolate.Internal.Tests`, `Mockolate.ExampleTests` | `net11.0` (union mode); `net10.0`, `net8.0` and `net48` (Windows only) in classic mode |
| `Mockolate.SourceGenerators.Tests` | `net11.0`, `net10.0` (compiles against `MinimumRoslynVersion`, without the snapshot and union tests) |
| `Mockolate.Analyzers.Tests` | `net11.0` |
| `Build.Tests` | `net10.0` |
| `Mockolate.Api.Tests` | `net8.0` |

Every framework compiles with C# 15. The setup surface is decided by `MockolateDisableUnionParameters`, which `Tests/Directory.Build.props` sets for every framework except `net11.0`, not by the framework itself. Guard tests of union-only syntax (a lambda as an argument, `AnyParameters()` after matchers, values mixed with matchers on a member with more than four parameters) with `#if NET11_0_OR_GREATER`; write tests that hold in both modes so they compile in both. Classic failure messages describe a null argument as an empty string, union ones as `null`.

A newer language feature can compile for `net10.0` and fail for `net48`; build or test `net48` too when you use one.

### Which tests to run

| Change in | Run |
|---|---|
| `Source/Mockolate` | `Mockolate.Tests`, `Mockolate.Internal.Tests`, `Mockolate.Api.Tests` |
| `Source/Mockolate.SourceGenerators` | `Mockolate.SourceGenerators.Tests`, `Mockolate.Tests` (it compiles the generated mocks) |
| `Source/Mockolate.SourceGenerators` | `Mockolate.SourceGenerators.Tests`, `Mockolate.Tests` (it compiles the generated mocks; `net11.0` for union mode and `net10.0` for classic mode) |
| `Source/Mockolate.Analyzers*` | `Mockolate.Analyzers.Tests` |
| Usage examples | `Mockolate.ExampleTests` |
| `Pipeline/BenchmarkReport.cs` | `Build.Tests` |
Expand Down
21 changes: 6 additions & 15 deletions Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5196,21 +5196,9 @@ private static void AppendIndexerVerifyImplementation(StringBuilder sb, Property
}

sb.Append("]\"");
int kk = 0;
foreach (string? parameterName in indexer.IndexerParameters.Value.Select(p => p.Name))
{
sb.Append(", ");
bool isValueParam = valueFlags?[kk] == true;
if (isValueParam)
{
sb.Append("(object?)").Append(parameterName);
}
else
{
sb.Append("(object?)").Append(parameterName).Append(" ?? \"null\"");
}

kk++;
sb.Append(", (object?)").Append(parameterName).Append(" ?? \"null\"");
}

sb.Append("));").AppendLine();
Expand Down Expand Up @@ -6207,7 +6195,7 @@ private static void AppendMethodVerifyImplementation(StringBuilder sb, Method me
}

sb.Append(", () => $\"").Append(method.Name).Append("(")
.Append(useParameters ? "{parameters}" : string.Join(", ", method.Parameters.Select(p => $"{{{p.Name}}}")))
.Append(useParameters ? "{parameters}" : string.Join(", ", method.Parameters.Select(ExpectationPlaceholder)))
.Append(")\");").AppendLine();
if (hasDeclaredDefaults)
{
Expand All @@ -6234,9 +6222,12 @@ private static void AppendStaticExpectationLambda(StringBuilder sb, Method metho
{
string parameters = string.Join(", ", method.Parameters.Select(p => p.Name));
sb.Append(", static (").Append(parameters).Append(") => $\"").Append(method.Name).Append("(")
.Append(string.Join(", ", method.Parameters.Select(p => $"{{{p.Name}}}"))).Append(")\");").AppendLine();
.Append(string.Join(", ", method.Parameters.Select(ExpectationPlaceholder))).Append(")\");").AppendLine();
}

private static string ExpectationPlaceholder(MethodParameter parameter)
=> $"{{(object?){parameter.Name} ?? \"null\"}}";

/// <summary>
/// The slow verify path's condition for one classic matcher argument, shared between the classic and the
/// union-mode emission. out/ref verify parameters don't inherit from <c>IParameter&lt;T&gt;</c>, so they keep
Expand Down
5 changes: 3 additions & 2 deletions Source/Mockolate/Internals/LiteralParameterMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace Mockolate.Internals;
/// <remarks>
/// Public only because generated code in the user's assembly calls it; not intended for direct use from user
/// code. It compares with <see cref="EqualityComparer{T}.Default" /> (expected value first) and renders the value
/// as string interpolation does, so failure messages read like the literal value itself.
/// as string interpolation does and <see langword="null" /> as <c>null</c>, so failure messages read like the
/// literal value itself.
/// </remarks>
/// <typeparam name="T">The declared type of the parameter.</typeparam>
#if !DEBUG
Expand Down Expand Up @@ -50,5 +51,5 @@ public void InvokeCallbacks(T value)

/// <inheritdoc cref="object.ToString()" />
public override string ToString()
=> $"{_value}";
=> _value is null ? "null" : $"{_value}";
}
5 changes: 5 additions & 0 deletions Tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<GenerateRuntimeConfigDevFile>false</GenerateRuntimeConfigDevFile>
</PropertyGroup>

<!-- Every framework compiles with C# 15, so the switch keeps the classic setup surface tested below net11.0. -->
<PropertyGroup Condition="'$(TargetFramework)' != 'net11.0'">
<MockolateDisableUnionParameters>true</MockolateDisableUnionParameters>
</PropertyGroup>

<!--
The tests run on TUnit as executables of the Microsoft.Testing.Platform. TUnit discovers them through
reflection instead of its source generator, which keeps the compilation of the large test projects fast.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public async Task ToString_ShouldFormatLikeStringInterpolation()
await That(result).IsEqualTo($"{value}");
await That(result).IsEqualTo("4,5");
await That(stringResult).IsEqualTo("Alice");
await That(nullResult).IsEqualTo($"{nullValue}");
await That(nullResult).IsEqualTo("null")
.Because("failure messages print a null argument as null instead of an empty string");
}
finally
{
Expand Down
35 changes: 20 additions & 15 deletions Tests/Mockolate.SourceGenerators.Tests/ClassicVerifyMessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public interface IFoo
void P1(string? value);
void P3(int a, string? b, int c);
void P4(int a, int b, int c, int d);
int this[string? key] { get; }
}

public static class Scenarios
Expand All @@ -31,6 +32,8 @@ public static string[] Run()
IFoo sut = IFoo.CreateMock();
return
[
Capture(() => sut.Mock.Verify[(string?)null].Got().AtLeastOnce()),
Capture(() => sut.Mock.Verify[(IParameter<string>?)null].Got().AtLeastOnce()),
Capture(() => sut.Mock.Verify.P1((IParameter<string>?)null).AtLeastOnce()),
Capture(() => sut.Mock.Verify.P1((string?)null).AtLeastOnce()),
Capture(() => sut.Mock.Verify.P3(1, null, 3).AtLeastOnce()),
Expand Down Expand Up @@ -58,13 +61,15 @@ private static string Capture(Action act)
string[] messages = (string[])assembly.GetType("MyCode.Scenarios")!.GetMethod("Run")!.Invoke(null, null)!;

await That(messages).IsEqualTo([
"Expected that mock invoked method P1() at least once, but it never did.",
"Expected that mock invoked method P1() at least once, but it never did.",
"Expected that mock invoked method P3(1, , 3) at least once, but it never did.",
"Expected that mock invoked method P3(It.IsAny<int>(), , It.IsAny<int>()) at least once, but it never did.",
"Expected that mock got indexer [null] at least once, but it never did.",
"Expected that mock got indexer [null] at least once, but it never did.",
"Expected that mock invoked method P1(null) at least once, but it never did.",
"Expected that mock invoked method P1(null) at least once, but it never did.",
"Expected that mock invoked method P3(1, null, 3) at least once, but it never did.",
"Expected that mock invoked method P3(It.IsAny<int>(), null, It.IsAny<int>()) at least once, but it never did.",
"Expected that mock invoked method P4(1, 2, 3, 4) at least once, but it never did.",
"Expected that mock invoked method P4(It.IsAny<int>(), , It.IsAny<int>(), It.IsAny<int>()) at least once, but it never did.",
]).Because("classic mode prints a null literal and a null matcher as an empty string");
"Expected that mock invoked method P4(It.IsAny<int>(), null, It.IsAny<int>(), It.IsAny<int>()) at least once, but it never did.",
]).Because("classic mode prints a null literal and a null matcher as null, like union mode does");
}

[Test]
Expand Down Expand Up @@ -142,10 +147,10 @@ private static string Capture(Action act)
await That(messages).IsEqualTo([
"0, 0, 0, 0, 0, 0",
"1, 1, 1, 1, 1",
"Expected that mock invoked method One() at least once, but it never did.",
"Expected that mock invoked method Optional(, 5) at least once, but it never did.",
"Expected that mock invoked method OptionalWithNonNullDefault(, 5) at least once, but it never did.",
"Expected that mock invoked method WithParams(, System.Int32[]) at least once, but it never did.",
"Expected that mock invoked method One(null) at least once, but it never did.",
"Expected that mock invoked method Optional(null, 5) at least once, but it never did.",
"Expected that mock invoked method OptionalWithNonNullDefault(null, 5) at least once, but it never did.",
"Expected that mock invoked method WithParams(null, System.Int32[]) at least once, but it never did.",
"no exception",
"no exception",
"no exception",
Expand Down Expand Up @@ -297,7 +302,7 @@ await That(messages).IsEqualTo([
"Expected that mock never invoked method Opt(a, 5), but it did once.",
"Expected that mock never invoked method Many(It.IsAny<int>(), x, Milk, 2), but it did once.",
"Expected that mock never invoked method Many(1, It.IsAny<string>(), Milk, 2), but it did once.",
"Expected that mock never invoked method Defaults(It.IsAny<int>(), , 00:00:00, , It.IsAny<DateTime>()), but it did once.",
"Expected that mock never invoked method Defaults(It.IsAny<int>(), null, 00:00:00, null, It.IsAny<DateTime>()), but it did once.",
"Expected that mock never invoked method Nullable(It.IsAny<int>(), 7), but it did once.",
"Expected that mock never invoked method Generic<T>(It.IsAny<string>(), 5), but it did once.",
"Expected that mock never invoked method Five(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<int>(), 5), but it did once.",
Expand Down Expand Up @@ -378,8 +383,8 @@ await That(messages).IsEqualTo([
"no exception",
"Expected that mock invoked method P2(b, It.IsAny<int>()) exactly once, but it never did.",
"Expected that mock invoked method P2(It.IsAny<string>(), 3) exactly once, but it never did.",
"Expected that mock invoked method P2(, 1) exactly once, but it never did.",
"Expected that mock invoked method P2(, 1) exactly once, but it never did.",
"Expected that mock invoked method P2(null, 1) exactly once, but it never did.",
"Expected that mock invoked method P2(null, 1) exactly once, but it never did.",
"Expected that mock invoked method P3(It.IsAny<int>(), y, 3) exactly once, but it never did.",
"Expected that mock invoked method P4(It.IsAny<int>(), 5, It.IsAny<int>(), 4) exactly once, but it never did.",
"no exception",
Expand Down Expand Up @@ -482,8 +487,8 @@ await That(messages).IsEqualTo([
"no exception",
"no exception",
"no exception",
"Expected that mock invoked method Q3(, It.IsAny<int>(), zz) exactly once, but it never did.",
"Expected that mock invoked method Q4(It.IsAny<int>(), , 9, It.IsAny<string>()) exactly once, but it never did.",
"Expected that mock invoked method Q3(null, It.IsAny<int>(), zz) exactly once, but it never did.",
"Expected that mock invoked method Q4(It.IsAny<int>(), null, 9, It.IsAny<string>()) exactly once, but it never did.",
"no exception",
"Expected that mock invoked method Money(4,75, It.IsAny<double>(), n) exactly once, but it never did.",
"Expected that mock invoked method Money(It.IsAny<decimal>(), 0,5, It.IsAny<string>()) exactly once, but it never did.",
Expand Down
Loading
Loading