Exemple #1
0
 def assert_no_match(self, value, *args, **kwargs):
     return super(Dict, self) \
         .assert_no_match(__unit__.Dict(*args, **kwargs), value)
Exemple #2
0
class Dict(MatcherTestCase):
    def test_invalid_arg(self):
        with self.assertRaises(TypeError):
            self.assert_match(None, of='not a pair of matchers')

    test_none = lambda self: self.assert_no_match(None)
    test_zero = lambda self: self.assert_no_match(0)
    test_empty_string = lambda self: self.assert_no_match('')
    test_empty_list = lambda self: self.assert_no_match([])
    test_empty_set = lambda self: self.assert_no_match(set())
    test_empty_tuple = lambda self: self.assert_no_match(())

    def test_empty_dict__regular(self):
        d = {}
        self.assert_match(d)
        self.assert_match(d, str, int)
        self.assert_match(d, keys=str, values=int)
        self.assert_match(d, of=(str, int))

    def test_empty_dict__custom(self):
        d = CustomDict()
        self.assert_no_match(d)
        self.assert_no_match(d, str, int)
        self.assert_no_match(d, keys=str, values=int)
        self.assert_no_match(d, of=(str, int))

    test_empty_generator = lambda self: self.assert_no_match(x for x in ())
    test_some_string = lambda self: self.assert_no_match("Alice has a cat")
    test_some_number = lambda self: self.assert_no_match(42)
    test_some_list = lambda self: self.assert_no_match([1, 2, 3, 5, 8, 13])
    test_some_set = lambda self: self.assert_no_match(set([2, 4, 6, 8, 10]))
    test_some_tuple = lambda self: self.assert_no_match(('foo', -1, ['bar']))

    def test_some_dict__regular(self):
        d = {'a': 1}
        self.assert_match(d)
        self.assert_match(d, str, int)
        self.assert_match(d, keys=str, values=int)
        self.assert_match(d, of=(str, int))

    def test_some_dict__custom(self):
        d = CustomDict({'a': 1})
        self.assert_no_match(d)
        self.assert_no_match(d, str, int)
        self.assert_no_match(d, keys=str, values=int)
        self.assert_no_match(d, of=(str, int))

    test_some_generator = lambda self: self.assert_no_match(x for x in [1, 2])
    test_some_object = lambda self: self.assert_no_match(object())

    test_repr = lambda self: self.assert_repr(__unit__.Dict())

    # Assertion functions

    def assert_match(self, value, *args, **kwargs):
        return super(Dict, self) \
            .assert_match(__unit__.Dict(*args, **kwargs), value)

    def assert_no_match(self, value, *args, **kwargs):
        return super(Dict, self) \
            .assert_no_match(__unit__.Dict(*args, **kwargs), value)