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)
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
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)
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 == ()
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})
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)
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
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)
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,)
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")
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)
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)
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))
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())
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)
def lis(obj): check_type(List(int, float), obj)
def dic(obj): check_type(Dict(key=str, val=int), obj)
def dict1(obj): check_type(Dict( int, {int: float}), obj)
def dict2(obj): check_type(Dict( (int, int), float ), obj)
def tup(obj): check_type(Tuple(int, float, int), obj)
def test_success_builtin_types(self): from typecheck import Single check_type(Single(int), 7) check_type(Single(float), 7.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)
def list1(obj): check_type(List( [int] ), obj)
def list2(obj): check_type(List( [[int]] ), obj)
def list3(obj): check_type(List( (int, float) ), obj)
def test_empty_tuple_success(self): from typecheck import Tuple check_type(Tuple(), tuple())
def check_type(typ, to_check): typecheck.check_type(typ, None, to_check)
def tup1(obj): check_type(Tuple( (int, int), int ), obj)
def check_type(typ, obj): typecheck.check_type(typ, None, obj)
def tup2(obj): check_type(Tuple(tup1, int), obj)
def tup3(obj): check_type(Tuple( [int], [str] ), obj)