diff options
Diffstat (limited to 'tests/test_regexopt.py')
-rw-r--r-- | tests/test_regexopt.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/test_regexopt.py b/tests/test_regexopt.py index dd56a446..6322c735 100644 --- a/tests/test_regexopt.py +++ b/tests/test_regexopt.py @@ -46,6 +46,7 @@ class RegexOptTestCase(unittest.TestCase): random.randint(1, len(kwlist) - 1)) no_match = set(kwlist) - set(to_match) rex = re.compile(regex_opt(to_match)) + self.assertEqual(rex.groups, 1) for w in to_match: self.assertTrue(rex.match(w)) for w in no_match: @@ -74,3 +75,36 @@ class RegexOptTestCase(unittest.TestCase): rex = re.compile(opt) m = rex.match('abfoo') self.assertEqual(5, m.end()) + + def test_different_length_grouping(self): + opt = regex_opt(('a', 'xyz')) + print(opt) + rex = re.compile(opt) + self.assertTrue(rex.match('a')) + self.assertTrue(rex.match('xyz')) + self.assertFalse(rex.match('b')) + self.assertEqual(1, rex.groups) + + def test_same_length_grouping(self): + opt = regex_opt(('a', 'b')) + print(opt) + rex = re.compile(opt) + self.assertTrue(rex.match('a')) + self.assertTrue(rex.match('b')) + self.assertFalse(rex.match('x')) + + self.assertEqual(1, rex.groups) + groups = rex.match('a').groups() + self.assertEqual(('a',), groups) + + def test_same_length_suffix_grouping(self): + opt = regex_opt(('a', 'b'), suffix='(m)') + print(opt) + rex = re.compile(opt) + self.assertTrue(rex.match('am')) + self.assertTrue(rex.match('bm')) + self.assertFalse(rex.match('xm')) + self.assertFalse(rex.match('ax')) + self.assertEqual(2, rex.groups) + groups = rex.match('am').groups() + self.assertEqual(('a', 'm'), groups) |