blob: 59b7afb2b4c26ece5cb1cfca6bfb7a6f0cca892c (
plain) (
blame)
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
|
require 'spec_helper'
require 'bigdecimal'
module Arel
describe "Attributes::Integer" do
before :all do
@relation = Model.build do |r|
r.engine Testing::Engine.new
r.attribute :age, Attributes::Integer
end
end
def type_cast(val)
@relation[:age].type_cast(val)
end
describe "#type_cast" do
it "returns same value if an integer" do
type_cast(24).should eql(24)
end
it "returns nil if passed nil" do
type_cast(nil).should be_nil
end
it "returns nil if passed empty string" do
type_cast('').should be_nil
end
it "returns integer representation of a zero string integer" do
type_cast('0').should eql(0)
end
it "returns integer representation of a positive string integer" do
type_cast('24').should eql(24)
end
it "returns integer representation of a positive string integer with spaces" do
type_cast(' 24').should eql(24)
type_cast('24 ').should eql(24)
end
it "returns integer representation of a negative string integer" do
type_cast('-24').should eql(-24)
end
it "returns integer representation of a negative string integer with spaces" do
type_cast('-24 ').should eql(-24)
type_cast(' -24').should eql(-24)
end
it "returns integer representation of a zero string float" do
type_cast('0.0').should eql(0)
end
it "returns integer representation of a positive string float" do
type_cast('24.35').should eql(24)
end
it "returns integer representation of a positive string float with spaces" do
type_cast(' 24.35').should eql(24)
type_cast('24.35 ').should eql(24)
end
it "returns integer representation of a negative string float" do
type_cast('-24.35').should eql(-24)
end
it "returns integer representation of a negative string float with spaces" do
type_cast(' -24.35 ').should eql(-24)
end
it "returns integer representation of a zero string float, with no leading digits" do
type_cast('.0').should eql(0)
end
it "returns integer representation of a zero string float, with no leading digits with spaces" do
type_cast(' .0').should eql(0)
end
it "returns integer representation of a positive string float, with no leading digits" do
type_cast('.41').should eql(0)
end
it "returns integer representation of a zero float" do
type_cast(0.0).should eql(0)
end
it "returns integer representation of a positive float" do
type_cast(24.35).should eql(24)
end
it "returns integer representation of a negative float" do
type_cast(-24.35).should eql(-24)
end
it "returns integer representation of a zero decimal" do
type_cast(BigDecimal('0.0')).should eql(0)
end
it "returns integer representation of a positive decimal" do
type_cast(BigDecimal('24.35')).should eql(24)
end
it "returns integer representation of a negative decimal" do
type_cast(BigDecimal('-24.35')).should eql(-24)
end
[ Object.new, true, '00.0', '0.', 'string' ].each do |value|
it "raises exception when attempting type_cast of non-numeric value #{value.inspect}" do
lambda do
type_cast(value)
end.should raise_error(TypecastError, /could not typecast/)
end
end
end
end
end
|