Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/enumerator/each_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
require_relative '../../spec_helper'
require_relative 'shared/each'

describe "Enumerator#each" do
describe "passing source-yielded arguments to the block" do
before :each do
@object = -> e { e }
end
it_behaves_like :enum_each, nil
end

before :each do
object_each_with_arguments = Object.new
def object_each_with_arguments.each_with_arguments(arg, *args)
Expand Down
11 changes: 11 additions & 0 deletions core/enumerator/lazy/each_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require_relative '../../../spec_helper'
require_relative '../shared/each'

describe "Enumerator::Lazy#each" do
describe "passing source-yielded arguments to the block (matches Enumerator#each)" do
before :each do
@object = -> e { e.lazy }
end
it_behaves_like :enum_each, nil
end
end
46 changes: 46 additions & 0 deletions core/enumerator/shared/each.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# #each passes source-yielded values to the block by ordinary block arity
# (rb_yield_values2 semantics in CRuby), unlike the Enumerable collection methods
# which pack them via rb_enum_values_pack() (see enumerable/shared/value_packing.rb).
describe :enum_each, shared: true do
# @object must be set to a Proc that wraps an Enumerator into the receiver
# under test (e.g. -> e { e } for Enumerator#each, -> e { e.lazy } for Lazy#each).
describe "with a source that yields multiple values" do
before :each do
@enum = @object.call(Enumerator.new { |y| y.yield 1, 2; y.yield 3, 4 })
end

it "yields the first value to a single-argument block" do
collected = []
@enum.each { |x| collected << x }
collected.should == [1, 3]
end

it "yields each value to a multi-argument block" do
collected = []
@enum.each { |x, y| collected << [x, y] }
collected.should == [[1, 2], [3, 4]]
end

it "gathers the values for a splat block" do
collected = []
@enum.each { |*args| collected << args }
collected.should == [[1, 2], [3, 4]]
end
end

describe "with a source that yields a single value" do
it "yields the value to a single-argument block" do
collected = []
@object.call(Enumerator.new { |y| y.yield 7; y.yield 8 }).each { |x| collected << x }
collected.should == [7, 8]
end
end

describe "with a source that yields no value" do
it "yields nil to a single-argument block" do
collected = []
@object.call(Enumerator.new { |y| y.yield; y.yield }).each { |x| collected << x }
collected.should == [nil, nil]
end
end
end
Loading