json: pass static command to std::system in tests (fixed temp files)

This commit is contained in:
ochafik 2024-03-15 00:03:06 +00:00
parent f2165502c9
commit 5a7deb27d5
2 changed files with 11 additions and 13 deletions

1
.gitignore vendored
View file

@ -11,6 +11,7 @@
*.gcda *.gcda
*.dot *.dot
*.bat *.bat
*.tmp
*.metallib *.metallib
.DS_Store .DS_Store
.build/ .build/

View file

@ -13,9 +13,6 @@
using namespace std; using namespace std;
string INPUT_NAME(tmpnam(nullptr));
string OUT_NAME(tmpnam(nullptr));
static std::string trim(const std::string & source) { static std::string trim(const std::string & source) {
std::string s(source); std::string s(source);
s.erase(0,s.find_first_not_of(" \n\r\t")); s.erase(0,s.find_first_not_of(" \n\r\t"));
@ -28,16 +25,16 @@ struct TestCase {
string schema; string schema;
string expected; string expected;
void prepare() const { void write_input() const {
ofstream f; ofstream f;
f.open(INPUT_NAME); f.open("test-json-schema-input.tmp");
f << schema.c_str(); f << schema.c_str();
f.close(); f.close();
} }
void read_and_verify(const string& series) const { void read_and_verify(const string& series) const {
ostringstream actuals; ostringstream actuals;
actuals << ifstream(OUT_NAME).rdbuf(); actuals << ifstream("test-grammar-output.tmp").rdbuf();
auto actual = actuals.str(); auto actual = actuals.str();
verify(series, actual); verify(series, actual);
} }
@ -58,21 +55,21 @@ struct TestCase {
static void run_py(const TestCase& tc) { static void run_py(const TestCase& tc) {
cerr << "# Running Python " << tc.name.c_str() << endl; cerr << "Testing JSON schema conversion: " << tc.name.c_str() << " (Python)" << endl;
tc.prepare(); tc.write_input();
assert(std::system(("python ./examples/json-schema-to-grammar.py " + INPUT_NAME + " > " + OUT_NAME).c_str()) == 0); assert(std::system("python ./examples/json-schema-to-grammar.py test-json-schema-input.tmp > test-grammar-output.tmp") == 0);
tc.read_and_verify("Python"); tc.read_and_verify("Python");
} }
static void run_mjs(const TestCase& tc) { static void run_mjs(const TestCase& tc) {
cerr << "# Running MJS " << tc.name.c_str() << endl; cerr << "Testing JSON schema conversion: " << tc.name.c_str() << " (JS)" << endl;
tc.prepare(); tc.write_input();
assert(std::system(("node ./tests/run-json-schema-to-grammar.mjs " + INPUT_NAME + " > " + OUT_NAME).c_str()) == 0); assert(std::system("node ./tests/run-json-schema-to-grammar.mjs test-json-schema-input.tmp > test-grammar-output.tmp") == 0);
tc.read_and_verify("JavaScript"); tc.read_and_verify("JavaScript");
} }
static void run_cpp(const TestCase& tc) { static void run_cpp(const TestCase& tc) {
cerr << "# Running C++ " << tc.name.c_str() << endl; cerr << "Testing JSON schema conversion: " << tc.name.c_str() << " (C++)" << endl;
auto actual = json_schema_to_grammar(nlohmann::json::parse(tc.schema)); auto actual = json_schema_to_grammar(nlohmann::json::parse(tc.schema));
tc.verify("C++", actual); tc.verify("C++", actual);
} }