"""Tests for objects"""
import xml.etree.ElementTree as etree
from contextlib import ExitStack as does_not_raise
from pathlib import Path
import pytest
from pytiled_parser import common_types
from pytiled_parser.parsers.tmx.tiled_object import parse
from pytiled_parser.tiled_object import (
Ellipse,
Point,
Polygon,
Polyline,
Rectangle,
Text,
Tile,
)
ELLIPSES = [
(
"""
""",
Ellipse(
id=6,
size=common_types.Size(57.4014, 18.5518),
name="ellipse",
coordinates=common_types.OrderedPair(37.5401, 81.1913),
),
),
(
"""
""",
Ellipse(
id=7,
size=common_types.Size(6.3294, 31.4289),
name="ellipse - invisible",
visible=False,
coordinates=common_types.OrderedPair(22.6986, 53.9093),
),
),
(
"""
""",
Ellipse(
id=8,
size=common_types.Size(29.6828, 24.2264),
name="ellipse - rotated",
rotation=111,
coordinates=common_types.OrderedPair(35.7940, 120.0409),
),
),
(
"""
""",
Ellipse(
id=29,
name="ellipse - no width or height",
coordinates=common_types.OrderedPair(72.4611, 127.6799),
),
),
]
RECTANGLES = [
(
"""
""",
Rectangle(
id=1,
size=common_types.Size(45.3973, 41.4687),
coordinates=common_types.OrderedPair(27.7185, 23.5717),
name="rectangle",
),
),
(
"""
""",
Rectangle(
id=4,
size=common_types.Size(30.9924, 32.7384),
coordinates=common_types.OrderedPair(163.9104, 91.0128),
name="rectangle - invisible",
visible=False,
),
),
(
"""
""",
Rectangle(
id=5,
size=common_types.Size(10, 22),
coordinates=common_types.OrderedPair(183.3352, 23.3534),
name="rectangle - rotated",
rotation=10,
),
),
(
"""
""",
Rectangle(
id=28,
coordinates=common_types.OrderedPair(131.1720, 53.4728),
name="rectangle - no width or height",
),
),
(
r"""
""",
Rectangle(
id=30,
size=common_types.Size(21.1709, 13.7501),
coordinates=common_types.OrderedPair(39.0679, 131.8268),
name="rectangle - properties",
properties={
"bool property": False,
"color property": common_types.Color(170, 0, 0, 255),
"file property": Path("../../../../../../dev/null"),
"float property": 42.1,
"int property": 8675309,
"string property": "pytiled_parser rulez!1!!",
},
),
),
]
POINTS = [
(
"""
""",
Point(
id=2, coordinates=common_types.OrderedPair(159.9818, 82.9374), name="point"
),
),
(
"""
""",
Point(
id=2,
coordinates=common_types.OrderedPair(159.9818, 82.9374),
name="point - invisible",
visible=False,
),
),
]
POLYGONS = [
(
"""
""",
Polygon(
id=9,
coordinates=common_types.OrderedPair(89.4851, 38.6314),
name="polygon",
points=[
common_types.OrderedPair(0, 0),
common_types.OrderedPair(19.4248, 27.0638),
common_types.OrderedPair(19.6431, 3.0556),
common_types.OrderedPair(-2.6191, 15.9327),
common_types.OrderedPair(25.3177, 16.3692),
],
),
),
(
"""
""",
Polygon(
id=9,
coordinates=common_types.OrderedPair(89.4851, 38.6314),
name="polygon - invisible",
points=[
common_types.OrderedPair(0, 0),
common_types.OrderedPair(19.4248, 27.0638),
common_types.OrderedPair(19.6431, 3.0556),
common_types.OrderedPair(-2.6191, 15.9327),
common_types.OrderedPair(25.3177, 16.3692),
],
visible=False,
),
),
(
"""
""",
Polygon(
id=9,
coordinates=common_types.OrderedPair(89.4851, 38.6314),
name="polygon - rotated",
points=[
common_types.OrderedPair(0, 0),
common_types.OrderedPair(19.4248, 27.0638),
common_types.OrderedPair(19.6431, 3.0556),
common_types.OrderedPair(-2.6191, 15.9327),
common_types.OrderedPair(25.3177, 16.3692),
],
rotation=123,
),
),
]
POLYLINES = [
(
"""
""",
Polyline(
id=12,
coordinates=common_types.OrderedPair(124.1878, 90.1398),
name="polyline",
points=[
common_types.OrderedPair(0, 0),
common_types.OrderedPair(-13.3136, 41.0321),
common_types.OrderedPair(21.3891, 16.8057),
],
),
),
(
"""
""",
Polyline(
id=12,
coordinates=common_types.OrderedPair(124.1878, 90.1398),
name="polyline - invisible",
points=[
common_types.OrderedPair(0, 0),
common_types.OrderedPair(-13.3136, 41.0321),
common_types.OrderedPair(21.3891, 16.8057),
],
visible=False,
),
),
(
"""
""",
Polyline(
id=12,
coordinates=common_types.OrderedPair(124.1878, 90.1398),
name="polyline - rotated",
points=[
common_types.OrderedPair(0, 0),
common_types.OrderedPair(-13.3136, 41.0321),
common_types.OrderedPair(21.3891, 16.8057),
],
rotation=110,
),
),
]
TEXTS = [
(
"""
""",
Text(
id=19,
name="text",
text="Hello World",
size=common_types.Size(92.375, 19),
coordinates=common_types.OrderedPair(93.2987, 81.7106),
),
),
(
"""
""",
Text(
id=19,
name="text - wrap",
text="Hello World",
wrap=True,
size=common_types.Size(92.375, 19),
coordinates=common_types.OrderedPair(93.2987, 81.7106),
),
),
(
"""
""",
Text(
id=19,
name="text - rotated",
text="Hello World",
rotation=110,
size=common_types.Size(92.375, 19),
coordinates=common_types.OrderedPair(93.2987, 81.7106),
),
),
(
"""
""",
Text(
id=19,
name="text - different font",
text="Hello World",
font_size=19,
font_family="DejaVu Sans",
rotation=110,
size=common_types.Size(92.375, 19),
coordinates=common_types.OrderedPair(93.2987, 81.7106),
),
),
(
"""
""",
Text(
id=19,
name="text - right bottom align",
text="Hello World",
horizontal_align="right",
vertical_align="bottom",
size=common_types.Size(92.375, 19),
coordinates=common_types.OrderedPair(93.2987, 81.7106),
),
),
(
"""
""",
Text(
id=19,
name="text - center center align",
text="Hello World",
horizontal_align="center",
vertical_align="center",
size=common_types.Size(92.375, 19),
coordinates=common_types.OrderedPair(93.2987, 81.7106),
),
),
(
"""
""",
Text(
id=19,
name="text - justified",
text="Hello World",
horizontal_align="justify",
size=common_types.Size(92.375, 19),
coordinates=common_types.OrderedPair(93.2987, 81.7106),
),
),
(
"""
""",
Text(
id=19,
name="text - colored",
text="Hello World",
color=common_types.Color(170, 0, 0, 255),
size=common_types.Size(92.375, 19),
coordinates=common_types.OrderedPair(93.2987, 81.7106),
),
),
(
"""
""",
Text(
id=19,
name="text - font options",
text="Hello World",
size=common_types.Size(92.375, 19),
bold=True,
italic=True,
kerning=True,
strike_out=True,
underline=True,
wrap=True,
coordinates=common_types.OrderedPair(93.2987, 81.7106),
),
),
]
TILES = [
(
"""
""",
Tile(
id=13,
size=common_types.Size(32, 32),
name="tile",
coordinates=common_types.OrderedPair(111.8981, 48.3019),
gid=79,
),
),
(
"""
""",
Tile(
id=13,
size=common_types.Size(32, 32),
name="tile - invisible",
class_="tile",
coordinates=common_types.OrderedPair(111.8981, 48.3019),
gid=79,
visible=False,
),
),
(
"""
""",
Tile(
id=13,
size=common_types.Size(32, 32),
name="tile - rotated",
coordinates=common_types.OrderedPair(111.8981, 48.3019),
gid=79,
rotation=110,
),
),
]
OBJECTS = ELLIPSES + RECTANGLES + POINTS + POLYGONS + POLYLINES + TEXTS + TILES
@pytest.mark.parametrize("raw_object_tmx,expected", OBJECTS)
def test_parse_layer(raw_object_tmx, expected):
raw_object = etree.fromstring(raw_object_tmx)
result = parse(raw_object)
assert result == expected