Beispiel #1
0
    def test_duplicate_key_00(self):
        from ruyaml import round_trip_load, safe_load, version_info
        from ruyaml.constructor import DuplicateKeyError, DuplicateKeyFutureWarning

        s = dedent(
            """\
        &anchor foo:
            foo: bar
            *anchor : duplicate key
            baz: bat
            *anchor : duplicate key
        """
        )
        if version_info < (0, 15, 1):
            pass
        elif version_info < (0, 16, 0):
            with pytest.warns(DuplicateKeyFutureWarning):
                safe_load(s)
            with pytest.warns(DuplicateKeyFutureWarning):
                round_trip_load(s)
        else:
            with pytest.raises(DuplicateKeyError):
                safe_load(s)
            with pytest.raises(DuplicateKeyError):
                round_trip_load(s)
Beispiel #2
0
    def test_yaml_1_1_no_dot(self):
        from ruamel.yaml.error import MantissaNoDotYAML1_1Warning

        with pytest.warns(MantissaNoDotYAML1_1Warning):
            round_trip_load("""\
            %YAML 1.1
            ---
            - 1e6
            """)
Beispiel #3
0
    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
Beispiel #4
0
 def test_issue_219(self):
     yaml_str = dedent(
         """\
     [StackName: AWS::StackName]
     """
     )
     d = round_trip_load(yaml_str)  # NOQA
Beispiel #5
0
    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
Beispiel #6
0
 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
Beispiel #7
0
    def test_issue_176_test_slicing(self):
        from ruamel.yaml.compat import PY2

        mss = round_trip_load('[0, 1, 2, 3, 4]')
        assert len(mss) == 5
        assert mss[2:2] == []
        assert mss[2:4] == [2, 3]
        assert mss[1::2] == [1, 3]

        # slice assignment
        m = mss[:]
        m[2:2] = [42]
        assert m == [0, 1, 42, 2, 3, 4]

        m = mss[:]
        m[:3] = [42, 43, 44]
        assert m == [42, 43, 44, 3, 4]
        m = mss[:]
        m[2:] = [42, 43, 44]
        assert m == [0, 1, 42, 43, 44]
        m = mss[:]
        m[:] = [42, 43, 44]
        assert m == [42, 43, 44]

        # extend slice assignment
        m = mss[:]
        m[2:4] = [42, 43, 44]
        assert m == [0, 1, 42, 43, 44, 4]
        m = mss[:]
        m[1::2] = [42, 43]
        assert m == [0, 42, 2, 43, 4]
        m = mss[:]
        if PY2:
            with pytest.raises(ValueError, match='attempt to assign'):
                m[1::2] = [42, 43, 44]
        else:
            with pytest.raises(TypeError, match='too many'):
                m[1::2] = [42, 43, 44]
        if PY2:
            with pytest.raises(ValueError, match='attempt to assign'):
                m[1::2] = [42]
        else:
            with pytest.raises(TypeError, match='not enough'):
                m[1::2] = [42]
        m = mss[:]
        m += [5]
        m[1::2] = [42, 43, 44]
        assert m == [0, 42, 2, 43, 4, 44]

        # deleting
        m = mss[:]
        del m[1:3]
        assert m == [0, 3, 4]
        m = mss[:]
        del m[::2]
        assert m == [1, 3]
        m = mss[:]
        del m[:]
        assert m == []
Beispiel #8
0
 def test_mul_00(self):
     # issue 149 reported by [email protected]
     d = round_trip_load("""\
     - 0.1
     """)
     d[0] *= -1
     x = round_trip_dump(d)
     assert x == '- -0.1\n'
Beispiel #9
0
 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
Beispiel #10
0
 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
Beispiel #11
0
 def test_some_eol_spaces(self):
     # spaces after tokens and on empty lines
     yaml_str = '---  \n  \na: "x"  \n   \nb: y  \n'
     d = round_trip_load(yaml_str, preserve_quotes=True)
     y = round_trip_dump(d, explicit_start=True)
     stripped = ""
     for line in yaml_str.splitlines():
         stripped += line.rstrip() + '\n'
         print(line + '$')
     assert stripped == y
Beispiel #12
0
 def test_line_with_only_spaces(self):
     # issue 54
     yaml_str = "---\n\na: 'x'\n \nb: y\n"
     d = round_trip_load(yaml_str, preserve_quotes=True)
     y = round_trip_dump(d, explicit_start=True)
     stripped = ""
     for line in yaml_str.splitlines():
         stripped += line.rstrip() + '\n'
         print(line + '$')
     assert stripped == y
Beispiel #13
0
 def test_issue_60(self):
     data = round_trip_load("""
     x: &base
       a: 1
     y:
       <<: *base
     """)
     assert data['x']['a'] == 1
     assert data['y']['a'] == 1
     assert str(data['y']) == """ordereddict([('a', 1)])"""
Beispiel #14
0
    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'
Beispiel #15
0
    def test_merge_items(self):
        from ruamel.yaml import YAML

        d = YAML(typ='safe', pure=True).load(self.yaml_str)
        data = round_trip_load(self.yaml_str)
        count = 0
        for x in data[2].items():
            count += 1
            print(count, x)
        assert count == len(d[2])
Beispiel #16
0
    def test_merge_items(self):
        from ruamel.yaml import safe_load

        d = safe_load(self.yaml_str)
        data = round_trip_load(self.yaml_str)
        count = 0
        for x in data[2].items():
            count += 1
            print(count, x)
        assert count == len(d[2])
Beispiel #17
0
 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'
Beispiel #18
0
 def test_roundtrip_four_space_indents_expl_indent_no_fail(self):
     assert round_trip_dump(round_trip_load("""
     a:
     -   foo
     -   bar
     """),
                            indent=4) == dedent("""
     a:
     -   foo
     -   bar
     """)
Beispiel #19
0
 def test_insert_at_pos_0(self):
     d = round_trip_load(self.ins)
     d.insert(0, 'last name', 'Vandelay', comment="new key")
     y = round_trip_dump(d)
     print(y)
     assert y == dedent("""\
     last name: Vandelay  # new key
     first_name: Art
     occupation: Architect  # This is an occupation comment
     about: Art Vandelay is a fictional character that George invents...
     """)
Beispiel #20
0
 def test_preserve_flow_style_simple(self):
     x = dedent("""\
     {foo: bar, baz: quux}
     """)
     data = round_trip_load(x)
     data_copy = copy.deepcopy(data)
     y = round_trip_dump(data_copy)
     print('x [{}]'.format(x))
     print('y [{}]'.format(y))
     assert y == x
     assert data.fa.flow_style() == data_copy.fa.flow_style()
 def test_00(self, capsys):
     s = dedent("""
     a: 1
     b: []
     c: [a, 1]
     d: {f: 3.14, g: 42}
     """)
     d = round_trip_load(s)
     round_trip_dump(d, sys.stdout)
     out, err = capsys.readouterr()
     assert out == s
 def test_01(self, capsys):
     s = dedent("""
     - 1
     - []
     - [a, 1]
     - {f: 3.14, g: 42}
     - - 123
     """)
     d = round_trip_load(s)
     round_trip_dump(d, sys.stdout)
     out, err = capsys.readouterr()
     assert out == s
Beispiel #23
0
 def test_standard_short_tag_no_fail(self):
     assert round_trip_dump(
         round_trip_load("""
     !!map
     name: Anthon
     location: Germany
     language: python
     """)) == dedent("""
     name: Anthon
     location: Germany
     language: python
     """)
Beispiel #24
0
 def test_insert_at_pos_3(self):
     # much more simple if done with appending.
     d = round_trip_load(self.ins)
     d.insert(3, 'last name', 'Vandelay', comment="new key")
     y = round_trip_dump(d)
     print(y)
     assert y == dedent("""\
     first_name: Art
     occupation: Architect  # This is an occupation comment
     about: Art Vandelay is a fictional character that George invents...
     last name: Vandelay  # new key
     """)
Beispiel #25
0
    def test_change_value_simple_mapping_key(self):
        from ruyaml.comments import CommentedKeyMap

        inp = """\
        {a: 1, b: 2}: hello world
        """
        d = round_trip_load(inp, preserve_quotes=True)
        d = {CommentedKeyMap([('a', 1), ('b', 2)]): 'goodbye'}
        exp = dedent("""\
        {a: 1, b: 2}: goodbye
        """)
        assert round_trip_dump(d) == exp
Beispiel #26
0
 def test_mlget_00(self):
     x = """\
     a:
     - b:
       c: 42
     - d:
         f: 196
       e:
         g: 3.14
     """
     d = round_trip_load(x)
     assert d.mlget(['a', 1, 'd', 'f'], list_ok=True) == 196
Beispiel #27
0
 def test_roundtrip_four_space_indents_no_fail(self):
     inp = """
     a:
     -   foo
     -   bar
     """
     exp = """
     a:
     - foo
     - bar
     """
     assert round_trip_dump(round_trip_load(inp)) == dedent(exp)
Beispiel #28
0
    def test_issue_54_not_ok(self):
        yaml_str = dedent("""\
        toplevel:

            # some comment
            sublevel: 300
        """)
        d = round_trip_load(yaml_str)
        print(d.ca)
        y = round_trip_dump(d, indent=4)
        print(y.replace('\n', '$\n'))
        assert yaml_str == y
Beispiel #29
0
 def test_comment_dash_line_fail(self):
     x = """
     - # abc
        a: 1
        b: 2
     """
     data = round_trip_load(x)
     # this is not nice
     assert round_trip_dump(data) == dedent("""
       # abc
     - a: 1
       b: 2
     """)
Beispiel #30
0
 def test_reindent(self):
     x = """\
     a:
       b:     # comment 1
         c: 1 # comment 2
     """
     d = round_trip_load(x)
     y = round_trip_dump(d, indent=4)
     assert y == dedent("""\
     a:
         b:   # comment 1
             c: 1 # comment 2
     """)