Invalid map format handling

This commit is contained in:
Darren Eberly
2022-06-01 00:39:59 -04:00
parent 9e8e3b980a
commit 96b104c931
3 changed files with 21 additions and 5 deletions

View File

@@ -21,9 +21,11 @@ def parse_map(file: Path) -> TiledMap:
# The type ignores are because mypy for some reason thinks those functions return Any
if parser == "tmx":
return tmx_map_parse(file) # type: ignore
elif parser == "json":
return json_map_parse(file) # type: ignore
else:
raise UnknownFormat(
"Unknown Map Format, please use either the TMX or JSON format."
)
try:
return json_map_parse(file) # type: ignore
except ValueError:
raise UnknownFormat(
"Unknown Map Format, please use either the TMX or JSON format. "
"This message could also mean your map file is invalid or corrupted."
)

View File

@@ -0,0 +1 @@
|thisissomegarbageformat||||ohmylookhowbaditis||thiswillcertainlybreakthigns|||

View File

@@ -0,0 +1,13 @@
import os
from pathlib import Path
import pytest
from pytiled_parser import UnknownFormat, parse_map
TESTS_DIR = Path(os.path.dirname(os.path.abspath(__file__)))
MAP_FILE = TESTS_DIR / "test_data/invalid_format.garbage"
def test_map_invalid_format():
with pytest.raises(UnknownFormat) as e:
parse_map(MAP_FILE)