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 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_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 _map_item_tokens(kv, settings): k, v = kv ret = [] ret.extend(_to_tokens(k, settings, key=True)) ret.extend((ast.Colon(':'), ast.Space(' '))) ret.extend(_to_tokens(v, settings)) return ret
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
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_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_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_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
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