def test_symbol(): assert Symbol('foo').ns is None assert Symbol('foo').rel == 'foo' assert Symbol('foo').name == 'foo' assert Symbol('./foo').ns == '.' assert Symbol('./foo').rel == 'foo' assert Symbol('./foo').name == './foo' assert Symbol('foo/bar').ns == 'foo' assert Symbol('foo/bar').rel == 'bar' assert Symbol('foo/bar').name == 'foo/bar' check_parse( 'print foo foo.bar foo.bar.baz ./foo', List([ Tuple([ Symbol('print'), Symbol('foo'), Tuple([Symbol('get'), Symbol('foo'), Symbol('bar')]), Tuple([ Symbol('get'), Tuple([Symbol('get'), Symbol('foo'), Symbol('bar')]), Symbol('baz') ]), Symbol('./foo') ]), ]), )
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_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_mixed_indented_arguments(): check_parse( """ foo :k1 v1 :k2 v2 :k3 v3 v4 v5 """, List([ Tuple([ Symbol('foo'), Keyword('k1'), Symbol('v1'), Keyword('k2'), Symbol('v2'), Keyword('k3'), Tuple([Symbol('v3')]), Tuple([ Symbol('join'), List([Tuple([Symbol('v4')]), Tuple([Symbol('v5')])]) ]) ]), ]), )
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_indent(): check_parse( """ foo "bar" """, List([ Tuple([Symbol('foo'), String('bar')]), ]), ) check_parse( """ foo "bar" 5 "baz" """, List([ Tuple([ Symbol('foo'), Tuple([ Symbol('join'), List([String('bar'), Number(5), String('baz')]) ]) ]), ]), )
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_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_interpolate(): src = 'foo "before {a} {b.c} after"' node = interpolate(parse_raw(src)) check_eq( node, List([ Tuple([ Symbol('foo'), Tuple([ Symbol('join'), List([ String('before '), Symbol('a'), String(' '), Symbol('b.c'), String(' after'), ]) ]), ]) ]), ) items = node.values[0].values[1].values[1].values check_location(src, items[0], '"before ') check_location(src, items[1], 'a') check_location(src, items[2], ' ') check_location(src, items[3], 'b.c') check_location(src, items[4], ' after"')
def test_interpolate_invalid(): check_eq( interpolate( parse_raw('foo "before {.a} {b.} {a..b} {.} ' '{..} after"')), List([ Tuple([ Symbol('foo'), String('before {.a} {b.} {a..b} {.} {..} after'), ]) ]), ) check_eq( interpolate( parse_raw('foo "before {.a} {b.} {c} {a..b} ' '{.} {..} after"')), List([ Tuple([ Symbol('foo'), Tuple([ Symbol('join'), List([ String('before {.a} {b.} '), Symbol('c'), String(' {a..b} {.} {..} after'), ]) ]), ]) ]), )
def testNested(self): self.assertPrints( Tuple([Symbol('html'), Keyword('foo'), String('bar'), Tuple([Symbol('head')])]), """ html :foo "bar" head """, )
def test_explicit_tuple(): check_parse( 'foo (bar 5) "baz"', List([ Tuple([ Symbol('foo'), Tuple([Symbol('bar'), Number(5)]), String('baz') ]), ]), )
def testJoin(self): self.assertPrints( Tuple([Symbol('html'), Keyword('foo'), String('bar'), Tuple([Symbol('join'), List([ Tuple([Symbol('head')]), Tuple([Symbol('body')]), ])])]), """ html :foo "bar" head body """, )
def test_dict(): check_parse( 'foo {:k1 v1 :k2 (v2 3)}', List([ Tuple([ Symbol('foo'), Dict([ Keyword('k1'), Symbol('v1'), Keyword('k2'), Tuple([Symbol('v2'), Number(3)]) ]) ]), ]), )
def test_list(): check_parse( 'foo [:k1 v1 1 (foo 2)]', List([ Tuple([ Symbol('foo'), List([ Keyword('k1'), Symbol('v1'), Number(1), Tuple([Symbol('foo'), Number(2)]) ]) ]), ]), )
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 testSimple(self): self.assertPrints( Tuple([Symbol('html'), Keyword('foo'), String('bar'), Symbol('baz')]), """ html :foo "bar" baz """, )
def testGet(self): self.assertPrints( Tuple([Symbol('html'), Keyword('foo'), Tuple([Symbol('get'), Symbol('bar'), Symbol('baz')])]), """ html :foo bar.baz """, ) self.assertPrints( Tuple([Symbol('html'), Keyword('foo'), Tuple([Symbol('get'), Placeholder('bar'), Symbol('baz')])]), """ html :foo #bar.baz """, )
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(self): node = self.parse( """ div "Some {var} text" """ ) self.assertEqual( Translator(self.translations).visit(node), List([Tuple([Symbol('div'), String('Какой-то {var} текст')])]), )
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_implicit_tuple(): check_parse( 'foo :bar 5 "baz"', List([ Tuple([Symbol('foo'), Keyword('bar'), Number(5), String('baz')]), ]), )
def test_indented_keywords(): check_parse( """ foo :k1 v1 :k2 v2 :k3 v3 """, List([ Tuple([ Symbol('foo'), Keyword('k1'), Symbol('v1'), Keyword('k2'), Symbol('v2'), Keyword('k3'), Tuple([Symbol('v3')]) ]), ]), )
def test_placeholder(): check_parse( 'print #foo #foo.bar #foo.bar.baz', List([ Tuple([ Symbol('print'), Placeholder('foo'), Tuple([Symbol('get'), Placeholder('foo'), Symbol('bar')]), Tuple([ Symbol('get'), Tuple([Symbol('get'), Placeholder('foo'), Symbol('bar')]), Symbol('baz') ]) ]), ]), )
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_interpolate_var_only(): src = 'foo "{a}"' node = interpolate(parse_raw(src)) check_eq( node, List([Tuple([ Symbol('foo'), Symbol('a'), ])]), ) check_location(src, node.values[0].values[1], 'a')
def test_interpolate_empty_string(): src = 'foo ""' node = interpolate(parse_raw(src)) check_eq( node, List([Tuple([ Symbol('foo'), String(''), ])]), ) check_location(src, node.values[0].values[1], '""')
def test_interpolate_first(): src = 'foo "{a} after"' node = interpolate(parse_raw(src)) check_eq( node, List([ Tuple([ Symbol('foo'), Tuple( [Symbol('join'), List([ Symbol('a'), String(' after'), ])]), ]) ]), ) items = node.values[0].values[1].values[1].values check_location(src, items[0], 'a') check_location(src, items[1], ' after"')
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_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}, )