def test_issue_221_sort_reverse(self): from io import StringIO from ruyaml import YAML yaml = YAML() inp = dedent( """\ - d - a # 1 - c # 3 - e # 5 - b # 2 """ ) a = yaml.load(dedent(inp)) a.sort(reverse=True) buf = StringIO() yaml.dump(a, buf) exp = dedent( """\ - e # 5 - d - c # 3 - b # 2 - a # 1 """ ) assert buf.getvalue() == exp
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_issue_221_sort_key_reverse(self): from io import StringIO from ruyaml import YAML yaml = YAML() inp = dedent( """\ - four - One # 1 - Three # 3 - five # 5 - two # 2 """ ) a = yaml.load(dedent(inp)) a.sort(key=str.lower, reverse=True) buf = StringIO() yaml.dump(a, buf) exp = dedent( """\ - two # 2 - Three # 3 - One # 1 - four - five # 5 """ ) assert buf.getvalue() == exp
def test_issue_219(self): yaml_str = dedent( """\ [StackName: AWS::StackName] """ ) d = round_trip_load(yaml_str) # NOQA
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_issue_238(self, tmpdir): program_src = r""" import ruamel.yaml from ruamel.yaml.compat import StringIO yaml = ruamel.yaml.YAML(typ='unsafe') class A: def __setstate__(self, d): self.__dict__ = d class B: pass a = A() b = B() a.x = b b.y = [b] assert a.x.y[0] == a.x buf = StringIO() yaml.dump(a, buf) data = yaml.load(buf.getvalue()) assert data.x.y[0] == data.x """ assert save_and_run(dedent(program_src), tmpdir) == 0
def test_issue_249(self): yaml = YAML() inp = dedent("""\ # comment - - 1 - 2 - 3 """) exp = dedent("""\ # comment - - 1 - 2 - 3 """) yaml.round_trip(inp, outp=exp) # NOQA
def test_issue_288a(self): import sys from io import StringIO from ruyaml import YAML yamldoc = dedent( """\ --- # Reusable values aliases: # First-element comment - &firstEntry First entry # Second-element comment - &secondEntry Second entry # Third-element comment is # a multi-line value - &thirdEntry Third entry # EOF Comment """ ) yaml = YAML() yaml.indent(mapping=2, sequence=4, offset=2) yaml.explicit_start = True yaml.preserve_quotes = True yaml.width = sys.maxsize data = yaml.load(yamldoc) buf = StringIO() yaml.dump(data, buf) assert buf.getvalue() == yamldoc
def test_roundtrip_mapping_of_inline_lists(self): s = dedent("""\ a: [a, b, c] j: [k, l, m] """) output = rt(s) assert s == output
def test_issue_163(self): s = dedent("""\ some-list: # List comment - {} """) x = round_trip(s, preserve_quotes=True) # NOQA
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_issue_290a(self): import sys from ruamel.yaml.compat import StringIO from ruamel.yaml import YAML yamldoc = dedent("""\ --- aliases: # Folded-element comment # for a multi-line value - &FoldedEntry > THIS IS A FOLDED, MULTI-LINE VALUE # Literal-element comment # for a multi-line value - &literalEntry | THIS IS A LITERAL, MULTI-LINE VALUE # Plain-element comment - &plainEntry Plain entry """) yaml = YAML() yaml.indent(mapping=2, sequence=4, offset=2) yaml.explicit_start = True yaml.preserve_quotes = True yaml.width = sys.maxsize data = yaml.load(yamldoc) buf = StringIO() yaml.dump(data, buf) assert buf.getvalue() == yamldoc
def test_start_no_newline(self): # special construct to prevent stripping of following whitespac x = dedent("""\ 123 456 """) assert x == '123\n 456\n'
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_start_newline(self): # fmt: off x = dedent(""" 123 456 """) # fmt: on assert x == '123\n 456\n'
def test_start_space_newline(self): # special construct to prevent stripping of following whitespace # fmt: off x = dedent(" " """ 123 """) # fmt: on assert x == '123\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_roundtrip_mapping_of_inline_sequence_eol_comments(self): s = dedent("""\ # comment A a: [a, b, c] # comment B j: [k, l, m] # comment C """) output = rt(s) assert s == output
def test_roundtrip_sequence_of_inline_mappings_eol_comments(self): s = dedent("""\ # comment A - {a: 1, b: hallo} # comment B - {j: fka, k: 42} # comment C """) output = rt(s) assert s == output
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_omap_out(self): # ordereddict mapped to !!omap x = ordereddict([('a', 1), ('b', 2)]) res = ruamel.yaml.dump(x, default_flow_style=False) assert res == dedent(""" !!omap - a: 1 - b: 2 """)
def test_issue_102(self): yaml_str = dedent(""" var1: #empty var2: something #notempty var3: {} #empty object var4: {a: 1} #filled object var5: [] #empty array """) x = round_trip(yaml_str, preserve_quotes=True) # NOQA
def test_issue_161a(self): yaml_str = dedent("""\ mapping-A: key-A:{} mapping-B: """) for comment in ['\n# between']: s = yaml_str.format(comment) res = round_trip(s) # NOQA
def test_issue_161(self): yaml_str = dedent("""\ mapping-A: key-A:{} mapping-B: """) for comment in ['', ' # no-newline', ' # some comment\n', '\n']: s = yaml_str.format(comment) res = round_trip(s) # NOQA
def test_issue_300(self): from ruamel.yaml import YAML inp = dedent(""" %YAML 1.2 %TAG ! tag:example.com,2019/path#fragment --- null """) YAML().load(inp)
def test_replace_preserved_scalar_string(self): import ruamel s = dedent("""\ foo: | foo foo bar foo """) data = round_trip_load(s, preserve_quotes=True) so = data['foo'].replace('foo', 'bar', 2) assert isinstance(so, ruamel.yaml.scalarstring.LiteralScalarString) assert so == dedent(""" bar bar bar foo """)
def test_replace_double_quoted_scalar_string(self): import ruyaml s = dedent("""\ foo: "foo foo bar foo" """) data = round_trip_load(s, preserve_quotes=True) so = data['foo'].replace('foo', 'bar', 2) assert isinstance(so, ruyaml.scalarstring.DoubleQuotedScalarString) assert so == 'bar bar bar foo'
def test_roundtrip_flow_mapping(self): import ruamel.yaml s = dedent("""\ - {a: 1, b: hallo} - {j: fka, k: 42} """) data = ruamel.yaml.load(s, Loader=ruamel.yaml.RoundTripLoader) output = ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper) assert s == output
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_issue_160(self): s = dedent("""\ root: # a comment - {some_key: "value"} foo: 32 bar: 32 """) x = round_trip(s, block_seq_indent=4, preserve_quotes=True) assert x['bar'] == 32