Skip to content

Commit 648771a

Browse files
Refactoring
1 parent a5dd9f9 commit 648771a

File tree

8 files changed

+72
-60
lines changed

8 files changed

+72
-60
lines changed

generators.new/CanonicalData.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,29 @@ internal record TestCase(
1717
dynamic Error,
1818
string[] Path);
1919

20-
internal static class CanonicalData
20+
internal record CanonicalData(Exercise Exercise, TestCase[] TestCases);
21+
22+
internal static class CanonicalDataParser
2123
{
22-
static CanonicalData() => ProbSpecs.Sync();
24+
static CanonicalDataParser() => ProbSpecs.Sync();
25+
26+
internal static CanonicalData Parse(Exercise exercise) => new(exercise, ParseTestCases(exercise));
2327

24-
internal static TestCase[] Parse(Exercise exercise)
28+
private static TestCase[] ParseTestCases(Exercise exercise)
2529
{
26-
var json = File.ReadAllText(Paths.CanonicalDataFile(exercise));
27-
var jsonObject = JObject.Parse(json);
28-
return Parse(jsonObject, ImmutableQueue<string>.Empty)
29-
.ToArray();
30+
var jsonObject = JObject.Parse(File.ReadAllText(Paths.CanonicalDataFile(exercise)));
31+
return ParseTestCases(jsonObject, ImmutableQueue<string>.Empty).ToArray();
3032
}
3133

32-
private static IEnumerable<TestCase> Parse(JObject jsonObject, ImmutableQueue<string> path)
34+
private static IEnumerable<TestCase> ParseTestCases(JObject jsonObject, ImmutableQueue<string> path)
3335
{
3436
var updatedPath = jsonObject.TryGetValue("description", out var description)
3537
? path.Enqueue(description.Value<string>()!)
3638
: path;
3739

38-
if (jsonObject.TryGetValue("cases", out var cases))
39-
return ((JArray)cases).Cast<JObject>().SelectMany(child => Parse(child, updatedPath));
40-
41-
return [ToTestCase(jsonObject, updatedPath)];
40+
return jsonObject.TryGetValue("cases", out var cases)
41+
? ((JArray)cases).Cast<JObject>().SelectMany(child => ParseTestCases(child, updatedPath))
42+
: [ToTestCase(jsonObject, updatedPath)];
4243
}
4344

4445
private static TestCase ToTestCase(JObject testCaseJson, IEnumerable<string> path)

generators.new/Exercises.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ internal record Exercise(string Slug, string Name);
66

77
internal static class Exercises
88
{
9-
internal static Exercise[] TemplatedExercises(string? slug) =>
10-
slug is null ? TemplatedExercises() : [TemplatedExercise(slug)];
11-
12-
private static Exercise[] TemplatedExercises() =>
9+
internal static Exercise[] TemplatedExercises() =>
1310
Directory.EnumerateFiles(Paths.PracticeExercisesDir, "Generator.tpl", SearchOption.AllDirectories)
1411
.Select(templateFile => Directory.GetParent(templateFile)!.Parent!.Name)
1512
.Select(ToExercise)
1613
.OrderBy(exercise => exercise.Slug)
1714
.ToArray();
1815

19-
private static Exercise TemplatedExercise(string slug)
16+
internal static Exercise TemplatedExercise(string slug)
2017
{
2118
var exercise = ToExercise(slug);
2219

generators.new/Program.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ static void Main(string[] args) =>
1414
Parser.Default.ParseArguments<Options>(args)
1515
.WithParsed(options =>
1616
{
17-
foreach (var exercise in Exercises.TemplatedExercises(options.Exercise))
18-
TestGenerator.GenerateTests(exercise);
17+
foreach (var exercise in Exercises(options))
18+
TestsGenerator.Generate(exercise);
1919
});
20+
21+
private static Exercise[] Exercises(Options options) =>
22+
options.Exercise is null
23+
? Generators.Exercises.TemplatedExercises()
24+
: [Generators.Exercises.TemplatedExercise(options.Exercise)];
2025
}

generators.new/Templates.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ static Templates()
4242
});
4343
}
4444

45-
public static string RenderTests(Exercise exercise, TestCase[] testCases) =>
46-
CompileTemplate(exercise)(ToTemplateData(exercise, testCases));
45+
public static string RenderTestsCode(CanonicalData canonicalData) =>
46+
CompileTemplate(canonicalData.Exercise)(ToTemplateData(canonicalData));
4747

4848
private static HandlebarsTemplate<object, object> CompileTemplate(Exercise exercise) =>
4949
HandlebarsContext.Compile(File.ReadAllText(Paths.TemplateFile(exercise)));
5050

51-
private static Dictionary<string, object> ToTemplateData(Exercise exercise, TestCase[] testCases) =>
51+
private static Dictionary<string, object> ToTemplateData(CanonicalData canonicalData) =>
5252
new()
5353
{
54-
{ "exercise", exercise },
55-
{ "test_cases", testCases },
56-
{ "test_cases_by_property", GroupTestCasesByProperty(testCases)}
54+
{ "exercise", canonicalData.Exercise },
55+
{ "test_cases", canonicalData.TestCases },
56+
{ "test_cases_by_property", GroupTestCasesByProperty(canonicalData.TestCases)}
5757
};
5858

5959
private static Dictionary<string, TestCase[]> GroupTestCasesByProperty(TestCase[] testCases) =>

generators.new/TestCaseConfiguration.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Tomlyn;
2+
using Tomlyn.Model;
3+
4+
namespace Generators;
5+
6+
internal static class TestCasesConfiguration
7+
{
8+
internal static CanonicalData RemoveExcludedTestCases(CanonicalData canonicalData)
9+
{
10+
var excludedTestCaseIds = ExcludedTestCaseIds(canonicalData.Exercise);
11+
var includedTestCases = canonicalData.TestCases
12+
.Where(testCase => !excludedTestCaseIds.Contains(testCase.Uuid))
13+
.ToArray();
14+
15+
return canonicalData with { TestCases = includedTestCases };
16+
}
17+
18+
private static HashSet<string> ExcludedTestCaseIds(Exercise exercise) =>
19+
Toml.ToModel(File.ReadAllText(Paths.TestsTomlFile(exercise)))
20+
.Where(IsExcluded)
21+
.Select(table => table.Key)
22+
.ToHashSet();
23+
24+
private static bool IsExcluded(KeyValuePair<string, object> idTablePair) =>
25+
idTablePair.Value is TomlTable table &&
26+
table.TryGetValue("include", out var includeValue) &&
27+
includeValue is false;
28+
}

generators.new/TestGenerator.cs

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

generators.new/TestsGenerator.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Generators;
2+
3+
internal static class TestsGenerator
4+
{
5+
internal static void Generate(Exercise exercise)
6+
{
7+
Console.WriteLine($"{exercise.Slug}: generating tests...");
8+
9+
var canonicalData = CanonicalDataParser.Parse(exercise);
10+
var filteredCanonicalData = TestCasesConfiguration.RemoveExcludedTestCases(canonicalData);
11+
12+
var testCode = Templates.RenderTestsCode(filteredCanonicalData);
13+
var formattedTestCode = Formatting.FormatCode(testCode);
14+
File.WriteAllText(Paths.TestsFile(exercise), formattedTestCode);
15+
}
16+
}

0 commit comments

Comments
 (0)