summaryrefslogtreecommitdiff
path: root/test/test_string.py
blob: eb2e95c558780bc4f26a74fd6d070b10d8e71af8 (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

from __future__ import print_function

"""
various test cases for string scalars in YAML files
'|' for preserved newlines
'>' for folded (newlines become spaces)

and the chomping modifiers:
'-' for stripping: final line break and any trailing empty lines are excluded
'+' for keeping: final line break and empty lines are preserved
''  for clipping: final line break preserved, empty lines at end not
    included in content (no modifier)

"""

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_basic_string(self):
        round_trip("""
        a: abcdefg
        """, )

    def test_quoted_string(self):
        round_trip("""
        a: '12345'
        """)

    def test_preserve_string(self):
        round_trip("""
            a: |
              abc
              def
            """, intermediate=dict(a='abc\ndef\n'))

    def test_preserve_string_strip(self):
        s = """
            a: |-
              abc
              def

            """
        o = dedent(s).rstrip() + '\n'
        round_trip(s, outp=o, intermediate=dict(a='abc\ndef'))

    def test_preserve_string_keep(self):
            # with pytest.raises(AssertionError) as excinfo:
            round_trip("""
            a: |+
              ghi
              jkl


            b: x
            """, intermediate=dict(a='ghi\njkl\n\n\n', b='x'))

    def test_preserve_string_keep_at_end(self):
        # at EOF you have to specify the ... to get proper "closure"
        # of the multiline scalar
        round_trip("""
            a: |+
              ghi
              jkl

            ...
            """, intermediate=dict(a='ghi\njkl\n\n'))

    def test_fold_string(self):
        with pytest.raises(AssertionError) as excinfo:
            round_trip("""
            a: >
              abc
              def

            """, intermediate=dict(a='abc def\n'))

    def test_fold_string_strip(self):
        with pytest.raises(AssertionError) as excinfo:
            round_trip("""
            a: >-
              abc
              def

            """, intermediate=dict(a='abc def'))

    def test_fold_string_keep(self):
        with pytest.raises(AssertionError) as excinfo:
            round_trip("""
            a: >+
              abc
              def

            """, intermediate=dict(a='abc def\n\n'))