2012-03-14 17:10:16 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestArity < Test::Unit::TestCase
|
|
|
|
def err_mess(method_proc = nil, argc = 0)
|
|
|
|
args = (1..argc).to_a
|
2015-10-24 02:42:22 -04:00
|
|
|
assert_raise_with_message(ArgumentError, /wrong number of arguments \(.*\b(\d+)\b.* (\d\S*?)\)/) do
|
2012-03-14 21:31:43 -04:00
|
|
|
case method_proc
|
2012-03-14 17:10:16 -04:00
|
|
|
when nil
|
|
|
|
yield
|
|
|
|
when Symbol
|
|
|
|
method(method_proc).call(*args)
|
|
|
|
else
|
|
|
|
method_proc.call(*args)
|
2012-03-14 21:31:43 -04:00
|
|
|
end
|
2012-03-14 17:10:16 -04:00
|
|
|
end
|
2015-10-24 02:42:22 -04:00
|
|
|
[$1, $2]
|
2012-03-14 17:10:16 -04:00
|
|
|
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
|
2015-10-24 02:42:22 -04:00
|
|
|
assert_equal %w[1 0], err_mess(:a, 1)
|
|
|
|
assert_equal %w[10 7..9], err_mess(:b, 10)
|
|
|
|
assert_equal %w[2 3+], err_mess(:c, 2)
|
|
|
|
assert_equal %w[2 1], err_mess(:d, 2)
|
|
|
|
assert_equal %w[0 1], err_mess(:d, 0)
|
|
|
|
assert_equal %w[2 1], err_mess(:e, 2)
|
|
|
|
assert_equal %w[0 1], err_mess(:e, 0)
|
|
|
|
assert_equal %w[1 2+], err_mess(:f, 1)
|
2012-03-14 17:10:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_proc_err_mess
|
2015-10-24 02:42:22 -04:00
|
|
|
assert_equal %w[0 1..2], err_mess(->(b, c=42){}, 0)
|
|
|
|
assert_equal %w[1 2+], err_mess(->(a, b, c=42, *d){}, 1)
|
|
|
|
assert_equal %w[3 4+], err_mess(->(a, b, *c, d, e){}, 3)
|
|
|
|
assert_equal %w[3 1..2], err_mess(->(b, c=42){}, 3)
|
|
|
|
assert_equal %w[1 0], err_mess(->(&block){}, 1)
|
2012-03-14 17:10:16 -04:00
|
|
|
# Double checking:
|
|
|
|
p = Proc.new{|b, c=42| :ok}
|
|
|
|
assert_equal :ok, p.call(1, 2, 3)
|
|
|
|
assert_equal :ok, p.call
|
|
|
|
end
|
2012-03-14 17:10:34 -04:00
|
|
|
|
|
|
|
def test_message_change_issue_6085
|
2015-10-24 02:42:22 -04:00
|
|
|
assert_equal %w[3 1..2], err_mess{ SignalException.new(1, "", nil) }
|
|
|
|
assert_equal %w[1 0], err_mess{ Hash.new(1){} }
|
|
|
|
assert_equal %w[3 1..2], err_mess{ Module.send :define_method, 1, 2, 3 }
|
|
|
|
assert_equal %w[1 2], err_mess{ "".sub!(//) }
|
|
|
|
assert_equal %w[0 1..2], err_mess{ "".sub!{} }
|
|
|
|
assert_equal %w[0 1+], err_mess{ exec }
|
|
|
|
assert_equal %w[0 1+], err_mess{ Struct.new }
|
2012-03-14 17:10:34 -04:00
|
|
|
end
|
2012-03-14 17:10:16 -04:00
|
|
|
end
|