Beispiel #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')
Beispiel #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')
Beispiel #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
Beispiel #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
     ]
Beispiel #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)
Beispiel #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')
Beispiel #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.
Beispiel #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')
Beispiel #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')
Beispiel #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')
Beispiel #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')]
Beispiel #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
Beispiel #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,
     })
Beispiel #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')))]
Beispiel #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')
Beispiel #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')
Beispiel #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')
Beispiel #18
0
 def f(x: Type):
     return match(x)({
         TypePattern('T'):
             lambda T:
                 T,
         TypePattern('int(*)()'):
             lambda:
                 Type('bool'),
     })
Beispiel #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}
Beispiel #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)
Beispiel #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.
Beispiel #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,
     })
Beispiel #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')
Beispiel #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')
Beispiel #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
     })
Beispiel #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')]
Beispiel #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')
Beispiel #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')
Beispiel #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
Beispiel #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,
     },
     {})