mirror of https://github.com/mongodb/mongo
Co-authored-by: auto-revert-processor <devprod-si-team@mongodb.com> GitOrigin-RevId: 22aa93de7f46ed72b6221e8756534c7676ebc2e6
This commit is contained in:
parent
deabe66e90
commit
2e0a3ace7a
|
|
@ -1,5 +1,5 @@
|
|||
import {describe, it} from "jstests/libs/mochalite.js";
|
||||
import {stringdiff, colorize} from "src/mongo/shell/stringdiff.js";
|
||||
import {stringdiff} from "src/mongo/shell/stringdiff.js";
|
||||
|
||||
describe("diff strings", () => {
|
||||
function difftest(oldStr, newStr, expectedDiff) {
|
||||
|
|
@ -131,27 +131,6 @@ describe("diff strings", () => {
|
|||
y
|
||||
z`;
|
||||
difftest(oldStr, newStr, expectedDiff);
|
||||
|
||||
let highlighted = colorize(stringdiff(oldStr, newStr));
|
||||
const expectedHighlighted = `\
|
||||
a
|
||||
b
|
||||
\u001b[31m-c\u001b[0m
|
||||
\u001b[32m+X\u001b[0m
|
||||
d
|
||||
e
|
||||
f
|
||||
g
|
||||
---
|
||||
t
|
||||
u
|
||||
v
|
||||
w
|
||||
\u001b[31m-x\u001b[0m
|
||||
\u001b[32m+Y\u001b[0m
|
||||
y
|
||||
z`;
|
||||
assert.eq(highlighted, expectedHighlighted);
|
||||
});
|
||||
|
||||
it("compound diff", () => {
|
||||
|
|
|
|||
|
|
@ -852,62 +852,13 @@ it("assertEqJsonFormat", function () {
|
|||
() => {
|
||||
assert.eq(5, 2 + 2, "lorem ipsum");
|
||||
},
|
||||
{
|
||||
msg: `\
|
||||
expected 5 to equal 4
|
||||
\u001b[32m+ expected\u001b[0m \u001b[31m- actual\u001b[0m
|
||||
|
||||
\u001b[31m-5\u001b[0m
|
||||
\u001b[32m+4\u001b[0m
|
||||
: lorem ipsum`,
|
||||
attr: {},
|
||||
},
|
||||
{msg: "[{a}] and [{b}] are not equal : lorem ipsum", attr: {a: 5, b: 4}},
|
||||
);
|
||||
assertThrowsErrorWithJson(
|
||||
() => {
|
||||
assert.eq(5, 2 + 2, "lorem ipsum", kAttr);
|
||||
},
|
||||
{
|
||||
msg: `\
|
||||
expected 5 to equal 4
|
||||
\u001b[32m+ expected\u001b[0m \u001b[31m- actual\u001b[0m
|
||||
|
||||
\u001b[31m-5\u001b[0m
|
||||
\u001b[32m+4\u001b[0m
|
||||
: lorem ipsum`,
|
||||
attr: {...kAttr},
|
||||
},
|
||||
);
|
||||
|
||||
assertThrowsErrorWithJson(
|
||||
() => {
|
||||
assert.eq([["a", "c"]], [["a", "b", "c"]]);
|
||||
},
|
||||
{
|
||||
msg: `\
|
||||
expected [ [Array] ] to equal [ [Array] ]
|
||||
\u001b[32m+ expected\u001b[0m \u001b[31m- actual\u001b[0m
|
||||
|
||||
\u001b[31m-[ [ "a", "c" ] ]\u001b[0m
|
||||
\u001b[32m+[ [ "a", "b", "c" ] ]\u001b[0m
|
||||
`,
|
||||
attr: {},
|
||||
},
|
||||
);
|
||||
assertThrowsErrorWithJson(
|
||||
() => {
|
||||
assert.eq([{a: 1, c: 3}], [{a: 1, b: 2, c: 3}]);
|
||||
},
|
||||
{
|
||||
msg: `\
|
||||
expected [ [Object] ] to equal [ [Object] ]
|
||||
\u001b[32m+ expected\u001b[0m \u001b[31m- actual\u001b[0m
|
||||
|
||||
\u001b[31m-[ { "a" : 1, "c" : 3 } ]\u001b[0m
|
||||
\u001b[32m+[ { "a" : 1, "b" : 2, "c" : 3 } ]\u001b[0m
|
||||
`,
|
||||
attr: {},
|
||||
},
|
||||
{msg: "[{a}] and [{b}] are not equal : lorem ipsum", attr: {a: 5, b: 4, ...kAttr}},
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -93,7 +93,6 @@ namespace mongo {
|
|||
|
||||
// Generated symbols for JS files
|
||||
namespace JSFiles {
|
||||
extern const JSFile stringdiff;
|
||||
extern const JSFile types;
|
||||
extern const JSFile assert;
|
||||
extern const JSFile assert_global;
|
||||
|
|
@ -590,7 +589,6 @@ MozJSImplScope::MozJSImplScope(MozJSScriptEngine* engine, boost::optional<int> j
|
|||
|
||||
JS_FireOnNewGlobalObject(_context, _global);
|
||||
|
||||
execSetup(JSFiles::stringdiff);
|
||||
execSetup(JSFiles::assert);
|
||||
execSetup(JSFiles::assert_global);
|
||||
execSetup(JSFiles::types);
|
||||
|
|
|
|||
|
|
@ -261,7 +261,6 @@ MONGOJS_CPP_JSFILES = [
|
|||
"query_global.js",
|
||||
"session.js",
|
||||
"session_global.js",
|
||||
"stringdiff.js",
|
||||
"types.js",
|
||||
"utils.js",
|
||||
"utils_global.js",
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import {stringdiff, colorize} from "src/mongo/shell/stringdiff.js";
|
||||
/**
|
||||
* Assertion Library
|
||||
*/
|
||||
|
|
@ -268,29 +267,9 @@ assert.eq = function (a, b, msg, attr) {
|
|||
return;
|
||||
}
|
||||
|
||||
// just use one level of display for a concise but helpful output
|
||||
const shortDisp = (x) => tojson(x, " ", true, tojson.MAX_DEPTH);
|
||||
const aDisplay = shortDisp(a);
|
||||
const bDisplay = shortDisp(b);
|
||||
const diff = patchDiff(a, b);
|
||||
|
||||
_doassert(msg, `expected ${aDisplay} to equal ${bDisplay}\n${diff}\n`, {...attr});
|
||||
_doassert(msg, `[{a}] and [{b}] are not equal`, {a, b, ...attr});
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a decorated patch diff with a header and colorized lines.
|
||||
* @param {string} diff
|
||||
* @returns {string}
|
||||
*/
|
||||
function patchDiff(a, b) {
|
||||
const header = colorize("+ expected\n- actual").replace("\n", " ");
|
||||
|
||||
const multilinestr = (x) => tojson(x, " ");
|
||||
const diff = colorize(stringdiff(multilinestr(a), multilinestr(b)));
|
||||
|
||||
return `${header}\n\n${diff}`;
|
||||
}
|
||||
|
||||
function _isDocEq(a, b) {
|
||||
return a === b || bsonUnorderedFieldsCompare(a, b) === 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@ export function stringdiff(oldStr, newStr) {
|
|||
const INS = "+";
|
||||
const DEL = "-";
|
||||
const PAD = " "; // matching lines
|
||||
const SEP = "---";
|
||||
|
||||
/**
|
||||
* Converts a full diff output into a patch format with context windows.
|
||||
|
|
@ -70,7 +69,7 @@ function patchdiff(fulldiff) {
|
|||
for (let i = 0; i < lines.length; i++) {
|
||||
if (keep[i]) {
|
||||
if (i > 0 && !keep[i - 1] && result.length > 0) {
|
||||
result.push(SEP);
|
||||
result.push("---");
|
||||
}
|
||||
result.push(lines[i]);
|
||||
}
|
||||
|
|
@ -186,25 +185,3 @@ function backtrack(aLines, bLines, trace, d) {
|
|||
|
||||
return diff.join("\n");
|
||||
}
|
||||
|
||||
const green = (s) => `\x1b[32m${s}\x1b[0m`;
|
||||
const red = (s) => `\x1b[31m${s}\x1b[0m`;
|
||||
|
||||
/**
|
||||
* Decorates a patch diff with colorized lines.
|
||||
* @param {string} diff
|
||||
* @returns {string}
|
||||
*/
|
||||
export function colorize(diff) {
|
||||
return diff
|
||||
.split("\n")
|
||||
.map((line) => {
|
||||
if (line.startsWith(INS)) {
|
||||
return green(line);
|
||||
} else if (line.startsWith(DEL) && line != SEP) {
|
||||
return red(line);
|
||||
}
|
||||
return line;
|
||||
})
|
||||
.join("\n");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue