forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
regexp_as_condition_spec.rb
48 lines (41 loc) · 1.18 KB
/
regexp_as_condition_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::RegexpAsCondition, :config do
it 'registers an offense and corrects for a regexp literal in `if` condition' do
expect_offense(<<~RUBY)
if /foo/
^^^^^ Do not use regexp literal as a condition. The regexp literal matches `$_` implicitly.
end
RUBY
expect_correction(<<~RUBY)
if /foo/ =~ $_
end
RUBY
end
it 'registers an offense and corrects for a regexp literal with bang in `if` condition' do
expect_offense(<<~RUBY)
if !/foo/
^^^^^ Do not use regexp literal as a condition. The regexp literal matches `$_` implicitly.
end
RUBY
expect_correction(<<~RUBY)
if !/foo/ =~ $_
end
RUBY
end
it 'does not register an offense for a regexp literal outside conditions' do
expect_no_offenses(<<~RUBY)
/foo/
RUBY
end
it 'does not register an offense for a regexp literal with bang outside conditions' do
expect_no_offenses(<<~RUBY)
!/foo/
RUBY
end
it 'does not register an offense for a regexp literal with `=~` operator' do
expect_no_offenses(<<~RUBY)
if /foo/ =~ str
end
RUBY
end
end