diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
commit | 1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch) | |
tree | a3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/core/array/at_spec.rb | |
parent | 75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff) | |
download | ruby-1d15d5f08032acf1b7bceacbb450d617ff6e0931.tar.gz |
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory.
[Misc #13792] [ruby-core:82287]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/core/array/at_spec.rb')
-rw-r--r-- | spec/ruby/core/array/at_spec.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/spec/ruby/core/array/at_spec.rb b/spec/ruby/core/array/at_spec.rb new file mode 100644 index 0000000000..e40c26f2cc --- /dev/null +++ b/spec/ruby/core/array/at_spec.rb @@ -0,0 +1,56 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Array#at" do + it "returns the (n+1)'th element for the passed index n" do + a = [1, 2, 3, 4, 5, 6] + a.at(0).should == 1 + a.at(1).should == 2 + a.at(5).should == 6 + end + + it "returns nil if the given index is greater than or equal to the array's length" do + a = [1, 2, 3, 4, 5, 6] + a.at(6).should == nil + a.at(7).should == nil + end + + it "returns the (-n)'th elemet from the last, for the given negative index n" do + a = [1, 2, 3, 4, 5, 6] + a.at(-1).should == 6 + a.at(-2).should == 5 + a.at(-6).should == 1 + end + + it "returns nil if the given index is less than -len, where len is length of the array" do + a = [1, 2, 3, 4, 5, 6] + a.at(-7).should == nil + a.at(-8).should == nil + end + + it "does not extend the array unless the given index is out of range" do + a = [1, 2, 3, 4, 5, 6] + a.length.should == 6 + a.at(100) + a.length.should == 6 + a.at(-100) + a.length.should == 6 + end + + it "tries to convert the passed argument to an Integer using #to_int" 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" + end + + it "raises a TypeError when the passed argument can't be coerced to Integer" do + lambda { [].at("cat") }.should raise_error(TypeError) + end + + it "raises an ArgumentError when 2 or more arguments is passed" do + lambda { [:a, :b].at(0,1) }.should raise_error(ArgumentError) + end +end |