Skip to content

Commit 51ed6aa

Browse files
committed
Fix numeric coercion when #coerce isn't public
1 parent f732c03 commit 51ed6aa

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

core/integer/divide_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,21 @@
106106
-> { @bignum / :symbol }.should raise_error(TypeError)
107107
end
108108
end
109+
110+
it "coerces the RHS and calls #coerce" do
111+
obj = mock("integer plus")
112+
obj.should_receive(:coerce).with(6).and_return([6, 3])
113+
(6 / obj).should == 2
114+
end
115+
116+
it "coerces the RHS and calls #coerce even if it's private" do
117+
obj = Object.new
118+
class << obj
119+
private def coerce(n)
120+
[n, 3]
121+
end
122+
end
123+
124+
(6 / obj).should == 2
125+
end
109126
end

core/integer/minus_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,21 @@
4040
-> { @bignum - :symbol }.should raise_error(TypeError)
4141
end
4242
end
43+
44+
it "coerces the RHS and calls #coerce" do
45+
obj = mock("integer plus")
46+
obj.should_receive(:coerce).with(5).and_return([5, 10])
47+
(5 - obj).should == -5
48+
end
49+
50+
it "coerces the RHS and calls #coerce even if it's private" do
51+
obj = Object.new
52+
class << obj
53+
private def coerce(n)
54+
[n, 10]
55+
end
56+
end
57+
58+
(5 - obj).should == -5
59+
end
4360
end

core/integer/plus_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,21 @@ def +(other)
5555
RUBY
5656
ruby_exe(code).should == "-1"
5757
end
58+
59+
it "coerces the RHS and calls #coerce" do
60+
obj = mock("integer plus")
61+
obj.should_receive(:coerce).with(6).and_return([6, 3])
62+
(6 + obj).should == 9
63+
end
64+
65+
it "coerces the RHS and calls #coerce even if it's private" do
66+
obj = Object.new
67+
class << obj
68+
private def coerce(n)
69+
[n, 3]
70+
end
71+
end
72+
73+
(6 + obj).should == 9
74+
end
5875
end

0 commit comments

Comments
 (0)