Beispiel #1
0
 def test_simple_pattern(self):
     p = pm.build_pattern('<foo>', '<bar>', '<toto>')
     self.assertIsNotNone(p.match(['foo', 'bar', 'toto']))
     self.assertIsNone(p.match(['foo', 'bar', 'titi']))
     p = pm.build_pattern('<c:foo>', foo=lambda x: x < 2)
     self.assertIsNotNone(p.match([1]))
     self.assertIsNone(p.match([4]))
Beispiel #2
0
 def test_simple_p(self):
     a = pm.build_pattern('<string>')
     self.assertIsInstance(a, pm.PatternSeq)
     self.assertIsInstance(a.patterns[0], pm.PatternString)
     self.assertEqual(a.patterns[0].value, 'string')
     a = pm.build_pattern('<s1><s2>')
     self.assertIsInstance(a, pm.PatternSeq)
     self.assertIsInstance(a.patterns[0], pm.PatternString)
     self.assertEqual(a.patterns[0].value, 's1')
     self.assertEqual(len(a.patterns), 2)
     self.assertEqual(a.patterns[1].value, 's2')
Beispiel #3
0
 def test_detect_cycle2(self):
     g = pm.build_pattern(r'.*(<a>.<b>)(.*?\1)')
     m = g.match(list('rtaebqwaeb'))
     self.assertIsNotNone(m)
     self.assertEqual(m.group(2), ['q', 'w', 'a', 'e', 'b'])
     m = g.match(list('a1bcva'))
     self.assertIsNone(m)
Beispiel #4
0
 def test_incremental_basic(self):
     pat = pm.build_pattern('a', 'b', 'c')
     a = pm.TokenProvider(['a'])
     self.assertIsNotNone(pat._match(a))
     self.assertIsNone(pat.match(a.fork()))
     a.append_tokens('b', 'c')
     self.assertIsNotNone(pat._match(a))
Beispiel #5
0
    def test_functions(self):
        @pm.F
        def is_str(x):
            return isinstance(x, str)

        pat = pm.build_pattern('.*(<c:is_str><c:is_int>)', is_str=is_str)
        self.assertEqual(
            pat.match([2, 2, 4, 'toto', 6, 8]).groups(), [['toto', 6]])
        self.assertIsNone(pat.match([2, 2, 4, 'toto', [], 1]))
Beispiel #6
0
 def test_empty(self):
     pat = pm.build_pattern(r"(<>*$")
     pat2 = pm.PatternSeq.make([
         pm.PatternCapture(
             pm.PatternSeq(
                 [pm.PatternStarGreedy(pm.PatternString('')), pm.END]))
     ])
     self.assertEqual(repr(pat), repr(pat2))
     self.assertEqual(pat2.match(['', '']).groups(), [['', '']])
Beispiel #7
0
    def test_functions2(self):
        @pm.F
        def is_str(x):
            return isinstance(x, str)

        pat = pm.build_pattern('.*(',
                               is_str | pm.is_int,
                               ')',
                               lambda x: x == 6,
                               is_str=is_str)
        self.assertEqual(
            pat.match([2, 2, 4, 'toto', 6, 8]).groups(), [['toto']])
        self.assertIsNone(pat.match([2, 2, 4, 'toto', [], 6, 1]))
Beispiel #8
0
    def test_functions4(self):
        @pm.F
        def is_str(x):
            return isinstance(x, str)

        pat = pm.build_pattern('.*(',
                               is_str & (lambda s: s.startswith('hello')),
                               lambda x: x == 6,
                               ')',
                               is_str=is_str)
        self.assertEqual(
            pat.match([2, 2, 4, 'hello world', 6, 8]).groups(),
            [['hello world', 6]])
        self.assertIsNone(pat.match([2, 2, 4, 'toto', [], 6, 1]))
Beispiel #9
0
 def test_detect_cycle(self):
     g = pm.build_pattern(r'(.).*?\1')
     m = g.match(list('abcvc'))
     self.assertIsNone(m)
     m = g.match(list('abcva'))
     self.assertIsNotNone(m)
Beispiel #10
0
 def test_named_capture(self):
     pat = pm.build_pattern('.*(?P<toto><re:bonj.ur>).*')
     a = pat.match(['titi', 'bonjaur'])
     self.assertIsNotNone(a)
     self.assertEqual(a.groupdict(), {'toto': ['bonjaur']})
Beispiel #11
0
 def test_empty2(self):
     pat = pm.build_pattern(r"(<>*(?P<commentaire>", (lambda x: x != ''),
                            r")<>*$)")
     self.assertEqual(
         pat.match(['', 'toto']).groupdict(), {'commentaire': ['toto']})
Beispiel #12
0
 def test_functional(self):
     pat = pm.build_pattern('(', pm.is_num, '*)$')
     self.assertIsNotNone(pat.match(['1.5', '1.2']))
Beispiel #13
0
 def test_1(self):
     pat = pm.build_pattern(
         r"(<re:[\d/]+><re:^(?![\\d%, ]+$).*>*<re:^[\d% ,]+$>*$)")
     self.assertIsNotNone(pat.match(['01/02/2000', 'toto', '10.00']))
     pat = pm.build_pattern(r"(<><re:.+><>*$)")
     self.assertIsNotNone(pat.match(['', 'toto', '']))
Beispiel #14
0
 def test_regex(self):
     a = pm.build_pattern('<re:toto><r:titi>')
     self.assertIsInstance(a.patterns[0], pm.PatternRegex)
     self.assertIsNotNone(a.patterns[0].value.match('toto'))
     self.assertIsInstance(a.patterns[1], pm.PatternRegex)
     self.assertIsNotNone(a.patterns[1].value.match('titi'))
Beispiel #15
0
 def test_P(self):
     p = pm.build_pattern('<a>')
     m = p.match('a')
     self.assertIsNotNone(m)
Beispiel #16
0
 def test_example(self):
     example_list = ['a', 'a', 1, '', None, 'b', 'c', 'e']
     g = pm.build_pattern((~pm.is_num).star(False), '(', pm.is_num, ')',
                          '.*', pm.is_none, '(', pm.ANY, pm.ANY, ')')
     self.assertEqual(g.match(example_list).groups(), [[1], ['b', 'c']])
     self.assertIsNone(g.match(['a', 'a', 'b', '', None, 'b', 'c', 'e']))