def test_root_literal_scalar_no_indent_1_1_no_raise(self): # from ruamel.yaml.parser import ParserError yaml = YAML() yaml.root_level_block_style_scalar_no_indent_error_1_1 = True s = 'testing123' # with pytest.raises(ParserError): if True: inp = """ %YAML 1.1 --- | {} """ yaml.load(inp.format(s))
def test_decorator_explicit(self): from ruamel.yaml import yaml_object yml = YAML() @yaml_object(yml) class User3(object): yaml_tag = u'!USER' def __init__(self, name, age): self.name = name self.age = age @classmethod def to_yaml(cls, representer, node): return representer.represent_scalar( cls.yaml_tag, u'{.name}-{.age}'.format(node, node)) @classmethod def from_yaml(cls, constructor, node): return cls(*node.value.split('-')) ys = """ - !USER Anthon-18 """ d = yml.load(ys) yml.dump(d, compare=ys)
def test_issue_300a(self): import ruyaml inp = dedent( """ %YAML 1.1 %TAG ! tag:example.com,2019/path#fragment --- null """ ) yaml = YAML() with pytest.raises( ruyaml.scanner.ScannerError, match='while scanning a directive' ): yaml.load(inp)
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_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_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_176(self): # basic request by Stuart Berg from ruyaml import YAML yaml = YAML() seq = yaml.load('[1,2,3]') seq[:] = [1, 2, 3, 4]
def test_register_1_rt(self): yaml = YAML() yaml.register_class(User1) ys = """ - !user Anthon-18 """ d = yaml.load(ys) yaml.dump(d, compare=ys)
def test_register_1_unsafe(self): yaml = YAML(typ='unsafe') yaml.register_class(User1) ys = """ [!user Anthon-18] """ d = yaml.load(ys) yaml.dump(d, compare=ys)
def test_issue_233a(self): import json from ruyaml import YAML yaml = YAML() data = yaml.load('[]') json_str = json.dumps(data) # NOQA
def test_register_0_unsafe(self): # default_flow_style = None yaml = YAML(typ='unsafe') yaml.register_class(User0) ys = """ - !User0 {age: 18, name: Anthon} """ d = yaml.load(ys) yaml.dump(d, compare=ys)
def test_root_folding_scalar_no_indent(self): yaml = YAML() s = 'testing123' inp = """ --- > {} """ d = yaml.load(inp.format(s)) print(d) assert d == s + '\n'
def test_root_folding_scalar_no_indent_special(self): yaml = YAML() s = '%!PS-Adobe-2.0' inp = """ --- > {} """ d = yaml.load(inp.format(s)) print(d) assert d == s + '\n'
def test_register_0_rt(self): yaml = YAML() yaml.register_class(User0) ys = """ - !User0 name: Anthon age: 18 """ d = yaml.load(ys) yaml.dump(d, compare=ys, unordered_lines=True)
def test_root_literal_scalar_indent_example_9_5(self): yaml = YAML() s = '%!PS-Adobe-2.0' inp = """ --- | {} """ d = yaml.load(inp.format(s)) print(d) assert d == s + '\n'
def test_rt_non_root_literal_scalar(self): yaml = YAML() s = 'testing123' ys = """ - | {} """ ys = ys.format(s) d = yaml.load(ys) yaml.dump(d, compare=ys)
def test_root_literal_scalar_indent_offset_one(self): yaml = YAML() s = 'testing123' inp = """ --- |1 {} """ d = yaml.load(inp.format(s)) print(d) assert d == s + '\n'
def test_root_literal_scalar_indent_offset_two_leading_space(self): yaml = YAML() s = ' testing123' inp = """ --- |4 {s} {s} """ d = yaml.load(inp.format(s=s)) print(d) assert d == (s + '\n') * 2
def test_root_literal_scalar_no_indent_1_1(self): yaml = YAML() s = 'testing123' inp = """ %YAML 1.1 --- | {} """ d = yaml.load(inp.format(s)) print(d) assert d == s + '\n'
def test_rt_root_literal_scalar_no_indent_no_eol(self): yaml = YAML() yaml.explicit_start = True s = 'testing123' ys = """ --- |- {} """ ys = ys.format(s) d = yaml.load(ys) yaml.dump(d, compare=ys)
def test_root_literal_doc_indent_marker(self): yaml = YAML() yaml.explicit_start = True inp = """ --- |2 some more text """ d = yaml.load(inp) print(type(d), repr(d)) yaml.round_trip(inp)
def test_rt_root_sq_scalar_expl_indent(self): yaml = YAML() yaml.explicit_start = True yaml.indent = 4 s = "'testing: 123'" ys = """ --- {} """ ys = ys.format(s) d = yaml.load(ys) yaml.dump(d, compare=ys)
def test_rt_root_plain_scalar_no_indent(self): yaml = YAML() yaml.explicit_start = True yaml.indent = 0 s = 'testing123' ys = """ --- {} """ ys = ys.format(s) d = yaml.load(ys) yaml.dump(d, compare=ys)
def test_rt_root_dq_scalar_expl_indent(self): # if yaml.indent is the default (None) # then write after the directive indicator yaml = YAML() yaml.explicit_start = True yaml.indent = 0 s = '"\'testing123"' ys = """ --- {} """ ys = ys.format(s) d = yaml.load(ys) yaml.dump(d, compare=ys)
def test_root_literal_scalar_no_indent_1_1_old_style(self): from textwrap import dedent from ruamel.yaml import YAML yaml = YAML(typ='safe', pure=True) s = 'testing123' inp = """ %YAML 1.1 --- | {} """ d = yaml.load(dedent(inp.format(s))) print(d) assert d == s + '\n'
def test_duplicate_key_01(self): # so issue https://stackoverflow.com/a/52852106/1307905 from ruyaml import version_info from ruyaml.constructor import DuplicateKeyError s = dedent( """\ - &name-name a: 1 - &help-name b: 2 - <<: *name-name <<: *help-name """ ) if version_info < (0, 15, 1): pass else: with pytest.raises(DuplicateKeyError): yaml = YAML(typ='safe') yaml.load(s) with pytest.raises(DuplicateKeyError): yaml = YAML() yaml.load(s)
def test_issue_150(self): from ruyaml import YAML inp = """\ base: &base_key first: 123 second: 234 child: <<: *base_key third: 345 """ yaml = YAML() data = yaml.load(inp) child = data['child'] assert 'second' in dict(**child)
def test_decorator_implicit(self): from ruamel.yaml import yaml_object yml = YAML() @yaml_object(yml) class User2(object): def __init__(self, name, age): self.name = name self.age = age ys = """ - !User2 name: Anthon age: 18 """ d = yml.load(ys) yml.dump(d, compare=ys, unordered_lines=True)
def test_issue_245(self): from ruyaml import YAML inp = """ d: yes """ for typ in ['safepure', 'rt', 'safe']: if typ.endswith('pure'): pure = True typ = typ[:-4] else: pure = None yaml = YAML(typ=typ, pure=pure) yaml.version = (1, 1) d = yaml.load(inp) print(typ, yaml.parser, yaml.resolver) assert d['d'] is True
def test_issue_286(self): from io import StringIO from ruyaml import YAML yaml = YAML() inp = dedent( """\ parent_key: - sub_key: sub_value # xxx""" ) a = yaml.load(inp) a['new_key'] = 'new_value' buf = StringIO() yaml.dump(a, buf) assert buf.getvalue().endswith('xxx\nnew_key: new_value\n')