ParseTree 1.5.0 → 1.6.0

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.
@@ -5,131 +5,6 @@ class Something
5
5
  1 + 1
6
6
  end
7
7
 
8
- # basically: do we work at all?
9
- def empty
10
- end
11
-
12
- # First order transformation: basic language constructs
13
- def stupid
14
- return nil
15
- end
16
-
17
- def simple(arg1)
18
- print arg1
19
- puts((4 + 2).to_s)
20
- end
21
-
22
- def global
23
- $stderr.fputs("blah")
24
- end
25
-
26
- def lasgn_call
27
- c = 2 + 3
28
- end
29
-
30
- def conditional1(arg1)
31
- if arg1 == 0 then
32
- return 1
33
- end
34
- end
35
-
36
- def conditional2(arg1)
37
- unless arg1 == 0 then
38
- return 2
39
- end
40
- end
41
-
42
- def conditional3(arg1)
43
- if arg1 == 0 then
44
- return 3
45
- else
46
- return 4
47
- end
48
- end
49
-
50
- def conditional4(arg1)
51
- if arg1 == 0 then
52
- return 2
53
- elsif arg1 < 0 then
54
- return 3
55
- else
56
- return 4
57
- end
58
- end
59
-
60
- def iteration1
61
- array = [1, 2, 3]
62
- array.each do |x|
63
- y = x.to_s
64
- puts(y)
65
- end
66
- end
67
-
68
- def iteration2
69
- array = [1, 2, 3]
70
- array.each { |x|
71
- y = x.to_s
72
- puts(y)
73
- }
74
- end
75
-
76
- def iteration3
77
- array1 = [1, 2, 3]
78
- array2 = [4, 5, 6, 7]
79
- array1.each do |x|
80
- array2.each do |y|
81
- puts(x.to_s)
82
- puts(y.to_s)
83
- end
84
- end
85
- end
86
-
87
- def iteration4
88
- 1.upto(3) do |n|
89
- puts n.to_s
90
- end
91
- end
92
-
93
- def iteration5
94
- 3.downto(1) do |n|
95
- puts n.to_s
96
- end
97
- end
98
-
99
- def iteration6
100
- 3.downto(1) do
101
- puts "hello"
102
- end
103
- end
104
-
105
- def case_stmt
106
- var = 2
107
- result = ""
108
- case var
109
- when 1 then
110
- # block
111
- puts "something"
112
- result = "red"
113
- when 2, 3 then
114
- result = "yellow"
115
- when 4 then
116
- # nothing
117
- else
118
- result = "green"
119
- end
120
-
121
- case result
122
- when "red" then
123
- var = 1
124
- when "yellow" then
125
- var = 2
126
- when "green" then
127
- var = 3
128
- end
129
-
130
- return result
131
- end
132
-
133
8
  # Other edge cases:
134
9
 
135
10
  def opt_args(arg1, arg2 = 42, *args)
@@ -144,26 +19,6 @@ class Something
144
19
  return "foo"
145
20
  end
146
21
 
147
- def bools(arg1)
148
- unless arg1.nil? then
149
- return true
150
- else
151
- return false
152
- end
153
- end
154
-
155
- def eric_is_stubborn
156
- var = 42
157
- var2 = var.to_s
158
- $stderr.fputs(var2)
159
- return var2
160
- end
161
-
162
- def interpolated
163
- var = 14
164
- var2 = "var is #{var}. So there."
165
- end
166
-
167
22
  def unknown_args(arg1, arg2)
168
23
  # does nothing
169
24
  return arg1
@@ -200,48 +55,4 @@ class Something
200
55
  6
201
56
  end
202
57
  end
203
-
204
- def op_asgn
205
- a = 0
206
- a ||= 1
207
- a &&= 2
208
- #a += 3 # doesn't use op_asgn
209
-
210
- b = []
211
- b[1] ||= 10
212
- b[2] &&= 11
213
- b[3] += 12
214
-
215
- s = Struct.new :var
216
- c = s.new nil
217
- c.var ||= 20
218
- c.var &&= 21
219
- c.var += 22
220
-
221
- c.d.e.f ||= 42
222
-
223
- return a
224
- end
225
-
226
- def whiles
227
- while false do
228
- puts "false"
229
- end
230
- begin
231
- puts "true"
232
- end while false
233
- end
234
-
235
- def self.bmethod_maker
236
- define_method(:bmethod_added) do |x|
237
- x + 1
238
- end
239
- end
240
-
241
- def self.dmethod_maker
242
- define_method :dmethod_added, self.method(:bmethod_maker)
243
- end if RUBY_VERSION < "1.9"
244
-
245
- bmethod_maker
246
- dmethod_maker if RUBY_VERSION < "1.9"
247
58
  end
@@ -9,6 +9,7 @@ end
9
9
 
10
10
  require 'test/unit'
11
11
  require 'parse_tree'
12
+ require 'pt_testcase'
12
13
  require 'test/something'
13
14
 
14
15
  class SomethingWithInitialize
@@ -17,26 +18,42 @@ class SomethingWithInitialize
17
18
  def protected_meth; end
18
19
  end
19
20
 
20
- class TestParseTree < Test::Unit::TestCase
21
+ class ParseTree
22
+ def process(input) # makes it play with with ParseTreeTestCase (FIX?)
23
+ # um. kinda stupid, but cleaner
24
+ case input
25
+ when Array then
26
+ ParseTree.translate(*input)
27
+ else
28
+ ParseTree.translate(input)
29
+ end
30
+ end
31
+ end
32
+
33
+ class TestParseTree < ParseTreeTestCase
34
+ def setup
35
+ super
36
+ @processor = ParseTree.new(false)
37
+ end
21
38
 
22
39
  def test_class_initialize
23
40
  expected = [[:class, :SomethingWithInitialize, :Object,
24
41
  [:defn, :initialize, [:scope, [:block, [:args], [:nil]]]],
25
42
  [:defn, :protected_meth, [:scope, [:block, [:args], [:nil]]]],
26
43
  ]]
27
- tree = @thing.parse_tree SomethingWithInitialize
44
+ tree = @processor.parse_tree SomethingWithInitialize
28
45
  assert_equal expected, tree
29
46
  end
30
47
 
31
48
  def test_parse_tree_for_string
32
- actual = @thing.parse_tree_for_string '1 + nil', '(string)', 1, false
49
+ actual = @processor.parse_tree_for_string '1 + nil', '(string)', 1, false
33
50
  expected = [[:call, [:lit, 1], :+, [:array, [:nil]]]]
34
51
 
35
52
  assert_equal expected, actual
36
53
  end
37
54
 
38
55
  def test_parse_tree_for_string_with_newlines
39
- actual = @thing.parse_tree_for_string "1 +\n nil", 'test.rb', 5, true
56
+ actual = @processor.parse_tree_for_string "1 +\n nil", 'test.rb', 5, true
40
57
  expected = [[:newline, 6, "test.rb"],
41
58
  [:call, [:lit, 1], :+, [:array, [:nil]]]]
42
59
 
@@ -44,201 +61,20 @@ class TestParseTree < Test::Unit::TestCase
44
61
  end
45
62
 
46
63
  def test_parse_tree_for_str
47
- actual = @thing.parse_tree_for_str '1 + nil', '(string)', 1, false
64
+ actual = @processor.parse_tree_for_str '1 + nil', '(string)', 1, false
48
65
  expected = [[:call, [:lit, 1], :+, [:array, [:nil]]]]
49
66
 
50
67
  assert_equal expected, actual
51
68
  end
52
69
 
53
- # TODO: need a test of interpolated strings
54
-
55
70
  @@self_classmethod = [:defn, :"self.classmethod",
56
71
  [:scope,
57
72
  [:block,
58
73
  [:args],
59
74
  [:call, [:lit, 1], :+, [:array, [:lit, 1]]]]]]
60
75
 
61
- @@self_bmethod_maker = [:defn,
62
- :"self.bmethod_maker",
63
- [:scope,
64
- [:block,
65
- [:args],
66
- [:iter,
67
- [:fcall, :define_method,
68
- [:array, [:lit, :bmethod_added]]],
69
- [:dasgn_curr, :x],
70
- [:call, [:dvar, :x], :+, [:array, [:lit, 1]]]]]]]
71
-
72
- @@self_dmethod_maker = [:defn,
73
- :"self.dmethod_maker",
74
- [:scope,
75
- [:block,
76
- [:args],
77
- [:fcall,
78
- :define_method,
79
- [:array,
80
- [:lit, :dmethod_added],
81
- [:call, [:self], :method,
82
- [:array, [:lit, :bmethod_maker]]]]]]]]
83
-
84
76
  @@missing = [nil]
85
- @@empty = [:defn, :empty,
86
- [:scope,
87
- [:block,
88
- [:args],
89
- [:nil]]]]
90
- @@stupid = [:defn, :stupid,
91
- [:scope,
92
- [:block,
93
- [:args],
94
- [:return, [:nil]]]]]
95
- @@simple = [:defn, :simple,
96
- [:scope,
97
- [:block,
98
- [:args, :arg1],
99
- [:fcall, :print,
100
- [:array, [:lvar, :arg1]]],
101
- [:fcall, :puts,
102
- [:array,
103
- [:call,
104
- [:call,
105
- [:lit, 4],
106
- :+,
107
- [:array, [:lit, 2]]],
108
- :to_s]]]]]]
109
- @@global = [:defn, :global,
110
- [:scope,
111
- [:block,
112
- [:args],
113
- [:call,
114
- [:gvar, :$stderr],
115
- :fputs,
116
- [:array, [:str, "blah"]]]]]]
117
- @@lasgn_call = [:defn, :lasgn_call,
118
- [:scope,
119
- [:block,
120
- [:args],
121
- [:lasgn, :c,
122
- [:call,
123
- [:lit, 2],
124
- :+,
125
- [:array, [:lit, 3]]]]]]]
126
- @@conditional1 = [:defn, :conditional1,
127
- [:scope,
128
- [:block,
129
- [:args, :arg1],
130
- [:if,
131
- [:call,
132
- [:lvar, :arg1],
133
- :==,
134
- [:array, [:lit, 0]]],
135
- [:return,
136
- [:lit, 1]], nil]]]]
137
- @@conditional2 = [:defn, :conditional2,
138
- [:scope,
139
- [:block,
140
- [:args, :arg1],
141
- [:if,
142
- [:call,
143
- [:lvar, :arg1],
144
- :==,
145
- [:array, [:lit, 0]]], nil,
146
- [:return,
147
- [:lit, 2]]]]]]
148
- @@conditional3 = [:defn, :conditional3,
149
- [:scope,
150
- [:block,
151
- [:args, :arg1],
152
- [:if,
153
- [:call,
154
- [:lvar, :arg1],
155
- :==,
156
- [:array, [:lit, 0]]],
157
- [:return,
158
- [:lit, 3]],
159
- [:return,
160
- [:lit, 4]]]]]]
161
- @@conditional4 = [:defn, :conditional4,
162
- [:scope,
163
- [:block,
164
- [:args, :arg1],
165
- [:if,
166
- [:call,
167
- [:lvar, :arg1],
168
- :==,
169
- [:array, [:lit, 0]]],
170
- [:return, [:lit, 2]],
171
- [:if,
172
- [:call,
173
- [:lvar, :arg1],
174
- :<,
175
- [:array, [:lit, 0]]],
176
- [:return, [:lit, 3]],
177
- [:return, [:lit, 4]]]]]]]
178
- @@iteration_body = [:scope,
179
- [:block,
180
- [:args],
181
- [:lasgn, :array,
182
- [:array, [:lit, 1], [:lit, 2], [:lit, 3]]],
183
- [:iter,
184
- [:call,
185
- [:lvar, :array], :each],
186
- [:dasgn_curr, :x],
187
- [:block,
188
- [:dasgn_curr, :y],
189
- [:dasgn_curr, :y, [:call, [:dvar, :x], :to_s]],
190
- [:fcall, :puts, [:array, [:dvar, :y]]]]]]]
191
77
 
192
- @@iteration1 = [:defn, :iteration1, @@iteration_body]
193
- @@iteration2 = [:defn, :iteration2, @@iteration_body]
194
- @@iteration3 = [:defn, :iteration3,
195
- [:scope,
196
- [:block,
197
- [:args],
198
- [:lasgn, :array1,
199
- [:array, [:lit, 1], [:lit, 2], [:lit, 3]]],
200
- [:lasgn, :array2,
201
- [:array, [:lit, 4], [:lit, 5], [:lit, 6], [:lit, 7]]],
202
- [:iter,
203
- [:call,
204
- [:lvar, :array1], :each],
205
- [:dasgn_curr, :x],
206
- [:iter,
207
- [:call,
208
- [:lvar, :array2], :each],
209
- [:dasgn_curr, :y],
210
- [:block,
211
- [:fcall, :puts,
212
- [:array, [:call, [:dvar, :x], :to_s]]],
213
- [:fcall, :puts,
214
- [:array, [:call, [:dvar, :y], :to_s]]]]]]]]]
215
- @@iteration4 = [:defn,
216
- :iteration4,
217
- [:scope,
218
- [:block,
219
- [:args],
220
- [:iter,
221
- [:call, [:lit, 1], :upto, [:array, [:lit, 3]]],
222
- [:dasgn_curr, :n],
223
- [:fcall, :puts, [:array, [:call, [:dvar, :n], :to_s]]]]]]]
224
- @@iteration5 = [:defn,
225
- :iteration5,
226
- [:scope,
227
- [:block,
228
- [:args],
229
- [:iter,
230
- [:call, [:lit, 3], :downto, [:array, [:lit, 1]]],
231
- [:dasgn_curr, :n],
232
- [:fcall, :puts, [:array, [:call, [:dvar, :n], :to_s]]]]]]]
233
- @@iteration6 = [:defn,
234
- :iteration6,
235
- [:scope,
236
- [:block,
237
- [:args],
238
- [:iter,
239
- [:call, [:lit, 3], :downto, [:array, [:lit, 1]]],
240
- nil,
241
- [:fcall, :puts, [:array, [:str, "hello"]]]]]]]
242
78
  @@opt_args = [:defn, :opt_args,
243
79
  [:scope,
244
80
  [:block,
@@ -269,58 +105,6 @@ class TestParseTree < Test::Unit::TestCase
269
105
  [:fcall, :puts, [:array, [:call, [:lvar, :arg3], :to_s]]],
270
106
  [:return,
271
107
  [:str, "foo"]]]]]
272
- @@bools = [:defn, :bools,
273
- [:scope,
274
- [:block,
275
- [:args, :arg1],
276
- [:if,
277
- [:call,
278
- [:lvar, :arg1], :nil?],
279
- [:return,
280
- [:false]],
281
- [:return,
282
- [:true]]]]]]
283
- @@case_stmt = [:defn, :case_stmt,
284
- [:scope,
285
- [:block,
286
- [:args],
287
- [:lasgn, :var, [:lit, 2]],
288
- [:lasgn, :result, [:str, ""]],
289
- [:case,
290
- [:lvar, :var],
291
- [:when,
292
- [:array, [:lit, 1]],
293
- [:block,
294
- [:fcall, :puts, [:array, [:str, "something"]]],
295
- [:lasgn, :result, [:str, "red"]]]],
296
- [:when,
297
- [:array, [:lit, 2], [:lit, 3]],
298
- [:lasgn, :result, [:str, "yellow"]]],
299
- [:when, [:array, [:lit, 4]], nil],
300
- [:lasgn, :result, [:str, "green"]]],
301
- [:case,
302
- [:lvar, :result],
303
- [:when, [:array, [:str, "red"]], [:lasgn, :var, [:lit, 1]]],
304
- [:when, [:array, [:str, "yellow"]], [:lasgn, :var, [:lit, 2]]],
305
- [:when, [:array, [:str, "green"]], [:lasgn, :var, [:lit, 3]]],
306
- nil],
307
- [:return, [:lvar, :result]]]]]
308
- @@eric_is_stubborn = [:defn,
309
- :eric_is_stubborn,
310
- [:scope,
311
- [:block,
312
- [:args],
313
- [:lasgn, :var, [:lit, 42]],
314
- [:lasgn, :var2, [:call, [:lvar, :var], :to_s]],
315
- [:call, [:gvar, :$stderr], :fputs, [:array, [:lvar, :var2]]],
316
- [:return, [:lvar, :var2]]]]]
317
- @@interpolated = [:defn,
318
- :interpolated,
319
- [:scope,
320
- [:block,
321
- [:args],
322
- [:lasgn, :var, [:lit, 14]],
323
- [:lasgn, :var2, [:dstr, "var is ", [:lvar, :var], [:str, ". So there."]]]]]]
324
108
  @@unknown_args = [:defn, :unknown_args,
325
109
  [:scope,
326
110
  [:block,
@@ -350,30 +134,7 @@ class TestParseTree < Test::Unit::TestCase
350
134
  [:rescue,
351
135
  [:lit, 5],
352
136
  [:resbody, nil, [:lit, 6]]]]]]]
353
- @@op_asgn = [:defn, :op_asgn,
354
- [:scope,
355
- [:block,
356
- [:args],
357
- [:lasgn, :a, [:lit, 0]],
358
- [:op_asgn_or, [:lvar, :a], [:lasgn, :a, [:lit, 1]]],
359
- [:op_asgn_and, [:lvar, :a], [:lasgn, :a, [:lit, 2]]],
360
137
 
361
- [:lasgn, :b, [:zarray]],
362
-
363
- [:op_asgn1, [:lvar, :b], [:array, [:lit, 1]], :"||", [:lit, 10]],
364
- [:op_asgn1, [:lvar, :b], [:array, [:lit, 2]], :"&&", [:lit, 11]],
365
- [:op_asgn1, [:lvar, :b], [:array, [:lit, 3]], :+, [:lit, 12]],
366
-
367
- [:lasgn, :s, [:call, [:const, :Struct], :new, [:array, [:lit, :var]]]],
368
- [:lasgn, :c, [:call, [:lvar, :s], :new, [:array, [:nil]]]],
369
-
370
- [:op_asgn2, [:lvar, :c], :var=, :"||", [:lit, 20]],
371
- [:op_asgn2, [:lvar, :c], :var=, :"&&", [:lit, 21]],
372
- [:op_asgn2, [:lvar, :c], :var=, :+, [:lit, 22]],
373
-
374
- [:op_asgn2, [:call, [:call, [:lvar, :c], :d], :e], :f=, :"||", [:lit, 42]],
375
-
376
- [:return, [:lvar, :a]]]]]
377
138
  @@determine_args = [:defn, :determine_args,
378
139
  [:scope,
379
140
  [:block,
@@ -386,24 +147,7 @@ class TestParseTree < Test::Unit::TestCase
386
147
  :unknown_args,
387
148
  [:array, [:lit, 4], [:str, "known"]]]]]]]]
388
149
 
389
- @@bmethod_added = [:defn,
390
- :bmethod_added,
391
- [:bmethod,
392
- [:dasgn_curr, :x],
393
- [:call, [:dvar, :x], :+, [:array, [:lit, 1]]]]]
394
150
 
395
- @@dmethod_added = [:defn,
396
- :dmethod_added,
397
- [:dmethod,
398
- :bmethod_maker,
399
- [:scope,
400
- [:block,
401
- [:args],
402
- [:iter,
403
- [:fcall, :define_method, [:array, [:lit, :bmethod_added]]],
404
- [:dasgn_curr, :x],
405
- [:call, [:dvar, :x], :+, [:array, [:lit, 1]]]]]]]] if RUBY_VERSION < "1.9"
406
-
407
151
  @@attrasgn = [:defn,
408
152
  :attrasgn,
409
153
  [:scope,
@@ -415,50 +159,21 @@ class TestParseTree < Test::Unit::TestCase
415
159
  :type=,
416
160
  [:array, [:call, [:vcall, :other], :type]]]]]]
417
161
 
418
- @@whiles = [:defn,
419
- :whiles,
420
- [:scope,
421
- [:block,
422
- [:args],
423
- [:while, [:false], [:fcall, :puts, [:array, [:str, "false"]]], true],
424
- [:while, [:false], [:fcall, :puts, [:array, [:str, "true"]]], false]]]]
425
- @@xstr = [:defn,
426
- :xstr,
427
- [:scope,
428
- [:block,
429
- [:args],
430
- [:xstr, 'touch 5']]]]
431
- @@dxstr = [:defn,
432
- :dxstr,
433
- [:scope,
434
- [:block,
435
- [:args],
436
- [:dxstr, 'touch ', [:lit, 5]]]]]
437
-
438
162
  @@__all = [:class, :Something, :Object]
439
163
 
440
- def setup
441
- @thing = ParseTree.new(false)
442
- end
443
-
444
- methods = Something.instance_methods(false)
445
-
446
- methods.sort.each do |meth|
164
+ Something.instance_methods(false).sort.each do |meth|
447
165
  if class_variables.include?("@@#{meth}") then
448
166
  @@__all << eval("@@#{meth}")
449
- eval "def test_#{meth}; assert_equal @@#{meth}, @thing.parse_tree_for_method(Something, :#{meth}); end"
167
+ eval "def test_#{meth}; assert_equal @@#{meth}, @processor.parse_tree_for_method(Something, :#{meth}); end"
450
168
  else
451
169
  eval "def test_#{meth}; flunk \"You haven't added @@#{meth} yet\"; end"
452
170
  end
453
171
  end
454
172
 
455
- methods = Something.singleton_methods
456
-
457
- # TODO: cleanup
458
- methods.sort.each do |meth|
173
+ Something.singleton_methods.sort.each do |meth|
459
174
  if class_variables.include?("@@self_#{meth}") then
460
175
  @@__all << eval("@@self_#{meth}")
461
- eval "def test_self_#{meth}; assert_equal @@self_#{meth}, @thing.parse_tree_for_method(Something, :#{meth}, true); end"
176
+ eval "def test_self_#{meth}; assert_equal @@self_#{meth}, @processor.parse_tree_for_method(Something, :#{meth}, true); end"
462
177
  else
463
178
  eval "def test_self_#{meth}; flunk \"You haven't added @@self_#{meth} yet\"; end"
464
179
  end
@@ -466,13 +181,13 @@ class TestParseTree < Test::Unit::TestCase
466
181
 
467
182
  def test_missing
468
183
  assert_equal(@@missing,
469
- @thing.parse_tree_for_method(Something, :missing),
184
+ @processor.parse_tree_for_method(Something, :missing),
470
185
  "Must return -3 for missing methods")
471
186
  end
472
187
 
473
- def test_class
188
+ def test_whole_class
474
189
  assert_equal([@@__all],
475
- @thing.parse_tree(Something),
190
+ @processor.parse_tree(Something),
476
191
  "Must return a lot of shit")
477
192
  end
478
193
  end