Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Ask not what RubySpec can do for you, but what you can do for RubySpec.
- Loading branch information
Brian Ford
committed
May 9, 2008
0 parents
commit 6c1c34a
Showing
2,216 changed files
with
61,634 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.rbc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
|
||
describe "Array.allocate" do | ||
it "returns an instance of Array" do | ||
ary = Array.allocate | ||
ary.should be_kind_of(Array) | ||
end | ||
|
||
it "returns a fully-formed instance of Array" do | ||
ary = Array.allocate | ||
ary.size.should == 0 | ||
ary << 1 | ||
ary.should == [1] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
require File.dirname(__FILE__) + '/fixtures/classes' | ||
|
||
describe "Array#<<" do | ||
it "pushes the object onto the end of the array" do | ||
([ 1, 2 ] << "c" << "d" << [ 3, 4 ]).should == [1, 2, "c", "d", [3, 4]] | ||
end | ||
|
||
it "returns self to allow chaining" do | ||
a = [] | ||
b = a | ||
(a << 1).equal?(b).should == true | ||
(a << 2 << 3).equal?(b).should == true | ||
end | ||
|
||
it "correctly resizes the Array" do | ||
a = [] | ||
a.size.should == 0 | ||
a << :foo | ||
a.size.should == 1 | ||
a << :bar << :baz | ||
a.size.should == 3 | ||
|
||
a = [1, 2, 3] | ||
a.shift | ||
a.shift | ||
a.shift | ||
a << :foo | ||
a.should == [:foo] | ||
end | ||
|
||
compliant_on :ruby, :jruby do | ||
it "raises a TypeError on a frozen array" do | ||
lambda { ArraySpecs.frozen_array << 5 }.should raise_error(TypeError) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
|
||
describe "Array" do | ||
it "includes Enumerable" do | ||
Array.ancestors.include?(Enumerable).should == true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
require File.dirname(__FILE__) + '/fixtures/classes' | ||
|
||
describe "Array#assoc" do | ||
it "returns the first array whose 1st item is == obj or nil" do | ||
s1 = ["colors", "red", "blue", "green"] | ||
s2 = [:letters, "a", "b", "c"] | ||
s3 = [4] | ||
s4 = [nil, nil] | ||
a = [s1, s2, s3, s4] | ||
a.assoc(s1.first).should == s1 | ||
a.assoc(s2.first).should == s2 | ||
a.assoc(s3.first).should == s3 | ||
a.assoc(s4.first).should == s4 | ||
a.assoc(4).should == [4] | ||
a.assoc("key not in array").should == nil | ||
end | ||
|
||
it "calls == on first element of each array" do | ||
key1 = 'it' | ||
key2 = mock('key2') | ||
items = [['not it', 1], [ArraySpecs::AssocKey.new, 2], ['na', 3]] | ||
|
||
items.assoc(key1).should == items[1] | ||
items.assoc(key2).should == nil | ||
end | ||
|
||
it "ignores any non-Array elements" do | ||
[1, 2, 3].assoc(2).should == nil | ||
s1 = [4] | ||
s2 = [5, 4, 3] | ||
a = ["foo", [], s1, s2, nil, []] | ||
a.assoc(s1.first).should == s1 | ||
a.assoc(s2.first).should == s2 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
require File.dirname(__FILE__) + '/fixtures/classes' | ||
|
||
describe "Array#at" do | ||
it "returns the element at index" do | ||
a = [1, 2, 3, 4, 5, 6] | ||
a.at(0).should == 1 | ||
a.at(-2).should == 5 | ||
a.at(10).should == nil | ||
a.at(-7).should == nil | ||
end | ||
|
||
it "calls to_int on its argument" do | ||
a = ["a", "b", "c"] | ||
a.at(0.5).should == "a" | ||
|
||
obj = mock('to_int') | ||
obj.should_receive(:to_int).and_return(2) | ||
a.at(obj).should == "c" | ||
|
||
obj = mock('method_missing to_int') | ||
obj.should_receive(:respond_to?).with(:to_int).any_number_of_times.and_return(true) | ||
obj.should_receive(:method_missing).with(:to_int).and_return(2) | ||
a.at(obj).should == "c" | ||
end | ||
|
||
it "raises TypeError if its argument can't be coerced" do | ||
a = ["a", "b", "c"] | ||
|
||
lambda { a.at("cat") }.should raise_error(TypeError) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
require File.dirname(__FILE__) + '/fixtures/classes' | ||
|
||
describe "Array#clear" do | ||
it "removes all elements" do | ||
a = [1, 2, 3, 4] | ||
a.clear.equal?(a).should == true | ||
a.should == [] | ||
end | ||
|
||
it "returns self" do | ||
a = [1] | ||
oid = a.object_id | ||
a.clear.object_id.should == oid | ||
end | ||
|
||
it "leaves the Array empty" do | ||
a = [1] | ||
a.clear | ||
a.empty?.should == true | ||
a.size.should == 0 | ||
end | ||
|
||
compliant_on :ruby, :jruby do | ||
it "raises a TypeError on a frozen array" do | ||
a = [1] | ||
a.freeze | ||
lambda { a.clear }.should raise_error(TypeError) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
require File.dirname(__FILE__) + '/fixtures/classes' | ||
require File.dirname(__FILE__) + '/shared/clone' | ||
|
||
describe "Array#clone" do | ||
it_behaves_like :array_clone, :clone | ||
|
||
not_compliant_on :rubinius do | ||
it "copies frozen status from the original" do | ||
a = [1, 2, 3, 4] | ||
b = [1, 2, 3, 4] | ||
a.freeze | ||
aa = a.clone | ||
bb = b.clone | ||
|
||
aa.frozen?.should == true | ||
bb.frozen?.should == false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
require File.dirname(__FILE__) + '/fixtures/classes' | ||
require File.dirname(__FILE__) + '/shared/collect' | ||
|
||
describe "Array#collect" do | ||
it_behaves_like(:array_collect, :collect) | ||
end | ||
|
||
describe "Array#collect!" do | ||
it_behaves_like(:array_collect_b, :collect!) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
require File.dirname(__FILE__) + '/fixtures/classes' | ||
|
||
describe "Array#compact" do | ||
it "returns a copy of array with all nil elements removed" do | ||
a = [1, 2, 4] | ||
a.compact.should == [1, 2, 4] | ||
a = [1, nil, 2, 4] | ||
a.compact.should == [1, 2, 4] | ||
a = [1, 2, 4, nil] | ||
a.compact.should == [1, 2, 4] | ||
a = [nil, 1, 2, 4] | ||
a.compact.should == [1, 2, 4] | ||
end | ||
|
||
it "returns subclass instance for Array subclasses" do | ||
ArraySpecs::MyArray[1, 2, 3, nil].compact.class.should == ArraySpecs::MyArray | ||
end | ||
end | ||
|
||
describe "Array#compact!" do | ||
it "removes all nil elements" do | ||
a = ['a', nil, 'b', false, 'c'] | ||
a.compact!.equal?(a).should == true | ||
a.should == ["a", "b", false, "c"] | ||
a = [nil, 'a', 'b', false, 'c'] | ||
a.compact!.equal?(a).should == true | ||
a.should == ["a", "b", false, "c"] | ||
a = ['a', 'b', false, 'c', nil] | ||
a.compact!.equal?(a).should == true | ||
a.should == ["a", "b", false, "c"] | ||
end | ||
|
||
it "returns nil if there are no nil elements to remove" do | ||
[1, 2, false, 3].compact!.should == nil | ||
end | ||
|
||
compliant_on :ruby, :jruby do | ||
it "raises a TypeError on a frozen array" do | ||
lambda { ArraySpecs.frozen_array.compact! }.should raise_error(TypeError) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
require File.dirname(__FILE__) + '/fixtures/classes' | ||
|
||
describe "Array#<=>" do | ||
it "calls <=> left to right and return first non-0 result" do | ||
[-1, +1, nil, "foobar"].each do |result| | ||
lhs = Array.new(3) { mock("#{result}") } | ||
rhs = Array.new(3) { mock("#{result}") } | ||
|
||
lhs[0].should_receive(:<=>).with(rhs[0]).and_return(0) | ||
lhs[1].should_receive(:<=>).with(rhs[1]).and_return(result) | ||
lhs[2].should_not_receive(:<=>) | ||
|
||
(lhs <=> rhs).should == result | ||
end | ||
end | ||
|
||
it "returns 0 if the arrays are equal" do | ||
([] <=> []).should == 0 | ||
([1, 2, 3, 4, 5, 6] <=> [1, 2, 3, 4, 5.0, 6.0]).should == 0 | ||
end | ||
|
||
it "returns -1 if the array is shorter than the other array" do | ||
([] <=> [1]).should == -1 | ||
([1, 1] <=> [1, 1, 1]).should == -1 | ||
end | ||
|
||
it "returns +1 if the array is longer than the other array" do | ||
([1] <=> []).should == +1 | ||
([1, 1, 1] <=> [1, 1]).should == +1 | ||
end | ||
|
||
it "calls to_ary on its argument" do | ||
obj = mock('to_ary') | ||
def obj.to_ary() [1, 2, 3] end | ||
([4, 5] <=> obj).should == ([4, 5] <=> obj.to_ary) | ||
|
||
obj = mock('method_missing to_ary') | ||
obj.should_receive(:respond_to?).with(:to_ary).any_number_of_times.and_return(true) | ||
obj.should_receive(:method_missing).with(:to_ary).and_return([4, 5]) | ||
([4, 5] <=> obj).should == 0 | ||
end | ||
|
||
it "does not call to_ary on array subclasses" do | ||
([5, 6, 7] <=> ArraySpecs::ToAryArray[5, 6, 7]).should == 0 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
require File.dirname(__FILE__) + '/fixtures/classes' | ||
|
||
describe "Array#concat" do | ||
it "appends the elements in the other array" do | ||
ary = [1, 2, 3] | ||
ary.concat([9, 10, 11]).equal?(ary).should == true | ||
ary.should == [1, 2, 3, 9, 10, 11] | ||
ary.concat([]) | ||
ary.should == [1, 2, 3, 9, 10, 11] | ||
end | ||
|
||
it "does not loop endlessly when argument is self" do | ||
ary = ["x", "y"] | ||
ary.concat(ary).should == ["x", "y", "x", "y"] | ||
end | ||
|
||
it "calls to_ary on its argument" do | ||
obj = mock('to_ary') | ||
def obj.to_ary() ["x", "y"] end | ||
[4, 5, 6].concat(obj).should == [4, 5, 6, "x", "y"] | ||
|
||
obj = mock('method_missing to_ary') | ||
obj.should_receive(:respond_to?).with(:to_ary).any_number_of_times.and_return(true) | ||
obj.should_receive(:method_missing).with(:to_ary).and_return([:x]) | ||
[].concat(obj).should == [:x] | ||
end | ||
|
||
it "does not call to_ary on array subclasses" do | ||
[].concat(ArraySpecs::ToAryArray[5, 6, 7]).should == [5, 6, 7] | ||
end | ||
|
||
compliant_on :ruby, :jruby do | ||
it "raises a TypeError when Array is frozen and modification occurs" do | ||
lambda { ArraySpecs.frozen_array.concat [1] }.should raise_error(TypeError) | ||
end | ||
|
||
it "does not raise a TypeError when Array is frozen but no modification occurs" do | ||
ArraySpecs.frozen_array.concat [] # ok, no modification | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
require File.dirname(__FILE__) + '/fixtures/classes' | ||
|
||
describe "Array.[]" do | ||
it "returns a new array populated with the given elements" do | ||
Array.[](5, true, nil, 'a', "Ruby").should == [5, true, nil, "a", "Ruby"] | ||
|
||
a = ArraySpecs::MyArray.[](5, true, nil, 'a', "Ruby") | ||
a.class.should == ArraySpecs::MyArray | ||
a.inspect.should == [5, true, nil, "a", "Ruby"].inspect | ||
end | ||
end | ||
|
||
describe "Array[]" do | ||
it "is a synonym for .[]" do | ||
Array[5, true, nil, 'a', "Ruby"].should == Array.[](5, true, nil, "a", "Ruby") | ||
|
||
a = ArraySpecs::MyArray[5, true, nil, 'a', "Ruby"] | ||
a.class.should == ArraySpecs::MyArray | ||
a.inspect.should == [5, true, nil, "a", "Ruby"].inspect | ||
end | ||
end |
Oops, something went wrong.