anbt-sql-formatter 0.1.0 → 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,447 +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 + "double-quoted schema and table",
309
- strip_indent(
310
- <<-EOB
311
- name ("admin"."test")
312
- EOB
313
- ),
314
- _format( @parser.parse( strip_indent(
315
- <<-EOB
316
- "admin"."test"
317
- EOB
318
- )))
319
- )
320
-
321
- ########
322
- assert_equals(
323
- msg + "minus + non-number",
324
- strip_indent(
325
- <<-EOB
326
- name ("admin"."test")
327
- EOB
328
- ),
329
- _format( @parser.parse( strip_indent(
330
- <<-EOB
331
- "admin"."test"
332
- EOB
333
- )))
334
- )
335
-
336
- ########
337
- assert_equals(
338
- msg + "minus + non-number",
339
- strip_indent(
340
- <<-EOB
341
- symbol (-)
342
- name (a)
343
- EOB
344
- ),
345
- _format( @parser.parse( strip_indent(
346
- <<-EOB
347
- -a
348
- EOB
349
- )))
350
- )
351
-
352
- ########
353
- assert_equals(
354
- msg + "single comment",
355
- strip_indent(
356
- <<-EOB
357
- keyword (select)
358
- space (\n)
359
- comment (-- x)
360
- name (a)
361
- EOB
362
- ),
363
- _format( @parser.parse(strip_indent(
364
- <<-EOB
365
- select
366
- -- x
367
- a
368
- EOB
369
- )))
370
- )
371
-
372
- ########
373
- assert_equals(
374
- msg + "parenthesis in single quote",
375
- strip_indent(
376
- <<-EOB
377
- value ('()')
378
- EOB
379
- ),
380
- _format( @parser.parse(strip_indent(
381
- <<-EOB
382
- '()'
383
- EOB
384
- )))
385
- )
386
-
387
- ########
388
- assert_equals(
389
- msg + "parenthesis in double quote",
390
- strip_indent(
391
- <<-EOB
392
- name ("()")
393
- EOB
394
- ),
395
- _format( @parser.parse(strip_indent(
396
- <<-EOB
397
- "()"
398
- EOB
399
- )))
400
- )
401
-
402
- ########
403
- assert_equals(
404
- msg + "multiple line comment: 1",
405
- strip_indent(
406
- <<-EOB
407
- name (aa)
408
- comment (/*bb*/)
409
- name (cc)
410
- EOB
411
- ),
412
- _format( @parser.parse(strip_indent(
413
- <<-EOB
414
- aa/*bb*/cc
415
- EOB
416
- )))
417
- )
418
-
419
- ########
420
- assert_equals(
421
- msg + "multiple line comment: 2",
422
- strip_indent(
423
- <<-EOB
424
- name (aa)
425
- comment (/*b
426
- b*/)
427
- name (cc)
428
- EOB
429
- ),
430
- _format( @parser.parse(strip_indent(
431
- <<-EOB
432
- aa/*b
433
- b*/cc
434
- EOB
435
- )))
436
- )
437
-
438
- ########
439
- assert_equals(
440
- msg + "invalid paired double quote",
441
- strip_indent(
442
- <<-EOB
443
- name (aa)
444
- name ("bb)
445
- EOB
446
- ),
447
- _format( @parser.parse(strip_indent(
448
- <<-EOB
449
- aa"bb
450
- EOB
451
- )))
452
- )
453
-
454
- ########
455
- assert_equals(
456
- msg + "multiwords keyword",
457
- strip_indent(
458
- <<-EOB
459
- keyword (group by)
460
- EOB
461
- ),
462
- _format( @parser.parse(strip_indent(
463
- <<-EOB
464
- group by
465
- EOB
466
- )))
467
- )
468
-
469
- ########
470
- assert_equals(
471
- msg + "multiwords keyword 2",
472
- strip_indent(
473
- <<-EOB
474
- name (a)
475
- space ( )
476
- keyword (group by)
477
- space ( )
478
- name (B)
479
- EOB
480
- ),
481
- _format( @parser.parse(strip_indent(
482
- <<-EOB
483
- a group by B
484
- EOB
485
- )))
486
- )
487
-
488
- ########
489
- assert_equals(
490
- msg + "multiwords keyword 3",
491
- strip_indent(
492
- <<-EOB
493
- keyword (select)
494
- space ( )
495
- value ('group by')
496
- EOB
497
- ),
498
- _format( @parser.parse(strip_indent(
499
- <<-EOB
500
- select 'group by'
501
- EOB
502
- )))
503
- )
504
-
505
- ########
506
- assert_equals(
507
- msg + "multiwords keyword 4",
508
- strip_indent(
509
- <<-EOB
510
- keyword (select)
511
- space ( )
512
- comment (/*group by*/)
513
- EOB
514
- ),
515
- _format( @parser.parse(strip_indent(
516
- <<-EOB
517
- select /*group by*/
518
- EOB
519
- )))
520
- )
521
-
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
522
516
  end
523
517
  end