/
sort_reverse_spec.rb
62 lines (52 loc) · 1.65 KB
/
sort_reverse_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Performance::SortReverse, :config do
let(:config) do
# Suppress ChainArrayAllocation offenses
RuboCop::Config.new('Performance/ChainArrayAllocation' => { 'Enabled' => false })
end
it 'registers an offense and corrects when sorting in reverse order' do
expect_offense(<<~RUBY)
array.sort { |a, b| b <=> a }
^^^^^^^^^^^^^^^^^^^^^^^ Use `sort.reverse` instead.
RUBY
expect_correction(<<~RUBY)
array.sort.reverse
RUBY
end
context 'when using numbered parameter', :ruby27 do
it 'registers an offense and corrects when sorting in reverse order' do
expect_offense(<<~RUBY)
array.sort { _2 <=> _1 }
^^^^^^^^^^^^^^^^^^ Use `sort.reverse` instead.
RUBY
expect_correction(<<~RUBY)
array.sort.reverse
RUBY
end
it 'does not register an offense when sorting in direct order' do
expect_no_offenses(<<~RUBY)
array.sort { _1 <=> _2 }
RUBY
end
it 'does not register an offense when sorting in reverse order by some property' do
expect_no_offenses(<<~RUBY)
array.sort { _2.x <=> _1.x }
RUBY
end
end
it 'does not register an offense when sorting in direct order' do
expect_no_offenses(<<~RUBY)
array.sort { |a, b| a <=> b }
RUBY
end
it 'does not register an offense when sorting in reverse order by some property' do
expect_no_offenses(<<~RUBY)
array.sort { |a, b| b.x <=> a.x }
RUBY
end
it 'does not register an offense when using `sort.reverse`' do
expect_no_offenses(<<~RUBY)
array.sort.reverse
RUBY
end
end