Esempio n. 1
0
    def test_star_match_produces_multiple_matches(self):
        pattern = (matcher.Star(1), matcher.Star(1))
        self.assertLen(list(matcher.match_all(pattern, ())), 1)
        self.assertLen(list(matcher.match_all(pattern, (1, ))), 2)
        self.assertLen(list(matcher.match_all(pattern, (1, 1))), 3)

        pattern = (matcher.Star(1), matcher.Star(1), matcher.Star(1))
        self.assertLen(list(matcher.match_all(pattern, ())), 1)
        self.assertLen(list(matcher.match_all(pattern, (1, ))), 3)
        self.assertLen(list(matcher.match_all(pattern, (1, 1))), 6)
        self.assertLen(list(matcher.match_all(pattern, (1, 1, 1))), 10)
Esempio n. 2
0
    def test_segment_must_be_the_same_when_given_same_name(self):
        pattern = (matcher.Segment('a'), matcher.Segment('a'))
        self.assertDictEqual(matcher.match(pattern, ()), {'a': ()})
        self.assertDictEqual(matcher.match(pattern, (1, 1)), {'a': (1, )})
        with self.assertRaises(matcher.MatchError):
            matcher.match(pattern, (1, 1, 1))

        pattern = (matcher.Segment('x'), matcher.Segment('y'),
                   matcher.Segment('x'))
        matches = list(matcher.match_all(pattern, (1, ) * 10))
        self.assertLen(matches, 6)
        for i in range(len(matches)):
            self.assertDictEqual(matches[i],
                                 dict(x=(1, ) * i, y=(1, ) * (10 - 2 * i)))
Esempio n. 3
0
 def test_match_all_errors_with_star_pattern(self):
     with self.assertRaises(ValueError):
         list(matcher.match_all(matcher.Star(1), 1.))
Esempio n. 4
0
 def test_segment_matches_multiple_tuple_slices(self):
     pattern = (matcher.Segment('a'), matcher.Segment('b'))
     self.assertLen(list(matcher.match_all(pattern, ())), 1)
     self.assertLen(list(matcher.match_all(pattern, (1, ))), 2)
     self.assertLen(list(matcher.match_all(pattern, (1, 1))), 3)
Esempio n. 5
0
 def test_can_match_input_to_primitive(self):
     pattern = Exp(matcher.Var('x'))
     expr = Exp(Exp(jr.Literal(0.)))
     self.assertListEqual(list(matcher.match_all(pattern, expr)),
                          [dict(x=Exp(jr.Literal(0.)))])