summaryrefslogtreecommitdiff
path: root/test/ruby/test_enumerator.rb
diff options
context:
space:
mode:
authorko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-08-20 18:58:32 +0000
committerko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-08-20 18:58:32 +0000
commitad56f8c6110bd8233a4f2224ccdd1cc7ef645dc1 (patch)
tree274ff252edc7e4aba38c03752ebba1e3000bfebe /test/ruby/test_enumerator.rb
parent009debfd02f5024b85b5c325f42533b40f4028e9 (diff)
* enumerator.c (next_i): fix to return with Fiber#yield at
the end of each block. [ruby-dev:31470] * enumerator.c (enumerator_next_p): call init_next if not initialized. [ruby-dev:31514] * test/ruby/test_enumerator.rb: add tests for Enumerator. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13120 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_enumerator.rb')
-rw-r--r--test/ruby/test_enumerator.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb
new file mode 100644
index 0000000000..6ff8f82019
--- /dev/null
+++ b/test/ruby/test_enumerator.rb
@@ -0,0 +1,48 @@
+require 'test/unit'
+
+class TestEnumerator < Test::Unit::TestCase
+ def enum_test obj
+ i = 0
+ obj.map{|e|
+ [i+=1, e]
+ }
+ end
+
+ def test_iterators
+ assert_equal [[1, 0], [2, 1], [3, 2]], enum_test(3.times)
+ assert_equal [[1, :x], [2, :y], [3, :z]], enum_test([:x, :y, :z].each)
+ assert_equal [[1, [:x, 1]], [2, [:y, 2]]], enum_test({:x=>1, :y=>2})
+ end
+
+ ## Enumerator as Iterator
+
+ def test_next
+ e = 3.times
+ 3.times{|i|
+ assert_equal i, e.next
+ }
+ assert_raise(StopIteration){e.next}
+ end
+
+ def test_next?
+ e = 3.times
+ assert_equal true, e.next?
+ 3.times{|i|
+ assert_equal true, e.next?
+ assert_equal i, e.next
+ }
+ assert_equal false, e.next?
+ end
+
+ def test_nested_itaration
+ def (o = Object.new).each
+ yield :ok1
+ yield [:ok2, :x].each.next
+ end
+ e = o.to_enum
+ assert_equal :ok1, e.next
+ assert_equal :ok2, e.next
+ assert_raise(StopIteration){e.next}
+ end
+end
+