diff options
Diffstat (limited to 'spec/ruby/core/enumerator/lazy')
-rw-r--r-- | spec/ruby/core/enumerator/lazy/compact_spec.rb | 16 | ||||
-rw-r--r-- | spec/ruby/core/enumerator/lazy/eager_spec.rb | 38 | ||||
-rw-r--r-- | spec/ruby/core/enumerator/lazy/filter_map_spec.rb | 14 | ||||
-rw-r--r-- | spec/ruby/core/enumerator/lazy/initialize_spec.rb | 4 | ||||
-rw-r--r-- | spec/ruby/core/enumerator/lazy/lazy_spec.rb | 4 | ||||
-rw-r--r-- | spec/ruby/core/enumerator/lazy/with_index_spec.rb | 44 |
6 files changed, 71 insertions, 49 deletions
diff --git a/spec/ruby/core/enumerator/lazy/compact_spec.rb b/spec/ruby/core/enumerator/lazy/compact_spec.rb new file mode 100644 index 0000000000..e678bc71eb --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/compact_spec.rb @@ -0,0 +1,16 @@ +require_relative '../../../spec_helper' +require_relative 'fixtures/classes' + +ruby_version_is '3.1' do + describe "Enumerator::Lazy#compact" do + it 'returns array without nil elements' do + arr = [1, nil, 3, false, 5].to_enum.lazy.compact + arr.should be_an_instance_of(Enumerator::Lazy) + arr.force.should == [1, 3, false, 5] + end + + it "sets #size to nil" do + Enumerator::Lazy.new(Object.new, 100) {}.compact.size.should == nil + end + end +end diff --git a/spec/ruby/core/enumerator/lazy/eager_spec.rb b/spec/ruby/core/enumerator/lazy/eager_spec.rb index 30ba2dfe0e..592da4fd8c 100644 --- a/spec/ruby/core/enumerator/lazy/eager_spec.rb +++ b/spec/ruby/core/enumerator/lazy/eager_spec.rb @@ -1,29 +1,27 @@ require_relative '../../../spec_helper' -ruby_version_is "2.7" do - describe "Enumerator::Lazy#eager" do - it "returns a non-lazy Enumerator converted from the lazy enumerator" do - enum = [1, 2, 3].lazy +describe "Enumerator::Lazy#eager" do + it "returns a non-lazy Enumerator converted from the lazy enumerator" do + enum = [1, 2, 3].lazy - enum.class.should == Enumerator::Lazy - enum.eager.class.should == Enumerator - end + enum.class.should == Enumerator::Lazy + enum.eager.class.should == Enumerator + end - it "does not enumerate an enumerator" do - ScratchPad.record [] + it "does not enumerate an enumerator" do + ScratchPad.record [] - sequence = [1, 2, 3] - enum_lazy = Enumerator::Lazy.new(sequence) do |yielder, value| - yielder << value - ScratchPad << value - end + sequence = [1, 2, 3] + enum_lazy = Enumerator::Lazy.new(sequence) do |yielder, value| + yielder << value + ScratchPad << value + end - ScratchPad.recorded.should == [] - enum = enum_lazy.eager - ScratchPad.recorded.should == [] + ScratchPad.recorded.should == [] + enum = enum_lazy.eager + ScratchPad.recorded.should == [] - enum.map { |i| i }.should == [1, 2, 3] - ScratchPad.recorded.should == [1, 2, 3] - end + enum.map { |i| i }.should == [1, 2, 3] + ScratchPad.recorded.should == [1, 2, 3] end end diff --git a/spec/ruby/core/enumerator/lazy/filter_map_spec.rb b/spec/ruby/core/enumerator/lazy/filter_map_spec.rb index 3480af0865..99dd59cebe 100644 --- a/spec/ruby/core/enumerator/lazy/filter_map_spec.rb +++ b/spec/ruby/core/enumerator/lazy/filter_map_spec.rb @@ -3,14 +3,12 @@ require_relative '../../../spec_helper' require_relative 'fixtures/classes' -ruby_version_is "2.7" do - describe "Enumerator::Lazy#filter_map" do - it "maps only truthy results" do - (1..Float::INFINITY).lazy.filter_map { |i| i if i.odd? }.first(4).should == [1, 3, 5, 7] - end +describe "Enumerator::Lazy#filter_map" do + it "maps only truthy results" do + (1..Float::INFINITY).lazy.filter_map { |i| i if i.odd? }.first(4).should == [1, 3, 5, 7] + end - it "does not map false results" do - (1..Float::INFINITY).lazy.filter_map { |i| i.odd? ? i : false }.first(4).should == [1, 3, 5, 7] - end + it "does not map false results" do + (1..Float::INFINITY).lazy.filter_map { |i| i.odd? ? i : false }.first(4).should == [1, 3, 5, 7] end end diff --git a/spec/ruby/core/enumerator/lazy/initialize_spec.rb b/spec/ruby/core/enumerator/lazy/initialize_spec.rb index f23018d010..e1e0b1d608 100644 --- a/spec/ruby/core/enumerator/lazy/initialize_spec.rb +++ b/spec/ruby/core/enumerator/lazy/initialize_spec.rb @@ -56,8 +56,8 @@ describe "Enumerator::Lazy#initialize" do end describe "on frozen instance" do - it "raises a RuntimeError" do - -> { @uninitialized.freeze.send(:initialize, @receiver) {} }.should raise_error(RuntimeError) + it "raises a FrozenError" do + -> { @uninitialized.freeze.send(:initialize, @receiver) {} }.should raise_error(FrozenError) end end end diff --git a/spec/ruby/core/enumerator/lazy/lazy_spec.rb b/spec/ruby/core/enumerator/lazy/lazy_spec.rb index cde9b31066..0fb104e25a 100644 --- a/spec/ruby/core/enumerator/lazy/lazy_spec.rb +++ b/spec/ruby/core/enumerator/lazy/lazy_spec.rb @@ -16,6 +16,10 @@ describe "Enumerator::Lazy" do ] lazy_methods += [:chunk_while, :uniq] + ruby_version_is '3.1' do + lazy_methods += [:compact] + end + Enumerator::Lazy.instance_methods(false).should include(*lazy_methods) end end diff --git a/spec/ruby/core/enumerator/lazy/with_index_spec.rb b/spec/ruby/core/enumerator/lazy/with_index_spec.rb index 3a59ba4116..a6b5c38777 100644 --- a/spec/ruby/core/enumerator/lazy/with_index_spec.rb +++ b/spec/ruby/core/enumerator/lazy/with_index_spec.rb @@ -3,28 +3,34 @@ require_relative '../../../spec_helper' require_relative 'fixtures/classes' -ruby_version_is "2.7" do - describe "Enumerator::Lazy#with_index" do - it "enumerates with an index" do - (0..Float::INFINITY).lazy.with_index.map { |i, idx| [i, idx] }.first(3).should == [[0, 0], [1, 1], [2, 2]] - end +describe "Enumerator::Lazy#with_index" do + it "enumerates with an index" do + (0..Float::INFINITY).lazy.with_index.map { |i, idx| [i, idx] }.first(3).should == [[0, 0], [1, 1], [2, 2]] + end + + it "enumerates with an index starting at a given offset" do + (0..Float::INFINITY).lazy.with_index(3).map { |i, idx| [i, idx] }.first(3).should == [[0, 3], [1, 4], [2, 5]] + end - it "enumerates with an index starting at a given offset" do - (0..Float::INFINITY).lazy.with_index(3).map { |i, idx| [i, idx] }.first(3).should == [[0, 3], [1, 4], [2, 5]] - end + it "enumerates with an index starting at 0 when offset is nil" do + (0..Float::INFINITY).lazy.with_index(nil).map { |i, idx| [i, idx] }.first(3).should == [[0, 0], [1, 1], [2, 2]] + end - it "enumerates with an index starting at 0 when offset is nil" do - (0..Float::INFINITY).lazy.with_index(nil).map { |i, idx| [i, idx] }.first(3).should == [[0, 0], [1, 1], [2, 2]] - end + it "raises TypeError when offset does not convert to Integer" do + -> { (0..Float::INFINITY).lazy.with_index(false).map { |i, idx| i }.first(3) }.should raise_error(TypeError) + end - it "raises TypeError when offset does not convert to Integer" do - -> { (0..Float::INFINITY).lazy.with_index(false).map { |i, idx| i }.first(3) }.should raise_error(TypeError) - end + it "enumerates with a given block" do + result = [] + (0..Float::INFINITY).lazy.with_index { |i, idx| result << [i * 2, idx] }.first(3) + result.should == [[0,0],[2,1],[4,2]] + end - it "enumerates with a given block" do - result = [] - (0..Float::INFINITY).lazy.with_index { |i, idx| result << [i * 2, idx] }.first(3) - result.should == [[0,0],[2,1],[4,2]] - end + it "resets after a new call to each" do + enum = (0..2).lazy.with_index.map { |i, idx| [i, idx] } + result = [] + enum.each { |i, idx| result << [i, idx] } + enum.each { |i, idx| result << [i, idx] } + result.should == [[0,0], [1,1], [2,2], [0,0], [1,1], [2,2]] end end |