/
numeric_literals_spec.rb
222 lines (185 loc) · 5.65 KB
/
numeric_literals_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::NumericLiterals, :config do
let(:cop_config) { { 'MinDigits' => 5 } }
it 'registers an offense for a long undelimited integer' do
expect_offense(<<~RUBY)
a = 12345
^^^^^ Use underscores(_) as thousands separator and separate every 3 digits with them.
RUBY
expect_correction(<<~RUBY)
a = 12_345
RUBY
end
it 'registers an offense for a float with a long undelimited integer part' do
expect_offense(<<~RUBY)
a = 123456.789
^^^^^^^^^^ Use underscores(_) as thousands separator and separate every 3 digits with them.
RUBY
expect_correction(<<~RUBY)
a = 123_456.789
RUBY
end
it 'accepts integers with less than three places at the end' do
expect_no_offenses(<<~RUBY)
a = 123_456_789_00
b = 819_2
RUBY
end
it 'registers an offense for an integer with misplaced underscore' do
expect_offense(<<~RUBY)
a = 123_456_78_90_00
^^^^^^^^^^^^^^^^ Use underscores(_) as thousands separator and separate every 3 digits with them.
b = 1_8192
^^^^^^ Use underscores(_) as thousands separator and separate every 3 digits with them.
RUBY
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
expect_correction(<<~RUBY)
a = 123_456_789_000
b = 18_192
RUBY
end
it 'accepts long numbers with underscore' do
expect_no_offenses(<<~RUBY)
a = 123_456
b = 123_456.55
RUBY
end
it 'accepts a short integer without underscore' do
expect_no_offenses('a = 123')
end
it 'does not count a leading minus sign as a digit' do
expect_no_offenses('a = -1230')
end
it 'accepts short numbers without underscore' do
expect_no_offenses(<<~RUBY)
a = 123
b = 123.456
RUBY
end
it 'ignores non-decimal literals' do
expect_no_offenses(<<~RUBY)
a = 0b1010101010101
b = 01717171717171
c = 0xab11111111bb
RUBY
end
it 'handles numeric literal with exponent' do
expect_offense(<<~RUBY)
a = 10e10
b = 3e12345
c = 12.345e3
d = 12345e3
^^^^^^^ Use underscores(_) as thousands separator and separate every 3 digits with them.
RUBY
expect_correction(<<~RUBY)
a = 10e10
b = 3e12345
c = 12.345e3
d = 12_345e3
RUBY
end
it 'autocorrects negative numbers' do
expect_offense(<<~RUBY)
a = -123456
^^^^^^^ Use underscores(_) as thousands separator and separate every 3 digits with them.
RUBY
expect_correction(<<~RUBY)
a = -123_456
RUBY
end
it 'autocorrects negative floating-point numbers' do
expect_offense(<<~RUBY)
a = -123456.78
^^^^^^^^^^ Use underscores(_) as thousands separator and separate every 3 digits with them.
RUBY
expect_correction(<<~RUBY)
a = -123_456.78
RUBY
end
it 'autocorrects numbers with spaces between leading minus and numbers' do
expect_offense(<<~RUBY)
a = -
^ Use underscores(_) as thousands separator and separate every 3 digits with them.
12345
RUBY
expect_correction(<<~RUBY)
a = -12_345
RUBY
end
it 'autocorrects numeric literal with exponent and dot' do
expect_offense(<<~RUBY)
a = 12345.6e3
^^^^^^^^^ Use underscores(_) as thousands separator and separate every 3 digits with them.
RUBY
expect_correction(<<~RUBY)
a = 12_345.6e3
RUBY
end
it 'autocorrects numeric literal with exponent (large E) and dot' do
expect_offense(<<~RUBY)
a = 12345.6E3
^^^^^^^^^ Use underscores(_) as thousands separator and separate every 3 digits with them.
RUBY
expect_correction(<<~RUBY)
a = 12_345.6E3
RUBY
end
context 'strict' do
let(:cop_config) { { 'MinDigits' => 5, 'Strict' => true } }
it 'registers an offense for an integer with misplaced underscore' do
expect_offense(<<~RUBY)
a = 123_456_78_90_00
^^^^^^^^^^^^^^^^ Use underscores(_) as thousands separator and separate every 3 digits with them.
RUBY
expect_correction(<<~RUBY)
a = 123_456_789_000
RUBY
end
end
context 'for --auto-gen-config' do
let(:enabled) { cop.config_to_allow_offenses['Enabled'] }
let(:min_digits) { cop.config_to_allow_offenses.dig(:exclude_limit, 'MinDigits') }
context 'when the number is only digits' do
it 'detects right value of MinDigits based on the longest number' do
expect_offense(<<~RUBY)
1234567890
^^^^^^^^^^ [...]
12345678901234567890
^^^^^^^^^^^^^^^^^^^^ [...]
123456789012
^^^^^^^^^^^^ [...]
RUBY
expect(min_digits).to eq(21)
expect(enabled.nil?).to be(true)
end
it 'sets the right value if one is disabled inline' do
expect_offense(<<~RUBY)
1234567890
^^^^^^^^^^ [...]
12345678901234567890 # rubocop:disable Style/NumericLiterals
123456789012
^^^^^^^^^^^^ [...]
RUBY
expect(min_digits).to eq(13)
expect(enabled.nil?).to be(true)
end
end
context 'with separators' do
it 'disables the cop' do
expect_offense(<<~RUBY)
1234_5678_90
^^^^^^^^^^^^ [...]
RUBY
expect(enabled).to eq(false)
expect(min_digits.nil?).to be(true)
end
it 'does not disable the cop if the line is disabled' do
expect_no_offenses(<<~RUBY)
1234_5678_90 # rubocop:disable Style/NumericLiterals
RUBY
expect(enabled.nil?).to be(true)
expect(min_digits.nil?).to be(true)
end
end
end
end