-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathtest_toml_tests.py
More file actions
134 lines (102 loc) · 3.65 KB
/
test_toml_tests.py
File metadata and controls
134 lines (102 loc) · 3.65 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
import json
import os
import pytest
from tomlkit import load
from tomlkit import parse
from tomlkit._compat import decode
from tomlkit._utils import parse_rfc3339
from tomlkit.exceptions import TOMLKitError
TESTS_ROOT = os.path.join(os.path.dirname(__file__), "toml-test", "tests")
FILES_LIST = os.path.join(TESTS_ROOT, "files-toml-1.1.0")
def to_bool(s):
assert s in ["true", "false"]
return s == "true"
stypes = {
"string": str,
"bool": to_bool,
"integer": int,
"float": float,
"datetime": parse_rfc3339,
"datetime-local": parse_rfc3339,
"date-local": parse_rfc3339,
"time-local": parse_rfc3339,
}
def untag(value):
if isinstance(value, list):
return [untag(i) for i in value]
elif "type" in value and "value" in value and len(value) == 2:
if value["type"] in stypes:
val = decode(value["value"])
return stypes[value["type"]](val)
elif value["type"] == "array":
return [untag(i) for i in value["value"]]
else:
raise Exception(f"Unsupported type {value['type']}")
else:
return {k: untag(v) for k, v in value.items()}
def _load_case_list():
with open(FILES_LIST, encoding="utf-8") as f:
return [line.strip() for line in f if line.strip()]
def _build_cases():
valid_cases = []
valid_ids = []
invalid_decode_cases = []
invalid_decode_ids = []
invalid_encode_cases = []
invalid_encode_ids = []
for relpath in _load_case_list():
full_path = os.path.join(TESTS_ROOT, relpath)
if not relpath.endswith(".toml"):
continue
case_id = relpath.rsplit(".", 1)[0]
if relpath.startswith("invalid/encoding/"):
invalid_encode_cases.append(full_path)
invalid_encode_ids.append(case_id)
elif relpath.startswith("valid/"):
with open(full_path, encoding="utf-8", newline="") as f:
toml_content = f.read()
json_path = full_path.rsplit(".", 1)[0] + ".json"
with open(json_path, encoding="utf-8") as f:
json_content = f.read()
valid_cases.append({"toml": toml_content, "json": json_content})
valid_ids.append(case_id)
elif relpath.startswith("invalid/"):
with open(full_path, encoding="utf-8", newline="") as f:
toml_content = f.read()
invalid_decode_cases.append({"toml": toml_content})
invalid_decode_ids.append(case_id)
return (
valid_cases,
valid_ids,
invalid_decode_cases,
invalid_decode_ids,
invalid_encode_cases,
invalid_encode_ids,
)
(
VALID_CASES,
VALID_IDS,
INVALID_DECODE_CASES,
INVALID_DECODE_IDS,
INVALID_ENCODE_CASES,
INVALID_ENCODE_IDS,
) = _build_cases()
@pytest.mark.parametrize("toml11_valid_case", VALID_CASES, ids=VALID_IDS)
def test_valid_decode(toml11_valid_case):
json_val = untag(json.loads(toml11_valid_case["json"]))
toml_val = parse(toml11_valid_case["toml"])
assert toml_val == json_val
assert toml_val.as_string() == toml11_valid_case["toml"]
@pytest.mark.parametrize(
"toml11_invalid_decode_case", INVALID_DECODE_CASES, ids=INVALID_DECODE_IDS
)
def test_invalid_decode(toml11_invalid_decode_case):
with pytest.raises(TOMLKitError):
parse(toml11_invalid_decode_case["toml"])
@pytest.mark.parametrize(
"toml11_invalid_encode_case", INVALID_ENCODE_CASES, ids=INVALID_ENCODE_IDS
)
def test_invalid_encode(toml11_invalid_encode_case):
with open(toml11_invalid_encode_case, encoding="utf-8") as f:
with pytest.raises((TOMLKitError, UnicodeDecodeError)):
load(f)