Ejemplo n.º 1
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
Ejemplo n.º 2
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
Ejemplo n.º 3
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}}]
     """)
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def test_example_2_1():
    yaml = YAML()
    yaml.round_trip("""
    - Mark McGwire
    - Sammy Sosa
    - Ken Griffey
    """)
Ejemplo n.º 6
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]
Ejemplo n.º 7
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
Ejemplo n.º 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)
Ejemplo n.º 9
0
    def test_issue_233a(self):
        import json

        from ruyaml import YAML

        yaml = YAML()
        data = yaml.load('[]')
        json_str = json.dumps(data)  # NOQA
Ejemplo n.º 10
0
def test_example_2_19():
    yaml = YAML()
    yaml.round_trip("""
    canonical: 12345
    decimal: +12345
    octal: 0o14
    hexadecimal: 0xC
    """)
Ejemplo n.º 11
0
 def test_sequence2(self):
     yaml = YAML()
     yaml.mapping_value_align = True
     yaml.round_trip(
         """
     - !Sequence [a, b: 1, c: {d: 3}]
     """
     )
Ejemplo n.º 12
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]
    """)
Ejemplo n.º 13
0
def test_example_2_13():
    yaml = YAML()
    yaml.round_trip(r"""
    # ASCII Art
    --- |
      \//||\/||
      // ||  ||__
    """)
Ejemplo n.º 14
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
    """)
Ejemplo n.º 15
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
    """)
Ejemplo n.º 16
0
 def test_root_literal_doc_indent_directives_end(self):
     yaml = YAML()
     yaml.explicit_start = True
     inp = """
     --- |-
       %YAML 1.3
       ---
       this: is a test
     """
     yaml.round_trip(inp)
Ejemplo n.º 17
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'
Ejemplo n.º 18
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)
Ejemplo n.º 19
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'
Ejemplo n.º 20
0
 def test_root_literal_doc_indent_document_end(self):
     yaml = YAML()
     yaml.explicit_start = True
     inp = """
     --- |-
       some more
       ...
       text
     """
     yaml.round_trip(inp)
Ejemplo n.º 21
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'
Ejemplo n.º 22
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"
    """)
Ejemplo n.º 23
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'
Ejemplo n.º 24
0
def test_example_2_6():
    yaml = YAML()
    # yaml.flow_mapping_final_comma = False
    yaml.flow_mapping_one_element_per_line = True
    yaml.round_trip("""
    Mark McGwire: {hr: 65, avg: 0.278}
    Sammy Sosa: {
        hr: 63,
        avg: 0.288
      }
    """)
Ejemplo n.º 25
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'
Ejemplo n.º 26
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
Ejemplo n.º 27
0
 def test_00(self):
     # old style
     yaml = YAML()
     yaml.indent = 6
     yaml.block_seq_indent = 3
     inp = """
     a:
        -  1
        -  [1, 2]
     """
     yaml.round_trip(inp)
Ejemplo n.º 28
0
def test_example_2_16():
    yaml = YAML()
    yaml.round_trip("""
    name: Mark McGwire
    accomplishment: >
      Mark set a major league
      home run record in 1998.
    stats: |
      65 Home Runs
      0.278 Batting Average
    """)
Ejemplo n.º 29
0
    def test_issue_280(self):
        from collections import namedtuple
        from sys import stdout

        from ruyaml import YAML
        from ruyaml.representer import RepresenterError

        T = namedtuple('T', ('a', 'b'))
        t = T(1, 2)
        yaml = YAML()
        with pytest.raises(RepresenterError, match='cannot represent'):
            yaml.dump({'t': t}, stdout)
Ejemplo n.º 30
0
def test_example_2_15():
    yaml = YAML()
    yaml.round_trip("""
    >
     Sammy Sosa completed another
     fine season with great stats.

       63 Home Runs
       0.288 Batting Average

     What a year!
    """)