forked from rubocop/rubocop
/
select_by_regexp_spec.rb
128 lines (108 loc) · 3.92 KB
/
select_by_regexp_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::SelectByRegexp, :config do
{ 'select' => 'grep', 'find_all' => 'grep', 'reject' => 'grep_v' }.each do |method, correction|
message = "Prefer `#{correction}` to `#{method}` with a regexp match."
context "with #{method}" do
it 'registers an offense and corrects for `match?`' do
expect_offense(<<~RUBY, method: method)
array.#{method} { |x| x.match? /regexp/ }
^^^^^^^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
RUBY
expect_correction(<<~RUBY)
array.#{correction}(/regexp/)
RUBY
end
it 'registers an offense and corrects for `Regexp#match?`' do
expect_offense(<<~RUBY, method: method)
array.#{method} { |x| /regexp/.match? x }
^^^^^^^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
RUBY
expect_correction(<<~RUBY)
array.#{correction}(/regexp/)
RUBY
end
it 'registers an offense and corrects for `blockvar =~ regexp`' do
expect_offense(<<~RUBY, method: method)
array.#{method} { |x| x =~ /regexp/ }
^^^^^^^{method}^^^^^^^^^^^^^^^^^^^^^^ #{message}
RUBY
expect_correction(<<~RUBY)
array.#{correction}(/regexp/)
RUBY
end
it 'registers an offense and corrects for `regexp =~ blockvar`' do
expect_offense(<<~RUBY, method: method)
array.#{method} { |x| /regexp/ =~ x }
^^^^^^^{method}^^^^^^^^^^^^^^^^^^^^^^ #{message}
RUBY
expect_correction(<<~RUBY)
array.#{correction}(/regexp/)
RUBY
end
it 'registers an offense and corrects when there is no explicit regexp' do
expect_offense(<<~RUBY, method: method)
array.#{method} { |x| x =~ y }
^^^^^^^{method}^^^^^^^^^^^^^^^ #{message}
array.#{method} { |x| x =~ REGEXP }
^^^^^^^{method}^^^^^^^^^^^^^^^^^^^^ #{message}
array.#{method} { |x| x =~ foo.bar.baz(quux) }
^^^^^^^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
RUBY
expect_correction(<<~RUBY)
array.#{correction}(y)
array.#{correction}(REGEXP)
array.#{correction}(foo.bar.baz(quux))
RUBY
end
it 'registers an offense and corrects with a multiline block' do
expect_offense(<<~RUBY, method: method)
array.#{method} do |x|
^^^^^^^{method}^^^^^^^ #{message}
x.match? /regexp/
end
RUBY
expect_correction(<<~RUBY)
array.#{correction}(/regexp/)
RUBY
end
it 'does not register an offense when there is no block' do
expect_no_offenses(<<~RUBY)
array.#{method}
RUBY
end
it 'does not register an offense when given a proc' do
expect_no_offenses(<<~RUBY)
array.#{method}(&:even?)
RUBY
end
it 'does not register an offense when the block does not match a regexp' do
expect_no_offenses(<<~RUBY)
array.#{method} { |x| x.even? }
RUBY
end
it 'does not register an offense when the block has multiple expressions' do
expect_no_offenses(<<~RUBY)
array.#{method} do |x|
next if x.even?
x.match? /regexp/
end
RUBY
end
it 'does not register an offense when the block arity is not 1' do
expect_no_offenses(<<~RUBY)
obj.#{method} { |x, y| y.match? /regexp/ }
RUBY
end
it 'does not register an offense when the block uses an external variable in a regexp match' do
expect_no_offenses(<<~RUBY)
array.#{method} { |x| y.match? /regexp/ }
RUBY
end
it 'does not register an offense when the block param is a method argument' do
expect_no_offenses(<<~RUBY)
array.#{method} { |x| /regexp/.match?(foo(x)) }
RUBY
end
end
end
end