def f(b: bool): try: raise MyError(b, Type.pointer(Type('int'))) except MyError as e: assert e.b == b assert e.x == Type.pointer(Type('int')) return Type('double')
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, })
def test_type_function_pointer_literal_success(): from tmppy import Type assert Type.pointer( Type.function( Type('int'), [Type('float'), Type('double')])) == Type.pointer( Type.function(Type('int'), [Type('float'), Type('double')]))
def f(x: Type): return match(x)(lambda T, U: { Type.pointer(Type.function(T, [U])): Type('double'), Type.pointer(Type.function(Type('int'), [T])): T, }, wrong_arg=True ) # error: Keyword arguments are not allowed in match
def f(x: Type): return match(x)(lambda T, U: { Type.pointer(Type.function(T, [U])): Type('double'), # note: A previous branch returning a Type was here. Type.pointer(Type.function(Type('int'), [T])): True, # error: All branches in a match\(\) must return the same type, but this branch returns a bool while a previous branch in this match expression returns a Type })
def f(y: Type): return match(Type.pointer(Type('int')), y)(lambda T, U: { (T, U): False, (Type.pointer(T), Type.pointer(U)): True, })
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
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')
def f(x: Type): return match(x)(lambda T, U: { # error: Malformed match\(\) Type.pointer(Type.function(T, [U])): Type('double'), Type.pointer(Type.function(Type('int'), [T])): T, }, {})
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')))]
def _f(t: Type): return match(t)(lambda T: { T: Type.reference(T), Type.pointer(T): Type.rvalue_reference(T), Type.pointer(Type.pointer(T)): Type.array(T), })
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, })
def test_match_multiple_success(): from tmppy import Type, match def f(y: Type): return match(Type.pointer(Type('int')), y)(lambda T, U: { (T, U): False, (Type.pointer(T), Type.pointer(U)): True, }) assert f(Type.pointer(Type.pointer(Type('double'))))
def test_match_optimization_with_multiple_specializations_chooses_more_specific_specialization(): from tmppy import Type, match def _f(t: Type): return match(t)(lambda T: { T: Type.reference(T), Type.pointer(T): Type.rvalue_reference(T), Type.pointer(Type.pointer(T)): Type.array(T), }) assert _f(Type.pointer(Type.pointer(Type('int')))) == Type.array(Type('int'))
def test_match_optimization_only_one_definition(): from tmppy import Type, match def _f(t: Type): return match(t)(lambda T: {Type.pointer(T): Type('float')}) assert _f(Type.pointer(Type('int'))) == Type('float')
def f(x: Type): return match(x)(lambda T: { T: T, Type.pointer(Type.function(Type('int'), empty_list(Type))): Type('bool'), })
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')
def test_list_comprehension_type_to_type_ok(): from tmppy import Type 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') assert [f(x) for x in [Type('int'), Type('float'), Type('double')]] == [ Type.pointer(Type('int')), Type.pointer(Type('float')), Type('void') ]
def test_match_variable_named_type_ok(): from tmppy import Type, match def f(b: bool): return match(Type('int'))(lambda type: { type: Type.pointer(type), }) assert f(True) == Type.pointer(Type('int'))
def test_match_variable_named_error_in_match_returning_type_ok(): from tmppy import Type, match def f(b: bool): return match(Type('int'))(lambda error: { error: Type.pointer(error), }) assert f(True) == Type.pointer(Type('int'))
def test_attribute_access_on_function_result_expr_success(): from tmppy import Type def f(x: Type): return x assert f( Type.template_instantiation( 'std::remove_pointer', [Type.pointer(Type('int'))])).type == Type('int')
def f(x: Type, y: Type): return match(x)(lambda T, U: { T: y, Type.pointer(Type.function(T, [U])): match(T, U)(lambda V: { (Type('int'), V): y, (Type('float'), V): Type('bool'), }), })
def test_array_type_expr_as_match_expr_not_matched_success(): from tmppy import Type, match def f(t: Type): return match(t)(lambda T: { Type.array(T): T, T: Type('double'), }) assert f(Type.pointer(Type('int'))) == Type('double')
def test_match_with_int_expr_call(): from tmppy import Type, match 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, }) assert f(Type.pointer(Type.function(Type('int'), [Type('int')]))) == 2
def test_match_with_capture_success(): from tmppy import Type, match def f(x: Type, y: Type): return match(x)(lambda T, U: { Type.pointer(Type.function(T, [U])): Type('double'), Type.pointer(Type.function(Type('int'), [T])): y, Type.pointer(Type.function(Type('float'), [T])): T, }) assert f(Type.pointer(Type.function(Type('int'), [Type('int')])), Type('bool')) == Type('bool')
def test_match_with_equality_comparison_success(): from tmppy import Type, match def f(x: Type): return match(x)(lambda T, U: { Type.pointer(Type.function(T, [U])): Type('double') == Type('int'), Type.pointer(Type.function(Type('int'), [T])): T == Type('int'), Type.pointer(Type.function(Type('float'), [T])): T == Type('int'), }) assert f(Type.pointer(Type.function(Type('int'), [Type('int')])))
def test_match_in_assignment_success(): from tmppy import Type, match 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])): T, Type.pointer(Type.function(Type('float'), [T])): T, }) return result assert f(Type.pointer(Type.function(Type('int'), [Type('int')]))) == Type('int')
def test_nested_match_with_capture(): from tmppy import Type, match def f(x: Type, y: Type): return match(x)(lambda T, U: { T: y, Type.pointer(Type.function(T, [U])): match(T, U)(lambda V: { (Type('int'), V): y, (Type('float'), V): Type('bool'), }), }) assert f(Type.pointer(Type.function(Type('int'), [Type('int')])), Type('bool')) == Type('bool')
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')
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')