1require 'test/unit'
2
3class TestLambdaParameters < Test::Unit::TestCase
4
5  def test_exact_parameter
6    assert_raise(ArgumentError){(1..3).each(&lambda{})}
7  end
8
9  def test_call_simple
10    assert_equal(1, lambda{|a| a}.call(1))
11    assert_equal([1,2], lambda{|a, b| [a,b]}.call(1,2))
12    assert_raise(ArgumentError) { lambda{|a|}.call(1,2) }
13    assert_raise(ArgumentError) { lambda{|a|}.call() }
14    assert_raise(ArgumentError) { lambda{}.call(1) }
15    assert_raise(ArgumentError) { lambda{|a, b|}.call(1,2,3) }
16
17    assert_equal(1, ->(a){ a }.call(1))
18    assert_equal([1,2], ->(a,b){ [a,b] }.call(1,2))
19    assert_raise(ArgumentError) { ->(a){ }.call(1,2) }
20    assert_raise(ArgumentError) { ->(a){ }.call() }
21    assert_raise(ArgumentError) { ->(){ }.call(1) }
22    assert_raise(ArgumentError) { ->(a,b){ }.call(1,2,3) }
23  end
24
25  def test_lambda_as_iterator
26    a = 0
27    2.times(&->(_){ a += 1 })
28    assert_equal(a, 2)
29    assert_raise(ArgumentError) {1.times(&->(){ a += 1 })}
30  end
31
32  def test_call_rest_args
33    assert_equal([1,2], ->(*a){ a }.call(1,2))
34    assert_equal([1,2,[]], ->(a,b,*c){ [a,b,c] }.call(1,2))
35    assert_raise(ArgumentError){ ->(a,*b){ }.call() }
36  end
37
38  def test_call_opt_args
39    assert_equal([1,2,3,4], ->(a,b,c=3,d=4){ [a,b,c,d] }.call(1,2))
40    assert_equal([1,2,3,4], ->(a,b,c=0,d=4){ [a,b,c,d] }.call(1,2,3))
41    assert_raise(ArgumentError){ ->(a,b=1){ }.call() }
42    assert_raise(ArgumentError){ ->(a,b=1){ }.call(1,2,3) }
43  end
44
45  def test_call_rest_and_opt
46    assert_equal([1,2,3,[]], ->(a,b=2,c=3,*d){ [a,b,c,d] }.call(1))
47    assert_equal([1,2,3,[]], ->(a,b=0,c=3,*d){ [a,b,c,d] }.call(1,2))
48    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))
49    assert_raise(ArgumentError){ ->(a,b=1,*c){ }.call() }
50  end
51
52  def test_call_with_block
53    f = ->(a,b,c=3,*d,&e){ [a,b,c,d,e.call(d + [a,b,c])] }
54    assert_equal([1,2,3,[],6], f.call(1,2){|z| z.inject{|s,x| s+x} } )
55    assert_equal(nil, ->(&b){ b }.call)
56    foo { puts "bogus block " }
57    assert_equal(1, ->(&b){ b.call }.call { 1 })
58    b = nil
59    assert_equal(1, ->(&b){ b.call }.call { 1 })
60    assert_nil(b)
61  end
62
63  def foo
64    assert_equal(nil, ->(&b){ b }.call)
65  end
66
67  def test_in_basic_object
68    bug5966 = '[ruby-core:42349]'
69    called = false
70    BasicObject.new.instance_eval {->() {called = true}.()}
71    assert_equal(true, called, bug5966)
72  end
73
74  def test_location_on_error
75    bug6151 = '[ruby-core:43314]'
76    called = 0
77    line, f = __LINE__, lambda do
78      called += 1
79      true
80    end
81    e = assert_raise(ArgumentError) do
82      f.call(42)
83    end
84    assert_send([e.backtrace.first, :start_with?, "#{__FILE__}:#{line}:"], bug6151)
85    assert_equal(0, called)
86    e = assert_raise(ArgumentError) do
87      42.times(&f)
88    end
89    assert_send([e.backtrace.first, :start_with?, "#{__FILE__}:#{line}:"], bug6151)
90    assert_equal(0, called)
91  end
92
93  def test_do_lambda_source_location
94    exp_lineno = __LINE__ + 3
95    lmd = ->(x,
96             y,
97             z) do
98      #
99    end
100    file, lineno = lmd.source_location
101    assert_match(/^#{ Regexp.quote(__FILE__) }$/, file)
102    assert_equal(exp_lineno, lineno, "must be at the beginning of the block")
103  end
104
105  def test_brace_lambda_source_location
106    exp_lineno = __LINE__ + 3
107    lmd = ->(x,
108             y,
109             z) {
110      #
111    }
112    file, lineno = lmd.source_location
113    assert_match(/^#{ Regexp.quote(__FILE__) }$/, file)
114    assert_equal(exp_lineno, lineno, "must be at the beginning of the block")
115  end
116end
117