Skip to content

Commit fafa409

Browse files
committed
Make a dedecated assertion to clarify failed assertions
1 parent 44998f2 commit fafa409

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

test/ruby/test_arity.rb

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require 'test/unit'
33

44
class TestArity < Test::Unit::TestCase
5-
def err_mess(method_proc = nil, argc = 0)
5+
def assert_arity(expected, method_proc = nil, argc = 0)
66
args = (1..argc).to_a
77
assert_raise_with_message(ArgumentError, /wrong number of arguments \(.*\b(\d+)\b.* (\d\S*?)\)/) do
88
case method_proc
@@ -14,7 +14,7 @@ def err_mess(method_proc = nil, argc = 0)
1414
method_proc.call(*args)
1515
end
1616
end
17-
[$1, $2]
17+
assert_equal expected, [$1, $2]
1818
end
1919

2020
def a
@@ -36,35 +36,35 @@ def f(a, b, c=1, *rest, d: 3)
3636
end
3737

3838
def test_method_err_mess
39-
assert_equal %w[1 0], err_mess(:a, 1)
40-
assert_equal %w[10 7..9], err_mess(:b, 10)
41-
assert_equal %w[2 3+], err_mess(:c, 2)
42-
assert_equal %w[2 1], err_mess(:d, 2)
43-
assert_equal %w[0 1], err_mess(:d, 0)
44-
assert_equal %w[2 1], err_mess(:e, 2)
45-
assert_equal %w[0 1], err_mess(:e, 0)
46-
assert_equal %w[1 2+], err_mess(:f, 1)
39+
assert_arity(%w[1 0], :a, 1)
40+
assert_arity(%w[10 7..9], :b, 10)
41+
assert_arity(%w[2 3+], :c, 2)
42+
assert_arity(%w[2 1], :d, 2)
43+
assert_arity(%w[0 1], :d, 0)
44+
assert_arity(%w[2 1], :e, 2)
45+
assert_arity(%w[0 1], :e, 0)
46+
assert_arity(%w[1 2+], :f, 1)
4747
end
4848

4949
def test_proc_err_mess
50-
assert_equal %w[0 1..2], err_mess(->(b, c=42){}, 0)
51-
assert_equal %w[1 2+], err_mess(->(a, b, c=42, *d){}, 1)
52-
assert_equal %w[3 4+], err_mess(->(a, b, *c, d, e){}, 3)
53-
assert_equal %w[3 1..2], err_mess(->(b, c=42){}, 3)
54-
assert_equal %w[1 0], err_mess(->(&block){}, 1)
50+
assert_arity(%w[0 1..2], ->(b, c=42){}, 0)
51+
assert_arity(%w[1 2+], ->(a, b, c=42, *d){}, 1)
52+
assert_arity(%w[3 4+], ->(a, b, *c, d, e){}, 3)
53+
assert_arity(%w[3 1..2], ->(b, c=42){}, 3)
54+
assert_arity(%w[1 0], ->(&block){}, 1)
5555
# Double checking:
5656
p = Proc.new{|b, c=42| :ok}
5757
assert_equal :ok, p.call(1, 2, 3)
5858
assert_equal :ok, p.call
5959
end
6060

6161
def test_message_change_issue_6085
62-
assert_equal %w[3 1..2], err_mess{ SignalException.new(1, "", nil) }
63-
assert_equal %w[1 0], err_mess{ Hash.new(1){} }
64-
assert_equal %w[3 1..2], err_mess{ Module.send :define_method, 1, 2, 3 }
65-
assert_equal %w[1 2], err_mess{ "".sub!(//) }
66-
assert_equal %w[0 1..2], err_mess{ "".sub!{} }
67-
assert_equal %w[0 1+], err_mess{ exec }
68-
assert_equal %w[0 1+], err_mess{ Struct.new }
62+
assert_arity(%w[3 1..2]) { SignalException.new(1, "", nil) }
63+
assert_arity(%w[1 0]) { Hash.new(1){} }
64+
assert_arity(%w[3 1..2]) { Module.send :define_method, 1, 2, 3 }
65+
assert_arity(%w[1 2]) { "".sub!(//) }
66+
assert_arity(%w[0 1..2]) { "".sub!{} }
67+
assert_arity(%w[0 1+]) { exec }
68+
assert_arity(%w[0 1+]) { Struct.new }
6969
end
7070
end

0 commit comments

Comments
 (0)