def test_end2(self): g = pm._build_pattern('a', '?', '{2}', 'b', '$') self.assertIsNotNone(g.match(list('b'))) self.assertIsNotNone(g.match(list('aab'))) self.assertIsNotNone(g.match(list('ab'))) self.assertIsNone(g.match(list('bc'))) self.assertIsNone(g.match(list('aaab')))
def test_capture2(self): g = pm._build_pattern('a', '(', '(', 'b', 'c', ')', '*', ')', 'd') m = g.match(list('abcbcd')) self.assertIsNotNone(m) self.assertEqual(m.groups(), [['b', 'c', 'b', 'c'], ['b', 'c']]) m = g.match(list('ad')) self.assertIsNotNone(m) self.assertEqual(m.groups(), [[], None])
def test_opt1(self): g = pm._build_pattern('a', '?', 'b') self.assertIsNotNone(g.match(list('ab'))) self.assertIsNotNone(g.match(list('b'))) self.assertIsNone(g.match(list('aab')))
def test_or2(self): g = pm._build_pattern('a', '|', '(', 'b', 'd', ')', 'c') self.assertIsNotNone(g.match(list('ac'))) self.assertIsNotNone(g.match(list('bdc'))) self.assertIsNone(g.match(list('bd')))
def test_or1(self): g = pm._build_pattern('a', '|', 'b', 'd') self.assertIsNotNone(g.match(list('ad'))) self.assertIsNone(g.match(list('ab')))
def test_capture_backref(self): g = pm._build_pattern('(', '.', ')', 'b', '\\1') m = g.match(list('aba')) self.assertIsNotNone(m)
def test_capture(self): g = pm._build_pattern('(', 'a', ')') self.assertIsNotNone(g.match(list('a'))) self.assertEqual(g.match(list('a')).groups(), [['a']])
def test_star5(self): g = pm._build_pattern('a', '*', 'b', 'c', '*', 'd', 'e', '*') self.assertIsNotNone(g.match(list('bcd'))) self.assertIsNotNone(g.match(list('aaaabccccccccd')))
def test_star3(self): gng = pm._build_pattern('<', '(', pm.ANY, '*?', ')', '>') g = pm._build_pattern('<', '(', pm.ANY, '*', ')', '>') s = list('<body></body>') self.assertEqual([list('body></body')], g.match(s).groups()) self.assertEqual([list('body')], gng.match(s).groups())
def test_inversible(self): g = pm._build_pattern(pm.PatternStarNonGreedy(pm.ANY), ~pm.is_num) self.assertIsNotNone(g.match([1, 2, 3, ''])) self.assertIsNone(g.match([1, 2, 3, 4]))
def test_mix2(self): g = pm._build_pattern('a', '(', 'b', '|', 'c', 'o', '*', ')', '{2}', 'd') self.assertIsNotNone(g.match(list('acoobd'))) self.assertEqual(repr(g), '[a,([b|[c],o*]){[2]},d]')
def test_mix1(self): g = pm._build_pattern('(', '.', '*?', 'a', ')', '{2}', 'd') self.assertIsNotNone(g.match(list('aad')))
def test_end(self): self.assertIsNotNone(pm._build_pattern(pm.END).match([])) self.assertIsNotNone(pm._build_pattern(pm.ANY, pm.END).match(['a'])) self.assertIsNone(pm._build_pattern(pm.END).match(['a', 'b']))
def test_mul2(self): g = pm._build_pattern('a', '{2}', 'b') self.assertIsNotNone(g.match(list('aab'))) self.assertIsNone(g.match(list('ab')))