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/lazy/chunk_spec.rb20
-rw-r--r--spec/ruby/core/enumerator/lazy/lazy_spec.rb4
-rw-r--r--spec/ruby/core/enumerator/lazy/select_spec.rb39
-rw-r--r--spec/ruby/core/enumerator/lazy/uniq_spec.rb114
4 files changed, 98 insertions, 79 deletions
diff --git a/spec/ruby/core/enumerator/lazy/chunk_spec.rb b/spec/ruby/core/enumerator/lazy/chunk_spec.rb
index 3f60c6ea3f..87d2b0c206 100644
--- a/spec/ruby/core/enumerator/lazy/chunk_spec.rb
+++ b/spec/ruby/core/enumerator/lazy/chunk_spec.rb
@@ -25,22 +25,12 @@ describe "Enumerator::Lazy#chunk" do
Enumerator::Lazy.new(Object.new, 100) {}.chunk { |v| v }.size.should == nil
end
- ruby_version_is ""..."2.4" do
- it "raises an ArgumentError if called without a block" do
- lambda do
- @yieldsmixed.chunk
- end.should raise_error(ArgumentError)
- end
- end
+ it "returns an Enumerator if called without a block" do
+ chunk = @yieldsmixed.chunk
+ chunk.should be_an_instance_of(Enumerator::Lazy)
- ruby_version_is "2.4" do
- it "returns an Enumerator if called without a block" do
- chunk = @yieldsmixed.chunk
- chunk.should be_an_instance_of(Enumerator::Lazy)
-
- res = chunk.each { |v| true }.force
- res.should == [[true, EnumeratorLazySpecs::YieldsMixed.gathered_yields]]
- end
+ res = chunk.each { |v| true }.force
+ res.should == [[true, EnumeratorLazySpecs::YieldsMixed.gathered_yields]]
end
describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
diff --git a/spec/ruby/core/enumerator/lazy/lazy_spec.rb b/spec/ruby/core/enumerator/lazy/lazy_spec.rb
index 21fbfc27ab..cde9b31066 100644
--- a/spec/ruby/core/enumerator/lazy/lazy_spec.rb
+++ b/spec/ruby/core/enumerator/lazy/lazy_spec.rb
@@ -14,9 +14,7 @@ describe "Enumerator::Lazy" do
:select, :slice_after, :slice_before, :slice_when, :take, :take_while,
:to_enum, :zip
]
- ruby_version_is "2.4" do
- lazy_methods += [:chunk_while, :uniq]
- end
+ lazy_methods += [:chunk_while, :uniq]
Enumerator::Lazy.instance_methods(false).should include(*lazy_methods)
end
diff --git a/spec/ruby/core/enumerator/lazy/select_spec.rb b/spec/ruby/core/enumerator/lazy/select_spec.rb
index c4143c5251..3773d8f0a8 100644
--- a/spec/ruby/core/enumerator/lazy/select_spec.rb
+++ b/spec/ruby/core/enumerator/lazy/select_spec.rb
@@ -5,4 +5,43 @@ require_relative 'shared/select'
describe "Enumerator::Lazy#select" do
it_behaves_like :enumerator_lazy_select, :select
+
+ it "doesn't pre-evaluate the next element" do
+ eval_count = 0
+ enum = %w[Text1 Text2 Text3].lazy.select do
+ eval_count += 1
+ true
+ end
+
+ eval_count.should == 0
+ enum.next
+ eval_count.should == 1
+ end
+
+ it "doesn't over-evaluate when peeked" do
+ eval_count = 0
+ enum = %w[Text1 Text2 Text3].lazy.select do
+ eval_count += 1
+ true
+ end
+
+ eval_count.should == 0
+ enum.peek
+ enum.peek
+ eval_count.should == 1
+ end
+
+ it "doesn't re-evaluate after peek" do
+ eval_count = 0
+ enum = %w[Text1 Text2 Text3].lazy.select do
+ eval_count += 1
+ true
+ end
+
+ eval_count.should == 0
+ enum.peek
+ eval_count.should == 1
+ enum.next
+ eval_count.should == 1
+ end
end
diff --git a/spec/ruby/core/enumerator/lazy/uniq_spec.rb b/spec/ruby/core/enumerator/lazy/uniq_spec.rb
index d337d063d6..ce67ace5ab 100644
--- a/spec/ruby/core/enumerator/lazy/uniq_spec.rb
+++ b/spec/ruby/core/enumerator/lazy/uniq_spec.rb
@@ -1,82 +1,74 @@
require_relative '../../../spec_helper'
require_relative 'fixtures/classes'
-ruby_version_is '2.4' do
- describe 'Enumerator::Lazy#uniq' do
- context 'without block' do
- before :each do
- @lazy = [0, 1, 0, 1].to_enum.lazy.uniq
- end
-
- it 'returns a lazy enumerator' do
- @lazy.should be_an_instance_of(Enumerator::Lazy)
- @lazy.force.should == [0, 1]
- end
+describe 'Enumerator::Lazy#uniq' do
+ context 'without block' do
+ before :each do
+ @lazy = [0, 1, 0, 1].to_enum.lazy.uniq
+ end
- ruby_bug "#14495", "2.4"..."2.5.2" do
- it 'return same value after rewind' do
- @lazy.force.should == [0, 1]
- @lazy.force.should == [0, 1]
- end
- 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
+ it 'return same value after rewind' do
+ @lazy.force.should == [0, 1]
+ @lazy.force.should == [0, 1]
end
- context 'when yielded with an argument' do
- before :each do
- @lazy = [0, 1, 2, 3].to_enum.lazy.uniq(&:even?)
- end
+ it 'sets the size to nil' do
+ @lazy.size.should == nil
+ end
+ end
- it 'returns a lazy enumerator' do
- @lazy.should be_an_instance_of(Enumerator::Lazy)
- @lazy.force.should == [0, 1]
- end
+ context 'when yielded with an argument' do
+ before :each do
+ @lazy = [0, 1, 2, 3].to_enum.lazy.uniq(&:even?)
+ end
- ruby_bug "#14495", "2.4"..."2.5.2" do
- it 'return same value after rewind' do
- @lazy.force.should == [0, 1]
- @lazy.force.should == [0, 1]
- end
- 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
+ it 'return same value after rewind' do
+ @lazy.force.should == [0, 1]
+ @lazy.force.should == [0, 1]
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 'sets the size to nil' do
+ @lazy.size.should == nil
+ end
+ end
- ruby_bug "#14495", "2.4"..."2.5.2" do
- it 'return same value after rewind' do
- enum = @lazy.uniq { |_, label| label.downcase }
- enum.force.should == [[0, 'foo'], [2, 'bar']]
- enum.force.should == [[0, 'foo'], [2, 'bar']]
+ 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
+ it 'return same value after rewind' do
+ enum = @lazy.uniq { |_, label| label.downcase }
+ enum.force.should == [[0, 'foo'], [2, 'bar']]
+ enum.force.should == [[0, 'foo'], [2, 'bar']]
end
- it "works with an infinite enumerable" do
- s = 0..Float::INFINITY
- s.lazy.uniq.first(100).should ==
- s.first(100).uniq
+ it 'returns all yield arguments as an array' do
+ @lazy.uniq { |_, label| label.downcase }.force.should == [[0, 'foo'], [2, 'bar']]
end
end
+
+ it "works with an infinite enumerable" do
+ s = 0..Float::INFINITY
+ s.lazy.uniq.first(100).should ==
+ s.first(100).uniq
+ end
end