blob: 384a834a512f520fd585c272bc74865b34983a78 (
plain)
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
|
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../../rubocop/cop/style/regexp_literal_mixed_preserve'
# This spec contains only relevant examples.
#
# See also https://github.com/rubocop/rubocop/pull/9688
RSpec.describe RuboCop::Cop::Style::RegexpLiteralMixedPreserve, :config do
let(:config) do
supported_styles = { 'SupportedStyles' => %w[slashes percent_r mixed mixed_preserve] }
RuboCop::Config.new('Style/PercentLiteralDelimiters' =>
percent_literal_delimiters_config,
'Style/RegexpLiteralMixedPreserve' =>
cop_config.merge(supported_styles))
end
let(:percent_literal_delimiters_config) { { 'PreferredDelimiters' => { '%r' => '{}' } } }
context 'when EnforcedStyle is set to mixed_preserve' do
let(:cop_config) { { 'EnforcedStyle' => 'mixed_preserve' } }
describe 'a single-line `//` regex without slashes' do
it 'is accepted' do
expect_no_offenses('foo = /a/')
end
end
describe 'a single-line `//` regex with slashes' do
it 'registers an offense and corrects' do
expect_offense(<<~'RUBY')
foo = /home\//
^^^^^^^^ Use `%r` around regular expression.
RUBY
expect_correction(<<~'RUBY')
foo = %r{home/}
RUBY
end
describe 'when configured to allow inner slashes' do
before do
cop_config['AllowInnerSlashes'] = true
end
it 'is accepted' do
expect_no_offenses('foo = /home\\//')
end
end
end
describe 'a multi-line `//` regex without slashes' do
it 'is accepted' do
expect_no_offenses(<<~'RUBY')
foo = /
foo
bar
/x
RUBY
end
end
describe 'a multi-line `//` regex with slashes' do
it 'registers an offense and corrects' do
expect_offense(<<~'RUBY')
foo = /
^ Use `%r` around regular expression.
https?:\/\/
example\.com
/x
RUBY
expect_correction(<<~'RUBY')
foo = %r{
https?://
example\.com
}x
RUBY
end
end
describe 'a single-line %r regex without slashes' do
it 'is accepted' do
expect_no_offenses(<<~RUBY)
foo = %r{a}
RUBY
end
end
describe 'a single-line %r regex with slashes' do
it 'is accepted' do
expect_no_offenses('foo = %r{home/}')
end
describe 'when configured to allow inner slashes' do
before do
cop_config['AllowInnerSlashes'] = true
end
it 'is accepted' do
expect_no_offenses(<<~RUBY)
foo = %r{home/}
RUBY
end
end
end
describe 'a multi-line %r regex without slashes' do
it 'is accepted' do
expect_no_offenses(<<~RUBY)
foo = %r{
foo
bar
}x
RUBY
end
end
describe 'a multi-line %r regex with slashes' do
it 'is accepted' do
expect_no_offenses(<<~RUBY)
foo = %r{
https?://
example\.com
}x
RUBY
end
end
end
end
|