コード例 #1
0
    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)
コード例 #2
0
                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")

        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):