diff options
Diffstat (limited to 'spec/ruby/core/enumerator')
61 files changed, 740 insertions, 211 deletions
diff --git a/spec/ruby/core/enumerator/arithmetic_sequence/each_spec.rb b/spec/ruby/core/enumerator/arithmetic_sequence/each_spec.rb index d4fff3e01f..0a83019d49 100644 --- a/spec/ruby/core/enumerator/arithmetic_sequence/each_spec.rb +++ b/spec/ruby/core/enumerator/arithmetic_sequence/each_spec.rb @@ -12,6 +12,6 @@ describe "Enumerator::ArithmeticSequence#each" do end it "returns self" do - @seq.each { |item| }.should equal(@seq) + @seq.each { |item| }.should.equal?(@seq) end end diff --git a/spec/ruby/core/enumerator/arithmetic_sequence/hash_spec.rb b/spec/ruby/core/enumerator/arithmetic_sequence/hash_spec.rb index bdb308074b..a18c554fb3 100644 --- a/spec/ruby/core/enumerator/arithmetic_sequence/hash_spec.rb +++ b/spec/ruby/core/enumerator/arithmetic_sequence/hash_spec.rb @@ -2,7 +2,7 @@ require_relative '../../../spec_helper' describe "Enumerator::ArithmeticSequence#hash" do it "is based on begin, end, step and exclude_end?" do - 1.step(10).hash.should be_an_instance_of(Integer) + 1.step(10).hash.should.instance_of?(Integer) 1.step(10).hash.should == 1.step(10).hash 1.step(10, 5).hash.should == 1.step(10, 5).hash diff --git a/spec/ruby/core/enumerator/arithmetic_sequence/new_spec.rb b/spec/ruby/core/enumerator/arithmetic_sequence/new_spec.rb index 2015983826..1bd2f7f0f7 100644 --- a/spec/ruby/core/enumerator/arithmetic_sequence/new_spec.rb +++ b/spec/ruby/core/enumerator/arithmetic_sequence/new_spec.rb @@ -4,7 +4,7 @@ describe "Enumerator::ArithmeticSequence.new" do it "is not defined" do -> { Enumerator::ArithmeticSequence.new - }.should raise_error(NoMethodError) + }.should.raise(NoMethodError) end end @@ -12,6 +12,6 @@ describe "Enumerator::ArithmeticSequence.allocate" do it "is not defined" do -> { Enumerator::ArithmeticSequence.allocate - }.should raise_error(TypeError, 'allocator undefined for Enumerator::ArithmeticSequence') + }.should.raise(TypeError, 'allocator undefined for Enumerator::ArithmeticSequence') end end diff --git a/spec/ruby/core/enumerator/chain/initialize_spec.rb b/spec/ruby/core/enumerator/chain/initialize_spec.rb index 69484dfcb4..1df1dec5f8 100644 --- a/spec/ruby/core/enumerator/chain/initialize_spec.rb +++ b/spec/ruby/core/enumerator/chain/initialize_spec.rb @@ -6,26 +6,26 @@ describe "Enumerator::Chain#initialize" do end it "is a private method" do - Enumerator::Chain.should have_private_instance_method(:initialize, false) + Enumerator::Chain.private_instance_methods(false).should.include?(:initialize) end it "returns self" do - @uninitialized.send(:initialize).should equal(@uninitialized) + @uninitialized.send(:initialize).should.equal?(@uninitialized) end it "accepts many arguments" do - @uninitialized.send(:initialize, 0..1, 2..3, 4..5).should equal(@uninitialized) + @uninitialized.send(:initialize, 0..1, 2..3, 4..5).should.equal?(@uninitialized) end it "accepts arguments that are not Enumerable nor responding to :each" do - @uninitialized.send(:initialize, Object.new).should equal(@uninitialized) + @uninitialized.send(:initialize, Object.new).should.equal?(@uninitialized) end describe "on frozen instance" do - it "raises a RuntimeError" do + it "raises a FrozenError" do -> { @uninitialized.freeze.send(:initialize) - }.should raise_error(RuntimeError) + }.should.raise(FrozenError) end end end diff --git a/spec/ruby/core/enumerator/chain/inspect_spec.rb b/spec/ruby/core/enumerator/chain/inspect_spec.rb index a0450c808a..9b5a442b75 100644 --- a/spec/ruby/core/enumerator/chain/inspect_spec.rb +++ b/spec/ruby/core/enumerator/chain/inspect_spec.rb @@ -11,4 +11,8 @@ describe "Enumerator::Chain#inspect" do obj.should_receive(:inspect).and_return('some desc') Enumerator::Chain.new(obj).inspect.should == "#<Enumerator::Chain: [some desc]>" end + + it "returns a not initialized representation if #initialized is not called yet" do + Enumerator::Chain.allocate.inspect.should == "#<Enumerator::Chain: uninitialized>" + end end diff --git a/spec/ruby/core/enumerator/chain/rewind_spec.rb b/spec/ruby/core/enumerator/chain/rewind_spec.rb index 5f51ce2cf1..4525b82f7b 100644 --- a/spec/ruby/core/enumerator/chain/rewind_spec.rb +++ b/spec/ruby/core/enumerator/chain/rewind_spec.rb @@ -10,7 +10,7 @@ describe "Enumerator::Chain#rewind" do end it "returns self" do - @enum.rewind.should equal @enum + @enum.rewind.should.equal? @enum end it "does nothing if receiver has not been iterated" do @@ -35,7 +35,7 @@ describe "Enumerator::Chain#rewind" do @obj.should_not_receive(:rewind) @second.should_receive(:rewind).and_raise(RuntimeError) @enum.each {} - -> { @enum.rewind }.should raise_error(RuntimeError) + -> { @enum.rewind }.should.raise(RuntimeError) end it "calls rewind only for objects that have actually been iterated on" do @@ -45,7 +45,7 @@ describe "Enumerator::Chain#rewind" do @obj.should_receive(:rewind) @second.should_not_receive(:rewind) - -> { @enum.each {} }.should raise_error(RuntimeError) + -> { @enum.each {} }.should.raise(RuntimeError) @enum.rewind end end diff --git a/spec/ruby/core/enumerator/each_spec.rb b/spec/ruby/core/enumerator/each_spec.rb index 99ac3120af..03be53fe05 100644 --- a/spec/ruby/core/enumerator/each_spec.rb +++ b/spec/ruby/core/enumerator/each_spec.rb @@ -10,58 +10,58 @@ describe "Enumerator#each" do @enum_with_arguments = object_each_with_arguments.to_enum(:each_with_arguments, :arg0, :arg1, :arg2) - @enum_with_yielder = Enumerator.new {|y| y.yield :ok} + @enum_with_yielder = Enumerator.new { |y| y.yield :ok } end it "yields each element of self to the given block" do acc = [] - [1,2,3].to_enum.each {|e| acc << e } - acc.should == [1,2,3] + [1, 2, 3].to_enum.each { |e| acc << e } + acc.should == [1, 2, 3] end it "calls #each on the object given in the constructor by default" do each = mock('each') each.should_receive(:each) - each.to_enum.each {|e| e } + each.to_enum.each { |e| e } end it "calls #each on the underlying object until it's exhausted" do each = mock('each') each.should_receive(:each).and_yield(1).and_yield(2).and_yield(3) acc = [] - each.to_enum.each {|e| acc << e } - acc.should == [1,2,3] + each.to_enum.each { |e| acc << e } + acc.should == [1, 2, 3] end it "calls the method given in the constructor instead of #each" do each = mock('peach') each.should_receive(:peach) - each.to_enum(:peach).each {|e| e } + each.to_enum(:peach).each { |e| e } end it "calls the method given in the constructor until it's exhausted" do each = mock('peach') each.should_receive(:peach).and_yield(1).and_yield(2).and_yield(3) acc = [] - each.to_enum(:peach).each {|e| acc << e } - acc.should == [1,2,3] + each.to_enum(:peach).each { |e| acc << e } + acc.should == [1, 2, 3] end it "raises a NoMethodError if the object doesn't respond to #each" do enum = Object.new.to_enum -> do enum.each { |e| e } - end.should raise_error(NoMethodError) + end.should.raise(NoMethodError) end it "returns self if not given arguments and not given a block" do - @enum_with_arguments.each.should equal(@enum_with_arguments) + @enum_with_arguments.each.should.equal?(@enum_with_arguments) - @enum_with_yielder.each.should equal(@enum_with_yielder) + @enum_with_yielder.each.should.equal?(@enum_with_yielder) end it "returns the same value from receiver.each if block is given" do - @enum_with_arguments.each {}.should equal(:method_returned) + @enum_with_arguments.each {}.should.equal?(:method_returned) end it "passes given arguments at initialized to receiver.each" do @@ -78,12 +78,27 @@ describe "Enumerator#each" do end it "returns the same value from receiver.each if block and arguments are given" do - @enum_with_arguments.each(:each1, :each2) {}.should equal(:method_returned) + @enum_with_arguments.each(:each1, :each2) {}.should.equal?(:method_returned) end it "returns new Enumerator if given arguments but not given a block" do ret = @enum_with_arguments.each 1 - ret.should be_an_instance_of(Enumerator) - ret.should_not equal(@enum_with_arguments) + ret.should.instance_of?(Enumerator) + ret.should_not.equal?(@enum_with_arguments) + end + + it "does not destructure yielded array values when chaining each.map" do + result = [[[1]]].each.map { |a, b| [a, b] } + result.should == [[[1], nil]] + end + + it "preserves array values yielded from the enumerator" do + result = [[1, 2]].each.map { |a| a } + result.should == [[1, 2]] + end + + it "allows destructuring to occur in the block, not the enumerator" do + result = [[1, 2]].each.map { |a, b| a } + result.should == [1] end end diff --git a/spec/ruby/core/enumerator/each_with_index_spec.rb b/spec/ruby/core/enumerator/each_with_index_spec.rb index 96e53a2804..0630b7045e 100644 --- a/spec/ruby/core/enumerator/each_with_index_spec.rb +++ b/spec/ruby/core/enumerator/each_with_index_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/enumerator/with_index' +require_relative 'shared/with_index' require_relative '../enumerable/shared/enumeratorized' describe "Enumerator#each_with_index" do @@ -9,28 +9,28 @@ describe "Enumerator#each_with_index" do it "returns a new Enumerator when no block is given" do enum1 = [1,2,3].select enum2 = enum1.each_with_index - enum2.should be_an_instance_of(Enumerator) + enum2.should.instance_of?(Enumerator) enum1.should_not == enum2 end it "raises an ArgumentError if passed extra arguments" do -> do [1].to_enum.each_with_index(:glark) - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) end it "passes on the given block's return value" do arr = [1,2,3] - arr.delete_if.with_index { |a,b| false } + arr.delete_if.each_with_index { |a,b| false } arr.should == [1,2,3] end it "returns the iterator's return value" do - [1,2,3].select.with_index { |a,b| false }.should == [] + [1,2,3].select.each_with_index { |a,b| false }.should == [] + [1,2,3].select.each_with_index { |a,b| true }.should == [1,2,3] end it "returns the correct value if chained with itself" do [:a].each_with_index.each_with_index.to_a.should == [[[:a,0],0]] - [:a].each.with_index.with_index.to_a.should == [[[:a,0],0]] end end diff --git a/spec/ruby/core/enumerator/each_with_object_spec.rb b/spec/ruby/core/enumerator/each_with_object_spec.rb index 68524dc74a..84a45ae89d 100644 --- a/spec/ruby/core/enumerator/each_with_object_spec.rb +++ b/spec/ruby/core/enumerator/each_with_object_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/enumerator/with_object' +require_relative 'shared/with_object' describe "Enumerator#each_with_object" do it_behaves_like :enum_with_object, :each_with_object diff --git a/spec/ruby/core/enumerator/enum_for_spec.rb b/spec/ruby/core/enumerator/enum_for_spec.rb index fd33f463bf..fbdf98545a 100644 --- a/spec/ruby/core/enumerator/enum_for_spec.rb +++ b/spec/ruby/core/enumerator/enum_for_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/enumerator/enum_for' +require_relative 'shared/enum_for' describe "Enumerator#enum_for" do it_behaves_like :enum_for, :enum_for diff --git a/spec/ruby/core/enumerator/feed_spec.rb b/spec/ruby/core/enumerator/feed_spec.rb index e387c6cd39..781947a8c7 100644 --- a/spec/ruby/core/enumerator/feed_spec.rb +++ b/spec/ruby/core/enumerator/feed_spec.rb @@ -33,20 +33,20 @@ describe "Enumerator#feed" do end it "returns nil" do - @enum.feed(:a).should be_nil + @enum.feed(:a).should == nil end it "raises a TypeError if called more than once without advancing the enumerator" do @enum.feed :a @enum.next - -> { @enum.feed :b }.should raise_error(TypeError) + -> { @enum.feed :b }.should.raise(TypeError) end it "sets the return value of Yielder#yield" do enum = Enumerator.new { |y| ScratchPad << y.yield } enum.next enum.feed :a - -> { enum.next }.should raise_error(StopIteration) + -> { enum.next }.should.raise(StopIteration) ScratchPad.recorded.should == [:a] end end diff --git a/spec/ruby/core/enumerator/fixtures/classes.rb b/spec/ruby/core/enumerator/fixtures/classes.rb new file mode 100644 index 0000000000..6f285b8efa --- /dev/null +++ b/spec/ruby/core/enumerator/fixtures/classes.rb @@ -0,0 +1,15 @@ +module EnumSpecs + class Numerous + + include Enumerable + + def initialize(*list) + @list = list.empty? ? [2, 5, 3, 6, 1, 4] : list + end + + def each + @list.each { |i| yield i } + end + end + +end diff --git a/spec/ruby/core/enumerator/generator/each_spec.rb b/spec/ruby/core/enumerator/generator/each_spec.rb index a43805dd16..41a494298b 100644 --- a/spec/ruby/core/enumerator/generator/each_spec.rb +++ b/spec/ruby/core/enumerator/generator/each_spec.rb @@ -10,7 +10,7 @@ describe "Enumerator::Generator#each" do end it "is an enumerable" do - @generator.should be_kind_of(Enumerable) + @generator.should.is_a?(Enumerable) end it "supports enumeration with a block" do @@ -21,11 +21,11 @@ describe "Enumerator::Generator#each" do end it "raises a LocalJumpError if no block given" do - -> { @generator.each }.should raise_error(LocalJumpError) + -> { @generator.each }.should.raise(LocalJumpError) end it "returns the block returned value" do - @generator.each {}.should equal(:block_returned) + @generator.each {}.should.equal?(:block_returned) end it "requires multiple arguments" do diff --git a/spec/ruby/core/enumerator/generator/initialize_spec.rb b/spec/ruby/core/enumerator/generator/initialize_spec.rb index f75c7d6f26..0f77d591d9 100644 --- a/spec/ruby/core/enumerator/generator/initialize_spec.rb +++ b/spec/ruby/core/enumerator/generator/initialize_spec.rb @@ -9,18 +9,18 @@ describe "Enumerator::Generator#initialize" do end it "is a private method" do - @class.should have_private_instance_method(:initialize, false) + @class.private_instance_methods(false).should.include?(:initialize) end it "returns self when given a block" do - @uninitialized.send(:initialize) {}.should equal(@uninitialized) + @uninitialized.send(:initialize) {}.should.equal?(@uninitialized) end describe "on frozen instance" do - it "raises a RuntimeError" do + it "raises a FrozenError" do -> { @uninitialized.freeze.send(:initialize) {} - }.should raise_error(RuntimeError) + }.should.raise(FrozenError) end end end diff --git a/spec/ruby/core/enumerator/initialize_spec.rb b/spec/ruby/core/enumerator/initialize_spec.rb index 217af1d3bc..9929494b5a 100644 --- a/spec/ruby/core/enumerator/initialize_spec.rb +++ b/spec/ruby/core/enumerator/initialize_spec.rb @@ -8,19 +8,11 @@ describe "Enumerator#initialize" do end it "is a private method" do - Enumerator.should have_private_instance_method(:initialize, false) - end - - ruby_version_is ''...'3.0' do - it "returns self when given an object" do - suppress_warning do - @uninitialized.send(:initialize, Object.new).should equal(@uninitialized) - end - end + Enumerator.private_instance_methods(false).should.include?(:initialize) end it "returns self when given a block" do - @uninitialized.send(:initialize) {}.should equal(@uninitialized) + @uninitialized.send(:initialize) {}.should.equal?(@uninitialized) end # Maybe spec should be broken up? @@ -29,22 +21,22 @@ describe "Enumerator#initialize" do r = yielder.yield 3 yielder << r << 2 << 1 end - @uninitialized.should be_an_instance_of(Enumerator) + @uninitialized.should.instance_of?(Enumerator) r = [] @uninitialized.each{|x| r << x; x * 2} r.should == [3, 6, 2, 1] end it "sets size to nil if size is not given" do - @uninitialized.send(:initialize) {}.size.should be_nil + @uninitialized.send(:initialize) {}.size.should == nil end it "sets size to nil if the given size is nil" do - @uninitialized.send(:initialize, nil) {}.size.should be_nil + @uninitialized.send(:initialize, nil) {}.size.should == nil end it "sets size to the given size if the given size is Float::INFINITY" do - @uninitialized.send(:initialize, Float::INFINITY) {}.size.should equal(Float::INFINITY) + @uninitialized.send(:initialize, Float::INFINITY) {}.size.should.equal?(Float::INFINITY) end it "sets size to the given size if the given size is an Integer" do @@ -56,10 +48,10 @@ describe "Enumerator#initialize" do end describe "on frozen instance" do - it "raises a RuntimeError" do + it "raises a FrozenError" do -> { @uninitialized.freeze.send(:initialize) {} - }.should raise_error(RuntimeError) + }.should.raise(FrozenError) end end end diff --git a/spec/ruby/core/enumerator/inspect_spec.rb b/spec/ruby/core/enumerator/inspect_spec.rb index 3bcf07e754..7e97864246 100644 --- a/spec/ruby/core/enumerator/inspect_spec.rb +++ b/spec/ruby/core/enumerator/inspect_spec.rb @@ -14,4 +14,9 @@ describe "Enumerator#inspect" do (1..3).each.each_slice(2).inspect.should == "#<Enumerator: #<Enumerator: 1..3:each>:each_slice(2)>" end end + + it "returns a not initialized representation if #initialized is not called yet" do + Enumerator.allocate.inspect.should == "#<Enumerator: uninitialized>" + Enumerator::Lazy.allocate.inspect.should == "#<Enumerator::Lazy: uninitialized>" + end end diff --git a/spec/ruby/core/enumerator/lazy/chunk_spec.rb b/spec/ruby/core/enumerator/lazy/chunk_spec.rb index 87d2b0c206..d0179d32b6 100644 --- a/spec/ruby/core/enumerator/lazy/chunk_spec.rb +++ b/spec/ruby/core/enumerator/lazy/chunk_spec.rb @@ -17,8 +17,8 @@ describe "Enumerator::Lazy#chunk" do it "returns a new instance of Enumerator::Lazy" do ret = @yieldsmixed.chunk {} - ret.should be_an_instance_of(Enumerator::Lazy) - ret.should_not equal(@yieldsmixed) + ret.should.instance_of?(Enumerator::Lazy) + ret.should_not.equal?(@yieldsmixed) end it "sets #size to nil" do @@ -27,7 +27,7 @@ describe "Enumerator::Lazy#chunk" do it "returns an Enumerator if called without a block" do chunk = @yieldsmixed.chunk - chunk.should be_an_instance_of(Enumerator::Lazy) + chunk.should.instance_of?(Enumerator::Lazy) res = chunk.each { |v| true }.force res.should == [[true, EnumeratorLazySpecs::YieldsMixed.gathered_yields]] diff --git a/spec/ruby/core/enumerator/lazy/chunk_while_spec.rb b/spec/ruby/core/enumerator/lazy/chunk_while_spec.rb index 772bd42de9..edba8e1363 100644 --- a/spec/ruby/core/enumerator/lazy/chunk_while_spec.rb +++ b/spec/ruby/core/enumerator/lazy/chunk_while_spec.rb @@ -9,6 +9,6 @@ describe "Enumerator::Lazy#chunk_while" do it "should return a lazy enumerator" do s = 0..Float::INFINITY - s.lazy.chunk_while { |a, b| false }.should be_kind_of(Enumerator::Lazy) + s.lazy.chunk_while { |a, b| false }.should.is_a?(Enumerator::Lazy) end end 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..7305e1c9c4 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/compact_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../../spec_helper' +require_relative 'fixtures/classes' + +describe "Enumerator::Lazy#compact" do + it 'returns array without nil elements' do + arr = [1, nil, 3, false, 5].to_enum.lazy.compact + arr.should.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 diff --git a/spec/ruby/core/enumerator/lazy/drop_spec.rb b/spec/ruby/core/enumerator/lazy/drop_spec.rb index 822b8034fb..95ac7e9ecc 100644 --- a/spec/ruby/core/enumerator/lazy/drop_spec.rb +++ b/spec/ruby/core/enumerator/lazy/drop_spec.rb @@ -16,8 +16,8 @@ describe "Enumerator::Lazy#drop" do 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) + ret.should.instance_of?(Enumerator::Lazy) + ret.should_not.equal?(@yieldsmixed) end it "sets difference of given count with old size to new size" do diff --git a/spec/ruby/core/enumerator/lazy/drop_while_spec.rb b/spec/ruby/core/enumerator/lazy/drop_while_spec.rb index 4f6e366f88..65f3007dec 100644 --- a/spec/ruby/core/enumerator/lazy/drop_while_spec.rb +++ b/spec/ruby/core/enumerator/lazy/drop_while_spec.rb @@ -16,8 +16,8 @@ describe "Enumerator::Lazy#drop_while" do 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) + ret.should.instance_of?(Enumerator::Lazy) + ret.should_not.equal?(@yieldsmixed) end it "sets #size to nil" do @@ -40,7 +40,7 @@ describe "Enumerator::Lazy#drop_while" do end it "raises an ArgumentError when not given a block" do - -> { @yieldsmixed.drop_while }.should raise_error(ArgumentError) + -> { @yieldsmixed.drop_while }.should.raise(ArgumentError) end describe "on a nested Lazy" do diff --git a/spec/ruby/core/enumerator/lazy/grep_spec.rb b/spec/ruby/core/enumerator/lazy/grep_spec.rb index e67686c9a3..383f80a918 100644 --- a/spec/ruby/core/enumerator/lazy/grep_spec.rb +++ b/spec/ruby/core/enumerator/lazy/grep_spec.rb @@ -20,12 +20,12 @@ describe "Enumerator::Lazy#grep" do 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.should.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) + ret.should.instance_of?(Enumerator::Lazy) + ret.should_not.equal?(@yieldsmixed) end it "sets #size to nil" do diff --git a/spec/ruby/core/enumerator/lazy/grep_v_spec.rb b/spec/ruby/core/enumerator/lazy/grep_v_spec.rb index 67173021bb..19c917f254 100644 --- a/spec/ruby/core/enumerator/lazy/grep_v_spec.rb +++ b/spec/ruby/core/enumerator/lazy/grep_v_spec.rb @@ -18,12 +18,12 @@ describe "Enumerator::Lazy#grep_v" do 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.should.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) + ret.should.instance_of?(Enumerator::Lazy) + ret.should_not.equal?(@yieldsmixed) end it "sets #size to nil" do diff --git a/spec/ruby/core/enumerator/lazy/initialize_spec.rb b/spec/ruby/core/enumerator/lazy/initialize_spec.rb index f23018d010..47765d32c3 100644 --- a/spec/ruby/core/enumerator/lazy/initialize_spec.rb +++ b/spec/ruby/core/enumerator/lazy/initialize_spec.rb @@ -16,11 +16,11 @@ describe "Enumerator::Lazy#initialize" do end it "is a private method" do - Enumerator::Lazy.should have_private_instance_method(:initialize, false) + Enumerator::Lazy.private_instance_methods(false).should.include?(:initialize) end it "returns self" do - @uninitialized.send(:initialize, @receiver) {}.should equal(@uninitialized) + @uninitialized.send(:initialize, @receiver) {}.should.equal?(@uninitialized) end describe "when the returned lazy enumerator is evaluated by Enumerable#first" do @@ -32,15 +32,15 @@ describe "Enumerator::Lazy#initialize" do end it "sets #size to nil if not given a size" do - @uninitialized.send(:initialize, @receiver) {}.size.should be_nil + @uninitialized.send(:initialize, @receiver) {}.size.should == nil end it "sets #size to nil if given size is nil" do - @uninitialized.send(:initialize, @receiver, nil) {}.size.should be_nil + @uninitialized.send(:initialize, @receiver, nil) {}.size.should == 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) + @uninitialized.send(:initialize, @receiver, Float::INFINITY) {}.size.should.equal?(Float::INFINITY) end it "sets given size to own size if the given size is an Integer" do @@ -52,12 +52,12 @@ describe "Enumerator::Lazy#initialize" do end it "raises an ArgumentError when block is not given" do - -> { @uninitialized.send :initialize, @receiver }.should raise_error(ArgumentError) + -> { @uninitialized.send :initialize, @receiver }.should.raise(ArgumentError) 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(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 683dfb81d7..12107383d6 100644 --- a/spec/ruby/core/enumerator/lazy/lazy_spec.rb +++ b/spec/ruby/core/enumerator/lazy/lazy_spec.rb @@ -4,39 +4,24 @@ require_relative '../../../spec_helper' describe "Enumerator::Lazy" do it "is a subclass of Enumerator" do - Enumerator::Lazy.superclass.should equal(Enumerator) + Enumerator::Lazy.superclass.should.equal?(Enumerator) end it "defines lazy versions of a whitelist of Enumerator methods" do - lazy_methods = [ - :chunk, :collect, :collect_concat, :drop, :drop_while, :enum_for, + lazy_methods = Set[ + :chunk, :chunk_while, :collect, :collect_concat, :compact, :drop, :drop_while, :enum_for, :find_all, :flat_map, :force, :grep, :grep_v, :lazy, :map, :reject, :select, :slice_after, :slice_before, :slice_when, :take, :take_while, - :to_enum, :zip + :to_enum, :uniq, :zip ] - lazy_methods += [:chunk_while, :uniq] - ruby_version_is '3.1' do - lazy_methods += [:compact] - end - - Enumerator::Lazy.instance_methods(false).should include(*lazy_methods) + Enumerator::Lazy.instance_methods(false).to_set.should >= lazy_methods end end describe "Enumerator::Lazy#lazy" do it "returns self" do lazy = (1..3).to_enum.lazy - lazy.lazy.should equal(lazy) - end -end - -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 + lazy.lazy.should.equal?(lazy) end end diff --git a/spec/ruby/core/enumerator/lazy/reject_spec.rb b/spec/ruby/core/enumerator/lazy/reject_spec.rb index 0e1632d667..374d4df14e 100644 --- a/spec/ruby/core/enumerator/lazy/reject_spec.rb +++ b/spec/ruby/core/enumerator/lazy/reject_spec.rb @@ -16,8 +16,8 @@ describe "Enumerator::Lazy#reject" do 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) + ret.should.instance_of?(Enumerator::Lazy) + ret.should_not.equal?(@yieldsmixed) end it "sets #size to nil" do @@ -42,7 +42,7 @@ describe "Enumerator::Lazy#reject" do -> { lazy.first - }.should raise_error(RuntimeError, "foo") + }.should.raise(RuntimeError, "foo") end it "calls the block with a gathered array when yield with multiple arguments" do @@ -52,7 +52,7 @@ describe "Enumerator::Lazy#reject" do end it "raises an ArgumentError when not given a block" do - -> { @yieldsmixed.reject }.should raise_error(ArgumentError) + -> { @yieldsmixed.reject }.should.raise(ArgumentError) end describe "on a nested Lazy" do diff --git a/spec/ruby/core/enumerator/lazy/shared/collect.rb b/spec/ruby/core/enumerator/lazy/shared/collect.rb index 5690255a0c..0ed04c8e02 100644 --- a/spec/ruby/core/enumerator/lazy/shared/collect.rb +++ b/spec/ruby/core/enumerator/lazy/shared/collect.rb @@ -16,8 +16,8 @@ describe :enumerator_lazy_collect, shared: true do 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) + ret.should.instance_of?(Enumerator::Lazy) + ret.should_not.equal?(@yieldsmixed) end it "keeps size" do diff --git a/spec/ruby/core/enumerator/lazy/shared/collect_concat.rb b/spec/ruby/core/enumerator/lazy/shared/collect_concat.rb index 00d7941a61..685e6d0594 100644 --- a/spec/ruby/core/enumerator/lazy/shared/collect_concat.rb +++ b/spec/ruby/core/enumerator/lazy/shared/collect_concat.rb @@ -16,8 +16,8 @@ describe :enumerator_lazy_collect_concat, shared: true do 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) + ret.should.instance_of?(Enumerator::Lazy) + ret.should_not.equal?(@yieldsmixed) end it "sets #size to nil" do @@ -34,7 +34,7 @@ describe :enumerator_lazy_collect_concat, shared: true do 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 }.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 @@ -46,7 +46,7 @@ describe :enumerator_lazy_collect_concat, shared: true do end it "raises an ArgumentError when not given a block" do - -> { @yieldsmixed.send(@method) }.should raise_error(ArgumentError) + -> { @yieldsmixed.send(@method) }.should.raise(ArgumentError) end describe "on a nested Lazy" do @@ -64,7 +64,7 @@ describe :enumerator_lazy_collect_concat, shared: true do 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 }.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 diff --git a/spec/ruby/core/enumerator/lazy/shared/select.rb b/spec/ruby/core/enumerator/lazy/shared/select.rb index 50a00bcbf4..2d15176620 100644 --- a/spec/ruby/core/enumerator/lazy/shared/select.rb +++ b/spec/ruby/core/enumerator/lazy/shared/select.rb @@ -16,8 +16,8 @@ describe :enumerator_lazy_select, shared: true do 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) + ret.should.instance_of?(Enumerator::Lazy) + ret.should_not.equal?(@yieldsmixed) end it "sets #size to nil" do @@ -40,7 +40,7 @@ describe :enumerator_lazy_select, shared: true do end it "raises an ArgumentError when not given a block" do - -> { @yieldsmixed.send(@method) }.should raise_error(ArgumentError) + -> { @yieldsmixed.send(@method) }.should.raise(ArgumentError) end describe "on a nested Lazy" do diff --git a/spec/ruby/core/enumerator/lazy/shared/to_enum.rb b/spec/ruby/core/enumerator/lazy/shared/to_enum.rb index 0c91ea55b9..75b9aefe8c 100644 --- a/spec/ruby/core/enumerator/lazy/shared/to_enum.rb +++ b/spec/ruby/core/enumerator/lazy/shared/to_enum.rb @@ -13,8 +13,8 @@ describe :enumerator_lazy_to_enum, shared: true do 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) + ret.should.instance_of?(Enumerator::Lazy) + ret.should_not.equal?(@infinite) end it "sets #size to nil when not given a block" do @@ -43,7 +43,7 @@ describe :enumerator_lazy_to_enum, shared: true do each_entry: [], each_cons: [2] }.each_pair do |method, args| - @infinite.send(method, *args).should be_an_instance_of(Enumerator::Lazy) + @infinite.send(method, *args).should.instance_of?(Enumerator::Lazy) end end diff --git a/spec/ruby/core/enumerator/lazy/slice_after_spec.rb b/spec/ruby/core/enumerator/lazy/slice_after_spec.rb index 8b08a1ecfd..f8cd97178f 100644 --- a/spec/ruby/core/enumerator/lazy/slice_after_spec.rb +++ b/spec/ruby/core/enumerator/lazy/slice_after_spec.rb @@ -9,6 +9,6 @@ describe "Enumerator::Lazy#slice_after" do it "should return a lazy enumerator" do s = 0..Float::INFINITY - s.lazy.slice_after { |n| true }.should be_kind_of(Enumerator::Lazy) + s.lazy.slice_after { |n| true }.should.is_a?(Enumerator::Lazy) end end diff --git a/spec/ruby/core/enumerator/lazy/slice_before_spec.rb b/spec/ruby/core/enumerator/lazy/slice_before_spec.rb index 9c1ec9ba4a..192e853343 100644 --- a/spec/ruby/core/enumerator/lazy/slice_before_spec.rb +++ b/spec/ruby/core/enumerator/lazy/slice_before_spec.rb @@ -9,6 +9,6 @@ describe "Enumerator::Lazy#slice_before" do it "should return a lazy enumerator" do s = 0..Float::INFINITY - s.lazy.slice_before { |n| true }.should be_kind_of(Enumerator::Lazy) + s.lazy.slice_before { |n| true }.should.is_a?(Enumerator::Lazy) end end diff --git a/spec/ruby/core/enumerator/lazy/slice_when_spec.rb b/spec/ruby/core/enumerator/lazy/slice_when_spec.rb index f83403425d..fc9d5f5069 100644 --- a/spec/ruby/core/enumerator/lazy/slice_when_spec.rb +++ b/spec/ruby/core/enumerator/lazy/slice_when_spec.rb @@ -9,6 +9,6 @@ describe "Enumerator::Lazy#slice_when" do it "should return a lazy enumerator" do s = 0..Float::INFINITY - s.lazy.slice_when { |a, b| true }.should be_kind_of(Enumerator::Lazy) + s.lazy.slice_when { |a, b| true }.should.is_a?(Enumerator::Lazy) end end diff --git a/spec/ruby/core/enumerator/lazy/take_spec.rb b/spec/ruby/core/enumerator/lazy/take_spec.rb index 9fc17e969f..f92360f543 100644 --- a/spec/ruby/core/enumerator/lazy/take_spec.rb +++ b/spec/ruby/core/enumerator/lazy/take_spec.rb @@ -16,8 +16,8 @@ describe "Enumerator::Lazy#take" do 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) + ret.should.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 diff --git a/spec/ruby/core/enumerator/lazy/take_while_spec.rb b/spec/ruby/core/enumerator/lazy/take_while_spec.rb index bcea0b1419..c369712c56 100644 --- a/spec/ruby/core/enumerator/lazy/take_while_spec.rb +++ b/spec/ruby/core/enumerator/lazy/take_while_spec.rb @@ -16,8 +16,8 @@ describe "Enumerator::Lazy#take_while" do 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) + ret.should.instance_of?(Enumerator::Lazy) + ret.should_not.equal?(@yieldsmixed) end it "sets #size to nil" do @@ -40,7 +40,7 @@ describe "Enumerator::Lazy#take_while" do end it "raises an ArgumentError when not given a block" do - -> { @yieldsmixed.take_while }.should raise_error(ArgumentError) + -> { @yieldsmixed.take_while }.should.raise(ArgumentError) end describe "on a nested Lazy" do diff --git a/spec/ruby/core/enumerator/lazy/uniq_spec.rb b/spec/ruby/core/enumerator/lazy/uniq_spec.rb index ce67ace5ab..d30ed8df2f 100644 --- a/spec/ruby/core/enumerator/lazy/uniq_spec.rb +++ b/spec/ruby/core/enumerator/lazy/uniq_spec.rb @@ -8,7 +8,7 @@ describe 'Enumerator::Lazy#uniq' do end it 'returns a lazy enumerator' do - @lazy.should be_an_instance_of(Enumerator::Lazy) + @lazy.should.instance_of?(Enumerator::Lazy) @lazy.force.should == [0, 1] end @@ -28,7 +28,7 @@ describe 'Enumerator::Lazy#uniq' do end it 'returns a lazy enumerator' do - @lazy.should be_an_instance_of(Enumerator::Lazy) + @lazy.should.instance_of?(Enumerator::Lazy) @lazy.force.should == [0, 1] end diff --git a/spec/ruby/core/enumerator/lazy/with_index_spec.rb b/spec/ruby/core/enumerator/lazy/with_index_spec.rb index a6b5c38777..2e983fd3b1 100644 --- a/spec/ruby/core/enumerator/lazy/with_index_spec.rb +++ b/spec/ruby/core/enumerator/lazy/with_index_spec.rb @@ -17,7 +17,7 @@ describe "Enumerator::Lazy#with_index" do 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) + -> { (0..Float::INFINITY).lazy.with_index(false).map { |i, idx| i }.first(3) }.should.raise(TypeError) end it "enumerates with a given block" do diff --git a/spec/ruby/core/enumerator/lazy/zip_spec.rb b/spec/ruby/core/enumerator/lazy/zip_spec.rb index 5a828c1dcc..9f612542d7 100644 --- a/spec/ruby/core/enumerator/lazy/zip_spec.rb +++ b/spec/ruby/core/enumerator/lazy/zip_spec.rb @@ -16,8 +16,8 @@ describe "Enumerator::Lazy#zip" do 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) + ret.should.instance_of?(Enumerator::Lazy) + ret.should_not.equal?(@yieldsmixed) end it "keeps size" do @@ -40,11 +40,11 @@ describe "Enumerator::Lazy#zip" do end it "returns a Lazy when no arguments given" do - @yieldsmixed.zip.should be_an_instance_of(Enumerator::Lazy) + @yieldsmixed.zip.should.instance_of?(Enumerator::Lazy) end it "raises a TypeError if arguments contain non-list object" do - -> { @yieldsmixed.zip [], Object.new, [] }.should raise_error(TypeError) + -> { @yieldsmixed.zip [], Object.new, [] }.should.raise(TypeError) end describe "on a nested Lazy" do diff --git a/spec/ruby/core/enumerator/new_spec.rb b/spec/ruby/core/enumerator/new_spec.rb index c439469525..539328a6c9 100644 --- a/spec/ruby/core/enumerator/new_spec.rb +++ b/spec/ruby/core/enumerator/new_spec.rb @@ -2,51 +2,8 @@ require_relative '../../spec_helper' describe "Enumerator.new" do context "no block given" do - ruby_version_is '3.0' do - it "raises" do - -> { Enumerator.new(1, :upto, 3) }.should raise_error(ArgumentError) - end - end - - ruby_version_is ''...'3.0' do - it "creates a new custom enumerator with the given object, iterator and arguments" do - enum = suppress_warning { Enumerator.new(1, :upto, 3) } - enum.should be_an_instance_of(Enumerator) - end - - it "creates a new custom enumerator that responds to #each" do - enum = suppress_warning { Enumerator.new(1, :upto, 3) } - enum.respond_to?(:each).should == true - end - - it "creates a new custom enumerator that runs correctly" do - suppress_warning { Enumerator.new(1, :upto, 3) }.map{ |x| x }.should == [1,2,3] - end - - it "aliases the second argument to :each" do - suppress_warning { Enumerator.new(1..2) }.to_a.should == - suppress_warning { Enumerator.new(1..2, :each) }.to_a - end - - it "doesn't check for the presence of the iterator method" do - suppress_warning { Enumerator.new(nil) }.should be_an_instance_of(Enumerator) - end - - it "uses the latest define iterator method" do - class StrangeEach - def each - yield :foo - end - end - enum = suppress_warning { Enumerator.new(StrangeEach.new) } - enum.to_a.should == [:foo] - class StrangeEach - def each - yield :bar - end - end - enum.to_a.should == [:bar] - end + it "raises" do + -> { Enumerator.new(1, :upto, 3) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/enumerator/next_spec.rb b/spec/ruby/core/enumerator/next_spec.rb index a5e01a399d..77e79185a9 100644 --- a/spec/ruby/core/enumerator/next_spec.rb +++ b/spec/ruby/core/enumerator/next_spec.rb @@ -13,14 +13,14 @@ describe "Enumerator#next" do it "raises a StopIteration exception at the end of the stream" do 3.times { @enum.next } - -> { @enum.next }.should raise_error(StopIteration) + -> { @enum.next }.should.raise(StopIteration) end it "cannot be called again until the enumerator is rewound" do 3.times { @enum.next } - -> { @enum.next }.should raise_error(StopIteration) - -> { @enum.next }.should raise_error(StopIteration) - -> { @enum.next }.should raise_error(StopIteration) + -> { @enum.next }.should.raise(StopIteration) + -> { @enum.next }.should.raise(StopIteration) + -> { @enum.next }.should.raise(StopIteration) @enum.rewind @enum.next.should == 1 end diff --git a/spec/ruby/core/enumerator/next_values_spec.rb b/spec/ruby/core/enumerator/next_values_spec.rb index 201b5d323f..63e024e2b4 100644 --- a/spec/ruby/core/enumerator/next_values_spec.rb +++ b/spec/ruby/core/enumerator/next_values_spec.rb @@ -11,6 +11,7 @@ describe "Enumerator#next_values" do yield :e1, :e2, :e3 yield nil yield + yield [:f1, :f2] end @e = o.to_enum @@ -48,8 +49,13 @@ describe "Enumerator#next_values" do @e.next_values.should == [] end - it "raises StopIteration if called on a finished enumerator" do + it "returns an array of array if yield is called with an array" do 7.times { @e.next } - -> { @e.next_values }.should raise_error(StopIteration) + @e.next_values.should == [[:f1, :f2]] + end + + it "raises StopIteration if called on a finished enumerator" do + 8.times { @e.next } + -> { @e.next_values }.should.raise(StopIteration) end end diff --git a/spec/ruby/core/enumerator/peek_spec.rb b/spec/ruby/core/enumerator/peek_spec.rb index 2334385437..096fd2b10c 100644 --- a/spec/ruby/core/enumerator/peek_spec.rb +++ b/spec/ruby/core/enumerator/peek_spec.rb @@ -31,6 +31,6 @@ describe "Enumerator#peek" do it "raises StopIteration if called on a finished enumerator" do 5.times { @e.next } - -> { @e.peek }.should raise_error(StopIteration) + -> { @e.peek }.should.raise(StopIteration) end end diff --git a/spec/ruby/core/enumerator/peek_values_spec.rb b/spec/ruby/core/enumerator/peek_values_spec.rb index 7865546515..63f7988bcd 100644 --- a/spec/ruby/core/enumerator/peek_values_spec.rb +++ b/spec/ruby/core/enumerator/peek_values_spec.rb @@ -11,6 +11,7 @@ describe "Enumerator#peek_values" do yield :e1, :e2, :e3 yield nil yield + yield [:f1, :f2] end @e = o.to_enum @@ -50,8 +51,13 @@ describe "Enumerator#peek_values" do @e.peek_values.should == [] end - it "raises StopIteration if called on a finished enumerator" do + it "returns an array of array if yield is called with an array" do 7.times { @e.next } - -> { @e.peek_values }.should raise_error(StopIteration) + @e.peek_values.should == [[:f1, :f2]] + end + + it "raises StopIteration if called on a finished enumerator" do + 8.times { @e.next } + -> { @e.peek_values }.should.raise(StopIteration) end end diff --git a/spec/ruby/core/enumerator/plus_spec.rb b/spec/ruby/core/enumerator/plus_spec.rb index 755be5b4eb..d6c0fa93ac 100644 --- a/spec/ruby/core/enumerator/plus_spec.rb +++ b/spec/ruby/core/enumerator/plus_spec.rb @@ -12,7 +12,7 @@ describe "Enumerator#+" do chain = one + two + three - chain.should be_an_instance_of(Enumerator::Chain) + chain.should.instance_of?(Enumerator::Chain) chain.each { |item| ScratchPad << item } ScratchPad.recorded.should == [1, 2, 3] end diff --git a/spec/ruby/core/enumerator/product/each_spec.rb b/spec/ruby/core/enumerator/product/each_spec.rb new file mode 100644 index 0000000000..164998404d --- /dev/null +++ b/spec/ruby/core/enumerator/product/each_spec.rb @@ -0,0 +1,71 @@ +require_relative '../../../spec_helper' +require_relative '../../enumerable/shared/enumeratorized' + +describe "Enumerator::Product#each" do + it_behaves_like :enumeratorized_with_origin_size, :each, Enumerator::Product.new([1, 2], [:a, :b]) + + it "yields each element of Cartesian product of enumerators" do + enum = Enumerator::Product.new([1, 2], [:a, :b]) + acc = [] + enum.each { |e| acc << e } + acc.should == [[1, :a], [1, :b], [2, :a], [2, :b]] + end + + it "calls #each_entry method on enumerators" do + object1 = Object.new + def object1.each_entry + yield 1 + yield 2 + end + + object2 = Object.new + def object2.each_entry + yield :a + yield :b + end + + enum = Enumerator::Product.new(object1, object2) + acc = [] + enum.each { |e| acc << e } + acc.should == [[1, :a], [1, :b], [2, :a], [2, :b]] + end + + it "raises a NoMethodError if the object doesn't respond to #each_entry" do + -> { + Enumerator::Product.new(Object.new).each {} + }.should.raise(NoMethodError, /undefined method [`']each_entry' for/) + end + + it "returns enumerator if not given a block" do + enum = Enumerator::Product.new([1, 2], [:a, :b]) + enum.each.should.kind_of?(Enumerator) + + enum = Enumerator::Product.new([1, 2], [:a, :b]) + enum.each.to_a.should == [[1, :a], [1, :b], [2, :a], [2, :b]] + end + + it "returns self if given a block" do + enum = Enumerator::Product.new([1, 2], [:a, :b]) + enum.each {}.should.equal?(enum) + end + + it "doesn't accept arguments" do + Enumerator::Product.instance_method(:each).arity.should == 0 + end + + it "yields each element to a block that takes multiple arguments" do + enum = Enumerator::Product.new([1, 2], [:a, :b]) + + acc = [] + enum.each { |x, y| acc << x } + acc.should == [1, 1, 2, 2] + + acc = [] + enum.each { |x, y| acc << y } + acc.should == [:a, :b, :a, :b] + + acc = [] + enum.each { |x, y, z| acc << z } + acc.should == [nil, nil, nil, nil] + end +end diff --git a/spec/ruby/core/enumerator/product/initialize_copy_spec.rb b/spec/ruby/core/enumerator/product/initialize_copy_spec.rb new file mode 100644 index 0000000000..b5d2b345a9 --- /dev/null +++ b/spec/ruby/core/enumerator/product/initialize_copy_spec.rb @@ -0,0 +1,52 @@ +require_relative '../../../spec_helper' + +describe "Enumerator::Product#initialize_copy" do + it "replaces content of the receiver with content of the other object" do + enum = Enumerator::Product.new([true, false]) + enum2 = Enumerator::Product.new([1, 2], [:a, :b]) + + enum.send(:initialize_copy, enum2) + enum.each.to_a.should == [[1, :a], [1, :b], [2, :a], [2, :b]] + end + + it "returns self" do + enum = Enumerator::Product.new([true, false]) + enum2 = Enumerator::Product.new([1, 2], [:a, :b]) + + enum.send(:initialize_copy, enum2).should.equal?(enum) + end + + it "is a private method" do + Enumerator::Product.private_instance_methods(false).should.include?(:initialize_copy) + end + + it "does nothing if the argument is the same as the receiver" do + enum = Enumerator::Product.new(1..2) + enum.send(:initialize_copy, enum).should.equal?(enum) + + enum.freeze + enum.send(:initialize_copy, enum).should.equal?(enum) + end + + it "raises FrozenError if the receiver is frozen" do + enum = Enumerator::Product.new(1..2) + enum2 = Enumerator::Product.new(3..4) + + -> { enum.freeze.send(:initialize_copy, enum2) }.should.raise(FrozenError) + end + + it "raises TypeError if the objects are of different class" do + enum = Enumerator::Product.new(1..2) + enum2 = Class.new(Enumerator::Product).new(3..4) + + -> { enum.send(:initialize_copy, enum2) }.should.raise(TypeError, 'initialize_copy should take same class object') + -> { enum2.send(:initialize_copy, enum) }.should.raise(TypeError, 'initialize_copy should take same class object') + end + + it "raises ArgumentError if the argument is not initialized yet" do + enum = Enumerator::Product.new(1..2) + enum2 = Enumerator::Product.allocate + + -> { enum.send(:initialize_copy, enum2) }.should.raise(ArgumentError, 'uninitialized product') + end +end diff --git a/spec/ruby/core/enumerator/product/initialize_spec.rb b/spec/ruby/core/enumerator/product/initialize_spec.rb new file mode 100644 index 0000000000..8814f9d3c7 --- /dev/null +++ b/spec/ruby/core/enumerator/product/initialize_spec.rb @@ -0,0 +1,31 @@ +require_relative '../../../spec_helper' + +describe "Enumerator::Product#initialize" do + before :each do + @uninitialized = Enumerator::Product.allocate + end + + it "is a private method" do + Enumerator::Product.private_instance_methods(false).should.include?(:initialize) + end + + it "returns self" do + @uninitialized.send(:initialize).should.equal?(@uninitialized) + end + + it "accepts many arguments" do + @uninitialized.send(:initialize, 0..1, 2..3, 4..5).should.equal?(@uninitialized) + end + + it "accepts arguments that are not Enumerable nor responding to :each_entry" do + @uninitialized.send(:initialize, Object.new).should.equal?(@uninitialized) + end + + describe "on frozen instance" do + it "raises a FrozenError" do + -> { + @uninitialized.freeze.send(:initialize, 0..1) + }.should.raise(FrozenError) + end + end +end diff --git a/spec/ruby/core/enumerator/product/inspect_spec.rb b/spec/ruby/core/enumerator/product/inspect_spec.rb new file mode 100644 index 0000000000..e0d7441f26 --- /dev/null +++ b/spec/ruby/core/enumerator/product/inspect_spec.rb @@ -0,0 +1,20 @@ +require_relative '../../../spec_helper' + +describe "Enumerator::Product#inspect" do + it "returns a String including enumerators" do + enum = Enumerator::Product.new([1, 2], [:a, :b]) + enum.inspect.should == "#<Enumerator::Product: [[1, 2], [:a, :b]]>" + end + + it "represents a recursive element with '[...]'" do + enum = [1, 2] + enum_recursive = Enumerator::Product.new(enum) + + enum << enum_recursive + enum_recursive.inspect.should == "#<Enumerator::Product: [[1, 2, #<Enumerator::Product: ...>]]>" + end + + it "returns a not initialized representation if #initialized is not called yet" do + Enumerator::Product.allocate.inspect.should == "#<Enumerator::Product: uninitialized>" + end +end diff --git a/spec/ruby/core/enumerator/product/rewind_spec.rb b/spec/ruby/core/enumerator/product/rewind_spec.rb new file mode 100644 index 0000000000..2beffaf5c1 --- /dev/null +++ b/spec/ruby/core/enumerator/product/rewind_spec.rb @@ -0,0 +1,62 @@ +require_relative '../../../spec_helper' + +describe "Enumerator::Product#rewind" do + before :each do + @enum = Enumerator::Product.new([1, 2].each.to_enum, [:a, :b].each.to_enum) + end + + it "resets the enumerator to its initial state" do + @enum.each.to_a.should == [[1, :a], [1, :b], [2, :a], [2, :b]] + @enum.rewind + @enum.each.to_a.should == [[1, :a], [1, :b], [2, :a], [2, :b]] + end + + it "returns self" do + @enum.rewind.should.equal? @enum + end + + it "has no effect on a new enumerator" do + @enum.rewind + @enum.each.to_a.should == [[1, :a], [1, :b], [2, :a], [2, :b]] + end + + it "has no effect if called multiple, consecutive times" do + @enum.each.to_a.should == [[1, :a], [1, :b], [2, :a], [2, :b]] + @enum.rewind + @enum.rewind + @enum.each.to_a.should == [[1, :a], [1, :b], [2, :a], [2, :b]] + end + + it "calls the enclosed object's rewind method if one exists" do + obj = mock('rewinder') + enum = Enumerator::Product.new(obj.to_enum) + + obj.should_receive(:rewind) + enum.rewind + end + + it "does nothing if the object doesn't have a #rewind method" do + obj = mock('rewinder') + enum = Enumerator::Product.new(obj.to_enum) + + enum.rewind.should == enum + end + + it "calls a rewind method on each enumerable in direct order" do + ScratchPad.record [] + + object1 = Object.new + def object1.rewind; ScratchPad << :object1; end + + object2 = Object.new + def object2.rewind; ScratchPad << :object2; end + + object3 = Object.new + def object3.rewind; ScratchPad << :object3; end + + enum = Enumerator::Product.new(object1, object2, object3) + enum.rewind + + ScratchPad.recorded.should == [:object1, :object2, :object3] + end +end diff --git a/spec/ruby/core/enumerator/product/size_spec.rb b/spec/ruby/core/enumerator/product/size_spec.rb new file mode 100644 index 0000000000..96632d6eee --- /dev/null +++ b/spec/ruby/core/enumerator/product/size_spec.rb @@ -0,0 +1,54 @@ +require_relative '../../../spec_helper' + +describe "Enumerator::Product#size" do + it "returns the total size of the enumerator product calculated by multiplying the sizes of enumerables in the product" do + product = Enumerator::Product.new(1..2, 1..3, 1..4) + product.size.should == 24 # 2 * 3 * 4 + end + + it "returns nil if any enumerable reports its size as nil" do + enum = Object.new + def enum.size; nil; end + + product = Enumerator::Product.new(1..2, enum) + product.size.should == nil + end + + it "returns Float::INFINITY if any enumerable reports its size as Float::INFINITY" do + enum = Object.new + def enum.size; Float::INFINITY; end + + product = Enumerator::Product.new(1..2, enum) + product.size.should == Float::INFINITY + end + + it "returns nil if any enumerable reports its size as Float::NAN" do + enum = Object.new + def enum.size; Float::NAN; end + + product = Enumerator::Product.new(1..2, enum) + product.size.should == nil + end + + it "returns nil if any enumerable doesn't respond to #size" do + enum = Object.new + product = Enumerator::Product.new(1..2, enum) + product.size.should == nil + end + + it "returns nil if any enumerable reports a not-convertible to Integer" do + enum = Object.new + def enum.size; :symbol; end + + product = Enumerator::Product.new(1..2, enum) + product.size.should == nil + end + + it "returns nil if any enumerable reports a non-Integer but convertible to Integer size" do + enum = Object.new + def enum.size; 1.0; end + + product = Enumerator::Product.new(1..2, enum) + product.size.should == nil + end +end diff --git a/spec/ruby/core/enumerator/product_spec.rb b/spec/ruby/core/enumerator/product_spec.rb new file mode 100644 index 0000000000..0f37ef76e7 --- /dev/null +++ b/spec/ruby/core/enumerator/product_spec.rb @@ -0,0 +1,91 @@ +require_relative '../../spec_helper' + +describe "Enumerator.product" do + it "returns a Cartesian product of enumerators" do + enum = Enumerator.product(1..2, ["A", "B"]) + enum.to_a.should == [[1, "A"], [1, "B"], [2, "A"], [2, "B"]] + end + + it "accepts a list of enumerators of any length" do + enum = Enumerator.product(1..2) + enum.to_a.should == [[1], [2]] + + enum = Enumerator.product(1..2, ["A"]) + enum.to_a.should == [[1, "A"], [2, "A"]] + + enum = Enumerator.product(1..2, ["A"], ["B"]) + enum.to_a.should == [[1, "A", "B"], [2, "A", "B"]] + + enum = Enumerator.product(2..3, ["A"], ["B"], ["C"]) + enum.to_a.should == [[2, "A", "B", "C"], [3, "A", "B", "C"]] + end + + it "returns an enumerator with an empty array when no arguments passed" do + enum = Enumerator.product + enum.to_a.should == [[]] + end + + it "returns an instance of Enumerator::Product" do + enum = Enumerator.product + enum.class.should == Enumerator::Product + end + + it "accepts infinite enumerators and returns infinite enumerator" do + enum = Enumerator.product(1.., ["A", "B"]) + enum.take(5).should == [[1, "A"], [1, "B"], [2, "A"], [2, "B"], [3, "A"]] + enum.size.should == Float::INFINITY + end + + it "accepts a block" do + elems = [] + enum = Enumerator.product(1..2, ["X", "Y"]) { elems << _1 } + + elems.should == [[1, "X"], [1, "Y"], [2, "X"], [2, "Y"]] + end + + it "returns nil when a block passed" do + Enumerator.product(1..2) {}.should == nil + end + + # https://bugs.ruby-lang.org/issues/19829 + it "reject keyword arguments" do + -> { + Enumerator.product(1..3, foo: 1, bar: 2) + }.should.raise(ArgumentError, "unknown keywords: :foo, :bar") + end + + it "calls only #each_entry method on arguments" do + object = Object.new + def object.each_entry + yield 1 + yield 2 + end + + enum = Enumerator.product(object, ["A", "B"]) + enum.to_a.should == [[1, "A"], [1, "B"], [2, "A"], [2, "B"]] + end + + it "raises NoMethodError when argument doesn't respond to #each_entry" do + -> { + Enumerator.product(Object.new).to_a + }.should.raise(NoMethodError, /undefined method [`']each_entry' for/) + end + + it "calls #each_entry lazily" do + Enumerator.product(Object.new).should.is_a?(Enumerator) + end + + it "iterates through consuming enumerator elements only once" do + a = [1, 2, 3] + i = 0 + + enum = Enumerator.new do |y| + while i < a.size + y << a[i] + i += 1 + end + end + + Enumerator.product(['a', 'b'], enum).to_a.should == [["a", 1], ["a", 2], ["a", 3]] + end +end diff --git a/spec/ruby/core/enumerator/rewind_spec.rb b/spec/ruby/core/enumerator/rewind_spec.rb index a105f2c619..6ba0edf174 100644 --- a/spec/ruby/core/enumerator/rewind_spec.rb +++ b/spec/ruby/core/enumerator/rewind_spec.rb @@ -14,7 +14,7 @@ describe "Enumerator#rewind" do end it "returns self" do - @enum.rewind.should == @enum + @enum.rewind.should.equal? @enum end it "has no effect on a new enumerator" do @@ -49,7 +49,7 @@ describe "Enumerator#rewind" do obj = mock('rewinder') enum = obj.to_enum obj.should_receive(:each).at_most(1) - -> { enum.rewind.should == enum }.should_not raise_error + enum.rewind.should == enum end end diff --git a/spec/ruby/core/enumerator/shared/enum_for.rb b/spec/ruby/core/enumerator/shared/enum_for.rb new file mode 100644 index 0000000000..4388103ecf --- /dev/null +++ b/spec/ruby/core/enumerator/shared/enum_for.rb @@ -0,0 +1,57 @@ +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_index.rb b/spec/ruby/core/enumerator/shared/with_index.rb new file mode 100644 index 0000000000..0992397e95 --- /dev/null +++ b/spec/ruby/core/enumerator/shared/with_index.rb @@ -0,0 +1,33 @@ +require_relative '../../../spec_helper' + +describe :enum_with_index, shared: true do + + require_relative '../fixtures/classes' + + before :each do + @origin = [1, 2, 3, 4] + @enum = @origin.to_enum + end + + it "passes each element and its index to block" do + a = [] + @enum.send(@method) { |o, i| a << [o, i] } + a.should == [[1, 0], [2, 1], [3, 2], [4, 3]] + end + + it "returns the object being enumerated when given a block" do + @enum.send(@method) { |o, i| :glark }.should.equal?(@origin) + end + + it "binds splat arguments properly" do + acc = [] + @enum.send(@method) { |*b| c,d = b; acc << c; acc << d } + [1, 0, 2, 1, 3, 2, 4, 3].should == acc + end + + it "returns an enumerator if no block is supplied" do + ewi = @enum.send(@method) + ewi.should.instance_of?(Enumerator) + ewi.to_a.should == [[1, 0], [2, 1], [3, 2], [4, 3]] + end +end diff --git a/spec/ruby/core/enumerator/shared/with_object.rb b/spec/ruby/core/enumerator/shared/with_object.rb new file mode 100644 index 0000000000..50d4f24eb3 --- /dev/null +++ b/spec/ruby/core/enumerator/shared/with_object.rb @@ -0,0 +1,42 @@ +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/size_spec.rb b/spec/ruby/core/enumerator/size_spec.rb index 6accd26a4e..4b2beffbbe 100644 --- a/spec/ruby/core/enumerator/size_spec.rb +++ b/spec/ruby/core/enumerator/size_spec.rb @@ -6,7 +6,7 @@ describe "Enumerator#size" do end it "returns nil if set size is nil" do - Enumerator.new(nil) {}.size.should be_nil + Enumerator.new(nil) {}.size.should == nil end it "returns returning value from size.call if set size is a Proc" do diff --git a/spec/ruby/core/enumerator/to_enum_spec.rb b/spec/ruby/core/enumerator/to_enum_spec.rb index cadfcf6314..7fb73d0c3c 100644 --- a/spec/ruby/core/enumerator/to_enum_spec.rb +++ b/spec/ruby/core/enumerator/to_enum_spec.rb @@ -1,6 +1,6 @@ require_relative '../../spec_helper' -require_relative '../../shared/enumerator/enum_for' +require_relative 'shared/enum_for' describe "Enumerator#to_enum" do - it_behaves_like :enum_for, :enum_for + it_behaves_like :enum_for, :to_enum end diff --git a/spec/ruby/core/enumerator/with_index_spec.rb b/spec/ruby/core/enumerator/with_index_spec.rb index ac37cee508..ca90fd18f7 100644 --- a/spec/ruby/core/enumerator/with_index_spec.rb +++ b/spec/ruby/core/enumerator/with_index_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/enumerator/with_index' +require_relative 'shared/with_index' require_relative '../enumerable/shared/enumeratorized' describe "Enumerator#with_index" do @@ -9,20 +9,20 @@ describe "Enumerator#with_index" do it "returns a new Enumerator when no block is given" do enum1 = [1,2,3].select enum2 = enum1.with_index - enum2.should be_an_instance_of(Enumerator) + enum2.should.instance_of?(Enumerator) enum1.should_not === enum2 end it "accepts an optional argument when given a block" do -> do @enum.with_index(1) { |f| f} - end.should_not raise_error(ArgumentError) + end.should_not.raise(ArgumentError) end it "accepts an optional argument when not given a block" do -> do @enum.with_index(1) - end.should_not raise_error(ArgumentError) + end.should_not.raise(ArgumentError) end it "numbers indices from the given index when given an offset but no block" do @@ -38,7 +38,7 @@ describe "Enumerator#with_index" do it "raises a TypeError when the argument cannot be converted to numeric" do -> do @enum.with_index('1') {|*i| i} - end.should raise_error(TypeError) + end.should.raise(TypeError) end it "converts non-numeric arguments to Integer via #to_int" do @@ -69,4 +69,21 @@ describe "Enumerator#with_index" do @enum.with_index(-1) { |*x| res << x} res.should == [[1,-1], [2,0], [3,1], [4,2]] end + + it "passes on the given block's return value" do + arr = [1,2,3] + arr.delete_if.with_index { |a,b| false } + arr.should == [1,2,3] + + arr.delete_if.with_index { |a,b| true } + arr.should == [] + end + + it "returns the iterator's return value" do + @enum.select.with_index { |a,b| false }.should == [] + end + + it "returns the correct value if chained with itself" do + [:a].each.with_index.with_index.to_a.should == [[[:a,0],0]] + end end diff --git a/spec/ruby/core/enumerator/with_object_spec.rb b/spec/ruby/core/enumerator/with_object_spec.rb index e7ba83fd9f..58031fd765 100644 --- a/spec/ruby/core/enumerator/with_object_spec.rb +++ b/spec/ruby/core/enumerator/with_object_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/enumerator/with_object' +require_relative 'shared/with_object' describe "Enumerator#with_object" do it_behaves_like :enum_with_object, :with_object diff --git a/spec/ruby/core/enumerator/yielder/append_spec.rb b/spec/ruby/core/enumerator/yielder/append_spec.rb index a36e5d64b6..2e1f5203d9 100644 --- a/spec/ruby/core/enumerator/yielder/append_spec.rb +++ b/spec/ruby/core/enumerator/yielder/append_spec.rb @@ -19,7 +19,7 @@ describe "Enumerator::Yielder#<<" do it "returns self" do y = Enumerator::Yielder.new {|x| x + 1} - (y << 1).should equal(y) + (y << 1).should.equal?(y) end context "when multiple arguments passed" do @@ -29,7 +29,7 @@ describe "Enumerator::Yielder#<<" do -> { y.<<(1, 2) - }.should raise_error(ArgumentError, /wrong number of arguments/) + }.should.raise(ArgumentError, /wrong number of arguments/) end end end diff --git a/spec/ruby/core/enumerator/yielder/initialize_spec.rb b/spec/ruby/core/enumerator/yielder/initialize_spec.rb index 5a6eee2d0f..925f561ec4 100644 --- a/spec/ruby/core/enumerator/yielder/initialize_spec.rb +++ b/spec/ruby/core/enumerator/yielder/initialize_spec.rb @@ -9,10 +9,10 @@ describe "Enumerator::Yielder#initialize" do end it "is a private method" do - @class.should have_private_instance_method(:initialize, false) + @class.private_instance_methods(false).should.include?(:initialize) end it "returns self when given a block" do - @uninitialized.send(:initialize) {}.should equal(@uninitialized) + @uninitialized.send(:initialize) {}.should.equal?(@uninitialized) end end |
