summaryrefslogtreecommitdiff
path: root/spec/ruby/core/array/shared/collect.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/array/shared/collect.rb')
-rw-r--r--spec/ruby/core/array/shared/collect.rb89
1 files changed, 47 insertions, 42 deletions
diff --git a/spec/ruby/core/array/shared/collect.rb b/spec/ruby/core/array/shared/collect.rb
index 80e2739893..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,45 +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)
- end
-
- it "does not copy tainted status" do
- a = [1, 2, 3]
- a.taint
- a.send(@method){|x| x}.tainted?.should be_false
- end
-
- it "does not copy untrusted status" do
- a = [1, 2, 3]
- a.untrust
- a.send(@method){|x| x}.untrusted?.should be_false
+ }.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
@@ -89,48 +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
- it "keeps tainted status" do
- a = [1, 2, 3]
- a.taint
- a.tainted?.should be_true
- a.send(@method){|x| x}
- a.tainted?.should be_true
- end
-
- it "keeps untrusted status" do
- a = [1, 2, 3]
- a.untrust
- a.send(@method){|x| x}
- a.untrusted?.should be_true
- end
-
describe "when frozen" do
- it "raises a #{frozen_error_class}" do
- -> { ArraySpecs.frozen_array.send(@method) {} }.should raise_error(frozen_error_class)
+ it "raises a FrozenError" do
+ -> { ArraySpecs.frozen_array.send(@method) {} }.should.raise(FrozenError)
end
- it "raises a #{frozen_error_class} when empty" do
- -> { ArraySpecs.empty_frozen_array.send(@method) {} }.should raise_error(frozen_error_class)
+ it "raises a FrozenError when empty" do
+ -> { ArraySpecs.empty_frozen_array.send(@method) {} }.should.raise(FrozenError)
end
- it "raises a #{frozen_error_class} when calling #each on the returned Enumerator" do
+ 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(frozen_error_class)
+ -> { enumerator.each {|x| x } }.should.raise(FrozenError)
end
- it "raises a #{frozen_error_class} when calling #each on the returned Enumerator when empty" do
+ 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(frozen_error_class)
+ -> { 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