lava-oushudb-dt-sql-parser/test/matchers.ts
JackWang032 a05f099aa1
feat: add toMatchUnorderedArrary matcher and apply it (#271)
* feat: add toMatchUnorderedArrary matcher replace tokenSuggestion test

* fix: revert benchmark and replace matcher of trinosql

---------

Co-authored-by: jialan <jialan@dtstack.com>
2024-03-01 16:48:53 +08:00

50 lines
1.4 KiB
TypeScript

export const toMatchUnorderedArrary: jest.CustomMatcher = function (
actual: Array<unknown>,
expected: Array<unknown>
) {
if (!Array.isArray(actual) || !Array.isArray(expected)) {
throw new TypeError('These must be of type array!');
}
const print = () =>
`Expected: ${this.utils.printExpected(expected)}. \n` +
`Received: ${this.utils.printReceived(actual)}. \n`;
const expectedMap = new Map(expected.map((item) => [item, false]));
const unexpectedItemList = [];
for (const item of actual) {
if (!expectedMap.has(item) || expectedMap.get(item)) unexpectedItemList.push(item);
else expectedMap.set(item, true);
}
if (unexpectedItemList.length) {
return {
pass: false,
message: () =>
`Receive unexpected items: ${this.utils.printReceived(unexpectedItemList)}. \n` +
print(),
};
}
const missingItemList = Array.from(expectedMap)
.filter(([_, isAppeared]) => !isAppeared)
.map(([item]) => item);
if (missingItemList.length) {
return {
pass: false,
message: () =>
`Missing expected items: ${this.utils.printExpected(missingItemList)}. \n` +
print(),
};
}
return {
pass: true,
message() {
return 'The received array matches the expected array.';
},
};
};