mirror of
https://github.com/ruby/ruby.git
synced 2025-08-25 22:14:37 +02:00
Document and test Enumerator.produce
Co-authored-by: Victor Shepelev <zverok.offline@gmail.com>
This commit is contained in:
parent
775037613b
commit
ac3e8834e0
2 changed files with 67 additions and 0 deletions
|
@ -811,4 +811,49 @@ class TestEnumerator < Test::Unit::TestCase
|
|||
e5.inspect
|
||||
)
|
||||
end
|
||||
|
||||
def test_produce
|
||||
assert_raise(ArgumentError) { Enumerator.produce }
|
||||
|
||||
# Without initial object
|
||||
passed_args = []
|
||||
enum = Enumerator.produce { |obj| passed_args << obj; (obj || 0).succ }
|
||||
assert_instance_of(Enumerator, enum)
|
||||
assert_equal Float::INFINITY, enum.size
|
||||
assert_equal [1, 2, 3], enum.take(3)
|
||||
assert_equal [nil, 1, 2], passed_args
|
||||
|
||||
# With initial object
|
||||
passed_args = []
|
||||
enum = Enumerator.produce(1) { |obj| passed_args << obj; obj.succ }
|
||||
assert_instance_of(Enumerator, enum)
|
||||
assert_equal Float::INFINITY, enum.size
|
||||
assert_equal [1, 2, 3], enum.take(3)
|
||||
assert_equal [1, 2], passed_args
|
||||
|
||||
# Raising StopIteration
|
||||
words = "The quick brown fox jumps over the lazy dog.".scan(/\w+/)
|
||||
enum = Enumerator.produce { words.shift or raise StopIteration }
|
||||
assert_equal Float::INFINITY, enum.size
|
||||
assert_instance_of(Enumerator, enum)
|
||||
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_equal Float::INFINITY, enum.size
|
||||
assert_instance_of(Enumerator, enum)
|
||||
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
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue