summaryrefslogtreecommitdiff
path: root/test/ruby/test_call.rb
blob: 33eec70f6a6c934349f347023b4ccb7524c7ab22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require 'test/unit'

class TestCall < Test::Unit::TestCase
  def aaa(a, b=100, *rest)
    res = [a, b]
    res += rest if rest
    return res
  end

  def test_call
    assert_raise(ArgumentError) {aaa()}
    assert_raise(ArgumentError) {aaa}

    assert_equal([1, 100], aaa(1))
    assert_equal([1, 2], aaa(1, 2))
    assert_equal([1, 2, 3, 4], aaa(1, 2, 3, 4))
    assert_equal([1, 2, 3, 4], aaa(1, *[2, 3, 4]))
  end

  def test_callinfo
    bug9622 = '[ruby-core:61422] [Bug #9622]'
    o = Class.new do
      def foo(*args)
        bar(:foo, *args)
      end
      def bar(name)
        name
      end
    end.new
    e = assert_raise(ArgumentError) {o.foo(100)}
    assert_nothing_raised(ArgumentError) {o.foo}
    assert_raise_with_message(ArgumentError, e.message, bug9622) {o.foo(100)}
  end

  def test_safe_call
    s = Struct.new(:x, :y)
    o = s.new("x")
    assert_equal("X", o.x.?upcase)
    assert_nil(o.y.?upcase)
    assert_equal("x", o.x)
    o.?x = 6
    assert_equal(6, o.x)
    o.?x *= 7
    assert_equal(42, o.x)

    o = nil
    assert_nil(o.?x)
    assert_nothing_raised(NoMethodError) {o.?x = 6}
    assert_nothing_raised(NoMethodError) {o.?x *= 7}
  end
end