Beispiel #1
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 #2
0
def test_bare_word_key_starts_with_other_token():
    tokens = tokenize('{true_values: []}')
    assert tokens == (
        ast.MapStart('{'),
        ast.BareWordKey('true_values', 'true_values'),
        ast.Colon(':'),
        ast.Space(' '),
        ast.ListStart('['),
        ast.ListEnd(']'),
        ast.MapEnd('}'),
        ast.EOF(''),
    )
Beispiel #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
0
    ('start', 'end', 'item_func', 'to_iter'),
)

_map_tokens = functools.partial(
    _container,
    container_settings=ContainerSettings(
        start=ast.MapStart('{'),
        end=ast.MapEnd('}'),
        item_func=_map_item_tokens,
        to_iter=lambda m: tuple(m.items()),
    ),
)
_list_tokens = functools.partial(
    _container,
    container_settings=ContainerSettings(
        start=ast.ListStart('['),
        end=ast.ListEnd(']'),
        item_func=_to_tokens,
        to_iter=tuple,
    ),
)


def _top_level_map_tokens(dct, settings):
    tokens = []
    for kv in dct.items():
        tokens.extend(_map_item_tokens(kv, settings))
        tokens.append(ast.NL('\n'))
    return tokens