def test_if_named_then_else(): inc_type = Func[[IntType], IntType] check_expr_type( """ if (inc 1) :then (inc 2) :else (inc 3) """, Tuple.typed(Union[IntType, ], [ Symbol.typed(IF3_TYPE, 'if'), Tuple.typed(IntType, [ Symbol.typed(inc_type, 'inc'), Number.typed(IntType, 1), ]), Keyword('then'), Tuple.typed(IntType, [ Symbol.typed(inc_type, 'inc'), Number.typed(IntType, 2), ]), Keyword('else'), Tuple.typed(IntType, [ Symbol.typed(inc_type, 'inc'), Number.typed(IntType, 3), ]), ]), {'inc': inc_type}, )
def test_list(): foo_type = Func[[ListType[Union[IntType, StringType]]], IntType] check_expr_type( """ foo [1 2 3] """, Tuple.typed(IntType, [ Symbol.typed(foo_type, 'foo'), List.typed(ListType[Union[IntType, ]], [ Number.typed(IntType, 1), Number.typed(IntType, 2), Number.typed(IntType, 3), ]), ]), {'foo': foo_type}, ) check_expr_type( """ foo [1 2 "3"] """, Tuple.typed(IntType, [ Symbol.typed(foo_type, 'foo'), List.typed(ListType[Union[IntType, StringType]], [ Number.typed(IntType, 1), Number.typed(IntType, 2), String.typed(StringType, '3'), ]), ]), {'foo': foo_type}, ) with py.test.raises(TypeCheckError): check_expr('foo [1 2 "3"]', {'foo': Func[[ListType[IntType]], IntType]})
def test_if_some_arg(): inc_type = Func[[IntType], IntType] foo_type1 = None # to simulate TypeVar[None] foo_type2 = Record[{'bar': Option[IntType]}] env = {'inc': inc_type, 'foo': foo_type1} check_expr_type( """ if-some [x #foo.bar] (inc x) """, Tuple.typed(Option[IntType], [ Symbol.typed(IF_SOME1_TYPE, 'if-some'), List([ Symbol('x'), Tuple.typed(Option[IntType], [ Symbol.typed(GET_TYPE, 'get'), Placeholder.typed(foo_type2, 'foo'), Symbol('bar'), ]), ]), Tuple.typed(IntType, [ Symbol.typed(inc_type, 'inc'), Symbol.typed(IntType, 'x'), ]), ]), env, )
def test_if_some(): inc_type = Func[[IntType], IntType] foo_type = Record[{'bar': Option[IntType]}] env = {'inc': inc_type, 'foo': foo_type} check_expr_type( """ if-some [x foo.bar] (inc x) """, Tuple.typed(Option[IntType], [ Symbol.typed(IF_SOME1_TYPE, 'if-some'), List([ Symbol('x'), Tuple.typed(Option[IntType], [ Symbol.typed(GET_TYPE, 'get'), Symbol.typed(foo_type, 'foo'), Symbol('bar'), ]), ]), Tuple.typed(IntType, [ Symbol.typed(inc_type, 'inc'), Symbol.typed(Union[IntType, ], 'x'), ]), ]), env, ) with py.test.raises(TypeCheckError): check_expr('inc foo.bar', env)
def test_env_var(): inc_type = Func[[IntType], IntType] check_expr_type( """ inc var """, Tuple.typed(IntType, [ Symbol.typed(inc_type, 'inc'), Symbol.typed(IntType, 'var'), ]), {'inc': inc_type, 'var': IntType}, )
def test_html_tags(): check_expr_type( """ div :foo "bar" span "Some Text" """, Tuple.typed(Markup, [ Symbol.typed(HTML_TAG_TYPE, 'div'), Keyword('foo'), String.typed(StringType, 'bar'), Tuple.typed(Markup, [ Symbol.typed(HTML_TAG_TYPE, 'span'), String.typed(StringType, 'Some Text'), ]), ]), )
def test_let(): inc_type = Func[[IntType], IntType] check_expr_type( 'let [x 1] (inc x)', Tuple.typed(IntType, [ Symbol.typed(LET_TYPE, 'let'), List([ Symbol.typed(IntType, 'x'), Number.typed(IntType, 1), ]), Tuple.typed(IntType, [ Symbol.typed(inc_type, 'inc'), Symbol.typed(IntType, 'x'), ]), ]), {'inc': inc_type}, )
def test_if_some_named_else(): inc_type = Func[[IntType], IntType] dec_type = Func[[IntType], IntType] foo_type = Record[{'bar': Option[IntType]}] env = {'inc': inc_type, 'dec': dec_type, 'foo': foo_type} check_expr_type( """ if-some [x foo.bar] :then (inc x) :else (dec x) """, Tuple.typed(Union[IntType, IntType], [ Symbol.typed(IF_SOME3_TYPE, 'if-some'), List([ Symbol('x'), Tuple.typed(Option[IntType], [ Symbol.typed(GET_TYPE, 'get'), Symbol.typed(foo_type, 'foo'), Symbol('bar'), ]), ]), Keyword('then'), Tuple.typed(IntType, [ Symbol.typed(inc_type, 'inc'), Symbol.typed(Union[IntType, ], 'x'), ]), Keyword('else'), Tuple.typed(IntType, [ Symbol.typed(dec_type, 'dec'), Symbol.typed(Union[IntType, ], 'x'), ]), ]), env, )
def test_infer(): inc_type = Func[[IntType], IntType] foo_type = Func[[NamedArg['arg', IntType]], IntType] check_expr_type( """ def foo inc #arg """, Tuple.typed(foo_type, [ Symbol.typed(DEF_TYPE, 'def'), Symbol('foo'), Tuple.typed(IntType, [ Symbol.typed(inc_type, 'inc'), Placeholder.typed(IntType, 'arg'), ]) ]), {'inc': inc_type}, )
def test_if_then(): inc_type = Func[[IntType], IntType] check_expr_type( """ if (inc 1) (inc 2) """, Tuple.typed(Option[IntType], [ Symbol.typed(IF1_TYPE, 'if'), Tuple.typed(IntType, [ Symbol.typed(inc_type, 'inc'), Number.typed(IntType, 1), ]), Tuple.typed(IntType, [ Symbol.typed(inc_type, 'inc'), Number.typed(IntType, 2), ]), ]), {'inc': inc_type}, )
def test_func(): inc_type = Func[[IntType], IntType] check_expr_type( 'inc 1', Tuple.typed(IntType, [Symbol.typed(inc_type, 'inc'), Number.typed(IntType, 1)]), {'inc': inc_type}, ) inc_step_type = Func[[IntType, NamedArg['step', IntType]], IntType] check_expr_type( 'inc-step 1 :step 2', Tuple.typed(IntType, [Symbol.typed(inc_step_type, 'inc-step'), Number.typed(IntType, 1), Keyword('step'), Number.typed(IntType, 2)]), {'inc-step': inc_step_type}, ) with py.test.raises(TypeCheckError): check_expr('inc "foo"', {'inc': inc_type})
def test_env(): node, refs = get_refs( """ def foo span var """, {'var': StringType}, ) var = node.values[0].values[2].values[1] var_type = ctx_var(StringType, 'var') check_eq(var, Symbol.typed(var_type, 'var'))
def test_record(): inc_type = Func[[IntType], IntType] bar_type = Record[{'baz': IntType}] check_expr_type( """ inc bar.baz """, Tuple.typed(IntType, [ Symbol.typed(inc_type, 'inc'), Tuple.typed(IntType, [ Symbol.typed(GET_TYPE, 'get'), Symbol.typed(bar_type, 'bar'), Symbol('baz'), ]), ]), {'inc': inc_type, 'bar': bar_type}, ) with py.test.raises(TypeCheckError): check_expr('inc bar.unknown', {'inc': inc_type, 'bar': bar_type})
def test_record_infer(): bar_type = Record[{'baz': IntType}] inc_type = Func[[IntType], IntType] foo_type = Func[[NamedArg['bar', bar_type]], IntType] check_expr_type( """ def foo inc #bar.baz """, Tuple.typed(foo_type, [ Symbol.typed(DEF_TYPE, 'def'), Symbol('foo'), Tuple.typed(IntType, [ Symbol.typed(inc_type, 'inc'), Tuple.typed(IntType, [ Symbol.typed(GET_TYPE, 'get'), Placeholder.typed(bar_type, 'bar'), Symbol('baz'), ]), ]) ]), {'inc': inc_type}, )
def test_each(): inc_type = Func[[IntType], IntType] rec_type = Record[{'attr': IntType}] list_rec_type = ListType[rec_type] check_expr_type( """ each i collection inc i.attr """, Tuple.typed(Markup, [ Symbol.typed(EACH_TYPE, 'each'), Symbol.typed(rec_type, 'i'), Symbol.typed(list_rec_type, 'collection'), Tuple.typed(IntType, [ Symbol.typed(inc_type, 'inc'), Tuple.typed(IntType, [ Symbol.typed(GET_TYPE, 'get'), Symbol.typed(rec_type, 'i'), Symbol('attr'), ]), ]), ]), {'inc': inc_type, 'collection': list_rec_type}, )