forked from rubocop/rubocop
/
duplicate_regexp_character_class_element_spec.rb
125 lines (107 loc) · 3.24 KB
/
duplicate_regexp_character_class_element_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::DuplicateRegexpCharacterClassElement do
subject(:cop) { described_class.new }
context 'with a repeated character class element' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
foo = /[xyx]/
^ Duplicate element inside regexp character class
RUBY
expect_correction(<<~RUBY)
foo = /[xy]/
RUBY
end
end
context 'with no repeated character class elements' do
it 'registers an offense and corrects' do
expect_no_offenses(<<~RUBY)
foo = /[xyz]/
RUBY
end
end
context 'with repeated elements in different character classes' do
it 'registers an offense and corrects' do
expect_no_offenses(<<~RUBY)
foo = /[xyz][xyz]/
RUBY
end
end
context 'with a repeated character class element and %r{} literal' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
foo = %r{[xyx]}
^ Duplicate element inside regexp character class
RUBY
expect_correction(<<~RUBY)
foo = %r{[xy]}
RUBY
end
end
context 'with a repeated character class element inside a group' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
foo = /([xyx])/
^ Duplicate element inside regexp character class
RUBY
expect_correction(<<~RUBY)
foo = /([xy])/
RUBY
end
end
context 'with a repeated character posix character class inside a group' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
foo = /([[:alnum:]y[:alnum:]])/
^^^^^^^^^ Duplicate element inside regexp character class
RUBY
expect_correction(<<~RUBY)
foo = /([[:alnum:]y])/
RUBY
end
end
context 'with a repeated character class element with interpolation' do
it 'registers an offense and corrects' do
expect_offense(<<~'RUBY')
foo = /([a#{foo}a#{bar}a])/
^ Duplicate element inside regexp character class
^ Duplicate element inside regexp character class
RUBY
expect_correction(<<~'RUBY')
foo = /([a#{foo}#{bar}])/
RUBY
end
end
context 'with a repeated range element' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
foo = /[0-9x0-9]/
^^^ Duplicate element inside regexp character class
RUBY
expect_correction(<<~RUBY)
foo = /[0-9x]/
RUBY
end
end
context 'with a repeated intersection character class' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
foo = /[ab&&ab]/
RUBY
end
end
context 'with a range that covers a repeated element character class' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
foo = /[a-cb]/
RUBY
end
end
context 'with multiple regexps with the same interpolation' do
it 'does not register an offense' do
expect_no_offenses(<<~'RUBY')
a_field.gsub!(/[#{bad_chars}]/, '')
some_other_field.gsub!(/[#{bad_chars}]/, '')
RUBY
end
end
end