File: test_yamlfile.py

package info (click to toggle)
python-ruyaml 0.92.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,732 kB
  • sloc: python: 19,399; makefile: 200; sh: 13
file content (261 lines) | stat: -rw-r--r-- 6,337 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# coding: utf-8

"""
various test cases for YAML files
"""

import io
import platform
import sys

import pytest  # type: ignore # NOQA
from roundtrip import (  # type: ignore # NOQA
    dedent,
    round_trip,
    round_trip_dump,
    round_trip_load,
)


class TestYAML:
    def test_backslash(self) -> None:
        round_trip(
            """
        handlers:
          static_files: applications/\\1/static/\\2
        """
        )

    def test_omap_out(self) -> None:
        # ordereddict mapped to !!omap
        import ruyaml  # NOQA
        from ruyaml.compat import ordereddict

        x = ordereddict([('a', 1), ('b', 2)])
        res = round_trip_dump(x, default_flow_style=False)
        assert res == dedent(
            """
        !!omap
        - a: 1
        - b: 2
        """
        )

    def test_omap_roundtrip(self) -> None:
        round_trip(
            """
        !!omap
        - a: 1
        - b: 2
        - c: 3
        - d: 4
        """
        )

    # @pytest.mark.skipif(sys.version_info < (2, 7),
    #                     reason='collections not available')
    # def test_dump_collections_ordereddict(self) -> None:
    #     from collections import OrderedDict
    #     import ruyaml  # NOQA

    #     # OrderedDict mapped to !!omap
    #     x = OrderedDict([('a', 1), ('b', 2)])
    #     res = round_trip_dump(x, default_flow_style=False)
    #     assert res == dedent("""
    #     !!omap
    #     - a: 1
    #     - b: 2
    #     """)

    @pytest.mark.skipif(  # type: ignore
        sys.version_info >= (3, 0) or platform.python_implementation() != 'CPython',
        reason='ruyaml not available',
    )
    def test_dump_ruyaml_ordereddict(self) -> None:
        import ruyaml  # NOQA
        from ruyaml.compat import ordereddict

        # OrderedDict mapped to !!omap
        x = ordereddict([('a', 1), ('b', 2)])
        res = round_trip_dump(x, default_flow_style=False)
        assert res == dedent(
            """
        !!omap
        - a: 1
        - b: 2
        """
        )

    def test_CommentedSet(self) -> None:
        from ruyaml.constructor import CommentedSet

        s = CommentedSet(['a', 'b', 'c'])
        s.remove('b')
        s.add('d')
        assert s == CommentedSet(['a', 'c', 'd'])
        s.add('e')
        s.add('f')
        s.remove('e')
        assert s == CommentedSet(['a', 'c', 'd', 'f'])

    def test_set_out(self) -> None:
        # preferable would be the shorter format without the ': null'
        import ruyaml  # NOQA

        x = set(['a', 'b', 'c'])  # NOQA
        # cannot use round_trip_dump, it doesn't show null in block style
        buf = io.StringIO()
        with pytest.warns(PendingDeprecationWarning):
            yaml = ruyaml.YAML(typ='unsafe', pure=True)
        yaml.default_flow_style = False
        yaml.dump(x, buf)
        assert buf.getvalue() == dedent(
            """
        !!set
        a: null
        b: null
        c: null
        """
        )

    # ordering is not preserved in a set
    def test_set_compact(self) -> None:
        # this format is read and also should be written by default
        round_trip(
            """
        !!set
        ? a
        ? b
        ? c
        """
        )

    def test_set_compact_flow(self) -> None:
        # this format is read and also should be written by default
        round_trip(
            """
        !!set {a, b, c}
        """
        )

    def test_blank_line_after_comment(self) -> None:
        round_trip(
            """
        # Comment with spaces after it.


        a: 1
        """
        )

    def test_blank_line_between_seq_items(self) -> None:
        round_trip(
            """
        # Seq with empty lines in between items.
        b:
        - bar


        - baz
        """
        )

    @pytest.mark.skipif(  # type: ignore
        platform.python_implementation() == 'Jython',
        reason='Jython throws RepresenterError',
    )
    def test_blank_line_after_literal_chip(self) -> None:
        s = """
        c:
        - |
          This item
          has a blank line
          following it.

        - |
          To visually separate it from this item.

          This item contains a blank line.


        """
        d = round_trip_load(dedent(s))
        print(d)
        round_trip(s)
        assert d['c'][0].split('it.')[1] == '\n'
        assert d['c'][1].split('line.')[1] == '\n'

    @pytest.mark.skipif(  # type: ignore
        platform.python_implementation() == 'Jython',
        reason='Jython throws RepresenterError',
    )
    def test_blank_line_after_literal_keep(self) -> None:
        """have to insert an eof marker in YAML to test this"""
        s = """
        c:
        - |+
          This item
          has a blank line
          following it.

        - |+
          To visually separate it from this item.

          This item contains a blank line.


        ...
        """
        d = round_trip_load(dedent(s))
        print(d)
        round_trip(s)
        assert d['c'][0].split('it.')[1] == '\n\n'
        assert d['c'][1].split('line.')[1] == '\n\n\n'

    @pytest.mark.skipif(  # type: ignore
        platform.python_implementation() == 'Jython',
        reason='Jython throws RepresenterError',
    )
    def test_blank_line_after_literal_strip(self) -> None:
        s = """
        c:
        - |-
          This item
          has a blank line
          following it.

        - |-
          To visually separate it from this item.

          This item contains a blank line.


        """
        d = round_trip_load(dedent(s))
        print(d)
        round_trip(s)
        assert d['c'][0].split('it.')[1] == ""
        assert d['c'][1].split('line.')[1] == ""

    def test_load_all_perserve_quotes(self) -> None:
        import ruyaml  # NOQA

        yaml = ruyaml.YAML()
        yaml.preserve_quotes = True
        s = dedent(
            """\
        a: 'hello'
        ---
        b: "goodbye"
        """
        )
        data = []
        for x in yaml.load_all(s):
            data.append(x)
        buf = ruyaml.compat.StringIO()
        yaml.dump_all(data, buf)
        out = buf.getvalue()
        print(type(data[0]['a']), data[0]['a'])
        # out = ruyaml.round_trip_dump_all(data)
        print(out)
        assert out == s