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_file_starting_in_ws(): ret = parse('\n\ntrue') expected = ast.Doc( head=(ast.NL('\n'), ast.NL('\n')), val=ast.Bool(val=True, src='true'), 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 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
def test_map_multiline_one_element(): ret = parse('{\n' ' true: false,\n' '}') expected = ast.Doc( head=(), val=ast.Map( head=(ast.MapStart('{'), ast.NL('\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_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_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
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
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_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 _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