Merge pull request #998 from davep/extend-test-coverage

Add and extend unit tests
This commit is contained in:
Will McGugan
2022-10-25 11:32:25 +01:00
committed by GitHub
5 changed files with 96 additions and 3 deletions

View File

@@ -1,3 +1,5 @@
import pytest
from textual._arrange import arrange, TOP_Z
from textual._layout import WidgetPlacement
from textual.geometry import Region, Size, Spacing
@@ -91,3 +93,9 @@ def test_arrange_dock_bottom():
]
assert widgets == {child, header}
assert spacing == Spacing(0, 0, 1, 0)
def test_arrange_dock_badly():
child = Widget(id="child")
child.styles.dock = "nowhere"
with pytest.raises(AssertionError):
_ = arrange( Widget(), [child], Size(80, 24), Size(80, 24))

View File

@@ -1,6 +1,8 @@
from string import ascii_lowercase
import pytest
from textual.binding import Bindings, Binding
from textual.binding import Bindings, Binding, BindingError, NoBinding
BINDING1 = Binding("a,b", action="action1", description="description1")
BINDING2 = Binding("c", action="action2", description="description2")
@@ -14,14 +16,14 @@ def bindings():
def test_bindings_get_key(bindings):
assert bindings.get_key("b") == Binding("b", action="action1", description="description1")
assert bindings.get_key("c") == BINDING2
with pytest.raises(NoBinding):
bindings.get_key("control+meta+alt+shift+super+hyper+t")
def test_bindings_merge_simple(bindings):
left = Bindings([BINDING1])
right = Bindings([BINDING2])
assert Bindings.merge([left, right]).keys == bindings.keys
def test_bindings_merge_overlap():
left = Bindings([BINDING1])
another_binding = Binding("a", action="another_action", description="another_description")
@@ -29,3 +31,20 @@ def test_bindings_merge_overlap():
"a": another_binding,
"b": Binding("b", action="action1", description="description1"),
}
def test_bad_binding_tuple():
with pytest.raises(BindingError):
_ = Bindings((("a", "action"),))
with pytest.raises(BindingError):
_ = Bindings((("a", "action", "description","too much"),))
def test_binding_from_tuples():
assert Bindings((( BINDING2.key, BINDING2.action, BINDING2.description),)).get_key("c") == BINDING2
def test_shown():
bindings = Bindings([
Binding(
key, action=f"action_{key}", description=f"Emits {key}",show=bool(ord(key)%2)
) for key in ascii_lowercase
])
assert len(bindings.shown_keys)==(len(ascii_lowercase)/2)

View File

@@ -61,6 +61,25 @@ def test_lru_cache_get():
assert "egg" not in cache
assert "eggegg" in cache
def test_lru_cache_maxsize():
cache = LRUCache(3)
# Be sure that maxsize reports what we gave above.
assert cache.maxsize == 3, "Incorrect cache maxsize"
# Now resize the cache by setting maxsize.
cache.maxsize = 30
# Check that it's reporting that back.
assert cache.maxsize == 30, "Incorrect cache maxsize after setting it"
# Add more than maxsize items to the cache and be sure
for spam in range(cache.maxsize+10):
cache[f"spam{spam}"] = spam
# Finally, check the cache is the max size we set.
assert len(cache) == 30, "Cache grew too large given maxsize"
def test_lru_cache_mapping():
"""Test cache values can be set and read back."""

View File

@@ -295,6 +295,10 @@ def test_size_sub():
Size(1, 2) - "foo"
def test_size_line_range():
assert Size(20, 0).line_range == range(0)
assert Size(0, 20).line_range == range(20)
def test_region_x_extents():
assert Region(5, 10, 20, 30).column_span == (5, 25)

43
tests/test_path.py Normal file
View File

@@ -0,0 +1,43 @@
from typing import Type
from pathlib import Path
from textual.app import App
class RelativePathObjectApp(App[None]):
CSS_PATH = Path("test.css")
class RelativePathStrApp(App[None]):
CSS_PATH = "test.css"
class AbsolutePathObjectApp(App[None]):
CSS_PATH = Path("/tmp/test.css")
class AbsolutePathStrApp(App[None]):
CSS_PATH = "/tmp/test.css"
def path_tester(obj_type: Type[App[None]], str_type: Type[App[None]], intended_result: Path) -> None:
assert isinstance(obj_type().css_path,Path), (
"CSS_PATH didn't stay as an object"
)
assert isinstance(str_type().css_path,Path), (
"CSS_PATH wasn't converted from str to Path"
)
assert obj_type().css_path == intended_result, (
"CSS_PATH doesn't match the intended result."
)
assert str_type().css_path == intended_result, (
"CSS_PATH doesn't match the intended result."
)
assert str_type().css_path == obj_type().css_path, (
"CSS_PATH str to Path conversion gave a different result"
)
def test_relative_path():
path_tester(RelativePathObjectApp, RelativePathStrApp, ((Path(__file__).absolute().parent ) / "test.css").absolute())
def test_absolute_path():
path_tester(AbsolutePathObjectApp, AbsolutePathStrApp, Path("/tmp/test.css").absolute())