diff options
Diffstat (limited to 'spec/ruby/core/enumerable')
65 files changed, 898 insertions, 730 deletions
diff --git a/spec/ruby/core/enumerable/all_spec.rb b/spec/ruby/core/enumerable/all_spec.rb index 8af80896a9..cbdd63f86a 100644 --- a/spec/ruby/core/enumerable/all_spec.rb +++ b/spec/ruby/core/enumerable/all_spec.rb @@ -10,66 +10,57 @@ describe "Enumerable#all?" do end it "always returns true on empty enumeration" do - @empty.all?.should == true + @empty.should.all? @empty.all? { nil }.should == true - [].all?.should == true + [].should.all? [].all? { false }.should == true - {}.all?.should == true + {}.should.all? {}.all? { nil }.should == true end it "raises an ArgumentError when more than 1 argument is provided" do - -> { @enum.all?(1, 2, 3) }.should raise_error(ArgumentError) - -> { [].all?(1, 2, 3) }.should raise_error(ArgumentError) - -> { {}.all?(1, 2, 3) }.should raise_error(ArgumentError) - end - - ruby_version_is ""..."2.5" do - it "raises an ArgumentError when any arguments provided" do - -> { @enum.all?(Proc.new {}) }.should raise_error(ArgumentError) - -> { @enum.all?(nil) }.should raise_error(ArgumentError) - -> { @empty.all?(1) }.should raise_error(ArgumentError) - -> { @enum1.all?(1) {} }.should raise_error(ArgumentError) - end + -> { @enum.all?(1, 2, 3) }.should.raise(ArgumentError) + -> { [].all?(1, 2, 3) }.should.raise(ArgumentError) + -> { {}.all?(1, 2, 3) }.should.raise(ArgumentError) end it "does not hide exceptions out of #each" do -> { EnumerableSpecs::ThrowingEach.new.all? - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) -> { EnumerableSpecs::ThrowingEach.new.all? { false } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end describe "with no block" do it "returns true if no elements are false or nil" do - @enum.all?.should == true - @enum1.all?.should == true - @enum2.all?.should == false + @enum.should.all? + @enum1.should.all? + @enum2.should_not.all? - EnumerableSpecs::Numerous.new('a','b','c').all?.should == true - EnumerableSpecs::Numerous.new(0, "x", true).all?.should == true + EnumerableSpecs::Numerous.new('a','b','c').should.all? + EnumerableSpecs::Numerous.new(0, "x", true).should.all? end it "returns false if there are false or nil elements" do - EnumerableSpecs::Numerous.new(false).all?.should == false - EnumerableSpecs::Numerous.new(false, false).all?.should == false + EnumerableSpecs::Numerous.new(false).should_not.all? + EnumerableSpecs::Numerous.new(false, false).should_not.all? - EnumerableSpecs::Numerous.new(nil).all?.should == false - EnumerableSpecs::Numerous.new(nil, nil).all?.should == false + EnumerableSpecs::Numerous.new(nil).should_not.all? + EnumerableSpecs::Numerous.new(nil, nil).should_not.all? - EnumerableSpecs::Numerous.new(1, nil, 2).all?.should == false - EnumerableSpecs::Numerous.new(0, "x", false, true).all?.should == false - @enum2.all?.should == false + EnumerableSpecs::Numerous.new(1, nil, 2).should_not.all? + EnumerableSpecs::Numerous.new(0, "x", false, true).should_not.all? + @enum2.should_not.all? end it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMultiWithFalse.new - multi.all?.should be_true + multi.all?.should == true end end @@ -115,7 +106,7 @@ describe "Enumerable#all?" do it "does not hide exceptions out of the block" do -> { @enum.all? { raise "from block" } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "gathers initial args as elements when each yields multiple" do @@ -133,69 +124,64 @@ describe "Enumerable#all?" do end end - ruby_version_is "2.5" do - describe 'when given a pattern argument' do - it "calls `===` on the pattern the return value " do - pattern = EnumerableSpecs::Pattern.new { |x| x >= 0 } - @enum1.all?(pattern).should == false - pattern.yielded.should == [[0], [1], [2], [-1]] - end - - # may raise an exception in future versions - ruby_version_is ""..."2.6" do - it "ignores block" do - @enum2.all?(NilClass) { raise }.should == false - [1, 2, nil].all?(NilClass) { raise }.should == false - {a: 1}.all?(Array) { raise }.should == true - end - end - - it "always returns true on empty enumeration" do - @empty.all?(Integer).should == true - [].all?(Integer).should == true - {}.all?(NilClass).should == true - end - - it "does not hide exceptions out of #each" do - -> { - EnumerableSpecs::ThrowingEach.new.all?(Integer) - }.should raise_error(RuntimeError) - end - - it "returns true if the pattern never returns false or nil" do - pattern = EnumerableSpecs::Pattern.new { |x| 42 } - @enum.all?(pattern).should == true - - [1, 42, 3].all?(pattern).should == true - - pattern = EnumerableSpecs::Pattern.new { |x| Array === x } - {a: 1, b: 2}.all?(pattern).should == true - end - - it "returns false if the pattern ever returns false or nil" do - pattern = EnumerableSpecs::Pattern.new { |x| x >= 0 } - @enum1.all?(pattern).should == false - pattern.yielded.should == [[0], [1], [2], [-1]] - - [1, 2, 3, -1].all?(pattern).should == false - - pattern = EnumerableSpecs::Pattern.new { |x| x[1] >= 0 } - {a: 1, b: -1}.all?(pattern).should == false - end - - it "does not hide exceptions out of pattern#===" do - pattern = EnumerableSpecs::Pattern.new { raise "from pattern" } - -> { - @enum.all?(pattern) - }.should raise_error(RuntimeError) - end - - it "calls the pattern with gathered array when yielded with multiple arguments" do - multi = EnumerableSpecs::YieldsMulti.new - pattern = EnumerableSpecs::Pattern.new { true } - multi.all?(pattern).should == true - pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]] - end + describe 'when given a pattern argument' do + it "calls `===` on the pattern the return value" do + pattern = EnumerableSpecs::Pattern.new { |x| x >= 0 } + @enum1.all?(pattern).should == false + pattern.yielded.should == [[0], [1], [2], [-1]] + end + + it "always returns true on empty enumeration" do + @empty.all?(Integer).should == true + [].all?(Integer).should == true + {}.all?(NilClass).should == true + end + + it "does not hide exceptions out of #each" do + -> { + EnumerableSpecs::ThrowingEach.new.all?(Integer) + }.should.raise(RuntimeError) + end + + it "returns true if the pattern never returns false or nil" do + pattern = EnumerableSpecs::Pattern.new { |x| 42 } + @enum.all?(pattern).should == true + + [1, 42, 3].all?(pattern).should == true + + pattern = EnumerableSpecs::Pattern.new { |x| Array === x } + {a: 1, b: 2}.all?(pattern).should == true + end + + it "returns false if the pattern ever returns false or nil" do + pattern = EnumerableSpecs::Pattern.new { |x| x >= 0 } + @enum1.all?(pattern).should == false + pattern.yielded.should == [[0], [1], [2], [-1]] + + [1, 2, 3, -1].all?(pattern).should == false + + pattern = EnumerableSpecs::Pattern.new { |x| x[1] >= 0 } + {a: 1, b: -1}.all?(pattern).should == false + end + + it "does not hide exceptions out of pattern#===" do + pattern = EnumerableSpecs::Pattern.new { raise "from pattern" } + -> { + @enum.all?(pattern) + }.should.raise(RuntimeError) + end + + it "calls the pattern with gathered array when yielded with multiple arguments" do + multi = EnumerableSpecs::YieldsMulti.new + pattern = EnumerableSpecs::Pattern.new { true } + multi.all?(pattern).should == true + pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]] + end + + it "ignores the block if there is an argument" do + -> { + EnumerableSpecs::Numerous.new(1, 2, 3, 4, 5).all?(String) { true }.should == false + }.should complain(/given block not used/) end end end diff --git a/spec/ruby/core/enumerable/any_spec.rb b/spec/ruby/core/enumerable/any_spec.rb index c800fe2d4b..4405d4740a 100644 --- a/spec/ruby/core/enumerable/any_spec.rb +++ b/spec/ruby/core/enumerable/any_spec.rb @@ -10,66 +10,57 @@ describe "Enumerable#any?" do end it "always returns false on empty enumeration" do - @empty.any?.should == false + @empty.should_not.any? @empty.any? { nil }.should == false - [].any?.should == false + [].should_not.any? [].any? { false }.should == false - {}.any?.should == false + {}.should_not.any? {}.any? { nil }.should == false end it "raises an ArgumentError when more than 1 argument is provided" do - -> { @enum.any?(1, 2, 3) }.should raise_error(ArgumentError) - -> { [].any?(1, 2, 3) }.should raise_error(ArgumentError) - -> { {}.any?(1, 2, 3) }.should raise_error(ArgumentError) - end - - ruby_version_is ""..."2.5" do - it "raises an ArgumentError when any arguments provided" do - -> { @enum.any?(Proc.new {}) }.should raise_error(ArgumentError) - -> { @enum.any?(nil) }.should raise_error(ArgumentError) - -> { @empty.any?(1) }.should raise_error(ArgumentError) - -> { @enum1.any?(1) {} }.should raise_error(ArgumentError) - end + -> { @enum.any?(1, 2, 3) }.should.raise(ArgumentError) + -> { [].any?(1, 2, 3) }.should.raise(ArgumentError) + -> { {}.any?(1, 2, 3) }.should.raise(ArgumentError) end it "does not hide exceptions out of #each" do -> { EnumerableSpecs::ThrowingEach.new.any? - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) -> { EnumerableSpecs::ThrowingEach.new.any? { false } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end describe "with no block" do it "returns true if any element is not false or nil" do - @enum.any?.should == true - @enum1.any?.should == true - @enum2.any?.should == true - EnumerableSpecs::Numerous.new(true).any?.should == true - EnumerableSpecs::Numerous.new('a','b','c').any?.should == true - EnumerableSpecs::Numerous.new('a','b','c', nil).any?.should == true - EnumerableSpecs::Numerous.new(1, nil, 2).any?.should == true - EnumerableSpecs::Numerous.new(1, false).any?.should == true - EnumerableSpecs::Numerous.new(false, nil, 1, false).any?.should == true - EnumerableSpecs::Numerous.new(false, 0, nil).any?.should == true + @enum.should.any? + @enum1.should.any? + @enum2.should.any? + EnumerableSpecs::Numerous.new(true).should.any? + EnumerableSpecs::Numerous.new('a','b','c').should.any? + EnumerableSpecs::Numerous.new('a','b','c', nil).should.any? + EnumerableSpecs::Numerous.new(1, nil, 2).should.any? + EnumerableSpecs::Numerous.new(1, false).should.any? + EnumerableSpecs::Numerous.new(false, nil, 1, false).should.any? + EnumerableSpecs::Numerous.new(false, 0, nil).should.any? end it "returns false if all elements are false or nil" do - EnumerableSpecs::Numerous.new(false).any?.should == false - EnumerableSpecs::Numerous.new(false, false).any?.should == false - EnumerableSpecs::Numerous.new(nil).any?.should == false - EnumerableSpecs::Numerous.new(nil, nil).any?.should == false - EnumerableSpecs::Numerous.new(nil, false, nil).any?.should == false + EnumerableSpecs::Numerous.new(false).should_not.any? + EnumerableSpecs::Numerous.new(false, false).should_not.any? + EnumerableSpecs::Numerous.new(nil).should_not.any? + EnumerableSpecs::Numerous.new(nil, nil).should_not.any? + EnumerableSpecs::Numerous.new(nil, false, nil).should_not.any? end it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMultiWithFalse.new - multi.any?.should be_true + multi.any?.should == true end end @@ -129,7 +120,7 @@ describe "Enumerable#any?" do it "does not hide exceptions out of the block" do -> { @enum.any? { raise "from block" } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "gathers initial args as elements when each yields multiple" do @@ -147,68 +138,63 @@ describe "Enumerable#any?" do end end - ruby_version_is "2.5" do - describe 'when given a pattern argument' do - it "calls `===` on the pattern the return value " do - pattern = EnumerableSpecs::Pattern.new { |x| x == 2 } - @enum1.any?(pattern).should == true - pattern.yielded.should == [[0], [1], [2]] - end - - # may raise an exception in future versions - ruby_version_is ""..."2.6" do - it "ignores block" do - @enum2.any?(NilClass) { raise }.should == true - [1, 2, nil].any?(NilClass) { raise }.should == true - {a: 1}.any?(Array) { raise }.should == true - end - end - - it "always returns false on empty enumeration" do - @empty.any?(Integer).should == false - [].any?(Integer).should == false - {}.any?(NilClass).should == false - end - - it "does not hide exceptions out of #each" do - -> { - EnumerableSpecs::ThrowingEach.new.any?(Integer) - }.should raise_error(RuntimeError) - end - - it "returns true if the pattern ever returns a truthy value" do - @enum2.any?(NilClass).should == true - pattern = EnumerableSpecs::Pattern.new { |x| 42 } - @enum.any?(pattern).should == true - - [1, 42, 3].any?(pattern).should == true - - pattern = EnumerableSpecs::Pattern.new { |x| x == [:b, 2] } - {a: 1, b: 2}.any?(pattern).should == true - end - - it "returns false if the block never returns other than false or nil" do - pattern = EnumerableSpecs::Pattern.new { |x| nil } - @enum1.any?(pattern).should == false - pattern.yielded.should == [[0], [1], [2], [-1]] - - [1, 2, 3].any?(pattern).should == false - {a: 1}.any?(pattern).should == false - end - - it "does not hide exceptions out of pattern#===" do - pattern = EnumerableSpecs::Pattern.new { raise "from pattern" } - -> { - @enum.any?(pattern) - }.should raise_error(RuntimeError) - end - - it "calls the pattern with gathered array when yielded with multiple arguments" do - multi = EnumerableSpecs::YieldsMulti.new - pattern = EnumerableSpecs::Pattern.new { false } - multi.any?(pattern).should == false - pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]] - end + describe 'when given a pattern argument' do + it "calls `===` on the pattern the return value" do + pattern = EnumerableSpecs::Pattern.new { |x| x == 2 } + @enum1.any?(pattern).should == true + pattern.yielded.should == [[0], [1], [2]] + end + + it "always returns false on empty enumeration" do + @empty.any?(Integer).should == false + [].any?(Integer).should == false + {}.any?(NilClass).should == false + end + + it "does not hide exceptions out of #each" do + -> { + EnumerableSpecs::ThrowingEach.new.any?(Integer) + }.should.raise(RuntimeError) + end + + it "returns true if the pattern ever returns a truthy value" do + @enum2.any?(NilClass).should == true + pattern = EnumerableSpecs::Pattern.new { |x| 42 } + @enum.any?(pattern).should == true + + [1, 42, 3].any?(pattern).should == true + + pattern = EnumerableSpecs::Pattern.new { |x| x == [:b, 2] } + {a: 1, b: 2}.any?(pattern).should == true + end + + it "returns false if the block never returns other than false or nil" do + pattern = EnumerableSpecs::Pattern.new { |x| nil } + @enum1.any?(pattern).should == false + pattern.yielded.should == [[0], [1], [2], [-1]] + + [1, 2, 3].any?(pattern).should == false + {a: 1}.any?(pattern).should == false + end + + it "does not hide exceptions out of pattern#===" do + pattern = EnumerableSpecs::Pattern.new { raise "from pattern" } + -> { + @enum.any?(pattern) + }.should.raise(RuntimeError) + end + + it "calls the pattern with gathered array when yielded with multiple arguments" do + multi = EnumerableSpecs::YieldsMulti.new + pattern = EnumerableSpecs::Pattern.new { false } + multi.any?(pattern).should == false + pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]] + end + + it "ignores the block if there is an argument" do + -> { + EnumerableSpecs::Numerous.new(1, 2, 3, 4, 5).any?(String) { true }.should == false + }.should complain(/given block not used/) end end end diff --git a/spec/ruby/core/enumerable/chain_spec.rb b/spec/ruby/core/enumerable/chain_spec.rb index 85c0c03603..a0597e46a1 100644 --- a/spec/ruby/core/enumerable/chain_spec.rb +++ b/spec/ruby/core/enumerable/chain_spec.rb @@ -1,25 +1,23 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -ruby_version_is "2.6" do - describe "Enumerable#chain" do - before :each do - ScratchPad.record [] - end +describe "Enumerable#chain" do + before :each do + ScratchPad.record [] + end - it "returns a chain of self and provided enumerables" do - one = EnumerableSpecs::Numerous.new(1) - two = EnumerableSpecs::Numerous.new(2, 3) - three = EnumerableSpecs::Numerous.new(4, 5, 6) + it "returns a chain of self and provided enumerables" do + one = EnumerableSpecs::Numerous.new(1) + two = EnumerableSpecs::Numerous.new(2, 3) + three = EnumerableSpecs::Numerous.new(4, 5, 6) - chain = one.chain(two, three) + chain = one.chain(two, three) - chain.each { |item| ScratchPad << item } - ScratchPad.recorded.should == [1, 2, 3, 4, 5, 6] - end + chain.each { |item| ScratchPad << item } + ScratchPad.recorded.should == [1, 2, 3, 4, 5, 6] + end - it "returns an Enumerator::Chain if given a block" do - EnumerableSpecs::Numerous.new.chain.should be_an_instance_of(Enumerator::Chain) - end + it "returns an Enumerator::Chain if given a block" do + EnumerableSpecs::Numerous.new.chain.should.instance_of?(Enumerator::Chain) end end diff --git a/spec/ruby/core/enumerable/chunk_spec.rb b/spec/ruby/core/enumerable/chunk_spec.rb index 548544f4e8..7c9b31c991 100644 --- a/spec/ruby/core/enumerable/chunk_spec.rb +++ b/spec/ruby/core/enumerable/chunk_spec.rb @@ -8,13 +8,13 @@ describe "Enumerable#chunk" do it "returns an Enumerator if called without a block" do chunk = EnumerableSpecs::Numerous.new(1, 2, 3, 1, 2).chunk - chunk.should be_an_instance_of(Enumerator) + chunk.should.instance_of?(Enumerator) result = chunk.with_index {|elt, i| elt - i }.to_a result.should == [[1, [1, 2, 3]], [-2, [1, 2]]] end it "returns an Enumerator if given a block" do - EnumerableSpecs::Numerous.new.chunk {}.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new.chunk {}.should.instance_of?(Enumerator) end it "yields the current element and the current chunk to the block" do @@ -29,12 +29,22 @@ describe "Enumerable#chunk" do result.should == [[1, [1, 2]], [0, [3]], [1, [2]], [0, [3]], [1, [2, 1]]] end + it "returns a partitioned Array of values" do + e = EnumerableSpecs::Numerous.new(1,2,3) + e.chunk { |x| x > 2 }.map(&:last).should == [[1, 2], [3]] + end + it "returns elements for which the block returns :_alone in separate Arrays" do e = EnumerableSpecs::Numerous.new(1, 2, 3, 2, 1) result = e.chunk { |x| x < 2 && :_alone }.to_a result.should == [[:_alone, [1]], [false, [2, 3, 2]], [:_alone, [1]]] end + it "yields Arrays as a single argument to a rest argument" do + e = EnumerableSpecs::Numerous.new([1, 2]) + result = e.chunk { |*x| x.should == [[1,2]] }.to_a + end + it "does not return elements for which the block returns :_separator" do e = EnumerableSpecs::Numerous.new(1, 2, 3, 3, 2, 1) result = e.chunk { |x| x == 2 ? :_separator : 1 }.to_a @@ -49,14 +59,14 @@ describe "Enumerable#chunk" do it "raises a RuntimeError if the block returns a Symbol starting with an underscore other than :_alone or :_separator" do e = EnumerableSpecs::Numerous.new(1, 2, 3, 2, 1) - -> { e.chunk { |x| :_arbitrary }.to_a }.should raise_error(RuntimeError) + -> { e.chunk { |x| :_arbitrary }.to_a }.should.raise(RuntimeError) end it "does not accept arguments" do e = EnumerableSpecs::Numerous.new(1, 2, 3) -> { e.chunk(1) {} - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it 'returned Enumerator size returns nil' do diff --git a/spec/ruby/core/enumerable/chunk_while_spec.rb b/spec/ruby/core/enumerable/chunk_while_spec.rb index 26bcc983db..286f717442 100644 --- a/spec/ruby/core/enumerable/chunk_while_spec.rb +++ b/spec/ruby/core/enumerable/chunk_while_spec.rb @@ -11,7 +11,7 @@ describe "Enumerable#chunk_while" do context "when given a block" do it "returns an enumerator" do - @result.should be_an_instance_of(Enumerator) + @result.should.instance_of?(Enumerator) end it "splits chunks between adjacent elements i and j where the block returns false" do @@ -30,7 +30,7 @@ describe "Enumerable#chunk_while" do context "when not given a block" do it "raises an ArgumentError" do - -> { @enum.chunk_while }.should raise_error(ArgumentError) + -> { @enum.chunk_while }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/enumerable/collect_concat_spec.rb b/spec/ruby/core/enumerable/collect_concat_spec.rb index 6e34c9eb93..59317cfe34 100644 --- a/spec/ruby/core/enumerable/collect_concat_spec.rb +++ b/spec/ruby/core/enumerable/collect_concat_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/collect_concat' describe "Enumerable#collect_concat" do - it_behaves_like :enumerable_collect_concat , :collect_concat + it_behaves_like :enumerable_collect_concat, :collect_concat end diff --git a/spec/ruby/core/enumerable/collect_spec.rb b/spec/ruby/core/enumerable/collect_spec.rb index 1016b67798..cfa2895cce 100644 --- a/spec/ruby/core/enumerable/collect_spec.rb +++ b/spec/ruby/core/enumerable/collect_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/collect' describe "Enumerable#collect" do - it_behaves_like :enumerable_collect , :collect + it_behaves_like :enumerable_collect, :collect end diff --git a/spec/ruby/core/enumerable/compact_spec.rb b/spec/ruby/core/enumerable/compact_spec.rb new file mode 100644 index 0000000000..1895430c4e --- /dev/null +++ b/spec/ruby/core/enumerable/compact_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Enumerable#compact" do + it 'returns array without nil elements' do + arr = EnumerableSpecs::Numerous.new(nil, 1, 2, nil, true) + arr.compact.should == [1, 2, true] + end +end diff --git a/spec/ruby/core/enumerable/cycle_spec.rb b/spec/ruby/core/enumerable/cycle_spec.rb index 487086cba3..1fb3cc3d41 100644 --- a/spec/ruby/core/enumerable/cycle_spec.rb +++ b/spec/ruby/core/enumerable/cycle_spec.rb @@ -17,7 +17,7 @@ describe "Enumerable#cycle" do it "returns nil if there are no elements" do out = EnumerableSpecs::Empty.new.cycle { break :nope } - out.should be_nil + out.should == nil end it "yields successive elements of the array repeatedly" do @@ -44,8 +44,8 @@ describe "Enumerable#cycle" do describe "passed a number n as an argument" do it "returns nil and does nothing for non positive n" do - EnumerableSpecs::ThrowingEach.new.cycle(0) {}.should be_nil - EnumerableSpecs::NoEach.new.cycle(-22) {}.should be_nil + EnumerableSpecs::ThrowingEach.new.cycle(0) {}.should == nil + EnumerableSpecs::NoEach.new.cycle(-22) {}.should == nil end it "calls each at most once" do @@ -71,12 +71,12 @@ describe "Enumerable#cycle" do it "raises a TypeError when the passed n cannot be coerced to Integer" do enum = EnumerableSpecs::Numerous.new - ->{ enum.cycle("cat"){} }.should raise_error(TypeError) + ->{ enum.cycle("cat"){} }.should.raise(TypeError) end it "raises an ArgumentError if more arguments are passed" do enum = EnumerableSpecs::Numerous.new - ->{ enum.cycle(1, 2) {} }.should raise_error(ArgumentError) + ->{ enum.cycle(1, 2) {} }.should.raise(ArgumentError) end it "gathers whole arrays as elements when each yields multiple" do diff --git a/spec/ruby/core/enumerable/detect_spec.rb b/spec/ruby/core/enumerable/detect_spec.rb index e912134fed..6959aadc44 100644 --- a/spec/ruby/core/enumerable/detect_spec.rb +++ b/spec/ruby/core/enumerable/detect_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/find' describe "Enumerable#detect" do - it_behaves_like :enumerable_find , :detect + it_behaves_like :enumerable_find, :detect end diff --git a/spec/ruby/core/enumerable/drop_spec.rb b/spec/ruby/core/enumerable/drop_spec.rb index 423cc0088b..8d95f464b3 100644 --- a/spec/ruby/core/enumerable/drop_spec.rb +++ b/spec/ruby/core/enumerable/drop_spec.rb @@ -7,13 +7,13 @@ describe "Enumerable#drop" do end it "requires exactly one argument" do - ->{ @enum.drop{} }.should raise_error(ArgumentError) - ->{ @enum.drop(1, 2){} }.should raise_error(ArgumentError) + ->{ @enum.drop{} }.should.raise(ArgumentError) + ->{ @enum.drop(1, 2){} }.should.raise(ArgumentError) end describe "passed a number n as an argument" do it "raises ArgumentError if n < 0" do - ->{ @enum.drop(-1) }.should raise_error(ArgumentError) + ->{ @enum.drop(-1) }.should.raise(ArgumentError) end it "tries to convert n to an Integer using #to_int" do @@ -35,8 +35,8 @@ describe "Enumerable#drop" do end it "raises a TypeError when the passed n cannot be coerced to Integer" do - ->{ @enum.drop("hat") }.should raise_error(TypeError) - ->{ @enum.drop(nil) }.should raise_error(TypeError) + ->{ @enum.drop("hat") }.should.raise(TypeError) + ->{ @enum.drop(nil) }.should.raise(TypeError) end end diff --git a/spec/ruby/core/enumerable/drop_while_spec.rb b/spec/ruby/core/enumerable/drop_while_spec.rb index 636c3d284a..4b4fdf2d4f 100644 --- a/spec/ruby/core/enumerable/drop_while_spec.rb +++ b/spec/ruby/core/enumerable/drop_while_spec.rb @@ -8,7 +8,7 @@ describe "Enumerable#drop_while" do end it "returns an Enumerator if no block given" do - @enum.drop_while.should be_an_instance_of(Enumerator) + @enum.drop_while.should.instance_of?(Enumerator) end it "returns no/all elements for {true/false} block" do @@ -38,7 +38,7 @@ describe "Enumerable#drop_while" do it "doesn't return self when it could" do a = [1,2,3] - a.drop_while{false}.should_not equal(a) + a.drop_while{false}.should_not.equal?(a) end it "gathers whole arrays as elements when each yields multiple" do diff --git a/spec/ruby/core/enumerable/each_cons_spec.rb b/spec/ruby/core/enumerable/each_cons_spec.rb index 7d44f54f74..c5e299fd00 100644 --- a/spec/ruby/core/enumerable/each_cons_spec.rb +++ b/spec/ruby/core/enumerable/each_cons_spec.rb @@ -10,24 +10,24 @@ describe "Enumerable#each_cons" do it "passes element groups to the block" do acc = [] - @enum.each_cons(3){|g| acc << g}.should be_nil + @enum.each_cons(3){|g| acc << g} acc.should == @in_threes end it "raises an ArgumentError if there is not a single parameter > 0" do - ->{ @enum.each_cons(0){} }.should raise_error(ArgumentError) - ->{ @enum.each_cons(-2){} }.should raise_error(ArgumentError) - ->{ @enum.each_cons{} }.should raise_error(ArgumentError) - ->{ @enum.each_cons(2,2){} }.should raise_error(ArgumentError) - ->{ @enum.each_cons(0) }.should raise_error(ArgumentError) - ->{ @enum.each_cons(-2) }.should raise_error(ArgumentError) - ->{ @enum.each_cons }.should raise_error(ArgumentError) - ->{ @enum.each_cons(2,2) }.should raise_error(ArgumentError) + ->{ @enum.each_cons(0){} }.should.raise(ArgumentError) + ->{ @enum.each_cons(-2){} }.should.raise(ArgumentError) + ->{ @enum.each_cons{} }.should.raise(ArgumentError) + ->{ @enum.each_cons(2,2){} }.should.raise(ArgumentError) + ->{ @enum.each_cons(0) }.should.raise(ArgumentError) + ->{ @enum.each_cons(-2) }.should.raise(ArgumentError) + ->{ @enum.each_cons }.should.raise(ArgumentError) + ->{ @enum.each_cons(2,2) }.should.raise(ArgumentError) end it "tries to convert n to an Integer using #to_int" do acc = [] - @enum.each_cons(3.3){|g| acc << g}.should == nil + @enum.each_cons(3.3){|g| acc << g} acc.should == @in_threes obj = mock('to_int') @@ -56,10 +56,14 @@ describe "Enumerable#each_cons" do multi.each_cons(2).to_a.should == [[[1, 2], [3, 4, 5]], [[3, 4, 5], [6, 7, 8, 9]]] end + it "returns self when a block is given" do + @enum.each_cons(3){}.should == @enum + end + describe "when no block is given" do it "returns an enumerator" do e = @enum.each_cons(3) - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == @in_threes end diff --git a/spec/ruby/core/enumerable/each_entry_spec.rb b/spec/ruby/core/enumerable/each_entry_spec.rb index edf00f3137..9dc89ec28e 100644 --- a/spec/ruby/core/enumerable/each_entry_spec.rb +++ b/spec/ruby/core/enumerable/each_entry_spec.rb @@ -11,13 +11,13 @@ describe "Enumerable#each_entry" do it "yields multiple arguments as an array" do acc = [] - @enum.each_entry {|g| acc << g}.should equal(@enum) + @enum.each_entry {|g| acc << g}.should.equal?(@enum) acc.should == @entries end it "returns an enumerator if no block" do e = @enum.each_entry - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == @entries end @@ -27,8 +27,8 @@ describe "Enumerable#each_entry" do end it "raises an ArgumentError when extra arguments" do - -> { @enum.each_entry("one").to_a }.should raise_error(ArgumentError) - -> { @enum.each_entry("one"){}.to_a }.should raise_error(ArgumentError) + -> { @enum.each_entry("one").to_a }.should.raise(ArgumentError) + -> { @enum.each_entry("one"){}.to_a }.should.raise(ArgumentError) end it "passes extra arguments to #each" do diff --git a/spec/ruby/core/enumerable/each_slice_spec.rb b/spec/ruby/core/enumerable/each_slice_spec.rb index ab3b79c344..d05abad1e9 100644 --- a/spec/ruby/core/enumerable/each_slice_spec.rb +++ b/spec/ruby/core/enumerable/each_slice_spec.rb @@ -10,24 +10,24 @@ describe "Enumerable#each_slice" do it "passes element groups to the block" do acc = [] - @enum.each_slice(3){|g| acc << g}.should be_nil + @enum.each_slice(3){|g| acc << g} acc.should == @sliced end it "raises an ArgumentError if there is not a single parameter > 0" do - ->{ @enum.each_slice(0){} }.should raise_error(ArgumentError) - ->{ @enum.each_slice(-2){} }.should raise_error(ArgumentError) - ->{ @enum.each_slice{} }.should raise_error(ArgumentError) - ->{ @enum.each_slice(2,2){} }.should raise_error(ArgumentError) - ->{ @enum.each_slice(0) }.should raise_error(ArgumentError) - ->{ @enum.each_slice(-2) }.should raise_error(ArgumentError) - ->{ @enum.each_slice }.should raise_error(ArgumentError) - ->{ @enum.each_slice(2,2) }.should raise_error(ArgumentError) + ->{ @enum.each_slice(0){} }.should.raise(ArgumentError) + ->{ @enum.each_slice(-2){} }.should.raise(ArgumentError) + ->{ @enum.each_slice{} }.should.raise(ArgumentError) + ->{ @enum.each_slice(2,2){} }.should.raise(ArgumentError) + ->{ @enum.each_slice(0) }.should.raise(ArgumentError) + ->{ @enum.each_slice(-2) }.should.raise(ArgumentError) + ->{ @enum.each_slice }.should.raise(ArgumentError) + ->{ @enum.each_slice(2,2) }.should.raise(ArgumentError) end it "tries to convert n to an Integer using #to_int" do acc = [] - @enum.each_slice(3.3){|g| acc << g}.should == nil + @enum.each_slice(3.3){|g| acc << g} acc.should == @sliced obj = mock('to_int') @@ -53,10 +53,14 @@ describe "Enumerable#each_slice" do it "returns an enumerator if no block" do e = @enum.each_slice(3) - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == @sliced end + it "returns self when a block is given" do + @enum.each_slice(3){}.should == @enum + end + it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMulti.new multi.each_slice(2).to_a.should == [[[1, 2], [3, 4, 5]], [[6, 7, 8, 9]]] @@ -65,7 +69,7 @@ describe "Enumerable#each_slice" do describe "when no block is given" do it "returns an enumerator" do e = @enum.each_slice(3) - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == @sliced end diff --git a/spec/ruby/core/enumerable/each_with_index_spec.rb b/spec/ruby/core/enumerable/each_with_index_spec.rb index 122e88eab7..fcb2f82f84 100644 --- a/spec/ruby/core/enumerable/each_with_index_spec.rb +++ b/spec/ruby/core/enumerable/each_with_index_spec.rb @@ -26,19 +26,19 @@ describe "Enumerable#each_with_index" do acc = [] res = @b.each_with_index {|a,i| acc << [a,i]} [[2, 0], [5, 1], [3, 2], [6, 3], [1, 4], [4, 5]].should == acc - res.should eql(@b) + res.should.eql?(@b) end it "binds splat arguments properly" do acc = [] res = @b.each_with_index { |*b| c,d = b; acc << c; acc << d } [2, 0, 5, 1, 3, 2, 6, 3, 1, 4, 4, 5].should == acc - res.should eql(@b) + res.should.eql?(@b) end it "returns an enumerator if no block" do e = @b.each_with_index - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == [[2, 0], [5, 1], [3, 2], [6, 3], [1, 4], [4, 5]] end diff --git a/spec/ruby/core/enumerable/each_with_object_spec.rb b/spec/ruby/core/enumerable/each_with_object_spec.rb index 35665e7019..1760d3b267 100644 --- a/spec/ruby/core/enumerable/each_with_object_spec.rb +++ b/spec/ruby/core/enumerable/each_with_object_spec.rb @@ -12,10 +12,10 @@ describe "Enumerable#each_with_object" do it "passes each element and its argument to the block" do acc = [] @enum.each_with_object(@initial) do |elem, obj| - obj.should equal(@initial) + obj.should.equal?(@initial) obj = 42 acc << elem - end.should equal(@initial) + end.should.equal?(@initial) acc.should == @values end @@ -23,10 +23,10 @@ describe "Enumerable#each_with_object" do acc = [] e = @enum.each_with_object(@initial) e.each do |elem, obj| - obj.should equal(@initial) + obj.should.equal?(@initial) obj = 42 acc << elem - end.should equal(@initial) + end.should.equal?(@initial) acc.should == @values end diff --git a/spec/ruby/core/enumerable/entries_spec.rb b/spec/ruby/core/enumerable/entries_spec.rb index 83232cfa06..2de4fc756a 100644 --- a/spec/ruby/core/enumerable/entries_spec.rb +++ b/spec/ruby/core/enumerable/entries_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/entries' describe "Enumerable#entries" do - it_behaves_like :enumerable_entries , :entries + it_behaves_like :enumerable_entries, :entries end diff --git a/spec/ruby/core/enumerable/filter_map_spec.rb b/spec/ruby/core/enumerable/filter_map_spec.rb index 31acc277b4..1ed131a960 100644 --- a/spec/ruby/core/enumerable/filter_map_spec.rb +++ b/spec/ruby/core/enumerable/filter_map_spec.rb @@ -1,26 +1,24 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -ruby_version_is '2.7' do - describe 'Enumerable#filter_map' do - before :each do - @numerous = EnumerableSpecs::Numerous.new(*(1..8).to_a) - end +describe 'Enumerable#filter_map' do + before :each do + @numerous = EnumerableSpecs::Numerous.new(*(1..8).to_a) + end - it 'returns an empty array if there are no elements' do - EnumerableSpecs::Empty.new.filter_map { true }.should == [] - end + it 'returns an empty array if there are no elements' do + EnumerableSpecs::Empty.new.filter_map { true }.should == [] + end - it 'returns an array with truthy results of passing each element to block' do - @numerous.filter_map { |i| i * 2 if i.even? }.should == [4, 8, 12, 16] - @numerous.filter_map { |i| i * 2 }.should == [2, 4, 6, 8, 10, 12, 14, 16] - @numerous.filter_map { 0 }.should == [0, 0, 0, 0, 0, 0, 0, 0] - @numerous.filter_map { false }.should == [] - @numerous.filter_map { nil }.should == [] - end + it 'returns an array with truthy results of passing each element to block' do + @numerous.filter_map { |i| i * 2 if i.even? }.should == [4, 8, 12, 16] + @numerous.filter_map { |i| i * 2 }.should == [2, 4, 6, 8, 10, 12, 14, 16] + @numerous.filter_map { 0 }.should == [0, 0, 0, 0, 0, 0, 0, 0] + @numerous.filter_map { false }.should == [] + @numerous.filter_map { nil }.should == [] + end - it 'returns an enumerator when no block given' do - @numerous.filter_map.should be_an_instance_of(Enumerator) - end + it 'returns an enumerator when no block given' do + @numerous.filter_map.should.instance_of?(Enumerator) end end diff --git a/spec/ruby/core/enumerable/filter_spec.rb b/spec/ruby/core/enumerable/filter_spec.rb index f2dc7a7b71..1c3a7e9ff5 100644 --- a/spec/ruby/core/enumerable/filter_spec.rb +++ b/spec/ruby/core/enumerable/filter_spec.rb @@ -2,8 +2,6 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' require_relative 'shared/find_all' -ruby_version_is "2.6" do - describe "Enumerable#filter" do - it_behaves_like(:enumerable_find_all , :filter) - end +describe "Enumerable#filter" do + it_behaves_like :enumerable_find_all, :filter end diff --git a/spec/ruby/core/enumerable/find_all_spec.rb b/spec/ruby/core/enumerable/find_all_spec.rb index ce9058fe77..9cd635f247 100644 --- a/spec/ruby/core/enumerable/find_all_spec.rb +++ b/spec/ruby/core/enumerable/find_all_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/find_all' describe "Enumerable#find_all" do - it_behaves_like :enumerable_find_all , :find_all + it_behaves_like :enumerable_find_all, :find_all end diff --git a/spec/ruby/core/enumerable/find_index_spec.rb b/spec/ruby/core/enumerable/find_index_spec.rb index 542660fe04..2e714367ba 100644 --- a/spec/ruby/core/enumerable/find_index_spec.rb +++ b/spec/ruby/core/enumerable/find_index_spec.rb @@ -47,7 +47,7 @@ describe "Enumerable#find_index" do end it "returns an Enumerator if no block given" do - @numerous.find_index.should be_an_instance_of(Enumerator) + @numerous.find_index.should.instance_of?(Enumerator) end it "uses #== for testing equality" do diff --git a/spec/ruby/core/enumerable/find_spec.rb b/spec/ruby/core/enumerable/find_spec.rb index 25aa3bf103..5ddebc05f8 100644 --- a/spec/ruby/core/enumerable/find_spec.rb +++ b/spec/ruby/core/enumerable/find_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/find' describe "Enumerable#find" do - it_behaves_like :enumerable_find , :find + it_behaves_like :enumerable_find, :find end diff --git a/spec/ruby/core/enumerable/first_spec.rb b/spec/ruby/core/enumerable/first_spec.rb index ed1ba599b4..592dff1ebc 100644 --- a/spec/ruby/core/enumerable/first_spec.rb +++ b/spec/ruby/core/enumerable/first_spec.rb @@ -19,7 +19,7 @@ describe "Enumerable#first" do it "raises a RangeError when passed a Bignum" do enum = EnumerableSpecs::Empty.new - -> { enum.first(bignum_value) }.should raise_error(RangeError) + -> { enum.first(bignum_value) }.should.raise(RangeError) end describe "when passed an argument" do diff --git a/spec/ruby/core/enumerable/fixtures/classes.rb b/spec/ruby/core/enumerable/fixtures/classes.rb index 5051196742..b5feafcfb7 100644 --- a/spec/ruby/core/enumerable/fixtures/classes.rb +++ b/spec/ruby/core/enumerable/fixtures/classes.rb @@ -38,12 +38,14 @@ module EnumerableSpecs class Empty include Enumerable def each + self end end class EmptyWithSize include Enumerable def each + self end def size 0 @@ -251,7 +253,7 @@ module EnumerableSpecs end end - class ComparableWithFixnum + class ComparableWithInteger include Comparable def initialize(num) @num = num @@ -342,4 +344,7 @@ module EnumerableSpecs @block.call(*args) end end + + class SetSubclass < Set + end end # EnumerableSpecs utility classes diff --git a/spec/ruby/core/enumerable/flat_map_spec.rb b/spec/ruby/core/enumerable/flat_map_spec.rb index a294b9ddad..bd07eab6c5 100644 --- a/spec/ruby/core/enumerable/flat_map_spec.rb +++ b/spec/ruby/core/enumerable/flat_map_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/collect_concat' describe "Enumerable#flat_map" do - it_behaves_like :enumerable_collect_concat , :flat_map + it_behaves_like :enumerable_collect_concat, :flat_map end diff --git a/spec/ruby/core/enumerable/grep_spec.rb b/spec/ruby/core/enumerable/grep_spec.rb index c9c0f34e27..965e183766 100644 --- a/spec/ruby/core/enumerable/grep_spec.rb +++ b/spec/ruby/core/enumerable/grep_spec.rb @@ -40,15 +40,28 @@ describe "Enumerable#grep" do $~.should == nil end - it "sets $~ to the last match when given no block" do + it "does not set $~ when given no block" do "z" =~ /z/ # Reset $~ ["abc", "def"].grep(/b/).should == ["abc"] + $&.should == "z" + end - # Set by the failed match of "def" - $~.should == nil + it "does not modify Regexp.last_match without block" do + "z" =~ /z/ # Reset last match + ["abc", "def"].grep(/b/).should == ["abc"] + Regexp.last_match[0].should == "z" + end + + it "correctly handles non-string elements" do + 'set last match' =~ /set last (.*)/ + [:a, 'b', 'z', :c, 42, nil].grep(/[a-d]/).should == [:a, 'b', :c] + $1.should == 'match' - ["abc", "def"].grep(/e/) - $&.should == "e" + o = Object.new + def o.to_str + 'hello' + end + [o].grep(/ll/).first.should.equal?(o) end describe "with a block" do @@ -68,7 +81,7 @@ describe "Enumerable#grep" do end it "raises an ArgumentError when not given a pattern" do - -> { @numerous.grep { |e| e } }.should raise_error(ArgumentError) + -> { @numerous.grep { |e| e } }.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/enumerable/grep_v_spec.rb b/spec/ruby/core/enumerable/grep_v_spec.rb index 6dec487065..ee99a77ac1 100644 --- a/spec/ruby/core/enumerable/grep_v_spec.rb +++ b/spec/ruby/core/enumerable/grep_v_spec.rb @@ -20,15 +20,28 @@ describe "Enumerable#grep_v" do $&.should == "e" end - it "sets $~ to the last match when given no block" do + it "does not set $~ when given no block" do "z" =~ /z/ # Reset $~ ["abc", "def"].grep_v(/e/).should == ["abc"] + $&.should == "z" + end - # Set by the match of "def" - $&.should == "e" + it "does not modify Regexp.last_match without block" do + "z" =~ /z/ # Reset last match + ["abc", "def"].grep_v(/e/).should == ["abc"] + Regexp.last_match[0].should == "z" + end + + it "correctly handles non-string elements" do + 'set last match' =~ /set last (.*)/ + [:a, 'b', 'z', :c, 42, nil].grep_v(/[a-d]/).should == ['z', 42, nil] + $1.should == 'match' - ["abc", "def"].grep_v(/b/) - $&.should == nil + o = Object.new + def o.to_str + 'hello' + end + [o].grep_v(/mm/).first.should.equal?(o) end describe "without block" do @@ -42,7 +55,7 @@ describe "Enumerable#grep_v" do end it "raises an ArgumentError when not given a pattern" do - -> { @numerous.grep_v }.should raise_error(ArgumentError) + -> { @numerous.grep_v }.should.raise(ArgumentError) end end @@ -57,7 +70,7 @@ describe "Enumerable#grep_v" do end it "raises an ArgumentError when not given a pattern" do - -> { @numerous.grep_v { |e| e } }.should raise_error(ArgumentError) + -> { @numerous.grep_v { |e| e } }.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/enumerable/group_by_spec.rb b/spec/ruby/core/enumerable/group_by_spec.rb index 52b5a68d64..904e5d6c68 100644 --- a/spec/ruby/core/enumerable/group_by_spec.rb +++ b/spec/ruby/core/enumerable/group_by_spec.rb @@ -16,13 +16,13 @@ describe "Enumerable#group_by" do it "returns a hash without default_proc" do e = EnumerableSpecs::Numerous.new("foo", "bar", "baz") h = e.group_by { |word| word[0..0].to_sym } - h[:some].should be_nil - h.default_proc.should be_nil - h.default.should be_nil + h[:some].should == nil + h.default_proc.should == nil + h.default.should == nil end it "returns an Enumerator if called without a block" do - EnumerableSpecs::Numerous.new.group_by.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new.group_by.should.instance_of?(Enumerator) end it "gathers whole arrays as elements when each yields multiple" do @@ -33,15 +33,5 @@ describe "Enumerable#group_by" do [3, 4, 5] => [[3, 4, 5]] } end - ruby_version_is ''...'2.7' do - it "returns a tainted hash if self is tainted" do - EnumerableSpecs::Empty.new.taint.group_by {}.tainted?.should be_true - end - - it "returns an untrusted hash if self is untrusted" do - EnumerableSpecs::Empty.new.untrust.group_by {}.untrusted?.should be_true - end - end - it_behaves_like :enumerable_enumeratorized_with_origin_size, :group_by end diff --git a/spec/ruby/core/enumerable/lazy_spec.rb b/spec/ruby/core/enumerable/lazy_spec.rb index 9a9ead81a0..935e574067 100644 --- a/spec/ruby/core/enumerable/lazy_spec.rb +++ b/spec/ruby/core/enumerable/lazy_spec.rb @@ -5,6 +5,6 @@ require_relative 'fixtures/classes' describe "Enumerable#lazy" do it "returns an instance of Enumerator::Lazy" do - EnumerableSpecs::Numerous.new.lazy.should be_an_instance_of(Enumerator::Lazy) + EnumerableSpecs::Numerous.new.lazy.should.instance_of?(Enumerator::Lazy) end end diff --git a/spec/ruby/core/enumerable/map_spec.rb b/spec/ruby/core/enumerable/map_spec.rb index d65aec238c..98a70781cf 100644 --- a/spec/ruby/core/enumerable/map_spec.rb +++ b/spec/ruby/core/enumerable/map_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/collect' describe "Enumerable#map" do - it_behaves_like :enumerable_collect , :map + it_behaves_like :enumerable_collect, :map end diff --git a/spec/ruby/core/enumerable/max_by_spec.rb b/spec/ruby/core/enumerable/max_by_spec.rb index ec1738ea3b..f67b5d15ea 100644 --- a/spec/ruby/core/enumerable/max_by_spec.rb +++ b/spec/ruby/core/enumerable/max_by_spec.rb @@ -4,7 +4,7 @@ require_relative 'shared/enumerable_enumeratorized' describe "Enumerable#max_by" do it "returns an enumerator if no block" do - EnumerableSpecs::Numerous.new(42).max_by.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new(42).max_by.should.instance_of?(Enumerator) end it "returns nil if #each yields no objects" do @@ -18,7 +18,7 @@ describe "Enumerable#max_by" do it "returns the object that appears first in #each in case of a tie" do a, b, c = '1', '2', '2' - EnumerableSpecs::Numerous.new(a, b, c).max_by {|obj| obj.to_i }.should equal(b) + EnumerableSpecs::Numerous.new(a, b, c).max_by {|obj| obj.to_i }.should.equal?(b) end it "uses max.<=>(current) to determine order" do @@ -48,7 +48,7 @@ describe "Enumerable#max_by" do context "without a block" do it "returns an enumerator" do - @enum.max_by(2).should be_an_instance_of(Enumerator) + @enum.max_by(2).should.instance_of?(Enumerator) end end @@ -67,7 +67,7 @@ describe "Enumerable#max_by" do context "when n is negative" do it "raises an ArgumentError" do - -> { @enum.max_by(-1) { |i| i.to_s } }.should raise_error(ArgumentError) + -> { @enum.max_by(-1) { |i| i.to_s } }.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/enumerable/max_spec.rb b/spec/ruby/core/enumerable/max_spec.rb index 0c11ca0969..d92700258b 100644 --- a/spec/ruby/core/enumerable/max_spec.rb +++ b/spec/ruby/core/enumerable/max_spec.rb @@ -38,16 +38,16 @@ describe "Enumerable#max" do it "raises a NoMethodError for elements without #<=>" do -> do EnumerableSpecs::EachDefiner.new(BasicObject.new, BasicObject.new).max - end.should raise_error(NoMethodError) + end.should.raise(NoMethodError) end it "raises an ArgumentError for incomparable elements" do -> do EnumerableSpecs::EachDefiner.new(11,"22").max - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) -> do EnumerableSpecs::EachDefiner.new(11,12,22,33).max{|a, b| nil} - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) end context "when passed a block" do @@ -106,7 +106,7 @@ describe "Enumerable#max" do context "that is negative" do it "raises an ArgumentError" do - -> { @e_ints.max(-1) }.should raise_error(ArgumentError) + -> { @e_ints.max(-1) }.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/enumerable/min_by_spec.rb b/spec/ruby/core/enumerable/min_by_spec.rb index 3ff87e49d8..4f949e2130 100644 --- a/spec/ruby/core/enumerable/min_by_spec.rb +++ b/spec/ruby/core/enumerable/min_by_spec.rb @@ -4,7 +4,7 @@ require_relative 'shared/enumerable_enumeratorized' describe "Enumerable#min_by" do it "returns an enumerator if no block" do - EnumerableSpecs::Numerous.new(42).min_by.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new(42).min_by.should.instance_of?(Enumerator) end it "returns nil if #each yields no objects" do @@ -18,7 +18,7 @@ describe "Enumerable#min_by" do it "returns the object that appears first in #each in case of a tie" do a, b, c = '2', '1', '1' - EnumerableSpecs::Numerous.new(a, b, c).min_by {|obj| obj.to_i }.should equal(b) + EnumerableSpecs::Numerous.new(a, b, c).min_by {|obj| obj.to_i }.should.equal?(b) end it "uses min.<=>(current) to determine order" do @@ -48,7 +48,7 @@ describe "Enumerable#min_by" do context "without a block" do it "returns an enumerator" do - @enum.min_by(2).should be_an_instance_of(Enumerator) + @enum.min_by(2).should.instance_of?(Enumerator) end end @@ -67,7 +67,7 @@ describe "Enumerable#min_by" do context "when n is negative" do it "raises an ArgumentError" do - -> { @enum.min_by(-1) { |i| i.to_s } }.should raise_error(ArgumentError) + -> { @enum.min_by(-1) { |i| i.to_s } }.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/enumerable/min_spec.rb b/spec/ruby/core/enumerable/min_spec.rb index 4b6ae248fa..f05d59c2c9 100644 --- a/spec/ruby/core/enumerable/min_spec.rb +++ b/spec/ruby/core/enumerable/min_spec.rb @@ -32,22 +32,22 @@ describe "Enumerable#min" do end it "returns nil for an empty Enumerable" do - EnumerableSpecs::EachDefiner.new.min.should be_nil + EnumerableSpecs::EachDefiner.new.min.should == nil end it "raises a NoMethodError for elements without #<=>" do -> do EnumerableSpecs::EachDefiner.new(BasicObject.new, BasicObject.new).min - end.should raise_error(NoMethodError) + end.should.raise(NoMethodError) end it "raises an ArgumentError for incomparable elements" do -> do EnumerableSpecs::EachDefiner.new(11,"22").min - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) -> do EnumerableSpecs::EachDefiner.new(11,12,22,33).min{|a, b| nil} - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) end it "returns the minimum when using a block rule" do @@ -110,7 +110,7 @@ describe "Enumerable#min" do context "that is negative" do it "raises an ArgumentError" do - -> { @e_ints.min(-1) }.should raise_error(ArgumentError) + -> { @e_ints.min(-1) }.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/enumerable/minmax_by_spec.rb b/spec/ruby/core/enumerable/minmax_by_spec.rb index a6a9249270..30c88cfcfb 100644 --- a/spec/ruby/core/enumerable/minmax_by_spec.rb +++ b/spec/ruby/core/enumerable/minmax_by_spec.rb @@ -4,7 +4,7 @@ require_relative 'shared/enumerable_enumeratorized' describe "Enumerable#minmax_by" do it "returns an enumerator if no block" do - EnumerableSpecs::Numerous.new(42).minmax_by.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new(42).minmax_by.should.instance_of?(Enumerator) end it "returns nil if #each yields no objects" do @@ -19,8 +19,8 @@ describe "Enumerable#minmax_by" do it "returns the object that appears first in #each in case of a tie" do a, b, c, d = '1', '1', '2', '2' mm = EnumerableSpecs::Numerous.new(a, b, c, d).minmax_by {|obj| obj.to_i } - mm[0].should equal(a) - mm[1].should equal(c) + mm[0].should.equal?(a) + mm[1].should.equal?(c) end it "uses min/max.<=>(current) to determine order" do diff --git a/spec/ruby/core/enumerable/minmax_spec.rb b/spec/ruby/core/enumerable/minmax_spec.rb index 29f1ecf82c..f5f17ef079 100644 --- a/spec/ruby/core/enumerable/minmax_spec.rb +++ b/spec/ruby/core/enumerable/minmax_spec.rb @@ -1,41 +1,17 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative '../../shared/enumerable/minmax' describe "Enumerable#minmax" do before :each do @enum = EnumerableSpecs::Numerous.new(6, 4, 5, 10, 8) - + @empty_enum = EnumerableSpecs::Empty.new + @incomparable_enum = EnumerableSpecs::Numerous.new(BasicObject.new, BasicObject.new) + @incompatible_enum = EnumerableSpecs::Numerous.new(11,"22") @strs = EnumerableSpecs::Numerous.new("333", "2", "60", "55555", "1010", "111") end - it "min should return the minimum element" do - @enum.minmax.should == [4, 10] - @strs.minmax.should == ["1010", "60" ] - end - - it "returns [nil, nil] for an empty Enumerable" do - EnumerableSpecs::Empty.new.minmax.should == [nil, nil] - end - - it "raises an ArgumentError when elements are incomparable" do - -> do - EnumerableSpecs::Numerous.new(11,"22").minmax - end.should raise_error(ArgumentError) - -> do - EnumerableSpecs::Numerous.new(11,12,22,33).minmax{|a, b| nil} - end.should raise_error(ArgumentError) - end - - it "raises a NoMethodError for elements without #<=>" do - -> do - EnumerableSpecs::Numerous.new(BasicObject.new, BasicObject.new).minmax - end.should raise_error(NoMethodError) - end - - it "returns the minimum when using a block rule" do - @enum.minmax {|a,b| b <=> a }.should == [10, 4] - @strs.minmax {|a,b| a.length <=> b.length }.should == ["2", "55555"] - end + it_behaves_like :enumerable_minmax, :minmax it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMulti.new diff --git a/spec/ruby/core/enumerable/none_spec.rb b/spec/ruby/core/enumerable/none_spec.rb index 7c17e58a13..d9ee0b441e 100644 --- a/spec/ruby/core/enumerable/none_spec.rb +++ b/spec/ruby/core/enumerable/none_spec.rb @@ -10,49 +10,40 @@ describe "Enumerable#none?" do end it "always returns true on empty enumeration" do - @empty.none?.should == true + @empty.should.none? @empty.none? { true }.should == true end it "raises an ArgumentError when more than 1 argument is provided" do - -> { @enum.none?(1, 2, 3) }.should raise_error(ArgumentError) - -> { [].none?(1, 2, 3) }.should raise_error(ArgumentError) - -> { {}.none?(1, 2, 3) }.should raise_error(ArgumentError) - end - - ruby_version_is ""..."2.5" do - it "raises an ArgumentError when any arguments provided" do - -> { @enum.none?(Proc.new {}) }.should raise_error(ArgumentError) - -> { @enum.none?(nil) }.should raise_error(ArgumentError) - -> { @empty.none?(1) }.should raise_error(ArgumentError) - -> { @enum.none?(1) {} }.should raise_error(ArgumentError) - end + -> { @enum.none?(1, 2, 3) }.should.raise(ArgumentError) + -> { [].none?(1, 2, 3) }.should.raise(ArgumentError) + -> { {}.none?(1, 2, 3) }.should.raise(ArgumentError) end it "does not hide exceptions out of #each" do -> { EnumerableSpecs::ThrowingEach.new.none? - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) -> { EnumerableSpecs::ThrowingEach.new.none? { false } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end describe "with no block" do it "returns true if none of the elements in self are true" do e = EnumerableSpecs::Numerous.new(false, nil, false) - e.none?.should be_true + e.none?.should == true end it "returns false if at least one of the elements in self are true" do e = EnumerableSpecs::Numerous.new(false, nil, true, false) - e.none?.should be_false + e.none?.should == false end it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMultiWithFalse.new - multi.none?.should be_false + multi.none?.should == false end end @@ -74,17 +65,17 @@ describe "Enumerable#none?" do end it "returns true if the block never returns true" do - @e.none? {|e| false }.should be_true + @e.none? {|e| false }.should == true end it "returns false if the block ever returns true" do - @e.none? {|e| e == 3 ? true : false }.should be_false + @e.none? {|e| e == 3 ? true : false }.should == false end it "does not hide exceptions out of the block" do -> { @enum.none? { raise "from block" } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "gathers initial args as elements when each yields multiple" do @@ -102,66 +93,61 @@ describe "Enumerable#none?" do end end - ruby_version_is "2.5" do - describe 'when given a pattern argument' do - it "calls `===` on the pattern the return value " do - pattern = EnumerableSpecs::Pattern.new { |x| x == 3 } - @enum1.none?(pattern).should == true - pattern.yielded.should == [[0], [1], [2], [-1]] - end - - # may raise an exception in future versions - ruby_version_is ""..."2.6" do - it "ignores block" do - @enum2.none?(Integer) { raise }.should == true - [1, 2, nil].none?(TrueClass) { raise }.should == true - {a: 1}.none?(Hash) { raise }.should == true - end - end - - it "always returns true on empty enumeration" do - @empty.none?(Integer).should == true - [].none?(Integer).should == true - {}.none?(NilClass).should == true - end - - it "does not hide exceptions out of #each" do - -> { - EnumerableSpecs::ThrowingEach.new.none?(Integer) - }.should raise_error(RuntimeError) - end - - it "returns true if the pattern never returns a truthy value" do - @enum2.none?(Integer).should == true - pattern = EnumerableSpecs::Pattern.new { |x| nil } - @enum.none?(pattern).should == true - - [1, 42, 3].none?(pattern).should == true - {a: 1, b: 2}.none?(pattern).should == true - end - - it "returns false if the pattern ever returns other than false or nil" do - pattern = EnumerableSpecs::Pattern.new { |x| x < 0 } - @enum1.none?(pattern).should == false - pattern.yielded.should == [[0], [1], [2], [-1]] - - [1, 2, 3, -1].none?(pattern).should == false - {a: 1}.none?(Array).should == false - end - - it "does not hide exceptions out of pattern#===" do - pattern = EnumerableSpecs::Pattern.new { raise "from pattern" } - -> { - @enum.none?(pattern) - }.should raise_error(RuntimeError) - end - - it "calls the pattern with gathered array when yielded with multiple arguments" do - multi = EnumerableSpecs::YieldsMulti.new - pattern = EnumerableSpecs::Pattern.new { false } - multi.none?(pattern).should == true - pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]] - end + describe 'when given a pattern argument' do + it "calls `===` on the pattern the return value" do + pattern = EnumerableSpecs::Pattern.new { |x| x == 3 } + @enum1.none?(pattern).should == true + pattern.yielded.should == [[0], [1], [2], [-1]] + end + + it "always returns true on empty enumeration" do + @empty.none?(Integer).should == true + [].none?(Integer).should == true + {}.none?(NilClass).should == true + end + + it "does not hide exceptions out of #each" do + -> { + EnumerableSpecs::ThrowingEach.new.none?(Integer) + }.should.raise(RuntimeError) + end + + it "returns true if the pattern never returns a truthy value" do + @enum2.none?(Integer).should == true + pattern = EnumerableSpecs::Pattern.new { |x| nil } + @enum.none?(pattern).should == true + + [1, 42, 3].none?(pattern).should == true + {a: 1, b: 2}.none?(pattern).should == true + end + + it "returns false if the pattern ever returns other than false or nil" do + pattern = EnumerableSpecs::Pattern.new { |x| x < 0 } + @enum1.none?(pattern).should == false + pattern.yielded.should == [[0], [1], [2], [-1]] + + [1, 2, 3, -1].none?(pattern).should == false + {a: 1}.none?(Array).should == false + end + + it "does not hide exceptions out of pattern#===" do + pattern = EnumerableSpecs::Pattern.new { raise "from pattern" } + -> { + @enum.none?(pattern) + }.should.raise(RuntimeError) + end + + it "calls the pattern with gathered array when yielded with multiple arguments" do + multi = EnumerableSpecs::YieldsMulti.new + pattern = EnumerableSpecs::Pattern.new { false } + multi.none?(pattern).should == true + pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]] + end + + it "ignores the block if there is an argument" do + -> { + EnumerableSpecs::Numerous.new(1, 2, 3, 4, 5).none?(String) { true }.should == true + }.should complain(/given block not used/) end end end diff --git a/spec/ruby/core/enumerable/one_spec.rb b/spec/ruby/core/enumerable/one_spec.rb index 2ae8b3efa4..cf966d4a9b 100644 --- a/spec/ruby/core/enumerable/one_spec.rb +++ b/spec/ruby/core/enumerable/one_spec.rb @@ -10,71 +10,62 @@ describe "Enumerable#one?" do end it "always returns false on empty enumeration" do - @empty.one?.should == false + @empty.should_not.one? @empty.one? { true }.should == false end it "raises an ArgumentError when more than 1 argument is provided" do - -> { @enum.one?(1, 2, 3) }.should raise_error(ArgumentError) - -> { [].one?(1, 2, 3) }.should raise_error(ArgumentError) - -> { {}.one?(1, 2, 3) }.should raise_error(ArgumentError) - end - - ruby_version_is ""..."2.5" do - it "raises an ArgumentError when any arguments provided" do - -> { @enum.one?(Proc.new {}) }.should raise_error(ArgumentError) - -> { @enum.one?(nil) }.should raise_error(ArgumentError) - -> { @empty.one?(1) }.should raise_error(ArgumentError) - -> { @enum.one?(1) {} }.should raise_error(ArgumentError) - end + -> { @enum.one?(1, 2, 3) }.should.raise(ArgumentError) + -> { [].one?(1, 2, 3) }.should.raise(ArgumentError) + -> { {}.one?(1, 2, 3) }.should.raise(ArgumentError) end it "does not hide exceptions out of #each" do -> { EnumerableSpecs::ThrowingEach.new.one? - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) -> { EnumerableSpecs::ThrowingEach.new.one? { false } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end describe "with no block" do it "returns true if only one element evaluates to true" do - [false, nil, true].one?.should be_true + [false, nil, true].one?.should == true end it "returns false if two elements evaluate to true" do - [false, :value, nil, true].one?.should be_false + [false, :value, nil, true].one?.should == false end it "returns false if all elements evaluate to false" do - [false, nil, false].one?.should be_false + [false, nil, false].one?.should == false end it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMultiWithSingleTrue.new - multi.one?.should be_false + multi.one?.should == false end end describe "with a block" do it "returns true if block returns true once" do - [:a, :b, :c].one? { |s| s == :a }.should be_true + [:a, :b, :c].one? { |s| s == :a }.should == true end it "returns false if the block returns true more than once" do - [:a, :b, :c].one? { |s| s == :a || s == :b }.should be_false + [:a, :b, :c].one? { |s| s == :a || s == :b }.should == false end it "returns false if the block only returns false" do - [:a, :b, :c].one? { |s| s == :d }.should be_false + [:a, :b, :c].one? { |s| s == :d }.should == false end it "does not hide exceptions out of the block" do -> { @enum.one? { raise "from block" } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "gathers initial args as elements when each yields multiple" do @@ -92,78 +83,72 @@ describe "Enumerable#one?" do end end + describe 'when given a pattern argument' do + it "calls `===` on the pattern the return value" do + pattern = EnumerableSpecs::Pattern.new { |x| x == 1 } + @enum1.one?(pattern).should == true + pattern.yielded.should == [[0], [1], [2], [-1]] + end + + it "always returns false on empty enumeration" do + @empty.one?(Integer).should == false + [].one?(Integer).should == false + {}.one?(NilClass).should == false + end + + it "does not hide exceptions out of #each" do + -> { + EnumerableSpecs::ThrowingEach.new.one?(Integer) + }.should.raise(RuntimeError) + end + + it "returns true if the pattern returns a truthy value only once" do + @enum2.one?(NilClass).should == true + pattern = EnumerableSpecs::Pattern.new { |x| x == 2 } + @enum1.one?(pattern).should == true + + [1, 2, 42, 3].one?(pattern).should == true + + pattern = EnumerableSpecs::Pattern.new { |x| x == [:b, 2] } + {a: 1, b: 2}.one?(pattern).should == true + end + + it "returns false if the pattern returns a truthy value more than once" do + pattern = EnumerableSpecs::Pattern.new { |x| !x } + @enum2.one?(pattern).should == false + pattern.yielded.should == [[nil], [false]] + + [1, 2, 3].one?(Integer).should == false + {a: 1, b: 2}.one?(Array).should == false + end + + it "returns false if the pattern never returns a truthy value" do + pattern = EnumerableSpecs::Pattern.new { |x| nil } + @enum1.one?(pattern).should == false + pattern.yielded.should == [[0], [1], [2], [-1]] - ruby_version_is "2.5" do - describe 'when given a pattern argument' do - it "calls `===` on the pattern the return value " do - pattern = EnumerableSpecs::Pattern.new { |x| x == 1 } - @enum1.one?(pattern).should == true - pattern.yielded.should == [[0], [1], [2], [-1]] - end - - # may raise an exception in future versions - ruby_version_is ""..."2.6" do - it "ignores block" do - @enum2.one?(NilClass) { raise }.should == true - [1, 2, nil].one?(NilClass) { raise }.should == true - {a: 1}.one?(Array) { raise }.should == true - end - end - - it "always returns false on empty enumeration" do - @empty.one?(Integer).should == false - [].one?(Integer).should == false - {}.one?(NilClass).should == false - end - - it "does not hide exceptions out of #each" do - -> { - EnumerableSpecs::ThrowingEach.new.one?(Integer) - }.should raise_error(RuntimeError) - end - - it "returns true if the pattern returns a truthy value only once" do - @enum2.one?(NilClass).should == true - pattern = EnumerableSpecs::Pattern.new { |x| x == 2 } - @enum1.one?(pattern).should == true - - [1, 2, 42, 3].one?(pattern).should == true - - pattern = EnumerableSpecs::Pattern.new { |x| x == [:b, 2] } - {a: 1, b: 2}.one?(pattern).should == true - end - - it "returns false if the pattern returns a truthy value more than once" do - pattern = EnumerableSpecs::Pattern.new { |x| !x } - @enum2.one?(pattern).should == false - pattern.yielded.should == [[nil], [false]] - - [1, 2, 3].one?(Integer).should == false - {a: 1, b: 2}.one?(Array).should == false - end - - it "returns false if the pattern never returns a truthy value" do - pattern = EnumerableSpecs::Pattern.new { |x| nil } - @enum1.one?(pattern).should == false - pattern.yielded.should == [[0], [1], [2], [-1]] - - [1, 2, 3].one?(pattern).should == false - {a: 1}.one?(pattern).should == false - end - - it "does not hide exceptions out of pattern#===" do - pattern = EnumerableSpecs::Pattern.new { raise "from pattern" } - -> { - @enum.one?(pattern) - }.should raise_error(RuntimeError) - end - - it "calls the pattern with gathered array when yielded with multiple arguments" do - multi = EnumerableSpecs::YieldsMulti.new - pattern = EnumerableSpecs::Pattern.new { false } - multi.one?(pattern).should == false - pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]] - end + [1, 2, 3].one?(pattern).should == false + {a: 1}.one?(pattern).should == false + end + + it "does not hide exceptions out of pattern#===" do + pattern = EnumerableSpecs::Pattern.new { raise "from pattern" } + -> { + @enum.one?(pattern) + }.should.raise(RuntimeError) + end + + it "calls the pattern with gathered array when yielded with multiple arguments" do + multi = EnumerableSpecs::YieldsMulti.new + pattern = EnumerableSpecs::Pattern.new { false } + multi.one?(pattern).should == false + pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]] + end + + it "ignores the block if there is an argument" do + -> { + EnumerableSpecs::Numerous.new(1, 2, 3, 4, "5").one?(String) { false }.should == true + }.should complain(/given block not used/) end end end diff --git a/spec/ruby/core/enumerable/partition_spec.rb b/spec/ruby/core/enumerable/partition_spec.rb index d3d220b4b4..8272ee189e 100644 --- a/spec/ruby/core/enumerable/partition_spec.rb +++ b/spec/ruby/core/enumerable/partition_spec.rb @@ -8,7 +8,7 @@ describe "Enumerable#partition" do end it "returns an Enumerator if called without a block" do - EnumerableSpecs::Numerous.new.partition.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new.partition.should.instance_of?(Enumerator) end it "gathers whole arrays as elements when each yields multiple" do diff --git a/spec/ruby/core/enumerable/reject_spec.rb b/spec/ruby/core/enumerable/reject_spec.rb index 0d86b49ea2..31e89f5b0e 100644 --- a/spec/ruby/core/enumerable/reject_spec.rb +++ b/spec/ruby/core/enumerable/reject_spec.rb @@ -13,7 +13,7 @@ describe "Enumerable#reject" do end it "returns an Enumerator if called without a block" do - EnumerableSpecs::Numerous.new.reject.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new.reject.should.instance_of?(Enumerator) end it "gathers whole arrays as elements when each yields multiple" do diff --git a/spec/ruby/core/enumerable/reverse_each_spec.rb b/spec/ruby/core/enumerable/reverse_each_spec.rb index 2b1c233488..4753956724 100644 --- a/spec/ruby/core/enumerable/reverse_each_spec.rb +++ b/spec/ruby/core/enumerable/reverse_each_spec.rb @@ -11,7 +11,7 @@ describe "Enumerable#reverse_each" do it "returns an Enumerator if no block given" do enum = EnumerableSpecs::Numerous.new.reverse_each - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.to_a.should == [4, 1, 6, 3, 5, 2] end diff --git a/spec/ruby/core/enumerable/select_spec.rb b/spec/ruby/core/enumerable/select_spec.rb index 11168eb42e..7fc61926f9 100644 --- a/spec/ruby/core/enumerable/select_spec.rb +++ b/spec/ruby/core/enumerable/select_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/find_all' describe "Enumerable#select" do - it_behaves_like :enumerable_find_all , :select + it_behaves_like :enumerable_find_all, :select end diff --git a/spec/ruby/core/enumerable/shared/collect.rb b/spec/ruby/core/enumerable/shared/collect.rb index 05e94777c7..4696d32454 100644 --- a/spec/ruby/core/enumerable/shared/collect.rb +++ b/spec/ruby/core/enumerable/shared/collect.rb @@ -30,9 +30,78 @@ describe :enumerable_collect, shared: true do it "returns an enumerator when no block given" do enum = EnumerableSpecs::Numerous.new.send(@method) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.each { |i| -i }.should == [-2, -5, -3, -6, -1, -4] end + it "reports the same arity as the given block" do + entries = [0, 1, 3, 4, 5, 6] + numerous = EnumerableSpecs::Numerous.new(*entries) + + def numerous.each(&block) + ScratchPad << block.arity + super + end + + numerous.send(@method) { |a, b| a % 2 }.should == [0, 1, 1, 0, 1, 0] + ScratchPad.recorded.should == [2] + ScratchPad.clear + ScratchPad.record [] + numerous.send(@method) { |i| i }.should == entries + ScratchPad.recorded.should == [1] + end + + it "yields an Array of 2 elements for a Hash when block arity is 1" do + c = Class.new do + def register(a) + ScratchPad << a + end + end + m = c.new.method(:register) + + ScratchPad.record [] + { 1 => 'a', 2 => 'b' }.map(&m) + ScratchPad.recorded.should == [[1, 'a'], [2, 'b']] + end + + it "yields 2 arguments for a Hash when block arity is 2" do + c = Class.new do + def register(a, b) + ScratchPad << [a, b] + end + end + m = c.new.method(:register) + + ScratchPad.record [] + { 1 => 'a', 2 => 'b' }.map(&m) + ScratchPad.recorded.should == [[1, 'a'], [2, 'b']] + end + + it "raises an error for a Hash when an arity enforcing block of arity >2 is passed in" do + c = Class.new do + def register(a, b, c) + end + end + m = c.new.method(:register) + + -> do + { 1 => 'a', 2 => 'b' }.map(&m) + end.should.raise(ArgumentError) + end + + it "calls the each method on sub-classes" do + c = Class.new(Hash) do + def each + ScratchPad << 'in each' + super + end + end + h = c.new + h[1] = 'a' + ScratchPad.record [] + h.send(@method) { |k,v| v } + ScratchPad.recorded.should == ['in each'] + end + it_should_behave_like :enumerable_enumeratorized_with_origin_size end diff --git a/spec/ruby/core/enumerable/shared/collect_concat.rb b/spec/ruby/core/enumerable/shared/collect_concat.rb index ddd431baeb..1694e3fdce 100644 --- a/spec/ruby/core/enumerable/shared/collect_concat.rb +++ b/spec/ruby/core/enumerable/shared/collect_concat.rb @@ -41,12 +41,12 @@ describe :enumerable_collect_concat, shared: true do obj = mock("to_ary defined") obj.should_receive(:to_ary).and_return("array") - -> { [1, obj, 3].send(@method) { |i| i } }.should raise_error(TypeError) + -> { [1, obj, 3].send(@method) { |i| i } }.should.raise(TypeError) end it "returns an enumerator when no block given" do enum = EnumerableSpecs::Numerous.new(1, 2).send(@method) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.each{ |i| [i] * i }.should == [1, 2, 2] end diff --git a/spec/ruby/core/enumerable/shared/entries.rb b/spec/ruby/core/enumerable/shared/entries.rb index 590ce73bcf..e32eb23d2a 100644 --- a/spec/ruby/core/enumerable/shared/entries.rb +++ b/spec/ruby/core/enumerable/shared/entries.rb @@ -13,14 +13,4 @@ describe :enumerable_entries, shared: true do count.send(@method, :hello, "world").should == [1, 2, 3] count.arguments_passed.should == [:hello, "world"] end - - ruby_version_is ''...'2.7' do - it "returns a tainted array if self is tainted" do - EnumerableSpecs::Empty.new.taint.send(@method).tainted?.should be_true - end - - it "returns an untrusted array if self is untrusted" do - EnumerableSpecs::Empty.new.untrust.send(@method).untrusted?.should be_true - end - end end diff --git a/spec/ruby/core/enumerable/shared/find.rb b/spec/ruby/core/enumerable/shared/find.rb index 61d63ba3d5..cdff640415 100644 --- a/spec/ruby/core/enumerable/shared/find.rb +++ b/spec/ruby/core/enumerable/shared/find.rb @@ -59,7 +59,7 @@ describe :enumerable_find, shared: true do end it "returns an enumerator when no block given" do - @numerous.send(@method).should be_an_instance_of(Enumerator) + @numerous.send(@method).should.instance_of?(Enumerator) end it "passes the ifnone proc to the enumerator" do diff --git a/spec/ruby/core/enumerable/shared/find_all.rb b/spec/ruby/core/enumerable/shared/find_all.rb index 1bbe71f372..27f01de6e0 100644 --- a/spec/ruby/core/enumerable/shared/find_all.rb +++ b/spec/ruby/core/enumerable/shared/find_all.rb @@ -14,7 +14,7 @@ describe :enumerable_find_all, shared: true do end it "returns an enumerator when no block given" do - @numerous.send(@method).should be_an_instance_of(Enumerator) + @numerous.send(@method).should.instance_of?(Enumerator) end it "passes through the values yielded by #each_with_index" do diff --git a/spec/ruby/core/enumerable/shared/include.rb b/spec/ruby/core/enumerable/shared/include.rb index 569f350fd5..ea250f032b 100644 --- a/spec/ruby/core/enumerable/shared/include.rb +++ b/spec/ruby/core/enumerable/shared/include.rb @@ -28,7 +28,7 @@ describe :enumerable_include, shared: true do it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMulti.new - multi.send(@method, [1,2]).should be_true + multi.send(@method, [1,2]).should == true end end diff --git a/spec/ruby/core/enumerable/shared/inject.rb b/spec/ruby/core/enumerable/shared/inject.rb index 12e0665dda..7da4f0ca99 100644 --- a/spec/ruby/core/enumerable/shared/inject.rb +++ b/spec/ruby/core/enumerable/shared/inject.rb @@ -1,3 +1,5 @@ +require_relative '../../array/shared/iterable_and_tolerating_size_increasing' + describe :enumerable_inject, shared: true do it "with argument takes a block with an accumulator (with argument as initial value) and the current element. Value of block becomes new accumulator" do a = [] @@ -14,14 +16,65 @@ describe :enumerable_inject, shared: true do it "can take two argument" do EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, :-).should == 4 + EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, "-").should == 4 + + [1, 2, 3].send(@method, 10, :-).should == 4 + [1, 2, 3].send(@method, 10, "-").should == 4 + end + + it "converts non-Symbol method name argument to String with #to_str if two arguments" do + name = Object.new + def name.to_str; "-"; end + + EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, name).should == 4 + [1, 2, 3].send(@method, 10, name).should == 4 + end + + it "raises TypeError when the second argument is not Symbol or String and it cannot be converted to String if two arguments" do + -> { EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, Object.new) }.should.raise(TypeError, /is not a symbol nor a string/) + -> { [1, 2, 3].send(@method, 10, Object.new) }.should.raise(TypeError, /is not a symbol nor a string/) end it "ignores the block if two arguments" do - EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, :-){ raise "we never get here"}.should == 4 + -> { + EnumerableSpecs::Numerous.new(1, 2, 3).send(@method, 10, :-) { raise "we never get here"}.should == 4 + }.should complain(/#{__FILE__}:#{__LINE__-1}: warning: given block not used/, verbose: true) + + -> { + [1, 2, 3].send(@method, 10, :-) { raise "we never get here"}.should == 4 + }.should complain(/#{__FILE__}:#{__LINE__-1}: warning: given block not used/, verbose: true) + end + + it "does not warn when given a Symbol with $VERBOSE true" do + -> { + [1, 2].send(@method, 0, :+) + [1, 2].send(@method, :+) + EnumerableSpecs::Numerous.new(1, 2).send(@method, 0, :+) + EnumerableSpecs::Numerous.new(1, 2).send(@method, :+) + }.should_not complain(verbose: true) end it "can take a symbol argument" do EnumerableSpecs::Numerous.new(10, 1, 2, 3).send(@method, :-).should == 4 + [10, 1, 2, 3].send(@method, :-).should == 4 + end + + it "can take a String argument" do + EnumerableSpecs::Numerous.new(10, 1, 2, 3).send(@method, "-").should == 4 + [10, 1, 2, 3].send(@method, "-").should == 4 + end + + it "converts non-Symbol method name argument to String with #to_str" do + name = Object.new + def name.to_str; "-"; end + + EnumerableSpecs::Numerous.new(10, 1, 2, 3).send(@method, name).should == 4 + [10, 1, 2, 3].send(@method, name).should == 4 + end + + it "raises TypeError when passed not Symbol or String method name argument and it cannot be converted to String" do + -> { EnumerableSpecs::Numerous.new(10, 1, 2, 3).send(@method, Object.new) }.should.raise(TypeError, /is not a symbol nor a string/) + -> { [10, 1, 2, 3].send(@method, Object.new) }.should.raise(TypeError, /is not a symbol nor a string/) end it "without argument takes a block with an accumulator (with first element as initial value) and the current element. Value of block becomes new accumulator" do @@ -30,10 +83,10 @@ describe :enumerable_inject, shared: true do a.should == [[2, 5], [5, 3], [3, 6], [6, 1], [1, 4]] end - it "gathers whole arrays as elements when each yields multiple" do - multi = EnumerableSpecs::YieldsMulti.new - multi.send(@method, []) {|acc, e| acc << e }.should == [[1, 2], [3, 4, 5], [6, 7, 8, 9]] - end + it "gathers whole arrays as elements when each yields multiple" do + multi = EnumerableSpecs::YieldsMulti.new + multi.send(@method, []) {|acc, e| acc << e }.should == [[1, 2], [3, 4, 5], [6, 7, 8, 9]] + end it "with inject arguments(legacy rubycon)" do # with inject argument @@ -50,7 +103,7 @@ describe :enumerable_inject, shared: true do it "without inject arguments(legacy rubycon)" do # no inject argument - EnumerableSpecs::EachDefiner.new(2).send(@method) {|acc,x| 999 } .should == 2 + EnumerableSpecs::EachDefiner.new(2).send(@method) {|acc,x| 999 }.should == 2 EnumerableSpecs::EachDefiner.new(2).send(@method) {|acc,x| acc }.should == 2 EnumerableSpecs::EachDefiner.new(2).send(@method) {|acc,x| x }.should == 2 @@ -60,10 +113,30 @@ describe :enumerable_inject, shared: true do EnumerableSpecs::EachDefiner.new('a','b','c').send(@method) {|result, i| i+result}.should == "cba" EnumerableSpecs::EachDefiner.new(3, 4, 5).send(@method) {|result, i| result*i}.should == 60 EnumerableSpecs::EachDefiner.new([1], 2, 'a','b').send(@method){|r,i| r<<i}.should == [1, 2, 'a', 'b'] - end it "returns nil when fails(legacy rubycon)" do EnumerableSpecs::EachDefiner.new().send(@method) {|acc,x| 999 }.should == nil end + + it "tolerates increasing a collection size during iterating Array" do + array = [:a, :b, :c] + ScratchPad.record [] + i = 0 + + array.send(@method, nil) do |_, e| + ScratchPad << e + array << i if i < 100 + i += 1 + end + + actual = ScratchPad.recorded + expected = [:a, :b, :c] + (0..99).to_a + actual.sort_by(&:to_s).should == expected.sort_by(&:to_s) + end + + it "raises an ArgumentError when no parameters or block is given" do + -> { [1,2].send(@method) }.should.raise(ArgumentError) + -> { {one: 1, two: 2}.send(@method) }.should.raise(ArgumentError) + end end diff --git a/spec/ruby/core/enumerable/shared/take.rb b/spec/ruby/core/enumerable/shared/take.rb index ce2ace20fa..a6da06325f 100644 --- a/spec/ruby/core/enumerable/shared/take.rb +++ b/spec/ruby/core/enumerable/shared/take.rb @@ -25,7 +25,7 @@ describe :enumerable_take, shared: true do end it "raises an ArgumentError when count is negative" do - -> { @enum.send(@method, -1) }.should raise_error(ArgumentError) + -> { @enum.send(@method, -1) }.should.raise(ArgumentError) end it "returns the entire array when count > length" do @@ -40,11 +40,11 @@ describe :enumerable_take, shared: true do end it "raises a TypeError if the passed argument is not numeric" do - -> { @enum.send(@method, nil) }.should raise_error(TypeError) - -> { @enum.send(@method, "a") }.should raise_error(TypeError) + -> { @enum.send(@method, nil) }.should.raise(TypeError) + -> { @enum.send(@method, "a") }.should.raise(TypeError) obj = mock("nonnumeric") - -> { @enum.send(@method, obj) }.should raise_error(TypeError) + -> { @enum.send(@method, obj) }.should.raise(TypeError) end it "gathers whole arrays as elements when each yields multiple" do diff --git a/spec/ruby/core/enumerable/slice_after_spec.rb b/spec/ruby/core/enumerable/slice_after_spec.rb index 0e46688db1..1ef37195e5 100644 --- a/spec/ruby/core/enumerable/slice_after_spec.rb +++ b/spec/ruby/core/enumerable/slice_after_spec.rb @@ -11,7 +11,7 @@ describe "Enumerable#slice_after" do arg = mock("filter") arg.should_receive(:===).and_return(false, true, false, false, false, true, false) e = @enum.slice_after(arg) - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == [[7, 6], [5, 4, 3, 2], [1]] end @@ -34,21 +34,21 @@ describe "Enumerable#slice_after" do describe "and no argument" do it "calls the block to determine when to yield" do e = @enum.slice_after{ |i| i == 6 || i == 2 } - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == [[7, 6], [5, 4, 3, 2], [1]] end end describe "and an argument" do it "raises an ArgumentError" do - -> { @enum.slice_after(42) { |i| i == 6 } }.should raise_error(ArgumentError) + -> { @enum.slice_after(42) { |i| i == 6 } }.should.raise(ArgumentError) end end end it "raises an ArgumentError when given an incorrect number of arguments" do - -> { @enum.slice_after("one", "two") }.should raise_error(ArgumentError) - -> { @enum.slice_after }.should raise_error(ArgumentError) + -> { @enum.slice_after("one", "two") }.should.raise(ArgumentError) + -> { @enum.slice_after }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/enumerable/slice_before_spec.rb b/spec/ruby/core/enumerable/slice_before_spec.rb index f9b33f7b28..7eb4410a25 100644 --- a/spec/ruby/core/enumerable/slice_before_spec.rb +++ b/spec/ruby/core/enumerable/slice_before_spec.rb @@ -12,7 +12,7 @@ describe "Enumerable#slice_before" do arg = mock "filter" arg.should_receive(:===).and_return(false, true, false, false, false, true, false) e = @enum.slice_before(arg) - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == [[7], [6, 5, 4, 3], [2, 1]] end @@ -35,7 +35,7 @@ describe "Enumerable#slice_before" do describe "and no argument" do it "calls the block to determine when to yield" do e = @enum.slice_before{|i| i == 6 || i == 2} - e.should be_an_instance_of(Enumerator) + e.should.instance_of?(Enumerator) e.to_a.should == [[7], [6, 5, 4, 3], [2, 1]] end end @@ -43,13 +43,13 @@ describe "Enumerable#slice_before" do it "does not accept arguments" do -> { @enum.slice_before(1) {} - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end end it "raises an ArgumentError when given an incorrect number of arguments" do - -> { @enum.slice_before("one", "two") }.should raise_error(ArgumentError) - -> { @enum.slice_before }.should raise_error(ArgumentError) + -> { @enum.slice_before("one", "two") }.should.raise(ArgumentError) + -> { @enum.slice_before }.should.raise(ArgumentError) end describe "when an iterator method yields more than one value" do diff --git a/spec/ruby/core/enumerable/slice_when_spec.rb b/spec/ruby/core/enumerable/slice_when_spec.rb index 6b8ea0923e..fe1ecd31e2 100644 --- a/spec/ruby/core/enumerable/slice_when_spec.rb +++ b/spec/ruby/core/enumerable/slice_when_spec.rb @@ -11,7 +11,7 @@ describe "Enumerable#slice_when" do context "when given a block" do it "returns an enumerator" do - @result.should be_an_instance_of(Enumerator) + @result.should.instance_of?(Enumerator) end it "splits chunks between adjacent elements i and j where the block returns true" do @@ -39,7 +39,7 @@ describe "Enumerable#slice_when" do context "when not given a block" do it "raises an ArgumentError" do - -> { @enum.slice_when }.should raise_error(ArgumentError) + -> { @enum.slice_when }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/enumerable/sort_by_spec.rb b/spec/ruby/core/enumerable/sort_by_spec.rb index 8fdd923fb4..62cf38ce3e 100644 --- a/spec/ruby/core/enumerable/sort_by_spec.rb +++ b/spec/ruby/core/enumerable/sort_by_spec.rb @@ -18,7 +18,7 @@ describe "Enumerable#sort_by" do it "returns an Enumerator when a block is not supplied" do a = EnumerableSpecs::Numerous.new("a","b") - a.sort_by.should be_an_instance_of(Enumerator) + a.sort_by.should.instance_of?(Enumerator) a.to_a.should == ["a", "b"] end diff --git a/spec/ruby/core/enumerable/sort_spec.rb b/spec/ruby/core/enumerable/sort_spec.rb index cff1a59986..427b1cd8f1 100644 --- a/spec/ruby/core/enumerable/sort_spec.rb +++ b/spec/ruby/core/enumerable/sort_spec.rb @@ -16,7 +16,7 @@ describe "Enumerable#sort" do it "raises a NoMethodError if elements do not define <=>" do -> do EnumerableSpecs::Numerous.new(BasicObject.new, BasicObject.new, BasicObject.new).sort - end.should raise_error(NoMethodError) + end.should.raise(NoMethodError) end it "sorts enumerables that contain nils" do @@ -31,16 +31,16 @@ describe "Enumerable#sort" do it "compare values returned by block with 0" do EnumerableSpecs::Numerous.new.sort { |n, m| -(n+m) * (n <=> m) }.should == [6, 5, 4, 3, 2, 1] EnumerableSpecs::Numerous.new.sort { |n, m| - EnumerableSpecs::ComparableWithFixnum.new(-(n+m) * (n <=> m)) + EnumerableSpecs::ComparableWithInteger.new(-(n+m) * (n <=> m)) }.should == [6, 5, 4, 3, 2, 1] -> { EnumerableSpecs::Numerous.new.sort { |n, m| (n <=> m).to_s } - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises an error if objects can't be compared" do a=EnumerableSpecs::Numerous.new(EnumerableSpecs::Uncomparable.new, EnumerableSpecs::Uncomparable.new) - -> {a.sort}.should raise_error(ArgumentError) + -> {a.sort}.should.raise(ArgumentError) end it "gathers whole arrays as elements when each yields multiple" do diff --git a/spec/ruby/core/enumerable/sum_spec.rb b/spec/ruby/core/enumerable/sum_spec.rb index c9d7017b45..2eb74db6ac 100644 --- a/spec/ruby/core/enumerable/sum_spec.rb +++ b/spec/ruby/core/enumerable/sum_spec.rb @@ -22,7 +22,29 @@ describe 'Enumerable#sum' do @enum.sum.should == 5/3r end - it 'takes a block to transform the elements' do - @enum.sum { |element| element * 2 }.should == 10/3r + context 'with a block' do + it 'transforms the elements' do + @enum.sum { |element| element * 2 }.should == 10/3r + end + + it 'does not destructure array elements' do + class << @enum + def each + yield [1,2] + yield [3] + end + end + + @enum.sum(&:last).should == 5 + end + end + + # https://bugs.ruby-lang.org/issues/12217 + # https://github.com/ruby/ruby/blob/master/doc/ChangeLog/ChangeLog-2.4.0#L6208-L6214 + it "uses Kahan's compensated summation algorithm for precise sum of float numbers" do + floats = [2.7800000000000002, 5.0, 2.5, 4.44, 3.89, 3.89, 4.44, 7.78, 5.0, 2.7800000000000002, 5.0, 2.5].to_enum + naive_sum = floats.reduce { |sum, e| sum + e } + naive_sum.should == 50.00000000000001 + floats.sum.should == 50.0 end end diff --git a/spec/ruby/core/enumerable/take_spec.rb b/spec/ruby/core/enumerable/take_spec.rb index 41a7438330..8cc746f88d 100644 --- a/spec/ruby/core/enumerable/take_spec.rb +++ b/spec/ruby/core/enumerable/take_spec.rb @@ -4,7 +4,7 @@ require_relative 'shared/take' describe "Enumerable#take" do it "requires an argument" do - ->{ EnumerableSpecs::Numerous.new.take}.should raise_error(ArgumentError) + ->{ EnumerableSpecs::Numerous.new.take}.should.raise(ArgumentError) end describe "when passed an argument" do diff --git a/spec/ruby/core/enumerable/take_while_spec.rb b/spec/ruby/core/enumerable/take_while_spec.rb index 26db39ac4b..918bfc897d 100644 --- a/spec/ruby/core/enumerable/take_while_spec.rb +++ b/spec/ruby/core/enumerable/take_while_spec.rb @@ -8,7 +8,7 @@ describe "Enumerable#take_while" do end it "returns an Enumerator if no block given" do - @enum.take_while.should be_an_instance_of(Enumerator) + @enum.take_while.should.instance_of?(Enumerator) end it "returns no/all elements for {true/false} block" do @@ -38,7 +38,7 @@ describe "Enumerable#take_while" do it "doesn't return self when it could" do a = [1,2,3] - a.take_while{true}.should_not equal(a) + a.take_while{true}.should_not.equal?(a) end it "calls the block with initial args when yielded with multiple arguments" do diff --git a/spec/ruby/core/enumerable/tally_spec.rb b/spec/ruby/core/enumerable/tally_spec.rb index 363b3def21..deef741407 100644 --- a/spec/ruby/core/enumerable/tally_spec.rb +++ b/spec/ruby/core/enumerable/tally_spec.rb @@ -1,35 +1,91 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -ruby_version_is "2.7" do - describe "Enumerable#tally" do - before :each do - ScratchPad.record [] - end - - it "returns a hash with counts according to the value" do - enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') - enum.tally.should == { 'foo' => 2, 'bar' => 1, 'baz' => 1} - end - - it "returns a hash without default" do - hash = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz').tally - hash.default_proc.should be_nil - hash.default.should be_nil - end - - it "returns an empty hash for empty enumerables" do - EnumerableSpecs::Empty.new.tally.should == {} - end - - it "counts values as gathered array when yielded with multiple arguments" do - EnumerableSpecs::YieldsMixed2.new.tally.should == EnumerableSpecs::YieldsMixed2.gathered_yields.group_by(&:itself).transform_values(&:size) - end - - it "does not call given block" do - enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') - enum.tally { |v| ScratchPad << v } - ScratchPad.recorded.should == [] - end +describe "Enumerable#tally" do + before :each do + ScratchPad.record [] + end + + it "returns a hash with counts according to the value" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + enum.tally.should == { 'foo' => 2, 'bar' => 1, 'baz' => 1} + end + + it "returns a hash without default" do + hash = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz').tally + hash.default_proc.should == nil + hash.default.should == nil + end + + it "returns an empty hash for empty enumerables" do + EnumerableSpecs::Empty.new.tally.should == {} + end + + it "counts values as gathered array when yielded with multiple arguments" do + EnumerableSpecs::YieldsMixed2.new.tally.should == EnumerableSpecs::YieldsMixed2.gathered_yields.group_by(&:itself).transform_values(&:size) + end + + it "does not call given block" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + enum.tally { |v| ScratchPad << v } + ScratchPad.recorded.should == [] + end +end + +describe "Enumerable#tally with a hash" do + before :each do + ScratchPad.record [] + end + + it "returns a hash with counts according to the value" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + enum.tally({ 'foo' => 1 }).should == { 'foo' => 3, 'bar' => 1, 'baz' => 1} + end + + it "returns the given hash" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + hash = { 'foo' => 1 } + enum.tally(hash).should.equal?(hash) + end + + it "calls #to_hash to convert argument to Hash implicitly if passed not a Hash" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + object = Object.new + def object.to_hash; { 'foo' => 1 }; end + enum.tally(object).should == { 'foo' => 3, 'bar' => 1, 'baz' => 1} + end + + it "raises a FrozenError and does not update the given hash when the hash is frozen" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + hash = { 'foo' => 1 }.freeze + -> { enum.tally(hash) }.should.raise(FrozenError) + hash.should == { 'foo' => 1 } + end + + it "raises a FrozenError even if enumerable is empty" do + enum = EnumerableSpecs::Numerous.new() + hash = { 'foo' => 1 }.freeze + -> { enum.tally(hash) }.should.raise(FrozenError) + end + + it "does not call given block" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + enum.tally({ 'foo' => 1 }) { |v| ScratchPad << v } + ScratchPad.recorded.should == [] + end + + it "ignores the default value" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + enum.tally(Hash.new(100)).should == { 'foo' => 2, 'bar' => 1, 'baz' => 1} + end + + it "ignores the default proc" do + enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz') + enum.tally(Hash.new {100}).should == { 'foo' => 2, 'bar' => 1, 'baz' => 1} + end + + it "needs the values counting each elements to be an integer" do + enum = EnumerableSpecs::Numerous.new('foo') + -> { enum.tally({ 'foo' => 'bar' }) }.should.raise(TypeError) end end diff --git a/spec/ruby/core/enumerable/to_a_spec.rb b/spec/ruby/core/enumerable/to_a_spec.rb index 0f3060dc48..723f922574 100644 --- a/spec/ruby/core/enumerable/to_a_spec.rb +++ b/spec/ruby/core/enumerable/to_a_spec.rb @@ -3,5 +3,5 @@ require_relative 'fixtures/classes' require_relative 'shared/entries' describe "Enumerable#to_a" do - it_behaves_like :enumerable_entries , :to_a + it_behaves_like :enumerable_entries, :to_a end diff --git a/spec/ruby/core/enumerable/to_h_spec.rb b/spec/ruby/core/enumerable/to_h_spec.rb index 63bfdf19af..38847eccfb 100644 --- a/spec/ruby/core/enumerable/to_h_spec.rb +++ b/spec/ruby/core/enumerable/to_h_spec.rb @@ -36,55 +36,61 @@ describe "Enumerable#to_h" do it "raises TypeError if an element is not an array" do enum = EnumerableSpecs::EachDefiner.new(:x) - -> { enum.to_h }.should raise_error(TypeError) + -> { enum.to_h }.should.raise(TypeError) end it "raises ArgumentError if an element is not a [key, value] pair" do enum = EnumerableSpecs::EachDefiner.new([:x]) - -> { enum.to_h }.should raise_error(ArgumentError) + -> { enum.to_h }.should.raise(ArgumentError) end - ruby_version_is "2.6" do - context "with block" do - before do - @enum = EnumerableSpecs::EachDefiner.new(:a, :b) - end - - it "converts [key, value] pairs returned by the block to a hash" do - @enum.to_h { |k| [k, k.to_s] }.should == { a: 'a', b: 'b' } - end - - it "raises ArgumentError if block returns longer or shorter array" do - -> do - @enum.to_h { |k| [k, k.to_s, 1] } - end.should raise_error(ArgumentError, /element has wrong array length/) - - -> do - @enum.to_h { |k| [k] } - end.should raise_error(ArgumentError, /element has wrong array length/) - end - - it "raises TypeError if block returns something other than Array" do - -> do - @enum.to_h { |k| "not-array" } - end.should raise_error(TypeError, /wrong element type String/) - end - - it "coerces returned pair to Array with #to_ary" do - x = mock('x') - x.stub!(:to_ary).and_return([:b, 'b']) - - @enum.to_h { |k| x }.should == { :b => 'b' } - end - - it "does not coerce returned pair to Array with #to_a" do - x = mock('x') - x.stub!(:to_a).and_return([:b, 'b']) - - -> do - @enum.to_h { |k| x } - end.should raise_error(TypeError, /wrong element type MockObject/) - end + context "with block" do + before do + @enum = EnumerableSpecs::EachDefiner.new(:a, :b) + end + + it "converts [key, value] pairs returned by the block to a hash" do + @enum.to_h { |k| [k, k.to_s] }.should == { a: 'a', b: 'b' } + end + + it "passes to a block each element as a single argument" do + enum_of_arrays = EnumerableSpecs::EachDefiner.new([:a, 1], [:b, 2]) + + ScratchPad.record [] + enum_of_arrays.to_h { |*args| ScratchPad << args; [args[0], args[1]] } + ScratchPad.recorded.sort.should == [[[:a, 1]], [[:b, 2]]] + end + + it "raises ArgumentError if block returns longer or shorter array" do + -> do + @enum.to_h { |k| [k, k.to_s, 1] } + end.should.raise(ArgumentError, /element has wrong array length/) + + -> do + @enum.to_h { |k| [k] } + end.should.raise(ArgumentError, /element has wrong array length/) + end + + it "raises TypeError if block returns something other than Array" do + -> do + @enum.to_h { |k| "not-array" } + end.should.raise(TypeError, /wrong element type String/) + end + + it "coerces returned pair to Array with #to_ary" do + x = mock('x') + x.stub!(:to_ary).and_return([:b, 'b']) + + @enum.to_h { |k| x }.should == { :b => 'b' } + end + + it "does not coerce returned pair to Array with #to_a" do + x = mock('x') + x.stub!(:to_a).and_return([:b, 'b']) + + -> do + @enum.to_h { |k| x } + end.should.raise(TypeError, /wrong element type MockObject/) end end end diff --git a/spec/ruby/core/enumerable/to_set_spec.rb b/spec/ruby/core/enumerable/to_set_spec.rb new file mode 100644 index 0000000000..7b04c72bce --- /dev/null +++ b/spec/ruby/core/enumerable/to_set_spec.rb @@ -0,0 +1,30 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Enumerable#to_set" do + it "returns a new Set created from self" do + [1, 2, 3].to_set.should == Set[1, 2, 3] + {a: 1, b: 2}.to_set.should == Set[[:b, 2], [:a, 1]] + end + + it "passes down passed blocks" do + [1, 2, 3].to_set { |x| x * x }.should == Set[1, 4, 9] + end + + ruby_version_is "4.0"..."4.1" do + it "instantiates an object of provided as the first argument set class" do + set = nil + proc{set = [1, 2, 3].to_set(EnumerableSpecs::SetSubclass)}.should complain(/Enumerable#to_set/) + set.should.is_a?(EnumerableSpecs::SetSubclass) + set.to_a.sort.should == [1, 2, 3] + end + end + + ruby_version_is ""..."4.0" do + it "instantiates an object of provided as the first argument set class" do + set = [1, 2, 3].to_set(EnumerableSpecs::SetSubclass) + set.should.is_a?(EnumerableSpecs::SetSubclass) + set.to_a.sort.should == [1, 2, 3] + end + end +end diff --git a/spec/ruby/core/enumerable/uniq_spec.rb b/spec/ruby/core/enumerable/uniq_spec.rb index 82c041d4ef..a1ed44796f 100644 --- a/spec/ruby/core/enumerable/uniq_spec.rb +++ b/spec/ruby/core/enumerable/uniq_spec.rb @@ -31,76 +31,32 @@ describe 'Enumerable#uniq' do [x, y].to_enum.uniq.should == [x, y] end - ruby_version_is '2.7' do - it "compares elements with matching hash codes with #eql?" do - a = Array.new(2) do - obj = mock('0') - obj.should_receive(:hash).at_least(1).and_return(0) - - def obj.eql?(o) - false - end - - obj - end - - a.uniq.should == a + it "compares elements with matching hash codes with #eql?" do + a = Array.new(2) do + obj = mock('0') + obj.should_receive(:hash).at_least(1).and_return(0) - a = Array.new(2) do - obj = mock('0') - obj.should_receive(:hash).at_least(1).and_return(0) - - def obj.eql?(o) - true - end - - obj + def obj.eql?(o) + false end - a.to_enum.uniq.size.should == 1 + obj end - end - ruby_version_is ''...'2.7' do - it "compares elements with matching hash codes with #eql?" do - a = Array.new(2) do - obj = mock('0') - obj.should_receive(:hash).at_least(1).and_return(0) - - def obj.eql?(o) - # It's undefined whether the impl does a[0].eql?(a[1]) or - # a[1].eql?(a[0]) so we taint both. - taint - o.taint - false - end - - obj - end - - a.uniq.should == a - a[0].tainted?.should == true - a[1].tainted?.should == true + a.uniq.should == a - a = Array.new(2) do - obj = mock('0') - obj.should_receive(:hash).at_least(1).and_return(0) + a = Array.new(2) do + obj = mock('0') + obj.should_receive(:hash).at_least(1).and_return(0) - def obj.eql?(o) - # It's undefined whether the impl does a[0].eql?(a[1]) or - # a[1].eql?(a[0]) so we taint both. - taint - o.taint - true - end - - obj + def obj.eql?(o) + true end - a.to_enum.uniq.size.should == 1 - a[0].tainted?.should == true - a[1].tainted?.should == true + obj end + + a.to_enum.uniq.size.should == 1 end context 'when yielded with multiple arguments' do diff --git a/spec/ruby/core/enumerable/zip_spec.rb b/spec/ruby/core/enumerable/zip_spec.rb index 9ec15aa030..c5f9a3e4d4 100644 --- a/spec/ruby/core/enumerable/zip_spec.rb +++ b/spec/ruby/core/enumerable/zip_spec.rb @@ -38,4 +38,9 @@ describe "Enumerable#zip" do multi.zip(multi).should == [[[1, 2], [1, 2]], [[3, 4, 5], [3, 4, 5]], [[6, 7, 8, 9], [6, 7, 8, 9]]] end + it "raises TypeError when some argument isn't Array and doesn't respond to #to_ary and #to_enum" do + -> { EnumerableSpecs::Numerous.new(1,2,3).zip(Object.new) }.should.raise(TypeError, "wrong argument type Object (must respond to :each)") + -> { EnumerableSpecs::Numerous.new(1,2,3).zip(1) }.should.raise(TypeError, "wrong argument type Integer (must respond to :each)") + -> { EnumerableSpecs::Numerous.new(1,2,3).zip(true) }.should.raise(TypeError, "wrong argument type TrueClass (must respond to :each)") + end end |
