Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More Array specs for not calling #initialize on subclasses.

  • Loading branch information...
commit 463efb9cff06cd17bd1bf75787d4beb8eb555c63 1 parent b99057d
Brian Shirai brixen authored
18 core/array/element_reference_spec.rb
View
@@ -36,9 +36,19 @@
EOS
end
end
-
- it "returns an instance of the subtype when called on an Array subclass" do
- ArraySub = Class.new Array
- ArraySub[1,2].should be_kind_of(ArraySub)
+
+ describe "with a subclass of Array" do
+ before :each do
+ ScratchPad.clear
+ end
+
+ it "returns an instance of the subclass" do
+ ArraySpecs::MyArray[1, 2, 3].should be_an_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
+ end
end
end
10 core/array/fixtures/classes.rb
View
@@ -44,7 +44,15 @@ def self.empty_recursive_array
a
end
- class MyArray < Array; end
+ class MyArray < Array
+ # The #initialize method has a different signature than Array to help
+ # catch places in the specs that do not assert the #initialize is not
+ # called when Array methods make new instances.
+ def initialize(a, b)
+ self << a << b
+ ScratchPad.record :my_array_initialize
+ end
+ end
class Sexp < Array
def initialize(*args)
2  core/array/join_spec.rb
View
@@ -1,6 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
-require File.dirname(__FILE__) + '/../../shared/array/join'
+require File.expand_path('../shared/join', __FILE__)
describe "Array#join" do
25 core/array/multiply_spec.rb
View
@@ -1,6 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
-require File.dirname(__FILE__) + '/../../shared/array/join'
+require File.expand_path('../shared/join', __FILE__)
describe "Array#*" do
it "tries to convert the passed argument to a String using #to_str" do
@@ -68,11 +68,24 @@ def obj.to_str() "2" end
lambda { [ 1, 2, 3 ] * -1 }.should raise_error(ArgumentError)
lambda { [] * -1 }.should raise_error(ArgumentError)
end
-
- it "returns subclass instance with Array subclasses" do
- (ArraySpecs::MyArray[1, 2, 3] * 0).should be_kind_of(ArraySpecs::MyArray)
- (ArraySpecs::MyArray[1, 2, 3] * 1).should be_kind_of(ArraySpecs::MyArray)
- (ArraySpecs::MyArray[1, 2, 3] * 2).should be_kind_of(ArraySpecs::MyArray)
+
+ describe "with a subclass of Array" do
+ before :each do
+ ScratchPad.clear
+
+ @array = ArraySpecs::MyArray[1, 2, 3, 4, 5]
+ end
+
+ 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
+
+ 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
+ end
end
ruby_version_is '' ... '1.8' do
2  core/array/new_spec.rb
View
@@ -7,7 +7,7 @@
end
it "returns an instance of a subclass" do
- ArraySpecs::MyArray.new.should be_kind_of(ArraySpecs::MyArray)
+ ArraySpecs::MyArray.new(1, 2).should be_an_instance_of(ArraySpecs::MyArray)
end
it "raise an ArgumentError if passed 3 or more arguments" do
0  shared/array/join.rb → core/array/shared/join.rb
View
File renamed without changes
73 core/array/shared/slice.rb
View
@@ -343,7 +343,7 @@ def to.to_int() -2 end
[1, 2, 3, 4, 5].send(@method, 0...-1).should == [1, 2, 3, 4]
end
- it "returns [3] for [2..-1] out of [1, 2, 3] <Specifies bug found by brixen, Defiler, mae>" do
+ it "returns [3] for [2..-1] out of [1, 2, 3]" do
[1,2,3].send(@method, 2..-1).should == [3]
end
@@ -369,11 +369,72 @@ def to.to_int() -2 end
a.should == [1, 2]
end
- it "returns a subclass instance when called on a subclass of Array" do
- ary = ArraySpecs::MyArray[1, 2, 3]
- ary.send(@method, 0, 0).should be_kind_of(ArraySpecs::MyArray)
- ary.send(@method, 0, 2).should be_kind_of(ArraySpecs::MyArray)
- ary.send(@method, 0..10).should be_kind_of(ArraySpecs::MyArray)
+ describe "with a subclass of Array" do
+ before :each do
+ ScratchPad.clear
+
+ @array = ArraySpecs::MyArray[1, 2, 3, 4, 5]
+ end
+
+ 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 an empty array when m == n with [m...n]" do
+ @array.send(@method, 1...1).should == []
+ ScratchPad.recorded.should be_nil
+ end
+
+ it "returns an empty array with [0...0]" do
+ @array.send(@method, 0...0).should == []
+ ScratchPad.recorded.should be_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
+ 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
+ end
+
+ it "returns [] if index == array.size with [index, length]" do
+ @array.send(@method, 5, 2).should == []
+ ScratchPad.recorded.should be_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
+ 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
+ end
end
not_compliant_on :rubinius do
5 core/array/to_a_spec.rb
View
@@ -9,10 +9,9 @@
end
it "does not return subclass instance on Array subclasses" do
- e = ArraySpecs::MyArray.new
- e << 1
+ e = ArraySpecs::MyArray.new(1, 2)
e.to_a.should be_kind_of(Array)
- e.to_a.should == [1]
+ e.to_a.should == [1, 2]
end
it "properly handles recursive arrays" do
Please sign in to comment.
Something went wrong with that request. Please try again.