2014-11-03 23:26:44 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string>
|
|
|
|
#include <chrono>
|
2014-11-12 10:43:10 +01:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
2017-02-10 21:41:34 +01:00
|
|
|
|
|
|
|
#include "thirdparty/microtest/microtest.h"
|
2014-11-03 23:26:44 +01:00
|
|
|
#include "SQLParser.h"
|
|
|
|
|
|
|
|
using namespace hsql;
|
|
|
|
|
2014-11-12 10:43:10 +01:00
|
|
|
|
|
|
|
std::vector<std::string> readlines(std::string path) {
|
2017-03-07 02:01:00 +01:00
|
|
|
std::ifstream infile(path);
|
|
|
|
std::vector<std::string> lines;
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(infile, line)) {
|
|
|
|
std::istringstream iss(line);
|
|
|
|
|
|
|
|
// Skip comments
|
|
|
|
if (line[0] == '#' ||
|
|
|
|
(line[0] == '-' && line[1] == '-')) {
|
|
|
|
continue;
|
2014-11-12 10:43:10 +01:00
|
|
|
}
|
2017-03-07 02:01:00 +01:00
|
|
|
|
|
|
|
lines.push_back(line);
|
|
|
|
}
|
|
|
|
return lines;
|
2014-11-12 10:43:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define STREQ(a, b) (std::string(a).compare(std::string(b)) == 0)
|
|
|
|
|
2017-02-10 21:41:34 +01:00
|
|
|
TEST(AutoGrammarTest) {
|
2017-03-07 02:01:00 +01:00
|
|
|
const std::vector<std::string>& args = mt::Runtime::args();
|
|
|
|
if (args.size() <= 1) {
|
|
|
|
fprintf(stderr, "Usage: grammar_test [--false] [-f path] query, ...\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool globalExpectFalse = false;
|
|
|
|
bool useFile = false;
|
|
|
|
std::string filePath = "";
|
|
|
|
|
|
|
|
// Parse command line arguments
|
|
|
|
uint i = 1;
|
|
|
|
for (; i < args.size(); ++i) {
|
|
|
|
if (STREQ(args[i], "--false")) globalExpectFalse = true;
|
|
|
|
else if (STREQ(args[i], "-f")) {
|
|
|
|
useFile = true;
|
|
|
|
filePath = args[++i];
|
|
|
|
} else {
|
|
|
|
break;
|
2014-11-03 23:26:44 +01:00
|
|
|
}
|
2017-03-07 02:01:00 +01:00
|
|
|
}
|
2014-11-03 23:26:44 +01:00
|
|
|
|
2014-11-12 10:43:10 +01:00
|
|
|
|
2017-03-07 02:01:00 +01:00
|
|
|
// Read list of queries for this rest
|
|
|
|
std::vector<std::string> queries;
|
|
|
|
if (useFile) {
|
|
|
|
queries = readlines(filePath);
|
|
|
|
} else {
|
|
|
|
for (; i < args.size(); ++i) queries.push_back(args[i]);
|
|
|
|
}
|
2014-11-12 10:43:10 +01:00
|
|
|
|
2017-03-07 02:01:00 +01:00
|
|
|
|
|
|
|
// Execute queries
|
|
|
|
int numFailed = 0;
|
|
|
|
for (std::string sql : queries) {
|
|
|
|
bool expectFalse = globalExpectFalse;
|
|
|
|
if (sql.at(0) == '!') {
|
|
|
|
expectFalse = !expectFalse;
|
|
|
|
sql = sql.substr(1);
|
2014-11-12 10:43:10 +01:00
|
|
|
}
|
2014-11-03 23:57:42 +01:00
|
|
|
|
2017-03-07 02:01:00 +01:00
|
|
|
// Measuring the parsing time
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> start, end;
|
|
|
|
start = std::chrono::system_clock::now();
|
2014-11-03 23:26:44 +01:00
|
|
|
|
2017-03-07 02:01:00 +01:00
|
|
|
// Parsing
|
2017-04-07 16:16:25 +02:00
|
|
|
SQLParserResult result;
|
|
|
|
SQLParser::parseSQLString(sql.c_str(), &result);
|
2017-03-07 02:01:00 +01:00
|
|
|
|
|
|
|
end = std::chrono::system_clock::now();
|
|
|
|
std::chrono::duration<double> elapsed_seconds = end - start;
|
|
|
|
double us = elapsed_seconds.count() * 1000 * 1000;
|
2014-11-03 23:26:44 +01:00
|
|
|
|
2017-04-07 16:16:25 +02:00
|
|
|
if (expectFalse == result.isValid()) {
|
2017-03-07 02:01:00 +01:00
|
|
|
printf("\033[0;31m{ failed}\033[0m\n");
|
2017-04-07 16:16:25 +02:00
|
|
|
printf("\t\033[0;31m%s (L%d:%d)\n\033[0m", result.errorMsg(), result.errorLine(), result.errorColumn());
|
2017-03-07 02:01:00 +01:00
|
|
|
printf("\t%s\n", sql.c_str());
|
|
|
|
numFailed++;
|
2014-11-12 10:43:10 +01:00
|
|
|
} else {
|
2017-03-07 02:01:00 +01:00
|
|
|
// TODO: indicate whether expectFalse was set
|
|
|
|
printf("\033[0;32m{ ok} (%.1fus)\033[0m %s\n", us, sql.c_str());
|
2014-11-12 10:43:10 +01:00
|
|
|
}
|
2017-03-07 02:01:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (numFailed == 0) {
|
|
|
|
printf("\033[0;32m{ ok} \033[0mAll %lu grammar tests completed successfully!\n", queries.size());
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "\033[0;31m{ failed} \033[0mSome grammar tests failed! %d out of %lu tests failed!\n", numFailed, queries.size());
|
|
|
|
}
|
|
|
|
ASSERT_EQ(numFailed, 0);
|
2017-02-10 21:41:34 +01:00
|
|
|
}
|
|
|
|
|