Skip to content

Commit 2a1b6a1

Browse files
WOrk on transformer
1 parent 3e78b3e commit 2a1b6a1

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
{{#if error}}
10+
Assert.Throws<ArgumentOutOfRangeException>(() => PerfectNumbers.Classify({{input.number}}));
11+
{{else}}
12+
Assert.Equal({{expected}}, PerfectNumbers.Classify({{input.number}}));
13+
{{/if}}
14+
}
15+
{{/test_cases}}
16+
}

generators.new/CanonicalData.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
using LibGit2Sharp;
66

7+
using Microsoft.CodeAnalysis.CSharp.Syntax;
8+
79
namespace Generators;
810

911
internal record TestCase(
@@ -12,6 +14,7 @@ internal record TestCase(
1214
string Property,
1315
Dictionary<string, dynamic> Input,
1416
dynamic Expected,
17+
dynamic Error,
1518
string[] Path);
1619

1720
internal static class CanonicalData
@@ -51,9 +54,19 @@ private static IEnumerable<TestCase> Parse(JsonObject jsonObject, ImmutableQueue
5154
: [ToTestCase(jsonObject, updatedPath)];
5255
}
5356

54-
private static TestCase ToTestCase(JsonObject jsonObject, IEnumerable<string> path)
57+
private static TestCase ToTestCase(JsonObject testCaseJson, IEnumerable<string> path)
5558
{
56-
jsonObject["path"] = JsonValue.Create(path);
57-
return jsonObject.Deserialize<TestCase>(SerializerOptions)!;
59+
testCaseJson["path"] = JsonValue.Create(path);
60+
testCaseJson["error"] = ToError(testCaseJson);
61+
return testCaseJson.Deserialize<TestCase>(SerializerOptions)!;
62+
}
63+
64+
private static JsonNode? ToError(JsonObject testCaseJson)
65+
{
66+
if (testCaseJson["expected"] is JsonObject expectedJson &&
67+
expectedJson.TryGetPropertyValue("error", out var error))
68+
return error!.DeepClone();
69+
70+
return null;
5871
}
5972
}

generators.new/TestGenerator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ internal static void GenerateTestsFromTemplate(Exercise exercise)
1111
.Where(testCase => !excludedTestCaseIds.Contains(testCase.Uuid))
1212
.ToArray();
1313

14-
var renderedTests = TemplateRenderer.RenderTests(exercise, testCases);
14+
var transformedTestCases = TestCaseTransformer.Transform(exercise, testCases);
15+
16+
var renderedTests = TemplateRenderer.RenderTests(exercise, transformedTestCases);
1517
File.WriteAllText(Paths.TestsFile(exercise), CodeFormatter.FormatCode(renderedTests));
1618
}
1719
}

generators/Exercises/Generators/PerfectNumbers.cs

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

0 commit comments

Comments
 (0)