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 })
|
|
|
|
assert_equal(a, 2)
|
2010-12-18 02:02:35 -05:00
|
|
|
assert_raise(ArgumentError) {1.times(&->(){ a += 1 })}
|
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 })
|
|
|
|
b = nil
|
|
|
|
assert_equal(1, ->(&b){ b.call }.call { 1 })
|
2005-09-12 19:09:39 -04:00
|
|
|
assert_nil(b)
|
2005-08-08 19:24:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
|
|
|
1.tap &->(*) {return 0}
|
|
|
|
val
|
|
|
|
end
|
|
|
|
|
|
|
|
def yield_block
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
|
|
|
def return_in_callee(val)
|
|
|
|
yield_block &->(*) {return 0}
|
|
|
|
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
|
2005-08-08 19:24:29 -04:00
|
|
|
end
|