Skip to content

Commit

Permalink
Update to ruby/spec@e7dc804
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Feb 27, 2023
1 parent de60139 commit 18b4def
Show file tree
Hide file tree
Showing 230 changed files with 1,887 additions and 187 deletions.
13 changes: 13 additions & 0 deletions spec/ruby/core/array/all_spec.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions spec/ruby/core/array/any_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,6 +20,9 @@
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
Expand All @@ -34,4 +38,12 @@
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
11 changes: 11 additions & 0 deletions spec/ruby/core/array/count_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,4 +13,14 @@
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
30 changes: 30 additions & 0 deletions spec/ruby/core/array/delete_if_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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
Expand Down Expand Up @@ -47,6 +48,35 @@
-> { ArraySpecs.empty_frozen_array.delete_if {} }.should raise_error(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
4 changes: 4 additions & 0 deletions spec/ruby/core/array/drop_while_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 16 additions & 0 deletions spec/ruby/core/array/each_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,19 @@
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
5 changes: 5 additions & 0 deletions spec/ruby/core/array/each_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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.
Expand Down Expand Up @@ -75,3 +76,7 @@
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
42 changes: 42 additions & 0 deletions spec/ruby/core/array/fill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,48 @@
-> { [].fill(1, 2) {|i|} }.should_not raise_error(ArgumentError)
-> { [].fill(1, 2, true) {|i|} }.should raise_error(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

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

ScratchPad.recorded.should == [0, 1, 2]
end
end

describe "Array#fill with (filler, index, length)" do
Expand Down
4 changes: 3 additions & 1 deletion spec/ruby/core/array/initialize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
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_error
}.should complain(/#{__FILE__}:#{__LINE__-1}: warning: given block not used/, verbose: true)
end
end

Expand Down
53 changes: 51 additions & 2 deletions spec/ruby/core/array/intersect_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,66 @@
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
[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
[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
end
4 changes: 3 additions & 1 deletion spec/ruby/core/array/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
end

it "does not use the given block" do
->{ Array.new { raise } }.should_not raise_error
-> {
-> { Array.new { raise } }.should_not raise_error
}.should complain(/warning: given block not used/, verbose: true)
end
end

Expand Down
13 changes: 13 additions & 0 deletions spec/ruby/core/array/none_spec.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions spec/ruby/core/array/one_spec.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions spec/ruby/core/array/reject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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
Expand Down Expand Up @@ -47,6 +48,10 @@
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]
Expand Down Expand Up @@ -111,6 +116,11 @@
-> { ArraySpecs.empty_frozen_array.reject! {} }.should raise_error(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_error(FrozenError)
end

it "does not truncate the array is the block raises an exception" do
a = [1, 2, 3]
begin
Expand Down Expand Up @@ -141,3 +151,8 @@
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
14 changes: 14 additions & 0 deletions spec/ruby/core/array/reverse_each_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@
[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
Loading

0 comments on commit 18b4def

Please sign in to comment.