mirror of
https://github.com/OMGeeky/pytiled_parser.git
synced 2025-12-28 15:22:28 +01:00
added a lot of tests yay
This commit is contained in:
@@ -362,7 +362,7 @@ class _TextObjectDefaults(_TiledObjectDefaults):
|
||||
font_family: str = "sans-serif"
|
||||
font_size: int = 16
|
||||
wrap: bool = False
|
||||
color: Color = Color(0xFF, 0, 0, 0)
|
||||
color: str = "#000000"
|
||||
bold: bool = False
|
||||
italic: bool = False
|
||||
underline: bool = False
|
||||
@@ -422,7 +422,7 @@ class ObjectLayer(Layer):
|
||||
|
||||
tiled_objects: List[TiledObject]
|
||||
|
||||
color: Optional[Color] = None
|
||||
color: Optional[str] = None
|
||||
draw_order: Optional[str] = "topdown"
|
||||
|
||||
|
||||
@@ -578,7 +578,7 @@ class TileMap:
|
||||
hex_side_length: Optional[int] = None
|
||||
stagger_axis: Optional[int] = None
|
||||
stagger_index: Optional[int] = None
|
||||
background_color: Optional[Color] = None
|
||||
background_color: Optional[str] = None
|
||||
|
||||
properties: Optional[Properties] = None
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ import pytiled_parser.utilities as utilities
|
||||
|
||||
|
||||
def _decode_base64_data(
|
||||
data_text: str, compression: Optional[str], layer_width: int
|
||||
data_text: str, layer_width: int, compression: Optional[str] = None
|
||||
) -> List[List[int]]:
|
||||
tile_grid: List[List[int]] = [[]]
|
||||
|
||||
@@ -49,7 +49,7 @@ def _decode_base64_data(
|
||||
return tile_grid
|
||||
|
||||
|
||||
def _decode_csv_layer(data_text: str) -> List[List[int]]:
|
||||
def _decode_csv_data(data_text: str) -> List[List[int]]:
|
||||
"""Decodes csv encoded layer data.
|
||||
|
||||
Credit:
|
||||
@@ -57,10 +57,10 @@ def _decode_csv_layer(data_text: str) -> List[List[int]]:
|
||||
tile_grid = []
|
||||
lines: List[str] = data_text.split("\n")
|
||||
# remove erronious empty lists due to a newline being on both ends of text
|
||||
lines = lines[1:]
|
||||
lines = lines[:-1]
|
||||
lines = lines[1:-1]
|
||||
for line in lines:
|
||||
line_list = line.split(",")
|
||||
# FIXME: what is this for?
|
||||
while "" in line_list:
|
||||
line_list.remove("")
|
||||
line_list_int = [int(item) for item in line_list]
|
||||
@@ -103,7 +103,7 @@ def _decode_data(
|
||||
raise AttributeError(f"{element} lacks layer data.")
|
||||
|
||||
if encoding == "csv":
|
||||
return _decode_csv_layer(data_text)
|
||||
return _decode_csv_data(data_text)
|
||||
|
||||
return _decode_base64_data(data_text, compression, layer_width)
|
||||
|
||||
@@ -162,7 +162,7 @@ def _parse_layer(
|
||||
layer_element: The layer element to be parsed.
|
||||
|
||||
Returns:
|
||||
|
||||
FIXME
|
||||
"""
|
||||
id = int(layer_element.attrib["id"])
|
||||
|
||||
@@ -313,7 +313,7 @@ def _parse_object_layer(element: etree.Element,) -> objects.ObjectLayer:
|
||||
tiled_objects = _parse_objects(element.findall("./object"))
|
||||
|
||||
try:
|
||||
color = utilities.parse_color(element.attrib["color"])
|
||||
color = element.attrib["color"]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
@@ -495,7 +495,7 @@ def _parse_image_element(image_element: etree.Element) -> objects.Image:
|
||||
"""Parse image element given.
|
||||
|
||||
Returns:
|
||||
:Color: Color in Arcade's preffered format.
|
||||
: Color in Arcade's preffered format.
|
||||
"""
|
||||
image = objects.Image(image_element.attrib["source"])
|
||||
|
||||
@@ -506,7 +506,7 @@ def _parse_image_element(image_element: etree.Element) -> objects.Image:
|
||||
image.size = objects.Size(int(width_attrib), int(height_attrib))
|
||||
|
||||
try:
|
||||
image.trans = utilities.parse_color(image_element.attrib["trans"])
|
||||
image.trans = image_element.attrib["trans"]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
@@ -547,7 +547,7 @@ def _parse_properties_element(
|
||||
elif property_type == "float":
|
||||
properties[name] = float(value)
|
||||
elif property_type == "color":
|
||||
properties[name] = utilities.parse_color(value)
|
||||
properties[name] = value
|
||||
elif property_type == "file":
|
||||
properties[name] = Path(value)
|
||||
elif property_type == "bool":
|
||||
@@ -731,11 +731,9 @@ def parse_tile_map(tmx_file: Union[str, Path]) -> objects.TileMap:
|
||||
pass
|
||||
|
||||
try:
|
||||
backgroundcolor = map_element.attrib["backgroundcolor"]
|
||||
tile_map.background_color = map_element.attrib["backgroundcolor"]
|
||||
except KeyError:
|
||||
pass
|
||||
else:
|
||||
tile_map.background_color = utilities.parse_color(backgroundcolor)
|
||||
|
||||
properties_element = map_tree.find("./properties")
|
||||
if properties_element is not None:
|
||||
|
||||
18
tests/test_data/test_map_simple_meme.tmx
Normal file
18
tests/test_data/test_map_simple_meme.tmx
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<map version="1.2" tiledversion="1.2.3" orientation="orthogonal" renderorder="right-down" width="8" height="6" tilewidth="32" tileheight="32" infinite="0" nextlayerid="2" nextobjectid="1">
|
||||
<properties>
|
||||
<property name="bool property - false" type="bool" value="false"/>
|
||||
<property name="bool property - true" type="bool" value="true"/>
|
||||
<property name="color property" type="color" value="#ff49fcff"/>
|
||||
<property name="file property" type="file" value="../../../../../../../../var/log/syslog"/>
|
||||
<property name="float property" type="float" value="1.23456789"/>
|
||||
<property name="int property" type="int" value="13"/>
|
||||
<property name="string property" value="Hello, World!!"/>
|
||||
</properties>
|
||||
<tileset firstgid="1" source="tile_set_image.tsx"/>
|
||||
<layer id="1" name="Tile Layer 1" width="8" height="6">
|
||||
<data encoding="base64" compression="gzip">
|
||||
H4sIAAAAAAAAAw3DBRKCQAAAwDMRA7BQLMTE9v+vY3dmWyGEth279uwbOTB26MixExNTM6fOnLtwae7KtYUbt+7ce7D0aOXJsxev3rxb+/Dpy7cfv/782wAcvDirwAAAAA==
|
||||
</data>
|
||||
</layer>
|
||||
</map>
|
||||
@@ -2,17 +2,169 @@ import pytest
|
||||
|
||||
import xml.etree.ElementTree as etree
|
||||
|
||||
from typing import Callable
|
||||
from contextlib import contextmanager
|
||||
from typing import Callable, List, Optional, Tuple
|
||||
|
||||
from pytiled_parser import xml_parser
|
||||
from pytiled_parser import objects, xml_parser, utilities
|
||||
|
||||
|
||||
def _get_root_element(xml: str) -> Callable:
|
||||
pass
|
||||
@contextmanager
|
||||
def does_not_raise():
|
||||
yield
|
||||
|
||||
|
||||
layer_data = []
|
||||
def _get_root_element(xml: str) -> etree.Element:
|
||||
return etree.fromstring(xml)
|
||||
|
||||
|
||||
def test_parse_layer(element, expected):
|
||||
pass
|
||||
layer_data = [
|
||||
(
|
||||
'<layer id="1" name="Tile Layer 1" width="10" height="10">'
|
||||
"</layer>",
|
||||
(int(1), "Tile Layer 1", None, None, None),
|
||||
),
|
||||
(
|
||||
'<layer id="2" name="Tile Layer 2" width="10" height="10" opacity="0.5">'
|
||||
"</layer>",
|
||||
(int(2), "Tile Layer 2", None, float(0.5), None),
|
||||
),
|
||||
(
|
||||
'<layer id="5" name="Tile Layer 4" width="10" height="10" offsetx="49" offsety="-50">'
|
||||
"<properties>"
|
||||
"</properties>"
|
||||
"</layer>",
|
||||
(
|
||||
int(5),
|
||||
"Tile Layer 4",
|
||||
objects.OrderedPair(49, -50),
|
||||
None,
|
||||
"properties",
|
||||
),
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("xml,expected", layer_data)
|
||||
def test_parse_layer(xml, expected, monkeypatch):
|
||||
def mockreturn(properties):
|
||||
return "properties"
|
||||
|
||||
monkeypatch.setattr(xml_parser, "_parse_properties_element", mockreturn)
|
||||
|
||||
assert xml_parser._parse_layer(_get_root_element(xml)) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"test_input,expected",
|
||||
[
|
||||
("#001122", (0x00, 0x11, 0x22, 0xFF)),
|
||||
("001122", (0x00, 0x11, 0x22, 0xFF)),
|
||||
("#FF001122", (0x00, 0x11, 0x22, 0xFF)),
|
||||
("FF001122", (0x00, 0x11, 0x22, 0xFF)),
|
||||
("FF001122", (0x00, 0x11, 0x22, 0xFF)),
|
||||
],
|
||||
)
|
||||
def test_color_parsing(test_input, expected):
|
||||
"""
|
||||
Tiled has a few different types of color representations.
|
||||
"""
|
||||
assert utilities.parse_color(test_input) == expected
|
||||
|
||||
|
||||
data_csv = [
|
||||
(
|
||||
"\n1,2,3,4,5,6,7,8,\n"
|
||||
"9,10,11,12,13,14,15,16,\n"
|
||||
"17,18,19,20,21,22,23,24,\n"
|
||||
"25,26,27,28,29,30,31,32,\n"
|
||||
"33,34,35,36,37,38,39,40,\n"
|
||||
"41,42,43,44,45,46,47,48\n",
|
||||
[
|
||||
[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],
|
||||
],
|
||||
),
|
||||
("\n0,0,0,0,0\n", [[0, 0, 0, 0, 0]]),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("data_csv,expected", data_csv)
|
||||
def test_decode_csv_data(data_csv, expected):
|
||||
assert xml_parser._decode_csv_data(data_csv) == expected
|
||||
|
||||
|
||||
data_base64 = [
|
||||
(
|
||||
"AQAAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAACgAAAAsAAAAMAAAADQAAAA4AAAAPAAAAEAAAABEAAAASAAAAEwAAABQAAAAVAAAAFgAAABcAAAAYAAAAGQAAABoAAAAbAAAAHAAAAB0AAAAeAAAAHwAAACAAAAAhAAAAIgAAACMAAAAkAAAAJQAAACYAAAAnAAAAKAAAACkAAAAqAAAAKwAAACwAAAAtAAAALgAAAC8AAAAwAAAA",
|
||||
8,
|
||||
None,
|
||||
[
|
||||
[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],
|
||||
],
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
"eJwNwwUSgkAAAMAzEQOwUCzExPb/r2N3ZlshhLYdu/bsGzkwdujIsRMTUzOnzpy7cGnuyrWFG7fu3Huw9GjlybMXr968W/vw6cu3H7/+/NsAMw8EmQ==",
|
||||
8,
|
||||
"zlib",
|
||||
[
|
||||
[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],
|
||||
],
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
"H4sIAAAAAAAAAw3DBRKCQAAAwDMRA7BQLMTE9v+vY3dmWyGEth279uwbOTB26MixExNTM6fOnLtwae7KtYUbt+7ce7D0aOXJsxev3rxb+/Dpy7cfv/782wAcvDirwAAAAA==",
|
||||
8,
|
||||
"gzip",
|
||||
[
|
||||
[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],
|
||||
],
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
"SGVsbG8gV29ybGQh",
|
||||
8,
|
||||
"lzma",
|
||||
[
|
||||
[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],
|
||||
],
|
||||
pytest.raises(ValueError),
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"data_base64,width,compression,expected,raises", data_base64
|
||||
)
|
||||
def test_decode_base64_data(
|
||||
data_base64, width, compression, expected, raises
|
||||
):
|
||||
with raises:
|
||||
assert (
|
||||
xml_parser._decode_base64_data(data_base64, width, compression)
|
||||
== expected
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user