summaryrefslogtreecommitdiff
path: root/_test/test_indentation.py
blob: 528e3b64754a08d3d2dfb02149aedac1976cab0c (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
# coding: utf-8

from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals


import pytest  # NOQA

from roundtrip import round_trip, round_trip_load, round_trip_dump, dedent, YAML


def rt(s):
    import ruamel.yaml
    return ruamel.yaml.dump(
        ruamel.yaml.load(s, Loader=ruamel.yaml.RoundTripLoader),
        Dumper=ruamel.yaml.RoundTripDumper,
    ).strip() + '\n'


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

    def test_roundtrip_mapping_of_inline_lists(self):
        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):
        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):
        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):
        import ruamel.yaml
        s1 = dedent("""
        a:
        - b
        - c
        - d
        """)
        s = 'a: [b, c, d]\n'
        data = ruamel.yaml.load(s1, Loader=ruamel.yaml.RoundTripLoader)
        val = data['a']
        val.fa.set_flow_style()
        # print(type(val), '_yaml_format' in dir(val))
        output = ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper)
        assert s == output

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

    def test_roundtrip_flow_mapping(self):
        import ruamel.yaml
        s = dedent("""\
        - {a: 1, b: hallo}
        - {j: fka, k: 42}
        """)
        data = ruamel.yaml.load(s, Loader=ruamel.yaml.RoundTripLoader)
        output = ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper)
        assert s == output

    def test_roundtrip_sequence_of_inline_mappings_eol_comments(self):
        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):
        round_trip("""
        -   a:
            -   b
        """, indent=4)

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

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

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

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

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

    def Xtest_set_indent_2_block_list_indent_2(self):
        round_trip("""
        a:
          -
           b: c
          -
           1
          -
           d:
             -
              2
        """, 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):
        round_trip("""
        a:
          - b: c
          - 1
          - d:
            - 2
        """, indent=2, block_seq_indent=2)

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

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


class TestYpkgIndent:
    def test_00(self):
        round_trip("""
        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).
        """, indent=4, block_seq_indent=2, top_level_colon_align=True, prefix_colon=' ')


def guess(s):
    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):
        assert guess("""\
        a:
        - 1
        """) == (2, 0)

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

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

    def test_guess_3None(self):
        assert guess("""\
        b:
           a: 1
        """) == (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):
        # old style
        yaml = YAML()
        yaml.indent = 6
        yaml.block_seq_indent = 3
        yaml.round_trip("""
        a:
           -  1
           -  [1, 2]
        """)

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

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

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

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

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