/
number_conversion_spec.rb
286 lines (241 loc) · 8.16 KB
/
number_conversion_spec.rb
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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::NumberConversion, :config do
context 'registers an offense' do
it 'when using `#to_i`' do
expect_offense(<<~RUBY)
"10".to_i
^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `"10".to_i`, use stricter `Integer("10", 10)`.
RUBY
expect_correction(<<~RUBY)
Integer("10", 10)
RUBY
end
it 'when using `#to_f`' do
expect_offense(<<~RUBY)
"10.2".to_f
^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `"10.2".to_f`, use stricter `Float("10.2")`.
RUBY
expect_correction(<<~RUBY)
Float("10.2")
RUBY
end
it 'when using `#to_c`' do
expect_offense(<<~RUBY)
"10".to_c
^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `"10".to_c`, use stricter `Complex("10")`.
RUBY
expect_correction(<<~RUBY)
Complex("10")
RUBY
end
it 'when using `#to_i` for number literals' do
expect_no_offenses(<<~RUBY)
42.to_i
42.0.to_i
RUBY
end
it 'when using `#to_f` for number literals' do
expect_no_offenses(<<~RUBY)
42.to_f
42.0.to_f
RUBY
end
it 'when using `#to_c` for number literals' do
expect_no_offenses(<<~RUBY)
42.to_c
42.0.to_c
RUBY
end
it 'when `#to_i` called on a variable' do
expect_offense(<<~RUBY)
string_value = '10'
string_value.to_i
^^^^^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `string_value.to_i`, use stricter `Integer(string_value, 10)`.
RUBY
expect_correction(<<~RUBY)
string_value = '10'
Integer(string_value, 10)
RUBY
end
it 'when `#to_i` called on a hash value' do
expect_offense(<<~RUBY)
params = { id: 10 }
params[:id].to_i
^^^^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `params[:id].to_i`, use stricter `Integer(params[:id], 10)`.
RUBY
expect_correction(<<~RUBY)
params = { id: 10 }
Integer(params[:id], 10)
RUBY
end
it 'when `#to_i` called on a variable on a array' do
expect_offense(<<~RUBY)
args = [1,2,3]
args[0].to_i
^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `args[0].to_i`, use stricter `Integer(args[0], 10)`.
RUBY
expect_correction(<<~RUBY)
args = [1,2,3]
Integer(args[0], 10)
RUBY
end
it 'when `#to_i` called on a variable on a hash' do
expect_offense(<<~RUBY)
params[:field].to_i
^^^^^^^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `params[:field].to_i`, use stricter `Integer(params[:field], 10)`.
RUBY
expect_correction(<<~RUBY)
Integer(params[:field], 10)
RUBY
end
end
context 'does not register an offense' do
it 'when using Integer() with integer' do
expect_no_offenses(<<~RUBY)
Integer(10)
RUBY
end
it 'when using Float()' do
expect_no_offenses(<<~RUBY)
Float('10')
RUBY
end
it 'when using Complex()' do
expect_no_offenses(<<~RUBY)
Complex('10')
RUBY
end
it 'when `#to_i` called without a receiver' do
expect_no_offenses(<<~RUBY)
to_i
RUBY
end
it 'when `:to_f` is one of multiple method arguments' do
expect_no_offenses(<<~RUBY)
delegate :to_f, to: :description, allow_nil: true
RUBY
end
end
context 'to_method in symbol form' do
it 'registers offense and autocorrects' do
expect_offense(<<~RUBY)
"1,2,3,foo,5,6,7,8".split(',').map(&:to_i)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `&:to_i`, use stricter `{ |i| Integer(i, 10) }`.
RUBY
expect_correction(<<~RUBY)
"1,2,3,foo,5,6,7,8".split(',').map { |i| Integer(i, 10) }
RUBY
end
it 'registers offense and autocorrects without parentheses' do
expect_offense(<<~RUBY)
"1,2,3,foo,5,6,7,8".split(',').map &:to_i
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `&:to_i`, use stricter `{ |i| Integer(i, 10) }`.
RUBY
expect_correction(<<~RUBY)
"1,2,3,foo,5,6,7,8".split(',').map { |i| Integer(i, 10) }
RUBY
end
it 'registers offense with try' do
expect_offense(<<~RUBY)
"foo".try(:to_f)
^^^^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `:to_f`, use stricter `{ |i| Float(i) }`.
RUBY
expect_correction(<<~RUBY)
"foo".try { |i| Float(i) }
RUBY
end
it 'registers an offense when using nested number conversion methods' do
expect_offense(<<~RUBY)
var.to_i.to_f
^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `var.to_i`, use stricter `Integer(var, 10)`.
RUBY
expect_correction(<<~RUBY)
Integer(var, 10).to_f
RUBY
end
it 'does not register an offense when using `Integer` constructor' do
expect_no_offenses(<<~RUBY)
Integer(var, 10).to_f
RUBY
end
it 'does not register an offense when using `Float` constructor' do
expect_no_offenses(<<~RUBY)
Float(var).to_i
RUBY
end
it 'does not register an offense when using `Complex` constructor' do
expect_no_offenses(<<~RUBY)
Complex(var).to_f
RUBY
end
it 'registers offense with send' do
expect_offense(<<~RUBY)
"foo".send(:to_c)
^^^^^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `:to_c`, use stricter `{ |i| Complex(i) }`.
RUBY
expect_correction(<<~RUBY)
"foo".send { |i| Complex(i) }
RUBY
end
end
context 'IgnoredClasses' do
let(:cop_config) { { 'IgnoredClasses' => %w[Time DateTime] } }
it 'when using Time' do
expect_no_offenses(<<~RUBY)
Time.now.to_i
Time.now.to_f
Time.strptime("2000-10-31", "%Y-%m-%d").to_i
Time.httpdate("Thu, 06 Oct 2011 02:26:12 GMT").to_f
Time.now.to_datetime.to_i
RUBY
end
it 'when using DateTime' do
expect_no_offenses(<<~RUBY)
DateTime.new(2012, 8, 29, 22, 35, 0).to_i
DateTime.new(2012, 8, 29, 22, 35, 0).to_f
RUBY
end
it 'when using Time/DateTime with multiple method calls' do
expect_no_offenses(<<~RUBY)
Time.now.to_datetime.to_i
DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc.to_f
Time.zone.now.to_datetime.to_f
DateTime.new(2012, 8, 29, 22, 35, 0)
.change(day: 1)
.change(month: 1)
.change(year: 2020)
.to_i
RUBY
end
end
context 'IgnoredMethods' do
context 'with a string' do
let(:cop_config) { { 'IgnoredMethods' => %w[minutes] } }
it 'does not register an offense for an ignored method' do
expect_no_offenses(<<~RUBY)
10.minutes.to_i
RUBY
end
it 'registers an offense for other methods' do
expect_offense(<<~RUBY)
10.hours.to_i
^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `10.hours.to_i`, use stricter `Integer(10.hours, 10)`.
RUBY
end
end
context 'with a regex' do
let(:cop_config) { { 'IgnoredMethods' => [/minutes/] } }
it 'does not register an offense for an ignored method' do
expect_no_offenses(<<~RUBY)
10.minutes.to_i
RUBY
end
it 'registers an offense for other methods' do
expect_offense(<<~RUBY)
10.hours.to_i
^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `10.hours.to_i`, use stricter `Integer(10.hours, 10)`.
RUBY
end
end
end
end