Skip to content

Commit 3e9592c

Browse files
linkdotnetegil
authored andcommitted
Moved extension method into generator
1 parent c69e458 commit 3e9592c

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@ public class StubGenerator : IIncrementalGenerator
1717
/// <inheritdoc/>
1818
public void Initialize(IncrementalGeneratorInitializationContext context)
1919
{
20+
context.RegisterPostInitializationOutput(
21+
ctx => ctx.AddSource("AddGeneratedStub.g.cs",
22+
@"namespace Bunit
23+
{
24+
public static class ComponentFactoriesExtensions
25+
{
26+
/// <summary>
27+
/// Marks a component as a stub component so that a stub gets generated for it. The stub has the same name as the component, but with the suffix ""Stub"" added.
28+
/// </summary>
29+
/// <typeparam name=""TComponent"">The type of component to generate a stub for.</typeparam>
30+
/// <remarks>
31+
/// When <c>ComponentFactories.AddGeneratedStub&lt;MyButton&gt;()</c> is called, a stub component is generated for the component
32+
/// with the name <c>MyButtonStub</c>. The stub component is added to the <see cref=""ComponentFactoryCollection""/> and can be used.
33+
/// It can also be retrieved via `cut.FindComponent&lt;MyButtonStub&gt;()`.
34+
/// This call does the same as <c>ComponentFactories.Add&lt;MyButton, MyButtonStub&gt;()</c>.
35+
/// </remarks>
36+
public static ComponentFactoryCollection AddGeneratedStub<TComponent>(this ComponentFactoryCollection factories)
37+
where TComponent : Microsoft.AspNetCore.Components.IComponent
38+
{
39+
return factories.AddGeneratedStubInterceptor<TComponent>();
40+
}
41+
}
42+
}"));
2043
var classesToStub = context.SyntaxProvider
2144
.CreateSyntaxProvider(
2245
predicate: static (s, _) => s is InvocationExpressionSyntax,

src/bunit.web/Extensions/StubComponentFactoryCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public static ComponentFactoryCollection AddStub(
174174
/// This call does the same as <c>ComponentFactories.Add&lt;MyButton, MyButtonStub&gt;()</c>.
175175
/// </remarks>
176176
#pragma warning disable S2326 // Unused type parameters should be removed - this is just the shape for the interceptor
177-
public static ComponentFactoryCollection AddGeneratedStub<TComponent>(this ComponentFactoryCollection factories)
177+
public static ComponentFactoryCollection AddGeneratedStub2<TComponent>(this ComponentFactoryCollection factories)
178178
where TComponent : IComponent
179179
#pragma warning restore S2326
180180
{

0 commit comments

Comments
 (0)