def test_delete_nested(): val = loads_roundtrip('{\n' ' a: {\n' ' b: true,\n' ' c: true,\n' ' },\n' '}\n') del val['a']['b'] ret = dumps_roundtrip(val) assert ret == ('{\n' ' a: {\n' ' c: true,\n' ' },\n' '}\n')
def test_replace_map_value_top_level(): val = loads_roundtrip('{\n' ' a: true, # comment\n' ' b: false, # comment\n' '}\n') val['b'] = None ret = dumps_roundtrip(val) assert ret == ('{\n' ' a: true, # comment\n' ' b: null, # comment\n' '}\n')
def test_replace_list_value_top_level(): val = loads_roundtrip('[\n' ' true, # comment\n' ' false, # comment\n' ']\n') val[0] = None ret = dumps_roundtrip(val) assert ret == ('[\n' ' null, # comment\n' ' false, # comment\n' ']\n')
def test_delete_dictionary_key(): val = loads_roundtrip('{\n' ' # comment documenting a\n' ' a: true, # comment\n' ' # comment documenting b\n' ' b: false, # comment\n' '}\n') del val['a'] ret = dumps_roundtrip(val) assert ret == ('{\n' ' # comment documenting b\n' ' b: false, # comment\n' '}\n')
def test_replace_nested_map_value(): val = loads_roundtrip('{\n' ' a: {\n' ' b: true, # comment\n' ' },\n' '}\n') val['a']['b'] = None ret = dumps_roundtrip(val) assert ret == ('{\n' ' a: {\n' ' b: null, # comment\n' ' },\n' '}\n')
def test_replace_key(): val = loads_roundtrip("{k: 'v'}") val['k'].replace_key('new key!') ret = dumps_roundtrip(val) assert ret == "{'new key!': 'v'}"
def test_replace_float(): val = loads_roundtrip('true # comment') val.replace_value(5.) ret = dumps_roundtrip(val) assert ret == '5.0 # comment'
def test_load_dump_roundtrip(): s = ('{\n' ' true: false, # comment\n' '}') sio = io.StringIO(s) assert dumps_roundtrip(load_roundtrip(sio)) == s
def test_replace_string(): val = loads_roundtrip('true # comment') val.replace_value('ohai') ret = dumps_roundtrip(val) assert ret == "'ohai' # comment"
def test_replace_value_new_type(): val = loads_roundtrip('true # comment') val.replace_value(None) ret = dumps_roundtrip(val) assert ret == 'null # comment'
def test_delete_fixup_indent(): val = loads_roundtrip('[\n' ' true, false,\n' ']') del val[0] ret = dumps_roundtrip(val) assert ret == ('[\n' ' false,\n' ']')
def test_delete_fixup_trailing_space_multiline(): val = loads_roundtrip('[\n' ' true, false,\n' ']') del val[1] ret = dumps_roundtrip(val) assert ret == ('[\n' ' true,\n' ']')
def test_replace_value_same_type(): val = loads_roundtrip('true # comment') val.replace_value(False) ret = dumps_roundtrip(val) assert ret == 'false # comment'
def test_delete_nested_fixup_trailing_comma_inline(): val = loads_roundtrip('{a: {b: {c: true, d: false}}}') del val['a']['b']['d'] ret = dumps_roundtrip(val) assert ret == '{a: {b: {c: true}}}'
def test_replace_nested_top_level_map(): val = loads_roundtrip('true: {false: "hello"}\n' 'false: {true: "world"}\n') val[True][False] = 'goodbye' ret = dumps_roundtrip(val) assert ret == ("true: {false: 'goodbye'}\n" 'false: {true: "world"}\n')
def test_replace_nested_map_value_deeper(): val = loads_roundtrip('{a: {b: {c: true}}}') val['a']['b']['c'] = False ret = dumps_roundtrip(val) assert ret == '{a: {b: {c: false}}}'