test/pt_testcase.rb in ParseTree-1.6.1 vs test/pt_testcase.rb in ParseTree-1.6.2
- old
+ new
@@ -12,15 +12,15 @@
def self.bmethod_maker
define_method(:bmethod_added) do |x|
x + 1
end
end
-
+
def self.dmethod_maker
define_method :dmethod_added, self.method(:bmethod_maker)
end if RUBY_VERSION < "1.9"
-
+
bmethod_maker
dmethod_maker if RUBY_VERSION < "1.9"
end
class ParseTreeTestCase < Test::Unit::TestCase
@@ -57,11 +57,11 @@
end
@@testcase_order = %w(Ruby ParseTree)
@@testcases = {
-
+
"alias" => {
"Ruby" => "class X\n alias :y :x\nend",
"ParseTree" => [:class, :X, nil,
[:scope, [:alias, [:lit, :y], [:lit, :x]]]],
"Ruby2Ruby" => "class X\n alias_method :y, :x\nend", # FIX dbl \n
@@ -85,40 +85,44 @@
"Ruby" => "a[*b] = c",
"ParseTree" => [:attrasgn,
[:vcall, :a],
:[]=,
[:argspush, [:splat, [:vcall, :b]], [:vcall, :c]]],
- "Ruby2Ruby" => "a.[]=(*b, c)" # HACK until we can get things ironed out
},
"array" => {
"Ruby" => "[1, :b, \"c\"]",
"ParseTree" => [:array, [:lit, 1], [:lit, :b], [:str, "c"]],
},
"attrasgn" => {
- "Ruby" => "y = 0\n42.method=(y)\n",
+ "Ruby" => "y = 0\n42.method = y\n",
"ParseTree" => [:block,
[:lasgn, :y, [:lit, 0]],
[:attrasgn, [:lit, 42], :method=, [:array, [:lvar, :y]]]],
},
+ "attrasgn_index_equals" => {
+ "Ruby" => "a[42] = 24",
+ "ParseTree" => [:attrasgn, [:vcall, :a], :[]=, [:array, [:lit, 42], [:lit, 24]]],
+ },
+
"attrset" => {
"Ruby" => [Examples, :writer=],
"ParseTree" => [:defn, :writer=, [:attrset, :@writer]],
"Ruby2Ruby" => "attr_writer :writer"
},
-
+
"back_ref" => {
"Ruby" => "[$&, $`, $', $+]",
"ParseTree" => [:array,
[:back_ref, :&],
[:back_ref, "`".intern], # symbol was fucking up emacs
[:back_ref, "'".intern], # s->e
[:back_ref, :+]],
},
-
+
"begin" => {
"Ruby" => "begin\n (1 + 1)\nend",
"ParseTree" => [:begin, [:call, [:lit, 1], :+, [:array, [:lit, 1]]]],
},
@@ -140,28 +144,33 @@
"break" => {
"Ruby" => "loop do\n if true then\n break\n end\nend",
"ParseTree" => [:iter,
[:fcall, :loop], nil, [:if, [:true], [:break], nil]],
},
-
+
"break_arg" => {
"Ruby" => "loop do\n if true then\n break 42\n end\nend",
"ParseTree" => [:iter,
[:fcall, :loop], nil,
[:if, [:true], [:break, [:lit, 42]], nil]],
},
-
+
"call" => {
"Ruby" => "self.method",
"ParseTree" => [:call, [:self], :method],
},
"call_arglist" => {
"Ruby" => "puts(42)",
"ParseTree" => [:fcall, :puts, [:array, [:lit, 42]]],
},
+ "call_index" => { # see attrasgn_index_equals for opposite
+ "Ruby" => "a[42]",
+ "ParseTree" => [:call, [:vcall, :a], :[], [:array, [:lit, 42]]],
+ },
+
"case" => {
"Ruby" => "var = 2\nresult = \"\"\ncase var\nwhen 1 then\n puts(\"something\")\n result = \"red\"\nwhen 2, 3 then\n result = \"yellow\"\nwhen 4 then\n # do nothing\nelse\n result = \"green\"\nend\ncase result\nwhen \"red\" then\n var = 1\nwhen \"yellow\" then\n var = 2\nwhen \"green\" then\n var = 3\nelse\n # do nothing\nend\n",
"ParseTree" => [:block,
[:lasgn, :var, [:lit, 2]],
[:lasgn, :result, [:str, ""]],
@@ -186,10 +195,21 @@
[:when, [:array, [:str, "green"]],
[:lasgn, :var, [:lit, 3]]],
nil]]
},
+ "case_no_expr" => { # TODO: nested
+ "Ruby" => "case\nwhen 1 then\n :a\nwhen 2 then\n :b\nelse\n :c\nend",
+ "ParseTree" => [:case, nil,
+ [:when,
+ [:array, [:lit, 1]],
+ [:lit, :a]],
+ [:when,
+ [:array, [:lit, 2]], [:lit, :b]],
+ [:lit, :c]],
+ },
+
"cdecl" => {
"Ruby" => "X = 42",
"ParseTree" => [:cdecl, :X, [:lit, 42]],
},
@@ -333,11 +353,11 @@
[:block_arg, :d],
[:fcall, :p,
[:array, [:lvar, :a], [:lvar, :b],
[:lvar, :c], [:lvar, :d]]]]]]
},
-
+
"defn_empty" => {
"Ruby" => "def empty\n # do nothing\nend",
"ParseTree" => [:defn, :empty, [:scope, [:block, [:args], [:nil]]]],
},
@@ -357,11 +377,11 @@
"defn_or" => {
"Ruby" => "def |(o)\n # do nothing\nend",
"ParseTree" => [:defn, :|, [:scope, [:block, [:args, :o], [:nil]]]],
},
-
+
"defn_zarray" => { # tests memory allocation for returns
"Ruby" => "def zarray\n a = []\n return a\nend",
"ParseTree" => [:defn, :zarray,
[:scope,
[:block, [:args],
@@ -394,16 +414,16 @@
"Ruby2Ruby" => "def dmethod_added(x)\n (x + 1)\nend"
},
"dot2" => {
"Ruby" => "(a..b)",
- "ParseTree" => [:dot2, [:vcall, :a], [:vcall, :b]],
+ "ParseTree" => [:dot2, [:vcall, :a], [:vcall, :b]],
},
"dot3" => {
"Ruby" => "(a...b)",
- "ParseTree" => [:dot3, [:vcall, :a], [:vcall, :b]],
+ "ParseTree" => [:dot3, [:vcall, :a], [:vcall, :b]],
},
"dregx" => {
"Ruby" => "/x#\{(1 + 1)}y/",
"ParseTree" => [:dregx, "x",
@@ -434,11 +454,11 @@
"Ruby" => "t = 5\n`touch #\{t}`\n",
"ParseTree" => [:block,
[:lasgn, :t, [:lit, 5]],
[:dxstr, 'touch ', [:lvar, :t]]],
},
-
+
"ensure" => {
"Ruby" => "begin
(1 + 1)
rescue SyntaxError => e1
2
@@ -684,11 +704,11 @@
"masgn" => {
"Ruby" => "a, b = c, d",
"ParseTree" => [:masgn,
[:array, [:lasgn, :a], [:lasgn, :b]],
- [:array, [:vcall, :c], [:vcall, :d]]],
+ [:array, [:vcall, :c], [:vcall, :d]]],
},
"match" => {
"Ruby" => "if /x/ then\n 1\nend",
"ParseTree" => [:if, [:match, [:lit, /x/]], [:lit, 1], nil],
@@ -747,17 +767,17 @@
[:lasgn, :s,
[:call, [:const, :Struct],
:new, [:array, [:lit, :var]]]],
[:lasgn, :c,
[:call, [:lvar, :s], :new, [:array, [:nil]]]],
-
+
[:op_asgn2, [:lvar, :c], :var=, "||".intern, # s->e
[:lit, 20]],
[:op_asgn2, [:lvar, :c], :var=, "&&".intern, # s->e
[:lit, 21]],
[:op_asgn2, [:lvar, :c], :var=, :+, [:lit, 22]],
-
+
[:op_asgn2,
[:call,
[:call, [:lvar, :c], :d], :e], :f=, "||".intern,
[:lit, 42]]],
},
@@ -784,10 +804,26 @@
"postexe" => {
"Ruby" => "END {\n 1\n}",
"ParseTree" => [:iter, [:postexe], nil, [:lit, 1]],
},
+ "proc_no_args" => {
+ "Ruby" => "proc do\n (x + 1)\nend",
+ "ParseTree" => [:iter,
+ [:fcall, :proc],
+ nil,
+ [:call, [:vcall, :x], :+, [:array, [:lit, 1]]]],
+ },
+
+ "proc_args" => {
+ "Ruby" => "proc do |x|\n (x + 1)\nend",
+ "ParseTree" => [:iter,
+ [:fcall, :proc],
+ [:dasgn_curr, :x],
+ [:call, [:dvar, :x], :+, [:array, [:lit, 1]]]],
+ },
+
"redo" => {
"Ruby" => "loop do\n if false then\n redo\n end\nend",
"ParseTree" => [:iter,
[:fcall, :loop], nil, [:if, [:false], [:redo], nil]],
},
@@ -961,11 +997,11 @@
# else
# SexpProcessor.new.unsupported
# # HACK
# nodes -= [:alloca, :argspush, :cfunc, :cref, :evstr, :ifunc, :last, :memo, :newline, :opt_n, :method].map { |s| s.to_s }
# end
-
+
# untested = nodes-tested
# puts
# p :untested_nodes => untested, :extra_nodes => tested-nodes
@@ -975,11 +1011,11 @@
# "Ruby" => "XXX",
# "ParseTree" => [],
# },
# )
# end
-
+
# flunk
# end
def self.previous(key, extra=0)
idx = @@testcase_order.index(key)-1-extra
@@ -1036,10 +1072,10 @@
extra_expected = []
extra_input = []
_, expected, extra_expected = *expected if Array === expected and expected.first == :defx
_, input, extra_input = *input if Array === input and input.first == :defx
-
+
debug = input.deep_clone
assert_equal expected, processor.process(input), "failed on input: #{debug.inspect}"
extra_input.each do |input| processor.process(input) end
extra = processor.extra_methods rescue []
assert_equal extra_expected, extra