ruby-changes:9104
From: knu <ko1@a...>
Date: Thu, 11 Dec 2008 20:39:09 +0900 (JST)
Subject: [ruby-changes:9104] Ruby:r20641 (ruby_1_8): * test/ruby/test_enumerator.rb: Add tests for the recently added
knu 2008-12-11 20:38:58 +0900 (Thu, 11 Dec 2008) New Revision: 20641 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=20641 Log: * test/ruby/test_enumerator.rb: Add tests for the recently added features. Modified files: branches/ruby_1_8/ChangeLog branches/ruby_1_8/test/ruby/test_enumerator.rb Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 20640) +++ ruby_1_8/ChangeLog (revision 20641) @@ -1,3 +1,8 @@ +Thu Dec 11 20:38:26 2008 Akinori MUSHA <knu@i...> + + * test/ruby/test_enumerator.rb: Add tests for the recently added + features. + Thu Dec 11 20:36:26 2008 Akinori MUSHA <knu@i...> * lib/generator.rb (Enumerator#__generator): Now that Index: ruby_1_8/test/ruby/test_enumerator.rb =================================================================== --- ruby_1_8/test/ruby/test_enumerator.rb (revision 20640) +++ ruby_1_8/test/ruby/test_enumerator.rb (revision 20641) @@ -58,6 +58,7 @@ def test_initialize assert_equal([1, 2, 3], @obj.to_enum(:foo, 1, 2, 3).to_a) assert_equal([1, 2, 3], Enumerator.new(@obj, :foo, 1, 2, 3).to_a) + assert_equal([1, 2, 3], Enumerator.new { |y| i = 0; loop { y << (i += 1) } }.take(3)) assert_raise(ArgumentError) { Enumerator.new } end @@ -66,6 +67,10 @@ e = @obj.to_enum(:foo, 1, 2, 3) assert_nothing_raised { assert_equal(1, e.next) } #assert_raise(TypeError) { e.dup } + + e = Enumerator.new { |y| i = 0; loop { y << (i += 1) } }.dup + assert_nothing_raised { assert_equal(1, e.next) } + #assert_raise(TypeError) { e.dup } end def test_gc @@ -91,6 +96,29 @@ assert_equal([[1,0],[2,1],[3,2]], @obj.to_enum(:foo, 1, 2, 3).with_index.to_a) end + def test_with_object + obj = [0, 1] + ret = (1..10).each.with_object(obj) {|i, memo| + memo[0] += i + memo[1] *= i + } + assert_same(obj, ret) + assert_equal([55, 3628800], ret) + + a = [2,5,2,1,5,3,4,2,1,0] + obj = {} + ret = a.delete_if.with_object(obj) {|i, seen| + if seen.key?(i) + true + else + seen[i] = true + false + end + } + assert_same(obj, ret) + assert_equal([2, 5, 1, 3, 4, 0], a) + end + def test_next_rewind e = @obj.to_enum(:foo, 1, 2, 3) assert_equal(1, e.next) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/