Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- Fixed multiple nested rescue/ensure exprs (larsch)

[git-p4: depot-paths = "//src/ruby2ruby/dev/": change = 6658]
  • Loading branch information...
commit 715bcdddecb8825b5869907320efef5d607c0ec6 1 parent 727ccda
@zenspider zenspider authored
Showing with 48 additions and 2 deletions.
  1. +7 −2 lib/ruby2ruby.rb
  2. +41 −0 test/test_ruby2ruby.rb
View
9 lib/ruby2ruby.rb
@@ -141,7 +141,7 @@ def process_begin(exp)
code << "begin"
until exp.empty?
src = process(exp.shift)
- src = indent(src) unless src =~ /(^|\n)rescue/ # ensure no level 0 rescues
+ src = indent(src) unless src =~ /(^|\n)(rescue|ensure)/ # ensure no level 0 rescues
code << src
end
code << "end"
@@ -400,8 +400,10 @@ def process_ensure(exp)
ens = exp.shift
ens = nil if ens == s(:nil)
ens = process(ens) || "# do nothing"
+ ens = "begin\n#{ens}\nend\n" if ens =~ /(^|\n)rescue/
body.sub!(/\n\s*end\z/, '')
+ body = indent(body) unless body =~ /(^|\n)rescue/
return "#{body}\nensure\n#{indent ens}"
end
@@ -722,10 +724,12 @@ def process_rescue exp
body ||= "# do nothing"
simple = exp.size == 1 && !exp.resbody.block
+
resbodies = []
until exp.empty? do
resbody = exp.shift
- simple &&= resbody[1] == s(:array) && resbody[2] != nil
+ simple &&= resbody[1] == s(:array)
+ simple &&= resbody[2] != nil && resbody[2].node_type != :block
resbodies << process(resbody)
end
@@ -908,6 +912,7 @@ def rewrite_rescue exp
complex ||= exp.resbody.block
complex ||= exp.find_nodes(:resbody).any? { |n| n[1] != s(:array) }
complex ||= exp.find_nodes(:resbody).any? { |n| n.last.nil? }
+ complex ||= exp.find_nodes(:resbody).any? { |n| n[2] and n[2].node_type == :block }
handled = context.first == :ensure
View
41 test/test_ruby2ruby.rb
@@ -306,6 +306,47 @@ def test_method_comments
util_compare inn, out
end
+ def test_basic_ensure
+ inn = s(:ensure, s(:lit, 1), s(:lit, 2))
+ out = "begin\n 1\nensure\n 2\nend"
+ util_compare inn, out
+ end
+
+ def test_nested_ensure
+ inn = s(:ensure, s(:lit, 1), s(:ensure, s(:lit, 2), s(:lit, 3)))
+ out = "begin\n 1\nensure\n begin\n 2\n ensure\n 3\n end\nend"
+ util_compare inn, out
+ end
+
+ def test_nested_rescue
+ inn = s(:ensure, s(:lit, 1), s(:rescue, s(:lit, 2), s(:resbody, s(:array), s(:lit, 3))))
+ out = "begin\n 1\nensure\n 2 rescue 3\nend"
+ util_compare inn, out
+ end
+
+ def test_nested_rescue_exception
+ inn = s(:ensure, s(:lit, 1), s(:rescue, s(:lit, 2), s(:resbody, s(:array, s(:const, :Exception)), s(:lit, 3))))
+ out = "begin\n 1\nensure\n begin\n 2\n rescue Exception\n 3\n end\nend"
+ util_compare inn, out
+ end
+
+ def test_nested_rescue_exception2
+ inn = s(:ensure, s(:rescue, s(:lit, 2), s(:resbody, s(:array, s(:const, :Exception)), s(:lit, 3))), s(:lit, 1))
+ out = "begin\n 2\nrescue Exception\n 3\nensure\n 1\nend"
+ util_compare inn, out
+ end
+
+ def test_rescue_block
+ inn = s(:rescue,
+ s(:call, nil, :alpha, s(:arglist)),
+ s(:resbody, s(:array),
+ s(:block,
+ s(:call, nil, :beta, s(:arglist)),
+ s(:call, nil, :gamma, s(:arglist)))))
+ out = "begin\n alpha\nrescue\n beta\n gamma\nend"
+ util_compare inn, out
+ end
+
def util_compare sexp, expected_ruby, expected_eval = nil
assert_equal expected_ruby, @processor.process(sexp)
assert_equal expected_eval, eval(expected_ruby) if expected_eval
Please sign in to comment.
Something went wrong with that request. Please try again.