diff options
Diffstat (limited to 'test/ruby/test_lambda.rb')
| -rw-r--r-- | test/ruby/test_lambda.rb | 203 |
1 files changed, 192 insertions, 11 deletions
diff --git a/test/ruby/test_lambda.rb b/test/ruby/test_lambda.rb index 40a6b84021..c1858a36dd 100644 --- a/test/ruby/test_lambda.rb +++ b/test/ruby/test_lambda.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: false require 'test/unit' class TestLambdaParameters < Test::Unit::TestCase @@ -30,8 +31,10 @@ class TestLambdaParameters < Test::Unit::TestCase bug9605 = '[ruby-core:61468] [Bug #9605]' assert_nothing_raised(ArgumentError, bug9605) {1.times(&->(n){ a += 1 })} assert_equal(3, a, bug9605) - assert_nothing_raised(ArgumentError, bug9605) {a = [[1, 2]].map(&->(x, y) {x+y})} - assert_equal([3], a, bug9605) + assert_nothing_raised(ArgumentError, bug9605) { + a = %w(Hi there how are you).each_with_index.detect(&->(w, i) {w.length == 3}) + } + assert_equal(["how", 2], a, bug9605) end def test_call_rest_args @@ -60,9 +63,9 @@ class TestLambdaParameters < Test::Unit::TestCase assert_equal(nil, ->(&b){ b }.call) foo { puts "bogus block " } assert_equal(1, ->(&b){ b.call }.call { 1 }) - b = nil - assert_equal(1, ->(&b){ b.call }.call { 1 }) - assert_nil(b) + _b = nil + assert_equal(1, ->(&_b){ _b.call }.call { 1 }) + assert_nil(_b) end def test_call_block_from_lambda @@ -71,6 +74,142 @@ class TestLambdaParameters < Test::Unit::TestCase assert_raise(ArgumentError, bug9605) {proc(&plus).call [1,2]} end + def test_proc_inside_lambda_inside_method_return_inside_lambda_inside_method + def self.a + -> do + p = Proc.new{return :a} + p.call + end.call + end + assert_equal(:a, a) + + def self.b + lambda do + p = Proc.new{return :b} + p.call + end.call + end + assert_equal(:b, b) + end + + def test_proc_inside_lambda_inside_method_return_inside_lambda_outside_method + def self.a + -> do + p = Proc.new{return :a} + p.call + end + end + assert_equal(:a, a.call) + + def self.b + lambda do + p = Proc.new{return :b} + p.call + end + end + assert_equal(:b, b.call) + end + + def test_proc_inside_lambda_inside_method_return_outside_lambda_inside_method + def self.a + -> do + Proc.new{return :a} + end.call.call + end + assert_raise(LocalJumpError) {a} + + def self.b + lambda do + Proc.new{return :b} + end.call.call + end + assert_raise(LocalJumpError) {b} + end + + def test_proc_inside_lambda_inside_method_return_outside_lambda_outside_method + def self.a + -> do + Proc.new{return :a} + end + end + assert_raise(LocalJumpError) {a.call.call} + + def self.b + lambda do + Proc.new{return :b} + end + end + assert_raise(LocalJumpError) {b.call.call} + end + + def test_proc_inside_lambda2_inside_method_return_outside_lambda1_inside_method + def self.a + -> do + -> do + Proc.new{return :a} + end.call.call + end.call + end + assert_raise(LocalJumpError) {a} + + def self.b + lambda do + lambda do + Proc.new{return :a} + end.call.call + end.call + end + assert_raise(LocalJumpError) {b} + end + + def test_proc_inside_lambda_toplevel + assert_ruby_status [], <<~RUBY + lambda{ + $g = proc{ return :pr } + }.call + begin + $g.call + rescue LocalJumpError + # OK! + else + raise + end + RUBY + end + + def test_instance_exec + bug12568 = '[ruby-core:76300] [Bug #12568]' + assert_nothing_raised(ArgumentError, bug12568) do + instance_exec([1,2,3], &->(a=[]){ a }) + end + end + + def test_instance_eval_return + bug13090 = '[ruby-core:78917] [Bug #13090] cannot return in lambdas' + x = :ng + assert_nothing_raised(LocalJumpError) do + x = instance_eval(&->(_){return :ok}) + end + ensure + assert_equal(:ok, x, bug13090) + end + + def test_instance_exec_return + bug13090 = '[ruby-core:78917] [Bug #13090] cannot return in lambdas' + x = :ng + assert_nothing_raised(LocalJumpError) do + x = instance_exec(&->(){return :ok}) + end + ensure + assert_equal(:ok, x, bug13090) + end + + def test_arity_error + assert_raise(ArgumentError, '[Bug #12705]') do + [1, 2].tap(&lambda {|a, b|}) + end + end + def foo assert_equal(nil, ->(&b){ b }.call) end @@ -121,27 +260,69 @@ class TestLambdaParameters < Test::Unit::TestCase assert_equal(42, return_in_callee(42), feature8693) end + def break_in_current(val) + 1.tap(&->(*) {break 0}) + val + end + + def break_in_callee(val) + yield_block(&->(*) {break 0}) + val + end + + def test_break + assert_equal(42, break_in_current(42)) + assert_equal(42, break_in_callee(42)) + end + def test_do_lambda_source_location - exp_lineno = __LINE__ + 3 + exp = [__LINE__ + 1, 10, __LINE__ + 5, 7] lmd = ->(x, y, z) do # end - file, lineno = lmd.source_location + file, *loc = lmd.source_location assert_match(/^#{ Regexp.quote(__FILE__) }$/, file) - assert_equal(exp_lineno, lineno, "must be at the beginning of the block") + assert_equal(exp, loc) end def test_brace_lambda_source_location - exp_lineno = __LINE__ + 3 + exp = [__LINE__ + 1, 10, __LINE__ + 5, 5] lmd = ->(x, y, z) { # } - file, lineno = lmd.source_location + file, *loc = lmd.source_location assert_match(/^#{ Regexp.quote(__FILE__) }$/, file) - assert_equal(exp_lineno, lineno, "must be at the beginning of the block") + assert_equal(exp, loc) + end + + def test_not_orphan_return + assert_equal(42, Module.new { extend self + def m1(&b) b.call end; def m2(); m1(&-> { return 42 }) end }.m2) + assert_equal(42, Module.new { extend self + def m1(&b) b end; def m2(); m1(&-> { return 42 }).call end }.m2) + assert_equal(42, Module.new { extend self + def m1(&b) b end; def m2(); m1(&-> { return 42 }) end }.m2.call) + end + + def test_not_orphan_break + assert_equal(42, Module.new { extend self + def m1(&b) b.call end; def m2(); m1(&-> { break 42 }) end }.m2) + assert_equal(42, Module.new { extend self + def m1(&b) b end; def m2(); m1(&-> { break 42 }).call end }.m2) + assert_equal(42, Module.new { extend self + def m1(&b) b end; def m2(); m1(&-> { break 42 }) end }.m2.call) + end + + def test_not_orphan_next + assert_equal(42, Module.new { extend self + def m1(&b) b.call end; def m2(); m1(&-> { next 42 }) end }.m2) + assert_equal(42, Module.new { extend self + def m1(&b) b end; def m2(); m1(&-> { next 42 }).call end }.m2) + assert_equal(42, Module.new { extend self + def m1(&b) b end; def m2(); m1(&-> { next 42 }) end }.m2.call) end end |
