Skip to content

Commit 215483e

Browse files
linkdotnetegil
authored andcommitted
Rename attribute and use generic version
1 parent df36f36 commit 215483e

File tree

6 files changed

+38
-44
lines changed

6 files changed

+38
-44
lines changed

docs/site/docs/extensions/bunit-generators.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ To use the generator, the **Interceptor** feature has to be used inside the cspr
6060

6161
Due to the usage of **Interceptors** the generator is only available for .NET 8.0 and above. The generator does create a `partial` class, so it can be extended with custom logic if needed.
6262

63-
## Component stub generator via `StubAttribute`
63+
## Component stub generator via `ComponentStubAttribute`
6464

6565
This generator adds the ability to automatically generate stubs for a given type via an attribute.
6666
The general setup for the given component above looks like this:
@@ -77,7 +77,7 @@ public class FeatureTests : TestContext
7777
}
7878
}
7979

80-
[Stub(typeof(ThirdPartyText))]
80+
[ComponentStub<ThirdPartyText>]
8181
internal partial class ThidPartyStub { }
8282
```
8383

src/bunit.generators/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ To use the generator, the **Interceptor** feature has to be used inside the cspr
5252

5353
This limits the usage to .NET 8 and above.
5454

55-
## `StubAttribute`
55+
## `ComponentStubAttribute`
5656
This generator adds the ability to automatically generate stubs for a given type via an attribute.
5757
The general setup for the given component above looks like this:
5858
```csharp
@@ -68,7 +68,7 @@ public class FeatureTests : TestContext
6868
}
6969
}
7070

71-
[Stub(typeof(ThirdPartyText))]
71+
[ComponentStub<ThirdPartyText>)]
7272
internal partial class ThidPartyStub { }
7373
```
7474

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Bunit.Web.Stubs.AttributeStubGenerator;
2+
3+
internal static class ComponentStubAttribute
4+
{
5+
public static string ComponentStubAttributeSource = """
6+
#if NET5_0_OR_GREATER
7+
namespace Bunit;
8+
9+
/// <summary>
10+
/// Indicates that the component will be enriched by a generated class.
11+
/// </summary>
12+
[global::System.AttributeUsage(global::System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
13+
internal sealed class ComponentStubAttribute<T> : global::System.Attribute
14+
where T : global::Microsoft.AspNetCore.Components.IComponent
15+
{
16+
}
17+
#endif
18+
""";
19+
}

src/bunit.generators/Web.Stubs/AttributeStubGenerator/StubAttributeGenerator.cs renamed to src/bunit.generators/Web.Stubs/AttributeStubGenerator/ComponentStubAttributeGenerator.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,36 @@ namespace Bunit.Web.Stubs.AttributeStubGenerator;
1010
/// Generator that creates a stub of a marked component.
1111
/// </summary>
1212
[Generator]
13-
public class StubAttributeGenerator : IIncrementalGenerator
13+
public class ComponentStubAttributeGenerator : IIncrementalGenerator
1414
{
15-
private const string AttributeFullQualifiedName = "Bunit.StubAttribute";
15+
private const string AttributeFullQualifiedName = "Bunit.ComponentStubAttribute`1";
1616

1717
/// <inheritdoc />
1818
public void Initialize(IncrementalGeneratorInitializationContext context)
1919
{
2020
context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
21-
"StubAttribute.g.cs",
22-
SourceText.From(StubAttribute.StubAttributeSource, Encoding.UTF8)));
21+
"ComponentStubAttribute.g.cs",
22+
SourceText.From(ComponentStubAttribute.ComponentStubAttributeSource, Encoding.UTF8)));
2323

2424
var classesToStub = context.SyntaxProvider
2525
.ForAttributeWithMetadataName(
2626
AttributeFullQualifiedName,
27-
predicate: static (s, _) => s is ClassDeclarationSyntax c && c.AttributeLists.SelectMany(a => a.Attributes).Any(at => at.Name.ToString() == "Stub"),
27+
predicate: static (s, _) => IsClassWithComponentStubAttribute(s),
2828
transform: static (ctx, _) => GetStubClassInfo(ctx))
2929
.Where(static m => m is not null);
3030

31-
3231
context.RegisterSourceOutput(
3332
classesToStub,
3433
static (spc, source) => Execute(source, spc));
3534
}
3635

36+
private static bool IsClassWithComponentStubAttribute(SyntaxNode s) =>
37+
s is ClassDeclarationSyntax c && c.AttributeLists.SelectMany(a => a.Attributes)
38+
.Any(at => at.Name.ToString().Contains("ComponentStub"));
39+
3740
private static StubClassInfo GetStubClassInfo(GeneratorAttributeSyntaxContext context)
3841
{
42+
#pragma warning disable RS1035
3943
foreach (var attribute in context.TargetSymbol.GetAttributes())
4044
{
4145
if (context.TargetSymbol is not ITypeSymbol stubbedType)
@@ -47,8 +51,8 @@ private static StubClassInfo GetStubClassInfo(GeneratorAttributeSyntaxContext co
4751
var className = context.TargetSymbol.Name;
4852
var visibility = context.TargetSymbol.DeclaredAccessibility.ToString().ToLower();
4953

50-
var originalTypeToStub = attribute.ConstructorArguments.FirstOrDefault().Value;
51-
if (originalTypeToStub is not ITypeSymbol originalType)
54+
var originalTypeToStub = attribute.AttributeClass?.TypeArguments.FirstOrDefault();
55+
if (originalTypeToStub is null)
5256
{
5357
continue;
5458
}
@@ -57,7 +61,7 @@ private static StubClassInfo GetStubClassInfo(GeneratorAttributeSyntaxContext co
5761
{
5862
ClassName = className,
5963
Namespace = namespaceName,
60-
TargetType = originalType,
64+
TargetType = originalTypeToStub,
6165
Visibility = visibility
6266
};
6367
}
@@ -107,7 +111,7 @@ private static void Execute(StubClassInfo classInfo, SourceProductionContext con
107111

108112
if (hasSomethingToStub)
109113
{
110-
context.AddSource($"{classInfo.ClassName}Stub.g.cs", sourceBuilder.ToString());
114+
context.AddSource($"{classInfo.ClassName}.g.cs", sourceBuilder.ToString());
111115
}
112116
}
113117
}

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

Lines changed: 0 additions & 29 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void Generated_stub_via_attribute_has_same_parameters()
6363
}
6464
}
6565

66-
[Stub(typeof(ButtonComponent))]
66+
[ComponentStub<ButtonComponent>]
6767
public partial class ButtonComponentStub
6868
{
6969
}

0 commit comments

Comments
 (0)