-
-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathTemplates.cs
51 lines (42 loc) · 1.99 KB
/
Templates.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Text.Json;
using System.Text.Json.Nodes;
using Humanizer;
using Scriban;
using Scriban.Runtime;
namespace Generators;
internal static class Templates
{
public static string RenderTestsCode(CanonicalData canonicalData)
{
var scriptObject = new ScriptObject();
scriptObject.Import("pascalize", new Func<string, string>((text) =>
text.Pascalize()));
scriptObject.Import("enum", new Func<string, string, string>((text, enumType) =>
$"{enumType.Pascalize()}.{text.Pascalize()}"));
scriptObject.Import("property", new Func<ScriptArray, string, ScriptArray>((testCases, name) =>
new ScriptArray(testCases.Cast<ScriptObject>().Where(testCase => testCase["property"].ToString() == name))));
scriptObject.Import(TemplateData.ForCanonicalData(canonicalData));
var context = new TemplateContext();
context.PushGlobal(scriptObject);
return Template.Parse(File.ReadAllText(Paths.TemplateFile(canonicalData.Exercise)))
.Render(context);
}
private static class TemplateData
{
internal static JsonElement ForCanonicalData(CanonicalData canonicalData) =>
JsonSerializer.SerializeToElement(
new
{
testClass = $"{canonicalData.Exercise.Name}Tests".Pascalize(),
testedClass = canonicalData.Exercise.Name.Pascalize(),
tests = canonicalData.TestCases.Select(Create).ToArray()
});
private static JsonElement Create(JsonNode testCase)
{
testCase["testMethod"] = Naming.ToTestMethodName(testCase["path"]!.AsArray().GetValues<string>().ToArray());
testCase["shortTestMethod"] = Naming.ToTestMethodName(testCase["description"]!.GetValue<string>());
testCase["testedMethod"] = Naming.ToMethodName(testCase["property"]!.GetValue<string>());
return JsonSerializer.SerializeToElement(testCase);
}
}
}