2026-12-04 08:37:28 -08:00
|
|
|
|
'use strict';
|
|
|
|
|
|
// 语义比较:对象键顺序不计入差异(另行提示),数组顺序与所有值严格比较。
|
2026-12-04 08:58:53 -08:00
|
|
|
|
function short(v) {
|
|
|
|
|
|
const s = JSON.stringify(v);
|
|
|
|
|
|
return s === undefined ? 'undefined' : s.length > 300 ? s.slice(0, 300) + '…' : s;
|
|
|
|
|
|
}
|
|
|
|
|
|
function compare(expected, actual, path = '$', out = { diffs: [], orderNotes: [] }) {
|
|
|
|
|
|
if (Object.is(expected, actual)) return out;
|
|
|
|
|
|
const te = Array.isArray(expected) ? 'array' : expected === null ? 'null' : typeof expected,
|
|
|
|
|
|
ta = Array.isArray(actual) ? 'array' : actual === null ? 'null' : typeof actual;
|
|
|
|
|
|
if (te !== ta) {
|
|
|
|
|
|
out.diffs.push({ path, expected: short(expected), actual: short(actual) });
|
|
|
|
|
|
return out;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (te === 'array') {
|
|
|
|
|
|
const n = Math.max(expected.length, actual.length);
|
|
|
|
|
|
if (expected.length !== actual.length)
|
|
|
|
|
|
out.diffs.push({ path: path + '.length', expected: String(expected.length), actual: String(actual.length) });
|
|
|
|
|
|
for (let i = 0; i < Math.min(expected.length, actual.length); i++)
|
|
|
|
|
|
compare(expected[i], actual[i], path + '[' + i + ']', out);
|
|
|
|
|
|
for (let i = Math.min(expected.length, actual.length); i < n && out.diffs.length < 100000; i++)
|
|
|
|
|
|
out.diffs.push({ path: path + '[' + i + ']', expected: short(expected[i]), actual: short(actual[i]) });
|
2026-12-04 08:37:28 -08:00
|
|
|
|
return out;
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
if (te === 'object') {
|
|
|
|
|
|
const ke = Object.keys(expected),
|
|
|
|
|
|
ka = Object.keys(actual);
|
|
|
|
|
|
for (const k of ke)
|
|
|
|
|
|
if (!(k in actual)) out.diffs.push({ path: path + '.' + k, expected: short(expected[k]), actual: '(缺失)' });
|
|
|
|
|
|
for (const k of ka)
|
|
|
|
|
|
if (!(k in expected)) out.diffs.push({ path: path + '.' + k, expected: '(缺失)', actual: short(actual[k]) });
|
|
|
|
|
|
for (const k of ke) if (k in actual) compare(expected[k], actual[k], path + '.' + k, out);
|
|
|
|
|
|
const common = ke.filter(k => k in actual),
|
|
|
|
|
|
commonActual = ka.filter(k => k in expected);
|
|
|
|
|
|
if (common.join('\u0000') !== commonActual.join('\u0000')) out.orderNotes.push(path);
|
2026-12-04 08:37:28 -08:00
|
|
|
|
return out;
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
if (te === 'string' && expected.length > 120) {
|
2026-12-04 08:37:28 -08:00
|
|
|
|
// 长字符串只显示首个不同字符附近的片段
|
2026-12-04 08:58:53 -08:00
|
|
|
|
let i = 0;
|
|
|
|
|
|
while (i < expected.length && expected[i] === actual[i]) i++;
|
|
|
|
|
|
const from = Math.max(0, i - 60);
|
|
|
|
|
|
out.diffs.push({
|
|
|
|
|
|
path: path + ` @字符${i}`,
|
|
|
|
|
|
expected:
|
|
|
|
|
|
(from ? '…' : '') +
|
|
|
|
|
|
JSON.stringify(expected.slice(from, i + 120)).slice(1, -1) +
|
|
|
|
|
|
(i + 120 < expected.length ? '…' : ''),
|
|
|
|
|
|
actual:
|
|
|
|
|
|
(from ? '…' : '') +
|
|
|
|
|
|
JSON.stringify(actual.slice(from, i + 120)).slice(1, -1) +
|
|
|
|
|
|
(i + 120 < actual.length ? '…' : ''),
|
|
|
|
|
|
});
|
|
|
|
|
|
return out;
|
2026-12-04 08:37:28 -08:00
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
out.diffs.push({ path, expected: short(expected), actual: short(actual) });
|
|
|
|
|
|
return out;
|
2026-12-04 08:37:28 -08:00
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
function report(name, result, limit = 30, describe = p => p) {
|
|
|
|
|
|
const lines = [];
|
|
|
|
|
|
if (result.diffs.length) {
|
|
|
|
|
|
lines.push(`${name}:发现 ${result.diffs.length} 处差异,前 ${Math.min(limit, result.diffs.length)} 处:`);
|
|
|
|
|
|
for (const d of result.diffs.slice(0, limit))
|
|
|
|
|
|
lines.push(` ${describe(d.path)}\n 基线:${d.expected}\n 实际:${d.actual}`);
|
2026-12-04 08:37:28 -08:00
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
if (result.orderNotes.length)
|
|
|
|
|
|
lines.push(
|
|
|
|
|
|
`${name}:${result.orderNotes.length} 处对象键顺序变化(不计为失败),例如 ${result.orderNotes.slice(0, 3).join('、')}`,
|
|
|
|
|
|
);
|
2026-12-04 08:37:28 -08:00
|
|
|
|
return lines.join('\n');
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
module.exports = { compare, report };
|