1require_relative '../../spec_helper' 2require_relative 'fixtures/classes' 3 4describe "Array#rassoc" do 5 it "returns the first contained array whose second element is == object" do 6 ary = [[1, "a", 0.5], [2, "b"], [3, "b"], [4, "c"], [], [5], [6, "d"]] 7 ary.rassoc("a").should == [1, "a", 0.5] 8 ary.rassoc("b").should == [2, "b"] 9 ary.rassoc("d").should == [6, "d"] 10 ary.rassoc("z").should == nil 11 end 12 13 it "properly handles recursive arrays" do 14 empty = ArraySpecs.empty_recursive_array 15 empty.rassoc([]).should be_nil 16 [[empty, empty]].rassoc(empty).should == [empty, empty] 17 18 array = ArraySpecs.recursive_array 19 array.rassoc(array).should be_nil 20 [[empty, array]].rassoc(array).should == [empty, array] 21 end 22 23 it "calls elem == obj on the second element of each contained array" do 24 key = 'foobar' 25 o = mock('foobar') 26 def o.==(other); other == 'foobar'; end 27 28 [[1, :foobar], [2, o], [3, mock('foo')]].rassoc(key).should == [2, o] 29 end 30 31 it "does not check the last element in each contained but speficically the second" do 32 key = 'foobar' 33 o = mock('foobar') 34 def o.==(other); other == 'foobar'; end 35 36 [[1, :foobar, o], [2, o, 1], [3, mock('foo')]].rassoc(key).should == [2, o, 1] 37 end 38end 39