diff options
Diffstat (limited to 'spec/ruby/core/array')
124 files changed, 2200 insertions, 1454 deletions
diff --git a/spec/ruby/core/array/all_spec.rb b/spec/ruby/core/array/all_spec.rb new file mode 100644 index 0000000000..680e8c26fa --- /dev/null +++ b/spec/ruby/core/array/all_spec.rb @@ -0,0 +1,13 @@ +require_relative '../../spec_helper' +require_relative 'shared/iterable_and_tolerating_size_increasing' + +describe "Array#all?" do + @value_to_return = -> _ { true } + it_behaves_like :array_iterable_and_tolerating_size_increasing, :all? + + it "ignores the block if there is an argument" do + -> { + ['bar', 'foobar'].all?(/bar/) { false }.should == true + }.should complain(/given block not used/) + end +end diff --git a/spec/ruby/core/array/allocate_spec.rb b/spec/ruby/core/array/allocate_spec.rb index 04f7c0d0ad..c9eceef590 100644 --- a/spec/ruby/core/array/allocate_spec.rb +++ b/spec/ruby/core/array/allocate_spec.rb @@ -3,7 +3,7 @@ require_relative '../../spec_helper' describe "Array.allocate" do it "returns an instance of Array" do ary = Array.allocate - ary.should be_an_instance_of(Array) + ary.should.instance_of?(Array) end it "returns a fully-formed instance of Array" do @@ -14,6 +14,6 @@ describe "Array.allocate" do end it "does not accept any arguments" do - -> { Array.allocate(1) }.should raise_error(ArgumentError) + -> { Array.allocate(1) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/array/any_spec.rb b/spec/ruby/core/array/any_spec.rb index 09d949fe6e..b51ce62f0f 100644 --- a/spec/ruby/core/array/any_spec.rb +++ b/spec/ruby/core/array/any_spec.rb @@ -1,4 +1,5 @@ require_relative '../../spec_helper' +require_relative 'shared/iterable_and_tolerating_size_increasing' describe "Array#any?" do describe 'with no block given (a default block of { |x| x } is implicit)' do @@ -19,6 +20,9 @@ describe "Array#any?" do end describe 'with a block given' do + @value_to_return = -> _ { false } + it_behaves_like :array_iterable_and_tolerating_size_increasing, :any? + it 'is false if the array is empty' do empty_array = [] empty_array.any? {|v| 1 == 1 }.should == false @@ -34,4 +38,12 @@ describe "Array#any?" do array_with_members.any? {|v| v == 42 }.should == false end end + + describe 'when given a pattern argument' do + it "ignores the block if there is an argument" do + -> { + ['bar', 'foobar'].any?(/bar/) { false }.should == true + }.should complain(/given block not used/) + end + end end diff --git a/spec/ruby/core/array/append_spec.rb b/spec/ruby/core/array/append_spec.rb index c12473dc07..de0e56b513 100644 --- a/spec/ruby/core/array/append_spec.rb +++ b/spec/ruby/core/array/append_spec.rb @@ -10,8 +10,8 @@ describe "Array#<<" do it "returns self to allow chaining" do a = [] b = a - (a << 1).should equal(b) - (a << 2 << 3).should equal(b) + (a << 1).should.equal?(b) + (a << 2 << 3).should.equal?(b) end it "correctly resizes the Array" do @@ -31,7 +31,7 @@ describe "Array#<<" do end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array << 5 }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array << 5 }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/array/assoc_spec.rb b/spec/ruby/core/array/assoc_spec.rb index f8479d763c..a5026cf5d4 100644 --- a/spec/ruby/core/array/assoc_spec.rb +++ b/spec/ruby/core/array/assoc_spec.rb @@ -6,18 +6,18 @@ describe "Array#assoc" do s1 = ["colors", "red", "blue", "green"] s2 = [:letters, "a", "b", "c"] s3 = [4] - s4 = ["colors", "cyan", "yellow", "magenda"] + s4 = ["colors", "cyan", "yellow", "magenta"] s5 = [:letters, "a", "i", "u"] s_nil = [nil, nil] a = [s1, s2, s3, s4, s5, s_nil] - a.assoc(s1.first).should equal(s1) - a.assoc(s2.first).should equal(s2) - a.assoc(s3.first).should equal(s3) - a.assoc(s4.first).should equal(s1) - a.assoc(s5.first).should equal(s2) - a.assoc(s_nil.first).should equal(s_nil) - a.assoc(4).should equal(s3) - a.assoc("key not in array").should be_nil + a.assoc(s1.first).should.equal?(s1) + a.assoc(s2.first).should.equal?(s2) + a.assoc(s3.first).should.equal?(s3) + a.assoc(s4.first).should.equal?(s1) + a.assoc(s5.first).should.equal?(s2) + a.assoc(s_nil.first).should.equal?(s_nil) + a.assoc(4).should.equal?(s3) + a.assoc("key not in array").should == nil end it "calls == on first element of each array" do @@ -25,16 +25,28 @@ describe "Array#assoc" do key2 = mock('key2') items = [['not it', 1], [ArraySpecs::AssocKey.new, 2], ['na', 3]] - items.assoc(key1).should equal(items[1]) - items.assoc(key2).should be_nil + items.assoc(key1).should.equal?(items[1]) + items.assoc(key2).should == nil end it "ignores any non-Array elements" do - [1, 2, 3].assoc(2).should be_nil + [1, 2, 3].assoc(2).should == nil s1 = [4] s2 = [5, 4, 3] a = ["foo", [], s1, s2, nil, []] - a.assoc(s1.first).should equal(s1) - a.assoc(s2.first).should equal(s2) + a.assoc(s1.first).should.equal?(s1) + a.assoc(s2.first).should.equal?(s2) + end + + it "calls to_ary on non-array elements" do + s1 = [1, 2] + s2 = ArraySpecs::ArrayConvertible.new(2, 3) + a = [s1, s2] + + s1.should_not_receive(:to_ary) + a.assoc(s1.first).should.equal?(s1) + + a.assoc(2).should == [2, 3] + s2.called.should.equal?(:to_ary) end end diff --git a/spec/ruby/core/array/at_spec.rb b/spec/ruby/core/array/at_spec.rb index 8bc789fef7..3c7c99fdff 100644 --- a/spec/ruby/core/array/at_spec.rb +++ b/spec/ruby/core/array/at_spec.rb @@ -47,10 +47,10 @@ describe "Array#at" do end it "raises a TypeError when the passed argument can't be coerced to Integer" do - -> { [].at("cat") }.should raise_error(TypeError) + -> { [].at("cat") }.should.raise(TypeError) end it "raises an ArgumentError when 2 or more arguments are passed" do - -> { [:a, :b].at(0,1) }.should raise_error(ArgumentError) + -> { [:a, :b].at(0,1) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/array/bsearch_index_spec.rb b/spec/ruby/core/array/bsearch_index_spec.rb index df2c7c098e..e1d5eb66bb 100644 --- a/spec/ruby/core/array/bsearch_index_spec.rb +++ b/spec/ruby/core/array/bsearch_index_spec.rb @@ -8,11 +8,11 @@ describe "Array#bsearch_index" do end it "returns an Enumerator" do - @enum.should be_an_instance_of(Enumerator) + @enum.should.instance_of?(Enumerator) end it "returns an Enumerator with unknown size" do - @enum.size.should be_nil + @enum.size.should == nil end it "returns index of element when block condition is satisfied" do @@ -21,11 +21,11 @@ describe "Array#bsearch_index" do end it "raises a TypeError when block returns a String" do - -> { [1, 2, 3].bsearch_index { "not ok" } }.should raise_error(TypeError) + -> { [1, 2, 3].bsearch_index { "not ok" } }.should.raise(TypeError) end it "returns nil when block is empty" do - [1, 2, 3].bsearch_index {}.should be_nil + [1, 2, 3].bsearch_index {}.should == nil end context "minimum mode" do @@ -40,8 +40,8 @@ describe "Array#bsearch_index" do end it "returns nil when block condition is never satisfied" do - @array.bsearch_index { false }.should be_nil - @array.bsearch_index { |x| x >= 100 }.should be_nil + @array.bsearch_index { false }.should == nil + @array.bsearch_index { |x| x >= 100 }.should == nil end end @@ -51,34 +51,30 @@ describe "Array#bsearch_index" do end it "returns the index of any matched elements where element is between 4 <= x < 8" do - [1, 2].should include(@array.bsearch_index { |x| 1 - x / 4 }) + [1, 2].should.include?(@array.bsearch_index { |x| 1 - x / 4 }) end it "returns the index of any matched elements where element is between 8 <= x < 10" do - @array.bsearch_index { |x| 4 - x / 2 }.should be_nil + @array.bsearch_index { |x| 4 - x / 2 }.should == nil end it "returns nil when block never returns 0" do - @array.bsearch_index { |x| 1 }.should be_nil - @array.bsearch_index { |x| -1 }.should be_nil - end - - it "returns the middle element when block always returns zero" do - @array.bsearch_index { |x| 0 }.should == 2 + @array.bsearch_index { |x| 1 }.should == nil + @array.bsearch_index { |x| -1 }.should == nil end context "magnitude does not effect the result" do it "returns the index of any matched elements where element is between 4n <= xn < 8n" do - [1, 2].should include(@array.bsearch_index { |x| (1 - x / 4) * (2**100) }) + [1, 2].should.include?(@array.bsearch_index { |x| (1 - x / 4) * (2**100) }) end it "returns nil when block never returns 0" do - @array.bsearch_index { |x| 1 * (2**100) }.should be_nil - @array.bsearch_index { |x| (-1) * (2**100) }.should be_nil + @array.bsearch_index { |x| 1 * (2**100) }.should == nil + @array.bsearch_index { |x| (-1) * (2**100) }.should == nil end it "handles values from Integer#coerce" do - [1, 2].should include(@array.bsearch_index { |x| (2**100).coerce((1 - x / 4) * (2**100)).first }) + [1, 2].should.include?(@array.bsearch_index { |x| (2**100).coerce((1 - x / 4) * (2**100)).first }) end end end diff --git a/spec/ruby/core/array/bsearch_spec.rb b/spec/ruby/core/array/bsearch_spec.rb index 8fa6245dbf..12aec60654 100644 --- a/spec/ruby/core/array/bsearch_spec.rb +++ b/spec/ruby/core/array/bsearch_spec.rb @@ -3,26 +3,26 @@ require_relative '../enumerable/shared/enumeratorized' describe "Array#bsearch" do it "returns an Enumerator when not passed a block" do - [1].bsearch.should be_an_instance_of(Enumerator) + [1].bsearch.should.instance_of?(Enumerator) end it_behaves_like :enumeratorized_with_unknown_size, :bsearch, [1,2,3] it "raises a TypeError if the block returns an Object" do - -> { [1].bsearch { Object.new } }.should raise_error(TypeError) + -> { [1].bsearch { Object.new } }.should.raise(TypeError) end it "raises a TypeError if the block returns a String" do - -> { [1].bsearch { "1" } }.should raise_error(TypeError) + -> { [1].bsearch { "1" } }.should.raise(TypeError) end context "with a block returning true or false" do it "returns nil if the block returns false for every element" do - [0, 1, 2, 3].bsearch { |x| x > 3 }.should be_nil + [0, 1, 2, 3].bsearch { |x| x > 3 }.should == nil end it "returns nil if the block returns nil for every element" do - [0, 1, 2, 3].bsearch { |x| nil }.should be_nil + [0, 1, 2, 3].bsearch { |x| nil }.should == nil end it "returns element at zero if the block returns true for every element" do @@ -38,21 +38,21 @@ describe "Array#bsearch" do context "with a block returning negative, zero, positive numbers" do it "returns nil if the block returns less than zero for every element" do - [0, 1, 2, 3].bsearch { |x| x <=> 5 }.should be_nil + [0, 1, 2, 3].bsearch { |x| x <=> 5 }.should == nil end it "returns nil if the block returns greater than zero for every element" do - [0, 1, 2, 3].bsearch { |x| x <=> -1 }.should be_nil + [0, 1, 2, 3].bsearch { |x| x <=> -1 }.should == nil end it "returns nil if the block never returns zero" do - [0, 1, 3, 4].bsearch { |x| x <=> 2 }.should be_nil + [0, 1, 3, 4].bsearch { |x| x <=> 2 }.should == nil end it "accepts (+/-)Float::INFINITY from the block" do - [0, 1, 3, 4].bsearch { |x| Float::INFINITY }.should be_nil - [0, 1, 3, 4].bsearch { |x| -Float::INFINITY }.should be_nil + [0, 1, 3, 4].bsearch { |x| Float::INFINITY }.should == nil + [0, 1, 3, 4].bsearch { |x| -Float::INFINITY }.should == nil end it "returns an element at an index for which block returns 0.0" do @@ -62,17 +62,17 @@ describe "Array#bsearch" do it "returns an element at an index for which block returns 0" do result = [0, 1, 2, 3, 4].bsearch { |x| x < 1 ? 1 : x > 3 ? -1 : 0 } - [1, 2].should include(result) + [1, 2].should.include?(result) end end context "with a block that calls break" do it "returns nil if break is called without a value" do - ['a', 'b', 'c'].bsearch { |v| break }.should be_nil + ['a', 'b', 'c'].bsearch { |v| break }.should == nil end it "returns nil if break is called with a nil value" do - ['a', 'b', 'c'].bsearch { |v| break nil }.should be_nil + ['a', 'b', 'c'].bsearch { |v| break nil }.should == nil end it "returns object if break is called with an object" do diff --git a/spec/ruby/core/array/clear_spec.rb b/spec/ruby/core/array/clear_spec.rb index 81ba56e01e..15778f864f 100644 --- a/spec/ruby/core/array/clear_spec.rb +++ b/spec/ruby/core/array/clear_spec.rb @@ -4,13 +4,13 @@ require_relative 'fixtures/classes' describe "Array#clear" do it "removes all elements" do a = [1, 2, 3, 4] - a.clear.should equal(a) + a.clear.should.equal?(a) a.should == [] end it "returns self" do a = [1] - a.should equal a.clear + a.should.equal? a.clear end it "leaves the Array empty" do @@ -21,12 +21,12 @@ describe "Array#clear" do end it "does not accept any arguments" do - -> { [1].clear(true) }.should raise_error(ArgumentError) + -> { [1].clear(true) }.should.raise(ArgumentError) end it "raises a FrozenError on a frozen array" do a = [1] a.freeze - -> { a.clear }.should raise_error(FrozenError) + -> { a.clear }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/array/clone_spec.rb b/spec/ruby/core/array/clone_spec.rb index e22a6c6d53..7ce9d40a81 100644 --- a/spec/ruby/core/array/clone_spec.rb +++ b/spec/ruby/core/array/clone_spec.rb @@ -23,9 +23,9 @@ describe "Array#clone" do aa = a.clone bb = b.clone - a.respond_to?(:a_singleton_method).should be_true - b.respond_to?(:a_singleton_method).should be_false - aa.respond_to?(:a_singleton_method).should be_true - bb.respond_to?(:a_singleton_method).should be_false + a.respond_to?(:a_singleton_method).should == true + b.respond_to?(:a_singleton_method).should == false + aa.respond_to?(:a_singleton_method).should == true + bb.respond_to?(:a_singleton_method).should == false end end diff --git a/spec/ruby/core/array/combination_spec.rb b/spec/ruby/core/array/combination_spec.rb index f16d6f98fc..ac570687ca 100644 --- a/spec/ruby/core/array/combination_spec.rb +++ b/spec/ruby/core/array/combination_spec.rb @@ -6,11 +6,11 @@ describe "Array#combination" do end it "returns an enumerator when no block is provided" do - @array.combination(2).should be_an_instance_of(Enumerator) + @array.combination(2).should.instance_of?(Enumerator) end it "returns self when a block is given" do - @array.combination(2){}.should equal(@array) + @array.combination(2){}.should.equal?(@array) end it "yields nothing for out of bounds length and return self" do @@ -30,7 +30,7 @@ describe "Array#combination" do it "yields a copy of self if the argument is the size of the receiver" do r = @array.combination(4).to_a r.should == [@array] - r[0].should_not equal(@array) + r[0].should_not.equal?(@array) end it "yields [] when length is 0" do diff --git a/spec/ruby/core/array/compact_spec.rb b/spec/ruby/core/array/compact_spec.rb index 83b3fa2a89..dbcd16da35 100644 --- a/spec/ruby/core/array/compact_spec.rb +++ b/spec/ruby/core/array/compact_spec.rb @@ -15,30 +15,30 @@ describe "Array#compact" do it "does not return self" do a = [1, 2, 3] - a.compact.should_not equal(a) + a.compact.should_not.equal?(a) end it "does not return subclass instance for Array subclasses" do - ArraySpecs::MyArray[1, 2, 3, nil].compact.should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3, nil].compact.should.instance_of?(Array) end end describe "Array#compact!" do it "removes all nil elements" do a = ['a', nil, 'b', false, 'c'] - a.compact!.should equal(a) + a.compact!.should.equal?(a) a.should == ["a", "b", false, "c"] a = [nil, 'a', 'b', false, 'c'] - a.compact!.should equal(a) + a.compact!.should.equal?(a) a.should == ["a", "b", false, "c"] a = ['a', 'b', false, 'c', nil] - a.compact!.should equal(a) + a.compact!.should.equal?(a) a.should == ["a", "b", false, "c"] end it "returns self if some nil elements are removed" do a = ['a', nil, 'b', false, 'c'] - a.compact!.should equal a + a.compact!.should.equal? a end it "returns nil if there are no nil elements to remove" do @@ -46,6 +46,6 @@ describe "Array#compact!" do end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array.compact! }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.compact! }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/array/comparison_spec.rb b/spec/ruby/core/array/comparison_spec.rb index 5d1c3265f1..14e8931e5a 100644 --- a/spec/ruby/core/array/comparison_spec.rb +++ b/spec/ruby/core/array/comparison_spec.rb @@ -92,6 +92,6 @@ describe "Array#<=>" do end it "returns nil when the argument is not array-like" do - ([] <=> false).should be_nil + ([] <=> false).should == nil end end diff --git a/spec/ruby/core/array/concat_spec.rb b/spec/ruby/core/array/concat_spec.rb index f3cab9c17c..1e8d20c36c 100644 --- a/spec/ruby/core/array/concat_spec.rb +++ b/spec/ruby/core/array/concat_spec.rb @@ -4,12 +4,12 @@ require_relative 'fixtures/classes' describe "Array#concat" do it "returns the array itself" do ary = [1,2,3] - ary.concat([4,5,6]).equal?(ary).should be_true + ary.concat([4,5,6]).equal?(ary).should == true end it "appends the elements in the other array" do ary = [1, 2, 3] - ary.concat([9, 10, 11]).should equal(ary) + ary.concat([9, 10, 11]).should.equal?(ary) ary.should == [1, 2, 3, 9, 10, 11] ary.concat([]) ary.should == [1, 2, 3, 9, 10, 11] @@ -33,12 +33,12 @@ describe "Array#concat" do end it "raises a FrozenError when Array is frozen and modification occurs" do - -> { ArraySpecs.frozen_array.concat [1] }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.concat [1] }.should.raise(FrozenError) end # see [ruby-core:23666] it "raises a FrozenError when Array is frozen and no modification occurs" do - -> { ArraySpecs.frozen_array.concat([]) }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.concat([]) }.should.raise(FrozenError) end it "appends elements to an Array with enough capacity that has been shifted" do @@ -68,7 +68,7 @@ describe "Array#concat" do it "returns self when given no arguments" do ary = [1, 2] - ary.concat.should equal(ary) + ary.concat.should.equal?(ary) ary.should == [1, 2] end end diff --git a/spec/ruby/core/array/constructor_spec.rb b/spec/ruby/core/array/constructor_spec.rb index 6f36074c45..c4398c535d 100644 --- a/spec/ruby/core/array/constructor_spec.rb +++ b/spec/ruby/core/array/constructor_spec.rb @@ -7,7 +7,7 @@ describe "Array.[]" do Array.[](5, true, nil, 'a', "Ruby", obj).should == [5, true, nil, "a", "Ruby", obj] a = ArraySpecs::MyArray.[](5, true, nil, 'a', "Ruby", obj) - a.should be_an_instance_of(ArraySpecs::MyArray) + a.should.instance_of?(ArraySpecs::MyArray) a.inspect.should == [5, true, nil, "a", "Ruby", obj].inspect end end @@ -18,7 +18,7 @@ describe "Array[]" do Array[5, true, nil, 'a', "Ruby", obj].should == Array.[](5, true, nil, "a", "Ruby", obj) a = ArraySpecs::MyArray[5, true, nil, 'a', "Ruby", obj] - a.should be_an_instance_of(ArraySpecs::MyArray) + a.should.instance_of?(ArraySpecs::MyArray) a.inspect.should == [5, true, nil, "a", "Ruby", obj].inspect end end diff --git a/spec/ruby/core/array/count_spec.rb b/spec/ruby/core/array/count_spec.rb index eaf275aeb7..e778233c16 100644 --- a/spec/ruby/core/array/count_spec.rb +++ b/spec/ruby/core/array/count_spec.rb @@ -1,4 +1,5 @@ require_relative '../../spec_helper' +require_relative 'shared/iterable_and_tolerating_size_increasing' describe "Array#count" do it "returns the number of elements" do @@ -12,4 +13,14 @@ describe "Array#count" do it "returns the number of element for which the block evaluates to true" do [:a, :b, :c].count { |s| s != :b }.should == 2 end + + it "ignores the block if there is an argument" do + -> { + [:a, :b, :b, :c].count(:b) { |e| e.size > 10 }.should == 2 + }.should complain(/given block not used/) + end + + context "when a block argument given" do + it_behaves_like :array_iterable_and_tolerating_size_increasing, :count + end end diff --git a/spec/ruby/core/array/cycle_spec.rb b/spec/ruby/core/array/cycle_spec.rb index 7219b49883..29284257e9 100644 --- a/spec/ruby/core/array/cycle_spec.rb +++ b/spec/ruby/core/array/cycle_spec.rb @@ -10,17 +10,17 @@ describe "Array#cycle" do end it "does not yield and returns nil when the array is empty and passed value is an integer" do - [].cycle(6, &@prc).should be_nil + [].cycle(6, &@prc).should == nil ScratchPad.recorded.should == [] end it "does not yield and returns nil when the array is empty and passed value is nil" do - [].cycle(nil, &@prc).should be_nil + [].cycle(nil, &@prc).should == nil ScratchPad.recorded.should == [] end it "does not yield and returns nil when passed 0" do - @array.cycle(0, &@prc).should be_nil + @array.cycle(0, &@prc).should == nil ScratchPad.recorded.should == [] end @@ -48,13 +48,13 @@ describe "Array#cycle" do it "does not rescue StopIteration when not passed a count" do -> do @array.cycle { raise StopIteration } - end.should raise_error(StopIteration) + end.should.raise(StopIteration) end it "does not rescue StopIteration when passed a count" do -> do @array.cycle(3) { raise StopIteration } - end.should raise_error(StopIteration) + end.should.raise(StopIteration) end it "iterates the array Integer(count) times when passed a Float count" do @@ -74,23 +74,23 @@ describe "Array#cycle" do count = mock("cycle count 2") count.should_receive(:to_int).and_return("2") - -> { @array.cycle(count, &@prc) }.should raise_error(TypeError) + -> { @array.cycle(count, &@prc) }.should.raise(TypeError) end it "raises a TypeError if passed a String" do - -> { @array.cycle("4") { } }.should raise_error(TypeError) + -> { @array.cycle("4") { } }.should.raise(TypeError) end it "raises a TypeError if passed an Object" do - -> { @array.cycle(mock("cycle count")) { } }.should raise_error(TypeError) + -> { @array.cycle(mock("cycle count")) { } }.should.raise(TypeError) end it "raises a TypeError if passed true" do - -> { @array.cycle(true) { } }.should raise_error(TypeError) + -> { @array.cycle(true) { } }.should.raise(TypeError) end it "raises a TypeError if passed false" do - -> { @array.cycle(false) { } }.should raise_error(TypeError) + -> { @array.cycle(false) { } }.should.raise(TypeError) end before :all do diff --git a/spec/ruby/core/array/deconstruct_spec.rb b/spec/ruby/core/array/deconstruct_spec.rb index ad67abe47b..11bb8e72c4 100644 --- a/spec/ruby/core/array/deconstruct_spec.rb +++ b/spec/ruby/core/array/deconstruct_spec.rb @@ -4,6 +4,6 @@ describe "Array#deconstruct" do it "returns self" do array = [1] - array.deconstruct.should equal array + array.deconstruct.should.equal? array end end diff --git a/spec/ruby/core/array/delete_at_spec.rb b/spec/ruby/core/array/delete_at_spec.rb index 80ec643702..1e298b6730 100644 --- a/spec/ruby/core/array/delete_at_spec.rb +++ b/spec/ruby/core/array/delete_at_spec.rb @@ -36,6 +36,6 @@ describe "Array#delete_at" do end it "raises a FrozenError on a frozen array" do - -> { [1,2,3].freeze.delete_at(0) }.should raise_error(FrozenError) + -> { [1,2,3].freeze.delete_at(0) }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/array/delete_if_spec.rb b/spec/ruby/core/array/delete_if_spec.rb index 1459cc8d04..701a612395 100644 --- a/spec/ruby/core/array/delete_if_spec.rb +++ b/spec/ruby/core/array/delete_if_spec.rb @@ -2,6 +2,7 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' require_relative 'shared/enumeratorize' require_relative 'shared/delete_if' +require_relative 'shared/iterable_and_tolerating_size_increasing' require_relative '../enumerable/shared/enumeratorized' describe "Array#delete_if" do @@ -16,7 +17,7 @@ describe "Array#delete_if" do end it "returns self" do - @a.delete_if{ true }.equal?(@a).should be_true + @a.delete_if{ true }.equal?(@a).should == true end it_behaves_like :enumeratorize, :delete_if @@ -24,29 +25,58 @@ describe "Array#delete_if" do it "returns self when called on an Array emptied with #shift" do array = [1] array.shift - array.delete_if { |x| true }.should equal(array) + array.delete_if { |x| true }.should.equal?(array) end it "returns an Enumerator if no block given, and the enumerator can modify the original array" do enum = @a.delete_if - enum.should be_an_instance_of(Enumerator) - @a.should_not be_empty + enum.should.instance_of?(Enumerator) + @a.should_not.empty? enum.each { true } - @a.should be_empty + @a.should.empty? end it "returns an Enumerator if no block given, and the array is frozen" do - @a.freeze.delete_if.should be_an_instance_of(Enumerator) + @a.freeze.delete_if.should.instance_of?(Enumerator) end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array.delete_if {} }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.delete_if {} }.should.raise(FrozenError) end it "raises a FrozenError on an empty frozen array" do - -> { ArraySpecs.empty_frozen_array.delete_if {} }.should raise_error(FrozenError) + -> { ArraySpecs.empty_frozen_array.delete_if {} }.should.raise(FrozenError) + end + + it "does not truncate the array is the block raises an exception" do + a = [1, 2, 3] + begin + a.delete_if { raise StandardError, 'Oops' } + rescue + end + + a.should == [1, 2, 3] + end + + it "only removes elements for which the block returns true, keeping the element which raised an error." do + a = [1, 2, 3, 4] + begin + a.delete_if do |e| + case e + when 2 then true + when 3 then raise StandardError, 'Oops' + else false + end + end + rescue StandardError + end + + a.should == [1, 3, 4] end it_behaves_like :enumeratorized_with_origin_size, :delete_if, [1,2,3] it_behaves_like :delete_if, :delete_if + + @value_to_return = -> _ { false } + it_behaves_like :array_iterable_and_tolerating_size_increasing, :delete_if end diff --git a/spec/ruby/core/array/delete_spec.rb b/spec/ruby/core/array/delete_spec.rb index dddbbe6bd3..0d80b2839d 100644 --- a/spec/ruby/core/array/delete_spec.rb +++ b/spec/ruby/core/array/delete_spec.rb @@ -41,6 +41,6 @@ describe "Array#delete" do end it "raises a FrozenError on a frozen array" do - -> { [1, 2, 3].freeze.delete(1) }.should raise_error(FrozenError) + -> { [1, 2, 3].freeze.delete(1) }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/array/difference_spec.rb b/spec/ruby/core/array/difference_spec.rb index 9f7d4c4a1a..63e32feca0 100644 --- a/spec/ruby/core/array/difference_spec.rb +++ b/spec/ruby/core/array/difference_spec.rb @@ -8,11 +8,11 @@ describe "Array#difference" do it "returns a copy when called without any parameter" do x = [1, 2, 3, 2] x.difference.should == x - x.difference.should_not equal x + x.difference.should_not.equal? x end it "does not return subclass instances for Array subclasses" do - ArraySpecs::MyArray[1, 2, 3].difference.should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].difference.should.instance_of?(Array) end it "accepts multiple arguments" do diff --git a/spec/ruby/core/array/dig_spec.rb b/spec/ruby/core/array/dig_spec.rb index f2d8ff47fd..4166ff9f1f 100644 --- a/spec/ruby/core/array/dig_spec.rb +++ b/spec/ruby/core/array/dig_spec.rb @@ -4,7 +4,7 @@ describe "Array#dig" do it "returns #at with one arg" do ['a'].dig(0).should == 'a' - ['a'].dig(1).should be_nil + ['a'].dig(1).should == nil end it "recurses array elements" do @@ -22,20 +22,20 @@ describe "Array#dig" do it "raises a TypeError for a non-numeric index" do -> { ['a'].dig(:first) - }.should raise_error(TypeError) + }.should.raise(TypeError) end it "raises a TypeError if any intermediate step does not respond to #dig" do a = [1, 2] -> { a.dig(0, 1) - }.should raise_error(TypeError) + }.should.raise(TypeError) end it "raises an ArgumentError if no arguments provided" do -> { [10].dig() - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "returns nil if any intermediate step is nil" do diff --git a/spec/ruby/core/array/drop_spec.rb b/spec/ruby/core/array/drop_spec.rb index f911fd9018..c0e1c9edce 100644 --- a/spec/ruby/core/array/drop_spec.rb +++ b/spec/ruby/core/array/drop_spec.rb @@ -7,7 +7,7 @@ describe "Array#drop" do end it "raises an ArgumentError if the number of elements specified is negative" do - -> { [1, 2].drop(-3) }.should raise_error(ArgumentError) + -> { [1, 2].drop(-3) }.should.raise(ArgumentError) end it "returns an empty Array if all elements are dropped" do @@ -40,25 +40,17 @@ describe "Array#drop" do end it "raises a TypeError when the passed argument can't be coerced to Integer" do - -> { [1, 2].drop("cat") }.should raise_error(TypeError) + -> { [1, 2].drop("cat") }.should.raise(TypeError) end it "raises a TypeError when the passed argument isn't an integer and #to_int returns non-Integer" do obj = mock("to_int") obj.should_receive(:to_int).and_return("cat") - -> { [1, 2].drop(obj) }.should raise_error(TypeError) + -> { [1, 2].drop(obj) }.should.raise(TypeError) end - ruby_version_is ''...'3.0' do - it 'returns a subclass instance for Array subclasses' do - ArraySpecs::MyArray[1, 2, 3, 4, 5].drop(1).should be_an_instance_of(ArraySpecs::MyArray) - end - end - - ruby_version_is '3.0' do - it 'returns a Array instance for Array subclasses' do - ArraySpecs::MyArray[1, 2, 3, 4, 5].drop(1).should be_an_instance_of(Array) - end + it 'returns a Array instance for Array subclasses' do + ArraySpecs::MyArray[1, 2, 3, 4, 5].drop(1).should.instance_of?(Array) end end diff --git a/spec/ruby/core/array/drop_while_spec.rb b/spec/ruby/core/array/drop_while_spec.rb index bb783d22a5..4fead3ff06 100644 --- a/spec/ruby/core/array/drop_while_spec.rb +++ b/spec/ruby/core/array/drop_while_spec.rb @@ -1,7 +1,11 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative 'shared/iterable_and_tolerating_size_increasing' describe "Array#drop_while" do + @value_to_return = -> _ { true } + it_behaves_like :array_iterable_and_tolerating_size_increasing, :drop_while + it "removes elements from the start of the array while the block evaluates to true" do [1, 2, 3, 4].drop_while { |n| n < 4 }.should == [4] end @@ -14,15 +18,7 @@ describe "Array#drop_while" do [1, 2, 3, false, 5].drop_while { |n| n }.should == [false, 5] end - ruby_version_is ''...'3.0' do - it 'returns a subclass instance for Array subclasses' do - ArraySpecs::MyArray[1, 2, 3, 4, 5].drop_while { |n| n < 4 }.should be_an_instance_of(ArraySpecs::MyArray) - end - end - - ruby_version_is '3.0' do - it 'returns a Array instance for Array subclasses' do - ArraySpecs::MyArray[1, 2, 3, 4, 5].drop_while { |n| n < 4 }.should be_an_instance_of(Array) - end + it 'returns a Array instance for Array subclasses' do + ArraySpecs::MyArray[1, 2, 3, 4, 5].drop_while { |n| n < 4 }.should.instance_of?(Array) end end diff --git a/spec/ruby/core/array/dup_spec.rb b/spec/ruby/core/array/dup_spec.rb index 17f467d5fc..f14aeca3b5 100644 --- a/spec/ruby/core/array/dup_spec.rb +++ b/spec/ruby/core/array/dup_spec.rb @@ -12,8 +12,8 @@ describe "Array#dup" do aa = a.dup bb = b.dup - aa.frozen?.should be_false - bb.frozen?.should be_false + aa.frozen?.should == false + bb.frozen?.should == false end it "does not copy singleton methods" do @@ -23,9 +23,9 @@ describe "Array#dup" do aa = a.dup bb = b.dup - a.respond_to?(:a_singleton_method).should be_true - b.respond_to?(:a_singleton_method).should be_false - aa.respond_to?(:a_singleton_method).should be_false - bb.respond_to?(:a_singleton_method).should be_false + a.respond_to?(:a_singleton_method).should == true + b.respond_to?(:a_singleton_method).should == false + aa.respond_to?(:a_singleton_method).should == false + bb.respond_to?(:a_singleton_method).should == false end end diff --git a/spec/ruby/core/array/each_index_spec.rb b/spec/ruby/core/array/each_index_spec.rb index 51af5842c4..b238a89d8a 100644 --- a/spec/ruby/core/array/each_index_spec.rb +++ b/spec/ruby/core/array/each_index_spec.rb @@ -5,7 +5,7 @@ require_relative '../enumerable/shared/enumeratorized' # Modifying a collection while the contents are being iterated # gives undefined behavior. See -# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/23633 +# https://blade.ruby-lang.org/ruby-core/23633 describe "Array#each_index" do before :each do @@ -20,7 +20,7 @@ describe "Array#each_index" do it "returns self" do a = [:a, :b, :c] - a.each_index { |i| }.should equal(a) + a.each_index { |i| }.should.equal?(a) end it "is not confused by removing elements from the front" do @@ -40,3 +40,19 @@ describe "Array#each_index" do it_behaves_like :enumeratorize, :each_index it_behaves_like :enumeratorized_with_origin_size, :each_index, [1,2,3] end + +describe "Array#each_index" do + it "tolerates increasing an array size during iteration" do + array = [:a, :b, :c] + ScratchPad.record [] + i = 0 + + array.each_index do |index| + ScratchPad << index + array << i if i < 100 + i += 1 + end + + ScratchPad.recorded.should == (0..102).to_a # element indices + end +end diff --git a/spec/ruby/core/array/each_spec.rb b/spec/ruby/core/array/each_spec.rb index cf8e9da6d8..73a4c36b17 100644 --- a/spec/ruby/core/array/each_spec.rb +++ b/spec/ruby/core/array/each_spec.rb @@ -1,18 +1,19 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' require_relative 'shared/enumeratorize' +require_relative 'shared/iterable_and_tolerating_size_increasing' require_relative '../enumerable/shared/enumeratorized' # Mutating the array while it is being iterated is discouraged as it can result in confusing behavior. # Yet a Ruby implementation must not crash in such a case, and following the simple CRuby behavior makes sense. # CRuby simply reads the array storage and checks the size for every iteration; -# like `i = 0; while i < size; yield self[i]; end` +# like `i = 0; while i < size; yield self[i]; i += 1; end` describe "Array#each" do it "yields each element to the block" do a = [] x = [1, 2, 3] - x.each { |item| a << item }.should equal(x) + x.each { |item| a << item }.should.equal?(x) a.should == [1, 2, 3] end @@ -75,3 +76,7 @@ describe "Array#each" do it_behaves_like :enumeratorize, :each it_behaves_like :enumeratorized_with_origin_size, :each, [1,2,3] end + +describe "Array#each" do + it_behaves_like :array_iterable_and_tolerating_size_increasing, :each +end diff --git a/spec/ruby/core/array/element_reference_spec.rb b/spec/ruby/core/array/element_reference_spec.rb index 31e5578a09..eb41a9e199 100644 --- a/spec/ruby/core/array/element_reference_spec.rb +++ b/spec/ruby/core/array/element_reference_spec.rb @@ -39,12 +39,12 @@ describe "Array.[]" do end it "returns an instance of the subclass" do - ArraySpecs::MyArray[1, 2, 3].should be_an_instance_of(ArraySpecs::MyArray) + ArraySpecs::MyArray[1, 2, 3].should.instance_of?(ArraySpecs::MyArray) end it "does not call #initialize on the subclass instance" do ArraySpecs::MyArray[1, 2, 3].should == [1, 2, 3] - ScratchPad.recorded.should be_nil + ScratchPad.recorded.should == nil end end end diff --git a/spec/ruby/core/array/element_set_spec.rb b/spec/ruby/core/array/element_set_spec.rb index df5ca9582e..671e4338de 100644 --- a/spec/ruby/core/array/element_set_spec.rb +++ b/spec/ruby/core/array/element_set_spec.rb @@ -95,8 +95,8 @@ describe "Array#[]=" do it "checks frozen before attempting to coerce arguments" do a = [1,2,3,4].freeze - -> {a[:foo] = 1}.should raise_error(FrozenError) - -> {a[:foo, :bar] = 1}.should raise_error(FrozenError) + -> {a[:foo] = 1}.should.raise(FrozenError) + -> {a[:foo, :bar] = 1}.should.raise(FrozenError) end it "sets elements in the range arguments when passed ranges" do @@ -197,25 +197,25 @@ describe "Array#[]=" do a[to .. from] = ["x"] a.should == [1, "a", "b", "x", "c", 4] - -> { a["a" .. "b"] = [] }.should raise_error(TypeError) - -> { a[from .. "b"] = [] }.should raise_error(TypeError) + -> { a["a" .. "b"] = [] }.should.raise(TypeError) + -> { a[from .. "b"] = [] }.should.raise(TypeError) end it "raises an IndexError when passed indexes out of bounds" do a = [1, 2, 3, 4] - -> { a[-5] = "" }.should raise_error(IndexError) - -> { a[-5, -1] = "" }.should raise_error(IndexError) - -> { a[-5, 0] = "" }.should raise_error(IndexError) - -> { a[-5, 1] = "" }.should raise_error(IndexError) - -> { a[-5, 2] = "" }.should raise_error(IndexError) - -> { a[-5, 10] = "" }.should raise_error(IndexError) - - -> { a[-5..-5] = "" }.should raise_error(RangeError) - -> { a[-5...-5] = "" }.should raise_error(RangeError) - -> { a[-5..-4] = "" }.should raise_error(RangeError) - -> { a[-5...-4] = "" }.should raise_error(RangeError) - -> { a[-5..10] = "" }.should raise_error(RangeError) - -> { a[-5...10] = "" }.should raise_error(RangeError) + -> { a[-5] = "" }.should.raise(IndexError) + -> { a[-5, -1] = "" }.should.raise(IndexError) + -> { a[-5, 0] = "" }.should.raise(IndexError) + -> { a[-5, 1] = "" }.should.raise(IndexError) + -> { a[-5, 2] = "" }.should.raise(IndexError) + -> { a[-5, 10] = "" }.should.raise(IndexError) + + -> { a[-5..-5] = "" }.should.raise(RangeError) + -> { a[-5...-5] = "" }.should.raise(RangeError) + -> { a[-5..-4] = "" }.should.raise(RangeError) + -> { a[-5...-4] = "" }.should.raise(RangeError) + -> { a[-5..10] = "" }.should.raise(RangeError) + -> { a[-5...10] = "" }.should.raise(RangeError) # ok a[0..-9] = [1] @@ -239,7 +239,7 @@ describe "Array#[]=" do end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array[0, 0] = [] }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array[0, 0] = [] }.should.raise(FrozenError) end end @@ -349,12 +349,12 @@ describe "Array#[]= with [index, count]" do it "raises an IndexError when passed start and negative length" do a = [1, 2, 3, 4] - -> { a[-2, -1] = "" }.should raise_error(IndexError) - -> { a[0, -1] = "" }.should raise_error(IndexError) - -> { a[2, -1] = "" }.should raise_error(IndexError) - -> { a[4, -1] = "" }.should raise_error(IndexError) - -> { a[10, -1] = "" }.should raise_error(IndexError) - -> { [1, 2, 3, 4, 5][2, -1] = [7, 8] }.should raise_error(IndexError) + -> { a[-2, -1] = "" }.should.raise(IndexError) + -> { a[0, -1] = "" }.should.raise(IndexError) + -> { a[2, -1] = "" }.should.raise(IndexError) + -> { a[4, -1] = "" }.should.raise(IndexError) + -> { a[10, -1] = "" }.should.raise(IndexError) + -> { [1, 2, 3, 4, 5][2, -1] = [7, 8] }.should.raise(IndexError) end end diff --git a/spec/ruby/core/array/eql_spec.rb b/spec/ruby/core/array/eql_spec.rb index 8565b94c60..9a7447c2e8 100644 --- a/spec/ruby/core/array/eql_spec.rb +++ b/spec/ruby/core/array/eql_spec.rb @@ -6,7 +6,7 @@ describe "Array#eql?" do it_behaves_like :array_eql, :eql? it "returns false if any corresponding elements are not #eql?" do - [1, 2, 3, 4].should_not eql([1, 2, 3, 4.0]) + [1, 2, 3, 4].should_not.eql?([1, 2, 3, 4.0]) end it "returns false if other is not a kind of Array" do @@ -14,6 +14,6 @@ describe "Array#eql?" do obj.should_not_receive(:to_ary) obj.should_not_receive(:eql?) - [1, 2, 3].should_not eql(obj) + [1, 2, 3].should_not.eql?(obj) end end diff --git a/spec/ruby/core/array/equal_value_spec.rb b/spec/ruby/core/array/equal_value_spec.rb index a82e07b218..4f7c0ce5e3 100644 --- a/spec/ruby/core/array/equal_value_spec.rb +++ b/spec/ruby/core/array/equal_value_spec.rb @@ -10,17 +10,17 @@ describe "Array#==" do obj.should_receive(:respond_to?).at_least(1).with(:to_ary).and_return(true) obj.should_receive(:==).with([1]).at_least(1).and_return(true) - ([1] == obj).should be_true - ([[1]] == [obj]).should be_true - ([[[1], 3], 2] == [[obj, 3], 2]).should be_true + ([1] == obj).should == true + ([[1]] == [obj]).should == true + ([[[1], 3], 2] == [[obj, 3], 2]).should == true # recursive arrays arr1 = [[1]] arr1 << arr1 arr2 = [obj] arr2 << arr2 - (arr1 == arr2).should be_true - (arr2 == arr1).should be_true + (arr1 == arr2).should == true + (arr2 == arr1).should == true end it "returns false if any corresponding elements are not #==" do diff --git a/spec/ruby/core/array/fetch_spec.rb b/spec/ruby/core/array/fetch_spec.rb index b81c0b48d7..ee94cfcbb2 100644 --- a/spec/ruby/core/array/fetch_spec.rb +++ b/spec/ruby/core/array/fetch_spec.rb @@ -12,9 +12,9 @@ describe "Array#fetch" do end it "raises an IndexError if there is no element at index" do - -> { [1, 2, 3].fetch(3) }.should raise_error(IndexError) - -> { [1, 2, 3].fetch(-4) }.should raise_error(IndexError) - -> { [].fetch(0) }.should raise_error(IndexError) + -> { [1, 2, 3].fetch(3) }.should.raise(IndexError, "index 3 outside of array bounds: -3...3") + -> { [1, 2, 3].fetch(-4) }.should.raise(IndexError, "index -4 outside of array bounds: -3...3") + -> { [].fetch(0) }.should.raise(IndexError, "index 0 outside of array bounds: 0...0") end it "returns default if there is no element at index if passed a default value" do @@ -33,7 +33,7 @@ describe "Array#fetch" do o = mock('5') def o.to_int(); 5; end - [1, 2, 3].fetch(o) { |i| i }.should equal(o) + [1, 2, 3].fetch(o) { |i| i }.should.equal?(o) end it "gives precedence to the default block over the default argument" do @@ -50,6 +50,6 @@ describe "Array#fetch" do end it "raises a TypeError when the passed argument can't be coerced to Integer" do - -> { [].fetch("cat") }.should raise_error(TypeError) + -> { [].fetch("cat") }.should.raise(TypeError, "no implicit conversion of String into Integer") end end diff --git a/spec/ruby/core/array/fetch_values_spec.rb b/spec/ruby/core/array/fetch_values_spec.rb new file mode 100644 index 0000000000..237d6ad7f7 --- /dev/null +++ b/spec/ruby/core/array/fetch_values_spec.rb @@ -0,0 +1,55 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Array#fetch_values" do + before :each do + @array = [:a, :b, :c] + end + + ruby_version_is "3.4" do + describe "with matched indexes" do + it "returns the values for indexes" do + @array.fetch_values(0).should == [:a] + @array.fetch_values(0, 2).should == [:a, :c] + @array.fetch_values(-1).should == [:c] + end + + it "returns the values for indexes ordered in the order of the requested indexes" do + @array.fetch_values(2, 0).should == [:c, :a] + end + end + + describe "with unmatched indexes" do + it "raises a index error if no block is provided" do + -> { @array.fetch_values(0, 1, 44) }.should.raise(IndexError, "index 44 outside of array bounds: -3...3") + end + + it "returns the default value from block" do + @array.fetch_values(44) { |index| "`#{index}' is not found" }.should == ["`44' is not found"] + @array.fetch_values(0, 44) { |index| "`#{index}' is not found" }.should == [:a, "`44' is not found"] + end + end + + describe "without keys" do + it "returns an empty Array" do + @array.fetch_values.should == [] + end + end + + it "tries to convert the passed argument to an Integer using #to_int" do + obj = mock('to_int') + obj.should_receive(:to_int).and_return(2) + @array.fetch_values(obj).should == [:c] + end + + it "does not support a Range object as argument" do + -> { + @array.fetch_values(1..2) + }.should.raise(TypeError, "no implicit conversion of Range into Integer") + end + + it "raises a TypeError when the passed argument can't be coerced to Integer" do + -> { [].fetch_values("cat") }.should.raise(TypeError, "no implicit conversion of String into Integer") + end + end +end diff --git a/spec/ruby/core/array/fill_spec.rb b/spec/ruby/core/array/fill_spec.rb index 23728414be..e4d51bd998 100644 --- a/spec/ruby/core/array/fill_spec.rb +++ b/spec/ruby/core/array/fill_spec.rb @@ -10,7 +10,7 @@ describe "Array#fill" do it "returns self" do ary = [1, 2, 3] - ary.fill(:a).should equal(ary) + ary.fill(:a).should.equal?(ary) end it "is destructive" do @@ -21,14 +21,14 @@ describe "Array#fill" do it "does not replicate the filler" do ary = [1, 2, 3, 4] - str = "x" + str = +"x" ary.fill(str).should == [str, str, str, str] str << "y" ary.should == [str, str, str, str] - ary[0].should equal(str) - ary[1].should equal(str) - ary[2].should equal(str) - ary[3].should equal(str) + ary[0].should.equal?(str) + ary[1].should.equal?(str) + ary[2].should.equal?(str) + ary[3].should.equal?(str) end it "replaces all elements in the array with the filler if not given a index nor a length" do @@ -44,33 +44,71 @@ describe "Array#fill" do end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array.fill('x') }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.fill('x') }.should.raise(FrozenError) end it "raises a FrozenError on an empty frozen array" do - -> { ArraySpecs.empty_frozen_array.fill('x') }.should raise_error(FrozenError) + -> { ArraySpecs.empty_frozen_array.fill('x') }.should.raise(FrozenError) end it "raises an ArgumentError if 4 or more arguments are passed when no block given" do - -> { [].fill('a') }.should_not raise_error(ArgumentError) - - -> { [].fill('a', 1) }.should_not raise_error(ArgumentError) - - -> { [].fill('a', 1, 2) }.should_not raise_error(ArgumentError) - -> { [].fill('a', 1, 2, true) }.should raise_error(ArgumentError) + [].fill('a').should == [] + [].fill('a', 1).should == [] + [].fill('a', 1, 2).should == [nil, 'a', 'a'] + -> { [].fill('a', 1, 2, true) }.should.raise(ArgumentError) end it "raises an ArgumentError if no argument passed and no block given" do - -> { [].fill }.should raise_error(ArgumentError) + -> { [].fill }.should.raise(ArgumentError) end it "raises an ArgumentError if 3 or more arguments are passed when a block given" do - -> { [].fill() {|i|} }.should_not raise_error(ArgumentError) + [].fill() {|i|}.should == [] + [].fill(1) {|i|}.should == [] + [].fill(1, 2) {|i|}.should == [nil, nil, nil] + -> { [].fill(1, 2, true) {|i|} }.should.raise(ArgumentError) + end + + it "does not truncate the array is the block raises an exception" do + a = [1, 2, 3] + begin + a.fill { raise StandardError, 'Oops' } + rescue + end - -> { [].fill(1) {|i|} }.should_not raise_error(ArgumentError) + a.should == [1, 2, 3] + end + + it "only changes elements before error is raised, keeping the element which raised an error." do + a = [1, 2, 3, 4] + begin + a.fill do |i| + case i + when 0 then -1 + when 1 then -2 + when 2 then raise StandardError, 'Oops' + else 0 + end + end + rescue StandardError + end + + a.should == [-1, -2, 3, 4] + end + + it "tolerates increasing an array size during iteration" do + array = [:a, :b, :c] + ScratchPad.record [] + i = 0 + + array.fill do |index| + ScratchPad << index + array << i if i < 100 + i++ + index + end - -> { [].fill(1, 2) {|i|} }.should_not raise_error(ArgumentError) - -> { [].fill(1, 2, true) {|i|} }.should raise_error(ArgumentError) + ScratchPad.recorded.should == [0, 1, 2] end end @@ -169,25 +207,25 @@ describe "Array#fill with (filler, index, length)" do [1, 2, 3, 4, 5].fill(-2, -2, &@never_passed).should == [1, 2, 3, 4, 5] end - # See: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/17481 + # See: https://blade.ruby-lang.org/ruby-core/17481 it "does not raise an exception if the given length is negative and its absolute value does not exceed the index" do - -> { [1, 2, 3, 4].fill('a', 3, -1)}.should_not raise_error(ArgumentError) - -> { [1, 2, 3, 4].fill('a', 3, -2)}.should_not raise_error(ArgumentError) - -> { [1, 2, 3, 4].fill('a', 3, -3)}.should_not raise_error(ArgumentError) + [1, 2, 3, 4].fill('a', 3, -1).should == [1, 2, 3, 4] + [1, 2, 3, 4].fill('a', 3, -2).should == [1, 2, 3, 4] + [1, 2, 3, 4].fill('a', 3, -3).should == [1, 2, 3, 4] - -> { [1, 2, 3, 4].fill(3, -1, &@never_passed)}.should_not raise_error(ArgumentError) - -> { [1, 2, 3, 4].fill(3, -2, &@never_passed)}.should_not raise_error(ArgumentError) - -> { [1, 2, 3, 4].fill(3, -3, &@never_passed)}.should_not raise_error(ArgumentError) + [1, 2, 3, 4].fill(3, -1, &@never_passed).should == [1, 2, 3, 4] + [1, 2, 3, 4].fill(3, -2, &@never_passed).should == [1, 2, 3, 4] + [1, 2, 3, 4].fill(3, -3, &@never_passed).should == [1, 2, 3, 4] end it "does not raise an exception even if the given length is negative and its absolute value exceeds the index" do - -> { [1, 2, 3, 4].fill('a', 3, -4)}.should_not raise_error(ArgumentError) - -> { [1, 2, 3, 4].fill('a', 3, -5)}.should_not raise_error(ArgumentError) - -> { [1, 2, 3, 4].fill('a', 3, -10000)}.should_not raise_error(ArgumentError) + [1, 2, 3, 4].fill('a', 3, -4).should == [1, 2, 3, 4] + [1, 2, 3, 4].fill('a', 3, -5).should == [1, 2, 3, 4] + [1, 2, 3, 4].fill('a', 3, -10000).should == [1, 2, 3, 4] - -> { [1, 2, 3, 4].fill(3, -4, &@never_passed)}.should_not raise_error(ArgumentError) - -> { [1, 2, 3, 4].fill(3, -5, &@never_passed)}.should_not raise_error(ArgumentError) - -> { [1, 2, 3, 4].fill(3, -10000, &@never_passed)}.should_not raise_error(ArgumentError) + [1, 2, 3, 4].fill(3, -4, &@never_passed).should == [1, 2, 3, 4] + [1, 2, 3, 4].fill(3, -5, &@never_passed).should == [1, 2, 3, 4] + [1, 2, 3, 4].fill(3, -10000, &@never_passed).should == [1, 2, 3, 4] end it "tries to convert the second and third arguments to Integers using #to_int" do @@ -199,24 +237,24 @@ describe "Array#fill with (filler, index, length)" do end it "raises a TypeError if the index is not numeric" do - -> { [].fill 'a', true }.should raise_error(TypeError) + -> { [].fill 'a', true }.should.raise(TypeError) obj = mock('nonnumeric') - -> { [].fill('a', obj) }.should raise_error(TypeError) + -> { [].fill('a', obj) }.should.raise(TypeError) end it "raises a TypeError when the length is not numeric" do - -> { [1, 2, 3].fill("x", 1, "foo") }.should raise_error(TypeError, /no implicit conversion of String into Integer/) - -> { [1, 2, 3].fill("x", 1, :"foo") }.should raise_error(TypeError, /no implicit conversion of Symbol into Integer/) - -> { [1, 2, 3].fill("x", 1, Object.new) }.should raise_error(TypeError, /no implicit conversion of Object into Integer/) + -> { [1, 2, 3].fill("x", 1, "foo") }.should.raise(TypeError, /no implicit conversion of String into Integer/) + -> { [1, 2, 3].fill("x", 1, :"foo") }.should.raise(TypeError, /no implicit conversion of Symbol into Integer/) + -> { [1, 2, 3].fill("x", 1, Object.new) }.should.raise(TypeError, /no implicit conversion of Object into Integer/) end not_supported_on :opal do it "raises an ArgumentError or RangeError for too-large sizes" do error_types = [RangeError, ArgumentError] arr = [1, 2, 3] - -> { arr.fill(10, 1, fixnum_max) }.should raise_error { |err| error_types.should include(err.class) } - -> { arr.fill(10, 1, bignum_value) }.should raise_error(RangeError) + -> { arr.fill(10, 1, fixnum_max) }.should.raise { |err| error_types.should.include?(err.class) } + -> { arr.fill(10, 1, bignum_value) }.should.raise(RangeError) end end end @@ -246,7 +284,7 @@ describe "Array#fill with (filler, range)" do end it "raises a TypeError with range and length argument" do - -> { [].fill('x', 0 .. 2, 5) }.should raise_error(TypeError) + -> { [].fill('x', 0 .. 2, 5) }.should.raise(TypeError) end it "replaces elements between the (-m)th to the last and the (n+1)th from the first if given an range m..n where m < 0 and n >= 0" do @@ -298,13 +336,13 @@ describe "Array#fill with (filler, range)" do end it "raises an exception if some of the given range lies before the first of the array" do - -> { [1, 2, 3].fill('x', -5..-3) }.should raise_error(RangeError) - -> { [1, 2, 3].fill('x', -5...-3) }.should raise_error(RangeError) - -> { [1, 2, 3].fill('x', -5..-4) }.should raise_error(RangeError) + -> { [1, 2, 3].fill('x', -5..-3) }.should.raise(RangeError) + -> { [1, 2, 3].fill('x', -5...-3) }.should.raise(RangeError) + -> { [1, 2, 3].fill('x', -5..-4) }.should.raise(RangeError) - -> { [1, 2, 3].fill(-5..-3, &@never_passed) }.should raise_error(RangeError) - -> { [1, 2, 3].fill(-5...-3, &@never_passed) }.should raise_error(RangeError) - -> { [1, 2, 3].fill(-5..-4, &@never_passed) }.should raise_error(RangeError) + -> { [1, 2, 3].fill(-5..-3, &@never_passed) }.should.raise(RangeError) + -> { [1, 2, 3].fill(-5...-3, &@never_passed) }.should.raise(RangeError) + -> { [1, 2, 3].fill(-5..-4, &@never_passed) }.should.raise(RangeError) end it "tries to convert the start and end of the passed range to Integers using #to_int" do @@ -319,7 +357,7 @@ describe "Array#fill with (filler, range)" do it "raises a TypeError if the start or end of the passed range is not numeric" do obj = mock('nonnumeric') def obj.<=>(rhs); rhs == self ? 0 : nil end - -> { [].fill('a', obj..obj) }.should raise_error(TypeError) + -> { [].fill('a', obj..obj) }.should.raise(TypeError) end it "works with endless ranges" do diff --git a/spec/ruby/core/array/filter_spec.rb b/spec/ruby/core/array/filter_spec.rb index 156ad14f9c..7807c3886d 100644 --- a/spec/ruby/core/array/filter_spec.rb +++ b/spec/ruby/core/array/filter_spec.rb @@ -7,7 +7,7 @@ end describe "Array#filter!" do it "returns nil if no changes were made in the array" do - [1, 2, 3].filter! { true }.should be_nil + [1, 2, 3].filter! { true }.should == nil end it_behaves_like :keep_if, :filter! diff --git a/spec/ruby/core/array/first_spec.rb b/spec/ruby/core/array/first_spec.rb index 66eeba6565..2c343ac8f6 100644 --- a/spec/ruby/core/array/first_spec.rb +++ b/spec/ruby/core/array/first_spec.rb @@ -30,11 +30,11 @@ describe "Array#first" do end it "raises an ArgumentError when count is negative" do - -> { [1, 2].first(-1) }.should raise_error(ArgumentError) + -> { [1, 2].first(-1) }.should.raise(ArgumentError) end it "raises a RangeError when count is a Bignum" do - -> { [].first(bignum_value) }.should raise_error(RangeError) + -> { [].first(bignum_value) }.should.raise(RangeError) end it "returns the entire array when count > length" do @@ -53,10 +53,10 @@ describe "Array#first" do it "properly handles recursive arrays" do empty = ArraySpecs.empty_recursive_array - empty.first.should equal(empty) + empty.first.should.equal?(empty) ary = ArraySpecs.head_recursive_array - ary.first.should equal(ary) + ary.first.should.equal?(ary) end it "tries to convert the passed argument to an Integer using #to_int" do @@ -66,19 +66,19 @@ describe "Array#first" do end it "raises a TypeError if the passed argument is not numeric" do - -> { [1,2].first(nil) }.should raise_error(TypeError) - -> { [1,2].first("a") }.should raise_error(TypeError) + -> { [1,2].first(nil) }.should.raise(TypeError) + -> { [1,2].first("a") }.should.raise(TypeError) obj = mock("nonnumeric") - -> { [1,2].first(obj) }.should raise_error(TypeError) + -> { [1,2].first(obj) }.should.raise(TypeError) end it "does not return subclass instance when passed count on Array subclasses" do - ArraySpecs::MyArray[].first(0).should be_an_instance_of(Array) - ArraySpecs::MyArray[].first(2).should be_an_instance_of(Array) - ArraySpecs::MyArray[1, 2, 3].first(0).should be_an_instance_of(Array) - ArraySpecs::MyArray[1, 2, 3].first(1).should be_an_instance_of(Array) - ArraySpecs::MyArray[1, 2, 3].first(2).should be_an_instance_of(Array) + ArraySpecs::MyArray[].first(0).should.instance_of?(Array) + ArraySpecs::MyArray[].first(2).should.instance_of?(Array) + ArraySpecs::MyArray[1, 2, 3].first(0).should.instance_of?(Array) + ArraySpecs::MyArray[1, 2, 3].first(1).should.instance_of?(Array) + ArraySpecs::MyArray[1, 2, 3].first(2).should.instance_of?(Array) end it "is not destructive" do diff --git a/spec/ruby/core/array/fixtures/classes.rb b/spec/ruby/core/array/fixtures/classes.rb index aa5fecd96b..05283c0f74 100644 --- a/spec/ruby/core/array/fixtures/classes.rb +++ b/spec/ruby/core/array/fixtures/classes.rb @@ -56,23 +56,20 @@ module ArraySpecs 101.621, 102.816, 104.010, 105.202, 106.393, 107.583, 108.771, 109.958, 111.144, 112.329, 113.512, 114.695, 115.876, 117.057, 118.236, 119.414, 120.591, 121.767, 122.942, 124.116, 125.289, 126.462, 127.633, 128.803, 129.973, 131.141, 132.309, 133.476, 134.642, 135.807, - ] + ] def self.measure_sample_fairness(size, samples, iters) ary = Array.new(size) { |x| x } + expected = iters.fdiv size (samples).times do |i| chi_results = [] 3.times do - counts = Array.new(size) { 0 } - expected = iters / size + counts = Array.new(size, 0) iters.times do x = ary.sample(samples)[i] counts[x] += 1 end - chi_squared = 0.0 - counts.each do |count| - chi_squared += (((count - expected) ** 2) * 1.0 / expected) - end + chi_squared = counts.sum {|count| (count - expected) ** 2} / expected chi_results << chi_squared break if chi_squared <= CHI_SQUARED_CRITICAL_VALUES[size] end @@ -83,17 +80,14 @@ module ArraySpecs def self.measure_sample_fairness_large_sample_size(size, samples, iters) ary = Array.new(size) { |x| x } - counts = Array.new(size) { 0 } - expected = iters * samples / size + counts = Array.new(size, 0) + expected = (iters * samples).fdiv size iters.times do ary.sample(samples).each do |sample| counts[sample] += 1 end end - chi_squared = 0.0 - counts.each do |count| - chi_squared += (((count - expected) ** 2) * 1.0 / expected) - end + chi_squared = counts.sum {|count| (count - expected) ** 2} / expected # Chi squared critical values for tests with 4 degrees of freedom # Values obtained from NIST Engineering Statistic Handbook at @@ -160,6 +154,16 @@ module ArraySpecs end end + class ArrayMethodMissing + def initialize(*values, &block) + @values = values; + end + + def method_missing(name, *args) + @values + end + end + class SortSame def <=>(other); 0; end def ==(other); true; end @@ -213,366 +217,370 @@ module ArraySpecs obj end - LargeArray = ["test_create_table_with_force_true_does_not_drop_nonexisting_table", - "test_add_table", - "assert_difference", - "assert_operator", - "instance_variables", - "class", - "instance_variable_get", - "__class__", - "expects", - "assert_no_difference", - "name", - "assert_blank", - "assert_not_same", - "is_a?", - "test_add_table_with_decimals", - "test_create_table_with_timestamps_should_create_datetime_columns", - "assert_present", - "assert_no_match", - "__instance_of__", - "assert_deprecated", - "assert", - "assert_throws", - "kind_of?", - "try", - "__instance_variable_get__", - "object_id", - "timeout", - "instance_variable_set", - "assert_nothing_thrown", - "__instance_variable_set__", - "copy_object", - "test_create_table_with_timestamps_should_create_datetime_columns_with_options", - "assert_not_deprecated", - "assert_in_delta", - "id", - "copy_metaclass", - "test_create_table_without_a_block", - "dup", - "assert_not_nil", - "send", - "__instance_variables__", - "to_sql", - "mock", - "assert_send", - "instance_variable_defined?", - "clone", - "require", - "test_migrator", - "__instance_variable_defined_eh__", - "frozen?", - "test_add_column_not_null_with_default", - "freeze", - "test_migrator_one_up", - "test_migrator_one_down", - "singleton_methods", - "method_exists?", - "create_fixtures", - "test_migrator_one_up_one_down", - "test_native_decimal_insert_manual_vs_automatic", - "instance_exec", - "__is_a__", - "test_migrator_double_up", - "stub", - "private_methods", - "stubs", - "test_migrator_double_down", - "fixture_path", - "private_singleton_methods", - "stub_everything", - "test_migrator_one_up_with_exception_and_rollback", - "sequence", - "protected_methods", - "enum_for", - "test_finds_migrations", - "run_before_mocha", - "states", - "protected_singleton_methods", - "to_json", - "instance_values", - "==", - "mocha_setup", - "public_methods", - "test_finds_pending_migrations", - "mocha_verify", - "assert_kind_of", - "===", - "=~", - "test_relative_migrations", - "mocha_teardown", - "gem", - "mocha", - "test_only_loads_pending_migrations", - "test_add_column_with_precision_and_scale", - "require_or_load", - "eql?", - "require_dependency", - "test_native_types", - "test_target_version_zero_should_run_only_once", - "extend", - "to_matcher", - "unloadable", - "require_association", - "hash", - "__id__", - "load_dependency", - "equals", - "test_migrator_db_has_no_schema_migrations_table", - "test_migrator_verbosity", - "kind_of", - "to_yaml", - "to_bool", - "test_migrator_verbosity_off", - "taint", - "test_migrator_going_down_due_to_version_target", - "tainted?", - "mocha_inspect", - "test_migrator_rollback", - "vim", - "untaint", - "taguri=", - "test_migrator_forward", - "test_schema_migrations_table_name", - "test_proper_table_name", - "all_of", - "test_add_drop_table_with_prefix_and_suffix", - "_setup_callbacks", - "setup", - "Not", - "test_create_table_with_binary_column", - "assert_not_equal", - "enable_warnings", - "acts_like?", - "Rational", - "_removed_setup_callbacks", - "Table", - "bind", - "any_of", - "__method__", - "test_migrator_with_duplicates", - "_teardown_callbacks", - "method", - "test_migrator_with_duplicate_names", - "_removed_teardown_callbacks", - "any_parameters", - "test_migrator_with_missing_version_numbers", - "test_add_remove_single_field_using_string_arguments", - "test_create_table_with_custom_sequence_name", - "test_add_remove_single_field_using_symbol_arguments", - "_one_time_conditions_valid_14?", - "_one_time_conditions_valid_16?", - "run_callbacks", - "anything", - "silence_warnings", - "instance_variable_names", - "_fixture_path", - "copy_instance_variables_from", - "fixture_path?", - "has_entry", - "__marshal__", - "_fixture_table_names", - "__kind_of__", - "fixture_table_names?", - "test_add_rename", - "assert_equal", - "_fixture_class_names", - "fixture_class_names?", - "has_entries", - "_use_transactional_fixtures", - "people", - "test_rename_column_using_symbol_arguments", - "use_transactional_fixtures?", - "instance_eval", - "blank?", - "with_warnings", - "__nil__", - "load", - "metaclass", - "_use_instantiated_fixtures", - "has_key", - "class_eval", - "present?", - "test_rename_column", - "teardown", - "use_instantiated_fixtures?", - "method_name", - "silence_stderr", - "presence", - "test_rename_column_preserves_default_value_not_null", - "silence_stream", - "_pre_loaded_fixtures", - "__metaclass__", - "__fixnum__", - "pre_loaded_fixtures?", - "has_value", - "suppress", - "to_yaml_properties", - "test_rename_nonexistent_column", - "test_add_index", - "includes", - "find_correlate_in", - "equality_predicate_sql", - "assert_nothing_raised", - "let", - "not_predicate_sql", - "test_rename_column_with_sql_reserved_word", - "singleton_class", - "test_rename_column_with_an_index", - "display", - "taguri", - "to_yaml_style", - "test_remove_column_with_index", - "size", - "current_adapter?", - "test_remove_column_with_multi_column_index", - "respond_to?", - "test_change_type_of_not_null_column", - "is_a", - "to_a", - "test_rename_table_for_sqlite_should_work_with_reserved_words", - "require_library_or_gem", - "setup_fixtures", - "equal?", - "teardown_fixtures", - "nil?", - "fixture_table_names", - "fixture_class_names", - "test_create_table_without_id", - "use_transactional_fixtures", - "test_add_column_with_primary_key_attribute", - "repair_validations", - "use_instantiated_fixtures", - "instance_of?", - "test_create_table_adds_id", - "test_rename_table", - "pre_loaded_fixtures", - "to_enum", - "test_create_table_with_not_null_column", - "instance_of", - "test_change_column_nullability", - "optionally", - "test_rename_table_with_an_index", - "run", - "test_change_column", - "default_test", - "assert_raise", - "test_create_table_with_defaults", - "assert_nil", - "flunk", - "regexp_matches", - "duplicable?", - "reset_mocha", - "stubba_method", - "filter_backtrace", - "test_create_table_with_limits", - "responds_with", - "stubba_object", - "test_change_column_with_nil_default", - "assert_block", - "__show__", - "assert_date_from_db", - "__respond_to_eh__", - "run_in_transaction?", - "inspect", - "assert_sql", - "test_change_column_with_new_default", - "yaml_equivalent", - "build_message", - "to_s", - "test_change_column_default", - "assert_queries", - "pending", - "as_json", - "assert_no_queries", - "test_change_column_quotes_column_names", - "assert_match", - "test_keeping_default_and_notnull_constraint_on_change", - "methods", - "connection_allow_concurrency_setup", - "connection_allow_concurrency_teardown", - "test_create_table_with_primary_key_prefix_as_table_name_with_underscore", - "__send__", - "make_connection", - "assert_raises", - "tap", - "with_kcode", - "assert_instance_of", - "test_create_table_with_primary_key_prefix_as_table_name", - "assert_respond_to", - "test_change_column_default_to_null", - "assert_same", - "__extend__"] - - LargeTestArraySorted = ["test_add_column_not_null_with_default", - "test_add_column_with_precision_and_scale", - "test_add_column_with_primary_key_attribute", - "test_add_drop_table_with_prefix_and_suffix", - "test_add_index", - "test_add_remove_single_field_using_string_arguments", - "test_add_remove_single_field_using_symbol_arguments", - "test_add_rename", - "test_add_table", - "test_add_table_with_decimals", - "test_change_column", - "test_change_column_default", - "test_change_column_default_to_null", - "test_change_column_nullability", - "test_change_column_quotes_column_names", - "test_change_column_with_new_default", - "test_change_column_with_nil_default", - "test_change_type_of_not_null_column", - "test_create_table_adds_id", - "test_create_table_with_binary_column", - "test_create_table_with_custom_sequence_name", - "test_create_table_with_defaults", - "test_create_table_with_force_true_does_not_drop_nonexisting_table", - "test_create_table_with_limits", - "test_create_table_with_not_null_column", - "test_create_table_with_primary_key_prefix_as_table_name", - "test_create_table_with_primary_key_prefix_as_table_name_with_underscore", - "test_create_table_with_timestamps_should_create_datetime_columns", - "test_create_table_with_timestamps_should_create_datetime_columns_with_options", - "test_create_table_without_a_block", - "test_create_table_without_id", - "test_finds_migrations", - "test_finds_pending_migrations", - "test_keeping_default_and_notnull_constraint_on_change", - "test_migrator", - "test_migrator_db_has_no_schema_migrations_table", - "test_migrator_double_down", - "test_migrator_double_up", - "test_migrator_forward", - "test_migrator_going_down_due_to_version_target", - "test_migrator_one_down", - "test_migrator_one_up", - "test_migrator_one_up_one_down", - "test_migrator_one_up_with_exception_and_rollback", - "test_migrator_rollback", - "test_migrator_verbosity", - "test_migrator_verbosity_off", - "test_migrator_with_duplicate_names", - "test_migrator_with_duplicates", - "test_migrator_with_missing_version_numbers", - "test_native_decimal_insert_manual_vs_automatic", - "test_native_types", - "test_only_loads_pending_migrations", - "test_proper_table_name", - "test_relative_migrations", - "test_remove_column_with_index", - "test_remove_column_with_multi_column_index", - "test_rename_column", - "test_rename_column_preserves_default_value_not_null", - "test_rename_column_using_symbol_arguments", - "test_rename_column_with_an_index", - "test_rename_column_with_sql_reserved_word", - "test_rename_nonexistent_column", - "test_rename_table", - "test_rename_table_for_sqlite_should_work_with_reserved_words", - "test_rename_table_with_an_index", - "test_schema_migrations_table_name", - "test_target_version_zero_should_run_only_once"] + LargeArray = [ + "test_create_table_with_force_true_does_not_drop_nonexisting_table", + "test_add_table", + "assert_difference", + "assert_operator", + "instance_variables", + "class", + "instance_variable_get", + "__class__", + "expects", + "assert_no_difference", + "name", + "assert_blank", + "assert_not_same", + "is_a?", + "test_add_table_with_decimals", + "test_create_table_with_timestamps_should_create_datetime_columns", + "assert_present", + "assert_no_match", + "__instance_of__", + "assert_deprecated", + "assert", + "assert_throws", + "kind_of?", + "try", + "__instance_variable_get__", + "object_id", + "timeout", + "instance_variable_set", + "assert_nothing_thrown", + "__instance_variable_set__", + "copy_object", + "test_create_table_with_timestamps_should_create_datetime_columns_with_options", + "assert_not_deprecated", + "assert_in_delta", + "id", + "copy_metaclass", + "test_create_table_without_a_block", + "dup", + "assert_not_nil", + "send", + "__instance_variables__", + "to_sql", + "mock", + "assert_send", + "instance_variable_defined?", + "clone", + "require", + "test_migrator", + "__instance_variable_defined_eh__", + "frozen?", + "test_add_column_not_null_with_default", + "freeze", + "test_migrator_one_up", + "test_migrator_one_down", + "singleton_methods", + "method_exists?", + "create_fixtures", + "test_migrator_one_up_one_down", + "test_native_decimal_insert_manual_vs_automatic", + "instance_exec", + "__is_a__", + "test_migrator_double_up", + "stub", + "private_methods", + "stubs", + "test_migrator_double_down", + "fixture_path", + "private_singleton_methods", + "stub_everything", + "test_migrator_one_up_with_exception_and_rollback", + "sequence", + "protected_methods", + "enum_for", + "test_finds_migrations", + "run_before_mocha", + "states", + "protected_singleton_methods", + "to_json", + "instance_values", + "==", + "mocha_setup", + "public_methods", + "test_finds_pending_migrations", + "mocha_verify", + "assert_kind_of", + "===", + "=~", + "test_relative_migrations", + "mocha_teardown", + "gem", + "mocha", + "test_only_loads_pending_migrations", + "test_add_column_with_precision_and_scale", + "require_or_load", + "eql?", + "require_dependency", + "test_native_types", + "test_target_version_zero_should_run_only_once", + "extend", + "to_matcher", + "unloadable", + "require_association", + "hash", + "__id__", + "load_dependency", + "equals", + "test_migrator_db_has_no_schema_migrations_table", + "test_migrator_verbosity", + "kind_of", + "to_yaml", + "to_bool", + "test_migrator_verbosity_off", + "taint", + "test_migrator_going_down_due_to_version_target", + "tainted?", + "mocha_inspect", + "test_migrator_rollback", + "vim", + "untaint", + "taguri=", + "test_migrator_forward", + "test_schema_migrations_table_name", + "test_proper_table_name", + "all_of", + "test_add_drop_table_with_prefix_and_suffix", + "_setup_callbacks", + "setup", + "Not", + "test_create_table_with_binary_column", + "assert_not_equal", + "enable_warnings", + "acts_like?", + "Rational", + "_removed_setup_callbacks", + "Table", + "bind", + "any_of", + "__method__", + "test_migrator_with_duplicates", + "_teardown_callbacks", + "method", + "test_migrator_with_duplicate_names", + "_removed_teardown_callbacks", + "any_parameters", + "test_migrator_with_missing_version_numbers", + "test_add_remove_single_field_using_string_arguments", + "test_create_table_with_custom_sequence_name", + "test_add_remove_single_field_using_symbol_arguments", + "_one_time_conditions_valid_14?", + "_one_time_conditions_valid_16?", + "run_callbacks", + "anything", + "silence_warnings", + "instance_variable_names", + "_fixture_path", + "copy_instance_variables_from", + "fixture_path?", + "has_entry", + "__marshal__", + "_fixture_table_names", + "__kind_of__", + "fixture_table_names?", + "test_add_rename", + "assert_equal", + "_fixture_class_names", + "fixture_class_names?", + "has_entries", + "_use_transactional_fixtures", + "people", + "test_rename_column_using_symbol_arguments", + "use_transactional_fixtures?", + "instance_eval", + "blank?", + "with_warnings", + "__nil__", + "load", + "metaclass", + "_use_instantiated_fixtures", + "has_key", + "class_eval", + "present?", + "test_rename_column", + "teardown", + "use_instantiated_fixtures?", + "method_name", + "silence_stderr", + "presence", + "test_rename_column_preserves_default_value_not_null", + "silence_stream", + "_pre_loaded_fixtures", + "__metaclass__", + "__fixnum__", + "pre_loaded_fixtures?", + "has_value", + "suppress", + "to_yaml_properties", + "test_rename_nonexistent_column", + "test_add_index", + "includes", + "find_correlate_in", + "equality_predicate_sql", + "assert_nothing_raised", + "let", + "not_predicate_sql", + "test_rename_column_with_sql_reserved_word", + "singleton_class", + "test_rename_column_with_an_index", + "display", + "taguri", + "to_yaml_style", + "test_remove_column_with_index", + "size", + "current_adapter?", + "test_remove_column_with_multi_column_index", + "respond_to?", + "test_change_type_of_not_null_column", + "is_a", + "to_a", + "test_rename_table_for_sqlite_should_work_with_reserved_words", + "require_library_or_gem", + "setup_fixtures", + "equal?", + "teardown_fixtures", + "nil?", + "fixture_table_names", + "fixture_class_names", + "test_create_table_without_id", + "use_transactional_fixtures", + "test_add_column_with_primary_key_attribute", + "repair_validations", + "use_instantiated_fixtures", + "instance_of?", + "test_create_table_adds_id", + "test_rename_table", + "pre_loaded_fixtures", + "to_enum", + "test_create_table_with_not_null_column", + "instance_of", + "test_change_column_nullability", + "optionally", + "test_rename_table_with_an_index", + "run", + "test_change_column", + "default_test", + "assert_raise", + "test_create_table_with_defaults", + "assert_nil", + "flunk", + "regexp_matches", + "duplicable?", + "reset_mocha", + "stubba_method", + "filter_backtrace", + "test_create_table_with_limits", + "responds_with", + "stubba_object", + "test_change_column_with_nil_default", + "assert_block", + "__show__", + "assert_date_from_db", + "__respond_to_eh__", + "run_in_transaction?", + "inspect", + "assert_sql", + "test_change_column_with_new_default", + "yaml_equivalent", + "build_message", + "to_s", + "test_change_column_default", + "assert_queries", + "pending", + "as_json", + "assert_no_queries", + "test_change_column_quotes_column_names", + "assert_match", + "test_keeping_default_and_notnull_constraint_on_change", + "methods", + "connection_allow_concurrency_setup", + "connection_allow_concurrency_teardown", + "test_create_table_with_primary_key_prefix_as_table_name_with_underscore", + "__send__", + "make_connection", + "assert_raises", + "tap", + "with_kcode", + "assert_instance_of", + "test_create_table_with_primary_key_prefix_as_table_name", + "assert_respond_to", + "test_change_column_default_to_null", + "assert_same", + "__extend__", + ] + + LargeTestArraySorted = [ + "test_add_column_not_null_with_default", + "test_add_column_with_precision_and_scale", + "test_add_column_with_primary_key_attribute", + "test_add_drop_table_with_prefix_and_suffix", + "test_add_index", + "test_add_remove_single_field_using_string_arguments", + "test_add_remove_single_field_using_symbol_arguments", + "test_add_rename", + "test_add_table", + "test_add_table_with_decimals", + "test_change_column", + "test_change_column_default", + "test_change_column_default_to_null", + "test_change_column_nullability", + "test_change_column_quotes_column_names", + "test_change_column_with_new_default", + "test_change_column_with_nil_default", + "test_change_type_of_not_null_column", + "test_create_table_adds_id", + "test_create_table_with_binary_column", + "test_create_table_with_custom_sequence_name", + "test_create_table_with_defaults", + "test_create_table_with_force_true_does_not_drop_nonexisting_table", + "test_create_table_with_limits", + "test_create_table_with_not_null_column", + "test_create_table_with_primary_key_prefix_as_table_name", + "test_create_table_with_primary_key_prefix_as_table_name_with_underscore", + "test_create_table_with_timestamps_should_create_datetime_columns", + "test_create_table_with_timestamps_should_create_datetime_columns_with_options", + "test_create_table_without_a_block", + "test_create_table_without_id", + "test_finds_migrations", + "test_finds_pending_migrations", + "test_keeping_default_and_notnull_constraint_on_change", + "test_migrator", + "test_migrator_db_has_no_schema_migrations_table", + "test_migrator_double_down", + "test_migrator_double_up", + "test_migrator_forward", + "test_migrator_going_down_due_to_version_target", + "test_migrator_one_down", + "test_migrator_one_up", + "test_migrator_one_up_one_down", + "test_migrator_one_up_with_exception_and_rollback", + "test_migrator_rollback", + "test_migrator_verbosity", + "test_migrator_verbosity_off", + "test_migrator_with_duplicate_names", + "test_migrator_with_duplicates", + "test_migrator_with_missing_version_numbers", + "test_native_decimal_insert_manual_vs_automatic", + "test_native_types", + "test_only_loads_pending_migrations", + "test_proper_table_name", + "test_relative_migrations", + "test_remove_column_with_index", + "test_remove_column_with_multi_column_index", + "test_rename_column", + "test_rename_column_preserves_default_value_not_null", + "test_rename_column_using_symbol_arguments", + "test_rename_column_with_an_index", + "test_rename_column_with_sql_reserved_word", + "test_rename_nonexistent_column", + "test_rename_table", + "test_rename_table_for_sqlite_should_work_with_reserved_words", + "test_rename_table_with_an_index", + "test_schema_migrations_table_name", + "test_target_version_zero_should_run_only_once", + ] class PrivateToAry private diff --git a/spec/ruby/core/array/fixtures/encoded_strings.rb b/spec/ruby/core/array/fixtures/encoded_strings.rb index 5b85bd0e06..b5888d86ae 100644 --- a/spec/ruby/core/array/fixtures/encoded_strings.rb +++ b/spec/ruby/core/array/fixtures/encoded_strings.rb @@ -2,14 +2,14 @@ module ArraySpecs def self.array_with_usascii_and_7bit_utf8_strings [ - 'foo'.force_encoding('US-ASCII'), + 'foo'.dup.force_encoding('US-ASCII'), 'bar' ] end def self.array_with_usascii_and_utf8_strings [ - 'foo'.force_encoding('US-ASCII'), + 'foo'.dup.force_encoding('US-ASCII'), 'báz' ] end @@ -17,7 +17,7 @@ module ArraySpecs def self.array_with_7bit_utf8_and_usascii_strings [ 'bar', - 'foo'.force_encoding('US-ASCII') + 'foo'.dup.force_encoding('US-ASCII') ] end @@ -25,13 +25,13 @@ module ArraySpecs [ 'báz', 'bar', - 'foo'.force_encoding('US-ASCII') + 'foo'.dup.force_encoding('US-ASCII') ] end def self.array_with_usascii_and_utf8_strings [ - 'foo'.force_encoding('US-ASCII'), + 'foo'.dup.force_encoding('US-ASCII'), 'bar', 'báz' ] @@ -41,7 +41,7 @@ module ArraySpecs [ 'bar', 'báz', - 'foo'.force_encoding('BINARY') + 'foo'.dup.force_encoding('BINARY') ] end @@ -55,14 +55,14 @@ module ArraySpecs def self.array_with_usascii_and_7bit_binary_strings [ - 'bar'.force_encoding('US-ASCII'), - 'foo'.force_encoding('BINARY') + 'bar'.dup.force_encoding('US-ASCII'), + 'foo'.dup.force_encoding('BINARY') ] end def self.array_with_usascii_and_binary_strings [ - 'bar'.force_encoding('US-ASCII'), + 'bar'.dup.force_encoding('US-ASCII'), [255].pack('C').force_encoding('BINARY') ] end diff --git a/spec/ruby/core/array/flatten_spec.rb b/spec/ruby/core/array/flatten_spec.rb index 1770b5389a..272406b8f9 100644 --- a/spec/ruby/core/array/flatten_spec.rb +++ b/spec/ruby/core/array/flatten_spec.rb @@ -13,7 +13,7 @@ describe "Array#flatten" do it "returns dup when the level of recursion is 0" do a = [ 1, 2, [3, [4, 5] ] ] a.flatten(0).should == a - a.flatten(0).should_not equal(a) + a.flatten(0).should_not.equal?(a) end it "ignores negative levels" do @@ -30,7 +30,7 @@ describe "Array#flatten" do it "raises a TypeError when the passed Object can't be converted to an Integer" do obj = mock("Not converted") - -> { [ 1, 2, [3, [4, 5] ] ].flatten(obj) }.should raise_error(TypeError) + -> { [ 1, 2, [3, [4, 5] ] ].flatten(obj) }.should.raise(TypeError) end it "does not call flatten on elements" do @@ -46,13 +46,13 @@ describe "Array#flatten" do it "raises an ArgumentError on recursive arrays" do x = [] x << x - -> { x.flatten }.should raise_error(ArgumentError) + -> { x.flatten }.should.raise(ArgumentError) x = [] y = [] x << y y << x - -> { x.flatten }.should raise_error(ArgumentError) + -> { x.flatten }.should.raise(ArgumentError) end it "flattens any element which responds to #to_ary, using the return value of said method" do @@ -75,24 +75,12 @@ describe "Array#flatten" do [[obj]].flatten(1) end - ruby_version_is ''...'3.0' do - it "returns subclass instance for Array subclasses" do - ArraySpecs::MyArray[].flatten.should be_an_instance_of(ArraySpecs::MyArray) - ArraySpecs::MyArray[1, 2, 3].flatten.should be_an_instance_of(ArraySpecs::MyArray) - ArraySpecs::MyArray[1, [2], 3].flatten.should be_an_instance_of(ArraySpecs::MyArray) - ArraySpecs::MyArray[1, [2, 3], 4].flatten.should == ArraySpecs::MyArray[1, 2, 3, 4] - [ArraySpecs::MyArray[1, 2, 3]].flatten.should be_an_instance_of(Array) - end - end - - ruby_version_is '3.0' do - it "returns Array instance for Array subclasses" do - ArraySpecs::MyArray[].flatten.should be_an_instance_of(Array) - ArraySpecs::MyArray[1, 2, 3].flatten.should be_an_instance_of(Array) - ArraySpecs::MyArray[1, [2], 3].flatten.should be_an_instance_of(Array) - ArraySpecs::MyArray[1, [2, 3], 4].flatten.should == [1, 2, 3, 4] - [ArraySpecs::MyArray[1, 2, 3]].flatten.should be_an_instance_of(Array) - end + it "returns Array instance for Array subclasses" do + ArraySpecs::MyArray[].flatten.should.instance_of?(Array) + ArraySpecs::MyArray[1, 2, 3].flatten.should.instance_of?(Array) + ArraySpecs::MyArray[1, [2], 3].flatten.should.instance_of?(Array) + ArraySpecs::MyArray[1, [2, 3], 4].flatten.should == [1, 2, 3, 4] + [ArraySpecs::MyArray[1, 2, 3]].flatten.should.instance_of?(Array) end it "is not destructive" do @@ -118,7 +106,7 @@ describe "Array#flatten" do it "raises a TypeError if #to_ary does not return an Array" do @obj.should_receive(:to_ary).and_return(1) - -> { [@obj].flatten }.should raise_error(TypeError) + -> { [@obj].flatten }.should.raise(TypeError) end it "calls respond_to_missing?(:to_ary, true) to try coercing" do @@ -137,7 +125,7 @@ describe "Array#flatten" do it "calls #to_ary if not defined when #respond_to_missing? returns true" do def @obj.respond_to_missing?(name, priv) ScratchPad << name; true end - -> { [@obj].flatten }.should raise_error(NoMethodError) + -> { [@obj].flatten }.should.raise(NoMethodError) ScratchPad.recorded.should == [:to_ary] end @@ -180,7 +168,7 @@ describe "Array#flatten!" do it "returns self if made some modifications" do a = [[[1, [2, 3]],[2, 3, [4, [4, [5, 5]], [1, 2, 3]]], [4]], []] - a.flatten!.should equal(a) + a.flatten!.should.equal?(a) end it "returns nil if no modifications took place" do @@ -220,7 +208,7 @@ describe "Array#flatten!" do it "raises a TypeError when the passed Object can't be converted to an Integer" do obj = mock("Not converted") - -> { [ 1, 2, [3, [4, 5] ] ].flatten!(obj) }.should raise_error(TypeError) + -> { [ 1, 2, [3, [4, 5] ] ].flatten!(obj) }.should.raise(TypeError) end it "does not call flatten! on elements" do @@ -236,13 +224,13 @@ describe "Array#flatten!" do it "raises an ArgumentError on recursive arrays" do x = [] x << x - -> { x.flatten! }.should raise_error(ArgumentError) + -> { x.flatten! }.should.raise(ArgumentError) x = [] y = [] x << y y << x - -> { x.flatten! }.should raise_error(ArgumentError) + -> { x.flatten! }.should.raise(ArgumentError) end it "flattens any elements which responds to #to_ary, using the return value of said method" do @@ -260,19 +248,19 @@ describe "Array#flatten!" do ary = [ArraySpecs::MyArray[1, 2, 3]] ary.flatten! - ary.should be_an_instance_of(Array) + ary.should.instance_of?(Array) ary.should == [1, 2, 3] end it "raises a FrozenError on frozen arrays when the array is modified" do nested_ary = [1, 2, []] nested_ary.freeze - -> { nested_ary.flatten! }.should raise_error(FrozenError) + -> { nested_ary.flatten! }.should.raise(FrozenError) end # see [ruby-core:23663] it "raises a FrozenError on frozen arrays when the array would not be modified" do - -> { ArraySpecs.frozen_array.flatten! }.should raise_error(FrozenError) - -> { ArraySpecs.empty_frozen_array.flatten! }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.flatten! }.should.raise(FrozenError) + -> { ArraySpecs.empty_frozen_array.flatten! }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/array/hash_spec.rb b/spec/ruby/core/array/hash_spec.rb index f3bcc83fce..3b7b6d5bed 100644 --- a/spec/ruby/core/array/hash_spec.rb +++ b/spec/ruby/core/array/hash_spec.rb @@ -7,16 +7,16 @@ describe "Array#hash" do [[], [1, 2, 3]].each do |ary| ary.hash.should == ary.dup.hash - ary.hash.should be_an_instance_of(Integer) + ary.hash.should.instance_of?(Integer) end end it "properly handles recursive arrays" do empty = ArraySpecs.empty_recursive_array - -> { empty.hash }.should_not raise_error + -> { empty.hash }.should_not.raise array = ArraySpecs.recursive_array - -> { array.hash }.should_not raise_error + -> { array.hash }.should_not.raise end it "returns the same hash for equal recursive arrays" do @@ -74,7 +74,7 @@ describe "Array#hash" do a.fill 'a', 0..3 b = %w|a a a a| a.hash.should == b.hash - a.should eql(b) + a.should.eql?(b) end it "produces different hashes for nested arrays with different values and empty terminator" do diff --git a/spec/ruby/core/array/initialize_spec.rb b/spec/ruby/core/array/initialize_spec.rb index a8deed2b84..19ee37825e 100644 --- a/spec/ruby/core/array/initialize_spec.rb +++ b/spec/ruby/core/array/initialize_spec.rb @@ -7,7 +7,7 @@ describe "Array#initialize" do end it "is private" do - Array.should have_private_instance_method("initialize") + Array.private_instance_methods(false).should.include?(:initialize) end it "is called on subclasses" do @@ -19,26 +19,26 @@ describe "Array#initialize" do it "preserves the object's identity even when changing its value" do a = [1, 2, 3] - a.send(:initialize).should equal(a) + a.send(:initialize).should.equal?(a) a.should_not == [1, 2, 3] end it "raises an ArgumentError if passed 3 or more arguments" do -> do [1, 2].send :initialize, 1, 'x', true - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) -> do [1, 2].send(:initialize, 1, 'x', true) {} - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) end it "raises a FrozenError on frozen arrays" do -> do ArraySpecs.frozen_array.send :initialize - end.should raise_error(FrozenError) + end.should.raise(FrozenError) -> do ArraySpecs.frozen_array.send :initialize, ArraySpecs.frozen_array - end.should raise_error(FrozenError) + end.should.raise(FrozenError) end it "calls #to_ary to convert the value to an array, even if it's private" do @@ -49,11 +49,13 @@ end describe "Array#initialize with no arguments" do it "makes the array empty" do - [1, 2, 3].send(:initialize).should be_empty + [1, 2, 3].send(:initialize).should.empty? end it "does not use the given block" do - ->{ [1, 2, 3].send(:initialize) { raise } }.should_not raise_error + -> { + -> { [1, 2, 3].send(:initialize) { raise } }.should_not.raise + }.should complain(/#{__FILE__}:#{__LINE__-1}: warning: given block not used/, verbose: true) end end @@ -64,7 +66,7 @@ describe "Array#initialize with (array)" do end it "does not use the given block" do - ->{ [1, 2, 3].send(:initialize) { raise } }.should_not raise_error + ->{ [1, 2, 3].send(:initialize) { raise } }.should_not.raise end it "calls #to_ary to convert the value to an array" do @@ -81,7 +83,7 @@ describe "Array#initialize with (array)" do end it "raises a TypeError if an Array type argument and a default object" do - -> { [].send(:initialize, [1, 2], 1) }.should raise_error(TypeError) + -> { [].send(:initialize, [1, 2], 1) }.should.raise(TypeError) end end @@ -90,8 +92,8 @@ describe "Array#initialize with (size, object=nil)" do a = [] obj = [3] a.send(:initialize, 2, obj).should == [obj, obj] - a[0].should equal(obj) - a[1].should equal(obj) + a[0].should.equal?(obj) + a[1].should.equal?(obj) b = [] b.send(:initialize, 3, 14).should == [14, 14, 14] @@ -103,12 +105,12 @@ describe "Array#initialize with (size, object=nil)" do end it "raises an ArgumentError if size is negative" do - -> { [].send(:initialize, -1, :a) }.should raise_error(ArgumentError) - -> { [].send(:initialize, -1) }.should raise_error(ArgumentError) + -> { [].send(:initialize, -1, :a) }.should.raise(ArgumentError) + -> { [].send(:initialize, -1) }.should.raise(ArgumentError) end it "raises an ArgumentError if size is too large" do - -> { [].send(:initialize, fixnum_max+1) }.should raise_error(ArgumentError) + -> { [].send(:initialize, fixnum_max+1) }.should.raise(ArgumentError) end it "calls #to_int to convert the size argument to an Integer when object is given" do @@ -126,7 +128,7 @@ describe "Array#initialize with (size, object=nil)" do it "raises a TypeError if the size argument is not an Integer type" do obj = mock('nonnumeric') obj.stub!(:to_ary).and_return([1, 2]) - ->{ [].send(:initialize, obj, :a) }.should raise_error(TypeError) + ->{ [].send(:initialize, obj, :a) }.should.raise(TypeError) end it "yields the index of the element and sets the element to the value of the block" do diff --git a/spec/ruby/core/array/insert_spec.rb b/spec/ruby/core/array/insert_spec.rb index 9e1757f68b..38e132fd25 100644 --- a/spec/ruby/core/array/insert_spec.rb +++ b/spec/ruby/core/array/insert_spec.rb @@ -4,8 +4,8 @@ require_relative 'fixtures/classes' describe "Array#insert" do it "returns self" do ary = [] - ary.insert(0).should equal(ary) - ary.insert(0, :a).should equal(ary) + ary.insert(0).should.equal?(ary) + ary.insert(0, :a).should.equal?(ary) end it "inserts objects before the element at index for non-negative index" do @@ -46,8 +46,8 @@ describe "Array#insert" do end it "raises an IndexError if the negative index is out of bounds" do - -> { [].insert(-2, 1) }.should raise_error(IndexError) - -> { [1].insert(-3, 2) }.should raise_error(IndexError) + -> { [].insert(-2, 1) }.should.raise(IndexError) + -> { [1].insert(-3, 2) }.should.raise(IndexError) end it "does nothing of no object is passed" do @@ -64,15 +64,15 @@ describe "Array#insert" do end it "raises an ArgumentError if no argument passed" do - -> { [].insert() }.should raise_error(ArgumentError) + -> { [].insert() }.should.raise(ArgumentError) end it "raises a FrozenError on frozen arrays when the array is modified" do - -> { ArraySpecs.frozen_array.insert(0, 'x') }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.insert(0, 'x') }.should.raise(FrozenError) end # see [ruby-core:23666] it "raises a FrozenError on frozen arrays when the array would not be modified" do - -> { ArraySpecs.frozen_array.insert(0) }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.insert(0) }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/array/intersect_spec.rb b/spec/ruby/core/array/intersect_spec.rb index b8c5b1e69a..456aa26c6e 100644 --- a/spec/ruby/core/array/intersect_spec.rb +++ b/spec/ruby/core/array/intersect_spec.rb @@ -1,17 +1,64 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe 'Array#intersect?' do - ruby_version_is '3.1' do # https://bugs.ruby-lang.org/issues/15198 - describe 'when at least one element in two Arrays is the same' do - it 'returns true' do - [1, 2].intersect?([2, 3]).should == true - end + describe 'when at least one element in two Arrays is the same' do + it 'returns true' do + [1, 2].intersect?([2, 3, 4]).should == true + [2, 3, 4].intersect?([1, 2]).should == true end + end - describe 'when there are no elements in common between two Arrays' do - it 'returns false' do - [1, 2].intersect?([3, 4]).should == false - end + describe 'when there are no elements in common between two Arrays' do + it 'returns false' do + [0, 1, 2].intersect?([3, 4]).should == false + [3, 4].intersect?([0, 1, 2]).should == false + [3, 4].intersect?([]).should == false + [].intersect?([0, 1, 2]).should == false end end + + it "tries to convert the passed argument to an Array using #to_ary" do + obj = mock('[1,2,3]') + obj.should_receive(:to_ary).and_return([1, 2, 3]) + + [1, 2].intersect?(obj).should == true + end + + it "determines equivalence between elements in the sense of eql?" do + obj1 = mock('1') + obj2 = mock('2') + obj1.stub!(:hash).and_return(0) + obj2.stub!(:hash).and_return(0) + obj1.stub!(:eql?).and_return(true) + obj2.stub!(:eql?).and_return(true) + + [obj1].intersect?([obj2]).should == true + + obj1 = mock('3') + obj2 = mock('4') + obj1.stub!(:hash).and_return(0) + obj2.stub!(:hash).and_return(0) + obj1.stub!(:eql?).and_return(false) + obj2.stub!(:eql?).and_return(false) + + [obj1].intersect?([obj2]).should == false + end + + it "does not call to_ary on array subclasses" do + [5, 6].intersect?(ArraySpecs::ToAryArray[1, 2, 5, 6]).should == true + end + + it "properly handles an identical item even when its #eql? isn't reflexive" do + x = mock('x') + x.stub!(:hash).and_return(42) + x.stub!(:eql?).and_return(false) # Stubbed for clarity and latitude in implementation; not actually sent by MRI. + + [x].intersect?([x]).should == true + end + + it "has semantic of !(a & b).empty?" do + [].intersect?([]).should == false + [nil].intersect?([nil]).should == true + end end diff --git a/spec/ruby/core/array/join_spec.rb b/spec/ruby/core/array/join_spec.rb index e78ea6f9e1..811db036a8 100644 --- a/spec/ruby/core/array/join_spec.rb +++ b/spec/ruby/core/array/join_spec.rb @@ -24,11 +24,11 @@ describe "Array#join" do it "raises a TypeError if the separator cannot be coerced to a String by calling #to_str" do obj = mock("not a string") - -> { [1, 2].join(obj) }.should raise_error(TypeError) + -> { [1, 2].join(obj) }.should.raise(TypeError) end it "raises a TypeError if passed false as the separator" do - -> { [1, 2].join(false) }.should raise_error(TypeError) + -> { [1, 2].join(false) }.should.raise(TypeError) end end diff --git a/spec/ruby/core/array/keep_if_spec.rb b/spec/ruby/core/array/keep_if_spec.rb index bf2bdeaf91..62a65a04e8 100644 --- a/spec/ruby/core/array/keep_if_spec.rb +++ b/spec/ruby/core/array/keep_if_spec.rb @@ -1,9 +1,10 @@ +require_relative '../../spec_helper' require_relative 'shared/keep_if' describe "Array#keep_if" do it "returns the same array if no changes were made" do array = [1, 2, 3] - array.keep_if { true }.should equal(array) + array.keep_if { true }.should.equal?(array) end it_behaves_like :keep_if, :keep_if diff --git a/spec/ruby/core/array/last_spec.rb b/spec/ruby/core/array/last_spec.rb index d6fefada09..ed417bcd2a 100644 --- a/spec/ruby/core/array/last_spec.rb +++ b/spec/ruby/core/array/last_spec.rb @@ -28,7 +28,7 @@ describe "Array#last" do end it "raises an ArgumentError when count is negative" do - -> { [1, 2].last(-1) }.should raise_error(ArgumentError) + -> { [1, 2].last(-1) }.should.raise(ArgumentError) end it "returns the entire array when count > length" do @@ -47,10 +47,10 @@ describe "Array#last" do it "properly handles recursive arrays" do empty = ArraySpecs.empty_recursive_array - empty.last.should equal(empty) + empty.last.should.equal?(empty) array = ArraySpecs.recursive_array - array.last.should equal(array) + array.last.should.equal?(array) end it "tries to convert the passed argument to an Integer using #to_int" do @@ -60,19 +60,19 @@ describe "Array#last" do end it "raises a TypeError if the passed argument is not numeric" do - -> { [1,2].last(nil) }.should raise_error(TypeError) - -> { [1,2].last("a") }.should raise_error(TypeError) + -> { [1,2].last(nil) }.should.raise(TypeError) + -> { [1,2].last("a") }.should.raise(TypeError) obj = mock("nonnumeric") - -> { [1,2].last(obj) }.should raise_error(TypeError) + -> { [1,2].last(obj) }.should.raise(TypeError) end it "does not return subclass instance on Array subclasses" do - ArraySpecs::MyArray[].last(0).should be_an_instance_of(Array) - ArraySpecs::MyArray[].last(2).should be_an_instance_of(Array) - ArraySpecs::MyArray[1, 2, 3].last(0).should be_an_instance_of(Array) - ArraySpecs::MyArray[1, 2, 3].last(1).should be_an_instance_of(Array) - ArraySpecs::MyArray[1, 2, 3].last(2).should be_an_instance_of(Array) + ArraySpecs::MyArray[].last(0).should.instance_of?(Array) + ArraySpecs::MyArray[].last(2).should.instance_of?(Array) + ArraySpecs::MyArray[1, 2, 3].last(0).should.instance_of?(Array) + ArraySpecs::MyArray[1, 2, 3].last(1).should.instance_of?(Array) + ArraySpecs::MyArray[1, 2, 3].last(2).should.instance_of?(Array) end it "is not destructive" do diff --git a/spec/ruby/core/array/max_spec.rb b/spec/ruby/core/array/max_spec.rb index d1c64519d0..868275a748 100644 --- a/spec/ruby/core/array/max_spec.rb +++ b/spec/ruby/core/array/max_spec.rb @@ -2,7 +2,7 @@ require_relative '../../spec_helper' describe "Array#max" do it "is defined on Array" do - [1].method(:max).owner.should equal Array + [1].method(:max).owner.should.equal? Array end it "returns nil with no values" do @@ -70,16 +70,16 @@ describe "Array#max" do it "raises a NoMethodError for elements without #<=>" do -> do [BasicObject.new, BasicObject.new].max - end.should raise_error(NoMethodError) + end.should.raise(NoMethodError) end it "raises an ArgumentError for incomparable elements" do -> do [11,"22"].max - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) -> do [11,12,22,33].max{|a, b| nil} - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) end it "returns the maximum element (with block)" do diff --git a/spec/ruby/core/array/min_spec.rb b/spec/ruby/core/array/min_spec.rb index 3bdef0dd00..5913e08cf8 100644 --- a/spec/ruby/core/array/min_spec.rb +++ b/spec/ruby/core/array/min_spec.rb @@ -2,7 +2,7 @@ require_relative '../../spec_helper' describe "Array#min" do it "is defined on Array" do - [1].method(:max).owner.should equal Array + [1].method(:max).owner.should.equal? Array end it "returns nil with no values" do @@ -64,22 +64,22 @@ describe "Array#min" do end it "returns nil for an empty Enumerable" do - [].min.should be_nil + [].min.should == nil end it "raises a NoMethodError for elements without #<=>" do -> do [BasicObject.new, BasicObject.new].min - end.should raise_error(NoMethodError) + end.should.raise(NoMethodError) end it "raises an ArgumentError for incomparable elements" do -> do [11,"22"].min - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) -> do [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 diff --git a/spec/ruby/core/array/multiply_spec.rb b/spec/ruby/core/array/multiply_spec.rb index 23d5c99f3a..1ac14e1b09 100644 --- a/spec/ruby/core/array/multiply_spec.rb +++ b/spec/ruby/core/array/multiply_spec.rb @@ -17,7 +17,7 @@ describe "Array#*" do it "raises a TypeError if the argument can neither be converted to a string nor an integer" do obj = mock('not a string or integer') - ->{ [1,2] * obj }.should raise_error(TypeError) + ->{ [1,2] * obj }.should.raise(TypeError) end it "converts the passed argument to a String rather than an Integer" do @@ -28,15 +28,15 @@ describe "Array#*" do end it "raises a TypeError is the passed argument is nil" do - ->{ [1,2] * nil }.should raise_error(TypeError) + ->{ [1,2] * nil }.should.raise(TypeError) end it "raises an ArgumentError when passed 2 or more arguments" do - ->{ [1,2].send(:*, 1, 2) }.should raise_error(ArgumentError) + ->{ [1,2].send(:*, 1, 2) }.should.raise(ArgumentError) end it "raises an ArgumentError when passed no arguments" do - ->{ [1,2].send(:*) }.should raise_error(ArgumentError) + ->{ [1,2].send(:*) }.should.raise(ArgumentError) end end @@ -50,7 +50,7 @@ describe "Array#* with an integer" do it "does not return self even if the passed integer is 1" do ary = [1, 2, 3] - (ary * 1).should_not equal(ary) + (ary * 1).should_not.equal?(ary) end it "properly handles recursive arrays" do @@ -65,8 +65,8 @@ describe "Array#* with an integer" do end it "raises an ArgumentError when passed a negative integer" do - -> { [ 1, 2, 3 ] * -1 }.should raise_error(ArgumentError) - -> { [] * -1 }.should raise_error(ArgumentError) + -> { [ 1, 2, 3 ] * -1 }.should.raise(ArgumentError) + -> { [] * -1 }.should.raise(ArgumentError) end describe "with a subclass of Array" do @@ -76,25 +76,15 @@ describe "Array#* with an integer" do @array = ArraySpecs::MyArray[1, 2, 3, 4, 5] end - ruby_version_is ''...'3.0' do - it "returns a subclass instance" do - (@array * 0).should be_an_instance_of(ArraySpecs::MyArray) - (@array * 1).should be_an_instance_of(ArraySpecs::MyArray) - (@array * 2).should be_an_instance_of(ArraySpecs::MyArray) - end - end - - ruby_version_is '3.0' do - it "returns an Array instance" do - (@array * 0).should be_an_instance_of(Array) - (@array * 1).should be_an_instance_of(Array) - (@array * 2).should be_an_instance_of(Array) - end + it "returns an Array instance" do + (@array * 0).should.instance_of?(Array) + (@array * 1).should.instance_of?(Array) + (@array * 2).should.instance_of?(Array) end it "does not call #initialize on the subclass instance" do (@array * 2).should == [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] - ScratchPad.recorded.should be_nil + ScratchPad.recorded.should == nil end end end diff --git a/spec/ruby/core/array/new_spec.rb b/spec/ruby/core/array/new_spec.rb index 96ec6b8198..b2f23e2f6b 100644 --- a/spec/ruby/core/array/new_spec.rb +++ b/spec/ruby/core/array/new_spec.rb @@ -3,30 +3,32 @@ require_relative 'fixtures/classes' describe "Array.new" do it "returns an instance of Array" do - Array.new.should be_an_instance_of(Array) + Array.new.should.instance_of?(Array) end it "returns an instance of a subclass" do - ArraySpecs::MyArray.new(1, 2).should be_an_instance_of(ArraySpecs::MyArray) + ArraySpecs::MyArray.new(1, 2).should.instance_of?(ArraySpecs::MyArray) end it "raises an ArgumentError if passed 3 or more arguments" do -> do [1, 2].send :initialize, 1, 'x', true - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) -> do [1, 2].send(:initialize, 1, 'x', true) {} - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) end end describe "Array.new with no arguments" do it "returns an empty array" do - Array.new.should be_empty + Array.new.should.empty? end it "does not use the given block" do - ->{ Array.new { raise } }.should_not raise_error + -> { + -> { Array.new { raise } }.should_not.raise + }.should complain(/warning: given block not used/, verbose: true) end end @@ -37,7 +39,7 @@ describe "Array.new with (array)" do end it "does not use the given block" do - ->{ Array.new([1, 2]) { raise } }.should_not raise_error + ->{ Array.new([1, 2]) { raise } }.should_not.raise end it "calls #to_ary to convert the value to an array" do @@ -54,7 +56,7 @@ describe "Array.new with (array)" do end it "raises a TypeError if an Array type argument and a default object" do - -> { Array.new([1, 2], 1) }.should raise_error(TypeError) + -> { Array.new([1, 2], 1) }.should.raise(TypeError) end end @@ -63,8 +65,8 @@ describe "Array.new with (size, object=nil)" do obj = [3] a = Array.new(2, obj) a.should == [obj, obj] - a[0].should equal(obj) - a[1].should equal(obj) + a[0].should.equal?(obj) + a[1].should.equal?(obj) Array.new(3, 14).should == [14, 14, 14] end @@ -74,12 +76,12 @@ describe "Array.new with (size, object=nil)" do end it "raises an ArgumentError if size is negative" do - -> { Array.new(-1, :a) }.should raise_error(ArgumentError) - -> { Array.new(-1) }.should raise_error(ArgumentError) + -> { Array.new(-1, :a) }.should.raise(ArgumentError) + -> { Array.new(-1) }.should.raise(ArgumentError) end it "raises an ArgumentError if size is too large" do - -> { Array.new(fixnum_max+1) }.should raise_error(ArgumentError) + -> { Array.new(fixnum_max+1) }.should.raise(ArgumentError) end it "calls #to_int to convert the size argument to an Integer when object is given" do @@ -97,7 +99,7 @@ describe "Array.new with (size, object=nil)" do it "raises a TypeError if the size argument is not an Integer type" do obj = mock('nonnumeric') obj.stub!(:to_ary).and_return([1, 2]) - ->{ Array.new(obj, :a) }.should raise_error(TypeError) + ->{ Array.new(obj, :a) }.should.raise(TypeError) end it "yields the index of the element and sets the element to the value of the block" do diff --git a/spec/ruby/core/array/none_spec.rb b/spec/ruby/core/array/none_spec.rb new file mode 100644 index 0000000000..31cd8c46d6 --- /dev/null +++ b/spec/ruby/core/array/none_spec.rb @@ -0,0 +1,13 @@ +require_relative '../../spec_helper' +require_relative 'shared/iterable_and_tolerating_size_increasing' + +describe "Array#none?" do + @value_to_return = -> _ { false } + it_behaves_like :array_iterable_and_tolerating_size_increasing, :none? + + it "ignores the block if there is an argument" do + -> { + ['bar', 'foobar'].none?(/baz/) { true }.should == true + }.should complain(/given block not used/) + end +end diff --git a/spec/ruby/core/array/one_spec.rb b/spec/ruby/core/array/one_spec.rb new file mode 100644 index 0000000000..0c61907881 --- /dev/null +++ b/spec/ruby/core/array/one_spec.rb @@ -0,0 +1,13 @@ +require_relative '../../spec_helper' +require_relative 'shared/iterable_and_tolerating_size_increasing' + +describe "Array#one?" do + @value_to_return = -> _ { false } + it_behaves_like :array_iterable_and_tolerating_size_increasing, :one? + + it "ignores the block if there is an argument" do + -> { + ['bar', 'foobar'].one?(/foo/) { false }.should == true + }.should complain(/given block not used/) + end +end diff --git a/spec/ruby/core/array/pack/a_spec.rb b/spec/ruby/core/array/pack/a_spec.rb index f4a40502c2..03bfd8214c 100644 --- a/spec/ruby/core/array/pack/a_spec.rb +++ b/spec/ruby/core/array/pack/a_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -19,15 +19,15 @@ describe "Array#pack with format 'A'" do end it "will not implicitly convert a number to a string" do - -> { [0].pack('A') }.should raise_error(TypeError) - -> { [0].pack('a') }.should raise_error(TypeError) + -> { [0].pack('A') }.should.raise(TypeError) + -> { [0].pack('a') }.should.raise(TypeError) end it "adds all the bytes to the output when passed the '*' modifier" do ["abc"].pack("A*").should == "abc" end - it "padds the output with spaces when the count exceeds the size of the String" do + it "pads the output with spaces when the count exceeds the size of the String" do ["abc"].pack("A6").should == "abc " end @@ -55,7 +55,7 @@ describe "Array#pack with format 'a'" do ["abc"].pack("a*").should == "abc" end - it "padds the output with NULL bytes when the count exceeds the size of the String" do + it "pads the output with NULL bytes when the count exceeds the size of the String" do ["abc"].pack("a6").should == "abc\x00\x00\x00" end diff --git a/spec/ruby/core/array/pack/at_spec.rb b/spec/ruby/core/array/pack/at_spec.rb index 3942677913..bb9801440a 100644 --- a/spec/ruby/core/array/pack/at_spec.rb +++ b/spec/ruby/core/array/pack/at_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' diff --git a/spec/ruby/core/array/pack/b_spec.rb b/spec/ruby/core/array/pack/b_spec.rb index ec82b7d1ab..f7576846ef 100644 --- a/spec/ruby/core/array/pack/b_spec.rb +++ b/spec/ruby/core/array/pack/b_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -19,8 +19,8 @@ describe "Array#pack with format 'B'" do end it "will not implicitly convert a number to a string" do - -> { [0].pack('B') }.should raise_error(TypeError) - -> { [0].pack('b') }.should raise_error(TypeError) + -> { [0].pack('B') }.should.raise(TypeError) + -> { [0].pack('b') }.should.raise(TypeError) end it "encodes one bit for each character starting with the most significant bit" do diff --git a/spec/ruby/core/array/pack/buffer_spec.rb b/spec/ruby/core/array/pack/buffer_spec.rb index ecb40bfd06..d104c80186 100644 --- a/spec/ruby/core/array/pack/buffer_spec.rb +++ b/spec/ruby/core/array/pack/buffer_spec.rb @@ -7,43 +7,53 @@ describe "Array#pack with :buffer option" do n = [ 65, 66, 67 ] buffer = " "*3 result = n.pack("ccc", buffer: buffer) #=> "ABC" - result.should equal(buffer) + result.should.equal?(buffer) end it "adds result at the end of buffer content" do n = [ 65, 66, 67 ] # result without buffer is "ABC" - buffer = "" + buffer = +"" n.pack("ccc", buffer: buffer).should == "ABC" - buffer = "123" + buffer = +"123" n.pack("ccc", buffer: buffer).should == "123ABC" - buffer = "12345" + buffer = +"12345" n.pack("ccc", buffer: buffer).should == "12345ABC" end it "raises TypeError exception if buffer is not String" do - -> { [65].pack("ccc", buffer: []) }.should raise_error( + -> { [65].pack("ccc", buffer: []) }.should.raise( TypeError, "buffer must be String, not Array") end + it "raise FrozenError if buffer is frozen" do + -> { [65].pack("c", buffer: "frozen-string".freeze) }.should.raise(FrozenError) + end + + it "preserves the encoding of the given buffer" do + buffer = ''.encode(Encoding::ISO_8859_1) + [65, 66, 67].pack("ccc", buffer: buffer) + buffer.encoding.should == Encoding::ISO_8859_1 + end + context "offset (@) is specified" do it 'keeps buffer content if it is longer than offset' do n = [ 65, 66, 67 ] - buffer = "123456" + buffer = +"123456" n.pack("@3ccc", buffer: buffer).should == "123ABC" end it "fills the gap with \\0 if buffer content is shorter than offset" do n = [ 65, 66, 67 ] - buffer = "123" + buffer = +"123" n.pack("@6ccc", buffer: buffer).should == "123\0\0\0ABC" end it 'does not keep buffer content if it is longer than offset + result' do n = [ 65, 66, 67 ] - buffer = "1234567890" + buffer = +"1234567890" n.pack("@3ccc", buffer: buffer).should == "123ABC" end end diff --git a/spec/ruby/core/array/pack/c_spec.rb b/spec/ruby/core/array/pack/c_spec.rb index 7200830331..de06207a23 100644 --- a/spec/ruby/core/array/pack/c_spec.rb +++ b/spec/ruby/core/array/pack/c_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' @@ -45,8 +45,10 @@ describe :array_pack_8bit, shared: true do [1, 2, 3, 4, 5].pack(pack_format('*')).should == "\x01\x02\x03\x04\x05" end - it "ignores NULL bytes between directives" do - [1, 2, 3].pack(pack_format("\000", 2)).should == "\x01\x02" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [1, 2, 3].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do diff --git a/spec/ruby/core/array/pack/comment_spec.rb b/spec/ruby/core/array/pack/comment_spec.rb index 254c827ccc..daf1cff06a 100644 --- a/spec/ruby/core/array/pack/comment_spec.rb +++ b/spec/ruby/core/array/pack/comment_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' diff --git a/spec/ruby/core/array/pack/h_spec.rb b/spec/ruby/core/array/pack/h_spec.rb index 2c1dac8d4a..1492d02b1f 100644 --- a/spec/ruby/core/array/pack/h_spec.rb +++ b/spec/ruby/core/array/pack/h_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -19,8 +19,8 @@ describe "Array#pack with format 'H'" do end it "will not implicitly convert a number to a string" do - -> { [0].pack('H') }.should raise_error(TypeError) - -> { [0].pack('h') }.should raise_error(TypeError) + -> { [0].pack('H') }.should.raise(TypeError) + -> { [0].pack('h') }.should.raise(TypeError) end it "encodes the first character as the most significant nibble when passed no count modifier" do diff --git a/spec/ruby/core/array/pack/l_spec.rb b/spec/ruby/core/array/pack/l_spec.rb index b446a7a36a..f6dfb1da83 100644 --- a/spec/ruby/core/array/pack/l_spec.rb +++ b/spec/ruby/core/array/pack/l_spec.rb @@ -29,7 +29,7 @@ describe "Array#pack with format 'L'" do it_behaves_like :array_pack_32bit_be, 'L>' end - platform_is wordsize: 32 do + platform_is c_long_size: 32 do describe "with modifier '<' and '_'" do it_behaves_like :array_pack_32bit_le, 'L<_' it_behaves_like :array_pack_32bit_le, 'L_<' @@ -51,7 +51,7 @@ describe "Array#pack with format 'L'" do end end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do describe "with modifier '<' and '_'" do it_behaves_like :array_pack_64bit_le, 'L<_' it_behaves_like :array_pack_64bit_le, 'L_<' @@ -83,7 +83,7 @@ describe "Array#pack with format 'l'" do it_behaves_like :array_pack_32bit_be, 'l>' end - platform_is wordsize: 32 do + platform_is c_long_size: 32 do describe "with modifier '<' and '_'" do it_behaves_like :array_pack_32bit_le, 'l<_' it_behaves_like :array_pack_32bit_le, 'l_<' @@ -105,7 +105,7 @@ describe "Array#pack with format 'l'" do end end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do describe "with modifier '<' and '_'" do it_behaves_like :array_pack_64bit_le, 'l<_' it_behaves_like :array_pack_64bit_le, 'l_<' @@ -137,7 +137,7 @@ little_endian do it_behaves_like :array_pack_32bit_le, 'l' end - platform_is wordsize: 32 do + platform_is c_long_size: 32 do describe "Array#pack with format 'L' with modifier '_'" do it_behaves_like :array_pack_32bit_le, 'L_' end @@ -155,7 +155,7 @@ little_endian do end end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do describe "Array#pack with format 'L' with modifier '_'" do it_behaves_like :array_pack_64bit_le, 'L_' end @@ -183,7 +183,7 @@ big_endian do it_behaves_like :array_pack_32bit_be, 'l' end - platform_is wordsize: 32 do + platform_is c_long_size: 32 do describe "Array#pack with format 'L' with modifier '_'" do it_behaves_like :array_pack_32bit_be, 'L_' end @@ -201,7 +201,7 @@ big_endian do end end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do describe "Array#pack with format 'L' with modifier '_'" do it_behaves_like :array_pack_64bit_be, 'L_' end diff --git a/spec/ruby/core/array/pack/m_spec.rb b/spec/ruby/core/array/pack/m_spec.rb index 2b1a84abca..fb670d120e 100644 --- a/spec/ruby/core/array/pack/m_spec.rb +++ b/spec/ruby/core/array/pack/m_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -80,8 +80,16 @@ describe "Array#pack with format 'M'" do ].should be_computed_by(:pack, "M") end - it "encodes a tab followed by a newline with an encoded newline" do + it "encodes a tab at the end of a line with an encoded newline" do + ["\t"].pack("M").should == "\t=\n" ["\t\n"].pack("M").should == "\t=\n\n" + ["abc\t\nxyz"].pack("M").should == "abc\t=\n\nxyz=\n" + end + + it "encodes a space at the end of a line with an encoded newline" do + [" "].pack("M").should == " =\n" + [" \n"].pack("M").should == " =\n\n" + ["abc \nxyz"].pack("M").should == "abc =\n\nxyz=\n" end it "encodes 127..255 in hex format" do @@ -147,7 +155,7 @@ describe "Array#pack with format 'M'" do it "encodes a recursive array" do empty = ArraySpecs.empty_recursive_array - empty.pack('M').should be_an_instance_of(String) + empty.pack('M').should.instance_of?(String) array = ArraySpecs.recursive_array array.pack('M').should == "1=\n" @@ -164,7 +172,7 @@ describe "Array#pack with format 'M'" do obj = mock("pack M non-string") obj.should_receive(:to_s).and_return(2) - [obj].pack("M").should be_an_instance_of(String) + [obj].pack("M").should.instance_of?(String) end it "encodes a Symbol as a String" do @@ -285,16 +293,16 @@ describe "Array#pack with format 'm'" do it "raises a TypeError if #to_str does not return a String" do obj = mock("pack m non-string") - -> { [obj].pack("m") }.should raise_error(TypeError) + -> { [obj].pack("m") }.should.raise(TypeError) end it "raises a TypeError if passed nil" do - -> { [nil].pack("m") }.should raise_error(TypeError) + -> { [nil].pack("m") }.should.raise(TypeError) end it "raises a TypeError if passed an Integer" do - -> { [0].pack("m") }.should raise_error(TypeError) - -> { [bignum_value].pack("m") }.should raise_error(TypeError) + -> { [0].pack("m") }.should.raise(TypeError) + -> { [bignum_value].pack("m") }.should.raise(TypeError) end it "does not emit a newline if passed zero as the count modifier" do diff --git a/spec/ruby/core/array/pack/percent_spec.rb b/spec/ruby/core/array/pack/percent_spec.rb index 5d56dea5fe..29b119732a 100644 --- a/spec/ruby/core/array/pack/percent_spec.rb +++ b/spec/ruby/core/array/pack/percent_spec.rb @@ -2,6 +2,6 @@ require_relative '../../../spec_helper' describe "Array#pack with format '%'" do it "raises an Argument Error" do - -> { [1].pack("%") }.should raise_error(ArgumentError) + -> { [1].pack("%") }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/array/pack/r_spec.rb b/spec/ruby/core/array/pack/r_spec.rb new file mode 100644 index 0000000000..cefe1388d2 --- /dev/null +++ b/spec/ruby/core/array/pack/r_spec.rb @@ -0,0 +1,89 @@ +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/numeric_basic' +require_relative 'shared/integer' + +ruby_version_is "4.1" do + describe "Array#pack with format 'R'" do + it_behaves_like :array_pack_basic, 'R' + it_behaves_like :array_pack_basic_non_float, 'R' + it_behaves_like :array_pack_arguments, 'R' + it_behaves_like :array_pack_numeric_basic, 'R' + it_behaves_like :array_pack_integer, 'R' + + it "encodes a ULEB128 integer" do + [ [[0], "\x00"], + [[1], "\x01"], + [[127], "\x7f"], + [[128], "\x80\x01"], + [[0x3fff], "\xff\x7f"], + [[0x4000], "\x80\x80\x01"], + [[0xffffffff], "\xff\xff\xff\xff\x0f"], + [[0x100000000], "\x80\x80\x80\x80\x10"], + [[0xffff_ffff_ffff_ffff], "\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01"], + [[0xffff_ffff_ffff_ffff_ffff_ffff], "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x1f"], + ].should be_computed_by(:pack, "R") + end + + it "encodes multiple values with '*' modifier" do + [1, 2].pack("R*").should == "\x01\x02" + [127, 128].pack("R*").should == "\x7f\x80\x01" + end + + it "raises an ArgumentError when passed a negative value" do + -> { [-1].pack("R") }.should.raise(ArgumentError) + -> { [-100].pack("R") }.should.raise(ArgumentError) + end + + it "round-trips values through pack and unpack" do + values = [0, 1, 127, 128, 0x3fff, 0x4000, 0xffffffff, 0x100000000] + values.pack("R*").unpack("R*").should == values + end + end + + describe "Array#pack with format 'r'" do + it_behaves_like :array_pack_basic, 'r' + it_behaves_like :array_pack_basic_non_float, 'r' + it_behaves_like :array_pack_arguments, 'r' + it_behaves_like :array_pack_numeric_basic, 'r' + it_behaves_like :array_pack_integer, 'r' + + it "encodes a SLEB128 integer" do + [ [[0], "\x00"], + [[1], "\x01"], + [[-1], "\x7f"], + [[-2], "\x7e"], + [[127], "\xff\x00"], + [[128], "\x80\x01"], + [[-127], "\x81\x7f"], + [[-128], "\x80\x7f"], + ].should be_computed_by(:pack, "r") + end + + it "encodes larger positive numbers" do + [0x3fff].pack("r").should == "\xff\xff\x00" + [0x4000].pack("r").should == "\x80\x80\x01" + end + + it "encodes larger negative numbers" do + [-0x3fff].pack("r").should == "\x81\x80\x7f" + [-0x4000].pack("r").should == "\x80\x80\x7f" + end + + it "encodes very large numbers" do + [0xffff_ffff_ffff_ffff_ffff_ffff].pack("r").should == "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1F" + [-0xffff_ffff_ffff_ffff_ffff_ffff].pack("r").should == "\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x60" + end + + it "encodes multiple values with '*' modifier" do + [0, 1, -1].pack("r*").should == "\x00\x01\x7f" + end + + it "round-trips values through pack and unpack" do + values = [0, 1, -1, 127, -127, 128, -128, 0x3fff, -0x3fff, 0x4000, -0x4000] + values.pack("r*").unpack("r*").should == values + end + end +end diff --git a/spec/ruby/core/array/pack/shared/basic.rb b/spec/ruby/core/array/pack/shared/basic.rb index 23e239d3de..2894369c71 100644 --- a/spec/ruby/core/array/pack/shared/basic.rb +++ b/spec/ruby/core/array/pack/shared/basic.rb @@ -1,6 +1,6 @@ describe :array_pack_arguments, shared: true do it "raises an ArgumentError if there are fewer elements than the format requires" do - -> { [].pack(pack_format(1)) }.should raise_error(ArgumentError) + -> { [].pack(pack_format(1)) }.should.raise(ArgumentError) end end @@ -10,11 +10,11 @@ describe :array_pack_basic, shared: true do end it "raises a TypeError when passed nil" do - -> { [@obj].pack(nil) }.should raise_error(TypeError) + -> { [@obj].pack(nil) }.should.raise(TypeError) end it "raises a TypeError when passed an Integer" do - -> { [@obj].pack(1) }.should raise_error(TypeError) + -> { [@obj].pack(1) }.should.raise(TypeError) end end @@ -24,34 +24,50 @@ describe :array_pack_basic_non_float, shared: true do end it "ignores whitespace in the format string" do - [@obj, @obj].pack("a \t\n\v\f\r"+pack_format).should be_an_instance_of(String) + [@obj, @obj].pack("a \t\n\v\f\r"+pack_format).should.instance_of?(String) + end + + it "ignores comments in the format string" do + # 2 additional directives ('a') are required for the X directive + [@obj, @obj, @obj, @obj].pack("aa #{pack_format} # some comment \n#{pack_format}").should.instance_of?(String) + end + + it "raise ArgumentError when a directive is unknown" do + # additional directive ('a') is required for the X directive + -> { [@obj, @obj].pack("a K" + pack_format) }.should.raise(ArgumentError, /unknown pack directive 'K'/) + -> { [@obj, @obj].pack("a 0" + pack_format) }.should.raise(ArgumentError, /unknown pack directive '0'/) + -> { [@obj, @obj].pack("a :" + pack_format) }.should.raise(ArgumentError, /unknown pack directive ':'/) end it "calls #to_str to coerce the directives string" do d = mock("pack directive") d.should_receive(:to_str).and_return("x"+pack_format) - [@obj, @obj].pack(d).should be_an_instance_of(String) + [@obj, @obj].pack(d).should.instance_of?(String) end end describe :array_pack_basic_float, shared: true do it "ignores whitespace in the format string" do - [9.3, 4.7].pack(" \t\n\v\f\r"+pack_format).should be_an_instance_of(String) + [9.3, 4.7].pack(" \t\n\v\f\r"+pack_format).should.instance_of?(String) + end + + it "ignores comments in the format string" do + [9.3, 4.7].pack(pack_format + "# some comment \n" + pack_format).should.instance_of?(String) end it "calls #to_str to coerce the directives string" do d = mock("pack directive") d.should_receive(:to_str).and_return("x"+pack_format) - [1.2, 4.7].pack(d).should be_an_instance_of(String) + [1.2, 4.7].pack(d).should.instance_of?(String) end end describe :array_pack_no_platform, shared: true do it "raises ArgumentError when the format modifier is '_'" do - ->{ [1].pack(pack_format("_")) }.should raise_error(ArgumentError) + ->{ [1].pack(pack_format("_")) }.should.raise(ArgumentError) end it "raises ArgumentError when the format modifier is '!'" do - ->{ [1].pack(pack_format("!")) }.should raise_error(ArgumentError) + ->{ [1].pack(pack_format("!")) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/array/pack/shared/encodings.rb b/spec/ruby/core/array/pack/shared/encodings.rb index 6b7ffac764..0b5a5cc8a0 100644 --- a/spec/ruby/core/array/pack/shared/encodings.rb +++ b/spec/ruby/core/array/pack/shared/encodings.rb @@ -5,12 +5,12 @@ describe :array_pack_hex, shared: true do it "raises a TypeError if the object does not respond to #to_str" do obj = mock("pack hex non-string") - -> { [obj].pack(pack_format) }.should raise_error(TypeError) + -> { [obj].pack(pack_format) }.should.raise(TypeError) end it "raises a TypeError if #to_str does not return a String" do obj = mock("pack hex non-string") obj.should_receive(:to_str).and_return(1) - -> { [obj].pack(pack_format) }.should raise_error(TypeError) + -> { [obj].pack(pack_format) }.should.raise(TypeError) end end diff --git a/spec/ruby/core/array/pack/shared/float.rb b/spec/ruby/core/array/pack/shared/float.rb index ba174a071a..c1efcd7677 100644 --- a/spec/ruby/core/array/pack/shared/float.rb +++ b/spec/ruby/core/array/pack/shared/float.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary describe :array_pack_float_le, shared: true do it "encodes a positive Float" do @@ -14,7 +14,7 @@ describe :array_pack_float_le, shared: true do end it "raises a TypeError if passed a String representation of a floating point number" do - -> { ["13"].pack(pack_format) }.should raise_error(TypeError) + -> { ["13"].pack(pack_format) }.should.raise(TypeError) end it "encodes the number of array elements specified by the count modifier" do @@ -25,8 +25,10 @@ describe :array_pack_float_le, shared: true do [2.9, 1.4, 8.2].pack(pack_format("*")).should == "\x9a\x999@33\xb3?33\x03A" end - it "ignores NULL bytes between directives" do - [5.3, 9.2].pack(pack_format("\000", 2)).should == "\x9a\x99\xa9@33\x13A" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [5.3, 9.2].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -43,7 +45,7 @@ describe :array_pack_float_le, shared: true do it "encodes NaN" do nans = ["\x00\x00\xc0\xff", "\x00\x00\xc0\x7f", "\xFF\xFF\xFF\x7F"] - nans.should include([nan_value].pack(pack_format)) + nans.should.include?([nan_value].pack(pack_format)) end it "encodes a positive Float outside the range of a single precision float" do @@ -74,10 +76,15 @@ describe :array_pack_float_be, shared: true do it "converts an Integer to a Float" do [8].pack(pack_format).should == "A\x00\x00\x00" + [bignum_value].pack(pack_format).should == "_\x80\x00\x00" + end + + it "converts a Rational to a Float" do + [Rational(8)].pack(pack_format).should == "A\x00\x00\x00" end it "raises a TypeError if passed a String representation of a floating point number" do - -> { ["13"].pack(pack_format) }.should raise_error(TypeError) + -> { ["13"].pack(pack_format) }.should.raise(TypeError) end it "encodes the number of array elements specified by the count modifier" do @@ -88,8 +95,10 @@ describe :array_pack_float_be, shared: true do [2.9, 1.4, 8.2].pack(pack_format("*")).should == "@9\x99\x9a?\xb333A\x0333" end - it "ignores NULL bytes between directives" do - [5.3, 9.2].pack(pack_format("\000", 2)).should == "@\xa9\x99\x9aA\x1333" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [5.3, 9.2].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -106,7 +115,7 @@ describe :array_pack_float_be, shared: true do it "encodes NaN" do nans = ["\xff\xc0\x00\x00", "\x7f\xc0\x00\x00", "\x7F\xFF\xFF\xFF"] - nans.should include([nan_value].pack(pack_format)) + nans.should.include?([nan_value].pack(pack_format)) end it "encodes a positive Float outside the range of a single precision float" do @@ -129,10 +138,15 @@ describe :array_pack_double_le, shared: true do it "converts an Integer to a Float" do [8].pack(pack_format).should == "\x00\x00\x00\x00\x00\x00\x20@" + [bignum_value].pack(pack_format).should == "\x00\x00\x00\x00\x00\x00\xF0C" + end + + it "converts a Rational to a Float" do + [Rational(8)].pack(pack_format).should == "\x00\x00\x00\x00\x00\x00 @" end it "raises a TypeError if passed a String representation of a floating point number" do - -> { ["13"].pack(pack_format) }.should raise_error(TypeError) + -> { ["13"].pack(pack_format) }.should.raise(TypeError) end it "encodes the number of array elements specified by the count modifier" do @@ -143,8 +157,10 @@ describe :array_pack_double_le, shared: true do [2.9, 1.4, 8.2].pack(pack_format("*")).should == "333333\x07@ffffff\xf6?ffffff\x20@" end - it "ignores NULL bytes between directives" do - [5.3, 9.2].pack(pack_format("\000", 2)).should == "333333\x15@ffffff\x22@" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [5.3, 9.2].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -165,7 +181,7 @@ describe :array_pack_double_le, shared: true do "\x00\x00\x00\x00\x00\x00\xf8\x7f", "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F" ] - nans.should include([nan_value].pack(pack_format)) + nans.should.include?([nan_value].pack(pack_format)) end it "encodes a positive Float outside the range of a single precision float" do @@ -191,7 +207,7 @@ describe :array_pack_double_be, shared: true do end it "raises a TypeError if passed a String representation of a floating point number" do - -> { ["13"].pack(pack_format) }.should raise_error(TypeError) + -> { ["13"].pack(pack_format) }.should.raise(TypeError) end it "encodes the number of array elements specified by the count modifier" do @@ -202,8 +218,10 @@ describe :array_pack_double_be, shared: true do [2.9, 1.4, 8.2].pack(pack_format("*")).should == "@\x07333333?\xf6ffffff@\x20ffffff" end - it "ignores NULL bytes between directives" do - [5.3, 9.2].pack(pack_format("\000", 2)).should == "@\x15333333@\x22ffffff" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [5.3, 9.2].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -224,7 +242,7 @@ describe :array_pack_double_be, shared: true do "\x7f\xf8\x00\x00\x00\x00\x00\x00", "\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF" ] - nans.should include([nan_value].pack(pack_format)) + nans.should.include?([nan_value].pack(pack_format)) end it "encodes a positive Float outside the range of a single precision float" do diff --git a/spec/ruby/core/array/pack/shared/integer.rb b/spec/ruby/core/array/pack/shared/integer.rb index 6592f85022..1cdd386cc1 100644 --- a/spec/ruby/core/array/pack/shared/integer.rb +++ b/spec/ruby/core/array/pack/shared/integer.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary describe :array_pack_16bit_le, shared: true do it "encodes the least significant 16 bits of a positive number" do @@ -41,9 +41,10 @@ describe :array_pack_16bit_le, shared: true do str.should == "\x78\x65\xcd\xab\x21\x43" end - it "ignores NULL bytes between directives" do - str = [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) - str.should == "\x78\x65\xcd\xab" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -93,9 +94,10 @@ describe :array_pack_16bit_be, shared: true do str.should == "\x65\x78\xab\xcd\x43\x21" end - it "ignores NULL bytes between directives" do - str = [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) - str.should == "\x65\x78\xab\xcd" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -145,9 +147,10 @@ describe :array_pack_32bit_le, shared: true do str.should == "\x78\x65\x43\x12\xcd\xab\xf0\xde\x21\x43\x65\x78" end - it "ignores NULL bytes between directives" do - str = [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) - str.should == "\x78\x65\x43\x12\xcd\xab\xf0\xde" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -197,9 +200,10 @@ describe :array_pack_32bit_be, shared: true do str.should == "\x12\x43\x65\x78\xde\xf0\xab\xcd\x78\x65\x43\x21" end - it "ignores NULL bytes between directives" do - str = [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) - str.should == "\x12\x43\x65\x78\xde\xf0\xab\xcd" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [0x1243_6578, 0xdef0_abcd].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -225,7 +229,7 @@ describe :array_pack_32bit_le_platform, shared: true do str.should == "\x78\x65\x43\x12\xcd\xab\xf0\xde\x21\x43\x65\x78" end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do it "encodes the least significant 32 bits of a number that is greater than 32 bits" do [ [[0xff_7865_4321], "\x21\x43\x65\x78"], [[-0xff_7865_4321], "\xdf\xbc\x9a\x87"] @@ -251,7 +255,7 @@ describe :array_pack_32bit_be_platform, shared: true do str.should == "\x12\x43\x65\x78\xde\xf0\xab\xcd\x78\x65\x43\x21" end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do it "encodes the least significant 32 bits of a number that is greater than 32 bits" do [ [[0xff_7865_4321], "\x78\x65\x43\x21"], [[-0xff_7865_4321], "\x87\x9a\xbc\xdf"] @@ -309,9 +313,10 @@ describe :array_pack_64bit_le, shared: true do str.should == "\x56\x78\x12\x34\xcd\xab\xf0\xde\xf0\xde\xba\xdc\x21\x43\x65\x78" end - it "ignores NULL bytes between directives" do - str = [0xdef0_abcd_3412_7856, 0x7865_4321_dcba_def0].pack(pack_format("\000", 2)) - str.should == "\x56\x78\x12\x34\xcd\xab\xf0\xde\xf0\xde\xba\xdc\x21\x43\x65\x78" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [0xdef0_abcd_3412_7856, 0x7865_4321_dcba_def0].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -369,9 +374,10 @@ describe :array_pack_64bit_be, shared: true do str.should == "\xde\xf0\xab\xcd\x34\x12\x78\x56\x78\x65\x43\x21\xdc\xba\xde\xf0" end - it "ignores NULL bytes between directives" do - str = [0xdef0_abcd_3412_7856, 0x7865_4321_dcba_def0].pack(pack_format("\000", 2)) - str.should == "\xde\xf0\xab\xcd\x34\x12\x78\x56\x78\x65\x43\x21\xdc\xba\xde\xf0" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [0xdef0_abcd_3412_7856, 0x7865_4321_dcba_def0].pack(pack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do diff --git a/spec/ruby/core/array/pack/shared/numeric_basic.rb b/spec/ruby/core/array/pack/shared/numeric_basic.rb index 7c36ba4a32..6594914933 100644 --- a/spec/ruby/core/array/pack/shared/numeric_basic.rb +++ b/spec/ruby/core/array/pack/shared/numeric_basic.rb @@ -4,15 +4,15 @@ describe :array_pack_numeric_basic, shared: true do end it "raises a TypeError when passed nil" do - -> { [nil].pack(pack_format) }.should raise_error(TypeError) + -> { [nil].pack(pack_format) }.should.raise(TypeError) end it "raises a TypeError when passed true" do - -> { [true].pack(pack_format) }.should raise_error(TypeError) + -> { [true].pack(pack_format) }.should.raise(TypeError) end it "raises a TypeError when passed false" do - -> { [false].pack(pack_format) }.should raise_error(TypeError) + -> { [false].pack(pack_format) }.should.raise(TypeError) end it "returns a binary string" do @@ -24,21 +24,27 @@ end describe :array_pack_integer, shared: true do it "raises a TypeError when the object does not respond to #to_int" do obj = mock('not an integer') - -> { [obj].pack(pack_format) }.should raise_error(TypeError) + -> { [obj].pack(pack_format) }.should.raise(TypeError) end it "raises a TypeError when passed a String" do - -> { ["5"].pack(pack_format) }.should raise_error(TypeError) + -> { ["5"].pack(pack_format) }.should.raise(TypeError) end end describe :array_pack_float, shared: true do it "raises a TypeError if a String does not represent a floating point number" do - -> { ["a"].pack(pack_format) }.should raise_error(TypeError) + -> { ["a"].pack(pack_format) }.should.raise(TypeError) end - it "raises a TypeError when the object does not respond to #to_f" do - obj = mock('not an float') - -> { [obj].pack(pack_format) }.should raise_error(TypeError) + it "raises a TypeError when the object is not Numeric" do + obj = Object.new + -> { [obj].pack(pack_format) }.should.raise(TypeError, /can't convert Object into Float/) + end + + it "raises a TypeError when the Numeric object does not respond to #to_f" do + klass = Class.new(Numeric) + obj = klass.new + -> { [obj].pack(pack_format) }.should.raise(TypeError) end end diff --git a/spec/ruby/core/array/pack/shared/string.rb b/spec/ruby/core/array/pack/shared/string.rb index 8c82e8c617..b02257059f 100644 --- a/spec/ruby/core/array/pack/shared/string.rb +++ b/spec/ruby/core/array/pack/shared/string.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary describe :array_pack_string, shared: true do it "adds count bytes of a String to the output" do ["abc"].pack(pack_format(2)).should == "ab" @@ -17,11 +17,11 @@ describe :array_pack_string, shared: true do end it "raises an ArgumentError when the Array is empty" do - -> { [].pack(pack_format) }.should raise_error(ArgumentError) + -> { [].pack(pack_format) }.should.raise(ArgumentError) end it "raises an ArgumentError when the Array has too few elements" do - -> { ["a"].pack(pack_format(nil, 2)) }.should raise_error(ArgumentError) + -> { ["a"].pack(pack_format(nil, 2)) }.should.raise(ArgumentError) end it "calls #to_str to convert the element to a String" do @@ -33,14 +33,14 @@ describe :array_pack_string, shared: true do it "raises a TypeError when the object does not respond to #to_str" do obj = mock("not a string") - -> { [obj].pack(pack_format) }.should raise_error(TypeError) + -> { [obj].pack(pack_format) }.should.raise(TypeError) end it "returns a string in encoding of common to the concatenated results" do f = pack_format("*") [ [["\u{3042 3044 3046 3048}", 0x2000B].pack(f+"U"), Encoding::BINARY], [["abcde\xd1", "\xFF\xFe\x81\x82"].pack(f+"u"), Encoding::BINARY], - [["a".force_encoding("ascii"), "\xFF\xFe\x81\x82"].pack(f+"u"), Encoding::BINARY], + [["a".dup.force_encoding("ascii"), "\xFF\xFe\x81\x82"].pack(f+"u"), Encoding::BINARY], # under discussion [ruby-dev:37294] [["\u{3042 3044 3046 3048}", 1].pack(f+"N"), Encoding::BINARY] ].should be_computed_by(:encoding) diff --git a/spec/ruby/core/array/pack/shared/unicode.rb b/spec/ruby/core/array/pack/shared/unicode.rb index dd0f8b38aa..58ba8a8b23 100644 --- a/spec/ruby/core/array/pack/shared/unicode.rb +++ b/spec/ruby/core/array/pack/shared/unicode.rb @@ -26,7 +26,7 @@ describe :array_pack_unicode, shared: true do it "constructs strings with valid encodings" do str = [0x85].pack("U*") str.should == "\xc2\x85" - str.valid_encoding?.should be_true + str.valid_encoding?.should == true end it "encodes values larger than UTF-8 max codepoints" do @@ -64,11 +64,13 @@ describe :array_pack_unicode, shared: true do it "raises a TypeError if #to_int does not return an Integer" do obj = mock('to_int') obj.should_receive(:to_int).and_return("5") - -> { [obj].pack("U") }.should raise_error(TypeError) + -> { [obj].pack("U") }.should.raise(TypeError) end - it "ignores NULL bytes between directives" do - [1, 2, 3].pack("U\x00U").should == "\x01\x02" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [1, 2, 3].pack("U\x00U") + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -76,11 +78,11 @@ describe :array_pack_unicode, shared: true do end it "raises a RangeError if passed a negative number" do - -> { [-1].pack("U") }.should raise_error(RangeError) + -> { [-1].pack("U") }.should.raise(RangeError) end it "raises a RangeError if passed a number larger than an unsigned 32-bit integer" do - -> { [2**32].pack("U") }.should raise_error(RangeError) + -> { [2**32].pack("U") }.should.raise(RangeError) end it "sets the output string to UTF-8 encoding" do diff --git a/spec/ruby/core/array/pack/u_spec.rb b/spec/ruby/core/array/pack/u_spec.rb index b20093a647..c6a0d77eb2 100644 --- a/spec/ruby/core/array/pack/u_spec.rb +++ b/spec/ruby/core/array/pack/u_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -25,7 +25,7 @@ describe "Array#pack with format 'u'" do end it "will not implicitly convert a number to a string" do - -> { [0].pack('u') }.should raise_error(TypeError) + -> { [0].pack('u') }.should.raise(TypeError) end it "encodes an empty string as an empty string" do @@ -122,16 +122,16 @@ describe "Array#pack with format 'u'" do it "raises a TypeError if #to_str does not return a String" do obj = mock("pack m non-string") - -> { [obj].pack("u") }.should raise_error(TypeError) + -> { [obj].pack("u") }.should.raise(TypeError) end it "raises a TypeError if passed nil" do - -> { [nil].pack("u") }.should raise_error(TypeError) + -> { [nil].pack("u") }.should.raise(TypeError) end it "raises a TypeError if passed an Integer" do - -> { [0].pack("u") }.should raise_error(TypeError) - -> { [bignum_value].pack("u") }.should raise_error(TypeError) + -> { [0].pack("u") }.should.raise(TypeError) + -> { [bignum_value].pack("u") }.should.raise(TypeError) end it "sets the output string to US-ASCII encoding" do diff --git a/spec/ruby/core/array/pack/w_spec.rb b/spec/ruby/core/array/pack/w_spec.rb index 439fa02198..263e2a2288 100644 --- a/spec/ruby/core/array/pack/w_spec.rb +++ b/spec/ruby/core/array/pack/w_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -24,8 +24,10 @@ describe "Array#pack with format 'w'" do [obj].pack("w").should == "\x05" end - it "ignores NULL bytes between directives" do - [1, 2, 3].pack("w\x00w").should == "\x01\x02" + it "raise ArgumentError for NULL bytes between directives" do + -> { + [1, 2, 3].pack("w\x00w") + }.should.raise(ArgumentError, /unknown pack directive/) end it "ignores spaces between directives" do @@ -33,7 +35,7 @@ describe "Array#pack with format 'w'" do end it "raises an ArgumentError when passed a negative value" do - -> { [-1].pack("w") }.should raise_error(ArgumentError) + -> { [-1].pack("w") }.should.raise(ArgumentError) end it "returns a binary string" do diff --git a/spec/ruby/core/array/pack/x_spec.rb b/spec/ruby/core/array/pack/x_spec.rb index 86c3ad1aa4..7ff587a01e 100644 --- a/spec/ruby/core/array/pack/x_spec.rb +++ b/spec/ruby/core/array/pack/x_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -56,10 +56,10 @@ describe "Array#pack with format 'X'" do end it "raises an ArgumentError if the output string is empty" do - -> { [1, 2, 3].pack("XC") }.should raise_error(ArgumentError) + -> { [1, 2, 3].pack("XC") }.should.raise(ArgumentError) end it "raises an ArgumentError if the count modifier is greater than the bytes in the string" do - -> { [1, 2, 3].pack("C2X3") }.should raise_error(ArgumentError) + -> { [1, 2, 3].pack("C2X3") }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/array/pack/z_spec.rb b/spec/ruby/core/array/pack/z_spec.rb index 5ad3afd69e..5cd084c825 100644 --- a/spec/ruby/core/array/pack/z_spec.rb +++ b/spec/ruby/core/array/pack/z_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -19,14 +19,14 @@ describe "Array#pack with format 'Z'" do end it "will not implicitly convert a number to a string" do - -> { [0].pack('Z') }.should raise_error(TypeError) + -> { [0].pack('Z') }.should.raise(TypeError) end it "adds all the bytes and appends a NULL byte when passed the '*' modifier" do ["abc"].pack("Z*").should == "abc\x00" end - it "padds the output with NULL bytes when the count exceeds the size of the String" do + it "pads the output with NULL bytes when the count exceeds the size of the String" do ["abc"].pack("Z6").should == "abc\x00\x00\x00" end diff --git a/spec/ruby/core/array/partition_spec.rb b/spec/ruby/core/array/partition_spec.rb index be36fffcab..bd3f3a6b6f 100644 --- a/spec/ruby/core/array/partition_spec.rb +++ b/spec/ruby/core/array/partition_spec.rb @@ -36,8 +36,8 @@ describe "Array#partition" do it "does not return subclass instances on Array subclasses" do result = ArraySpecs::MyArray[1, 2, 3].partition { |x| x % 2 == 0 } - result.should be_an_instance_of(Array) - result[0].should be_an_instance_of(Array) - result[1].should be_an_instance_of(Array) + result.should.instance_of?(Array) + result[0].should.instance_of?(Array) + result[1].should.instance_of?(Array) end end diff --git a/spec/ruby/core/array/permutation_spec.rb b/spec/ruby/core/array/permutation_spec.rb index f15bd76639..b5df84b52b 100644 --- a/spec/ruby/core/array/permutation_spec.rb +++ b/spec/ruby/core/array/permutation_spec.rb @@ -11,7 +11,7 @@ describe "Array#permutation" do it "returns an Enumerator of all permutations when called without a block or arguments" do enum = @numbers.permutation - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.to_a.sort.should == [ [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] ].sort @@ -19,13 +19,13 @@ describe "Array#permutation" do it "returns an Enumerator of permutations of given length when called with an argument but no block" do enum = @numbers.permutation(1) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.to_a.sort.should == [[1],[2],[3]] end it "yields all permutations to the block then returns self when called with block but no arguments" do array = @numbers.permutation {|n| @yielded << n} - array.should be_an_instance_of(Array) + array.should.instance_of?(Array) array.sort.should == @numbers.sort @yielded.sort.should == [ [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] @@ -34,7 +34,7 @@ describe "Array#permutation" do it "yields all permutations of given length to the block then returns self when called with block and argument" do array = @numbers.permutation(2) {|n| @yielded << n} - array.should be_an_instance_of(Array) + array.should.instance_of?(Array) array.sort.should == @numbers.sort @yielded.sort.should == [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]].sort end @@ -78,7 +78,7 @@ describe "Array#permutation" do [3, 1], [3, 2], [3, [4, 5]], [[4, 5], 1], [[4, 5], 2], [[4, 5], 3] ] - expected.each {|e| got.include?(e).should be_true} + expected.each {|e| got.include?(e).should == true} got.size.should == expected.size end diff --git a/spec/ruby/core/array/plus_spec.rb b/spec/ruby/core/array/plus_spec.rb index 3962b05c39..7ead927fc0 100644 --- a/spec/ruby/core/array/plus_spec.rb +++ b/spec/ruby/core/array/plus_spec.rb @@ -14,10 +14,23 @@ describe "Array#+" do (ary + ary).should == [1, 2, 3, 1, 2, 3] end - it "tries to convert the passed argument to an Array using #to_ary" do - obj = mock('["x", "y"]') - obj.should_receive(:to_ary).and_return(["x", "y"]) - ([1, 2, 3] + obj).should == [1, 2, 3, "x", "y"] + describe "converts the passed argument to an Array using #to_ary" do + it "successfully concatenates the resulting array from the #to_ary call" do + obj = mock('["x", "y"]') + obj.should_receive(:to_ary).and_return(["x", "y"]) + ([1, 2, 3] + obj).should == [1, 2, 3, "x", "y"] + end + + it "raises a TypeError if the given argument can't be converted to an array" do + -> { [1, 2, 3] + nil }.should.raise(TypeError) + -> { [1, 2, 3] + "abc" }.should.raise(TypeError) + end + + it "raises a NoMethodError if the given argument raises a NoMethodError during type coercion to an Array" do + obj = mock("hello") + obj.should_receive(:to_ary).and_raise(NoMethodError) + -> { [1, 2, 3] + obj }.should.raise(NoMethodError) + end end it "properly handles recursive arrays" do @@ -32,9 +45,9 @@ describe "Array#+" do end it "does return subclass instances with Array subclasses" do - (ArraySpecs::MyArray[1, 2, 3] + []).should be_an_instance_of(Array) - (ArraySpecs::MyArray[1, 2, 3] + ArraySpecs::MyArray[]).should be_an_instance_of(Array) - ([1, 2, 3] + ArraySpecs::MyArray[]).should be_an_instance_of(Array) + (ArraySpecs::MyArray[1, 2, 3] + []).should.instance_of?(Array) + (ArraySpecs::MyArray[1, 2, 3] + ArraySpecs::MyArray[]).should.instance_of?(Array) + ([1, 2, 3] + ArraySpecs::MyArray[]).should.instance_of?(Array) end it "does not call to_ary on array subclasses" do diff --git a/spec/ruby/core/array/pop_spec.rb b/spec/ruby/core/array/pop_spec.rb index 2a19408660..069083331c 100644 --- a/spec/ruby/core/array/pop_spec.rb +++ b/spec/ruby/core/array/pop_spec.rb @@ -31,11 +31,11 @@ describe "Array#pop" do end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array.pop }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.pop }.should.raise(FrozenError) end it "raises a FrozenError on an empty frozen array" do - -> { ArraySpecs.empty_frozen_array.pop }.should raise_error(FrozenError) + -> { ArraySpecs.empty_frozen_array.pop }.should.raise(FrozenError) end describe "passed a number n as an argument" do @@ -71,7 +71,7 @@ describe "Array#pop" do popped2.should == [] a.should == [] - popped1.should_not equal(popped2) + popped1.should_not.equal?(popped2) end it "returns whole elements if n exceeds size of the array" do @@ -82,14 +82,14 @@ describe "Array#pop" do it "does not return self even when it returns whole elements" do a = [1, 2, 3, 4, 5] - a.pop(5).should_not equal(a) + a.pop(5).should_not.equal?(a) a = [1, 2, 3, 4, 5] - a.pop(6).should_not equal(a) + a.pop(6).should_not.equal?(a) end it "raises an ArgumentError if n is negative" do - ->{ [1, 2, 3].pop(-1) }.should raise_error(ArgumentError) + ->{ [1, 2, 3].pop(-1) }.should.raise(ArgumentError) end it "tries to convert n to an Integer using #to_int" do @@ -104,21 +104,21 @@ describe "Array#pop" do end it "raises a TypeError when the passed n cannot be coerced to Integer" do - ->{ [1, 2].pop("cat") }.should raise_error(TypeError) - ->{ [1, 2].pop(nil) }.should raise_error(TypeError) + ->{ [1, 2].pop("cat") }.should.raise(TypeError) + ->{ [1, 2].pop(nil) }.should.raise(TypeError) end it "raises an ArgumentError if more arguments are passed" do - ->{ [1, 2].pop(1, 2) }.should raise_error(ArgumentError) + ->{ [1, 2].pop(1, 2) }.should.raise(ArgumentError) end it "does not return subclass instances with Array subclass" do - ArraySpecs::MyArray[1, 2, 3].pop(2).should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].pop(2).should.instance_of?(Array) end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array.pop(2) }.should raise_error(FrozenError) - -> { ArraySpecs.frozen_array.pop(0) }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.pop(2) }.should.raise(FrozenError) + -> { ArraySpecs.frozen_array.pop(0) }.should.raise(FrozenError) end end end diff --git a/spec/ruby/core/array/product_spec.rb b/spec/ruby/core/array/product_spec.rb index 07d2880a96..837f0eaf34 100644 --- a/spec/ruby/core/array/product_spec.rb +++ b/spec/ruby/core/array/product_spec.rb @@ -3,12 +3,17 @@ require_relative 'fixtures/classes' describe "Array#product" do it "returns converted arguments using :to_ary" do - ->{ [1].product(2..3) }.should raise_error(TypeError) + ->{ [1].product(2..3) }.should.raise(TypeError) ar = ArraySpecs::ArrayConvertible.new(2,3) [1].product(ar).should == [[1,2],[1,3]] ar.called.should == :to_ary end + it "returns converted arguments using :method_missing" do + ar = ArraySpecs::ArrayMethodMissing.new(2,3) + [1].product(ar).should == [[1,2],[1,3]] + end + it "returns the expected result" do [1,2].product([3,4,5],[6,8]).should == [[1, 3, 6], [1, 3, 8], [1, 4, 6], [1, 4, 8], [1, 5, 6], [1, 5, 8], [2, 3, 6], [2, 3, 8], [2, 4, 6], [2, 4, 8], [2, 5, 6], [2, 5, 8]] @@ -26,7 +31,7 @@ describe "Array#product" do a = (0..100).to_a -> do a.product(a, a, a, a, a, a, a, a, a, a) - end.should raise_error(RangeError) + end.should.raise(RangeError) end describe "when given a block" do @@ -38,7 +43,7 @@ describe "Array#product" do acc = [] [1,2].product([3,4,5],[],[6,8]){|array| acc << array} - acc.should be_empty + acc.should.empty? end it "returns self" do @@ -51,18 +56,18 @@ describe "Array#product" do a = (0..100).to_a -> do a.product(a, a, a, a, a, a, a, a, a, a) - end.should raise_error(RangeError) + end.should.raise(RangeError) end end describe "when given an empty block" do it "returns self" do arr = [1,2] - arr.product([3,4,5],[6,8]){}.should equal(arr) + arr.product([3,4,5],[6,8]){}.should.equal?(arr) arr = [] - arr.product([3,4,5],[6,8]){}.should equal(arr) + arr.product([3,4,5],[6,8]){}.should.equal?(arr) arr = [1,2] - arr.product([]){}.should equal(arr) + arr.product([]){}.should.equal?(arr) end end end diff --git a/spec/ruby/core/array/rassoc_spec.rb b/spec/ruby/core/array/rassoc_spec.rb index 62fbd40611..95e4ed1892 100644 --- a/spec/ruby/core/array/rassoc_spec.rb +++ b/spec/ruby/core/array/rassoc_spec.rb @@ -12,11 +12,11 @@ describe "Array#rassoc" do it "properly handles recursive arrays" do empty = ArraySpecs.empty_recursive_array - empty.rassoc([]).should be_nil + empty.rassoc([]).should == nil [[empty, empty]].rassoc(empty).should == [empty, empty] array = ArraySpecs.recursive_array - array.rassoc(array).should be_nil + array.rassoc(array).should == nil [[empty, array]].rassoc(array).should == [empty, array] end @@ -35,4 +35,16 @@ describe "Array#rassoc" do [[1, :foobar, o], [2, o, 1], [3, mock('foo')]].rassoc(key).should == [2, o, 1] end + + it "calls to_ary on non-array elements" do + s1 = [1, 2] + s2 = ArraySpecs::ArrayConvertible.new(2, 3) + a = [s1, s2] + + s1.should_not_receive(:to_ary) + a.rassoc(2).should.equal?(s1) + + a.rassoc(3).should == [2, 3] + s2.called.should.equal?(:to_ary) + end end diff --git a/spec/ruby/core/array/reject_spec.rb b/spec/ruby/core/array/reject_spec.rb index fcf43fabde..8d237b3a75 100644 --- a/spec/ruby/core/array/reject_spec.rb +++ b/spec/ruby/core/array/reject_spec.rb @@ -2,6 +2,7 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' require_relative 'shared/enumeratorize' require_relative 'shared/delete_if' +require_relative 'shared/iterable_and_tolerating_size_increasing' require_relative '../enumerable/shared/enumeratorized' describe "Array#reject" do @@ -9,9 +10,9 @@ describe "Array#reject" do ary = [1, 2, 3, 4, 5] ary.reject { true }.should == [] ary.reject { false }.should == ary - ary.reject { false }.should_not equal ary + ary.reject { false }.should_not.equal? ary ary.reject { nil }.should == ary - ary.reject { nil }.should_not equal ary + ary.reject { nil }.should_not.equal? ary ary.reject { 5 }.should == [] ary.reject { |i| i < 3 }.should == [3, 4, 5] ary.reject { |i| i % 2 == 0 }.should == [1, 3, 5] @@ -34,7 +35,7 @@ describe "Array#reject" do end it "does not return subclass instance on Array subclasses" do - ArraySpecs::MyArray[1, 2, 3].reject { |x| x % 2 == 0 }.should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].reject { |x| x % 2 == 0 }.should.instance_of?(Array) end it "does not retain instance variables" do @@ -47,10 +48,14 @@ describe "Array#reject" do it_behaves_like :enumeratorized_with_origin_size, :reject, [1,2,3] end +describe "Array#reject" do + it_behaves_like :array_iterable_and_tolerating_size_increasing, :reject +end + describe "Array#reject!" do it "removes elements for which block is true" do a = [3, 4, 5, 6, 7, 8, 9, 10, 11] - a.reject! { |i| i % 2 == 0 }.should equal(a) + a.reject! { |i| i % 2 == 0 }.should.equal?(a) a.should == [3, 5, 7, 9, 11] a.reject! { |i| i > 8 } a.should == [3, 5, 7] @@ -100,15 +105,20 @@ describe "Array#reject!" do end it "returns an Enumerator if no block given, and the array is frozen" do - ArraySpecs.frozen_array.reject!.should be_an_instance_of(Enumerator) + ArraySpecs.frozen_array.reject!.should.instance_of?(Enumerator) end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array.reject! {} }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.reject! {} }.should.raise(FrozenError) end it "raises a FrozenError on an empty frozen array" do - -> { ArraySpecs.empty_frozen_array.reject! {} }.should raise_error(FrozenError) + -> { ArraySpecs.empty_frozen_array.reject! {} }.should.raise(FrozenError) + end + + it "raises a FrozenError on a frozen array only during iteration if called without a block" do + enum = ArraySpecs.frozen_array.reject! + -> { enum.each {} }.should.raise(FrozenError) end it "does not truncate the array is the block raises an exception" do @@ -141,3 +151,8 @@ describe "Array#reject!" do it_behaves_like :enumeratorized_with_origin_size, :reject!, [1,2,3] it_behaves_like :delete_if, :reject! end + +describe "Array#reject!" do + @value_to_return = -> _ { false } + it_behaves_like :array_iterable_and_tolerating_size_increasing, :reject! +end diff --git a/spec/ruby/core/array/repeated_combination_spec.rb b/spec/ruby/core/array/repeated_combination_spec.rb index b62382024a..a714f05f54 100644 --- a/spec/ruby/core/array/repeated_combination_spec.rb +++ b/spec/ruby/core/array/repeated_combination_spec.rb @@ -6,16 +6,16 @@ describe "Array#repeated_combination" do end it "returns an enumerator when no block is provided" do - @array.repeated_combination(2).should be_an_instance_of(Enumerator) + @array.repeated_combination(2).should.instance_of?(Enumerator) end it "returns self when a block is given" do - @array.repeated_combination(2){}.should equal(@array) + @array.repeated_combination(2){}.should.equal?(@array) end it "yields nothing for negative length and return self" do - @array.repeated_combination(-1){ fail }.should equal(@array) - @array.repeated_combination(-10){ fail }.should equal(@array) + @array.repeated_combination(-1){ fail }.should.equal?(@array) + @array.repeated_combination(-10){ fail }.should.equal?(@array) end it "yields the expected repeated_combinations" do diff --git a/spec/ruby/core/array/repeated_permutation_spec.rb b/spec/ruby/core/array/repeated_permutation_spec.rb index a165fda09e..c54a8c0c2b 100644 --- a/spec/ruby/core/array/repeated_permutation_spec.rb +++ b/spec/ruby/core/array/repeated_permutation_spec.rb @@ -10,13 +10,13 @@ describe "Array#repeated_permutation" do it "returns an Enumerator of all repeated permutations of given length when called without a block" do enum = @numbers.repeated_permutation(2) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.to_a.sort.should == @permutations end it "yields all repeated_permutations to the block then returns self when called with block but no arguments" do yielded = [] - @numbers.repeated_permutation(2) {|n| yielded << n}.should equal(@numbers) + @numbers.repeated_permutation(2) {|n| yielded << n}.should.equal?(@numbers) yielded.sort.should == @permutations end diff --git a/spec/ruby/core/array/reverse_each_spec.rb b/spec/ruby/core/array/reverse_each_spec.rb index 28b8bfcb34..8fa5ce6da1 100644 --- a/spec/ruby/core/array/reverse_each_spec.rb +++ b/spec/ruby/core/array/reverse_each_spec.rb @@ -5,7 +5,7 @@ require_relative '../enumerable/shared/enumeratorized' # Modifying a collection while the contents are being iterated # gives undefined behavior. See -# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/23633 +# https://blade.ruby-lang.org/ruby-core/23633 describe "Array#reverse_each" do before :each do @@ -19,7 +19,7 @@ describe "Array#reverse_each" do it "returns self" do a = [:a, :b, :c] - a.reverse_each { |x| }.should equal(a) + a.reverse_each { |x| }.should.equal?(a) end it "yields only the top level element of an empty recursive arrays" do @@ -38,6 +38,20 @@ describe "Array#reverse_each" do [1, 2, 3].reverse_each.size.should == 3 end + it "tolerates increasing an array size during iteration" do + array = [:a, :b, :c] + ScratchPad.record [] + i = 0 + + array.reverse_each do |e| + ScratchPad << e + array.prepend i if i < 100 + i += 1 + end + + ScratchPad.recorded.should == [:c, :a, 1] + end + it_behaves_like :enumeratorize, :reverse_each it_behaves_like :enumeratorized_with_origin_size, :reverse_each, [1,2,3] end diff --git a/spec/ruby/core/array/reverse_spec.rb b/spec/ruby/core/array/reverse_spec.rb index 05dbd2efcf..f25a484be8 100644 --- a/spec/ruby/core/array/reverse_spec.rb +++ b/spec/ruby/core/array/reverse_spec.rb @@ -16,14 +16,14 @@ describe "Array#reverse" do end it "does not return subclass instance on Array subclasses" do - ArraySpecs::MyArray[1, 2, 3].reverse.should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].reverse.should.instance_of?(Array) end end describe "Array#reverse!" do it "reverses the elements in place" do a = [6, 3, 4, 2, 1] - a.reverse!.should equal(a) + a.reverse!.should.equal?(a) a.should == [1, 2, 4, 3, 6] [].reverse!.should == [] end @@ -37,6 +37,6 @@ describe "Array#reverse!" do end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array.reverse! }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.reverse! }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/array/rindex_spec.rb b/spec/ruby/core/array/rindex_spec.rb index 175c7bcfe2..858c39dc92 100644 --- a/spec/ruby/core/array/rindex_spec.rb +++ b/spec/ruby/core/array/rindex_spec.rb @@ -4,7 +4,7 @@ require_relative '../enumerable/shared/enumeratorized' # Modifying a collection while the contents are being iterated # gives undefined behavior. See -# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/23633 +# https://blade.ruby-lang.org/ruby-core/23633 describe "Array#rindex" do it "returns the first index backwards from the end where element == to object" do @@ -41,7 +41,7 @@ describe "Array#rindex" do it "properly handles empty recursive arrays" do empty = ArraySpecs.empty_recursive_array empty.rindex(empty).should == 0 - empty.rindex(1).should be_nil + empty.rindex(1).should == nil end it "properly handles recursive arrays" do @@ -68,10 +68,25 @@ describe "Array#rindex" do seen.should == [3] end + it "tolerates increasing an array size during iteration" do + array = [:a, :b, :c] + ScratchPad.record [] + i = 0 + + array.rindex do |e| + ScratchPad << e + array.prepend i if i < 100 + i += 1 + false + end + + ScratchPad.recorded.should == [:c, :a, 1] + end + describe "given no argument and no block" do it "produces an Enumerator" do enum = [4, 2, 1, 5, 1, 3].rindex - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.each { |x| x < 2 }.should == 4 end end diff --git a/spec/ruby/core/array/rotate_spec.rb b/spec/ruby/core/array/rotate_spec.rb index 60dcc8b113..009ce5ed49 100644 --- a/spec/ruby/core/array/rotate_spec.rb +++ b/spec/ruby/core/array/rotate_spec.rb @@ -29,10 +29,10 @@ describe "Array#rotate" do it "raises a TypeError if not passed an integer-like argument" do -> { [1, 2].rotate(nil) - }.should raise_error(TypeError) + }.should.raise(TypeError) -> { [1, 2].rotate("4") - }.should raise_error(TypeError) + }.should.raise(TypeError) end end @@ -50,18 +50,18 @@ describe "Array#rotate" do [].freeze.rotate [2].freeze.rotate(2) [1,2,3].freeze.rotate(-3) - }.should_not raise_error + }.should_not.raise end it "does not return self" do a = [1, 2, 3] - a.rotate.should_not equal(a) + a.rotate.should_not.equal?(a) a = [] - a.rotate(0).should_not equal(a) + a.rotate(0).should_not.equal?(a) end it "does not return subclass instance for Array subclasses" do - ArraySpecs::MyArray[1, 2, 3].rotate.should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].rotate.should.instance_of?(Array) end end @@ -69,7 +69,7 @@ describe "Array#rotate!" do describe "when passed no argument" do it "moves the first element to the end and returns self" do a = [1, 2, 3, 4, 5] - a.rotate!.should equal(a) + a.rotate!.should.equal?(a) a.should == [2, 3, 4, 5, 1] end end @@ -77,11 +77,11 @@ describe "Array#rotate!" do describe "with an argument n" do it "moves the first (n % size) elements at the end and returns self" do a = [1, 2, 3, 4, 5] - a.rotate!(2).should equal(a) + a.rotate!(2).should.equal?(a) a.should == [3, 4, 5, 1, 2] - a.rotate!(-12).should equal(a) + a.rotate!(-12).should.equal?(a) a.should == [1, 2, 3, 4, 5] - a.rotate!(13).should equal(a) + a.rotate!(13).should.equal?(a) a.should == [4, 5, 1, 2, 3] end @@ -96,34 +96,34 @@ describe "Array#rotate!" do it "raises a TypeError if not passed an integer-like argument" do -> { [1, 2].rotate!(nil) - }.should raise_error(TypeError) + }.should.raise(TypeError) -> { [1, 2].rotate!("4") - }.should raise_error(TypeError) + }.should.raise(TypeError) end end it "does nothing and returns self when the length is zero or one" do a = [1] - a.rotate!.should equal(a) + a.rotate!.should.equal?(a) a.should == [1] - a.rotate!(2).should equal(a) + a.rotate!(2).should.equal?(a) a.should == [1] - a.rotate!(-21).should equal(a) + a.rotate!(-21).should.equal?(a) a.should == [1] a = [] - a.rotate!.should equal(a) + a.rotate!.should.equal?(a) a.should == [] - a.rotate!(2).should equal(a) + a.rotate!(2).should.equal?(a) a.should == [] - a.rotate!(-21).should equal(a) + a.rotate!(-21).should.equal?(a) a.should == [] end it "raises a FrozenError on a frozen array" do - -> { [1, 2, 3].freeze.rotate!(0) }.should raise_error(FrozenError) - -> { [1].freeze.rotate!(42) }.should raise_error(FrozenError) - -> { [].freeze.rotate! }.should raise_error(FrozenError) + -> { [1, 2, 3].freeze.rotate!(0) }.should.raise(FrozenError) + -> { [1].freeze.rotate!(42) }.should.raise(FrozenError) + -> { [].freeze.rotate! }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/array/sample_spec.rb b/spec/ruby/core/array/sample_spec.rb index 5b3aac9aed..fd443b47de 100644 --- a/spec/ruby/core/array/sample_spec.rb +++ b/spec/ruby/core/array/sample_spec.rb @@ -14,19 +14,23 @@ describe "Array#sample" do end it "returns nil for an empty Array" do - [].sample.should be_nil + [].sample.should == nil end it "returns nil for an empty array when called without n and a Random is given" do - [].sample(random: Random.new(42)).should be_nil + [].sample(random: Random.new(42)).should == nil end it "returns a single value when not passed a count" do - [4].sample.should equal(4) + [4].sample.should.equal?(4) end it "returns a single value when not passed a count and a Random is given" do - [4].sample(random: Random.new(42)).should equal(4) + [4].sample(random: Random.new(42)).should.equal?(4) + end + + it "returns a single value when not passed a count and a Random class is given" do + [4].sample(random: Random).should.equal?(4) end it "returns an empty Array when passed zero" do @@ -34,12 +38,12 @@ describe "Array#sample" do end it "returns an Array of elements when passed a count" do - [1, 2, 3, 4].sample(3).should be_an_instance_of(Array) + [1, 2, 3, 4].sample(3).should.instance_of?(Array) end it "returns elements from the Array" do array = [1, 2, 3, 4] - array.sample(3).all? { |x| array.should include(x) } + array.sample(3).all? { |x| array.should.include?(x) } end it "returns at most the number of elements in the Array" do @@ -63,11 +67,11 @@ describe "Array#sample" do end it "raises ArgumentError when passed a negative count" do - -> { [1, 2].sample(-1) }.should raise_error(ArgumentError) + -> { [1, 2].sample(-1) }.should.raise(ArgumentError) end it "does not return subclass instances with Array subclass" do - ArraySpecs::MyArray[1, 2, 3].sample(2).should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].sample(2).should.instance_of?(Array) end describe "with options" do @@ -75,13 +79,13 @@ describe "Array#sample" do obj = mock("array_sample_random") obj.should_receive(:rand).and_return(0.5) - [1, 2].sample(random: obj).should be_an_instance_of(Integer) + [1, 2].sample(random: obj).should.instance_of?(Integer) end it "raises a NoMethodError if an object passed for the RNG does not define #rand" do obj = BasicObject.new - -> { [1, 2].sample(random: obj) }.should raise_error(NoMethodError) + -> { [1, 2].sample(random: obj) }.should.raise(NoMethodError) end describe "when the object returned by #rand is an Integer" do @@ -101,14 +105,21 @@ describe "Array#sample" do random = mock("array_sample_random") random.should_receive(:rand).and_return(-1) - -> { [1, 2].sample(random: random) }.should raise_error(RangeError) + -> { [1, 2].sample(random: random) }.should.raise(RangeError) end it "raises a RangeError if the value is equal to the Array size" do random = mock("array_sample_random") random.should_receive(:rand).and_return(2) - -> { [1, 2].sample(random: random) }.should raise_error(RangeError) + -> { [1, 2].sample(random: random) }.should.raise(RangeError) + end + + it "raises a RangeError if the value is greater than the Array size" do + random = mock("array_sample_random") + random.should_receive(:rand).and_return(3) + + -> { [1, 2].sample(random: random) }.should.raise(RangeError) end end end @@ -129,7 +140,7 @@ describe "Array#sample" do random = mock("array_sample_random") random.should_receive(:rand).and_return(value) - -> { [1, 2].sample(random: random) }.should raise_error(RangeError) + -> { [1, 2].sample(random: random) }.should.raise(RangeError) end it "raises a RangeError if the value is equal to the Array size" do @@ -138,7 +149,7 @@ describe "Array#sample" do random = mock("array_sample_random") random.should_receive(:rand).and_return(value) - -> { [1, 2].sample(random: random) }.should raise_error(RangeError) + -> { [1, 2].sample(random: random) }.should.raise(RangeError) end end end diff --git a/spec/ruby/core/array/select_spec.rb b/spec/ruby/core/array/select_spec.rb index 298b591744..e8775ee5ac 100644 --- a/spec/ruby/core/array/select_spec.rb +++ b/spec/ruby/core/array/select_spec.rb @@ -7,7 +7,7 @@ end describe "Array#select!" do it "returns nil if no changes were made in the array" do - [1, 2, 3].select! { true }.should be_nil + [1, 2, 3].select! { true }.should == nil end it_behaves_like :keep_if, :select! diff --git a/spec/ruby/core/array/shared/clone.rb b/spec/ruby/core/array/shared/clone.rb index 035b45ec99..1a45c2fe2c 100644 --- a/spec/ruby/core/array/shared/clone.rb +++ b/spec/ruby/core/array/shared/clone.rb @@ -1,14 +1,14 @@ describe :array_clone, shared: true do it "returns an Array or a subclass instance" do - [].send(@method).should be_an_instance_of(Array) - ArraySpecs::MyArray[1, 2].send(@method).should be_an_instance_of(ArraySpecs::MyArray) + [].send(@method).should.instance_of?(Array) + ArraySpecs::MyArray[1, 2].send(@method).should.instance_of?(ArraySpecs::MyArray) end it "produces a shallow copy where the references are directly copied" do a = [mock('1'), mock('2')] b = a.send @method - b.first.should equal a.first - b.last.should equal a.last + b.first.should.equal? a.first + b.last.should.equal? a.last end it "creates a new array containing all elements or the original" do diff --git a/spec/ruby/core/array/shared/collect.rb b/spec/ruby/core/array/shared/collect.rb index 8d75f7db9a..aec51c9dc9 100644 --- a/spec/ruby/core/array/shared/collect.rb +++ b/spec/ruby/core/array/shared/collect.rb @@ -1,15 +1,16 @@ require_relative '../../enumerable/shared/enumeratorized' +require_relative '../shared/iterable_and_tolerating_size_increasing' describe :array_collect, shared: true do it "returns a copy of array with each element replaced by the value returned by block" do a = ['a', 'b', 'c', 'd'] b = a.send(@method) { |i| i + '!' } b.should == ["a!", "b!", "c!", "d!"] - b.should_not equal a + b.should_not.equal? a end it "does not return subclass instance" do - ArraySpecs::MyArray[1, 2, 3].send(@method) { |x| x + 1 }.should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].send(@method) { |x| x + 1 }.should.instance_of?(Array) end it "does not change self" do @@ -32,33 +33,35 @@ describe :array_collect, shared: true do it "returns an Enumerator when no block given" do a = [1, 2, 3] - a.send(@method).should be_an_instance_of(Enumerator) + a.send(@method).should.instance_of?(Enumerator) end it "raises an ArgumentError when no block and with arguments" do a = [1, 2, 3] -> { a.send(@method, :foo) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end before :all do @object = [1, 2, 3, 4] end it_should_behave_like :enumeratorized_with_origin_size + + it_should_behave_like :array_iterable_and_tolerating_size_increasing end describe :array_collect_b, shared: true do it "replaces each element with the value returned by block" do a = [7, 9, 3, 5] - a.send(@method) { |i| i - 1 }.should equal(a) + a.send(@method) { |i| i - 1 }.should.equal?(a) a.should == [6, 8, 2, 4] end it "returns self" do a = [1, 2, 3, 4, 5] b = a.send(@method) {|i| i+1 } - a.should equal b + a.should.equal? b end it "returns the evaluated value of block but its contents is partially modified, if it broke in the block" do @@ -77,33 +80,62 @@ describe :array_collect_b, shared: true do it "returns an Enumerator when no block given, and the enumerator can modify the original array" do a = [1, 2, 3] enum = a.send(@method) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.each{|i| "#{i}!" } a.should == ["1!", "2!", "3!"] end describe "when frozen" do it "raises a FrozenError" do - -> { ArraySpecs.frozen_array.send(@method) {} }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.send(@method) {} }.should.raise(FrozenError) end it "raises a FrozenError when empty" do - -> { ArraySpecs.empty_frozen_array.send(@method) {} }.should raise_error(FrozenError) + -> { ArraySpecs.empty_frozen_array.send(@method) {} }.should.raise(FrozenError) end it "raises a FrozenError when calling #each on the returned Enumerator" do enumerator = ArraySpecs.frozen_array.send(@method) - -> { enumerator.each {|x| x } }.should raise_error(FrozenError) + -> { enumerator.each {|x| x } }.should.raise(FrozenError) end it "raises a FrozenError when calling #each on the returned Enumerator when empty" do enumerator = ArraySpecs.empty_frozen_array.send(@method) - -> { enumerator.each {|x| x } }.should raise_error(FrozenError) + -> { enumerator.each {|x| x } }.should.raise(FrozenError) + end + end + + it "does not truncate the array is the block raises an exception" do + a = [1, 2, 3] + begin + a.send(@method) { raise StandardError, 'Oops' } + rescue + end + + a.should == [1, 2, 3] + end + + it "only changes elements before error is raised, keeping the element which raised an error." do + a = [1, 2, 3, 4] + begin + a.send(@method) do |e| + case e + when 1 then -1 + when 2 then -2 + when 3 then raise StandardError, 'Oops' + else 0 + end + end + rescue StandardError end + + a.should == [-1, -2, 3, 4] end before :all do @object = [1, 2, 3, 4] end it_should_behave_like :enumeratorized_with_origin_size + + it_should_behave_like :array_iterable_and_tolerating_size_increasing end diff --git a/spec/ruby/core/array/shared/difference.rb b/spec/ruby/core/array/shared/difference.rb index 3e69050d82..3fe22331bd 100644 --- a/spec/ruby/core/array/shared/difference.rb +++ b/spec/ruby/core/array/shared/difference.rb @@ -27,13 +27,13 @@ describe :array_binary_difference, shared: true do it "raises a TypeError if the argument cannot be coerced to an Array by calling #to_ary" do obj = mock('not an array') - -> { [1, 2, 3].send(@method, obj) }.should raise_error(TypeError) + -> { [1, 2, 3].send(@method, obj) }.should.raise(TypeError) end it "does not return subclass instance for Array subclasses" do - ArraySpecs::MyArray[1, 2, 3].send(@method, []).should be_an_instance_of(Array) - ArraySpecs::MyArray[1, 2, 3].send(@method, ArraySpecs::MyArray[]).should be_an_instance_of(Array) - [1, 2, 3].send(@method, ArraySpecs::MyArray[]).should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].send(@method, []).should.instance_of?(Array) + ArraySpecs::MyArray[1, 2, 3].send(@method, ArraySpecs::MyArray[]).should.instance_of?(Array) + [1, 2, 3].send(@method, ArraySpecs::MyArray[]).should.instance_of?(Array) end it "does not call to_ary on array subclasses" do diff --git a/spec/ruby/core/array/shared/enumeratorize.rb b/spec/ruby/core/array/shared/enumeratorize.rb index a19a5d3b9b..5beab5c4c4 100644 --- a/spec/ruby/core/array/shared/enumeratorize.rb +++ b/spec/ruby/core/array/shared/enumeratorize.rb @@ -1,5 +1,5 @@ describe :enumeratorize, shared: true do it "returns an Enumerator if no block given" do - [1,2].send(@method).should be_an_instance_of(Enumerator) + [1,2].send(@method).should.instance_of?(Enumerator) end end diff --git a/spec/ruby/core/array/shared/eql.rb b/spec/ruby/core/array/shared/eql.rb index b5d9128434..5e770bf167 100644 --- a/spec/ruby/core/array/shared/eql.rb +++ b/spec/ruby/core/array/shared/eql.rb @@ -1,59 +1,59 @@ describe :array_eql, shared: true do it "returns true if other is the same array" do a = [1] - a.send(@method, a).should be_true + a.send(@method, a).should == true end it "returns true if corresponding elements are #eql?" do - [].send(@method, []).should be_true - [1, 2, 3, 4].send(@method, [1, 2, 3, 4]).should be_true + [].send(@method, []).should == true + [1, 2, 3, 4].send(@method, [1, 2, 3, 4]).should == true end it "returns false if other is shorter than self" do - [1, 2, 3, 4].send(@method, [1, 2, 3]).should be_false + [1, 2, 3, 4].send(@method, [1, 2, 3]).should == false end it "returns false if other is longer than self" do - [1, 2, 3, 4].send(@method, [1, 2, 3, 4, 5]).should be_false + [1, 2, 3, 4].send(@method, [1, 2, 3, 4, 5]).should == false end it "returns false immediately when sizes of the arrays differ" do obj = mock('1') obj.should_not_receive(@method) - [] .send(@method, [obj] ).should be_false - [obj] .send(@method, [] ).should be_false + [] .send(@method, [obj] ).should == false + [obj] .send(@method, [] ).should == false end it "handles well recursive arrays" do a = ArraySpecs.empty_recursive_array - a .send(@method, [a] ).should be_true - a .send(@method, [[a]] ).should be_true - [a] .send(@method, a ).should be_true - [[a]] .send(@method, a ).should be_true + a .send(@method, [a] ).should == true + a .send(@method, [[a]] ).should == true + [a] .send(@method, a ).should == true + [[a]] .send(@method, a ).should == true # These may be surprising, but no difference can be # found between these arrays, so they are ==. # There is no "path" that will lead to a difference # (contrary to other examples below) a2 = ArraySpecs.empty_recursive_array - a .send(@method, a2 ).should be_true - a .send(@method, [a2] ).should be_true - a .send(@method, [[a2]] ).should be_true - [a] .send(@method, a2 ).should be_true - [[a]] .send(@method, a2 ).should be_true + a .send(@method, a2 ).should == true + a .send(@method, [a2] ).should == true + a .send(@method, [[a2]] ).should == true + [a] .send(@method, a2 ).should == true + [[a]] .send(@method, a2 ).should == true back = [] forth = [back]; back << forth; - back .send(@method, a ).should be_true + back .send(@method, a ).should == true x = []; x << x << x - x .send(@method, a ).should be_false # since x.size != a.size - x .send(@method, [a, a] ).should be_false # since x[0].size != [a, a][0].size - x .send(@method, [x, a] ).should be_false # since x[1].size != [x, a][1].size - [x, a] .send(@method, [a, x] ).should be_false # etc... - x .send(@method, [x, x] ).should be_true - x .send(@method, [[x, x], [x, x]] ).should be_true + x .send(@method, a ).should == false # since x.size != a.size + x .send(@method, [a, a] ).should == false # since x[0].size != [a, a][0].size + x .send(@method, [x, a] ).should == false # since x[1].size != [x, a][1].size + [x, a] .send(@method, [a, x] ).should == false # etc... + x .send(@method, [x, x] ).should == true + x .send(@method, [[x, x], [x, x]] ).should == true tree = []; branch = []; branch << tree << tree; tree << branch @@ -62,31 +62,31 @@ describe :array_eql, shared: true do forest = [tree, branch, :bird, a]; forest << forest forest2 = [tree2, branch2, :bird, a2]; forest2 << forest2 - forest .send(@method, forest2 ).should be_true - forest .send(@method, [tree2, branch, :bird, a, forest2]).should be_true + forest .send(@method, forest2 ).should == true + forest .send(@method, [tree2, branch, :bird, a, forest2]).should == true diffforest = [branch2, tree2, :bird, a2]; diffforest << forest2 - forest .send(@method, diffforest ).should be_false # since forest[0].size == 1 != 3 == diffforest[0] - forest .send(@method, [nil] ).should be_false - forest .send(@method, [forest] ).should be_false + forest .send(@method, diffforest ).should == false # since forest[0].size == 1 != 3 == diffforest[0] + forest .send(@method, [nil] ).should == false + forest .send(@method, [forest] ).should == false end it "does not call #to_ary on its argument" do obj = mock('to_ary') obj.should_not_receive(:to_ary) - [1, 2, 3].send(@method, obj).should be_false + [1, 2, 3].send(@method, obj).should == false end it "does not call #to_ary on Array subclasses" do ary = ArraySpecs::ToAryArray[5, 6, 7] ary.should_not_receive(:to_ary) - [5, 6, 7].send(@method, ary).should be_true + [5, 6, 7].send(@method, ary).should == true end it "ignores array class differences" do - ArraySpecs::MyArray[1, 2, 3].send(@method, [1, 2, 3]).should be_true - ArraySpecs::MyArray[1, 2, 3].send(@method, ArraySpecs::MyArray[1, 2, 3]).should be_true - [1, 2, 3].send(@method, ArraySpecs::MyArray[1, 2, 3]).should be_true + ArraySpecs::MyArray[1, 2, 3].send(@method, [1, 2, 3]).should == true + ArraySpecs::MyArray[1, 2, 3].send(@method, ArraySpecs::MyArray[1, 2, 3]).should == true + [1, 2, 3].send(@method, ArraySpecs::MyArray[1, 2, 3]).should == true end end diff --git a/spec/ruby/core/array/shared/index.rb b/spec/ruby/core/array/shared/index.rb index a9896554f2..cc6d6cfb5b 100644 --- a/spec/ruby/core/array/shared/index.rb +++ b/spec/ruby/core/array/shared/index.rb @@ -1,3 +1,5 @@ +require_relative '../shared/iterable_and_tolerating_size_increasing' + describe :array_index, shared: true do it "returns the index of the first element == to object" do x = mock('3') @@ -31,7 +33,9 @@ describe :array_index, shared: true do describe "given no argument and no block" do it "produces an Enumerator" do - [].send(@method).should be_an_instance_of(Enumerator) + [].send(@method).should.instance_of?(Enumerator) end end + + it_should_behave_like :array_iterable_and_tolerating_size_increasing end diff --git a/spec/ruby/core/array/shared/inspect.rb b/spec/ruby/core/array/shared/inspect.rb index a2b43d4959..7197cd7f26 100644 --- a/spec/ruby/core/array/shared/inspect.rb +++ b/spec/ruby/core/array/shared/inspect.rb @@ -2,7 +2,7 @@ require_relative '../fixtures/encoded_strings' describe :array_inspect, shared: true do it "returns a string" do - [1, 2, 3].send(@method).should be_an_instance_of(String) + [1, 2, 3].send(@method).should.instance_of?(String) end it "returns '[]' for an empty Array" do @@ -19,7 +19,7 @@ describe :array_inspect, shared: true do end it "does not call #to_s on a String returned from #inspect" do - str = "abc" + str = +"abc" str.should_not_receive(:to_s) [str].send(@method).should == '["abc"]' @@ -55,7 +55,7 @@ describe :array_inspect, shared: true do obj.should_receive(:inspect).and_return(obj) obj.should_receive(:to_s).and_raise(Exception) - -> { [obj].send(@method) }.should raise_error(Exception) + -> { [obj].send(@method) }.should.raise(Exception) end it "represents a recursive element with '[...]'" do @@ -98,8 +98,8 @@ describe :array_inspect, shared: true do end it "does not raise if inspected result is not default external encoding" do - utf_16be = mock("utf_16be") - utf_16be.should_receive(:inspect).and_return(%<"utf_16be \u3042">.encode!(Encoding::UTF_16BE)) + utf_16be = mock(+"utf_16be") + utf_16be.should_receive(:inspect).and_return(%<"utf_16be \u3042">.encode(Encoding::UTF_16BE)) [utf_16be].send(@method).should == '["utf_16be \u3042"]' end diff --git a/spec/ruby/core/array/shared/intersection.rb b/spec/ruby/core/array/shared/intersection.rb index 49849b08c2..dda72e8bd7 100644 --- a/spec/ruby/core/array/shared/intersection.rb +++ b/spec/ruby/core/array/shared/intersection.rb @@ -11,7 +11,8 @@ describe :array_intersection, shared: true do end it "creates an array with elements in order they are first encountered" do - [ 1, 2, 3, 2, 5 ].send(@method, [ 5, 2, 3, 4 ]).should == [2, 3, 5] + [ 1, 2, 3, 2, 5, 6, 7, 8 ].send(@method, [ 5, 2, 3, 4 ]).should == [2, 3, 5] # array > other + [ 5, 2, 3, 4 ].send(@method, [ 1, 2, 3, 2, 5, 6, 7, 8 ]).should == [5, 2, 3] # array < other end it "does not modify the original Array" do @@ -65,9 +66,9 @@ describe :array_intersection, shared: true do end it "does return subclass instances for Array subclasses" do - ArraySpecs::MyArray[1, 2, 3].send(@method, []).should be_an_instance_of(Array) - ArraySpecs::MyArray[1, 2, 3].send(@method, ArraySpecs::MyArray[1, 2, 3]).should be_an_instance_of(Array) - [].send(@method, ArraySpecs::MyArray[1, 2, 3]).should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].send(@method, []).should.instance_of?(Array) + ArraySpecs::MyArray[1, 2, 3].send(@method, ArraySpecs::MyArray[1, 2, 3]).should.instance_of?(Array) + [].send(@method, ArraySpecs::MyArray[1, 2, 3]).should.instance_of?(Array) end it "does not call to_ary on array subclasses" do diff --git a/spec/ruby/core/array/shared/iterable_and_tolerating_size_increasing.rb b/spec/ruby/core/array/shared/iterable_and_tolerating_size_increasing.rb new file mode 100644 index 0000000000..3e73bad44b --- /dev/null +++ b/spec/ruby/core/array/shared/iterable_and_tolerating_size_increasing.rb @@ -0,0 +1,25 @@ +describe :array_iterable_and_tolerating_size_increasing, shared: true do + before do + @value_to_return ||= -> _ { nil } + end + + it "tolerates increasing an array size during iteration" do + # The goal is to trigger potential reallocation of internal array storage, so we: + # - use elements of different types, starting with the less generic (Integer) + # - add reasonably big number of new elements (~ 100) + array = [1, 2, 3] # to test some methods we need several uniq elements + array_to_join = [:a, :b, :c] + (4..100).to_a + + ScratchPad.record [] + i = 0 + + array.send(@method) do |e| + ScratchPad << e + array << array_to_join[i] if i < array_to_join.size + i += 1 + @value_to_return.call(e) + end + + ScratchPad.recorded.should == [1, 2, 3] + array_to_join + end +end diff --git a/spec/ruby/core/array/shared/join.rb b/spec/ruby/core/array/shared/join.rb index 507b13e3c8..2be60a4dbc 100644 --- a/spec/ruby/core/array/shared/join.rb +++ b/spec/ruby/core/array/shared/join.rb @@ -49,13 +49,13 @@ describe :array_join_with_default_separator, shared: true do it "raises a NoMethodError if an element does not respond to #to_str, #to_ary, or #to_s" do obj = mock('o') class << obj; undef :to_s; end - -> { [1, obj].send(@method) }.should raise_error(NoMethodError) + -> { [1, obj].send(@method) }.should.raise(NoMethodError) end it "raises an ArgumentError when the Array is recursive" do - -> { ArraySpecs.recursive_array.send(@method) }.should raise_error(ArgumentError) - -> { ArraySpecs.head_recursive_array.send(@method) }.should raise_error(ArgumentError) - -> { ArraySpecs.empty_recursive_array.send(@method) }.should raise_error(ArgumentError) + -> { ArraySpecs.recursive_array.send(@method) }.should.raise(ArgumentError) + -> { ArraySpecs.head_recursive_array.send(@method) }.should.raise(ArgumentError) + -> { ArraySpecs.empty_recursive_array.send(@method) }.should.raise(ArgumentError) end it "uses the first encoding when other strings are compatible" do @@ -81,7 +81,7 @@ describe :array_join_with_default_separator, shared: true do it "fails for arrays with incompatibly-encoded strings" do ary_utf8_bad_binary = ArraySpecs.array_with_utf8_and_binary_strings - -> { ary_utf8_bad_binary.send(@method) }.should raise_error(EncodingError) + -> { ary_utf8_bad_binary.send(@method) }.should.raise(EncodingError) end context "when $, is not nil" do diff --git a/spec/ruby/core/array/shared/keep_if.rb b/spec/ruby/core/array/shared/keep_if.rb index f26aff028c..44625eebd1 100644 --- a/spec/ruby/core/array/shared/keep_if.rb +++ b/spec/ruby/core/array/shared/keep_if.rb @@ -1,14 +1,15 @@ require_relative '../../enumerable/shared/enumeratorized' +require_relative '../shared/iterable_and_tolerating_size_increasing' describe :keep_if, shared: true do it "deletes elements for which the block returns a false value" do array = [1, 2, 3, 4, 5] - array.send(@method) {|item| item > 3 }.should equal(array) + array.send(@method) {|item| item > 3 }.should.equal?(array) array.should == [4, 5] end it "returns an enumerator if no block is given" do - [1, 2, 3].send(@method).should be_an_instance_of(Enumerator) + [1, 2, 3].send(@method).should.instance_of?(Enumerator) end it "updates the receiver after all blocks" do @@ -32,29 +33,63 @@ describe :keep_if, shared: true do end it "returns an Enumerator if no block is given" do - @frozen.send(@method).should be_an_instance_of(Enumerator) + @frozen.send(@method).should.instance_of?(Enumerator) end describe "with truthy block" do it "keeps elements after any exception" do - -> { @frozen.send(@method) { true } }.should raise_error(Exception) + -> { @frozen.send(@method) { true } }.should.raise(Exception) @frozen.should == @origin end it "raises a FrozenError" do - -> { @frozen.send(@method) { true } }.should raise_error(FrozenError) + -> { @frozen.send(@method) { true } }.should.raise(FrozenError) end end describe "with falsy block" do it "keeps elements after any exception" do - -> { @frozen.send(@method) { false } }.should raise_error(Exception) + -> { @frozen.send(@method) { false } }.should.raise(Exception) @frozen.should == @origin end it "raises a FrozenError" do - -> { @frozen.send(@method) { false } }.should raise_error(FrozenError) + -> { @frozen.send(@method) { false } }.should.raise(FrozenError) end end + + it "raises a FrozenError on a frozen array only during iteration if called without a block" do + enum = @frozen.send(@method) + -> { enum.each {} }.should.raise(FrozenError) + end + end + + it "does not truncate the array is the block raises an exception" do + a = [1, 2, 3] + begin + a.send(@method) { raise StandardError, 'Oops' } + rescue + end + + a.should == [1, 2, 3] end + + it "only changes elements before error is raised, keeping the element which raised an error." do + a = [1, 2, 3, 4] + begin + a.send(@method) do |e| + case e + when 2 then false + when 3 then raise StandardError, 'Oops' + else true + end + end + rescue StandardError + end + + a.should == [1, 3, 4] + end + + @value_to_return = -> _ { true } + it_should_behave_like :array_iterable_and_tolerating_size_increasing end diff --git a/spec/ruby/core/array/shared/push.rb b/spec/ruby/core/array/shared/push.rb index ac790fb6a4..ec406e506e 100644 --- a/spec/ruby/core/array/shared/push.rb +++ b/spec/ruby/core/array/shared/push.rb @@ -1,7 +1,7 @@ describe :array_push, shared: true do it "appends the arguments to the array" do a = [ "a", "b", "c" ] - a.send(@method, "d", "e", "f").should equal(a) + a.send(@method, "d", "e", "f").should.equal?(a) a.send(@method).should == ["a", "b", "c", "d", "e", "f"] a.send(@method, 5) a.should == ["a", "b", "c", "d", "e", "f", 5] @@ -27,7 +27,7 @@ describe :array_push, shared: true do end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array.send(@method, 1) }.should raise_error(FrozenError) - -> { ArraySpecs.frozen_array.send(@method) }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.send(@method, 1) }.should.raise(FrozenError) + -> { ArraySpecs.frozen_array.send(@method) }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/array/shared/replace.rb b/spec/ruby/core/array/shared/replace.rb index 9a6e60c1b0..06bfd00795 100644 --- a/spec/ruby/core/array/shared/replace.rb +++ b/spec/ruby/core/array/shared/replace.rb @@ -2,9 +2,9 @@ describe :array_replace, shared: true do it "replaces the elements with elements from other array" do a = [1, 2, 3, 4, 5] b = ['a', 'b', 'c'] - a.send(@method, b).should equal(a) + a.send(@method, b).should.equal?(a) a.should == b - a.should_not equal(b) + a.should_not.equal?(b) a.send(@method, [4] * 10) a.should == [4] * 10 @@ -27,7 +27,7 @@ describe :array_replace, shared: true do it "returns self" do ary = [1, 2, 3] other = [:a, :b, :c] - ary.send(@method, other).should equal(ary) + ary.send(@method, other).should.equal?(ary) end it "does not make self dependent to the original array" do @@ -55,6 +55,6 @@ describe :array_replace, shared: true do it "raises a FrozenError on a frozen array" do -> { ArraySpecs.frozen_array.send(@method, ArraySpecs.frozen_array) - }.should raise_error(FrozenError) + }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/array/shared/select.rb b/spec/ruby/core/array/shared/select.rb index 09101e8ab5..cb4f9acbb7 100644 --- a/spec/ruby/core/array/shared/select.rb +++ b/spec/ruby/core/array/shared/select.rb @@ -2,11 +2,14 @@ require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative '../shared/enumeratorize' require_relative '../shared/keep_if' +require_relative '../shared/iterable_and_tolerating_size_increasing' require_relative '../../enumerable/shared/enumeratorized' describe :array_select, shared: true do it_should_behave_like :enumeratorize + it_should_behave_like :array_iterable_and_tolerating_size_increasing + before :each do @object = [1,2,3] end @@ -17,7 +20,7 @@ describe :array_select, shared: true do end it "does not return subclass instance on Array subclasses" do - ArraySpecs::MyArray[1, 2, 3].send(@method) { true }.should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].send(@method) { true }.should.instance_of?(Array) end it "properly handles recursive arrays" do diff --git a/spec/ruby/core/array/shared/slice.rb b/spec/ruby/core/array/shared/slice.rb index 3b09fdcbc6..b838d86118 100644 --- a/spec/ruby/core/array/shared/slice.rb +++ b/spec/ruby/core/array/shared/slice.rb @@ -130,12 +130,12 @@ describe :array_slice, shared: true do def from.to_int() 'cat' end def to.to_int() -2 end - -> { a.send(@method, from..to) }.should raise_error(TypeError) + -> { a.send(@method, from..to) }.should.raise(TypeError) def from.to_int() 1 end def to.to_int() 'cat' end - -> { a.send(@method, from..to) }.should raise_error(TypeError) + -> { a.send(@method, from..to) }.should.raise(TypeError) end it "returns the elements specified by Range indexes with [m..n]" do @@ -287,10 +287,10 @@ describe :array_slice, shared: true do a.send(@method, 1..0).should == [] a.send(@method, 1...0).should == [] - -> { a.send(@method, "a" .. "b") }.should raise_error(TypeError) - -> { a.send(@method, "a" ... "b") }.should raise_error(TypeError) - -> { a.send(@method, from .. "b") }.should raise_error(TypeError) - -> { a.send(@method, from ... "b") }.should raise_error(TypeError) + -> { a.send(@method, "a" .. "b") }.should.raise(TypeError) + -> { a.send(@method, "a" ... "b") }.should.raise(TypeError) + -> { a.send(@method, from .. "b") }.should.raise(TypeError) + -> { a.send(@method, from ... "b") }.should.raise(TypeError) end it "returns the same elements as [m..n] and [m...n] with Range subclasses" do @@ -397,92 +397,64 @@ describe :array_slice, shared: true do @array = ArraySpecs::MyArray[1, 2, 3, 4, 5] end - ruby_version_is ''...'3.0' do - it "returns a subclass instance with [n, m]" do - @array.send(@method, 0, 2).should be_an_instance_of(ArraySpecs::MyArray) - end - - it "returns a subclass instance with [-n, m]" do - @array.send(@method, -3, 2).should be_an_instance_of(ArraySpecs::MyArray) - end - - it "returns a subclass instance with [n..m]" do - @array.send(@method, 1..3).should be_an_instance_of(ArraySpecs::MyArray) - end - - it "returns a subclass instance with [n...m]" do - @array.send(@method, 1...3).should be_an_instance_of(ArraySpecs::MyArray) - end - - it "returns a subclass instance with [-n..-m]" do - @array.send(@method, -3..-1).should be_an_instance_of(ArraySpecs::MyArray) - end - - it "returns a subclass instance with [-n...-m]" do - @array.send(@method, -3...-1).should be_an_instance_of(ArraySpecs::MyArray) - end + it "returns a Array instance with [n, m]" do + @array.send(@method, 0, 2).should.instance_of?(Array) end - ruby_version_is '3.0' do - it "returns a Array instance with [n, m]" do - @array.send(@method, 0, 2).should be_an_instance_of(Array) - end - - it "returns a Array instance with [-n, m]" do - @array.send(@method, -3, 2).should be_an_instance_of(Array) - end + it "returns a Array instance with [-n, m]" do + @array.send(@method, -3, 2).should.instance_of?(Array) + end - it "returns a Array instance with [n..m]" do - @array.send(@method, 1..3).should be_an_instance_of(Array) - end + it "returns a Array instance with [n..m]" do + @array.send(@method, 1..3).should.instance_of?(Array) + end - it "returns a Array instance with [n...m]" do - @array.send(@method, 1...3).should be_an_instance_of(Array) - end + it "returns a Array instance with [n...m]" do + @array.send(@method, 1...3).should.instance_of?(Array) + end - it "returns a Array instance with [-n..-m]" do - @array.send(@method, -3..-1).should be_an_instance_of(Array) - end + it "returns a Array instance with [-n..-m]" do + @array.send(@method, -3..-1).should.instance_of?(Array) + end - it "returns a Array instance with [-n...-m]" do - @array.send(@method, -3...-1).should be_an_instance_of(Array) - end + it "returns a Array instance with [-n...-m]" do + @array.send(@method, -3...-1).should.instance_of?(Array) end it "returns an empty array when m == n with [m...n]" do @array.send(@method, 1...1).should == [] - ScratchPad.recorded.should be_nil + ScratchPad.recorded.should == nil end it "returns an empty array with [0...0]" do @array.send(@method, 0...0).should == [] - ScratchPad.recorded.should be_nil + ScratchPad.recorded.should == nil end it "returns an empty array when m > n and m, n are positive with [m..n]" do @array.send(@method, 3..2).should == [] - ScratchPad.recorded.should be_nil + ScratchPad.recorded.should == nil end it "returns an empty array when m > n and m, n are negative with [m..n]" do @array.send(@method, -2..-3).should == [] - ScratchPad.recorded.should be_nil + ScratchPad.recorded.should == nil end it "returns [] if index == array.size with [index, length]" do @array.send(@method, 5, 2).should == [] - ScratchPad.recorded.should be_nil + ScratchPad.recorded.should == nil end it "returns [] if the index is valid but length is zero with [index, length]" do @array.send(@method, 0, 0).should == [] @array.send(@method, 2, 0).should == [] - ScratchPad.recorded.should be_nil + ScratchPad.recorded.should == nil end it "does not call #initialize on the subclass instance" do @array.send(@method, 0, 3).should == [1, 2, 3] - ScratchPad.recorded.should be_nil + ScratchPad.recorded.should == nil end end @@ -490,13 +462,13 @@ describe :array_slice, shared: true do array = [1, 2, 3, 4, 5, 6] obj = mock('large value') obj.should_receive(:to_int).and_return(bignum_value) - -> { array.send(@method, obj) }.should raise_error(RangeError) + -> { array.send(@method, obj) }.should.raise(RangeError) obj = 8e19 - -> { array.send(@method, obj) }.should raise_error(RangeError) + -> { array.send(@method, obj) }.should.raise(RangeError) # boundary value when longs are 64 bits - -> { array.send(@method, 2.0**63) }.should raise_error(RangeError) + -> { array.send(@method, 2.0**63) }.should.raise(RangeError) # just under the boundary value when longs are 64 bits array.send(@method, max_long.to_f.prev_float).should == nil @@ -506,20 +478,20 @@ describe :array_slice, shared: true do array = [1, 2, 3, 4, 5, 6] obj = mock('large value') obj.should_receive(:to_int).and_return(bignum_value) - -> { array.send(@method, 1, obj) }.should raise_error(RangeError) + -> { array.send(@method, 1, obj) }.should.raise(RangeError) obj = 8e19 - -> { array.send(@method, 1, obj) }.should raise_error(RangeError) + -> { array.send(@method, 1, obj) }.should.raise(RangeError) end it "raises a type error if a range is passed with a length" do - ->{ [1, 2, 3].send(@method, 1..2, 1) }.should raise_error(TypeError) + ->{ [1, 2, 3].send(@method, 1..2, 1) }.should.raise(TypeError) end it "raises a RangeError if passed a range with a bound that is too large" do array = [1, 2, 3, 4, 5, 6] - -> { array.send(@method, bignum_value..(bignum_value + 1)) }.should raise_error(RangeError) - -> { array.send(@method, 0..bignum_value) }.should raise_error(RangeError) + -> { array.send(@method, bignum_value..(bignum_value + 1)) }.should.raise(RangeError) + -> { array.send(@method, 0..bignum_value) }.should.raise(RangeError) end it "can accept endless ranges" do @@ -534,239 +506,237 @@ describe :array_slice, shared: true do a.send(@method, eval("(-9...)")).should == nil end - ruby_version_is "3.0" do - describe "can be sliced with Enumerator::ArithmeticSequence" do - before :each do - @array = [0, 1, 2, 3, 4, 5] - end + describe "can be sliced with Enumerator::ArithmeticSequence" do + before :each do + @array = [0, 1, 2, 3, 4, 5] + end - it "has endless range and positive steps" do - @array.send(@method, eval("(0..).step(1)")).should == [0, 1, 2, 3, 4, 5] - @array.send(@method, eval("(0..).step(2)")).should == [0, 2, 4] - @array.send(@method, eval("(0..).step(10)")).should == [0] + it "has endless range and positive steps" do + @array.send(@method, eval("(0..).step(1)")).should == [0, 1, 2, 3, 4, 5] + @array.send(@method, eval("(0..).step(2)")).should == [0, 2, 4] + @array.send(@method, eval("(0..).step(10)")).should == [0] - @array.send(@method, eval("(2..).step(1)")).should == [2, 3, 4, 5] - @array.send(@method, eval("(2..).step(2)")).should == [2, 4] - @array.send(@method, eval("(2..).step(10)")).should == [2] + @array.send(@method, eval("(2..).step(1)")).should == [2, 3, 4, 5] + @array.send(@method, eval("(2..).step(2)")).should == [2, 4] + @array.send(@method, eval("(2..).step(10)")).should == [2] - @array.send(@method, eval("(-3..).step(1)")).should == [3, 4, 5] - @array.send(@method, eval("(-3..).step(2)")).should == [3, 5] - @array.send(@method, eval("(-3..).step(10)")).should == [3] - end + @array.send(@method, eval("(-3..).step(1)")).should == [3, 4, 5] + @array.send(@method, eval("(-3..).step(2)")).should == [3, 5] + @array.send(@method, eval("(-3..).step(10)")).should == [3] + end - it "has beginless range and positive steps" do - # end with zero index - @array.send(@method, (..0).step(1)).should == [0] - @array.send(@method, (...0).step(1)).should == [] + it "has beginless range and positive steps" do + # end with zero index + @array.send(@method, (..0).step(1)).should == [0] + @array.send(@method, (...0).step(1)).should == [] - @array.send(@method, (..0).step(2)).should == [0] - @array.send(@method, (...0).step(2)).should == [] + @array.send(@method, (..0).step(2)).should == [0] + @array.send(@method, (...0).step(2)).should == [] - @array.send(@method, (..0).step(10)).should == [0] - @array.send(@method, (...0).step(10)).should == [] + @array.send(@method, (..0).step(10)).should == [0] + @array.send(@method, (...0).step(10)).should == [] - # end with positive index - @array.send(@method, (..3).step(1)).should == [0, 1, 2, 3] - @array.send(@method, (...3).step(1)).should == [0, 1, 2] + # end with positive index + @array.send(@method, (..3).step(1)).should == [0, 1, 2, 3] + @array.send(@method, (...3).step(1)).should == [0, 1, 2] - @array.send(@method, (..3).step(2)).should == [0, 2] - @array.send(@method, (...3).step(2)).should == [0, 2] + @array.send(@method, (..3).step(2)).should == [0, 2] + @array.send(@method, (...3).step(2)).should == [0, 2] - @array.send(@method, (..3).step(10)).should == [0] - @array.send(@method, (...3).step(10)).should == [0] + @array.send(@method, (..3).step(10)).should == [0] + @array.send(@method, (...3).step(10)).should == [0] - # end with negative index - @array.send(@method, (..-2).step(1)).should == [0, 1, 2, 3, 4,] - @array.send(@method, (...-2).step(1)).should == [0, 1, 2, 3] + # end with negative index + @array.send(@method, (..-2).step(1)).should == [0, 1, 2, 3, 4,] + @array.send(@method, (...-2).step(1)).should == [0, 1, 2, 3] - @array.send(@method, (..-2).step(2)).should == [0, 2, 4] - @array.send(@method, (...-2).step(2)).should == [0, 2] + @array.send(@method, (..-2).step(2)).should == [0, 2, 4] + @array.send(@method, (...-2).step(2)).should == [0, 2] - @array.send(@method, (..-2).step(10)).should == [0] - @array.send(@method, (...-2).step(10)).should == [0] - end + @array.send(@method, (..-2).step(10)).should == [0] + @array.send(@method, (...-2).step(10)).should == [0] + end - it "has endless range and negative steps" do - @array.send(@method, eval("(0..).step(-1)")).should == [0] - @array.send(@method, eval("(0..).step(-2)")).should == [0] - @array.send(@method, eval("(0..).step(-10)")).should == [0] + it "has endless range and negative steps" do + @array.send(@method, eval("(0..).step(-1)")).should == [0] + @array.send(@method, eval("(0..).step(-2)")).should == [0] + @array.send(@method, eval("(0..).step(-10)")).should == [0] - @array.send(@method, eval("(2..).step(-1)")).should == [2, 1, 0] - @array.send(@method, eval("(2..).step(-2)")).should == [2, 0] + @array.send(@method, eval("(2..).step(-1)")).should == [2, 1, 0] + @array.send(@method, eval("(2..).step(-2)")).should == [2, 0] - @array.send(@method, eval("(-3..).step(-1)")).should == [3, 2, 1, 0] - @array.send(@method, eval("(-3..).step(-2)")).should == [3, 1] - end + @array.send(@method, eval("(-3..).step(-1)")).should == [3, 2, 1, 0] + @array.send(@method, eval("(-3..).step(-2)")).should == [3, 1] + end - it "has closed range and positive steps" do - # start and end with 0 - @array.send(@method, eval("(0..0).step(1)")).should == [0] - @array.send(@method, eval("(0...0).step(1)")).should == [] + it "has closed range and positive steps" do + # start and end with 0 + @array.send(@method, eval("(0..0).step(1)")).should == [0] + @array.send(@method, eval("(0...0).step(1)")).should == [] - @array.send(@method, eval("(0..0).step(2)")).should == [0] - @array.send(@method, eval("(0...0).step(2)")).should == [] + @array.send(@method, eval("(0..0).step(2)")).should == [0] + @array.send(@method, eval("(0...0).step(2)")).should == [] - @array.send(@method, eval("(0..0).step(10)")).should == [0] - @array.send(@method, eval("(0...0).step(10)")).should == [] + @array.send(@method, eval("(0..0).step(10)")).should == [0] + @array.send(@method, eval("(0...0).step(10)")).should == [] - # start and end with positive index - @array.send(@method, eval("(1..3).step(1)")).should == [1, 2, 3] - @array.send(@method, eval("(1...3).step(1)")).should == [1, 2] + # start and end with positive index + @array.send(@method, eval("(1..3).step(1)")).should == [1, 2, 3] + @array.send(@method, eval("(1...3).step(1)")).should == [1, 2] - @array.send(@method, eval("(1..3).step(2)")).should == [1, 3] - @array.send(@method, eval("(1...3).step(2)")).should == [1] + @array.send(@method, eval("(1..3).step(2)")).should == [1, 3] + @array.send(@method, eval("(1...3).step(2)")).should == [1] - @array.send(@method, eval("(1..3).step(10)")).should == [1] - @array.send(@method, eval("(1...3).step(10)")).should == [1] + @array.send(@method, eval("(1..3).step(10)")).should == [1] + @array.send(@method, eval("(1...3).step(10)")).should == [1] - # start with positive index, end with negative index - @array.send(@method, eval("(1..-2).step(1)")).should == [1, 2, 3, 4] - @array.send(@method, eval("(1...-2).step(1)")).should == [1, 2, 3] + # start with positive index, end with negative index + @array.send(@method, eval("(1..-2).step(1)")).should == [1, 2, 3, 4] + @array.send(@method, eval("(1...-2).step(1)")).should == [1, 2, 3] - @array.send(@method, eval("(1..-2).step(2)")).should == [1, 3] - @array.send(@method, eval("(1...-2).step(2)")).should == [1, 3] + @array.send(@method, eval("(1..-2).step(2)")).should == [1, 3] + @array.send(@method, eval("(1...-2).step(2)")).should == [1, 3] - @array.send(@method, eval("(1..-2).step(10)")).should == [1] - @array.send(@method, eval("(1...-2).step(10)")).should == [1] + @array.send(@method, eval("(1..-2).step(10)")).should == [1] + @array.send(@method, eval("(1...-2).step(10)")).should == [1] - # start with negative index, end with positive index - @array.send(@method, eval("(-4..4).step(1)")).should == [2, 3, 4] - @array.send(@method, eval("(-4...4).step(1)")).should == [2, 3] + # start with negative index, end with positive index + @array.send(@method, eval("(-4..4).step(1)")).should == [2, 3, 4] + @array.send(@method, eval("(-4...4).step(1)")).should == [2, 3] - @array.send(@method, eval("(-4..4).step(2)")).should == [2, 4] - @array.send(@method, eval("(-4...4).step(2)")).should == [2] + @array.send(@method, eval("(-4..4).step(2)")).should == [2, 4] + @array.send(@method, eval("(-4...4).step(2)")).should == [2] - @array.send(@method, eval("(-4..4).step(10)")).should == [2] - @array.send(@method, eval("(-4...4).step(10)")).should == [2] + @array.send(@method, eval("(-4..4).step(10)")).should == [2] + @array.send(@method, eval("(-4...4).step(10)")).should == [2] - # start with negative index, end with negative index - @array.send(@method, eval("(-4..-2).step(1)")).should == [2, 3, 4] - @array.send(@method, eval("(-4...-2).step(1)")).should == [2, 3] + # start with negative index, end with negative index + @array.send(@method, eval("(-4..-2).step(1)")).should == [2, 3, 4] + @array.send(@method, eval("(-4...-2).step(1)")).should == [2, 3] - @array.send(@method, eval("(-4..-2).step(2)")).should == [2, 4] - @array.send(@method, eval("(-4...-2).step(2)")).should == [2] + @array.send(@method, eval("(-4..-2).step(2)")).should == [2, 4] + @array.send(@method, eval("(-4...-2).step(2)")).should == [2] - @array.send(@method, eval("(-4..-2).step(10)")).should == [2] - @array.send(@method, eval("(-4...-2).step(10)")).should == [2] - end + @array.send(@method, eval("(-4..-2).step(10)")).should == [2] + @array.send(@method, eval("(-4...-2).step(10)")).should == [2] + end - it "has closed range and negative steps" do - # start and end with 0 - @array.send(@method, eval("(0..0).step(-1)")).should == [0] - @array.send(@method, eval("(0...0).step(-1)")).should == [] + it "has closed range and negative steps" do + # start and end with 0 + @array.send(@method, eval("(0..0).step(-1)")).should == [0] + @array.send(@method, eval("(0...0).step(-1)")).should == [] - @array.send(@method, eval("(0..0).step(-2)")).should == [0] - @array.send(@method, eval("(0...0).step(-2)")).should == [] + @array.send(@method, eval("(0..0).step(-2)")).should == [0] + @array.send(@method, eval("(0...0).step(-2)")).should == [] - @array.send(@method, eval("(0..0).step(-10)")).should == [0] - @array.send(@method, eval("(0...0).step(-10)")).should == [] + @array.send(@method, eval("(0..0).step(-10)")).should == [0] + @array.send(@method, eval("(0...0).step(-10)")).should == [] - # start and end with positive index - @array.send(@method, eval("(1..3).step(-1)")).should == [] - @array.send(@method, eval("(1...3).step(-1)")).should == [] + # start and end with positive index + @array.send(@method, eval("(1..3).step(-1)")).should == [] + @array.send(@method, eval("(1...3).step(-1)")).should == [] - @array.send(@method, eval("(1..3).step(-2)")).should == [] - @array.send(@method, eval("(1...3).step(-2)")).should == [] + @array.send(@method, eval("(1..3).step(-2)")).should == [] + @array.send(@method, eval("(1...3).step(-2)")).should == [] - @array.send(@method, eval("(1..3).step(-10)")).should == [] - @array.send(@method, eval("(1...3).step(-10)")).should == [] + @array.send(@method, eval("(1..3).step(-10)")).should == [] + @array.send(@method, eval("(1...3).step(-10)")).should == [] - # start with positive index, end with negative index - @array.send(@method, eval("(1..-2).step(-1)")).should == [] - @array.send(@method, eval("(1...-2).step(-1)")).should == [] + # start with positive index, end with negative index + @array.send(@method, eval("(1..-2).step(-1)")).should == [] + @array.send(@method, eval("(1...-2).step(-1)")).should == [] - @array.send(@method, eval("(1..-2).step(-2)")).should == [] - @array.send(@method, eval("(1...-2).step(-2)")).should == [] + @array.send(@method, eval("(1..-2).step(-2)")).should == [] + @array.send(@method, eval("(1...-2).step(-2)")).should == [] - @array.send(@method, eval("(1..-2).step(-10)")).should == [] - @array.send(@method, eval("(1...-2).step(-10)")).should == [] + @array.send(@method, eval("(1..-2).step(-10)")).should == [] + @array.send(@method, eval("(1...-2).step(-10)")).should == [] - # start with negative index, end with positive index - @array.send(@method, eval("(-4..4).step(-1)")).should == [] - @array.send(@method, eval("(-4...4).step(-1)")).should == [] + # start with negative index, end with positive index + @array.send(@method, eval("(-4..4).step(-1)")).should == [] + @array.send(@method, eval("(-4...4).step(-1)")).should == [] - @array.send(@method, eval("(-4..4).step(-2)")).should == [] - @array.send(@method, eval("(-4...4).step(-2)")).should == [] + @array.send(@method, eval("(-4..4).step(-2)")).should == [] + @array.send(@method, eval("(-4...4).step(-2)")).should == [] - @array.send(@method, eval("(-4..4).step(-10)")).should == [] - @array.send(@method, eval("(-4...4).step(-10)")).should == [] + @array.send(@method, eval("(-4..4).step(-10)")).should == [] + @array.send(@method, eval("(-4...4).step(-10)")).should == [] - # start with negative index, end with negative index - @array.send(@method, eval("(-4..-2).step(-1)")).should == [] - @array.send(@method, eval("(-4...-2).step(-1)")).should == [] + # start with negative index, end with negative index + @array.send(@method, eval("(-4..-2).step(-1)")).should == [] + @array.send(@method, eval("(-4...-2).step(-1)")).should == [] - @array.send(@method, eval("(-4..-2).step(-2)")).should == [] - @array.send(@method, eval("(-4...-2).step(-2)")).should == [] + @array.send(@method, eval("(-4..-2).step(-2)")).should == [] + @array.send(@method, eval("(-4...-2).step(-2)")).should == [] - @array.send(@method, eval("(-4..-2).step(-10)")).should == [] - @array.send(@method, eval("(-4...-2).step(-10)")).should == [] - end + @array.send(@method, eval("(-4..-2).step(-10)")).should == [] + @array.send(@method, eval("(-4...-2).step(-10)")).should == [] + end - it "has inverted closed range and positive steps" do - # start and end with positive index - @array.send(@method, eval("(3..1).step(1)")).should == [] - @array.send(@method, eval("(3...1).step(1)")).should == [] + it "has inverted closed range and positive steps" do + # start and end with positive index + @array.send(@method, eval("(3..1).step(1)")).should == [] + @array.send(@method, eval("(3...1).step(1)")).should == [] - @array.send(@method, eval("(3..1).step(2)")).should == [] - @array.send(@method, eval("(3...1).step(2)")).should == [] + @array.send(@method, eval("(3..1).step(2)")).should == [] + @array.send(@method, eval("(3...1).step(2)")).should == [] - @array.send(@method, eval("(3..1).step(10)")).should == [] - @array.send(@method, eval("(3...1).step(10)")).should == [] + @array.send(@method, eval("(3..1).step(10)")).should == [] + @array.send(@method, eval("(3...1).step(10)")).should == [] - # start with negative index, end with positive index - @array.send(@method, eval("(-2..1).step(1)")).should == [] - @array.send(@method, eval("(-2...1).step(1)")).should == [] + # start with negative index, end with positive index + @array.send(@method, eval("(-2..1).step(1)")).should == [] + @array.send(@method, eval("(-2...1).step(1)")).should == [] - @array.send(@method, eval("(-2..1).step(2)")).should == [] - @array.send(@method, eval("(-2...1).step(2)")).should == [] + @array.send(@method, eval("(-2..1).step(2)")).should == [] + @array.send(@method, eval("(-2...1).step(2)")).should == [] - @array.send(@method, eval("(-2..1).step(10)")).should == [] - @array.send(@method, eval("(-2...1).step(10)")).should == [] + @array.send(@method, eval("(-2..1).step(10)")).should == [] + @array.send(@method, eval("(-2...1).step(10)")).should == [] - # start with positive index, end with negative index - @array.send(@method, eval("(4..-4).step(1)")).should == [] - @array.send(@method, eval("(4...-4).step(1)")).should == [] + # start with positive index, end with negative index + @array.send(@method, eval("(4..-4).step(1)")).should == [] + @array.send(@method, eval("(4...-4).step(1)")).should == [] - @array.send(@method, eval("(4..-4).step(2)")).should == [] - @array.send(@method, eval("(4...-4).step(2)")).should == [] + @array.send(@method, eval("(4..-4).step(2)")).should == [] + @array.send(@method, eval("(4...-4).step(2)")).should == [] - @array.send(@method, eval("(4..-4).step(10)")).should == [] - @array.send(@method, eval("(4...-4).step(10)")).should == [] + @array.send(@method, eval("(4..-4).step(10)")).should == [] + @array.send(@method, eval("(4...-4).step(10)")).should == [] - # start with negative index, end with negative index - @array.send(@method, eval("(-2..-4).step(1)")).should == [] - @array.send(@method, eval("(-2...-4).step(1)")).should == [] + # start with negative index, end with negative index + @array.send(@method, eval("(-2..-4).step(1)")).should == [] + @array.send(@method, eval("(-2...-4).step(1)")).should == [] - @array.send(@method, eval("(-2..-4).step(2)")).should == [] - @array.send(@method, eval("(-2...-4).step(2)")).should == [] + @array.send(@method, eval("(-2..-4).step(2)")).should == [] + @array.send(@method, eval("(-2...-4).step(2)")).should == [] - @array.send(@method, eval("(-2..-4).step(10)")).should == [] - @array.send(@method, eval("(-2...-4).step(10)")).should == [] - end + @array.send(@method, eval("(-2..-4).step(10)")).should == [] + @array.send(@method, eval("(-2...-4).step(10)")).should == [] + end - it "has range with bounds outside of array" do - # end is equal to array's length - @array.send(@method, (0..6).step(1)).should == [0, 1, 2, 3, 4, 5] - -> { @array.send(@method, (0..6).step(2)) }.should raise_error(RangeError) + it "has range with bounds outside of array" do + # end is equal to array's length + @array.send(@method, (0..6).step(1)).should == [0, 1, 2, 3, 4, 5] + -> { @array.send(@method, (0..6).step(2)) }.should.raise(RangeError) - # end is greater than length with positive steps - @array.send(@method, (1..6).step(2)).should == [1, 3, 5] - @array.send(@method, (2..7).step(2)).should == [2, 4] - -> { @array.send(@method, (2..8).step(2)) }.should raise_error(RangeError) + # end is greater than length with positive steps + @array.send(@method, (1..6).step(2)).should == [1, 3, 5] + @array.send(@method, (2..7).step(2)).should == [2, 4] + -> { @array.send(@method, (2..8).step(2)) }.should.raise(RangeError) - # begin is greater than length with negative steps - @array.send(@method, (6..1).step(-2)).should == [5, 3, 1] - @array.send(@method, (7..2).step(-2)).should == [5, 3] - -> { @array.send(@method, (8..2).step(-2)) }.should raise_error(RangeError) - end + # begin is greater than length with negative steps + @array.send(@method, (6..1).step(-2)).should == [5, 3, 1] + @array.send(@method, (7..2).step(-2)).should == [5, 3] + -> { @array.send(@method, (8..2).step(-2)) }.should.raise(RangeError) + end - it "has endless range with start outside of array's bounds" do - @array.send(@method, eval("(6..).step(1)")).should == [] - @array.send(@method, eval("(7..).step(1)")).should == nil + it "has endless range with start outside of array's bounds" do + @array.send(@method, eval("(6..).step(1)")).should == [] + @array.send(@method, eval("(7..).step(1)")).should == nil - @array.send(@method, eval("(6..).step(2)")).should == [] - -> { @array.send(@method, eval("(7..).step(2)")) }.should raise_error(RangeError) - end + @array.send(@method, eval("(6..).step(2)")).should == [] + -> { @array.send(@method, eval("(7..).step(2)")) }.should.raise(RangeError) end end @@ -784,6 +754,100 @@ describe :array_slice, shared: true do a.send(@method, (...-9)).should == [] end + describe "can be sliced with Enumerator::ArithmeticSequence" do + it "with infinite/inverted ranges and negative steps" do + @array = [0, 1, 2, 3, 4, 5] + @array.send(@method, (2..).step(-1)).should == [2, 1, 0] + @array.send(@method, (2..).step(-2)).should == [2, 0] + @array.send(@method, (2..).step(-3)).should == [2] + @array.send(@method, (2..).step(-4)).should == [2] + + @array.send(@method, (-3..).step(-1)).should == [3, 2, 1, 0] + @array.send(@method, (-3..).step(-2)).should == [3, 1] + @array.send(@method, (-3..).step(-3)).should == [3, 0] + @array.send(@method, (-3..).step(-4)).should == [3] + @array.send(@method, (-3..).step(-5)).should == [3] + + @array.send(@method, (..0).step(-1)).should == [5, 4, 3, 2, 1, 0] + @array.send(@method, (..0).step(-2)).should == [5, 3, 1] + @array.send(@method, (..0).step(-3)).should == [5, 2] + @array.send(@method, (..0).step(-4)).should == [5, 1] + @array.send(@method, (..0).step(-5)).should == [5, 0] + @array.send(@method, (..0).step(-6)).should == [5] + @array.send(@method, (..0).step(-7)).should == [5] + + @array.send(@method, (...0).step(-1)).should == [5, 4, 3, 2, 1] + @array.send(@method, (...0).step(-2)).should == [5, 3, 1] + @array.send(@method, (...0).step(-3)).should == [5, 2] + @array.send(@method, (...0).step(-4)).should == [5, 1] + @array.send(@method, (...0).step(-5)).should == [5] + @array.send(@method, (...0).step(-6)).should == [5] + + @array.send(@method, (...1).step(-1)).should == [5, 4, 3, 2] + @array.send(@method, (...1).step(-2)).should == [5, 3] + @array.send(@method, (...1).step(-3)).should == [5, 2] + @array.send(@method, (...1).step(-4)).should == [5] + @array.send(@method, (...1).step(-5)).should == [5] + + @array.send(@method, (..-5).step(-1)).should == [5, 4, 3, 2, 1] + @array.send(@method, (..-5).step(-2)).should == [5, 3, 1] + @array.send(@method, (..-5).step(-3)).should == [5, 2] + @array.send(@method, (..-5).step(-4)).should == [5, 1] + @array.send(@method, (..-5).step(-5)).should == [5] + @array.send(@method, (..-5).step(-6)).should == [5] + + @array.send(@method, (...-5).step(-1)).should == [5, 4, 3, 2] + @array.send(@method, (...-5).step(-2)).should == [5, 3] + @array.send(@method, (...-5).step(-3)).should == [5, 2] + @array.send(@method, (...-5).step(-4)).should == [5] + @array.send(@method, (...-5).step(-5)).should == [5] + + @array.send(@method, (4..1).step(-1)).should == [4, 3, 2, 1] + @array.send(@method, (4..1).step(-2)).should == [4, 2] + @array.send(@method, (4..1).step(-3)).should == [4, 1] + @array.send(@method, (4..1).step(-4)).should == [4] + @array.send(@method, (4..1).step(-5)).should == [4] + + @array.send(@method, (4...1).step(-1)).should == [4, 3, 2] + @array.send(@method, (4...1).step(-2)).should == [4, 2] + @array.send(@method, (4...1).step(-3)).should == [4] + @array.send(@method, (4...1).step(-4)).should == [4] + + @array.send(@method, (-2..1).step(-1)).should == [4, 3, 2, 1] + @array.send(@method, (-2..1).step(-2)).should == [4, 2] + @array.send(@method, (-2..1).step(-3)).should == [4, 1] + @array.send(@method, (-2..1).step(-4)).should == [4] + @array.send(@method, (-2..1).step(-5)).should == [4] + + @array.send(@method, (-2...1).step(-1)).should == [4, 3, 2] + @array.send(@method, (-2...1).step(-2)).should == [4, 2] + @array.send(@method, (-2...1).step(-3)).should == [4] + @array.send(@method, (-2...1).step(-4)).should == [4] + + @array.send(@method, (4..-5).step(-1)).should == [4, 3, 2, 1] + @array.send(@method, (4..-5).step(-2)).should == [4, 2] + @array.send(@method, (4..-5).step(-3)).should == [4, 1] + @array.send(@method, (4..-5).step(-4)).should == [4] + @array.send(@method, (4..-5).step(-5)).should == [4] + + @array.send(@method, (4...-5).step(-1)).should == [4, 3, 2] + @array.send(@method, (4...-5).step(-2)).should == [4, 2] + @array.send(@method, (4...-5).step(-3)).should == [4] + @array.send(@method, (4...-5).step(-4)).should == [4] + + @array.send(@method, (-2..-5).step(-1)).should == [4, 3, 2, 1] + @array.send(@method, (-2..-5).step(-2)).should == [4, 2] + @array.send(@method, (-2..-5).step(-3)).should == [4, 1] + @array.send(@method, (-2..-5).step(-4)).should == [4] + @array.send(@method, (-2..-5).step(-5)).should == [4] + + @array.send(@method, (-2...-5).step(-1)).should == [4, 3, 2] + @array.send(@method, (-2...-5).step(-2)).should == [4, 2] + @array.send(@method, (-2...-5).step(-3)).should == [4] + @array.send(@method, (-2...-5).step(-4)).should == [4] + end + end + it "can accept nil...nil ranges" do a = [0, 1, 2, 3, 4, 5] a.send(@method, eval("(nil...nil)")).should == a diff --git a/spec/ruby/core/array/shared/union.rb b/spec/ruby/core/array/shared/union.rb index 0b60df9ca4..0b225b9a31 100644 --- a/spec/ruby/core/array/shared/union.rb +++ b/spec/ruby/core/array/shared/union.rb @@ -60,9 +60,9 @@ describe :array_binary_union, shared: true do end it "does not return subclass instances for Array subclasses" do - ArraySpecs::MyArray[1, 2, 3].send(@method, []).should be_an_instance_of(Array) - ArraySpecs::MyArray[1, 2, 3].send(@method, ArraySpecs::MyArray[1, 2, 3]).should be_an_instance_of(Array) - [].send(@method, ArraySpecs::MyArray[1, 2, 3]).should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].send(@method, []).should.instance_of?(Array) + ArraySpecs::MyArray[1, 2, 3].send(@method, ArraySpecs::MyArray[1, 2, 3]).should.instance_of?(Array) + [].send(@method, ArraySpecs::MyArray[1, 2, 3]).should.instance_of?(Array) end it "does not call to_ary on array subclasses" do diff --git a/spec/ruby/core/array/shared/unshift.rb b/spec/ruby/core/array/shared/unshift.rb index fc82e19e2a..b636347cd3 100644 --- a/spec/ruby/core/array/shared/unshift.rb +++ b/spec/ruby/core/array/shared/unshift.rb @@ -1,9 +1,9 @@ describe :array_unshift, shared: true do it "prepends object to the original array" do a = [1, 2, 3] - a.send(@method, "a").should equal(a) + a.send(@method, "a").should.equal?(a) a.should == ['a', 1, 2, 3] - a.send(@method).should equal(a) + a.send(@method).should.equal?(a) a.should == ['a', 1, 2, 3] a.send(@method, 5, 4, 3) a.should == [5, 4, 3, 'a', 1, 2, 3] @@ -22,6 +22,11 @@ describe :array_unshift, shared: true do a.should == [3, 4] end + it "returns self" do + a = [1, 2, 3] + a.send(@method, "a").should.equal?(a) + end + it "quietly ignores unshifting nothing" do [].send(@method).should == [] end @@ -36,11 +41,24 @@ describe :array_unshift, shared: true do end it "raises a FrozenError on a frozen array when the array is modified" do - -> { ArraySpecs.frozen_array.send(@method, 1) }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.send(@method, 1) }.should.raise(FrozenError) end # see [ruby-core:23666] it "raises a FrozenError on a frozen array when the array would not be modified" do - -> { ArraySpecs.frozen_array.send(@method) }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.send(@method) }.should.raise(FrozenError) + end + + # https://github.com/truffleruby/truffleruby/issues/2772 + it "doesn't rely on Array#[]= so it can be overridden" do + subclass = Class.new(Array) do + def []=(*) + raise "[]= is called" + end + end + + array = subclass.new + array.send(@method, 1) + array.should == [1] end end diff --git a/spec/ruby/core/array/shift_spec.rb b/spec/ruby/core/array/shift_spec.rb index 6b4ef39f77..09dfa79c45 100644 --- a/spec/ruby/core/array/shift_spec.rb +++ b/spec/ruby/core/array/shift_spec.rb @@ -31,10 +31,10 @@ describe "Array#shift" do end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array.shift }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.shift }.should.raise(FrozenError) end it "raises a FrozenError on an empty frozen array" do - -> { ArraySpecs.empty_frozen_array.shift }.should raise_error(FrozenError) + -> { ArraySpecs.empty_frozen_array.shift }.should.raise(FrozenError) end describe "passed a number n as an argument" do @@ -72,7 +72,7 @@ describe "Array#shift" do popped2.should == [] a.should == [] - popped1.should_not equal(popped2) + popped1.should_not.equal?(popped2) end it "returns whole elements if n exceeds size of the array" do @@ -83,14 +83,14 @@ describe "Array#shift" do it "does not return self even when it returns whole elements" do a = [1, 2, 3, 4, 5] - a.shift(5).should_not equal(a) + a.shift(5).should_not.equal?(a) a = [1, 2, 3, 4, 5] - a.shift(6).should_not equal(a) + a.shift(6).should_not.equal?(a) end it "raises an ArgumentError if n is negative" do - ->{ [1, 2, 3].shift(-1) }.should raise_error(ArgumentError) + ->{ [1, 2, 3].shift(-1) }.should.raise(ArgumentError) end it "tries to convert n to an Integer using #to_int" do @@ -105,16 +105,16 @@ describe "Array#shift" do end it "raises a TypeError when the passed n cannot be coerced to Integer" do - ->{ [1, 2].shift("cat") }.should raise_error(TypeError) - ->{ [1, 2].shift(nil) }.should raise_error(TypeError) + ->{ [1, 2].shift("cat") }.should.raise(TypeError) + ->{ [1, 2].shift(nil) }.should.raise(TypeError) end it "raises an ArgumentError if more arguments are passed" do - ->{ [1, 2].shift(1, 2) }.should raise_error(ArgumentError) + ->{ [1, 2].shift(1, 2) }.should.raise(ArgumentError) end it "does not return subclass instances with Array subclass" do - ArraySpecs::MyArray[1, 2, 3].shift(2).should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].shift(2).should.instance_of?(Array) end end end diff --git a/spec/ruby/core/array/shuffle_spec.rb b/spec/ruby/core/array/shuffle_spec.rb index b255147c75..9bc9df73ac 100644 --- a/spec/ruby/core/array/shuffle_spec.rb +++ b/spec/ruby/core/array/shuffle_spec.rb @@ -10,7 +10,7 @@ describe "Array#shuffle" do s.sort.should == a different ||= (a != s) end - different.should be_true # Will fail once in a blue moon (4!^10) + different.should == true # Will fail once in a blue moon (4!^10) end it "is not destructive" do @@ -22,7 +22,7 @@ describe "Array#shuffle" do end it "does not return subclass instances with Array subclass" do - ArraySpecs::MyArray[1, 2, 3].shuffle.should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].shuffle.should.instance_of?(Array) end it "calls #rand on the Object passed by the :random key in the arguments Hash" do @@ -31,20 +31,24 @@ describe "Array#shuffle" do result = [1, 2].shuffle(random: obj) result.size.should == 2 - result.should include(1, 2) + result.sort.should == [1, 2] end it "raises a NoMethodError if an object passed for the RNG does not define #rand" do obj = BasicObject.new - -> { [1, 2].shuffle(random: obj) }.should raise_error(NoMethodError) + -> { [1, 2].shuffle(random: obj) }.should.raise(NoMethodError) end it "accepts a Float for the value returned by #rand" do random = mock("array_shuffle_random") random.should_receive(:rand).at_least(1).times.and_return(0.3) - [1, 2].shuffle(random: random).should be_an_instance_of(Array) + [1, 2].shuffle(random: random).should.instance_of?(Array) + end + + it "accepts a Random class for the value for random: argument" do + [1, 2].shuffle(random: Random).should.instance_of?(Array) end it "calls #to_int on the Object returned by #rand" do @@ -53,7 +57,7 @@ describe "Array#shuffle" do random = mock("array_shuffle_random") random.should_receive(:rand).at_least(1).times.and_return(value) - [1, 2].shuffle(random: random).should be_an_instance_of(Array) + [1, 2].shuffle(random: random).should.instance_of?(Array) end it "raises a RangeError if the value is less than zero" do @@ -62,16 +66,25 @@ describe "Array#shuffle" do random = mock("array_shuffle_random") random.should_receive(:rand).and_return(value) - -> { [1, 2].shuffle(random: random) }.should raise_error(RangeError) + -> { [1, 2].shuffle(random: random) }.should.raise(RangeError) + end + + it "raises a RangeError if the value is equal to the Array size" do + value = mock("array_shuffle_random_value") + value.should_receive(:to_int).at_least(1).times.and_return(2) + random = mock("array_shuffle_random") + random.should_receive(:rand).at_least(1).times.and_return(value) + + -> { [1, 2].shuffle(random: random) }.should.raise(RangeError) end - it "raises a RangeError if the value is equal to one" do + it "raises a RangeError if the value is greater than the Array size" do value = mock("array_shuffle_random_value") - value.should_receive(:to_int).at_least(1).times.and_return(1) + value.should_receive(:to_int).at_least(1).times.and_return(3) random = mock("array_shuffle_random") random.should_receive(:rand).at_least(1).times.and_return(value) - -> { [1, 2].shuffle(random: random) }.should raise_error(RangeError) + -> { [1, 2].shuffle(random: random) }.should.raise(RangeError) end end @@ -85,12 +98,22 @@ describe "Array#shuffle!" do a.sort.should == [1, 2, 3, 4] different ||= (a != [1, 2, 3, 4]) end - different.should be_true # Will fail once in a blue moon (4!^10) - a.should equal(original) + different.should == true # Will fail once in a blue moon (4!^10) + a.should.equal?(original) end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array.shuffle! }.should raise_error(FrozenError) - -> { ArraySpecs.empty_frozen_array.shuffle! }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.shuffle! }.should.raise(FrozenError) + -> { ArraySpecs.empty_frozen_array.shuffle! }.should.raise(FrozenError) + end + + it "matches CRuby with random:" do + %w[a b c].shuffle(random: Random.new(1)).should == %w[a c b] + (0..10).to_a.shuffle(random: Random.new(10)).should == [2, 6, 8, 5, 7, 10, 3, 1, 0, 4, 9] + end + + it "matches CRuby with srand" do + srand(123) + %w[a b c d e f g h i j k].shuffle.should == %w[a e f h i j d b g k c] end end diff --git a/spec/ruby/core/array/slice_spec.rb b/spec/ruby/core/array/slice_spec.rb index 8e1499855a..eb7e47d947 100644 --- a/spec/ruby/core/array/slice_spec.rb +++ b/spec/ruby/core/array/slice_spec.rb @@ -116,8 +116,8 @@ describe "Array#slice!" do a.slice!(from .. to).should == [2, 3, 4] a.should == [1, 5] - -> { a.slice!("a" .. "b") }.should raise_error(TypeError) - -> { a.slice!(from .. "b") }.should raise_error(TypeError) + -> { a.slice!("a" .. "b") }.should.raise(TypeError) + -> { a.slice!(from .. "b") }.should.raise(TypeError) end it "returns last element for consecutive calls at zero index" do @@ -151,7 +151,7 @@ describe "Array#slice!" do end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array.slice!(0, 0) }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.slice!(0, 0) }.should.raise(FrozenError) end it "works with endless ranges" do @@ -187,56 +187,28 @@ describe "Array#slice!" do @array = ArraySpecs::MyArray[1, 2, 3, 4, 5] end - ruby_version_is ''...'3.0' do - it "returns a subclass instance with [n, m]" do - @array.slice!(0, 2).should be_an_instance_of(ArraySpecs::MyArray) - end - - it "returns a subclass instance with [-n, m]" do - @array.slice!(-3, 2).should be_an_instance_of(ArraySpecs::MyArray) - end - - it "returns a subclass instance with [n..m]" do - @array.slice!(1..3).should be_an_instance_of(ArraySpecs::MyArray) - end - - it "returns a subclass instance with [n...m]" do - @array.slice!(1...3).should be_an_instance_of(ArraySpecs::MyArray) - end - - it "returns a subclass instance with [-n..-m]" do - @array.slice!(-3..-1).should be_an_instance_of(ArraySpecs::MyArray) - end - - it "returns a subclass instance with [-n...-m]" do - @array.slice!(-3...-1).should be_an_instance_of(ArraySpecs::MyArray) - end + it "returns a Array instance with [n, m]" do + @array.slice!(0, 2).should.instance_of?(Array) end - ruby_version_is '3.0' do - it "returns a Array instance with [n, m]" do - @array.slice!(0, 2).should be_an_instance_of(Array) - end - - it "returns a Array instance with [-n, m]" do - @array.slice!(-3, 2).should be_an_instance_of(Array) - end + it "returns a Array instance with [-n, m]" do + @array.slice!(-3, 2).should.instance_of?(Array) + end - it "returns a Array instance with [n..m]" do - @array.slice!(1..3).should be_an_instance_of(Array) - end + it "returns a Array instance with [n..m]" do + @array.slice!(1..3).should.instance_of?(Array) + end - it "returns a Array instance with [n...m]" do - @array.slice!(1...3).should be_an_instance_of(Array) - end + it "returns a Array instance with [n...m]" do + @array.slice!(1...3).should.instance_of?(Array) + end - it "returns a Array instance with [-n..-m]" do - @array.slice!(-3..-1).should be_an_instance_of(Array) - end + it "returns a Array instance with [-n..-m]" do + @array.slice!(-3..-1).should.instance_of?(Array) + end - it "returns a Array instance with [-n...-m]" do - @array.slice!(-3...-1).should be_an_instance_of(Array) - end + it "returns a Array instance with [-n...-m]" do + @array.slice!(-3...-1).should.instance_of?(Array) end end end diff --git a/spec/ruby/core/array/sort_by_spec.rb b/spec/ruby/core/array/sort_by_spec.rb index 7cea6ec6d3..132abb028a 100644 --- a/spec/ruby/core/array/sort_by_spec.rb +++ b/spec/ruby/core/array/sort_by_spec.rb @@ -1,5 +1,6 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative 'shared/iterable_and_tolerating_size_increasing' require_relative '../enumerable/shared/enumeratorized' describe "Array#sort_by!" do @@ -10,25 +11,30 @@ describe "Array#sort_by!" do end it "returns an Enumerator if not given a block" do - (1..10).to_a.sort_by!.should be_an_instance_of(Enumerator) + (1..10).to_a.sort_by!.should.instance_of?(Enumerator) end it "completes when supplied a block that always returns the same result" do a = [2, 3, 5, 1, 4] a.sort_by!{ 1 } - a.should be_an_instance_of(Array) + a.should.instance_of?(Array) a.sort_by!{ 0 } - a.should be_an_instance_of(Array) + a.should.instance_of?(Array) a.sort_by!{ -1 } - a.should be_an_instance_of(Array) + a.should.instance_of?(Array) end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array.sort_by! {}}.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.sort_by! {}}.should.raise(FrozenError) end it "raises a FrozenError on an empty frozen array" do - -> { ArraySpecs.empty_frozen_array.sort_by! {}}.should raise_error(FrozenError) + -> { ArraySpecs.empty_frozen_array.sort_by! {}}.should.raise(FrozenError) + end + + it "raises a FrozenError on a frozen array only during iteration if called without a block" do + enum = ArraySpecs.frozen_array.sort_by! + -> { enum.each {} }.should.raise(FrozenError) end it "returns the specified value when it would break in the given block" do @@ -41,12 +47,39 @@ describe "Array#sort_by!" do ary.sort_by!{|x,y| break if x==i; x<=>y} ary } - partially_sorted.any?{|ary| ary != [1, 2, 3, 4, 5]}.should be_true + partially_sorted.any?{|ary| ary != [1, 2, 3, 4, 5]}.should == true end it "changes nothing when called on a single element array" do [1].sort_by!(&:to_s).should == [1] end + it "does not truncate the array is the block raises an exception" do + a = [1, 2, 3] + begin + a.sort_by! { raise StandardError, 'Oops' } + rescue + end + + a.should == [1, 2, 3] + end + + it "doesn't change array if error is raised" do + a = [4, 3, 2, 1] + begin + a.sort_by! do |e| + raise StandardError, 'Oops' if e == 1 + e + end + rescue StandardError + end + + a.should == [4, 3, 2, 1] + end + it_behaves_like :enumeratorized_with_origin_size, :sort_by!, [1,2,3] end + +describe "Array#sort_by!" do + it_behaves_like :array_iterable_and_tolerating_size_increasing, :sort_by! +end diff --git a/spec/ruby/core/array/sort_spec.rb b/spec/ruby/core/array/sort_spec.rb index e20b650516..27300c3385 100644 --- a/spec/ruby/core/array/sort_spec.rb +++ b/spec/ruby/core/array/sort_spec.rb @@ -42,7 +42,7 @@ describe "Array#sort" do a = [1, 2, 3] sorted = a.sort sorted.should == a - sorted.should_not equal(a) + sorted.should_not.equal?(a) end it "properly handles recursive arrays" do @@ -68,7 +68,7 @@ describe "Array#sort" do -> { [o, 1].sort - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "may take a block which is used to determine the order of objects a and b described as -1, 0 or +1" do @@ -78,28 +78,28 @@ describe "Array#sort" do end it "raises an error when a given block returns nil" do - -> { [1, 2].sort {} }.should raise_error(ArgumentError) + -> { [1, 2].sort {} }.should.raise(ArgumentError) end it "does not call #<=> on contained objects when invoked with a block" do a = Array.new(25) (0...25).each {|i| a[i] = ArraySpecs::UFOSceptic.new } - a.sort { -1 }.should be_an_instance_of(Array) + a.sort { -1 }.should.instance_of?(Array) end it "does not call #<=> on elements when invoked with a block even if Array is large (Rubinius #412)" do a = Array.new(1500) (0...1500).each {|i| a[i] = ArraySpecs::UFOSceptic.new } - a.sort { -1 }.should be_an_instance_of(Array) + a.sort { -1 }.should.instance_of?(Array) end it "completes when supplied a block that always returns the same result" do a = [2, 3, 5, 1, 4] - a.sort { 1 }.should be_an_instance_of(Array) - a.sort { 0 }.should be_an_instance_of(Array) - a.sort { -1 }.should be_an_instance_of(Array) + a.sort { 1 }.should.instance_of?(Array) + a.sort { 0 }.should.instance_of?(Array) + a.sort { -1 }.should.instance_of?(Array) end it "does not freezes self during being sorted" do @@ -136,7 +136,7 @@ describe "Array#sort" do }.should == [-4, 1, 2, 5, 7, 10, 12] -> { a.sort { |n, m| (n - m).to_s } - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "sorts an array that has a value shifted off without a block" do @@ -155,7 +155,7 @@ describe "Array#sort" do it "raises an error if objects can't be compared" do a=[ArraySpecs::Uncomparable.new, ArraySpecs::Uncomparable.new] - -> {a.sort}.should raise_error(ArgumentError) + -> {a.sort}.should.raise(ArgumentError) end # From a strange Rubinius bug @@ -166,7 +166,7 @@ describe "Array#sort" do it "does not return subclass instance on Array subclasses" do ary = ArraySpecs::MyArray[1, 2, 3] - ary.sort.should be_an_instance_of(Array) + ary.sort.should.instance_of?(Array) end end @@ -184,13 +184,13 @@ describe "Array#sort!" do it "returns self if the order of elements changed" do a = [6, 7, 2, 3, 7] - a.sort!.should equal(a) + a.sort!.should.equal?(a) a.should == [2, 3, 6, 7, 7] end it "returns self even if makes no modification" do a = [1, 2, 3, 4, 5] - a.sort!.should equal(a) + a.sort!.should.equal?(a) a.should == [1, 2, 3, 4, 5] end @@ -216,25 +216,25 @@ describe "Array#sort!" do a = Array.new(25) (0...25).each {|i| a[i] = ArraySpecs::UFOSceptic.new } - a.sort! { -1 }.should be_an_instance_of(Array) + a.sort! { -1 }.should.instance_of?(Array) end it "does not call #<=> on elements when invoked with a block even if Array is large (Rubinius #412)" do a = Array.new(1500) (0...1500).each {|i| a[i] = ArraySpecs::UFOSceptic.new } - a.sort! { -1 }.should be_an_instance_of(Array) + a.sort! { -1 }.should.instance_of?(Array) end it "completes when supplied a block that always returns the same result" do a = [2, 3, 5, 1, 4] - a.sort!{ 1 }.should be_an_instance_of(Array) - a.sort!{ 0 }.should be_an_instance_of(Array) - a.sort!{ -1 }.should be_an_instance_of(Array) + a.sort!{ 1 }.should.instance_of?(Array) + a.sort!{ 0 }.should.instance_of?(Array) + a.sort!{ -1 }.should.instance_of?(Array) end it "raises a FrozenError on a frozen array" do - -> { ArraySpecs.frozen_array.sort! }.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.sort! }.should.raise(FrozenError) end it "returns the specified value when it would break in the given block" do @@ -247,6 +247,6 @@ describe "Array#sort!" do ary.sort!{|x,y| break if x==i; x<=>y} ary } - partially_sorted.any?{|ary| ary != [1, 2, 3, 4, 5]}.should be_true + partially_sorted.any?{|ary| ary != [1, 2, 3, 4, 5]}.should == true end end diff --git a/spec/ruby/core/array/sum_spec.rb b/spec/ruby/core/array/sum_spec.rb index 8ca8353a67..cd4ba4c2d8 100644 --- a/spec/ruby/core/array/sum_spec.rb +++ b/spec/ruby/core/array/sum_spec.rb @@ -1,4 +1,5 @@ require_relative '../../spec_helper' +require_relative 'shared/iterable_and_tolerating_size_increasing' describe "Array#sum" do it "returns the sum of elements" do @@ -9,8 +10,12 @@ describe "Array#sum" do [1, 2, 3].sum { |i| i * 10 }.should == 60 end + it "doesn't apply the block init" do + [1, 2, 3].sum(1) { |i| i * 10 }.should == 61 + end + # https://bugs.ruby-lang.org/issues/12217 - # https://github.com/ruby/ruby/blob/master/doc/ChangeLog-2.4.0#L6208-L6214 + # 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] naive_sum = floats.reduce { |sum, e| sum + e } @@ -55,11 +60,11 @@ describe "Array#sum" do end it 'raises TypeError if any element are not numeric' do - -> { ["a"].sum }.should raise_error(TypeError) + -> { ["a"].sum }.should.raise(TypeError) end it 'raises TypeError if any element cannot be added to init value' do - -> { [1].sum([]) }.should raise_error(TypeError) + -> { [1].sum([]) }.should.raise(TypeError) end it "calls + to sum the elements" do @@ -68,4 +73,16 @@ describe "Array#sum" do a.should_receive(:+).with(b).and_return(42) [b].sum(a).should == 42 end + + it "calls + on the init value" do + a = mock("a") + b = mock("b") + a.should_receive(:+).with(42).and_return(b) + [42].sum(a).should == b + end +end + +describe "Array#sum" do + @value_to_return = -> _ { 1 } + it_behaves_like :array_iterable_and_tolerating_size_increasing, :sum end diff --git a/spec/ruby/core/array/take_spec.rb b/spec/ruby/core/array/take_spec.rb index 4fb6f0ce75..837c734b77 100644 --- a/spec/ruby/core/array/take_spec.rb +++ b/spec/ruby/core/array/take_spec.rb @@ -23,18 +23,10 @@ describe "Array#take" do end it "raises an ArgumentError when the argument is negative" do - ->{ [1].take(-3) }.should raise_error(ArgumentError) + ->{ [1].take(-3) }.should.raise(ArgumentError) end - ruby_version_is ''...'3.0' do - it 'returns a subclass instance for Array subclasses' do - ArraySpecs::MyArray[1, 2, 3, 4, 5].take(1).should be_an_instance_of(ArraySpecs::MyArray) - end - end - - ruby_version_is '3.0' do - it 'returns a Array instance for Array subclasses' do - ArraySpecs::MyArray[1, 2, 3, 4, 5].take(1).should be_an_instance_of(Array) - end + it 'returns a Array instance for Array subclasses' do + ArraySpecs::MyArray[1, 2, 3, 4, 5].take(1).should.instance_of?(Array) end end diff --git a/spec/ruby/core/array/take_while_spec.rb b/spec/ruby/core/array/take_while_spec.rb index 363419b265..7811edab9e 100644 --- a/spec/ruby/core/array/take_while_spec.rb +++ b/spec/ruby/core/array/take_while_spec.rb @@ -1,5 +1,6 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative 'shared/iterable_and_tolerating_size_increasing' describe "Array#take_while" do it "returns all elements until the block returns false" do @@ -14,15 +15,12 @@ describe "Array#take_while" do [1, 2, false, 4].take_while{ |element| element }.should == [1, 2] end - ruby_version_is ''...'3.0' do - it 'returns a subclass instance for Array subclasses' do - ArraySpecs::MyArray[1, 2, 3, 4, 5].take_while { |n| n < 4 }.should be_an_instance_of(ArraySpecs::MyArray) - end + it 'returns a Array instance for Array subclasses' do + ArraySpecs::MyArray[1, 2, 3, 4, 5].take_while { |n| n < 4 }.should.instance_of?(Array) end +end - ruby_version_is '3.0' do - it 'returns a Array instance for Array subclasses' do - ArraySpecs::MyArray[1, 2, 3, 4, 5].take_while { |n| n < 4 }.should be_an_instance_of(Array) - end - end +describe "Array#take_while" do + @value_to_return = -> _ { true } + it_behaves_like :array_iterable_and_tolerating_size_increasing, :take_while end diff --git a/spec/ruby/core/array/to_a_spec.rb b/spec/ruby/core/array/to_a_spec.rb index 49d0a4782e..078de1638a 100644 --- a/spec/ruby/core/array/to_a_spec.rb +++ b/spec/ruby/core/array/to_a_spec.rb @@ -5,12 +5,12 @@ describe "Array#to_a" do it "returns self" do a = [1, 2, 3] a.to_a.should == [1, 2, 3] - a.should equal(a.to_a) + a.should.equal?(a.to_a) end it "does not return subclass instance on Array subclasses" do e = ArraySpecs::MyArray.new(1, 2) - e.to_a.should be_an_instance_of(Array) + e.to_a.should.instance_of?(Array) e.to_a.should == [1, 2] end diff --git a/spec/ruby/core/array/to_ary_spec.rb b/spec/ruby/core/array/to_ary_spec.rb index 314699b709..dc5193158d 100644 --- a/spec/ruby/core/array/to_ary_spec.rb +++ b/spec/ruby/core/array/to_ary_spec.rb @@ -4,9 +4,9 @@ require_relative 'fixtures/classes' describe "Array#to_ary" do it "returns self" do a = [1, 2, 3] - a.should equal(a.to_ary) + a.should.equal?(a.to_ary) a = ArraySpecs::MyArray[1, 2, 3] - a.should equal(a.to_ary) + a.should.equal?(a.to_ary) end it "properly handles recursive arrays" do diff --git a/spec/ruby/core/array/to_h_spec.rb b/spec/ruby/core/array/to_h_spec.rb index f5a7e546e6..1d626763c2 100644 --- a/spec/ruby/core/array/to_h_spec.rb +++ b/spec/ruby/core/array/to_h_spec.rb @@ -1,5 +1,6 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative 'shared/iterable_and_tolerating_size_increasing' describe "Array#to_h" do it "converts empty array to empty hash" do @@ -24,19 +25,19 @@ describe "Array#to_h" do end it "raises TypeError if an element is not an array" do - -> { [:x].to_h }.should raise_error(TypeError) + -> { [:x].to_h }.should.raise(TypeError) end it "raises ArgumentError if an element is not a [key, value] pair" do - -> { [[:x]].to_h }.should raise_error(ArgumentError) + -> { [[:x]].to_h }.should.raise(ArgumentError) end it "does not accept arguments" do - -> { [].to_h(:a, :b) }.should raise_error(ArgumentError) + -> { [].to_h(:a, :b) }.should.raise(ArgumentError) end it "produces a hash that returns nil for a missing element" do - [[:a, 1], [:b, 2]].to_h[:c].should be_nil + [[:a, 1], [:b, 2]].to_h[:c].should == nil end context "with block" do @@ -44,20 +45,26 @@ describe "Array#to_h" do [:a, :b].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 + ScratchPad.record [] + [[:a, 1], [:b, 2]].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 [:a, :b].to_h { |k| [k, k.to_s, 1] } - end.should raise_error(ArgumentError, /wrong array length at 0/) + end.should.raise(ArgumentError, /wrong array length at 0/) -> do [:a, :b].to_h { |k| [k] } - end.should raise_error(ArgumentError, /wrong array length at 0/) + end.should.raise(ArgumentError, /wrong array length at 0/) end it "raises TypeError if block returns something other than Array" do -> do [:a, :b].to_h { |k| "not-array" } - end.should raise_error(TypeError, /wrong element type String at 0/) + end.should.raise(TypeError, /wrong element type String at 0/) end it "coerces returned pair to Array with #to_ary" do @@ -73,7 +80,12 @@ describe "Array#to_h" do -> do [:a].to_h { |k| x } - end.should raise_error(TypeError, /wrong element type MockObject at 0/) + end.should.raise(TypeError, /wrong element type MockObject at 0/) end end end + +describe "Array#to_h" do + @value_to_return = -> e { [e, e.to_s] } + it_behaves_like :array_iterable_and_tolerating_size_increasing, :to_h +end diff --git a/spec/ruby/core/array/transpose_spec.rb b/spec/ruby/core/array/transpose_spec.rb index b39077f4c9..d45e9c351c 100644 --- a/spec/ruby/core/array/transpose_spec.rb +++ b/spec/ruby/core/array/transpose_spec.rb @@ -32,7 +32,7 @@ describe "Array#transpose" do end it "raises a TypeError if the passed Argument does not respond to #to_ary" do - -> { [Object.new, [:a, :b]].transpose }.should raise_error(TypeError) + -> { [Object.new, [:a, :b]].transpose }.should.raise(TypeError) end it "does not call to_ary on array subclass elements" do @@ -41,13 +41,13 @@ describe "Array#transpose" do end it "raises an IndexError if the arrays are not of the same length" do - -> { [[1, 2], [:a]].transpose }.should raise_error(IndexError) + -> { [[1, 2], [:a]].transpose }.should.raise(IndexError) end it "does not return subclass instance on Array subclasses" do result = ArraySpecs::MyArray[ArraySpecs::MyArray[1, 2, 3], ArraySpecs::MyArray[4, 5, 6]].transpose - result.should be_an_instance_of(Array) - result[0].should be_an_instance_of(Array) - result[1].should be_an_instance_of(Array) + result.should.instance_of?(Array) + result[0].should.instance_of?(Array) + result[1].should.instance_of?(Array) end end diff --git a/spec/ruby/core/array/try_convert_spec.rb b/spec/ruby/core/array/try_convert_spec.rb index 47b4722d80..3eaa0f4b7c 100644 --- a/spec/ruby/core/array/try_convert_spec.rb +++ b/spec/ruby/core/array/try_convert_spec.rb @@ -4,47 +4,47 @@ require_relative 'fixtures/classes' describe "Array.try_convert" do it "returns the argument if it's an Array" do x = Array.new - Array.try_convert(x).should equal(x) + Array.try_convert(x).should.equal?(x) end it "returns the argument if it's a kind of Array" do x = ArraySpecs::MyArray[] - Array.try_convert(x).should equal(x) + Array.try_convert(x).should.equal?(x) end it "returns nil when the argument does not respond to #to_ary" do - Array.try_convert(Object.new).should be_nil + Array.try_convert(Object.new).should == nil end it "sends #to_ary to the argument and returns the result if it's nil" do obj = mock("to_ary") obj.should_receive(:to_ary).and_return(nil) - Array.try_convert(obj).should be_nil + Array.try_convert(obj).should == nil end it "sends #to_ary to the argument and returns the result if it's an Array" do x = Array.new obj = mock("to_ary") obj.should_receive(:to_ary).and_return(x) - Array.try_convert(obj).should equal(x) + Array.try_convert(obj).should.equal?(x) end it "sends #to_ary to the argument and returns the result if it's a kind of Array" do x = ArraySpecs::MyArray[] obj = mock("to_ary") obj.should_receive(:to_ary).and_return(x) - Array.try_convert(obj).should equal(x) + Array.try_convert(obj).should.equal?(x) end it "sends #to_ary to the argument and raises TypeError if it's not a kind of Array" do obj = mock("to_ary") obj.should_receive(:to_ary).and_return(Object.new) - -> { Array.try_convert obj }.should raise_error(TypeError) + -> { Array.try_convert obj }.should raise_consistent_error(TypeError, "can't convert MockObject into Array (MockObject#to_ary gives Object)") end it "does not rescue exceptions raised by #to_ary" do obj = mock("to_ary") obj.should_receive(:to_ary).and_raise(RuntimeError) - -> { Array.try_convert obj }.should raise_error(RuntimeError) + -> { Array.try_convert obj }.should.raise(RuntimeError) end end diff --git a/spec/ruby/core/array/union_spec.rb b/spec/ruby/core/array/union_spec.rb index ba2cc0d6b7..110894e83d 100644 --- a/spec/ruby/core/array/union_spec.rb +++ b/spec/ruby/core/array/union_spec.rb @@ -15,7 +15,7 @@ describe "Array#union" do end it "does not return subclass instances for Array subclasses" do - ArraySpecs::MyArray[1, 2, 3].union.should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].union.should.instance_of?(Array) end it "accepts multiple arguments" do diff --git a/spec/ruby/core/array/uniq_spec.rb b/spec/ruby/core/array/uniq_spec.rb index 4461cae16b..0289bee7c2 100644 --- a/spec/ruby/core/array/uniq_spec.rb +++ b/spec/ruby/core/array/uniq_spec.rb @@ -1,5 +1,6 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative 'shared/iterable_and_tolerating_size_increasing' describe "Array#uniq" do it "returns an array with no duplicates" do @@ -84,16 +85,8 @@ describe "Array#uniq" do [false, nil, 42].uniq { :bar }.should == [false] end - ruby_version_is ''...'3.0' do - it "returns subclass instance on Array subclasses" do - ArraySpecs::MyArray[1, 2, 3].uniq.should be_an_instance_of(ArraySpecs::MyArray) - end - end - - ruby_version_is '3.0' do - it "returns Array instance on Array subclasses" do - ArraySpecs::MyArray[1, 2, 3].uniq.should be_an_instance_of(Array) - end + it "returns Array instance on Array subclasses" do + ArraySpecs::MyArray[1, 2, 3].uniq.should.instance_of?(Array) end it "properly handles an identical item even when its #eql? isn't reflexive" do @@ -131,6 +124,11 @@ describe "Array#uniq" do end end +describe "Array#uniq" do + @value_to_return = -> e { e } + it_behaves_like :array_iterable_and_tolerating_size_increasing, :uniq +end + describe "Array#uniq!" do it "modifies the array in place" do a = [ "a", "a", "b", "b", "c" ] @@ -140,7 +138,7 @@ describe "Array#uniq!" do it "returns self" do a = [ "a", "a", "b", "b", "c" ] - a.should equal(a.uniq!) + a.should.equal?(a.uniq!) end it "properly handles recursive arrays" do @@ -187,17 +185,17 @@ describe "Array#uniq!" do it "raises a FrozenError on a frozen array when the array is modified" do dup_ary = [1, 1, 2] dup_ary.freeze - -> { dup_ary.uniq! }.should raise_error(FrozenError) + -> { dup_ary.uniq! }.should.raise(FrozenError) end # see [ruby-core:23666] it "raises a FrozenError on a frozen array when the array would not be modified" do - -> { ArraySpecs.frozen_array.uniq!}.should raise_error(FrozenError) - -> { ArraySpecs.empty_frozen_array.uniq!}.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.uniq!}.should.raise(FrozenError) + -> { ArraySpecs.empty_frozen_array.uniq!}.should.raise(FrozenError) end it "doesn't yield to the block on a frozen array" do - -> { ArraySpecs.frozen_array.uniq!{ raise RangeError, "shouldn't yield"}}.should raise_error(FrozenError) + -> { ArraySpecs.frozen_array.uniq!{ raise RangeError, "shouldn't yield"}}.should.raise(FrozenError) end it "compares elements based on the value returned from the block" do @@ -214,4 +212,32 @@ describe "Array#uniq!" do a.uniq! a.should == [x] end + + it "does not truncate the array is the block raises an exception" do + a = [1, 2, 3] + begin + a.send(@method) { raise StandardError, 'Oops' } + rescue + end + + a.should == [1, 2, 3] + end + + it "doesn't change array if error is raised" do + a = [1, 1, 2, 2, 3, 3, 4, 4] + begin + a.send(@method) do |e| + raise StandardError, 'Oops' if e == 3 + e + end + rescue StandardError + end + + a.should == [1, 1, 2, 2, 3, 3, 4, 4] + end +end + +describe "Array#uniq!" do + @value_to_return = -> e { e } + it_behaves_like :array_iterable_and_tolerating_size_increasing, :uniq! end diff --git a/spec/ruby/core/array/values_at_spec.rb b/spec/ruby/core/array/values_at_spec.rb index 2c6fd16947..e11e7e4451 100644 --- a/spec/ruby/core/array/values_at_spec.rb +++ b/spec/ruby/core/array/values_at_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +# Should be synchronized with core/struct/values_at_spec.rb describe "Array#values_at" do it "returns an array of elements at the indexes when passed indexes" do [1, 2, 3, 4, 5].values_at().should == [] @@ -58,7 +59,7 @@ describe "Array#values_at" do end it "does not return subclass instance on Array subclasses" do - ArraySpecs::MyArray[1, 2, 3].values_at(0, 1..2, 1).should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].values_at(0, 1..2, 1).should.instance_of?(Array) end it "works when given endless ranges" do diff --git a/spec/ruby/core/array/zip_spec.rb b/spec/ruby/core/array/zip_spec.rb index af4013debe..3ccdf143d6 100644 --- a/spec/ruby/core/array/zip_spec.rb +++ b/spec/ruby/core/array/zip_spec.rb @@ -60,6 +60,12 @@ describe "Array#zip" do end it "does not return subclass instance on Array subclasses" do - ArraySpecs::MyArray[1, 2, 3].zip(["a", "b"]).should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].zip(["a", "b"]).should.instance_of?(Array) + end + + it "raises TypeError when some argument isn't Array and doesn't respond to #to_ary and #to_enum" do + -> { [1, 2, 3].zip(Object.new) }.should.raise(TypeError, "wrong argument type Object (must respond to :each)") + -> { [1, 2, 3].zip(1) }.should.raise(TypeError, "wrong argument type Integer (must respond to :each)") + -> { [1, 2, 3].zip(true) }.should.raise(TypeError, "wrong argument type TrueClass (must respond to :each)") end end |
