def test_issue_160(self): from io import StringIO s = dedent( """\ root: # a comment - {some_key: "value"} foo: 32 bar: 32 """ ) a = round_trip_load(s) del a['root'][0]['some_key'] buf = StringIO() round_trip_dump(a, buf, block_seq_indent=4) exp = dedent( """\ root: # a comment - {} foo: 32 bar: 32 """ ) assert buf.getvalue() == exp
def test_00(self, capsys): s = dedent(""" a: 1 b: [] c: [a, 1] d: {f: 3.14, g: 42} """) d = round_trip_load(s) round_trip_dump(d, sys.stdout) out, err = capsys.readouterr() assert out == s
def test_01(self, capsys): s = dedent(""" - 1 - [] - [a, 1] - {f: 3.14, g: 42} - - 123 """) d = round_trip_load(s) round_trip_dump(d, sys.stdout) out, err = capsys.readouterr() assert out == s
def test_issue_295(self): # deepcopy also makes a copy of the start and end mark, and these did not # have any comparison beyond their ID, which of course changed, breaking # some old merge_comment code import copy inp = dedent( """ A: b: # comment - l1 - l2 C: d: e f: # comment2 - - l31 - l32 - l33: '5' """ ) data = round_trip_load(inp) # NOQA dc = copy.deepcopy(data) assert round_trip_dump(dc) == inp
def test_deepcopy_datestring(self): # reported by Quuxplusone, http://stackoverflow.com/a/41577841/1307905 x = dedent("""\ foo: 2016-10-12T12:34:56 """) data = copy.deepcopy(round_trip_load(x)) assert round_trip_dump(data) == x
def test_roundtrip_flow_mapping(self): s = dedent("""\ - {a: 1, b: hallo} - {j: fka, k: 42} """) data = round_trip_load(s) output = round_trip_dump(data) assert s == output
def test_mul_00(self): # issue 149 reported by [email protected] d = round_trip_load("""\ - 0.1 """) d[0] *= -1 x = round_trip_dump(d) assert x == '- -0.1\n'
def test_set_simple_mapping_key(self): from ruyaml.comments import CommentedKeyMap d = {CommentedKeyMap([('a', 1), ('b', 2)]): 'hello world'} exp = dedent("""\ {a: 1, b: 2}: hello world """) assert round_trip_dump(d) == exp
def test_issue_54_ok(self): yaml_str = dedent("""\ toplevel: # some comment sublevel: 300 """) d = round_trip_load(yaml_str) y = round_trip_dump(d, indent=4) assert yaml_str == y
def test_some_eol_spaces(self): # spaces after tokens and on empty lines yaml_str = '--- \n \na: "x" \n \nb: y \n' d = round_trip_load(yaml_str, preserve_quotes=True) y = round_trip_dump(d, explicit_start=True) stripped = "" for line in yaml_str.splitlines(): stripped += line.rstrip() + '\n' print(line + '$') assert stripped == y
def test_line_with_only_spaces(self): # issue 54 yaml_str = "---\n\na: 'x'\n \nb: y\n" d = round_trip_load(yaml_str, preserve_quotes=True) y = round_trip_dump(d, explicit_start=True) stripped = "" for line in yaml_str.splitlines(): stripped += line.rstrip() + '\n' print(line + '$') assert stripped == y
def test_preserve_flow_style_simple(self): x = dedent("""\ {foo: bar, baz: quux} """) data = round_trip_load(x) data_copy = copy.deepcopy(data) y = round_trip_dump(data_copy) print('x [{}]'.format(x)) print('y [{}]'.format(y)) assert y == x assert data.fa.flow_style() == data_copy.fa.flow_style()
def test_issue_184(self): yaml_str = dedent("""\ test::test: # test foo: bar: baz """) d = round_trip_load(yaml_str) d['bar'] = 'foo' d.yaml_add_eol_comment('test1', 'bar') assert round_trip_dump(d) == yaml_str + 'bar: foo # test1\n'
def test_insert_at_pos_0(self): d = round_trip_load(self.ins) d.insert(0, 'last name', 'Vandelay', comment="new key") y = round_trip_dump(d) print(y) assert y == dedent("""\ last name: Vandelay # new key first_name: Art occupation: Architect # This is an occupation comment about: Art Vandelay is a fictional character that George invents... """)
def test_roundtrip_four_space_indents_expl_indent_no_fail(self): assert round_trip_dump(round_trip_load(""" a: - foo - bar """), indent=4) == dedent(""" a: - foo - bar """)
def test_standard_short_tag_no_fail(self): assert round_trip_dump( round_trip_load(""" !!map name: Anthon location: Germany language: python """)) == dedent(""" name: Anthon location: Germany language: python """)
def test_insert_at_pos_3(self): # much more simple if done with appending. d = round_trip_load(self.ins) d.insert(3, 'last name', 'Vandelay', comment="new key") y = round_trip_dump(d) print(y) assert y == dedent("""\ first_name: Art occupation: Architect # This is an occupation comment about: Art Vandelay is a fictional character that George invents... last name: Vandelay # new key """)
def test_change_value_simple_mapping_key(self): from ruyaml.comments import CommentedKeyMap inp = """\ {a: 1, b: 2}: hello world """ d = round_trip_load(inp, preserve_quotes=True) d = {CommentedKeyMap([('a', 1), ('b', 2)]): 'goodbye'} exp = dedent("""\ {a: 1, b: 2}: goodbye """) assert round_trip_dump(d) == exp
def test_issue_54_not_ok(self): yaml_str = dedent("""\ toplevel: # some comment sublevel: 300 """) d = round_trip_load(yaml_str) print(d.ca) y = round_trip_dump(d, indent=4) print(y.replace('\n', '$\n')) assert yaml_str == y
def test_omap_out(self): # ordereddict mapped to !!omap from ruamel.yaml.compat import ordereddict import ruamel.yaml # NOQA x = ordereddict([('a', 1), ('b', 2)]) res = round_trip_dump(x, default_flow_style=False) assert res == dedent(""" !!omap - a: 1 - b: 2 """)
def test_dump_ruamel_ordereddict(self): from ruamel.ordereddict import ordereddict import ruamel.yaml # NOQA # OrderedDict mapped to !!omap x = ordereddict([('a', 1), ('b', 2)]) res = round_trip_dump(x, default_flow_style=False) assert res == dedent(""" !!omap - a: 1 - b: 2 """)
def test_copy_flow_style_nested_dict(self): x = dedent("""\ a: {foo: bar, baz: quux} """) data = round_trip_load(x) assert data['a'].fa.flow_style() is True data_copy = copy.copy(data) assert data_copy['a'].fa.flow_style() is True data_copy['a'].fa.set_block_style() assert data['a'].fa.flow_style() == data_copy['a'].fa.flow_style() assert data['a'].fa._flow_style is False assert data_copy['a'].fa._flow_style is False y = round_trip_dump(data_copy) z = round_trip_dump(data) assert y == z assert y == dedent("""\ a: foo: bar baz: quux """)
def test_roundtrip_four_space_indents_no_fail(self): inp = """ a: - foo - bar """ exp = """ a: - foo - bar """ assert round_trip_dump(round_trip_load(inp)) == dedent(exp)
def test_comment_dash_line_fail(self): x = """ - # abc a: 1 b: 2 """ data = round_trip_load(x) # this is not nice assert round_trip_dump(data) == dedent(""" # abc - a: 1 b: 2 """)
def test_reindent(self): x = """\ a: b: # comment 1 c: 1 # comment 2 """ d = round_trip_load(x) y = round_trip_dump(d, indent=4) assert y == dedent("""\ a: b: # comment 1 c: 1 # comment 2 """)
def test_change_key_simple_mapping_key(self): from ruyaml.comments import CommentedKeyMap inp = """\ {a: 1, b: 2}: hello world """ d = round_trip_load(inp, preserve_quotes=True) d[CommentedKeyMap([('b', 1), ('a', 2) ])] = d.pop(CommentedKeyMap([('a', 1), ('b', 2)])) exp = dedent("""\ {b: 1, a: 2}: hello world """) assert round_trip_dump(d) == exp
def test_before_top_seq_rt(self): data = load(""" - a - b """) data.yaml_set_start_comment('Hello\nWorld\n') print(round_trip_dump(data)) exp = """ # Hello # World - a - b """ compare(data, exp)
def test_issue_176_preserve_comments_on_extended_slice_assignment(self): yaml_str = dedent("""\ - a - b # comment - c # commment c # comment c+ - d - e # comment """) seq = round_trip_load(yaml_str) seq[1::2] = ['B', 'D'] res = round_trip_dump(seq) assert res == yaml_str.replace(' b ', ' B ').replace(' d\n', ' D\n')
def test_pop_3(self): d = round_trip_load(self.ins) d['ab'].pop(3) y = round_trip_dump(d, indent=2) print(y) assert y == dedent("""\ ab: - a # a - b # b - c de: - 1 - 2 """)
def test_added_inline_list(self): s1 = dedent(""" a: - b - c - d """) s = 'a: [b, c, d]\n' data = round_trip_load(s1) val = data['a'] val.fa.set_flow_style() # print(type(val), '_yaml_format' in dir(val)) output = round_trip_dump(data) assert s == output