forked from rubocop/rubocop
/
out_of_range_regexp_ref_spec.rb
81 lines (70 loc) · 2.36 KB
/
out_of_range_regexp_ref_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::OutOfRangeRegexpRef do
subject(:cop) { described_class.new(config) }
let(:config) { RuboCop::Config.new }
it 'registers an offense when references are used before any Regexp' do
expect_offense(<<~RUBY)
puts $3
^^ Do not use out of range reference for the Regexp.
RUBY
end
it 'registers an offense when out of range references are used for named captures' do
expect_offense(<<~RUBY)
/(?<foo>FOO)(?<bar>BAR)/ =~ "FOOBAR"
puts $3
^^ Do not use out of range reference for the Regexp.
RUBY
end
it 'registers an offense when out of range references are used for numbered captures' do
expect_offense(<<~RUBY)
/(foo)(bar)/ =~ "foobar"
puts $3
^^ Do not use out of range reference for the Regexp.
RUBY
end
it 'registers an offense when out of range references are used for mix of numbered and named captures' do
expect_offense(<<~RUBY)
/(?<foo>FOO)(BAR)/ =~ "FOOBAR"
puts $2
^^ Do not use out of range reference for the Regexp.
RUBY
end
it 'registers an offense when out of range references are used for non captures' do
expect_offense(<<~RUBY)
/bar/ =~ 'foo'
puts $1
^^ Do not use out of range reference for the Regexp.
RUBY
end
it 'does not register offense to a regexp with valid references for named captures' do
expect_no_offenses(<<~RUBY)
/(?<foo>FOO)(?<bar>BAR)/ =~ "FOOBAR"
puts $1
puts $2
RUBY
end
it 'does not register offense to a regexp with valid references for numbered captures' do
expect_no_offenses(<<~RUBY)
/(foo)(bar)/ =~ "foobar"
puts $1
puts $2
RUBY
end
it 'does not register offense to a regexp with valid references for a mix named and numbered captures' do
expect_no_offenses(<<~RUBY)
/(?<foo>FOO)(BAR)/ =~ "FOOBAR"
puts $1
RUBY
end
# RuboCop does not know a value of variables that it will contain in the regexp literal.
# For example, `/(?<foo>#{var}*)` is interpreted as `/(?<foo>*)`.
# So it does not offense when variables are used in regexp literals.
it 'does not register an offence Regexp containing non literal' do
expect_no_offenses(<<~'RUBY')
var = '(\d+)'
/(?<foo>#{var}*)/ =~ "12"
puts $1
puts $2
RUBY
end
end