diff --git a/tests/test_data/images/hexmini.png b/tests/test_data/images/hexmini.png new file mode 100644 index 0000000..12d17bf Binary files /dev/null and b/tests/test_data/images/hexmini.png differ diff --git a/tests/test_data/map_tests/embedded_tileset/expected.py b/tests/test_data/map_tests/embedded_tileset/expected.py new file mode 100644 index 0000000..ffc69c7 --- /dev/null +++ b/tests/test_data/map_tests/embedded_tileset/expected.py @@ -0,0 +1,31 @@ +from pathlib import Path + +from pytiled_parser import common_types, map, tileset + +EXPECTED = map.Map( + infinite=False, + layers=[], + map_size=common_types.Size(8, 6), + next_layer_id=2, + next_object_id=1, + orientation="orthogonal", + render_order="right-down", + tiled_version="1.4.1", + tile_size=common_types.Size(32, 32), + version=1.4, + tilesets={ + 1: tileset.TileSet( + columns=8, + image=Path("../../images/tmw_desert_spacing.png"), + image_width=265, + image_height=199, + margin=1, + spacing=1, + name="tileset", + tile_count=48, + tile_height=32, + tile_width=32, + firstgid=1, + ) + }, +) diff --git a/tests/test_data/map_tests/embedded_tileset/map.json b/tests/test_data/map_tests/embedded_tileset/map.json new file mode 100644 index 0000000..7d247c1 --- /dev/null +++ b/tests/test_data/map_tests/embedded_tileset/map.json @@ -0,0 +1,36 @@ +{ "compressionlevel":0, + "editorsettings": + { + "export": + { + "target":"." + } + }, + "height":6, + "infinite":false, + "layers":[], + "nextlayerid":2, + "nextobjectid":1, + "orientation":"orthogonal", + "renderorder":"right-down", + "tiledversion":"1.4.1", + "tileheight":32, + "tilesets":[ + { + "columns":8, + "firstgid":1, + "image":"..\/..\/images\/tmw_desert_spacing.png", + "imageheight":199, + "imagewidth":265, + "margin":1, + "name":"tileset", + "spacing":1, + "tilecount":48, + "tileheight":32, + "tilewidth":32 + }], + "tilewidth":32, + "type":"map", + "version":1.4, + "width":8 +} \ No newline at end of file diff --git a/tests/test_data/map_tests/hexagonal/expected.py b/tests/test_data/map_tests/hexagonal/expected.py new file mode 100644 index 0000000..a3daadc --- /dev/null +++ b/tests/test_data/map_tests/hexagonal/expected.py @@ -0,0 +1,147 @@ +from pathlib import Path + +from pytiled_parser import common_types, layer, map, tileset + +EXPECTED = map.Map( + hex_side_length=6, + stagger_axis="y", + stagger_index="odd", + infinite=False, + layers=[ + layer.TileLayer( + name="Tile Layer 1", + opacity=1, + visible=True, + size=common_types.Size(10, 10), + id=1, + data=[ + 3, + 3, + 3, + 3, + 9, + 9, + 9, + 9, + 17, + 17, + 3, + 3, + 3, + 9, + 9, + 9, + 9, + 17, + 17, + 17, + 3, + 3, + 3, + 9, + 9, + 9, + 9, + 9, + 17, + 17, + 3, + 3, + 1, + 7, + 9, + 9, + 9, + 15, + 17, + 17, + 1, + 1, + 12, + 5, + 7, + 7, + 7, + 15, + 15, + 15, + 12, + 1, + 5, + 5, + 7, + 7, + 7, + 15, + 15, + 15, + 2, + 2, + 5, + 5, + 5, + 5, + 4, + 14, + 14, + 14, + 2, + 2, + 5, + 5, + 5, + 4, + 14, + 14, + 14, + 14, + 2, + 2, + 2, + 5, + 5, + 5, + 4, + 14, + 14, + 14, + 2, + 2, + 2, + 2, + 5, + 5, + 4, + 4, + 14, + 14, + ], + ) + ], + map_size=common_types.Size(10, 10), + next_layer_id=2, + next_object_id=1, + orientation="hexagonal", + render_order="right-down", + tiled_version="1.4.1", + tile_size=common_types.Size(14, 12), + version=1.4, + tilesets={ + 1: tileset.TileSet( + columns=5, + image=Path("../../images/hexmini.png"), + image_width=106, + image_height=72, + margin=0, + spacing=0, + name="tileset", + tile_count=20, + tiled_version="1.4.1", + tile_height=18, + tile_width=18, + version=1.4, + type="tileset", + tile_offset=common_types.OrderedPair(0, 1), + ) + }, +) diff --git a/tests/test_data/map_tests/hexagonal/map.json b/tests/test_data/map_tests/hexagonal/map.json new file mode 100644 index 0000000..847f382 --- /dev/null +++ b/tests/test_data/map_tests/hexagonal/map.json @@ -0,0 +1,35 @@ +{ "compressionlevel":-1, + "height":10, + "hexsidelength":6, + "infinite":false, + "layers":[ + { + "data":[3, 3, 3, 3, 9, 9, 9, 9, 17, 17, 3, 3, 3, 9, 9, 9, 9, 17, 17, 17, 3, 3, 3, 9, 9, 9, 9, 9, 17, 17, 3, 3, 1, 7, 9, 9, 9, 15, 17, 17, 1, 1, 12, 5, 7, 7, 7, 15, 15, 15, 12, 1, 5, 5, 7, 7, 7, 15, 15, 15, 2, 2, 5, 5, 5, 5, 4, 14, 14, 14, 2, 2, 5, 5, 5, 4, 14, 14, 14, 14, 2, 2, 2, 5, 5, 5, 4, 14, 14, 14, 2, 2, 2, 2, 5, 5, 4, 4, 14, 14], + "height":10, + "id":1, + "name":"Tile Layer 1", + "opacity":1, + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }], + "nextlayerid":2, + "nextobjectid":1, + "orientation":"hexagonal", + "renderorder":"right-down", + "staggeraxis":"y", + "staggerindex":"odd", + "tiledversion":"1.4.1", + "tileheight":12, + "tilesets":[ + { + "firstgid":1, + "source":"tileset.json" + }], + "tilewidth":14, + "type":"map", + "version":1.4, + "width":10 +} \ No newline at end of file diff --git a/tests/test_data/map_tests/hexagonal/tileset.json b/tests/test_data/map_tests/hexagonal/tileset.json new file mode 100644 index 0000000..be8e7b4 --- /dev/null +++ b/tests/test_data/map_tests/hexagonal/tileset.json @@ -0,0 +1,19 @@ +{ "columns":5, + "image":"..\/..\/images\/hexmini.png", + "imageheight":72, + "imagewidth":106, + "margin":0, + "name":"tileset", + "spacing":0, + "tilecount":20, + "tiledversion":"1.4.1", + "tileheight":18, + "tileoffset": + { + "x":0, + "y":1 + }, + "tilewidth":18, + "type":"tileset", + "version":1.4 +} \ No newline at end of file diff --git a/tests/test_data/map_tests/no_background_color/expected.py b/tests/test_data/map_tests/no_background_color/expected.py new file mode 100644 index 0000000..6e2de52 --- /dev/null +++ b/tests/test_data/map_tests/no_background_color/expected.py @@ -0,0 +1,33 @@ +from pathlib import Path + +from pytiled_parser import common_types, map, tileset + +EXPECTED = map.Map( + infinite=False, + layers=[], + map_size=common_types.Size(8, 6), + next_layer_id=2, + next_object_id=1, + orientation="orthogonal", + render_order="right-down", + tiled_version="1.4.1", + tile_size=common_types.Size(32, 32), + version=1.4, + tilesets={ + 1: tileset.TileSet( + columns=8, + image=Path("../../images/tmw_desert_spacing.png"), + image_width=265, + image_height=199, + margin=1, + spacing=1, + name="tile_set_image", + tile_count=48, + tiled_version="1.3.1", + tile_height=32, + tile_width=32, + version=1.2, + type="tileset", + ) + }, +) diff --git a/tests/test_data/map_tests/no_background_color/map.json b/tests/test_data/map_tests/no_background_color/map.json new file mode 100644 index 0000000..124e37e --- /dev/null +++ b/tests/test_data/map_tests/no_background_color/map.json @@ -0,0 +1,27 @@ +{ "compressionlevel":0, + "editorsettings": + { + "export": + { + "target":"." + } + }, + "height":6, + "infinite":false, + "layers":[], + "nextlayerid":2, + "nextobjectid":1, + "orientation":"orthogonal", + "renderorder":"right-down", + "tiledversion":"1.4.1", + "tileheight":32, + "tilesets":[ + { + "firstgid":1, + "source":"tileset_image.json" + }], + "tilewidth":32, + "type":"map", + "version":1.4, + "width":8 +} \ No newline at end of file diff --git a/tests/test_data/map_tests/no_background_color/tileset_image.json b/tests/test_data/map_tests/no_background_color/tileset_image.json new file mode 100644 index 0000000..e88a3f1 --- /dev/null +++ b/tests/test_data/map_tests/no_background_color/tileset_image.json @@ -0,0 +1,14 @@ +{ "columns":8, + "image":"..\/..\/images\/tmw_desert_spacing.png", + "imageheight":199, + "imagewidth":265, + "margin":1, + "name":"tile_set_image", + "spacing":1, + "tilecount":48, + "tiledversion":"1.3.1", + "tileheight":32, + "tilewidth":32, + "type":"tileset", + "version":1.2 +} diff --git a/tests/test_data/map_tests/no_layers/expected.py b/tests/test_data/map_tests/no_layers/expected.py index e69de29..b719a60 100644 --- a/tests/test_data/map_tests/no_layers/expected.py +++ b/tests/test_data/map_tests/no_layers/expected.py @@ -0,0 +1,42 @@ +from pathlib import Path + +from pytiled_parser import common_types, map, tileset + +EXPECTED = map.Map( + infinite=False, + layers=[], + map_size=common_types.Size(8, 6), + next_layer_id=2, + next_object_id=1, + orientation="orthogonal", + render_order="right-down", + tiled_version="1.4.1", + tile_size=common_types.Size(32, 32), + version=1.4, + background_color=common_types.Color("#ff0004"), + tilesets={ + 1: tileset.TileSet( + columns=8, + image=Path("../../images/tmw_desert_spacing.png"), + image_width=265, + image_height=199, + margin=1, + spacing=1, + name="tile_set_image", + tile_count=48, + tiled_version="1.3.1", + tile_height=32, + tile_width=32, + version=1.2, + type="tileset", + ) + }, + properties={ + "bool property - true": True, + "color property": common_types.Color("#ff49fcff"), + "file property": Path("../../../../../../var/log/syslog"), + "float property": 1.23456789, + "int property": 13, + "string property": "Hello, World!!", + }, +) diff --git a/tests/test_data/map_tests/no_layers/map.json b/tests/test_data/map_tests/no_layers/map.json index ac423c0..0c35133 100644 --- a/tests/test_data/map_tests/no_layers/map.json +++ b/tests/test_data/map_tests/no_layers/map.json @@ -1,4 +1,5 @@ -{ "compressionlevel":0, +{ "backgroundcolor":"#ff0004", + "compressionlevel":0, "editorsettings": { "export": @@ -13,11 +14,6 @@ "nextobjectid":1, "orientation":"orthogonal", "properties":[ - { - "name":"bool property - false", - "type":"bool", - "value":false - }, { "name":"bool property - true", "type":"bool", diff --git a/tests/test_map.py b/tests/test_map.py new file mode 100644 index 0000000..d841e09 --- /dev/null +++ b/tests/test_map.py @@ -0,0 +1,34 @@ +"""Tests for maps""" +import importlib.util +import os +from pathlib import Path + +import pytest + +from pytiled_parser import map as map_ + +TESTS_DIR = Path(os.path.dirname(os.path.abspath(__file__))) +TEST_DATA = TESTS_DIR / "test_data" +MAP_TESTS = TEST_DATA / "map_tests" + +ALL_MAP_TESTS = [ + MAP_TESTS / "no_layers", + MAP_TESTS / "no_background_color", + MAP_TESTS / "hexagonal", + MAP_TESTS / "embedded_tileset", +] + + +@pytest.mark.parametrize("map_test", ALL_MAP_TESTS) +def test_map_integration(map_test): + # it's a PITA to import like this, don't do it + # https://stackoverflow.com/a/67692/1342874 + spec = importlib.util.spec_from_file_location("expected", map_test / "expected.py") + expected = importlib.util.module_from_spec(spec) + spec.loader.exec_module(expected) + + raw_maps_path = map_test / "map.json" + + casted_map = map_.cast(raw_maps_path) + + assert casted_map == expected.EXPECTED