summaryrefslogtreecommitdiff
path: root/_test/test_literal.py
blob: f5c42e487380068931b6686397aa34d71c903d78 (plain)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# coding: utf-8

import pytest  # type: ignore  # NOQA

from roundtrip import YAML  # type: ignore # does an automatic dedent on load


"""
YAML 1.0 allowed root level literal style without indentation:
  "Usually top level nodes are not indented" (example 4.21 in 4.6.3)
YAML 1.1 is a bit vague but says:
  "Regardless of style, scalar content must always be indented by at least one space"
  (4.4.3)
  "In general, the document’s node is indented as if it has a parent indented at -1 spaces."
  (4.3.3)
YAML 1.2 is again clear about root literal level scalar after directive in example 9.5:

%YAML 1.2
--- |
%!PS-Adobe-2.0
...
%YAML1.2
---
# Empty
...
"""


class TestNoIndent:
    def test_root_literal_scalar_indent_example_9_5(self) -> None:
        yaml = YAML()
        s = '%!PS-Adobe-2.0'
        inp = """
        --- |
          {}
        """
        d = yaml.load(inp.format(s))
        print(d)
        assert d == s + '\n'

    def test_root_literal_scalar_no_indent(self) -> None:
        yaml = YAML()
        s = 'testing123'
        inp = """
        --- |
        {}
        """
        d = yaml.load(inp.format(s))
        print(d)
        assert d == s + '\n'

    def test_root_literal_scalar_no_indent_1_1(self) -> None:
        yaml = YAML()
        s = 'testing123'
        inp = """
        %YAML 1.1
        --- |
        {}
        """
        d = yaml.load(inp.format(s))
        print(d)
        assert d == s + '\n'

    def test_root_literal_scalar_no_indent_1_1_old_style(self) -> None:
        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'

    def test_root_literal_scalar_no_indent_1_1_no_raise(self) -> None:
        # 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))

    def test_root_literal_scalar_indent_offset_one(self) -> None:
        yaml = YAML()
        s = 'testing123'
        inp = """
        --- |1
         {}
        """
        d = yaml.load(inp.format(s))
        print(d)
        assert d == s + '\n'

    def test_root_literal_scalar_indent_offset_four(self) -> None:
        yaml = YAML()
        s = 'testing123'
        inp = """
        --- |4
            {}
        """
        d = yaml.load(inp.format(s))
        print(d)
        assert d == s + '\n'

    def test_root_literal_scalar_indent_offset_two_leading_space(self) -> None:
        yaml = YAML()
        s = ' testing123'
        inp = """
        --- |4
            {s}
            {s}
        """
        d = yaml.load(inp.format(s=s))
        print(d)
        assert d == (s + '\n') * 2

    def test_root_literal_scalar_no_indent_special(self) -> None:
        yaml = YAML()
        s = '%!PS-Adobe-2.0'
        inp = """
        --- |
        {}
        """
        d = yaml.load(inp.format(s))
        print(d)
        assert d == s + '\n'

    def test_root_folding_scalar_indent(self) -> None:
        yaml = YAML()
        s = '%!PS-Adobe-2.0'
        inp = """
        --- >
          {}
        """
        d = yaml.load(inp.format(s))
        print(d)
        assert d == s + '\n'

    def test_root_folding_scalar_no_indent(self) -> None:
        yaml = YAML()
        s = 'testing123'
        inp = """
        --- >
        {}
        """
        d = yaml.load(inp.format(s))
        print(d)
        assert d == s + '\n'

    def test_root_folding_scalar_no_indent_special(self) -> None:
        yaml = YAML()
        s = '%!PS-Adobe-2.0'
        inp = """
        --- >
        {}
        """
        d = yaml.load(inp.format(s))
        print(d)
        assert d == s + '\n'

    def test_root_literal_multi_doc(self) -> None:
        yaml = YAML(typ='safe', pure=True)
        s1 = 'abc'
        s2 = 'klm'
        inp = """
        --- |-
        {}
        --- |
        {}
        """
        for idx, d1 in enumerate(yaml.load_all(inp.format(s1, s2))):
            print('d1:', d1)
            assert ['abc', 'klm\n'][idx] == d1

    def test_root_literal_doc_indent_directives_end(self) -> None:
        yaml = YAML()
        yaml.explicit_start = True
        inp = """
        --- |-
          %YAML 1.3
          ---
          this: is a test
        """
        yaml.round_trip(inp)

    def test_root_literal_doc_indent_document_end(self) -> None:
        yaml = YAML()
        yaml.explicit_start = True
        inp = """
        --- |-
          some more
          ...
          text
        """
        yaml.round_trip(inp)

    def test_root_literal_doc_indent_marker(self) -> None:
        yaml = YAML()
        yaml.explicit_start = True
        inp = """
        --- |2
           some more
          text
        """
        d = yaml.load(inp)
        print(type(d), repr(d))
        yaml.round_trip(inp)

    def test_nested_literal_doc_indent_marker(self) -> None:
        yaml = YAML()
        yaml.explicit_start = True
        inp = """
        ---
        a: |2
           some more
          text
        """
        d = yaml.load(inp)
        print(type(d), repr(d))
        yaml.round_trip(inp)


class Test_RoundTripLiteral:
    def test_rt_root_literal_scalar_no_indent(self) -> None:
        yaml = YAML()
        yaml.explicit_start = True
        s = 'testing123'
        ys = """
        --- |
        {}
        """
        ys = ys.format(s)
        d = yaml.load(ys)
        yaml.dump(d, compare=ys)

    def test_rt_root_literal_scalar_indent(self) -> None:
        yaml = YAML()
        yaml.explicit_start = True
        yaml.indent = 4
        s = 'testing123'
        ys = """
        --- |
            {}
        """
        ys = ys.format(s)
        d = yaml.load(ys)
        yaml.dump(d, compare=ys)

    def test_rt_root_plain_scalar_no_indent(self) -> None:
        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)

    def test_rt_root_plain_scalar_expl_indent(self) -> None:
        yaml = YAML()
        yaml.explicit_start = True
        yaml.indent = 4
        s = 'testing123'
        ys = """
        ---
            {}
        """
        ys = ys.format(s)
        d = yaml.load(ys)
        yaml.dump(d, compare=ys)

    def test_rt_root_sq_scalar_expl_indent(self) -> None:
        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)

    def test_rt_root_dq_scalar_expl_indent(self) -> None:
        # 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)

    def test_rt_root_literal_scalar_no_indent_no_eol(self) -> None:
        yaml = YAML()
        yaml.explicit_start = True
        s = 'testing123'
        ys = """
        --- |-
        {}
        """
        ys = ys.format(s)
        d = yaml.load(ys)
        yaml.dump(d, compare=ys)

    def test_rt_non_root_literal_scalar(self) -> None:
        yaml = YAML()
        s = 'testing123'
        ys = """
        - |
          {}
        """
        ys = ys.format(s)
        d = yaml.load(ys)
        yaml.dump(d, compare=ys)