Skip to content

Commit 7ee2404

Browse files
linkdotnetegil
authored andcommitted
refactor: Code organization
1 parent bc2e420 commit 7ee2404

1 file changed

Lines changed: 47 additions & 41 deletions

File tree

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

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.Collections.Immutable;
23
using System.Linq;
34
using System.Text;
@@ -91,47 +92,7 @@ private static void Execute(ImmutableArray<StubClassInfo> classInfos, SourceProd
9192
return;
9293
}
9394

94-
// Generate the attribute
95-
const string attribute = @"namespace System.Runtime.CompilerServices
96-
{
97-
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
98-
sealed file class InterceptsLocationAttribute : Attribute
99-
{
100-
public InterceptsLocationAttribute(string filePath, int line, int column)
101-
{
102-
_ = filePath;
103-
_ = line;
104-
_ = column;
105-
}
106-
}
107-
}";
108-
109-
// Generate the interceptor
110-
var interceptorSource = new StringBuilder();
111-
interceptorSource.AppendLine(attribute);
112-
interceptorSource.AppendLine();
113-
interceptorSource.AppendLine("namespace Bunit");
114-
interceptorSource.AppendLine("{");
115-
interceptorSource.AppendLine($"\tstatic class Interceptor{stubbedComponentGroup.StubClassName}");
116-
interceptorSource.AppendLine("\t{");
117-
118-
foreach (var hit in stubClassGrouped)
119-
{
120-
interceptorSource.AppendLine(
121-
$"\t\t[System.Runtime.CompilerServices.InterceptsLocationAttribute(\"{hit.Path}\", {hit.Line}, {hit.Column})]");
122-
}
123-
124-
interceptorSource.AppendLine(
125-
"\t\tpublic static global::Bunit.ComponentFactoryCollection AddGeneratedStubInterceptor<TComponent>(this global::Bunit.ComponentFactoryCollection factories)");
126-
interceptorSource.AppendLine("\t\t\twhere TComponent : Microsoft.AspNetCore.Components.IComponent");
127-
interceptorSource.AppendLine("\t\t{");
128-
interceptorSource.AppendLine(
129-
$"\t\t\treturn factories.Add<global::{stubbedComponentGroup.TargetType.ToDisplayString()}, {stubbedComponentGroup.TargetTypeNamespace}.{stubbedComponentGroup.StubClassName}>();");
130-
interceptorSource.AppendLine("\t\t}");
131-
interceptorSource.AppendLine("\t}");
132-
interceptorSource.AppendLine("}");
133-
134-
context.AddSource($"Interceptor{stubbedComponentGroup.StubClassName}.g.cs", interceptorSource.ToString());
95+
GenerateInterceptorCode(stubbedComponentGroup, stubClassGrouped, context);
13596
}
13697
}
13798

@@ -180,6 +141,51 @@ private static bool GenerateStubComponent(StubClassInfo classInfo, SourceProduct
180141

181142
return hasSomethingToStub;
182143
}
144+
145+
private static void GenerateInterceptorCode(StubClassInfo stubbedComponentGroup, IEnumerable<StubClassInfo> stubClassGrouped, SourceProductionContext context)
146+
{
147+
// Generate the attribute
148+
const string attribute = @"namespace System.Runtime.CompilerServices
149+
{
150+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
151+
sealed file class InterceptsLocationAttribute : Attribute
152+
{
153+
public InterceptsLocationAttribute(string filePath, int line, int column)
154+
{
155+
_ = filePath;
156+
_ = line;
157+
_ = column;
158+
}
159+
}
160+
}";
161+
162+
// Generate the interceptor
163+
var interceptorSource = new StringBuilder();
164+
interceptorSource.AppendLine(attribute);
165+
interceptorSource.AppendLine();
166+
interceptorSource.AppendLine("namespace Bunit");
167+
interceptorSource.AppendLine("{");
168+
interceptorSource.AppendLine($"\tstatic class Interceptor{stubbedComponentGroup.StubClassName}");
169+
interceptorSource.AppendLine("\t{");
170+
171+
foreach (var hit in stubClassGrouped)
172+
{
173+
interceptorSource.AppendLine(
174+
$"\t\t[System.Runtime.CompilerServices.InterceptsLocationAttribute(\"{hit.Path}\", {hit.Line}, {hit.Column})]");
175+
}
176+
177+
interceptorSource.AppendLine(
178+
"\t\tpublic static global::Bunit.ComponentFactoryCollection AddGeneratedStubInterceptor<TComponent>(this global::Bunit.ComponentFactoryCollection factories)");
179+
interceptorSource.AppendLine("\t\t\twhere TComponent : Microsoft.AspNetCore.Components.IComponent");
180+
interceptorSource.AppendLine("\t\t{");
181+
interceptorSource.AppendLine(
182+
$"\t\t\treturn factories.Add<global::{stubbedComponentGroup.TargetType.ToDisplayString()}, {stubbedComponentGroup.TargetTypeNamespace}.{stubbedComponentGroup.StubClassName}>();");
183+
interceptorSource.AppendLine("\t\t}");
184+
interceptorSource.AppendLine("\t}");
185+
interceptorSource.AppendLine("}");
186+
187+
context.AddSource($"Interceptor{stubbedComponentGroup.StubClassName}.g.cs", interceptorSource.ToString());
188+
}
183189
}
184190

185191
internal sealed class StubClassInfo

0 commit comments

Comments
 (0)