Debian Codegraph @

spec/re2/regexp_spec.rb

raw symbols SCIP
1 # frozen_string_literal: true
2
3 require "rbconfig/sizeof"
4
5 RSpec.describe RE2::Regexp do
6 INT_MAX = 2**(RbConfig::SIZEOF.fetch("int") * 8 - 1) - 1
7
8 describe "#initialize" do
9 it "returns an instance given only a pattern" do
10 re = RE2::Regexp.new('woo')
11
12 expect(re).to be_a(RE2::Regexp)
13 end
14
15 it "returns an instance given a pattern and options" do
16 re = RE2::Regexp.new('woo', case_sensitive: false)
17
18 expect(re).to be_a(RE2::Regexp)
19 end
20
21 it "accepts patterns containing null bytes" do
22 re = RE2::Regexp.new("a\0b")
23
24 expect(re.pattern).to eq("a\0b")
25 end
26
27 it "raises an error if given an inappropriate type" do
28 expect { RE2::Regexp.new(nil) }.to raise_error(TypeError)
29 end
30
31 it "allows invalid patterns to be created" do
32 re = RE2::Regexp.new('???', log_errors: false)
33
34 expect(re).to be_a(RE2::Regexp)
35 end
36
37 it "supports passing something that can be coerced to a String as input" do
38 re = RE2::Regexp.new(StringLike.new('w(o)(o)'))
39
40 expect(re).to be_a(RE2::Regexp)
41 end
42
43 it "returns a frozen object" do
44 expect(RE2::Regexp.new('woo')).to be_frozen
45 end
46
47 it "cannot be re-initialized" do
48 re = RE2::Regexp.new('woo')
49
50 expect { re.send(:initialize, 'bar') }.to raise_error(FrozenError)
51 end
52 end
53
54 describe "#dup" do
55 it "returns a copy with the same pattern" do
56 re = described_class.new('(\d+)')
57 copy = re.dup
58
59 expect(copy.to_s).to eq('(\d+)')
60 end
61
62 it "returns a different object" do
63 re = described_class.new('(\d+)')
64 copy = re.dup
65
66 expect(copy).to_not equal(re)
67 end
68
69 it "returns a valid copy" do
70 re = described_class.new('(\d+)')
71 copy = re.dup
72
73 expect(copy.ok?).to eq(true)
74 end
75
76 it "copies options from the original" do
77 re = described_class.new('(\d+)', case_sensitive: false)
78 copy = re.dup
79
80 expect(copy).to_not be_case_sensitive
81 end
82
83 it "returns a frozen copy" do
84 re = described_class.new('(\d+)')
85 copy = re.dup
86
87 expect(copy).to be_frozen
88 end
89
90 it "raises an error when called on an uninitialized object" do
91 expect { described_class.allocate.dup }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
92 end
93 end
94
95 describe "#clone" do
96 it "returns a copy with the same pattern" do
97 re = described_class.new('woo')
98 copy = re.clone
99
100 expect(copy.to_s).to eq('woo')
101 end
102
103 it "returns a frozen copy" do
104 re = described_class.new('woo')
105 copy = re.clone
106
107 expect(copy).to be_frozen
108 end
109
110 it "raises an error when called on an uninitialized object" do
111 expect { described_class.allocate.clone }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
112 end
113 end
114
115 describe ".compile" do
116 it "returns an instance given only a pattern" do
117 re = RE2::Regexp.compile('woo')
118
119 expect(re).to be_a(RE2::Regexp)
120 end
121
122 it "returns an instance given a pattern and options" do
123 re = RE2::Regexp.compile('woo', case_sensitive: false)
124 expect(re).to be_a(RE2::Regexp)
125 end
126
127 it "accepts patterns containing null bytes" do
128 re = RE2::Regexp.compile("a\0b")
129
130 expect(re.pattern).to eq("a\0b")
131 end
132
133 it "raises an error if given an inappropriate type" do
134 expect { RE2::Regexp.compile(nil) }.to raise_error(TypeError)
135 end
136
137 it "allows invalid patterns to be created" do
138 re = RE2::Regexp.compile('???', log_errors: false)
139
140 expect(re).to be_a(RE2::Regexp)
141 end
142
143 it "supports passing something that can be coerced to a String as input" do
144 re = RE2::Regexp.compile(StringLike.new('w(o)(o)'))
145
146 expect(re).to be_a(RE2::Regexp)
147 end
148 end
149
150 describe "#options" do
151 it "returns a hash of options" do
152 options = RE2::Regexp.new('woo').options
153 expect(options).to be_a(Hash)
154 end
155
156 it "is populated with default options when nothing has been set" do
157 expect(RE2::Regexp.new('woo').options).to include(
158 utf8: true,
159 posix_syntax: false,
160 longest_match: false,
161 log_errors: true,
162 literal: false,
163 never_nl: false,
164 case_sensitive: true,
165 perl_classes: false,
166 word_boundary: false,
167 one_line: false
168 )
169 end
170
171 it "is populated with overridden options when specified" do
172 options = RE2::Regexp.new('woo', case_sensitive: false).options
173
174 expect(options).to include(case_sensitive: false)
175 end
176
177 it "raises an error when called on an uninitialized object" do
178 expect { described_class.allocate.options }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
179 end
180 end
181
182 describe "#error" do
183 it "returns nil if there is no error" do
184 error = RE2::Regexp.new('woo').error
185
186 expect(error).to be_nil
187 end
188
189 # Use log_errors: false to suppress RE2's logging to STDERR.
190 it "contains the error string if there is an error" do
191 error = RE2::Regexp.new('wo(o', log_errors: false).error
192
193 expect(error).to eq("missing ): wo(o")
194 end
195
196 it "raises an error when called on an uninitialized object" do
197 expect { described_class.allocate.error }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
198 end
199 end
200
201 describe "#error_arg" do
202 it "returns nil if there is no error" do
203 error_arg = RE2::Regexp.new('woo').error_arg
204
205 expect(error_arg).to be_nil
206 end
207
208 it "returns the offending portion of the pattern if there is an error" do
209 error_arg = RE2::Regexp.new('wo(o', log_errors: false).error_arg
210
211 expect(error_arg).to eq("wo(o")
212 end
213
214 it "raises an error when called on an uninitialized object" do
215 expect { described_class.allocate.error_arg }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
216 end
217 end
218
219 describe "#program_size" do
220 it "returns a numeric value" do
221 program_size = RE2::Regexp.new('w(o)(o)').program_size
222
223 expect(program_size).to be_an(Integer)
224 end
225
226 it "returns -1 for an invalid pattern" do
227 program_size = RE2::Regexp.new('???', log_errors: false).program_size
228
229 expect(program_size).to eq(-1)
230 end
231
232 it "raises an error when called on an uninitialized object" do
233 expect { described_class.allocate.program_size }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
234 end
235 end
236
237 describe "#to_str" do
238 it "returns the original pattern" do
239 string = RE2::Regexp.new('w(o)(o)').to_str
240
241 expect(string).to eq("w(o)(o)")
242 end
243
244 it "returns the pattern even if invalid" do
245 string = RE2::Regexp.new('???', log_errors: false).to_str
246
247 expect(string).to eq("???")
248 end
249
250 it "raises an error when called on an uninitialized object" do
251 expect { described_class.allocate.to_str }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
252 end
253 end
254
255 describe "#pattern" do
256 it "returns the original pattern" do
257 pattern = RE2::Regexp.new('w(o)(o)').pattern
258
259 expect(pattern).to eq("w(o)(o)")
260 end
261
262 it "returns the pattern even if invalid" do
263 pattern = RE2::Regexp.new('???', log_errors: false).pattern
264
265 expect(pattern).to eq("???")
266 end
267
268 it "raises an error when called on an uninitialized object" do
269 expect { described_class.allocate.pattern }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
270 end
271 end
272
273 describe "#inspect" do
274 it "shows the class name and original pattern" do
275 string = RE2::Regexp.new('w(o)(o)').inspect
276
277 expect(string).to eq("#<RE2::Regexp /w(o)(o)/>")
278 end
279
280 it "respects the pattern's original encoding" do
281 string = RE2::Regexp.new('w(o)(o)', utf8: false).inspect
282
283 expect(string.encoding).to eq(Encoding::ISO_8859_1)
284 end
285
286 it "raises an error when called on an uninitialized object" do
287 expect { described_class.allocate.inspect }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
288 end
289 end
290
291 describe "#utf8?" do
292 it "returns true by default" do
293 expect(RE2::Regexp.new('woo')).to be_utf8
294 end
295
296 it "can be overridden on initialization" do
297 re = RE2::Regexp.new('woo', utf8: false)
298
299 expect(re).to_not be_utf8
300 end
301
302 it "raises an error when called on an uninitialized object" do
303 expect { described_class.allocate.utf8? }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
304 end
305 end
306
307 describe "#posix_syntax?" do
308 it "returns false by default" do
309 expect(RE2::Regexp.new('woo')).to_not be_posix_syntax
310 end
311
312 it "can be overridden on initialization" do
313 re = RE2::Regexp.new('woo', posix_syntax: true)
314
315 expect(re).to be_posix_syntax
316 end
317
318 it "raises an error when called on an uninitialized object" do
319 expect { described_class.allocate.posix_syntax? }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
320 end
321 end
322
323 describe "#literal?" do
324 it "returns false by default" do
325 expect(RE2::Regexp.new('woo')).to_not be_literal
326 end
327
328 it "can be overridden on initialization" do
329 re = RE2::Regexp.new('woo', literal: true)
330
331 expect(re).to be_literal
332 end
333
334 it "raises an error when called on an uninitialized object" do
335 expect { described_class.allocate.literal? }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
336 end
337 end
338
339 describe "#never_nl?" do
340 it "returns false by default" do
341 expect(RE2::Regexp.new('woo')).to_not be_never_nl
342 end
343
344 it "can be overridden on initialization" do
345 re = RE2::Regexp.new('woo', never_nl: true)
346
347 expect(re).to be_never_nl
348 end
349
350 it "raises an error when called on an uninitialized object" do
351 expect { described_class.allocate.never_nl? }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
352 end
353 end
354
355 describe "#case_sensitive?" do
356 it "returns true by default" do
357 expect(RE2::Regexp.new('woo')).to be_case_sensitive
358 end
359
360 it "can be overridden on initialization" do
361 re = RE2::Regexp.new('woo', case_sensitive: false)
362 expect(re).to_not be_case_sensitive
363 end
364
365 it "raises an error when called on an uninitialized object" do
366 expect { described_class.allocate.case_sensitive? }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
367 end
368 end
369
370 describe "#case_insensitive?" do
371 it "returns false by default" do
372 expect(RE2::Regexp.new('woo')).to_not be_case_insensitive
373 end
374
375 it "can be overridden on initialization" do
376 re = RE2::Regexp.new('woo', case_sensitive: false)
377
378 expect(re).to be_case_insensitive
379 end
380
381 it "raises an error when called on an uninitialized object" do
382 expect { described_class.allocate.case_insensitive? }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
383 end
384 end
385
386 describe "#casefold?" do
387 it "returns true by default" do
388 expect(RE2::Regexp.new('woo')).to_not be_casefold
389 end
390
391 it "can be overridden on initialization" do
392 re = RE2::Regexp.new('woo', case_sensitive: false)
393
394 expect(re).to be_casefold
395 end
396
397 it "raises an error when called on an uninitialized object" do
398 expect { described_class.allocate.casefold? }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
399 end
400 end
401
402 describe "#longest_match?" do
403 it "returns false by default" do
404 expect(RE2::Regexp.new('woo')).to_not be_casefold
405 end
406
407 it "can be overridden on initialization" do
408 re = RE2::Regexp.new('woo', longest_match: true)
409
410 expect(re).to be_longest_match
411 end
412
413 it "raises an error when called on an uninitialized object" do
414 expect { described_class.allocate.longest_match? }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
415 end
416 end
417
418 describe "#log_errors?" do
419 it "returns true by default" do
420 expect(RE2::Regexp.new('woo')).to be_log_errors
421 end
422
423 it "can be overridden on initialization" do
424 re = RE2::Regexp.new('woo', log_errors: false)
425
426 expect(re).to_not be_log_errors
427 end
428
429 it "raises an error when called on an uninitialized object" do
430 expect { described_class.allocate.log_errors? }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
431 end
432 end
433
434 describe "#perl_classes?" do
435 it "returns false by default" do
436 expect(RE2::Regexp.new('woo')).to_not be_perl_classes
437 end
438
439 it "can be overridden on initialization" do
440 re = RE2::Regexp.new('woo', perl_classes: true)
441
442 expect(re).to be_perl_classes
443 end
444
445 it "raises an error when called on an uninitialized object" do
446 expect { described_class.allocate.perl_classes? }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
447 end
448 end
449
450 describe "#word_boundary?" do
451 it "returns false by default" do
452 expect(RE2::Regexp.new('woo')).to_not be_word_boundary
453 end
454
455 it "can be overridden on initialization" do
456 re = RE2::Regexp.new('woo', word_boundary: true)
457
458 expect(re).to be_word_boundary
459 end
460
461 it "raises an error when called on an uninitialized object" do
462 expect { described_class.allocate.word_boundary? }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
463 end
464 end
465
466 describe "#one_line?" do
467 it "returns false by default" do
468 expect(RE2::Regexp.new('woo')).to_not be_one_line
469 end
470
471 it "can be overridden on initialization" do
472 re = RE2::Regexp.new('woo', one_line: true)
473
474 expect(re).to be_one_line
475 end
476
477 it "raises an error when called on an uninitialized object" do
478 expect { described_class.allocate.one_line? }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
479 end
480 end
481
482 describe "#max_mem" do
483 it "returns the default max memory" do
484 expect(RE2::Regexp.new('woo').max_mem).to eq(8388608)
485 end
486
487 it "can be overridden on initialization" do
488 re = RE2::Regexp.new('woo', max_mem: 1024)
489
490 expect(re.max_mem).to eq(1024)
491 end
492
493 it "raises an error when called on an uninitialized object" do
494 expect { described_class.allocate.max_mem }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
495 end
496 end
497
498 describe "#match" do
499 it "returns match data given only text if the pattern has capturing groups" do
500 re = RE2::Regexp.new('My name is (\w+) (\w+)')
501
502 expect(re.match("My name is Alice Bloggs")).to be_a(RE2::MatchData)
503 end
504
505 it "returns only true or false given only text if the pattern has no capturing groups" do
506 re = RE2::Regexp.new('My name is \w+ \w+')
507
508 expect(re.match("My name is Alice Bloggs")).to eq(true)
509 end
510
511 it "supports matching against text containing null bytes" do
512 re = RE2::Regexp.new("a\0b")
513
514 expect(re.match("a\0b")).to eq(true)
515 end
516
517 it "returns nil if the text does not match the pattern" do
518 re = RE2::Regexp.new('My name is (\w+) (\w+)')
519
520 expect(re.match("My age is 99")).to be_nil
521 end
522
523 it "accepts text that can be coerced to a string" do
524 re = RE2::Regexp.new('My name is (\w+) (\w+)')
525
526 expect(re.match(StringLike.new("My name is Alice Bloggs"))).to be_a(RE2::MatchData)
527 end
528
529 it "raises an exception when given text that cannot be coerced to a string" do
530 re = RE2::Regexp.new('My name is (\w+) (\w+)')
531
532 expect { re.match(nil) }.to raise_error(TypeError)
533 end
534
535 it "returns nil with an invalid pattern" do
536 re = RE2::Regexp.new('???', log_errors: false)
537
538 expect(re.match("My name is Alice Bloggs")).to be_nil
539 end
540
541 it "returns nil with an invalid pattern and options" do
542 re = RE2::Regexp.new('???', log_errors: false)
543
544 expect(re.match('foo bar', startpos: 1)).to be_nil
545 end
546
547 it "accepts an offset at which to start matching", :aggregate_failures do
548 re = RE2::Regexp.new('(\w+) (\w+)')
549 md = re.match("one two three", startpos: 4)
550
551 expect(md[1]).to eq("two")
552 expect(md[2]).to eq("three")
553 end
554
555 it "returns nil if using a starting offset past the end of the text" do
556 skip "Underlying RE2::Match does not have endpos argument" unless RE2::Regexp.match_has_endpos_argument?
557
558 re = RE2::Regexp.new('(\w+) (\w+)', log_errors: false)
559
560 expect(re.match("one two three", startpos: 20, endpos: 21)).to be_nil
561 end
562
563 it "raises an exception when given a negative starting offset" do
564 re = RE2::Regexp.new('(\w+) (\w+)')
565
566 expect { re.match("one two three", startpos: -1) }.to raise_error(ArgumentError, "startpos should be >= 0")
567 end
568
569 it "raises an exception when given a starting offset past the default ending offset" do
570 re = RE2::Regexp.new('(\w+) (\w+)')
571
572 expect { re.match("one two three", startpos: 30) }.to raise_error(ArgumentError, "startpos should be <= endpos")
573 end
574
575 it "accepts an offset at which to end matching", :aggregate_failures do
576 skip "Underlying RE2::Match does not have endpos argument" unless RE2::Regexp.match_has_endpos_argument?
577
578 re = RE2::Regexp.new('(\w+) (\w+)')
579 md = re.match("one two three", endpos: 6)
580
581 expect(md[1]).to eq("one")
582 expect(md[2]).to eq("tw")
583 end
584
585 it "returns nil if using a ending offset at the start of the text" do
586 skip "Underlying RE2::Match does not have endpos argument" unless RE2::Regexp.match_has_endpos_argument?
587
588 re = RE2::Regexp.new('(\w+) (\w+)')
589
590 expect(re.match("one two three", endpos: 0)).to be_nil
591 end
592
593 it "raises an exception when given a negative ending offset" do
594 skip "Underlying RE2::Match does not have endpos argument" unless RE2::Regexp.match_has_endpos_argument?
595
596 re = RE2::Regexp.new('(\w+) (\w+)')
597
598 expect { re.match("one two three", endpos: -1) }.to raise_error(ArgumentError, "endpos should be >= 0")
599 end
600
601 it "raises an exception when given an ending offset before the starting offset" do
602 skip "Underlying RE2::Match does not have endpos argument" unless RE2::Regexp.match_has_endpos_argument?
603
604 re = RE2::Regexp.new('(\w+) (\w+)')
605
606 expect { re.match("one two three", startpos: 3, endpos: 0) }.to raise_error(ArgumentError, "startpos should be <= endpos")
607 end
608
609 it "raises an error if given an ending offset and RE2 does not support it" do
610 skip "Underlying RE2::Match has endpos argument" if RE2::Regexp.match_has_endpos_argument?
611
612 re = RE2::Regexp.new('(\w+) (\w+)')
613
614 expect { re.match("one two three", endpos: 3) }.to raise_error(RE2::Regexp::UnsupportedError)
615 end
616
617 it "does not truncate startpos to 32 bits" do
618 skip "Underlying RE2::Match does not have endpos argument" unless RE2::Regexp.match_has_endpos_argument?
619 skip "size_t is not larger than a 32-bit int" if RbConfig::SIZEOF.fetch("size_t") <= (32 / 8)
620
621 re = RE2::Regexp.new('(\w+)', log_errors: false)
622
623 expect(re.match("one two three", startpos: 2_147_483_648, endpos: 2_147_483_649)).to be_nil
624 end
625
626 it "does not truncate endpos to 32 bits" do
627 skip "Underlying RE2::Match does not have endpos argument" unless RE2::Regexp.match_has_endpos_argument?
628 skip "size_t is not larger than a 32-bit int" if RbConfig::SIZEOF.fetch("size_t") <= (32 / 8)
629
630 re = RE2::Regexp.new('(\w+)', log_errors: false)
631
632 expect(re.match("one two three", endpos: 2_147_483_648)).to be_nil
633 end
634
635 it "does not anchor matches by default when extracting submatches" do
636 re = RE2::Regexp.new('(two)')
637
638 expect(re.match("one two three")).to be_a(RE2::MatchData)
639 end
640
641 it "does not anchor matches by default without extracting submatches" do
642 re = RE2::Regexp.new('(two)')
643
644 expect(re.match("one two three", submatches: 0)).to eq(true)
645 end
646
647 it "can explicitly match without anchoring when extracting submatches" do
648 re = RE2::Regexp.new('(two)')
649
650 expect(re.match("one two three", anchor: :unanchored)).to be_a(RE2::MatchData)
651 end
652
653 it "can explicitly match with neither anchoring nor extracting submatches" do
654 re = RE2::Regexp.new('(two)')
655
656 expect(re.match("one two three", anchor: :unanchored, submatches: 0)).to eq(true)
657 end
658
659 it "can anchor matches at the start when extracting submatches", :aggregate_failures do
660 re = RE2::Regexp.new('(two)')
661
662 expect(re.match("two three", anchor: :anchor_start)).to be_a(RE2::MatchData)
663 expect(re.match("one two three", anchor: :anchor_start)).to be_nil
664 end
665
666 it "can anchor matches at the start without extracting submatches", :aggregate_failures do
667 re = RE2::Regexp.new('(two)')
668
669 expect(re.match("two three", anchor: :anchor_start, submatches: 0)).to eq(true)
670 expect(re.match("one two three", anchor: :anchor_start, submatches: 0)).to eq(false)
671 end
672
673 it "can anchor matches at both ends when extracting submatches", :aggregate_failures do
674 re = RE2::Regexp.new('(two)')
675
676 expect(re.match("two three", anchor: :anchor_both)).to be_nil
677 expect(re.match("two", anchor: :anchor_both)).to be_a(RE2::MatchData)
678 end
679
680 it "does not anchor matches when given a nil anchor" do
681 re = RE2::Regexp.new('(two)')
682
683 expect(re.match("one two three", anchor: nil)).to be_a(RE2::MatchData)
684 end
685
686 it "raises an exception when given an invalid anchor" do
687 re = RE2::Regexp.new('(two)')
688
689 expect { re.match("one two three", anchor: :invalid) }.to raise_error(ArgumentError, "anchor should be one of: :unanchored, :anchor_start, :anchor_both")
690 end
691
692 it "raises an exception when given a non-symbol anchor" do
693 re = RE2::Regexp.new('(two)')
694
695 expect { re.match("one two three", anchor: 0) }.to raise_error(TypeError)
696 end
697
698 it "extracts all submatches by default", :aggregate_failures do
699 re = RE2::Regexp.new('(\w+) (\w+) (\w+)')
700 md = re.match("one two three")
701
702 expect(md[1]).to eq("one")
703 expect(md[2]).to eq("two")
704 expect(md[3]).to eq("three")
705 end
706
707 it "supports extracting submatches containing null bytes" do
708 re = RE2::Regexp.new("(a\0b)")
709 md = re.match("a\0bc")
710
711 expect(md[1]).to eq("a\0b")
712 end
713
714 it "extracts a specific number of submatches", :aggregate_failures do
715 re = RE2::Regexp.new('(\w+) (\w+) (\w+)')
716 md = re.match("one two three", submatches: 2)
717
718 expect(md[1]).to eq("one")
719 expect(md[2]).to eq("two")
720 expect(md[3]).to be_nil
721 end
722
723 it "pads submatches with nil when requesting more than the number of capturing groups" do
724 re = RE2::Regexp.new('(\w+) (\w+) (\w+)')
725 md = re.match("one two three", submatches: 5)
726
727 expect(md.to_a).to eq(["one two three", "one", "two", "three", nil, nil])
728 end
729
730 it "raises an exception when given a negative number of submatches" do
731 re = RE2::Regexp.new('(\w+) (\w+) (\w+)')
732
733 expect { re.match("one two three", submatches: -1) }.to raise_error(ArgumentError, "number of matches should be >= 0")
734 end
735
736 it "raises an exception when given a non-numeric number of submatches" do
737 re = RE2::Regexp.new('(\w+) (\w+) (\w+)')
738
739 expect { re.match("one two three", submatches: :invalid) }.to raise_error(TypeError)
740 end
741
742 it "raises an exception when given too large a number of submatches" do
743 re = RE2::Regexp.new('(\w+) (\w+) (\w+)')
744
745 expect { re.match("one two three", submatches: INT_MAX) }.to raise_error(RangeError, "number of matches should be < #{INT_MAX}")
746 end
747
748 it "defaults to extracting all submatches when given nil", :aggregate_failures do
749 re = RE2::Regexp.new('(\w+) (\w+) (\w+)')
750 md = re.match("one two three", submatches: nil)
751
752 expect(md[1]).to eq("one")
753 expect(md[2]).to eq("two")
754 expect(md[3]).to eq("three")
755 end
756
757 it "accepts passing the number of submatches instead of options for backward compatibility", :aggregate_failures do
758 re = RE2::Regexp.new('(\w+) (\w+) (\w+)')
759 md = re.match("one two three", 2)
760
761 expect(md[1]).to eq("one")
762 expect(md[2]).to eq("two")
763 expect(md[3]).to be_nil
764 end
765
766 it "raises an exception if given too large a number of submatches instead of options" do
767 re = RE2::Regexp.new('(\w+) (\w+) (\w+)')
768
769 expect { re.match("one two three", INT_MAX) }.to raise_error(RangeError, "number of matches should be < #{INT_MAX}")
770 end
771
772 it "raises an exception when given invalid options" do
773 re = RE2::Regexp.new('(\w+) (\w+) (\w+)')
774
775 expect { re.match("one two three", :invalid) }.to raise_error(TypeError)
776 end
777
778 it "accepts anything that can be coerced to a hash as options", :aggregate_failures do
779 re = RE2::Regexp.new('(\w+) (\w+) (\w+)')
780
781 expect(re.match("one two three", nil)).to be_a(RE2::MatchData)
782 end
783
784 it "raises an error when startpos exceeds INT_MAX on old RE2 ABI" do
785 skip "Underlying RE2::Match does not take int startpos" if RE2::Regexp.match_has_endpos_argument?
786 skip "size_t is not larger than a 32-bit int" if RbConfig::SIZEOF.fetch("size_t") <= (32 / 8)
787
788 re = RE2::Regexp.new('(\w+)', log_errors: false)
789
790 expect { re.match("test", start_pos: 2_147_483_648) }.to raise_error(RangeError, /startpos should be <=/)
791 end
792
793 it "raises an error when called on an uninitialized object" do
794 expect { described_class.allocate.match("test") }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
795 end
796
797 it "can be run concurrently" do
798 re = RE2::Regexp.new('(\w+)\s(\w+)')
799
800 threads = 10.times.map do
801 Thread.new { re.match("one two").values_at(1, 2) }
802 end
803
804 expect(threads.map(&:value)).to all(eq(["one", "two"]))
805 end
806 end
807
808 describe "#match?" do
809 it "returns only true or false even if there are capturing groups", :aggregate_failures do
810 re = RE2::Regexp.new('My name is (\S+) (\S+)')
811
812 expect(re.match?("My name is Alice Bloggs")).to eq(true)
813 expect(re.match?("My age is 99")).to eq(false)
814 end
815
816 it "returns false if the pattern is invalid" do
817 re = RE2::Regexp.new('???', log_errors: false)
818
819 expect(re.match?("My name is Alice Bloggs")).to eq(false)
820 end
821
822 it "raises an exception if text cannot be coerced to a string" do
823 re = RE2::Regexp.new('My name is (\S+) (\S+)')
824
825 expect { re.match?(0) }.to raise_error(TypeError)
826 end
827
828 it "raises an error when called on an uninitialized object" do
829 expect { described_class.allocate.match?("test") }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
830 end
831
832 it "can be run concurrently" do
833 re = RE2::Regexp.new('(\w+)\s(\w+)')
834
835 threads = 10.times.map do
836 Thread.new { re.match?("one two") }
837 end
838
839 expect(threads.map(&:value)).to all(eq(true))
840 end
841 end
842
843 describe "#partial_match?" do
844 it "returns only true or false even if there are capturing groups", :aggregate_failures do
845 re = RE2::Regexp.new('My name is (\S+) (\S+)')
846
847 expect(re.partial_match?("My name is Alice Bloggs")).to eq(true)
848 expect(re.partial_match?("My age is 99")).to eq(false)
849 end
850
851 it "supports matching against text containing null bytes", :aggregate_failures do
852 re = RE2::Regexp.new("a\0b")
853
854 expect(re.partial_match?("a\0b")).to eq(true)
855 expect(re.partial_match?("ab")).to eq(false)
856 end
857
858 it "returns false if the pattern is invalid" do
859 re = RE2::Regexp.new('???', log_errors: false)
860
861 expect(re.partial_match?("My name is Alice Bloggs")).to eq(false)
862 end
863
864 it "raises an exception if text cannot be coerced to a string" do
865 re = RE2::Regexp.new('My name is (\S+) (\S+)')
866
867 expect { re.partial_match?(0) }.to raise_error(TypeError)
868 end
869
870 it "raises an error when called on an uninitialized object" do
871 expect { described_class.allocate.partial_match?("test") }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
872 end
873
874 it "can be run concurrently" do
875 re = RE2::Regexp.new('(\d+)')
876
877 threads = 10.times.map do
878 Thread.new { re.partial_match?("alice 123") }
879 end
880
881 expect(threads.map(&:value)).to all(eq(true))
882 end
883 end
884
885 describe "#=~" do
886 it "returns only true or false even if there are capturing groups", :aggregate_failures do
887 re = RE2::Regexp.new('My name is (\S+) (\S+)')
888
889 expect(re =~ "My name is Alice Bloggs").to eq(true)
890 expect(re =~ "My age is 99").to eq(false)
891 end
892
893 it "supports matching against text containing null bytes", :aggregate_failures do
894 re = RE2::Regexp.new("a\0b")
895
896 expect(re =~ "a\0b").to eq(true)
897 expect(re =~ "ab").to eq(false)
898 end
899
900 it "returns false if the pattern is invalid" do
901 re = RE2::Regexp.new('???', log_errors: false)
902
903 expect(re =~ "My name is Alice Bloggs").to eq(false)
904 end
905
906 it "raises an exception if text cannot be coerced to a string" do
907 re = RE2::Regexp.new('My name is (\S+) (\S+)')
908
909 expect { re =~ 0 }.to raise_error(TypeError)
910 end
911
912 it "raises an error when called on an uninitialized object" do
913 expect { described_class.allocate =~ "test" }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
914 end
915 end
916
917 describe "#===" do
918 it "returns only true or false even if there are capturing groups", :aggregate_failures do
919 re = RE2::Regexp.new('My name is (\S+) (\S+)')
920
921 expect(re === "My name is Alice Bloggs").to eq(true)
922 expect(re === "My age is 99").to eq(false)
923 end
924
925 it "returns false if the pattern is invalid" do
926 re = RE2::Regexp.new('???', log_errors: false)
927
928 expect(re === "My name is Alice Bloggs").to eq(false)
929 end
930
931 it "raises an exception if text cannot be coerced to a string" do
932 re = RE2::Regexp.new('My name is (\S+) (\S+)')
933
934 expect { re === 0 }.to raise_error(TypeError)
935 end
936
937 it "raises an error when called on an uninitialized object" do
938 expect { described_class.allocate === "test" }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
939 end
940 end
941
942 describe "#full_match?" do
943 it "returns only true or false even if there are capturing groups", :aggregate_failures do
944 re = RE2::Regexp.new('My name is (\S+) (\S+)')
945
946 expect(re.full_match?("My name is Alice Bloggs")).to eq(true)
947 expect(re.full_match?("My name is Alice Bloggs and I am 99")).to eq(false)
948 end
949
950 it "supports matching against text containing null bytes", :aggregate_failures do
951 re = RE2::Regexp.new("a\0b")
952
953 expect(re.full_match?("a\0b")).to eq(true)
954 expect(re.full_match?("a\0bc")).to eq(false)
955 end
956
957 it "returns false if the pattern is invalid" do
958 re = RE2::Regexp.new('???', log_errors: false)
959
960 expect(re.full_match?("My name is Alice Bloggs")).to eq(false)
961 end
962
963 it "raises an exception if text cannot be coerced to a string" do
964 re = RE2::Regexp.new('My name is (\S+) (\S+)')
965
966 expect { re.full_match?(0) }.to raise_error(TypeError)
967 end
968
969 it "raises an error when called on an uninitialized object" do
970 expect { described_class.allocate.full_match?("test") }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
971 end
972
973 it "can be run concurrently" do
974 re = RE2::Regexp.new('(\w+) (\d+)')
975
976 threads = 10.times.map do
977 Thread.new { re.full_match?("alice 123") }
978 end
979
980 expect(threads.map(&:value)).to all(eq(true))
981 end
982 end
983
984 describe "#ok?" do
985 it "returns true for valid patterns", :aggregate_failures do
986 expect(RE2::Regexp.new('woo')).to be_ok
987 expect(RE2::Regexp.new('wo(o)')).to be_ok
988 expect(RE2::Regexp.new('((\d)\w+){3,}')).to be_ok
989 end
990
991 it "returns false for invalid patterns", :aggregate_failures do
992 expect(RE2::Regexp.new('wo(o', log_errors: false)).to_not be_ok
993 expect(RE2::Regexp.new('wo[o', log_errors: false)).to_not be_ok
994 expect(RE2::Regexp.new('*', log_errors: false)).to_not be_ok
995 end
996
997 it "raises an error when called on an uninitialized object" do
998 expect { described_class.allocate.ok? }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
999 end
1000 end
1001
1002 describe ".escape" do
1003 it "transforms a string into a regexp" do
1004 expect(RE2::Regexp.escape("1.5-2.0?")).to eq('1\.5\-2\.0\?')
1005 end
1006 end
1007
1008 describe ".quote" do
1009 it "transforms a string into a regexp" do
1010 expect(RE2::Regexp.quote("1.5-2.0?")).to eq('1\.5\-2\.0\?')
1011 end
1012 end
1013
1014 describe "#number_of_capturing_groups" do
1015 it "returns the number of groups in a pattern", :aggregate_failures do
1016 expect(RE2::Regexp.new('(a)(b)(c)').number_of_capturing_groups).to eq(3)
1017 expect(RE2::Regexp.new('abc').number_of_capturing_groups).to eq(0)
1018 expect(RE2::Regexp.new('a((b)c)').number_of_capturing_groups).to eq(2)
1019 end
1020
1021 it "returns -1 for an invalid pattern" do
1022 expect(RE2::Regexp.new('???', log_errors: false).number_of_capturing_groups).to eq(-1)
1023 end
1024
1025 it "raises an error when called on an uninitialized object" do
1026 expect { described_class.allocate.number_of_capturing_groups }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
1027 end
1028 end
1029
1030 describe "#named_capturing_groups" do
1031 it "returns a hash of names to indices" do
1032 expect(RE2::Regexp.new('(?P<bob>a)').named_capturing_groups).to be_a(Hash)
1033 end
1034
1035 it "maps names to indices with only one group" do
1036 groups = RE2::Regexp.new('(?P<bob>a)').named_capturing_groups
1037
1038 expect(groups).to eq("bob" => 1)
1039 end
1040
1041 it "maps names to indices with several groups" do
1042 groups = RE2::Regexp.new('(?P<bob>a)(o)(?P<rob>e)').named_capturing_groups
1043
1044 expect(groups).to eq("bob" => 1, "rob" => 3)
1045 end
1046
1047 it "returns an empty hash for an invalid regexp" do
1048 expect(RE2::Regexp.new('???', log_errors: false).named_capturing_groups).to be_empty
1049 end
1050
1051 it "raises an error when called on an uninitialized object" do
1052 expect { described_class.allocate.named_capturing_groups }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
1053 end
1054 end
1055
1056 describe "#names" do
1057 it "returns an array of names of named capturing groups" do
1058 expect(RE2::Regexp.new('(?P<bob>a)(?P<rob>b)').names).to eq(["bob", "rob"])
1059 end
1060
1061 it "returns an empty array if there are no named capturing groups" do
1062 expect(RE2::Regexp.new('(a)(b)').names).to be_empty
1063 end
1064
1065 it "returns an empty array for a pattern with no capturing groups" do
1066 expect(RE2::Regexp.new('ab').names).to be_empty
1067 end
1068
1069 it "returns an empty array for an invalid regexp" do
1070 expect(RE2::Regexp.new('???', log_errors: false).names).to be_empty
1071 end
1072
1073 it "returns UTF-8 strings if the pattern is UTF-8" do
1074 names = RE2::Regexp.new('(?P<bob>a)').names
1075
1076 expect(names.first.encoding).to eq(Encoding::UTF_8)
1077 end
1078
1079 it "returns ISO-8859-1 strings if the pattern is not UTF-8" do
1080 names = RE2::Regexp.new('(?P<bob>a)', utf8: false).names
1081
1082 expect(names.first.encoding).to eq(Encoding::ISO_8859_1)
1083 end
1084
1085 it "raises an error when called on an uninitialized object" do
1086 expect { described_class.allocate.names }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
1087 end
1088 end
1089
1090 describe "#named_captures" do
1091 it "returns a hash of names to indices" do
1092 expect(RE2::Regexp.new('(?P<bob>a)').named_captures).to eq("bob" => 1)
1093 end
1094
1095 it "maps names to indices with several groups" do
1096 groups = RE2::Regexp.new('(?P<bob>a)(o)(?P<rob>e)').named_captures
1097
1098 expect(groups).to eq("bob" => 1, "rob" => 3)
1099 end
1100
1101 it "returns an empty hash for a pattern with no named groups" do
1102 expect(RE2::Regexp.new('(a)(b)').named_captures).to be_empty
1103 end
1104
1105 it "returns an empty hash for an invalid regexp" do
1106 expect(RE2::Regexp.new('???', log_errors: false).named_captures).to be_empty
1107 end
1108
1109 it "raises an error when called on an uninitialized object" do
1110 expect { described_class.allocate.named_captures }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
1111 end
1112 end
1113
1114 describe "#scan" do
1115 it "returns a scanner" do
1116 r = RE2::Regexp.new('(\w+)')
1117 scanner = r.scan("It is a truth universally acknowledged")
1118
1119 expect(scanner).to be_a(RE2::Scanner)
1120 end
1121
1122 it "raises a type error if given invalid input" do
1123 r = RE2::Regexp.new('(\w+)')
1124
1125 expect { r.scan(nil) }.to raise_error(TypeError)
1126 end
1127
1128 it "raises an error when called on an uninitialized object" do
1129 expect { described_class.allocate.scan("test") }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
1130 end
1131 end
1132
1133 describe "#partial_match" do
1134 it "matches the pattern anywhere within the given text" do
1135 r = RE2::Regexp.new('f(o+)')
1136
1137 expect(r.partial_match("foo bar")).to be_a(RE2::MatchData)
1138 end
1139
1140 it "returns true or false if there are no capturing groups" do
1141 r = RE2::Regexp.new('fo+')
1142
1143 expect(r.partial_match("foo bar")).to eq(true)
1144 end
1145
1146 it "can set the number of submatches to extract", :aggregate_failures do
1147 r = RE2::Regexp.new('f(o+)(a+)')
1148 m = r.partial_match("fooaa bar", submatches: 1)
1149
1150 expect(m[1]).to eq("oo")
1151 expect(m[2]).to be_nil
1152
1153 m = r.partial_match("fooaa bar", submatches: 2)
1154
1155 expect(m[1]).to eq("oo")
1156 expect(m[2]).to eq("aa")
1157 end
1158
1159 it "raises an error if given non-hash options" do
1160 r = RE2::Regexp.new('f(o+)(a+)')
1161
1162 expect { r.partial_match("fooaa bar", "not a hash") }.to raise_error(TypeError)
1163 end
1164
1165 it "accepts options that can be coerced to a hash", :aggregate_failures do
1166 r = RE2::Regexp.new('f(o+)(a+)')
1167
1168 m = r.partial_match("fooaa bar", nil)
1169 expect(m[1]).to eq('oo')
1170
1171 m = r.partial_match("fooaa bar", [])
1172 expect(m[1]).to eq('oo')
1173 end
1174
1175 it "accepts anything that can be coerced to a string" do
1176 r = RE2::Regexp.new('f(o+)(a+)')
1177
1178 expect(r.partial_match(StringLike.new("fooaa bar"))).to be_a(RE2::MatchData)
1179 end
1180
1181 it "does not allow the anchor to be overridden" do
1182 r = RE2::Regexp.new('(\d+)')
1183
1184 expect(r.partial_match('ruby:1234', anchor: :anchor_both)).to be_a(RE2::MatchData)
1185 end
1186
1187 it "raises an error when called on an uninitialized object" do
1188 expect { described_class.allocate.partial_match('test') }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
1189 end
1190 end
1191
1192 describe "#full_match" do
1193 it "only matches the pattern if all of the given text matches", :aggregate_failures do
1194 r = RE2::Regexp.new('f(o+)')
1195
1196 expect(r.full_match("foo")).to be_a(RE2::MatchData)
1197 expect(r.full_match("foo bar")).to be_nil
1198 end
1199
1200 it "returns true or false if there are no capturing groups" do
1201 r = RE2::Regexp.new('fo+')
1202
1203 expect(r.full_match("foo")).to eq(true)
1204 end
1205
1206 it "can set the number of submatches to extract", :aggregate_failures do
1207 r = RE2::Regexp.new('f(o+)(a+)')
1208 m = r.full_match("fooaa", submatches: 1)
1209
1210 expect(m[1]).to eq("oo")
1211 expect(m[2]).to be_nil
1212
1213 m = r.full_match("fooaa", submatches: 2)
1214
1215 expect(m[1]).to eq("oo")
1216 expect(m[2]).to eq("aa")
1217 end
1218
1219 it "raises an error if given non-hash options" do
1220 r = RE2::Regexp.new('f(o+)(a+)')
1221
1222 expect { r.full_match("fooaa", "not a hash") }.to raise_error(TypeError)
1223 end
1224
1225 it "accepts options that can be coerced to a hash", :aggregate_failures do
1226 r = RE2::Regexp.new('f(o+)(a+)')
1227
1228 m = r.full_match("fooaa", nil)
1229 expect(m[1]).to eq("oo")
1230
1231 m = r.full_match("fooaa", [])
1232 expect(m[1]).to eq("oo")
1233 end
1234
1235 it "accepts anything that can be coerced to a string" do
1236 r = RE2::Regexp.new('f(o+)(a+)')
1237
1238 expect(r.full_match(StringLike.new("fooaa"), submatches: 0)).to eq(true)
1239 end
1240
1241 it "does not allow the anchor to be overridden" do
1242 r = RE2::Regexp.new('(\d+)')
1243
1244 expect(r.full_match('ruby:1234', anchor: :unanchored)).to be_nil
1245 end
1246
1247 it "raises an error when called on an uninitialized object" do
1248 expect { described_class.allocate.full_match('test') }.to raise_error(TypeError, /uninitialized RE2::Regexp/)
1249 end
1250 end
1251 end