Skip to content

Commit 30d2583

Browse files
linkdotnetegil
authored andcommitted
Use AddStub instead of AddGeneratedStub
1 parent d63fd8a commit 30d2583

3 files changed

Lines changed: 14 additions & 37 deletions

File tree

src/bunit.generators/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This package contains source generators for bUnit, to make it easier and more convenient to write tests.
44

5-
## `AddGeneratedStub` Generator
5+
## `AddStub` Generator
66
This generator adds the ability to automatically generate stubs for a given type. This comes in handy, when dealing
77
with 3rd party components that might need an extensive setup. Here a small example:
88

@@ -28,7 +28,7 @@ public void Text_button_gets_initial_count()
2828
{
2929
// This call will automatically generate a stub for the ThirdPartyButton component
3030
// with the name "ThirdPartyButtonStub"
31-
ComponentFactories.AddGeneratedStub<ThirdPartyText>();
31+
ComponentFactories.AddStub<ThirdPartyText>();
3232
var cut = Render<Counter>(@<Counter />);
3333

3434
cut.Find("button").Click();

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

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,6 @@ public class StubGenerator : IIncrementalGenerator
2020
/// <inheritdoc/>
2121
public void Initialize(IncrementalGeneratorInitializationContext context)
2222
{
23-
context.RegisterPostInitializationOutput(
24-
ctx => ctx.AddSource("AddGeneratedStub.g.cs",
25-
@"namespace Bunit
26-
{
27-
public static class ComponentFactoriesExtensions
28-
{
29-
/// <summary>
30-
/// Marks a component, so that a stub get is generated for it. The stub has the same name as the component, but with the suffix ""Stub"" added.
31-
/// </summary>
32-
/// <typeparam name=""TComponent"">The type of component to generate a stub for.</typeparam>
33-
/// <remarks>
34-
/// When <c>ComponentFactories.AddGeneratedStub&lt;MyButton&gt;()</c> is called, a stub component is generated for the component
35-
/// with the name <c>MyButtonStub</c>. The stub component is added to the <see cref=""ComponentFactoryCollection""/> and can be used.
36-
/// It can also be retrieved via <c>cut.FindComponent&lt;MyButtonStub&gt;()</c>.
37-
/// This call does the same as <c>ComponentFactories.Add&lt;MyButton, MyButtonStub&gt;()</c>.
38-
/// </remarks>
39-
public static ComponentFactoryCollection AddGeneratedStub<TComponent>(this ComponentFactoryCollection factories)
40-
where TComponent : Microsoft.AspNetCore.Components.IComponent
41-
{
42-
return factories.AddGeneratedStubInterceptor<TComponent>();
43-
}
44-
}
45-
}"));
4623
var classesToStub = context.SyntaxProvider
4724
.CreateSyntaxProvider(
4825
predicate: static (s, _) => s is InvocationExpressionSyntax,
@@ -71,7 +48,7 @@ private static StubClassInfo GetStubClassInfo(GeneratorSyntaxContext context)
7148
var path = GetInterceptorFilePath(context.Node.SyntaxTree, context.SemanticModel.Compilation);
7249
var lineSpan = context.SemanticModel.SyntaxTree.GetLineSpan(context.Node.Span);
7350
var line = lineSpan.StartLinePosition.Line + 1;
74-
var column = lineSpan.Span.Start.Character + context.Node.ToString().IndexOf("AddGeneratedStub", StringComparison.Ordinal);
51+
var column = lineSpan.Span.Start.Character + context.Node.ToString().IndexOf("AddStub", StringComparison.Ordinal) + 1;
7552

7653
return new StubClassInfo
7754
{
@@ -80,7 +57,7 @@ private static StubClassInfo GetStubClassInfo(GeneratorSyntaxContext context)
8057
TargetType = symbol,
8158
Path = path,
8259
Line = line,
83-
Column = column + 1,
60+
Column = column,
8461
};
8562
}
8663
}
@@ -94,7 +71,7 @@ static bool IsComponentFactoryStubMethod(InvocationExpressionSyntax invocation,
9471
return false;
9572
}
9673

97-
if (memberAccess.Name.Identifier.Text != "AddGeneratedStub" ||
74+
if (memberAccess.Name.Identifier.Text != "AddStub" ||
9875
invocation.ArgumentList.Arguments.Count != 0)
9976
{
10077
return false;
@@ -132,7 +109,7 @@ private static bool GenerateStubComponent(StubClassInfo classInfo, SourceProduct
132109

133110
sourceBuilder.AppendLine($"namespace {classInfo.TargetTypeNamespace};");
134111
sourceBuilder.AppendLine();
135-
sourceBuilder.AppendLine($"internal partial class {classInfo.StubClassName} : Microsoft.AspNetCore.Components.ComponentBase");
112+
sourceBuilder.AppendLine($"internal partial class {classInfo.StubClassName} : global::Microsoft.AspNetCore.Components.ComponentBase");
136113
sourceBuilder.Append("{");
137114

138115
foreach (var member in targetTypeSymbol
@@ -222,21 +199,21 @@ public InterceptsLocationAttribute(string filePath, int line, int column)
222199
interceptorSource.AppendLine();
223200
interceptorSource.AppendLine("namespace Bunit");
224201
interceptorSource.AppendLine("{");
225-
interceptorSource.AppendLine($"\tstatic class Interceptor{stubbedComponentGroup.StubClassName}");
202+
interceptorSource.AppendLine($"\tinternal static class Interceptor{stubbedComponentGroup.StubClassName}");
226203
interceptorSource.AppendLine("\t{");
227204

228205
foreach (var hit in stubClassGrouped)
229206
{
230207
interceptorSource.AppendLine(
231-
$"\t\t[System.Runtime.CompilerServices.InterceptsLocationAttribute(\"{hit.Path}\", {hit.Line}, {hit.Column})]");
208+
$"\t\t[global::System.Runtime.CompilerServices.InterceptsLocationAttribute(\"{hit.Path}\", {hit.Line}, {hit.Column})]");
232209
}
233210

234211
interceptorSource.AppendLine(
235-
"\t\tpublic static global::Bunit.ComponentFactoryCollection AddGeneratedStubInterceptor<TComponent>(this global::Bunit.ComponentFactoryCollection factories)");
236-
interceptorSource.AppendLine("\t\t\twhere TComponent : Microsoft.AspNetCore.Components.IComponent");
212+
"\t\tpublic static global::Bunit.ComponentFactoryCollection AddStubInterceptor<TComponent>(this global::Bunit.ComponentFactoryCollection factories)");
213+
interceptorSource.AppendLine("\t\t\twhere TComponent : global::Microsoft.AspNetCore.Components.IComponent");
237214
interceptorSource.AppendLine("\t\t{");
238215
interceptorSource.AppendLine(
239-
$"\t\t\treturn factories.Add<global::{stubbedComponentGroup.TargetType.ToDisplayString()}, {stubbedComponentGroup.TargetTypeNamespace}.{stubbedComponentGroup.StubClassName}>();");
216+
$"\t\t\treturn factories.Add<global::{stubbedComponentGroup.TargetType.ToDisplayString()}, global::{stubbedComponentGroup.TargetTypeNamespace}.{stubbedComponentGroup.StubClassName}>();");
240217
interceptorSource.AppendLine("\t\t}");
241218
interceptorSource.AppendLine("\t}");
242219
interceptorSource.AppendLine("}");

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class StubTests : TestContext
77
[Fact]
88
public void Stubbed_component_has_same_parameters()
99
{
10-
ComponentFactories.AddGeneratedStub<CounterComponent>();
10+
ComponentFactories.AddStub<CounterComponent>();
1111

1212
var cut = RenderComponent<ParentComponent>();
1313

@@ -18,7 +18,7 @@ public void Stubbed_component_has_same_parameters()
1818
[Fact]
1919
public void Generated_stub_can_handle_cascading_parameters()
2020
{
21-
ComponentFactories.AddGeneratedStub<CounterComponent>();
21+
ComponentFactories.AddStub<CounterComponent>();
2222

2323
var cut = RenderComponent<ParentComponent>(p => p.AddCascadingValue("Cascading", 3));
2424

@@ -29,7 +29,7 @@ public void Generated_stub_can_handle_cascading_parameters()
2929
[Fact]
3030
public void Generated_stub_can_handle_unmatched_parameters()
3131
{
32-
ComponentFactories.AddGeneratedStub<CounterComponent>();
32+
ComponentFactories.AddStub<CounterComponent>();
3333

3434
var cut = Render(builder =>
3535
{

0 commit comments

Comments
 (0)