summaryrefslogtreecommitdiff
path: root/test/ruby/test_enumerator.rb
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-01-29 13:59:13 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-01-29 13:59:13 +0000
commit7390164a646c83b22b4f265f29968664b6cad059 (patch)
tree5dd8fbbd359c454cb1a29b2755ecdea7708dfae1 /test/ruby/test_enumerator.rb
parentc8048c5f070403cbed0674cbc243a10ea86568ea (diff)
* test/ruby/test_enumerator.rb: add tests to achieve over 90% test
coverage of enumerator.c. * test/ruby/test_enum.rb: add for enum.c. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15328 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_enumerator.rb')
-rw-r--r--test/ruby/test_enumerator.rb59
1 files changed, 59 insertions, 0 deletions
diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb
index cb0c393236..5f62a1f3b2 100644
--- a/test/ruby/test_enumerator.rb
+++ b/test/ruby/test_enumerator.rb
@@ -1,6 +1,16 @@
require 'test/unit'
class TestEnumerator < Test::Unit::TestCase
+ def setup
+ @obj = Object.new
+ class << @obj
+ include Enumerable
+ def foo(*a)
+ a.each {|x| yield x }
+ end
+ end
+ end
+
def enum_test obj
i = 0
obj.map{|e|
@@ -43,5 +53,54 @@ class TestEnumerator < Test::Unit::TestCase
assert_equal :ok2, e.next
assert_raise(StopIteration){e.next}
end
+
+
+ def test_initialize
+ assert_equal([1, 2, 3], @obj.to_enum(:foo, 1, 2, 3).to_a)
+ assert_equal([2, 3, 4], @obj.to_enum(:foo, 1, 2, 3) {|x| x + 1 }.to_a)
+ assert_equal([1, 2, 3], Enumerable::Enumerator.new(@obj, :foo, 1, 2, 3).to_a)
+ assert_raise(ArgumentError) { Enumerable::Enumerator.new }
+ end
+
+ def test_initialize_copy
+ assert_equal([1, 2, 3], @obj.to_enum(:foo, 1, 2, 3).dup.to_a)
+ e = @obj.to_enum(:foo, 1, 2, 3)
+ assert_nothing_raised { assert_equal(1, e.next) }
+ assert_raise(TypeError) { e.dup }
+ end
+
+ def test_gc
+ assert_nothing_raised do
+ 1.times do
+ foo = [1,2,3].to_enum
+ GC.start
+ end
+ GC.start
+ end
+ end
+
+ def test_slice
+ assert_equal([[1,2,3],[4,5,6],[7,8,9],[10]], (1..10).each_slice(3).to_a)
+ end
+
+ def test_cons
+ a = [[1,2,3], [2,3,4], [3,4,5], [4,5,6], [5,6,7], [6,7,8], [7,8,9], [8,9,10]]
+ assert_equal(a, (1..10).each_cons(3).to_a)
+ end
+
+ def test_with_index
+ assert_equal([[1,0],[2,1],[3,2]], @obj.to_enum(:foo, 1, 2, 3).with_index.to_a)
+ end
+
+ def test_next_rewind
+ e = @obj.to_enum(:foo, 1, 2, 3)
+ assert_equal(1, e.next)
+ assert_equal(2, e.next)
+ e.rewind
+ assert_equal(1, e.next)
+ assert_equal(2, e.next)
+ assert_equal(3, e.next)
+ assert_raise(StopIteration) { e.next }
+ end
end