summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-enumerator/test
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2019-09-12 22:41:33 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2019-09-13 02:28:56 +0900
commitf5542b992731f4d1af1505837a71e782decab25f (patch)
treec9de1c20abef942ebddc1bf6bae34ca05dfcae3c /mrbgems/mruby-enumerator/test
parentd4af765651193d342488cfbbf81f352f53d5377e (diff)
downloadmruby-f5542b992731f4d1af1505837a71e782decab25f.tar.gz
mruby-f5542b992731f4d1af1505837a71e782decab25f.zip
Add `Enumerator.produce` from Ruby2.7
Diffstat (limited to 'mrbgems/mruby-enumerator/test')
-rw-r--r--mrbgems/mruby-enumerator/test/enumerator.rb40
1 files changed, 39 insertions, 1 deletions
diff --git a/mrbgems/mruby-enumerator/test/enumerator.rb b/mrbgems/mruby-enumerator/test/enumerator.rb
index dce0c2cf2..7cff49c3b 100644
--- a/mrbgems/mruby-enumerator/test/enumerator.rb
+++ b/mrbgems/mruby-enumerator/test/enumerator.rb
@@ -10,9 +10,9 @@ def assert_take(exp, enumerator)
result = []
n = exp.size
enumerator.each do |v|
- break if n == 0
result << v
n -= 1
+ break if n == 0
end
assert_equal exp, result
end
@@ -560,3 +560,41 @@ assert 'Enumerable#zip' do
assert_raise(TypeError) { [1].zip(1) }
end
+
+assert 'Enumerator.produce' do
+ assert_raise(ArgumentError) { Enumerator.produce }
+
+ # Without initial object
+ passed_args = []
+ enum = Enumerator.produce {|obj| passed_args << obj; (obj || 0).succ }
+ assert_equal Enumerator, enum.class
+ assert_take [1, 2, 3], enum
+ assert_equal [nil, 1, 2], passed_args
+
+ # With initial object
+ passed_args = []
+ enum = Enumerator.produce(1) {|obj| passed_args << obj; obj.succ }
+ assert_take [1, 2, 3], enum
+ assert_equal [1, 2], passed_args
+
+ # Raising StopIteration
+ words = %w[The quick brown fox jumps over the lazy dog]
+ enum = Enumerator.produce { words.shift or raise StopIteration }
+ assert_equal %w[The quick brown fox jumps over the lazy dog], enum.to_a
+
+ # Raising StopIteration
+ object = [[[["abc", "def"], "ghi", "jkl"], "mno", "pqr"], "stuv", "wxyz"]
+ enum = Enumerator.produce(object) {|obj|
+ obj.respond_to?(:first) or raise StopIteration
+ obj.first
+ }
+ assert_nothing_raised {
+ assert_equal [
+ [[[["abc", "def"], "ghi", "jkl"], "mno", "pqr"], "stuv", "wxyz"],
+ [[["abc", "def"], "ghi", "jkl"], "mno", "pqr"],
+ [["abc", "def"], "ghi", "jkl"],
+ ["abc", "def"],
+ "abc",
+ ], enum.to_a
+ }
+end