def test_yield_fail(self): from typecheck import yields from typecheck import TypeCheckError, _TC_IndexError, _TC_TypeError from typecheck import calculate_type, _TC_GeneratorError from typecheck import TypeVariables gen_mappings = TypeVariables._TypeVariables__gen_mappings class Bad: pass bad = Bad() @yields('a', int, 'a') def foo(a, b, c): yield a, b, bad class A: pass a = A() for args in ((5, 5, 5), (5.0, 5, 7.0), ([4, 5], 5, [8, 9, 10]), (a, 5, a)): g = foo(*args) try: assert g.next() == args except TypeCheckError, e: assert isinstance(e.internal, _TC_GeneratorError) assert e.internal.yield_no == 1 assert isinstance(e.internal.inner, _TC_IndexError) assert e.internal.inner.index == 2 assert isinstance(e.internal.inner.inner, _TC_TypeError) assert e.internal.inner.inner.right == calculate_type(args[0]) assert e.internal.inner.inner.wrong == Bad assert len(gen_mappings) == 1 assert convert_mapping(gen_mappings.values()[0]) == { 'a': calculate_type(args[0]) } else: raise AssertionError( "Failed to raise TypeCheckError at the right place") try: g.next() except StopIteration: assert TypeVariables._TypeVariables__mapping_stack == [] assert TypeVariables._TypeVariables__active_mapping is None assert len(gen_mappings) == 0 else: raise AssertionError( "Failed to raise StopIteration at the right place")
def test_unicode_tvars(self): from typecheck import accepts, calculate_type from typecheck import TypeCheckError, _TC_TypeError from typecheck import TypeVariables class Bad: pass bad = Bad() @accepts(u'a', int, u'a') def foo(a, b, c): return a, b, c class A: pass a = A() for args in ((5, 5, bad), (5.0, 5, bad), ([4, 5], 5, bad), (a, 5, bad)): try: foo(*args) except TypeCheckError, e: assert isinstance(e.internal, _TC_TypeError) assert e.internal.right == calculate_type(args[0]) assert e.internal.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")
def run_test(dec_1, dec_2): @dec_1 @dec_2 def foo(a, b, c, d, e): return a, b, c, bad, e class A: pass class B: pass a = A() b = B() for args in ((5, 5.0, 6, 7, 7.0), ('a', 4, 6, 'b', 6), (a, b, 5, a, b)): try: 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")
def test_yield_fail(self): from typecheck import yields from typecheck import TypeCheckError, _TC_IndexError, _TC_TypeError from typecheck import calculate_type, _TC_GeneratorError from typecheck import TypeVariables gen_mappings = TypeVariables._TypeVariables__gen_mappings class Bad: pass bad = Bad() @yields('a', int, 'a') def foo(a, b, c): yield a, b, bad class A: pass a = A() for args in ((5, 5, 5), (5.0, 5, 7.0), ([4, 5], 5, [8, 9, 10]), (a, 5, a)): g = foo(*args) try: assert g.next() == args except TypeCheckError, e: assert isinstance(e.internal, _TC_GeneratorError) assert e.internal.yield_no == 1 assert isinstance(e.internal.inner, _TC_IndexError) assert e.internal.inner.index == 2 assert isinstance(e.internal.inner.inner, _TC_TypeError) assert e.internal.inner.inner.right == calculate_type(args[0]) assert e.internal.inner.inner.wrong == Bad assert len(gen_mappings) == 1 assert convert_mapping(gen_mappings.values()[0]) == {'a': calculate_type(args[0])} else: raise AssertionError("Failed to raise TypeCheckError at the right place") try: g.next() except StopIteration: assert TypeVariables._TypeVariables__mapping_stack == [] assert TypeVariables._TypeVariables__active_mapping is None assert len(gen_mappings) == 0 else: raise AssertionError("Failed to raise StopIteration at the right place")
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 bad, b, c, bad, e class A: pass class B: pass a = A() b = B() problem_set = [0] * 3 problem_set[0] = ((5, 5.0, 6, 7, 7.0), {'a': int, 'b': float}) problem_set[1] = (('a', 4, 6, 'b', 6), {'a': str, 'b': int}) problem_set[2] = ((a, b, 5, a, b), {'a': A, 'b': B}) for args, mapping in problem_set: gen = foo(*args) try: assert gen.next() == args except TypeCheckError, e: assert isinstance(e.internal, _TC_GeneratorError) assert e.internal.yield_no == 1 assert isinstance(e.internal.inner, _TC_IndexError) assert e.internal.inner.index == 0 assert isinstance(e.internal.inner.inner, _TC_TypeError) assert e.internal.inner.inner.wrong == Bad assert e.internal.inner.inner.right == calculate_type( args[0]) assert TypeVariables._TypeVariables__mapping_stack == [] assert TypeVariables._TypeVariables__active_mapping == None assert len(gen_mappings) == 1 assert convert_mapping(gen_mappings.values()[0]) == mapping try: gen.next() except StopIteration: assert TypeVariables._TypeVariables__mapping_stack == [] assert TypeVariables._TypeVariables__active_mapping is None assert len(gen_mappings) == 0 else: raise AssertionError( "Failed to raise StopIteration at the right time")
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 bad, b, c, bad, e class A: pass class B: pass a = A() b = B() problem_set = [0]*3 problem_set[0] = ((5, 5.0, 6, 7, 7.0), {'a': int, 'b': float}) problem_set[1] = (('a', 4, 6, 'b', 6), {'a': str, 'b': int}) problem_set[2] = ((a, b, 5, a, b), {'a': A, 'b': B}) for args, mapping in problem_set: gen = foo(*args) try: assert gen.next() == args except TypeCheckError, e: assert isinstance(e.internal, _TC_GeneratorError) assert e.internal.yield_no == 1 assert isinstance(e.internal.inner, _TC_IndexError) assert e.internal.inner.index == 0 assert isinstance(e.internal.inner.inner, _TC_TypeError) assert e.internal.inner.inner.wrong == Bad assert e.internal.inner.inner.right == calculate_type(args[0]) assert TypeVariables._TypeVariables__mapping_stack == [] assert TypeVariables._TypeVariables__active_mapping == None assert len(gen_mappings) == 1 assert convert_mapping(gen_mappings.values()[0]) == mapping try: gen.next() except StopIteration: assert TypeVariables._TypeVariables__mapping_stack == [] assert TypeVariables._TypeVariables__active_mapping is None assert len(gen_mappings) == 0 else: raise AssertionError("Failed to raise StopIteration at the right time")