1
1
2
- import { assert , describe , expect , test } from 'vitest'
2
+ import { describe , expect , test } from 'vitest'
3
3
import Document from '../document' ;
4
- import { StatementType } from '../types' ;
5
4
6
- describe ( `Block statement tests` , ( ) => {
5
+ const parserScenarios = describe . each ( [
6
+ { newDoc : ( content : string ) => new Document ( content ) , isFormatted : false } ,
7
+ ] ) ;
8
+
9
+ parserScenarios ( `Block statement tests` , ( { newDoc, isFormatted} ) => {
7
10
test ( 'Block start tests' , ( ) => {
8
11
const lines = [
9
12
`CREATE ALIAS "TestDelimiters"."Delimited Alias" FOR "TestDelimiters"."Delimited Table";` ,
@@ -15,19 +18,19 @@ describe(`Block statement tests`, () => {
15
18
`LANGUAGE SQL BEGIN SET "Delimited Parameter" = 13; END;` ,
16
19
] . join ( `\n` ) ;
17
20
18
- const doc = new Document ( lines ) ;
21
+ const doc = newDoc ( lines ) ;
19
22
20
23
// CREATE, CREATE, RETURN, END, CREATE, SET, END
21
24
expect ( doc . statements . length ) . toBe ( 7 ) ;
22
25
23
26
const aliasDef = doc . statements [ 0 ] ;
24
- expect ( aliasDef . isBlockOpener ( ) ) . toBeFalsy ( ) ;
27
+ expect ( aliasDef . isCompoundStart ( ) ) . toBeFalsy ( ) ;
25
28
26
29
const functionDef = doc . statements [ 1 ] ;
27
- expect ( functionDef . isBlockOpener ( ) ) . toBeTruthy ( ) ;
30
+ expect ( functionDef . isCompoundStart ( ) ) . toBeTruthy ( ) ;
28
31
29
32
const procedureDef = doc . statements [ 4 ] ;
30
- expect ( procedureDef . isBlockOpener ( ) ) . toBeTruthy ( ) ;
33
+ expect ( procedureDef . isCompoundStart ( ) ) . toBeTruthy ( ) ;
31
34
} ) ;
32
35
33
36
test ( 'Compound statement test' , ( ) => {
@@ -53,21 +56,21 @@ describe(`Block statement tests`, () => {
53
56
`LANGUAGE SQL BEGIN SET "Delimited Parameter" = 13; END;` ,
54
57
] . join ( `\n` ) ;
55
58
56
- const doc = new Document ( lines ) ;
59
+ const doc = newDoc ( lines ) ;
57
60
58
61
const t = doc . statements . length ;
59
62
60
63
const aliasDef = doc . statements [ 0 ] ;
61
- expect ( aliasDef . isBlockOpener ( ) ) . toBeFalsy ( ) ;
64
+ expect ( aliasDef . isCompoundStart ( ) ) . toBeFalsy ( ) ;
62
65
63
66
const functionDef = doc . statements [ 1 ] ;
64
- expect ( functionDef . isBlockOpener ( ) ) . toBeTruthy ( ) ;
67
+ expect ( functionDef . isCompoundStart ( ) ) . toBeTruthy ( ) ;
65
68
66
69
const functionEnd = doc . statements [ 3 ] ;
67
- expect ( functionEnd . isBlockEnder ( ) ) . toBeTruthy ( ) ;
70
+ expect ( functionEnd . isCompoundEnd ( ) ) . toBeTruthy ( ) ;
68
71
69
72
const beginBlock = doc . statements [ 4 ] ;
70
- expect ( beginBlock . isBlockOpener ( ) ) . toBeTruthy ( ) ;
73
+ expect ( beginBlock . isCompoundStart ( ) ) . toBeTruthy ( ) ;
71
74
} ) ;
72
75
73
76
test ( 'Statement groups' , ( ) => {
@@ -96,23 +99,58 @@ describe(`Block statement tests`, () => {
96
99
`LANGUAGE SQL BEGIN SET "Delimited Parameter" = 13; END;` ,
97
100
] . join ( `\r\n` ) ;
98
101
99
- const doc = new Document ( lines ) ;
102
+ const doc = newDoc ( lines ) ;
100
103
101
104
const groups = doc . getStatementGroups ( ) ;
102
105
103
106
expect ( groups . length ) . toBe ( 4 ) ;
104
107
105
108
const aliasStatement = groups [ 0 ] ;
106
- const aliasSubstring = lines . substring ( aliasStatement . range . start , aliasStatement . range . end ) ;
109
+ const aliasSubstring = doc . content . substring ( aliasStatement . range . start , aliasStatement . range . end ) ;
107
110
expect ( aliasSubstring ) . toBe ( `CREATE ALIAS "TestDelimiters"."Delimited Alias" FOR "TestDelimiters"."Delimited Table"` ) ;
108
111
112
+ const functionStatement = groups [ 1 ] ;
113
+ const functionSubstring = doc . content . substring ( functionStatement . range . start , functionStatement . range . end ) ;
114
+
115
+ if ( isFormatted ) {
116
+ expect ( functionSubstring ) . toBe ( [
117
+ `CREATE FUNCTION "TestDelimiters"."Delimited Function"(` ,
118
+ ` "Delimited Parameter" INTEGER` ,
119
+ `) RETURNS INTEGER LANGUAGE SQL BEGIN` ,
120
+ ` RETURN "Delimited Parameter";` ,
121
+ `END` ,
122
+ ] . join ( `\r\n` ) ) ;
123
+ } else {
124
+ expect ( functionSubstring ) . toBe ( [
125
+ `CREATE FUNCTION "TestDelimiters"."Delimited Function" ("Delimited Parameter" INTEGER) ` ,
126
+ `RETURNS INTEGER LANGUAGE SQL BEGIN RETURN "Delimited Parameter"; END`
127
+ ] . join ( `\r\n` ) )
128
+ }
109
129
const beginStatement = groups [ 2 ] ;
110
- const compoundSubstring = lines . substring ( beginStatement . range . start , beginStatement . range . end ) ;
111
- expect ( compoundSubstring ) . toBe ( compoundStatement ) ;
130
+ expect ( beginStatement . statements . length ) . toBe ( 9 ) ;
131
+ const compoundSubstring = doc . content . substring ( beginStatement . range . start , beginStatement . range . end ) ;
132
+
133
+ if ( isFormatted ) {
134
+ expect ( compoundSubstring ) . toBe ( [
135
+ `BEGIN` ,
136
+ ` DECLARE already_exists SMALLINT DEFAULT 0;` ,
137
+ ` DECLARE dup_object_hdlr CONDITION FOR SQLSTATE '42710';` ,
138
+ ` DECLARE CONTINUE HANDLER FOR dup_object_hdlr SET already_exists = 1;` ,
139
+ ` CREATE TABLE table1(` ,
140
+ ` col1 INT` ,
141
+ ` );` ,
142
+ ` IF already_exists > 0 THEN;` ,
143
+ ` DELETE FROM table1;` ,
144
+ ` END IF;` ,
145
+ `END` ,
146
+ ] . join ( `\r\n` ) ) ;
147
+ } else {
148
+ expect ( compoundSubstring ) . toBe ( compoundStatement ) ;
149
+ }
112
150
} ) ;
113
151
} ) ;
114
152
115
- describe ( `Definition tests` , ( ) => {
153
+ parserScenarios ( `Definition tests` , ( { newDoc } ) => {
116
154
test ( `Alias, function, procedure` , ( ) => {
117
155
const lines = [
118
156
`CREATE ALIAS "TestDelimiters"."Delimited Alias" FOR "TestDelimiters"."Delimited Table";` ,
@@ -124,7 +162,7 @@ describe(`Definition tests`, () => {
124
162
`LANGUAGE SQL BEGIN SET "Delimited Parameter" = 13; END;` ,
125
163
] . join ( `\n` ) ;
126
164
127
- const doc = new Document ( lines ) ;
165
+ const doc = newDoc ( lines ) ;
128
166
129
167
const defs = doc . getDefinitions ( ) ;
130
168
@@ -161,7 +199,7 @@ describe(`Definition tests`, () => {
161
199
`END;` ,
162
200
] . join ( `\r\n` ) ;
163
201
164
- const doc = new Document ( lines ) ;
202
+ const doc = newDoc ( lines ) ;
165
203
166
204
const defs = doc . getDefinitions ( ) ;
167
205
@@ -245,7 +283,7 @@ describe(`Definition tests`, () => {
245
283
`END ; ` ,
246
284
] . join ( `\n` ) ;
247
285
248
- const doc = new Document ( lines ) ;
286
+ const doc = newDoc ( lines ) ;
249
287
250
288
const groups = doc . getStatementGroups ( ) ;
251
289
expect ( groups . length ) . toBe ( 1 ) ;
0 commit comments