Skip to content

Commit

Permalink
Merge pull request #1110 from bolshakov/assoc-rassoc-to_ary
Browse files Browse the repository at this point in the history
Add #to_ary specs for Array's assoc and rassoc
  • Loading branch information
andrykonchin authored Nov 12, 2023
2 parents 49c26cf + b4c340f commit f3b5ec7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/array/assoc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@
a.assoc(s1.first).should equal(s1)
a.assoc(s2.first).should equal(s2)
end

it "calls to_ary on non-array elements" do
s1 = [1, 2]
s2 = ArraySpecs::ArrayConvertible.new(2, 3)
a = [s1, s2]

s1.should_not_receive(:to_ary)
a.assoc(s1.first).should equal(s1)

a.assoc(2).should == [2, 3]
s2.called.should equal(:to_ary)
end
end
12 changes: 12 additions & 0 deletions core/array/rassoc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ def o.==(other); other == 'foobar'; end

[[1, :foobar, o], [2, o, 1], [3, mock('foo')]].rassoc(key).should == [2, o, 1]
end

it "does not call to_ary on non-array elements" do
s1 = [1, 2]
s2 = ArraySpecs::ArrayConvertible.new(2, 3)
a = [s1, s2]

s1.should_not_receive(:to_ary)
a.rassoc(2).should equal(s1)

s2.should_not_receive(:to_ary)
a.rassoc(3).should equal(nil)
end
end

0 comments on commit f3b5ec7

Please sign in to comment.