-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_roundtrip.py
More file actions
139 lines (111 loc) · 4.59 KB
/
Copy pathtest_roundtrip.py
File metadata and controls
139 lines (111 loc) · 4.59 KB
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
"""Round-trip preservation tests."""
import math
from yamltrip import Document
class TestCommentPreservation:
def test_inline_comment(self):
source = "name: foo # important\nage: 30\n"
doc = Document(source).replace("name", value="bar")
assert "# important" in doc.source
def test_header_comment(self):
source = "# File header\nname: foo\n"
doc = Document(source).replace("name", value="bar")
assert "# File header" in doc.source
def test_comment_between_keys(self):
source = "a: 1\n# middle\nb: 2\n"
doc = Document(source).replace("a", value=10)
assert "# middle" in doc.source
class TestQuotePreservation:
def test_single_quotes_replaced(self):
source = "name: 'foo'\n"
doc = Document(source).replace("name", value="bar")
# The value is replaced; we mainly check it round-trips without errors
assert "bar" in doc.source
def test_double_quotes(self):
source = 'name: "foo"\n'
doc = Document(source).replace("name", value="bar")
assert "bar" in doc.source
class TestBlankLinePreservation:
def test_blank_lines_between_sections(self):
source = "a: 1\n\nb: 2\n"
doc = Document(source).replace("a", value=10)
assert "\n\n" in doc.source
def test_trailing_newline(self):
source = "name: foo\n"
doc = Document(source).replace("name", value="bar")
assert doc.source.endswith("\n")
class TestIndentationPreservation:
def test_nested_indent_preserved(self):
source = "parent:\n child: foo\n other: bar\n"
doc = Document(source).replace("parent", "child", value="baz")
# Check indentation is preserved (4-space indent)
lines = doc.source.split("\n")
child_line = next(line for line in lines if "child" in line)
assert child_line.startswith(" ")
def test_two_space_indent(self):
source = "parent:\n child: foo\n"
doc = Document(source).replace("parent", "child", value="baz")
lines = doc.source.split("\n")
child_line = next(line for line in lines if "child" in line)
assert child_line.startswith(" ")
class TestSequencePreservation:
def test_append_preserves_sequence_style(self):
source = "items:\n - a\n - b\n"
doc = Document(source).append("items", value="c")
assert "- a" in doc.source
assert "- b" in doc.source
assert "- c" in doc.source
def test_remove_preserves_others(self):
source = "items:\n - a\n - b\n - c\n"
doc = Document(source).remove_from_list("items", values=["b"])
assert "- a" in doc.source
assert "- c" in doc.source
assert "- b" not in doc.source
class TestIdempotent:
def test_no_op_roundtrip(self):
source = "# header\nname: foo # inline\nitems:\n - a\n - b\n"
doc = Document(source)
assert doc.source == source
def test_replace_same_value(self):
source = "name: foo\n"
doc = Document(source).replace("name", value="foo")
# Value should still be there
assert doc["name"] == "foo"
class TestComplexDocument:
def test_multi_operation_preserves_structure(self):
source = (
"# Config file\n"
"server:\n"
" host: localhost # default\n"
" port: 8080\n"
"\n"
"database:\n"
" url: postgres://localhost\n"
" pool: 5\n"
)
doc = Document(source)
doc = doc.replace("server", "port", value=9090)
doc = doc.replace("database", "pool", value=10)
assert "# Config file" in doc.source
assert "# default" in doc.source
assert doc["server", "host"] == "localhost"
assert doc["server", "port"] == 9090
assert doc["database", "pool"] == 10
class TestNonFiniteFloatRoundTrip:
def test_nan_replace_roundtrip(self):
source = "val: .nan\n"
doc = Document(source)
assert math.isnan(doc[("val",)])
doc2 = doc.replace("val", value=float("nan"))
assert math.isnan(doc2[("val",)])
def test_inf_replace_roundtrip(self):
source = "val: .inf\n"
doc = Document(source)
assert doc[("val",)] == float("inf")
doc2 = doc.replace("val", value=float("inf"))
assert doc2[("val",)] == float("inf")
def test_neg_inf_replace_roundtrip(self):
source = "val: -.inf\n"
doc = Document(source)
assert doc[("val",)] == float("-inf")
doc2 = doc.replace("val", value=float("-inf"))
assert doc2[("val",)] == float("-inf")