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

"""
comment testing is all about roundtrips
these can be done in the "old" way by creating a file.data and file.roundtrip
but there is little flexibility in doing that

but some things are not easily tested, eog. how a
roundtrip changes

"""

import pytest

import ruamel.yaml
from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump


class TestComments:
    def test_no_end_of_file_eol(self):
        """not excluding comments caused some problems if at the end of
        the file without a newline. First error, then included \0 """
        x = """\
        - europe: 10 # abc"""
        round_trip(x, extra='\n')
        with pytest.raises(AssertionError):
            round_trip(x, extra='a\n')

    def test_no_comments(self):
        round_trip("""
        - europe: 10
        - usa:
          - ohio: 2
          - california: 9
        """)

    def test_round_trip_ordering(self):
        round_trip("""
        a: 1
        b: 2
        c: 3
        b1: 2
        b2: 2
        d: 4
        e: 5
        f: 6
        """)

    def test_complex(self):
        round_trip("""
        - europe: 10 # top
        - usa:
          - ohio: 2
          - california: 9 # o
        """)

    def test_dropped(self):
        round_trip("""
        # comment
        scalar
        ...
        """, "scalar\n...\n")

    def test_main_mapping_begin_end(self):
        round_trip("""
        # C start a
        # C start b
        abc: 1
        ghi: 2
        klm: 3
        # C end a
        # C end b
        """)

    def test_main_mapping_begin_end_items_post(self):
        round_trip("""
        # C start a
        # C start b
        abc: 1      # abc comment
        ghi: 2
        klm: 3      # klm comment
        # C end a
        # C end b
        """)

    def test_main_sequence_begin_end(self):
        round_trip("""
        # C start a
        # C start b
        - abc
        - ghi
        - klm
        # C end a
        # C end b
        """)

    def test_main_sequence_begin_end_items_post(self):
        round_trip("""
        # C start a
        # C start b
        - abc      # abc comment
        - ghi
        - klm      # klm comment
        # C end a
        # C end b
        """)

    def test_main_mapping_begin_end_complex(self):
        round_trip("""
        # C start a
        # C start b
        abc: 1
        ghi: 2
        klm:
          3a: alpha
          3b: beta   # it is all greek to me
        # C end a
        # C end b
        """)

    def test_09(self):   # 2.9 from the examples in the spec
        round_trip("""
        hr: # 1998 hr ranking
          - Mark McGwire
          - Sammy Sosa
        rbi:
          # 1998 rbi ranking
        - Sammy Sosa
        - Ken Griffey
        """)

    def test_simple_map_middle_comment(self):
        round_trip("""
        abc: 1
        # C 3a
        # C 3b
        ghi: 2
        """)

    def test_map_in_map_0(self):
        round_trip("""
        map1: # comment 1
          # comment 2
          map2:
            key1: val1
        """)

    def test_map_in_map_1(self):
        # comment is moved from value to key
        round_trip("""
        map1:
          # comment 1
          map2:
            key1: val1
        """)

    def test_application_arguments(self):
        # application configur
        round_trip("""
        args:
          username: anthon
          passwd: secret
          fullname: Anthon van der Neut
          tmux:
            session-name: test
          loop:
            wait: 10
        """)

    def test_substitute(self):
        x = dedent("""
        args:
          username: anthon          # name
          passwd: secret            # password
          fullname: Anthon van der Neut
          tmux:
            session-name: test
          loop:
            wait: 10
        """)
        data = round_trip_load(x)
        data['args']['passwd'] = 'deleted password'
        # note the requirement to add spaces for alignment of comment
        x = x.replace(': secret          ', ': deleted password')
        assert round_trip_dump(data) == x


    def test_set_comment(self):
        round_trip("""
        !!set
        # the beginning
        ? a
        # next one is B (lowercase)
        ? b  #  You see? Promised you.
        ? c
        # this is the end
        """)

    @pytest.mark.xfail
    def XXXtest_set_comment_before_tag(self):
        # no comments before tags
        round_trip("""
        # the beginning
        !!set
        # or this one?
        ? a
        # next one is B (lowercase)
        ? b  #  You see? Promised you.
        ? c
        # this is the end
        """)

    def test_omap_comment_roundtrip(self):
        round_trip("""
        !!omap
        - a: 1
        - b: 2  # two
        - c: 3  # three
        - d: 4
        """)

    def test_omap_comment_roundtrip_pre_comment(self):
        round_trip("""
        !!omap
        - a: 1
        - b: 2  # two
        - c: 3  # three
        # last one
        - d: 4
        """)

    @pytest.mark.xfail
    def test_non_ascii_comment(self):
        round_trip("""
        verbosity: 1                  # 0 is minimal output, -1 none
        base_url: http://gopher.net
        special_indices: [1, 5, 8]
        also_special:
        - a
        - 19
        - 32
        asia and europe: &asia_europe
            Turkey: Ankara
            Russia: Moscow
        countries:
            Asia:
                <<: *asia_europe
                Japan: Tokyo # 東京
            Europe:
                <<: *asia_europe
                Spain: Madrid
                Italy: Rome
        """)


class TestMultiLevelGet:
    def test_mlget_00(self):
        x = dedent("""\
        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
        with pytest.raises(AssertionError):
            d.mlget(['a', 1, 'd', 'f']) == 196