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_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
def _multiline(val, settings, container_settings): ret = [container_settings.start, ast.NL('\n')] for item in container_settings.to_iter(val): ret.append(ast.Indent(' ' * (settings.indented.indent))) ret.extend(container_settings.item_func(item, settings.indented)) ret.extend((ast.Comma(','), ast.NL('\n'))) if settings.indent > 0: ret.append(ast.Indent(' ' * settings.indent)) ret.append(container_settings.end) return ret
def _inline(val, settings, container_settings): ret = [container_settings.start] if val: items = container_settings.to_iter(val) ret.extend(container_settings.item_func(items[0], settings)) for item in items[1:]: ret.extend((ast.Comma(','), ast.Space(' '))) ret.extend(container_settings.item_func(item, settings)) ret.append(container_settings.end) return ret
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_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_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
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