summaryrefslogtreecommitdiff
path: root/test/test_yaml.py
blob: 503e1532327fe5183699c6137f18e435b0c11e96 (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

from __future__ import print_function

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

import pytest

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


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

    def test_omap_out(self):
        # ordereddict mapped to !!omap
        x = ordereddict([('a', 1), ('b', 2)])
        res = ruamel.yaml.dump(x, default_flow_style=False)
        assert res == dedent("""
        !!omap
        - a: 1
        - b: 2
        """)

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

    def test_CommentedSet(self):
        from ruamel.yaml.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):
        # preferable would be the shorter format without the ': null'
        from ruamel.yaml.compat import ordereddict
        x = set(['a', 'b', 'c'])
        res = ruamel.yaml.dump(x, default_flow_style=False)
        assert res == dedent("""
        !!set
        a: null
        b: null
        c: null
        """)

    # @pytest.mark.xfail
    # ordering is not preserved in a set
    def test_set_compact(self):
        # this format is read and also should be written by default
        round_trip("""
        !!set
        ? a
        ? b
        ? c
        """)

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


        a: 1
        """)

    def test_blank_line_between_seq_items(self):
        round_trip("""
        # Seq with spaces in between items.
        b:
        - bar


        - baz
        """)

    def test_blank_line_after_literal(self):
        round_trip("""
        c:
        - |
          This item
          has a blank line
          following it.

        - |
          To visually separate it from this item.

          This item contains a blank line.


        """)