Skip to content

Commit

Permalink
Update examples in specs
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalinavlakhe committed Aug 5, 2020
1 parent 6984914 commit 29d162f
Showing 1 changed file with 15 additions and 56 deletions.
71 changes: 15 additions & 56 deletions spec/rubocop/cop/lint/out_of_range_regexp_ref_spec.rb
Expand Up @@ -5,6 +5,13 @@

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"
Expand Down Expand Up @@ -64,67 +71,19 @@
it 'does not register offense when using a Regexp cannot be processed by regexp_parser gem' do
expect_no_offenses(<<~'RUBY')
/data = ({"words":.+}}}[^}]*})/m
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.
context 'when containing a non-regexp literal' do
it 'does not register an offence when containing a lvar' do
expect_no_offenses(<<~'RUBY')
var = '(\d+)'
/(?<foo>#{var}*)/ =~ "12"
puts $1
puts $2
RUBY
end

it 'does not register an offence when containing a ivar' do
expect_no_offenses(<<~'RUBY')
@var = '(\d+)'
/(?<foo>#{@var}*)/ =~ "12"
puts $1
puts $3
RUBY
end

it 'does not register an offence when containing a cvar' do
expect_no_offenses(<<~'RUBY')
@@var = '(\d+)'
/(?<foo>#{@@var}*)/ =~ "12"
puts $1
puts $4
RUBY
end

it 'does not register an offence when containing a gvar' do
expect_no_offenses(<<~'RUBY')
$var = '(\d+)'
/(?<foo>#{$var}*)/ =~ "12"
puts $1
puts $2
RUBY
end

it 'does not register an offence when containing a method' do
expect_no_offenses(<<~'RUBY')
def do_something
'(\d+)'
end
/(?<foo>#{do_something}*)/ =~ "12"
puts $1
puts $4
RUBY
end

it 'does not register an offence when containing a constant' do
expect_no_offenses(<<~'RUBY')
CONST = "12"
/(?<foo>#{CONST}*)/ =~ "12"
puts $1
puts $3
RUBY
end
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

0 comments on commit 29d162f

Please sign in to comment.