1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
describe :kernel_float, shared: true do
it "returns the identical Float for numeric Floats" do
float = 1.12
float2 = @object.send(:Float, float)
float2.should == float
float2.should equal float
end
it "returns a Float for Fixnums" do
@object.send(:Float, 1).should == 1.0
end
it "returns a Float for Complex with only a real part" do
@object.send(:Float, Complex(1)).should == 1.0
end
it "returns a Float for Bignums" do
@object.send(:Float, 1000000000000).should == 1000000000000.0
end
it "raises an ArgumentError for nil" do
-> { @object.send(:Float, nil) }.should raise_error(TypeError)
end
it "returns the identical NaN for NaN" do
nan = nan_value
nan.nan?.should be_true
nan2 = @object.send(:Float, nan)
nan2.nan?.should be_true
nan2.should equal(nan)
end
it "returns the same Infinity for Infinity" do
infinity = infinity_value
infinity2 = @object.send(:Float, infinity)
infinity2.should == infinity_value
infinity.should equal(infinity2)
end
it "converts Strings to floats without calling #to_f" do
string = "10"
string.should_not_receive(:to_f)
@object.send(:Float, string).should == 10.0
end
it "converts Strings with decimal points into Floats" do
@object.send(:Float, "10.0").should == 10.0
end
it "raises an ArgumentError for a String of word characters" do
-> { @object.send(:Float, "float") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError for a String with string in error message" do
-> { @object.send(:Float, "foo") }.should raise_error(ArgumentError) { |e|
e.message.should == 'invalid value for Float(): "foo"'
}
end
it "raises an ArgumentError if there are two decimal points in the String" do
-> { @object.send(:Float, "10.0.0") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError for a String of numbers followed by word characters" do
-> { @object.send(:Float, "10D") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError for a String of word characters followed by numbers" do
-> { @object.send(:Float, "D10") }.should raise_error(ArgumentError)
end
it "is strict about the string form even across newlines" do
-> { @object.send(:Float, "not a number\n10") }.should raise_error(ArgumentError)
-> { @object.send(:Float, "10\nnot a number") }.should raise_error(ArgumentError)
end
it "converts String subclasses to floats without calling #to_f" do
my_string = Class.new(String) do
def to_f() 1.2 end
end
@object.send(:Float, my_string.new("10")).should == 10.0
end
it "returns a positive Float if the string is prefixed with +" do
@object.send(:Float, "+10").should == 10.0
@object.send(:Float, " +10").should == 10.0
end
it "returns a negative Float if the string is prefixed with +" do
@object.send(:Float, "-10").should == -10.0
@object.send(:Float, " -10").should == -10.0
end
it "raises an ArgumentError if a + or - is embedded in a String" do
-> { @object.send(:Float, "1+1") }.should raise_error(ArgumentError)
-> { @object.send(:Float, "1-1") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError if a String has a trailing + or -" do
-> { @object.send(:Float, "11+") }.should raise_error(ArgumentError)
-> { @object.send(:Float, "11-") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError for a String with a leading _" do
-> { @object.send(:Float, "_1") }.should raise_error(ArgumentError)
end
it "returns a value for a String with an embedded _" do
@object.send(:Float, "1_000").should == 1000.0
end
it "raises an ArgumentError for a String with a trailing _" do
-> { @object.send(:Float, "10_") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError for a String of \\0" do
-> { @object.send(:Float, "\0") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError for a String with a leading \\0" do
-> { @object.send(:Float, "\01") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError for a String with an embedded \\0" do
-> { @object.send(:Float, "1\01") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError for a String with a trailing \\0" do
-> { @object.send(:Float, "1\0") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError for a String that is just an empty space" do
-> { @object.send(:Float, " ") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError for a String that with an embedded space" do
-> { @object.send(:Float, "1 2") }.should raise_error(ArgumentError)
end
it "returns a value for a String with a leading space" do
@object.send(:Float, " 1").should == 1.0
end
it "returns a value for a String with a trailing space" do
@object.send(:Float, "1 ").should == 1.0
end
it "returns a value for a String with any leading whitespace" do
@object.send(:Float, "\t\n1").should == 1.0
end
it "returns a value for a String with any trailing whitespace" do
@object.send(:Float, "1\t\n").should == 1.0
end
%w(e E).each do |e|
it "raises an ArgumentError if #{e} is the trailing character" do
-> { @object.send(:Float, "2#{e}") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError if #{e} is the leading character" do
-> { @object.send(:Float, "#{e}2") }.should raise_error(ArgumentError)
end
it "returns Infinity for '2#{e}1000'" do
@object.send(:Float, "2#{e}1000").should == Float::INFINITY
end
it "returns 0 for '2#{e}-1000'" do
@object.send(:Float, "2#{e}-1000").should == 0
end
it "allows embedded _ in a number on either side of the #{e}" do
@object.send(:Float, "2_0#{e}100").should == 20e100
@object.send(:Float, "20#{e}1_00").should == 20e100
@object.send(:Float, "2_0#{e}1_00").should == 20e100
end
it "raises an exception if a space is embedded on either side of the '#{e}'" do
-> { @object.send(:Float, "2 0#{e}100") }.should raise_error(ArgumentError)
-> { @object.send(:Float, "20#{e}1 00") }.should raise_error(ArgumentError)
end
it "raises an exception if there's a leading _ on either side of the '#{e}'" do
-> { @object.send(:Float, "_20#{e}100") }.should raise_error(ArgumentError)
-> { @object.send(:Float, "20#{e}_100") }.should raise_error(ArgumentError)
end
it "raises an exception if there's a trailing _ on either side of the '#{e}'" do
-> { @object.send(:Float, "20_#{e}100") }.should raise_error(ArgumentError)
-> { @object.send(:Float, "20#{e}100_") }.should raise_error(ArgumentError)
end
it "allows decimal points on the left side of the '#{e}'" do
@object.send(:Float, "2.0#{e}2").should == 2e2
end
it "raises an ArgumentError if there's a decimal point on the right side of the '#{e}'" do
-> { @object.send(:Float, "20#{e}2.0") }.should raise_error(ArgumentError)
end
end
describe "for hexadecimal literals with binary exponent" do
%w(p P).each do |p|
it "interprets the fractional part (on the left side of '#{p}') in hexadecimal" do
@object.send(:Float, "0x10#{p}0").should == 16.0
end
it "interprets the exponent (on the right of '#{p}') in decimal" do
@object.send(:Float, "0x1#{p}10").should == 1024.0
end
it "raises an ArgumentError if #{p} is the trailing character" do
-> { @object.send(:Float, "0x1#{p}") }.should raise_error(ArgumentError)
end
it "raises an ArgumentError if #{p} is the leading character" do
-> { @object.send(:Float, "0x#{p}1") }.should raise_error(ArgumentError)
end
it "returns Infinity for '0x1#{p}10000'" do
@object.send(:Float, "0x1#{p}10000").should == Float::INFINITY
end
it "returns 0 for '0x1#{p}-10000'" do
@object.send(:Float, "0x1#{p}-10000").should == 0
end
it "allows embedded _ in a number on either side of the #{p}" do
@object.send(:Float, "0x1_0#{p}10").should == 16384.0
@object.send(:Float, "0x10#{p}1_0").should == 16384.0
@object.send(:Float, "0x1_0#{p}1_0").should == 16384.0
end
it "raises an exception if a space is embedded on either side of the '#{p}'" do
-> { @object.send(:Float, "0x1 0#{p}10") }.should raise_error(ArgumentError)
-> { @object.send(:Float, "0x10#{p}1 0") }.should raise_error(ArgumentError)
end
it "raises an exception if there's a leading _ on either side of the '#{p}'" do
-> { @object.send(:Float, "0x_10#{p}10") }.should raise_error(ArgumentError)
-> { @object.send(:Float, "0x10#{p}_10") }.should raise_error(ArgumentError)
end
it "raises an exception if there's a trailing _ on either side of the '#{p}'" do
-> { @object.send(:Float, "0x10_#{p}10") }.should raise_error(ArgumentError)
-> { @object.send(:Float, "0x10#{p}10_") }.should raise_error(ArgumentError)
end
it "allows hexadecimal points on the left side of the '#{p}'" do
@object.send(:Float, "0x1.8#{p}0").should == 1.5
end
it "raises an ArgumentError if there's a decimal point on the right side of the '#{p}'" do
-> { @object.send(:Float, "0x1#{p}1.0") }.should raise_error(ArgumentError)
end
end
end
it "returns a Float that can be a parameter to #Float again" do
float = @object.send(:Float, "10")
@object.send(:Float, float).should == 10.0
end
it "otherwise, converts the given argument to a Float by calling #to_f" do
(obj = mock('1.2')).should_receive(:to_f).once.and_return(1.2)
obj.should_not_receive(:to_i)
@object.send(:Float, obj).should == 1.2
end
it "returns the identical NaN if to_f is called and it returns NaN" do
nan = nan_value
(nan_to_f = mock('NaN')).should_receive(:to_f).once.and_return(nan)
nan2 = @object.send(:Float, nan_to_f)
nan2.nan?.should be_true
nan2.should equal(nan)
end
it "returns the identical Infinity if to_f is called and it returns Infinity" do
infinity = infinity_value
(infinity_to_f = mock('Infinity')).should_receive(:to_f).once.and_return(infinity)
infinity2 = @object.send(:Float, infinity_to_f)
infinity2.should equal(infinity)
end
it "raises a TypeError if #to_f is not provided" do
-> { @object.send(:Float, mock('x')) }.should raise_error(TypeError)
end
it "raises a TypeError if #to_f returns a String" do
(obj = mock('ha!')).should_receive(:to_f).once.and_return('ha!')
-> { @object.send(:Float, obj) }.should raise_error(TypeError)
end
it "raises a TypeError if #to_f returns an Integer" do
(obj = mock('123')).should_receive(:to_f).once.and_return(123)
-> { @object.send(:Float, obj) }.should raise_error(TypeError)
end
it "raises a RangeError when passed a Complex argument" do
c = Complex(2, 3)
-> { @object.send(:Float, c) }.should raise_error(RangeError)
end
describe "when passed exception: false" do
describe "and valid input" do
it "returns a Float number" do
@object.send(:Float, 1, exception: false).should == 1.0
@object.send(:Float, "1", exception: false).should == 1.0
@object.send(:Float, "1.23", exception: false).should == 1.23
end
end
describe "and invalid input" do
it "swallows an error" do
@object.send(:Float, "abc", exception: false).should == nil
@object.send(:Float, :sym, exception: false).should == nil
end
end
describe "and nil" do
it "swallows it" do
@object.send(:Float, nil, exception: false).should == nil
end
end
end
end
describe "Kernel.Float" do
it_behaves_like :kernel_float, :Float, Kernel
end
describe "Kernel#Float" do
it_behaves_like :kernel_float, :Float, Object.new
end
describe "Kernel#Float" do
it "is a private method" do
Kernel.should have_private_instance_method(:Float)
end
end
|