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.
@@ -20,483 +20,478 @@ class TestAnbtSqlFormatter < Test::Unit::TestCase
20
20
  Helper.format_tokens(tokens)
21
21
  end
22
22
 
23
- def test_modify_keyword_case
24
- msg = "upcase"
25
-
26
- ########
27
- @rule.keyword = AnbtSql::Rule::KEYWORD_UPPER_CASE
28
-
29
- tokens = @parser.parse("select")
30
- @fmt.modify_keyword_case(tokens)
31
- assert_equals(
32
- msg + "",
33
- strip_indent(
34
- <<-EOB
35
- keyword (SELECT)
36
- EOB
37
- ),
38
- _format(tokens)
39
- )
40
-
41
- ########
42
- msg = "downcase"
43
- @rule.keyword = AnbtSql::Rule::KEYWORD_LOWER_CASE
44
-
45
- tokens = @parser.parse("SELECT")
46
- @fmt.modify_keyword_case(tokens)
47
- assert_equals(
48
- msg + "",
49
- strip_indent(
50
- <<-EOB
51
- keyword (select)
52
- EOB
53
- ),
54
- _format(tokens)
55
- )
23
+ def token_new(type, string)
24
+ type_map = {
25
+ name: AnbtSql::TokenConstants::NAME,
26
+ space: AnbtSql::TokenConstants::SPACE
27
+ }
28
+ _type = type_map.fetch(type)
29
+
30
+ AnbtSql::Token.new(_type, string)
56
31
  end
57
32
 
58
33
 
59
- def test_concat_operator_for_oracle
60
- msg = "concat_operator_for_oracle - "
61
-
62
- ########
63
- tokens = @parser.parse("a+")
64
- @fmt.concat_operator_for_oracle(tokens)
65
- assert_equals(
66
- msg + "length is less than 3, should do nothing",
67
- strip_indent(
68
- <<-EOB
69
- name (a)
70
- symbol (+)
71
- EOB
72
- ),
73
- _format(tokens)
74
- )
75
-
76
- ########
77
- tokens = @parser.parse("(+)")
78
- @fmt.concat_operator_for_oracle(tokens)
79
- assert_equals(
80
- msg + "",
81
- strip_indent(
82
- <<-EOB
83
- symbol ((+))
84
- EOB
85
- ),
86
- _format(tokens)
87
- )
88
-
89
- ########
90
- tokens = @parser.parse("(+)")
91
- tokens = @fmt.format_list(tokens)
92
- assert_equals(
93
- msg + "format_list()",
94
- strip_indent(
95
- <<-EOB
96
- symbol ((+))
97
- EOB
98
- ),
99
- _format(tokens)
100
- )
34
+ sub_test_case "modify_keyword_case" do
35
+ test "upper case" do
36
+ @rule.keyword = AnbtSql::Rule::KEYWORD_UPPER_CASE
37
+
38
+ tokens = @parser.parse("select")
39
+ @fmt.modify_keyword_case(tokens)
40
+ assert_equal(
41
+ strip_indent(
42
+ <<-EOB
43
+ keyword (SELECT)
44
+ EOB
45
+ ),
46
+ _format(tokens)
47
+ )
48
+ end
49
+
50
+ test "lower case" do
51
+ @rule.keyword = AnbtSql::Rule::KEYWORD_LOWER_CASE
52
+
53
+ tokens = @parser.parse("SELECT")
54
+ @fmt.modify_keyword_case(tokens)
55
+ assert_equal(
56
+ strip_indent(
57
+ <<-EOB
58
+ keyword (select)
59
+ EOB
60
+ ),
61
+ _format(tokens)
62
+ )
63
+ end
101
64
  end
102
65
 
103
-
104
- def test_remove_symbol_side_space
105
- msg = "remove_symbol_side_space - "
106
-
107
- ########
108
- tokens = @parser.parse("a (b")
109
- @fmt.remove_symbol_side_space(tokens)
110
- assert_equals(
111
- msg + "",
112
- strip_indent(
113
- <<-EOB
114
- name (a)
115
- symbol (()
116
- name (b)
117
- EOB
118
- ),
119
- _format(tokens)
120
- )
121
-
122
66
 
123
- ########
124
- tokens = @parser.parse("a( b")
125
- @fmt.remove_symbol_side_space(tokens)
126
- assert_equals(
127
- msg + "", strip_indent(
128
- <<-EOB
129
- name (a)
130
- symbol (()
131
- name (b)
132
- EOB
133
- ),
134
- _format(tokens)
135
- )
136
-
137
-
138
- ########
139
- tokens = @parser.parse("a ( b")
140
- @fmt.remove_symbol_side_space(tokens)
141
- assert_equals(
142
- msg + "",
143
- strip_indent(
144
- <<-EOB
145
- name (a)
146
- symbol (()
147
- name (b)
148
- EOB
149
- ),
150
- _format(tokens)
151
- )
67
+ sub_test_case "concat_operator_for_oracle" do
68
+ test "length is less than 3, should do nothing" do
69
+ tokens = @parser.parse("a+")
70
+ @fmt.concat_operator_for_oracle(tokens)
71
+ assert_equal(
72
+ strip_indent(
73
+ <<-EOB
74
+ name (a)
75
+ symbol (+)
76
+ EOB
77
+ ),
78
+ _format(tokens)
79
+ )
80
+ end
81
+
82
+ test "basic" do
83
+ tokens = @parser.parse("(+)")
84
+ @fmt.concat_operator_for_oracle(tokens)
85
+ assert_equal(
86
+ strip_indent(
87
+ <<-EOB
88
+ symbol ((+))
89
+ EOB
90
+ ),
91
+ _format(tokens)
92
+ )
93
+ end
94
+
95
+ test "format_list" do
96
+ tokens = @parser.parse("(+)")
97
+ tokens = @fmt.format_list(tokens)
98
+ assert_equal(
99
+ strip_indent(
100
+ <<-EOB
101
+ symbol ((+))
102
+ EOB
103
+ ),
104
+ _format(tokens)
105
+ )
106
+ end
152
107
  end
153
-
154
-
155
- def test_special_treatment_for_parenthesis_with_one_element
156
- msg = "special_treatment_for_parenthesis_with_one_element - "
157
-
158
- ########
159
- tokens = @parser.parse("( 1 )")
160
- @fmt.special_treatment_for_parenthesis_with_one_element(tokens)
161
- assert_equals(
162
- msg + "one element, should not separate",
163
- strip_indent(
164
- <<-EOB
165
- symbol ((1))
166
- EOB
167
- ),
168
- _format(tokens)
169
- )
170
108
 
171
109
 
172
- ########
173
- tokens = @parser.parse("(1,2)")
174
- @fmt.special_treatment_for_parenthesis_with_one_element(tokens)
175
- assert_equals(
176
- msg + "more than one element, should separate",
177
- strip_indent(
178
- <<-EOB
179
- symbol (()
180
- value (1)
181
- symbol (,)
182
- value (2)
183
- symbol ())
184
- EOB
185
- ),
186
- _format(tokens)
187
- )
110
+ sub_test_case "remove_symbol_side_space" do
111
+ test "a (b" do
112
+ tokens = @parser.parse("a (b")
113
+ @fmt.remove_symbol_side_space(tokens)
114
+ assert_equal(
115
+ strip_indent(
116
+ <<-EOB
117
+ name (a)
118
+ symbol (()
119
+ name (b)
120
+ EOB
121
+ ),
122
+ _format(tokens)
123
+ )
124
+ end
125
+
126
+ test "a( b" do
127
+ tokens = @parser.parse("a( b")
128
+ @fmt.remove_symbol_side_space(tokens)
129
+ assert_equal(
130
+ strip_indent(
131
+ <<-EOB
132
+ name (a)
133
+ symbol (()
134
+ name (b)
135
+ EOB
136
+ ),
137
+ _format(tokens)
138
+ )
139
+ end
140
+
141
+ test "a ( b" do
142
+ tokens = @parser.parse("a ( b")
143
+ @fmt.remove_symbol_side_space(tokens)
144
+ assert_equal(
145
+ strip_indent(
146
+ <<-EOB
147
+ name (a)
148
+ symbol (()
149
+ name (b)
150
+ EOB
151
+ ),
152
+ _format(tokens)
153
+ )
154
+ end
188
155
  end
189
-
190
156
 
191
- def test_insert_space_between_tokens
192
- msg = "insert_space_between_tokens - "
193
157
 
194
- ########
195
- tokens = @parser.parse("a=")
196
- @fmt.insert_space_between_tokens(tokens)
197
- assert_equals(msg,
198
- strip_indent(
199
- <<-EOB
200
- name (a)
201
- space ( )
202
- symbol (=)
203
- EOB
204
- ),
205
- _format(tokens)
206
- )
207
-
208
- ########
209
- tokens = @parser.parse("=b")
210
- @fmt.insert_space_between_tokens(tokens)
211
- assert_equals(msg,
212
- strip_indent(
213
- <<-EOB
214
- symbol (=)
215
- space ( )
216
- name (b)
217
- EOB
218
- ),
219
- _format(tokens)
220
- )
158
+ sub_test_case "special_treatment_for_parenthesis_with_one_element" do
159
+ test "one element, should not separate" do
160
+ tokens = @parser.parse("( 1 )")
161
+ @fmt.special_treatment_for_parenthesis_with_one_element(tokens)
162
+ assert_equal(
163
+ strip_indent(
164
+ <<-EOB
165
+ symbol ((1))
166
+ EOB
167
+ ),
168
+ _format(tokens)
169
+ )
170
+ end
171
+
172
+ test "more than one element, should separate" do
173
+ tokens = @parser.parse("(1,2)")
174
+ @fmt.special_treatment_for_parenthesis_with_one_element(tokens)
175
+ assert_equal(
176
+ strip_indent(
177
+ <<-EOB
178
+ symbol (()
179
+ value (1)
180
+ symbol (,)
181
+ value (2)
182
+ symbol ())
183
+ EOB
184
+ ),
185
+ _format(tokens)
186
+ )
187
+ end
221
188
  end
222
189
 
223
-
224
- def test_insert_return_and_indent
225
- msg = "insert_return_and_indent - "
226
-
227
- ########
228
- tokens = @parser.parse("foo bar")
229
-
230
- index, indent_depth = 1, 1
231
-
232
- assert_equals(
233
- msg + "before",
234
- strip_indent(
235
- <<-EOB
236
- name (foo)
237
- space ( )
238
- name (bar)
239
- EOB
240
- ),
241
- _format(tokens)
242
- )
243
-
244
- result = @fmt.insert_return_and_indent(tokens, index, indent_depth)
245
-
246
- assert_equals(
247
- msg + "index: #{index} / indent depth: #{indent_depth}",
248
- strip_indent(
249
- <<-EOB
250
- name (foo)
251
- space (\n#{INDENT_STR})
252
- name (bar)
253
- EOB
254
- ),
255
- _format(tokens)
256
- )
257
-
258
- ########
259
- # msg = "" #"後の空白を置き換え"
260
- tokens = @parser.parse("select foo")
261
-
262
- index, indent_depth = 1, 1
263
-
264
- assert_equals(
265
- msg + "before",
266
- strip_indent(
267
- <<-EOB
268
- keyword (select)
269
- space ( )
270
- name (foo)
271
- EOB
272
- ),
273
- _format(tokens)
274
- )
275
-
276
- result = @fmt.insert_return_and_indent(tokens, index, indent_depth)
277
-
278
- assert_equals(
279
- msg + "#{msg}: index: #{index} / indent depth: #{indent_depth}",
280
- strip_indent(
281
- <<-EOB
282
- keyword (select)
283
- space (\n#{INDENT_STR})
284
- name (foo)
285
- EOB
286
- ),
287
- _format(tokens)
288
- )
289
-
290
- ########
291
- msg = "" #"前の空白を置き換え"
292
- tokens = @parser.parse("select foo")
293
- index, indent_depth = 2, 1
294
-
295
- assert_equals(
296
- msg + "before",
297
- strip_indent(
298
- <<-EOB
299
- keyword (select)
300
- space ( )
301
- name (foo)
302
- EOB
303
- ),
304
- _format(tokens)
305
- )
306
-
307
- result = @fmt.insert_return_and_indent(tokens, index, indent_depth)
308
- assert_equals(
309
- msg + "", 0, result)
310
-
311
- assert_equals(
312
- msg + "#{msg}: index: #{index} / indent depth: #{indent_depth}",
313
- strip_indent(
314
- <<-EOB
315
- keyword (select)
316
- space (\n#{INDENT_STR})
317
- name (foo)
318
- EOB
319
- ),
320
- _format(tokens)
321
- )
322
-
323
- ########
324
- msg = "indent depth = 2"
325
- tokens = @parser.parse("foo bar")
326
- index, indent_depth = 1, 2
327
-
328
- assert_equals(
329
- msg + "before",
330
- strip_indent(
331
- <<-EOB
332
- name (foo)
333
- space ( )
334
- name (bar)
335
- EOB
336
- ),
337
- _format(tokens)
338
- )
339
-
340
- result = @fmt.insert_return_and_indent(tokens, index, indent_depth)
341
-
342
- assert_equals(
343
- msg + "#{msg}: index: #{index} / indent depth: #{indent_depth}",
344
- strip_indent(
345
- <<-EOB
346
- name (foo)
347
- space (\n#{INDENT_STR}#{INDENT_STR})
348
- name (bar)
349
- EOB
350
- ),
351
- _format(tokens)
352
- )
353
-
354
- ########
355
- msg = "kw, nl, kw"
356
- tokens = @parser.parse("select\ncase")
357
190
 
358
- assert_equals(
359
- msg + "",
360
- strip_indent(
361
- <<-EOB
362
- keyword (select)
363
- space (\n)
364
- keyword (case)
365
- EOB
366
- ),
367
- _format(tokens)
368
- )
191
+ sub_test_case "insert_space_between_tokens" do
192
+ test "a=" do
193
+ tokens = @parser.parse("a=")
194
+ @fmt.insert_space_between_tokens(tokens)
195
+ assert_equal(
196
+ strip_indent(
197
+ <<-EOB
198
+ name (a)
199
+ space ( )
200
+ symbol (=)
201
+ EOB
202
+ ),
203
+ _format(tokens)
204
+ )
205
+ end
206
+
207
+ test "=b" do
208
+ tokens = @parser.parse("=b")
209
+ @fmt.insert_space_between_tokens(tokens)
210
+ assert_equal(
211
+ strip_indent(
212
+ <<-EOB
213
+ symbol (=)
214
+ space ( )
215
+ name (b)
216
+ EOB
217
+ ),
218
+ _format(tokens)
219
+ )
220
+ end
221
+ end
369
222
 
370
- ########
371
- =begin
372
- msg = "FROM の前で改行"
373
223
 
374
- assert_equals(
375
- msg + "",
376
- strip_indent(
377
- <<-EOB
378
- SELECT
379
- <-indent-><-indent->aa
380
- <-indent-><-indent->,bb
381
- <-indent-><-indent->,cc
382
- <-indent-><-indent->,dd
383
- <-indent-><-indent->,ee
384
- <-indent->FROM
385
- <-indent-><-indent->foo
386
- ;
387
- EOB
388
- ),
389
- # _format(tokens),
390
- @fmt.format("SELECT aa ,bb ,cc ,dd ,ee FROM foo;")
391
- )
392
- =end
393
-
394
- # ########
395
- # msg = "指定した index に対して tokens[index] が存在するので 1 を返すべき"
396
- # 間違い。tokens[index] が存在していても 1 を返すとは限らない。
397
- # tokens = parser.parse("foo bar")
398
- # #pp tokens
399
- # index = 1
400
- # result = @fmt.insert_return_and_indent(tokens, index, 1)
401
-
402
- # assert_equals(msg + "", 1, result, msg)
403
-
404
- ########
405
- msg = "指定した index に対して tokens[index] が存在しないので 0 を返すべき"
406
- tokens = @parser.parse("foo bar")
407
-
408
- index = 10
409
- result = @fmt.insert_return_and_indent(tokens, index, 1)
410
-
411
- assert_equals(msg + "", 0, result)
224
+ sub_test_case "insert_return_and_indent" do
225
+ test "basic" do
226
+ msg = "basic - "
227
+ tokens = @parser.parse("foo bar")
228
+
229
+ index, indent_depth = 1, 1
230
+
231
+ assert_equals(
232
+ msg + "before",
233
+ strip_indent(
234
+ <<-EOB
235
+ name (foo)
236
+ space ( )
237
+ name (bar)
238
+ EOB
239
+ ),
240
+ _format(tokens)
241
+ )
242
+
243
+ result = @fmt.insert_return_and_indent(tokens, index, indent_depth)
244
+
245
+ assert_equals(
246
+ msg + "index: #{index} / indent depth: #{indent_depth}",
247
+ strip_indent(
248
+ <<-EOB
249
+ name (foo)
250
+ space (\n#{INDENT_STR})
251
+ name (bar)
252
+ EOB
253
+ ),
254
+ _format(tokens)
255
+ )
256
+ end
257
+
258
+ test "replace: after" do
259
+ msg = "replace: after - " # 後の空白を置き換え
260
+ tokens = @parser.parse("select foo")
261
+
262
+ index, indent_depth = 1, 1
263
+
264
+ assert_equals(
265
+ msg + "before",
266
+ strip_indent(
267
+ <<-EOB
268
+ keyword (select)
269
+ space ( )
270
+ name (foo)
271
+ EOB
272
+ ),
273
+ _format(tokens)
274
+ )
275
+
276
+ result = @fmt.insert_return_and_indent(tokens, index, indent_depth)
277
+
278
+ assert_equals(
279
+ msg + "#{msg}: index: #{index} / indent depth: #{indent_depth}",
280
+ strip_indent(
281
+ <<-EOB
282
+ keyword (select)
283
+ space (\n#{INDENT_STR})
284
+ name (foo)
285
+ EOB
286
+ ),
287
+ _format(tokens)
288
+ )
289
+ end
290
+
291
+ test "replace: before" do
292
+ msg = "replace: before - " # 前の空白を置き換え
293
+ tokens = @parser.parse("select foo")
294
+ index, indent_depth = 2, 1
295
+
296
+ assert_equals(
297
+ msg + "before",
298
+ strip_indent(
299
+ <<-EOB
300
+ keyword (select)
301
+ space ( )
302
+ name (foo)
303
+ EOB
304
+ ),
305
+ _format(tokens)
306
+ )
307
+
308
+ result = @fmt.insert_return_and_indent(tokens, index, indent_depth)
309
+ assert_equals(
310
+ msg + "", 0, result)
311
+
312
+ assert_equals(
313
+ msg + "#{msg}: index: #{index} / indent depth: #{indent_depth}",
314
+ strip_indent(
315
+ <<-EOB
316
+ keyword (select)
317
+ space (\n#{INDENT_STR})
318
+ name (foo)
319
+ EOB
320
+ ),
321
+ _format(tokens)
322
+ )
323
+ end
324
+
325
+ test "indent depth = 2" do
326
+ msg = "indent depth = 2 - "
327
+ tokens = @parser.parse("foo bar")
328
+ index, indent_depth = 1, 2
329
+
330
+ assert_equals(
331
+ msg + "before",
332
+ strip_indent(
333
+ <<-EOB
334
+ name (foo)
335
+ space ( )
336
+ name (bar)
337
+ EOB
338
+ ),
339
+ _format(tokens)
340
+ )
341
+
342
+ result = @fmt.insert_return_and_indent(tokens, index, indent_depth)
343
+
344
+ assert_equals(
345
+ msg + "#{msg}: index: #{index} / indent depth: #{indent_depth}",
346
+ strip_indent(
347
+ <<-EOB
348
+ name (foo)
349
+ space (\n#{INDENT_STR}#{INDENT_STR})
350
+ name (bar)
351
+ EOB
352
+ ),
353
+ _format(tokens)
354
+ )
355
+ end
356
+
357
+ test "kw, nl, kw" do
358
+ tokens = @parser.parse("select\ncase")
359
+
360
+ assert_equal(
361
+ strip_indent(
362
+ <<-EOB
363
+ keyword (select)
364
+ space (\n)
365
+ keyword (case)
366
+ EOB
367
+ ),
368
+ _format(tokens)
369
+ )
370
+ end
371
+
372
+ test "insert: return 1" do
373
+ tokens = [
374
+ token_new(:name, "foo"),
375
+ token_new(:name, "bar")
376
+ ]
377
+
378
+ index = 1
379
+ result = @fmt.insert_return_and_indent(tokens, index, 1)
380
+
381
+ assert_equal(1, result)
382
+ end
383
+
384
+ test "replace: return 0" do
385
+ tokens = [
386
+ token_new(:name, "foo"),
387
+ token_new(:space, " "),
388
+ token_new(:name, "bar")
389
+ ]
390
+
391
+ index = 2
392
+ result = @fmt.insert_return_and_indent(tokens, index, 1)
393
+
394
+ assert_equal(0, result)
395
+ end
396
+
397
+ test "out of bounds" do
398
+ tokens = [
399
+ token_new(:name, "foo"),
400
+ token_new(:space, " "),
401
+ token_new(:name, "bar")
402
+ ]
403
+
404
+ index = 10
405
+ result = @fmt.insert_return_and_indent(tokens, index, 1)
406
+
407
+ assert_equal(0, result)
408
+ end
412
409
  end ## insert_return_and_indent
413
410
 
414
411
 
415
- def test_format
416
- msg = "format - "
417
-
418
- ########
419
- func_name = "TEST_FUNCTION"
420
- @rule.function_names << func_name
421
-
422
- assert_equals(
423
- msg + "function with parenthesis",
424
- strip_indent(
425
- <<-EOB
426
- SELECT
427
- <-indent-><-indent->#{func_name}( * )
428
- EOB
429
- ),
430
- @fmt.format("select #{func_name}(*)")
431
- )
432
-
433
- @rule.function_names.delete func_name
434
-
435
- ########
436
- assert_equals(
437
- msg + "Next line of single commnet",
438
- strip_indent(
439
- <<-EOB
440
- SELECT
441
- <-indent-><-indent->-- comment
442
- <-indent-><-indent->name
443
- EOB
444
- ),
445
- @fmt.format(strip_indent(
446
- <<-EOB
447
- select
448
- -- comment
449
- name
450
- EOB
451
- ))
452
- )
453
-
454
- ########
455
- assert_equals(
456
- msg + "new line after single line comment",
457
- strip_indent(
458
- <<-EOB
459
- --a
460
- b
461
- EOB
462
- ),
463
- @fmt.format(strip_indent(
464
- <<-EOB
465
- --a
466
- b
467
- EOB
468
- ))
469
- );
470
-
471
- ########
472
- assert_equals(
473
- msg + "two line breaks after semicolon",
474
- strip_indent(
475
- <<-EOB
476
- a
477
- ;
478
-
479
- b
480
- EOB
481
- ),
482
- @fmt.format(strip_indent(
483
- <<-EOB
484
- a;b
485
- EOB
486
- ))
487
- );
488
-
489
- ########
490
- assert_equals(
491
- msg + "two line breaks after semicolon",
492
- strip_indent(
493
- <<-EOB
494
- a
495
- ;
496
- EOB
497
- ),
498
- @fmt.format("a;")
499
- )
412
+ sub_test_case "format" do
413
+ test "function with parenthesis" do
414
+ func_name = "TEST_FUNCTION"
415
+ @rule.function_names << func_name
416
+
417
+ assert_equal(
418
+ strip_indent(
419
+ <<-EOB
420
+ SELECT
421
+ <-indent-><-indent->#{func_name}( * )
422
+ EOB
423
+ ),
424
+ @fmt.format("select #{func_name}(*)")
425
+ )
426
+
427
+ @rule.function_names.delete func_name
428
+ end
429
+
430
+ test "Next line of single comment" do
431
+ assert_equal(
432
+ strip_indent(
433
+ <<-EOB
434
+ SELECT
435
+ <-indent-><-indent->-- comment
436
+ <-indent-><-indent->name
437
+ EOB
438
+ ),
439
+ @fmt.format(strip_indent(
440
+ <<-EOB
441
+ select
442
+ -- comment
443
+ name
444
+ EOB
445
+ ))
446
+ )
447
+ end
448
+
449
+ test "new line after single line comment" do
450
+ assert_equal(
451
+ strip_indent(
452
+ <<-EOB
453
+ --a
454
+ b
455
+ EOB
456
+ ),
457
+ @fmt.format(strip_indent(
458
+ <<-EOB
459
+ --a
460
+ b
461
+ EOB
462
+ ))
463
+ )
464
+ end
465
+
466
+ test "two line breaks after semicolon" do
467
+ assert_equal(
468
+ strip_indent(
469
+ <<-EOB
470
+ a
471
+ ;
472
+
473
+ b
474
+ EOB
475
+ ),
476
+ @fmt.format(strip_indent(
477
+ <<-EOB
478
+ a;b
479
+ EOB
480
+ ))
481
+ )
482
+ end
483
+
484
+ test "no line breaks after semicolon" do
485
+ assert_equal(
486
+ strip_indent(
487
+ <<-EOB
488
+ a
489
+ ;
490
+ EOB
491
+ ),
492
+ @fmt.format("a;")
493
+ )
494
+ end
500
495
  end
501
496
 
502
497
  def test_format_between
@@ -512,77 +507,59 @@ class TestAnbtSqlFormatter < Test::Unit::TestCase
512
507
  end
513
508
 
514
509
 
515
- def test_split_to_statements
516
- msg = "split_to_statements - "
517
-
518
- ########
519
- tokens = @parser.parse("a;b")
520
-
521
- assert_equals(
522
- msg + "first statement",
523
- "a",
524
- @fmt.split_to_statements(tokens)[0][0].string
525
- )
526
- assert_equals(
527
- msg + "second statement",
528
- "b",
529
- @fmt.split_to_statements(tokens)[1][0].string
530
- )
531
-
532
- ########
533
- tokens = @parser.parse(";")
534
- statements = @fmt.split_to_statements(tokens)
535
- assert_equals(
536
- msg,
537
- [],
538
- statements[0]
539
- )
540
- assert_equals(
541
- msg,
542
- [],
543
- statements[1]
544
- )
545
-
546
- ########
547
- tokens = @parser.parse("a;")
548
- statements = @fmt.split_to_statements(tokens)
549
- assert_equals(
550
- msg,
551
- "name (a)",
552
- _format( statements[0] )
553
- )
554
- assert_equals(
555
- msg,
556
- [],
557
- statements[1]
558
- )
559
-
560
- ########
561
- tokens = @parser.parse(";a")
562
- statements = @fmt.split_to_statements(tokens)
563
- assert_equals(
564
- msg,
565
- [],
566
- statements[0]
567
- )
568
- assert_equals(
569
- msg,
570
- "name (a)",
571
- _format( statements[1] )
572
- )
573
-
574
- ########
575
- tokens = @parser.parse("a;b")
576
- statements = @fmt.split_to_statements(tokens)
577
- assert_equals(
578
- msg,
579
- "name (a)",
580
- _format( statements[0] )
581
- )
582
- assert_equals(
583
- msg,
584
- "name (b)",
585
- _format( statements[1] )
586
- )
510
+ sub_test_case "split_to_statements" do
511
+ test "a;b" do
512
+ tokens = @parser.parse("a;b")
513
+ statements = @fmt.split_to_statements(tokens)
514
+
515
+ assert_equal(2, statements.size)
516
+ assert_equal(
517
+ "name (a)",
518
+ _format( statements[0] )
519
+ )
520
+ assert_equal(
521
+ "name (b)",
522
+ _format( statements[1] )
523
+ )
524
+ end
525
+
526
+ test ";" do
527
+ tokens = @parser.parse(";")
528
+ statements = @fmt.split_to_statements(tokens)
529
+ assert_equal(
530
+ [],
531
+ statements[0]
532
+ )
533
+ assert_equal(
534
+ [],
535
+ statements[1]
536
+ )
537
+ end
538
+
539
+ test "a;" do
540
+ tokens = @parser.parse("a;")
541
+ statements = @fmt.split_to_statements(tokens)
542
+ assert_equal(
543
+ "name (a)",
544
+ _format( statements[0] )
545
+ )
546
+ assert_equal(
547
+ [],
548
+ statements[1]
549
+ )
550
+ end
551
+
552
+ test ";a" do
553
+ tokens = @parser.parse(";a")
554
+ statements = @fmt.split_to_statements(tokens)
555
+ assert_equal(
556
+ [],
557
+ statements[0]
558
+ )
559
+ assert_equal(
560
+ "name (a)",
561
+ _format( statements[1] )
562
+ )
563
+ end
587
564
  end
588
565
  end