Skip to content

Commit 56e4d0d

Browse files
committed
test: complete after error syntax
1 parent a94e7fc commit 56e4d0d

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { SparkSQL } from 'src/parser/spark';
4+
import { CaretPosition, EntityContextType } from 'src/parser/common/types';
5+
6+
const syntaxSql = fs.readFileSync(
7+
path.join(__dirname, 'fixtures', 'completeAfterSyntaxError.sql'),
8+
'utf-8'
9+
);
10+
11+
describe('SparkSQL Complete After Syntax Error', () => {
12+
const spark = new SparkSQL();
13+
14+
test('Syntax error but end with semi, should suggest keywords which can start a single statement', () => {
15+
const sql1 = `SELECT FROM tb1;\nI`;
16+
const pos: CaretPosition = {
17+
lineNumber: 2,
18+
column: 2,
19+
};
20+
const keywords = spark.getSuggestionAtCaretPosition(sql1, pos)?.keywords;
21+
expect(keywords).toMatchUnorderedArrary([
22+
'WITH',
23+
'SELECT',
24+
'MAP',
25+
'REDUCE',
26+
'FROM',
27+
'TABLE',
28+
'VALUES',
29+
'INSERT',
30+
'DELETE',
31+
'UPDATE',
32+
'MERGE',
33+
'USE',
34+
'SET',
35+
'CREATE',
36+
'ALTER',
37+
'DROP',
38+
'SHOW',
39+
'REPLACE',
40+
'ANALYZE',
41+
'DECLARE',
42+
'EXPLAIN',
43+
'DESC',
44+
'DESCRIBE',
45+
'COMMENT',
46+
'REFRESH',
47+
'CACHE',
48+
'UNCACHE',
49+
'CLEAR',
50+
'LOAD',
51+
'TRUNCATE',
52+
'MSCK',
53+
'REPAIR',
54+
'ADD',
55+
'LIST',
56+
'RESET',
57+
'OPTIMIZE',
58+
'GRANT',
59+
'REVOKE',
60+
'EXPORT',
61+
'IMPORT',
62+
'LOCK',
63+
'UNLOCK',
64+
'START',
65+
'COMMIT',
66+
'ROLLBACK',
67+
'DFS',
68+
]);
69+
});
70+
71+
test('Syntax error but end with semi, should suggest token', () => {
72+
const pos: CaretPosition = {
73+
lineNumber: 3,
74+
column: 13,
75+
};
76+
const suggestion = spark.getSuggestionAtCaretPosition(syntaxSql, pos);
77+
expect(suggestion).not.toBeUndefined();
78+
79+
// syntax
80+
const syntaxes = suggestion?.syntax;
81+
expect(syntaxes.length).toBe(1);
82+
expect(syntaxes[0].syntaxContextType).toBe(EntityContextType.TABLE);
83+
84+
const keywords = suggestion?.keywords;
85+
expect(keywords.length).toBe(1);
86+
expect(keywords[0]).toBe('TABLE');
87+
});
88+
89+
test('Syntax error but start with keyword, should suggest token', () => {
90+
const pos: CaretPosition = {
91+
lineNumber: 9,
92+
column: 13,
93+
};
94+
const suggestion = spark.getSuggestionAtCaretPosition(syntaxSql, pos);
95+
expect(suggestion).not.toBeUndefined();
96+
97+
// syntax
98+
const syntaxes = suggestion?.syntax;
99+
expect(syntaxes.length).toBe(1);
100+
expect(syntaxes[0].syntaxContextType).toBe(EntityContextType.TABLE);
101+
102+
const keywords = suggestion?.keywords;
103+
expect(keywords.length).toBe(1);
104+
expect(keywords[0]).toBe('TABLE');
105+
});
106+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- error syntax but end with semi
2+
SELECT FROM tb2;
3+
INSERT INTO
4+
5+
6+
7+
-- error syntax but start with keyword
8+
SELECT FROM tb3
9+
INSERT INTO

0 commit comments

Comments
 (0)