Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Respect base properties #1572

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
linkdotnet marked this conversation as resolved.
Show resolved Hide resolved
{
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();
}
}