mirror of
https://github.com/OMGeeky/pytiled_parser.git
synced 2025-12-26 17:02:28 +01:00
Invalid map format handling
This commit is contained in:
@@ -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."
|
||||
)
|
||||
|
||||
1
tests/test_data/invalid_format.garbage
Normal file
1
tests/test_data/invalid_format.garbage
Normal file
@@ -0,0 +1 @@
|
||||
|thisissomegarbageformat||||ohmylookhowbaditis||thiswillcertainlybreakthigns|||
|
||||
13
tests/test_invalid_format.py
Normal file
13
tests/test_invalid_format.py
Normal 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)
|
||||
Reference in New Issue
Block a user