def testTypesMatch(self):
     # Test that when match is given a type as a pattern, it matches any
     # value of that type
     assert match([int, str, dict], [2, 'foo', {}])
     assert not match([int, str], [2, 'foo', {}]) # length matters
     assert not match([int, int, int], [2, 'foo', {}]) # type matters
     assert match(dict, {})
Exemplo n.º 2
0
 def testTypesMatch(self):
     # Test that when match is given a type as a pattern, it matches any
     # value of that type
     assert match([int, str, dict], [2, 'foo', {}])
     assert not match([int, str], [2, 'foo', {}])  # length matters
     assert not match([int, int, int], [2, 'foo', {}])  # type matters
     assert match(dict, {})
 def testAll(self):
     # Test all() match pattern
     cv = CapturedValues()
     assert match(all(cv.a, int), 4)
     self.assertEquals(4, cv.a)
     assert not match(all(cv.a, int), 9)
     assert match(all(), 4)
Exemplo n.º 4
0
 def testAll(self):
     # Test all() match pattern
     cv = CapturedValues()
     assert match(all(cv.a, int), 4)
     self.assertEquals(4, cv.a)
     assert not match(all(cv.a, int), 9)
     assert match(all(), 4)
    def testMultipleUses(self):
        # Test behaviour when a value is captured multiple times in
        # the match pattern
        cv = CapturedValues()
        assert match((cv.a, cv.b, cv.a), (1, 2, 1))
        self.assertEquals(1, cv.a)        
        self.assertEquals(2, cv.b)        

        cv = CapturedValues()
        assert not match((cv.a, cv.b, cv.a), (1, 2, 3))
Exemplo n.º 6
0
    def testMultipleUses(self):
        # Test behaviour when a value is captured multiple times in
        # the match pattern
        cv = CapturedValues()
        assert match((cv.a, cv.b, cv.a), (1, 2, 1))
        self.assertEquals(1, cv.a)
        self.assertEquals(2, cv.b)

        cv = CapturedValues()
        assert not match((cv.a, cv.b, cv.a), (1, 2, 3))
 def testDict(self):
     # Test dt() match pattern
     cv = CapturedValues()
     assert match(dt(foo=cv.foo, baz=(1, cv.baz)), 
       {'foo': 6, 'bar': 9, 'baz': (1, 3)})
     self.assertEquals(6, cv.foo)
     self.assertEquals(3, cv.baz)
 def testDeepCapture(self):
     # Test capture recursively
     cv = CapturedValues()
     assert match([[[cv.a, cv.b], (cv.c, cv.d)], 5], [[[1, 2], (3, 4)], 5])
     self.assertEquals(1, cv.a)
     self.assertEquals(2, cv.b)
     self.assertEquals(3, cv.c)
     self.assertEquals(4, cv.d)
Exemplo n.º 9
0
 def testDeepCapture(self):
     # Test capture recursively
     cv = CapturedValues()
     assert match([[[cv.a, cv.b], (cv.c, cv.d)], 5], [[[1, 2], (3, 4)], 5])
     self.assertEquals(1, cv.a)
     self.assertEquals(2, cv.b)
     self.assertEquals(3, cv.c)
     self.assertEquals(4, cv.d)
Exemplo n.º 10
0
 def testCaptureBadMatch(self):
     # Test behaviour when pattern is not matched
     cv = CapturedValues()
     assert not match([(cv.a, cv.b), int], [(5, "bar"), "foo"])
     try:
         l = cv.a
         assert False
     except KeyError:
         pass
Exemplo n.º 11
0
 def testCaptureBadMatch(self):
     # Test behaviour when pattern is not matched
     cv = CapturedValues()
     assert not match([(cv.a, cv.b), int], [(5, "bar"), "foo"])
     try:
         l = cv.a
         assert False
     except KeyError:
         pass
Exemplo n.º 12
0
 def testDict(self):
     # Test dt() match pattern
     cv = CapturedValues()
     assert match(dt(foo=cv.foo, baz=(1, cv.baz)), {
         'foo': 6,
         'bar': 9,
         'baz': (1, 3)
     })
     self.assertEquals(6, cv.foo)
     self.assertEquals(3, cv.baz)
Exemplo n.º 13
0
    def testSome(self):
        # Test some() match pattern
        cv = CapturedValues()
        assert match(some(int, str), "abc")
        assert not match(some(int, str), [])
        assert match((cv.a, (cv.b, (cv.c, ()))), (1, (2, (3, ()))))
        self.assertEquals(1, cv.a)

        cv = CapturedValues()
        assert match(some((cv.a, (cv.b, (cv.c, ()))), [cv.a, cv.b, cv.c]),
                     (1, (2, (3, ()))))
        self.assertEquals(1, cv.a)

        cv = CapturedValues()
        assert match(some((cv.a, (cv.b, (cv.c, ()))), [cv.d, cv.e, cv.f]),
                     [1, 2, 3])
        self.assertEquals(1, cv.d)

        try:
            cv.a
            assert False
        except KeyError:
            pass
Exemplo n.º 14
0
    def testSome(self):
        # Test some() match pattern
        cv = CapturedValues()
        assert match(some(int, str), "abc")
        assert not match(some(int, str), [])
        assert match((cv.a, (cv.b, (cv.c, ()))), (1, (2, (3, ()))))
        self.assertEquals(1, cv.a)
        
        cv = CapturedValues()
        assert match(some((cv.a, (cv.b, (cv.c, ()))), [cv.a, cv.b, cv.c]),
          (1, (2, (3, ()))))
        self.assertEquals(1, cv.a)

        cv = CapturedValues()
        assert match(some((cv.a, (cv.b, (cv.c, ()))), [cv.d, cv.e, cv.f]),
          [1, 2, 3])
        self.assertEquals(1, cv.d)
        
        try:
            cv.a
            assert False
        except KeyError:
            pass
Exemplo n.º 15
0
    def testObject(self):
        # Test ob() match pattern
        class Book:
            def __init__(self,
                         title,
                         author,
                         year=None,
                         publisher=None,
                         location=None):
                self.title = title
                self.author = author
                self.year = year
                self.publisher = publisher
                self.location = location

        class Film:
            pass

        jazz = Book("Tales of the Jazz Age", "F. Scott Fitzgerald", 1922)
        selfish = Book("The Selfish Gene", "Richard Dawkins", 1976, "Oxford",
                       "London")
        fifth = Book("Fifth Business", "Robertson Davies", 1970, "Penguin",
                     "Toronto")

        cv = CapturedValues()
        assert match(ob(Book, title=cv.title, author=cv.author), jazz)
        self.assertEquals("Tales of the Jazz Age", cv.title)
        self.assertEquals("F. Scott Fitzgerald", cv.author)

        cv = CapturedValues()
        assert not match(ob(Book, abc=cv.abc), jazz)

        cv = CapturedValues()
        assert match(
            ob(Book,
               title=cv.title,
               author="Richard Dawkins",
               year=pred(lambda x: x > 1970)), selfish)
        self.assertEquals("The Selfish Gene", cv.title)

        cv = CapturedValues()
        assert match(ob(title=str, author=str, year=1970), fifth)

        cv = CapturedValues()
        assert not match(
            ob(Book,
               title=cv.title,
               author="Robertson Davies",
               year=pred(lambda x: x > 1970)), selfish)

        assert not match(ob(Book), Film())
Exemplo n.º 16
0
    def testObject(self):
        # Test ob() match pattern
        class Book:
            def __init__(self, title, author, year=None, publisher=None,
              location=None):
                self.title = title
                self.author = author
                self.year = year
                self.publisher = publisher
                self.location = location

        class Film:
            pass

        jazz = Book("Tales of the Jazz Age", "F. Scott Fitzgerald", 1922)
        selfish = Book("The Selfish Gene", "Richard Dawkins",
            1976, "Oxford", "London")
        fifth = Book("Fifth Business", "Robertson Davies",
            1970, "Penguin", "Toronto")

        cv = CapturedValues()
        assert match(ob(Book, title=cv.title, author=cv.author), jazz)
        self.assertEquals("Tales of the Jazz Age", cv.title)
        self.assertEquals("F. Scott Fitzgerald", cv.author)
        
        cv = CapturedValues()
        assert not match(ob(Book, abc=cv.abc), jazz)

        cv = CapturedValues()
        assert match(ob(Book, title=cv.title, author="Richard Dawkins",
          year=pred(lambda x: x > 1970)), selfish)
        self.assertEquals("The Selfish Gene", cv.title)

        cv = CapturedValues()
        assert match(ob(title=str, author=str, year=1970), fifth)

        cv = CapturedValues()
        assert not match(ob(Book, title=cv.title, author="Robertson Davies",
          year=pred(lambda x: x > 1970)), selfish)

        assert not match(ob(Book), Film())
Exemplo n.º 17
0
 def testSequence(self):
     # Test that a tuple is matched
     assert match((1, int), (1, 2))
     assert not match((1, 2), [1, 2])
Exemplo n.º 18
0
 def testType(self):
     # Test type() match pattern
     assert match(type_is(int), 4)
     assert not match(type_is(int), int)
Exemplo n.º 19
0
 def testEq(self):
     # Test eq() match pattern
     assert match(eq('abc'), 'abc')
     assert match(eq(int), int)
     assert not match(eq(int), 4)
Exemplo n.º 20
0
 def testSequence(self):
     # Test that a tuple is matched
     assert match((1, int), (1, 2))
     assert not match((1, 2), [1, 2])
Exemplo n.º 21
0
 def testDeepSequence(self):
     # Test that a list is matched recursively
     assert match([(int, (str, dict))], [(3, ("foo", {}))])
Exemplo n.º 22
0
 def testCaptureValues(self):
     # Test capturing values from the expression
     cv = CapturedValues()
     assert match(cv.i, 3)
     self.assertEquals(3, cv.i)
Exemplo n.º 23
0
 def testDeepSequence(self):
     # Test that a list is matched recursively
     assert match([(int, (str, dict))], [(3, ("foo", {}))])
Exemplo n.º 24
0
 def testValue(self):
     # Test that equal values match.
     assert match(1, 1)
     assert not match(1, 'foo')
     assert not match(1, 2)
Exemplo n.º 25
0
 def testCaptureValues(self):
     # Test capturing values from the expression
     cv = CapturedValues()
     assert match(cv.i, 3)
     self.assertEquals(3, cv.i)
Exemplo n.º 26
0
 def testPredicate(self):
     # Test pred() match pattern
     assert match(pred(lambda x: x > 4), 7)
     assert not match(pred(lambda x: x > 4), 2)
Exemplo n.º 27
0
 def testCons(self):
     # Test cons() match pattern
     cv = CapturedValues()
     assert match(cons(cv.a, cv.b), [1, 2, 3])
     self.assertEquals(1, cv.a)
     self.assertEquals([2, 3], cv.b)
Exemplo n.º 28
0
 def testEq(self):
     # Test eq() match pattern
     assert match(eq('abc'), 'abc')
     assert match(eq(int), int)
     assert not match(eq(int), 4)
Exemplo n.º 29
0
 def testValue(self):
     # Test that equal values match.
     assert match(1, 1)
     assert not match(1, 'foo')
     assert not match(1, 2)
Exemplo n.º 30
0
 def testCons(self):
     # Test cons() match pattern
     cv = CapturedValues()
     assert match(cons(cv.a, cv.b), [1,2,3])
     self.assertEquals(1, cv.a)
     self.assertEquals([2, 3], cv.b)
Exemplo n.º 31
0
 def testPredicate(self):
     # Test pred() match pattern
     assert match(pred(lambda x: x > 4), 7)
     assert not match(pred(lambda x: x > 4), 2)
Exemplo n.º 32
0
 def testType(self):
     # Test type() match pattern
     assert match(type_is(int), 4)
     assert not match(type_is(int), int)