Skip to content

Commit bb982f1

Browse files
linkdotnetegil
authored andcommitted
Use render tests instead of reflection
1 parent 76b6225 commit bb982f1

2 files changed

Lines changed: 39 additions & 20 deletions

File tree

src/bunit.generators/Web.Stubs/StubGenerator.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ namespace Bunit.Web.Stubs;
1414
[Generator]
1515
public class StubGenerator : IIncrementalGenerator
1616
{
17+
private static string CascadingParameterAttributeQualifier;
18+
private const string ParameterAttributeQualifier = "Microsoft.AspNetCore.Components.ParameterAttribute";
19+
1720
/// <inheritdoc/>
1821
public void Initialize(IncrementalGeneratorInitializationContext context)
1922
{
@@ -131,15 +134,16 @@ private static bool GenerateStubComponent(StubClassInfo classInfo, SourceProduct
131134
sourceBuilder.AppendLine($"internal partial class {classInfo.StubClassName} : Microsoft.AspNetCore.Components.ComponentBase");
132135
sourceBuilder.Append("{");
133136

137+
CascadingParameterAttributeQualifier = "Microsoft.AspNetCore.Components.CascadingParameterAttribute";
134138
foreach (var member in targetTypeSymbol
135139
.GetMembers()
136140
.OfType<IPropertySymbol>()
137141
.Where(p => p.GetAttributes()
138142
.Any(attr =>
139143
attr.AttributeClass?.ToDisplayString() ==
140-
"Microsoft.AspNetCore.Components.ParameterAttribute" ||
144+
ParameterAttributeQualifier ||
141145
attr.AttributeClass?.ToDisplayString() ==
142-
"Microsoft.AspNetCore.Components.CascadingParameterAttribute")))
146+
CascadingParameterAttributeQualifier)))
143147
{
144148
sourceBuilder.AppendLine();
145149

@@ -165,13 +169,13 @@ private static bool GenerateStubComponent(StubClassInfo classInfo, SourceProduct
165169
string GetAttributeLine(ISymbol member)
166170
{
167171
var attribute = member.GetAttributes().First(attr =>
168-
attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute" ||
169-
attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.CascadingParameterAttribute");
172+
attr.AttributeClass?.ToDisplayString() == ParameterAttributeQualifier ||
173+
attr.AttributeClass?.ToDisplayString() == CascadingParameterAttributeQualifier);
170174

171175
var attributeLine = new StringBuilder("\t[");
172-
if (attribute.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute")
176+
if (attribute.AttributeClass?.ToDisplayString() == ParameterAttributeQualifier)
173177
{
174-
attributeLine.Append("global::Microsoft.AspNetCore.Components.Parameter");
178+
attributeLine.Append($"global::{ParameterAttributeQualifier}");
175179
var captureUnmatchedValuesArg = attribute.NamedArguments
176180
.FirstOrDefault(arg => arg.Key == "CaptureUnmatchedValues").Value;
177181
if (captureUnmatchedValuesArg.Value is bool captureUnmatchedValues)
@@ -180,10 +184,9 @@ string GetAttributeLine(ISymbol member)
180184
attributeLine.Append($"(CaptureUnmatchedValues = {captureString})");
181185
}
182186
}
183-
else if (attribute.AttributeClass?.ToDisplayString() ==
184-
"Microsoft.AspNetCore.Components.CascadingParameterAttribute")
187+
else if (attribute.AttributeClass?.ToDisplayString() == CascadingParameterAttributeQualifier)
185188
{
186-
attributeLine.Append("global::Microsoft.AspNetCore.Components.CascadingParameter");
189+
attributeLine.Append($"global::{CascadingParameterAttributeQualifier}");
187190
var nameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "Name").Value;
188191
if (!nameArg.IsNull)
189192
{

tests/bunit.generators.tests/Web.Stub/StubTests.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,33 @@ public void Stubbed_component_has_same_parameters()
1818
}
1919

2020
[Fact]
21-
public void Stubbed_component_has_same_parameter_values()
21+
public void Generated_stub_can_handle_cascading_parameters()
2222
{
23-
var type = typeof(CounterComponentStub);
24-
var cascadingParameterValue = type.GetProperty(nameof(CounterComponentStub.CascadingCount))
25-
?.GetCustomAttribute<CascadingParameterAttribute>()
26-
?.Name;
27-
var captureUnmatchedValues = type.GetProperty(nameof(CounterComponentStub.UnmatchedValues))
28-
?.GetCustomAttribute<ParameterAttribute>()
29-
?.CaptureUnmatchedValues;
30-
31-
Assert.Equal("Cascading", cascadingParameterValue);
32-
Assert.True(captureUnmatchedValues);
23+
ComponentFactories.AddGeneratedStub<CounterComponent>();
24+
25+
var cut = RenderComponent<ParentComponent>(p => p.AddCascadingValue("Cascading", 3));
26+
27+
var stub = cut.FindComponent<CounterComponentStub>();
28+
Assert.Equal(3, stub.Instance.CascadingCount);
29+
}
30+
31+
[Fact]
32+
public void Generated_stub_can_handle_unmatched_parameters()
33+
{
34+
ComponentFactories.AddGeneratedStub<CounterComponent>();
35+
36+
var cut = Render(builder =>
37+
{
38+
builder.OpenComponent<CounterComponent>(1);
39+
builder.AddAttribute(2, "Class", "test");
40+
builder.AddAttribute(3, "Data", 3);
41+
builder.CloseComponent();
42+
});
43+
44+
var stub = cut.FindComponent<CounterComponentStub>();
45+
var captured = stub.Instance.UnmatchedValues;
46+
Assert.Equal(2, captured.Count);
47+
Assert.Equal("test", captured.GetValueOrDefault("Class", null));
48+
Assert.Equal(3, captured.GetValueOrDefault("Data", 0));
3349
}
3450
}

0 commit comments

Comments
 (0)