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
18 changes: 18 additions & 0 deletions core/enumerator/lazy/compact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,21 @@
Enumerator::Lazy.new(Object.new, 100) {}.compact.size.should == nil
end
end

describe "Enumerator::Lazy#compact" do
# Cannot use shared/packed_propagation.rb wholesale: #compact removes the
# packed nil, so neither YieldsMixed nor a zero-argument yield survives it.
describe "propagating the source yield arity to a later stage" do
it "passes every value of a multiple-argument source yield to a later stage's splat block" do
args = nil
Enumerator.new { |y| y.yield 1, 2 }.lazy.compact.map { |*a| args = a }.force
args.should == [1, 2]
end

it "stops propagating once a stage replaces the value" do
yields = []
Enumerator.new { |y| y.yield 1, 2 }.lazy.compact.map { |x| x }.map { |*a| yields << a }.force
yields.should == [[1]]
end
end
end
10 changes: 10 additions & 0 deletions core/enumerator/lazy/drop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative '../../../spec_helper'
require_relative 'fixtures/classes'
require_relative '../../enumerable/shared/value_packing'
require_relative 'shared/packed_propagation'

describe "Enumerator::Lazy#drop" do
describe "value packing of source yields (matches Enumerable#drop)" do
Expand Down Expand Up @@ -72,3 +73,12 @@
s.first(200).drop(100)
end
end

describe "Enumerator::Lazy#drop" do
describe "propagating the source yield arity to a later stage" do
before :each do
@stage = -> e { e.drop(0) }
end
it_behaves_like :enumerator_lazy_packed_propagation, nil
end
end
10 changes: 10 additions & 0 deletions core/enumerator/lazy/drop_while_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative '../../../spec_helper'
require_relative 'fixtures/classes'
require_relative '../../enumerable/shared/value_packing'
require_relative 'shared/packed_propagation'

describe "Enumerator::Lazy#drop_while" do
describe "value packing of source yields (matches Enumerable#drop_while)" do
Expand Down Expand Up @@ -80,3 +81,12 @@
s.first(200).drop_while { |n| n < 100 }
end
end

describe "Enumerator::Lazy#drop_while" do
describe "propagating the source yield arity to a later stage" do
before :each do
@stage = -> e { e.drop_while { false } }
end
it_behaves_like :enumerator_lazy_packed_propagation, nil
end
end
18 changes: 18 additions & 0 deletions core/enumerator/lazy/grep_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

require_relative '../../../spec_helper'
require_relative 'fixtures/classes'
require_relative '../../enumerable/shared/value_packing'
require_relative 'shared/packed_propagation'

describe "Enumerator::Lazy#grep" do
describe "value packing of source yields (matches Enumerable#grep)" do
before :each do
@take = -> e { e.lazy.grep(Object) }
end
it_behaves_like :enumerable_value_packing, nil
end

before :each do
@yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy
@eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy
Expand Down Expand Up @@ -119,3 +128,12 @@
s.first(100).grep(Numeric)
end
end

describe "Enumerator::Lazy#grep" do
describe "propagating the source yield arity to a later stage" do
before :each do
@stage = -> e { e.grep(Object) }
end
it_behaves_like :enumerator_lazy_packed_propagation, nil
end
end
10 changes: 10 additions & 0 deletions core/enumerator/lazy/grep_v_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative '../../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/packed_propagation'

describe "Enumerator::Lazy#grep_v" do
before(:each) do
Expand Down Expand Up @@ -121,3 +122,12 @@
s.first(100).grep_v(String)
end
end

describe "Enumerator::Lazy#grep_v" do
describe "propagating the source yield arity to a later stage" do
before :each do
@stage = -> e { e.grep_v(Float) }
end
it_behaves_like :enumerator_lazy_packed_propagation, nil
end
end
10 changes: 10 additions & 0 deletions core/enumerator/lazy/reject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative '../../../spec_helper'
require_relative 'fixtures/classes'
require_relative '../../enumerable/shared/value_packing'
require_relative 'shared/packed_propagation'

describe "Enumerator::Lazy#reject" do
describe "value packing of source yields (matches Enumerable#reject)" do
Expand Down Expand Up @@ -84,3 +85,12 @@
s.first(100).reject { |n| false }
end
end

describe "Enumerator::Lazy#reject" do
describe "propagating the source yield arity to a later stage" do
before :each do
@stage = -> e { e.reject { false } }
end
it_behaves_like :enumerator_lazy_packed_propagation, nil
end
end
10 changes: 10 additions & 0 deletions core/enumerator/lazy/select_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require_relative '../../../spec_helper'
require_relative 'fixtures/classes'
require_relative '../../enumerable/shared/value_packing'
require_relative 'shared/packed_propagation'

describe "Enumerator::Lazy#select" do
describe "value packing of source yields (matches Enumerable#select)" do
Expand Down Expand Up @@ -109,3 +110,12 @@
eval_count.should == 1
end
end

describe "Enumerator::Lazy#select" do
describe "propagating the source yield arity to a later stage" do
before :each do
@stage = -> e { e.select { true } }
end
it_behaves_like :enumerator_lazy_packed_propagation, nil
end
end
32 changes: 32 additions & 0 deletions core/enumerator/lazy/shared/packed_propagation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# The propagation checked here is the LAZY_MEMO_PACKED bit traveling along the chain in CRuby.
describe :enumerator_lazy_packed_propagation, shared: true do
# @stage: a Proc wrapping a lazy enumerator in the stage under test, e.g. -> e { e.take(12) }.

before :each do
@yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy
end

it "passes a multiple-argument source yield to a later stage's single-argument block as the first value" do
yields = []
@stage.call(@yieldsmixed).map { |v| yields << v }.force
yields.should == EnumeratorLazySpecs::YieldsMixed.initial_yields
end

it "passes every value of a multiple-argument source yield to a later stage's splat block" do
args = nil
@stage.call(Enumerator.new { |y| y.yield 1, 2 }.lazy).map { |*a| args = a }.force
args.should == [1, 2]
end

it "passes a zero-argument source yield on as a single nil" do
args = nil
@stage.call(Enumerator.new { |y| y.yield }.lazy).map { |*a| args = a }.force
args.should == [nil]
end

it "stops propagating once a stage replaces the value" do
yields = []
@stage.call(Enumerator.new { |y| y.yield 1, 2 }.lazy).map { |x| x }.map { |*a| yields << a }.force
yields.should == [[1]]
end
end
10 changes: 10 additions & 0 deletions core/enumerator/lazy/take_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative '../../../spec_helper'
require_relative 'fixtures/classes'
require_relative '../../enumerable/shared/value_packing'
require_relative 'shared/packed_propagation'

describe "Enumerator::Lazy#take" do
describe "value packing of source yields (matches Enumerable#take)" do
Expand Down Expand Up @@ -78,3 +79,12 @@
end
end
end

describe "Enumerator::Lazy#take" do
describe "propagating the source yield arity to a later stage" do
before :each do
@stage = -> e { e.take(12) }
end
it_behaves_like :enumerator_lazy_packed_propagation, nil
end
end
10 changes: 10 additions & 0 deletions core/enumerator/lazy/take_while_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative '../../../spec_helper'
require_relative 'fixtures/classes'
require_relative '../../enumerable/shared/value_packing'
require_relative 'shared/packed_propagation'

describe "Enumerator::Lazy#take_while" do
describe "value packing of source yields (matches Enumerable#take_while)" do
Expand Down Expand Up @@ -66,3 +67,12 @@
end
end
end

describe "Enumerator::Lazy#take_while" do
describe "propagating the source yield arity to a later stage" do
before :each do
@stage = -> e { e.take_while { true } }
end
it_behaves_like :enumerator_lazy_packed_propagation, nil
end
end
24 changes: 24 additions & 0 deletions core/enumerator/lazy/uniq_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,27 @@ def each
s.first(100).uniq
end
end

describe "Enumerator::Lazy#uniq" do
# Cannot use shared/packed_propagation.rb wholesale: the repeated yields of
# YieldsMixed are dropped by #uniq.
describe "propagating the source yield arity to a later stage" do
it "passes every value of a multiple-argument source yield to a later stage's splat block" do
args = nil
Enumerator.new { |y| y.yield 1, 2 }.lazy.uniq.map { |*a| args = a }.force
args.should == [1, 2]
end

it "passes a zero-argument source yield on as a single nil" do
args = nil
Enumerator.new { |y| y.yield }.lazy.uniq.map { |*a| args = a }.force
args.should == [nil]
end

it "stops propagating once a stage replaces the value" do
yields = []
Enumerator.new { |y| y.yield 1, 2 }.lazy.uniq.map { |x| x }.map { |*a| yields << a }.force
yields.should == [[1]]
end
end
end