|
| 1 | +using System.Collections.Generic; |
1 | 2 | using System.Collections.Immutable; |
2 | 3 | using System.Linq; |
3 | 4 | using System.Text; |
@@ -91,47 +92,7 @@ private static void Execute(ImmutableArray<StubClassInfo> classInfos, SourceProd |
91 | 92 | return; |
92 | 93 | } |
93 | 94 |
|
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); |
135 | 96 | } |
136 | 97 | } |
137 | 98 |
|
@@ -180,6 +141,51 @@ private static bool GenerateStubComponent(StubClassInfo classInfo, SourceProduct |
180 | 141 |
|
181 | 142 | return hasSomethingToStub; |
182 | 143 | } |
| 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 | + } |
183 | 189 | } |
184 | 190 |
|
185 | 191 | internal sealed class StubClassInfo |
|
0 commit comments