MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_re_groupref_exists

Method test_re_groupref_exists

Lib/test/test_re.py:637–665  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

635 re.compile(r".*?").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3))
636
637 def test_re_groupref_exists(self):
638 self.assertEqual(re.match(r'^(\()?([^()]+)(?(1)\))$', '(a)').groups(),
639 ('(', 'a'))
640 self.assertEqual(re.match(r'^(\()?([^()]+)(?(1)\))$', 'a').groups(),
641 (None, 'a'))
642 self.assertIsNone(re.match(r'^(\()?([^()]+)(?(1)\))$', 'a)'))
643 self.assertIsNone(re.match(r'^(\()?([^()]+)(?(1)\))$', '(a'))
644 self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups(),
645 ('a', 'b'))
646 self.assertEqual(re.match(r'^(?:(a)|c)((?(1)b|d))$', 'cd').groups(),
647 (None, 'd'))
648 self.assertEqual(re.match(r'^(?:(a)|c)((?(1)|d))$', 'cd').groups(),
649 (None, 'd'))
650 self.assertEqual(re.match(r'^(?:(a)|c)((?(1)|d))$', 'a').groups(),
651 ('a', ''))
652
653 # Tests for bug #1177831: exercise groups other than the first group
654 p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))')
655 self.assertEqual(p.match('abc').groups(),
656 ('a', 'b', 'c'))
657 self.assertEqual(p.match('ad').groups(),
658 ('a', None, 'd'))
659 self.assertIsNone(p.match('abd'))
660 self.assertIsNone(p.match('ac'))
661
662 # Support > 100 groups.
663 pat = '|'.join('x(?P<a%d>%x)y' % (i, i) for i in range(1, 200 + 1))
664 pat = '(?:%s)(?(200)z)' % pat
665 self.assertEqual(re.match(pat, 'xc8yz').span(), (0, 5))
666
667 def test_re_groupref_exists_errors(self):
668 self.checkPatternError(r'(?P<a>)(?(0)a|b)', 'bad group number', 10)

Callers

nothing calls this directly

Calls 7

assertIsNoneMethod · 0.80
spanMethod · 0.80
assertEqualMethod · 0.45
groupsMethod · 0.45
matchMethod · 0.45
compileMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected