test/pt_testcase.rb in ParseTree-1.7.1 vs test/pt_testcase.rb in ParseTree-2.0.0

- old
+ new

@@ -1,5 +1,7 @@ +$TESTING = true + require 'test/unit/testcase' require 'sexp_processor' # for deep_clone require 'unique' class Examples @@ -7,10 +9,14 @@ attr_writer :writer def a_method(x); x+1; end alias an_alias a_method + define_method(:bmethod_noargs) do + x + 1 + end + define_method(:unsplatted) do |x| x + 1 end define_method :splatted do |*args| @@ -131,15 +137,36 @@ [:rescue, [:resbody, nil]], [:nil]]] }, + "block_lasgn" => { + "Ruby" => "x = (y = 1\n(y + 2))", + "ParseTree" => [:lasgn, :x, + [:block, + [:lasgn, :y, [:lit, 1]], + [:call, [:lvar, :y], :+, [:array, [:lit, 2]]]]], + }, + "block_pass" => { "Ruby" => "a(&b)", "ParseTree" => [:block_pass, [:vcall, :b], [:fcall, :a]], }, + "block_pass_args_and_splat" => { + "Ruby" => "def blah(*args, &block)\n other(42, *args, &block)\nend", + "ParseTree" => [:defn, :blah, + [:scope, + [:block, + [:args, "*args".intern], + [:block_arg, :block], + [:block_pass, + [:lvar, :block], + [:fcall, :other, + [:argscat, [:array, [:lit, 42]], [:lvar, :args]]]]]]], + }, + "block_pass_omgwtf" => { "Ruby" => "define_attr_method(:x, :sequence_name, &Proc.new { |*args| nil })", "ParseTree" => [:block_pass, [:iter, [:call, [:const, :Proc], :new], @@ -159,33 +186,30 @@ [:block_pass, [:lvar, :block], [:fcall, :other, [:splat, [:lvar, :args]]]]]]], }, - "block_pass_args_and_splat" => { - "Ruby" => "def blah(*args, &block)\n other(42, *args, &block)\nend", - "ParseTree" => [:defn, :blah, - [:scope, - [:block, - [:args, "*args".intern], - [:block_arg, :block], - [:block_pass, - [:lvar, :block], - [:fcall, :other, - [:argscat, [:array, [:lit, 42]], [:lvar, :args]]]]]]], - }, - "bmethod" => { "Ruby" => [Examples, :unsplatted], "ParseTree" => [:defn, :unsplatted, [:bmethod, [:dasgn_curr, :x], - [:call, [:dvar, :x], "+".intern, [:array, [:lit, 1]]]]], + [:call, [:dvar, :x], :+, [:array, [:lit, 1]]]]], "Ruby2Ruby" => "def unsplatted(x)\n (x + 1)\nend" }, + "bmethod_noargs" => { + "Ruby" => [Examples, :bmethod_noargs], + "ParseTree" => [:defn, + :bmethod_noargs, + [:bmethod, + nil, + [:call, [:vcall, :x], "+".intern, [:array, [:lit, 1]]]]], + "Ruby2Ruby" => "def bmethod_noargs\n (x + 1)\nend" + }, + "bmethod_splat" => { "Ruby" => [Examples, :splatted], "ParseTree" => [:defn, :splatted, [:bmethod, [:masgn, [:dasgn_curr, :args]], @@ -216,10 +240,18 @@ "call_arglist" => { "Ruby" => "puts(42)", "ParseTree" => [:fcall, :puts, [:array, [:lit, 42]]], }, + "call_expr" => { + "Ruby" => "(v = (1 + 1)).zero?", + "ParseTree" => [:call, + [:lasgn, :v, + [:call, [:lit, 1], :+, [:array, [:lit, 1]]]], + :zero?], + }, + "call_index" => { # see attrasgn_index_equals for opposite "Ruby" => "a[42]", "ParseTree" => [:call, [:vcall, :a], :[], [:array, [:lit, 42]]], }, @@ -308,18 +340,10 @@ [:block, [:args], [:fcall, :puts, [:array, [:str, "hello"]]]]]]]]], }, - "class_super_object" => { - "Ruby" => "class X < Object\nend", - "ParseTree" => [:class, - :X, - [:const, :Object], - [:scope]], - }, - "class_super_array" => { "Ruby" => "class X < Array\nend", "ParseTree" => [:class, :X, [:const, :Array], @@ -332,10 +356,18 @@ :X, [:vcall, :expr], [:scope]], }, + "class_super_object" => { + "Ruby" => "class X < Object\nend", + "ParseTree" => [:class, + :X, + [:const, :Object], + [:scope]], + }, + "colon2" => { "Ruby" => "X::Y", "ParseTree" => [:colon2, [:const, :X], :Y], }, @@ -456,38 +488,49 @@ "defn_is_something" => { "Ruby" => "def something?\n # do nothing\nend", "ParseTree" => [:defn, :something?, [:scope, [:block, [:args], [:nil]]]], }, -# TODO: -# add_test("defn_optargs", -# s(:defn, :x, -# s(:args, :a, "*args".intern), -# s(:scope, -# s(:block, -# s(:call, nil, :p, -# s(:arglist, s(:lvar, :a), s(:lvar, :args))))))) + "defn_optargs" => { + "Ruby" => "def x(a, *args)\n p(a, args)\nend", + "ParseTree" => [:defn, :x, + [:scope, + [:block, + [:args, :a, "*args".intern], + [:fcall, :p, + [:array, [:lvar, :a], [:lvar, :args]]]]]], + }, "defn_or" => { "Ruby" => "def |(o)\n # do nothing\nend", "ParseTree" => [:defn, :|, [:scope, [:block, [:args, :o], [:nil]]]], }, "defn_rescue" => { "Ruby" => "def eql?(resource)\n (self.uuid == resource.uuid) rescue false\nend", "ParseTree" => [:defn, :eql?, - [:scope, - [:block, - [:args, :resource], - [:rescue, - [:call, - [:call, [:self], :uuid], - :==, - [:array, [:call, [:lvar, :resource], :uuid]]], - [:resbody, nil, [:false]]]]]], + [:scope, + [:block, + [:args, :resource], + [:rescue, + [:call, + [:call, [:self], :uuid], + :==, + [:array, [:call, [:lvar, :resource], :uuid]]], + [:resbody, nil, [:false]]]]]], }, + "defn_splat_no_name" => { + "Ruby" => "def x(a, *)\n p(a)\nend", + "ParseTree" => [:defn, :x, + [:scope, + [:block, + [:args, :a, "*".intern], + [:fcall, :p, + [:array, [:lvar, :a]]]]]], + }, + "defn_zarray" => { # tests memory allocation for returns "Ruby" => "def zarray\n a = []\n return a\nend", "ParseTree" => [:defn, :zarray, [:scope, [:block, [:args], @@ -666,15 +709,45 @@ "hash" => { "Ruby" => "{ 1 => 2, 3 => 4 }", "ParseTree" => [:hash, [:lit, 1], [:lit, 2], [:lit, 3], [:lit, 4]], }, + "hash_rescue" => { + "Ruby" => "{ 1 => (2 rescue 3) }", + "ParseTree" => [:hash, + [:lit, 1], + [:rescue, [:lit, 2], [:resbody, nil, [:lit, 3]]]], + }, + "iasgn" => { "Ruby" => "@a = 4", "ParseTree" => [:iasgn, :@a, [:lit, 4]], }, + "if_block_condition" => { + "Ruby" => "if (x = 5\n(x + 1)) then\n nil\nend", + "ParseTree" => [:if, + [:block, + [:lasgn, :x, [:lit, 5]], + [:call, + [:lvar, :x], + :+, + [:array, [:lit, 1]]]], + [:nil], + nil], + }, + + "if_lasgn_short" => { + "Ruby" => "if x = obj.x then\n x.do_it\nend", + "ParseTree" => [:if, + [:lasgn, :x, + [:call, [:vcall, :obj], :x]], + [:call, + [:lvar, :x], :do_it], + nil], + }, + "iteration1" => { "Ruby" => "loop { }", "ParseTree" => [:iter, [:fcall, :loop], nil], }, @@ -809,35 +882,62 @@ "ParseTree" => [:masgn, [:array, [:lasgn, :a], [:lasgn, :b]], [:array, [:vcall, :c], [:vcall, :d]]], }, - "masgn_iasgn" => { - "Ruby" => "a, @b = c, d", + "masgn_argscat" => { + "Ruby" => "a, b, *c = 1, 2, *[3, 4]", "ParseTree" => [:masgn, - [:array, [:lasgn, :a], [:iasgn, "@b".intern]], - [:array, [:vcall, :c], [:vcall, :d]]], + [:array, [:lasgn, :a], [:lasgn, :b]], + [:lasgn, :c], + [:argscat, + [:array, [:lit, 1], [:lit, 2]], + [:array, [:lit, 3], [:lit, 4]]]] }, "masgn_attrasgn" => { "Ruby" => "a, b.c = d, e", "ParseTree" => [:masgn, [:array, [:lasgn, :a], [:attrasgn, [:vcall, :b], :c=]], [:array, [:vcall, :d], [:vcall, :e]]], }, + "masgn_iasgn" => { + "Ruby" => "a, @b = c, d", + "ParseTree" => [:masgn, + [:array, [:lasgn, :a], [:iasgn, "@b".intern]], + [:array, [:vcall, :c], [:vcall, :d]]], + }, + + "masgn_masgn" => { + "Ruby" => "a, (b, c) = [1, [2, 3]]", + "ParseTree" => [:masgn, + [:array, + [:lasgn, :a], + [:masgn, + [:array, + [:lasgn, :b], + [:lasgn, :c]]]], + [:to_ary, + [:array, + [:lit, 1], + [:array, + [:lit, 2], + [:lit, 3]]]]] + + }, + "masgn_splat" => { "Ruby" => "a, b, *c = d, e, f, g", "ParseTree" => [:masgn, [:array, [:lasgn, :a], [:lasgn, :b]], [:lasgn, :c], [:array, [:vcall, :d], [:vcall, :e], [:vcall, :f], [:vcall, :g]]] }, - "match" => { "Ruby" => "1 if /x/", "ParseTree" => [:if, [:match, [:lit, /x/]], [:lit, 1], nil], }, @@ -992,12 +1092,16 @@ "Ruby" => "class << self\n 42\nend", "ParseTree" => [:sclass, [:self], [:scope, [:lit, 42]]], }, "splat" => { - "Ruby" => "a(*b)", - "ParseTree" => [:fcall, :a, [:splat, [:vcall, :b]]], + "Ruby" => "def x(*b)\n a(*b)\nend", + "ParseTree" => [:defn, :x, + [:scope, + [:block, + [:args, :"*b"], + [:fcall, :a, [:splat, [:lvar, :b]]]]]], }, # TODO: all supers need to pass args "super" => { "Ruby" => "def x\n super(4)\nend", @@ -1068,10 +1172,16 @@ "vcall" => { "Ruby" => "method", "ParseTree" => [:vcall, :method], }, + "while_post" => { + "Ruby" => "begin\n (1 + 1)\nend while false", + "ParseTree" => [:while, [:false], + [:call, [:lit, 1], :+, [:array, [:lit, 1]]], false], + }, + "while_pre" => { "Ruby" => "while false do\n (1 + 1)\nend", "ParseTree" => [:while, [:false], [:call, [:lit, 1], :+, [:array, [:lit, 1]]], true], }, @@ -1079,16 +1189,10 @@ "while_pre_nil" => { "Ruby" => "while false do\nend", "ParseTree" => [:while, [:false], nil, true], }, - "while_post" => { - "Ruby" => "begin\n (1 + 1)\nend while false", - "ParseTree" => [:while, [:false], - [:call, [:lit, 1], :+, [:array, [:lit, 1]]], false], - }, - "xstr" => { "Ruby" => "`touch 5`", "ParseTree" => [:xstr, 'touch 5'], }, @@ -1168,10 +1272,11 @@ # end # end def self.inherited(c) output_name = c.name.to_s.sub(/^Test/, '') - raise "Unknown class #{c}" unless @@testcase_order.include? output_name + raise "Unknown class #{c} in @@testcase_order" unless + @@testcase_order.include? output_name input_name = self.previous(output_name) @@testcases.each do |node, data| next if [:skip, :unsupported].include? data[input_name]