rb-ruby_parser 2.0.4.1

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.
@@ -0,0 +1,480 @@
1
+ #!/usr/local/bin/ruby
2
+
3
+ require 'rubygems'
4
+ require 'minitest/autorun'
5
+ require 'ruby_parser'
6
+
7
+ $: << File.expand_path('~/Work/p4/zss/src/ParseTree/dev/test')
8
+
9
+ require 'pt_testcase'
10
+
11
+ class RubyParser
12
+ def process input
13
+ parse input
14
+ end
15
+ end
16
+
17
+ class RubyParserTestCase < ParseTreeTestCase
18
+ def self.previous key
19
+ "Ruby"
20
+ end
21
+
22
+ def self.generate_test klass, node, data, input_name, output_name
23
+ return if node.to_s =~ /bmethod|dmethod/
24
+ return if Array === data['Ruby']
25
+
26
+ output_name = "ParseTree"
27
+
28
+ super
29
+ end
30
+ end
31
+
32
+ class TestRubyParser < RubyParserTestCase
33
+ def setup
34
+ super
35
+
36
+ # puts self.name
37
+
38
+ @processor = RubyParser.new
39
+ end
40
+
41
+ def test_attrasgn_array_lhs
42
+ rb = '[1, 2, 3, 4][from .. to] = ["a", "b", "c"]'
43
+ pt = s(:attrasgn,
44
+ s(:array, s(:lit, 1), s(:lit, 2), s(:lit, 3), s(:lit, 4)),
45
+ :[]=,
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)
53
+
54
+ assert_equal pt, result
55
+ end
56
+
57
+ def test_block_append
58
+ head = s(:args)
59
+ tail = s(:zsuper)
60
+ expected = s(:block, s(:args), s(:zsuper))
61
+ assert_equal expected, @processor.block_append(head, tail)
62
+ end
63
+
64
+ def test_block_append_begin_begin
65
+ head = s(:begin, s(:args))
66
+ tail = s(:begin, s(:args))
67
+ expected = s(:block, s(:args), s(:begin, s(:args)))
68
+ assert_equal expected, @processor.block_append(head, tail)
69
+ end
70
+
71
+ def test_block_append_block
72
+ head = s(:block, s(:args))
73
+ tail = s(:zsuper)
74
+ expected = s(:block, s(:args), s(:zsuper))
75
+ assert_equal expected, @processor.block_append(head, tail)
76
+ end
77
+
78
+ def test_block_append_nil_head
79
+ head = nil
80
+ tail = s(:zsuper)
81
+ expected = s(:zsuper)
82
+ assert_equal expected, @processor.block_append(head, tail)
83
+ end
84
+
85
+ def test_block_append_nil_tail
86
+ head = s(:args)
87
+ tail = nil
88
+ expected = s(:args)
89
+ assert_equal expected, @processor.block_append(head, tail)
90
+ end
91
+
92
+ def test_block_append_tail_block
93
+ head = s(:call, nil, :f1, s(:arglist))
94
+ tail = s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y)))
95
+ expected = s(:block,
96
+ s(:call, nil, :f1, s(:arglist)),
97
+ s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y))))
98
+ assert_equal expected, @processor.block_append(head, tail)
99
+ end
100
+
101
+ def test_call_env
102
+ @processor.env[:a] = :lvar
103
+ expected = s(:call, s(:lvar, :a), :happy, s(:arglist))
104
+
105
+ assert_equal expected, @processor.parse('a.happy')
106
+ end
107
+
108
+ def test_dasgn_icky2
109
+ rb = "a do\n v = nil\n begin\n yield\n rescue Exception => v\n break\n end\nend"
110
+ pt = s(:iter,
111
+ s(:call, nil, :a, s(:arglist)),
112
+ nil,
113
+ s(:block,
114
+ s(:lasgn, :v, s(:nil)),
115
+ s(:rescue,
116
+ s(:yield),
117
+ s(:resbody,
118
+ s(:array, s(:const, :Exception), s(:lasgn, :v, s(:gvar, :$!))),
119
+ s(:break)))))
120
+
121
+ assert_equal pt, @processor.parse(rb)
122
+ end
123
+
124
+ def test_class_comments
125
+ rb = "# blah 1\n# blah 2\n\nclass X\n # blah 3\n def blah\n # blah 4\n end\nend"
126
+ pt = s(:class, :X, nil,
127
+ s(:scope,
128
+ s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))))
129
+
130
+ actual = @processor.parse(rb)
131
+ assert_equal pt, actual
132
+
133
+ assert_equal "# blah 1\n# blah 2\n\n", actual.comments
134
+ assert_equal "# blah 3\n", actual.scope.defn.comments
135
+ end
136
+
137
+ def test_module_comments
138
+ rb = "# blah 1\n \n # blah 2\n\nmodule X\n # blah 3\n def blah\n # blah 4\n end\nend"
139
+ pt = s(:module, :X,
140
+ s(:scope,
141
+ s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))))
142
+
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
147
+ end
148
+
149
+ def test_defn_comments
150
+ rb = "# blah 1\n# blah 2\n\ndef blah\nend"
151
+ pt = s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))
152
+
153
+ actual = @processor.parse(rb)
154
+ assert_equal pt, actual
155
+ assert_equal "# blah 1\n# blah 2\n\n", actual.comments
156
+ end
157
+
158
+ def test_defs_comments
159
+ rb = "# blah 1\n# blah 2\n\ndef self.blah\nend"
160
+ pt = s(:defs, s(:self), :blah, s(:args), s(:scope, s(:block)))
161
+
162
+ actual = @processor.parse(rb)
163
+ assert_equal pt, actual
164
+ assert_equal "# blah 1\n# blah 2\n\n", actual.comments
165
+ end
166
+
167
+ def test_do_bug # TODO: rename
168
+ rb = "a 1\na.b do |c|\n # do nothing\nend"
169
+ pt = s(:block,
170
+ s(:call, nil, :a, s(:arglist, s(:lit, 1))),
171
+ s(:iter,
172
+ s(:call, s(:call, nil, :a, s(:arglist)), :b, s(:arglist)),
173
+ s(:lasgn, :c)))
174
+
175
+ assert_equal pt, @processor.parse(rb)
176
+ end
177
+
178
+ def test_dstr_evstr
179
+ rb = "\"#\{'a'}#\{b}\""
180
+ pt = s(:dstr, "a", s(:evstr, s(:call, nil, :b, s(:arglist))))
181
+
182
+ assert_equal pt, @processor.parse(rb)
183
+ end
184
+
185
+ def test_dstr_str
186
+ rb = "\"#\{'a'} b\""
187
+ pt = s(:str, "a b")
188
+
189
+ assert_equal pt, @processor.parse(rb)
190
+ end
191
+
192
+ def test_empty
193
+ rb = ""
194
+ pt = nil
195
+
196
+ assert_equal pt, @processor.parse(rb)
197
+ end
198
+
199
+ def test_evstr_evstr
200
+ rb = "\"#\{a}#\{b}\""
201
+ pt = s(:dstr, "", s(:evstr, s(:call, nil, :a, s(:arglist))), s(:evstr, s(:call, nil, :b, s(:arglist))))
202
+
203
+ assert_equal pt, @processor.parse(rb)
204
+ end
205
+
206
+ def test_evstr_str
207
+ rb = "\"#\{a} b\""
208
+ pt = s(:dstr, "", s(:evstr, s(:call, nil, :a, s(:arglist))), s(:str, " b"))
209
+
210
+ assert_equal pt, @processor.parse(rb)
211
+ end
212
+
213
+ def test_lasgn_env
214
+ rb = 'a = 42'
215
+ pt = s(:lasgn, :a, s(:lit, 42))
216
+ expected_env = { :a => :lvar }
217
+
218
+ assert_equal pt, @processor.parse(rb)
219
+ assert_equal expected_env, @processor.env.all
220
+ end
221
+
222
+ def test_list_append
223
+ a = s(:lit, 1)
224
+ b = s(:lit, 2)
225
+ c = s(:lit, 3)
226
+
227
+ result = @processor.list_append(s(:array, b.dup), c.dup)
228
+
229
+ assert_equal s(:array, b, c), result
230
+
231
+ result = @processor.list_append(b.dup, c.dup)
232
+
233
+ assert_equal s(:array, b, c), result
234
+
235
+ result = @processor.list_append(result, a.dup)
236
+
237
+ assert_equal s(:array, b, c, a), result
238
+
239
+ lhs, rhs = s(:array, s(:lit, :iter)), s(:when, s(:const, :BRANCHING), nil)
240
+ expected = s(:array, s(:lit, :iter), s(:when, s(:const, :BRANCHING), nil))
241
+
242
+ assert_equal expected, @processor.list_append(lhs, rhs)
243
+ end
244
+
245
+ def test_list_prepend
246
+ a = s(:lit, 1)
247
+ b = s(:lit, 2)
248
+ c = s(:lit, 3)
249
+
250
+ result = @processor.list_prepend(b.dup, s(:array, c.dup))
251
+
252
+ assert_equal s(:array, b, c), result
253
+
254
+ result = @processor.list_prepend(b.dup, c.dup)
255
+
256
+ assert_equal s(:array, b, c), result
257
+
258
+ result = @processor.list_prepend(a.dup, result)
259
+
260
+ assert_equal s(:array, a, b, c), result
261
+ end
262
+
263
+ def test_literal_concat_dstr_dstr
264
+ lhs = s(:dstr, "Failed to download spec ",
265
+ s(:evstr, s(:call, nil, :spec_name, s(:arglist))),
266
+ s(:str, " from "),
267
+ s(:evstr, s(:call, nil, :source_uri, s(:arglist))),
268
+ s(:str, ":\n"))
269
+ rhs = s(:dstr, "\t",
270
+ s(:evstr, s(:call, s(:ivar, :@fetch_error), :message)))
271
+ expected = s(:dstr, "Failed to download spec ",
272
+ s(:evstr, s(:call, nil, :spec_name, s(:arglist))),
273
+ s(:str, " from "),
274
+ s(:evstr, s(:call, nil, :source_uri, s(:arglist))),
275
+ s(:str, ":\n"),
276
+ s(:str, "\t"),
277
+ s(:evstr, s(:call, s(:ivar, :@fetch_error), :message)))
278
+
279
+ assert_equal expected, @processor.literal_concat(lhs, rhs)
280
+ end
281
+
282
+ 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))))
285
+
286
+ assert_equal expected, @processor.literal_concat(lhs, rhs)
287
+ end
288
+
289
+ def test_literal_concat_evstr_evstr
290
+ lhs, rhs = s(:evstr, s(:lit, 1)), s(:evstr, s(:lit, 2))
291
+ expected = s(:dstr, "", s(:evstr, s(:lit, 1)), s(:evstr, s(:lit, 2)))
292
+
293
+ assert_equal expected, @processor.literal_concat(lhs, rhs)
294
+ end
295
+
296
+ def test_literal_concat_str_evstr
297
+ lhs, rhs = s(:str, ""), s(:evstr, s(:str, "blah"))
298
+
299
+ assert_equal s(:str, "blah"), @processor.literal_concat(lhs, rhs)
300
+ end
301
+
302
+ def test_logop_12
303
+ lhs = s(:lit, 1)
304
+ rhs = s(:lit, 2)
305
+ exp = s(:and, s(:lit, 1), s(:lit, 2))
306
+
307
+ assert_equal exp, @processor.logop(:and, lhs, rhs)
308
+ end
309
+
310
+ def test_logop_1234_5
311
+ lhs = s(:and, s(:lit, 1), s(:and, s(:lit, 2), s(:and, s(:lit, 3), s(:lit, 4))))
312
+ rhs = s(:lit, 5)
313
+ exp = s(:and,
314
+ s(:lit, 1),
315
+ s(:and,
316
+ s(:lit, 2),
317
+ s(:and,
318
+ s(:lit, 3),
319
+ s(:and,
320
+ s(:lit, 4),
321
+ s(:lit, 5)))))
322
+
323
+ assert_equal exp, @processor.logop(:and, lhs, rhs)
324
+ end
325
+
326
+ def test_logop_123_4
327
+ lhs = s(:and, s(:lit, 1), s(:and, s(:lit, 2), s(:lit, 3)))
328
+ rhs = s(:lit, 4)
329
+ exp = s(:and,
330
+ s(:lit, 1),
331
+ s(:and,
332
+ s(:lit, 2),
333
+ s(:and,
334
+ s(:lit, 3),
335
+ s(:lit, 4))))
336
+
337
+ assert_equal exp, @processor.logop(:and, lhs, rhs)
338
+ end
339
+
340
+ def test_logop_12_3
341
+ lhs = s(:and, s(:lit, 1), s(:lit, 2))
342
+ rhs = s(:lit, 3)
343
+ exp = s(:and, s(:lit, 1), s(:and, s(:lit, 2), s(:lit, 3)))
344
+
345
+ assert_equal exp, @processor.logop(:and, lhs, rhs)
346
+ end
347
+
348
+ 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)))
351
+ 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))))
354
+
355
+ lhs.paren = true
356
+ rhs.paren = true
357
+
358
+ assert_equal exp, @processor.logop(:or, lhs, rhs)
359
+ end
360
+
361
+ def test_str_evstr
362
+ rb = "\"a #\{b}\""
363
+ pt = s(:dstr, "a ", s(:evstr, s(:call, nil, :b, s(:arglist))))
364
+
365
+ assert_equal pt, @processor.parse(rb)
366
+ end
367
+
368
+ def test_regexp
369
+ regexps = {
370
+ "/wtf/" => /wtf/,
371
+ "/wtf/n" => /wtf/n,
372
+ "/wtf/m" => /wtf/m,
373
+ "/wtf/nm" => /wtf/nm,
374
+ "/wtf/nmnmnmnm" => /wtf/nm,
375
+ }
376
+
377
+ regexps.each do |rb, lit|
378
+ assert_equal s(:lit, lit), @processor.parse(rb)
379
+ end
380
+
381
+ # TODO: add more including interpolation etc
382
+ end
383
+
384
+ def test_str_pct_Q_nested
385
+ rb = "%Q[before [#\{nest}] after]"
386
+ pt = s(:dstr, "before [", s(:evstr, s(:call, nil, :nest, s(:arglist))), s(:str, "] after"))
387
+
388
+ assert_equal pt, @processor.parse(rb)
389
+ end
390
+
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
397
+
398
+ def test_str_str
399
+ rb = "\"a #\{'b'}\""
400
+ pt = s(:str, "a b")
401
+
402
+ assert_equal pt, @processor.parse(rb)
403
+ end
404
+
405
+ def test_str_str_str
406
+ rb = "\"a #\{'b'} c\""
407
+ pt = s(:str, "a b c")
408
+
409
+ assert_equal pt, @processor.parse(rb)
410
+ end
411
+
412
+ STARTING_LINE = {
413
+ "case_nested_inner_no_expr" => 2,
414
+ "case_no_expr" => 2,
415
+ "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,
423
+ "structure_unused_literal_wwtt" => 3, # yes, 3... odd test
424
+ "undef_block_1" => 2,
425
+ "undef_block_2" => 2,
426
+ "undef_block_3" => 2,
427
+ "undef_block_wtf" => 2,
428
+ }
429
+
430
+ def after_process_hook klass, node, data, input_name, output_name
431
+ expected = STARTING_LINE[node] || 1
432
+ assert_equal expected, @result.line, "should have proper line number"
433
+ end
434
+
435
+ def test_position_info
436
+ rb = "a = 42\np a"
437
+ pt = s(:block,
438
+ s(:lasgn, :a, s(:lit, 42)),
439
+ s(:call, nil, :p, s(:arglist, s(:lvar, :a))))
440
+
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"
446
+ assert_equal 1, result.lasgn.line, "lasgn should have line number"
447
+ assert_equal 2, result.call.line, "call should have line number"
448
+
449
+ expected = "blah.rb"
450
+
451
+ assert_equal expected, result.file
452
+ assert_equal expected, result.lasgn.file
453
+ assert_equal expected, result.call.file
454
+
455
+ assert_same result.file, result.lasgn.file
456
+ assert_same result.file, result.call.file
457
+ end
458
+
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)))))
468
+
469
+ result = @processor.parse(rb)
470
+
471
+ assert_equal pt, result
472
+
473
+ body = result.scope.block
474
+
475
+ assert_equal 1, result.line, "defn should have line number"
476
+ assert_equal 2, body.call.line, "call should have line number"
477
+ assert_equal 3, body.lasgn.line, "lasgn should have line number"
478
+ assert_equal 4, body.return.line, "return should have line number"
479
+ end
480
+ end