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_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 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_repr(self): from typecheck import Function def foo(*vargs): return vargs assert repr(Function(foo)) == "Function(%s)" % foo
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_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 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)