Skip to content

Commit 8db0c65

Browse files
WOrk on transformer
1 parent 3e78b3e commit 8db0c65

File tree

4 files changed

+77
-42
lines changed

4 files changed

+77
-42
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Xunit;
2+
3+
public class PerfectNumbersTests
4+
{
5+
{{#test_cases}}
6+
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
7+
public void {{method_name path}}()
8+
{
9+
Assert.Equal(Classification.{{expected}}, PerfectNumbers.Classify({{input.number}}));
10+
}
11+
{{/test_cases}}
12+
}
Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,92 @@
1-
using System;
21
using Xunit;
32

43
public class PerfectNumbersTests
54
{
65
[Fact]
7-
public void Smallest_perfect_number_is_classified_correctly()
6+
public void PerfectNumbersSmallestPerfectNumberIsClassifiedCorrectly()
87
{
9-
Assert.Equal(Classification.Perfect, PerfectNumbers.Classify(6));
8+
Assert.Equal(Classification."perfect", PerfectNumbers.Classify(6));
109
}
1110

1211
[Fact(Skip = "Remove this Skip property to run this test")]
13-
public void Medium_perfect_number_is_classified_correctly()
12+
public void PerfectNumbersMediumPerfectNumberIsClassifiedCorrectly()
1413
{
15-
Assert.Equal(Classification.Perfect, PerfectNumbers.Classify(28));
14+
Assert.Equal(Classification."perfect", PerfectNumbers.Classify(28));
1615
}
1716

1817
[Fact(Skip = "Remove this Skip property to run this test")]
19-
public void Large_perfect_number_is_classified_correctly()
18+
public void PerfectNumbersLargePerfectNumberIsClassifiedCorrectly()
2019
{
21-
Assert.Equal(Classification.Perfect, PerfectNumbers.Classify(33550336));
20+
Assert.Equal(Classification."perfect", PerfectNumbers.Classify(33550336));
2221
}
2322

2423
[Fact(Skip = "Remove this Skip property to run this test")]
25-
public void Smallest_abundant_number_is_classified_correctly()
24+
public void AbundantNumbersSmallestAbundantNumberIsClassifiedCorrectly()
2625
{
27-
Assert.Equal(Classification.Abundant, PerfectNumbers.Classify(12));
26+
Assert.Equal(Classification."abundant", PerfectNumbers.Classify(12));
2827
}
2928

3029
[Fact(Skip = "Remove this Skip property to run this test")]
31-
public void Medium_abundant_number_is_classified_correctly()
30+
public void AbundantNumbersMediumAbundantNumberIsClassifiedCorrectly()
3231
{
33-
Assert.Equal(Classification.Abundant, PerfectNumbers.Classify(30));
32+
Assert.Equal(Classification."abundant", PerfectNumbers.Classify(30));
3433
}
3534

3635
[Fact(Skip = "Remove this Skip property to run this test")]
37-
public void Large_abundant_number_is_classified_correctly()
36+
public void AbundantNumbersLargeAbundantNumberIsClassifiedCorrectly()
3837
{
39-
Assert.Equal(Classification.Abundant, PerfectNumbers.Classify(33550335));
38+
Assert.Equal(Classification."abundant", PerfectNumbers.Classify(33550335));
4039
}
4140

4241
[Fact(Skip = "Remove this Skip property to run this test")]
43-
public void Smallest_prime_deficient_number_is_classified_correctly()
42+
public void DeficientNumbersSmallestPrimeDeficientNumberIsClassifiedCorrectly()
4443
{
45-
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(2));
44+
Assert.Equal(Classification."deficient", PerfectNumbers.Classify(2));
4645
}
4746

4847
[Fact(Skip = "Remove this Skip property to run this test")]
49-
public void Smallest_non_prime_deficient_number_is_classified_correctly()
48+
public void DeficientNumbersSmallestNonPrimeDeficientNumberIsClassifiedCorrectly()
5049
{
51-
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(4));
50+
Assert.Equal(Classification."deficient", PerfectNumbers.Classify(4));
5251
}
5352

5453
[Fact(Skip = "Remove this Skip property to run this test")]
55-
public void Medium_deficient_number_is_classified_correctly()
54+
public void DeficientNumbersMediumDeficientNumberIsClassifiedCorrectly()
5655
{
57-
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(32));
56+
Assert.Equal(Classification."deficient", PerfectNumbers.Classify(32));
5857
}
5958

6059
[Fact(Skip = "Remove this Skip property to run this test")]
61-
public void Large_deficient_number_is_classified_correctly()
60+
public void DeficientNumbersLargeDeficientNumberIsClassifiedCorrectly()
6261
{
63-
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(33550337));
62+
Assert.Equal(Classification."deficient", PerfectNumbers.Classify(33550337));
6463
}
6564

6665
[Fact(Skip = "Remove this Skip property to run this test")]
67-
public void Edge_case_no_factors_other_than_itself_is_classified_correctly()
66+
public void DeficientNumbersEdgeCaseNoFactorsOtherThanItselfIsClassifiedCorrectly()
6867
{
69-
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(1));
68+
Assert.Equal(Classification."deficient", PerfectNumbers.Classify(1));
7069
}
7170

7271
[Fact(Skip = "Remove this Skip property to run this test")]
73-
public void Zero_is_rejected_as_it_is_not_a_positive_integer_()
72+
public void InvalidInputsZeroIsRejectedAsItIsNotAPositiveInteger()
7473
{
75-
Assert.Throws<ArgumentOutOfRangeException>(() => PerfectNumbers.Classify(0));
74+
Assert.Equal(Classification.
75+
{
76+
"error" :
77+
"Classification is only possible for positive integers."
78+
} , PerfectNumbers.Classify(0) )
79+
;
7680
}
7781

7882
[Fact(Skip = "Remove this Skip property to run this test")]
79-
public void Negative_integer_is_rejected_as_it_is_not_a_positive_integer_()
83+
public void InvalidInputsNegativeIntegerIsRejectedAsItIsNotAPositiveInteger()
8084
{
81-
Assert.Throws<ArgumentOutOfRangeException>(() => PerfectNumbers.Classify(-1));
85+
Assert.Equal(Classification.
86+
{
87+
"error" :
88+
"Classification is only possible for positive integers."
89+
} , PerfectNumbers.Classify(-1) )
90+
;
8291
}
8392
}

generators.new/Transformation.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace Generators;
2+
3+
internal class PerfectNumbersTestCaseTransformer : TestCaseTransformer
4+
{
5+
protected override TestCase UpdateTestCase(TestCase testCase) =>
6+
testCase with { Expected = $"Classification.{testCase.Expected.ToString().Pascalize()}" };
7+
}
8+
9+
abstract class TestCaseTransformer
10+
{
11+
protected virtual TestCase UpdateTestCase(TestCase testCase) => testCase;
12+
13+
protected virtual TestCase[] AddOrRemoveTestCases(TestCase[] testCases) => testCases;
14+
15+
public static TestCase[] Transform(Exercise exercise, TestCase[] testCases)
16+
{
17+
var transformerType = Type.GetType($"Generators.{exercise.Name}TestCaseTransformer");
18+
if (transformerType is null)
19+
return testCases;
20+
21+
22+
if (Activator.CreateInstance(transformerType) is not TestCaseTransformer transformer)
23+
throw new InvalidOperationException($"Could not instantiate {transformerType} type");
24+
25+
return transformer.AddOrRemoveTestCases(testCases)
26+
.Select(transformer.UpdateTestCase)
27+
.ToArray();
28+
}
29+
}

generators/Exercises/Generators/PerfectNumbers.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)