Esempio n. 1
0
 def test_check_type_None_means_success(self):
     from typecheck import Function, check_type
     
     def checker_function(*vargs):
         return None
         
     check_type(Function(checker_function), None, 66)
Esempio n. 2
0
 def test_failure(self):
     from typecheck import Single, _TC_TypeError
     
     try:
         check_type(Single(int), 7.0)
     except _TC_TypeError, e:
         assert e.right == int
         assert e.wrong == float
Esempio n. 3
0
 def __typecheck__(self, func, to_check):
     if not isinstance(to_check, Tree):
         raise _TC_TypeError(to_check, self._type)
 
     try:
         check_type(self._type, func, to_check.val)
     except _TC_Exception, e:
         raise _TC_TypeError(to_check.val, self._type.type)
Esempio n. 4
0
 def test_empty_tuple_failure(self):
     from typecheck import Tuple, _TC_TypeError
     
     try:
         check_type(Tuple(), (5, 6))
     except _TC_TypeError, e:
         assert e.wrong == (int, int)
         assert e.right == ()
Esempio n. 5
0
 def test_success(self):
     from typecheck import IsCallable
 
     Xor = self.Xor
     check_type = self.check_type
     
     check_type(Xor(dict, IsCallable()), pow)
     check_type(Xor(dict, IsCallable()), {'a': 5})
Esempio n. 6
0
    def __typecheck__(self, func, to_check):
        if not isinstance(to_check, self.__cls):
            raise _TC_TypeError(to_check, self)

        for i, item in enumerate(to_check):
            try:
                check_type(self._type, func, item)
            except _TC_Exception, e:
                raise _TC_IterationError(i, item, e)
Esempio n. 7
0
    def __typecheck__(self, func, to_check):
        if not isinstance(to_check, self.__cls):
            raise _TC_TypeError(to_check, self)

        for i, item in enumerate(to_check):
            try:
                check_type(self._type, func, item)
            except _TC_Exception, e:
                raise _TC_IterationError(i, item, e)
Esempio n. 8
0
 def test_check_type_False_means_failure(self):
     from typecheck import Function, check_type, _TC_FunctionError
     
     def checker_function(*vargs):
         return False
     try:
         check_type(Function(checker_function), None, 66)
     except _TC_FunctionError, e:
         assert e.checking_func is checker_function
         assert e.rejected_obj == 66
Esempio n. 9
0
 def test_check_type_false_values_mean_success(self):
     from typecheck import Function, check_type
     
     def checker_function(*vargs):
         return []
     check_type(Function(checker_function), None, 66)
     
     
     def checker_function(*vargs):
         return {}
     check_type(Function(checker_function), None, 66)
Esempio n. 10
0
 def test_check_type_errors_pass_through(self):
     from typecheck import Function, check_type
     
     class MyCustomException(Exception):
         pass
         
     def checker_function(*vargs):
         raise MyCustomException(*vargs)
         
     try:
         check_type(Function(checker_function), None, 66)
     except MyCustomException, e:
         assert e.args == (66,)
Esempio n. 11
0
 def test_failure(self):
     from typecheck import IsIterable, _TC_TypeError
 
     Xor = self.Xor
     check_type = self.check_type
     
     for obj in (pow, {'a': 5}):
         try:
             check_type(Xor(dict, IsIterable()), pow)
         except _TC_TypeError:
             pass
         else:
             raise AssertionError("Failed to raise _TC_TypeError")
Esempio n. 12
0
            def __typecheck__(self, func, to_check):
                if not isinstance(to_check, Tree):
                    raise _TC_TypeError(to_check, self._type)

                try:
                    typecheck.check_type(self._type, func, to_check.val)
                except _TC_Exception:
                    raise _TC_TypeError(to_check.val, self._type.type)

                for side in ('right', 'left'):
                    child = getattr(to_check, side)
                    if child is not None:
                        try:
                            typecheck.check_type(self, func, child)
                        except _TC_Exception, e:
                            raise _TC_TreeChildTypeError(side, e)
Esempio n. 13
0
    def __typecheck__(self, func, to_check):
        if not isinstance(to_check, list):
            raise _TC_TypeError(to_check, self.type)

        # lists are patterned, meaning that [int, float] requires that the
        # to-be-checked list contain an alternating sequence of integers and
        # floats. The pattern must be completed (e.g, [5, 5.0, 6, 6.0] but not
        # [5, 5.0, 6]) for the list to typecheck successfully.

        if len(to_check) % len(self._types):
            raise _TC_LengthError(len(to_check))

        pat_len = len(self._types)
        for i, val in enumerate(to_check):
            type = self._types[i % pat_len]
            try:
                check_type(type, func, val)
            except _TC_Exception, e:
                raise _TC_IndexError(i, e)
Esempio n. 14
0
 def __typecheck__(self, func, to_check):
     if not isinstance(to_check, set):
         raise _TC_TypeError(to_check, self.type)
         
     if len(self._types) == 0 and len(to_check) > 0:
         raise _TC_LengthError(len(to_check), 0)
         
     for obj in to_check:
         error = False
         for type in self._types:
             try:
                 check_type(type, func, obj)
             except _TC_Exception:
                 error = True
                 continue
             else:
                 error = False
                 break
         if error:
             raise _TC_KeyError(obj, _TC_TypeError(obj, self._type))
Esempio n. 15
0
 def test_success_userdef_classes_newstyle(self):
     from typecheck import Single
         
     class A(object): pass
     class B(A): pass
     
     check_type(Single(A), A())
     check_type(Single(A), B())
     check_type(Single(B), B())
Esempio n. 16
0
    def test_check_type_true_values_mean_success(self):
        from typecheck import Function, check_type
        
        def checker_function(*vargs):
            return True
        check_type(Function(checker_function), None, 66)
        

        def checker_function(*vargs):
            return 5
        check_type(Function(checker_function), None, 66)
        
        
        def checker_function(*vargs):
            return "abc"
        check_type(Function(checker_function), None, 66)
Esempio n. 17
0
 def lis(obj):
     check_type(List(int, float), obj)
Esempio n. 18
0
 def dic(obj):
     check_type(Dict(key=str, val=int), obj)
Esempio n. 19
0
 def dict1(obj):
     check_type(Dict( int, {int: float}), obj)
Esempio n. 20
0
 def dict2(obj):
     check_type(Dict( (int, int), float ), obj)
Esempio n. 21
0
 def tup(obj):
     check_type(Tuple(int, float, int), obj)
Esempio n. 22
0
 def test_success_builtin_types(self):
     from typecheck import Single
     
     check_type(Single(int), 7)
     check_type(Single(float), 7.0)
Esempio n. 23
0
        def checker_function(*vargs):
            return False
        try:
            check_type(Function(checker_function), None, 66)
        except _TC_FunctionError, e:
            assert e.checking_func is checker_function
            assert e.rejected_obj == 66
        else:
            raise AssertionError("Failed to raise _TC_FunctionError")
        
        
        # (0 == False) == True
        def checker_function(*vargs):
            return 0
        try:
            check_type(Function(checker_function), None, 66)
        except _TC_FunctionError, e:
            assert e.checking_func is checker_function
            assert e.rejected_obj == 66
        else:
            raise AssertionError("Failed to raise _TC_FunctionError")

def convert_mapping(mapping):
    if mapping is None:
        return None
    # k[0] is necessary because k looks like ("string", string_type)
    return dict([(k[0], v.type) for k, v in mapping.items()])

def active_mapping():
    from typecheck import TypeVariables as TV
    return convert_mapping(TV._TypeVariables__active_mapping)
Esempio n. 24
0
 def list1(obj):
     check_type(List( [int] ), obj)
Esempio n. 25
0
 def list2(obj):
     check_type(List( [[int]] ), obj)
Esempio n. 26
0
 def list3(obj):
     check_type(List( (int, float) ), obj)
Esempio n. 27
0
 def test_empty_tuple_success(self):
     from typecheck import Tuple
 
     check_type(Tuple(), tuple())
Esempio n. 28
0
 def check_type(typ, to_check):
     typecheck.check_type(typ, None, to_check)
Esempio n. 29
0
 def tup1(obj):
     check_type(Tuple( (int, int), int ), obj)
Esempio n. 30
0
def check_type(typ, obj):
    typecheck.check_type(typ, None, obj)
Esempio n. 31
0
 def tup2(obj):
     check_type(Tuple(tup1, int), obj)
Esempio n. 32
0
def check_type(typ, obj):
    typecheck.check_type(typ, None, obj)
Esempio n. 33
0
 def tup3(obj):
     check_type(Tuple( [int], [str] ), obj)