Skip to content

Commit fa14f22

Browse files
Improve rustdoc JS tests error output
1 parent 53d19b3 commit fa14f22

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/tools/rustdoc-js/tester.js

+31
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,30 @@ function loadThings(thingsToLoad, kindOfLoad, funcToCall, fileContent) {
190190
return content;
191191
}
192192

193+
function contentToDiffLine(key, value) {
194+
return `"${key}": "${value}",`;
195+
}
196+
197+
// This function is only called when no matching result was found and therefore will only display
198+
// the diff between the two items.
199+
function betterLookingDiff(entry, data) {
200+
let output = ' {\n';
201+
let spaces = ' ';
202+
for (let key in entry) {
203+
if (!entry.hasOwnProperty(key)) {
204+
continue;
205+
}
206+
let value = data[key];
207+
if (value !== entry[key]) {
208+
output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';
209+
output += '+' + spaces + contentToDiffLine(key, value) + '\n';
210+
} else {
211+
output += spaces + contentToDiffLine(key, value) + '\n';
212+
}
213+
}
214+
return output + ' }';
215+
}
216+
193217
function lookForEntry(entry, data) {
194218
for (var i = 0; i < data.length; ++i) {
195219
var allGood = true;
@@ -281,6 +305,13 @@ function runSearch(query, expected, index, loaded, loadedFile, queryName) {
281305
if (entry_pos === null) {
282306
error_text.push(queryName + "==> Result not found in '" + key + "': '" +
283307
JSON.stringify(entry[i]) + "'");
308+
// By default, we just compare the two first items.
309+
let item_to_diff = 0;
310+
if ((ignore_order === false || exact_check === true) && i < results[key].length) {
311+
item_to_diff = i;
312+
}
313+
error_text.push("Diff of first error:\n" +
314+
betterLookingDiff(entry[i], results[key][item_to_diff]));
284315
} else if (exact_check === true && prev_pos + 1 !== entry_pos) {
285316
error_text.push(queryName + "==> Exact check failed at position " + (prev_pos + 1) +
286317
": expected '" + JSON.stringify(entry[i]) + "' but found '" +

0 commit comments

Comments
 (0)