re2 0.7.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/README.md +35 -19
- data/Rakefile +4 -9
- data/ext/re2/extconf.rb +62 -19
- data/ext/re2/re2.cc +55 -16
- data/spec/kernel_spec.rb +4 -6
- data/spec/re2/match_data_spec.rb +88 -52
- data/spec/re2/regexp_spec.rb +94 -96
- data/spec/re2/scanner_spec.rb +128 -20
- data/spec/re2/string_spec.rb +12 -13
- data/spec/re2_spec.rb +15 -19
- data/spec/spec_helper.rb +18 -2
- metadata +12 -14
data/spec/kernel_spec.rb
CHANGED
@@ -1,15 +1,13 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
describe Kernel do
|
1
|
+
RSpec.describe Kernel do
|
4
2
|
describe "#RE2" do
|
5
3
|
it "returns an RE2::Regexp instance given a pattern" do
|
6
|
-
RE2('w(o)(o)').
|
4
|
+
expect(RE2('w(o)(o)')).to be_a(RE2::Regexp)
|
7
5
|
end
|
8
6
|
|
9
7
|
it "returns an RE2::Regexp instance given a pattern and options" do
|
10
8
|
re = RE2('w(o)(o)', :case_sensitive => false)
|
11
|
-
re.
|
12
|
-
re.
|
9
|
+
expect(re).to be_a(RE2::Regexp)
|
10
|
+
expect(re).to_not be_case_sensitive
|
13
11
|
end
|
14
12
|
end
|
15
13
|
end
|
data/spec/re2/match_data_spec.rb
CHANGED
@@ -1,94 +1,90 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
describe RE2::MatchData do
|
6
|
-
|
2
|
+
RSpec.describe RE2::MatchData do
|
7
3
|
describe "#to_a" do
|
8
4
|
it "is populated with the match and capturing groups" do
|
9
5
|
a = RE2::Regexp.new('w(o)(o)').match('woo').to_a
|
10
|
-
a.
|
6
|
+
expect(a).to eq(["woo", "o", "o"])
|
11
7
|
end
|
12
8
|
|
13
9
|
it "populates optional capturing groups with nil if they are missing" do
|
14
10
|
a = RE2::Regexp.new('(\d?)(a)(b)').match('ab').to_a
|
15
|
-
a.
|
11
|
+
expect(a).to eq(["ab", nil, "a", "b"])
|
16
12
|
end
|
17
13
|
end
|
18
14
|
|
19
15
|
describe "#[]" do
|
20
16
|
it "accesses capturing groups by numerical index" do
|
21
17
|
md = RE2::Regexp.new('(\d)(\d{2})').match("123")
|
22
|
-
md[1].
|
23
|
-
md[2].
|
18
|
+
expect(md[1]).to eq("1")
|
19
|
+
expect(md[2]).to eq("23")
|
24
20
|
end
|
25
21
|
|
26
22
|
it "has the whole match as the 0th item" do
|
27
23
|
md = RE2::Regexp.new('(\d)(\d{2})').match("123")
|
28
|
-
md[0].
|
24
|
+
expect(md[0]).to eq("123")
|
29
25
|
end
|
30
26
|
|
31
27
|
it "supports access by numerical ranges" do
|
32
28
|
md = RE2::Regexp.new('(\d+) (\d+) (\d+)').match("123 456 789")
|
33
|
-
md[1..3].
|
34
|
-
md[1...3].
|
29
|
+
expect(md[1..3]).to eq(["123", "456", "789"])
|
30
|
+
expect(md[1...3]).to eq(["123", "456"])
|
35
31
|
end
|
36
32
|
|
37
33
|
it "supports slicing" do
|
38
34
|
md = RE2::Regexp.new('(\d+) (\d+) (\d+)').match("123 456 789")
|
39
|
-
md[1, 3].
|
40
|
-
md[1, 2].
|
35
|
+
expect(md[1, 3]).to eq(["123", "456", "789"])
|
36
|
+
expect(md[1, 2]).to eq(["123", "456"])
|
41
37
|
end
|
42
38
|
|
43
39
|
it "returns nil if attempting to access non-existent capturing groups by index" do
|
44
40
|
md = RE2::Regexp.new('(\d+)').match('bob 123')
|
45
|
-
md[2].
|
46
|
-
md[3].
|
41
|
+
expect(md[2]).to be_nil
|
42
|
+
expect(md[3]).to be_nil
|
47
43
|
end
|
48
44
|
|
49
45
|
it "allows access by string names when there are named groups" do
|
50
46
|
md = RE2::Regexp.new('(?P<numbers>\d+)').match('bob 123')
|
51
|
-
md["numbers"].
|
47
|
+
expect(md["numbers"]).to eq("123")
|
52
48
|
end
|
53
49
|
|
54
50
|
it "allows access by symbol names when there are named groups" do
|
55
51
|
md = RE2::Regexp.new('(?P<numbers>\d+)').match('bob 123')
|
56
|
-
md[:numbers].
|
52
|
+
expect(md[:numbers]).to eq("123")
|
57
53
|
end
|
58
54
|
|
59
55
|
it "allows access by names and indices with mixed groups" do
|
60
56
|
md = RE2::Regexp.new('(?P<name>\w+)(\s*)(?P<numbers>\d+)').match("bob 123")
|
61
|
-
md["name"].
|
62
|
-
md[:name].
|
63
|
-
md[2].
|
64
|
-
md["numbers"].
|
65
|
-
md[:numbers].
|
57
|
+
expect(md["name"]).to eq("bob")
|
58
|
+
expect(md[:name]).to eq("bob")
|
59
|
+
expect(md[2]).to eq(" ")
|
60
|
+
expect(md["numbers"]).to eq("123")
|
61
|
+
expect(md[:numbers]).to eq("123")
|
66
62
|
end
|
67
63
|
|
68
64
|
it "returns nil if no such named group exists" do
|
69
65
|
md = RE2::Regexp.new('(\d+)').match("bob 123")
|
70
|
-
md["missing"].
|
71
|
-
md[:missing].
|
66
|
+
expect(md["missing"]).to be_nil
|
67
|
+
expect(md[:missing]).to be_nil
|
72
68
|
end
|
73
69
|
|
74
70
|
it "raises an error if given an inappropriate index" do
|
75
71
|
md = RE2::Regexp.new('(\d+)').match("bob 123")
|
76
|
-
|
72
|
+
expect { md[nil] }.to raise_error(TypeError)
|
77
73
|
end
|
78
74
|
|
79
75
|
if String.method_defined?(:encoding)
|
80
76
|
it "returns UTF-8 encoded strings by default" do
|
81
77
|
md = RE2::Regexp.new('(?P<name>\S+)').match("bob")
|
82
|
-
md[0].encoding.name.
|
83
|
-
md["name"].encoding.name.
|
84
|
-
md[:name].encoding.name.
|
78
|
+
expect(md[0].encoding.name).to eq("UTF-8")
|
79
|
+
expect(md["name"].encoding.name).to eq("UTF-8")
|
80
|
+
expect(md[:name].encoding.name).to eq("UTF-8")
|
85
81
|
end
|
86
82
|
|
87
83
|
it "returns Latin 1 strings encoding when utf-8 is false" do
|
88
84
|
md = RE2::Regexp.new('(?P<name>\S+)', :utf8 => false).match('bob')
|
89
|
-
md[0].encoding.name.
|
90
|
-
md["name"].encoding.name.
|
91
|
-
md[:name].encoding.name.
|
85
|
+
expect(md[0].encoding.name).to eq("ISO-8859-1")
|
86
|
+
expect(md["name"].encoding.name).to eq("ISO-8859-1")
|
87
|
+
expect(md[:name].encoding.name).to eq("ISO-8859-1")
|
92
88
|
end
|
93
89
|
end
|
94
90
|
end
|
@@ -96,32 +92,32 @@ describe RE2::MatchData do
|
|
96
92
|
describe "#string" do
|
97
93
|
it "returns the original string to match against" do
|
98
94
|
re = RE2::Regexp.new('(\D+)').match("bob")
|
99
|
-
re.string.
|
95
|
+
expect(re.string).to eq("bob")
|
100
96
|
end
|
101
97
|
|
102
98
|
it "returns a copy, not the actual original" do
|
103
99
|
string = "bob"
|
104
100
|
re = RE2::Regexp.new('(\D+)').match(string)
|
105
|
-
re.string.
|
101
|
+
expect(re.string).to_not equal(string)
|
106
102
|
end
|
107
103
|
|
108
104
|
it "returns a frozen string" do
|
109
105
|
re = RE2::Regexp.new('(\D+)').match("bob")
|
110
|
-
re.string.
|
106
|
+
expect(re.string).to be_frozen
|
111
107
|
end
|
112
108
|
end
|
113
109
|
|
114
110
|
describe "#size" do
|
115
111
|
it "returns the number of capturing groups plus the matching string" do
|
116
112
|
md = RE2::Regexp.new('(\d+) (\d+)').match("1234 56")
|
117
|
-
md.size.
|
113
|
+
expect(md.size).to eq(3)
|
118
114
|
end
|
119
115
|
end
|
120
116
|
|
121
117
|
describe "#length" do
|
122
118
|
it "returns the number of capturing groups plus the matching string" do
|
123
119
|
md = RE2::Regexp.new('(\d+) (\d+)').match("1234 56")
|
124
|
-
md.length.
|
120
|
+
expect(md.length).to eq(3)
|
125
121
|
end
|
126
122
|
end
|
127
123
|
|
@@ -129,26 +125,26 @@ describe RE2::MatchData do
|
|
129
125
|
it "returns the original RE2::Regexp used" do
|
130
126
|
re = RE2::Regexp.new('(\d+)')
|
131
127
|
md = re.match("123")
|
132
|
-
md.regexp.
|
128
|
+
expect(md.regexp).to equal(re)
|
133
129
|
end
|
134
130
|
end
|
135
131
|
|
136
132
|
describe "#inspect" do
|
137
133
|
it "returns a text representation of the object and indices" do
|
138
134
|
md = RE2::Regexp.new('(\d+) (\d+)').match("1234 56")
|
139
|
-
md.inspect.
|
135
|
+
expect(md.inspect).to eq('#<RE2::MatchData "1234 56" 1:"1234" 2:"56">')
|
140
136
|
end
|
141
137
|
|
142
138
|
it "represents missing matches as nil" do
|
143
139
|
md = RE2::Regexp.new('(\d+) (\d+)?').match("1234 ")
|
144
|
-
md.inspect.
|
140
|
+
expect(md.inspect).to eq('#<RE2::MatchData "1234 " 1:"1234" 2:nil>')
|
145
141
|
end
|
146
142
|
end
|
147
143
|
|
148
144
|
describe "#to_s" do
|
149
145
|
it "returns the matching part of the original string" do
|
150
146
|
md = RE2::Regexp.new('(\d{2,5})').match("one two 23456")
|
151
|
-
md.to_s.
|
147
|
+
expect(md.to_s).to eq("23456")
|
152
148
|
end
|
153
149
|
end
|
154
150
|
|
@@ -156,53 +152,93 @@ describe RE2::MatchData do
|
|
156
152
|
it "allows the object to be expanded with an asterisk" do
|
157
153
|
md = RE2::Regexp.new('(\d+) (\d+)').match("1234 56")
|
158
154
|
m1, m2, m3 = *md
|
159
|
-
m1.
|
160
|
-
m2.
|
161
|
-
m3.
|
155
|
+
expect(m1).to eq("1234 56")
|
156
|
+
expect(m2).to eq("1234")
|
157
|
+
expect(m3).to eq("56")
|
162
158
|
end
|
163
159
|
end
|
164
160
|
|
165
161
|
describe "#begin" do
|
166
162
|
it "returns the offset of the start of a match by index" do
|
167
163
|
md = RE2::Regexp.new('(wo{2})').match('a woohoo')
|
168
|
-
md.string[md.begin(0)..-1].
|
164
|
+
expect(md.string[md.begin(0)..-1]).to eq('woohoo')
|
169
165
|
end
|
170
166
|
|
171
167
|
it "returns the offset of the start of a match by string name" do
|
172
168
|
md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar')
|
173
|
-
md.string[md.begin('foo')..-1].
|
169
|
+
expect(md.string[md.begin('foo')..-1]).to eq('foobar')
|
174
170
|
end
|
175
171
|
|
176
172
|
it "returns the offset of the start of a match by symbol name" do
|
177
173
|
md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar')
|
178
|
-
md.string[md.begin(:foo)..-1].
|
174
|
+
expect(md.string[md.begin(:foo)..-1]).to eq('foobar')
|
179
175
|
end
|
180
176
|
|
181
177
|
it "returns the offset despite multibyte characters" do
|
182
178
|
md = RE2::Regexp.new('(Ruby)').match('I ♥ Ruby')
|
183
|
-
md.string[md.begin(0)..-1].
|
179
|
+
expect(md.string[md.begin(0)..-1]).to eq('Ruby')
|
180
|
+
end
|
181
|
+
|
182
|
+
it "returns nil for non-existent numerical matches" do
|
183
|
+
md = RE2::Regexp.new('(\d)').match('123')
|
184
|
+
expect(md.begin(10)).to be_nil
|
185
|
+
end
|
186
|
+
|
187
|
+
it "returns nil for negative numerical matches" do
|
188
|
+
md = RE2::Regexp.new('(\d)').match('123')
|
189
|
+
expect(md.begin(-4)).to be_nil
|
190
|
+
end
|
191
|
+
|
192
|
+
it "returns nil for non-existent named matches" do
|
193
|
+
md = RE2::Regexp.new('(\d)').match('123')
|
194
|
+
expect(md.begin('foo')).to be_nil
|
195
|
+
end
|
196
|
+
|
197
|
+
it "returns nil for non-existent symbol named matches" do
|
198
|
+
md = RE2::Regexp.new('(\d)').match('123')
|
199
|
+
expect(md.begin(:foo)).to be_nil
|
184
200
|
end
|
185
201
|
end
|
186
202
|
|
187
203
|
describe "#end" do
|
188
204
|
it "returns the offset of the character following the end of a match" do
|
189
205
|
md = RE2::Regexp.new('(wo{2})').match('a woohoo')
|
190
|
-
md.string[0...md.end(0)].
|
206
|
+
expect(md.string[0...md.end(0)]).to eq('a woo')
|
191
207
|
end
|
192
208
|
|
193
209
|
it "returns the offset of a match by string name" do
|
194
210
|
md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar')
|
195
|
-
md.string[0...md.end('foo')].
|
211
|
+
expect(md.string[0...md.end('foo')]).to eq('a foo')
|
196
212
|
end
|
197
213
|
|
198
214
|
it "returns the offset of a match by symbol name" do
|
199
215
|
md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar')
|
200
|
-
md.string[0...md.end(:foo)].
|
216
|
+
expect(md.string[0...md.end(:foo)]).to eq('a foo')
|
201
217
|
end
|
202
218
|
|
203
219
|
it "returns the offset despite multibyte characters" do
|
204
220
|
md = RE2::Regexp.new('(Ruby)').match('I ♥ Ruby')
|
205
|
-
md.string[0...md.end(0)].
|
221
|
+
expect(md.string[0...md.end(0)]).to eq('I ♥ Ruby')
|
222
|
+
end
|
223
|
+
|
224
|
+
it "returns nil for non-existent numerical matches" do
|
225
|
+
md = RE2::Regexp.new('(\d)').match('123')
|
226
|
+
expect(md.end(10)).to be_nil
|
227
|
+
end
|
228
|
+
|
229
|
+
it "returns nil for negative numerical matches" do
|
230
|
+
md = RE2::Regexp.new('(\d)').match('123')
|
231
|
+
expect(md.end(-4)).to be_nil
|
232
|
+
end
|
233
|
+
|
234
|
+
it "returns nil for non-existent named matches" do
|
235
|
+
md = RE2::Regexp.new('(\d)').match('123')
|
236
|
+
expect(md.end('foo')).to be_nil
|
237
|
+
end
|
238
|
+
|
239
|
+
it "returns nil for non-existent symbol named matches" do
|
240
|
+
md = RE2::Regexp.new('(\d)').match('123')
|
241
|
+
expect(md.end(:foo)).to be_nil
|
206
242
|
end
|
207
243
|
end
|
208
244
|
end
|
data/spec/re2/regexp_spec.rb
CHANGED
@@ -1,253 +1,251 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
describe RE2::Regexp do
|
1
|
+
RSpec.describe RE2::Regexp do
|
4
2
|
describe "#initialize" do
|
5
3
|
it "returns an instance given only a pattern" do
|
6
4
|
re = RE2::Regexp.new('woo')
|
7
|
-
re.
|
5
|
+
expect(re).to be_a(RE2::Regexp)
|
8
6
|
end
|
9
7
|
|
10
8
|
it "returns an instance given a pattern and options" do
|
11
9
|
re = RE2::Regexp.new('woo', :case_sensitive => false)
|
12
|
-
re.
|
10
|
+
expect(re).to be_a(RE2::Regexp)
|
13
11
|
end
|
14
12
|
|
15
13
|
it "raises an error if given an inappropriate type" do
|
16
|
-
|
14
|
+
expect { RE2::Regexp.new(nil) }.to raise_error(TypeError)
|
17
15
|
end
|
18
16
|
end
|
19
17
|
|
20
18
|
describe "#compile" do
|
21
19
|
it "returns an instance given only a pattern" do
|
22
20
|
re = RE2::Regexp.compile('woo')
|
23
|
-
re.
|
21
|
+
expect(re).to be_a(RE2::Regexp)
|
24
22
|
end
|
25
23
|
|
26
24
|
it "returns an instance given a pattern and options" do
|
27
25
|
re = RE2::Regexp.compile('woo', :case_sensitive => false)
|
28
|
-
re.
|
26
|
+
expect(re).to be_a(RE2::Regexp)
|
29
27
|
end
|
30
28
|
end
|
31
29
|
|
32
30
|
describe "#options" do
|
33
31
|
it "returns a hash of options" do
|
34
32
|
options = RE2::Regexp.new('woo').options
|
35
|
-
options.
|
33
|
+
expect(options).to be_a(Hash)
|
36
34
|
end
|
37
35
|
|
38
36
|
it "is populated with default options when nothing has been set" do
|
39
37
|
options = RE2::Regexp.new('woo').options
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
38
|
+
expect(options).to include(:utf8 => true,
|
39
|
+
:posix_syntax => false,
|
40
|
+
:longest_match => false,
|
41
|
+
:log_errors => true,
|
42
|
+
:literal => false,
|
43
|
+
:never_nl => false,
|
44
|
+
:case_sensitive => true,
|
45
|
+
:perl_classes => false,
|
46
|
+
:word_boundary => false,
|
47
|
+
:one_line => false)
|
50
48
|
end
|
51
49
|
|
52
50
|
it "is populated with overridden options when specified" do
|
53
51
|
options = RE2::Regexp.new('woo', :case_sensitive => false).options
|
54
|
-
|
52
|
+
expect(options[:case_sensitive]).to eq(false)
|
55
53
|
end
|
56
54
|
end
|
57
55
|
|
58
56
|
describe "#error" do
|
59
57
|
it "returns nil if there is no error" do
|
60
58
|
error = RE2::Regexp.new('woo').error
|
61
|
-
error.
|
59
|
+
expect(error).to be_nil
|
62
60
|
end
|
63
61
|
|
64
62
|
# Use log_errors => false to suppress RE2's logging to STDERR.
|
65
63
|
it "contains the error string if there is an error" do
|
66
64
|
error = RE2::Regexp.new('wo(o', :log_errors => false).error
|
67
|
-
error.
|
65
|
+
expect(error).to eq("missing ): wo(o")
|
68
66
|
end
|
69
67
|
end
|
70
68
|
|
71
69
|
describe "#error_arg" do
|
72
70
|
it "returns nil if there is no error" do
|
73
71
|
error_arg = RE2::Regexp.new('woo').error_arg
|
74
|
-
error_arg.
|
72
|
+
expect(error_arg).to be_nil
|
75
73
|
end
|
76
74
|
|
77
75
|
it "returns the offending portin of the regexp if there is an error" do
|
78
76
|
error_arg = RE2::Regexp.new('wo(o', :log_errors => false).error_arg
|
79
|
-
error_arg.
|
77
|
+
expect(error_arg).to eq("wo(o")
|
80
78
|
end
|
81
79
|
end
|
82
80
|
|
83
81
|
describe "#program_size" do
|
84
82
|
it "returns a numeric value" do
|
85
83
|
program_size = RE2::Regexp.new('w(o)(o)').program_size
|
86
|
-
program_size.
|
84
|
+
expect(program_size).to be_a(Fixnum)
|
87
85
|
end
|
88
86
|
end
|
89
87
|
|
90
88
|
describe "#to_str" do
|
91
89
|
it "returns the original pattern" do
|
92
90
|
string = RE2::Regexp.new('w(o)(o)').to_str
|
93
|
-
string.
|
91
|
+
expect(string).to eq("w(o)(o)")
|
94
92
|
end
|
95
93
|
end
|
96
94
|
|
97
95
|
describe "#pattern" do
|
98
96
|
it "returns the original pattern" do
|
99
97
|
pattern = RE2::Regexp.new('w(o)(o)').pattern
|
100
|
-
pattern.
|
98
|
+
expect(pattern).to eq("w(o)(o)")
|
101
99
|
end
|
102
100
|
end
|
103
101
|
|
104
102
|
describe "#inspect" do
|
105
103
|
it "shows the class name and original pattern" do
|
106
104
|
string = RE2::Regexp.new('w(o)(o)').inspect
|
107
|
-
string.
|
105
|
+
expect(string).to eq("#<RE2::Regexp /w(o)(o)/>")
|
108
106
|
end
|
109
107
|
end
|
110
108
|
|
111
109
|
describe "#utf8?" do
|
112
110
|
it "returns true by default" do
|
113
|
-
RE2::Regexp.new('woo').
|
111
|
+
expect(RE2::Regexp.new('woo')).to be_utf8
|
114
112
|
end
|
115
113
|
|
116
114
|
it "can be overridden on initialization" do
|
117
115
|
re = RE2::Regexp.new('woo', :utf8 => false)
|
118
|
-
re.
|
116
|
+
expect(re).to_not be_utf8
|
119
117
|
end
|
120
118
|
end
|
121
119
|
|
122
120
|
describe "#posix_syntax?" do
|
123
121
|
it "returns false by default" do
|
124
|
-
RE2::Regexp.new('woo').
|
122
|
+
expect(RE2::Regexp.new('woo')).to_not be_posix_syntax
|
125
123
|
end
|
126
124
|
|
127
125
|
it "can be overridden on initialization" do
|
128
126
|
re = RE2::Regexp.new('woo', :posix_syntax => true)
|
129
|
-
re.
|
127
|
+
expect(re).to be_posix_syntax
|
130
128
|
end
|
131
129
|
end
|
132
130
|
|
133
131
|
describe "#literal?" do
|
134
132
|
it "returns false by default" do
|
135
|
-
RE2::Regexp.new('woo').
|
133
|
+
expect(RE2::Regexp.new('woo')).to_not be_literal
|
136
134
|
end
|
137
135
|
|
138
136
|
it "can be overridden on initialization" do
|
139
137
|
re = RE2::Regexp.new('woo', :literal => true)
|
140
|
-
re.
|
138
|
+
expect(re).to be_literal
|
141
139
|
end
|
142
140
|
end
|
143
141
|
|
144
142
|
describe "#never_nl?" do
|
145
143
|
it "returns false by default" do
|
146
|
-
RE2::Regexp.new('woo').
|
144
|
+
expect(RE2::Regexp.new('woo')).to_not be_never_nl
|
147
145
|
end
|
148
146
|
|
149
147
|
it "can be overridden on initialization" do
|
150
148
|
re = RE2::Regexp.new('woo', :never_nl => true)
|
151
|
-
re.
|
149
|
+
expect(re).to be_never_nl
|
152
150
|
end
|
153
151
|
end
|
154
152
|
|
155
153
|
describe "#case_sensitive?" do
|
156
154
|
it "returns true by default" do
|
157
|
-
RE2::Regexp.new('woo').
|
155
|
+
expect(RE2::Regexp.new('woo')).to be_case_sensitive
|
158
156
|
end
|
159
157
|
|
160
158
|
it "can be overridden on initialization" do
|
161
159
|
re = RE2::Regexp.new('woo', :case_sensitive => false)
|
162
|
-
re.
|
160
|
+
expect(re).to_not be_case_sensitive
|
163
161
|
end
|
164
162
|
end
|
165
163
|
|
166
164
|
describe "#case_insensitive?" do
|
167
165
|
it "returns false by default" do
|
168
|
-
RE2::Regexp.new('woo').
|
166
|
+
expect(RE2::Regexp.new('woo')).to_not be_case_insensitive
|
169
167
|
end
|
170
168
|
|
171
169
|
it "can be overridden on initialization" do
|
172
170
|
re = RE2::Regexp.new('woo', :case_sensitive => false)
|
173
|
-
re.
|
171
|
+
expect(re).to be_case_insensitive
|
174
172
|
end
|
175
173
|
end
|
176
174
|
|
177
175
|
describe "#casefold?" do
|
178
176
|
it "returns true by default" do
|
179
|
-
RE2::Regexp.new('woo').
|
177
|
+
expect(RE2::Regexp.new('woo')).to_not be_casefold
|
180
178
|
end
|
181
179
|
|
182
180
|
it "can be overridden on initialization" do
|
183
181
|
re = RE2::Regexp.new('woo', :case_sensitive => false)
|
184
|
-
re.
|
182
|
+
expect(re).to be_casefold
|
185
183
|
end
|
186
184
|
end
|
187
185
|
|
188
186
|
describe "#longest_match?" do
|
189
187
|
it "returns false by default" do
|
190
|
-
RE2::Regexp.new('woo').
|
188
|
+
expect(RE2::Regexp.new('woo')).to_not be_casefold
|
191
189
|
end
|
192
190
|
|
193
191
|
it "can be overridden on initialization" do
|
194
192
|
re = RE2::Regexp.new('woo', :longest_match => true)
|
195
|
-
re.
|
193
|
+
expect(re).to be_longest_match
|
196
194
|
end
|
197
195
|
end
|
198
196
|
|
199
197
|
describe "#log_errors?" do
|
200
198
|
it "returns true by default" do
|
201
|
-
RE2::Regexp.new('woo').
|
199
|
+
expect(RE2::Regexp.new('woo')).to be_log_errors
|
202
200
|
end
|
203
201
|
|
204
202
|
it "can be overridden on initialization" do
|
205
203
|
re = RE2::Regexp.new('woo', :log_errors => false)
|
206
|
-
re.
|
204
|
+
expect(re).to_not be_log_errors
|
207
205
|
end
|
208
206
|
end
|
209
207
|
|
210
208
|
describe "#perl_classes?" do
|
211
209
|
it "returns false by default" do
|
212
|
-
RE2::Regexp.new('woo').
|
210
|
+
expect(RE2::Regexp.new('woo')).to_not be_perl_classes
|
213
211
|
end
|
214
212
|
|
215
213
|
it "can be overridden on initialization" do
|
216
214
|
re = RE2::Regexp.new('woo', :perl_classes => true)
|
217
|
-
re.
|
215
|
+
expect(re).to be_perl_classes
|
218
216
|
end
|
219
217
|
end
|
220
218
|
|
221
219
|
describe "#word_boundary?" do
|
222
220
|
it "returns false by default" do
|
223
|
-
RE2::Regexp.new('woo').
|
221
|
+
expect(RE2::Regexp.new('woo')).to_not be_word_boundary
|
224
222
|
end
|
225
223
|
|
226
224
|
it "can be overridden on initialization" do
|
227
225
|
re = RE2::Regexp.new('woo', :word_boundary => true)
|
228
|
-
re.
|
226
|
+
expect(re).to be_word_boundary
|
229
227
|
end
|
230
228
|
end
|
231
229
|
|
232
230
|
describe "#one_line?" do
|
233
231
|
it "returns false by default" do
|
234
|
-
RE2::Regexp.new('woo').
|
232
|
+
expect(RE2::Regexp.new('woo')).to_not be_one_line
|
235
233
|
end
|
236
234
|
|
237
235
|
it "can be overridden on initialization" do
|
238
236
|
re = RE2::Regexp.new('woo', :one_line => true)
|
239
|
-
re.
|
237
|
+
expect(re).to be_one_line
|
240
238
|
end
|
241
239
|
end
|
242
240
|
|
243
241
|
describe "#max_mem" do
|
244
242
|
it "returns the default max memory" do
|
245
|
-
RE2::Regexp.new('woo').max_mem.
|
243
|
+
expect(RE2::Regexp.new('woo').max_mem).to eq(8388608)
|
246
244
|
end
|
247
245
|
|
248
246
|
it "can be overridden on initialization" do
|
249
247
|
re = RE2::Regexp.new('woo', :max_mem => 1024)
|
250
|
-
re.max_mem.
|
248
|
+
expect(re.max_mem).to eq(1024)
|
251
249
|
end
|
252
250
|
end
|
253
251
|
|
@@ -256,43 +254,43 @@ describe RE2::Regexp do
|
|
256
254
|
|
257
255
|
it "returns match data given only text" do
|
258
256
|
md = re.match("My name is Robert Paulson")
|
259
|
-
md.
|
257
|
+
expect(md).to be_a(RE2::MatchData)
|
260
258
|
end
|
261
259
|
|
262
260
|
it "returns nil if there is no match for the given text" do
|
263
|
-
re.match("My age is 99").
|
261
|
+
expect(re.match("My age is 99")).to be_nil
|
264
262
|
end
|
265
263
|
|
266
264
|
it "returns only true or false if no matches are requested" do
|
267
|
-
re.match("My name is Robert Paulson", 0).
|
268
|
-
re.match("My age is 99", 0).
|
265
|
+
expect(re.match("My name is Robert Paulson", 0)).to eq(true)
|
266
|
+
expect(re.match("My age is 99", 0)).to eq(false)
|
269
267
|
end
|
270
268
|
|
271
269
|
it "raises an exception when given nil" do
|
272
|
-
|
270
|
+
expect { re.match(nil) }.to raise_error(TypeError)
|
273
271
|
end
|
274
272
|
|
275
273
|
it "raises an exception when given an inappropriate number of matches" do
|
276
|
-
|
274
|
+
expect { re.match("My name is Robert Paulson", {}) }.to raise_error(TypeError)
|
277
275
|
end
|
278
276
|
|
279
277
|
describe "with a specific number of matches under the total in the pattern" do
|
280
278
|
subject { re.match("My name is Robert Paulson", 1) }
|
281
279
|
|
282
280
|
it "returns a match data object" do
|
283
|
-
subject.
|
281
|
+
expect(subject).to be_a(RE2::MatchData)
|
284
282
|
end
|
285
283
|
|
286
284
|
it "has the whole match and only the specified number of matches" do
|
287
|
-
subject.size.
|
285
|
+
expect(subject.size).to eq(2)
|
288
286
|
end
|
289
287
|
|
290
288
|
it "populates any specified matches" do
|
291
|
-
subject[1].
|
289
|
+
expect(subject[1]).to eq("Robert")
|
292
290
|
end
|
293
291
|
|
294
292
|
it "does not populate any matches that weren't included" do
|
295
|
-
subject[2].
|
293
|
+
expect(subject[2]).to be_nil
|
296
294
|
end
|
297
295
|
end
|
298
296
|
|
@@ -300,23 +298,23 @@ describe RE2::Regexp do
|
|
300
298
|
subject { re.match("My name is Robert Paulson", 5) }
|
301
299
|
|
302
300
|
it "returns a match data object" do
|
303
|
-
subject.
|
301
|
+
expect(subject).to be_a(RE2::MatchData)
|
304
302
|
end
|
305
303
|
|
306
304
|
it "has the whole match the specified number of matches" do
|
307
|
-
subject.size.
|
305
|
+
expect(subject.size).to eq(6)
|
308
306
|
end
|
309
307
|
|
310
308
|
it "populates any specified matches" do
|
311
|
-
subject[1].
|
312
|
-
subject[2].
|
309
|
+
expect(subject[1]).to eq("Robert")
|
310
|
+
expect(subject[2]).to eq("Paulson")
|
313
311
|
end
|
314
312
|
|
315
313
|
it "pads the remaining matches with nil" do
|
316
|
-
subject[3].
|
317
|
-
subject[4].
|
318
|
-
subject[5].
|
319
|
-
subject[6].
|
314
|
+
expect(subject[3]).to be_nil
|
315
|
+
expect(subject[4]).to be_nil
|
316
|
+
expect(subject[5]).to be_nil
|
317
|
+
expect(subject[6]).to be_nil
|
320
318
|
end
|
321
319
|
end
|
322
320
|
end
|
@@ -324,83 +322,83 @@ describe RE2::Regexp do
|
|
324
322
|
describe "#match?" do
|
325
323
|
it "returns only true or false if no matches are requested" do
|
326
324
|
re = RE2::Regexp.new('My name is (\S+) (\S+)')
|
327
|
-
re.match?("My name is Robert Paulson").
|
328
|
-
re.match?("My age is 99").
|
325
|
+
expect(re.match?("My name is Robert Paulson")).to eq(true)
|
326
|
+
expect(re.match?("My age is 99")).to eq(false)
|
329
327
|
end
|
330
328
|
end
|
331
329
|
|
332
330
|
describe "#=~" do
|
333
331
|
it "returns only true or false if no matches are requested" do
|
334
332
|
re = RE2::Regexp.new('My name is (\S+) (\S+)')
|
335
|
-
(re =~ "My name is Robert Paulson").
|
336
|
-
(re =~ "My age is 99").
|
333
|
+
expect(re =~ "My name is Robert Paulson").to eq(true)
|
334
|
+
expect(re =~ "My age is 99").to eq(false)
|
337
335
|
end
|
338
336
|
end
|
339
337
|
|
340
338
|
describe "#!~" do
|
341
339
|
it "returns only true or false if no matches are requested" do
|
342
340
|
re = RE2::Regexp.new('My name is (\S+) (\S+)')
|
343
|
-
(re !~ "My name is Robert Paulson").
|
344
|
-
(re !~ "My age is 99").
|
341
|
+
expect(re !~ "My name is Robert Paulson").to eq(false)
|
342
|
+
expect(re !~ "My age is 99").to eq(true)
|
345
343
|
end
|
346
344
|
end
|
347
345
|
|
348
346
|
describe "#===" do
|
349
347
|
it "returns only true or false if no matches are requested" do
|
350
348
|
re = RE2::Regexp.new('My name is (\S+) (\S+)')
|
351
|
-
(re === "My name is Robert Paulson").
|
352
|
-
(re === "My age is 99").
|
349
|
+
expect(re === "My name is Robert Paulson").to eq(true)
|
350
|
+
expect(re === "My age is 99").to eq(false)
|
353
351
|
end
|
354
352
|
end
|
355
353
|
|
356
354
|
describe "#ok?" do
|
357
355
|
it "returns true for valid regexps" do
|
358
|
-
RE2::Regexp.new('woo').
|
359
|
-
RE2::Regexp.new('wo(o)').
|
360
|
-
RE2::Regexp.new('((\d)\w+){3,}').
|
356
|
+
expect(RE2::Regexp.new('woo')).to be_ok
|
357
|
+
expect(RE2::Regexp.new('wo(o)')).to be_ok
|
358
|
+
expect(RE2::Regexp.new('((\d)\w+){3,}')).to be_ok
|
361
359
|
end
|
362
360
|
|
363
361
|
it "returns false for invalid regexps" do
|
364
|
-
RE2::Regexp.new('wo(o', :log_errors => false).
|
365
|
-
RE2::Regexp.new('wo[o', :log_errors => false).
|
366
|
-
RE2::Regexp.new('*', :log_errors => false).
|
362
|
+
expect(RE2::Regexp.new('wo(o', :log_errors => false)).to_not be_ok
|
363
|
+
expect(RE2::Regexp.new('wo[o', :log_errors => false)).to_not be_ok
|
364
|
+
expect(RE2::Regexp.new('*', :log_errors => false)).to_not be_ok
|
367
365
|
end
|
368
366
|
end
|
369
367
|
|
370
368
|
describe "#escape" do
|
371
369
|
it "transforms a string into a regexp" do
|
372
|
-
RE2::Regexp.escape("1.5-2.0?").
|
370
|
+
expect(RE2::Regexp.escape("1.5-2.0?")).to eq('1\.5\-2\.0\?')
|
373
371
|
end
|
374
372
|
end
|
375
373
|
|
376
374
|
describe "#quote" do
|
377
375
|
it "transforms a string into a regexp" do
|
378
|
-
RE2::Regexp.quote("1.5-2.0?").
|
376
|
+
expect(RE2::Regexp.quote("1.5-2.0?")).to eq('1\.5\-2\.0\?')
|
379
377
|
end
|
380
378
|
end
|
381
379
|
|
382
380
|
describe "#number_of_capturing_groups" do
|
383
381
|
it "returns the number of groups in a regexp" do
|
384
|
-
RE2::Regexp.new('(a)(b)(c)').number_of_capturing_groups.
|
385
|
-
RE2::Regexp.new('abc').number_of_capturing_groups.
|
386
|
-
RE2::Regexp.new('a((b)c)').number_of_capturing_groups.
|
382
|
+
expect(RE2::Regexp.new('(a)(b)(c)').number_of_capturing_groups).to eq(3)
|
383
|
+
expect(RE2::Regexp.new('abc').number_of_capturing_groups).to eq(0)
|
384
|
+
expect(RE2::Regexp.new('a((b)c)').number_of_capturing_groups).to eq(2)
|
387
385
|
end
|
388
386
|
end
|
389
387
|
|
390
388
|
describe "#named_capturing_groups" do
|
391
389
|
it "returns a hash of names to indices" do
|
392
|
-
RE2::Regexp.new('(?P<bob>a)').named_capturing_groups.
|
390
|
+
expect(RE2::Regexp.new('(?P<bob>a)').named_capturing_groups).to be_a(Hash)
|
393
391
|
end
|
394
392
|
|
395
393
|
it "maps names to indices with only one group" do
|
396
394
|
groups = RE2::Regexp.new('(?P<bob>a)').named_capturing_groups
|
397
|
-
groups["bob"].
|
395
|
+
expect(groups["bob"]).to eq(1)
|
398
396
|
end
|
399
397
|
|
400
398
|
it "maps names to indices with several groups" do
|
401
399
|
groups = RE2::Regexp.new('(?P<bob>a)(o)(?P<rob>e)').named_capturing_groups
|
402
|
-
groups["bob"].
|
403
|
-
groups["rob"].
|
400
|
+
expect(groups["bob"]).to eq(1)
|
401
|
+
expect(groups["rob"]).to eq(3)
|
404
402
|
end
|
405
403
|
end
|
406
404
|
|
@@ -409,7 +407,7 @@ describe RE2::Regexp do
|
|
409
407
|
r = RE2::Regexp.new('(\w+)')
|
410
408
|
scanner = r.scan("It is a truth universally acknowledged")
|
411
409
|
|
412
|
-
scanner.
|
410
|
+
expect(scanner).to be_a(RE2::Scanner)
|
413
411
|
end
|
414
412
|
end
|
415
413
|
end
|