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
|
# coding: utf-8
"""
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 platform
import pytest
# from ruyaml.compat import ordereddict
from roundtrip import ( # type: ignore # NOQA
dedent,
round_trip,
round_trip_dump,
round_trip_load,
)
import ruyaml
class TestLiteralScalarString:
def test_basic_string(self) -> None:
round_trip(
"""
a: abcdefg
"""
)
def test_quoted_integer_string(self) -> None:
round_trip(
"""
a: '12345'
"""
)
@pytest.mark.skipif( # type: ignore
platform.python_implementation() == 'Jython',
reason='Jython throws RepresenterError',
)
def test_preserve_string(self) -> None:
inp = """
a: |
abc
def
"""
round_trip(inp, intermediate=dict(a='abc\ndef\n'))
@pytest.mark.skipif( # type: ignore
platform.python_implementation() == 'Jython',
reason='Jython throws RepresenterError',
)
def test_preserve_string_strip(self) -> None:
s = """
a: |-
abc
def
"""
round_trip(s, intermediate=dict(a='abc\ndef'))
@pytest.mark.skipif( # type: ignore
platform.python_implementation() == 'Jython',
reason='Jython throws RepresenterError',
)
def test_preserve_string_keep(self) -> None:
# with pytest.raises(AssertionError) as excinfo:
inp = """
a: |+
ghi
jkl
b: x
"""
round_trip(inp, intermediate=dict(a='ghi\njkl\n\n\n', b='x'))
@pytest.mark.skipif( # type: ignore
platform.python_implementation() == 'Jython',
reason='Jython throws RepresenterError',
)
def test_preserve_string_keep_at_end(self) -> None:
# at EOF you have to specify the ... to get proper "closure"
# of the multiline scalar
inp = """
a: |+
ghi
jkl
...
"""
round_trip(inp, intermediate=dict(a='ghi\njkl\n\n'))
def test_fold_string(self) -> None:
inp = """
a: >
abc
def
"""
round_trip(inp)
def test_fold_string_strip(self) -> None:
inp = """
a: >-
abc
def
"""
round_trip(inp)
def test_fold_string_keep(self) -> None:
with pytest.raises(AssertionError) as excinfo: # NOQA
inp = """
a: >+
abc
def
"""
round_trip(inp, intermediate=dict(a='abc def\n\n'))
class TestQuotedScalarString:
def test_single_quoted_string(self) -> None:
inp = """
a: 'abc'
"""
round_trip(inp, preserve_quotes=True)
def test_double_quoted_string(self) -> None:
inp = """
a: "abc"
"""
round_trip(inp, preserve_quotes=True)
def test_non_preserved_double_quoted_string(self) -> None:
inp = """
a: "abc"
"""
exp = """
a: abc
"""
round_trip(inp, outp=exp)
class TestReplace:
"""inspired by issue 110 from sandres23"""
def test_replace_preserved_scalar_string(self) -> None:
s = dedent(
"""\
foo: |
foo
foo
bar
foo
"""
)
data = round_trip_load(s, preserve_quotes=True)
so = data['foo'].replace('foo', 'bar', 2)
assert isinstance(so, ruyaml.scalarstring.LiteralScalarString)
assert so == dedent(
"""
bar
bar
bar
foo
"""
)
def test_replace_double_quoted_scalar_string(self) -> None:
s = dedent(
"""\
foo: "foo foo bar foo"
"""
)
data = round_trip_load(s, preserve_quotes=True)
so = data['foo'].replace('foo', 'bar', 2)
assert isinstance(so, ruyaml.scalarstring.DoubleQuotedScalarString)
assert so == 'bar bar bar foo'
class TestWalkTree:
def test_basic(self) -> None:
from ruyaml.comments import CommentedMap
from ruyaml.scalarstring import walk_tree
data = CommentedMap()
data[1] = 'a'
data[2] = 'with\nnewline\n'
walk_tree(data)
exp = """\
1: a
2: |
with
newline
"""
assert round_trip_dump(data) == dedent(exp)
def test_map(self) -> None:
from ruyaml.comments import CommentedMap
from ruyaml.compat import ordereddict
from ruyaml.scalarstring import DoubleQuotedScalarString as dq
from ruyaml.scalarstring import SingleQuotedScalarString as sq
from ruyaml.scalarstring import preserve_literal, walk_tree
data = CommentedMap()
data[1] = 'a'
data[2] = 'with\nnew : line\n'
data[3] = '${abc}'
data[4] = 'almost:mapping'
m = ordereddict([('\n', preserve_literal), ('${', sq), (':', dq)])
walk_tree(data, map=m)
exp = """\
1: a
2: |
with
new : line
3: '${abc}'
4: "almost:mapping"
"""
assert round_trip_dump(data) == dedent(exp)
|