diff --git a/src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/AddStubGenerator.cs b/src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/AddStubGenerator.cs index 40e0574d3..907dd6e78 100644 --- a/src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/AddStubGenerator.cs +++ b/src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/AddStubGenerator.cs @@ -99,9 +99,7 @@ static string GetInterceptorFilePath(SyntaxTree tree, Compilation compilation) static bool IsParameterOrCascadingParameter(ISymbol member) { - return member.GetAttributes().Any(attr => - attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute" || - attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.CascadingParameterAttribute"); + return member.GetAttributes().Any(SupportedAttributes.IsSupportedAttribute); } static StubPropertyInfo CreateFromProperty(IPropertySymbol member) diff --git a/src/bunit.generators/Web.Stubs/AttributeLineGenerator.cs b/src/bunit.generators/Web.Stubs/AttributeLineGenerator.cs index 8c69433e0..8185929a2 100644 --- a/src/bunit.generators/Web.Stubs/AttributeLineGenerator.cs +++ b/src/bunit.generators/Web.Stubs/AttributeLineGenerator.cs @@ -6,19 +6,14 @@ namespace Bunit.Web.Stubs; internal static class AttributeLineGenerator { - private const string CascadingParameterAttributeQualifier = "Microsoft.AspNetCore.Components.CascadingParameterAttribute"; - private const string ParameterAttributeQualifier = "Microsoft.AspNetCore.Components.ParameterAttribute"; - public static string GetAttributeLine(ISymbol member) { - var attribute = member.GetAttributes().First(attr => - attr.AttributeClass?.ToDisplayString() == ParameterAttributeQualifier || - attr.AttributeClass?.ToDisplayString() == CascadingParameterAttributeQualifier); + var attribute = member.GetAttributes().First(SupportedAttributes.IsSupportedAttribute); var attributeLine = new StringBuilder("\t["); - if (attribute.AttributeClass?.ToDisplayString() == ParameterAttributeQualifier) + if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.ParameterAttributeQualifier) { - attributeLine.Append($"global::{ParameterAttributeQualifier}"); + attributeLine.Append($"global::{SupportedAttributes.ParameterAttributeQualifier}"); var captureUnmatchedValuesArg = attribute.NamedArguments .FirstOrDefault(arg => arg.Key == "CaptureUnmatchedValues").Value; if (captureUnmatchedValuesArg.Value is bool captureUnmatchedValues) @@ -27,9 +22,41 @@ public static string GetAttributeLine(ISymbol member) attributeLine.Append($"(CaptureUnmatchedValues = {captureString})"); } } - else if (attribute.AttributeClass?.ToDisplayString() == CascadingParameterAttributeQualifier) + else if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.CascadingParameterAttributeQualifier) + { + attributeLine.Append($"global::{SupportedAttributes.CascadingParameterAttributeQualifier}"); + var nameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "Name").Value; + if (!nameArg.IsNull) + { + attributeLine.Append($"(Name = \"{nameArg.Value}\")"); + } + } + else if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.SupplyParameterFromQueryAttributeQualifier) + { + attributeLine.Append($"global::{SupportedAttributes.SupplyParameterFromQueryAttributeQualifier}"); + var nameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "Name").Value; + if (!nameArg.IsNull) + { + attributeLine.Append($"(Name = \"{nameArg.Value}\")"); + } + } + else if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.SupplyParameterFromFormAttributeQualifier) + { + attributeLine.Append($"global::{SupportedAttributes.SupplyParameterFromFormAttributeQualifier}"); + var nameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "Name").Value; + if (!nameArg.IsNull) + { + attributeLine.Append($"(Name = \"{nameArg.Value}\")"); + } + var formNameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "FormName").Value; + if (!formNameArg.IsNull) + { + attributeLine.Append($", (FormName = \"{formNameArg.Value}\")"); + } + } + else if (attribute.AttributeClass?.ToDisplayString() == SupportedAttributes.SupplyParameterFromQueryAttributeQualifier) { - attributeLine.Append($"global::{CascadingParameterAttributeQualifier}"); + attributeLine.Append($"global::{SupportedAttributes.SupplyParameterFromQueryAttributeQualifier}"); var nameArg = attribute.NamedArguments.FirstOrDefault(arg => arg.Key == "Name").Value; if (!nameArg.IsNull) { diff --git a/src/bunit.generators/Web.Stubs/AttributeStubGenerator/ComponentStubAttributeGenerator.cs b/src/bunit.generators/Web.Stubs/AttributeStubGenerator/ComponentStubAttributeGenerator.cs index 214c87f5c..df91cc095 100644 --- a/src/bunit.generators/Web.Stubs/AttributeStubGenerator/ComponentStubAttributeGenerator.cs +++ b/src/bunit.generators/Web.Stubs/AttributeStubGenerator/ComponentStubAttributeGenerator.cs @@ -81,9 +81,7 @@ private static StubClassInfo GetStubClassInfo(GeneratorAttributeSyntaxContext co static bool IsParameterOrCascadingParameter(ISymbol member) { - return member.GetAttributes().Any(attr => - attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute" || - attr.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.CascadingParameterAttribute"); + return member.GetAttributes().Any(SupportedAttributes.IsSupportedAttribute); } static StubPropertyInfo CreateFromProperty(IPropertySymbol member) diff --git a/src/bunit.generators/Web.Stubs/SupportedAttributes.cs b/src/bunit.generators/Web.Stubs/SupportedAttributes.cs new file mode 100644 index 000000000..b50e2f767 --- /dev/null +++ b/src/bunit.generators/Web.Stubs/SupportedAttributes.cs @@ -0,0 +1,17 @@ +using Microsoft.CodeAnalysis; + +namespace Bunit.Web.Stubs; + +internal static class SupportedAttributes +{ + public const string CascadingParameterAttributeQualifier = "Microsoft.AspNetCore.Components.CascadingParameterAttribute"; + public const string ParameterAttributeQualifier = "Microsoft.AspNetCore.Components.ParameterAttribute"; + public const string SupplyParameterFromQueryAttributeQualifier = "Microsoft.AspNetCore.Components.SupplyParameterFromQueryAttribute"; + public const string SupplyParameterFromFormAttributeQualifier = "Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute"; + + public static bool IsSupportedAttribute(AttributeData attribute) + { + var displayString = attribute.AttributeClass?.ToDisplayString(); + return displayString is ParameterAttributeQualifier or CascadingParameterAttributeQualifier or SupplyParameterFromQueryAttributeQualifier or SupplyParameterFromFormAttributeQualifier; + } +}