GeezerVison is showing github.com — fix(mocks): guard closed setup analysis against emitter drift (#7302) · thomhurst/TUnit@360a7ca
File treeExpand file treeCollapse file tree Open diff view settings src/TUnit.Mocks.SourceGenerator/Builders tests/TUnit.Mocks.SourceGenerator.Tests
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/TUnit.Mocks.SourceGenerator/Builders/MockMembersBuilder.cs +11-11Lines changed: 11 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -878,21 +878,21 @@ private static void GenerateGenericMethodExtensionBlock(CodeWriter writer, MockM | 878 | 878 | } | 879 | 879 |
| 880 | 880 | // GetClosedReceiverGenericMethods(model, typeArguments) parses this output to detect | 881 | | - // closed-signature collisions. Keep its two framework configurations in sync with | 882 | | - // any conditional overloads emitted here. | 883 | | - private static void GenerateGenericMethodMembersInCurrentBlock(CodeWriter writer, MockMemberModel method, MockTypeModel model, string safeName) | | 881 | + // closed-signature collisions. Analysis emits all framework overloads together; the | | 882 | + // emission coverage regression verifies that no conditional signature is hidden. | | 883 | + internal static void GenerateGenericMethodMembersInCurrentBlock(CodeWriter writer, MockMemberModel method, MockTypeModel model, string safeName, bool includeAllFrameworkOverloads = false) | 884 | 884 | { | 885 | 885 | if (method.HasRefStructParams) | 886 | 886 | { | 887 | | - writer.AppendLine("#if NET9_0_OR_GREATER"); | | 887 | + if (!includeAllFrameworkOverloads) writer.AppendLine("#if NET9_0_OR_GREATER"); | 888 | 888 | EmitMemberMethodBody(writer, method, model, safeName, includeRefStructArgs: true, captureModelTypeParameters: true, receiverIsThis: false); | 889 | 889 | EmitFuncOverloads(writer, method, model, safeName, includeRefStructArgs: true, captureModelTypeParameters: true, receiverIsThis: false); | 890 | 890 | EmitParamsExpandedOverload(writer, method, model, safeName, includeRefStructArgs: true, captureModelTypeParameters: true, receiverIsThis: false); | 891 | | - writer.AppendLine("#else"); | | 891 | + if (!includeAllFrameworkOverloads) writer.AppendLine("#else"); | 892 | 892 | EmitMemberMethodBody(writer, method, model, safeName, includeRefStructArgs: false, captureModelTypeParameters: true, receiverIsThis: false); | 893 | 893 | EmitFuncOverloads(writer, method, model, safeName, includeRefStructArgs: false, captureModelTypeParameters: true, receiverIsThis: false); | 894 | 894 | EmitParamsExpandedOverload(writer, method, model, safeName, includeRefStructArgs: false, captureModelTypeParameters: true, receiverIsThis: false); | 895 | | - writer.AppendLine("#endif"); | | 895 | + if (!includeAllFrameworkOverloads) writer.AppendLine("#endif"); | 896 | 896 | } | 897 | 897 | else | 898 | 898 | { | @@ -957,12 +957,12 @@ internal static List<MockMemberModel> GetClosedReceiverGenericMethods(MockTypeMo | 957 | 957 | foreach (var method in group) | 958 | 958 | { | 959 | 959 | var writer = new CodeWriter(); | 960 | | - GenerateGenericMethodMembersInCurrentBlock(writer, method, model, MockImplBuilder.GetCompositeSafeName(model)); | | 960 | + GenerateGenericMethodMembersInCurrentBlock(writer, method, model, MockImplBuilder.GetCompositeSafeName(model), includeAllFrameworkOverloads: true); | 961 | 961 | var source = "class Signatures { " + writer + " }"; | 962 | | - // Ref-struct setup overloads differ across target frameworks. Check both branches. | 963 | | - var declarations = new[] { new CSharpParseOptions(), new CSharpParseOptions(preprocessorSymbols: new[] { "NET9_0_OR_GREATER" }) } | 964 | | - .SelectMany(options => CSharpSyntaxTree.ParseText(source, options).GetRoot() | 965 | | - .DescendantNodes().OfType<MethodDeclarationSyntax>()); | | 962 | + // The emitter includes every framework branch without directives, so one parse | | 963 | + // sees all signatures without a separate list of preprocessor configurations. | | 964 | + var declarations = CSharpSyntaxTree.ParseText(source).GetRoot() | | 965 | + .DescendantNodes().OfType<MethodDeclarationSyntax>(); | 966 | 966 | var rewriter = new ClosedSignatureRewriter(model, typeArguments, method); | 967 | 967 | foreach (var declaration in declarations) | 968 | 968 | { | |
Collapse file Copy file name to clipboardExpand all lines: tests/TUnit.Mocks.SourceGenerator.Tests/Issue7302Tests.cs +81Lines changed: 81 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +using System.Collections.Immutable; | | 2 | +using Microsoft.CodeAnalysis; | | 3 | +using Microsoft.CodeAnalysis.CSharp; | | 4 | +using Microsoft.CodeAnalysis.CSharp.Syntax; | | 5 | +using TUnit.Mocks.SourceGenerator; | | 6 | +using TUnit.Mocks.SourceGenerator.Builders; | | 7 | +using TUnit.Mocks.SourceGenerator.Models; | | 8 | + | 1 | 9 | namespace TUnit.Mocks.SourceGenerator.Tests; | 2 | 10 |
| 3 | 11 | // Regression: https://github.com/thomhurst/TUnit/issues/7302 | @@ -41,6 +49,79 @@ public struct Point | 41 | 49 |
| 42 | 50 | private const string SetupsFileMarker = "_ClosedMockMemberExtensions_"; | 43 | 51 |
| | 52 | + // Compare the analysis surface to the actual emitted signatures, rather than maintaining | | 53 | + // a second list of matcher/predicate/params/any-args overloads. A new conditional shape | | 54 | + // must be included in analysis too; a new condition requires extending this guard. | | 55 | + [Test] | | 56 | + [Arguments(0, false)] | | 57 | + [Arguments(1, false)] | | 58 | + [Arguments(4, false)] | | 59 | + [Arguments(5, false)] | | 60 | + [Arguments(9, false)] | | 61 | + [Arguments(1, true)] | | 62 | + [Arguments(4, true)] | | 63 | + [Arguments(5, true)] | | 64 | + public async Task Collision_Analysis_Sees_Every_Emitted_Overload(int parameterCount, bool hasRefStruct) | | 65 | + { | | 66 | + var model = new MockTypeModel | | 67 | + { | | 68 | + Name = "ISurface", | | 69 | + FullyQualifiedName = "global::TestNamespace.ISurface<T>", | | 70 | + Namespace = "TestNamespace", | | 71 | + IsInterface = true, | | 72 | + TypeParameters = new(ImmutableArray.Create(new MockTypeParameterModel { Name = "T" })) | | 73 | + }; | | 74 | + var parameters = Enumerable.Range(0, parameterCount).Select(index => new MockParameterModel | | 75 | + { | | 76 | + Name = "value" + index, | | 77 | + Type = index == 0 && hasRefStruct ? "Span<T>" : "T[]", | | 78 | + FullyQualifiedType = index == 0 && hasRefStruct ? "global::System.Span<T>" : "T[]", | | 79 | + IsRefStruct = index == 0 && hasRefStruct, | | 80 | + SpanElementType = index == 0 && hasRefStruct ? "T" : null, | | 81 | + ParamsElementType = index == parameterCount - 1 && !(index == 0 && hasRefStruct) ? "T" : null | | 82 | + }).ToImmutableArray(); | | 83 | + var method = new MockMemberModel | | 84 | + { | | 85 | + Name = "Map", | | 86 | + ReturnType = "U", | | 87 | + UnwrappedReturnType = "U", | | 88 | + IsGenericMethod = true, | | 89 | + TypeParameters = new(ImmutableArray.Create(new MockTypeParameterModel { Name = "U" })), | | 90 | + Parameters = new(parameters) | | 91 | + }; | | 92 | + | | 93 | + var emitted = new CodeWriter(); | | 94 | + MockMembersBuilder.GenerateClosedReceiverGenericMethodMembers(emitted, method, model, "ISurface"); | | 95 | + var analysis = new CodeWriter(); | | 96 | + MockMembersBuilder.GenerateGenericMethodMembersInCurrentBlock(analysis, method, model, "ISurface", includeAllFrameworkOverloads: true); | | 97 | + var analysisRoot = CSharpSyntaxTree.ParseText("class Surface { " + analysis + " }").GetRoot(); | | 98 | + await Assert.That(analysisRoot.DescendantTrivia().Any(trivia => trivia.IsDirective)).IsFalse(); | | 99 | + await Assert.That(analysisRoot.GetDiagnostics().Any(d => d.Severity == DiagnosticSeverity.Error)).IsFalse(); | | 100 | + | | 101 | + var emittedSource = "class Surface { " + emitted + " }"; | | 102 | + var roots = new[] | | 103 | + { | | 104 | + CSharpSyntaxTree.ParseText(emittedSource).GetRoot(), | | 105 | + CSharpSyntaxTree.ParseText(emittedSource, new CSharpParseOptions(preprocessorSymbols: ["NET9_0_OR_GREATER"])).GetRoot() | | 106 | + }; | | 107 | + var conditions = roots.SelectMany(root => root.DescendantTrivia(descendIntoTrivia: true)) | | 108 | + .Select(trivia => trivia.GetStructure()) | | 109 | + .Select(directive => directive switch | | 110 | + { | | 111 | + IfDirectiveTriviaSyntax condition => condition.Condition.ToString(), | | 112 | + ElifDirectiveTriviaSyntax condition => condition.Condition.ToString(), | | 113 | + _ => null | | 114 | + }).Where(condition => condition is not null).Distinct(); | | 115 | + await Assert.That(conditions.All(condition => condition == "NET9_0_OR_GREATER")).IsTrue(); | | 116 | + | | 117 | + static IEnumerable<string> Signatures(SyntaxNode root) => root.DescendantNodes() | | 118 | + .OfType<MethodDeclarationSyntax>() | | 119 | + .Select(declaration => declaration.Identifier.ValueText + declaration.TypeParameterList + declaration.ParameterList.NormalizeWhitespace().ToFullString()); | | 120 | + var expected = string.Join("\n", roots.SelectMany(Signatures).Distinct().OrderBy(signature => signature, StringComparer.Ordinal)); | | 121 | + var actual = string.Join("\n", Signatures(analysisRoot).Distinct().OrderBy(signature => signature, StringComparer.Ordinal)); | | 122 | + await Assert.That(actual).IsEqualTo(expected); | | 123 | + } | | 124 | + | 44 | 125 | [Test] | 45 | 126 | public async Task Nullable_Containers_Are_Kept_And_Nullable_Model_Elements_Are_Skipped() | 46 | 127 | { | |
|