Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 215 lines (199 sloc) 10.345 kb
47fc3bf @headius Fix for JRUBY-1468, fixes the original issue, adds a whole raft of tests...
headius authored
1 require 'test/unit'
2
3 class TestVarArgBlock < Test::Unit::TestCase
4 def blockyield(arg)
5 yield arg
6 end
7
8 def blockarg(arg, &b)
9 b.call(arg)
10 end
11
12 def test_vararg_blocks
13 Proc.new { |*element| assert_equal [["a"]], element }.call( ["a"] )
14 Proc.new { |*element| assert_equal ["a"], element }.call( "a" )
15 proc { |*element| assert_equal [["a"]], element }.call( ["a"] )
16 proc { |*element| assert_equal ["a"], element }.call( "a" )
17 lambda { |*element| assert_equal [["a"]], element }.call( ["a"] )
18 lambda { |*element| assert_equal ["a"], element }.call( "a" )
19 blockyield(["a"]) { |*element| assert_equal [["a"]], element }
20 blockyield("a") { |*element| assert_equal ["a"], element }
21 blockyield(["a"], &Proc.new { |*element| assert_equal [["a"]], element })
22 blockyield("a", &Proc.new { |*element| assert_equal ["a"], element })
23 blockyield(["a"], &proc { |*element| assert_equal [["a"]], element })
24 blockyield("a", &proc { |*element| assert_equal ["a"], element })
25 blockyield(["a"], &lambda { |*element| assert_equal [["a"]], element })
26 blockyield("a", &lambda { |*element| assert_equal ["a"], element })
27 blockarg(["a"]) { |*element| assert_equal [["a"]], element }
28 blockarg("a") { |*element| assert_equal ["a"], element }
29 blockarg(["a"], &Proc.new { |*element| assert_equal [["a"]], element })
30 blockarg("a", &Proc.new { |*element| assert_equal ["a"], element })
31 blockarg(["a"], &proc { |*element| assert_equal [["a"]], element })
32 blockarg("a", &proc { |*element| assert_equal ["a"], element })
33 blockarg(["a"], &lambda { |*element| assert_equal [["a"]], element })
34 blockarg("a", &lambda { |*element| assert_equal ["a"], element })
35 end
36
37 def test_requiredarg_blocks
38 Proc.new { |element| assert_equal ["a"], element }.call( ["a"] )
39 Proc.new { |element| assert_equal "a", element }.call( "a" )
40 proc { |element| assert_equal ["a"], element }.call( ["a"] )
41 proc { |element| assert_equal "a", element }.call( "a" )
42 lambda { |element| assert_equal ["a"], element }.call( ["a"] )
43 lambda { |element| assert_equal "a", element }.call( "a" )
44 blockyield(["a"]) { |element| assert_equal ["a"], element }
45 blockyield("a") { |element| assert_equal "a", element }
46 blockyield(["a"], &Proc.new { |element| assert_equal ["a"], element })
47 blockyield("a", &Proc.new { |element| assert_equal "a", element })
48 blockyield(["a"], &proc { |element| assert_equal ["a"], element })
49 blockyield("a", &proc { |element| assert_equal "a", element })
50 blockyield(["a"], &lambda { |element| assert_equal ["a"], element })
51 blockyield("a", &lambda { |element| assert_equal "a", element })
52 blockarg(["a"]) { |element| assert_equal ["a"], element }
53 blockarg("a") { |element| assert_equal "a", element }
54 blockarg(["a"], &Proc.new { |element| assert_equal ["a"], element })
55 blockarg("a", &Proc.new { |element| assert_equal "a", element })
56 blockarg(["a"], &proc { |element| assert_equal ["a"], element })
57 blockarg("a", &proc { |element| assert_equal "a", element })
58 blockarg(["a"], &lambda { |element| assert_equal ["a"], element })
59 blockarg("a", &lambda { |element| assert_equal "a", element })
60 end
61
62 def test_requiredargs_blocks
63 Proc.new { |element, a| assert_equal "a", element }.call( ["a"] )
64 Proc.new { |element, a| assert_equal "a", element }.call( "a" )
65 assert_raises(ArgumentError) {
66 proc { |element, a| assert_equal ["a"], element }.call( ["a"] )
67 }
68 assert_raises(ArgumentError) {
69 proc { |element, a| assert_equal "a", element }.call( "a" )
70 }
71 assert_raises(ArgumentError) {
72 lambda { |element, a| assert_equal ["a"], element }.call( ["a"] )
73 }
74 assert_raises(ArgumentError) {
75 lambda { |element, a| assert_equal "a", element }.call( "a" )
76 }
77 blockyield(["a"]) { |element, a| assert_equal "a", element }
78 blockyield("a") { |element, a| assert_equal "a", element }
79 blockyield(["a"], &Proc.new { |element, a| assert_equal "a", element })
80 blockyield("a", &Proc.new { |element, a| assert_equal "a", element })
81 blockyield(["a"], &proc { |element, a| assert_equal "a", element })
82 blockyield("a", &proc { |element, a| assert_equal "a", element })
83 blockyield(["a"], &lambda { |element, a| assert_equal "a", element })
84 blockyield("a", &lambda { |element, a| assert_equal "a", element })
85 blockarg(["a"]) { |element, a| assert_equal "a", element }
86 blockarg("a") { |element, a| assert_equal "a", element }
87 blockarg(["a"], &Proc.new { |element, a| assert_equal "a", element })
88 blockarg("a", &Proc.new { |element, a| assert_equal "a", element })
89 assert_raises(ArgumentError) {
90 blockarg(["a"], &proc { |element, a| assert_equal ["a"], element })
91 }
92 assert_raises(ArgumentError) {
93 blockarg("a", &proc { |element, a| assert_equal "a", element })
94 }
95 assert_raises(ArgumentError) {
96 blockarg(["a"], &lambda { |element, a| assert_equal ["a"], element })
97 }
98 assert_raises(ArgumentError) {
99 blockarg("a", &lambda { |element, a| assert_equal "a", element })
100 }
101 end
102
103 def check_all_definemethods(obj)
104 results = obj.foo1 ["a"]
105 assert_equal(results[0], results[1])
106 results = obj.foo2 "a"
107 assert_equal(results[0], results[1])
108 results = obj.foo3 ["a"]
109 assert_equal(results[0], results[1])
110 results = obj.foo4 "a"
111 assert_equal(results[0], results[1])
112 results = obj.foo5 ["a"]
113 assert_equal(results[0], results[1])
114 results = obj.foo6 "a"
115 assert_equal(results[0], results[1])
116 results = obj.foo7 ["a"]
117 assert_equal(results[0], results[1])
118 results = obj.foo8 "a"
119 assert_equal(results[0], results[1])
120 results = obj.foo9 ["a"]
121 assert_equal(results[0], results[1])
122 results = obj.foo10 "a"
123 assert_equal(results[0], results[1])
124 results = obj.foo11 ["a"]
125 assert_equal(results[0], results[1])
126 results = obj.foo12 "a"
127 assert_equal(results[0], results[1])
128 results = obj.foo13 ["a"]
129 assert_equal(results[0], results[1])
130 results = obj.foo14 "a"
131 assert_equal(results[0], results[1])
132 end
133
134 def check_requiredargs_definemethods(obj)
135 results = obj.foo1 ["a"]
136 assert_equal(results[0], results[1])
137 results = obj.foo2 "a"
138 assert_equal(results[0], results[1])
139 assert_raises(ArgumentError) { results = obj.foo3 ["a"] }
140 assert_raises(ArgumentError) { results = obj.foo4 "a" }
141 assert_raises(ArgumentError) { results = obj.foo5 ["a"] }
142 assert_raises(ArgumentError) { results = obj.foo6 "a" }
143 assert_raises(ArgumentError) { results = obj.foo7 ["a"] }
144 assert_raises(ArgumentError) { results = obj.foo8 "a" }
145 assert_raises(ArgumentError) { results = obj.foo9 ["a"] }
146 assert_raises(ArgumentError) { results = obj.foo10 "a" }
147 assert_raises(ArgumentError) { results = obj.foo11 ["a"] }
148 assert_raises(ArgumentError) { results = obj.foo12 "a" }
149 assert_raises(ArgumentError) { results = obj.foo13 ["a"] }
150 assert_raises(ArgumentError) { results = obj.foo14 "a" }
151 end
152
153 def test_definemethods
154 obj = Object.new
155
156 class << obj
157 define_method :foo1, Proc.new { |*element| [[["a"]], element] }
158 define_method :foo2, Proc.new { |*element| [["a"], element] }
159 define_method :foo3, proc { |*element| [[["a"]], element] }
160 define_method :foo4, proc { |*element| [["a"], element] }
161 define_method :foo5, lambda { |*element| [[["a"]], element] }
162 define_method :foo6, lambda { |*element| [["a"], element] }
163 define_method(:foo7) { |*element| [[["a"]], element] }
164 define_method(:foo8) { |*element| [["a"], element] }
165 define_method :foo9, &Proc.new { |*element| [[["a"]], element] }
166 define_method :foo10, &Proc.new { |*element| [["a"], element] }
167 define_method :foo11, &proc { |*element| [[["a"]], element] }
168 define_method :foo12, &proc { |*element| [["a"], element] }
169 define_method :foo13, &lambda { |*element| [[["a"]], element] }
170 define_method :foo14, &lambda { |*element| [["a"], element] }
171 end
172
173 check_all_definemethods(obj)
174
175 class << obj
176 define_method :foo1, Proc.new { |element| [["a"], element] }
177 define_method :foo2, Proc.new { |element| ["a", element] }
178 define_method :foo3, proc { |element| [["a"], element] }
179 define_method :foo4, proc { |element| ["a", element] }
180 define_method :foo5, lambda { |element| [["a"], element] }
181 define_method :foo6, lambda { |element| ["a", element] }
182 define_method(:foo7) { |element| [["a"], element] }
183 define_method(:foo8) { |element| ["a", element] }
184 define_method :foo9, &Proc.new { |element| [["a"], element] }
185 define_method :foo10, &Proc.new { |element| ["a", element] }
186 define_method :foo11, &proc { |element| [["a"], element] }
187 define_method :foo12, &proc { |element| ["a", element] }
188 define_method :foo13, &lambda { |element| [["a"], element] }
189 define_method :foo14, &lambda { |element| ["a", element] }
190 end
191
192 check_all_definemethods(obj)
193
194 class << obj
195 define_method :foo1, Proc.new { |element, a| ["a", element] }
196 define_method :foo2, Proc.new { |element, a| ["a", element] }
197 define_method :foo3, proc { |element, a| [["a"], element] }
198 define_method :foo4, proc { |element, a| ["a", element] }
199 define_method :foo5, lambda { |element, a| [["a"], element] }
200 define_method :foo6, lambda { |element, a| ["a", element] }
201 define_method(:foo7) { |element, a| [["a"], element] }
202 define_method(:foo8) { |element, a| ["a", element] }
203 define_method :foo9, &Proc.new { |element, a| [["a"], element] }
204 define_method :foo10, &Proc.new { |element, a| ["a", element] }
205 define_method :foo11, &proc { |element, a| [["a"], element] }
206 define_method :foo12, &proc { |element, a| ["a", element] }
207 define_method :foo13, &lambda { |element, a| [["a"], element] }
208 define_method :foo14, &lambda { |element, a| ["a", element] }
209 end
210
211 check_requiredargs_definemethods(obj)
212 end
213 end
214
Something went wrong with that request. Please try again.