예제 #1
0
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"')
예제 #2
0
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},
    )
예제 #3
0
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]})
예제 #4
0
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'),
                    ])
                ]),
            ])
        ]),
    )
예제 #5
0
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')])
                ])
            ]),
        ]),
    )
예제 #6
0
 def testSimple(self):
     self.assertPrints(
         Tuple([Symbol('html'),
                Keyword('foo'), String('bar'), Symbol('baz')]),
         """
         html :foo "bar" baz
         """,
     )
예제 #7
0
 def testNested(self):
     self.assertPrints(
         Tuple([Symbol('html'),
                Keyword('foo'), String('bar'),
                Tuple([Symbol('head')])]),
         """
         html :foo "bar"
           head
         """,
     )
예제 #8
0
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,
    )
예제 #9
0
def test_explicit_tuple():
    check_parse(
        'foo (bar 5) "baz"',
        List([
            Tuple([
                Symbol('foo'),
                Tuple([Symbol('bar'), Number(5)]),
                String('baz')
            ]),
        ]),
    )
예제 #10
0
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')
예제 #11
0
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},
    )
예제 #12
0
 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
         """,
     )
예제 #13
0
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)])
                ])
            ]),
        ]),
    )
예제 #14
0
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)])
                ])
            ]),
        ]),
    )
예제 #15
0
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'),
            ]),
        ]),
    )
예제 #16
0
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},
    )
예제 #17
0
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)
예제 #18
0
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')])])
                ])
            ]),
        ]),
    )
예제 #19
0
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},
    )
예제 #20
0
 def test(self):
     node = self.parse(
         """
         div "Some {var} text"
         """
     )
     self.assertEqual(
         Translator(self.translations).visit(node),
         List([Tuple([Symbol('div'), String('Какой-то {var} текст')])]),
     )
예제 #21
0
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},
    )
예제 #22
0
def test_implicit_tuple():
    check_parse(
        'foo :bar 5 "baz"',
        List([
            Tuple([Symbol('foo'),
                   Keyword('bar'),
                   Number(5),
                   String('baz')]),
        ]),
    )
예제 #23
0
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})
예제 #24
0
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"')
예제 #25
0
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})
예제 #26
0
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], '""')
예제 #27
0
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'))
예제 #28
0
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')])
            ]),
        ]),
    )
예제 #29
0
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'),
                    ])
                ]),
            ]),
        ]),
    )
예제 #30
0
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,
    )
예제 #31
0
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},
    )
예제 #32
0
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},
    )