1describe :array_index, shared: true do 2 it "returns the index of the first element == to object" do 3 x = mock('3') 4 def x.==(obj) 3 == obj; end 5 6 [2, x, 3, 1, 3, 1].send(@method, 3).should == 1 7 [2, 3.0, 3, x, 1, 3, 1].send(@method, x).should == 1 8 end 9 10 it "returns 0 if first element == to object" do 11 [2, 1, 3, 2, 5].send(@method, 2).should == 0 12 end 13 14 it "returns size-1 if only last element == to object" do 15 [2, 1, 3, 1, 5].send(@method, 5).should == 4 16 end 17 18 it "returns nil if no element == to object" do 19 [2, 1, 1, 1, 1].send(@method, 3).should == nil 20 end 21 22 it "accepts a block instead of an argument" do 23 [4, 2, 1, 5, 1, 3].send(@method) {|x| x < 2}.should == 2 24 end 25 26 it "ignores the block if there is an argument" do 27 -> { 28 [4, 2, 1, 5, 1, 3].send(@method, 5) {|x| x < 2}.should == 3 29 }.should complain(/given block not used/) 30 end 31 32 describe "given no argument and no block" do 33 it "produces an Enumerator" do 34 [].send(@method).should be_an_instance_of(Enumerator) 35 end 36 end 37end 38