summaryrefslogtreecommitdiff
path: root/test/ruby/test_lambda.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_lambda.rb')
-rw-r--r--test/ruby/test_lambda.rb59
1 files changed, 53 insertions, 6 deletions
diff --git a/test/ruby/test_lambda.rb b/test/ruby/test_lambda.rb
index 24efa71bc1..3ac2e4cb98 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
@@ -25,8 +26,15 @@ class TestLambdaParameters < Test::Unit::TestCase
def test_lambda_as_iterator
a = 0
2.times(&->(_){ a += 1 })
- assert_equal(a, 2)
+ assert_equal(2, a)
assert_raise(ArgumentError) {1.times(&->(){ a += 1 })}
+ 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 = %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
@@ -55,9 +63,48 @@ 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
+ bug9605 = '[ruby-core:61470] [Bug #9605]'
+ plus = ->(x,y) {x+y}
+ assert_raise(ArgumentError, bug9605) {proc(&plus).call [1,2]}
+ 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
@@ -91,7 +138,7 @@ class TestLambdaParameters < Test::Unit::TestCase
end
def return_in_current(val)
- 1.tap &->(*) {return 0}
+ 1.tap(&->(*) {return 0})
val
end
@@ -100,7 +147,7 @@ class TestLambdaParameters < Test::Unit::TestCase
end
def return_in_callee(val)
- yield_block &->(*) {return 0}
+ yield_block(&->(*) {return 0})
val
end