Skip to content

Commit efb09e6

Browse files
linkdotnetegil
authored andcommitted
Added ability to take over parameter values
1 parent 00c82fe commit efb09e6

4 files changed

Lines changed: 62 additions & 20 deletions

File tree

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

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private static bool GenerateStubComponent(StubClassInfo classInfo, SourceProduct
128128
var sourceBuilder = new StringBuilder();
129129

130130
sourceBuilder.AppendLine($"namespace {classInfo.TargetTypeNamespace};");
131-
sourceBuilder.AppendLine($"public partial class {classInfo.StubClassName} : Microsoft.AspNetCore.Components.ComponentBase");
131+
sourceBuilder.AppendLine($"internal partial class {classInfo.StubClassName} : Microsoft.AspNetCore.Components.ComponentBase");
132132
sourceBuilder.Append("{");
133133

134134
foreach (var member in targetTypeSymbol
@@ -147,13 +147,9 @@ private static bool GenerateStubComponent(StubClassInfo classInfo, SourceProduct
147147
var propertyType = member.Type.ToDisplayString();
148148
var propertyName = member.Name;
149149

150-
var isParameterAttribute = member.GetAttributes().Any(attr =>
151-
attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute");
152-
var attributeLine = isParameterAttribute
153-
? "\t[global::Microsoft.AspNetCore.Components.Parameter]"
154-
: "\t[global::Microsoft.AspNetCore.Components.CascadingParameter]";
155-
150+
var attributeLine = GetAttributeLine(member);
156151
sourceBuilder.AppendLine(attributeLine);
152+
157153
sourceBuilder.AppendLine($"\tpublic {propertyType} {propertyName} {{ get; set; }}");
158154
}
159155

@@ -165,6 +161,39 @@ private static bool GenerateStubComponent(StubClassInfo classInfo, SourceProduct
165161
}
166162

167163
return hasSomethingToStub;
164+
165+
string GetAttributeLine(IPropertySymbol member)
166+
{
167+
var attribute = member.GetAttributes().First(attr =>
168+
attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute" ||
169+
attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.CascadingParameterAttribute");
170+
171+
var attributeLine = new StringBuilder("\t[");
172+
if (attribute.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute")
173+
{
174+
attributeLine.Append("global::Microsoft.AspNetCore.Components.Parameter");
175+
var captureUnmatchedValuesArg = attribute.NamedArguments
176+
.FirstOrDefault(arg => arg.Key == "CaptureUnmatchedValues").Value;
177+
if (captureUnmatchedValuesArg.Value is bool captureUnmatchedValues)
178+
{
179+
var captureString = captureUnmatchedValues ? "true" : "false";
180+
attributeLine.Append($"(CaptureUnmatchedValues = {captureString})");
181+
}
182+
}
183+
else if (attribute.AttributeClass?.ToDisplayString() ==
184+
"Microsoft.AspNetCore.Components.CascadingParameterAttribute")
185+
{
186+
attributeLine.Append("global::Microsoft.AspNetCore.Components.CascadingParameter");
187+
var nameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "Name").Value;
188+
if (!nameArg.IsNull)
189+
{
190+
attributeLine.Append($"(Name = \"{nameArg.Value}\")");
191+
}
192+
}
193+
194+
attributeLine.Append("]");
195+
return attributeLine.ToString();
196+
}
168197
}
169198

170199
private static void GenerateInterceptorCode(StubClassInfo stubbedComponentGroup, IEnumerable<StubClassInfo> stubClassGrouped, SourceProductionContext context)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Microsoft.AspNetCore.Components;
2+
3+
namespace Bunit.Web.Stub.Components;
4+
5+
public class CounterComponent : ComponentBase
6+
{
7+
[Parameter] public int Count { get; set; }
8+
[CascadingParameter(Name = "Cascading")] public int CascadingCount { get; set; }
9+
[Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object> UnmatchedValues { get; set; }
10+
}

tests/bunit.generators.tests/Web.Stub/CounterComponent.cs renamed to tests/bunit.generators.tests/Web.Stub/Components/ParentComponent.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Microsoft.AspNetCore.Components;
22
using Microsoft.AspNetCore.Components.Rendering;
33

4-
namespace Bunit.Web.Stub;
4+
namespace Bunit.Web.Stub.Components;
55

66
public class ParentComponent : ComponentBase
77
{
@@ -12,10 +12,3 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
1212
builder.CloseComponent();
1313
}
1414
}
15-
16-
public class CounterComponent : ComponentBase
17-
{
18-
[Parameter] public int Count { get; set; }
19-
[CascadingParameter] public int CascadingCount { get; set; }
20-
[Parameter] public EventCallback IncrementCount { get; set; }
21-
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
using System.Reflection;
2+
using Bunit.Web.Stub.Components;
3+
using Microsoft.AspNetCore.Components;
4+
using Microsoft.Extensions.DependencyInjection;
5+
16
namespace Bunit.Web.Stub;
27

38
public class StubTests : TestContext
@@ -14,12 +19,17 @@ public void Stubbed_component_has_same_parameters()
1419
}
1520

1621
[Fact]
17-
public void Stubbed_component_can_be_used_in_multiple_tests()
22+
public void Stubbed_component_has_same_parameter_values()
1823
{
19-
ComponentFactories.AddGeneratedStub<CounterComponent>();
20-
21-
var cut = RenderComponent<ParentComponent>();
24+
var type = typeof(CounterComponentStub);
25+
var cascadingParameterValue = type.GetProperty(nameof(CounterComponentStub.CascadingCount))
26+
?.GetCustomAttribute<CascadingParameterAttribute>()
27+
?.Name;
28+
var captureUnmatchedValues = type.GetProperty(nameof(CounterComponentStub.UnmatchedValues))
29+
?.GetCustomAttribute<ParameterAttribute>()
30+
?.CaptureUnmatchedValues;
2231

23-
Assert.True(cut.HasComponent<CounterComponentStub>());
32+
Assert.Equal("Cascading", cascadingParameterValue);
33+
Assert.True(captureUnmatchedValues);
2434
}
2535
}

0 commit comments

Comments
 (0)