Esempio n. 1
0
def test_meta_meta_function_variable_success():
    from tmppy import Type

    def f1(x: Type):
        return Type('int')

    def f2(x: bool):
        return f1

    def f3(x: Type):
        return f2

    def f4(x: Type):
        return f3

    assert f4(Type('float'))(Type('int'))(True)(Type('int')) == Type('int')
Esempio n. 2
0
def test_catch_does_not_catch_other_exception():
    from tmppy import Type
    class MyError1(Exception):
        def __init__(self, b: bool, x: Type):
            self.message = 'Something went wrong 1'
            self.b = b
            self.x = x
    class MyError2(Exception):
        def __init__(self, b: bool, x: Type):
            self.message = 'Something went wrong 2'
            self.b = b
            self.x = x
    def f(b: bool):
        if b:
            raise MyError1(b, Type.pointer(Type('int')))
        return Type('float')
    def g(b: bool):
        try:
            x = f(b)
            return x
        except MyError2 as e:
            assert e.b == b
            assert e.x == Type.pointer(Type('int'))
            return Type('double')
    assert g(True) == Type('double')
Esempio n. 3
0
def test_function_arg_named_error_in_function_returning_value_ok():
    from tmppy import Type

    def f(error: Type):
        return 2

    assert f(Type('int')) == 2
Esempio n. 4
0
 def f(x: bool):
     return [
         x,  # note: A previous list element with type bool was here.
         Type(
             'int'
         )  # error: Found different types in list elements, this is not supported. The type of this element was Type instead of bool
     ]
Esempio n. 5
0
def test_list_comprehension_from_type_list_throws_toplevel():
    from tmppy import empty_list, Type

    class MyError(Exception):
        def __init__(self, b: bool):
            self.message = 'Something went wrong'
            self.b = b

    def f(x: Type):
        if x == Type('float'):
            raise MyError(True)
        return True

    assert [f(x)
            for x in [Type('int'), Type('float'),
                      Type('double')]] == empty_list(bool)
Esempio n. 6
0
def test_function_returning_function_returning_type_with_forward_success():
    from tmppy import Type

    def f(x: Type):
        return x

    def g(b: bool):
        return f

    def g2(b: bool):
        return g(b)

    def h(x: Type):
        return g2(True)(x)

    assert h(Type('int')) == Type('int')
Esempio n. 7
0
 def f(x: Type):
     if True:
         return Type(
             'int'
         )  # note: A previous return statement returning a Type was here.
     else:
         return True  # error: Found return statement with different return type: bool instead of Type.
Esempio n. 8
0
def test_match_with_function_expr_call():
    from tmppy import Type, match
    def g(x: Type):
        return x
    def h(x: Type):
        return g
    def f(x: Type):
        return match(x)(lambda T, U: {
            Type.pointer(Type.function(T, [U])):
                h(T)(Type('double')),
            Type.pointer(Type.function(Type('int'), [T])):
                T,
            Type.pointer(Type.function(Type('float'), [T])):
                T,
        })
    assert f(Type.pointer(Type.function(Type('int'), [Type('int')]))) == Type('int')
Esempio n. 9
0
def test_match_calling_function_success():
    from tmppy import Type, match

    def id(x: Type):
        return x

    def f(x: Type):
        result = match(x)(lambda T, U: {
            Type.pointer(Type.function(T, [U])): Type('double'),
            Type.pointer(Type.function(Type('int'), [T])): id(T),
            Type.pointer(Type.function(Type('float'), [T])): T,
        })
        return result

    assert f(Type.pointer(Type.function(Type('int'),
                                        [Type('int')]))) == Type('int')
Esempio n. 10
0
def test_nested_match_success():
    from tmppy import Type, match

    def f(x: Type):
        return match(x)(lambda T, U: {
            T:
            Type('double'),
            Type.pointer(Type.function(T, [U])):
            match(T, U)(lambda V: {
                (Type('int'), V): V,
                (Type('float'), V): Type('bool'),
            }),
        })

    assert f(Type.pointer(Type.function(Type('int'),
                                        [Type('int')]))) == Type('int')
Esempio n. 11
0
def test_match_expr_extract_list_multiple_times_not_matched():
    from tmppy import Type, match

    def unpack_tuple_of_tuples(t: Type):
        return match(t)(lambda T, Ts: {
            Type.template_instantiation('std::tuple', [
                Type.template_instantiation('std::tuple', [*Ts]),
                Type.template_instantiation('std::tuple', [Type('int'), *Ts])
            ]):
            Ts,
            T: [Type('void')],
        })
    assert unpack_tuple_of_tuples(Type.template_instantiation('std::tuple',
                                                              [Type.template_instantiation('std::tuple', [Type('float'), Type('double')]),
                                                               Type.template_instantiation('std::tuple', [Type('int'), Type('double'), Type('double')])])) \
        == [Type('void')]
Esempio n. 12
0
 def f(x: Type):
     if True:
         y = Type(
             'int')  # note: A previous definition with type Type was here.
     else:
         y = True  # error: The variable y is defined with type bool here, but it was previously defined with type Type in another branch.
     return True
Esempio n. 13
0
 def f(x: Type):
     return match(x)(lambda T, U: {
         Type.pointer(Type.function(T, [U])):
             1,
         Type.pointer(Type.function(Type('int'), [T])):
             2,
         Type.pointer(Type.function(Type('float'), [T])):
             3,
     })
Esempio n. 14
0
def test_optimization_multiple_list_comprehensions():
    from typing import List
    from tmppy import Type
    def f(l0: List[Type]):
        l1 = [Type.pointer(x) for x in l0]
        l2 = [Type.const(x) for x in l1]
        return l2
    assert f([Type('int'), Type('float')]) == [Type.const(Type.pointer(Type('int'))),
                                               Type.const(Type.pointer(Type('float')))]
Esempio n. 15
0
def test_variable_named_error_in_function_returning_type_ok():
    from tmppy import Type

    def f(b: bool):
        error = Type('int')
        return Type('float')

    assert f(True) == Type('float')
Esempio n. 16
0
def test_variable_named_type_ok():
    from tmppy import Type

    def f(b: bool):
        type = Type('int')
        return Type('float')

    assert f(True) == Type('float')
Esempio n. 17
0
 def f(x: Type):
     if x == Type('int'):
         return Type.pointer(Type('int'))
     elif x == Type('float'):
         return Type.pointer(Type('float'))
     else:
         return Type('void')
Esempio n. 18
0
 def f(x: Type):
     return match(x)({
         TypePattern('T'):
             lambda T:
                 T,
         TypePattern('int(*)()'):
             lambda:
                 Type('bool'),
     })
Esempio n. 19
0
def test_function_returning_function_returning_set_success():
    from tmppy import Type
    def f(x: bool):
        return {x}
    def g(b: Type):
        return f
    def h(x: Type):
        return g(x)(True)
    assert h(Type('int')) == {True}
Esempio n. 20
0
def test_match_expr_extract_list_empty():
    from tmppy import Type, match, empty_list

    def unpack_tuple(t: Type):
        return match(t)(lambda Ts: {
            Type.template_instantiation('std::tuple', [*Ts]): Ts
        })
    assert unpack_tuple(Type.template_instantiation('std::tuple', empty_list(Type))) \
        == empty_list(Type)
Esempio n. 21
0
def test_attribute_access_on_function_error():
    from tmppy import Type

    def f(x: Type):
        return x

    assert f.type == Type(
        'int'
    )  # error: Attribute access is not supported for values of type \(Type\) -> Type.
Esempio n. 22
0
 def f(y: Type):
     return match(Type('int*'), y)({
         TypePattern('T', 'U'):
             lambda T, U:
                 False,
         TypePattern('T*', 'U*'):
             lambda T, U:
                 True,
     })
Esempio n. 23
0
def test_if_returns_success():
    from tmppy import Type

    def f(x: bool):
        if x:
            return Type('int')
        return Type('float')

    assert f(True) == Type('int')
Esempio n. 24
0
def test_match_calling_function_success():
    from tmppy import Type, TypePattern, match
    def id(x: Type):
        return x
    def f(x: Type):
        result = match(x)({
            TypePattern('T(*)(U)'):
                lambda T, U:
                    Type('double'),
            TypePattern('int(*)(T)'):
                lambda T:
                    id(T),
            TypePattern('float(*)(T)'):
                lambda T:
                    T,
        })
        return result
    assert f(Type('int(*)(int)')) == Type('int')
Esempio n. 25
0
 def f(x: Type):
     return match(x)({
         TypePattern('T(*)(U)'):
             lambda T, U:
                 Type('double'),  # note: A previous lambda returning a Type was here.
         TypePattern('int(*)(T)'):
             lambda T:
                 True,  # error: All lambdas in a match\(...\)\({...}\) expression should return the same type, but this lambda returns a bool while a previous lambda in this match expression returns a Type
     })
Esempio n. 26
0
def test_list_comprehension_type_to_const_type_ok():
    from tmppy import Type
    assert [
        Type('float')
        for x in [Type('int'), Type('float'),
                  Type('double')]
    ] == [Type('float'), Type('float'),
          Type('float')]
Esempio n. 27
0
def test_nested_match_with_capture():
    from tmppy import Type, TypePattern, match
    def f(x: Type, y: Type):
        return match(x)({
            TypePattern('T'):
                lambda T:
                    y,
            TypePattern('T(*)(U)'):
                lambda T, U:
                    match(T, U)({
                        TypePattern('int', 'V'):
                            lambda V:
                                y,
                        TypePattern('float', 'V'):
                            lambda V:
                                Type('bool'),
                    }),
        })
    assert f(Type('int(*)(int)'), Type('bool')) == Type('bool')
Esempio n. 28
0
def test_match_with_function_expr_call():
    from tmppy import Type, TypePattern, match
    def g(x: Type):
        return x
    def h(x: Type):
        return g
    def f(x: Type):
        return match(x)({
            TypePattern('T(*)(U)'):
                lambda T, U:
                    h(T)(Type('double')),
            TypePattern('int(*)(T)'):
                lambda T:
                    T,
            TypePattern('float(*)(T)'):
                lambda T:
                    T,
        })
    assert f(Type('int(*)(int)')) == Type('int')
Esempio n. 29
0
 def f(x: Type):
     return match(x)({
         TypePattern('T(*)(U)'):
             lambda T, U:
                 Type('double'),
         TypePattern('int(*)(T)'):
             lambda T:
                 T,
     },
     wrong_arg=True)  # error: Keyword arguments are not allowed in match
Esempio n. 30
0
 def f(x: Type):
     return match(x)({  # error: Malformed match\(...\)\({...}\)
         TypePattern('T(*)(U)'):
             lambda T, U:
                 Type('double'),
         TypePattern('int(*)(T)'):
             lambda T:
                 T,
     },
     {})