1- // SwitchCase/SwitchDefault attribute-based generation is commented out pending replacement with a data-driven approach.
2- // See DataMethodBodyBuilders.cs for details on the planned replacement.
3-
4- /*
51using EasySourceGenerators . Abstractions ;
6- // ReSharper disable ConvertClosureToMethodGroup
72
83namespace EasySourceGenerators . Tests ;
94
105[ TestFixture ]
116public class PiExampleTests
127{
13- [Test]
14- public void SwitchCaseAttribute_StoresArg1Value()
15- {
16- SwitchCase switchCase = new(arg1: 7);
17-
18- Assert.That(switchCase.Arg1, Is.EqualTo(7));
19- }
20-
21- [TestCase(0, 3)]
22- [TestCase(1, 1)]
23- [TestCase(2, 4)]
24- [TestCase(5, 9)]
25- public void PiExampleLikeGenerator_ProducesExpectedRuntimeOutput(int decimalNumber, int expectedDigit)
26- {
27- int result = TestPiClass.GetPiDecimal(decimalNumber);
28-
29- Assert.That(result, Is.EqualTo(expectedDigit));
30- }
31-
328 [ Test ]
339 public void PiExampleLikeGenerator_ProducesExpectedGeneratedCode ( )
3410 {
35- string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCode("TestPiClass_GetPiDecimal.g.cs");
11+ string generatedCode = GeneratedCodeTestHelper . ReadGeneratedCode ( "TestPiExampleFluent_GetPiDecimal.g.cs" ) ;
12+
13+ //TODO: The specifics here, like the formatting or where the "constants" is declared might be off here, but the general idea must be working
3614 string expectedCode = """
3715 namespace EasySourceGenerators.Tests;
3816
39- static partial class TestPiClass
17+ static partial class TestPiExampleFluent
4018 {
4119 public static partial int GetPiDecimal(int decimalNumber)
4220 {
43- switch (decimalNumber)
21+ var constants = new
4422 {
45- case 0: return 3;
46- case 1: return 1;
47- case 2: return 4;
48- default: return TestSlowMath.CalculatePiDecimal(decimalNumber);
49- }
23+ PrecomputedTargets = (new int[] { 0, 1, 2, 300, 301, 302, 303 }).ToDictionary(i => i, i => SlowMath.CalculatePiDecimal(i))
24+ };
25+
26+ if (constants.PrecomputedTargets.TryGetValue(decimalNumber, out int precomputedResult)) return precomputedResult;
27+
28+ return SlowMath.CalculatePiDecimal(decimalNumber);
5029 }
5130 }
5231 """ . ReplaceLineEndings ( "\n " ) . TrimEnd ( ) ;
@@ -55,19 +34,23 @@ public static partial int GetPiDecimal(int decimalNumber)
5534 }
5635}
5736
58- public static partial class TestPiClass
37+ public static partial class TestPiExampleFluent
5938{
6039 public static partial int GetPiDecimal ( int decimalNumber ) ;
6140
6241 [ MethodBodyGenerator ( nameof ( GetPiDecimal ) ) ]
63- [SwitchCase(arg1: 0)]
64- [SwitchCase(arg1: 1)]
65- [SwitchCase(arg1: 2)]
66- static int GetPiDecimal_Generator_Specialized(int decimalNumber) =>
67- TestSlowMath.CalculatePiDecimal(decimalNumber);
68-
69- [MethodBodyGenerator(nameof(GetPiDecimal))]
70- [SwitchDefault]
71- static Func<int, int> GetPiDecimal_Generator_Fallback() => decimalNumber => TestSlowMath.CalculatePiDecimal(decimalNumber);
42+ static IMethodBodyGenerator GetPiDecimal_Generator ( ) =>
43+ Generate . MethodBody ( )
44+ . ForMethod ( ) . WithReturnType < int > ( ) . WithParameter < int > ( )
45+ . WithCompileTimeConstants ( ( ) => new
46+ {
47+ PrecomputedTargets = ( new int [ ] { 0 , 1 , 2 , 300 , 301 , 302 , 303 } ) . ToDictionary ( i => i , i => SlowMath . CalculatePiDecimal ( i ) )
48+ } )
49+ . UseProvidedBody ( ( constants , decimalNumber ) =>
50+ {
51+ if ( constants . PrecomputedTargets . TryGetValue ( decimalNumber , out int precomputedResult ) ) return precomputedResult ;
52+
53+ return SlowMath . CalculatePiDecimal ( decimalNumber ) ;
54+ } ) ;
7255}
73- */
56+
0 commit comments