Skip to content

Commit 8603e56

Browse files
authored
Merge pull request #998 from davep/extend-test-coverage
Add and extend unit tests
2 parents ed395e3 + 512af0c commit 8603e56

File tree

5 files changed

+96
-3
lines changed

5 files changed

+96
-3
lines changed

tests/test_arrange.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from textual._arrange import arrange, TOP_Z
24
from textual._layout import WidgetPlacement
35
from textual.geometry import Region, Size, Spacing
@@ -91,3 +93,9 @@ def test_arrange_dock_bottom():
9193
]
9294
assert widgets == {child, header}
9395
assert spacing == Spacing(0, 0, 1, 0)
96+
97+
def test_arrange_dock_badly():
98+
child = Widget(id="child")
99+
child.styles.dock = "nowhere"
100+
with pytest.raises(AssertionError):
101+
_ = arrange( Widget(), [child], Size(80, 24), Size(80, 24))

tests/test_binding.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from string import ascii_lowercase
2+
13
import pytest
24

3-
from textual.binding import Bindings, Binding
5+
from textual.binding import Bindings, Binding, BindingError, NoBinding
46

57
BINDING1 = Binding("a,b", action="action1", description="description1")
68
BINDING2 = Binding("c", action="action2", description="description2")
@@ -14,18 +16,35 @@ def bindings():
1416
def test_bindings_get_key(bindings):
1517
assert bindings.get_key("b") == Binding("b", action="action1", description="description1")
1618
assert bindings.get_key("c") == BINDING2
17-
19+
with pytest.raises(NoBinding):
20+
bindings.get_key("control+meta+alt+shift+super+hyper+t")
1821

1922
def test_bindings_merge_simple(bindings):
2023
left = Bindings([BINDING1])
2124
right = Bindings([BINDING2])
2225
assert Bindings.merge([left, right]).keys == bindings.keys
2326

24-
2527
def test_bindings_merge_overlap():
2628
left = Bindings([BINDING1])
2729
another_binding = Binding("a", action="another_action", description="another_description")
2830
assert Bindings.merge([left, Bindings([another_binding])]).keys == {
2931
"a": another_binding,
3032
"b": Binding("b", action="action1", description="description1"),
3133
}
34+
35+
def test_bad_binding_tuple():
36+
with pytest.raises(BindingError):
37+
_ = Bindings((("a", "action"),))
38+
with pytest.raises(BindingError):
39+
_ = Bindings((("a", "action", "description","too much"),))
40+
41+
def test_binding_from_tuples():
42+
assert Bindings((( BINDING2.key, BINDING2.action, BINDING2.description),)).get_key("c") == BINDING2
43+
44+
def test_shown():
45+
bindings = Bindings([
46+
Binding(
47+
key, action=f"action_{key}", description=f"Emits {key}",show=bool(ord(key)%2)
48+
) for key in ascii_lowercase
49+
])
50+
assert len(bindings.shown_keys)==(len(ascii_lowercase)/2)

tests/test_cache.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ def test_lru_cache_get():
6161
assert "egg" not in cache
6262
assert "eggegg" in cache
6363

64+
def test_lru_cache_maxsize():
65+
cache = LRUCache(3)
66+
67+
# Be sure that maxsize reports what we gave above.
68+
assert cache.maxsize == 3, "Incorrect cache maxsize"
69+
70+
# Now resize the cache by setting maxsize.
71+
cache.maxsize = 30
72+
73+
# Check that it's reporting that back.
74+
assert cache.maxsize == 30, "Incorrect cache maxsize after setting it"
75+
76+
# Add more than maxsize items to the cache and be sure
77+
for spam in range(cache.maxsize+10):
78+
cache[f"spam{spam}"] = spam
79+
80+
# Finally, check the cache is the max size we set.
81+
assert len(cache) == 30, "Cache grew too large given maxsize"
82+
6483

6584
def test_lru_cache_mapping():
6685
"""Test cache values can be set and read back."""

tests/test_geometry.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ def test_size_sub():
295295
Size(1, 2) - "foo"
296296

297297

298+
def test_size_line_range():
299+
assert Size(20, 0).line_range == range(0)
300+
assert Size(0, 20).line_range == range(20)
301+
298302
def test_region_x_extents():
299303
assert Region(5, 10, 20, 30).column_span == (5, 25)
300304

tests/test_path.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import Type
2+
from pathlib import Path
3+
4+
from textual.app import App
5+
6+
class RelativePathObjectApp(App[None]):
7+
8+
CSS_PATH = Path("test.css")
9+
10+
class RelativePathStrApp(App[None]):
11+
12+
CSS_PATH = "test.css"
13+
14+
class AbsolutePathObjectApp(App[None]):
15+
16+
CSS_PATH = Path("/tmp/test.css")
17+
18+
class AbsolutePathStrApp(App[None]):
19+
20+
CSS_PATH = "/tmp/test.css"
21+
22+
def path_tester(obj_type: Type[App[None]], str_type: Type[App[None]], intended_result: Path) -> None:
23+
assert isinstance(obj_type().css_path,Path), (
24+
"CSS_PATH didn't stay as an object"
25+
)
26+
assert isinstance(str_type().css_path,Path), (
27+
"CSS_PATH wasn't converted from str to Path"
28+
)
29+
assert obj_type().css_path == intended_result, (
30+
"CSS_PATH doesn't match the intended result."
31+
)
32+
assert str_type().css_path == intended_result, (
33+
"CSS_PATH doesn't match the intended result."
34+
)
35+
assert str_type().css_path == obj_type().css_path, (
36+
"CSS_PATH str to Path conversion gave a different result"
37+
)
38+
39+
def test_relative_path():
40+
path_tester(RelativePathObjectApp, RelativePathStrApp, ((Path(__file__).absolute().parent ) / "test.css").absolute())
41+
42+
def test_absolute_path():
43+
path_tester(AbsolutePathObjectApp, AbsolutePathStrApp, Path("/tmp/test.css").absolute())

0 commit comments

Comments
 (0)