Extend rustdoc-js tester to allow to test multiple queries in one file
This commit is contained in:
parent
97f3eeec82
commit
d80ac14fe6
1 changed files with 66 additions and 34 deletions
|
@ -181,7 +181,7 @@ function loadThings(thingsToLoad, kindOfLoad, funcToCall, fileContent) {
|
||||||
for (var i = 0; i < thingsToLoad.length; ++i) {
|
for (var i = 0; i < thingsToLoad.length; ++i) {
|
||||||
var tmp = funcToCall(fileContent, thingsToLoad[i]);
|
var tmp = funcToCall(fileContent, thingsToLoad[i]);
|
||||||
if (tmp === null) {
|
if (tmp === null) {
|
||||||
console.error('unable to find ' + kindOfLoad + ' "' + thingsToLoad[i] + '"');
|
console.log('unable to find ' + kindOfLoad + ' "' + thingsToLoad[i] + '"');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
content += tmp;
|
content += tmp;
|
||||||
|
@ -223,7 +223,8 @@ function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
|
||||||
searchIndex.pop();
|
searchIndex.pop();
|
||||||
}
|
}
|
||||||
searchIndex.pop();
|
searchIndex.pop();
|
||||||
searchIndex = loadContent(searchIndex.join("\n") + '\nexports.searchIndex = searchIndex;');
|
var fullSearchIndex = searchIndex.join("\n") + '\nexports.rawSearchIndex = searchIndex;';
|
||||||
|
searchIndex = loadContent(fullSearchIndex);
|
||||||
var finalJS = "";
|
var finalJS = "";
|
||||||
|
|
||||||
var arraysToLoad = ["itemTypes"];
|
var arraysToLoad = ["itemTypes"];
|
||||||
|
@ -235,7 +236,7 @@ function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
|
||||||
// execQuery last parameter is built in buildIndex.
|
// execQuery last parameter is built in buildIndex.
|
||||||
// buildIndex requires the hashmap from search-index.
|
// buildIndex requires the hashmap from search-index.
|
||||||
var functionsToLoad = ["buildHrefAndPath", "pathSplitter", "levenshtein", "validateResult",
|
var functionsToLoad = ["buildHrefAndPath", "pathSplitter", "levenshtein", "validateResult",
|
||||||
"getQuery", "buildIndex", "execQuery", "execSearch"];
|
"handleAliases", "getQuery", "buildIndex", "execQuery", "execSearch"];
|
||||||
|
|
||||||
finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
|
finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
|
||||||
finalJS += 'var rootPath = "../";\n';
|
finalJS += 'var rootPath = "../";\n';
|
||||||
|
@ -245,24 +246,19 @@ function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
|
||||||
finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
|
finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
|
||||||
|
|
||||||
var loaded = loadContent(finalJS);
|
var loaded = loadContent(finalJS);
|
||||||
var index = loaded.buildIndex(searchIndex.searchIndex);
|
var index = loaded.buildIndex(searchIndex.rawSearchIndex);
|
||||||
|
// We make it "global" so that the "loaded.execSearch" function will find it.
|
||||||
|
rawSearchIndex = searchIndex.rawSearchIndex;
|
||||||
|
|
||||||
return [loaded, index];
|
return [loaded, index];
|
||||||
}
|
}
|
||||||
|
|
||||||
function runChecks(testFile, loaded, index) {
|
function runSearch(query, expected, index, loaded, loadedFile, queryName) {
|
||||||
var errors = 0;
|
|
||||||
var loadedFile = loadContent(
|
|
||||||
readFile(testFile) + 'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;');
|
|
||||||
|
|
||||||
const expected = loadedFile.EXPECTED;
|
|
||||||
const query = loadedFile.QUERY;
|
|
||||||
const filter_crate = loadedFile.FILTER_CRATE;
|
const filter_crate = loadedFile.FILTER_CRATE;
|
||||||
const ignore_order = loadedFile.ignore_order;
|
const ignore_order = loadedFile.ignore_order;
|
||||||
const exact_check = loadedFile.exact_check;
|
const exact_check = loadedFile.exact_check;
|
||||||
const should_fail = loadedFile.should_fail;
|
|
||||||
|
|
||||||
var results = loaded.execSearch(loaded.getQuery(query), index);
|
var results = loaded.execSearch(loaded.getQuery(query), index, filter_crate);
|
||||||
var error_text = [];
|
var error_text = [];
|
||||||
|
|
||||||
for (var key in expected) {
|
for (var key in expected) {
|
||||||
|
@ -278,32 +274,68 @@ function runChecks(testFile, loaded, index) {
|
||||||
for (var i = 0; i < entry.length; ++i) {
|
for (var i = 0; i < entry.length; ++i) {
|
||||||
var entry_pos = lookForEntry(entry[i], results[key]);
|
var entry_pos = lookForEntry(entry[i], results[key]);
|
||||||
if (entry_pos === null) {
|
if (entry_pos === null) {
|
||||||
error_text.push("==> Result not found in '" + key + "': '" +
|
error_text.push(queryName + "==> Result not found in '" + key + "': '" +
|
||||||
JSON.stringify(entry[i]) + "'");
|
JSON.stringify(entry[i]) + "'");
|
||||||
} else if (exact_check === true && prev_pos + 1 !== entry_pos) {
|
} else if (exact_check === true && prev_pos + 1 !== entry_pos) {
|
||||||
error_text.push("==> Exact check failed at position " + (prev_pos + 1) + ": " +
|
error_text.push(queryName + "==> Exact check failed at position " + (prev_pos + 1) +
|
||||||
"expected '" + JSON.stringify(entry[i]) + "' but found '" +
|
": expected '" + JSON.stringify(entry[i]) + "' but found '" +
|
||||||
JSON.stringify(results[key][i]) + "'");
|
JSON.stringify(results[key][i]) + "'");
|
||||||
} else if (ignore_order === false && entry_pos < prev_pos) {
|
} else if (ignore_order === false && entry_pos < prev_pos) {
|
||||||
error_text.push("==> '" + JSON.stringify(entry[i]) + "' was supposed to be " +
|
error_text.push(queryName + "==> '" + JSON.stringify(entry[i]) + "' was supposed " +
|
||||||
" before '" + JSON.stringify(results[key][entry_pos]) + "'");
|
"to be before '" + JSON.stringify(results[key][entry_pos]) + "'");
|
||||||
} else {
|
} else {
|
||||||
prev_pos = entry_pos;
|
prev_pos = entry_pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (error_text.length === 0 && should_fail === true) {
|
return error_text;
|
||||||
errors += 1;
|
}
|
||||||
console.error("FAILED");
|
|
||||||
console.error("==> Test was supposed to fail but all items were found...");
|
function checkResult(error_text, loadedFile, displaySuccess) {
|
||||||
} else if (error_text.length !== 0 && should_fail === false) {
|
if (error_text.length === 0 && loadedFile.should_fail === true) {
|
||||||
errors += 1;
|
console.log("FAILED");
|
||||||
console.error("FAILED");
|
console.log("==> Test was supposed to fail but all items were found...");
|
||||||
console.error(error_text.join("\n"));
|
} else if (error_text.length !== 0 && loadedFile.should_fail === false) {
|
||||||
|
console.log("FAILED");
|
||||||
|
console.log(error_text.join("\n"));
|
||||||
} else {
|
} else {
|
||||||
|
if (displaySuccess) {
|
||||||
console.log("OK");
|
console.log("OK");
|
||||||
}
|
}
|
||||||
return errors;
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function runChecks(testFile, loaded, index) {
|
||||||
|
var loadedFile = loadContent(
|
||||||
|
readFile(testFile) + 'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;');
|
||||||
|
|
||||||
|
const expected = loadedFile.EXPECTED;
|
||||||
|
const query = loadedFile.QUERY;
|
||||||
|
|
||||||
|
if (Array.isArray(query)) {
|
||||||
|
if (!Array.isArray(expected)) {
|
||||||
|
console.log("FAILED");
|
||||||
|
console.log("==> If QUERY variable is an array, EXPECTED should be an array too");
|
||||||
|
return 1;
|
||||||
|
} else if (query.length !== expected.length) {
|
||||||
|
console.log("FAILED");
|
||||||
|
console.log("==> QUERY variable should have the same length as EXPECTED");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
for (var i = 0; i < query.length; ++i) {
|
||||||
|
var error_text = runSearch(query[i], expected[i], index, loaded, loadedFile,
|
||||||
|
"[ query `" + query[i] + "`]");
|
||||||
|
if (checkResult(error_text, loadedFile, false) !== 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log("OK");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
var error_text = runSearch(query, expected, index, loaded, loadedFile, "");
|
||||||
|
return checkResult(error_text, loadedFile, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
function load_files(doc_folder, resource_suffix, crate) {
|
function load_files(doc_folder, resource_suffix, crate) {
|
||||||
|
@ -349,7 +381,7 @@ function parseOptions(args) {
|
||||||
|| args[i] === "--crate-name") {
|
|| args[i] === "--crate-name") {
|
||||||
i += 1;
|
i += 1;
|
||||||
if (i >= args.length) {
|
if (i >= args.length) {
|
||||||
console.error("Missing argument after `" + args[i - 1] + "` option.");
|
console.log("Missing argument after `" + args[i - 1] + "` option.");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
opts[correspondances[args[i - 1]]] = args[i];
|
opts[correspondances[args[i - 1]]] = args[i];
|
||||||
|
@ -357,17 +389,17 @@ function parseOptions(args) {
|
||||||
showHelp();
|
showHelp();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
} else {
|
} else {
|
||||||
console.error("Unknown option `" + args[i] + "`.");
|
console.log("Unknown option `" + args[i] + "`.");
|
||||||
console.error("Use `--help` to see the list of options");
|
console.log("Use `--help` to see the list of options");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (opts["doc_folder"].length < 1) {
|
if (opts["doc_folder"].length < 1) {
|
||||||
console.error("Missing `--doc-folder` option.");
|
console.log("Missing `--doc-folder` option.");
|
||||||
} else if (opts["crate_name"].length < 1) {
|
} else if (opts["crate_name"].length < 1) {
|
||||||
console.error("Missing `--crate-name` option.");
|
console.log("Missing `--crate-name` option.");
|
||||||
} else if (opts["test_folder"].length < 1 && opts["test_file"].length < 1) {
|
} else if (opts["test_folder"].length < 1 && opts["test_file"].length < 1) {
|
||||||
console.error("At least one of `--test-folder` or `--test-file` option is required.");
|
console.log("At least one of `--test-folder` or `--test-file` option is required.");
|
||||||
} else {
|
} else {
|
||||||
return opts;
|
return opts;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue