Skip to content

Commit af77825

Browse files
Move to expandoobject
1 parent 9c82be0 commit af77825

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

generators.new/Templates.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Dynamic;
12
using System.Globalization;
23

34
using HandlebarsDotNet;
@@ -45,16 +46,16 @@ static Templates()
4546
}
4647

4748
public static string RenderTestsCode(CanonicalData canonicalData) =>
48-
CompileTemplate(canonicalData.Exercise)(TemplateData.Create(canonicalData));
49+
CompileTemplate(canonicalData.Exercise)(TemplateData.ForCanonicalData(canonicalData));
4950

5051
private static HandlebarsTemplate<object, object> CompileTemplate(Exercise exercise) =>
5152
HandlebarsContext.Compile(File.ReadAllText(Paths.TemplateFile(exercise)));
5253

5354
private static class TemplateData
5455
{
55-
internal static Dictionary<string, object> Create(CanonicalData canonicalData)
56+
internal static Dictionary<string, object> ForCanonicalData(CanonicalData canonicalData)
5657
{
57-
var testCases = canonicalData.TestCases.Select(ForTestCase).ToArray();
58+
var testCases = canonicalData.TestCases.Select(Create).ToArray();
5859

5960
return new Dictionary<string, object>
6061
{
@@ -64,11 +65,11 @@ internal static Dictionary<string, object> Create(CanonicalData canonicalData)
6465
};
6566
}
6667

67-
private static Dictionary<string, object> ForTestCase(JToken testCase, int index)
68+
private static ExpandoObject Create(JToken testCase)
6869
{
69-
var testData = testCase.ToObject<Dictionary<string, object>>()!;
70-
testData["test_method_name"] = ToMethodName(testCase["path"]!.ToObject<string[]>()!);
71-
testData["short_test_method_name"] = ToMethodName(testCase["property"]!.ToObject<string>()!);
70+
dynamic testData = testCase.ToObject<ExpandoObject>()!;
71+
testData.test_method_name = ToMethodName(testData.path.ToArray());
72+
testData.short_test_method_name = ToMethodName(testData.property);
7273

7374
return testData;
7475
}
@@ -82,21 +83,23 @@ private static Dictionary<string, object> Create(Exercise exercise) =>
8283
["tested_class_name"] = $"{exercise.Name}"
8384
};
8485

85-
private static Dictionary<string, Dictionary<string, object>[]> GroupTestCasesByProperty(IEnumerable<Dictionary<string, object>> testCases) =>
86+
private static Dictionary<string, dynamic[]> GroupTestCasesByProperty(IEnumerable<dynamic> testCases) =>
8687
testCases
87-
.GroupBy(testCase => testCase["property"].ToString()!)
88+
.GroupBy(testCase => (string)testCase.property)
8889
.ToDictionary(kv => kv.Key, kv => kv.ToArray());
8990

90-
private static string ToMethodName(params string[] path)
91+
private static string ToMethodName(params object[] path)
9192
{
93+
var stringPath = path.Select(obj => obj.ToString()!).ToArray();
94+
9295
// Fix method names that start with a number
93-
if (char.IsNumber(path[0][0]))
96+
if (char.IsNumber(stringPath[0][0]))
9497
{
95-
var parts = path[0].Split(' ');
96-
path[0] = string.Join(" ", [Convert.ToInt32(parts[0]).ToWords(), ..parts[1..]]);
98+
var parts = stringPath[0].Split(' ');
99+
stringPath[0] = string.Join(" ", [Convert.ToInt32(parts[0]).ToWords(), ..parts[1..]]);
97100
}
98101

99-
return string.Join(" ", path).Dehumanize();
102+
return string.Join(" ", stringPath).Dehumanize();
100103
}
101104
}
102105
}

0 commit comments

Comments
 (0)