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 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_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_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_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_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_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_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_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_implicit_tuple(): check_parse( 'foo :bar 5 "baz"', List([ Tuple([Symbol('foo'), Keyword('bar'), Number(5), String('baz')]), ]), )
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_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_explicit_tuple(): check_parse( 'foo (bar 5) "baz"', List([ Tuple([ Symbol('foo'), Tuple([Symbol('bar'), Number(5)]), String('baz') ]), ]), )
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_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_parser_interpolate(): node = parse(""" foo "bar {value} baz" """) check_eq( node, List([ Tuple([ Symbol('foo'), Tuple([ Symbol('join'), List([ String('bar '), Symbol('value'), String(' baz'), ]) ]), ]), ]), )
def test_nested_indent(): check_parse( """ foo bar 1 baz 2 """, List([ Tuple([ Symbol('foo'), Tuple([ Symbol('join'), List([ Tuple([Symbol('bar'), Number(1)]), Tuple([Symbol('baz'), Number(2)]) ]) ]) ]), ]), )
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_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_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') ]) ]), ]), )