forked from rubocop/rubocop
/
slicing_with_range_spec.rb
63 lines (53 loc) · 1.42 KB
/
slicing_with_range_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::SlicingWithRange, :config do
subject(:cop) { described_class.new(config) }
context '<= Ruby 2.5', :ruby25 do
it 'reports no offense for array slicing with -1' do
expect_no_offenses(<<~RUBY)
ary[1..-1]
RUBY
end
end
context '>= Ruby 2.6', :ruby26 do
it 'reports an offense for slicing to ..-1' do
expect_offense(<<~RUBY)
ary[1..-1]
^^^^^ Prefer ary[n..] over ary[n..-1].
RUBY
expect_correction(<<~RUBY)
ary[1..]
RUBY
end
it 'reports an offense for slicing from expression to ..-1' do
expect_offense(<<~RUBY)
ary[fetch_start(true).first..-1]
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer ary[n..] over ary[n..-1].
RUBY
expect_correction(<<~RUBY)
ary[fetch_start(true).first..]
RUBY
end
it 'reports no offense for excluding end' do
expect_no_offenses(<<~RUBY)
ary[1...-1]
RUBY
end
it 'reports no offense for other methods' do
expect_no_offenses(<<~RUBY)
ary.push(1..-1)
RUBY
end
it 'reports no offense for array with range inside' do
expect_no_offenses(<<~RUBY)
ranges = [1..-1]
RUBY
end
end
context '>= Ruby 2.7', :ruby27 do
it 'reports no offense for startless' do
expect_no_offenses(<<~RUBY)
ary[..-1]
RUBY
end
end
end