json: simplify test
This commit is contained in:
parent
5602a8b649
commit
f30d6c27b9
1 changed files with 55 additions and 60 deletions
|
@ -25,24 +25,10 @@ struct TestCase {
|
||||||
string schema;
|
string schema;
|
||||||
string expected;
|
string expected;
|
||||||
|
|
||||||
void write_input() const {
|
void verify(const string& actual) const {
|
||||||
ofstream f;
|
|
||||||
f.open("test-json-schema-input.tmp");
|
|
||||||
f << schema.c_str();
|
|
||||||
f.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
void read_and_verify(const string& series) const {
|
|
||||||
ostringstream actuals;
|
|
||||||
actuals << ifstream("test-grammar-output.tmp").rdbuf();
|
|
||||||
auto actual = actuals.str();
|
|
||||||
verify(series, actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
void verify(const string& series, const string& actual) const {
|
|
||||||
if (trim(actual) != trim(expected)) {
|
if (trim(actual) != trim(expected)) {
|
||||||
cerr << "#" << endl;
|
cerr << "#" << endl;
|
||||||
cerr << "# Test " << name.c_str() << " (" << series.c_str() << ") failed." << endl;
|
cerr << "# Test '" << name.c_str() << "' failed." << endl;
|
||||||
cerr << "#" << endl;
|
cerr << "#" << endl;
|
||||||
cerr << schema.c_str() << endl;
|
cerr << schema.c_str() << endl;
|
||||||
cerr << "# EXPECTED:\n" << expected.c_str() << endl;
|
cerr << "# EXPECTED:\n" << expected.c_str() << endl;
|
||||||
|
@ -53,35 +39,27 @@ struct TestCase {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void write(const string& file, const string& content) {
|
||||||
static void run_py(const TestCase& tc) {
|
ofstream f;
|
||||||
cerr << "Testing JSON schema conversion: " << tc.name.c_str() << " (Python)" << endl;
|
f.open(file.c_str());
|
||||||
tc.write_input();
|
f << content.c_str();
|
||||||
assert(std::system("python ./examples/json-schema-to-grammar.py test-json-schema-input.tmp > test-grammar-output.tmp") == 0);
|
f.close();
|
||||||
tc.read_and_verify("Python");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void run_mjs(const TestCase& tc) {
|
static string read(const string& file) {
|
||||||
cerr << "Testing JSON schema conversion: " << tc.name.c_str() << " (JS)" << endl;
|
ostringstream actuals;
|
||||||
tc.write_input();
|
actuals << ifstream(file.c_str()).rdbuf();
|
||||||
assert(std::system("node ./tests/run-json-schema-to-grammar.mjs test-json-schema-input.tmp > test-grammar-output.tmp") == 0);
|
return actuals.str();
|
||||||
tc.read_and_verify("JavaScript");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void run_cpp(const TestCase& tc) {
|
static void test(const string& lang, std::function<void(const TestCase&)> runner) {
|
||||||
cerr << "Testing JSON schema conversion: " << tc.name.c_str() << " (C++)" << endl;
|
cerr << "Testing JSON schema conversion (" << lang.c_str() << ")" << endl;
|
||||||
auto actual = json_schema_to_grammar(nlohmann::json::parse(tc.schema));
|
auto run = [&](const TestCase& tc) {
|
||||||
tc.verify("C++", actual);
|
cerr << "- " << tc.name.c_str() << endl;
|
||||||
}
|
runner(tc);
|
||||||
|
};
|
||||||
|
|
||||||
static void run_all(const TestCase& tc) {
|
run({
|
||||||
run_py(tc);
|
|
||||||
run_mjs(tc);
|
|
||||||
run_cpp(tc);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
run_all({
|
|
||||||
"exotic formats",
|
"exotic formats",
|
||||||
R"""({
|
R"""({
|
||||||
"items": [
|
"items": [
|
||||||
|
@ -104,7 +82,7 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"string",
|
"string",
|
||||||
R"""({
|
R"""({
|
||||||
"type": "string"
|
"type": "string"
|
||||||
|
@ -118,7 +96,7 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"boolean",
|
"boolean",
|
||||||
R"""({
|
R"""({
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
|
@ -129,7 +107,7 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"integer",
|
"integer",
|
||||||
R"""({
|
R"""({
|
||||||
"type": "integer"
|
"type": "integer"
|
||||||
|
@ -140,7 +118,7 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"tuple1",
|
"tuple1",
|
||||||
R"""({
|
R"""({
|
||||||
"prefixItems": [{ "type": "string" }]
|
"prefixItems": [{ "type": "string" }]
|
||||||
|
@ -155,7 +133,7 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"tuple2",
|
"tuple2",
|
||||||
R"""({
|
R"""({
|
||||||
"prefixItems": [{ "type": "string" }, { "type": "number" }]
|
"prefixItems": [{ "type": "string" }, { "type": "number" }]
|
||||||
|
@ -171,7 +149,7 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"number",
|
"number",
|
||||||
R"""({
|
R"""({
|
||||||
"type": "number"
|
"type": "number"
|
||||||
|
@ -182,7 +160,7 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"minItems",
|
"minItems",
|
||||||
R"""({
|
R"""({
|
||||||
"items": {
|
"items": {
|
||||||
|
@ -197,7 +175,7 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"maxItems 1",
|
"maxItems 1",
|
||||||
R"""({
|
R"""({
|
||||||
"items": {
|
"items": {
|
||||||
|
@ -212,7 +190,7 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"maxItems 2",
|
"maxItems 2",
|
||||||
R"""({
|
R"""({
|
||||||
"items": {
|
"items": {
|
||||||
|
@ -227,7 +205,7 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"min + maxItems",
|
"min + maxItems",
|
||||||
R"""({
|
R"""({
|
||||||
"items": {
|
"items": {
|
||||||
|
@ -245,7 +223,7 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"regexp",
|
"regexp",
|
||||||
R"""({
|
R"""({
|
||||||
"type": "string",
|
"type": "string",
|
||||||
|
@ -259,8 +237,8 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"object w/ required props",
|
"required props",
|
||||||
R"""({
|
R"""({
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
@ -290,8 +268,8 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"1 optional",
|
"1 optional prop",
|
||||||
R"""({
|
R"""({
|
||||||
"properties": {
|
"properties": {
|
||||||
"a": {
|
"a": {
|
||||||
|
@ -311,8 +289,8 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"optionals",
|
"N optionals",
|
||||||
R"""({
|
R"""({
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
@ -381,7 +359,7 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"top-level $ref",
|
"top-level $ref",
|
||||||
R"""({
|
R"""({
|
||||||
"$ref": "#/definitions/MyType",
|
"$ref": "#/definitions/MyType",
|
||||||
|
@ -412,7 +390,7 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
|
|
||||||
run_all({
|
run({
|
||||||
"conflicting names",
|
"conflicting names",
|
||||||
R"""({
|
R"""({
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
@ -457,3 +435,20 @@ int main() {
|
||||||
)"""
|
)"""
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
test("Python", [](const TestCase& tc) {
|
||||||
|
write("test-json-schema-input.tmp", tc.schema);
|
||||||
|
assert(std::system("python ./examples/json-schema-to-grammar.py test-json-schema-input.tmp > test-grammar-output.tmp") == 0);
|
||||||
|
tc.verify(read("test-grammar-output.tmp"));
|
||||||
|
});
|
||||||
|
test("JavaScript", [](const TestCase& tc) {
|
||||||
|
write("test-json-schema-input.tmp", tc.schema);
|
||||||
|
assert(std::system("node ./tests/run-json-schema-to-grammar.mjs test-json-schema-input.tmp > test-grammar-output.tmp") == 0);
|
||||||
|
tc.verify(read("test-grammar-output.tmp"));
|
||||||
|
});
|
||||||
|
test("C++", [](const TestCase& tc) {
|
||||||
|
tc.verify(json_schema_to_grammar(nlohmann::json::parse(tc.schema)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue