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>
This commit is contained in:
JackWang032 2024-03-01 16:48:53 +08:00 committed by GitHub
parent 195878da9b
commit a05f099aa1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 136 additions and 48 deletions

View File

@ -129,7 +129,7 @@ module.exports = {
// setupFiles: [], // setupFiles: [],
// A list of paths to modules that run some code to configure or set up the testing framework before each test // A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [], setupFilesAfterEnv: ['<rootDir>/test/setupTests.ts'],
// The number of seconds after which a test is considered as slow and reported as such in the results. // The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5, // slowTestThreshold: 5,

17
test/jest.d.ts vendored Normal file
View File

@ -0,0 +1,17 @@
namespace jest {
interface Matchers<R = void, T = {}> {
/**
* @description
* Check whether two arrays are equal without considering the order of items.
*
* Make sure expected array has no duplicate item.
*
* Every item must be primitive type, like string, number, etc.
*
* @example
* expect(['a', 'b']).toMatchUnorderedArrary(['b', 'a']) // pass
* expect(['b', 'a']).toMatchUnorderedArrary(['c', 'b', 'a']) // not pass, missing item 'c'
*/
toMatchUnorderedArrary(expected: unknown[]): R;
}
}

49
test/matchers.ts Normal file
View File

@ -0,0 +1,49 @@
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.';
},
};
};

View File

@ -19,7 +19,7 @@ describe('Flink SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['MODULES', 'CATALOG']); expect(suggestion).toMatchUnorderedArrary(['MODULES', 'CATALOG']);
}); });
test('Create Statement ', () => { test('Create Statement ', () => {
@ -32,7 +32,7 @@ describe('Flink SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'CATALOG', 'CATALOG',
'FUNCTION', 'FUNCTION',
'TEMPORARY', 'TEMPORARY',
@ -52,7 +52,7 @@ describe('Flink SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'MODULES', 'MODULES',
'FULL', 'FULL',
'FUNCTIONS', 'FUNCTIONS',

View File

@ -18,7 +18,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'APPLICATION', 'APPLICATION',
'GROUP', 'GROUP',
'USER', 'USER',
@ -45,7 +45,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'CONNECTOR', 'CONNECTOR',
'APPLICATION', 'APPLICATION',
'GROUP', 'GROUP',
@ -80,7 +80,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['FROM']); expect(suggestion).toMatchUnorderedArrary(['FROM']);
}); });
test('After DESCRIBE', () => { test('After DESCRIBE', () => {
@ -92,7 +92,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'EXTENDED', 'EXTENDED',
'FORMATTED', 'FORMATTED',
'FUNCTION', 'FUNCTION',
@ -111,7 +111,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'CONNECTOR', 'CONNECTOR',
'APPLICATION', 'APPLICATION',
'GROUP', 'GROUP',
@ -141,7 +141,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['TABLE']); expect(suggestion).toMatchUnorderedArrary(['TABLE']);
}); });
test('After IMPORT', () => { test('After IMPORT', () => {
@ -153,7 +153,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['FROM', 'TABLE', 'EXTERNAL']); expect(suggestion).toMatchUnorderedArrary(['FROM', 'TABLE', 'EXTERNAL']);
}); });
test('After INSERT', () => { test('After INSERT', () => {
@ -165,7 +165,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['INTO', 'OVERWRITE']); expect(suggestion).toMatchUnorderedArrary(['INTO', 'OVERWRITE']);
}); });
test('After LOAD', () => { test('After LOAD', () => {
@ -177,7 +177,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['DATA']); expect(suggestion).toMatchUnorderedArrary(['DATA']);
}); });
test('After SHOW', () => { test('After SHOW', () => {
@ -189,7 +189,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'CURRENT', 'CURRENT',
'ROLES', 'ROLES',
'PRINCIPALS', 'PRINCIPALS',

View File

@ -19,7 +19,7 @@ describe('Impala SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['TABLE', 'VIEW', 'DATABASE']); expect(suggestion).toMatchUnorderedArrary(['TABLE', 'VIEW', 'DATABASE']);
}); });
test('After CREATE', () => { test('After CREATE', () => {
@ -32,7 +32,7 @@ describe('Impala SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'TABLE', 'TABLE',
'EXTERNAL', 'EXTERNAL',
'VIEW', 'VIEW',
@ -54,7 +54,7 @@ describe('Impala SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'DATABASE', 'DATABASE',
'SCHEMA', 'SCHEMA',
'TABLE', 'TABLE',
@ -77,7 +77,7 @@ describe('Impala SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['INTO', 'OVERWRITE']); expect(suggestion).toMatchUnorderedArrary(['INTO', 'OVERWRITE']);
}); });
test('After SHOW', () => { test('After SHOW', () => {
@ -90,7 +90,7 @@ describe('Impala SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'DATABASES', 'DATABASES',
'SCHEMAS', 'SCHEMAS',
'TABLES', 'TABLES',

View File

@ -19,7 +19,7 @@ describe('MySQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'RESOURCE', 'RESOURCE',
'USER', 'USER',
'VIEW', 'VIEW',
@ -50,7 +50,7 @@ describe('MySQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'RESOURCE', 'RESOURCE',
'USER', 'USER',
'ROLE', 'ROLE',
@ -91,7 +91,7 @@ describe('MySQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['FROM', 'IGNORE', 'QUICK', 'LOW_PRIORITY']); expect(suggestion).toMatchUnorderedArrary(['FROM', 'IGNORE', 'QUICK', 'LOW_PRIORITY']);
}); });
test('After DESCRIBE', () => { test('After DESCRIBE', () => {
@ -104,7 +104,7 @@ describe('MySQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'ANALYZE', 'ANALYZE',
'SELECT', 'SELECT',
'DELETE', 'DELETE',
@ -128,7 +128,7 @@ describe('MySQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'RESOURCE', 'RESOURCE',
'USER', 'USER',
'PREPARE', 'PREPARE',
@ -161,7 +161,13 @@ describe('MySQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['INTO', 'IGNORE', 'DELAYED', 'HIGH_PRIORITY', 'LOW_PRIORITY']); expect(suggestion).toMatchUnorderedArrary([
'INTO',
'IGNORE',
'DELAYED',
'HIGH_PRIORITY',
'LOW_PRIORITY',
]);
}); });
test('After LOAD', () => { test('After LOAD', () => {
@ -174,7 +180,7 @@ describe('MySQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['INDEX', 'XML', 'DATA']); expect(suggestion).toMatchUnorderedArrary(['INDEX', 'XML', 'DATA']);
}); });
test('After SHOW', () => { test('After SHOW', () => {
@ -187,7 +193,7 @@ describe('MySQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'REPLICAS', 'REPLICAS',
'REPLICA', 'REPLICA',
'SLAVE', 'SLAVE',

View File

@ -17,7 +17,7 @@ describe('Postgres SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'TYPE', 'TYPE',
'TEXT', 'TEXT',
'STATISTICS', 'STATISTICS',
@ -66,7 +66,7 @@ describe('Postgres SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'RECURSIVE', 'RECURSIVE',
'VIEW', 'VIEW',
'TEMPORARY', 'TEMPORARY',
@ -126,7 +126,7 @@ describe('Postgres SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['FROM']); expect(suggestion).toMatchUnorderedArrary(['FROM']);
}); });
test('After DROP', () => { test('After DROP', () => {
@ -138,7 +138,7 @@ describe('Postgres SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'OPERATOR', 'OPERATOR',
'ROUTINE', 'ROUTINE',
'PROCEDURE', 'PROCEDURE',
@ -188,6 +188,6 @@ describe('Postgres SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber), commentOtherLine(tokenSql, pos.lineNumber),
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['INTO']); expect(suggestion).toMatchUnorderedArrary(['INTO']);
}); });
}); });

View File

@ -19,7 +19,7 @@ describe('Spark SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'TABLE', 'TABLE',
'INDEX', 'INDEX',
'VIEW', 'VIEW',
@ -40,7 +40,7 @@ describe('Spark SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'TEMPORARY', 'TEMPORARY',
'INDEX', 'INDEX',
'ROLE', 'ROLE',
@ -67,7 +67,7 @@ describe('Spark SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['FROM']); expect(suggestion).toMatchUnorderedArrary(['FROM']);
}); });
test('After DESCRIBE', () => { test('After DESCRIBE', () => {
@ -80,7 +80,7 @@ describe('Spark SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'WITH', 'WITH',
'SELECT', 'SELECT',
'MAP', 'MAP',
@ -106,7 +106,7 @@ describe('Spark SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'TEMPORARY', 'TEMPORARY',
'INDEX', 'INDEX',
'ROLE', 'ROLE',
@ -130,7 +130,7 @@ describe('Spark SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['OVERWRITE', 'INTO']); expect(suggestion).toMatchUnorderedArrary(['OVERWRITE', 'INTO']);
}); });
test('After LOAD', () => { test('After LOAD', () => {
@ -143,7 +143,7 @@ describe('Spark SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['DATA']); expect(suggestion).toMatchUnorderedArrary(['DATA']);
}); });
test('After SHOW', () => { test('After SHOW', () => {
@ -156,7 +156,7 @@ describe('Spark SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual([ expect(suggestion).toMatchUnorderedArrary([
'LOCKS', 'LOCKS',
'INDEXES', 'INDEXES',
'TRANSACTIONS', 'TRANSACTIONS',
@ -195,6 +195,6 @@ describe('Spark SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['TABLE']); expect(suggestion).toMatchUnorderedArrary(['TABLE']);
}); });
}); });

View File

@ -19,7 +19,7 @@ describe('Trino SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['VIEW', 'MATERIALIZED', 'TABLE', 'SCHEMA']); expect(suggestion).toMatchUnorderedArrary(['VIEW', 'MATERIALIZED', 'TABLE', 'SCHEMA']);
}); });
test('After CREATE', () => { test('After CREATE', () => {
@ -32,7 +32,14 @@ describe('Trino SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['ROLE', 'VIEW', 'OR', 'MATERIALIZED', 'TABLE', 'SCHEMA']); expect(suggestion).toMatchUnorderedArrary([
'ROLE',
'VIEW',
'OR',
'MATERIALIZED',
'TABLE',
'SCHEMA',
]);
}); });
test('After DEALLOCATE', () => { test('After DEALLOCATE', () => {
@ -45,7 +52,7 @@ describe('Trino SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['PREPARE']); expect(suggestion).toMatchUnorderedArrary(['PREPARE']);
}); });
test('After DELETE', () => { test('After DELETE', () => {
@ -58,7 +65,7 @@ describe('Trino SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['FROM']); expect(suggestion).toMatchUnorderedArrary(['FROM']);
}); });
test('After DESCRIBE', () => { test('After DESCRIBE', () => {
@ -71,7 +78,7 @@ describe('Trino SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['OUTPUT', 'INPUT']); expect(suggestion).toMatchUnorderedArrary(['OUTPUT', 'INPUT']);
}); });
test('After DROP', () => { test('After DROP', () => {
@ -84,7 +91,13 @@ describe('Trino SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['ROLE', 'VIEW', 'MATERIALIZED', 'TABLE', 'SCHEMA']); expect(suggestion).toMatchUnorderedArrary([
'ROLE',
'VIEW',
'MATERIALIZED',
'TABLE',
'SCHEMA',
]);
}); });
test('After INSERT', () => { test('After INSERT', () => {
@ -97,6 +110,6 @@ describe('Trino SQL Token Suggestion', () => {
pos pos
)?.keywords; )?.keywords;
expect(suggestion).toEqual(['INTO']); expect(suggestion).toMatchUnorderedArrary(['INTO']);
}); });
}); });

3
test/setupTests.ts Normal file
View File

@ -0,0 +1,3 @@
import { toMatchUnorderedArrary } from './matchers';
expect.extend({ toMatchUnorderedArrary });