Skip to content

Commit 7bd0f12

Browse files
committed
Add specs for modifying Array methods for a case when error is raised
1 parent e11a36d commit 7bd0f12

File tree

6 files changed

+152
-0
lines changed

6 files changed

+152
-0
lines changed

spec/ruby/core/array/delete_if_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,32 @@
4848
-> { ArraySpecs.empty_frozen_array.delete_if {} }.should raise_error(FrozenError)
4949
end
5050

51+
it "does not truncate the array is the block raises an exception" do
52+
a = [1, 2, 3]
53+
begin
54+
a.delete_if { raise StandardError, 'Oops' }
55+
rescue
56+
end
57+
58+
a.should == [1, 2, 3]
59+
end
60+
61+
it "only removes elements for which the block returns true, keeping the element which raised an error." do
62+
a = [1, 2, 3, 4]
63+
begin
64+
a.delete_if do |e|
65+
case e
66+
when 2 then true
67+
when 3 then raise StandardError, 'Oops'
68+
else false
69+
end
70+
end
71+
rescue StandardError
72+
end
73+
74+
a.should == [1, 3, 4]
75+
end
76+
5177
it_behaves_like :enumeratorized_with_origin_size, :delete_if, [1,2,3]
5278
it_behaves_like :delete_if, :delete_if
5379

spec/ruby/core/array/fill_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,33 @@
7373
-> { [].fill(1, 2, true) {|i|} }.should raise_error(ArgumentError)
7474
end
7575

76+
it "does not truncate the array is the block raises an exception" do
77+
a = [1, 2, 3]
78+
begin
79+
a.fill { raise StandardError, 'Oops' }
80+
rescue
81+
end
82+
83+
a.should == [1, 2, 3]
84+
end
85+
86+
it "only changes elements before error is raised, keeping the element which raised an error." do
87+
a = [1, 2, 3, 4]
88+
begin
89+
a.fill do |i|
90+
case i
91+
when 0 then -1
92+
when 1 then -2
93+
when 2 then raise StandardError, 'Oops'
94+
else 0
95+
end
96+
end
97+
rescue StandardError
98+
end
99+
100+
a.should == [-1, -2, 3, 4]
101+
end
102+
76103
it "tolerates increasing an array size during iteration" do
77104
array = [:a, :b, :c]
78105
ScratchPad.record []

spec/ruby/core/array/shared/collect.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,33 @@
105105
end
106106
end
107107

108+
it "does not truncate the array is the block raises an exception" do
109+
a = [1, 2, 3]
110+
begin
111+
a.send(@method) { raise StandardError, 'Oops' }
112+
rescue
113+
end
114+
115+
a.should == [1, 2, 3]
116+
end
117+
118+
it "only changes elements before error is raised, keeping the element which raised an error." do
119+
a = [1, 2, 3, 4]
120+
begin
121+
a.send(@method) do |e|
122+
case e
123+
when 1 then -1
124+
when 2 then -2
125+
when 3 then raise StandardError, 'Oops'
126+
else 0
127+
end
128+
end
129+
rescue StandardError
130+
end
131+
132+
a.should == [-1, -2, 3, 4]
133+
end
134+
108135
before :all do
109136
@object = [1, 2, 3, 4]
110137
end

spec/ruby/core/array/shared/keep_if.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,32 @@
5959
end
6060
end
6161

62+
it "does not truncate the array is the block raises an exception" do
63+
a = [1, 2, 3]
64+
begin
65+
a.send(@method) { raise StandardError, 'Oops' }
66+
rescue
67+
end
68+
69+
a.should == [1, 2, 3]
70+
end
71+
72+
it "only changes elements before error is raised, keeping the element which raised an error." do
73+
a = [1, 2, 3, 4]
74+
begin
75+
a.send(@method) do |e|
76+
case e
77+
when 2 then false
78+
when 3 then raise StandardError, 'Oops'
79+
else true
80+
end
81+
end
82+
rescue StandardError
83+
end
84+
85+
a.should == [1, 3, 4]
86+
end
87+
6288
@value_to_return = -> (_) { true }
6389
it_should_behave_like :array_iterable_and_tolerating_size_increasing
6490
end

spec/ruby/core/array/sort_by_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,29 @@
4949
[1].sort_by!(&:to_s).should == [1]
5050
end
5151

52+
it "does not truncate the array is the block raises an exception" do
53+
a = [1, 2, 3]
54+
begin
55+
a.sort_by! { raise StandardError, 'Oops' }
56+
rescue
57+
end
58+
59+
a.should == [1, 2, 3]
60+
end
61+
62+
it "doesn't change array if error is raised" do
63+
a = [4, 3, 2, 1]
64+
begin
65+
a.sort_by! do |e|
66+
raise StandardError, 'Oops' if e == 1
67+
e
68+
end
69+
rescue StandardError
70+
end
71+
72+
a.should == [4, 3, 2, 1]
73+
end
74+
5275
it_behaves_like :enumeratorized_with_origin_size, :sort_by!, [1,2,3]
5376
end
5477

spec/ruby/core/array/uniq_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,29 @@ def hash
220220
a.uniq!
221221
a.should == [x]
222222
end
223+
224+
it "does not truncate the array is the block raises an exception" do
225+
a = [1, 2, 3]
226+
begin
227+
a.send(@method) { raise StandardError, 'Oops' }
228+
rescue
229+
end
230+
231+
a.should == [1, 2, 3]
232+
end
233+
234+
it "doesn't change array if error is raised" do
235+
a = [1, 1, 2, 2, 3, 3, 4, 4]
236+
begin
237+
a.send(@method) do |e|
238+
raise StandardError, 'Oops' if e == 3
239+
e
240+
end
241+
rescue StandardError
242+
end
243+
244+
a.should == [1, 1, 2, 2, 3, 3, 4, 4]
245+
end
223246
end
224247

225248
describe "Array#uniq!" do

0 commit comments

Comments
 (0)