mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* test/ruby/test_enumerator.rb: Add tests for the recently added
features. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@20641 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
1c9afaea7f
commit
1312189505
2 changed files with 33 additions and 0 deletions
|
@ -58,6 +58,7 @@ class TestEnumerator < Test::Unit::TestCase
|
|||
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 @@ class TestEnumerator < Test::Unit::TestCase
|
|||
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 @@ class TestEnumerator < Test::Unit::TestCase
|
|||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue