Skip to content

Commit

Permalink
feat: Respect base properties
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Oct 7, 2024
1 parent b9f8911 commit 4a9f522
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

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

var properties = symbol.GetMembers()
var properties = symbol.GetAllMembersRecursively()
.OfType<IPropertySymbol>()
.Where(IsParameterOrCascadingParameter)
.Select(CreateFromProperty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static bool IsClassWithComponentStubAttribute(SyntaxNode s) =>
}

var parameter = attribute.AttributeClass!.TypeArguments
.SelectMany(s => s.GetMembers())
.SelectMany(s => s.GetAllMembersRecursively())
.OfType<IPropertySymbol>()
.Where(IsParameterOrCascadingParameter)
.Select(CreateFromProperty)
Expand Down
19 changes: 19 additions & 0 deletions src/bunit.generators/Web.Stubs/MemberRetriever.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.CodeAnalysis;

namespace Bunit.Web.Stubs;

internal static class MemberRetriever
{
public static IEnumerable<ISymbol> GetAllMembersRecursively(this ITypeSymbol type)
{
var currentType = type;
while (currentType != null)
{
foreach (var member in currentType.GetMembers())
{
yield return member;
}
currentType = currentType.BaseType;
}
}
}
37 changes: 36 additions & 1 deletion tests/bunit.generators.tests/Web.Stub/AddStubGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,42 @@ public void Generated_stub_via_attribute_has_same_parameters()
var stub = cut.FindComponent<ButtonComponentStub>();
Assert.Equal("test", stub.Instance.Text);
}

[Fact]
public void Generated_Stub_respects_base_class_parameters()
{
ComponentFactories.AddStub<DerivedComponent>();

var cut = RenderComponent<ContainerComponent>(c => c.AddChildContent<DerivedComponent>());

var stub = cut.FindComponent<DerivedComponentStub>();
Assert.Equal(0, stub.Instance.BaseCount);
}

[Fact]
public void Generated_stub_via_attribute_respects_base_class_parameters()
{
ComponentFactories.Add<DerivedComponent, DerivedComponentStubViaAttributeAnnotation>();

var cut = RenderComponent<ContainerComponent>(c => c.AddChildContent<DerivedComponent>());

var stub = cut.FindComponent<DerivedComponentStubViaAttributeAnnotation>();
Assert.Equal(0, stub.Instance.BaseCount);
}
}

[ComponentStub<ButtonComponent>]
public partial class ButtonComponentStub;
public partial class ButtonComponentStub;

public abstract class BaseComponent : ComponentBase
{
[Parameter] public int BaseCount { get; set; }
}

public class DerivedComponent : BaseComponent
{
[Parameter] public int DerivedCount { get; set; }
}

[ComponentStub<DerivedComponent>]
public partial class DerivedComponentStubViaAttributeAnnotation;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Bunit.Web.Stub.Components;

public class ContainerComponent : ComponentBase
{
[Parameter]
public RenderFragment ChildContent { get; set; }

protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, "div");
builder.AddContent(1, ChildContent);
builder.CloseElement();
}
}

0 comments on commit 4a9f522

Please sign in to comment.