rf: better implementation and testing of the offset feature of layers

This commit is contained in:
Benjamin Kirkbride
2020-04-19 21:25:43 -04:00
parent 86f7adb984
commit 156597637a
3 changed files with 32 additions and 15 deletions

View File

@@ -0,0 +1,6 @@
def is_float(string: str):
try:
float(string)
return True
except (ValueError, TypeError):
return False

View File

@@ -10,6 +10,7 @@ from pathlib import Path
from typing import Callable, Dict, List, Optional, Tuple, Union
import pytiled_parser.objects as objects
import pytiled_parser.typing_helpers as TH
from pytiled_parser.utilities import parse_color
@@ -187,21 +188,12 @@ def _parse_layer(
name = layer_element.attrib["name"]
offset: Optional[objects.OrderedPair]
offset_x_attrib = layer_element.attrib.get("offsetx")
offset_y_attrib = layer_element.attrib.get("offsety")
# If any offset is present, we need to return an OrderedPair
# Unknown if only one of the offsets could be absent.
if any([offset_x_attrib, offset_y_attrib]):
if offset_x_attrib:
offset_x = float(offset_x_attrib)
else:
offset_x = 0.0
if offset_y_attrib:
offset_y = float(offset_y_attrib)
else:
offset_y = 0.0
offset = objects.OrderedPair(offset_x, offset_y)
offset_x = layer_element.attrib.get("offsetx")
offset_y = layer_element.attrib.get("offsety")
if offset_x and offset_y:
assert TH.is_float(offset_x)
assert TH.is_float(offset_y)
offset = objects.OrderedPair(float(offset_x), float(offset_y))
else:
offset = None

View File

@@ -0,0 +1,19 @@
"""Tests for typing helpers"""
import pytest
from pytiled_parser import typing_helpers as TH
TEST_IS_FLOAT_PARAMS = [
(1, True),
("1", True),
(1.1, True),
("1.1", True),
("one", False),
(None, False),
]
@pytest.mark.parametrize("string,expected", TEST_IS_FLOAT_PARAMS)
def test_is_float(string, expected):
assert TH.is_float(string) == expected