Last active
March 11, 2024 16:14
-
-
Save ilmanzo/3163cad4e2246f2553f1a90735e79e6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module gen; | |
import mustache; | |
import std.stdio; | |
import std.json; | |
import std.array; | |
import painlessjson; | |
class TestSuite | |
{ | |
string description; | |
TestCase[] cases; | |
string expected; | |
} | |
class TestCase | |
{ | |
string uuid; | |
string description; | |
string expected; | |
string[string] input; | |
} | |
// read json data from stdin and generates a testcase | |
alias MustacheEngine!(string) Mustache; | |
void main(string[] args) | |
{ | |
auto jsonString = stdin.byLineCopy.array.join; | |
auto json = parseJSON(jsonString); | |
string slug = json["exercise"].str; | |
auto tests = fromJSON!(TestSuite[])(json["cases"]); | |
Mustache mustache; | |
auto context = new Mustache.Context; | |
context["slug"] = slug; | |
foreach (t; tests) | |
{ | |
auto ctxcase = context.addSubContext("testcase"); | |
ctxcase["description"] = t.description; | |
foreach (c; t.cases) | |
{ | |
auto ctxtest = ctxcase.addSubContext("test"); | |
ctxtest["description"] = c.description; | |
ctxtest["expected"] = c.expected; | |
ctxtest["uuid"] = c.uuid; | |
ctxtest["input"] = c.input["phrase"]; | |
} | |
stdout.rawWrite(mustache.renderString(tlate, context)); | |
} | |
} | |
immutable string tlate = " | |
module {{slug}}; | |
{{#testcase}}// --- {{description}} --- | |
unittest { | |
{{#test}} | |
// Description: {{description}} | |
// uuid: {{uuid}} | |
assert translate(\"{{input}}\") == \"{{expected}}\"); | |
{{/test}} | |
}; | |
{{/testcase}} | |
"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output: