Beispiel #1
0
    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
Beispiel #2
0
 def test_sequence(self):
     yaml = YAML()
     yaml.brace_single_entry_mapping_in_flow_sequence = True
     yaml.mapping_value_align = True
     yaml.round_trip("""
     - !Sequence [a, {b: 1}, {c: {d: 3}}]
     """)
Beispiel #3
0
def test_example_2_1():
    yaml = YAML()
    yaml.round_trip("""
    - Mark McGwire
    - Sammy Sosa
    - Ken Griffey
    """)
Beispiel #4
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
Beispiel #5
0
    def test_issue_176(self):
        # basic request by Stuart Berg
        from ruamel.yaml import YAML

        yaml = YAML()
        seq = yaml.load('[1,2,3]')
        seq[:] = [1, 2, 3, 4]
Beispiel #6
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
Beispiel #7
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
Beispiel #8
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)
Beispiel #9
0
    def test_issue_233a(self):
        from ruamel.yaml import YAML
        import json

        yaml = YAML()
        data = yaml.load('[]')
        json_str = json.dumps(data)  # NOQA
Beispiel #10
0
 def test_sequence2(self):
     yaml = YAML()
     yaml.mapping_value_align = True
     yaml.round_trip(
         """
     - !Sequence [a, b: 1, c: {d: 3}]
     """
     )
Beispiel #11
0
    def test_issue_233(self):
        import json

        from ruyaml import YAML

        yaml = YAML()
        data = yaml.load('{}')
        json_str = json.dumps(data)  # NOQA
Beispiel #12
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)
Beispiel #13
0
def test_example_2_19():
    yaml = YAML()
    yaml.round_trip("""
    canonical: 12345
    decimal: +12345
    octal: 0o14
    hexadecimal: 0xC
    """)
Beispiel #14
0
def test_example_2_5():
    yaml = YAML()
    yaml.flow_sequence_element_align = True
    yaml.round_trip("""
    - [name        , hr, avg  ]
    - [Mark McGwire, 65, 0.278]
    - [Sammy Sosa  , 63, 0.288]
    """)
Beispiel #15
0
def test_example_2_2():
    yaml = YAML()
    yaml.mapping_value_align = True
    yaml.round_trip("""
    hr:  65    # Home runs
    avg: 0.278 # Batting average
    rbi: 147   # Runs Batted In
    """)
Beispiel #16
0
def test_example_2_13():
    yaml = YAML()
    yaml.round_trip(r"""
    # ASCII Art
    --- |
      \//||\/||
      // ||  ||__
    """)
Beispiel #17
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)
Beispiel #18
0
def test_example_2_20():
    yaml = YAML()
    yaml.round_trip("""
    canonical: 1.23015e+3
    exponential: 12.3015e+02
    fixed: 1230.15
    negative infinity: -.inf
    not a number: .NaN
    """)
Beispiel #19
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)
Beispiel #20
0
    def test_issue_232(self):
        import ruamel.yaml

        yaml = YAML(typ='safe', pure=True)

        with pytest.raises(ruamel.yaml.parser.ParserError):
            yaml.load(']')
        with pytest.raises(ruamel.yaml.parser.ParserError):
            yaml.load('{]')
Beispiel #21
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'
Beispiel #22
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)
Beispiel #23
0
def test_example_2_18():
    yaml = YAML()
    yaml.round_trip("""
    plain:
      This unquoted scalar
      spans many lines.

    quoted: "So does this
      quoted scalar.\n"
    """)
Beispiel #24
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)
Beispiel #25
0
 def test_02(self):
     yaml = YAML()
     yaml.indent(mapping=5, sequence=6, offset=3)
     inp = """
     a:
          b:
             -  1
             -  [1, 2]
     """
     yaml.round_trip(inp)
Beispiel #26
0
 def test_01(self):
     yaml = YAML()
     yaml.indent(sequence=6)
     yaml.indent(offset=3)
     inp = """
     a:
        -  1
        -  {b: 3}
     """
     yaml.round_trip(inp)
Beispiel #27
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'
Beispiel #28
0
 def test_root_literal_doc_indent_document_end(self):
     yaml = YAML()
     yaml.explicit_start = True
     inp = """
     --- |-
       some more
       ...
       text
     """
     yaml.round_trip(inp)
Beispiel #29
0
def test_example_2_14():
    yaml = YAML()
    yaml.explicit_start = True
    yaml.indent(root_scalar=2)  # needs to be added
    yaml.round_trip("""
    --- >
      Mark McGwire's
      year was crippled
      by a knee injury.
    """)
Beispiel #30
0
 def test_issue_280(self):
     from ruamel.yaml import YAML
     from ruamel.yaml.representer import RepresenterError
     from collections import namedtuple
     from sys import stdout
     T = namedtuple('T', ('a', 'b'))
     t = T(1, 2)
     yaml = YAML()
     with pytest.raises(RepresenterError, match='cannot represent'):
         yaml.dump({'t': t}, stdout)