Skip to content

Commit d36db5f

Browse files
committed
Add specs for a fixed bug in String#to_f when decimal part omitted
1 parent 8287b05 commit d36db5f

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

core/kernel/Float_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def to_f() 1.2 end
163163
-> { @object.send(:Float, "+1.") }.should raise_error(ArgumentError)
164164
-> { @object.send(:Float, "-1.") }.should raise_error(ArgumentError)
165165
-> { @object.send(:Float, "1.e+0") }.should raise_error(ArgumentError)
166+
-> { @object.send(:Float, "1.e-2") }.should raise_error(ArgumentError)
166167
end
167168
end
168169

@@ -172,6 +173,7 @@ def to_f() 1.2 end
172173
@object.send(:Float, "+1.").should == 1.0
173174
@object.send(:Float, "-1.").should == -1.0
174175
@object.send(:Float, "1.e+0").should == 1.0
176+
@object.send(:Float, "1.e-2").should be_close(0.01, TOLERANCE)
175177
end
176178
end
177179

core/string/to_f_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,16 @@
127127
}.should raise_error(Encoding::CompatibilityError, "ASCII incompatible encoding: UTF-16")
128128
end
129129
end
130+
131+
it "allows String representation without a fractional part" do
132+
"1.".to_f.should == 1.0
133+
"+1.".to_f.should == 1.0
134+
"-1.".to_f.should == -1.0
135+
"1.e+0".to_f.should == 1.0
136+
"1.e+0".to_f.should == 1.0
137+
138+
ruby_bug "#20705", ""..."3.4" do
139+
"1.e-2".to_f.should be_close(0.01, TOLERANCE)
140+
end
141+
end
130142
end

0 commit comments

Comments
 (0)