forked from rubocop/rubocop
/
out_of_range_regexp_ref_spec.rb
136 lines (117 loc) · 3.68 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
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
# 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
it 'registers an offense when the regexp comes after `=~`' do
expect_offense(<<~RUBY)
"foobar" =~ /(foo)(bar)/
puts $3
^^ Do not use out of range reference for the Regexp.
RUBY
end
it 'registers an offense when the regexp is matched with `===`' 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 the regexp is matched with `match`' do
expect_offense(<<~RUBY)
/(foo)(bar)/.match("foobar")
puts $3
^^ Do not use out of range reference for the Regexp.
RUBY
end
it 'ignores calls to `match?`' do
expect_offense(<<~RUBY)
/(foo)(bar)/.match("foobar")
/(foo)(bar)(baz)/.match?("foobarbaz")
puts $3
^^ Do not use out of range reference for the Regexp.
RUBY
end
it 'handles `match` with no arguments' do
expect_no_offenses(<<~RUBY)
foo.match
RUBY
end
it 'handles `match` with no receiver' do
expect_no_offenses(<<~RUBY)
match(bar)
RUBY
end
it 'only registers an offense when the regexp is matched as a literal' do
expect_no_offenses(<<~RUBY)
foo_bar_regexp = /(foo)(bar)/
foo_regexp = /(foo)/
foo_bar_regexp =~ 'foobar'
puts $2
RUBY
end
end