forked from rubocop/rubocop
/
redundant_sort_spec.rb
300 lines (245 loc) · 8.53 KB
/
redundant_sort_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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::RedundantSort, :config do
it 'registers an offense when first is called with sort' do
expect_offense(<<~RUBY)
[1, 2, 3].sort.first
^^^^^^^^^^ Use `min` instead of `sort...first`.
RUBY
expect_correction(<<~RUBY)
[1, 2, 3].min
RUBY
end
it 'registers an offense when last is called with sort' do
expect_offense(<<~RUBY)
[1, 2].sort.last
^^^^^^^^^ Use `max` instead of `sort...last`.
RUBY
expect_correction(<<~RUBY)
[1, 2].max
RUBY
end
it 'registers an offense when last is called on sort with comparator' do
expect_offense(<<~RUBY)
foo.sort { |a, b| b <=> a }.last
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `max` instead of `sort...last`.
RUBY
expect_correction(<<~RUBY)
foo.max { |a, b| b <=> a }
RUBY
end
it 'registers an offense when first is called on sort_by' do
expect_offense(<<~RUBY)
[1, 2, 3].sort_by { |x| x.length }.first
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `min_by` instead of `sort_by...first`.
RUBY
expect_correction(<<~RUBY)
[1, 2, 3].min_by { |x| x.length }
RUBY
end
it 'registers an offense when last is called on sort_by' do
expect_offense(<<~RUBY)
foo.sort_by { |x| x.something }.last
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `max_by` instead of `sort_by...last`.
RUBY
expect_correction(<<~RUBY)
foo.max_by { |x| x.something }
RUBY
end
it 'registers an offense when first is called on sort_by no block' do
expect_offense(<<~RUBY)
[1, 2].sort_by(&:something).first
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `min_by` instead of `sort_by...first`.
RUBY
expect_correction(<<~RUBY)
[1, 2].min_by(&:something)
RUBY
end
it 'registers an offense when last is called on sort_by no block' do
expect_offense(<<~RUBY)
[1, 2, 3].sort_by(&:length).last
^^^^^^^^^^^^^^^^^^^^^^ Use `max_by` instead of `sort_by...last`.
RUBY
expect_correction(<<~RUBY)
[1, 2, 3].max_by(&:length)
RUBY
end
it 'registers an offense when at(-1) is called with sort' do
expect_offense(<<~RUBY)
[1, 2].sort.at(-1)
^^^^^^^^^^^ Use `max` instead of `sort...at(-1)`.
RUBY
expect_correction(<<~RUBY)
[1, 2].max
RUBY
end
it 'registers an offense when slice(0) is called on sort' do
expect_offense(<<~RUBY)
[1, 2, 3].sort.slice(0)
^^^^^^^^^^^^^ Use `min` instead of `sort...slice(0)`.
RUBY
expect_correction(<<~RUBY)
[1, 2, 3].min
RUBY
end
it 'registers an offense when [0] is called on sort' do
expect_offense(<<~RUBY)
[1, 2, 3].sort[0]
^^^^^^^ Use `min` instead of `sort...[0]`.
RUBY
expect_correction(<<~RUBY)
[1, 2, 3].min
RUBY
end
it 'registers an offense when [](0) is called on sort' do
expect_offense(<<~RUBY)
[1, 2, 3].sort.[](0)
^^^^^^^^^^ Use `min` instead of `sort...[](0)`.
RUBY
expect_correction(<<~RUBY)
[1, 2, 3].min
RUBY
end
it 'registers an offense when [](-1) is called on sort_by' do
expect_offense(<<~RUBY)
foo.sort_by { |x| x.foo }.[](-1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `max_by` instead of `sort_by...[](-1)`.
RUBY
expect_correction(<<~RUBY)
foo.max_by { |x| x.foo }
RUBY
end
it 'registers an offense when at(0) is called on sort_by' do
expect_offense(<<~RUBY)
[1, 2, 3].sort_by(&:foo).at(0)
^^^^^^^^^^^^^^^^^^^^ Use `min_by` instead of `sort_by...at(0)`.
RUBY
expect_correction(<<~RUBY)
[1, 2, 3].min_by(&:foo)
RUBY
end
it 'registers an offense when slice(0) is called on sort_by' do
expect_offense(<<~RUBY)
[1, 2].sort_by(&:foo).slice(0)
^^^^^^^^^^^^^^^^^^^^^^^ Use `min_by` instead of `sort_by...slice(0)`.
RUBY
expect_correction(<<~RUBY)
[1, 2].min_by(&:foo)
RUBY
end
it 'registers an offense when slice(-1) is called on sort_by' do
expect_offense(<<~RUBY)
[1, 2, 3].sort_by(&:foo).slice(-1)
^^^^^^^^^^^^^^^^^^^^^^^^ Use `max_by` instead of `sort_by...slice(-1)`.
RUBY
expect_correction(<<~RUBY)
[1, 2, 3].max_by(&:foo)
RUBY
end
it 'registers an offense when [-1] is called on sort' do
expect_offense(<<~RUBY)
[1, 2, 3].sort[-1]
^^^^^^^^ Use `max` instead of `sort...[-1]`.
RUBY
expect_correction(<<~RUBY)
[1, 2, 3].max
RUBY
end
it 'registers an offense when [0] is called on sort_by' do
expect_offense(<<~RUBY)
[1, 2].sort_by(&:foo)[0]
^^^^^^^^^^^^^^^^^ Use `min_by` instead of `sort_by...[0]`.
RUBY
expect_correction(<<~RUBY)
[1, 2].min_by(&:foo)
RUBY
end
it 'registers an offense when [-1] is called on sort_by' do
expect_offense(<<~RUBY)
foo.sort_by { |x| x.foo }[-1]
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `max_by` instead of `sort_by...[-1]`.
RUBY
expect_correction(<<~RUBY)
foo.max_by { |x| x.foo }
RUBY
end
# Arguments get too complicated to handle easily, e.g.
# '[1, 2, 3].sort.last(2)' is not equivalent to '[1, 2, 3].max(2)',
# so we don't register an offense.
it 'does not register an offense when first has an argument' do
expect_no_offenses('[1, 2, 3].sort.first(1)')
end
# Some gems like mongo provides sort method with an argument
it 'does not register an offense when sort has an argument' do
expect_no_offenses('mongo_client["users"].find.sort(_id: 1).first')
end
it 'does not register an offense for sort!.first' do
expect_no_offenses('[1, 2, 3].sort!.first')
end
it 'does not register an offense for sort_by!(&:something).last' do
expect_no_offenses('[1, 2, 3].sort_by!(&:something).last')
end
it 'does not register an offense when sort_by is used without first' do
expect_no_offenses('[1, 2, 3].sort_by { |x| -x }')
end
it 'does not register an offense when first is used without sort_by' do
expect_no_offenses('[1, 2, 3].first')
end
it 'does not register an offense when first is used before sort' do
expect_no_offenses('[[1, 2], [3, 4]].first.sort')
end
# `[3, 1, 2].sort_by(&:size).last` is equivalent to `[3, 1, 2].max_by(&:size)`.
it 'does not register an offense when using `sort_by(&:size).last`' do
expect_no_offenses('[2, 1, 3].sort_by(&:size).last')
end
# `[3, 1, 2].sort_by { |i| i.size }.last` is equivalent to `[3, 1, 2].max_by { |i| i.size }`.
it 'does not register an offense when using `sort_by { |i| i.size }.last`' do
expect_no_offenses('[2, 1, 3].sort_by { |i| i.size }.last')
end
# `[2, 1, 3].sort_by(&:size).first` is not equivalent to `[2, 1, 3].first`, but this
# cop would "correct" it to `[2, 1, 3].min_by`.
it 'does not register an offense when sort_by is not given a block' do
expect_no_offenses('[2, 1, 3].sort_by.first')
end
context 'when not taking first or last element' do
it 'does not register an offense when [1] is called on sort' do
expect_no_offenses('[1, 2, 3].sort[1]')
end
it 'does not register an offense when at(-2) is called on sort_by' do
expect_no_offenses('[1, 2, 3].sort_by(&:foo).at(-2)')
end
it 'does not register an offense when [-1] is called on sort with an argument' do
expect_no_offenses('mongo_client["users"].find.sort(_id: 1)[-1]')
end
end
context '>= Ruby 2.7', :ruby27 do
context 'when using numbered parameter' do
it 'registers an offense and corrects when last is called on sort with comparator' do
expect_offense(<<~RUBY)
foo.sort { _2 <=> _1 }.last
^^^^^^^^^^^^^^^^^^^^^^^ Use `max` instead of `sort...last`.
RUBY
expect_correction(<<~RUBY)
foo.max { _2 <=> _1 }
RUBY
end
it 'registers an offense and corrects when first is called on sort_by' do
expect_offense(<<~RUBY)
[1, 2, 3].sort_by { _1.length }.first
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `min_by` instead of `sort_by...first`.
RUBY
expect_correction(<<~RUBY)
[1, 2, 3].min_by { _1.length }
RUBY
end
it 'registers an offense and corrects when at(0) is called on sort_by' do
expect_offense(<<~RUBY)
[1, 2, 3].sort_by { _1.foo }.at(0)
^^^^^^^^^^^^^^^^^^^^^^^^ Use `min_by` instead of `sort_by...at(0)`.
RUBY
expect_correction(<<~RUBY)
[1, 2, 3].min_by { _1.foo }
RUBY
end
end
end
end