2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2005-08-08 19:24:29 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestLambdaParameters < Test::Unit::TestCase
|
2007-06-05 13:26:00 -04:00
|
|
|
|
|
|
|
def test_exact_parameter
|
|
|
|
assert_raise(ArgumentError){(1..3).each(&lambda{})}
|
|
|
|
end
|
|
|
|
|
2005-08-08 19:24:29 -04:00
|
|
|
def test_call_simple
|
2007-06-05 13:26:00 -04:00
|
|
|
assert_equal(1, lambda{|a| a}.call(1))
|
|
|
|
assert_equal([1,2], lambda{|a, b| [a,b]}.call(1,2))
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(ArgumentError) { lambda{|a|}.call(1,2) }
|
|
|
|
assert_raise(ArgumentError) { lambda{|a|}.call() }
|
|
|
|
assert_raise(ArgumentError) { lambda{}.call(1) }
|
|
|
|
assert_raise(ArgumentError) { lambda{|a, b|}.call(1,2,3) }
|
2007-06-05 13:26:00 -04:00
|
|
|
|
2005-08-08 19:24:29 -04:00
|
|
|
assert_equal(1, ->(a){ a }.call(1))
|
|
|
|
assert_equal([1,2], ->(a,b){ [a,b] }.call(1,2))
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(ArgumentError) { ->(a){ }.call(1,2) }
|
|
|
|
assert_raise(ArgumentError) { ->(a){ }.call() }
|
|
|
|
assert_raise(ArgumentError) { ->(){ }.call(1) }
|
|
|
|
assert_raise(ArgumentError) { ->(a,b){ }.call(1,2,3) }
|
2005-08-08 19:24:29 -04:00
|
|
|
end
|
|
|
|
|
2007-01-04 22:43:12 -05:00
|
|
|
def test_lambda_as_iterator
|
|
|
|
a = 0
|
|
|
|
2.times(&->(_){ a += 1 })
|
2014-03-13 12:18:45 -04:00
|
|
|
assert_equal(2, a)
|
2010-12-18 02:02:35 -05:00
|
|
|
assert_raise(ArgumentError) {1.times(&->(){ a += 1 })}
|
2014-03-13 12:18:45 -04:00
|
|
|
bug9605 = '[ruby-core:61468] [Bug #9605]'
|
|
|
|
assert_nothing_raised(ArgumentError, bug9605) {1.times(&->(n){ a += 1 })}
|
|
|
|
assert_equal(3, a, bug9605)
|
2017-03-18 11:30:14 -04:00
|
|
|
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)
|
2007-01-04 22:43:12 -05:00
|
|
|
end
|
|
|
|
|
2005-08-08 19:24:29 -04:00
|
|
|
def test_call_rest_args
|
|
|
|
assert_equal([1,2], ->(*a){ a }.call(1,2))
|
|
|
|
assert_equal([1,2,[]], ->(a,b,*c){ [a,b,c] }.call(1,2))
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(ArgumentError){ ->(a,*b){ }.call() }
|
2005-08-08 19:24:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_call_opt_args
|
|
|
|
assert_equal([1,2,3,4], ->(a,b,c=3,d=4){ [a,b,c,d] }.call(1,2))
|
|
|
|
assert_equal([1,2,3,4], ->(a,b,c=0,d=4){ [a,b,c,d] }.call(1,2,3))
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(ArgumentError){ ->(a,b=1){ }.call() }
|
|
|
|
assert_raise(ArgumentError){ ->(a,b=1){ }.call(1,2,3) }
|
2005-08-08 19:24:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_call_rest_and_opt
|
|
|
|
assert_equal([1,2,3,[]], ->(a,b=2,c=3,*d){ [a,b,c,d] }.call(1))
|
|
|
|
assert_equal([1,2,3,[]], ->(a,b=0,c=3,*d){ [a,b,c,d] }.call(1,2))
|
|
|
|
assert_equal([1,2,3,[4,5,6]], ->(a,b=0,c=0,*d){ [a,b,c,d] }.call(1,2,3,4,5,6))
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(ArgumentError){ ->(a,b=1,*c){ }.call() }
|
2005-08-08 19:24:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_call_with_block
|
|
|
|
f = ->(a,b,c=3,*d,&e){ [a,b,c,d,e.call(d + [a,b,c])] }
|
|
|
|
assert_equal([1,2,3,[],6], f.call(1,2){|z| z.inject{|s,x| s+x} } )
|
|
|
|
assert_equal(nil, ->(&b){ b }.call)
|
|
|
|
foo { puts "bogus block " }
|
|
|
|
assert_equal(1, ->(&b){ b.call }.call { 1 })
|
2016-02-19 02:48:02 -05:00
|
|
|
_b = nil
|
|
|
|
assert_equal(1, ->(&_b){ _b.call }.call { 1 })
|
|
|
|
assert_nil(_b)
|
2005-08-08 19:24:29 -04:00
|
|
|
end
|
|
|
|
|
2014-03-13 12:18:45 -04:00
|
|
|
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
|
|
|
|
|
2021-04-01 13:28:00 -04:00
|
|
|
def test_proc_inside_lambda_inside_method_return_inside_lambda_inside_method
|
|
|
|
def self.a
|
2021-04-04 02:26:09 -04:00
|
|
|
-> do
|
2021-04-01 13:28:00 -04:00
|
|
|
p = Proc.new{return :a}
|
|
|
|
p.call
|
|
|
|
end.call
|
|
|
|
end
|
|
|
|
assert_equal(:a, a)
|
|
|
|
|
|
|
|
def self.b
|
2021-04-04 02:26:09 -04:00
|
|
|
lambda do
|
2021-04-01 13:28:00 -04:00
|
|
|
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
|
2021-04-04 02:26:09 -04:00
|
|
|
-> do
|
2021-04-01 13:28:00 -04:00
|
|
|
p = Proc.new{return :a}
|
|
|
|
p.call
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal(:a, a.call)
|
|
|
|
|
|
|
|
def self.b
|
2021-04-04 02:26:09 -04:00
|
|
|
lambda do
|
2021-04-01 13:28:00 -04:00
|
|
|
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
|
2021-04-04 02:26:09 -04:00
|
|
|
-> do
|
2021-04-01 13:28:00 -04:00
|
|
|
Proc.new{return :a}
|
|
|
|
end.call.call
|
|
|
|
end
|
|
|
|
assert_raise(LocalJumpError) {a}
|
|
|
|
|
|
|
|
def self.b
|
2021-04-04 02:26:09 -04:00
|
|
|
lambda do
|
2021-04-01 13:28:00 -04:00
|
|
|
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
|
2021-04-04 02:26:09 -04:00
|
|
|
-> do
|
2021-04-01 13:28:00 -04:00
|
|
|
Proc.new{return :a}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_raise(LocalJumpError) {a.call.call}
|
|
|
|
|
|
|
|
def self.b
|
2021-04-04 02:26:09 -04:00
|
|
|
lambda do
|
2021-04-01 13:28:00 -04:00
|
|
|
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
|
2021-04-04 02:26:09 -04:00
|
|
|
-> do
|
2021-04-01 13:28:00 -04:00
|
|
|
-> do
|
|
|
|
Proc.new{return :a}
|
|
|
|
end.call.call
|
|
|
|
end.call
|
|
|
|
end
|
|
|
|
assert_raise(LocalJumpError) {a}
|
|
|
|
|
|
|
|
def self.b
|
2021-04-04 02:26:09 -04:00
|
|
|
lambda do
|
2021-04-01 13:28:00 -04:00
|
|
|
lambda do
|
|
|
|
Proc.new{return :a}
|
|
|
|
end.call.call
|
|
|
|
end.call
|
|
|
|
end
|
|
|
|
assert_raise(LocalJumpError) {b}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proc_inside_lambda_toplevel
|
|
|
|
assert_separately [], <<~RUBY
|
|
|
|
lambda{
|
|
|
|
$g = proc{ return :pr }
|
|
|
|
}.call
|
|
|
|
begin
|
|
|
|
$g.call
|
|
|
|
rescue LocalJumpError
|
|
|
|
# OK!
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
2019-07-13 12:04:01 -04:00
|
|
|
def pass_along(&block)
|
|
|
|
lambda(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pass_along2(&block)
|
|
|
|
pass_along(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_create_non_lambda_for_proc_one_level
|
2020-12-12 09:11:47 -05:00
|
|
|
prev_warning, Warning[:deprecated] = Warning[:deprecated], false
|
2019-07-13 12:04:01 -04:00
|
|
|
f = pass_along {}
|
|
|
|
refute_predicate(f, :lambda?, '[Bug #15620]')
|
|
|
|
assert_nothing_raised(ArgumentError) { f.call(:extra_arg) }
|
2020-12-12 09:11:47 -05:00
|
|
|
ensure
|
|
|
|
Warning[:deprecated] = prev_warning
|
2019-07-13 12:04:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_create_non_lambda_for_proc_two_levels
|
2020-12-12 09:11:47 -05:00
|
|
|
prev_warning, Warning[:deprecated] = Warning[:deprecated], false
|
2019-07-13 12:04:01 -04:00
|
|
|
f = pass_along2 {}
|
|
|
|
refute_predicate(f, :lambda?, '[Bug #15620]')
|
|
|
|
assert_nothing_raised(ArgumentError) { f.call(:extra_arg) }
|
2020-12-12 09:11:47 -05:00
|
|
|
ensure
|
|
|
|
Warning[:deprecated] = prev_warning
|
2019-07-13 12:04:01 -04:00
|
|
|
end
|
|
|
|
|
2016-07-07 14:59:38 -04:00
|
|
|
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
|
|
|
|
|
2016-12-31 03:07:58 -05:00
|
|
|
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
|
|
|
|
|
2017-03-18 21:11:12 -04:00
|
|
|
def test_arity_error
|
|
|
|
assert_raise(ArgumentError, '[Bug #12705]') do
|
|
|
|
[1, 2].tap(&lambda {|a, b|})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-08-08 19:24:29 -04:00
|
|
|
def foo
|
|
|
|
assert_equal(nil, ->(&b){ b }.call)
|
|
|
|
end
|
2012-02-25 21:26:49 -05:00
|
|
|
|
|
|
|
def test_in_basic_object
|
|
|
|
bug5966 = '[ruby-core:42349]'
|
|
|
|
called = false
|
|
|
|
BasicObject.new.instance_eval {->() {called = true}.()}
|
|
|
|
assert_equal(true, called, bug5966)
|
|
|
|
end
|
2012-03-15 23:00:44 -04:00
|
|
|
|
|
|
|
def test_location_on_error
|
|
|
|
bug6151 = '[ruby-core:43314]'
|
|
|
|
called = 0
|
|
|
|
line, f = __LINE__, lambda do
|
|
|
|
called += 1
|
|
|
|
true
|
|
|
|
end
|
|
|
|
e = assert_raise(ArgumentError) do
|
|
|
|
f.call(42)
|
|
|
|
end
|
|
|
|
assert_send([e.backtrace.first, :start_with?, "#{__FILE__}:#{line}:"], bug6151)
|
|
|
|
assert_equal(0, called)
|
|
|
|
e = assert_raise(ArgumentError) do
|
|
|
|
42.times(&f)
|
|
|
|
end
|
|
|
|
assert_send([e.backtrace.first, :start_with?, "#{__FILE__}:#{line}:"], bug6151)
|
|
|
|
assert_equal(0, called)
|
|
|
|
end
|
2013-08-08 21:49:38 -04:00
|
|
|
|
|
|
|
def return_in_current(val)
|
2014-03-05 08:27:22 -05:00
|
|
|
1.tap(&->(*) {return 0})
|
2013-08-08 21:49:38 -04:00
|
|
|
val
|
|
|
|
end
|
|
|
|
|
|
|
|
def yield_block
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
|
|
|
def return_in_callee(val)
|
2014-03-05 08:27:22 -05:00
|
|
|
yield_block(&->(*) {return 0})
|
2013-08-08 21:49:38 -04:00
|
|
|
val
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_return
|
|
|
|
feature8693 = '[ruby-core:56193] [Feature #8693]'
|
|
|
|
assert_equal(42, return_in_current(42), feature8693)
|
|
|
|
assert_equal(42, return_in_callee(42), feature8693)
|
|
|
|
end
|
2013-09-19 03:59:07 -04:00
|
|
|
|
2019-07-14 23:59:53 -04:00
|
|
|
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
|
|
|
|
|
2013-09-19 03:59:07 -04:00
|
|
|
def test_do_lambda_source_location
|
|
|
|
exp_lineno = __LINE__ + 3
|
|
|
|
lmd = ->(x,
|
|
|
|
y,
|
|
|
|
z) do
|
|
|
|
#
|
|
|
|
end
|
|
|
|
file, lineno = lmd.source_location
|
|
|
|
assert_match(/^#{ Regexp.quote(__FILE__) }$/, file)
|
2013-09-19 16:02:06 -04:00
|
|
|
assert_equal(exp_lineno, lineno, "must be at the beginning of the block")
|
2013-09-19 03:59:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_brace_lambda_source_location
|
|
|
|
exp_lineno = __LINE__ + 3
|
|
|
|
lmd = ->(x,
|
|
|
|
y,
|
|
|
|
z) {
|
|
|
|
#
|
|
|
|
}
|
|
|
|
file, lineno = lmd.source_location
|
|
|
|
assert_match(/^#{ Regexp.quote(__FILE__) }$/, file)
|
2013-09-19 16:02:06 -04:00
|
|
|
assert_equal(exp_lineno, lineno, "must be at the beginning of the block")
|
2013-09-19 03:59:07 -04:00
|
|
|
end
|
2019-07-15 01:17:27 -04:00
|
|
|
|
|
|
|
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
|
2005-08-08 19:24:29 -04:00
|
|
|
end
|