/
perl_backrefs_spec.rb
167 lines (137 loc) · 3.59 KB
/
perl_backrefs_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::PerlBackrefs, :config do
it 'autocorrects puts $1 to puts Regexp.last_match(1)' do
expect_offense(<<~RUBY)
puts $1
^^ Prefer `Regexp.last_match(1)` over `$1`.
RUBY
expect_correction(<<~RUBY)
puts Regexp.last_match(1)
RUBY
end
it 'autocorrects $9 to Regexp.last_match(9)' do
expect_offense(<<~RUBY)
$9
^^ Prefer `Regexp.last_match(9)` over `$9`.
RUBY
expect_correction(<<~RUBY)
Regexp.last_match(9)
RUBY
end
it 'autocorrects $& to Regexp.last_match(0)' do
expect_offense(<<~RUBY)
$&
^^ Prefer `Regexp.last_match(0)` over `$&`.
RUBY
expect_correction(<<~RUBY)
Regexp.last_match(0)
RUBY
end
it 'autocorrects $` to Regexp.last_match.pre_match' do
expect_offense(<<~RUBY)
$`
^^ Prefer `Regexp.last_match.pre_match` over `$``.
RUBY
expect_correction(<<~RUBY)
Regexp.last_match.pre_match
RUBY
end
it 'autocorrects $\' to Regexp.last_match.post_match' do
expect_offense(<<~RUBY)
$'
^^ Prefer `Regexp.last_match.post_match` over `$'`.
RUBY
expect_correction(<<~RUBY)
Regexp.last_match.post_match
RUBY
end
it 'autocorrects $+ to Regexp.last_match(-1)' do
expect_offense(<<~RUBY)
$+
^^ Prefer `Regexp.last_match(-1)` over `$+`.
RUBY
expect_correction(<<~RUBY)
Regexp.last_match(-1)
RUBY
end
it 'autocorrects $MATCH to Regexp.last_match(0)' do
expect_offense(<<~RUBY)
$MATCH
^^^^^^ Prefer `Regexp.last_match(0)` over `$MATCH`.
RUBY
expect_correction(<<~RUBY)
Regexp.last_match(0)
RUBY
end
it 'autocorrects $PREMATCH to Regexp.last_match.pre_match' do
expect_offense(<<~RUBY)
$PREMATCH
^^^^^^^^^ Prefer `Regexp.last_match.pre_match` over `$PREMATCH`.
RUBY
expect_correction(<<~RUBY)
Regexp.last_match.pre_match
RUBY
end
it 'autocorrects $POSTMATCH to Regexp.last_match.post_match' do
expect_offense(<<~RUBY)
$POSTMATCH
^^^^^^^^^^ Prefer `Regexp.last_match.post_match` over `$POSTMATCH`.
RUBY
expect_correction(<<~RUBY)
Regexp.last_match.post_match
RUBY
end
it 'autocorrects $LAST_PAREN_MATCH to Regexp.last_match(-1)' do
expect_offense(<<~RUBY)
$LAST_PAREN_MATCH
^^^^^^^^^^^^^^^^^ Prefer `Regexp.last_match(-1)` over `$LAST_PAREN_MATCH`.
RUBY
expect_correction(<<~RUBY)
Regexp.last_match(-1)
RUBY
end
it 'autocorrects "#$1" to "#{Regexp.last_match(1)}"' do
expect_offense(<<~'RUBY')
"#$1"
^^ Prefer `Regexp.last_match(1)` over `$1`.
RUBY
expect_correction(<<~'RUBY')
"#{Regexp.last_match(1)}"
RUBY
end
it 'autocorrects `#$1` to `#{Regexp.last_match(1)}`' do
expect_offense(<<~'RUBY')
`#$1`
^^ Prefer `Regexp.last_match(1)` over `$1`.
RUBY
expect_correction(<<~'RUBY')
`#{Regexp.last_match(1)}`
RUBY
end
it 'autocorrects /#$1/ to /#{Regexp.last_match(1)}/' do
expect_offense(<<~'RUBY')
/#$1/
^^ Prefer `Regexp.last_match(1)` over `$1`.
RUBY
expect_correction(<<~'RUBY')
/#{Regexp.last_match(1)}/
RUBY
end
it 'autocorrects $1 to ::Regexp.last_match(1) in namespace' do
expect_offense(<<~RUBY)
module Foo
class Regexp
end
puts $1
^^ Prefer `::Regexp.last_match(1)` over `$1`.
end
RUBY
expect_correction(<<~RUBY)
module Foo
class Regexp
end
puts ::Regexp.last_match(1)
end
RUBY
end
end