anbt-sql-formatter 0.0.7 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_parser.rb CHANGED
@@ -34,8 +34,8 @@ class TestAnbtSqlParser < Test::Unit::TestCase
34
34
 
35
35
  def test_letter?
36
36
  msg = "letter? - "
37
- assert_equals( msg, false, @parser.letter?("'") )
38
- assert_equals( msg, false, @parser.letter?('"') )
37
+ assert_equals( msg, false, @parser.letter?("'") )
38
+ assert_equals( msg, false, @parser.letter?('"') )
39
39
  end
40
40
 
41
41
 
@@ -77,417 +77,441 @@ class TestAnbtSqlParser < Test::Unit::TestCase
77
77
 
78
78
  ##
79
79
  # コメントと文字列のテストは coarse tokenize で行う。
80
- def test_next_sql_token
81
- msg = "token type recognition"
82
-
83
- ########
84
- @parser.before = " "
85
- @parser.pos = 0
86
- assert_equals(
87
- msg + "space",
88
- strip_indent(
89
- <<-EOB
90
- space ( )
91
- EOB
92
- ),
93
- _format([ @parser.next_sql_token ])
94
- )
95
-
96
- ########
97
- @parser.before = "!="
98
- @parser.pos = 0
99
- assert_equals(
100
- msg + "!=",
101
- strip_indent(
102
- <<-EOB
103
- symbol (!=)
104
- EOB
105
- ),
106
- _format([ @parser.next_sql_token ])
107
- )
108
-
109
-
110
- ########
111
- @parser.before = "a b"
112
- @parser.pos = 1
113
- assert_equals(
114
- msg + "space",
115
- strip_indent(
116
- <<-EOB
117
- space ( )
118
- EOB
119
- ),
120
- _format([ @parser.next_sql_token ])
121
- )
122
-
123
- ########
124
- @parser.before = ","
125
- @parser.pos = 0
126
- assert_equals(
127
- msg + "symbol",
128
- strip_indent(
129
- <<-EOB
130
- symbol (,)
131
- EOB
132
- ),
133
- _format([ @parser.next_sql_token ])
134
- )
135
-
136
- ########
137
- @parser.before = "select"
138
- @parser.pos = 0
139
- assert_equals(
140
- msg + "keyword: select",
141
- strip_indent(
142
- <<-EOB
143
- keyword (select)
144
- EOB
145
- ),
146
- _format([ @parser.next_sql_token ])
147
- )
148
-
149
- ########
150
- @parser.before = "case"
151
- @parser.pos = 0
152
- assert_equals(
153
- msg + "keyword: case",
154
- strip_indent(
155
- <<-EOB
156
- keyword (case)
157
- EOB
158
- ),
159
- _format([ @parser.next_sql_token ])
160
- )
161
-
162
- ########
163
- @parser.before = "xxx123"
164
- @parser.pos = 0
165
- assert_equals(
166
- msg + "name",
167
- strip_indent(
168
- <<-EOB
169
- name (xxx123)
170
- EOB
171
- ),
172
- _format([ @parser.next_sql_token ])
173
- )
174
-
175
- ########
176
- @parser.before = '123'
177
- @parser.pos = 0
178
- assert_equals(
179
- msg + "value",
180
- strip_indent(
181
- <<-EOB
182
- value (123)
183
- EOB
184
- ),
185
- _format([ @parser.next_sql_token ])
186
- )
187
-
188
- ########
189
- @parser.before = '1.23'
190
- @parser.pos = 0
191
- assert_equals(
192
- msg + "value",
193
- strip_indent(
194
- <<-EOB
195
- value (1.23)
196
- EOB
197
- ),
198
- _format([ @parser.next_sql_token ])
199
- )
200
-
201
- ########
202
- @parser.before = '-1.23 '
203
- @parser.pos = 0
204
- assert_equals(
205
- msg + "value",
206
- strip_indent(
207
- <<-EOB
208
- value (-1.23)
209
- EOB
210
- ),
211
- _format([ @parser.next_sql_token ])
212
- )
213
-
214
- ########
215
- @parser.before = '1.23e45 '
216
- @parser.pos = 0
217
- assert_equals(
218
- msg + "value",
219
- strip_indent(
220
- <<-EOB
221
- value (1.23e45)
222
- EOB
223
- ),
224
- _format([ @parser.next_sql_token ])
225
- )
226
-
227
- ########
228
- @parser.before = '1.23e-45 '
229
- @parser.pos = 0
230
- assert_equals(
231
- msg + "value",
232
- strip_indent(
233
- <<-EOB
234
- value (1.23e-45)
235
- EOB
236
- ),
237
- _format([ @parser.next_sql_token ])
238
- )
239
-
240
- ########
241
- @parser.before = '-1.23e-45 '
242
- @parser.pos = 0
243
- assert_equals(
244
- msg + "value",
245
- strip_indent(
246
- <<-EOB
247
- value (-1.23e-45)
248
- EOB
249
- ),
250
- _format([ @parser.next_sql_token ])
251
- )
252
-
253
- ########
254
- @parser.before = '0x01 '
255
- @parser.pos = 0
256
- assert_equals(
257
- msg + "value",
258
- strip_indent(
259
- <<-EOB
260
- value (0x01)
261
- EOB
262
- ),
263
- _format([ @parser.next_sql_token ])
264
- )
265
-
266
- ########
267
- @parser.before = '1x'
268
- @parser.pos = 0
269
- assert_equals(
270
- msg + "value",
271
- strip_indent(
272
- <<-EOB
273
- value (1)
274
- EOB
275
- ),
276
- _format([ @parser.next_sql_token ])
277
- )
80
+ sub_test_case "next_sql_token" do
81
+ test "space 010" do
82
+ @parser.before = " "
83
+ @parser.pos = 0
84
+ assert_equal(
85
+ strip_indent(
86
+ <<-EOB
87
+ space ( )
88
+ EOB
89
+ ),
90
+ _format([ @parser.next_sql_token ])
91
+ )
92
+ end
93
+
94
+ test "symbol 010" do
95
+ @parser.before = "!="
96
+ @parser.pos = 0
97
+ assert_equal(
98
+ strip_indent(
99
+ <<-EOB
100
+ symbol (!=)
101
+ EOB
102
+ ),
103
+ _format([ @parser.next_sql_token ])
104
+ )
105
+ end
106
+
107
+ test "pos = 1" do
108
+ @parser.before = "a b"
109
+ @parser.pos = 1
110
+ assert_equal(
111
+ strip_indent(
112
+ <<-EOB
113
+ space ( )
114
+ EOB
115
+ ),
116
+ _format([ @parser.next_sql_token ])
117
+ )
118
+ end
119
+
120
+ test "symbol 020" do
121
+ @parser.before = ","
122
+ @parser.pos = 0
123
+ assert_equal(
124
+ strip_indent(
125
+ <<-EOB
126
+ symbol (,)
127
+ EOB
128
+ ),
129
+ _format([ @parser.next_sql_token ])
130
+ )
131
+ end
132
+
133
+ test "keyword 010" do
134
+ @parser.before = "select"
135
+ @parser.pos = 0
136
+ assert_equal(
137
+ strip_indent(
138
+ <<-EOB
139
+ keyword (select)
140
+ EOB
141
+ ),
142
+ _format([ @parser.next_sql_token ])
143
+ )
144
+ end
145
+
146
+ test "keyword 020" do
147
+ @parser.before = "case"
148
+ @parser.pos = 0
149
+ assert_equal(
150
+ strip_indent(
151
+ <<-EOB
152
+ keyword (case)
153
+ EOB
154
+ ),
155
+ _format([ @parser.next_sql_token ])
156
+ )
157
+ end
158
+
159
+ test "name 010" do
160
+ @parser.before = "xxx123"
161
+ @parser.pos = 0
162
+ assert_equal(
163
+ strip_indent(
164
+ <<-EOB
165
+ name (xxx123)
166
+ EOB
167
+ ),
168
+ _format([ @parser.next_sql_token ])
169
+ )
170
+ end
171
+
172
+ test "value 010" do
173
+ @parser.before = '123'
174
+ @parser.pos = 0
175
+ assert_equal(
176
+ strip_indent(
177
+ <<-EOB
178
+ value (123)
179
+ EOB
180
+ ),
181
+ _format([ @parser.next_sql_token ])
182
+ )
183
+ end
184
+
185
+ test "value 020" do
186
+ @parser.before = '1.23'
187
+ @parser.pos = 0
188
+ assert_equal(
189
+ strip_indent(
190
+ <<-EOB
191
+ value (1.23)
192
+ EOB
193
+ ),
194
+ _format([ @parser.next_sql_token ])
195
+ )
196
+ end
197
+
198
+ test "value 030" do
199
+ @parser.before = '-1.23 '
200
+ @parser.pos = 0
201
+ assert_equal(
202
+ strip_indent(
203
+ <<-EOB
204
+ value (-1.23)
205
+ EOB
206
+ ),
207
+ _format([ @parser.next_sql_token ])
208
+ )
209
+ end
210
+
211
+ test "value 040" do
212
+ @parser.before = '1.23e45 '
213
+ @parser.pos = 0
214
+ assert_equal(
215
+ strip_indent(
216
+ <<-EOB
217
+ value (1.23e45)
218
+ EOB
219
+ ),
220
+ _format([ @parser.next_sql_token ])
221
+ )
222
+ end
223
+
224
+ test "value 050" do
225
+ @parser.before = '1.23e-45 '
226
+ @parser.pos = 0
227
+ assert_equal(
228
+ strip_indent(
229
+ <<-EOB
230
+ value (1.23e-45)
231
+ EOB
232
+ ),
233
+ _format([ @parser.next_sql_token ])
234
+ )
235
+ end
236
+
237
+ test "value 060" do
238
+ @parser.before = '-1.23e-45 '
239
+ @parser.pos = 0
240
+ assert_equal(
241
+ strip_indent(
242
+ <<-EOB
243
+ value (-1.23e-45)
244
+ EOB
245
+ ),
246
+ _format([ @parser.next_sql_token ])
247
+ )
248
+ end
249
+
250
+ test "value 070" do
251
+ @parser.before = '0x01 '
252
+ @parser.pos = 0
253
+ assert_equal(
254
+ strip_indent(
255
+ <<-EOB
256
+ value (0x01)
257
+ EOB
258
+ ),
259
+ _format([ @parser.next_sql_token ])
260
+ )
261
+ end
262
+
263
+ test "value 080" do
264
+ @parser.before = '1x'
265
+ @parser.pos = 0
266
+ assert_equal(
267
+ strip_indent(
268
+ <<-EOB
269
+ value (1)
270
+ EOB
271
+ ),
272
+ _format([ @parser.next_sql_token ])
273
+ )
274
+ end
278
275
  end
279
276
 
280
277
 
281
- def test_parser
282
- msg = "parser basic case - "
283
-
284
- ########
285
- assert_equals(
286
- msg + "",
287
- strip_indent(
288
- <<-EOB
289
- keyword (select)
290
- space ( )
291
- name (a)
292
- space ( )
293
- keyword (from)
294
- space ( )
295
- name (b)
296
- symbol (;)
297
- EOB
298
- ),
299
- _format( @parser.parse( strip_indent(
300
- <<-EOB
301
- select a from b;
302
- EOB
303
- )))
304
- )
305
-
306
- ########
307
- assert_equals(
308
- msg + "minus + non-number",
309
- strip_indent(
310
- <<-EOB
311
- symbol (-)
312
- name (a)
313
- EOB
314
- ),
315
- _format( @parser.parse( strip_indent(
316
- <<-EOB
317
- -a
318
- EOB
319
- )))
320
- )
321
-
322
- ########
323
- assert_equals(
324
- msg + "single comment",
325
- strip_indent(
326
- <<-EOB
327
- keyword (select)
328
- space (\n)
329
- comment (-- x)
330
- name (a)
331
- EOB
332
- ),
333
- _format( @parser.parse(strip_indent(
334
- <<-EOB
335
- select
336
- -- x
337
- a
338
- EOB
339
- )))
340
- )
341
-
342
- ########
343
- assert_equals(
344
- msg + "parenthesis in single quote",
345
- strip_indent(
346
- <<-EOB
347
- value ('()')
348
- EOB
349
- ),
350
- _format( @parser.parse(strip_indent(
351
- <<-EOB
352
- '()'
353
- EOB
354
- )))
355
- )
356
-
357
- ########
358
- assert_equals(
359
- msg + "parenthesis in double quote",
360
- strip_indent(
361
- <<-EOB
362
- name ("()")
363
- EOB
364
- ),
365
- _format( @parser.parse(strip_indent(
366
- <<-EOB
367
- "()"
368
- EOB
369
- )))
370
- )
371
-
372
- ########
373
- assert_equals(
374
- msg + "multiple line comment: 1",
375
- strip_indent(
376
- <<-EOB
377
- name (aa)
378
- comment (/*bb*/)
379
- name (cc)
380
- EOB
381
- ),
382
- _format( @parser.parse(strip_indent(
383
- <<-EOB
384
- aa/*bb*/cc
385
- EOB
386
- )))
387
- )
388
-
389
- ########
390
- assert_equals(
391
- msg + "multiple line comment: 2",
392
- strip_indent(
393
- <<-EOB
394
- name (aa)
395
- comment (/*b
396
- b*/)
397
- name (cc)
398
- EOB
399
- ),
400
- _format( @parser.parse(strip_indent(
401
- <<-EOB
402
- aa/*b
403
- b*/cc
404
- EOB
405
- )))
406
- )
407
-
408
- ########
409
- assert_equals(
410
- msg + "invalid paired double quote",
411
- strip_indent(
412
- <<-EOB
413
- name (aa)
414
- name ("bb)
415
- EOB
416
- ),
417
- _format( @parser.parse(strip_indent(
418
- <<-EOB
419
- aa"bb
420
- EOB
421
- )))
422
- )
423
-
424
- ########
425
- assert_equals(
426
- msg + "multiwords keyword",
427
- strip_indent(
428
- <<-EOB
429
- keyword (group by)
430
- EOB
431
- ),
432
- _format( @parser.parse(strip_indent(
433
- <<-EOB
434
- group by
435
- EOB
436
- )))
437
- )
438
-
439
- ########
440
- assert_equals(
441
- msg + "multiwords keyword 2",
442
- strip_indent(
443
- <<-EOB
444
- name (a)
445
- space ( )
446
- keyword (group by)
447
- space ( )
448
- name (B)
449
- EOB
450
- ),
451
- _format( @parser.parse(strip_indent(
452
- <<-EOB
453
- a group by B
454
- EOB
455
- )))
456
- )
457
-
458
- ########
459
- assert_equals(
460
- msg + "multiwords keyword 3",
461
- strip_indent(
462
- <<-EOB
463
- keyword (select)
464
- space ( )
465
- value ('group by')
466
- EOB
467
- ),
468
- _format( @parser.parse(strip_indent(
469
- <<-EOB
470
- select 'group by'
471
- EOB
472
- )))
473
- )
474
-
475
- ########
476
- assert_equals(
477
- msg + "multiwords keyword 4",
478
- strip_indent(
479
- <<-EOB
480
- keyword (select)
481
- space ( )
482
- comment (/*group by*/)
483
- EOB
484
- ),
485
- _format( @parser.parse(strip_indent(
486
- <<-EOB
487
- select /*group by*/
488
- EOB
489
- )))
490
- )
491
-
278
+ sub_test_case "parser" do
279
+ test "basic case" do
280
+ assert_equal(
281
+ strip_indent(
282
+ <<-EOB
283
+ keyword (select)
284
+ space ( )
285
+ name (a)
286
+ space ( )
287
+ keyword (from)
288
+ space ( )
289
+ name (b)
290
+ symbol (;)
291
+ EOB
292
+ ),
293
+ _format( @parser.parse( strip_indent(
294
+ <<-EOB
295
+ select a from b;
296
+ EOB
297
+ )))
298
+ )
299
+ end
300
+
301
+ test "double-quoted schema and table" do
302
+ assert_equal(
303
+ strip_indent(
304
+ <<-EOB
305
+ name ("admin"."test")
306
+ EOB
307
+ ),
308
+ _format( @parser.parse( strip_indent(
309
+ <<-EOB
310
+ "admin"."test"
311
+ EOB
312
+ )))
313
+ )
314
+ end
315
+
316
+ test "name with dot" do
317
+ assert_equal(
318
+ strip_indent(
319
+ <<-EOB
320
+ name ("admin"."test")
321
+ EOB
322
+ ),
323
+ _format( @parser.parse( strip_indent(
324
+ <<-EOB
325
+ "admin"."test"
326
+ EOB
327
+ )))
328
+ )
329
+ end
330
+
331
+ test "minus + non-number" do
332
+ assert_equal(
333
+ strip_indent(
334
+ <<-EOB
335
+ symbol (-)
336
+ name (a)
337
+ EOB
338
+ ),
339
+ _format( @parser.parse( strip_indent(
340
+ <<-EOB
341
+ -a
342
+ EOB
343
+ )))
344
+ )
345
+ end
346
+
347
+ test "single comment" do
348
+ assert_equal(
349
+ strip_indent(
350
+ <<-EOB
351
+ keyword (select)
352
+ space (\n)
353
+ comment (-- x)
354
+ name (a)
355
+ EOB
356
+ ),
357
+ _format( @parser.parse(strip_indent(
358
+ <<-EOB
359
+ select
360
+ -- x
361
+ a
362
+ EOB
363
+ )))
364
+ )
365
+ end
366
+
367
+ test "parenthesis in single quote" do
368
+ assert_equal(
369
+ strip_indent(
370
+ <<-EOB
371
+ value ('()')
372
+ EOB
373
+ ),
374
+ _format( @parser.parse(strip_indent(
375
+ <<-EOB
376
+ '()'
377
+ EOB
378
+ )))
379
+ )
380
+ end
381
+
382
+ test "parenthesis in double quote" do
383
+ assert_equal(
384
+ strip_indent(
385
+ <<-EOB
386
+ name ("()")
387
+ EOB
388
+ ),
389
+ _format( @parser.parse(strip_indent(
390
+ <<-EOB
391
+ "()"
392
+ EOB
393
+ )))
394
+ )
395
+ end
396
+
397
+ test "multiple line comment: 1" do
398
+ assert_equal(
399
+ strip_indent(
400
+ <<-EOB
401
+ name (aa)
402
+ comment (/*bb*/)
403
+ name (cc)
404
+ EOB
405
+ ),
406
+ _format( @parser.parse(strip_indent(
407
+ <<-EOB
408
+ aa/*bb*/cc
409
+ EOB
410
+ )))
411
+ )
412
+ end
413
+
414
+ test "multiple line comment: 2" do
415
+ assert_equal(
416
+ strip_indent(
417
+ <<-EOB
418
+ name (aa)
419
+ comment (/*b
420
+ b*/)
421
+ name (cc)
422
+ EOB
423
+ ),
424
+ _format( @parser.parse(strip_indent(
425
+ <<-EOB
426
+ aa/*b
427
+ b*/cc
428
+ EOB
429
+ )))
430
+ )
431
+ end
432
+
433
+ test "invalid paired double quote" do
434
+ assert_equal(
435
+ strip_indent(
436
+ <<-EOB
437
+ name (aa)
438
+ name ("bb)
439
+ EOB
440
+ ),
441
+ _format( @parser.parse(strip_indent(
442
+ <<-EOB
443
+ aa"bb
444
+ EOB
445
+ )))
446
+ )
447
+ end
448
+
449
+ test "multiwords keyword" do
450
+ assert_equal(
451
+ strip_indent(
452
+ <<-EOB
453
+ keyword (group by)
454
+ EOB
455
+ ),
456
+ _format( @parser.parse(strip_indent(
457
+ <<-EOB
458
+ group by
459
+ EOB
460
+ )))
461
+ )
462
+ end
463
+
464
+ test "multiwords keyword 2" do
465
+ assert_equal(
466
+ strip_indent(
467
+ <<-EOB
468
+ name (a)
469
+ space ( )
470
+ keyword (group by)
471
+ space ( )
472
+ name (B)
473
+ EOB
474
+ ),
475
+ _format( @parser.parse(strip_indent(
476
+ <<-EOB
477
+ a group by B
478
+ EOB
479
+ )))
480
+ )
481
+ end
482
+
483
+ test "multiwords keyword 3" do
484
+ assert_equal(
485
+ strip_indent(
486
+ <<-EOB
487
+ keyword (select)
488
+ space ( )
489
+ value ('group by')
490
+ EOB
491
+ ),
492
+ _format( @parser.parse(strip_indent(
493
+ <<-EOB
494
+ select 'group by'
495
+ EOB
496
+ )))
497
+ )
498
+ end
499
+
500
+ test "multiwords keyword 4" do
501
+ assert_equal(
502
+ strip_indent(
503
+ <<-EOB
504
+ keyword (select)
505
+ space ( )
506
+ comment (/*group by*/)
507
+ EOB
508
+ ),
509
+ _format( @parser.parse(strip_indent(
510
+ <<-EOB
511
+ select /*group by*/
512
+ EOB
513
+ )))
514
+ )
515
+ end
492
516
  end
493
517
  end