-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
redundant_match_spec.rb
114 lines (101 loc) · 2.9 KB
/
redundant_match_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
# frozen_string_literal: true
# do_something if str.match(/regex/)
# while regex.match('str')
# do_something
# end
#
# @good
# method(str.match(/regex/))
# return regex.match('str')
RSpec.describe RuboCop::Cop::Performance::RedundantMatch, :config do
it 'autocorrects .match in if condition' do
new_source = autocorrect_source('something if str.match(/regex/)')
expect(new_source).to eq 'something if str =~ /regex/'
end
it 'autocorrects .match in unless condition' do
new_source = autocorrect_source('something unless str.match(/regex/)')
expect(new_source).to eq 'something unless str =~ /regex/'
end
it 'autocorrects .match in while condition' do
new_source = autocorrect_source(<<~RUBY)
while str.match(/regex/)
do_something
end
RUBY
expect(new_source).to eq(<<~RUBY)
while str =~ /regex/
do_something
end
RUBY
end
it 'autocorrects .match in until condition' do
new_source = autocorrect_source(<<~RUBY)
until str.match(/regex/)
do_something
end
RUBY
expect(new_source).to eq(<<~RUBY)
until str =~ /regex/
do_something
end
RUBY
end
it 'autocorrects .match in method body (but not tail position)' do
new_source = autocorrect_source(<<~RUBY)
def method(str)
str.match(/regex/)
true
end
RUBY
expect(new_source).to eq(<<~RUBY)
def method(str)
str =~ /regex/
true
end
RUBY
end
it 'does not autocorrect if .match has a string agrgument' do
new_source = autocorrect_source('something if str.match("string")')
expect(new_source).to eq 'something if str.match("string")'
end
it 'does not register an error when return value of .match is passed ' \
'to another method' do
expect_no_offenses(<<~RUBY)
def method(str)
something(str.match(/regex/))
end
RUBY
end
it 'does not register an error when return value of .match is stored in an ' \
'instance variable' do
expect_no_offenses(<<~RUBY)
def method(str)
@var = str.match(/regex/)
true
end
RUBY
end
it 'does not register an error when return value of .match is returned from surrounding method' do
expect_no_offenses(<<~RUBY)
def method(str)
str.match(/regex/)
end
RUBY
end
it 'does not register an offense when match has a block' do
expect_no_offenses(<<~RUBY)
/regex/.match(str) do |m|
something(m)
end
RUBY
end
it 'does not register an error when there is no receiver to the match call' do
expect_no_offenses('match("bar")')
end
it 'formats error message correctly for something if str.match(/regex/)' do
expect_offense(<<~RUBY)
something if str.match(/regex/)
^^^^^^^^^^^^^^^^^^ Use `=~` in places where the `MatchData` returned by `#match` will not be used.
RUBY
end
end