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
|
# 3rd party
import tomllib as tomli
# this package
import dom_toml
from dom_toml.encoder import TomlEncoder
def test_newline_before_table():
actual = dom_toml.dumps({"table": {}})
expected = "[table]\n"
assert actual == expected
actual = dom_toml.dumps({"table": {"nested": {}, "val3": 3}, "val2": 2, "val1": 1})
expected = """\
val2 = 2
val1 = 1
[table]
val3 = 3
[table.nested]
"""
assert actual == expected
def test_empty_doc():
assert dom_toml.dumps({}) == ''
def test_dont_write_redundant_tables():
actual = dom_toml.dumps({"tab1": {"tab2": {"tab3": {}}}})
expected = "[tab1.tab2.tab3]\n"
assert actual == expected
def test_multiline():
multiline_string = ("This is longer than threshold!\nShould be formatted as a multiline basic string")
actual = dom_toml.dumps({"ml_string": multiline_string}, encoder=TomlEncoder(multiline_strings=True))
expected = '''\
ml_string = """
This is longer than threshold!
Should be formatted as a multiline basic string"""
'''
assert actual == expected
def test_only_tables():
actual = dom_toml.dumps({"tab1": {}, "tab2": {}})
expected = """\
[tab1]
[tab2]
"""
assert actual == expected
def test_tricky_keys():
actual = dom_toml.dumps({'f': 1, "tab1": {}, '': {'f': 2, '': {'': 1}}, "tab3": {}})
expected = """\
f = 1
[tab1]
[""]
f = 2
["".""]
"" = 1
[tab3]
"""
assert actual == expected
def test_nested_keys():
actual = dom_toml.dumps({
'k': 1,
'a': {'b': {'c': {'d': {'e': {'f': {}}, "e2": {"f2": {}}}, "d_key1": 1}}},
})
expected = """\
k = 1
[a.b.c]
d_key1 = 1
[a.b.c.d.e.f]
[a.b.c.d.e2.f2]
"""
assert actual == expected
def test_array_of_tables_containing_lists():
example: dict = {"aot": [{'a': [0, 1, 2, 3]}]}
expected = """\
aot = [ { a = [ 0, 1, 2, 3,] },]
"""
actual = dom_toml.dumps(example)
assert actual == expected
assert tomli.loads(actual) == example
example = {'a': {"nested": example}}
expected = """\
[a.nested]
aot = [ { a = [ 0, 1, 2, 3,] },]
"""
actual = dom_toml.dumps(example)
assert actual == expected
def test_array_of_long_tables():
long_dict = {
"long-value": "Lorem ipsum sith",
"another-long-value": "consectetur adipis",
"simple-value": 3,
}
example = {"table": {"nested-array": [{'a': 42}, long_dict]}}
expected = """\
[[table.nested-array]]
a = 42
[[table.nested-array]]
long-value = "Lorem ipsum sith"
another-long-value = "consectetur adipis"
simple-value = 3
"""
actual = dom_toml.dumps(example)
assert actual == expected
assert tomli.loads(actual) == example
def test_array_of_short_tables():
long_name = 'a' * 87
example = {"table": {"nested-array": [{long_name: 0}, {'b': 1}, {'c': 2}]}}
expected = f"""\
[table]
nested-array = [
{{ {long_name} = 0 }},
{{ b = 1 }},
{{ c = 2 }},
]
"""
actual = dom_toml.dumps(example)
assert actual == expected
def test_example_issue_12():
example = {"table": {"nested_table": [
{"array_options": [1, 2, 3]},
{"another_array": [1, 2]},
{'c': 3},
]}}
expected = """\
[table]
nested_table = [ { array_options = [ 1, 2, 3,] }, { another_array = [ 1, 2,] }, { c = 3 },]
"""
actual = dom_toml.dumps(example)
assert actual == expected
assert tomli.loads(actual) == example
def test_table_with_empty_array():
# Empty arrays should never be AoTs
example: dict = {"table": {"array": []}}
expected = """\
[table]
array = []
"""
actual = dom_toml.dumps(example)
assert actual == expected
assert tomli.loads(actual) == example
def test_non_trivial_nesting():
long = {
"long-value": "Lorem ipsum dolor sit amet",
"another-long-value": "consectetur adipiscing elit",
"a-third-one": "sed do eiusmod tempor incididunt ut labore et dolore magna",
"simple-value": 3,
}
example = {
"table": {
"aot": [
{"nested-table": {"nested_aot": [{'a': [0, 1]}, {'b': 2}, {'c': 3}]}},
{"other-nested-table": {'d': 4, 'e': 5, 'f': [{'g': 6}], 'h': [long]}},
]
}
}
expected = """\
[[table.aot]]
[table.aot.nested-table]
nested_aot = [ { a = [ 0, 1,] }, { b = 2 }, { c = 3 },]
[[table.aot]]
[table.aot.other-nested-table]
d = 4
e = 5
f = [ { g = 6 },]
[[table.aot.other-nested-table.h]]
long-value = "Lorem ipsum dolor sit amet"
another-long-value = "consectetur adipiscing elit"
a-third-one = "sed do eiusmod tempor incididunt ut labore et dolore magna"
simple-value = 3
"""
actual = dom_toml.dumps(example)
assert actual == expected
assert tomli.loads(actual) == example
def test_multiline_in_aot():
data = {"aot": [{"multiline_string": "line1\nline2"}]}
assert (
dom_toml.dumps(data, encoder=TomlEncoder(multiline_strings=True)) == '''\
[[aot]]
multiline_string = """
line1
line2"""
'''
)
assert (
dom_toml.dumps(data, encoder=TomlEncoder(multiline_strings=False)) == """\
aot = [ { multiline_string = "line1\\nline2" },]
"""
)
|