GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(mocks): guard closed setup analysis against emitter drift (#7302) · thomhurst/TUnit@360a7ca

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/TUnit.Mocks.SourceGenerator/Builders
    • MockMembersBuilder.cs
  • tests/TUnit.Mocks.SourceGenerator.Tests
    • Issue7302Tests.cs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/TUnit.Mocks.SourceGenerator/Builders/MockMembersBuilder.cs‎

Copy file name to clipboardExpand all lines: src/TUnit.Mocks.SourceGenerator/Builders/MockMembersBuilder.cs
+11-11Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -878,21 +878,21 @@ private static void GenerateGenericMethodExtensionBlock(CodeWriter writer, MockM
878878
}
879879

880880
// 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)
884884
{
885885
if (method.HasRefStructParams)
886886
{
887-
writer.AppendLine("#if NET9_0_OR_GREATER");
887+
if (!includeAllFrameworkOverloads) writer.AppendLine("#if NET9_0_OR_GREATER");
888888
EmitMemberMethodBody(writer, method, model, safeName, includeRefStructArgs: true, captureModelTypeParameters: true, receiverIsThis: false);
889889
EmitFuncOverloads(writer, method, model, safeName, includeRefStructArgs: true, captureModelTypeParameters: true, receiverIsThis: false);
890890
EmitParamsExpandedOverload(writer, method, model, safeName, includeRefStructArgs: true, captureModelTypeParameters: true, receiverIsThis: false);
891-
writer.AppendLine("#else");
891+
if (!includeAllFrameworkOverloads) writer.AppendLine("#else");
892892
EmitMemberMethodBody(writer, method, model, safeName, includeRefStructArgs: false, captureModelTypeParameters: true, receiverIsThis: false);
893893
EmitFuncOverloads(writer, method, model, safeName, includeRefStructArgs: false, captureModelTypeParameters: true, receiverIsThis: false);
894894
EmitParamsExpandedOverload(writer, method, model, safeName, includeRefStructArgs: false, captureModelTypeParameters: true, receiverIsThis: false);
895-
writer.AppendLine("#endif");
895+
if (!includeAllFrameworkOverloads) writer.AppendLine("#endif");
896896
}
897897
else
898898
{
@@ -957,12 +957,12 @@ internal static List<MockMemberModel> GetClosedReceiverGenericMethods(MockTypeMo
957957
foreach (var method in group)
958958
{
959959
var writer = new CodeWriter();
960-
GenerateGenericMethodMembersInCurrentBlock(writer, method, model, MockImplBuilder.GetCompositeSafeName(model));
960+
GenerateGenericMethodMembersInCurrentBlock(writer, method, model, MockImplBuilder.GetCompositeSafeName(model), includeAllFrameworkOverloads: true);
961961
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>();
966966
var rewriter = new ClosedSignatureRewriter(model, typeArguments, method);
967967
foreach (var declaration in declarations)
968968
{
Collapse file

‎tests/TUnit.Mocks.SourceGenerator.Tests/Issue7302Tests.cs‎

Copy file name to clipboardExpand all lines: tests/TUnit.Mocks.SourceGenerator.Tests/Issue7302Tests.cs
+81Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
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+
19
namespace TUnit.Mocks.SourceGenerator.Tests;
210

311
// Regression: https://github.com/thomhurst/TUnit/issues/7302
@@ -41,6 +49,79 @@ public struct Point
4149

4250
private const string SetupsFileMarker = "_ClosedMockMemberExtensions_";
4351

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+
44125
[Test]
45126
public async Task Nullable_Containers_Are_Kept_And_Nullable_Model_Elements_Are_Skipped()
46127
{

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.