summaryrefslogtreecommitdiff
path: root/_test/test_indentation.py
blob: 995c3b3014efab90088248955285490395613d90 (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

from typing import Any
import pytest  # type: ignore  # NOQA

from roundtrip import round_trip, round_trip_load, round_trip_dump, dedent, YAML  # type: ignore  # NOQA


def rt(s: str) -> str:
    res = round_trip_dump(round_trip_load(s))
    assert res is not None
    return res.strip() + '\n'  # type: ignore


class TestIndent:
    def test_roundtrip_inline_list(self) -> None:
        s = 'a: [a, b, c]\n'
        output = rt(s)
        assert s == output

    def test_roundtrip_mapping_of_inline_lists(self) -> None:
        s = dedent("""\
        a: [a, b, c]
        j: [k, l, m]
        """)
        output = rt(s)
        assert s == output

    def test_roundtrip_mapping_of_inline_lists_comments(self) -> None:
        s = dedent("""\
        # comment A
        a: [a, b, c]
        # comment B
        j: [k, l, m]
        """)
        output = rt(s)
        assert s == output

    def test_roundtrip_mapping_of_inline_sequence_eol_comments(self) -> None:
        s = dedent("""\
        # comment A
        a: [a, b, c]  # comment B
        j: [k, l, m]  # comment C
        """)
        output = rt(s)
        assert s == output

    # first test by explicitly setting flow style
    def test_added_inline_list(self) -> None:
        s1 = dedent("""
        a:
        - b
        - c
        - d
        """)
        s = 'a: [b, c, d]\n'
        data = round_trip_load(s1)
        val = data['a']
        val.fa.set_flow_style()
        # print(type(val), '_yaml_format' in dir(val))
        output = round_trip_dump(data)
        assert s == output

    # ############ flow mappings

    def test_roundtrip_flow_mapping(self) -> None:
        s = dedent("""\
        - {a: 1, b: hallo}
        - {j: fka, k: 42}
        """)
        data = round_trip_load(s)
        output = round_trip_dump(data)
        assert s == output

    def test_roundtrip_sequence_of_inline_mappings_eol_comments(self) -> None:
        s = dedent("""\
        # comment A
        - {a: 1, b: hallo}  # comment B
        - {j: fka, k: 42}  # comment C
        """)
        output = rt(s)
        assert s == output

    def test_indent_top_level(self) -> None:
        inp = """
        -   a:
            -   b
        """
        round_trip(inp, indent=4)

    def test_set_indent_5_block_list_indent_1(self) -> None:
        inp = """
        a:
         -   b: c
         -   1
         -   d:
              -   2
        """
        round_trip(inp, indent=5, block_seq_indent=1)

    def test_set_indent_4_block_list_indent_2(self) -> None:
        inp = """
        a:
          - b: c
          - 1
          - d:
              - 2
        """
        round_trip(inp, indent=4, block_seq_indent=2)

    def test_set_indent_3_block_list_indent_0(self) -> None:
        inp = """
        a:
        -  b: c
        -  1
        -  d:
           -  2
        """
        round_trip(inp, indent=3, block_seq_indent=0)

    def Xtest_set_indent_3_block_list_indent_2(self) -> None:
        inp = """
        a:
          -
           b: c
          -
           1
          -
           d:
             -
              2
        """
        round_trip(inp, indent=3, block_seq_indent=2)

    def test_set_indent_3_block_list_indent_2(self) -> None:
        inp = """
        a:
          - b: c
          - 1
          - d:
             - 2
        """
        round_trip(inp, indent=3, block_seq_indent=2)

    def Xtest_set_indent_2_block_list_indent_2(self) -> None:
        inp = """
        a:
          -
           b: c
          -
           1
          -
           d:
             -
              2
        """
        round_trip(inp, indent=2, block_seq_indent=2)

    # this is how it should be: block_seq_indent stretches the indent
    def test_set_indent_2_block_list_indent_2(self) -> None:
        inp = """
        a:
          - b: c
          - 1
          - d:
            - 2
        """
        round_trip(inp, indent=2, block_seq_indent=2)

    # have to set indent!
    def test_roundtrip_four_space_indents(self) -> None:
        # fmt: off
        s = (
            'a:\n'
            '-   foo\n'
            '-   bar\n'
        )
        # fmt: on
        round_trip(s, indent=4)

    def test_roundtrip_four_space_indents_no_fail(self) -> None:
        inp = """
        a:
        -   foo
        -   bar
        """
        exp = """
        a:
        - foo
        - bar
        """
        assert round_trip_dump(round_trip_load(inp)) == dedent(exp)


class TestYpkgIndent:
    def test_00(self) -> None:
        inp = """
        name       : nano
        version    : 2.3.2
        release    : 1
        homepage   : http://www.nano-editor.org
        source     :
          - http://www.nano-editor.org/dist/v2.3/nano-2.3.2.tar.gz : ff30924807ea289f5b60106be8
        license    : GPL-2.0
        summary    : GNU nano is an easy-to-use text editor
        builddeps  :
          - ncurses-devel
        description: |
            GNU nano is an easy-to-use text editor originally designed
            as a replacement for Pico, the ncurses-based editor from the non-free mailer
            package Pine (itself now available under the Apache License as Alpine).
        """
        round_trip(
            inp, indent=4, block_seq_indent=2, top_level_colon_align=True, prefix_colon=' '
        )


def guess(s: str) -> Any:
    from ruamel.yaml.util import load_yaml_guess_indent

    x, y, z = load_yaml_guess_indent(dedent(s))
    return y, z


class TestGuessIndent:
    def test_guess_20(self) -> None:
        inp = """\
        a:
        - 1
        """
        assert guess(inp) == (2, 0)

    def test_guess_42(self) -> None:
        inp = """\
        a:
          - 1
        """
        assert guess(inp) == (4, 2)

    def test_guess_42a(self) -> None:
        # block seq indent prevails over nested key indent level
        inp = """\
        b:
              a:
                - 1
        """
        assert guess(inp) == (4, 2)

    def test_guess_3None(self) -> None:
        inp = """\
        b:
           a: 1
        """
        assert guess(inp) == (3, None)


class TestSeparateMapSeqIndents:
    # using uncommon 6 indent with 3 push in as 2 push in automatically
    # gets you 4 indent even if not set
    def test_00(self) -> None:
        # old style
        yaml = YAML()
        yaml.indent = 6
        yaml.block_seq_indent = 3
        inp = """
        a:
           -  1
           -  [1, 2]
        """
        yaml.round_trip(inp)

    def test_01(self) -> None:
        yaml = YAML()
        yaml.indent(sequence=6)
        yaml.indent(offset=3)
        inp = """
        a:
           -  1
           -  {b: 3}
        """
        yaml.round_trip(inp)

    def test_02(self) -> None:
        yaml = YAML()
        yaml.indent(mapping=5, sequence=6, offset=3)
        inp = """
        a:
             b:
                -  1
                -  [1, 2]
        """
        yaml.round_trip(inp)

    def test_03(self) -> None:
        inp = """
        a:
            b:
                c:
                -   1
                -   [1, 2]
        """
        round_trip(inp, indent=4)

    def test_04(self) -> None:
        yaml = YAML()
        yaml.indent(mapping=5, sequence=6)
        inp = """
        a:
             b:
             -     1
             -     [1, 2]
             -     {d: 3.14}
        """
        yaml.round_trip(inp)

    def test_issue_51(self) -> None:
        yaml = YAML()
        # yaml.map_indent = 2 # the default
        yaml.indent(sequence=4, offset=2)
        yaml.preserve_quotes = True
        yaml.round_trip("""
        role::startup::author::rsyslog_inputs:
          imfile:
            - ruleset: 'AEM-slinglog'
              File: '/opt/aem/author/crx-quickstart/logs/error.log'
              startmsg.regex: '^[-+T.:[:digit:]]*'
              tag: 'error'
            - ruleset: 'AEM-slinglog'
              File: '/opt/aem/author/crx-quickstart/logs/stdout.log'
              startmsg.regex: '^[-+T.:[:digit:]]*'
              tag: 'stdout'
        """)


# ############ indentation