diff --git a/AGENTS.md b/AGENTS.md index 48229b09c..0cdd7baba 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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)`. diff --git a/Docs/agents/workflows.md b/Docs/agents/workflows.md index 24854fa8f..c4ae8f397 100644 --- a/Docs/agents/workflows.md +++ b/Docs/agents/workflows.md @@ -32,12 +32,14 @@ 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 @@ -45,7 +47,7 @@ A newer language feature can compile for `net10.0` and fail for `net48`; build o | 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` | diff --git a/Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs b/Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs index d44602e36..6693d9885 100644 --- a/Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs +++ b/Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs @@ -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(); @@ -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) { @@ -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\"}}"; + /// /// 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 IParameter<T>, so they keep diff --git a/Source/Mockolate/Internals/LiteralParameterMatch.cs b/Source/Mockolate/Internals/LiteralParameterMatch.cs index ca97b0eba..112f87897 100644 --- a/Source/Mockolate/Internals/LiteralParameterMatch.cs +++ b/Source/Mockolate/Internals/LiteralParameterMatch.cs @@ -11,7 +11,8 @@ namespace Mockolate.Internals; /// /// Public only because generated code in the user's assembly calls it; not intended for direct use from user /// code. It compares with (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 as null, so failure messages read like the +/// literal value itself. /// /// The declared type of the parameter. #if !DEBUG @@ -50,5 +51,5 @@ public void InvokeCallbacks(T value) /// public override string ToString() - => $"{_value}"; + => _value is null ? "null" : $"{_value}"; } diff --git a/Tests/Directory.Build.props b/Tests/Directory.Build.props index 6dd97102f..8a78c9344 100644 --- a/Tests/Directory.Build.props +++ b/Tests/Directory.Build.props @@ -22,6 +22,11 @@ false + + + true + +