re2 0.7.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,253 +1,251 @@
1
- require "spec_helper"
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.must_be_instance_of(RE2::Regexp)
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.must_be_instance_of(RE2::Regexp)
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
- lambda { RE2::Regexp.new(nil) }.must_raise(TypeError)
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.must_be_instance_of(RE2::Regexp)
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.must_be_instance_of(RE2::Regexp)
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.must_be_instance_of(Hash)
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
- assert options[:utf8]
41
- refute options[:posix_syntax]
42
- refute options[:longest_match]
43
- assert [:log_errors]
44
- refute options[:literal]
45
- refute options[:never_nl]
46
- assert options[:case_sensitive]
47
- refute options[:perl_classes]
48
- refute options[:word_boundary]
49
- refute options[:one_line]
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
- refute options[:case_sensitive]
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.must_be_nil
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.must_equal("missing ): wo(o")
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.must_be_nil
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.must_equal("wo(o")
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.must_be_instance_of(Fixnum)
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.must_equal("w(o)(o)")
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.must_equal("w(o)(o)")
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.must_equal("#<RE2::Regexp /w(o)(o)/>")
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').must_be(:utf8?)
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.wont_be(:utf8?)
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').wont_be(:posix_syntax?)
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.must_be(:posix_syntax?)
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').wont_be(:literal?)
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.must_be(:literal?)
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').wont_be(:never_nl?)
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.must_be(:never_nl?)
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').must_be(:case_sensitive?)
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.wont_be(:case_sensitive?)
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').wont_be(:case_insensitive?)
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.must_be(:case_insensitive?)
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').wont_be(:casefold?)
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.must_be(:casefold?)
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').wont_be(:casefold?)
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.must_be(:longest_match?)
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').must_be(:log_errors?)
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.wont_be(:log_errors?)
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').wont_be(:perl_classes?)
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.must_be(:perl_classes?)
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').wont_be(:word_boundary?)
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.must_be(:word_boundary?)
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').wont_be(:one_line?)
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.must_be(:one_line?)
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.must_equal(8388608)
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.must_equal(1024)
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.must_be_instance_of(RE2::MatchData)
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").must_be_nil
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).must_equal(true)
268
- re.match("My age is 99", 0).must_equal(false)
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
- lambda { re.match(nil) }.must_raise(TypeError)
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
- lambda { re.match("My name is Robert Paulson", {}) }.must_raise(TypeError)
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.must_be_instance_of(RE2::MatchData)
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.must_equal(2)
285
+ expect(subject.size).to eq(2)
288
286
  end
289
287
 
290
288
  it "populates any specified matches" do
291
- subject[1].must_equal("Robert")
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].must_be_nil
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.must_be_instance_of(RE2::MatchData)
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.must_equal(6)
305
+ expect(subject.size).to eq(6)
308
306
  end
309
307
 
310
308
  it "populates any specified matches" do
311
- subject[1].must_equal("Robert")
312
- subject[2].must_equal("Paulson")
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].must_be_nil
317
- subject[4].must_be_nil
318
- subject[5].must_be_nil
319
- subject[6].must_be_nil
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").must_equal(true)
328
- re.match?("My age is 99").must_equal(false)
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").must_equal(true)
336
- (re =~ "My age is 99").must_equal(false)
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").must_equal(false)
344
- (re !~ "My age is 99").must_equal(true)
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").must_equal(true)
352
- (re === "My age is 99").must_equal(false)
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').must_be(:ok?)
359
- RE2::Regexp.new('wo(o)').must_be(:ok?)
360
- RE2::Regexp.new('((\d)\w+){3,}').must_be(:ok?)
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).wont_be(:ok?)
365
- RE2::Regexp.new('wo[o', :log_errors => false).wont_be(:ok?)
366
- RE2::Regexp.new('*', :log_errors => false).wont_be(:ok?)
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?").must_equal('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?").must_equal('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.must_equal(3)
385
- RE2::Regexp.new('abc').number_of_capturing_groups.must_equal(0)
386
- RE2::Regexp.new('a((b)c)').number_of_capturing_groups.must_equal(2)
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.must_be_instance_of(Hash)
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"].must_equal(1)
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"].must_equal(1)
403
- groups["rob"].must_equal(3)
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.must_be_instance_of(RE2::Scanner)
410
+ expect(scanner).to be_a(RE2::Scanner)
413
411
  end
414
412
  end
415
413
  end