-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiExampleTests.cs
More file actions
60 lines (51 loc) · 2.15 KB
/
Copy pathPiExampleTests.cs
File metadata and controls
60 lines (51 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using MattSourceGenHelpers.Abstractions;
// ReSharper disable ConvertClosureToMethodGroup
namespace MattSourceGenHelpers.Tests;
[TestFixture]
public class PiExampleTests
{
[TestCase(0, 3)]
[TestCase(1, 1)]
[TestCase(2, 4)]
[TestCase(5, 9)]
public void PiExampleLikeGenerator_ProducesExpectedRuntimeOutput(int decimalNumber, int expectedDigit)
{
int result = TestPiClass.GetPiDecimal(decimalNumber);
Assert.That(result, Is.EqualTo(expectedDigit));
}
[Test]
public void PiExampleLikeGenerator_ProducesExpectedGeneratedCode()
{
string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCode("TestPiClass_GetPiDecimal.g.cs");
string expectedCode = """
namespace MattSourceGenHelpers.Tests;
static partial class TestPiClass
{
public static partial int GetPiDecimal(int decimalNumber)
{
switch (decimalNumber)
{
case 0: return 3;
case 1: return 1;
case 2: return 4;
default: return TestSlowMath.CalculatePiDecimal(decimalNumber);
}
}
}
""".ReplaceLineEndings("\n").TrimEnd();
Assert.That(generatedCode, Is.EqualTo(expectedCode));
}
}
public static partial class TestPiClass
{
public static partial int GetPiDecimal(int decimalNumber);
[GeneratesMethod(nameof(GetPiDecimal))]
[SwitchCase(arg1: 0)]
[SwitchCase(arg1: 1)]
[SwitchCase(arg1: 2)]
static int GetPiDecimal_Generator_Specialized(int decimalNumber) =>
TestSlowMath.CalculatePiDecimal(decimalNumber);
[GeneratesMethod(nameof(GetPiDecimal))]
[SwitchDefault]
static Func<int, int> GetPiDecimal_Generator_Fallback() => decimalNumber => TestSlowMath.CalculatePiDecimal(decimalNumber);
}