summaryrefslogtreecommitdiff
path: root/spec/ruby/core/enumerator
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/enumerator')
-rw-r--r--spec/ruby/core/enumerator/each_spec.rb15
-rw-r--r--spec/ruby/core/enumerator/each_with_object_spec.rb40
-rw-r--r--spec/ruby/core/enumerator/enum_for_spec.rb6
-rw-r--r--spec/ruby/core/enumerator/lazy/collect_concat_spec.rb8
-rw-r--r--spec/ruby/core/enumerator/lazy/collect_spec.rb8
-rw-r--r--spec/ruby/core/enumerator/lazy/enum_for_spec.rb8
-rw-r--r--spec/ruby/core/enumerator/lazy/filter_spec.rb6
-rw-r--r--spec/ruby/core/enumerator/lazy/find_all_spec.rb8
-rw-r--r--spec/ruby/core/enumerator/lazy/flat_map_spec.rb76
-rw-r--r--spec/ruby/core/enumerator/lazy/map_spec.rb60
-rw-r--r--spec/ruby/core/enumerator/lazy/select_spec.rb64
-rw-r--r--spec/ruby/core/enumerator/lazy/shared/collect.rb62
-rw-r--r--spec/ruby/core/enumerator/lazy/shared/collect_concat.rb78
-rw-r--r--spec/ruby/core/enumerator/lazy/shared/select.rb66
-rw-r--r--spec/ruby/core/enumerator/lazy/shared/to_enum.rb55
-rw-r--r--spec/ruby/core/enumerator/lazy/to_enum_spec.rb54
-rw-r--r--spec/ruby/core/enumerator/shared/each.rb46
-rw-r--r--spec/ruby/core/enumerator/shared/enum_for.rb57
-rw-r--r--spec/ruby/core/enumerator/shared/with_object.rb42
-rw-r--r--spec/ruby/core/enumerator/to_enum_spec.rb6
-rw-r--r--spec/ruby/core/enumerator/with_object_spec.rb5
21 files changed, 360 insertions, 410 deletions
diff --git a/spec/ruby/core/enumerator/each_spec.rb b/spec/ruby/core/enumerator/each_spec.rb
index 03be53fe05..64912b98b6 100644
--- a/spec/ruby/core/enumerator/each_spec.rb
+++ b/spec/ruby/core/enumerator/each_spec.rb
@@ -1,6 +1,21 @@
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
+
+ describe "passing source-yielded arguments to the block (lazy)" do
+ before :each do
+ @object = -> e { e.lazy }
+ 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)
diff --git a/spec/ruby/core/enumerator/each_with_object_spec.rb b/spec/ruby/core/enumerator/each_with_object_spec.rb
index 84a45ae89d..0e0a4496f4 100644
--- a/spec/ruby/core/enumerator/each_with_object_spec.rb
+++ b/spec/ruby/core/enumerator/each_with_object_spec.rb
@@ -1,6 +1,42 @@
require_relative '../../spec_helper'
-require_relative 'shared/with_object'
describe "Enumerator#each_with_object" do
- it_behaves_like :enum_with_object, :each_with_object
+ before :each do
+ @enum = [:a, :b].to_enum
+ @memo = ''
+ @block_params = @enum.each_with_object(@memo).to_a
+ end
+
+ it "receives an argument" do
+ @enum.method(:each_with_object).arity.should == 1
+ end
+
+ context "with block" do
+ it "returns the given object" do
+ ret = @enum.each_with_object(@memo) do |elm, memo|
+ # nothing
+ end
+ ret.should.equal?(@memo)
+ end
+
+ context "the block parameter" do
+ it "passes each element to first parameter" do
+ @block_params[0][0].should.equal?(:a)
+ @block_params[1][0].should.equal?(:b)
+ end
+
+ it "passes the given object to last parameter" do
+ @block_params[0][1].should.equal?(@memo)
+ @block_params[1][1].should.equal?(@memo)
+ end
+ end
+ end
+
+ context "without block" do
+ it "returns new Enumerator" do
+ ret = @enum.each_with_object(@memo)
+ ret.should.instance_of?(Enumerator)
+ ret.should_not.equal?(@enum)
+ end
+ end
end
diff --git a/spec/ruby/core/enumerator/enum_for_spec.rb b/spec/ruby/core/enumerator/enum_for_spec.rb
deleted file mode 100644
index fbdf98545a..0000000000
--- a/spec/ruby/core/enumerator/enum_for_spec.rb
+++ /dev/null
@@ -1,6 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'shared/enum_for'
-
-describe "Enumerator#enum_for" do
- it_behaves_like :enum_for, :enum_for
-end
diff --git a/spec/ruby/core/enumerator/lazy/collect_concat_spec.rb b/spec/ruby/core/enumerator/lazy/collect_concat_spec.rb
index 8765bb2190..d9fd576e43 100644
--- a/spec/ruby/core/enumerator/lazy/collect_concat_spec.rb
+++ b/spec/ruby/core/enumerator/lazy/collect_concat_spec.rb
@@ -1,8 +1,8 @@
-# -*- encoding: us-ascii -*-
-
require_relative '../../../spec_helper'
-require_relative 'shared/collect_concat'
describe "Enumerator::Lazy#collect_concat" do
- it_behaves_like :enumerator_lazy_collect_concat, :collect_concat
+ it "is an alias of Enumerator::Lazy#flat_map" do
+ Enumerator::Lazy.instance_method(:collect_concat).should ==
+ Enumerator::Lazy.instance_method(:flat_map)
+ end
end
diff --git a/spec/ruby/core/enumerator/lazy/collect_spec.rb b/spec/ruby/core/enumerator/lazy/collect_spec.rb
index 14b79ce16d..53a477e053 100644
--- a/spec/ruby/core/enumerator/lazy/collect_spec.rb
+++ b/spec/ruby/core/enumerator/lazy/collect_spec.rb
@@ -1,8 +1,8 @@
-# -*- encoding: us-ascii -*-
-
require_relative '../../../spec_helper'
-require_relative 'shared/collect'
describe "Enumerator::Lazy#collect" do
- it_behaves_like :enumerator_lazy_collect, :collect
+ it "is an alias of Enumerator::Lazy#map" do
+ Enumerator::Lazy.instance_method(:collect).should ==
+ Enumerator::Lazy.instance_method(:map)
+ end
end
diff --git a/spec/ruby/core/enumerator/lazy/enum_for_spec.rb b/spec/ruby/core/enumerator/lazy/enum_for_spec.rb
index 7e7783f6f1..b40c5d9915 100644
--- a/spec/ruby/core/enumerator/lazy/enum_for_spec.rb
+++ b/spec/ruby/core/enumerator/lazy/enum_for_spec.rb
@@ -1,8 +1,8 @@
-# -*- encoding: us-ascii -*-
-
require_relative '../../../spec_helper'
-require_relative 'shared/to_enum'
describe "Enumerator::Lazy#enum_for" do
- it_behaves_like :enumerator_lazy_to_enum, :enum_for
+ it "is an alias of Enumerator::Lazy#to_enum" do
+ Enumerator::Lazy.instance_method(:enum_for).should ==
+ Enumerator::Lazy.instance_method(:to_enum)
+ end
end
diff --git a/spec/ruby/core/enumerator/lazy/filter_spec.rb b/spec/ruby/core/enumerator/lazy/filter_spec.rb
index 43128241e0..3ca5376faa 100644
--- a/spec/ruby/core/enumerator/lazy/filter_spec.rb
+++ b/spec/ruby/core/enumerator/lazy/filter_spec.rb
@@ -1,6 +1,8 @@
require_relative '../../../spec_helper'
-require_relative 'shared/select'
describe "Enumerator::Lazy#filter" do
- it_behaves_like :enumerator_lazy_select, :filter
+ it "is an alias of Enumerator::Lazy#select" do
+ Enumerator::Lazy.instance_method(:filter).should ==
+ Enumerator::Lazy.instance_method(:select)
+ end
end
diff --git a/spec/ruby/core/enumerator/lazy/find_all_spec.rb b/spec/ruby/core/enumerator/lazy/find_all_spec.rb
index 8b05c53803..64930dc61b 100644
--- a/spec/ruby/core/enumerator/lazy/find_all_spec.rb
+++ b/spec/ruby/core/enumerator/lazy/find_all_spec.rb
@@ -1,8 +1,8 @@
-# -*- encoding: us-ascii -*-
-
require_relative '../../../spec_helper'
-require_relative 'shared/select'
describe "Enumerator::Lazy#find_all" do
- it_behaves_like :enumerator_lazy_select, :find_all
+ it "is an alias of Enumerator::Lazy#select" do
+ Enumerator::Lazy.instance_method(:find_all).should ==
+ Enumerator::Lazy.instance_method(:select)
+ end
end
diff --git a/spec/ruby/core/enumerator/lazy/flat_map_spec.rb b/spec/ruby/core/enumerator/lazy/flat_map_spec.rb
index 5dcaa8bfa1..609bf95b9e 100644
--- a/spec/ruby/core/enumerator/lazy/flat_map_spec.rb
+++ b/spec/ruby/core/enumerator/lazy/flat_map_spec.rb
@@ -1,10 +1,78 @@
-# -*- encoding: us-ascii -*-
-
require_relative '../../../spec_helper'
-require_relative 'shared/collect_concat'
+require_relative 'fixtures/classes'
describe "Enumerator::Lazy#flat_map" do
- it_behaves_like :enumerator_lazy_collect_concat, :flat_map
+ 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.flat_map {}
+ ret.should.instance_of?(Enumerator::Lazy)
+ ret.should_not.equal?(@yieldsmixed)
+ end
+
+ it "sets #size to nil" do
+ Enumerator::Lazy.new(Object.new, 100) {}.flat_map { 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.flat_map { |n| (n * 10).to_s }.first(6).should == %w[0 10 20 30 40 50]
+
+ @eventsmixed.flat_map {}.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.flat_map { |n| (n * 10).to_s.chars }.first(6).should == %w[0 1 0 2 0 3]
+ (0..Float::INFINITY).lazy.flat_map { |n| (n * 10).to_s.each_char }.first(6).all? { |o| o.instance_of? Enumerator }.should == true
+ (0..Float::INFINITY).lazy.flat_map { |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.flat_map { |v| yields << v }.force
+ yields.should == EnumeratorLazySpecs::YieldsMixed.initial_yields
+ end
+
+ it "raises an ArgumentError when not given a block" do
+ -> { @yieldsmixed.flat_map }.should.raise(ArgumentError)
+ end
+
+ describe "on a nested Lazy" do
+ it "sets #size to nil" do
+ Enumerator::Lazy.new(Object.new, 100) {}.take(50) {}.flat_map {}.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 }.flat_map { |n| n.to_s }.first(6).should == %w[0 10 20 30 40 50]
+
+ @eventsmixed.flat_map {}.flat_map {}.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 }.flat_map { |n| n.to_s.chars }.first(6).should == %w[0 1 0 2 0 3]
+ (0..Float::INFINITY).lazy.map {|n| n * 10 }.flat_map { |n| n.to_s.each_char }.first(6).all? { |o| o.instance_of? Enumerator }.should == true
+ (0..Float::INFINITY).lazy.map {|n| n * 10 }.flat_map { |n| n.to_s.each_char.lazy }.first(6).should == %w[0 1 0 2 0 3]
+ end
+ end
+ end
+
+ it "works with an infinite enumerable" do
+ s = 0..Float::INFINITY
+ s.lazy.flat_map { |n| [-n, +n] }.first(200).should ==
+ s.first(100).flat_map { |n| [-n, +n] }.to_a
+ end
it "properly unwraps nested yields" do
s = Enumerator.new do |y| loop do y << [1, 2] end end
diff --git a/spec/ruby/core/enumerator/lazy/map_spec.rb b/spec/ruby/core/enumerator/lazy/map_spec.rb
index 5cb998f5f7..2c7f8efab8 100644
--- a/spec/ruby/core/enumerator/lazy/map_spec.rb
+++ b/spec/ruby/core/enumerator/lazy/map_spec.rb
@@ -1,10 +1,62 @@
-# -*- encoding: us-ascii -*-
-
require_relative '../../../spec_helper'
-require_relative 'shared/collect'
+require_relative 'fixtures/classes'
describe "Enumerator::Lazy#map" do
- it_behaves_like :enumerator_lazy_collect, :map
+ 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.map {}
+ ret.should.instance_of?(Enumerator::Lazy)
+ ret.should_not.equal?(@yieldsmixed)
+ end
+
+ it "keeps size" do
+ Enumerator::Lazy.new(Object.new, 100) {}.map {}.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.map(&:succ).first(3).should == [1, 2, 3]
+
+ @eventsmixed.map {}.first(1)
+ ScratchPad.recorded.should == [:before_yield]
+ end
+ end
+
+ it "calls the block with initial values when yield with multiple arguments" do
+ yields = []
+ @yieldsmixed.map { |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) {}.map {}.map {}.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.map(&:succ).map(&:succ).first(3).should == [2, 3, 4]
+
+ @eventsmixed.map {}.map {}.first(1)
+ ScratchPad.recorded.should == [:before_yield]
+ end
+ end
+ end
+
+ it "works with an infinite enumerable" do
+ s = 0..Float::INFINITY
+ s.lazy.map { |n| n }.first(100).should ==
+ s.first(100).map { |n| n }.to_a
+ end
it "doesn't unwrap Arrays" do
Enumerator.new {|y| y.yield([1])}.lazy.to_a.should == [[1]]
diff --git a/spec/ruby/core/enumerator/lazy/select_spec.rb b/spec/ruby/core/enumerator/lazy/select_spec.rb
index 3773d8f0a8..29c8f1bd80 100644
--- a/spec/ruby/core/enumerator/lazy/select_spec.rb
+++ b/spec/ruby/core/enumerator/lazy/select_spec.rb
@@ -1,10 +1,66 @@
-# -*- encoding: us-ascii -*-
-
require_relative '../../../spec_helper'
-require_relative 'shared/select'
+require_relative 'fixtures/classes'
describe "Enumerator::Lazy#select" do
- it_behaves_like :enumerator_lazy_select, :select
+ 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.select {}
+ ret.should.instance_of?(Enumerator::Lazy)
+ ret.should_not.equal?(@yieldsmixed)
+ end
+
+ it "sets #size to nil" do
+ Enumerator::Lazy.new(Object.new, 100) {}.select { 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.select(&:even?).first(3).should == [0, 2, 4]
+
+ @eventsmixed.select { 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.select { |v| yields << v }.force
+ yields.should == EnumeratorLazySpecs::YieldsMixed.gathered_yields
+ end
+
+ it "raises an ArgumentError when not given a block" do
+ -> { @yieldsmixed.select }.should.raise(ArgumentError)
+ end
+
+ describe "on a nested Lazy" do
+ it "sets #size to nil" do
+ Enumerator::Lazy.new(Object.new, 100) {}.take(50) {}.select { 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.select { |n| n > 5 }.select(&:even?).first(3).should == [6, 8, 10]
+
+ @eventsmixed.select { true }.select { true }.first(1)
+ ScratchPad.recorded.should == [:before_yield]
+ end
+ end
+ end
+
+ it "works with an infinite enumerable" do
+ s = 0..Float::INFINITY
+ s.lazy.select { |n| true }.first(100).should ==
+ s.first(100).select { |n| true }
+ end
it "doesn't pre-evaluate the next element" do
eval_count = 0
diff --git a/spec/ruby/core/enumerator/lazy/shared/collect.rb b/spec/ruby/core/enumerator/lazy/shared/collect.rb
deleted file mode 100644
index 0ed04c8e02..0000000000
--- a/spec/ruby/core/enumerator/lazy/shared/collect.rb
+++ /dev/null
@@ -1,62 +0,0 @@
-# -*- encoding: us-ascii -*-
-
-require_relative '../../../../spec_helper'
-require_relative '../fixtures/classes'
-
-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.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
-
- it "works with an infinite enumerable" do
- s = 0..Float::INFINITY
- s.lazy.send(@method) { |n| n }.first(100).should ==
- s.first(100).send(@method) { |n| n }.to_a
- end
-end
diff --git a/spec/ruby/core/enumerator/lazy/shared/collect_concat.rb b/spec/ruby/core/enumerator/lazy/shared/collect_concat.rb
deleted file mode 100644
index 685e6d0594..0000000000
--- a/spec/ruby/core/enumerator/lazy/shared/collect_concat.rb
+++ /dev/null
@@ -1,78 +0,0 @@
-# -*- encoding: us-ascii -*-
-
-require_relative '../../../../spec_helper'
-require_relative '../fixtures/classes'
-
-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.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 == 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
- -> { @yieldsmixed.send(@method) }.should.raise(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 == 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
-
- it "works with an infinite enumerable" do
- s = 0..Float::INFINITY
- s.lazy.send(@method) { |n| [-n, +n] }.first(200).should ==
- s.first(100).send(@method) { |n| [-n, +n] }.to_a
- end
-end
diff --git a/spec/ruby/core/enumerator/lazy/shared/select.rb b/spec/ruby/core/enumerator/lazy/shared/select.rb
deleted file mode 100644
index 2d15176620..0000000000
--- a/spec/ruby/core/enumerator/lazy/shared/select.rb
+++ /dev/null
@@ -1,66 +0,0 @@
-# -*- encoding: us-ascii -*-
-
-require_relative '../../../../spec_helper'
-require_relative '../fixtures/classes'
-
-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.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
- -> { @yieldsmixed.send(@method) }.should.raise(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
-
- it "works with an infinite enumerable" do
- s = 0..Float::INFINITY
- s.lazy.send(@method) { |n| true }.first(100).should ==
- s.first(100).send(@method) { |n| true }
- end
-end
diff --git a/spec/ruby/core/enumerator/lazy/shared/to_enum.rb b/spec/ruby/core/enumerator/lazy/shared/to_enum.rb
deleted file mode 100644
index 75b9aefe8c..0000000000
--- a/spec/ruby/core/enumerator/lazy/shared/to_enum.rb
+++ /dev/null
@@ -1,55 +0,0 @@
-# -*- encoding: us-ascii -*-
-
-require_relative '../../../../spec_helper'
-
-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.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.send(method, *args).should.instance_of?(Enumerator::Lazy)
- end
- end
-
- it "works with an infinite enumerable" do
- s = 0..Float::INFINITY
- s.lazy.send(@method, :with_index).first(100).should ==
- s.first(100).to_enum.send(@method, :with_index).to_a
- end
-end
diff --git a/spec/ruby/core/enumerator/lazy/to_enum_spec.rb b/spec/ruby/core/enumerator/lazy/to_enum_spec.rb
index 210e5294b7..c0233d60fa 100644
--- a/spec/ruby/core/enumerator/lazy/to_enum_spec.rb
+++ b/spec/ruby/core/enumerator/lazy/to_enum_spec.rb
@@ -1,8 +1,54 @@
-# -*- encoding: us-ascii -*-
-
require_relative '../../../spec_helper'
-require_relative 'shared/to_enum'
+require_relative 'fixtures/classes'
describe "Enumerator::Lazy#to_enum" do
- it_behaves_like :enumerator_lazy_to_enum, :to_enum
+ before :each do
+ @infinite = (0..Float::INFINITY).lazy
+ end
+
+ it "requires multiple arguments" do
+ Enumerator::Lazy.instance_method(:to_enum).arity.should < 0
+ end
+
+ it "returns a new instance of Enumerator::Lazy" do
+ ret = @infinite.to_enum
+ ret.should.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) {}.to_enum.size.should == nil
+ end
+
+ it "sets given block to size when given a block" do
+ Enumerator::Lazy.new(Object.new, 100) {}.to_enum { 30 }.size.should == 30
+ end
+
+ it "generates a lazy enumerator from the given name" do
+ @infinite.to_enum(:with_index, 10).first(3).should == [[0, 10], [1, 11], [2, 12]]
+ end
+
+ it "passes given arguments to wrapped method" do
+ @infinite.to_enum(: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.send(method, *args).should.instance_of?(Enumerator::Lazy)
+ end
+ end
+
+ it "works with an infinite enumerable" do
+ s = 0..Float::INFINITY
+ s.lazy.to_enum(:with_index).first(100).should ==
+ s.first(100).to_enum.to_enum(:with_index).to_a
+ end
end
diff --git a/spec/ruby/core/enumerator/shared/each.rb b/spec/ruby/core/enumerator/shared/each.rb
new file mode 100644
index 0000000000..18ca773207
--- /dev/null
+++ b/spec/ruby/core/enumerator/shared/each.rb
@@ -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
diff --git a/spec/ruby/core/enumerator/shared/enum_for.rb b/spec/ruby/core/enumerator/shared/enum_for.rb
deleted file mode 100644
index 4388103ecf..0000000000
--- a/spec/ruby/core/enumerator/shared/enum_for.rb
+++ /dev/null
@@ -1,57 +0,0 @@
-describe :enum_for, shared: true do
- it "is defined in Kernel" do
- Kernel.method_defined?(@method).should == true
- end
-
- it "returns a new enumerator" do
- "abc".send(@method).should.instance_of?(Enumerator)
- end
-
- it "defaults the first argument to :each" do
- enum = [1,2].send(@method)
- enum.map { |v| v }.should == [1,2].each { |v| v }
- end
-
- it "sets regexp matches in the caller" do
- "wawa".send(@method, :scan, /./).map {|o| $& }.should == ["w", "a", "w", "a"]
- a = []
- "wawa".send(@method, :scan, /./).each {|o| a << $& }
- a.should == ["w", "a", "w", "a"]
- end
-
- it "exposes multi-arg yields as an array" do
- o = Object.new
- def o.each
- yield :a
- yield :b1, :b2
- yield [:c]
- yield :d1, :d2
- yield :e1, :e2, :e3
- end
-
- enum = o.send(@method)
- enum.next.should == :a
- enum.next.should == [:b1, :b2]
- enum.next.should == [:c]
- enum.next.should == [:d1, :d2]
- enum.next.should == [:e1, :e2, :e3]
- end
-
- it "uses the passed block's value to calculate the size of the enumerator" do
- Object.new.enum_for { 100 }.size.should == 100
- end
-
- it "defers the evaluation of the passed block until #size is called" do
- ScratchPad.record []
-
- enum = Object.new.enum_for do
- ScratchPad << :called
- 100
- end
-
- ScratchPad.recorded.should.empty?
-
- enum.size
- ScratchPad.recorded.should == [:called]
- end
-end
diff --git a/spec/ruby/core/enumerator/shared/with_object.rb b/spec/ruby/core/enumerator/shared/with_object.rb
deleted file mode 100644
index 50d4f24eb3..0000000000
--- a/spec/ruby/core/enumerator/shared/with_object.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-require_relative '../../../spec_helper'
-
-describe :enum_with_object, shared: true do
- before :each do
- @enum = [:a, :b].to_enum
- @memo = ''
- @block_params = @enum.send(@method, @memo).to_a
- end
-
- it "receives an argument" do
- @enum.method(@method).arity.should == 1
- end
-
- context "with block" do
- it "returns the given object" do
- ret = @enum.send(@method, @memo) do |elm, memo|
- # nothing
- end
- ret.should.equal?(@memo)
- end
-
- context "the block parameter" do
- it "passes each element to first parameter" do
- @block_params[0][0].should.equal?(:a)
- @block_params[1][0].should.equal?(:b)
- end
-
- it "passes the given object to last parameter" do
- @block_params[0][1].should.equal?(@memo)
- @block_params[1][1].should.equal?(@memo)
- end
- end
- end
-
- context "without block" do
- it "returns new Enumerator" do
- ret = @enum.send(@method, @memo)
- ret.should.instance_of?(Enumerator)
- ret.should_not.equal?(@enum)
- end
- end
-end
diff --git a/spec/ruby/core/enumerator/to_enum_spec.rb b/spec/ruby/core/enumerator/to_enum_spec.rb
deleted file mode 100644
index 7fb73d0c3c..0000000000
--- a/spec/ruby/core/enumerator/to_enum_spec.rb
+++ /dev/null
@@ -1,6 +0,0 @@
-require_relative '../../spec_helper'
-require_relative 'shared/enum_for'
-
-describe "Enumerator#to_enum" do
- it_behaves_like :enum_for, :to_enum
-end
diff --git a/spec/ruby/core/enumerator/with_object_spec.rb b/spec/ruby/core/enumerator/with_object_spec.rb
index 58031fd765..790be66a11 100644
--- a/spec/ruby/core/enumerator/with_object_spec.rb
+++ b/spec/ruby/core/enumerator/with_object_spec.rb
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'shared/with_object'
describe "Enumerator#with_object" do
- it_behaves_like :enum_with_object, :with_object
+ it "is an alias of Enumerator#each_with_object" do
+ Enumerator.instance_method(:with_object).should == Enumerator.instance_method(:each_with_object)
+ end
end