1 # frozen_string_literal: false
6 collect{|e| [e, yield(e)]}.sort{|a,b|a[1]<=>b[1]}
9 ary = collect{|e| [e, yield(e)]}
10 ary.sort{|a,b|a[1]<=>b[1]}
14 class TestIterator < Test::Unit::TestCase
15 def test_yield_at_toplevel
16 assert_separately([],"#{<<~"begin;"}\n#{<<~'end;'}")
19 assert(!defined?(yield))
45 tt2(raise(ArgumentError,""),&block)
48 def test_nested_iterator
50 tt{|j| break if j == 5}
53 assert_raise(ArgumentError) do
59 tt2(raise(ArgumentError,""),&block)
62 def test_block_argument_without_paren
63 assert_raise(ArgumentError) do
68 # iterator break/redo/next
73 done = false # should not reach here
83 bad = true # should not reach here
93 bad = true # should not reach here
101 assert_equal(7, x.size)
102 assert_equal([1, 2, 3, 4, 5, 6, 7], x)
105 def test_array_for_masgn
106 a = [Struct.new(:to_ary).new([1,2])]
108 a.each {|i,j|x << [i,j]}
109 assert_equal([[1,2]], x)
111 for i,j in a; x << [i,j]; end
112 assert_equal([[1,2]], x)
115 def test_append_method_to_built_in_class
116 x = [[1,2],[3,4],[5,6]]
117 assert_equal(x.iter_test1{|e|e}, x.iter_test2{|e|e})
121 def initialize(e); @body = e; end
123 def each0(&block); @body.each(&block); end
124 def each1(&block); @body.each {|*x| block.call(*x) } end
125 def each2(&block); @body.each {|*x| block.call(x) } end
126 def each3(&block); @body.each {|x| block.call(*x) } end
127 def each4(&block); @body.each {|x| block.call(x) } end
128 def each5; @body.each {|*x| yield(*x) } end
129 def each6; @body.each {|*x| yield(x) } end
130 def each7; @body.each {|x| yield(*x) } end
131 def each8; @body.each {|x| yield(x) } end
139 assert_equal([1], IterTest.new(nil).method(:f).to_proc.call([1]))
140 m = /\w+/.match("abc")
141 assert_equal([m], IterTest.new(nil).method(:f).to_proc.call([m]))
143 IterTest.new([0]).each0 {|x| assert_equal(0, x)}
144 IterTest.new([1]).each1 {|x| assert_equal(1, x)}
145 IterTest.new([2]).each2 {|x| assert_equal([2], x)}
146 IterTest.new([4]).each4 {|x| assert_equal(4, x)}
147 IterTest.new([5]).each5 {|x| assert_equal(5, x)}
148 IterTest.new([6]).each6 {|x| assert_equal([6], x)}
149 IterTest.new([8]).each8 {|x| assert_equal(8, x)}
151 IterTest.new([[0]]).each0 {|x| assert_equal([0], x)}
152 IterTest.new([[1]]).each1 {|x| assert_equal([1], x)}
153 IterTest.new([[2]]).each2 {|x| assert_equal([[2]], x)}
154 IterTest.new([[3]]).each3 {|x| assert_equal(3, x)}
155 IterTest.new([[4]]).each4 {|x| assert_equal([4], x)}
156 IterTest.new([[5]]).each5 {|x| assert_equal([5], x)}
157 IterTest.new([[6]]).each6 {|x| assert_equal([[6]], x)}
158 IterTest.new([[7]]).each7 {|x| assert_equal(7, x)}
159 IterTest.new([[8]]).each8 {|x| assert_equal([8], x)}
161 IterTest.new([[0,0]]).each0 {|*x| assert_equal([[0,0]], x)}
162 IterTest.new([[8,8]]).each8 {|*x| assert_equal([[8,8]], x)}
174 m(block_given?,&proc{})
185 m(yield(var), &block)
192 def test_block_passing
195 assert_equal(100, m3(10) {|x|x*x})
209 assert_equal([1,2,3], C.new.collect{|n| n})
213 assert_instance_of(Proc, lambda{})
214 assert_instance_of(Proc, Proc.new{})
215 lambda{|a|assert_equal(a, 1)}.call(1)
219 assert_instance_of(NilClass, get_block)
220 assert_instance_of(Proc, get_block{})
224 assert_nothing_raised {lambda{||}.call}
225 assert_raise(ArgumentError) {lambda{||}.call(1)}
226 assert_nothing_raised {lambda{|a,|}.call(1)}
227 assert_raise(ArgumentError) {lambda{|a,|}.call()}
228 assert_raise(ArgumentError) {lambda{|a,|}.call(1,2)}
231 def get_block(&block)
236 assert_instance_of(Proc, get_block{})
237 assert_nothing_raised {get_block{||}.call()}
238 assert_nothing_raised {get_block{||}.call(1)}
239 assert_nothing_raised {get_block{|a,|}.call(1)}
240 assert_nothing_raised {get_block{|a,|}.call()}
241 assert_nothing_raised {get_block{|a,|}.call(1,2)}
243 assert_nothing_raised {get_block(&lambda{||}).call()}
244 assert_raise(ArgumentError) {get_block(&lambda{||}).call(1)}
245 assert_nothing_raised {get_block(&lambda{|a,|}).call(1)}
246 assert_raise(ArgumentError) {get_block(&lambda{|a,|}).call(1,2)}
248 block = get_block{11}
249 assert_instance_of(Proc, block)
250 assert_instance_of(Proc, block.to_proc)
251 assert_equal(block.clone.call, 11)
252 assert_instance_of(Proc, get_block(&block))
255 assert_instance_of(Proc, lmd)
256 assert_instance_of(Proc, lmd.to_proc)
257 assert_equal(lmd.clone.call, 44)
258 assert_instance_of(Proc, get_block(&lmd))
260 assert_equal(1, Proc.new{|a,| a}.call(1,2,3))
261 assert_nothing_raised {Proc.new{|a,|}.call(1,2)}
271 assert_equal(55, return1_test())
281 assert_equal(60, return2_test())
294 proc_call{return 42}+1
297 def test_proc_return1
298 assert_equal(42, proc_return1())
302 proc_yield{return 42}+1
305 def test_proc_return2
306 assert_equal(42, proc_return2())
310 assert_raise(LocalJumpError) {get_block{break}.call}
311 assert_raise(LocalJumpError) {proc_call2(get_block{break}){}}
313 # cannot use assert_nothing_raised due to passing block.
315 val = lambda{break 11}.call
316 rescue LocalJumpError
317 assert(false, "LocalJumpError occurred from break in lambda")
319 assert_equal(11, val)
322 block = get_block{11}
324 assert_equal(0, block.arity)
325 assert_equal(0, lmd.arity)
326 assert_equal(0, lambda{||}.arity)
327 assert_equal(1, lambda{|a|}.arity)
328 assert_equal(1, lambda{|a,|}.arity)
329 assert_equal(2, lambda{|a,b|}.arity)
334 assert_equal(mobj.arity, mobj.to_proc.arity)
339 marity_test(:marity_test)
342 get_block{|a,n| assert(a,n)}.call(true, "marity")
349 blk.call(:key, :value)
352 def test_yield_vs_call
353 foo{|k,v| assert_equal([:key, :value], [k,v])}
354 bar{|k,v| assert_equal([:key, :value], [k,v])}
365 [{:key=>:value}, H.new].each {|h|
366 h.each{|a| assert_equal([:key, :value], a)}
367 h.each{|a,| assert_equal(:key, a)}
368 h.each{|*a| assert_equal([[:key, :value]], a)}
369 h.each{|k,v| assert_equal([:key, :value], [k,v])}
370 h.each_pair{|a| assert_equal([:key, :value], a)}
371 h.each_pair{|a,| assert_equal(:key, a)}
372 h.each_pair{|*a| assert_equal([[:key, :value]], a)}
373 h.each_pair{|k,v| assert_equal([:key, :value], [k,v])}
383 class ITER_TEST2 < ITER_TEST1
384 include Test::Unit::Assertions
392 assert(ITER_TEST2.new.a {})
397 return yield if block_given?
402 class ITER_TEST4 < ITER_TEST3
403 include Test::Unit::Assertions
405 assert_equal(super, yield)
406 assert_equal(x, super(x, &nil))
411 ITER_TEST4.new.foo(44){55}
414 def test_break__nested_loop1
415 _test_break__nested_loop1 do
420 def _test_break__nested_loop1
424 assert(false, "must not reach here")
427 def test_break__nested_loop2
428 _test_break__nested_loop2 do
433 def _test_break__nested_loop2
437 assert(false, "must not reach here")
440 def test_break__nested_loop3
441 _test_break__nested_loop3 do
446 def _test_break__nested_loop3
450 assert(false, "must not reach here")
453 def test_break_from_enum
454 result = ["a"].inject("ng") {|x,y| break "ok"}
455 assert_equal("ok", result)
458 def _test_return_trace_func(x)
459 set_trace_func(proc {})
460 [].fetch(2) {return x}
465 def test_return_trace_func
466 ok = "returned gracefully"
468 result = _test_return_trace_func(ok)
470 assert_equal(ok, result)
474 class IterString < ::String
476 super if !block_given?
480 # Check that the block passed to an iterator
481 # does not get propagated inappropriately
482 def test_block_given_within_iterator
483 assert_equal(["b"], ["a", "b", "c"].grep(IterString.new("b")) {|s| s})
487 [1,2,3].each.with_index {|x,i|
492 assert_equal(1, e.next)
493 assert_equal(2, e.next)
494 assert_equal(3, e.next)
495 assert_raise(StopIteration){e.next}
497 assert_equal(1, e.next)
501 assert_equal([1,2,3], a)
503 assert_equal([[8, 1, 10], [6, 2, 11], [4, 3, 12]],
504 [8,6,4].zip((1..10),(10..100)).to_a)