Skip to content

Commit

Permalink
json: pass static command to std::system in tests (fixed temp files)
Browse files Browse the repository at this point in the history
  • Loading branch information
ochafik committed Mar 15, 2024
1 parent f216550 commit 5a7deb2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*.gcda
*.dot
*.bat
*.tmp
*.metallib
.DS_Store
.build/
Expand Down
23 changes: 10 additions & 13 deletions tests/test-json-schema-to-grammar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

using namespace std;

string INPUT_NAME(tmpnam(nullptr));
string OUT_NAME(tmpnam(nullptr));

static std::string trim(const std::string & source) {
std::string s(source);
s.erase(0,s.find_first_not_of(" \n\r\t"));
Expand All @@ -28,16 +25,16 @@ struct TestCase {
string schema;
string expected;

void prepare() const {
void write_input() const {
ofstream f;
f.open(INPUT_NAME);
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(OUT_NAME).rdbuf();
actuals << ifstream("test-grammar-output.tmp").rdbuf();
auto actual = actuals.str();
verify(series, actual);
}
Expand All @@ -58,21 +55,21 @@ struct TestCase {


static void run_py(const TestCase& tc) {
cerr << "# Running Python " << tc.name.c_str() << endl;
tc.prepare();
assert(std::system(("python ./examples/json-schema-to-grammar.py " + INPUT_NAME + " > " + OUT_NAME).c_str()) == 0);
cerr << "Testing JSON schema conversion: " << tc.name.c_str() << " (Python)" << endl;
tc.write_input();
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");
}

static void run_mjs(const TestCase& tc) {
cerr << "# Running MJS " << tc.name.c_str() << endl;
tc.prepare();
assert(std::system(("node ./tests/run-json-schema-to-grammar.mjs " + INPUT_NAME + " > " + OUT_NAME).c_str()) == 0);
cerr << "Testing JSON schema conversion: " << tc.name.c_str() << " (JS)" << endl;
tc.write_input();
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");
}

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));
tc.verify("C++", actual);
}
Expand Down

0 comments on commit 5a7deb2

Please sign in to comment.