Read the queries from all files that were supplied to the test through the -f argument. For all queries it is checked whether they can be parsed successfully.
| 15 | // through the -f argument. For all queries it is checked whether they |
| 16 | // can be parsed successfully. |
| 17 | TEST(AutoQueryFileTest) { |
| 18 | const std::vector<std::string>& args = mt::Runtime::args(); |
| 19 | |
| 20 | std::vector<std::string> query_files; |
| 21 | |
| 22 | // Parse command line arguments to retrieve query files. |
| 23 | uint i = 1; |
| 24 | for (; i < args.size(); ++i) { |
| 25 | if (args[i] == "-f") { |
| 26 | query_files.push_back(args[++i]); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // Read list of queries from all input files. |
| 31 | std::vector<std::string> lines; |
| 32 | for (std::string path : query_files) { |
| 33 | std::vector<std::string> tmp = readlines(path); |
| 34 | lines.insert(lines.end(), tmp.begin(), tmp.end()); |
| 35 | } |
| 36 | |
| 37 | // Execute queries. |
| 38 | size_t num_executed = 0; |
| 39 | size_t num_failed = 0; |
| 40 | for (std::string line : lines) { |
| 41 | bool expected_result = true; |
| 42 | std::string query = line; |
| 43 | |
| 44 | // If a line starts with '!' parsing is expected to fail. |
| 45 | if (query.at(0) == '!') { |
| 46 | expected_result = false; |
| 47 | query = query.substr(1); |
| 48 | } |
| 49 | |
| 50 | // Measuring the parsing time. |
| 51 | std::chrono::time_point<std::chrono::system_clock> start, end; |
| 52 | start = std::chrono::system_clock::now(); |
| 53 | |
| 54 | // Parse the query. |
| 55 | hsql::SQLParserResult result; |
| 56 | hsql::SQLParser::parse(query, &result); |
| 57 | |
| 58 | end = std::chrono::system_clock::now(); |
| 59 | std::chrono::duration<double> elapsed_seconds = end - start; |
| 60 | double us = elapsed_seconds.count() * 1000 * 1000; |
| 61 | |
| 62 | if (expected_result == result.isValid()) { |
| 63 | printf("\033[0;32m{ ok} (%.1fus)\033[0m %s\n", us, line.c_str()); |
| 64 | } else { |
| 65 | printf("\033[0;31m{ failed}\033[0m\n"); |
| 66 | printf("\t\033[0;31m%s (L%d:%d)\n\033[0m", result.errorMsg(), result.errorLine(), result.errorColumn()); |
| 67 | printf("\t%s\n", line.c_str()); |
| 68 | ++num_failed; |
| 69 | } |
| 70 | ++num_executed; |
| 71 | } |
| 72 | |
| 73 | if (num_failed == 0) { |
| 74 | printf("\033[0;32m{ ok} \033[0mAll %lu grammar tests completed successfully!\n", num_executed); |
nothing calls this directly
no test coverage detected