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
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(''), )
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
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
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
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
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
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
) _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 def _to_ast(*args, **kwargs):