forked from rubocop/rubocop
/
redundant_sort_spec.rb
219 lines (173 loc) · 6.78 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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::RedundantSort do
subject(:cop) { described_class.new }
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
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
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
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
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
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
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
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
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
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
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
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
# `[2, 1, 3].sort_by.first` is 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
end
context 'autocorrect' do
it 'corrects sort.first to min' do
new_source = autocorrect_source('[1, 2].sort.first')
expect(new_source).to eq('[1, 2].min')
end
it 'corrects sort.last to max' do
new_source = autocorrect_source('[1, 2].sort.last')
expect(new_source).to eq('[1, 2].max')
end
it 'corrects sort.first (with comparator) to min' do
new_source = autocorrect_source('[1, 2].sort { |a, b| b <=> a }.first')
expect(new_source).to eq('[1, 2].min { |a, b| b <=> a }')
end
it 'corrects sort.at(-1) to max' do
new_source = autocorrect_source('[1, 2].sort.at(-1)')
expect(new_source).to eq('[1, 2].max')
end
it 'corrects sort_by(&:foo).slice(0) to min_by(&:foo)' do
new_source = autocorrect_source('[1, 2].sort_by(&:foo).slice(0)')
expect(new_source).to eq('[1, 2].min_by(&:foo)')
end
it 'corrects sort_by(&:foo)[0] to min_by(&:foo)' do
new_source = autocorrect_source('[1, 2].sort_by(&:foo)[0]')
expect(new_source).to eq('[1, 2].min_by(&:foo)')
end
it 'corrects sort_by(&:something).first to min_by(&:something)' do
new_source = autocorrect_source('[1, 2].sort_by(&:something).first')
expect(new_source).to eq('[1, 2].min_by(&:something)')
end
it 'corrects sort_by { |x| x.foo }[-1] to max_by { |x| x.foo }' do
new_source = autocorrect_source('foo.sort_by { |x| x.foo }[-1]')
expect(new_source).to eq('foo.max_by { |x| x.foo }')
end
it 'corrects sort_by { |x| x.foo }.[](-1) to max_by { |x| x.foo }' do
new_source = autocorrect_source('foo.sort_by { |x| x.foo }.[](-1)')
expect(new_source).to eq('foo.max_by { |x| x.foo }')
end
it 'corrects sort_by { |x| x.something }.last ' \
'to max_by { |x| x.something }' do
new_source = autocorrect_source('foo.sort_by { |x| x.something }.last')
expect(new_source).to eq('foo.max_by { |x| x.something }')
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