1
1
# frozen_string_literal: true
2
2
3
3
RSpec . describe RuboCop ::Cop ::Performance ::Count , :config do
4
- shared_examples 'selectors' do |selector |
4
+ shared_examples 'selectors' do |selector , negate_condition |
5
5
it "registers an offense for using array.#{ selector } ...size" do
6
6
expect_offense ( <<~RUBY , selector : selector )
7
7
[1, 2, 3].#{ selector } { |e| e.even? }.size
8
8
^{selector}^^^^^^^^^^^^^^^^^^^^^ Use `count` instead of `#{ selector } ...size`.
9
9
RUBY
10
+
11
+ if negate_condition
12
+ expect_correction ( <<~RUBY )
13
+ [1, 2, 3].count { |e| !(e.even?) }
14
+ RUBY
15
+ else
16
+ expect_correction ( <<~RUBY )
17
+ [1, 2, 3].count { |e| e.even? }
18
+ RUBY
19
+ end
10
20
end
11
21
12
22
it "registers an offense for using array&.#{ selector } ...size" do
13
23
expect_offense ( <<~RUBY , selector : selector )
14
24
[1, 2, 3]&.#{ selector } { |e| e.even? }&.size
15
25
^{selector}^^^^^^^^^^^^^^^^^^^^^^ Use `count` instead of `#{ selector } ...size`.
16
26
RUBY
27
+
28
+ if negate_condition
29
+ expect_correction ( <<~RUBY )
30
+ [1, 2, 3]&.count { |e| !(e.even?) }
31
+ RUBY
32
+ else
33
+ expect_correction ( <<~RUBY )
34
+ [1, 2, 3]&.count { |e| e.even? }
35
+ RUBY
36
+ end
17
37
end
18
38
19
39
it "registers an offense for using hash.#{ selector } ...size" do
20
40
expect_offense ( <<~RUBY , selector : selector )
21
41
{a: 1, b: 2, c: 3}.#{ selector } { |e| e == :a }.size
22
42
^{selector}^^^^^^^^^^^^^^^^^^^^^ Use `count` instead of `#{ selector } ...size`.
23
43
RUBY
44
+
45
+ if negate_condition
46
+ expect_correction ( <<~RUBY )
47
+ {a: 1, b: 2, c: 3}.count { |e| !(e == :a) }
48
+ RUBY
49
+ else
50
+ expect_correction ( <<~RUBY )
51
+ {a: 1, b: 2, c: 3}.count { |e| e == :a }
52
+ RUBY
53
+ end
24
54
end
25
55
26
56
it "registers an offense for using array.#{ selector } ...length" do
27
57
expect_offense ( <<~RUBY , selector : selector )
28
58
[1, 2, 3].#{ selector } { |e| e.even? }.length
29
59
^{selector}^^^^^^^^^^^^^^^^^^^^^^^ Use `count` instead of `#{ selector } ...length`.
30
60
RUBY
61
+
62
+ if negate_condition
63
+ expect_correction ( <<~RUBY )
64
+ [1, 2, 3].count { |e| !(e.even?) }
65
+ RUBY
66
+ else
67
+ expect_correction ( <<~RUBY )
68
+ [1, 2, 3].count { |e| e.even? }
69
+ RUBY
70
+ end
31
71
end
32
72
33
73
it "registers an offense for using hash.#{ selector } ...length" do
34
74
expect_offense ( <<~RUBY , selector : selector )
35
75
{a: 1, b: 2}.#{ selector } { |e| e == :a }.length
36
76
^{selector}^^^^^^^^^^^^^^^^^^^^^^^ Use `count` instead of `#{ selector } ...length`.
37
77
RUBY
78
+
79
+ if negate_condition
80
+ expect_correction ( <<~RUBY )
81
+ {a: 1, b: 2}.count { |e| !(e == :a) }
82
+ RUBY
83
+ else
84
+ expect_correction ( <<~RUBY )
85
+ {a: 1, b: 2}.count { |e| e == :a }
86
+ RUBY
87
+ end
38
88
end
39
89
40
90
it "registers an offense for using array.#{ selector } ...count" do
41
91
expect_offense ( <<~RUBY , selector : selector )
42
92
[1, 2, 3].#{ selector } { |e| e.even? }.count
43
93
^{selector}^^^^^^^^^^^^^^^^^^^^^^ Use `count` instead of `#{ selector } ...count`.
44
94
RUBY
95
+
96
+ if negate_condition
97
+ expect_correction ( <<~RUBY )
98
+ [1, 2, 3].count { |e| !(e.even?) }
99
+ RUBY
100
+ else
101
+ expect_correction ( <<~RUBY )
102
+ [1, 2, 3].count { |e| e.even? }
103
+ RUBY
104
+ end
45
105
end
46
106
47
107
it "registers an offense for using hash.#{ selector } ...count" do
48
108
expect_offense ( <<~RUBY , selector : selector )
49
109
{a: 1, b: 2}.#{ selector } { |e| e == :a }.count
50
110
^{selector}^^^^^^^^^^^^^^^^^^^^^^ Use `count` instead of `#{ selector } ...count`.
51
111
RUBY
112
+
113
+ if negate_condition
114
+ expect_correction ( <<~RUBY )
115
+ {a: 1, b: 2}.count { |e| !(e == :a) }
116
+ RUBY
117
+ else
118
+ expect_correction ( <<~RUBY )
119
+ {a: 1, b: 2}.count { |e| e == :a }
120
+ RUBY
121
+ end
52
122
end
53
123
54
124
it "allows usage of #{ selector } ...count with a block on an array" do
70
140
puts array.#{ selector } (&:value).count
71
141
^{selector}^^^^^^^^^^^^^^^ Use `count` instead of `#{ selector } ...count`.
72
142
RUBY
143
+
144
+ if negate_condition
145
+ expect_correction ( <<~RUBY )
146
+ Data = Struct.new(:value)
147
+ array = [Data.new(2), Data.new(3), Data.new(2)]
148
+ puts array.count { |element| !element.value }
149
+ RUBY
150
+ else
151
+ expect_correction ( <<~RUBY )
152
+ Data = Struct.new(:value)
153
+ array = [Data.new(2), Data.new(3), Data.new(2)]
154
+ puts array.count(&:value)
155
+ RUBY
156
+ end
73
157
end
74
158
75
159
it "registers an offense for #{ selector } (&:something).count" do
76
160
expect_offense ( <<~RUBY , selector : selector )
77
161
foo.#{ selector } (&:something).count
78
162
^{selector}^^^^^^^^^^^^^^^^^^^ Use `count` instead of `#{ selector } ...count`.
79
163
RUBY
164
+
165
+ if negate_condition
166
+ expect_correction ( <<~RUBY )
167
+ foo.count { |element| !element.something }
168
+ RUBY
169
+ else
170
+ expect_correction ( <<~RUBY )
171
+ foo.count(&:something)
172
+ RUBY
173
+ end
174
+ end
175
+
176
+ it "registers an offense for multiline #{ selector } (&:something).count.positive?" do
177
+ expect_offense ( <<~RUBY , selector : selector )
178
+ foo
179
+ .#{ selector } (&:something)
180
+ ^{selector}^^^^^^^^^^^^^ Use `count` instead of `#{ selector } ...count`.
181
+ .count
182
+ .positive?
183
+ RUBY
184
+
185
+ if negate_condition
186
+ expect_correction ( <<~RUBY )
187
+ foo
188
+ .count { |element| !element.something }
189
+ .positive?
190
+ RUBY
191
+ else
192
+ expect_correction ( <<~RUBY )
193
+ foo
194
+ .count(&:something)
195
+ .positive?
196
+ RUBY
197
+ end
80
198
end
81
199
82
200
it "registers an offense for #{ selector } (&:something)&.count" do
83
201
expect_offense ( <<~RUBY , selector : selector )
84
202
foo&.#{ selector } (&:something)&.count
85
203
^{selector}^^^^^^^^^^^^^^^^^^^^ Use `count` instead of `#{ selector } ...count`.
86
204
RUBY
205
+
206
+ if negate_condition
207
+ expect_correction ( <<~RUBY )
208
+ foo&.count { |element| !element.something }
209
+ RUBY
210
+ else
211
+ expect_correction ( <<~RUBY )
212
+ foo&.count(&:something)
213
+ RUBY
214
+ end
87
215
end
88
216
89
217
it "registers an offense for #{ selector } (&:something).count " \
@@ -96,6 +224,24 @@ def count(&block)
96
224
end
97
225
end
98
226
RUBY
227
+
228
+ if negate_condition
229
+ expect_correction ( <<~RUBY )
230
+ class A < Array
231
+ def count(&block)
232
+ count { !block.call }
233
+ end
234
+ end
235
+ RUBY
236
+ else
237
+ expect_correction ( <<~RUBY )
238
+ class A < Array
239
+ def count(&block)
240
+ count(&block)
241
+ end
242
+ end
243
+ RUBY
244
+ end
99
245
end
100
246
101
247
it "allows usage of #{ selector } without getting the size" do
@@ -117,10 +263,10 @@ def count(&block)
117
263
end
118
264
end
119
265
120
- it_behaves_like ( 'selectors' , 'select' )
121
- it_behaves_like ( 'selectors' , 'find_all' )
122
- it_behaves_like ( 'selectors' , 'filter' )
123
- it_behaves_like ( 'selectors' , 'reject' )
266
+ it_behaves_like ( 'selectors' , 'select' , false )
267
+ it_behaves_like ( 'selectors' , 'find_all' , false )
268
+ it_behaves_like ( 'selectors' , 'filter' , false )
269
+ it_behaves_like ( 'selectors' , 'reject' , true )
124
270
125
271
context 'Active Record select' do
126
272
it 'allows usage of select with a string' do
0 commit comments