forked from samuelcolvin/rtoml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dump.py
109 lines (93 loc) · 3.63 KB
/
test_dump.py
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
from datetime import datetime, timezone
import pytest
import rtoml
@pytest.mark.parametrize(
'input_obj,output_toml',
[
({'text': '\nfoo\nbar\n'}, 'text = "\\nfoo\\nbar\\n"\n'),
({'foo': 'bar'}, 'foo = "bar"\n'),
([1, 2, 3], '[1, 2, 3]'),
(datetime(1979, 5, 27, 7, 32), '1979-05-27T07:32:00'),
(datetime(1979, 5, 27, 7, 32, tzinfo=timezone.utc), '1979-05-27T07:32:00Z'),
({'x': datetime(1979, 5, 27, 7, 32)}, 'x = 1979-05-27T07:32:00\n'),
# order changed to avoid https://github.com/alexcrichton/toml-rs/issues/142
({'x': {'a': 1}, 'y': 4}, 'y = 4\n\n[x]\na = 1\n'),
((1, 2, 3), '[1, 2, 3]'),
({'emoji': '😷'}, 'emoji = "😷"\n'),
({'polish': 'Witaj świecie'}, 'polish = "Witaj świecie"\n'),
],
)
def test_dumps(input_obj, output_toml):
assert rtoml.dumps(input_obj) == output_toml
@pytest.mark.parametrize(
'input_obj,output_toml',
[
({'text': '\nfoo\nbar\n'}, "text = '''\n\nfoo\nbar\n'''\n"),
({'foo': 'bar'}, "foo = 'bar'\n"),
([1, 2, 3], '[\n 1,\n 2,\n 3,\n]'),
((1, 2, 3), '[\n 1,\n 2,\n 3,\n]'),
],
)
def test_dumps_pretty(input_obj, output_toml):
assert rtoml.dumps(input_obj, pretty=True) == output_toml
@pytest.mark.parametrize(
'input_obj,output_toml,size',
[
({'foo': 'bar'}, 'foo = "bar"\n', 12),
({'emoji': '😷'}, 'emoji = "😷"\n', 12),
({'polish': 'Witaj świecie'}, 'polish = "Witaj świecie"\n', 25),
],
)
def test_dump_path(tmp_path, input_obj, output_toml, size):
p = tmp_path / 'test.toml'
assert rtoml.dump(input_obj, p) == size
assert p.read_text(encoding='UTF-8') == output_toml
def test_dump_file(tmp_path):
p = tmp_path / 'test.toml'
with p.open('w') as f:
assert rtoml.dump({'foo': 'bar'}, f) == 12
assert p.read_text() == 'foo = "bar"\n'
def test_varied_list():
assert rtoml.dumps({'test': [1, '2']}) == 'test = [1, "2"]\n'
@pytest.mark.parametrize(
'input_obj,output_toml',
[
({None: 1}, ''),
({'key': None}, ''),
({'foo': 'bar', 'key1': None}, 'foo = "bar"\n'),
({'key1': None, 'foo': 'bar'}, 'foo = "bar"\n'),
({'key1': None, 'foo': 'bar', 'key2': None}, 'foo = "bar"\n'),
],
)
def test_none_map_value(input_obj, output_toml):
assert rtoml.dumps(input_obj, include_none=False) == output_toml
@pytest.mark.parametrize(
'input_obj,output_toml',
[
([None], '[]'),
(["a", None], '["a"]'),
([None, "b"], '["b"]'),
(["a", None, "b"], '["a", "b"]'),
({'foo': 'bar', 'list': [None]}, 'foo = "bar"\nlist = []\n'),
({'foo': 'bar', 'list': [None, "b"]}, 'foo = "bar"\nlist = ["b"]\n'),
({'foo': 'bar', 'list': ["a", None]}, 'foo = "bar"\nlist = ["a"]\n'),
({'foo': 'bar', 'list': ["a", None, "b"]}, 'foo = "bar"\nlist = ["a", "b"]\n'),
],
)
def test_none_values_inside_list(input_obj, output_toml):
assert rtoml.dumps(input_obj, include_none=False) == output_toml
@pytest.mark.parametrize(
'input_obj,output_toml',
[
((None), '"null"'),
(("a", None), '["a"]'),
((None, "b"), '["b"]'),
(("a", None, "b"), '["a", "b"]'),
({'foo': 'bar', 'list': (None)}, 'foo = "bar"\n'),
({'foo': 'bar', 'list': (None, "b")}, 'foo = "bar"\nlist = ["b"]\n'),
({'foo': 'bar', 'list': ("a", None)}, 'foo = "bar"\nlist = ["a"]\n'),
({'foo': 'bar', 'list': ("a", None, "b")}, 'foo = "bar"\nlist = ["a", "b"]\n'),
],
)
def test_none_values_inside_tuple(input_obj, output_toml):
assert rtoml.dumps(input_obj, include_none=False) == output_toml