Skip to content

Commit 299929e

Browse files
authored
Rollup merge of #112468 - GuillaumeGomez:change-rustdoc-js-formats, r=notriddle
Change format of rustdoc-js tests by putting query and correction directly alongside the expected values As I was working on fixing merge conflicts in #108537, I faced quite a big issue when trying to update the `rustdoc-js*` tests. To make it much simpler, this PR moves the `query` and `correction` directly alongside the expected data so now we know what is the query that is being run without needing to add comments or going back to the top of the file. r? ```@notriddle```
2 parents 4d36c84 + 9803651 commit 299929e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+285
-441
lines changed

src/tools/rustdoc-js/tester.js

+49-41
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ function contentToDiffLine(key, value) {
2222
return `"${key}": "${value}",`;
2323
}
2424

25+
function shouldIgnoreField(fieldName) {
26+
return fieldName === "query" || fieldName === "correction";
27+
}
28+
2529
// This function is only called when no matching result was found and therefore will only display
2630
// the diff between the two items.
2731
function betterLookingDiff(entry, data) {
@@ -135,6 +139,9 @@ function valueCheck(fullPath, expected, result, error_text, queryName) {
135139
} else if (expected !== null && typeof expected !== "undefined" &&
136140
expected.constructor == Object) { // eslint-disable-line eqeqeq
137141
for (const key in expected) {
142+
if (shouldIgnoreField(key)) {
143+
continue;
144+
}
138145
if (!Object.prototype.hasOwnProperty.call(expected, key)) {
139146
continue;
140147
}
@@ -184,6 +191,9 @@ function runSearch(query, expected, doSearch, loadedFile, queryName) {
184191
const error_text = [];
185192

186193
for (const key in expected) {
194+
if (shouldIgnoreField(key)) {
195+
continue;
196+
}
187197
if (!Object.prototype.hasOwnProperty.call(expected, key)) {
188198
continue;
189199
}
@@ -260,84 +270,83 @@ function checkResult(error_text, loadedFile, displaySuccess) {
260270
return 1;
261271
}
262272

263-
function runCheck(loadedFile, key, callback) {
264-
const expected = loadedFile[key];
265-
const query = loadedFile.QUERY;
266-
267-
if (Array.isArray(query)) {
268-
if (!Array.isArray(expected)) {
269-
console.log("FAILED");
270-
console.log(`==> If QUERY variable is an array, ${key} should be an array too`);
271-
return 1;
272-
} else if (query.length !== expected.length) {
273-
console.log("FAILED");
274-
console.log(`==> QUERY variable should have the same length as ${key}`);
275-
return 1;
273+
function runCheckInner(callback, loadedFile, entry, getCorrections, extra) {
274+
if (typeof entry.query !== "string") {
275+
console.log("FAILED");
276+
console.log("==> Missing `query` field");
277+
return false;
278+
}
279+
let error_text = callback(entry.query, entry, extra ? "[ query `" + entry.query + "`]" : "");
280+
if (checkResult(error_text, loadedFile, false) !== 0) {
281+
return false;
282+
}
283+
if (entry.correction !== undefined) {
284+
error_text = runCorrections(entry.query, entry.correction, getCorrections, loadedFile);
285+
if (checkResult(error_text, loadedFile, false) !== 0) {
286+
return false;
276287
}
277-
for (let i = 0; i < query.length; ++i) {
278-
const error_text = callback(query[i], expected[i], "[ query `" + query[i] + "`]");
279-
if (checkResult(error_text, loadedFile, false) !== 0) {
288+
}
289+
return true;
290+
}
291+
292+
function runCheck(loadedFile, key, getCorrections, callback) {
293+
const expected = loadedFile[key];
294+
295+
if (Array.isArray(expected)) {
296+
for (const entry of expected) {
297+
if (!runCheckInner(callback, loadedFile, entry, getCorrections, true)) {
280298
return 1;
281299
}
282300
}
283-
console.log("OK");
284-
} else {
285-
const error_text = callback(query, expected, "");
286-
if (checkResult(error_text, loadedFile, true) !== 0) {
287-
return 1;
288-
}
301+
} else if (!runCheckInner(callback, loadedFile, expected, getCorrections, false)) {
302+
return 1;
289303
}
304+
console.log("OK");
290305
return 0;
291306
}
292307

308+
function hasCheck(content, checkName) {
309+
return content.startsWith(`const ${checkName}`) || content.includes(`\nconst ${checkName}`);
310+
}
311+
293312
function runChecks(testFile, doSearch, parseQuery, getCorrections) {
294313
let checkExpected = false;
295314
let checkParsed = false;
296-
let checkCorrections = false;
297-
let testFileContent = readFile(testFile) + "exports.QUERY = QUERY;";
315+
let testFileContent = readFile(testFile);
298316

299317
if (testFileContent.indexOf("FILTER_CRATE") !== -1) {
300318
testFileContent += "exports.FILTER_CRATE = FILTER_CRATE;";
301319
} else {
302320
testFileContent += "exports.FILTER_CRATE = null;";
303321
}
304322

305-
if (testFileContent.indexOf("\nconst EXPECTED") !== -1) {
323+
if (hasCheck(testFileContent, "EXPECTED")) {
306324
testFileContent += "exports.EXPECTED = EXPECTED;";
307325
checkExpected = true;
308326
}
309-
if (testFileContent.indexOf("\nconst PARSED") !== -1) {
327+
if (hasCheck(testFileContent, "PARSED")) {
310328
testFileContent += "exports.PARSED = PARSED;";
311329
checkParsed = true;
312330
}
313-
if (testFileContent.indexOf("\nconst CORRECTIONS") !== -1) {
314-
testFileContent += "exports.CORRECTIONS = CORRECTIONS;";
315-
checkCorrections = true;
316-
}
317-
if (!checkParsed && !checkExpected && !checkCorrections) {
331+
if (!checkParsed && !checkExpected) {
318332
console.log("FAILED");
319-
console.log("==> At least `PARSED`, `EXPECTED`, or `CORRECTIONS` is needed!");
333+
console.log("==> At least `PARSED` or `EXPECTED` is needed!");
320334
return 1;
321335
}
322336

323337
const loadedFile = loadContent(testFileContent);
324338
let res = 0;
325339

326340
if (checkExpected) {
327-
res += runCheck(loadedFile, "EXPECTED", (query, expected, text) => {
341+
res += runCheck(loadedFile, "EXPECTED", getCorrections, (query, expected, text) => {
328342
return runSearch(query, expected, doSearch, loadedFile, text);
329343
});
330344
}
331345
if (checkParsed) {
332-
res += runCheck(loadedFile, "PARSED", (query, expected, text) => {
346+
res += runCheck(loadedFile, "PARSED", getCorrections, (query, expected, text) => {
333347
return runParser(query, expected, parseQuery, text);
334348
});
335349
}
336-
if (checkCorrections) {
337-
res += runCheck(loadedFile, "CORRECTIONS", (query, expected) => {
338-
return runCorrections(query, expected, getCorrections, loadedFile);
339-
});
340-
}
341350
return res;
342351
}
343352

@@ -367,8 +376,7 @@ function loadSearchJS(doc_folder, resource_suffix) {
367376
},
368377
getCorrections: function(queryStr, filterCrate, currentCrate) {
369378
const parsedQuery = searchModule.parseQuery(queryStr);
370-
searchModule.execQuery(parsedQuery, searchWords,
371-
filterCrate, currentCrate);
379+
searchModule.execQuery(parsedQuery, searchWords, filterCrate, currentCrate);
372380
return parsedQuery.correction;
373381
},
374382
parseQuery: searchModule.parseQuery,

tests/rustdoc-js-std/alias-1.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const QUERY = '&';
2-
31
const EXPECTED = {
2+
'query': '&',
43
'others': [
54
{ 'path': 'std', 'name': 'reference' },
65
],

tests/rustdoc-js-std/alias-2.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const QUERY = '+';
2-
31
const EXPECTED = {
2+
'query': '+',
43
'others': [
54
{ 'path': 'std::ops', 'name': 'AddAssign' },
65
{ 'path': 'std::ops', 'name': 'Add' },

tests/rustdoc-js-std/alias-3.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const QUERY = '!';
2-
31
const EXPECTED = {
2+
'query': '!',
43
'others': [
54
{ 'path': 'std', 'name': 'never' },
65
],

tests/rustdoc-js-std/alias-4.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const QUERY = '<';
2-
31
const EXPECTED = {
2+
'query': '<',
43
'others': [
54
{ 'name': 'Ord' },
65
],

tests/rustdoc-js-std/alias.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// ignore-order
22

3-
const QUERY = '[';
4-
53
const EXPECTED = {
4+
'query': '[',
65
'others': [
76
{ 'path': 'std', 'name': 'slice' },
87
{ 'path': 'std::ops', 'name': 'IndexMut' },

tests/rustdoc-js-std/asrawfd.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// ignore-order
22

3-
const QUERY = 'RawFd::as_raw_fd';
4-
53
const EXPECTED = {
4+
'query': 'RawFd::as_raw_fd',
65
'others': [
76
// Reproduction test for https://github.com/rust-lang/rust/issues/78724
87
// Validate that type alias methods get the correct path.

tests/rustdoc-js-std/basic.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const QUERY = 'String';
2-
31
const EXPECTED = {
2+
'query': 'String',
43
'others': [
54
{ 'path': 'std::string', 'name': 'String' },
65
{ 'path': 'std::ffi', 'name': 'CString' },

tests/rustdoc-js-std/deduplication.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// ignore-order
22

3-
const QUERY = 'is_nan';
4-
53
const EXPECTED = {
4+
'query': 'is_nan',
65
'others': [
76
{ 'path': 'std::f32', 'name': 'is_nan' },
87
{ 'path': 'std::f64', 'name': 'is_nan' },

tests/rustdoc-js-std/enum-option.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const QUERY = 'enum:Option';
2-
31
const EXPECTED = {
2+
'query': 'enum:Option',
43
'others': [
54
{ 'path': 'std::option', 'name': 'Option' },
65
],

tests/rustdoc-js-std/filter-crate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// exact-check
22

3-
const QUERY = '"hashmap"';
43
const FILTER_CRATE = 'core';
54

65
const EXPECTED = {
6+
'query': 'hashmap',
77
'others': [
88
],
99
};

tests/rustdoc-js-std/fn-forget.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const QUERY = 'fn:forget';
2-
31
const EXPECTED = {
2+
'query': 'fn:forget',
43
'others': [
54
{ 'path': 'std::mem', 'name': 'forget' },
65
{ 'path': 'std::fmt', 'name': 'format' },

tests/rustdoc-js-std/from_u.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const QUERY = 'from_u';
2-
31
const EXPECTED = {
2+
'query': 'from_u',
43
'others': [
54
{ 'path': 'std::char', 'name': 'from_u32' },
65
{ 'path': 'std::str', 'name': 'from_utf8' },

tests/rustdoc-js-std/keyword.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// ignore-order
22

3-
const QUERY = 'fn';
4-
53
const EXPECTED = {
4+
'query': 'fn',
65
'others': [
76
{ 'path': 'std', 'name': 'fn', ty: 15 }, // 15 is for primitive types
87
{ 'path': 'std', 'name': 'fn', ty: 21 }, // 21 is for keywords

tests/rustdoc-js-std/macro-check.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// ignore-order
22

3-
const QUERY = 'panic';
4-
53
const EXPECTED = {
4+
'query': 'panic',
65
'others': [
76
{ 'path': 'std', 'name': 'panic', ty: 14 }, // 15 is for macros
87
{ 'path': 'std', 'name': 'panic', ty: 0 }, // 0 is for modules

tests/rustdoc-js-std/macro-print.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const QUERY = 'macro:print';
2-
31
const EXPECTED = {
2+
'query': 'macro:print',
43
'others': [
54
{ 'path': 'std', 'name': 'print' },
65
{ 'path': 'std', 'name': 'println' },

tests/rustdoc-js-std/never.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const QUERY = '!';
2-
31
const EXPECTED = {
2+
'query': '!',
43
'others': [
54
{ 'path': 'std', 'name': 'never' },
65
],

tests/rustdoc-js-std/option-type-signatures.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
const QUERY = [
2-
'option, fnonce -> option',
3-
'option -> default',
4-
];
5-
61
const EXPECTED = [
72
{
3+
'query': 'option, fnonce -> option',
84
'others': [
95
{ 'path': 'std::option::Option', 'name': 'map' },
106
],
117
},
128
{
9+
'query': 'option -> default',
1310
'others': [
1411
{ 'path': 'std::option::Option', 'name': 'unwrap_or_default' },
1512
{ 'path': 'std::option::Option', 'name': 'get_or_insert_default' },

0 commit comments

Comments
 (0)