json: iostream -> fprintf

This commit is contained in:
ochafik 2024-03-21 10:03:51 +00:00
parent b8c0025c6d
commit ad6c4755e0
2 changed files with 9 additions and 15 deletions

View file

@ -1,7 +1,6 @@
#include "json-schema-to-grammar.h" #include "json-schema-to-grammar.h"
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
#include <iostream>
#include <map> #include <map>
#include <regex> #include <regex>
#include <sstream> #include <sstream>
@ -703,7 +702,7 @@ public:
throw std::runtime_error("JSON schema conversion failed:\n" + join(_errors.begin(), _errors.end(), "\n")); throw std::runtime_error("JSON schema conversion failed:\n" + join(_errors.begin(), _errors.end(), "\n"));
} }
if (!_warnings.empty()) { if (!_warnings.empty()) {
std::cerr << "WARNING: JSON schema conversion was incomplete: " + join(_warnings.begin(), _warnings.end(), "; ") << std::endl; fprintf(stderr, "WARNING: JSON schema conversion was incomplete: %s\n", join(_warnings.begin(), _warnings.end(), "; ").c_str());
} }
} }

View file

@ -2,7 +2,6 @@
#undef NDEBUG #undef NDEBUG
#endif #endif
#include <iostream>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <regex> #include <regex>
@ -29,16 +28,12 @@ struct TestCase {
std::string expected_grammar; std::string expected_grammar;
void _print_failure_header() const { void _print_failure_header() const {
std::cerr << "#" << std::endl; fprintf(stderr, "#\n# Test '%s' failed.\n#\n%s\n", name.c_str(), schema.c_str());
std::cerr << "# Test '" << name.c_str() << "' failed." << std::endl;
std::cerr << "#" << std::endl;
std::cerr << schema.c_str() << std::endl;
} }
void verify(const std::string& actual_grammar) const { void verify(const std::string& actual_grammar) const {
if (trim(actual_grammar) != trim(expected_grammar)) { if (trim(actual_grammar) != trim(expected_grammar)) {
_print_failure_header(); _print_failure_header();
std::cerr << "# EXPECTED:\n" << expected_grammar.c_str() << std::endl; fprintf(stderr, "# EXPECTED:\n%s\n# ACTUAL:\n%s\n", expected_grammar.c_str(), actual_grammar.c_str());
std::cerr << "# ACTUAL:\n" << actual_grammar.c_str() << std::endl;
assert(false); assert(false);
} }
} }
@ -50,15 +45,15 @@ struct TestCase {
} }
} catch (const std::runtime_error& ex) { } catch (const std::runtime_error& ex) {
_print_failure_header(); _print_failure_header();
std::cerr << "# GRAMMAR ERROR: " << ex.what() << std::endl; fprintf(stderr, "# GRAMMAR ERROR: %s\n", ex.what());
assert(false); assert(false);
} }
} }
void verify_status(TestCaseStatus status) const { void verify_status(TestCaseStatus status) const {
if (status != expected_status) { if (status != expected_status) {
_print_failure_header(); _print_failure_header();
std::cerr << "# EXPECTED STATUS: " << (expected_status == SUCCESS ? "SUCCESS" : "FAILURE") << std::endl; fprintf(stderr, "# EXPECTED STATUS: %s\n", expected_status == SUCCESS ? "SUCCESS" : "FAILURE");
std::cerr << "# ACTUAL STATUS: " << (status == SUCCESS ? "SUCCESS" : "FAILURE") << std::endl; fprintf(stderr, "# ACTUAL STATUS: %s\n", status == SUCCESS ? "SUCCESS" : "FAILURE");
assert(false); assert(false);
} }
} }
@ -78,9 +73,9 @@ static std::string read(const std::string& file) {
} }
static void test_all(const std::string& lang, std::function<void(const TestCase&)> runner) { static void test_all(const std::string& lang, std::function<void(const TestCase&)> runner) {
std::cerr << "#\n# Testing JSON schema conversion (" << lang.c_str() << ")\n#" << std::endl; fprintf(stderr, "#\n# Testing JSON schema conversion (%s)\n#\n", lang.c_str());
auto test = [&](const TestCase& tc) { auto test = [&](const TestCase& tc) {
std::cerr << "- " << tc.name.c_str() << (tc.expected_status == FAILURE ? " (failure expected)" : "") << std::endl; fprintf(stderr, "- %s%s\n", tc.name.c_str(), tc.expected_status == FAILURE ? " (failure expected)" : "");
runner(tc); runner(tc);
}; };
@ -804,7 +799,7 @@ int main() {
tc.verify(json_schema_to_grammar(nlohmann::json::parse(tc.schema))); tc.verify(json_schema_to_grammar(nlohmann::json::parse(tc.schema)));
tc.verify_status(SUCCESS); tc.verify_status(SUCCESS);
} catch (const std::runtime_error& ex) { } catch (const std::runtime_error& ex) {
std::cerr << "Error: " << ex.what() << std::endl; fprintf(stderr, "Error: %s\n", ex.what());
tc.verify_status(FAILURE); tc.verify_status(FAILURE);
} }
}); });