Ejemplo n.º 1
0
    def test_success(self):
        from typecheck import Or, And

        # Built-in types
        self.or_type(5)
        self.or_type(7.0)

        class A(object):
            pass  # New-style classes

        class B:
            pass  # Old-style classes

        class C(A, B):
            pass

        check_type(Or(A, B), C())
        check_type(Or(A, B), A())
        check_type(Or(A, B), B())

        # Nested extension classes
        check_type(Or(A, And(A, B)), C())

        # Complex-er types
        check_type(Or((int, int), [int, float]), (5, 6))
Ejemplo n.º 2
0
    def test_combined(self):
        from typecheck import Exact, Or

        or_type = Or(Exact(5), Exact(6), Exact(7))

        for num in (5, 6, 7):
            check_type(or_type, num)
Ejemplo n.º 3
0
    def test_constructor(self):
        from typecheck import Or

        try:
            Or()
        except TypeError, e:
            assert str(e) == "__init__() takes at least 3 arguments (1 given)"
Ejemplo n.º 4
0
    def test_failure(self):
        from typecheck import _TC_TypeError, Or

        try:
            self.or_type("foo")
        except _TC_TypeError, e:
            assert e.right == Or(int, float)
            assert e.wrong == str
Ejemplo n.º 5
0
    def test_distinct_parameters(self):
        from typecheck import Or

        try:
            Or(int, int)
        except TypeError, e:
            self.assertEqual(
                str(e),
                "there must be at least 2 distinct parameters to __init__()")
Ejemplo n.º 6
0
    def test_failure_multitypes(self):
        from typecheck import Or, _TC_KeyError, _TC_TypeError

        try:
            check_type(Set([int, float]), set([4, 5, 6.0, 's']))
        except _TC_KeyError, e:
            assert e.key == 's'
            assert isinstance(e.inner, _TC_TypeError)
            assert e.inner.right == Or(int, float)
            assert e.inner.wrong == str
Ejemplo n.º 7
0
    def __init__(self, class_name, obj):
        vals = [o for o in obj]

        self.type = self
        self._type = Or(*vals)
        self.__cls = obj.__class__
        self.__vals = vals
        # This is necessary because it's a huge pain in the ass
        # to get the "raw" name of the class once it's created
        self.__cls_name = class_name
Ejemplo n.º 8
0
 def __init__(self, set_list):
     self.type = set(set_list)
     self._types = [Type(t) for t in self.type]
     
     # self._type is used to build _TC_TypeError
     if len(self._types) > 1:
         self._type = Or(*self.type)
     elif len(self._types) == 1:
         # XXX Is there an easier way to get this?
         t = self.type.pop()
         self._type = t
         self.type.add(t)
Ejemplo n.º 9
0
 def test_failure(self):
     from typecheck import typecheck, TypeCheckError, Or
     from typecheck import _TC_TypeError, _TC_IndexError
     from typecheck.mixins import _TC_IterationError
 
     MyIter = self.Iter
     foo = self.foo
     
     try:
         foo(MyIter(5, 6, 9.0, 4, 'four'))
     except TypeCheckError, e:
         assert isinstance(e.internal, _TC_IterationError)
         assert e.internal.iteration == 4
         assert isinstance(e.internal.inner, _TC_TypeError)
         assert e.internal.inner.right == Or(int, float)
         assert e.internal.inner.wrong == str
         self.assertEqual(str(e), "Argument itr: for MyIterator(5, 6, 9.0, 4, 'four'), at iteration 4 (value: 'four'), expected Or(<type 'float'>, <type 'int'>), got <type 'str'>")
Ejemplo n.º 10
0
    def test_hash(self):
        from typecheck import Xor, Or

        eq_tests = [(Xor(int, float), Xor(int, float)),
                    (Xor(int, float), Xor(int, int, int, float)),
                    (Xor(int, int, int, float), Xor(int, float)),
                    (Xor(int, float), Xor(float, int)),
                    (Xor(Xor(int, str), float), Xor(float, Xor(str, int))),
                    (Xor(Xor(int, str), float), Xor(int, str, float)),
                    (Xor(Xor(int, float), Xor(str, int)), Xor(int, float,
                                                              str))]

        ne_tests = [(Xor(float, str), Xor(int, float)),
                    (Xor(int, float, str), Xor(int, float)),
                    (Xor(int, float), Xor(int, float, str)),
                    (Xor(int, float), Or(int, float))]

        self.multipleAssertEqualHashes(eq_tests, ne_tests)
Ejemplo n.º 11
0
    def test_hash(self):
        from typecheck import And, Or

        eq_tests = [(And(int, float), And(int, float)),
                    (And(int, float), And(int, int, int, float)),
                    (And(int, int, int, float), And(int, float)),
                    (And(int, float), And(float, int)),
                    (And(And(int, str), float), And(float, And(str, int))),
                    (And(And(int, str), float), And(int, str, float)),
                    (And(And(int, float), And(str, int)), And(int, float,
                                                              str))]

        ne_tests = [(And(float, str), And(int, float)),
                    (And(int, float, str), And(int, float)),
                    (And(int, float), And(int, float, str)),
                    (And(int, float), Or(int, float))]

        self.multipleAssertEqualHashes(eq_tests, ne_tests)
Ejemplo n.º 12
0
    def test_hash(self):
        from typecheck import Not, Or

        eq_tests = [(Not(int, float), Not(int, float)),
                    (Not(int, float), Not(int, int, int, float)),
                    (Not(int, int, int, float), Not(int, float)),
                    (Not(int, float), Not(float, int)),
                    (Not(Not(int, str), float), Not(float, Not(str, int)))]

        ne_tests = [(Not(float, str), Not(int, float)),
                    (Not(int, float, str), Not(int, float)),
                    (Not(int, float), Not(int, float, str)),
                    (Not(int, float), Or(int, float)),
                    (Not(Not(int, str), float), Not(int, str, float)),
                    (Not(Not(int, float), Not(str, int)), Not(int, float,
                                                              str))]

        self.multipleAssertEqualHashes(eq_tests, ne_tests)
Ejemplo n.º 13
0
    def test_success_nested(self):
        from typecheck import Or

        check_type(Set([(int, int)]), set([(4, 5), (6, 7)]))

        check_type(Set([Or(int, float)]), set([4, 4.0, 5, 5.0]))
Ejemplo n.º 14
0
 def Xor(cond_1, cond_2):
     return Or(And(cond_1, Not(cond_2)), And(cond_2, Not(cond_1)))
Ejemplo n.º 15
0
    def test_equality(self):
        from typecheck import Or, And

        eq_tests = [(Or(int, float), Or(int, float)),
                    (Or(int, float), Or(int, int, int, float)),
                    (Or(int, int, int, float), Or(int, float)),
                    (Or(int, float), Or(float, int)),
                    (Or(Or(int, str), float), Or(float, Or(str, int))),
                    (Or(Or(int, str), float), Or(int, str, float))]

        ne_tests = [(Or(float, str), Or(int, float)),
                    (Or(int, float, str), Or(int, float)),
                    (Or(int, float), Or(int, float, str)),
                    (Or(int, float), And(int, float))]

        self.multipleAssertEqual(eq_tests, ne_tests)
Ejemplo n.º 16
0
 def or_type(obj):
     check_type(Or(int, float), obj)
Ejemplo n.º 17
0
    def test_pass(self):
        from typecheck import Exact, Or

        for obj in (5, [5, 6], Exact, Or(int, float)):
            check_type(Exact(obj), obj)