Skip to content

Commit 0f76e56

Browse files
linkdotnetegil
andauthored
feat: Respect base properties (#1572)
* feat: Respect base properties * Update src/bunit.generators/Web.Stubs/MemberRetriever.cs Co-authored-by: Egil Hansen <[email protected]> --------- Co-authored-by: Egil Hansen <[email protected]>
1 parent b9f8911 commit 0f76e56

File tree

5 files changed

+72
-5
lines changed

5 files changed

+72
-5
lines changed

src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/AddStubGenerator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
3131

3232
private static AddStubClassInfo? GetStubClassInfo(GeneratorSyntaxContext context)
3333
{
34-
var invocation = context.Node as InvocationExpressionSyntax;
35-
if (invocation is null || !IsComponentFactoryStubMethod(invocation, context.SemanticModel))
34+
if (context.Node is not InvocationExpressionSyntax invocation || !IsComponentFactoryStubMethod(invocation, context.SemanticModel))
3635
{
3736
return null;
3837
}
@@ -56,7 +55,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
5655
var line = lineSpan.StartLinePosition.Line + 1;
5756
var column = lineSpan.Span.Start.Character + context.Node.ToString().IndexOf("AddStub", StringComparison.Ordinal) + 1;
5857

59-
var properties = symbol.GetMembers()
58+
var properties = symbol.GetAllMembersRecursively()
6059
.OfType<IPropertySymbol>()
6160
.Where(IsParameterOrCascadingParameter)
6261
.Select(CreateFromProperty)

src/bunit.generators/Web.Stubs/AttributeStubGenerator/ComponentStubAttributeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private static bool IsClassWithComponentStubAttribute(SyntaxNode s) =>
6262
}
6363

6464
var parameter = attribute.AttributeClass!.TypeArguments
65-
.SelectMany(s => s.GetMembers())
65+
.SelectMany(s => s.GetAllMembersRecursively())
6666
.OfType<IPropertySymbol>()
6767
.Where(IsParameterOrCascadingParameter)
6868
.Select(CreateFromProperty)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.CodeAnalysis;
2+
3+
namespace Bunit.Web.Stubs;
4+
5+
internal static class MemberRetriever
6+
{
7+
public static IEnumerable<ISymbol> GetAllMembersRecursively(this ITypeSymbol type)
8+
{
9+
var currentType = type;
10+
while (currentType is not null)
11+
{
12+
foreach (var member in currentType.GetMembers())
13+
{
14+
yield return member;
15+
}
16+
currentType = currentType.BaseType;
17+
}
18+
}
19+
}

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,42 @@ public void Generated_stub_via_attribute_has_same_parameters()
6161
var stub = cut.FindComponent<ButtonComponentStub>();
6262
Assert.Equal("test", stub.Instance.Text);
6363
}
64+
65+
[Fact]
66+
public void Generated_Stub_respects_base_class_parameters()
67+
{
68+
ComponentFactories.AddStub<DerivedComponent>();
69+
70+
var cut = RenderComponent<ContainerComponent>(c => c.AddChildContent<DerivedComponent>());
71+
72+
var stub = cut.FindComponent<DerivedComponentStub>();
73+
Assert.Equal(0, stub.Instance.BaseCount);
74+
}
75+
76+
[Fact]
77+
public void Generated_stub_via_attribute_respects_base_class_parameters()
78+
{
79+
ComponentFactories.Add<DerivedComponent, DerivedComponentStubViaAttributeAnnotation>();
80+
81+
var cut = RenderComponent<ContainerComponent>(c => c.AddChildContent<DerivedComponent>());
82+
83+
var stub = cut.FindComponent<DerivedComponentStubViaAttributeAnnotation>();
84+
Assert.Equal(0, stub.Instance.BaseCount);
85+
}
6486
}
6587

6688
[ComponentStub<ButtonComponent>]
67-
public partial class ButtonComponentStub;
89+
public partial class ButtonComponentStub;
90+
91+
public abstract class BaseComponent : ComponentBase
92+
{
93+
[Parameter] public int BaseCount { get; set; }
94+
}
95+
96+
public class DerivedComponent : BaseComponent
97+
{
98+
[Parameter] public int DerivedCount { get; set; }
99+
}
100+
101+
[ComponentStub<DerivedComponent>]
102+
public partial class DerivedComponentStubViaAttributeAnnotation;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Bunit.Web.Stub.Components;
2+
3+
public class ContainerComponent : ComponentBase
4+
{
5+
[Parameter]
6+
public RenderFragment ChildContent { get; set; }
7+
8+
protected override void BuildRenderTree(RenderTreeBuilder builder)
9+
{
10+
builder.OpenElement(0, "div");
11+
builder.AddContent(1, ChildContent);
12+
builder.CloseElement();
13+
}
14+
}

0 commit comments

Comments
 (0)