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_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_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_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 _parse_map_item(tokens, offset, head): key, offset = get_pattern(tokens, offset, PT_KEY, single=True) colon_space, offset = get_pattern(tokens, offset, PT_COLON_SPACE) val, offset = _parse_val(tokens, offset) return ast.MapItem(head, key, colon_space, val, ()), offset