@@ -189,4 +189,198 @@ public void FullFluentChain_UseProvidedBody_ProducesCorrectData()
189189 object ? bodyValue = generator . Data . RuntimeDelegateBody ! . DynamicInvoke ( ) ;
190190 Assert . That ( bodyValue , Is . EqualTo ( 42 ) ) ;
191191 }
192+
193+ [ Test ]
194+ public void WithCompileTimeConstants_WithParam_ReturnsStage5WithConstants ( )
195+ {
196+ DataMethodBodyBuilderStage4 < int , string > stage4 = new DataMethodBodyBuilderStage4 < int , string > (
197+ new BodyGenerationData ( ReturnType : typeof ( string ) , ParametersTypes : [ typeof ( int ) ] ) ) ;
198+
199+ IMethodBodyBuilderStage5WithConstants < int , string , int > result = stage4 . WithCompileTimeConstants ( ( ) => 42 ) ;
200+
201+ Assert . That ( result , Is . TypeOf < DataMethodBodyBuilderStage5WithConstants < int , string , int > > ( ) ) ;
202+ DataMethodBodyBuilderStage5WithConstants < int , string , int > stage5 = ( DataMethodBodyBuilderStage5WithConstants < int , string , int > ) result ;
203+ Assert . That ( stage5 . Data . CompileTimeConstants , Is . EqualTo ( 42 ) ) ;
204+ }
205+
206+ [ Test ]
207+ public void WithCompileTimeConstants_NoArg_ReturnsStage5NoArgWithConstants ( )
208+ {
209+ DataMethodBodyBuilderStage4NoArg < string > stage4 = new DataMethodBodyBuilderStage4NoArg < string > (
210+ new BodyGenerationData ( ReturnType : typeof ( string ) , ParametersTypes : [ ] ) ) ;
211+
212+ IMethodBodyBuilderStage5NoArgWithConstants < string , int > result = stage4 . WithCompileTimeConstants ( ( ) => 99 ) ;
213+
214+ Assert . That ( result , Is . TypeOf < DataMethodBodyBuilderStage5NoArgWithConstants < string , int > > ( ) ) ;
215+ DataMethodBodyBuilderStage5NoArgWithConstants < string , int > stage5 = ( DataMethodBodyBuilderStage5NoArgWithConstants < string , int > ) result ;
216+ Assert . That ( stage5 . Data . CompileTimeConstants , Is . EqualTo ( 99 ) ) ;
217+ }
218+
219+ [ Test ]
220+ public void WithCompileTimeConstants_ReturnVoid_ReturnsStage5ReturnVoidWithConstants ( )
221+ {
222+ DataMethodBodyBuilderStage4ReturnVoid < int > stage4 = new DataMethodBodyBuilderStage4ReturnVoid < int > (
223+ new BodyGenerationData ( ReturnType : typeof ( void ) , ParametersTypes : [ typeof ( int ) ] ) ) ;
224+
225+ IMethodBodyBuilderStage5ReturnVoidWithConstants < int , string > result = stage4 . WithCompileTimeConstants ( ( ) => "test" ) ;
226+
227+ Assert . That ( result , Is . TypeOf < DataMethodBodyBuilderStage5ReturnVoidWithConstants < int , string > > ( ) ) ;
228+ DataMethodBodyBuilderStage5ReturnVoidWithConstants < int , string > stage5 = ( DataMethodBodyBuilderStage5ReturnVoidWithConstants < int , string > ) result ;
229+ Assert . That ( stage5 . Data . CompileTimeConstants , Is . EqualTo ( "test" ) ) ;
230+ }
231+
232+ [ Test ]
233+ public void WithCompileTimeConstants_ReturnVoidNoArg_ReturnsStage5ReturnVoidNoArgWithConstants ( )
234+ {
235+ DataMethodBodyBuilderStage4ReturnVoidNoArg stage4 = new DataMethodBodyBuilderStage4ReturnVoidNoArg (
236+ new BodyGenerationData ( ReturnType : typeof ( void ) , ParametersTypes : [ ] ) ) ;
237+
238+ IMethodBodyBuilderStage5ReturnVoidNoArgWithConstants < int > result = stage4 . WithCompileTimeConstants ( ( ) => 7 ) ;
239+
240+ Assert . That ( result , Is . TypeOf < DataMethodBodyBuilderStage5ReturnVoidNoArgWithConstants < int > > ( ) ) ;
241+ DataMethodBodyBuilderStage5ReturnVoidNoArgWithConstants < int > stage5 = ( DataMethodBodyBuilderStage5ReturnVoidNoArgWithConstants < int > ) result ;
242+ Assert . That ( stage5 . Data . CompileTimeConstants , Is . EqualTo ( 7 ) ) ;
243+ }
244+
245+ [ Test ]
246+ public void Stage5WithConstants_UseProvidedBody_SetsRuntimeDelegateBody ( )
247+ {
248+ DataMethodBodyBuilderStage5WithConstants < int , string , int > stage5 = new DataMethodBodyBuilderStage5WithConstants < int , string , int > (
249+ new BodyGenerationData ( ReturnType : typeof ( string ) , ParametersTypes : [ typeof ( int ) ] , CompileTimeConstants : 42 ) ) ;
250+
251+ IMethodBodyGenerator result = stage5 . UseProvidedBody ( ( constants , param ) => $ "{ constants } _{ param } ") ;
252+
253+ Assert . That ( result , Is . TypeOf < DataMethodBodyGenerator > ( ) ) ;
254+ DataMethodBodyGenerator generator = ( DataMethodBodyGenerator ) result ;
255+ Assert . That ( generator . Data . RuntimeDelegateBody , Is . Not . Null ) ;
256+ Assert . That ( generator . Data . CompileTimeConstants , Is . EqualTo ( 42 ) ) ;
257+ }
258+
259+ [ Test ]
260+ public void Stage5WithConstants_BodyReturningConstant_SetsReturnConstantValueFactory ( )
261+ {
262+ DataMethodBodyBuilderStage5WithConstants < int , string , int > stage5 = new DataMethodBodyBuilderStage5WithConstants < int , string , int > (
263+ new BodyGenerationData ( ReturnType : typeof ( string ) , ParametersTypes : [ typeof ( int ) ] , CompileTimeConstants : 42 ) ) ;
264+
265+ IMethodBodyGenerator result = stage5 . BodyReturningConstant ( constants => $ "value_{ constants } ") ;
266+
267+ Assert . That ( result , Is . TypeOf < DataMethodBodyGenerator > ( ) ) ;
268+ DataMethodBodyGenerator generator = ( DataMethodBodyGenerator ) result ;
269+ Assert . That ( generator . Data . ReturnConstantValueFactory , Is . Not . Null ) ;
270+ Assert . That ( generator . Data . CompileTimeConstants , Is . EqualTo ( 42 ) ) ;
271+ }
272+
273+ [ Test ]
274+ public void Stage5NoArgWithConstants_UseProvidedBody_SetsRuntimeDelegateBody ( )
275+ {
276+ DataMethodBodyBuilderStage5NoArgWithConstants < string , int > stage5 = new DataMethodBodyBuilderStage5NoArgWithConstants < string , int > (
277+ new BodyGenerationData ( ReturnType : typeof ( string ) , ParametersTypes : [ ] , CompileTimeConstants : 10 ) ) ;
278+
279+ IMethodBodyGenerator result = stage5 . UseProvidedBody ( constants => $ "value_{ constants } ") ;
280+
281+ Assert . That ( result , Is . TypeOf < DataMethodBodyGenerator > ( ) ) ;
282+ DataMethodBodyGenerator generator = ( DataMethodBodyGenerator ) result ;
283+ Assert . That ( generator . Data . RuntimeDelegateBody , Is . Not . Null ) ;
284+ Assert . That ( generator . Data . CompileTimeConstants , Is . EqualTo ( 10 ) ) ;
285+ }
286+
287+ [ Test ]
288+ public void Stage5NoArgWithConstants_BodyReturningConstant_SetsReturnConstantValueFactory ( )
289+ {
290+ DataMethodBodyBuilderStage5NoArgWithConstants < string , int > stage5 = new DataMethodBodyBuilderStage5NoArgWithConstants < string , int > (
291+ new BodyGenerationData ( ReturnType : typeof ( string ) , ParametersTypes : [ ] , CompileTimeConstants : 10 ) ) ;
292+
293+ IMethodBodyGenerator result = stage5 . BodyReturningConstant ( constants => $ "const_{ constants } ") ;
294+
295+ Assert . That ( result , Is . TypeOf < DataMethodBodyGenerator > ( ) ) ;
296+ DataMethodBodyGenerator generator = ( DataMethodBodyGenerator ) result ;
297+ Assert . That ( generator . Data . ReturnConstantValueFactory , Is . Not . Null ) ;
298+ Assert . That ( generator . Data . CompileTimeConstants , Is . EqualTo ( 10 ) ) ;
299+ }
300+
301+ [ Test ]
302+ public void Stage5ReturnVoidWithConstants_UseProvidedBody_SetsRuntimeDelegateBody ( )
303+ {
304+ DataMethodBodyBuilderStage5ReturnVoidWithConstants < int , string > stage5 = new DataMethodBodyBuilderStage5ReturnVoidWithConstants < int , string > (
305+ new BodyGenerationData ( ReturnType : typeof ( void ) , ParametersTypes : [ typeof ( int ) ] , CompileTimeConstants : "ctx" ) ) ;
306+
307+ IMethodBodyGenerator result = stage5 . UseProvidedBody ( ( constants , param ) => { } ) ;
308+
309+ Assert . That ( result , Is . TypeOf < DataMethodBodyGenerator > ( ) ) ;
310+ DataMethodBodyGenerator generator = ( DataMethodBodyGenerator ) result ;
311+ Assert . That ( generator . Data . RuntimeDelegateBody , Is . Not . Null ) ;
312+ Assert . That ( generator . Data . CompileTimeConstants , Is . EqualTo ( "ctx" ) ) ;
313+ }
314+
315+ [ Test ]
316+ public void Stage5ReturnVoidNoArgWithConstants_UseProvidedBody_SetsRuntimeDelegateBody ( )
317+ {
318+ DataMethodBodyBuilderStage5ReturnVoidNoArgWithConstants < string > stage5 = new DataMethodBodyBuilderStage5ReturnVoidNoArgWithConstants < string > (
319+ new BodyGenerationData ( ReturnType : typeof ( void ) , ParametersTypes : [ ] , CompileTimeConstants : "ctx" ) ) ;
320+
321+ IMethodBodyGenerator result = stage5 . UseProvidedBody ( constants => { } ) ;
322+
323+ Assert . That ( result , Is . TypeOf < DataMethodBodyGenerator > ( ) ) ;
324+ DataMethodBodyGenerator generator = ( DataMethodBodyGenerator ) result ;
325+ Assert . That ( generator . Data . RuntimeDelegateBody , Is . Not . Null ) ;
326+ Assert . That ( generator . Data . CompileTimeConstants , Is . EqualTo ( "ctx" ) ) ;
327+ }
328+
329+ [ Test ]
330+ public void FullFluentChain_WithConstants_NoArg_BodyReturningConstant_ProducesCorrectData ( )
331+ {
332+ DataGeneratorsFactory factory = new DataGeneratorsFactory ( ) ;
333+
334+ IMethodBodyGenerator result = factory . StartFluentApiBuilderForBody ( )
335+ . ForMethod ( )
336+ . WithReturnType < string > ( )
337+ . WithNoParameters ( )
338+ . WithCompileTimeConstants ( ( ) => 42 )
339+ . BodyReturningConstant ( constants => $ "value_{ constants } ") ;
340+
341+ Assert . That ( result , Is . TypeOf < DataMethodBodyGenerator > ( ) ) ;
342+ DataMethodBodyGenerator generator = ( DataMethodBodyGenerator ) result ;
343+ Assert . That ( generator . Data . ReturnType , Is . EqualTo ( typeof ( string ) ) ) ;
344+ Assert . That ( generator . Data . ParametersTypes , Is . Empty ) ;
345+ Assert . That ( generator . Data . CompileTimeConstants , Is . EqualTo ( 42 ) ) ;
346+ Assert . That ( generator . Data . ReturnConstantValueFactory , Is . Not . Null ) ;
347+ object ? constantValue = generator . Data . ReturnConstantValueFactory ! . DynamicInvoke ( 42 ) ;
348+ Assert . That ( constantValue , Is . EqualTo ( "value_42" ) ) ;
349+ }
350+
351+ [ Test ]
352+ public void FullFluentChain_WithConstants_WithParam_UseProvidedBody_ProducesCorrectData ( )
353+ {
354+ DataGeneratorsFactory factory = new DataGeneratorsFactory ( ) ;
355+
356+ IMethodBodyGenerator result = factory . StartFluentApiBuilderForBody ( )
357+ . ForMethod ( )
358+ . WithReturnType < int > ( )
359+ . WithParameter < int > ( )
360+ . WithCompileTimeConstants ( ( ) => new { Offset = 100 } )
361+ . UseProvidedBody ( ( constants , param ) => constants . Offset + param ) ;
362+
363+ Assert . That ( result , Is . TypeOf < DataMethodBodyGenerator > ( ) ) ;
364+ DataMethodBodyGenerator generator = ( DataMethodBodyGenerator ) result ;
365+ Assert . That ( generator . Data . ReturnType , Is . EqualTo ( typeof ( int ) ) ) ;
366+ Assert . That ( generator . Data . ParametersTypes , Is . EqualTo ( new [ ] { typeof ( int ) } ) ) ;
367+ Assert . That ( generator . Data . CompileTimeConstants , Is . Not . Null ) ;
368+ Assert . That ( generator . Data . RuntimeDelegateBody , Is . Not . Null ) ;
369+ }
370+
371+ [ Test ]
372+ public void WithCompileTimeConstants_FactoryIsInvokedImmediately ( )
373+ {
374+ int invocationCount = 0 ;
375+ DataMethodBodyBuilderStage4NoArg < string > stage4 = new DataMethodBodyBuilderStage4NoArg < string > (
376+ new BodyGenerationData ( ReturnType : typeof ( string ) , ParametersTypes : [ ] ) ) ;
377+
378+ stage4 . WithCompileTimeConstants ( ( ) =>
379+ {
380+ invocationCount ++ ;
381+ return 42 ;
382+ } ) ;
383+
384+ Assert . That ( invocationCount , Is . EqualTo ( 1 ) ) ;
385+ }
192386}
0 commit comments