1 const fs = require('fs');
2 const { spawnSync } = require('child_process');
4 const TEST_FOLDER = 'src/test/rustdoc-js/';
6 function getNextStep(content, pos, stop) {
7 while (pos < content.length && content[pos] !== stop &&
8 (content[pos] === ' ' || content[pos] === '\t' || content[pos] === '\n')) {
11 if (pos >= content.length) {
14 if (content[pos] !== stop) {
20 // Stupid function extractor based on indent. Doesn't support block
21 // comments. If someone puts a ' or an " in a block comment this
22 // will blow up. Template strings are not tested and might also be
24 function extractFunction(content, functionName) {
26 var splitter = "function " + functionName + "(";
29 var start = content.indexOf(splitter);
34 while (pos < content.length && content[pos] !== ')') {
37 if (pos >= content.length) {
40 pos = getNextStep(content, pos + 1, '{');
44 content = content.slice(-pos);
47 while (pos < content.length) {
48 // Eat single-line comments
49 if (content[pos] === '/' && pos > 0 && content[pos-1] === '/') {
52 } while (pos < content.length && content[pos] !== '\n');
55 } else if (content[pos] === '"' || content[pos] === "'" || content[pos] === "`") {
56 var stop = content[pos];
57 var is_escaped = false;
59 if (content[pos] === '\\') {
64 } while (pos < content.length &&
65 (content[pos] !== stop || content[pos - 1] === '\\'));
67 // Otherwise, check for indent
68 } else if (content[pos] === '{') {
70 } else if (content[pos] === '}') {
73 return content.slice(start, pos + 1);
78 content = content.slice(start + 1);
83 // Stupid function extractor for array.
84 function extractArrayVariable(content, arrayName) {
85 var splitter = "var " + arrayName;
87 var start = content.indexOf(splitter);
91 var pos = getNextStep(content, start, '=');
95 content = content.slice(-pos);
98 pos = getNextStep(content, pos, '[');
101 } else if (pos < 0) {
102 content = content.slice(-pos);
105 while (pos < content.length) {
106 if (content[pos] === '"' || content[pos] === "'") {
107 var stop = content[pos];
109 if (content[pos] === '\\') {
114 } while (pos < content.length &&
115 (content[pos] !== stop || content[pos - 1] === '\\'));
116 } else if (content[pos] === ']' &&
117 pos + 1 < content.length &&
118 content[pos + 1] === ';') {
119 return content.slice(start, pos + 2);
123 content = content.slice(start + 1);
128 // Stupid function extractor for variable.
129 function extractVariable(content, varName) {
130 var splitter = "var " + varName;
132 var start = content.indexOf(splitter);
136 var pos = getNextStep(content, start, '=');
139 } else if (pos < 0) {
140 content = content.slice(-pos);
143 while (pos < content.length) {
144 if (content[pos] === '"' || content[pos] === "'") {
145 var stop = content[pos];
147 if (content[pos] === '\\') {
152 } while (pos < content.length &&
153 (content[pos] !== stop || content[pos - 1] === '\\'));
154 } else if (content[pos] === ';') {
155 return content.slice(start, pos + 1);
159 content = content.slice(start + 1);
164 function loadContent(content) {
165 var Module = module.constructor;
166 var m = new Module();
167 m._compile(content, "tmp.js");
168 m.exports.ignore_order = content.indexOf("\n// ignore-order\n") !== -1 ||
169 content.startsWith("// ignore-order\n");
170 m.exports.exact_check = content.indexOf("\n// exact-check\n") !== -1 ||
171 content.startsWith("// exact-check\n");
172 m.exports.should_fail = content.indexOf("\n// should-fail\n") !== -1 ||
173 content.startsWith("// should-fail\n");
177 function readFile(filePath) {
178 return fs.readFileSync(filePath, 'utf8');
181 function loadThings(thingsToLoad, kindOfLoad, funcToCall, fileContent) {
183 for (var i = 0; i < thingsToLoad.length; ++i) {
184 var tmp = funcToCall(fileContent, thingsToLoad[i]);
186 console.error('unable to find ' + kindOfLoad + ' "' + thingsToLoad[i] + '"');
190 content += 'exports.' + thingsToLoad[i] + ' = ' + thingsToLoad[i] + ';';
195 function lookForEntry(entry, data) {
196 for (var i = 0; i < data.length; ++i) {
198 for (var key in entry) {
199 if (!entry.hasOwnProperty(key)) {
202 var value = data[i][key];
203 // To make our life easier, if there is a "parent" type, we add it to the path.
204 if (key === 'path' && data[i]['parent'] !== undefined) {
205 if (value.length > 0) {
206 value += '::' + data[i]['parent']['name'];
208 value = data[i]['parent']['name'];
211 if (value !== entry[key]) {
216 if (allGood === true) {
223 function load_files(out_folder, crate) {
224 var mainJs = readFile(out_folder + "/main.js");
225 var ALIASES = readFile(out_folder + "/aliases.js");
226 var searchIndex = readFile(out_folder + "/search-index.js").split("\n");
227 if (searchIndex[searchIndex.length - 1].length === 0) {
231 searchIndex = loadContent(searchIndex.join("\n") + '\nexports.searchIndex = searchIndex;');
234 var arraysToLoad = ["itemTypes"];
235 var variablesToLoad = ["MAX_LEV_DISTANCE", "MAX_RESULTS",
236 "GENERICS_DATA", "NAME", "INPUTS_DATA", "OUTPUT_DATA",
237 "TY_PRIMITIVE", "TY_KEYWORD",
239 // execQuery first parameter is built in getQuery (which takes in the search input).
240 // execQuery last parameter is built in buildIndex.
241 // buildIndex requires the hashmap from search-index.
242 var functionsToLoad = ["buildHrefAndPath", "pathSplitter", "levenshtein", "validateResult",
243 "getQuery", "buildIndex", "execQuery", "execSearch"];
245 finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
246 finalJS += 'var rootPath = "../";\n';
248 finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
249 finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);
250 finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
252 var loaded = loadContent(finalJS);
253 return [loaded, loaded.buildIndex(searchIndex.searchIndex)];
256 function main(argv) {
257 if (argv.length < 4) {
258 console.error("USAGE: node tester.js OUT_FOLDER [TESTS]");
261 if (argv[2].substr(-1) !== "/") {
264 const out_folder = argv[2];
268 for (var j = 3; j < argv.length; ++j) {
269 const test_name = argv[j];
271 process.stdout.write('Checking "' + test_name + '" ... ');
272 if (!fs.existsSync(TEST_FOLDER + test_name + ".js")) {
274 console.error("FAILED");
275 console.error("==> Missing '" + test_name + ".js' file...");
279 const test_out_folder = out_folder + test_name;
281 var [loaded, index] = load_files(test_out_folder, test_name);
282 var loadedFile = loadContent(readFile(TEST_FOLDER + test_name + ".js") +
283 'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;');
284 const expected = loadedFile.EXPECTED;
285 const query = loadedFile.QUERY;
286 const filter_crate = loadedFile.FILTER_CRATE;
287 const ignore_order = loadedFile.ignore_order;
288 const exact_check = loadedFile.exact_check;
289 const should_fail = loadedFile.should_fail;
290 var results = loaded.execSearch(loaded.getQuery(query), index);
292 for (var key in expected) {
293 if (!expected.hasOwnProperty(key)) {
296 if (!results.hasOwnProperty(key)) {
297 error_text.push('==> Unknown key "' + key + '"');
300 var entry = expected[key];
302 for (var i = 0; i < entry.length; ++i) {
303 var entry_pos = lookForEntry(entry[i], results[key]);
304 if (entry_pos === null) {
305 error_text.push("==> Result not found in '" + key + "': '" +
306 JSON.stringify(entry[i]) + "'");
307 } else if (exact_check === true && prev_pos + 1 !== entry_pos) {
308 error_text.push("==> Exact check failed at position " + (prev_pos + 1) + ": " +
309 "expected '" + JSON.stringify(entry[i]) + "' but found '" +
310 JSON.stringify(results[key][i]) + "'");
311 } else if (ignore_order === false && entry_pos < prev_pos) {
312 error_text.push("==> '" + JSON.stringify(entry[i]) + "' was supposed to be " +
313 " before '" + JSON.stringify(results[key][entry_pos]) + "'");
315 prev_pos = entry_pos;
319 if (error_text.length === 0 && should_fail === true) {
321 console.error("FAILED");
322 console.error("==> Test was supposed to fail but all items were found...");
323 } else if (error_text.length !== 0 && should_fail === false) {
325 console.error("FAILED");
326 console.error(error_text.join("\n"));
334 process.exit(main(process.argv));