forked from rubocop/rubocop-performance
/
redundant_sort_block_spec.rb
57 lines (48 loc) · 1.47 KB
/
redundant_sort_block_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Performance::RedundantSortBlock, :config do
it 'registers an offense and corrects when sorting in direct order' do
expect_offense(<<~RUBY)
array.sort { |a, b| a <=> b }
^^^^^^^^^^^^^^^^^^^^^^^ Use `sort` without block.
RUBY
expect_correction(<<~RUBY)
array.sort
RUBY
end
it 'does not register an offense when sorting in reverse order' do
expect_no_offenses(<<~RUBY)
array.sort { |a, b| b <=> a }
RUBY
end
it 'does not register an offense when sorting in direct order by some property' do
expect_no_offenses(<<~RUBY)
array.sort { |a, b| a.x <=> b.x }
RUBY
end
it 'does not register an offense when using `sort`' do
expect_no_offenses(<<~RUBY)
array.sort
RUBY
end
context 'when using numbered parameter', :ruby27 do
it 'registers an offense and corrects when sorting in direct order' do
expect_offense(<<~RUBY)
array.sort { _1 <=> _2 }
^^^^^^^^^^^^^^^^^^ Use `sort` without block.
RUBY
expect_correction(<<~RUBY)
array.sort
RUBY
end
it 'does not register an offense when sorting in reverse order' do
expect_no_offenses(<<~RUBY)
array.sort { _2 <=> _1 }
RUBY
end
it 'does not register an offense when sorting in direct order by some property' do
expect_no_offenses(<<~RUBY)
array.sort { _1.x <=> _2.x }
RUBY
end
end
end