diff options
Diffstat (limited to 'spec/ruby/core/array/flatten_spec.rb')
| -rw-r--r-- | spec/ruby/core/array/flatten_spec.rb | 76 |
1 files changed, 28 insertions, 48 deletions
diff --git a/spec/ruby/core/array/flatten_spec.rb b/spec/ruby/core/array/flatten_spec.rb index 3b20e976b6..8c97000c79 100644 --- a/spec/ruby/core/array/flatten_spec.rb +++ b/spec/ruby/core/array/flatten_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/classes', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe "Array#flatten" do it "returns a one-dimensional flattening recursively" 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") - lambda { [ 1, 2, [3, [4, 5] ] ].flatten(obj) }.should raise_error(TypeError) + -> { [ 1, 2, [3, [4, 5] ] ].flatten(obj) }.should raise_error(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 - lambda { x.flatten }.should raise_error(ArgumentError) + -> { x.flatten }.should raise_error(ArgumentError) x = [] y = [] x << y y << x - lambda { x.flatten }.should raise_error(ArgumentError) + -> { x.flatten }.should raise_error(ArgumentError) end it "flattens any element which responds to #to_ary, using the return value of said method" do @@ -69,19 +69,17 @@ describe "Array#flatten" do [1, z, 6].flatten.should == [1, 2, 3, 4, 5, 6] end - ruby_version_is "2.3" do - it "does not call #to_ary on elements beyond the given level" do - obj = mock("1") - obj.should_not_receive(:to_ary) - [[obj]].flatten(1) - end + it "does not call #to_ary on elements beyond the given level" do + obj = mock("1") + obj.should_not_receive(:to_ary) + [[obj]].flatten(1) end - 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] + 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 @@ -108,23 +106,13 @@ 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) - lambda { [@obj].flatten }.should raise_error(TypeError) - end - - ruby_version_is ""..."2.5" do - it "calls respond_to_missing?(:to_ary, false) to try coercing" do - def @obj.respond_to_missing?(*args) ScratchPad << args; false end - [@obj].flatten.should == [@obj] - ScratchPad.recorded.should == [[:to_ary, false]] - end + -> { [@obj].flatten }.should raise_error(TypeError) end - ruby_version_is "2.5" do - it "calls respond_to_missing?(:to_ary, true) to try coercing" do - def @obj.respond_to_missing?(*args) ScratchPad << args; false end - [@obj].flatten.should == [@obj] - ScratchPad.recorded.should == [[:to_ary, true]] - end + it "calls respond_to_missing?(:to_ary, true) to try coercing" do + def @obj.respond_to_missing?(*args) ScratchPad << args; false end + [@obj].flatten.should == [@obj] + ScratchPad.recorded.should == [[:to_ary, true]] end it "does not call #to_ary if not defined when #respond_to_missing? returns false" 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 - lambda { [@obj].flatten }.should raise_error(NoMethodError) + -> { [@obj].flatten }.should raise_error(NoMethodError) ScratchPad.recorded.should == [:to_ary] end @@ -147,14 +135,6 @@ describe "Array#flatten" do end end - it "returns a tainted array if self is tainted" do - [].taint.flatten.tainted?.should be_true - end - - it "returns an untrusted array if self is untrusted" do - [].untrust.flatten.untrusted?.should be_true - end - it "performs respond_to? and method_missing-aware checks when coercing elements to array" do bo = BasicObject.new [bo].flatten.should == [bo] @@ -228,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") - lambda { [ 1, 2, [3, [4, 5] ] ].flatten!(obj) }.should raise_error(TypeError) + -> { [ 1, 2, [3, [4, 5] ] ].flatten!(obj) }.should raise_error(TypeError) end it "does not call flatten! on elements" do @@ -244,13 +224,13 @@ describe "Array#flatten!" do it "raises an ArgumentError on recursive arrays" do x = [] x << x - lambda { x.flatten! }.should raise_error(ArgumentError) + -> { x.flatten! }.should raise_error(ArgumentError) x = [] y = [] x << y y << x - lambda { x.flatten! }.should raise_error(ArgumentError) + -> { x.flatten! }.should raise_error(ArgumentError) end it "flattens any elements which responds to #to_ary, using the return value of said method" do @@ -272,15 +252,15 @@ describe "Array#flatten!" do ary.should == [1, 2, 3] end - it "raises a RuntimeError on frozen arrays when the array is modified" do + it "raises a FrozenError on frozen arrays when the array is modified" do nested_ary = [1, 2, []] nested_ary.freeze - lambda { nested_ary.flatten! }.should raise_error(RuntimeError) + -> { nested_ary.flatten! }.should raise_error(FrozenError) end # see [ruby-core:23663] - it "raises a RuntimeError on frozen arrays when the array would not be modified" do - lambda { ArraySpecs.frozen_array.flatten! }.should raise_error(RuntimeError) - lambda { ArraySpecs.empty_frozen_array.flatten! }.should raise_error(RuntimeError) + 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) end end |
