/
redundant_match_spec.rb
177 lines (151 loc) · 5.18 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# 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
it 'registers an offense and corrects when receiver is a Regexp literal' do
expect_offense(<<~RUBY)
something if /regex/.match(str)
^^^^^^^^^^^^^^^^^^ Use `=~` in places where the `MatchData` returned by `#match` will not be used.
RUBY
expect_correction(<<~RUBY)
something if /regex/ =~ str
RUBY
end
shared_examples 'require parentheses' do |arg|
it "registers an offense and corrects when argument is `#{arg}`" do
expect_offense(<<~RUBY, arg: arg)
something if /regex/.match(%{arg})
^^^^^^^^^^^^^^^{arg}^ Use `=~` in places where the `MatchData` returned by `#match` will not be used.
RUBY
expect_correction(<<~RUBY)
something if /regex/ =~ (#{arg})
RUBY
end
end
it_behaves_like 'require parentheses', 'a ? b : c'
it_behaves_like 'require parentheses', 'a && b'
it_behaves_like 'require parentheses', 'a || b'
it_behaves_like 'require parentheses', 'a..b'
it_behaves_like 'require parentheses', 'method a'
context 'when Ruby <= 3.2', :ruby32, unsupported_on: :prism do # rubocop:disable RSpec/RepeatedExampleGroupDescription
it_behaves_like 'require parentheses', 'yield a'
end
it_behaves_like 'require parentheses', 'super a'
it_behaves_like 'require parentheses', 'a == b'
shared_examples 'require no parentheses' do |arg|
it "registers an offense and corrects when argument is `#{arg}`" do
expect_offense(<<~RUBY, arg: arg)
something if /regex/.match(%{arg})
^^^^^^^^^^^^^^^{arg}^ Use `=~` in places where the `MatchData` returned by `#match` will not be used.
RUBY
expect_correction(<<~RUBY)
something if /regex/ =~ #{arg}
RUBY
end
end
it_behaves_like 'require no parentheses', 'if a then b else c end'
it_behaves_like 'require no parentheses', 'method(a)'
it_behaves_like 'require no parentheses', 'method'
context 'when Ruby <= 3.2', :ruby32, unsupported_on: :prism do # rubocop:disable RSpec/RepeatedExampleGroupDescription
it_behaves_like 'require no parentheses', 'yield'
end
it_behaves_like 'require no parentheses', 'super'
it_behaves_like 'require no parentheses', 'a.==(b)'
%w[| ^ & + - * / % ** > >= < <= << >>].each do |op|
it_behaves_like 'require no parentheses', "a #{op} b"
end
end