summaryrefslogtreecommitdiff
path: root/test/ruby/test_arity.rb
blob: bd26d5f0f53019fcf17e0771d9218ebf55276f9d (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: false
require 'test/unit'

class TestArity < Test::Unit::TestCase
  def assert_arity(expected, method_proc = nil, argc = 0)
    args = (1..argc).to_a
    assert_raise_with_message(ArgumentError, /wrong number of arguments \(.*\b(\d+)\b.* (\d\S*?)\)/) do
      case method_proc
      when nil
        yield
      when Symbol
        method(method_proc).call(*args)
      else
        method_proc.call(*args)
      end
    end
    assert_equal expected, [$1, $2]
  end

  def a
  end

  def b(a, b, c, d=1, e=2, f, g, h, i, &block)
  end

  def c(a, b, c, d=1, e=2, *rest)
  end

  def d(a, b: 42)
  end

  def e(a, b:42, **c)
  end

  def f(a, b, c=1, *rest, d: 3)
  end

  def test_method_err_mess
    assert_arity(%w[1 0],     :a, 1)
    assert_arity(%w[10 7..9], :b, 10)
    assert_arity(%w[2 3+],    :c, 2)
    assert_arity(%w[2 1],     :d, 2)
    assert_arity(%w[0 1],     :d, 0)
    assert_arity(%w[2 1],     :e, 2)
    assert_arity(%w[0 1],     :e, 0)
    assert_arity(%w[1 2+],    :f, 1)
  end

  def test_proc_err_mess
    assert_arity(%w[0 1..2],  ->(b, c=42){}, 0)
    assert_arity(%w[1 2+],    ->(a, b, c=42, *d){}, 1)
    assert_arity(%w[3 4+],    ->(a, b, *c, d, e){}, 3)
    assert_arity(%w[3 1..2],  ->(b, c=42){}, 3)
    assert_arity(%w[1 0],     ->(&block){}, 1)
    # Double checking:
    p = Proc.new{|b, c=42| :ok}
    assert_equal :ok,  p.call(1, 2, 3)
    assert_equal :ok,  p.call
  end

  def test_message_change_issue_6085
    assert_arity(%w[3 1..2])  { SignalException.new(1, "", nil) }
    assert_arity(%w[1 0])     { Hash.new(1){} }
    assert_arity(%w[3 1..2])  { Module.send :define_method, 1, 2, 3 }
    assert_arity(%w[1 2])     { "".sub!(//) }
    assert_arity(%w[0 1..2])  { "".sub!{} }
    assert_arity(%w[0 1+])    { exec }
  end
end