forked from rubocop/rubocop-ast
/
regexp_node_spec.rb
163 lines (114 loc) · 3.98 KB
/
regexp_node_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
# frozen_string_literal: true
RSpec.describe RuboCop::AST::RegexpNode do
let(:regexp_node) { parse_source(source).ast }
describe '.new' do
let(:source) { '/re/' }
it { expect(regexp_node.is_a?(described_class)).to be(true) }
end
describe '#to_regexp' do
# rubocop:disable Security/Eval
context 'with an empty regexp' do
let(:source) { '//' }
it { expect(regexp_node.to_regexp).to eq(eval(source)) }
end
context 'with a regexp without option' do
let(:source) { '/.+/' }
it { expect(regexp_node.to_regexp).to eq(eval(source)) }
end
context 'with a multi-line regexp without option' do
let(:source) { "/\n.+\n/" }
it { expect(regexp_node.to_regexp).to eq(eval(source)) }
end
context 'with an empty regexp with option' do
let(:source) { '//ix' }
it { expect(regexp_node.to_regexp).to eq(eval(source)) }
end
context 'with a regexp with option' do
let(:source) { '/.+/imx' }
it { expect(regexp_node.to_regexp).to eq(eval(source)) }
end
context 'with a multi-line regexp with option' do
let(:source) { "/\n.+\n/ix" }
it { expect(regexp_node.to_regexp).to eq(eval(source)) }
end
# rubocop:enable Security/Eval
context 'with a regexp with an "o" option' do
let(:source) { '/abc/io' }
it { expect(regexp_node.to_regexp.inspect).to eq('/abc/i') }
end
end
describe '#regopt' do
let(:regopt) { regexp_node.regopt }
context 'with an empty regexp' do
let(:source) { '//' }
it { expect(regopt.regopt_type?).to be(true) }
it { expect(regopt.children.empty?).to be(true) }
end
context 'with a regexp without option' do
let(:source) { '/.+/' }
it { expect(regopt.regopt_type?).to be(true) }
it { expect(regopt.children.empty?).to be(true) }
end
context 'with a multi-line regexp without option' do
let(:source) { "/\n.+\n/" }
it { expect(regopt.regopt_type?).to be(true) }
it { expect(regopt.children.empty?).to be(true) }
end
context 'with an empty regexp with option' do
let(:source) { '//ix' }
it { expect(regopt.regopt_type?).to be(true) }
it { expect(regopt.children).to eq(%i[i x]) }
end
context 'with a regexp with option' do
let(:source) { '/.+/imx' }
it { expect(regopt.regopt_type?).to be(true) }
it { expect(regopt.children).to eq(%i[i m x]) }
end
context 'with a multi-line regexp with option' do
let(:source) { "/\n.+\n/imx" }
it { expect(regopt.regopt_type?).to be(true) }
it { expect(regopt.children).to eq(%i[i m x]) }
end
end
describe '#content' do
let(:content) { regexp_node.content }
context 'with an empty regexp' do
let(:source) { '//' }
it { expect(content).to eq('') }
end
context 'with a regexp without option' do
let(:source) { '/.+/' }
it { expect(content).to eq('.+') }
end
context 'with a multi-line regexp without option' do
let(:source) { "/\n.+\n/" }
it { expect(content).to eq("\n.+\n") }
end
context 'with an empty regexp with option' do
let(:source) { '//ix' }
it { expect(content).to eq('') }
end
context 'with a regexp with option' do
let(:source) { '/.+/imx' }
it { expect(content).to eq('.+') }
end
context 'with a multi-line regexp with option' do
let(:source) { "/\n.+\n/imx" }
it { expect(content).to eq("\n.+\n") }
end
end
describe '#has_interpolation?' do
context 'with direct variable interpoation' do
let(:source) { '/\n\n#{foo}(abc)+/' }
it { expect(regexp_node.interpolation?).to eq(true) }
end
context 'with regexp quote' do
let(:source) { '/\n\n#{Regexp.quote(foo)}(abc)+/' }
it { expect(regexp_node.interpolation?).to eq(true) }
end
context 'with no interpolation returns false' do
let(:source) { '/a{3,6}/' }
it { expect(regexp_node.interpolation?).to eq(false) }
end
end
end