@@ -49,6 +49,40 @@ public static partial int GetPiDecimal(int decimalNumber)
4949
5050 Assert . That ( generatedCode , Is . EqualTo ( expectedCode ) ) ;
5151 }
52+
53+ [ TestCase ( 1 , "Dog" ) ]
54+ [ TestCase ( 2 , "Cat" ) ]
55+ [ TestCase ( 3 , "Unknown" ) ]
56+ public void MapperFluentLikeGenerator_ProducesExpectedRuntimeOutput ( int source , string expected )
57+ {
58+ string result = TestMapperFluent . MapToMammal ( source ) ;
59+
60+ Assert . That ( result , Is . EqualTo ( expected ) ) ;
61+ }
62+
63+ [ Test ]
64+ public void MapperFluentLikeGenerator_ProducesExpectedGeneratedCode ( )
65+ {
66+ string generatedCode = GeneratedCodeTestHelper . ReadGeneratedCode ( "TestMapperFluent_MapToMammal.g.cs" ) ;
67+ string expectedCode = """
68+ namespace MattSourceGenHelpers.Tests;
69+
70+ static partial class TestMapperFluent
71+ {
72+ public static partial string MapToMammal(int animalCode)
73+ {
74+ switch (animalCode)
75+ {
76+ case 1: return "Dog";
77+ case 2: return "Cat";
78+ default: return "Unknown";
79+ }
80+ }
81+ }
82+ """ . ReplaceLineEndings ( "\n " ) . TrimEnd ( ) ;
83+
84+ Assert . That ( generatedCode , Is . EqualTo ( expectedCode ) ) ;
85+ }
5286}
5387
5488public static partial class TestPiFluentClass
@@ -58,8 +92,22 @@ public static partial class TestPiFluentClass
5892 [ GeneratesMethod ( nameof ( GetPiDecimal ) ) ]
5993 static IMethodImplementationGenerator GetPiDecimal_Generator ( ) =>
6094 Generate
61- . MethodImplementation < int , int > ( )
95+ . Method ( ) . WithParameter < int > ( ) . WithReturnType < int > ( )
6296 . WithSwitchBody ( )
6397 . ForCases ( 0 , 1 , 2 , Integer . Range ( 300 , 303 ) ) . ReturnConstantValue ( decimalNumber => TestSlowMath . CalculatePiDecimal ( decimalNumber ) )
6498 . ForDefaultCase ( ) . WithBody ( decimalNumber => ( ) => TestSlowMath . CalculatePiDecimal ( decimalNumber ) ) ;
6599}
100+
101+ public static partial class TestMapperFluent
102+ {
103+ public static partial string MapToMammal ( int animalCode ) ;
104+
105+ [ GeneratesMethod ( nameof ( MapToMammal ) ) ]
106+ static IMethodImplementationGenerator MapToMammal_Generator ( ) =>
107+ Generate
108+ . Method ( ) . WithParameter < int > ( ) . WithReturnType < string > ( )
109+ . WithSwitchBody ( )
110+ . ForCases ( 1 ) . ReturnConstantValue ( _ => "Dog" )
111+ . ForCases ( 2 ) . ReturnConstantValue ( _ => "Cat" )
112+ . ForDefaultCase ( ) . WithBody ( _ => ( ) => "Unknown" ) ;
113+ }
0 commit comments