Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pytiled_parser/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def parse_color(color: str) -> Color:

def check_format(file_path: Path, encoding: str) -> str:
with open(file_path, encoding=encoding) as file:
line = file.readline().rstrip().strip()
if line[0] == "<":
return "tmx"
else:
return "json"
for line in file:
line = line.strip()
if line:
return "tmx" if line.startswith("<") else "json"
return "json"


def load_object_template(file_path: Path, encoding: str) -> Any:
Expand Down
48 changes: 48 additions & 0 deletions tests/test_format_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import json

import pytest

from pytiled_parser import UnknownFormat, parse_map
from pytiled_parser.common_types import Size

MAP = {
"version": "1.9",
"tiledversion": "1.9.1",
"orientation": "orthogonal",
"renderorder": "right-down",
"width": 1,
"height": 1,
"tilewidth": 16,
"tileheight": 16,
"infinite": False,
"nextlayerid": 1,
"nextobjectid": 1,
"layers": [],
"tilesets": [],
}

TMX = (
'<map version="1.9" tiledversion="1.9.1" orientation="orthogonal" '
'renderorder="right-down" width="1" height="1" tilewidth="16" '
'tileheight="16" infinite="0" nextlayerid="1" nextobjectid="1"/>'
)


@pytest.mark.parametrize("prefix", ["", "\n", "\r\n\r\n", " \t\n\n"])
@pytest.mark.parametrize("format_", ["json", "tmx"])
def test_leading_blank_lines(tmp_path, prefix, format_):
map_file = tmp_path / ("map." + format_)
content = json.dumps(MAP) if format_ == "json" else TMX
map_file.write_text(prefix + content, encoding="utf-8")
parsed = parse_map(map_file)
assert parsed.map_size == Size(1, 1)
assert parsed.tile_size == Size(16, 16)
assert parsed.layers == []


@pytest.mark.parametrize("content", ["", "\n", " \t\n\r\n"])
def test_empty_map_has_format_error(tmp_path, content):
map_file = tmp_path / "map.json"
map_file.write_text(content, encoding="utf-8")
with pytest.raises(UnknownFormat):
parse_map(map_file)