MCPcopy Index your code
hub / github.com/python/cpython / test_re_groupref_exists

Method test_re_groupref_exists

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected