Skip to content

Commit 2722aca

Browse files
committed
feat: Enable support for SupplyFrom cascading attributes
1 parent 77e8c54 commit 2722aca

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ static string GetInterceptorFilePath(SyntaxTree tree, Compilation compilation)
9999

100100
static bool IsParameterOrCascadingParameter(ISymbol member)
101101
{
102-
return member.GetAttributes().Any(attr =>
103-
attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute" ||
104-
attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.CascadingParameterAttribute");
102+
return member.GetAttributes().Any(SupportedAttributes.IsSupportedAttribute);
105103
}
106104

107105
static StubPropertyInfo CreateFromProperty(IPropertySymbol member)

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

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,14 @@ namespace Bunit.Web.Stubs;
66

77
internal static class AttributeLineGenerator
88
{
9-
private const string CascadingParameterAttributeQualifier = "Microsoft.AspNetCore.Components.CascadingParameterAttribute";
10-
private const string ParameterAttributeQualifier = "Microsoft.AspNetCore.Components.ParameterAttribute";
11-
129
public static string GetAttributeLine(ISymbol member)
1310
{
14-
var attribute = member.GetAttributes().First(attr =>
15-
attr.AttributeClass?.ToDisplayString() == ParameterAttributeQualifier ||
16-
attr.AttributeClass?.ToDisplayString() == CascadingParameterAttributeQualifier);
11+
var attribute = member.GetAttributes().First(SupportedAttributes.IsSupportedAttribute);
1712

1813
var attributeLine = new StringBuilder("\t[");
19-
if (attribute.AttributeClass?.ToDisplayString() == ParameterAttributeQualifier)
14+
if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.ParameterAttributeQualifier)
2015
{
21-
attributeLine.Append($"global::{ParameterAttributeQualifier}");
16+
attributeLine.Append($"global::{SupportedAttributes.ParameterAttributeQualifier}");
2217
var captureUnmatchedValuesArg = attribute.NamedArguments
2318
.FirstOrDefault(arg => arg.Key == "CaptureUnmatchedValues").Value;
2419
if (captureUnmatchedValuesArg.Value is bool captureUnmatchedValues)
@@ -27,9 +22,41 @@ public static string GetAttributeLine(ISymbol member)
2722
attributeLine.Append($"(CaptureUnmatchedValues = {captureString})");
2823
}
2924
}
30-
else if (attribute.AttributeClass?.ToDisplayString() == CascadingParameterAttributeQualifier)
25+
else if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.CascadingParameterAttributeQualifier)
26+
{
27+
attributeLine.Append($"global::{SupportedAttributes.CascadingParameterAttributeQualifier}");
28+
var nameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "Name").Value;
29+
if (!nameArg.IsNull)
30+
{
31+
attributeLine.Append($"(Name = \"{nameArg.Value}\")");
32+
}
33+
}
34+
else if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.SupplyParameterFromQueryAttributeQualifier)
35+
{
36+
attributeLine.Append($"global::{SupportedAttributes.SupplyParameterFromQueryAttributeQualifier}");
37+
var nameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "Name").Value;
38+
if (!nameArg.IsNull)
39+
{
40+
attributeLine.Append($"(Name = \"{nameArg.Value}\")");
41+
}
42+
}
43+
else if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.SupplyParameterFromFormAttributeQualifier)
44+
{
45+
attributeLine.Append($"global::{SupportedAttributes.SupplyParameterFromFormAttributeQualifier}");
46+
var nameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "Name").Value;
47+
if (!nameArg.IsNull)
48+
{
49+
attributeLine.Append($"(Name = \"{nameArg.Value}\")");
50+
}
51+
var formNameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "FormName").Value;
52+
if (!formNameArg.IsNull)
53+
{
54+
attributeLine.Append($", (FormName = \"{formNameArg.Value}\")");
55+
}
56+
}
57+
else if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.SupplyParameterFromQueryAttributeQualifier)
3158
{
32-
attributeLine.Append($"global::{CascadingParameterAttributeQualifier}");
59+
attributeLine.Append($"global::{SupportedAttributes.SupplyParameterFromQueryAttributeQualifier}");
3360
var nameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "Name").Value;
3461
if (!nameArg.IsNull)
3562
{

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ private static StubClassInfo GetStubClassInfo(GeneratorAttributeSyntaxContext co
8181

8282
static bool IsParameterOrCascadingParameter(ISymbol member)
8383
{
84-
return member.GetAttributes().Any(attr =>
85-
attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute" ||
86-
attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.CascadingParameterAttribute");
84+
return member.GetAttributes().Any(SupportedAttributes.IsSupportedAttribute);
8785
}
8886

8987
static StubPropertyInfo CreateFromProperty(IPropertySymbol member)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.CodeAnalysis;
2+
3+
namespace Bunit.Web.Stubs;
4+
5+
internal static class SupportedAttributes
6+
{
7+
public const string CascadingParameterAttributeQualifier = "Microsoft.AspNetCore.Components.CascadingParameterAttribute";
8+
public const string ParameterAttributeQualifier = "Microsoft.AspNetCore.Components.ParameterAttribute";
9+
public const string SupplyParameterFromQueryAttributeQualifier = "Microsoft.AspNetCore.Components.SupplyParameterFromQueryAttribute";
10+
public const string SupplyParameterFromFormAttributeQualifier = "Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute";
11+
12+
public static bool IsSupportedAttribute(AttributeData attribute)
13+
{
14+
var displayString = attribute.AttributeClass?.ToDisplayString();
15+
return displayString is ParameterAttributeQualifier or CascadingParameterAttributeQualifier or SupplyParameterFromQueryAttributeQualifier or SupplyParameterFromFormAttributeQualifier;
16+
}
17+
}

0 commit comments

Comments
 (0)