Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add toMatchUnorderedArrary matcher replace tokenSuggestion test #271

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ module.exports = {
// setupFiles: [],

// 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.
// slowTestThreshold: 5,
Expand Down
17 changes: 17 additions & 0 deletions test/jest.d.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 49 additions & 0 deletions test/matchers.ts
Original file line number Diff line number Diff line change
@@ -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.';
},
};
};
6 changes: 3 additions & 3 deletions test/parser/flinksql/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('Flink SQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual(['MODULES', 'CATALOG']);
expect(suggestion).toMatchUnorderedArrary(['MODULES', 'CATALOG']);
});

test('Create Statement ', () => {
Expand All @@ -32,7 +32,7 @@ describe('Flink SQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'CATALOG',
'FUNCTION',
'TEMPORARY',
Expand All @@ -52,7 +52,7 @@ describe('Flink SQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'MODULES',
'FULL',
'FUNCTIONS',
Expand Down
20 changes: 10 additions & 10 deletions test/parser/hive/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'APPLICATION',
'GROUP',
'USER',
Expand All @@ -45,7 +45,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'CONNECTOR',
'APPLICATION',
'GROUP',
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual(['FROM']);
expect(suggestion).toMatchUnorderedArrary(['FROM']);
});

test('After DESCRIBE', () => {
Expand All @@ -92,7 +92,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'EXTENDED',
'FORMATTED',
'FUNCTION',
Expand All @@ -111,7 +111,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'CONNECTOR',
'APPLICATION',
'GROUP',
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual(['TABLE']);
expect(suggestion).toMatchUnorderedArrary(['TABLE']);
});

test('After IMPORT', () => {
Expand All @@ -153,7 +153,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual(['FROM', 'TABLE', 'EXTERNAL']);
expect(suggestion).toMatchUnorderedArrary(['FROM', 'TABLE', 'EXTERNAL']);
});

test('After INSERT', () => {
Expand All @@ -165,7 +165,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual(['INTO', 'OVERWRITE']);
expect(suggestion).toMatchUnorderedArrary(['INTO', 'OVERWRITE']);
});

test('After LOAD', () => {
Expand All @@ -177,7 +177,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual(['DATA']);
expect(suggestion).toMatchUnorderedArrary(['DATA']);
});

test('After SHOW', () => {
Expand All @@ -189,7 +189,7 @@ describe('Hive SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'CURRENT',
'ROLES',
'PRINCIPALS',
Expand Down
10 changes: 5 additions & 5 deletions test/parser/impala/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('Impala SQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual(['TABLE', 'VIEW', 'DATABASE']);
expect(suggestion).toMatchUnorderedArrary(['TABLE', 'VIEW', 'DATABASE']);
});

test('After CREATE', () => {
Expand All @@ -32,7 +32,7 @@ describe('Impala SQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'TABLE',
'EXTERNAL',
'VIEW',
Expand All @@ -54,7 +54,7 @@ describe('Impala SQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'DATABASE',
'SCHEMA',
'TABLE',
Expand All @@ -77,7 +77,7 @@ describe('Impala SQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual(['INTO', 'OVERWRITE']);
expect(suggestion).toMatchUnorderedArrary(['INTO', 'OVERWRITE']);
});

test('After SHOW', () => {
Expand All @@ -90,7 +90,7 @@ describe('Impala SQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'DATABASES',
'SCHEMAS',
'TABLES',
Expand Down
22 changes: 14 additions & 8 deletions test/parser/mysql/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('MySQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'RESOURCE',
'USER',
'VIEW',
Expand Down Expand Up @@ -50,7 +50,7 @@ describe('MySQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'RESOURCE',
'USER',
'ROLE',
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('MySQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual(['FROM', 'IGNORE', 'QUICK', 'LOW_PRIORITY']);
expect(suggestion).toMatchUnorderedArrary(['FROM', 'IGNORE', 'QUICK', 'LOW_PRIORITY']);
});

test('After DESCRIBE', () => {
Expand All @@ -104,7 +104,7 @@ describe('MySQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'ANALYZE',
'SELECT',
'DELETE',
Expand All @@ -128,7 +128,7 @@ describe('MySQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'RESOURCE',
'USER',
'PREPARE',
Expand Down Expand Up @@ -161,7 +161,13 @@ describe('MySQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual(['INTO', 'IGNORE', 'DELAYED', 'HIGH_PRIORITY', 'LOW_PRIORITY']);
expect(suggestion).toMatchUnorderedArrary([
'INTO',
'IGNORE',
'DELAYED',
'HIGH_PRIORITY',
'LOW_PRIORITY',
]);
});

test('After LOAD', () => {
Expand All @@ -174,7 +180,7 @@ describe('MySQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual(['INDEX', 'XML', 'DATA']);
expect(suggestion).toMatchUnorderedArrary(['INDEX', 'XML', 'DATA']);
});

test('After SHOW', () => {
Expand All @@ -187,7 +193,7 @@ describe('MySQL Token Suggestion', () => {
pos
)?.keywords;

expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'REPLICAS',
'REPLICA',
'SLAVE',
Expand Down
10 changes: 5 additions & 5 deletions test/parser/pgsql/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Postgres SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'TYPE',
'TEXT',
'STATISTICS',
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('Postgres SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'RECURSIVE',
'VIEW',
'TEMPORARY',
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('Postgres SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual(['FROM']);
expect(suggestion).toMatchUnorderedArrary(['FROM']);
});

test('After DROP', () => {
Expand All @@ -138,7 +138,7 @@ describe('Postgres SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual([
expect(suggestion).toMatchUnorderedArrary([
'OPERATOR',
'ROUTINE',
'PROCEDURE',
Expand Down Expand Up @@ -188,6 +188,6 @@ describe('Postgres SQL Token Suggestion', () => {
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual(['INTO']);
expect(suggestion).toMatchUnorderedArrary(['INTO']);
});
});
Loading
Loading