@@ -104,6 +104,34 @@ protected BaseClass(in int foo, ref int bar, out int baz, params int[] qux)
104104 ) ;
105105 }
106106
107+ [ Test ]
108+ public Task should_forward_constructor_from_embedded_library ( )
109+ {
110+ return Verify (
111+ """
112+ @inherits Foo.BaseClass
113+ """ ,
114+ embeddedLibrary : """
115+ using System;
116+ using RazorBlade.Support;
117+
118+ namespace Foo;
119+
120+ public abstract class BaseClass : RazorBlade.HtmlTemplate
121+ {
122+ protected BaseClass(int notIncluded)
123+ {
124+ }
125+
126+ [TemplateConstructor]
127+ protected BaseClass(int? foo, string? bar)
128+ {
129+ }
130+ }
131+ """
132+ ) ;
133+ }
134+
107135 [ Test ]
108136 public Task should_reject_model_directive ( )
109137 {
@@ -181,43 +209,60 @@ public void OnlyOnSync(double i) {}
181209 ) ;
182210 }
183211
184- private static GeneratorDriverRunResult Generate ( string input , string ? csharpCode = null )
212+ private static GeneratorDriverRunResult Generate ( string input , string ? csharpCode , string ? embeddedLibrary )
185213 {
186214 var runtimeDir = Path . GetDirectoryName ( typeof ( object ) . Assembly . Location ) ! ;
187215
188216 var compilation = CSharpCompilation . Create ( "TestAssembly" )
189- . AddReferences (
190- MetadataReference . CreateFromFile ( typeof ( object ) . Assembly . Location ) ,
191- MetadataReference . CreateFromFile ( Path . Combine ( runtimeDir , "netstandard.dll" ) ) ,
192- MetadataReference . CreateFromFile ( Path . Combine ( runtimeDir , "System.Runtime.dll" ) ) ,
193- MetadataReference . CreateFromFile ( typeof ( RazorTemplate ) . Assembly . Location )
217+ . AddReferences ( new [ ]
218+ {
219+ MetadataReference . CreateFromFile ( typeof ( object ) . Assembly . Location ) ,
220+ MetadataReference . CreateFromFile ( Path . Combine ( runtimeDir , "netstandard.dll" ) ) ,
221+ MetadataReference . CreateFromFile ( Path . Combine ( runtimeDir , "System.Runtime.dll" ) ) ,
222+ embeddedLibrary is null
223+ ? MetadataReference . CreateFromFile ( typeof ( RazorTemplate ) . Assembly . Location )
224+ : null
225+ } . Where ( i => i is not null ) !
226+ )
227+ . AddSyntaxTrees (
228+ CSharpSyntaxTree . ParseText ( csharpCode ?? string . Empty ) ,
229+ CSharpSyntaxTree . ParseText ( embeddedLibrary ?? string . Empty )
194230 )
195- . AddSyntaxTrees ( CSharpSyntaxTree . ParseText ( csharpCode ?? string . Empty ) )
196231 . WithOptions ( new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary ) . WithNullableContextOptions ( NullableContextOptions . Enable ) ) ;
197232
233+ var analyzerConfigOptionsProvider = new AnalyzerConfigOptionsProviderMock
234+ {
235+ { "IsRazorBlade" , "True" } ,
236+ { "Namespace" , "TestNamespace" }
237+ } ;
238+
239+ if ( embeddedLibrary is not null )
240+ analyzerConfigOptionsProvider . Add ( "RazorBladeEmbeddedLibrary" , "true" ) ;
241+
198242 var result = CSharpGeneratorDriver . Create ( new RazorBladeSourceGenerator ( ) )
199243 . AddAdditionalTexts ( ImmutableArray . Create < AdditionalText > ( new AdditionalTextMock ( input , "./TestFile.cshtml" ) ) )
200- . WithUpdatedAnalyzerConfigOptions ( new AnalyzerConfigOptionsProviderMock
201- {
202- { "IsRazorBlade" , "True" } ,
203- { "Namespace" , "TestNamespace" }
204- } )
244+ . WithUpdatedAnalyzerConfigOptions ( analyzerConfigOptionsProvider )
205245 . RunGeneratorsAndUpdateCompilation ( compilation , out var updatedCompilation , out _ )
206246 . GetRunResult ( ) ;
207247
208248 var diagnostics = updatedCompilation . GetDiagnostics ( ) ;
209249
210- if ( ! diagnostics . IsEmpty )
211- Console . WriteLine ( result . GeneratedTrees . FirstOrDefault ( ) ) ;
250+ if ( embeddedLibrary is null ) // Don't validate the embedded library generator here, assume the output will compile.
251+ {
252+ if ( ! diagnostics . IsEmpty )
253+ Console . WriteLine ( result . GeneratedTrees . FirstOrDefault ( ) ) ;
212254
213- diagnostics . ShouldBeEmpty ( ) ;
255+ diagnostics . ShouldBeEmpty ( ) ;
256+ }
214257
215258 return result ;
216259 }
217260
218- private static Task Verify ( [ StringSyntax ( "razor" ) ] string input , [ StringSyntax ( "csharp" ) ] string ? csharpCode = null )
261+ private static Task Verify ( [ StringSyntax ( "razor" ) ] string input ,
262+ [ StringSyntax ( "csharp" ) ] string ? csharpCode = null ,
263+ [ StringSyntax ( "csharp" ) ] string ? embeddedLibrary = null )
219264 {
220- var result = Generate ( input , csharpCode ) ;
265+ var result = Generate ( input , csharpCode , embeddedLibrary ) ;
221266 return Verifier . Verify ( result ) ;
222267 }
223268}
0 commit comments