Skip to content

Commit d80ac14

Browse files
Extend rustdoc-js tester to allow to test multiple queries in one file
1 parent 97f3eee commit d80ac14

File tree

1 file changed

+65
-33
lines changed

1 file changed

+65
-33
lines changed

src/tools/rustdoc-js/tester.js

+65-33
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ function loadThings(thingsToLoad, kindOfLoad, funcToCall, fileContent) {
181181
for (var i = 0; i < thingsToLoad.length; ++i) {
182182
var tmp = funcToCall(fileContent, thingsToLoad[i]);
183183
if (tmp === null) {
184-
console.error('unable to find ' + kindOfLoad + ' "' + thingsToLoad[i] + '"');
184+
console.log('unable to find ' + kindOfLoad + ' "' + thingsToLoad[i] + '"');
185185
process.exit(1);
186186
}
187187
content += tmp;
@@ -223,7 +223,8 @@ function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
223223
searchIndex.pop();
224224
}
225225
searchIndex.pop();
226-
searchIndex = loadContent(searchIndex.join("\n") + '\nexports.searchIndex = searchIndex;');
226+
var fullSearchIndex = searchIndex.join("\n") + '\nexports.rawSearchIndex = searchIndex;';
227+
searchIndex = loadContent(fullSearchIndex);
227228
var finalJS = "";
228229

229230
var arraysToLoad = ["itemTypes"];
@@ -235,7 +236,7 @@ function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
235236
// execQuery last parameter is built in buildIndex.
236237
// buildIndex requires the hashmap from search-index.
237238
var functionsToLoad = ["buildHrefAndPath", "pathSplitter", "levenshtein", "validateResult",
238-
"getQuery", "buildIndex", "execQuery", "execSearch"];
239+
"handleAliases", "getQuery", "buildIndex", "execQuery", "execSearch"];
239240

240241
finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
241242
finalJS += 'var rootPath = "../";\n';
@@ -245,24 +246,19 @@ function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
245246
finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
246247

247248
var loaded = loadContent(finalJS);
248-
var index = loaded.buildIndex(searchIndex.searchIndex);
249+
var index = loaded.buildIndex(searchIndex.rawSearchIndex);
250+
// We make it "global" so that the "loaded.execSearch" function will find it.
251+
rawSearchIndex = searchIndex.rawSearchIndex;
249252

250253
return [loaded, index];
251254
}
252255

253-
function runChecks(testFile, loaded, index) {
254-
var errors = 0;
255-
var loadedFile = loadContent(
256-
readFile(testFile) + 'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;');
257-
258-
const expected = loadedFile.EXPECTED;
259-
const query = loadedFile.QUERY;
256+
function runSearch(query, expected, index, loaded, loadedFile, queryName) {
260257
const filter_crate = loadedFile.FILTER_CRATE;
261258
const ignore_order = loadedFile.ignore_order;
262259
const exact_check = loadedFile.exact_check;
263-
const should_fail = loadedFile.should_fail;
264260

265-
var results = loaded.execSearch(loaded.getQuery(query), index);
261+
var results = loaded.execSearch(loaded.getQuery(query), index, filter_crate);
266262
var error_text = [];
267263

268264
for (var key in expected) {
@@ -278,32 +274,68 @@ function runChecks(testFile, loaded, index) {
278274
for (var i = 0; i < entry.length; ++i) {
279275
var entry_pos = lookForEntry(entry[i], results[key]);
280276
if (entry_pos === null) {
281-
error_text.push("==> Result not found in '" + key + "': '" +
277+
error_text.push(queryName + "==> Result not found in '" + key + "': '" +
282278
JSON.stringify(entry[i]) + "'");
283279
} else if (exact_check === true && prev_pos + 1 !== entry_pos) {
284-
error_text.push("==> Exact check failed at position " + (prev_pos + 1) + ": " +
285-
"expected '" + JSON.stringify(entry[i]) + "' but found '" +
280+
error_text.push(queryName + "==> Exact check failed at position " + (prev_pos + 1) +
281+
": expected '" + JSON.stringify(entry[i]) + "' but found '" +
286282
JSON.stringify(results[key][i]) + "'");
287283
} else if (ignore_order === false && entry_pos < prev_pos) {
288-
error_text.push("==> '" + JSON.stringify(entry[i]) + "' was supposed to be " +
289-
" before '" + JSON.stringify(results[key][entry_pos]) + "'");
284+
error_text.push(queryName + "==> '" + JSON.stringify(entry[i]) + "' was supposed " +
285+
"to be before '" + JSON.stringify(results[key][entry_pos]) + "'");
290286
} else {
291287
prev_pos = entry_pos;
292288
}
293289
}
294290
}
295-
if (error_text.length === 0 && should_fail === true) {
296-
errors += 1;
297-
console.error("FAILED");
298-
console.error("==> Test was supposed to fail but all items were found...");
299-
} else if (error_text.length !== 0 && should_fail === false) {
300-
errors += 1;
301-
console.error("FAILED");
302-
console.error(error_text.join("\n"));
291+
return error_text;
292+
}
293+
294+
function checkResult(error_text, loadedFile, displaySuccess) {
295+
if (error_text.length === 0 && loadedFile.should_fail === true) {
296+
console.log("FAILED");
297+
console.log("==> Test was supposed to fail but all items were found...");
298+
} else if (error_text.length !== 0 && loadedFile.should_fail === false) {
299+
console.log("FAILED");
300+
console.log(error_text.join("\n"));
303301
} else {
302+
if (displaySuccess) {
303+
console.log("OK");
304+
}
305+
return 0;
306+
}
307+
return 1;
308+
}
309+
310+
function runChecks(testFile, loaded, index) {
311+
var loadedFile = loadContent(
312+
readFile(testFile) + 'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;');
313+
314+
const expected = loadedFile.EXPECTED;
315+
const query = loadedFile.QUERY;
316+
317+
if (Array.isArray(query)) {
318+
if (!Array.isArray(expected)) {
319+
console.log("FAILED");
320+
console.log("==> If QUERY variable is an array, EXPECTED should be an array too");
321+
return 1;
322+
} else if (query.length !== expected.length) {
323+
console.log("FAILED");
324+
console.log("==> QUERY variable should have the same length as EXPECTED");
325+
return 1;
326+
}
327+
for (var i = 0; i < query.length; ++i) {
328+
var error_text = runSearch(query[i], expected[i], index, loaded, loadedFile,
329+
"[ query `" + query[i] + "`]");
330+
if (checkResult(error_text, loadedFile, false) !== 0) {
331+
return 1;
332+
}
333+
}
304334
console.log("OK");
335+
return 0;
305336
}
306-
return errors;
337+
var error_text = runSearch(query, expected, index, loaded, loadedFile, "");
338+
return checkResult(error_text, loadedFile, true);
307339
}
308340

309341
function load_files(doc_folder, resource_suffix, crate) {
@@ -349,25 +381,25 @@ function parseOptions(args) {
349381
|| args[i] === "--crate-name") {
350382
i += 1;
351383
if (i >= args.length) {
352-
console.error("Missing argument after `" + args[i - 1] + "` option.");
384+
console.log("Missing argument after `" + args[i - 1] + "` option.");
353385
return null;
354386
}
355387
opts[correspondances[args[i - 1]]] = args[i];
356388
} else if (args[i] === "--help") {
357389
showHelp();
358390
process.exit(0);
359391
} else {
360-
console.error("Unknown option `" + args[i] + "`.");
361-
console.error("Use `--help` to see the list of options");
392+
console.log("Unknown option `" + args[i] + "`.");
393+
console.log("Use `--help` to see the list of options");
362394
return null;
363395
}
364396
}
365397
if (opts["doc_folder"].length < 1) {
366-
console.error("Missing `--doc-folder` option.");
398+
console.log("Missing `--doc-folder` option.");
367399
} else if (opts["crate_name"].length < 1) {
368-
console.error("Missing `--crate-name` option.");
400+
console.log("Missing `--crate-name` option.");
369401
} else if (opts["test_folder"].length < 1 && opts["test_file"].length < 1) {
370-
console.error("At least one of `--test-folder` or `--test-file` option is required.");
402+
console.log("At least one of `--test-folder` or `--test-file` option is required.");
371403
} else {
372404
return opts;
373405
}

0 commit comments

Comments
 (0)