/
test_toml_compliance.py
57 lines (45 loc) · 1.49 KB
/
test_toml_compliance.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
import json
from pathlib import Path
import pytest
import tomli
from . import burntsushi
class MissingFile:
def __init__(self, path: Path):
self.path = path
DATA_DIR = Path(__file__).parent / "data" / "toml-lang-compliance"
VALID_FILES = tuple((DATA_DIR / "valid").glob("**/*.toml"))
# VALID_FILES_EXPECTED = tuple(
# json.loads(p.with_suffix(".json").read_bytes().decode()) for p in VALID_FILES
# )
_expected_files = []
for p in VALID_FILES:
json_path = p.with_suffix(".json")
try:
text = json.loads(json_path.read_bytes().decode())
except FileNotFoundError:
text = MissingFile(json_path)
_expected_files.append(text)
VALID_FILES_EXPECTED = tuple(_expected_files)
INVALID_FILES = tuple((DATA_DIR / "invalid").glob("**/*.toml"))
@pytest.mark.parametrize(
"invalid",
INVALID_FILES,
ids=[p.stem for p in INVALID_FILES],
)
def test_invalid(invalid):
toml_str = invalid.read_bytes().decode()
with pytest.raises(tomli.TOMLDecodeError):
tomli.loads(toml_str)
@pytest.mark.parametrize(
"valid,expected",
zip(VALID_FILES, VALID_FILES_EXPECTED),
ids=[p.stem for p in VALID_FILES],
)
def test_valid(valid, expected):
if isinstance(expected, MissingFile):
pytest.xfail(f"Missing a .json file corresponding the .toml: {expected.path}")
toml_str = valid.read_bytes().decode()
actual = tomli.loads(toml_str)
actual = burntsushi.convert(actual)
expected = burntsushi.normalize(expected)
assert actual == expected