anbt-sql-formatter 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,10 +14,10 @@ class TestAnbtSqlHelper < Test::Unit::TestCase
14
14
  def test_array_get
15
15
  arr = %w(a b c) # index = 0, 1, 2
16
16
  assert_raise(IndexOutOfBoundsException, "値が範囲外なのに例外が発生しない"){
17
- arr.get(3)
17
+ ::AnbtSql::ArrayUtil.get(arr, 3)
18
18
  }
19
19
  assert_raise(IndexOutOfBoundsException, "値が範囲外なのに例外が発生しない"){
20
- arr.get(-1)
20
+ ::AnbtSql::ArrayUtil.get(arr, -1)
21
21
  }
22
22
  end
23
23
  end
@@ -18,6 +18,10 @@ class TestAnbtSqlParser < Test::Unit::TestCase
18
18
  @parser = AnbtSql::Parser.new(AnbtSql::Rule.new)
19
19
  end
20
20
 
21
+ def _format(tokens)
22
+ Helper.format_tokens(tokens)
23
+ end
24
+
21
25
 
22
26
  def test_space?
23
27
  msg = "space? - "
@@ -27,18 +31,19 @@ class TestAnbtSqlParser < Test::Unit::TestCase
27
31
  assert_equals( msg, true, @parser.space?("\n") )
28
32
  end
29
33
 
30
-
34
+
31
35
  def test_letter?
32
36
  msg = "letter? - "
33
37
  assert_equals( msg, false, @parser.letter?("'") )
34
38
  assert_equals( msg, false, @parser.letter?('"') )
35
39
  end
36
-
40
+
37
41
 
38
42
  def test_symbol?
39
43
  msg = "symbol?"
40
44
  assert_equals( msg, true, @parser.symbol?('"') )
41
45
  assert_equals( msg, true, @parser.symbol?("'") )
46
+ assert_equals( msg, true, @parser.symbol?("!") )
42
47
  end
43
48
 
44
49
 
@@ -50,7 +55,7 @@ class TestAnbtSqlParser < Test::Unit::TestCase
50
55
  assert_equals( msg, false, @parser.digit?("a") )
51
56
  end
52
57
 
53
-
58
+
54
59
  ##
55
60
  # コメントと文字列のテストは coarse tokenize で行う。
56
61
  def test_next_sql_token_pos
@@ -78,156 +83,198 @@ class TestAnbtSqlParser < Test::Unit::TestCase
78
83
  ########
79
84
  @parser.before = " "
80
85
  @parser.pos = 0
81
- assert_equals( msg + "space",
82
- (<<EOB
83
- <space> </>
84
- EOB
85
- ).chomp,
86
- Helper.format_tokens([ @parser.next_sql_token ])
87
- )
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
+
88
109
 
89
110
  ########
90
111
  @parser.before = "a b"
91
112
  @parser.pos = 1
92
- assert_equals( msg + "space",
93
- (<<EOB
94
- <space> </>
95
- EOB
96
- ).chomp,
97
- Helper.format_tokens([ @parser.next_sql_token ])
98
- )
113
+ assert_equals(
114
+ msg + "space",
115
+ strip_indent(
116
+ <<-EOB
117
+ space ( )
118
+ EOB
119
+ ),
120
+ _format([ @parser.next_sql_token ])
121
+ )
99
122
 
100
123
  ########
101
124
  @parser.before = ","
102
125
  @parser.pos = 0
103
- assert_equals( msg + "symbol",
104
- (<<EOB
105
- <symbol>,</>
106
- EOB
107
- ).chomp,
108
- Helper.format_tokens([ @parser.next_sql_token ])
109
- )
126
+ assert_equals(
127
+ msg + "symbol",
128
+ strip_indent(
129
+ <<-EOB
130
+ symbol (,)
131
+ EOB
132
+ ),
133
+ _format([ @parser.next_sql_token ])
134
+ )
110
135
 
111
136
  ########
112
137
  @parser.before = "select"
113
138
  @parser.pos = 0
114
- assert_equals( msg + "keyword: select",
115
- (<<EOB
116
- <keyword>select</>
117
- EOB
118
- ).chomp,
119
- Helper.format_tokens([ @parser.next_sql_token ])
120
- )
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
+ )
121
148
 
122
149
  ########
123
150
  @parser.before = "case"
124
151
  @parser.pos = 0
125
- assert_equals( msg + "keyword: case",
126
- (<<EOB
127
- <keyword>case</>
128
- EOB
129
- ).chomp,
130
- Helper.format_tokens([ @parser.next_sql_token ])
131
- )
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
+ )
132
161
 
133
162
  ########
134
163
  @parser.before = "xxx123"
135
164
  @parser.pos = 0
136
- assert_equals( msg + "name",
137
- (<<EOB
138
- <name>xxx123</>
139
- EOB
140
- ).chomp,
141
- Helper.format_tokens([ @parser.next_sql_token ])
142
- )
165
+ assert_equals(
166
+ msg + "name",
167
+ strip_indent(
168
+ <<-EOB
169
+ name (xxx123)
170
+ EOB
171
+ ),
172
+ _format([ @parser.next_sql_token ])
173
+ )
143
174
 
144
175
  ########
145
176
  @parser.before = '123'
146
177
  @parser.pos = 0
147
- assert_equals( msg + "value",
148
- (<<EOB
149
- <value>123</>
150
- EOB
151
- ).chomp,
152
- Helper.format_tokens([ @parser.next_sql_token ])
153
- )
178
+ assert_equals(
179
+ msg + "value",
180
+ strip_indent(
181
+ <<-EOB
182
+ value (123)
183
+ EOB
184
+ ),
185
+ _format([ @parser.next_sql_token ])
186
+ )
154
187
 
155
188
  ########
156
189
  @parser.before = '1.23'
157
190
  @parser.pos = 0
158
- assert_equals( msg + "value",
159
- (<<EOB
160
- <value>1.23</>
161
- EOB
162
- ).chomp,
163
- Helper.format_tokens([ @parser.next_sql_token ])
164
- )
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
+ )
165
200
 
166
201
  ########
167
202
  @parser.before = '-1.23 '
168
203
  @parser.pos = 0
169
- assert_equals( msg + "value",
170
- (<<EOB
171
- <value>-1.23</>
172
- EOB
173
- ).chomp,
174
- Helper.format_tokens([ @parser.next_sql_token ])
175
- )
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
+ )
176
213
 
177
214
  ########
178
215
  @parser.before = '1.23e45 '
179
216
  @parser.pos = 0
180
- assert_equals( msg + "value",
181
- (<<EOB
182
- <value>1.23e45</>
183
- EOB
184
- ).chomp,
185
- Helper.format_tokens([ @parser.next_sql_token ])
186
- )
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
+ )
187
226
 
188
227
  ########
189
228
  @parser.before = '1.23e-45 '
190
229
  @parser.pos = 0
191
- assert_equals( msg + "value",
192
- (<<EOB
193
- <value>1.23e-45</>
194
- EOB
195
- ).chomp,
196
- Helper.format_tokens([ @parser.next_sql_token ])
197
- )
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
+ )
198
239
 
199
240
  ########
200
241
  @parser.before = '-1.23e-45 '
201
242
  @parser.pos = 0
202
- assert_equals( msg + "value",
203
- (<<EOB
204
- <value>-1.23e-45</>
205
- EOB
206
- ).chomp,
207
- Helper.format_tokens([ @parser.next_sql_token ])
208
- )
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
+ )
209
252
 
210
253
  ########
211
254
  @parser.before = '0x01 '
212
255
  @parser.pos = 0
213
- assert_equals( msg + "value",
214
- (<<EOB
215
- <value>0x01</>
216
- EOB
217
- ).chomp,
218
- Helper.format_tokens([ @parser.next_sql_token ])
219
- )
256
+ assert_equals(
257
+ msg + "value",
258
+ strip_indent(
259
+ <<-EOB
260
+ value (0x01)
261
+ EOB
262
+ ),
263
+ _format([ @parser.next_sql_token ])
264
+ )
220
265
 
221
266
  ########
222
267
  @parser.before = '1x'
223
268
  @parser.pos = 0
224
- assert_equals( msg + "value",
225
- (<<EOB
226
- <value>1</>
227
- EOB
228
- ).chomp,
229
- Helper.format_tokens([ @parser.next_sql_token ])
230
- )
269
+ assert_equals(
270
+ msg + "value",
271
+ strip_indent(
272
+ <<-EOB
273
+ value (1)
274
+ EOB
275
+ ),
276
+ _format([ @parser.next_sql_token ])
277
+ )
231
278
  end
232
279
 
233
280
 
@@ -235,136 +282,178 @@ EOB
235
282
  msg = "parser basic case - "
236
283
 
237
284
  ########
238
- assert_equals( msg + "", (<<EOB
239
- <keyword>select</>
240
- <space> </>
241
- <name>a</>
242
- <space> </>
243
- <keyword>from</>
244
- <space> </>
245
- <name>b</>
246
- <symbol>;</>
247
- EOB
248
- ).strip, Helper.format_tokens( @parser.parse( (<<EOB
249
- select a from b;
250
- EOB
251
- ).chop
252
- ))
253
- )
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
+ )
254
305
 
255
306
  ########
256
- assert_equals( msg + "minus + non-number", (<<EOB
257
- <symbol>-</>
258
- <name>a</>
259
- EOB
260
- ).strip, Helper.format_tokens( @parser.parse( (<<EOB
261
- -a
262
- EOB
263
- ).chop
264
- ))
265
- )
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
+ )
266
321
 
267
322
  ########
268
- assert_equals( msg + "single comment", (<<EOB
269
- <keyword>select</>
270
- <space>\n</>
271
- <comment>-- x</>
272
- <name>a</>
273
- <space>\n</>
274
- EOB
275
- ).strip, Helper.format_tokens( @parser.parse(<<EOB
276
- select
277
- -- x
278
- a
279
- EOB
280
- ))
281
- )
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
+ )
282
341
 
283
342
  ########
284
- assert_equals( msg + "parenthesis in single quote", (<<EOB
285
- <value>'()'</>
286
- EOB
287
- ).strip, Helper.format_tokens( @parser.parse((<<EOB
288
- '()'
289
- EOB
290
- ).strip
291
- ))
292
- )
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
+ )
293
356
 
294
357
  ########
295
- assert_equals( msg + "parenthesis in double quote", (<<EOB
296
- <name>"()"</>
297
- EOB
298
- ).strip, Helper.format_tokens( @parser.parse((<<EOB
299
- "()"
300
- EOB
301
- ).strip
302
- ))
303
- )
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
+ )
304
371
 
305
372
  ########
306
- assert_equals( msg + "multiple line comment: 1", (<<EOB
307
- <name>aa</>
308
- <comment>/*bb*/</>
309
- <name>cc</>
310
- EOB
311
- ).strip, Helper.format_tokens( @parser.parse((<<EOB
312
- aa/*bb*/cc
313
- EOB
314
- ).strip
315
- ))
316
- )
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
+ )
317
388
 
318
389
  ########
319
- assert_equals( msg + "multiple line comment: 2", (<<EOB
320
- <name>aa</>
321
- <comment>/*b
322
- b*/</>
323
- <name>cc</>
324
- EOB
325
- ).strip, Helper.format_tokens( @parser.parse((<<EOB
326
- aa/*b
327
- b*/cc
328
- EOB
329
- ).strip
330
- ))
331
- )
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
+ )
332
407
 
333
408
  ########
334
- assert_equals( msg + "invalid paired double quote", (<<EOB
335
- <name>aa</>
336
- <name>"bb</>
337
- EOB
338
- ).strip, Helper.format_tokens( @parser.parse((<<EOB
339
- aa"bb
340
- EOB
341
- ).strip
342
- ))
343
- )
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
+ )
344
423
 
345
424
  ########
346
- assert_equals( msg + "multiwords keyword", (<<EOB
347
- <keyword>group by</>
348
- EOB
349
- ).strip, Helper.format_tokens( @parser.parse((<<EOB
350
- group by
351
- EOB
352
- ).strip
353
- ))
354
- )
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
+ )
355
438
 
356
439
  ########
357
- assert_equals( msg + "multiwords keyword 2",
358
- (<<EOB
359
- <name>a</>
360
- <space> </>
361
- <keyword>group by</>
362
- <space> </>
363
- <name>B</>
364
- EOB
365
- ).strip,
366
- Helper.format_tokens( @parser.parse("a group by B") )
367
- )
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
+ )
368
457
 
369
458
  end
370
459
  end