#include "tests.h" #include "error.h" #include "logger.h" #include #include #include using std::cerr; using std::cout; using std::string; auto test_file(std::istream& tfile, const template_machine& BM) -> bool { std::string cTestCase; std::string cToken; while (tfile) { std::getline(tfile, cTestCase); cout << kblib::quoted(cTestCase); try { auto n = parse_tstring(BM, cTestCase); print_texpr(n, std::cout); std::cout << '\n'; continue; for (const auto& tok : BM.compile(cTestCase)) { auto got = serialize(tok, BM); std::getline(tfile, cToken); log_debug("Expect:\t", cToken); log_debug("Get:\t", got); if (cToken.empty()) { cerr << "Test case failed: (mode 1)" << "\nCase: " << cTestCase << "\nParsed: " << got << "\nExpected: end of tokens\n"; return false; } else if (cToken[0] == 'E') { auto code = std::stoul(cToken.substr(1)); cerr << "Test case failed: (mode 2)" << "\nCase: " << cTestCase << "\nParsed: " << got << "\nExpected: " << format_err(static_cast(code)) << "\n"; return false; } else if (cToken != got) { cerr << "Test case failed: (mode 3)" << "\nCase: " << cTestCase << "\nParsed: " << got << "\nExpected: " << cToken << "\n"; return false; } // success } std::getline(tfile, cToken); if (not cToken.empty()) { cerr << "Test case failed: (mode 4)" << "\nCase: " << cTestCase << "\nParsed: end of tokens" << "\nExpected: " << cToken << "\n"; return false; } cout << "Passed.\n"; } catch (const wordgen_error& e) { std::getline(tfile, cToken); log_debug(kblib::concat('\t', cToken)); if (cToken.empty()) { cerr << "Test case failed: (mode 5)" << "\nCase: " << cTestCase << "\nError: " << format_err(e.code()) << "\nExpected: end of tokens\n"; cerr << "Extra information:\n" << *e.l2 << '\n' << *e.l3 << '\n' << *e.l4 << '\n'; return false; } if (cToken[0] != 'E') { cerr << "Test case failed: (mode 6)" << "\nCase: " << cTestCase << "\nError: " << format_err(e.code()) << "\nExpected: " << cToken << "\n"; cerr << "Extra information:\n" << *e.l2 << '\n' << *e.l3 << '\n' << *e.l4 << '\n'; return false; } else { auto code = std::stoul(cToken.substr(1)); if (code != kblib::etoi(e.code())) { cerr << "Test case failed: (mode 7)" << "\nCase: " << cTestCase << "\nError: " << format_err(e.code()) << "\nExpected: " << format_err(static_cast(code)) << "\n"; cerr << "Extra information:\n" << e.what() << '\n'; return false; } // success std::getline(tfile, cToken); if (not cToken.empty()) { std::cerr << "Malformed test file.\n" << cToken << '\n'; return false; } } cout << "Passed - got expected error.\n"; } } return true; } auto testRE(std::istream& tfile) -> void { std::string line; const auto flag = srell::regex_constants::dotall; while (tfile) { std::getline(tfile, line); try { srell::regex{line, flag}; } catch (srell::regex_error& e) { cerr << "Regex error: " << e.code() << '\n' << "Re: " << kblib::quoted(line) << '\n'; } } } auto check_tokenizer(std::istream& data) -> void { auto err_count = 0; auto lines = 0; while (data) { std::string type, value, regex; data >> type; if (type == "#") { std::getline(data, value); log_debug("#", value); continue; } else if (data) { ++lines; data >> kblib::unformatted_expect('\t') >> kblib::get_line(value, '\t') >> kblib::get_line(regex); try { (void)srell::regex{regex, srell::regex_constants::dotall}; log_debug(type, '\t', value, '\t', regex); } catch (srell::regex_error& e) { ++err_count; log_err("Failed:"); log_err(type, '\t', value, '\t', regex); log_err(e.code(), '\t', e.what()); } } } if (err_count == 0) { log_notice("All checks passed"); } else { log_err(err_count, " out of ", lines, "checks failed"); } }