Skip to content

Commit 062a992

Browse files
committed
Fixed
1 parent b4f9ea7 commit 062a992

4 files changed

Lines changed: 47 additions & 222 deletions

File tree

EasySourceGenerators.Tests/EasySourceGenerators.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<ItemGroup>
1414
<PackageReference Include="coverlet.collector" Version="6.0.4"/>
15+
<PackageReference Include="ExtendedNumerics.BigDecimal" Version="3003.0.0.346" />
1516
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.0"/>
1617
<PackageReference Include="NUnit" Version="4.3.2"/>
1718
<PackageReference Include="NUnit.Analyzers" Version="4.7.0"/>

EasySourceGenerators.Tests/PiExampleFluentTests.cs

Lines changed: 0 additions & 179 deletions
This file was deleted.
Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,31 @@
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-
/*
51
using EasySourceGenerators.Abstractions;
6-
// ReSharper disable ConvertClosureToMethodGroup
72

83
namespace EasySourceGenerators.Tests;
94

105
[TestFixture]
116
public 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+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Globalization;
2+
using ExtendedNumerics;
3+
4+
namespace EasySourceGenerators.Tests;
5+
6+
public static class SlowMath
7+
{
8+
public static int CalculatePiDecimal(int decimalNumber)
9+
{
10+
if (decimalNumber < 0) throw new ArgumentOutOfRangeException(nameof(decimalNumber), "Decimal number must be non-negative.");
11+
if (decimalNumber == 0) return 3;
12+
13+
BigDecimal pi = BigDecimal.ApproximatePi(decimalNumber + 1);
14+
15+
if(pi.DecimalPlaces < decimalNumber) throw new ArgumentException($"Failed to calculate pi to {decimalNumber} decimal places.");
16+
17+
string piString = pi.ToString(CultureInfo.InvariantCulture);
18+
return int.Parse(piString.Substring(decimalNumber + 1, 1));
19+
}
20+
}

0 commit comments

Comments
 (0)