Skip to content

Commit 3d43e58

Browse files
committed
Adjust TypeError messages to match CRuby
1 parent 597fe2c commit 3d43e58

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

spec/ruby/core/range/bsearch_spec.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,30 @@
99
it_behaves_like :enumeratorized_with_unknown_size, :bsearch, (1..3)
1010

1111
it "raises a TypeError if the block returns an Object" do
12-
-> { (0..1).bsearch { Object.new } }.should raise_error(TypeError)
12+
-> { (0..1).bsearch { Object.new } }.should raise_error(TypeError, "wrong argument type Object (must be numeric, true, false or nil)")
1313
end
1414

15-
it "raises a TypeError if the block returns a String" do
16-
-> { (0..1).bsearch { "1" } }.should raise_error(TypeError)
15+
it "raises a TypeError if the block returns a String and boundaries are Integer values" do
16+
-> { (0..1).bsearch { "1" } }.should raise_error(TypeError, "wrong argument type String (must be numeric, true, false or nil)")
17+
end
18+
19+
it "raises a TypeError if the block returns a String and boundaries are Float values" do
20+
-> { (0.0..1.0).bsearch { "1" } }.should raise_error(TypeError, "wrong argument type String (must be numeric, true, false or nil)")
1721
end
1822

1923
it "raises a TypeError if the Range has Object values" do
2024
value = mock("range bsearch")
2125
r = Range.new value, value
2226

23-
-> { r.bsearch { true } }.should raise_error(TypeError)
27+
-> { r.bsearch { true } }.should raise_error(TypeError, "can't do binary search for MockObject")
2428
end
2529

2630
it "raises a TypeError if the Range has String values" do
27-
-> { ("a".."e").bsearch { true } }.should raise_error(TypeError)
31+
-> { ("a".."e").bsearch { true } }.should raise_error(TypeError, "can't do binary search for String")
2832
end
2933

3034
it "raises TypeError when non-Numeric begin/end and block not passed" do
31-
-> { ("a".."e").bsearch }.should raise_error(TypeError)
35+
-> { ("a".."e").bsearch }.should raise_error(TypeError, "can't do binary search for String")
3236
end
3337

3438
context "with Integer values" do

src/main/ruby/truffleruby/core/range.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ def bsearch(&block)
7777
elsif Primitive.is_a?(stop, Integer)
7878
bsearch_integer(&block)
7979
else
80-
raise TypeError, "bsearch is not available for #{Primitive.class(stop)}"
80+
raise TypeError, "can't do binary search for #{Primitive.class(stop)}"
8181
end
8282
elsif Primitive.nil?(start) && Primitive.is_a?(stop, Integer)
8383
bsearch_beginless(&block)
8484
else
85-
raise TypeError, "bsearch is not available for #{Primitive.class(start)}"
85+
raise TypeError, "can't do binary search for #{Primitive.class(start)}"
8686
end
8787
end
8888

@@ -165,7 +165,7 @@ def bsearch(&block)
165165
when false, nil
166166
min = mid
167167
else
168-
raise TypeError, 'Range#bsearch block must return Numeric or boolean'
168+
raise TypeError, "wrong argument type #{Primitive.class(result)} (must be numeric, true, false or nil)"
169169
end
170170
end
171171

@@ -261,7 +261,7 @@ def bsearch(&block)
261261
when false, nil
262262
min = mid + 1
263263
else
264-
raise TypeError, 'Range#bsearch block must return Numeric or boolean'
264+
raise TypeError, "wrong argument type #{Primitive.class(result)} (must be numeric, true, false or nil)"
265265
end
266266
end
267267

0 commit comments

Comments
 (0)