|
| 1 | +"""Image dimensions are optional in TSX, just as in JSON tilesets.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import xml.etree.ElementTree as etree |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from pytiled_parser import parse_tileset |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize("format_", ["json", "tsx"]) |
| 13 | +@pytest.mark.parametrize("collection", [False, True]) |
| 14 | +@pytest.mark.parametrize( |
| 15 | + "dimensions", [{}, {"width": 32}, {"height": 16}, {"width": 32, "height": 16}] |
| 16 | +) |
| 17 | +def test_optional_image_dimensions(tmp_path, format_, collection, dimensions): |
| 18 | + attributes = { |
| 19 | + "name": "test", |
| 20 | + "spacing": 0, |
| 21 | + "margin": 0, |
| 22 | + "tilecount": 1, |
| 23 | + "tilewidth": 32, |
| 24 | + "tileheight": 16, |
| 25 | + "columns": 0 if collection else 1, |
| 26 | + } |
| 27 | + filename = tmp_path / ("tileset." + format_) |
| 28 | + if format_ == "json": |
| 29 | + image = {"image": "tile.png"} |
| 30 | + image.update({"image" + key: value for key, value in dimensions.items()}) |
| 31 | + if collection: |
| 32 | + attributes["tiles"] = [dict(image, id=0)] |
| 33 | + else: |
| 34 | + attributes.update(image) |
| 35 | + filename.write_text(json.dumps(attributes), encoding="utf-8") |
| 36 | + else: |
| 37 | + root = etree.Element( |
| 38 | + "tileset", {key: str(value) for key, value in attributes.items()} |
| 39 | + ) |
| 40 | + parent = etree.SubElement(root, "tile", id="0") if collection else root |
| 41 | + etree.SubElement( |
| 42 | + parent, |
| 43 | + "image", |
| 44 | + source="tile.png", |
| 45 | + **{key: str(value) for key, value in dimensions.items()}, |
| 46 | + ) |
| 47 | + etree.ElementTree(root).write(filename, encoding="utf-8") |
| 48 | + |
| 49 | + tileset = parse_tileset(filename) |
| 50 | + image = tileset.tiles[0] if collection else tileset |
| 51 | + assert image.image == Path("tile.png") |
| 52 | + assert image.image_width == dimensions.get("width") |
| 53 | + assert image.image_height == dimensions.get("height") |
| 54 | + if collection: |
| 55 | + assert image.width == dimensions.get("width", 0) |
| 56 | + assert image.height == dimensions.get("height", 0) |
| 57 | + assert tileset.tile_width == 32 |
| 58 | + assert tileset.tile_height == 16 |
0 commit comments