Beispiel #1
0
def test_map_multiple_elements_inline():
    ret = parse('{true: false, false: true}')
    expected = ast.Doc(
        head=(),
        val=ast.Map(
            head=(ast.MapStart('{'), ),
            items=(
                ast.MapItem(
                    head=(),
                    key=ast.Bool(val=True, src='true'),
                    inner=(ast.Colon(':'), ast.Space(' ')),
                    val=ast.Bool(val=False, src='false'),
                    tail=(ast.Comma(','), ast.Space(' ')),
                ),
                ast.MapItem(
                    head=(),
                    key=ast.Bool(val=False, src='false'),
                    inner=(ast.Colon(':'), ast.Space(' ')),
                    val=ast.Bool(val=True, src='true'),
                    tail=(),
                ),
            ),
            tail=(ast.MapEnd('}'), ),
        ),
        tail=(),
    )
    assert ret == expected
Beispiel #2
0
def test_list_multiline_multiple_items_on_one_line():
    ret = parse('[\n' '    true, false,\n' '    false, true,\n' ']')
    expected = ast.Doc(
        head=(),
        val=ast.List(
            head=(ast.ListStart('['), ast.NL('\n')),
            items=(
                ast.ListItem(
                    head=(ast.Indent('    '), ),
                    val=ast.Bool(val=True, src='true'),
                    tail=(ast.Comma(','), ast.Space(' ')),
                ),
                ast.ListItem(
                    head=(),
                    val=ast.Bool(val=False, src='false'),
                    tail=(ast.Comma(','), ast.NL('\n')),
                ),
                ast.ListItem(
                    head=(ast.Indent('    '), ),
                    val=ast.Bool(val=False, src='false'),
                    tail=(ast.Comma(','), ast.Space(' ')),
                ),
                ast.ListItem(
                    head=(),
                    val=ast.Bool(val=True, src='true'),
                    tail=(ast.Comma(','), ast.NL('\n')),
                ),
            ),
            tail=(ast.ListEnd(']'), ),
        ),
        tail=(),
    )
    assert ret == expected
Beispiel #3
0
def test_top_level_map():
    ret = parse('true: false\n' 'false: true')
    expected = ast.Doc(
        head=(),
        val=ast.Map(
            head=(),
            items=(
                ast.MapItem(
                    head=(),
                    key=ast.Bool(val=True, src='true'),
                    inner=(ast.Colon(':'), ast.Space(' ')),
                    val=ast.Bool(val=False, src='false'),
                    tail=(ast.NL('\n'), ),
                ),
                ast.MapItem(
                    head=(),
                    key=ast.Bool(val=False, src='false'),
                    inner=(ast.Colon(':'), ast.Space(' ')),
                    val=ast.Bool(val=True, src='true'),
                    tail=(),
                ),
            ),
            tail=(),
        ),
        tail=(),
    )
    assert ret == expected
    assert ret.val.is_top_level_style
Beispiel #4
0
def test_parse_boolean(s, expected_val):
    expected = ast.Doc(
        head=(),
        val=ast.Bool(val=expected_val, src=s),
        tail=(),
    )
    assert parse(s) == expected
Beispiel #5
0
def test_file_ending_in_comment_no_nl():
    ret = parse('true # ohai')
    expected = ast.Doc(
        head=(),
        val=ast.Bool(val=True, src='true'),
        tail=(ast.Space(' '), ast.Comment('# ohai')),
    )
    assert ret == expected
Beispiel #6
0
def test_file_ending_in_ws():
    ret = parse('true\n')
    expected = ast.Doc(
        head=(),
        val=ast.Bool(val=True, src='true'),
        tail=(ast.NL('\n'), ),
    )
    assert ret == expected
Beispiel #7
0
def test_file_starting_with_comments():
    ret = parse('# hello\ntrue')
    expected = ast.Doc(
        head=(ast.Comment('# hello\n'), ),
        val=ast.Bool(val=True, src='true'),
        tail=(),
    )
    assert ret == expected
Beispiel #8
0
def test_parse_quoted_string(quote, s, expected_val):
    s = s.replace("'", quote)
    expected_val = expected_val.replace("'", quote)
    expected = ast.Doc(
        head=(),
        val=ast.String(val=expected_val, src=s),
        tail=(),
    )
    assert parse(s) == expected
Beispiel #9
0
def test_file_ending_in_several_comments():
    ret = parse('true\n# hello\n# there\n')
    expected = ast.Doc(
        head=(),
        val=ast.Bool(val=True, src='true'),
        tail=(
            ast.NL('\n'),
            ast.Comment('# hello\n'),
            ast.Comment('# there\n'),
        ),
    )
    assert ret == expected
Beispiel #10
0
def test_list_multiline_trivial():
    ret = parse('[\n]')
    expected = ast.Doc(
        head=(),
        val=ast.List(
            head=(ast.ListStart('['), ast.NL('\n')),
            items=(),
            tail=(ast.ListEnd(']'), ),
        ),
        tail=(),
    )
    assert ret == expected
    assert ret.val.is_multiline
    assert not ret.val.is_top_level_style
Beispiel #11
0
def test_map_multiline_trivial():
    ret = parse('{\n}')
    expected = ast.Doc(
        head=(),
        val=ast.Map(
            head=(ast.MapStart('{'), ast.NL('\n')),
            items=(),
            tail=(ast.MapEnd('}'), ),
        ),
        tail=(),
    )
    assert ret == expected
    assert ret.val.is_multiline
    assert not ret.val.is_top_level_style
Beispiel #12
0
def test_list_one_value_inline():
    ret = parse('[true]')
    expected = ast.Doc(
        head=(),
        val=ast.List(
            head=(ast.ListStart('['), ),
            items=(ast.ListItem(
                head=(),
                val=ast.Bool(val=True, src='true'),
                tail=(),
            ), ),
            tail=(ast.ListEnd(']'), ),
        ),
        tail=(),
    )
    assert ret == expected
Beispiel #13
0
def test_list_multiline_comments():
    ret = parse('[\n' '    # Comment\n' ']')
    expected = ast.Doc(
        head=(),
        val=ast.List(
            head=(
                ast.ListStart('['),
                ast.NL('\n'),
                ast.Indent('    '),
                ast.Comment('# Comment\n'),
            ),
            items=(),
            tail=(ast.ListEnd(']'), ),
        ),
        tail=(),
    )
    assert ret == expected
Beispiel #14
0
def test_map_bare_word_key():
    ret = parse("{_Key-str1ng: 'value'}")
    expected = ast.Doc(
        head=(),
        val=ast.Map(
            head=(ast.MapStart('{'), ),
            items=(ast.MapItem(
                head=(),
                key=ast.BareWordKey('_Key-str1ng', '_Key-str1ng'),
                inner=(ast.Colon(':'), ast.Space(' ')),
                val=ast.String(val='value', src="'value'"),
                tail=(),
            ), ),
            tail=(ast.MapEnd('}'), ),
        ),
        tail=(),
    )
    assert ret == expected
Beispiel #15
0
def test_list_multiline_comment_before():
    ret = parse('[\n' '    # Hello\n' '    true,\n' ']')
    expected = ast.Doc(
        head=(),
        val=ast.List(
            head=(ast.ListStart('['), ast.NL('\n')),
            items=(ast.ListItem(
                head=(
                    ast.Indent('    '),
                    ast.Comment('# Hello\n'),
                    ast.Indent('    '),
                ),
                val=ast.Bool(val=True, src='true'),
                tail=(ast.Comma(','), ast.NL('\n')),
            ), ),
            tail=(ast.ListEnd(']'), ),
        ),
        tail=(),
    )
    assert ret == expected
Beispiel #16
0
def test_nested_list():
    ret = parse('[\n' '    [\n' '        1,\n' '    ],\n' ']')
    expected = ast.Doc(
        head=(),
        val=ast.List(
            head=(ast.ListStart('['), ast.NL('\n')),
            items=(ast.ListItem(head=(ast.Indent('    '), ),
                                val=ast.List(
                                    head=(ast.ListStart('['), ast.NL('\n')),
                                    items=(ast.ListItem(
                                        head=(ast.Indent('        '), ),
                                        val=ast.Int(val=1, src='1'),
                                        tail=(ast.Comma(','), ast.NL('\n')),
                                    ), ),
                                    tail=(ast.Indent('    '),
                                          ast.ListEnd(']')),
                                ),
                                tail=(ast.Comma(','), ast.NL('\n'))), ),
            tail=(ast.ListEnd(']'), ),
        ),
        tail=(),
    )
    assert ret == expected
Beispiel #17
0
def test_list_several_values_inline():
    ret = parse('[true, false]')
    expected = ast.Doc(
        head=(),
        val=ast.List(
            head=(ast.ListStart('['), ),
            items=(
                ast.ListItem(
                    head=(),
                    val=ast.Bool(val=True, src='true'),
                    tail=(ast.Comma(','), ast.Space(' ')),
                ),
                ast.ListItem(
                    head=(),
                    val=ast.Bool(val=False, src='false'),
                    tail=(),
                ),
            ),
            tail=(ast.ListEnd(']'), ),
        ),
        tail=(),
    )
    assert ret == expected
Beispiel #18
0
def test_comment_at_start_of_multiline_json():
    ret = parse('{  # bar\n' '    true: false,\n' '}')
    expected = ast.Doc(
        head=(),
        val=ast.Map(
            head=(
                ast.MapStart('{'),
                ast.Space(' '),
                ast.Space(' '),
                ast.Comment('# bar\n'),
            ),
            items=(ast.MapItem(
                head=(ast.Indent('    '), ),
                key=ast.Bool(val=True, src='true'),
                inner=(ast.Colon(':'), ast.Space(' ')),
                val=ast.Bool(val=False, src='false'),
                tail=(ast.Comma(','), ast.NL('\n')),
            ), ),
            tail=(ast.MapEnd('}'), ),
        ),
        tail=(),
    )
    assert ret == expected
Beispiel #19
0
def test_parse_null():
    expected = ast.Doc(head=(), val=ast.Null(None, src='null'), tail=())
    assert parse('null') == expected
Beispiel #20
0
def test_parse_integer(s, expected_val):
    ret = parse(s)
    expected = ast.Doc(head=(), val=ast.Int(expected_val, src=s), tail=())
    assert ret == expected
Beispiel #21
0
def test_parse_float(s, expected_val):
    ret = parse(s)
    expected = ast.Doc(head=(), val=ast.Float(expected_val, src=s), tail=())
    assert ret == expected
Beispiel #22
0
def parse_from_tokens(tokens, offset=0):
    head, offset = get_pattern(tokens, offset, PT_HEAD)
    val, offset = _parse_top_level(tokens, offset)
    tail, offset = _parse_eof(tokens, offset)
    return ast.Doc(head, val, tail)