forked from samuelcolvin/rtoml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_load.py
228 lines (185 loc) · 6.47 KB
/
test_load.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
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
from datetime import date, datetime, time, timedelta, timezone
import pytest
import rtoml
@pytest.mark.parametrize(
'input_toml,output_obj',
[
('foo = "bar"', {'foo': 'bar'}),
('ports = [ 8001, 8001, 8002 ]', {'ports': [8001, 8001, 8002]}),
('x = 1979-05-27T07:32:00', {'x': datetime(1979, 5, 27, 7, 32)}),
('x = 1979-05-27T07:32:00.123', {'x': datetime(1979, 5, 27, 7, 32, 0, 123000)}),
('x = 1979-05-27T07:32:00.123000', {'x': datetime(1979, 5, 27, 7, 32, 0, 123000)}),
('x = 1979-05-27T07:32:00.000123', {'x': datetime(1979, 5, 27, 7, 32, 0, 123)}),
('x = 1979-05-27T07:32:00Z', {'x': datetime(1979, 5, 27, 7, 32, tzinfo=timezone.utc)}),
('x = 1979-05-27T07:32:00-08:00', {'x': datetime(1979, 5, 27, 7, 32, tzinfo=timezone(timedelta(hours=-8)))}),
('x = 1979-05-27T07:32:00+08:00', {'x': datetime(1979, 5, 27, 7, 32, tzinfo=timezone(timedelta(hours=8)))}),
(
'x = 1979-05-27T07:32:00+08:15',
{'x': datetime(1979, 5, 27, 7, 32, tzinfo=timezone(timedelta(hours=8, minutes=15)))},
),
(
'x = 1979-05-27T00:32:00.123-07:00',
{'x': datetime(1979, 5, 27, 0, 32, 0, 123000, tzinfo=timezone(timedelta(hours=-7)))},
),
('x = 1979-05-27 07:32:00', {'x': datetime(1979, 5, 27, 7, 32)}),
('x = 1979-05-27T00:32:00.999999', {'x': datetime(1979, 5, 27, 0, 32, 0, 999999)}),
('x = 1987-01-28', {'x': date(1987, 1, 28)}),
('x = 12:34:56', {'x': time(12, 34, 56)}),
('foo.bar = "thing"', {'foo': {'bar': 'thing'}}),
(
'''
foo.bar = """
thing"""
''',
{'foo': {'bar': 'thing'}},
),
(
'''
foo.bar = """
thing
"""
''',
{'foo': {'bar': 'thing\n'}},
),
(
'''
foo.bar = """
thing
more"""
''',
{'foo': {'bar': 'thing\n\nmore'}},
),
(
"""
# This is a TOML document. (from https://github.com/toml-lang/toml)
title = "TOML Example"
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00 # First class dates
[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true
[servers]
# Indentation (tabs and/or spaces) is allowed but not required
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"
[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"
[clients]
data = [ ["gamma", "delta"], [1, 2] ]
# Line breaks are OK when inside arrays
hosts = [
"alpha",
"omega"
]
""",
{
'clients': {'data': [['gamma', 'delta'], [1, 2]], 'hosts': ['alpha', 'omega']},
'database': {
'connection_max': 5000,
'enabled': True,
'ports': [8001, 8001, 8002],
'server': '192.168.1.1',
},
'owner': {
'dob': datetime(1979, 5, 27, 7, 32, tzinfo=timezone(timedelta(hours=-8))),
'name': 'Tom Preston-Werner',
},
'servers': {'alpha': {'dc': 'eqdc10', 'ip': '10.0.0.1'}, 'beta': {'dc': 'eqdc10', 'ip': '10.0.0.2'}},
'title': 'TOML Example',
},
),
(
"""
[[fruit]]
name = "apple"
[fruit.physical] # subtable
color = "red"
shape = "round"
[[fruit.variety]] # nested array of tables
name = "red delicious"
[[fruit.variety]]
name = "granny smith"
[[fruit]]
name = "banana"
[[fruit.variety]]
name = "plantain"
""",
{
'fruit': [
{
'name': 'apple',
'physical': {'color': 'red', 'shape': 'round'},
'variety': [{'name': 'red delicious'}, {'name': 'granny smith'}],
},
{'name': 'banana', 'variety': [{'name': 'plantain'}]},
]
},
),
("""[ j . "ʞ" . 'l' ]""", {'j': {'ʞ': {'l': {}}}}),
],
)
def test_load(input_toml, output_obj):
assert rtoml.load(input_toml) == output_obj
def test_load_str():
assert rtoml.load('foo = "bar"') == {'foo': 'bar'}
@pytest.mark.parametrize(
'input_toml,output_obj',
[
('foo = "bar"', {'foo': 'bar'}),
('emoji = "😷"', {'emoji': '😷'}),
('polish = "Witaj świecie"', {'polish': 'Witaj świecie'}),
],
)
def test_load_path(tmp_path, input_toml, output_obj):
p = tmp_path / 'test.toml'
p.write_text(input_toml, encoding='UTF-8')
assert rtoml.load(p) == output_obj
def test_load_file(tmp_path):
p = tmp_path / 'test.toml'
p.write_text('foo = "bar"')
with p.open() as f:
assert rtoml.load(f) == {'foo': 'bar'}
def test_invalid_type():
with pytest.raises(TypeError, match="invalid toml input, must be str not <class 'bytes'>"):
rtoml.load(b'foobar')
def test_datetime_tz_neg():
d: datetime = rtoml.load('date = 1979-05-27T07:32:00.999999-08:00')['date']
assert d == datetime(1979, 5, 27, 7, 32, 0, 999999, tzinfo=timezone(timedelta(seconds=-28800)))
assert d.tzinfo.tzname(datetime.now()) == 'UTC-08:00'
def test_datetime_tz_pos():
d: datetime = rtoml.load('date = 1979-05-27T07:32:00+05:15')['date']
assert d == datetime(1979, 5, 27, 7, 32, tzinfo=timezone(timedelta(seconds=18900)))
assert d.tzinfo.tzname(datetime.now()) == 'UTC+05:15'
def test_datetime_tz_utc():
d: datetime = rtoml.load('date = 1979-05-27T07:32:00Z')['date']
assert d == datetime(1979, 5, 27, 7, 32, tzinfo=timezone.utc)
assert d.tzinfo.tzname(datetime.now()) == 'UTC'
def test_datetime_invalid():
with pytest.raises(rtoml.TomlParsingError, match='day is out of range for month'):
rtoml.load('date = 1979-02-30T07:32:00')
def test_invalid_toml():
m = r'^invalid TOML value, did you mean to use a quoted string\? at line 1 column 5$'
with pytest.raises(rtoml.TomlParsingError, match=m):
rtoml.load('x = y')
def test_mixed_array():
assert rtoml.loads('x = [1.1, 2, 3.3]') == {'x': [1.1, 2, 3.3]}
assert rtoml.loads('x = [1, ["Arrays are not integers."]]') == {'x': [1, ['Arrays are not integers.']]}
assert rtoml.loads('x = ["hi", 42]') == {'x': ['hi', 42]}
# https://github.com/alexcrichton/toml-rs/issues/367
@pytest.mark.xfail
def test_subtable():
s = """\
[fruit]
apple.color = "red"
apple.taste.sweet = true
[fruit.apple.texture] # you can add sub-tables
smooth = true
"""
assert rtoml.loads(s) == ...
def test_empty_toml():
assert rtoml.loads('') == {}