ruby_parser 2.0.5 → 3.0.0.a1

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,9 @@
1
1
  #!/usr/local/bin/ruby
2
2
 
3
+ ENV['VERBOSE'] = "1"
4
+
3
5
  require 'rubygems'
6
+ gem "minitest"
4
7
  require 'minitest/autorun'
5
8
  require 'ruby_parser'
6
9
 
@@ -8,13 +11,21 @@ $: << File.expand_path('~/Work/p4/zss/src/ParseTree/dev/test')
8
11
 
9
12
  require 'pt_testcase'
10
13
 
11
- class RubyParser
14
+ class Ruby18Parser # FIX
15
+ def process input
16
+ parse input
17
+ end
18
+ end
19
+
20
+ class Ruby19Parser
12
21
  def process input
13
22
  parse input
14
23
  end
15
24
  end
16
25
 
17
26
  class RubyParserTestCase < ParseTreeTestCase
27
+ attr_accessor :result, :processor
28
+
18
29
  def self.previous key
19
30
  "Ruby"
20
31
  end
@@ -27,88 +38,95 @@ class RubyParserTestCase < ParseTreeTestCase
27
38
 
28
39
  super
29
40
  end
30
- end
31
41
 
32
- class TestRubyParser < RubyParserTestCase
33
- def setup
34
- super
35
-
36
- # puts self.name
42
+ def assert_parse rb, pt
43
+ self.result = processor.parse rb
44
+ assert_equal pt, result
45
+ end
37
46
 
38
- @processor = RubyParser.new
47
+ def assert_parse_line rb, pt, line
48
+ assert_parse rb, pt
49
+ assert_equal line, result.line, "call should have line number"
39
50
  end
51
+ end
40
52
 
53
+ module TestRubyParser
41
54
  def test_attrasgn_array_lhs
42
55
  rb = '[1, 2, 3, 4][from .. to] = ["a", "b", "c"]'
43
56
  pt = s(:attrasgn,
44
57
  s(:array, s(:lit, 1), s(:lit, 2), s(:lit, 3), s(:lit, 4)),
45
58
  :[]=,
46
- s(:arglist,
47
- s(:dot2,
48
- s(:call, nil, :from, s(:arglist)),
49
- s(:call, nil, :to, s(:arglist))),
50
- s(:array, s(:str, "a"), s(:str, "b"), s(:str, "c"))))
51
-
52
- result = @processor.parse(rb)
59
+ s(:dot2,
60
+ s(:call, nil, :from),
61
+ s(:call, nil, :to)),
62
+ s(:array, s(:str, "a"), s(:str, "b"), s(:str, "c")))
53
63
 
54
- assert_equal pt, result
64
+ assert_parse rb, pt
55
65
  end
56
66
 
57
67
  def test_block_append
58
68
  head = s(:args)
59
69
  tail = s(:zsuper)
60
70
  expected = s(:block, s(:args), s(:zsuper))
61
- assert_equal expected, @processor.block_append(head, tail)
71
+ assert_equal expected, processor.block_append(head, tail)
62
72
  end
63
73
 
64
74
  def test_block_append_begin_begin
65
75
  head = s(:begin, s(:args))
66
76
  tail = s(:begin, s(:args))
67
77
  expected = s(:block, s(:args), s(:begin, s(:args)))
68
- assert_equal expected, @processor.block_append(head, tail)
78
+ assert_equal expected, processor.block_append(head, tail)
69
79
  end
70
80
 
71
81
  def test_block_append_block
72
82
  head = s(:block, s(:args))
73
83
  tail = s(:zsuper)
74
84
  expected = s(:block, s(:args), s(:zsuper))
75
- assert_equal expected, @processor.block_append(head, tail)
85
+ assert_equal expected, processor.block_append(head, tail)
76
86
  end
77
87
 
78
88
  def test_block_append_nil_head
79
89
  head = nil
80
90
  tail = s(:zsuper)
81
91
  expected = s(:zsuper)
82
- assert_equal expected, @processor.block_append(head, tail)
92
+ assert_equal expected, processor.block_append(head, tail)
83
93
  end
84
94
 
85
95
  def test_block_append_nil_tail
86
96
  head = s(:args)
87
97
  tail = nil
88
98
  expected = s(:args)
89
- assert_equal expected, @processor.block_append(head, tail)
99
+ assert_equal expected, processor.block_append(head, tail)
90
100
  end
91
101
 
92
102
  def test_block_append_tail_block
93
- head = s(:call, nil, :f1, s(:arglist))
103
+ head = s(:call, nil, :f1)
94
104
  tail = s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y)))
95
105
  expected = s(:block,
96
- s(:call, nil, :f1, s(:arglist)),
106
+ s(:call, nil, :f1),
97
107
  s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y))))
98
- assert_equal expected, @processor.block_append(head, tail)
108
+ assert_equal expected, processor.block_append(head, tail)
109
+ end
110
+
111
+ def test_call_array_arg
112
+ rb = "1 == [:b, :c]"
113
+ pt = s(:call, s(:lit, 1), :==, s(:array, s(:lit, :b), s(:lit, :c)))
114
+
115
+ assert_parse rb, pt
99
116
  end
100
117
 
101
118
  def test_call_env
102
- @processor.env[:a] = :lvar
103
- expected = s(:call, s(:lvar, :a), :happy, s(:arglist))
119
+ processor.env[:a] = :lvar
120
+ rb = "a.happy"
121
+ pt = s(:call, s(:lvar, :a), :happy)
104
122
 
105
- assert_equal expected, @processor.parse('a.happy')
123
+ assert_parse rb, pt
106
124
  end
107
125
 
108
126
  def test_dasgn_icky2
109
127
  rb = "a do\n v = nil\n begin\n yield\n rescue Exception => v\n break\n end\nend"
110
128
  pt = s(:iter,
111
- s(:call, nil, :a, s(:arglist)),
129
+ s(:call, nil, :a),
112
130
  nil,
113
131
  s(:block,
114
132
  s(:lasgn, :v, s(:nil)),
@@ -118,96 +136,125 @@ class TestRubyParser < RubyParserTestCase
118
136
  s(:array, s(:const, :Exception), s(:lasgn, :v, s(:gvar, :$!))),
119
137
  s(:break)))))
120
138
 
121
- assert_equal pt, @processor.parse(rb)
139
+ assert_parse rb, pt
122
140
  end
123
141
 
124
142
  def test_class_comments
125
143
  rb = "# blah 1\n# blah 2\n\nclass X\n # blah 3\n def blah\n # blah 4\n end\nend"
126
144
  pt = s(:class, :X, nil,
127
- s(:scope,
128
- s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))))
145
+ s(:defn, :blah, s(:args), s(:nil)))
129
146
 
130
- actual = @processor.parse(rb)
131
- assert_equal pt, actual
147
+ assert_parse rb, pt
132
148
 
133
- assert_equal "# blah 1\n# blah 2\n\n", actual.comments
134
- assert_equal "# blah 3\n", actual.scope.defn.comments
149
+ assert_equal "# blah 1\n# blah 2\n\n", result.comments
150
+ assert_equal "# blah 3\n", result.defn.comments
135
151
  end
136
152
 
137
153
  def test_module_comments
138
154
  rb = "# blah 1\n \n # blah 2\n\nmodule X\n # blah 3\n def blah\n # blah 4\n end\nend"
139
155
  pt = s(:module, :X,
140
- s(:scope,
141
- s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))))
156
+ s(:defn, :blah, s(:args), s(:nil)))
142
157
 
143
- actual = @processor.parse(rb)
144
- assert_equal pt, actual
145
- assert_equal "# blah 1\n\n# blah 2\n\n", actual.comments
146
- assert_equal "# blah 3\n", actual.scope.defn.comments
158
+ assert_parse rb, pt
159
+ assert_equal "# blah 1\n\n# blah 2\n\n", result.comments
160
+ assert_equal "# blah 3\n", result.defn.comments
147
161
  end
148
162
 
149
163
  def test_defn_comments
150
164
  rb = "# blah 1\n# blah 2\n\ndef blah\nend"
151
- pt = s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))
165
+ pt = s(:defn, :blah, s(:args), s(:nil))
152
166
 
153
- actual = @processor.parse(rb)
154
- assert_equal pt, actual
155
- assert_equal "# blah 1\n# blah 2\n\n", actual.comments
167
+ assert_parse rb, pt
168
+ assert_equal "# blah 1\n# blah 2\n\n", result.comments
156
169
  end
157
170
 
158
171
  def test_defs_comments
159
172
  rb = "# blah 1\n# blah 2\n\ndef self.blah\nend"
160
- pt = s(:defs, s(:self), :blah, s(:args), s(:scope, s(:block)))
173
+ pt = s(:defs, s(:self), :blah, s(:args))
161
174
 
162
- actual = @processor.parse(rb)
163
- assert_equal pt, actual
164
- assert_equal "# blah 1\n# blah 2\n\n", actual.comments
175
+ assert_parse rb, pt
176
+ assert_equal "# blah 1\n# blah 2\n\n", result.comments
165
177
  end
166
178
 
167
179
  def test_do_bug # TODO: rename
168
180
  rb = "a 1\na.b do |c|\n # do nothing\nend"
169
181
  pt = s(:block,
170
- s(:call, nil, :a, s(:arglist, s(:lit, 1))),
182
+ s(:call, nil, :a, s(:lit, 1)),
171
183
  s(:iter,
172
- s(:call, s(:call, nil, :a, s(:arglist)), :b, s(:arglist)),
184
+ s(:call, s(:call, nil, :a), :b),
173
185
  s(:lasgn, :c)))
174
186
 
175
- assert_equal pt, @processor.parse(rb)
187
+ assert_parse rb, pt
188
+ end
189
+
190
+ def test_bug_comment_eq_begin
191
+ rb = "\n\n#\n=begin\nblah\n=end\n\n"
192
+ pt = nil
193
+ exp = rb.strip + "\n"
194
+
195
+ assert_parse rb, pt
196
+ assert_equal exp, processor.lexer.comments
197
+ end
198
+
199
+ def test_bug_call_arglist_parens
200
+ rb = 'g ( 1), 2'
201
+ pt = s(:call, nil, :g, s(:lit, 1), s(:lit, 2))
202
+
203
+ assert_parse rb, pt
204
+
205
+ rb = <<-CODE
206
+ def f
207
+ g ( 1), 2
208
+ end
209
+ CODE
210
+
211
+ pt = s(:defn, :f, s(:args),
212
+ s(:call, nil, :g, s(:lit, 1), s(:lit, 2)))
213
+
214
+ assert_parse rb, pt
215
+
216
+ rb = <<-CODE
217
+ def f()
218
+ g (1), 2
219
+ end
220
+ CODE
221
+
222
+ assert_parse rb, pt
176
223
  end
177
224
 
178
225
  def test_dstr_evstr
179
226
  rb = "\"#\{'a'}#\{b}\""
180
- pt = s(:dstr, "a", s(:evstr, s(:call, nil, :b, s(:arglist))))
227
+ pt = s(:dstr, "a", s(:evstr, s(:call, nil, :b)))
181
228
 
182
- assert_equal pt, @processor.parse(rb)
229
+ assert_parse rb, pt
183
230
  end
184
231
 
185
232
  def test_dstr_str
186
233
  rb = "\"#\{'a'} b\""
187
234
  pt = s(:str, "a b")
188
235
 
189
- assert_equal pt, @processor.parse(rb)
236
+ assert_parse rb, pt
190
237
  end
191
238
 
192
239
  def test_empty
193
240
  rb = ""
194
241
  pt = nil
195
242
 
196
- assert_equal pt, @processor.parse(rb)
243
+ assert_parse rb, pt
197
244
  end
198
245
 
199
246
  def test_evstr_evstr
200
247
  rb = "\"#\{a}#\{b}\""
201
- pt = s(:dstr, "", s(:evstr, s(:call, nil, :a, s(:arglist))), s(:evstr, s(:call, nil, :b, s(:arglist))))
248
+ pt = s(:dstr, "", s(:evstr, s(:call, nil, :a)), s(:evstr, s(:call, nil, :b)))
202
249
 
203
- assert_equal pt, @processor.parse(rb)
250
+ assert_parse rb, pt
204
251
  end
205
252
 
206
253
  def test_evstr_str
207
254
  rb = "\"#\{a} b\""
208
- pt = s(:dstr, "", s(:evstr, s(:call, nil, :a, s(:arglist))), s(:str, " b"))
255
+ pt = s(:dstr, "", s(:evstr, s(:call, nil, :a)), s(:str, " b"))
209
256
 
210
- assert_equal pt, @processor.parse(rb)
257
+ assert_parse rb, pt
211
258
  end
212
259
 
213
260
  def test_lasgn_env
@@ -215,8 +262,8 @@ class TestRubyParser < RubyParserTestCase
215
262
  pt = s(:lasgn, :a, s(:lit, 42))
216
263
  expected_env = { :a => :lvar }
217
264
 
218
- assert_equal pt, @processor.parse(rb)
219
- assert_equal expected_env, @processor.env.all
265
+ assert_parse rb, pt
266
+ assert_equal expected_env, processor.env.all
220
267
  end
221
268
 
222
269
  def test_list_append
@@ -224,22 +271,22 @@ class TestRubyParser < RubyParserTestCase
224
271
  b = s(:lit, 2)
225
272
  c = s(:lit, 3)
226
273
 
227
- result = @processor.list_append(s(:array, b.dup), c.dup)
274
+ result = processor.list_append(s(:array, b.dup), c.dup)
228
275
 
229
276
  assert_equal s(:array, b, c), result
230
277
 
231
- result = @processor.list_append(b.dup, c.dup)
278
+ result = processor.list_append(b.dup, c.dup)
232
279
 
233
280
  assert_equal s(:array, b, c), result
234
281
 
235
- result = @processor.list_append(result, a.dup)
282
+ result = processor.list_append(result, a.dup)
236
283
 
237
284
  assert_equal s(:array, b, c, a), result
238
285
 
239
286
  lhs, rhs = s(:array, s(:lit, :iter)), s(:when, s(:const, :BRANCHING), nil)
240
287
  expected = s(:array, s(:lit, :iter), s(:when, s(:const, :BRANCHING), nil))
241
288
 
242
- assert_equal expected, @processor.list_append(lhs, rhs)
289
+ assert_equal expected, processor.list_append(lhs, rhs)
243
290
  end
244
291
 
245
292
  def test_list_prepend
@@ -247,56 +294,56 @@ class TestRubyParser < RubyParserTestCase
247
294
  b = s(:lit, 2)
248
295
  c = s(:lit, 3)
249
296
 
250
- result = @processor.list_prepend(b.dup, s(:array, c.dup))
297
+ result = processor.list_prepend(b.dup, s(:array, c.dup))
251
298
 
252
299
  assert_equal s(:array, b, c), result
253
300
 
254
- result = @processor.list_prepend(b.dup, c.dup)
301
+ result = processor.list_prepend(b.dup, c.dup)
255
302
 
256
303
  assert_equal s(:array, b, c), result
257
304
 
258
- result = @processor.list_prepend(a.dup, result)
305
+ result = processor.list_prepend(a.dup, result)
259
306
 
260
307
  assert_equal s(:array, a, b, c), result
261
308
  end
262
309
 
263
310
  def test_literal_concat_dstr_dstr
264
311
  lhs = s(:dstr, "Failed to download spec ",
265
- s(:evstr, s(:call, nil, :spec_name, s(:arglist))),
312
+ s(:evstr, s(:call, nil, :spec_name)),
266
313
  s(:str, " from "),
267
- s(:evstr, s(:call, nil, :source_uri, s(:arglist))),
314
+ s(:evstr, s(:call, nil, :source_uri)),
268
315
  s(:str, ":\n"))
269
316
  rhs = s(:dstr, "\t",
270
317
  s(:evstr, s(:call, s(:ivar, :@fetch_error), :message)))
271
318
  expected = s(:dstr, "Failed to download spec ",
272
- s(:evstr, s(:call, nil, :spec_name, s(:arglist))),
319
+ s(:evstr, s(:call, nil, :spec_name)),
273
320
  s(:str, " from "),
274
- s(:evstr, s(:call, nil, :source_uri, s(:arglist))),
321
+ s(:evstr, s(:call, nil, :source_uri)),
275
322
  s(:str, ":\n"),
276
323
  s(:str, "\t"),
277
324
  s(:evstr, s(:call, s(:ivar, :@fetch_error), :message)))
278
325
 
279
- assert_equal expected, @processor.literal_concat(lhs, rhs)
326
+ assert_equal expected, processor.literal_concat(lhs, rhs)
280
327
  end
281
328
 
282
329
  def test_literal_concat_dstr_evstr
283
- lhs, rhs = s(:dstr, "a"), s(:evstr, s(:call, nil, :b, s(:arglist)))
284
- expected = s(:dstr, "a", s(:evstr, s(:call, nil, :b, s(:arglist))))
330
+ lhs, rhs = s(:dstr, "a"), s(:evstr, s(:call, nil, :b))
331
+ expected = s(:dstr, "a", s(:evstr, s(:call, nil, :b)))
285
332
 
286
- assert_equal expected, @processor.literal_concat(lhs, rhs)
333
+ assert_equal expected, processor.literal_concat(lhs, rhs)
287
334
  end
288
335
 
289
336
  def test_literal_concat_evstr_evstr
290
337
  lhs, rhs = s(:evstr, s(:lit, 1)), s(:evstr, s(:lit, 2))
291
338
  expected = s(:dstr, "", s(:evstr, s(:lit, 1)), s(:evstr, s(:lit, 2)))
292
339
 
293
- assert_equal expected, @processor.literal_concat(lhs, rhs)
340
+ assert_equal expected, processor.literal_concat(lhs, rhs)
294
341
  end
295
342
 
296
343
  def test_literal_concat_str_evstr
297
344
  lhs, rhs = s(:str, ""), s(:evstr, s(:str, "blah"))
298
345
 
299
- assert_equal s(:str, "blah"), @processor.literal_concat(lhs, rhs)
346
+ assert_equal s(:str, "blah"), processor.literal_concat(lhs, rhs)
300
347
  end
301
348
 
302
349
  def test_logop_12
@@ -304,7 +351,7 @@ class TestRubyParser < RubyParserTestCase
304
351
  rhs = s(:lit, 2)
305
352
  exp = s(:and, s(:lit, 1), s(:lit, 2))
306
353
 
307
- assert_equal exp, @processor.logop(:and, lhs, rhs)
354
+ assert_equal exp, processor.logop(:and, lhs, rhs)
308
355
  end
309
356
 
310
357
  def test_logop_1234_5
@@ -320,7 +367,7 @@ class TestRubyParser < RubyParserTestCase
320
367
  s(:lit, 4),
321
368
  s(:lit, 5)))))
322
369
 
323
- assert_equal exp, @processor.logop(:and, lhs, rhs)
370
+ assert_equal exp, processor.logop(:and, lhs, rhs)
324
371
  end
325
372
 
326
373
  def test_logop_123_4
@@ -334,7 +381,7 @@ class TestRubyParser < RubyParserTestCase
334
381
  s(:lit, 3),
335
382
  s(:lit, 4))))
336
383
 
337
- assert_equal exp, @processor.logop(:and, lhs, rhs)
384
+ assert_equal exp, processor.logop(:and, lhs, rhs)
338
385
  end
339
386
 
340
387
  def test_logop_12_3
@@ -342,27 +389,37 @@ class TestRubyParser < RubyParserTestCase
342
389
  rhs = s(:lit, 3)
343
390
  exp = s(:and, s(:lit, 1), s(:and, s(:lit, 2), s(:lit, 3)))
344
391
 
345
- assert_equal exp, @processor.logop(:and, lhs, rhs)
392
+ assert_equal exp, processor.logop(:and, lhs, rhs)
346
393
  end
347
394
 
348
395
  def test_logop_nested_mix
349
- lhs = s(:or, s(:call, nil, :a, s(:arglist)), s(:call, nil, :b, s(:arglist)))
350
- rhs = s(:and, s(:call, nil, :c, s(:arglist)), s(:call, nil, :d, s(:arglist)))
396
+ lhs = s(:or, s(:call, nil, :a), s(:call, nil, :b))
397
+ rhs = s(:and, s(:call, nil, :c), s(:call, nil, :d))
351
398
  exp = s(:or,
352
- s(:or, s(:call, nil, :a, s(:arglist)), s(:call, nil, :b, s(:arglist))),
353
- s(:and, s(:call, nil, :c, s(:arglist)), s(:call, nil, :d, s(:arglist))))
399
+ s(:or, s(:call, nil, :a), s(:call, nil, :b)),
400
+ s(:and, s(:call, nil, :c), s(:call, nil, :d)))
354
401
 
355
402
  lhs.paren = true
356
403
  rhs.paren = true
357
404
 
358
- assert_equal exp, @processor.logop(:or, lhs, rhs)
405
+ assert_equal exp, processor.logop(:or, lhs, rhs)
359
406
  end
360
407
 
361
408
  def test_str_evstr
362
409
  rb = "\"a #\{b}\""
363
- pt = s(:dstr, "a ", s(:evstr, s(:call, nil, :b, s(:arglist))))
410
+ pt = s(:dstr, "a ", s(:evstr, s(:call, nil, :b)))
411
+
412
+ assert_parse rb, pt
413
+ end
414
+
415
+ def test_dsym_to_sym
416
+ pt = s(:alias, s(:lit, :<<), s(:lit, :>>))
364
417
 
365
- assert_equal pt, @processor.parse(rb)
418
+ rb = 'alias :<< :>>'
419
+ assert_parse rb, pt
420
+
421
+ rb = 'alias :"<<" :">>"'
422
+ assert_parse rb, pt
366
423
  end
367
424
 
368
425
  def test_regexp
@@ -375,7 +432,7 @@ class TestRubyParser < RubyParserTestCase
375
432
  }
376
433
 
377
434
  regexps.each do |rb, lit|
378
- assert_equal s(:lit, lit), @processor.parse(rb)
435
+ assert_parse rb, s(:lit, lit)
379
436
  end
380
437
 
381
438
  # TODO: add more including interpolation etc
@@ -383,43 +440,44 @@ class TestRubyParser < RubyParserTestCase
383
440
 
384
441
  def test_str_pct_Q_nested
385
442
  rb = "%Q[before [#\{nest}] after]"
386
- pt = s(:dstr, "before [", s(:evstr, s(:call, nil, :nest, s(:arglist))), s(:str, "] after"))
443
+ pt = s(:dstr, "before [", s(:evstr, s(:call, nil, :nest)), s(:str, "] after"))
387
444
 
388
- assert_equal pt, @processor.parse(rb)
445
+ assert_parse rb, pt
389
446
  end
390
447
 
391
- # def test_str_pct_nested_nested
392
- # rb = "%{ { #\{ \"#\{1}\" } } }"
393
- # pt = s(:dstr, " { ", s(:evstr, s(:lit, 1)), s(:str, " } "))
394
-
395
- # assert_equal pt, @processor.parse(rb)
396
- # end
448
+ # def test_str_pct_nested_nested
449
+ # rb = "%{ { #\{ \"#\{1}\" } } }"
450
+ # assert_equal " { 1 } ", eval(rb)
451
+ # pt = s(:dstr, " { ", s(:evstr, s(:lit, 1)), s(:str, " } "))
452
+ #
453
+ # assert_parse rb, pt
454
+ # end
397
455
 
398
456
  def test_str_str
399
457
  rb = "\"a #\{'b'}\""
400
458
  pt = s(:str, "a b")
401
459
 
402
- assert_equal pt, @processor.parse(rb)
460
+ assert_parse rb, pt
403
461
  end
404
462
 
405
463
  def test_str_str_str
406
464
  rb = "\"a #\{'b'} c\""
407
465
  pt = s(:str, "a b c")
408
466
 
409
- assert_equal pt, @processor.parse(rb)
467
+ assert_parse rb, pt
410
468
  end
411
469
 
412
470
  STARTING_LINE = {
413
471
  "case_nested_inner_no_expr" => 2,
414
472
  "case_no_expr" => 2,
415
473
  "case_splat" => 2,
416
- "dstr_heredoc_expand" => 2,
417
- "dstr_heredoc_windoze_sucks" => 2,
418
- "dstr_heredoc_yet_again" => 2,
419
- "str_heredoc" => 2,
420
- "str_heredoc_call" => 2,
421
- "str_heredoc_empty" => 2,
422
- "str_heredoc_indent" => 2,
474
+ "dstr_heredoc_expand" => 1,
475
+ "dstr_heredoc_windoze_sucks" => 1,
476
+ "dstr_heredoc_yet_again" => 1,
477
+ "str_heredoc" => 1,
478
+ "str_heredoc_call" => 1,
479
+ "str_heredoc_empty" => 1,
480
+ "str_heredoc_indent" => 1,
423
481
  "structure_unused_literal_wwtt" => 3, # yes, 3... odd test
424
482
  "undef_block_1" => 2,
425
483
  "undef_block_2" => 2,
@@ -432,22 +490,17 @@ class TestRubyParser < RubyParserTestCase
432
490
  assert_equal expected, @result.line, "should have proper line number"
433
491
  end
434
492
 
435
- def test_position_info
493
+ def test_parse_line_block
436
494
  rb = "a = 42\np a"
437
495
  pt = s(:block,
438
496
  s(:lasgn, :a, s(:lit, 42)),
439
- s(:call, nil, :p, s(:arglist, s(:lvar, :a))))
497
+ s(:call, nil, :p, s(:lvar, :a)))
440
498
 
441
- result = @processor.parse(rb, "blah.rb")
442
-
443
- assert_equal pt, result
444
-
445
- assert_equal 1, result.line, "block should have line number"
499
+ assert_parse_line rb, pt, 1
446
500
  assert_equal 1, result.lasgn.line, "lasgn should have line number"
447
501
  assert_equal 2, result.call.line, "call should have line number"
448
502
 
449
- expected = "blah.rb"
450
-
503
+ expected = "(string)"
451
504
  assert_equal expected, result.file
452
505
  assert_equal expected, result.lasgn.file
453
506
  assert_equal expected, result.call.file
@@ -456,25 +509,237 @@ class TestRubyParser < RubyParserTestCase
456
509
  assert_same result.file, result.call.file
457
510
  end
458
511
 
459
- def test_position_info2
460
- rb = "def x(y)\n p(y)\n y *= 2\n return y;\nend" # TODO: remove () & ;
461
- pt = s(:defn, :x, s(:args, :y),
462
- s(:scope,
463
- s(:block,
464
- s(:call, nil, :p, s(:arglist, s(:lvar, :y))),
465
- s(:lasgn, :y,
466
- s(:call, s(:lvar, :y), :*, s(:arglist, s(:lit, 2)))),
467
- s(:return, s(:lvar, :y)))))
512
+ def test_parse_line_call_no_args
513
+ rb = "f do |x, y|\n x + y\nend"
468
514
 
469
- result = @processor.parse(rb)
515
+ pt = s(:iter,
516
+ s(:call, nil, :f),
517
+ s(:masgn, s(:array, s(:lasgn, :x), s(:lasgn, :y))),
518
+ s(:call, s(:lvar, :x), :+, s(:lvar, :y)))
470
519
 
471
- assert_equal pt, result
520
+ assert_parse_line rb, pt, 1
521
+ assert_equal 1, result[1].line, "call should have line number"
522
+ assert_equal 1, result[2].line, "masgn should have line number"
523
+ assert_equal 2, result[3].line, "call should have line number"
524
+ end
472
525
 
473
- body = result.scope.block
526
+ def test_parse_line_defn_no_parens
527
+ pt = s(:defn, :f, s(:args), s(:nil))
474
528
 
475
- assert_equal 1, result.line, "defn should have line number"
529
+ rb = "def f\nend"
530
+ assert_parse_line rb, pt, 1
531
+
532
+ rb = "def f\n\nend"
533
+ assert_parse_line rb, pt, 1
534
+ end
535
+
536
+ def test_parse_line_defn_complex
537
+ rb = "def x(y)\n p(y)\n y *= 2\n return y;\nend" # TODO: remove () & ;
538
+ pt = s(:defn, :x, s(:args, :y),
539
+ s(:call, nil, :p, s(:lvar, :y)),
540
+ s(:lasgn, :y, s(:call, s(:lvar, :y), :*, s(:lit, 2))),
541
+ s(:return, s(:lvar, :y)))
542
+
543
+ assert_parse_line rb, pt, 1
544
+
545
+ body = result
476
546
  assert_equal 2, body.call.line, "call should have line number"
477
547
  assert_equal 3, body.lasgn.line, "lasgn should have line number"
478
548
  assert_equal 4, body.return.line, "return should have line number"
479
549
  end
550
+
551
+ def test_parse_line_iter_call_parens
552
+ rb = "f(a) do |x, y|\n x + y\nend"
553
+
554
+ pt = s(:iter,
555
+ s(:call, nil, :f, s(:call, nil, :a)),
556
+ s(:masgn, s(:array, s(:lasgn, :x), s(:lasgn, :y))),
557
+ s(:call, s(:lvar, :x), :+, s(:lvar, :y)))
558
+
559
+ assert_parse_line rb, pt, 1
560
+
561
+ assert_equal 1, result[1].line, "call should have line number"
562
+ assert_equal 1, result[2].line, "masgn should have line number"
563
+ assert_equal 2, result[3].line, "call should have line number"
564
+ end
565
+
566
+ def test_parse_line_iter_call_no_parens
567
+ rb = "f a do |x, y|\n x + y\nend"
568
+
569
+ pt = s(:iter,
570
+ s(:call, nil, :f, s(:call, nil, :a)),
571
+ s(:masgn, s(:array, s(:lasgn, :x), s(:lasgn, :y))),
572
+ s(:call, s(:lvar, :x), :+, s(:lvar, :y)))
573
+
574
+ assert_parse_line rb, pt, 1
575
+
576
+ assert_equal 1, result[1].line, "call should have line number"
577
+ assert_equal 1, result[2].line, "masgn should have line number"
578
+ assert_equal 2, result[3].line, "call should have line number"
579
+ end
580
+
581
+ def test_parse_line_heredoc
582
+ rb = <<-CODE
583
+ string = <<-HEREDOC
584
+ very long string
585
+ HEREDOC
586
+ puts string
587
+ CODE
588
+
589
+ result = processor.parse rb
590
+ assert_equal 1, result.lasgn.line
591
+ assert_equal 4, result.call.line
592
+ end
593
+
594
+ def test_parse_line_newlines
595
+ rb = "true\n\n"
596
+ pt = s(:true)
597
+
598
+ assert_parse_line rb, pt, 1
599
+ end
600
+
601
+ def test_parse_line_return
602
+ rb = <<-RUBY
603
+ def blah
604
+ if true then
605
+ return 42
606
+ end
607
+ end
608
+ RUBY
609
+
610
+ pt = s(:defn, :blah, s(:args),
611
+ s(:if, s(:true),
612
+ s(:return, s(:lit, 42)),
613
+ nil))
614
+
615
+ assert_parse_line rb, pt, 1
616
+
617
+ assert_equal 3, result.if.return.line
618
+ assert_equal 3, result.if.return.lit.line
619
+ end
620
+
621
+ def test_parse_if_not_canonical
622
+ rb = "if not var.nil? then 'foo' else 'bar'\nend"
623
+ pt = s(:if,
624
+ s(:call, s(:call, nil, :var), :nil?),
625
+ s(:str, "bar"),
626
+ s(:str, "foo"))
627
+
628
+ assert_parse rb, pt
629
+ end
630
+
631
+ def test_parse_if_not_noncanonical
632
+ rb = "if not var.nil? then 'foo' else 'bar'\nend"
633
+ pt = s(:if,
634
+ s(:not,
635
+ s(:call, s(:call, nil, :var), :nil?)),
636
+ s(:str, "foo"),
637
+ s(:str, "bar"))
638
+
639
+ processor.canonicalize_conditions = false
640
+
641
+ assert_parse rb, pt
642
+ end
643
+
644
+ def test_parse_while_not_canonical
645
+ rb = "while not var.nil?\n 'foo'\nend"
646
+ pt = s(:until,
647
+ s(:call, s(:call, nil, :var), :nil?),
648
+ s(:str, "foo"), true)
649
+
650
+ assert_parse rb, pt
651
+ end
652
+
653
+ def test_parse_while_not_noncanonical
654
+ rb = "while not var.nil?\n 'foo'\nend"
655
+ pt = s(:while,
656
+ s(:not,
657
+ s(:call, s(:call, nil, :var), :nil?)),
658
+ s(:str, "foo"), true)
659
+
660
+ processor.canonicalize_conditions = false
661
+
662
+ assert_parse rb, pt
663
+ end
664
+
665
+ def test_parse_until_not_canonical
666
+ rb = "until not var.nil?\n 'foo'\nend"
667
+
668
+ pt = s(:while,
669
+ s(:call, s(:call, nil, :var), :nil?),
670
+ s(:str, "foo"), true)
671
+
672
+ assert_parse rb, pt
673
+ end
674
+
675
+ def test_parse_until_not_noncanonical
676
+ rb = "until not var.nil?\n 'foo'\nend"
677
+ pt = s(:until,
678
+ s(:not,
679
+ s(:call, s(:call, nil, :var), :nil?)),
680
+ s(:str, "foo"), true)
681
+
682
+ processor.canonicalize_conditions = false
683
+
684
+ assert_parse rb, pt
685
+ end
480
686
  end
687
+
688
+ class TestRuby18Parser < RubyParserTestCase
689
+ include TestRubyParser
690
+
691
+ def setup
692
+ super
693
+
694
+ self.processor = Ruby18Parser.new
695
+ end
696
+
697
+ def test_flip2_env_lvar
698
+ rb = "if a..b then end"
699
+ pt = s(:if, s(:flip2, s(:call, nil, :a), s(:call, nil, :b)), nil, nil)
700
+
701
+ assert_parse rb, pt
702
+
703
+ top_env = processor.env.env.first
704
+
705
+ assert_kind_of Hash, top_env
706
+
707
+ flip = top_env.find { |k,v| k =~ /^flip/ }
708
+
709
+ assert flip
710
+ assert_equal :lvar, flip.last
711
+ end
712
+ end
713
+
714
+ class TestRuby19Parser < RubyParserTestCase
715
+ include TestRubyParser
716
+
717
+ def setup
718
+ super
719
+
720
+ self.processor = Ruby19Parser.new
721
+ end
722
+
723
+ # HACK: need to figure out the desired structure and get this working
724
+ # def test_wtf
725
+ # # lambda -> f_larglist lambda_body
726
+ # # f_larglist -> f_args opt_bv_decl
727
+ # # opt_bv_decl
728
+ # # bv_decls
729
+ # # bvar
730
+ #
731
+ # rb = "->(a, b=nil) { p [a, b] }"
732
+ # pt = s(:iter,
733
+ # s(:call, nil, :lambda),
734
+ # s(:args, :a, :b,
735
+ # s(:block, s(:lasgn, :b, s(nil)))),
736
+ # s(:call, nil, :p, s(:array, s(:lvar, :a), s(:lvar, :b))))
737
+ #
738
+ # assert_parse rb, pt
739
+ #
740
+ # rb = "->(a; b) { p [a, b] }"
741
+ #
742
+ # assert_parse rb, pt
743
+ # end
744
+ end
745
+