This commit is contained in:
Will McGugan
2024-07-20 18:07:22 +01:00
parent af4b277564
commit 1c5c08040d
3 changed files with 29 additions and 6 deletions

View File

@@ -18,11 +18,11 @@ class DurationParseError(DurationError):
def _duration_as_seconds(duration: str) -> float:
"""
Args:
duration: A string of the form ``"2s"`` or ``"300ms"``, representing 2 seconds and
300 milliseconds respectively. If no unit is supplied, e.g. ``"2"``, then the duration is
duration: A string of the form `"2s"` or `"300ms"`, representing 2 seconds and
300 milliseconds respectively. If no unit is supplied, e.g. `"2"`, then the duration is
assumed to be in seconds.
Raises:
DurationParseError: If the argument ``duration`` is not a valid duration string.
DurationParseError: If the argument `duration` is not a valid duration string.
Returns:
The duration in seconds.
"""
@@ -39,8 +39,6 @@ def _duration_as_seconds(duration: str) -> float:
try:
duration_secs = float(duration)
except ValueError:
raise DurationParseError(
f"{duration!r} is not a valid duration."
) from ValueError
raise DurationParseError(f"{duration!r} is not a valid duration.") from None
return duration_secs

15
tests/test_duration.py Normal file
View File

@@ -0,0 +1,15 @@
import pytest
from textual._duration import DurationParseError, _duration_as_seconds
def test_parse() -> None:
assert _duration_as_seconds("30") == 30.0
assert _duration_as_seconds("30s") == 30.0
assert _duration_as_seconds("30000ms") == 30.0
assert _duration_as_seconds("0.5") == 0.5
assert _duration_as_seconds("0.5s") == 0.5
assert _duration_as_seconds("500ms") == 0.5
with pytest.raises(DurationParseError):
_duration_as_seconds("300x")

View File

@@ -16,6 +16,16 @@ def test_add_one():
assert len(nodes) == 1
def test_length_hint():
"""Check length hint dunder method."""
nodes = NodeList()
assert nodes.__length_hint__() == 0
nodes._append(Widget())
nodes._append(Widget())
nodes._append(Widget())
assert nodes.__length_hint__() == 3
def test_repeat_add_one():
"""Does adding the same item to the node list ignore the additional adds?"""
nodes = NodeList()