Esempio n. 1
0
    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))
Esempio n. 2
0
    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)
Esempio n. 3
0
    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)
Esempio n. 4
0
    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
Esempio n. 5
0
    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
Esempio n. 6
0
    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
Esempio n. 7
0
    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]
Esempio n. 8
0
 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)
Esempio n. 9
0
 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)
Esempio n. 10
0
    def test_issue_233a(self):
        import json

        from ruyaml import YAML

        yaml = YAML()
        data = yaml.load('[]')
        json_str = json.dumps(data)  # NOQA
Esempio n. 11
0
 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)
Esempio n. 12
0
 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'
Esempio n. 13
0
 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'
Esempio n. 14
0
 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)
Esempio n. 15
0
 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'
Esempio n. 16
0
 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)
Esempio n. 17
0
 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'
Esempio n. 18
0
 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
Esempio n. 19
0
 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'
Esempio n. 20
0
 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)
Esempio n. 21
0
 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)
Esempio n. 22
0
 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)
Esempio n. 23
0
 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)
Esempio n. 24
0
 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)
Esempio n. 25
0
    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'
Esempio n. 26
0
    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)
Esempio n. 27
0
    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)
Esempio n. 28
0
    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)
Esempio n. 29
0
    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
Esempio n. 30
0
    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')