| 1 |
# SPDX-License-Identifier: MIT |
| 2 |
# SPDX-FileCopyrightText: 2021 Taneli Hukkinen |
| 3 |
# Licensed to PSF under a Contributor Agreement. |
| 4 |
|
| 5 |
import json |
| 6 |
from pathlib import Path |
| 7 |
import unittest |
| 8 |
|
| 9 |
from . import burntsushi, tomllib |
| 10 |
|
| 11 |
|
| 12 |
class MissingFile: |
| 13 |
def __init__(self, path: Path): |
| 14 |
self.path = path |
| 15 |
|
| 16 |
|
| 17 |
DATA_DIR = Path(__file__).parent / "data" |
| 18 |
|
| 19 |
VALID_FILES = tuple((DATA_DIR / "valid").glob("**/*.toml")) |
| 20 |
assert VALID_FILES, "Valid TOML test files not found" |
| 21 |
|
| 22 |
_expected_files = [] |
| 23 |
for p in VALID_FILES: |
| 24 |
json_path = p.with_suffix(".json") |
| 25 |
try: |
| 26 |
text = json.loads(json_path.read_bytes().decode()) |
| 27 |
except FileNotFoundError: |
| 28 |
text = MissingFile(json_path) |
| 29 |
_expected_files.append(text) |
| 30 |
VALID_FILES_EXPECTED = tuple(_expected_files) |
| 31 |
|
| 32 |
INVALID_FILES = tuple((DATA_DIR / "invalid").glob("**/*.toml")) |
| 33 |
assert INVALID_FILES, "Invalid TOML test files not found" |
| 34 |
|
| 35 |
|
| 36 |
class TestData(unittest.TestCase): |
| 37 |
def test_invalid(self): |
| 38 |
for invalid in INVALID_FILES: |
| 39 |
with self.subTest(msg=invalid.stem): |
| 40 |
toml_bytes = invalid.read_bytes() |
| 41 |
try: |
| 42 |
toml_str = toml_bytes.decode() |
| 43 |
except UnicodeDecodeError: |
| 44 |
# Some BurntSushi tests are not valid UTF-8. Skip those. |
| 45 |
continue |
| 46 |
with self.assertRaises(tomllib.TOMLDecodeError): |
| 47 |
tomllib.loads(toml_str) |
| 48 |
|
| 49 |
def test_valid(self): |
| 50 |
for valid, expected in zip(VALID_FILES, VALID_FILES_EXPECTED): |
| 51 |
with self.subTest(msg=valid.stem): |
| 52 |
if isinstance(expected, MissingFile): |
| 53 |
# For a poor man's xfail, assert that this is one of the |
| 54 |
# test cases where expected data is known to be missing. |
| 55 |
assert valid.stem in { |
| 56 |
"qa-array-inline-nested-1000", |
| 57 |
"qa-table-inline-nested-1000", |
| 58 |
} |
| 59 |
continue |
| 60 |
toml_str = valid.read_bytes().decode() |
| 61 |
actual = tomllib.loads(toml_str) |
| 62 |
actual = burntsushi.convert(actual) |
| 63 |
expected = burntsushi.normalize(expected) |
| 64 |
self.assertEqual(actual, expected) |