3 class TestLambdaParameters < Test::Unit::TestCase
5 def test_exact_parameter
6 assert_raise(ArgumentError){(1..3).each(&lambda{})}
10 assert_equal(1, lambda{|a| a}.call(1))
11 assert_equal([1,2], lambda{|a, b| [a,b]}.call(1,2))
12 assert_raises(ArgumentError) { lambda{|a|}.call(1,2) }
13 assert_raises(ArgumentError) { lambda{|a|}.call() }
14 assert_raises(ArgumentError) { lambda{}.call(1) }
15 assert_raises(ArgumentError) { lambda{|a, b|}.call(1,2,3) }
17 assert_equal(1, ->(a){ a }.call(1))
18 assert_equal([1,2], ->(a,b){ [a,b] }.call(1,2))
19 assert_raises(ArgumentError) { ->(a){ }.call(1,2) }
20 assert_raises(ArgumentError) { ->(a){ }.call() }
21 assert_raises(ArgumentError) { ->(){ }.call(1) }
22 assert_raises(ArgumentError) { ->(a,b){ }.call(1,2,3) }
28 def test_lambda_as_iterator
30 2.times(&->(_){ a += 1 })
34 def test_call_rest_args
35 assert_equal([1,2], ->(*a){ a }.call(1,2))
36 assert_equal([1,2,[]], ->(a,b,*c){ [a,b,c] }.call(1,2))
37 assert_raises(ArgumentError){ ->(a,*b){ }.call() }
40 def test_call_opt_args
41 assert_equal([1,2,3,4], ->(a,b,c=3,d=4){ [a,b,c,d] }.call(1,2))
42 assert_equal([1,2,3,4], ->(a,b,c=0,d=4){ [a,b,c,d] }.call(1,2,3))
43 assert_raises(ArgumentError){ ->(a,b=1){ }.call() }
44 assert_raises(ArgumentError){ ->(a,b=1){ }.call(1,2,3) }
47 def test_call_rest_and_opt
48 assert_equal([1,2,3,[]], ->(a,b=2,c=3,*d){ [a,b,c,d] }.call(1))
49 assert_equal([1,2,3,[]], ->(a,b=0,c=3,*d){ [a,b,c,d] }.call(1,2))
50 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))
51 assert_raises(ArgumentError){ ->(a,b=1,*c){ }.call() }
54 def test_call_with_block
55 f = ->(a,b,c=3,*d,&e){ [a,b,c,d,e.call(d + [a,b,c])] }
56 assert_equal([1,2,3,[],6], f.call(1,2){|z| z.inject{|s,x| s+x} } )
57 assert_equal(nil, ->(&b){ b }.call)
58 foo { puts "bogus block " }
59 assert_equal(1, ->(&b){ b.call }.call { 1 })
61 assert_equal(1, ->(&b){ b.call }.call { 1 })
66 assert_equal(nil, ->(&b){ b }.call)