diff options
Diffstat (limited to 'spec/ruby/core/enumerator/lazy')
25 files changed, 1048 insertions, 0 deletions
diff --git a/spec/ruby/core/enumerator/lazy/collect_concat_spec.rb b/spec/ruby/core/enumerator/lazy/collect_concat_spec.rb new file mode 100644 index 0000000000..8c19dcbbf9 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/collect_concat_spec.rb @@ -0,0 +1,8 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/collect_concat', __FILE__) + +describe "Enumerator::Lazy#collect_concat" do + it_behaves_like :enumerator_lazy_collect_concat, :collect_concat +end diff --git a/spec/ruby/core/enumerator/lazy/collect_spec.rb b/spec/ruby/core/enumerator/lazy/collect_spec.rb new file mode 100644 index 0000000000..764b8af36d --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/collect_spec.rb @@ -0,0 +1,8 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/collect', __FILE__) + +describe "Enumerator::Lazy#collect" do + it_behaves_like :enumerator_lazy_collect, :collect +end diff --git a/spec/ruby/core/enumerator/lazy/drop_spec.rb b/spec/ruby/core/enumerator/lazy/drop_spec.rb new file mode 100644 index 0000000000..eb65bb246b --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/drop_spec.rb @@ -0,0 +1,52 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Enumerator::Lazy#drop" do + before :each do + @yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy + @eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy + ScratchPad.record [] + end + + after :each do + ScratchPad.clear + end + + it "returns a new instance of Enumerator::Lazy" do + ret = @yieldsmixed.drop(1) + ret.should be_an_instance_of(Enumerator::Lazy) + ret.should_not equal(@yieldsmixed) + end + + it "sets difference of given count with old size to new size" do + Enumerator::Lazy.new(Object.new, 100) {}.drop(20).size.should == 80 + Enumerator::Lazy.new(Object.new, 100) {}.drop(200).size.should == 0 + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.drop(2).first(2).should == [2, 3] + + @eventsmixed.drop(0).first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + + describe "on a nested Lazy" do + it "sets difference of given count with old size to new size" do + Enumerator::Lazy.new(Object.new, 100) {}.drop(20).drop(50).size.should == 30 + Enumerator::Lazy.new(Object.new, 100) {}.drop(50).drop(20).size.should == 30 + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.drop(2).drop(2).first(2).should == [4, 5] + + @eventsmixed.drop(0).drop(0).first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/drop_while_spec.rb b/spec/ruby/core/enumerator/lazy/drop_while_spec.rb new file mode 100644 index 0000000000..a08644a20c --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/drop_while_spec.rb @@ -0,0 +1,60 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Enumerator::Lazy#drop_while" do + before :each do + @yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy + @eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy + ScratchPad.record [] + end + + after :each do + ScratchPad.clear + end + + it "returns a new instance of Enumerator::Lazy" do + ret = @yieldsmixed.drop_while {} + ret.should be_an_instance_of(Enumerator::Lazy) + ret.should_not equal(@yieldsmixed) + end + + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.drop_while { |v| v }.size.should == nil + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.drop_while { |n| n < 5 }.first(2).should == [5, 6] + + @eventsmixed.drop_while { false }.first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + + it "calls the block with initial values when yield with multiple arguments" do + yields = [] + @yieldsmixed.drop_while { |v| yields << v; true }.force + yields.should == EnumeratorLazySpecs::YieldsMixed.initial_yields + end + + it "raises an ArgumentError when not given a block" do + lambda { @yieldsmixed.drop_while }.should raise_error(ArgumentError) + end + + describe "on a nested Lazy" do + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.take(20).drop_while { |v| v }.size.should == nil + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.drop_while { |n| n < 5 }.drop_while { |n| n.odd? }.first(2).should == [6, 7] + + @eventsmixed.drop_while { false }.drop_while { false }.first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/enum_for_spec.rb b/spec/ruby/core/enumerator/lazy/enum_for_spec.rb new file mode 100644 index 0000000000..b2ef2c881e --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/enum_for_spec.rb @@ -0,0 +1,8 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/to_enum', __FILE__) + +describe "Enumerator::Lazy#enum_for" do + it_behaves_like :enumerator_lazy_to_enum, :enum_for +end diff --git a/spec/ruby/core/enumerator/lazy/find_all_spec.rb b/spec/ruby/core/enumerator/lazy/find_all_spec.rb new file mode 100644 index 0000000000..ab2e69c857 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/find_all_spec.rb @@ -0,0 +1,8 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/select', __FILE__) + +describe "Enumerator::Lazy#find_all" do + it_behaves_like :enumerator_lazy_select, :find_all +end diff --git a/spec/ruby/core/enumerator/lazy/fixtures/classes.rb b/spec/ruby/core/enumerator/lazy/fixtures/classes.rb new file mode 100644 index 0000000000..e35592ba1c --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/fixtures/classes.rb @@ -0,0 +1,54 @@ +# -*- encoding: us-ascii -*- + +module EnumeratorLazySpecs + class SpecificError < Exception; end + + class YieldsMixed + def self.initial_yields + [nil, 0, 0, 0, 0, nil, :default_arg, [], [], [0], [0, 1], [0, 1, 2]] + end + + def self.gathered_yields + [nil, 0, [0, 1], [0, 1, 2], [0, 1, 2], nil, :default_arg, [], [], [0], [0, 1], [0, 1, 2]] + end + + def self.gathered_non_array_yields + [nil, 0, nil, :default_arg] + end + + def self.gathered_yields_with_args(arg, *args) + [nil, 0, [0, 1], [0, 1, 2], [0, 1, 2], nil, arg, args, [], [0], [0, 1], [0, 1, 2]] + end + + def each(arg=:default_arg, *args) + yield + yield 0 + yield 0, 1 + yield 0, 1, 2 + yield(*[0, 1, 2]) + yield nil + yield arg + yield args + yield [] + yield [0] + yield [0, 1] + yield [0, 1, 2] + end + end + + class EventsMixed + def each + ScratchPad << :before_yield + + yield 0 + + ScratchPad << :after_yield + + raise SpecificError + + ScratchPad << :after_error + + :should_not_reach_here + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/flat_map_spec.rb b/spec/ruby/core/enumerator/lazy/flat_map_spec.rb new file mode 100644 index 0000000000..b7fba5e81c --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/flat_map_spec.rb @@ -0,0 +1,8 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/collect_concat', __FILE__) + +describe "Enumerator::Lazy#flat_map" do + it_behaves_like :enumerator_lazy_collect_concat, :flat_map +end diff --git a/spec/ruby/core/enumerator/lazy/force_spec.rb b/spec/ruby/core/enumerator/lazy/force_spec.rb new file mode 100644 index 0000000000..03ff9a0fb6 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/force_spec.rb @@ -0,0 +1,30 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Enumerator::Lazy#force" do + before :each do + @yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy + @eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy + ScratchPad.record [] + end + + after :each do + ScratchPad.clear + end + + it "passes given arguments to receiver.each" do + @yieldsmixed.force(:arg1, :arg2, :arg3).should == + EnumeratorLazySpecs::YieldsMixed.gathered_yields_with_args(:arg1, :arg2, :arg3) + end + + describe "on a nested Lazy" do + it "calls all block and returns an Array" do + (0..Float::INFINITY).lazy.map(&:succ).take(2).force.should == [1, 2] + + @eventsmixed.take(1).map(&:succ).force.should == [1] + ScratchPad.recorded == [:after_yields] + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/grep_spec.rb b/spec/ruby/core/enumerator/lazy/grep_spec.rb new file mode 100644 index 0000000000..372be80d61 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/grep_spec.rb @@ -0,0 +1,82 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Enumerator::Lazy#grep" do + before :each do + @yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy + @eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy + ScratchPad.record [] + end + + after :each do + ScratchPad.clear + end + + it "requires an argument" do + Enumerator::Lazy.instance_method(:grep).arity.should == 1 + end + + it "returns a new instance of Enumerator::Lazy" do + ret = @yieldsmixed.grep(Object) {} + ret.should be_an_instance_of(Enumerator::Lazy) + ret.should_not equal(@yieldsmixed) + + ret = @yieldsmixed.grep(Object) + ret.should be_an_instance_of(Enumerator::Lazy) + ret.should_not equal(@yieldsmixed) + end + + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.grep(Object) {}.size.should == nil + Enumerator::Lazy.new(Object.new, 100) {}.grep(Object).size.should == nil + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times when not given a block" do + (0..Float::INFINITY).lazy.grep(Integer).first(3).should == [0, 1, 2] + + @eventsmixed.grep(BasicObject).first(1) + ScratchPad.recorded.should == [:before_yield] + end + + it "stops after specified times when given a block" do + (0..Float::INFINITY).lazy.grep(Integer, &:succ).first(3).should == [1, 2, 3] + + @eventsmixed.grep(BasicObject) {}.first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + + it "calls the block with a gathered array when yield with multiple arguments" do + yields = [] + @yieldsmixed.grep(BasicObject) { |v| yields << v }.force + yields.should == EnumeratorLazySpecs::YieldsMixed.gathered_yields + + @yieldsmixed.grep(BasicObject).force.should == yields + end + + describe "on a nested Lazy" do + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.grep(Object) {}.size.should == nil + Enumerator::Lazy.new(Object.new, 100) {}.grep(Object).size.should == nil + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times when not given a block" do + (0..Float::INFINITY).lazy.grep(Integer).grep(Object).first(3).should == [0, 1, 2] + + @eventsmixed.grep(BasicObject).grep(Object).first(1) + ScratchPad.recorded.should == [:before_yield] + end + + it "stops after specified times when given a block" do + (0..Float::INFINITY).lazy.grep(Integer) { |n| n > 3 ? n : false }.grep(Integer) { |n| n.even? ? n : false }.first(3).should == [4, false, 6] + + @eventsmixed.grep(BasicObject) {}.grep(Object) {}.first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/grep_v_spec.rb b/spec/ruby/core/enumerator/lazy/grep_v_spec.rb new file mode 100644 index 0000000000..123cbae58c --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/grep_v_spec.rb @@ -0,0 +1,86 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +ruby_version_is "2.3" do + describe "Enumerator::Lazy#grep_v" do + before(:each) do + @yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy + @eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy + ScratchPad.record [] + end + + after(:each) do + ScratchPad.clear + end + + it "requires an argument" do + Enumerator::Lazy.instance_method(:grep_v).arity.should == 1 + end + + it "returns a new instance of Enumerator::Lazy" do + ret = @yieldsmixed.grep_v(Object) {} + ret.should be_an_instance_of(Enumerator::Lazy) + ret.should_not equal(@yieldsmixed) + + ret = @yieldsmixed.grep_v(Object) + ret.should be_an_instance_of(Enumerator::Lazy) + ret.should_not equal(@yieldsmixed) + end + + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.grep_v(Object) {}.size.should == nil + Enumerator::Lazy.new(Object.new, 100) {}.grep_v(Object).size.should == nil + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times when not given a block" do + (0..Float::INFINITY).lazy.grep_v(3..5).first(3).should == [0, 1, 2] + + @eventsmixed.grep_v(Symbol).first(1) + ScratchPad.recorded.should == [:before_yield] + end + + it "stops after specified times when given a block" do + (0..Float::INFINITY).lazy.grep_v(4..8, &:succ).first(3).should == [1, 2, 3] + + @eventsmixed.grep_v(Symbol) {}.first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + + it "calls the block with a gathered array when yield with multiple arguments" do + yields = [] + @yieldsmixed.grep_v(Array) { |v| yields << v }.force + yields.should == EnumeratorLazySpecs::YieldsMixed.gathered_non_array_yields + + @yieldsmixed.grep_v(Array).force.should == yields + end + + describe "on a nested Lazy" do + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.grep_v(Object).grep_v(Object) {}.size.should == nil + Enumerator::Lazy.new(Object.new, 100) {}.grep_v(Object).grep_v(Object).size.should == nil + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times when not given a block" do + (0..Float::INFINITY).lazy.grep_v(3..5).grep_v(6..10).first(3).should == [0, 1, 2] + + @eventsmixed.grep_v(Symbol).grep_v(String).first(1) + ScratchPad.recorded.should == [:before_yield] + end + + it "stops after specified times when given a block" do + (0..Float::INFINITY).lazy + .grep_v(1..2) { |n| n > 3 ? n : false } + .grep_v(false) { |n| n.even? ? n : false } + .first(3) + .should == [4, false, 6] + + @eventsmixed.grep_v(Symbol) {}.grep_v(String) {}.first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/initialize_spec.rb b/spec/ruby/core/enumerator/lazy/initialize_spec.rb new file mode 100644 index 0000000000..47eeafb5cf --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/initialize_spec.rb @@ -0,0 +1,63 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "Enumerator::Lazy#initialize" do + before :each do + @receiver = receiver = Object.new + + def receiver.each + yield 0 + yield 1 + yield 2 + end + + @uninitialized = Enumerator::Lazy.allocate + end + + it "is a private method" do + Enumerator::Lazy.should have_private_instance_method(:initialize, false) + end + + it "returns self" do + @uninitialized.send(:initialize, @receiver) {}.should equal(@uninitialized) + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + @uninitialized.send(:initialize, @receiver) do |yielder, *values| + yielder.<<(*values) + end.first(2).should == [0, 1] + end + end + + it "sets #size to nil if not given a size" do + @uninitialized.send(:initialize, @receiver) {}.size.should be_nil + end + + it "sets #size to nil if given size is nil" do + @uninitialized.send(:initialize, @receiver, nil) {}.size.should be_nil + end + + it "sets given size to own size if the given size is Float::INFINITY" do + @uninitialized.send(:initialize, @receiver, Float::INFINITY) {}.size.should equal(Float::INFINITY) + end + + it "sets given size to own size if the given size is a Fixnum" do + @uninitialized.send(:initialize, @receiver, 100) {}.size.should == 100 + end + + it "sets given size to own size if the given size is a Proc" do + @uninitialized.send(:initialize, @receiver, lambda { 200 }) {}.size.should == 200 + end + + it "raises an ArgumentError when block is not given" do + lambda { @uninitialized.send :initialize, @receiver }.should raise_error(ArgumentError) + end + + describe "on frozen instance" do + it "raises a RuntimeError" do + lambda { @uninitialized.freeze.send(:initialize, @receiver) {} }.should raise_error(RuntimeError) + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/lazy_spec.rb b/spec/ruby/core/enumerator/lazy/lazy_spec.rb new file mode 100644 index 0000000000..a82a1af271 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/lazy_spec.rb @@ -0,0 +1,16 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "Enumerator::Lazy" do + it "is a subclass of Enumerator" do + Enumerator::Lazy.superclass.should equal(Enumerator) + end +end + +describe "Enumerator::Lazy#lazy" do + it "returns self" do + lazy = (1..3).to_enum.lazy + lazy.lazy.should equal(lazy) + end +end diff --git a/spec/ruby/core/enumerator/lazy/map_spec.rb b/spec/ruby/core/enumerator/lazy/map_spec.rb new file mode 100644 index 0000000000..8ff2573fe5 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/map_spec.rb @@ -0,0 +1,12 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/collect', __FILE__) + +describe "Enumerator::Lazy#map" do + it_behaves_like :enumerator_lazy_collect, :map + + it "doesn't unwrap Arrays" do + Enumerator.new {|y| y.yield([1])}.lazy.to_a.should == [[1]] + end +end diff --git a/spec/ruby/core/enumerator/lazy/reject_spec.rb b/spec/ruby/core/enumerator/lazy/reject_spec.rb new file mode 100644 index 0000000000..317e927f02 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/reject_spec.rb @@ -0,0 +1,60 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Enumerator::Lazy#reject" do + before :each do + @yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy + @eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy + ScratchPad.record [] + end + + after :each do + ScratchPad.clear + end + + it "returns a new instance of Enumerator::Lazy" do + ret = @yieldsmixed.reject {} + ret.should be_an_instance_of(Enumerator::Lazy) + ret.should_not equal(@yieldsmixed) + end + + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.reject {}.size.should == nil + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.reject(&:even?).first(3).should == [1, 3, 5] + + @eventsmixed.reject { false }.first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + + it "calls the block with a gathered array when yield with multiple arguments" do + yields = [] + @yieldsmixed.reject { |v| yields << v }.force + yields.should == EnumeratorLazySpecs::YieldsMixed.gathered_yields + end + + it "raises an ArgumentError when not given a block" do + lambda { @yieldsmixed.reject }.should raise_error(ArgumentError) + end + + describe "on a nested Lazy" do + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.take(20).reject {}.size.should == nil + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.reject { |n| n < 4 }.reject(&:even?).first(3).should == [5, 7, 9] + + @eventsmixed.reject { false }.reject { false }.first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/select_spec.rb b/spec/ruby/core/enumerator/lazy/select_spec.rb new file mode 100644 index 0000000000..ba5823c7ad --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/select_spec.rb @@ -0,0 +1,8 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/select', __FILE__) + +describe "Enumerator::Lazy#select" do + it_behaves_like :enumerator_lazy_select, :select +end diff --git a/spec/ruby/core/enumerator/lazy/shared/collect.rb b/spec/ruby/core/enumerator/lazy/shared/collect.rb new file mode 100644 index 0000000000..c892784bc2 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/shared/collect.rb @@ -0,0 +1,56 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe :enumerator_lazy_collect, shared: true do + before :each do + @yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy + @eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy + ScratchPad.record [] + end + + after :each do + ScratchPad.clear + end + + it "returns a new instance of Enumerator::Lazy" do + ret = @yieldsmixed.send(@method) {} + ret.should be_an_instance_of(Enumerator::Lazy) + ret.should_not equal(@yieldsmixed) + end + + it "keeps size" do + Enumerator::Lazy.new(Object.new, 100) {}.send(@method) {}.size.should == 100 + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.send(@method, &:succ).first(3).should == [1, 2, 3] + + @eventsmixed.send(@method) {}.first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + + it "calls the block with initial values when yield with multiple arguments" do + yields = [] + @yieldsmixed.send(@method) { |v| yields << v }.force + yields.should == EnumeratorLazySpecs::YieldsMixed.initial_yields + end + + describe "on a nested Lazy" do + it "keeps size" do + Enumerator::Lazy.new(Object.new, 100) {}.send(@method) {}.send(@method) {}.size.should == 100 + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.send(@method, &:succ).send(@method, &:succ).first(3).should == [2, 3, 4] + + @eventsmixed.send(@method) {}.send(@method) {}.first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/shared/collect_concat.rb b/spec/ruby/core/enumerator/lazy/shared/collect_concat.rb new file mode 100644 index 0000000000..69bc10c1a4 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/shared/collect_concat.rb @@ -0,0 +1,72 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe :enumerator_lazy_collect_concat, shared: true do + before :each do + @yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy + @eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy + ScratchPad.record [] + end + + after :each do + ScratchPad.clear + end + + it "returns a new instance of Enumerator::Lazy" do + ret = @yieldsmixed.send(@method) {} + ret.should be_an_instance_of(Enumerator::Lazy) + ret.should_not equal(@yieldsmixed) + end + + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.send(@method) { true }.size.should == nil + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.send(@method) { |n| (n * 10).to_s }.first(6).should == %w[0 10 20 30 40 50] + + @eventsmixed.send(@method) {}.first(1) + ScratchPad.recorded.should == [:before_yield] + end + + it "flattens elements when the given block returned an array or responding to .each and .force" do + (0..Float::INFINITY).lazy.send(@method) { |n| (n * 10).to_s.chars }.first(6).should == %w[0 1 0 2 0 3] + (0..Float::INFINITY).lazy.send(@method) { |n| (n * 10).to_s.each_char }.first(6).all? { |o| o.instance_of? Enumerator }.should be_true + (0..Float::INFINITY).lazy.send(@method) { |n| (n * 10).to_s.each_char.lazy }.first(6).should == %w[0 1 0 2 0 3] + end + end + + it "calls the block with initial values when yield with multiple arguments" do + yields = [] + @yieldsmixed.send(@method) { |v| yields << v }.force + yields.should == EnumeratorLazySpecs::YieldsMixed.initial_yields + end + + it "raises an ArgumentError when not given a block" do + lambda { @yieldsmixed.send(@method) }.should raise_error(ArgumentError) + end + + describe "on a nested Lazy" do + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.take(50) {}.send(@method) {}.size.should == nil + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.map {|n| n * 10 }.send(@method) { |n| n.to_s }.first(6).should == %w[0 10 20 30 40 50] + + @eventsmixed.send(@method) {}.send(@method) {}.first(1) + ScratchPad.recorded.should == [:before_yield] + end + + it "flattens elements when the given block returned an array or responding to .each and .force" do + (0..Float::INFINITY).lazy.map {|n| n * 10 }.send(@method) { |n| n.to_s.chars }.first(6).should == %w[0 1 0 2 0 3] + (0..Float::INFINITY).lazy.map {|n| n * 10 }.send(@method) { |n| n.to_s.each_char }.first(6).all? { |o| o.instance_of? Enumerator }.should be_true + (0..Float::INFINITY).lazy.map {|n| n * 10 }.send(@method) { |n| n.to_s.each_char.lazy }.first(6).should == %w[0 1 0 2 0 3] + end + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/shared/select.rb b/spec/ruby/core/enumerator/lazy/shared/select.rb new file mode 100644 index 0000000000..546256360e --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/shared/select.rb @@ -0,0 +1,60 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe :enumerator_lazy_select, shared: true do + before :each do + @yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy + @eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy + ScratchPad.record [] + end + + after :each do + ScratchPad.clear + end + + it "returns a new instance of Enumerator::Lazy" do + ret = @yieldsmixed.send(@method) {} + ret.should be_an_instance_of(Enumerator::Lazy) + ret.should_not equal(@yieldsmixed) + end + + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.send(@method) { true }.size.should == nil + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.send(@method, &:even?).first(3).should == [0, 2, 4] + + @eventsmixed.send(@method) { true }.first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + + it "calls the block with a gathered array when yield with multiple arguments" do + yields = [] + @yieldsmixed.send(@method) { |v| yields << v }.force + yields.should == EnumeratorLazySpecs::YieldsMixed.gathered_yields + end + + it "raises an ArgumentError when not given a block" do + lambda { @yieldsmixed.send(@method) }.should raise_error(ArgumentError) + end + + describe "on a nested Lazy" do + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.take(50) {}.send(@method) { true }.size.should == nil + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.send(@method) { |n| n > 5 }.send(@method, &:even?).first(3).should == [6, 8, 10] + + @eventsmixed.send(@method) { true }.send(@method) { true }.first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/shared/to_enum.rb b/spec/ruby/core/enumerator/lazy/shared/to_enum.rb new file mode 100644 index 0000000000..5e6935b45a --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/shared/to_enum.rb @@ -0,0 +1,50 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../../spec_helper', __FILE__) + +describe :enumerator_lazy_to_enum, shared: true do + before :each do + @infinite = (0..Float::INFINITY).lazy + end + + it "requires multiple arguments" do + Enumerator::Lazy.instance_method(@method).arity.should < 0 + end + + it "returns a new instance of Enumerator::Lazy" do + ret = @infinite.send @method + ret.should be_an_instance_of(Enumerator::Lazy) + ret.should_not equal(@infinite) + end + + it "sets #size to nil when not given a block" do + Enumerator::Lazy.new(Object.new, 100) {}.send(@method).size.should == nil + end + + it "sets given block to size when given a block" do + Enumerator::Lazy.new(Object.new, 100) {}.send(@method) { 30 }.size.should == 30 + end + + it "generates a lazy enumerator from the given name" do + @infinite.send(@method, :with_index, 10).first(3).should == [[0, 10], [1, 11], [2, 12]] + end + + it "passes given arguments to wrapped method" do + @infinite.send(@method, :each_slice, 2).map { |assoc| assoc.first * assoc.last }.first(4).should == [0, 6, 20, 42] + end + + it "used by some parent's methods though returning Lazy" do + { each_with_index: [], + with_index: [], + cycle: [1], + each_with_object: [Object.new], + with_object: [Object.new], + each_slice: [2], + each_entry: [], + each_cons: [2] + }.each_pair do |method, args| + @infinite.method(method).owner.should_not equal(Enumerator::Lazy) + @infinite.send(method, *args).should be_an_instance_of(Enumerator::Lazy) + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/take_spec.rb b/spec/ruby/core/enumerator/lazy/take_spec.rb new file mode 100644 index 0000000000..5ef732237d --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/take_spec.rb @@ -0,0 +1,66 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Enumerator::Lazy#take" do + before :each do + @yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy + @eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy + ScratchPad.record [] + end + + after :each do + ScratchPad.clear + end + + it "returns a new instance of Enumerator::Lazy" do + ret = @yieldsmixed.take(1) + ret.should be_an_instance_of(Enumerator::Lazy) + ret.should_not equal(@yieldsmixed) + end + + it "sets given count to size if the given count is less than old size" do + Enumerator::Lazy.new(Object.new, 100) {}.take(20).size.should == 20 + Enumerator::Lazy.new(Object.new, 100) {}.take(200).size.should == 100 + end + + it "sets given count to size if the old size is Infinity" do + loop.lazy.take(20).size.should == 20 + end + + describe "when the returned lazy enumerator is evaluated by .force" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.take(2).force.should == [0, 1] + + @eventsmixed.take(1).force + ScratchPad.recorded.should == [:before_yield] + end + + it "stops without iterations if the given argument is 0" do + @eventsmixed.take(0).force + ScratchPad.recorded.should == [] + end + end + + describe "on a nested Lazy" do + it "sets given count to size if the given count is less than old size" do + Enumerator::Lazy.new(Object.new, 100) {}.take(20).take(50).size.should == 20 + Enumerator::Lazy.new(Object.new, 100) {}.take(50).take(20).size.should == 20 + end + + describe "when the returned lazy enumerator is evaluated by .force" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.map(&:succ).take(2).force.should == [1, 2] + + @eventsmixed.take(10).take(1).force + ScratchPad.recorded.should == [:before_yield] + end + + it "stops without iterations if the given argument is 0" do + @eventsmixed.take(10).take(0).force + ScratchPad.recorded.should == [] + end + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/take_while_spec.rb b/spec/ruby/core/enumerator/lazy/take_while_spec.rb new file mode 100644 index 0000000000..8647dfcaf0 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/take_while_spec.rb @@ -0,0 +1,60 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Enumerator::Lazy#take_while" do + before :each do + @yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy + @eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy + ScratchPad.record [] + end + + after :each do + ScratchPad.clear + end + + it "returns a new instance of Enumerator::Lazy" do + ret = @yieldsmixed.take_while {} + ret.should be_an_instance_of(Enumerator::Lazy) + ret.should_not equal(@yieldsmixed) + end + + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.take_while { true }.size.should == nil + end + + describe "when the returned lazy enumerator is evaluated by .force" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.take_while { |n| n < 3 }.force.should == [0, 1, 2] + + @eventsmixed.take_while { false }.force + ScratchPad.recorded.should == [:before_yield] + end + end + + it "calls the block with initial values when yield with multiple arguments" do + yields = [] + @yieldsmixed.take_while { |v| yields << v; true }.force + yields.should == EnumeratorLazySpecs::YieldsMixed.initial_yields + end + + it "raises an ArgumentError when not given a block" do + lambda { @yieldsmixed.take_while }.should raise_error(ArgumentError) + end + + describe "on a nested Lazy" do + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.take(20).take_while { true }.size.should == nil + end + + describe "when the returned lazy enumerator is evaluated by .force" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.take_while { |n| n < 3 }.take_while(&:even?).force.should == [0] + + @eventsmixed.take_while { true }.take_while { false }.force + ScratchPad.recorded.should == [:before_yield] + end + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/to_enum_spec.rb b/spec/ruby/core/enumerator/lazy/to_enum_spec.rb new file mode 100644 index 0000000000..e0966037ab --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/to_enum_spec.rb @@ -0,0 +1,8 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/to_enum', __FILE__) + +describe "Enumerator::Lazy#to_enum" do + it_behaves_like :enumerator_lazy_to_enum, :to_enum +end diff --git a/spec/ruby/core/enumerator/lazy/uniq_spec.rb b/spec/ruby/core/enumerator/lazy/uniq_spec.rb new file mode 100644 index 0000000000..6220c7ba34 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/uniq_spec.rb @@ -0,0 +1,39 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +ruby_version_is '2.4' do + describe 'Enumerator::Lazy#uniq' do + context 'when yielded with an argument' do + before :each do + @lazy = [0, 1, 2, 3].to_enum.lazy.uniq(&:even?) + end + + it 'returns a lazy enumerator' do + @lazy.should be_an_instance_of(Enumerator::Lazy) + @lazy.force.should == [0, 1] + end + + it 'sets the size to nil' do + @lazy.size.should == nil + end + end + + context 'when yielded with multiple arguments' do + before :each do + enum = Object.new.to_enum + class << enum + def each + yield 0, 'foo' + yield 1, 'FOO' + yield 2, 'bar' + end + end + @lazy = enum.lazy + end + + it 'returns all yield arguments as an array' do + @lazy.uniq { |_, label| label.downcase }.force.should == [[0, 'foo'], [2, 'bar']] + end + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/zip_spec.rb b/spec/ruby/core/enumerator/lazy/zip_spec.rb new file mode 100644 index 0000000000..9c728364ce --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/zip_spec.rb @@ -0,0 +1,74 @@ +# -*- encoding: us-ascii -*- + +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Enumerator::Lazy#zip" do + before :each do + @yieldsmixed = EnumeratorLazySpecs::YieldsMixed.new.to_enum.lazy + @eventsmixed = EnumeratorLazySpecs::EventsMixed.new.to_enum.lazy + ScratchPad.record [] + end + + after :each do + ScratchPad.clear + end + + it "returns a new instance of Enumerator::Lazy" do + ret = @yieldsmixed.zip [] + ret.should be_an_instance_of(Enumerator::Lazy) + ret.should_not equal(@yieldsmixed) + end + + it "keeps size" do + Enumerator::Lazy.new(Object.new, 100) {}.zip([], []).size.should == 100 + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.zip([4, 5], [8]).first(2).should == [[0, 4, 8], [1, 5, nil]] + + @eventsmixed.zip([0, 1]).first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + + it "calls the block with a gathered array when yield with multiple arguments" do + yields = @yieldsmixed.zip(EnumeratorLazySpecs::YieldsMixed.new.to_enum).force + yields.should == [EnumeratorLazySpecs::YieldsMixed.gathered_yields, + EnumeratorLazySpecs::YieldsMixed.gathered_yields].transpose + end + + it "returns a Lazy when no arguments given" do + @yieldsmixed.zip.should be_an_instance_of(Enumerator::Lazy) + end + + it "raises a TypeError if arguments contain non-list object" do + lambda { @yieldsmixed.zip [], Object.new, [] }.should raise_error(TypeError) + end + + describe "on a nested Lazy" do + it "keeps size" do + Enumerator::Lazy.new(Object.new, 100) {}.map {}.zip([], []).size.should == 100 + end + + it "behaves as Enumerable#zip when given a block" do + lazy_yields = [] + lazy_ret = @yieldsmixed.zip(EnumeratorLazySpecs::YieldsMixed.new.to_enum) { |lists| lazy_yields << lists } + enum_yields = [] + enum_ret = EnumeratorLazySpecs::YieldsMixed.new.to_enum.zip(EnumeratorLazySpecs::YieldsMixed.new.to_enum) { |lists| enum_yields << lists } + + lazy_yields.should == enum_yields + lazy_ret.should == enum_ret + end + + describe "when the returned lazy enumerator is evaluated by Enumerable#first" do + it "stops after specified times" do + (0..Float::INFINITY).lazy.map(&:succ).zip([4, 5], [8]).first(2).should == [[1, 4, 8], [2, 5, nil]] + + @eventsmixed.zip([0, 1]).zip([0, 1]).first(1) + ScratchPad.recorded.should == [:before_yield] + end + end + end +end |