anbt-sql-formatter 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require "pp"
3
+ require "anbt-sql-formatter/helper"
4
4
 
5
5
  =begin
6
6
  AnbtSqlFormatter: SQL整形ツール. SQL文を決められたルールに従い整形します。
@@ -19,6 +19,9 @@ http://homepage2.nifty.com/igat/igapyon/diary/2005/ig050613.html
19
19
 
20
20
  class AnbtSql
21
21
  class Rule
22
+
23
+ include StringUtil
24
+
22
25
  attr_accessor :keyword, :indent_string, :function_names, :space_after_comma
23
26
  attr_accessor :kw_multi_words
24
27
 
@@ -110,7 +113,7 @@ class AnbtSql
110
113
  end
111
114
 
112
115
  for i in 0...(@function_names.length)
113
- if (@function_names[i].equalsIgnoreCase(name))
116
+ if (equals_ignore_case(@function_names[i], name))
114
117
  return true
115
118
  end
116
119
  end
@@ -1,7 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require "pp"
4
-
5
3
  class AnbtSql
6
4
  class TokenConstants
7
5
 
@@ -56,14 +54,7 @@ class AnbtSql
56
54
  # @return:: バリューオブジェクトの文字列表現。
57
55
  #
58
56
  def to_s
59
- buf = ""
60
- buf << "AbstractAnbtSqlToken["
61
- buf << "_type=" + @_type
62
- buf << ",string=" + @string
63
- buf << ",pos=" + @pos
64
- buf << "]"
65
-
66
- buf
57
+ @string
67
58
  end
68
59
  end
69
60
 
@@ -1,7 +1,7 @@
1
1
  module Anbt
2
2
  module Sql
3
3
  module Formatter
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
6
6
  end
7
7
  end
@@ -6,7 +6,7 @@ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib")
6
6
  class Helper
7
7
  def Helper.format_tokens(list)
8
8
  list.map{|token|
9
- "<#{token._type}>#{token.string}</>"
9
+ "#{token._type} (#{token.string})"
10
10
  }.join("\n")
11
11
  end
12
12
  end
@@ -15,3 +15,9 @@ end
15
15
  def assert_equals(a,b,c)
16
16
  assert_equal(b,c,a)
17
17
  end
18
+
19
+ def strip_indent(text)
20
+ text.split("\n").map{|line|
21
+ line.sub(/^ */, "")
22
+ }.join("\n")
23
+ end
@@ -9,16 +9,18 @@ class CoarseTokenizer
9
9
  end
10
10
 
11
11
 
12
- def format(tokens)
13
- tokens.map{|t| t.to_s }.join("\n")
14
- end
15
-
16
12
 
17
13
  class TestCoarseTokenizer < Test::Unit::TestCase
18
14
  def setup
19
15
  @tok = CoarseTokenizer.new
20
16
  end
21
17
 
18
+ def _format(tokens)
19
+ tokens.map{|t|
20
+ "#{t._type} (#{t.string})"
21
+ }.join("\n")
22
+ end
23
+
22
24
 
23
25
  def test_shift_to_buf
24
26
  @tok.buf = ""
@@ -62,78 +64,102 @@ class TestCoarseTokenizer < Test::Unit::TestCase
62
64
  def test_tokenize
63
65
  msg = "tokenize - "
64
66
 
65
- assert_equals( msg, (<<EOB
66
- <plain>aa</>
67
- EOB
68
- ).chomp,
69
- format(@tok.tokenize((<<EOB
70
- aa
71
- EOB
72
- ).chomp))
73
- )
67
+ assert_equals(
68
+ msg,
69
+ strip_indent(
70
+ <<-EOB
71
+ plain (aa)
72
+ EOB
73
+ ),
74
+ _format(@tok.tokenize(strip_indent(
75
+ <<-EOB
76
+ aa
77
+ EOB
78
+ )))
79
+ )
74
80
 
75
81
  ########
76
- assert_equals( msg, (<<EOB
77
- <plain>aa </>
78
- <quote_double>"bb"</>
79
- EOB
80
- ).chomp,
81
- format(@tok.tokenize((<<EOB
82
- aa "bb"
83
- EOB
84
- ).chomp))
85
- )
82
+ assert_equals(
83
+ msg,
84
+ strip_indent(
85
+ <<-EOB
86
+ plain (aa )
87
+ quote_double ("bb")
88
+ EOB
89
+ ),
90
+ _format(@tok.tokenize(strip_indent(
91
+ <<-EOB
92
+ aa "bb"
93
+ EOB
94
+ )))
95
+ )
86
96
 
87
97
  ########
88
- assert_equals( msg, (<<EOB
89
- <plain>aa </>
90
- <quote_single>'bb'</>
91
- EOB
92
- ).chomp,
93
- format(@tok.tokenize((<<EOB
94
- aa 'bb'
95
- EOB
96
- ).chomp))
97
- )
98
+ assert_equals(
99
+ msg,
100
+ strip_indent(
101
+ <<-EOB
102
+ plain (aa )
103
+ quote_single ('bb')
104
+ EOB
105
+ ),
106
+ _format(@tok.tokenize(strip_indent(
107
+ <<-EOB
108
+ aa 'bb'
109
+ EOB
110
+ )))
111
+ )
98
112
 
99
113
  ########
100
- assert_equals( msg, (<<EOB
101
- <plain>aa </>
102
- <comment_single>--bb<br></>
103
- <plain>cc</>
104
- EOB
105
- ).chomp,
106
- format(@tok.tokenize((<<EOB
107
- aa --bb
108
- cc
109
- EOB
110
- ).chomp))
111
- )
114
+ assert_equals(
115
+ msg,
116
+ strip_indent(
117
+ <<-EOB
118
+ plain (aa )
119
+ comment_single (--bb\n)
120
+ plain (cc)
121
+ EOB
122
+ ),
123
+ _format(@tok.tokenize(strip_indent(
124
+ <<-EOB
125
+ aa --bb
126
+ cc
127
+ EOB
128
+ )))
129
+ )
112
130
 
113
131
  ########
114
- assert_equals( msg, (<<EOB
115
- <plain>aa </>
116
- <comment_multi>/* bb */</>
117
- <plain> cc</>
118
- EOB
119
- ).chomp,
120
- format(@tok.tokenize((<<EOB
121
- aa /* bb */ cc
122
- EOB
123
- ).chomp))
124
- )
132
+ assert_equals(
133
+ msg,
134
+ strip_indent(
135
+ <<-EOB
136
+ plain (aa )
137
+ comment_multi (/* bb */)
138
+ plain ( cc)
139
+ EOB
140
+ ),
141
+ _format(@tok.tokenize(strip_indent(
142
+ <<-EOB
143
+ aa /* bb */ cc
144
+ EOB
145
+ )))
146
+ )
125
147
 
126
148
  ########
127
- assert_equals( msg + "begin with multiline comment", (<<EOB
128
- <comment_multi>/* bb */</>
129
- <plain> cc</>
130
- EOB
131
- ).chomp,
132
- format(@tok.tokenize((<<EOB
133
- /* bb */ cc
134
- EOB
135
- ).chomp))
136
- )
149
+ assert_equals(
150
+ msg + "begin with multiline comment",
151
+ strip_indent(
152
+ <<-EOB
153
+ comment_multi (/* bb */)
154
+ plain ( cc)
155
+ EOB
156
+ ),
157
+ _format(@tok.tokenize(strip_indent(
158
+ <<-EOB
159
+ /* bb */ cc
160
+ EOB
161
+ )))
162
+ )
137
163
  end
138
164
 
139
165
 
@@ -141,74 +167,99 @@ EOB
141
167
  msg = "string_in_string"
142
168
 
143
169
  ########
144
- assert_equals( msg, (<<EOB
145
- <quote_double>"aa'bb'cc"</>
146
- EOB
147
- ).chomp,
148
- format(@tok.tokenize((<<EOB
149
- "aa'bb'cc"
150
- EOB
151
- ).chomp))
152
- )
170
+ assert_equals(
171
+ msg,
172
+ strip_indent(
173
+ <<-EOB
174
+ quote_double ("aa'bb'cc")
175
+ EOB
176
+ ),
177
+ _format(@tok.tokenize(strip_indent(
178
+ <<-EOB
179
+ "aa'bb'cc"
180
+ EOB
181
+ )))
182
+ )
153
183
 
154
184
  ########
155
- assert_equals( msg, (<<EOB
156
- <quote_single>'aa"bb"cc'</>
157
- EOB
158
- ).chomp,
159
- format(@tok.tokenize((<<EOB
160
- 'aa"bb"cc'
161
- EOB
162
- ).chomp))
163
- )
185
+ assert_equals(
186
+ msg,
187
+ strip_indent(
188
+ <<-EOB
189
+ quote_single ('aa"bb"cc')
190
+ EOB
191
+ ),
192
+ _format(@tok.tokenize(strip_indent(
193
+ <<-EOB
194
+ 'aa"bb"cc'
195
+ EOB
196
+ )))
197
+ )
164
198
  end
165
199
 
166
200
 
167
201
  def test_comment_in_comment
168
202
  msg = "comment_in_comment - "
203
+
169
204
  ########
170
- assert_equals( msg, (<<EOB
171
- <comment_single>--a--b</>
172
- EOB
173
- ).chomp,
174
- format(@tok.tokenize((<<EOB
175
- --a--b
176
- EOB
177
- ).chomp))
178
- )
205
+ assert_equals(
206
+ msg,
207
+ strip_indent(
208
+ <<-EOB
209
+ comment_single (--a--b)
210
+ EOB
211
+ ),
212
+ _format(@tok.tokenize(strip_indent(
213
+ <<-EOB
214
+ --a--b
215
+ EOB
216
+ )))
217
+ )
179
218
 
180
219
  ########
181
- assert_equals( msg, (<<EOB
182
- <comment_single>-- aa /* bb */</>
183
- EOB
184
- ).chomp,
185
- format(@tok.tokenize((<<EOB
186
- -- aa /* bb */
187
- EOB
188
- ).chomp))
189
- )
220
+ assert_equals(
221
+ msg,
222
+ strip_indent(
223
+ <<-EOB
224
+ comment_single (-- aa /* bb */)
225
+ EOB
226
+ ),
227
+ _format(@tok.tokenize(strip_indent(
228
+ <<-EOB
229
+ -- aa /* bb */
230
+ EOB
231
+ )))
232
+ )
190
233
 
191
234
  ########
192
- assert_equals( msg, (<<EOB
193
- <comment_multi>/* aa /* bb */</>
194
- EOB
195
- ).chomp,
196
- format(@tok.tokenize((<<EOB
197
- /* aa /* bb */
198
- EOB
199
- ).chomp))
200
- )
235
+ assert_equals(
236
+ msg,
237
+ strip_indent(
238
+ <<-EOB
239
+ comment_multi (/* aa /* bb */)
240
+ EOB
241
+ ),
242
+ _format(@tok.tokenize(strip_indent(
243
+ <<-EOB
244
+ /* aa /* bb */
245
+ EOB
246
+ )))
247
+ )
201
248
 
202
249
  ########
203
- assert_equals( msg, (<<EOB
204
- <comment_single>-- aa /* bb */</>
205
- EOB
206
- ).chomp,
207
- format(@tok.tokenize((<<EOB
208
- -- aa /* bb */
209
- EOB
210
- ).chomp))
211
- )
250
+ assert_equals(
251
+ msg,
252
+ strip_indent(
253
+ <<-EOB
254
+ comment_multi (/* aa -- bb */)
255
+ EOB
256
+ ),
257
+ _format(@tok.tokenize(strip_indent(
258
+ <<-EOB
259
+ /* aa -- bb */
260
+ EOB
261
+ )))
262
+ )
212
263
  end
213
264
 
214
265
 
@@ -216,48 +267,64 @@ EOB
216
267
  msg = "string_in_comment - "
217
268
 
218
269
  ########
219
- assert_equals( msg, (<<EOB
220
- <comment_single>-- aa "bb" cc</>
221
- EOB
222
- ).chomp,
223
- format(@tok.tokenize((<<EOB
224
- -- aa "bb" cc
225
- EOB
226
- ).chomp))
227
- )
270
+ assert_equals(
271
+ msg,
272
+ strip_indent(
273
+ <<-EOB
274
+ comment_single (-- aa "bb" cc)
275
+ EOB
276
+ ),
277
+ _format(@tok.tokenize(strip_indent(
278
+ <<-EOB
279
+ -- aa "bb" cc
280
+ EOB
281
+ )))
282
+ )
228
283
 
229
284
  ########
230
- assert_equals( msg, (<<EOB
231
- <comment_single>-- aa 'bb' cc</>
232
- EOB
233
- ).chomp,
234
- format(@tok.tokenize((<<EOB
235
- -- aa 'bb' cc
236
- EOB
237
- ).chomp))
238
- )
285
+ assert_equals(
286
+ msg,
287
+ strip_indent(
288
+ <<-EOB
289
+ comment_single (-- aa 'bb' cc)
290
+ EOB
291
+ ),
292
+ _format(@tok.tokenize(strip_indent(
293
+ <<-EOB
294
+ -- aa 'bb' cc
295
+ EOB
296
+ )))
297
+ )
239
298
 
240
299
  ########
241
- assert_equals( msg, (<<EOB
242
- <comment_multi>/* aa "bb" cc */</>
243
- EOB
244
- ).chomp,
245
- format(@tok.tokenize((<<EOB
246
- /* aa "bb" cc */
247
- EOB
248
- ).chomp))
249
- )
300
+ assert_equals(
301
+ msg,
302
+ strip_indent(
303
+ <<-EOB
304
+ comment_multi (/* aa "bb" cc */)
305
+ EOB
306
+ ),
307
+ _format(@tok.tokenize(strip_indent(
308
+ <<-EOB
309
+ /* aa "bb" cc */
310
+ EOB
311
+ )))
312
+ )
250
313
 
251
314
  ########
252
- assert_equals( msg, (<<EOB
253
- <comment_multi>/* aa 'bb' cc */</>
254
- EOB
255
- ).chomp,
256
- format(@tok.tokenize((<<EOB
257
- /* aa 'bb' cc */
258
- EOB
259
- ).chomp))
260
- )
315
+ assert_equals(
316
+ msg,
317
+ strip_indent(
318
+ <<-EOB
319
+ comment_multi (/* aa 'bb' cc */)
320
+ EOB
321
+ ),
322
+ _format(@tok.tokenize(strip_indent(
323
+ <<-EOB
324
+ /* aa 'bb' cc */
325
+ EOB
326
+ )))
327
+ )
261
328
  end
262
329
 
263
330
 
@@ -265,48 +332,64 @@ EOB
265
332
  msg = "comment_in_string - "
266
333
 
267
334
  ########
268
- assert_equals( msg + "comment_single in quote_single", (<<EOB
269
- <quote_single>'aa--bb'</>
270
- EOB
271
- ).chomp,
272
- format(@tok.tokenize((<<EOB
273
- 'aa--bb'
274
- EOB
275
- ).chomp))
276
- )
335
+ assert_equals(
336
+ msg + "comment_single in quote_single",
337
+ strip_indent(
338
+ <<-EOB
339
+ quote_single ('aa--bb')
340
+ EOB
341
+ ),
342
+ _format(@tok.tokenize(strip_indent(
343
+ <<-EOB
344
+ 'aa--bb'
345
+ EOB
346
+ )))
347
+ )
277
348
 
278
349
  ########
279
- assert_equals( msg + "comment_single in quote_double", (<<EOB
280
- <quote_double>"aa--bb"</>
281
- EOB
282
- ).chomp,
283
- format(@tok.tokenize((<<EOB
284
- "aa--bb"
285
- EOB
286
- ).chomp))
287
- )
350
+ assert_equals(
351
+ msg + "comment_single in quote_double",
352
+ strip_indent(
353
+ <<-EOB
354
+ quote_double ("aa--bb")
355
+ EOB
356
+ ),
357
+ _format(@tok.tokenize(strip_indent(
358
+ <<-EOB
359
+ "aa--bb"
360
+ EOB
361
+ )))
362
+ )
288
363
 
289
364
  ########
290
- assert_equals( msg + "comment_multi in quote_double", (<<EOB
291
- <quote_double>"aa /* bb */ cc"</>
292
- EOB
293
- ).chomp,
294
- format(@tok.tokenize((<<EOB
295
- "aa /* bb */ cc"
296
- EOB
297
- ).chomp))
298
- )
365
+ assert_equals(
366
+ msg + "comment_multi in quote_double",
367
+ strip_indent(
368
+ <<-EOB
369
+ quote_double ("aa /* bb */ cc")
370
+ EOB
371
+ ),
372
+ _format(@tok.tokenize(strip_indent(
373
+ <<-EOB
374
+ "aa /* bb */ cc"
375
+ EOB
376
+ )))
377
+ )
299
378
 
300
379
  ########
301
- assert_equals( msg + "comment_multi in quote_double", (<<EOB
302
- <quote_single>'aa /* bb */ cc'</>
303
- EOB
304
- ).chomp,
305
- format(@tok.tokenize((<<EOB
306
- 'aa /* bb */ cc'
307
- EOB
308
- ).chomp))
309
- )
380
+ assert_equals(
381
+ msg + "comment_multi in quote_double",
382
+ strip_indent(
383
+ <<-EOB
384
+ quote_single ('aa /* bb */ cc')
385
+ EOB
386
+ ),
387
+ _format(@tok.tokenize(strip_indent(
388
+ <<-EOB
389
+ 'aa /* bb */ cc'
390
+ EOB
391
+ )))
392
+ )
310
393
  end
311
394
 
312
395
 
@@ -314,47 +397,63 @@ EOB
314
397
  msg = "string_escape"
315
398
 
316
399
  ########
317
- assert_equals( msg, (<<EOB
318
- <quote_double>"_a_\\\\_b_<br>_c_\\'_d_"</>
319
- EOB
320
- ).chomp,
321
- format(@tok.tokenize((<<EOB
322
- "_a_\\\\_b_\n_c_\\'_d_"
323
- EOB
324
- ).chomp))
325
- )
400
+ assert_equals(
401
+ msg,
402
+ strip_indent(
403
+ <<-EOB
404
+ quote_double ("_a_\\\\_b_\n_c_\\'_d_")
405
+ EOB
406
+ ),
407
+ _format(@tok.tokenize(strip_indent(
408
+ <<-EOB
409
+ "_a_\\\\_b_\n_c_\\'_d_"
410
+ EOB
411
+ )))
412
+ )
326
413
 
327
414
  ########
328
- assert_equals( msg, (<<EOB
329
- <quote_single>'_a_\\\\_b_<br>_c_\\'_d_'</>
330
- EOB
331
- ).chomp,
332
- format(@tok.tokenize((<<EOB
333
- '_a_\\\\_b_\n_c_\\'_d_'
334
- EOB
335
- ).chomp))
336
- )
415
+ assert_equals(
416
+ msg,
417
+ strip_indent(
418
+ <<-EOB
419
+ quote_single ('_a_\\\\_b_\n_c_\\'_d_')
420
+ EOB
421
+ ),
422
+ _format(@tok.tokenize(strip_indent(
423
+ <<-EOB
424
+ '_a_\\\\_b_\n_c_\\'_d_'
425
+ EOB
426
+ )))
427
+ )
337
428
 
338
429
  ########
339
- assert_equals( msg, (<<EOB
340
- <quote_double>"_a_""_b_"</>
341
- EOB
342
- ).chomp,
343
- format(@tok.tokenize((<<EOB
344
- "_a_""_b_"
345
- EOB
346
- ).chomp))
347
- )
430
+ assert_equals(
431
+ msg,
432
+ strip_indent(
433
+ <<-EOB
434
+ quote_double ("_a_""_b_")
435
+ EOB
436
+ ),
437
+ _format(@tok.tokenize(strip_indent(
438
+ <<-EOB
439
+ "_a_""_b_"
440
+ EOB
441
+ )))
442
+ )
348
443
 
349
444
  ########
350
- assert_equals( msg, (<<EOB
351
- <quote_single>'_a_''_b_'</>
352
- EOB
353
- ).chomp,
354
- format(@tok.tokenize((<<EOB
355
- '_a_''_b_'
356
- EOB
357
- ).chomp))
358
- )
445
+ assert_equals(
446
+ msg,
447
+ strip_indent(
448
+ <<-EOB
449
+ quote_single ('_a_''_b_')
450
+ EOB
451
+ ),
452
+ _format(@tok.tokenize(strip_indent(
453
+ <<-EOB
454
+ '_a_''_b_'
455
+ EOB
456
+ )))
457
+ )
359
458
  end
360
459
  end