2017-12-12 22:53:24 +00:00
|
|
|
const fs = require('fs');
|
2020-03-13 23:24:12 +00:00
|
|
|
const path = require('path');
|
2019-02-23 23:08:43 +00:00
|
|
|
const { spawnSync } = require('child_process');
|
2017-12-12 22:53:24 +00:00
|
|
|
|
2018-05-11 22:45:41 +00:00
|
|
|
function getNextStep(content, pos, stop) {
|
|
|
|
while (pos < content.length && content[pos] !== stop &&
|
|
|
|
(content[pos] === ' ' || content[pos] === '\t' || content[pos] === '\n')) {
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
if (pos >= content.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (content[pos] !== stop) {
|
|
|
|
return pos * -1;
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2018-12-18 07:47:56 +00:00
|
|
|
// Stupid function extractor based on indent. Doesn't support block
|
|
|
|
// comments. If someone puts a ' or an " in a block comment this
|
|
|
|
// will blow up. Template strings are not tested and might also be
|
|
|
|
// broken.
|
2018-01-07 15:20:25 +00:00
|
|
|
function extractFunction(content, functionName) {
|
|
|
|
var indent = 0;
|
2018-05-11 22:45:41 +00:00
|
|
|
var splitter = "function " + functionName + "(";
|
2018-01-07 15:20:25 +00:00
|
|
|
|
2018-05-11 22:45:41 +00:00
|
|
|
while (true) {
|
|
|
|
var start = content.indexOf(splitter);
|
|
|
|
if (start === -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var pos = start;
|
|
|
|
while (pos < content.length && content[pos] !== ')') {
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
if (pos >= content.length) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos = getNextStep(content, pos + 1, '{');
|
|
|
|
if (pos === null) {
|
|
|
|
break;
|
|
|
|
} else if (pos < 0) {
|
|
|
|
content = content.slice(-pos);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
while (pos < content.length) {
|
2018-12-18 07:47:56 +00:00
|
|
|
// Eat single-line comments
|
|
|
|
if (content[pos] === '/' && pos > 0 && content[pos-1] === '/') {
|
|
|
|
do {
|
|
|
|
pos += 1;
|
|
|
|
} while (pos < content.length && content[pos] !== '\n');
|
|
|
|
|
|
|
|
// Eat quoted strings
|
|
|
|
} else if (content[pos] === '"' || content[pos] === "'" || content[pos] === "`") {
|
2018-05-11 22:45:41 +00:00
|
|
|
var stop = content[pos];
|
|
|
|
var is_escaped = false;
|
|
|
|
do {
|
|
|
|
if (content[pos] === '\\') {
|
|
|
|
pos += 2;
|
|
|
|
} else {
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
} while (pos < content.length &&
|
|
|
|
(content[pos] !== stop || content[pos - 1] === '\\'));
|
2018-12-18 07:47:56 +00:00
|
|
|
|
|
|
|
// Otherwise, check for indent
|
2018-05-11 22:45:41 +00:00
|
|
|
} else if (content[pos] === '{') {
|
|
|
|
indent += 1;
|
|
|
|
} else if (content[pos] === '}') {
|
|
|
|
indent -= 1;
|
|
|
|
if (indent === 0) {
|
|
|
|
return content.slice(start, pos + 1);
|
|
|
|
}
|
2018-01-07 15:20:25 +00:00
|
|
|
}
|
2018-05-11 22:45:41 +00:00
|
|
|
pos += 1;
|
2018-01-07 15:20:25 +00:00
|
|
|
}
|
2018-05-11 22:45:41 +00:00
|
|
|
content = content.slice(start + 1);
|
2018-01-07 15:20:25 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stupid function extractor for array.
|
|
|
|
function extractArrayVariable(content, arrayName) {
|
2018-05-11 22:45:41 +00:00
|
|
|
var splitter = "var " + arrayName;
|
|
|
|
while (true) {
|
|
|
|
var start = content.indexOf(splitter);
|
|
|
|
if (start === -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var pos = getNextStep(content, start, '=');
|
|
|
|
if (pos === null) {
|
|
|
|
break;
|
|
|
|
} else if (pos < 0) {
|
|
|
|
content = content.slice(-pos);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pos = getNextStep(content, pos, '[');
|
|
|
|
if (pos === null) {
|
|
|
|
break;
|
|
|
|
} else if (pos < 0) {
|
|
|
|
content = content.slice(-pos);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
while (pos < content.length) {
|
|
|
|
if (content[pos] === '"' || content[pos] === "'") {
|
|
|
|
var stop = content[pos];
|
|
|
|
do {
|
|
|
|
if (content[pos] === '\\') {
|
|
|
|
pos += 2;
|
|
|
|
} else {
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
} while (pos < content.length &&
|
|
|
|
(content[pos] !== stop || content[pos - 1] === '\\'));
|
|
|
|
} else if (content[pos] === ']' &&
|
|
|
|
pos + 1 < content.length &&
|
|
|
|
content[pos + 1] === ';') {
|
|
|
|
return content.slice(start, pos + 2);
|
2018-01-07 15:20:25 +00:00
|
|
|
}
|
2018-05-11 22:45:41 +00:00
|
|
|
pos += 1;
|
2018-01-07 15:20:25 +00:00
|
|
|
}
|
2018-05-11 22:45:41 +00:00
|
|
|
content = content.slice(start + 1);
|
2018-01-07 15:20:25 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stupid function extractor for variable.
|
|
|
|
function extractVariable(content, varName) {
|
2018-05-11 22:45:41 +00:00
|
|
|
var splitter = "var " + varName;
|
|
|
|
while (true) {
|
|
|
|
var start = content.indexOf(splitter);
|
|
|
|
if (start === -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var pos = getNextStep(content, start, '=');
|
|
|
|
if (pos === null) {
|
|
|
|
break;
|
|
|
|
} else if (pos < 0) {
|
|
|
|
content = content.slice(-pos);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
while (pos < content.length) {
|
|
|
|
if (content[pos] === '"' || content[pos] === "'") {
|
|
|
|
var stop = content[pos];
|
|
|
|
do {
|
|
|
|
if (content[pos] === '\\') {
|
|
|
|
pos += 2;
|
|
|
|
} else {
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
} while (pos < content.length &&
|
|
|
|
(content[pos] !== stop || content[pos - 1] === '\\'));
|
|
|
|
} else if (content[pos] === ';') {
|
|
|
|
return content.slice(start, pos + 1);
|
2018-01-07 15:20:25 +00:00
|
|
|
}
|
2018-05-11 22:45:41 +00:00
|
|
|
pos += 1;
|
2018-01-07 15:20:25 +00:00
|
|
|
}
|
2018-05-11 22:45:41 +00:00
|
|
|
content = content.slice(start + 1);
|
2018-01-07 15:20:25 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadContent(content) {
|
2017-12-12 22:53:24 +00:00
|
|
|
var Module = module.constructor;
|
|
|
|
var m = new Module();
|
2018-01-07 15:20:25 +00:00
|
|
|
m._compile(content, "tmp.js");
|
2018-12-26 16:00:55 +00:00
|
|
|
m.exports.ignore_order = content.indexOf("\n// ignore-order\n") !== -1 ||
|
|
|
|
content.startsWith("// ignore-order\n");
|
|
|
|
m.exports.exact_check = content.indexOf("\n// exact-check\n") !== -1 ||
|
|
|
|
content.startsWith("// exact-check\n");
|
|
|
|
m.exports.should_fail = content.indexOf("\n// should-fail\n") !== -1 ||
|
|
|
|
content.startsWith("// should-fail\n");
|
2018-01-07 15:20:25 +00:00
|
|
|
return m.exports;
|
|
|
|
}
|
|
|
|
|
|
|
|
function readFile(filePath) {
|
|
|
|
return fs.readFileSync(filePath, 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadThings(thingsToLoad, kindOfLoad, funcToCall, fileContent) {
|
|
|
|
var content = '';
|
|
|
|
for (var i = 0; i < thingsToLoad.length; ++i) {
|
|
|
|
var tmp = funcToCall(fileContent, thingsToLoad[i]);
|
|
|
|
if (tmp === null) {
|
2018-05-11 22:45:41 +00:00
|
|
|
console.error('unable to find ' + kindOfLoad + ' "' + thingsToLoad[i] + '"');
|
2018-01-07 15:20:25 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
content += tmp;
|
|
|
|
content += 'exports.' + thingsToLoad[i] + ' = ' + thingsToLoad[i] + ';';
|
|
|
|
}
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
function lookForEntry(entry, data) {
|
|
|
|
for (var i = 0; i < data.length; ++i) {
|
|
|
|
var allGood = true;
|
|
|
|
for (var key in entry) {
|
|
|
|
if (!entry.hasOwnProperty(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-16 14:38:08 +00:00
|
|
|
var value = data[i][key];
|
2018-01-07 15:20:25 +00:00
|
|
|
// To make our life easier, if there is a "parent" type, we add it to the path.
|
|
|
|
if (key === 'path' && data[i]['parent'] !== undefined) {
|
|
|
|
if (value.length > 0) {
|
|
|
|
value += '::' + data[i]['parent']['name'];
|
|
|
|
} else {
|
|
|
|
value = data[i]['parent']['name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value !== entry[key]) {
|
|
|
|
allGood = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (allGood === true) {
|
2018-04-28 15:21:12 +00:00
|
|
|
return i;
|
2018-01-07 15:20:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-28 15:21:12 +00:00
|
|
|
return null;
|
2018-01-07 15:20:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 23:08:43 +00:00
|
|
|
function load_files(out_folder, crate) {
|
|
|
|
var mainJs = readFile(out_folder + "/main.js");
|
|
|
|
var ALIASES = readFile(out_folder + "/aliases.js");
|
|
|
|
var searchIndex = readFile(out_folder + "/search-index.js").split("\n");
|
2018-01-07 15:20:25 +00:00
|
|
|
if (searchIndex[searchIndex.length - 1].length === 0) {
|
|
|
|
searchIndex.pop();
|
|
|
|
}
|
|
|
|
searchIndex.pop();
|
|
|
|
searchIndex = loadContent(searchIndex.join("\n") + '\nexports.searchIndex = searchIndex;');
|
|
|
|
finalJS = "";
|
|
|
|
|
|
|
|
var arraysToLoad = ["itemTypes"];
|
2018-06-04 19:22:51 +00:00
|
|
|
var variablesToLoad = ["MAX_LEV_DISTANCE", "MAX_RESULTS",
|
2018-06-16 18:44:55 +00:00
|
|
|
"GENERICS_DATA", "NAME", "INPUTS_DATA", "OUTPUT_DATA",
|
2018-06-04 19:22:51 +00:00
|
|
|
"TY_PRIMITIVE", "TY_KEYWORD",
|
|
|
|
"levenshtein_row2"];
|
2018-01-07 15:20:25 +00:00
|
|
|
// execQuery first parameter is built in getQuery (which takes in the search input).
|
|
|
|
// execQuery last parameter is built in buildIndex.
|
|
|
|
// buildIndex requires the hashmap from search-index.
|
2018-05-12 12:57:52 +00:00
|
|
|
var functionsToLoad = ["buildHrefAndPath", "pathSplitter", "levenshtein", "validateResult",
|
|
|
|
"getQuery", "buildIndex", "execQuery", "execSearch"];
|
2018-01-07 15:20:25 +00:00
|
|
|
|
2019-02-23 23:08:43 +00:00
|
|
|
finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
|
2018-05-12 12:57:52 +00:00
|
|
|
finalJS += 'var rootPath = "../";\n';
|
2018-04-19 18:14:24 +00:00
|
|
|
finalJS += ALIASES;
|
2018-01-07 15:20:25 +00:00
|
|
|
finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
|
|
|
|
finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);
|
|
|
|
finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
|
|
|
|
|
|
|
|
var loaded = loadContent(finalJS);
|
2019-02-23 23:08:43 +00:00
|
|
|
return [loaded, loaded.buildIndex(searchIndex.searchIndex)];
|
|
|
|
}
|
|
|
|
|
|
|
|
function main(argv) {
|
2019-02-24 00:04:07 +00:00
|
|
|
if (argv.length < 4) {
|
|
|
|
console.error("USAGE: node tester.js OUT_FOLDER [TESTS]");
|
2019-02-23 23:08:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-02-24 00:04:07 +00:00
|
|
|
if (argv[2].substr(-1) !== "/") {
|
|
|
|
argv[2] += "/";
|
|
|
|
}
|
|
|
|
const out_folder = argv[2];
|
2018-01-07 15:20:25 +00:00
|
|
|
|
|
|
|
var errors = 0;
|
|
|
|
|
2019-02-24 00:04:07 +00:00
|
|
|
for (var j = 3; j < argv.length; ++j) {
|
2020-03-13 23:24:12 +00:00
|
|
|
const test_file = argv[j];
|
|
|
|
const test_name = path.basename(test_file, ".js");
|
2019-02-23 23:08:43 +00:00
|
|
|
|
2019-02-24 00:04:07 +00:00
|
|
|
process.stdout.write('Checking "' + test_name + '" ... ');
|
2020-03-13 23:24:12 +00:00
|
|
|
if (!fs.existsSync(test_file)) {
|
2019-02-23 23:08:43 +00:00
|
|
|
errors += 1;
|
|
|
|
console.error("FAILED");
|
2019-02-24 00:04:07 +00:00
|
|
|
console.error("==> Missing '" + test_name + ".js' file...");
|
|
|
|
continue;
|
2019-02-23 23:08:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 00:04:07 +00:00
|
|
|
const test_out_folder = out_folder + test_name;
|
|
|
|
|
|
|
|
var [loaded, index] = load_files(test_out_folder, test_name);
|
2020-03-13 23:24:12 +00:00
|
|
|
var loadedFile = loadContent(readFile(test_file) +
|
2018-01-07 15:20:25 +00:00
|
|
|
'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;');
|
|
|
|
const expected = loadedFile.EXPECTED;
|
|
|
|
const query = loadedFile.QUERY;
|
2018-09-30 22:55:00 +00:00
|
|
|
const filter_crate = loadedFile.FILTER_CRATE;
|
2018-04-28 15:21:12 +00:00
|
|
|
const ignore_order = loadedFile.ignore_order;
|
2018-05-03 20:54:04 +00:00
|
|
|
const exact_check = loadedFile.exact_check;
|
2018-06-15 21:39:20 +00:00
|
|
|
const should_fail = loadedFile.should_fail;
|
2018-04-14 15:01:28 +00:00
|
|
|
var results = loaded.execSearch(loaded.getQuery(query), index);
|
2018-01-07 15:20:25 +00:00
|
|
|
var error_text = [];
|
|
|
|
for (var key in expected) {
|
|
|
|
if (!expected.hasOwnProperty(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!results.hasOwnProperty(key)) {
|
|
|
|
error_text.push('==> Unknown key "' + key + '"');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var entry = expected[key];
|
2018-05-03 20:54:04 +00:00
|
|
|
var prev_pos = -1;
|
2018-01-07 15:20:25 +00:00
|
|
|
for (var i = 0; i < entry.length; ++i) {
|
2018-04-28 15:21:12 +00:00
|
|
|
var entry_pos = lookForEntry(entry[i], results[key]);
|
|
|
|
if (entry_pos === null) {
|
2018-01-07 15:20:25 +00:00
|
|
|
error_text.push("==> Result not found in '" + key + "': '" +
|
|
|
|
JSON.stringify(entry[i]) + "'");
|
2018-05-03 20:54:04 +00:00
|
|
|
} else if (exact_check === true && prev_pos + 1 !== entry_pos) {
|
|
|
|
error_text.push("==> Exact check failed at position " + (prev_pos + 1) + ": " +
|
|
|
|
"expected '" + JSON.stringify(entry[i]) + "' but found '" +
|
|
|
|
JSON.stringify(results[key][i]) + "'");
|
|
|
|
} else if (ignore_order === false && entry_pos < prev_pos) {
|
2018-04-28 15:21:12 +00:00
|
|
|
error_text.push("==> '" + JSON.stringify(entry[i]) + "' was supposed to be " +
|
|
|
|
" before '" + JSON.stringify(results[key][entry_pos]) + "'");
|
|
|
|
} else {
|
|
|
|
prev_pos = entry_pos;
|
2018-01-07 15:20:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-15 21:39:20 +00:00
|
|
|
if (error_text.length === 0 && should_fail === true) {
|
|
|
|
errors += 1;
|
|
|
|
console.error("FAILED");
|
|
|
|
console.error("==> Test was supposed to fail but all items were found...");
|
|
|
|
} else if (error_text.length !== 0 && should_fail === false) {
|
2018-01-07 15:20:25 +00:00
|
|
|
errors += 1;
|
|
|
|
console.error("FAILED");
|
|
|
|
console.error(error_text.join("\n"));
|
|
|
|
} else {
|
|
|
|
console.log("OK");
|
|
|
|
}
|
2019-02-24 00:04:07 +00:00
|
|
|
}
|
2018-01-07 15:20:25 +00:00
|
|
|
return errors;
|
2017-12-12 22:53:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 15:20:25 +00:00
|
|
|
process.exit(main(process.argv));
|