Skip to content

Commit 0f5556c

Browse files
Copilotdex3r
andcommitted
Add tests for PiExample and PiExampleFluent; refactor to share helper code
Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
1 parent dcd9243 commit 0f5556c

5 files changed

Lines changed: 203 additions & 35 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace MattSourceGenHelpers.Tests;
2+
3+
internal static class GeneratedCodeTestHelper
4+
{
5+
private const char UnicodeBom = '\uFEFF';
6+
7+
internal static string ReadGeneratedCode(string generatedFileName)
8+
{
9+
string generatedCodePath = GetGeneratedCodePath(generatedFileName);
10+
return File.ReadAllText(generatedCodePath).TrimStart(UnicodeBom).ReplaceLineEndings("\n").TrimEnd();
11+
}
12+
13+
private static string GetGeneratedCodePath(string generatedFileName)
14+
{
15+
string projectDirectory = FindProjectDirectory();
16+
string[] generatedFiles = Directory.GetFiles(projectDirectory, generatedFileName, SearchOption.AllDirectories);
17+
18+
if (generatedFiles.Length != 1)
19+
{
20+
throw new AssertionException($"Expected exactly one generated file '{generatedFileName}', but found {generatedFiles.Length}.");
21+
}
22+
23+
return generatedFiles[0];
24+
}
25+
26+
private static string FindProjectDirectory()
27+
{
28+
string? currentDirectory = TestContext.CurrentContext.TestDirectory;
29+
30+
while (currentDirectory is not null)
31+
{
32+
string projectFilePath = Path.Combine(currentDirectory, "MattSourceGenHelpers.Tests.csproj");
33+
if (File.Exists(projectFilePath))
34+
{
35+
return currentDirectory;
36+
}
37+
38+
currentDirectory = Directory.GetParent(currentDirectory)?.FullName;
39+
}
40+
41+
throw new DirectoryNotFoundException("Could not locate MattSourceGenHelpers.Tests project directory.");
42+
}
43+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using MattSourceGenHelpers.Abstractions;
2+
// ReSharper disable ConvertClosureToMethodGroup
3+
4+
namespace MattSourceGenHelpers.Tests;
5+
6+
[TestFixture]
7+
public class PiExampleFluentTests
8+
{
9+
[TestCase(0, 3)]
10+
[TestCase(1, 1)]
11+
[TestCase(2, 4)]
12+
[TestCase(300, 3)]
13+
[TestCase(301, 7)]
14+
[TestCase(302, 2)]
15+
[TestCase(303, 4)]
16+
[TestCase(5, 9)]
17+
public void PiExampleFluentLikeGenerator_ProducesExpectedRuntimeOutput(int decimalNumber, int expectedDigit)
18+
{
19+
int result = TestPiFluentClass.GetPiDecimal(decimalNumber);
20+
21+
Assert.That(result, Is.EqualTo(expectedDigit));
22+
}
23+
24+
[Test]
25+
public void PiExampleFluentLikeGenerator_ProducesExpectedGeneratedCode()
26+
{
27+
string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCode("TestPiFluentClass_GetPiDecimal.g.cs");
28+
string expectedCode = """
29+
namespace MattSourceGenHelpers.Tests;
30+
31+
static partial class TestPiFluentClass
32+
{
33+
public static partial int GetPiDecimal(int decimalNumber)
34+
{
35+
switch (decimalNumber)
36+
{
37+
case 0: return 3;
38+
case 1: return 1;
39+
case 2: return 4;
40+
case 300: return 3;
41+
case 301: return 7;
42+
case 302: return 2;
43+
case 303: return 4;
44+
default: return TestSlowMath.CalculatePiDecimal(decimalNumber);
45+
}
46+
}
47+
}
48+
""".ReplaceLineEndings("\n").TrimEnd();
49+
50+
Assert.That(generatedCode, Is.EqualTo(expectedCode));
51+
}
52+
}
53+
54+
public static partial class TestPiFluentClass
55+
{
56+
public static partial int GetPiDecimal(int decimalNumber);
57+
58+
[GeneratesMethod(nameof(GetPiDecimal))]
59+
static IMethodImplementationGenerator GetPiDecimal_Generator() =>
60+
Generator
61+
.MethodImplementation<int, int>()
62+
.WithSwitchBody()
63+
.ForCases(0, 1, 2, Integer.Range(300, 303)).CompileTimeBody(decimalNumber => TestSlowMath.CalculatePiDecimal(decimalNumber))
64+
.ForDefaultCase().RuntimeBody(decimalNumber => () => TestSlowMath.CalculatePiDecimal(decimalNumber));
65+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using MattSourceGenHelpers.Abstractions;
2+
// ReSharper disable ConvertClosureToMethodGroup
3+
4+
namespace MattSourceGenHelpers.Tests;
5+
6+
[TestFixture]
7+
public class PiExampleTests
8+
{
9+
[TestCase(0, 3)]
10+
[TestCase(1, 1)]
11+
[TestCase(2, 4)]
12+
[TestCase(5, 9)]
13+
public void PiExampleLikeGenerator_ProducesExpectedRuntimeOutput(int decimalNumber, int expectedDigit)
14+
{
15+
int result = TestPiClass.GetPiDecimal(decimalNumber);
16+
17+
Assert.That(result, Is.EqualTo(expectedDigit));
18+
}
19+
20+
[Test]
21+
public void PiExampleLikeGenerator_ProducesExpectedGeneratedCode()
22+
{
23+
string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCode("TestPiClass_GetPiDecimal.g.cs");
24+
string expectedCode = """
25+
namespace MattSourceGenHelpers.Tests;
26+
27+
static partial class TestPiClass
28+
{
29+
public static partial int GetPiDecimal(int decimalNumber)
30+
{
31+
switch (decimalNumber)
32+
{
33+
case 0: return 3;
34+
case 1: return 1;
35+
case 2: return 4;
36+
default: return TestSlowMath.CalculatePiDecimal(decimalNumber);
37+
}
38+
}
39+
}
40+
""".ReplaceLineEndings("\n").TrimEnd();
41+
42+
Assert.That(generatedCode, Is.EqualTo(expectedCode));
43+
}
44+
}
45+
46+
public static partial class TestPiClass
47+
{
48+
public static partial int GetPiDecimal(int decimalNumber);
49+
50+
[GeneratesMethod(nameof(GetPiDecimal))]
51+
[SwitchCase(arg1: 0)]
52+
[SwitchCase(arg1: 1)]
53+
[SwitchCase(arg1: 2)]
54+
static int GetPiDecimal_Generator_Specialized(int decimalNumber) =>
55+
TestSlowMath.CalculatePiDecimal(decimalNumber);
56+
57+
[GeneratesMethod(nameof(GetPiDecimal))]
58+
[SwitchDefault]
59+
static Func<int, int> GetPiDecimal_Generator_Fallback() => decimalNumber => TestSlowMath.CalculatePiDecimal(decimalNumber);
60+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace MattSourceGenHelpers.Tests;
2+
3+
internal static class TestSlowMath
4+
{
5+
// Pi digits string (0-indexed: position 0 = '3', position 1 = '1', position 2 = '4', ...)
6+
private static readonly string PiDigits =
7+
"31415926535897932384626433832795028841971693993751" +
8+
"05820974944592307816406286208998628034825342117067" +
9+
"98214808651328230664709384460955058223172535940812" +
10+
"84811174502841027019385211055596446229489549303819" +
11+
"64428810975665933446128475648233786783165271201909" +
12+
"14564856692346034861045432664821339360726024914127" +
13+
"37245870066063155881748815209209628292540917153643" +
14+
"67892590360011330530548820466521384146951941511609" +
15+
"43305727036575959195309218611738193261179310511854" +
16+
"80744623799627495673518857527248912279381830119491" +
17+
"29833673362440656643086021394946395224737190702179" +
18+
"86094370277053921717629317675238467481846766940513" +
19+
"20005681271452635608277857713427577896091736371787" +
20+
"21468440901224953430146549585371050792279689258923" +
21+
"54201995611212902196086403441815981362977477130996" +
22+
"05187072113499999983729780499510597317328160963185" +
23+
"95024459455346908302642522308253344685035261931188" +
24+
"17101000313783875288658753320838142061717766914730" +
25+
"35982534904287554687311595628638823537875937519577" +
26+
"81857780532171226806613001927876611195909216420198";
27+
28+
public static int CalculatePiDecimal(int decimalNumber)
29+
{
30+
if (decimalNumber >= 0 && decimalNumber < PiDigits.Length)
31+
return PiDigits[decimalNumber] - '0';
32+
return 0;
33+
}
34+
}

MattSourceGenHelpers.Tests/UnitTest1.cs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ namespace MattSourceGenHelpers.Tests;
44

55
public class Tests
66
{
7-
private const char UnicodeBom = '\uFEFF';
8-
97
[Test]
108
public void ColorsClassLikeGenerator_ProducesExpectedRuntimeOutput()
119
{
@@ -19,8 +17,7 @@ public void ColorsClassLikeGenerator_ProducesExpectedRuntimeOutput()
1917
[Test]
2018
public void ColorsClassLikeGenerator_ProducesExpectedGeneratedCode()
2119
{
22-
string generatedCodePath = GetGeneratedCodePath();
23-
string generatedCode = File.ReadAllText(generatedCodePath).TrimStart(UnicodeBom).ReplaceLineEndings("\n").TrimEnd();
20+
string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCode("TestColorsClass_GetAllColorsString.g.cs");
2421
string expectedCode = """
2522
namespace MattSourceGenHelpers.Tests;
2623
@@ -35,37 +32,6 @@ public partial string GetAllColorsString()
3532

3633
Assert.That(generatedCode, Is.EqualTo(expectedCode));
3734
}
38-
39-
private static string GetGeneratedCodePath()
40-
{
41-
string projectDirectory = FindProjectDirectory();
42-
string[] generatedFiles = Directory.GetFiles(projectDirectory, "TestColorsClass_GetAllColorsString.g.cs", SearchOption.AllDirectories);
43-
44-
if (generatedFiles.Length != 1)
45-
{
46-
throw new AssertionException($"Expected exactly one generated file, but found {generatedFiles.Length}.");
47-
}
48-
49-
return generatedFiles[0];
50-
}
51-
52-
private static string FindProjectDirectory()
53-
{
54-
string? currentDirectory = TestContext.CurrentContext.TestDirectory;
55-
56-
while (currentDirectory is not null)
57-
{
58-
string projectFilePath = Path.Combine(currentDirectory, "MattSourceGenHelpers.Tests.csproj");
59-
if (File.Exists(projectFilePath))
60-
{
61-
return currentDirectory;
62-
}
63-
64-
currentDirectory = Directory.GetParent(currentDirectory)?.FullName;
65-
}
66-
67-
throw new DirectoryNotFoundException("Could not locate MattSourceGenHelpers.Tests project directory.");
68-
}
6935
}
7036

7137
public enum TestColorsEnum

0 commit comments

Comments
 (0)