def test_add_accepts_to_naked_function_4(): """Adding accepts to a naked function: kw params, kw types""" def foo(kw_1=5, kw_2=6, kw_3=7): pass t = time.time() for _ in xrange(0, 10000): accepts(kw_1=int, kw_2=int, kw_3=int)(foo) return time.time() - t
def test_add_accepts_to_naked_function_3(): """Adding accepts to a naked function: pos params, kw types""" def foo(int_1, int_2, int_3): pass t = time.time() for _ in xrange(0, 10000): accepts(int_2=int, int_1=int, int_3=int)(foo) return time.time() - t
def test_add_accepts_to_naked_function_2(): """Adding accepts to a naked function: multi positional params""" def foo(int_1, int_2, int_3): pass t = time.time() for _ in xrange(0, 10000): accepts(int, int, int)(foo) return time.time() - t
def test_add_accepts_to_naked_function_1(): """Adding accepts to a naked function: single positional param""" def foo(int_1): pass t = time.time() for _ in xrange(0, 10000): accepts(int)(foo) return time.time() - t
def test_add_accepts_to_naked_function_4(): """Adding accepts to a naked function: kw params, kw types""" def foo(kw_1=5, kw_2=6, kw_3=7): pass t = time.time() for i in range(0, 10000): accepts(kw_1=int, kw_2=int, kw_3=int)(foo) return time.time() - t
def test_add_accepts_to_naked_function_3(): """Adding accepts to a naked function: pos params, kw types""" def foo(int_1, int_2, int_3): pass t = time.time() for i in range(0, 10000): accepts(int_2=int, int_1=int, int_3=int)(foo) return time.time() - t
def test_add_accepts_to_naked_function_2(): """Adding accepts to a naked function: multi positional params""" def foo(int_1, int_2, int_3): pass t = time.time() for i in range(0, 10000): accepts(int, int, int)(foo) return time.time() - t
def test_add_accepts_to_naked_function_1(): """Adding accepts to a naked function: single positional param""" def foo(int_1): pass t = time.time() for i in range(0, 10000): accepts(int)(foo) return time.time() - t
def test_recursive_functions(self): from typecheck import returns, accepts from typecheck import TypeVariables mapping_stack = TypeVariables._TypeVariables__mapping_stack mappings = [None, {'a': str}, {'a': (int, int)}, {'a': float}, None] stack = list(mappings) stack.reverse() stack.pop() def check(n): assert active_mapping() == mappings[n] assert [convert_mapping(m) for m in mapping_stack] == stack[:-n] def run_test(dec_1, dec_2): @dec_1 @dec_2 def foo(obj_1, obj_2, n): if n == 1: check(n) return [obj_1, obj_2] elif n == 2: check(n) foo('jabber', 'wocky', n-1) check(n) return [(4, 5), (6, 7)] elif n == 3: check(n) foo((5, 6), (7, 8), n-1) check(n) return [obj_1, obj_2] assert foo(6.0, 7.0, 3) == [6.0, 7.0] assert mapping_stack == [] assert TypeVariables._TypeVariables__active_mapping == None assert len(TypeVariables._TypeVariables__gen_mappings) == 0 t_r = returns(['a']) t_a = accepts('a', 'a', int) run_test(t_r, t_a) run_test(t_a, t_r)
foo(*args) except TypeCheckError, e: assert isinstance(e.internal, _TC_IndexError) assert e.internal.index == 3 assert isinstance(e.internal.inner, _TC_TypeError) assert e.internal.inner.right == calculate_type(args[0]) assert e.internal.inner.wrong == Bad assert TypeVariables._TypeVariables__mapping_stack == [] assert TypeVariables._TypeVariables__active_mapping is None assert len(TypeVariables._TypeVariables__gen_mappings) == 0 else: raise AssertionError("Failed to raise TypeCheckError at the proper place") t_r = returns('a', 'b', int, 'a', 'b') t_a = accepts('a', 'b', int, 'a', 'b') run_test(t_r, t_a) run_test(t_a, t_r) def test_args_yield_pass(self): from typecheck import yields, accepts from typecheck import TypeVariables gen_mappings = TypeVariables._TypeVariables__gen_mappings def run_test(dec_1, dec_2): @dec_1('a', 'b', int, 'a', 'b') @dec_2('a', 'b', int, 'a', 'b') def foo(a, b, c, d, e): yield a, b, c, d, e