Skip to content

Commit

Permalink
Extended tests to check that the sexp is still right if CHECK_SEXPS i…
Browse files Browse the repository at this point in the history
…s set in the environment

[git-p4: depot-paths = "//src/ruby2ruby/dev/": change = 9088]
  • Loading branch information
zenspider committed Dec 14, 2013
1 parent 2981a4a commit accbc5d
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions test/test_ruby2ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'pt_testcase'
require 'fileutils'
require 'tmpdir'
require 'ruby_parser' if ENV["CHECK_SEXPS"]

class R2RTestCase < ParseTreeTestCase
def self.previous key
Expand Down Expand Up @@ -39,9 +40,14 @@ def test_#{node}
class TestRuby2Ruby < R2RTestCase
def setup
super
@check_sexp = ENV["CHECK_SEXPS"]
@processor = Ruby2Ruby.new
end

def do_not_check_sexp!
@check_sexp = false
end

def test_util_dthing_dregx
inn = util_thingy(:dregx)
inn.shift
Expand Down Expand Up @@ -87,6 +93,8 @@ def test_attr_reader_diff
end

def test_attr_reader_same
do_not_check_sexp!

inn = s(:defn, :same, s(:args), s(:ivar, :@same))
out = "attr_reader :same"
util_compare inn, out
Expand All @@ -99,6 +107,8 @@ def test_attr_reader_double
end

def test_attr_reader_same_name_diff_body
do_not_check_sexp!

inn = s(:defn, :same, s(:args), s(:not, s(:ivar, :@same)))
out = "def same\n (not @same)\nend"
util_compare inn, out
Expand All @@ -124,12 +134,16 @@ def test_attr_writer_same_name_diff_body
end

def test_attr_writer_same
do_not_check_sexp!

inn = s(:defn, :same=, s(:args, :o), s(:iasgn, :@same , s(:lvar, :o)))
out = "attr_writer :same"
util_compare inn, out
end

def test_dregx_slash
do_not_check_sexp!

inn = util_thingy(:dregx)
out = '/a"b#{(1 + 1)}c"d\/e/'
util_compare inn, out, /a"b2c"d\/e/
Expand All @@ -148,6 +162,8 @@ def test_dsym_quote
end

def test_lit_regexp_slash
do_not_check_sexp! # dunno why on this one

util_compare s(:lit, /blah\/blah/), '/blah\/blah/', /blah\/blah/
end

Expand Down Expand Up @@ -210,16 +226,15 @@ def test_masgn_wtf
inn = s(:block,
s(:masgn,
s(:array, s(:lasgn, :k), s(:lasgn, :v)),
s(:array,
s(:splat,
s(:call,
s(:call, nil, :line),
:split,
s(:lit, /\=/), s(:lit, 2))))),
s(:splat,
s(:call,
s(:call, nil, :line),
:split,
s(:lit, /\=/), s(:lit, 2)))),
s(:attrasgn,
s(:self),
:[]=,
s(:arglist, s(:lvar, :k)),
s(:lvar, :k),
s(:call, s(:lvar, :v), :strip)))

out = "k, v = *line.split(/\\=/, 2)\nself[k] = v.strip\n"
Expand All @@ -230,12 +245,11 @@ def test_masgn_wtf
def test_masgn_splat_wtf
inn = s(:masgn,
s(:array, s(:lasgn, :k), s(:lasgn, :v)),
s(:array,
s(:splat,
s(:call,
s(:call, nil, :line),
:split,
s(:lit, /\=/), s(:lit, 2)))))
s(:splat,
s(:call,
s(:call, nil, :line),
:split,
s(:lit, /\=/), s(:lit, 2))))
out = 'k, v = *line.split(/\\=/, 2)'
util_compare inn, out
end
Expand Down Expand Up @@ -264,9 +278,8 @@ def test_resbody_block
s(:call, nil, :x1),
s(:resbody,
s(:array),
s(:block,
s(:call, nil, :x2),
s(:call, nil, :x3))))
s(:call, nil, :x2),
s(:call, nil, :x3)))

out = "begin\n x1\nrescue\n x2\n x3\nend"
util_compare inn, out
Expand Down Expand Up @@ -352,7 +365,7 @@ def test_binary_operators
end

def test_call_empty_hash
inn = s(:call, nil, :foo, s(:arglist, s(:hash)))
inn = s(:call, nil, :foo, s(:hash))
out = "foo({})"
util_compare inn, out
end
Expand Down Expand Up @@ -431,9 +444,8 @@ def test_rescue_block
inn = s(:rescue,
s(:call, nil, :alpha),
s(:resbody, s(:array),
s(:block,
s(:call, nil, :beta),
s(:call, nil, :gamma))))
s(:call, nil, :beta),
s(:call, nil, :gamma)))
out = "begin\n alpha\nrescue\n beta\n gamma\nend"
util_compare inn, out
end
Expand All @@ -446,7 +458,7 @@ def test_array_adds_parens_around_rescue

util_compare inn, out
end

def test_call_arglist_rescue
inn = s(:call,
nil,
Expand Down Expand Up @@ -474,7 +486,10 @@ def test_unless_vs_if_not
end

def util_compare sexp, expected_ruby, expected_eval = nil
assert_equal expected_ruby, @processor.process(sexp)
assert_equal sexp, RubyParser.new.process(expected_ruby), "ruby -> sexp" if
@check_sexp

assert_equal expected_ruby, @processor.process(sexp), "sexp -> ruby"
assert_equal expected_eval, eval(expected_ruby) if expected_eval
end

Expand Down

0 comments on commit accbc5d

Please sign in to comment.