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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
require 'test/unit'
class TestKeywordArguments < Test::Unit::TestCase
def f1(str: "foo", num: 424242)
[str, num]
end
def test_f1
assert_equal(["foo", 424242], f1)
assert_equal(["bar", 424242], f1(str: "bar"))
assert_equal(["foo", 111111], f1(num: 111111))
assert_equal(["bar", 111111], f1(str: "bar", num: 111111))
assert_raise(ArgumentError) { f1(str: "bar", check: true) }
assert_raise(ArgumentError) { f1("string") }
end
def f2(x, str: "foo", num: 424242)
[x, str, num]
end
def test_f2
assert_equal([:xyz, "foo", 424242], f2(:xyz))
end
def f3(str: "foo", num: 424242, **h)
[str, num, h]
end
def test_f3
assert_equal(["foo", 424242, {}], f3)
assert_equal(["bar", 424242, {}], f3(str: "bar"))
assert_equal(["foo", 111111, {}], f3(num: 111111))
assert_equal(["bar", 111111, {}], f3(str: "bar", num: 111111))
assert_equal(["bar", 424242, {:check=>true}], f3(str: "bar", check: true))
assert_raise(ArgumentError) { f3("string") }
end
define_method(:f4) {|str: "foo", num: 424242| [str, num] }
def test_f4
assert_equal(["foo", 424242], f4)
assert_equal(["bar", 424242], f4(str: "bar"))
assert_equal(["foo", 111111], f4(num: 111111))
assert_equal(["bar", 111111], f4(str: "bar", num: 111111))
assert_raise(ArgumentError) { f4(str: "bar", check: true) }
assert_raise(ArgumentError) { f4("string") }
end
define_method(:f5) {|str: "foo", num: 424242, **h| [str, num, h] }
def test_f5
assert_equal(["foo", 424242, {}], f5)
assert_equal(["bar", 424242, {}], f5(str: "bar"))
assert_equal(["foo", 111111, {}], f5(num: 111111))
assert_equal(["bar", 111111, {}], f5(str: "bar", num: 111111))
assert_equal(["bar", 424242, {:check=>true}], f5(str: "bar", check: true))
assert_raise(ArgumentError) { f5("string") }
end
def f6(str: "foo", num: 424242, **h, &blk)
[str, num, h, blk]
end
def test_f6 # [ruby-core:40518]
assert_equal(["foo", 424242, {}, nil], f6)
assert_equal(["bar", 424242, {}, nil], f6(str: "bar"))
assert_equal(["foo", 111111, {}, nil], f6(num: 111111))
assert_equal(["bar", 111111, {}, nil], f6(str: "bar", num: 111111))
assert_equal(["bar", 424242, {:check=>true}, nil], f6(str: "bar", check: true))
a = f6 {|x| x + 42 }
assert_equal(["foo", 424242, {}], a[0, 3])
assert_equal(43, a.last.call(1))
end
def f7(*r, str: "foo", num: 424242, **h)
[r, str, num, h]
end
def test_f7 # [ruby-core:41772]
assert_equal([[], "foo", 424242, {}], f7)
assert_equal([[], "bar", 424242, {}], f7(str: "bar"))
assert_equal([[], "foo", 111111, {}], f7(num: 111111))
assert_equal([[], "bar", 111111, {}], f7(str: "bar", num: 111111))
assert_equal([[1], "foo", 424242, {}], f7(1))
assert_equal([[1, 2], "foo", 424242, {}], f7(1, 2))
assert_equal([[1, 2, 3], "foo", 424242, {}], f7(1, 2, 3))
assert_equal([[1], "bar", 424242, {}], f7(1, str: "bar"))
assert_equal([[1, 2], "bar", 424242, {}], f7(1, 2, str: "bar"))
assert_equal([[1, 2, 3], "bar", 424242, {}], f7(1, 2, 3, str: "bar"))
end
define_method(:f8) { |opt = :ion, *rest, key: :word|
[opt, rest, key]
}
def test_f8
assert_equal([:ion, [], :word], f8)
assert_equal([1, [], :word], f8(1))
assert_equal([1, [2], :word], f8(1, 2))
end
def f9(r, o=42, *args, p, k: :key, **kw, &b)
[r, o, args, p, k, kw, b]
end
def test_f9
assert_equal([1, 42, [], 2, :key, {}, nil], f9(1, 2))
assert_equal([1, 2, [], 3, :key, {}, nil], f9(1, 2, 3))
assert_equal([1, 2, [3], 4, :key, {}, nil], f9(1, 2, 3, 4))
assert_equal([1, 2, [3, 4], 5, :key, {str: "bar"}, nil], f9(1, 2, 3, 4, 5, str: "bar"))
end
def test_method_parameters
assert_equal([[:key, :str], [:key, :num]], method(:f1).parameters);
assert_equal([[:req, :x], [:key, :str], [:key, :num]], method(:f2).parameters);
assert_equal([[:key, :str], [:key, :num], [:keyrest, :h]], method(:f3).parameters);
assert_equal([[:key, :str], [:key, :num]], method(:f4).parameters);
assert_equal([[:key, :str], [:key, :num], [:keyrest, :h]], method(:f5).parameters);
assert_equal([[:key, :str], [:key, :num], [:keyrest, :h], [:block, :blk]], method(:f6).parameters);
assert_equal([[:rest, :r], [:key, :str], [:key, :num], [:keyrest, :h]], method(:f7).parameters);
assert_equal([[:opt, :opt], [:rest, :rest], [:key, :key]], method(:f8).parameters) # [Bug #7540] [ruby-core:50735]
assert_equal([[:req, :r], [:opt, :o], [:rest, :args], [:req, :p], [:key, :k],
[:keyrest, :kw], [:block, :b]], method(:f9).parameters)
end
def test_lambda
f = ->(str: "foo", num: 424242) { [str, num] }
assert_equal(["foo", 424242], f[])
assert_equal(["bar", 424242], f[str: "bar"])
assert_equal(["foo", 111111], f[num: 111111])
assert_equal(["bar", 111111], f[str: "bar", num: 111111])
end
end
|