From a44c3523d4f2aa6c09a5c669cef13494d6c75e1b Mon Sep 17 00:00:00 2001 From: Benjamin Kirkbride Date: Wed, 8 May 2019 21:05:57 -0400 Subject: [PATCH] added a lot of tests yay --- pytiled_parser/objects.py | 6 +- pytiled_parser/xml_parser.py | 24 ++-- tests/test_data/test_map_simple_meme.tmx | 18 +++ tests/unit2/test_parser.py | 166 ++++++++++++++++++++++- 4 files changed, 191 insertions(+), 23 deletions(-) create mode 100644 tests/test_data/test_map_simple_meme.tmx diff --git a/pytiled_parser/objects.py b/pytiled_parser/objects.py index 49a15e6..a805a7b 100644 --- a/pytiled_parser/objects.py +++ b/pytiled_parser/objects.py @@ -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 diff --git a/pytiled_parser/xml_parser.py b/pytiled_parser/xml_parser.py index 4f7c19d..8ca4649 100644 --- a/pytiled_parser/xml_parser.py +++ b/pytiled_parser/xml_parser.py @@ -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: diff --git a/tests/test_data/test_map_simple_meme.tmx b/tests/test_data/test_map_simple_meme.tmx new file mode 100644 index 0000000..bc47bdc --- /dev/null +++ b/tests/test_data/test_map_simple_meme.tmx @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + H4sIAAAAAAAAAw3DBRKCQAAAwDMRA7BQLMTE9v+vY3dmWyGEth279uwbOTB26MixExNTM6fOnLtwae7KtYUbt+7ce7D0aOXJsxev3rxb+/Dpy7cfv/782wAcvDirwAAAAA== + + + diff --git a/tests/unit2/test_parser.py b/tests/unit2/test_parser.py index a03f8bd..3d58ab1 100644 --- a/tests/unit2/test_parser.py +++ b/tests/unit2/test_parser.py @@ -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 = [ + ( + '' + "", + (int(1), "Tile Layer 1", None, None, None), + ), + ( + '' + "", + (int(2), "Tile Layer 2", None, float(0.5), None), + ), + ( + '' + "" + "" + "", + ( + 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 + )