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.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/CHANGELOG.md +38 -1
- data/{readme.ja.txt → README.ja.rdoc} +2 -2
- data/{readme.txt → README.rdoc} +2 -2
- data/anbt-sql-formatter.gemspec +2 -0
- data/lib/anbt-sql-formatter/coarse-tokenizer.rb +28 -27
- data/lib/anbt-sql-formatter/constants.rb +1 -1
- data/lib/anbt-sql-formatter/parser.rb +1 -1
- data/lib/anbt-sql-formatter/rule.rb +1 -1
- data/lib/anbt-sql-formatter/token.rb +2 -2
- data/lib/anbt-sql-formatter/version.rb +1 -1
- data/misc/anbt-sql-formatter-customize-example +4 -4
- data/sample.sql +4 -0
- data/test/test_coarse-tokenizer.rb +5 -5
- data/test/test_formatter.rb +506 -529
- data/test/test_in_values.rb +6 -6
- data/test/test_parser.rb +435 -411
- data/test.sh +10 -0
- metadata +21 -10
data/test/test_formatter.rb
CHANGED
@@ -20,483 +20,478 @@ class TestAnbtSqlFormatter < Test::Unit::TestCase
|
|
20
20
|
Helper.format_tokens(tokens)
|
21
21
|
end
|
22
22
|
|
23
|
-
def
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
)
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
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
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
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
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
)
|
186
|
-
|
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
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
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
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
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
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
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
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
@
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
;
|
496
|
-
|
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
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
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
|