Skip to content
This repository
Newer
Older
100644 52 lines (42 sloc) 1.795 kb
957c2c9c » brixen
2010-02-17 Standardize require lines.
1 require File.expand_path('../../../spec_helper', __FILE__)
2 require File.expand_path('../fixtures/classes', __FILE__)
6c1c34a6 » brixen
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
3
4 describe "Array#fetch" do
a4700686 » arthurschreiber
2008-06-09 Updated Array#fetch specs for compatibility with 1.8.7.
5 it "returns the element at the passed index" do
6c1c34a6 » brixen
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
6 [1, 2, 3].fetch(1).should == 2
7 [nil].fetch(0).should == nil
8 end
9
10 it "counts negative indices backwards from end" do
11 [1, 2, 3, 4].fetch(-1).should == 4
12 end
356fa4fb » brixen
2011-04-14 Finally cleaned up trailing whitespace in spec files.
13
6c1c34a6 » brixen
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
14 it "raises an IndexError if there is no element at index" do
15 lambda { [1, 2, 3].fetch(3) }.should raise_error(IndexError)
16 lambda { [1, 2, 3].fetch(-4) }.should raise_error(IndexError)
17 lambda { [].fetch(0) }.should raise_error(IndexError)
18 end
356fa4fb » brixen
2011-04-14 Finally cleaned up trailing whitespace in spec files.
19
6c1c34a6 » brixen
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
20 it "returns default if there is no element at index if passed a default value" do
21 [1, 2, 3].fetch(5, :not_found).should == :not_found
22 [1, 2, 3].fetch(5, nil).should == nil
23 [1, 2, 3].fetch(-4, :not_found).should == :not_found
24 [nil].fetch(0, :not_found).should == nil
25 end
26
27 it "returns the value of block if there is no element at index if passed a block" do
28 [1, 2, 3].fetch(9) { |i| i * i }.should == 81
29 [1, 2, 3].fetch(-9) { |i| i * i }.should == 81
30 end
31
32 it "passes the original index argument object to the block, not the converted Integer" do
33 o = mock('5')
34 def o.to_int(); 5; end
35
3777b9ed » arthurschreiber
2008-05-18 Use 'obj.should equal(other_obj)' instead of 'obj.equal?(other_obj).s…
36 [1, 2, 3].fetch(o) { |i| i }.should equal(o)
6c1c34a6 » brixen
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
37 end
38
39 it "gives precedence to the default block over the default argument" do
40 [1, 2, 3].fetch(9, :foo) { |i| i * i }.should == 81
41 end
42
a4700686 » arthurschreiber
2008-06-09 Updated Array#fetch specs for compatibility with 1.8.7.
43 it "tries to convert the passed argument to an Integer using #to_int" do
44 obj = mock('to_int')
45 obj.should_receive(:to_int).and_return(2)
7341a8bf » arthurschreiber
2008-06-09 Fixed a small copy and paste error.
46 ["a", "b", "c"].fetch(obj).should == "c"
a4700686 » arthurschreiber
2008-06-09 Updated Array#fetch specs for compatibility with 1.8.7.
47 end
627d205b » brixen
2009-01-19 Removed specs mocking #respond_to? and #method_missing.
48
a4700686 » arthurschreiber
2008-06-09 Updated Array#fetch specs for compatibility with 1.8.7.
49 it "raises a TypeError when the passed argument can't be coerced to Integer" do
50 lambda { [].fetch("cat") }.should raise_error(TypeError)
6c1c34a6 » brixen
2008-05-09 Ask not what RubySpec can do for you, but what you can do for RubySpec.
51 end
52 end
Something went wrong with that request. Please try again.