8000 test(random): enhance test coverage · tmux-python/libtmux@6675ab2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6675ab2

Browse files
committed
test(random): enhance test coverage
- Add test for logger configuration - Add tests for multiple collisions in name generation - Add test for Self type annotation (Python 3.11+) - Add test for global namer instance - Add doctest example coverage
1 parent fbefdde commit 6675ab2

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

tests/test/test_random.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
from __future__ import annotations
44

5+
import logging
56
import string
7+
import sys
68
import typing as t
79

810
import pytest
@@ -12,13 +14,21 @@
1214
RandomStrSequence,
1315
get_test_session_name,
1416
get_test_window_name,
17+
logger,
18+
namer,
1519
)
1620

1721
if t.TYPE_CHECKING:
1822
from libtmux.server import Server
1923
from libtmux.session import Session
2024

2125

26+
def test_logger() -> None:
27+
"""Test that the logger is properly configured."""
28+
assert isinstance(logger, logging.Logger)
29+
assert logger.name == "libtmux.test.random"
30+
31+
2232
def test_random_str_sequence_default() -> None:
2333
"""Test RandomStrSequence with default characters."""
2434
rng = RandomStrSequence()
@@ -68,6 +78,35 @@ def test_random_str_sequence_doctest_examples() -> None:
6878
assert isinstance(next(rng), str)
6979

7080

81+
def test_namer_global_instance() -> None:
82+
"""Test the global namer instance."""
83+
# Test that namer is an instance of RandomStrSequence
84+
assert isinstance(namer, RandomStrSequence)
85+
86+
# Test that it generates valid strings
87+
result = next(namer)
88+
assert isinstance(result, str)
89+
assert len(result) == 8
90+
assert all(c in namer.characters for c in result)
91+
92+
# Test uniqueness
93+
results = [next(namer) for _ in range(10)]
94+
assert len(set(results)) == len(results)
95+
96+
97+
def test_get_test_session_name_doctest_examples(server: Server) -> None:
98+
"""Test the doctest examples for get_test_session_name."""
99+
# Test basic functionality
100+
result = get_test_session_name(server=server)
101+
assert result.startswith(TEST_SESSION_PREFIX)
102+
assert len(result) == len(TEST_SESSION_PREFIX) + 8
103+
104+
# Test uniqueness (from doctest example)
105+
result1 = get_test_session_name(server=server)
106+
result2 = get_test_session_name(server=server)
107+
assert result1 != result2
108+
109+
71110
def test_get_test_session_name_default_prefix(server: Server) -> None:
72111
"""Test get_test_session_name with default prefix."""
73112
result = get_test_session_name(server=server)
@@ -110,6 +149,42 @@ def mock_next(self: t.Any) -> str:
110149
assert not server.has_session(result)
111150

112151

152+
def test_get_test_session_name_multiple_collisions(
153+
server: Server,
154+
monkeypatch: pytest.MonkeyPatch,
155+
) -> None:
156+
"""Test get_test_session_name with multiple collisions."""
157+
names = ["collision1", "collision2", "success"]
158+
collision_names = [TEST_SESSION_PREFIX + name for name in names[:-1]]
159+
success_name = TEST_SESSION_PREFIX + names[-1]
160+
name_iter = iter(names)
161+
162+
def mock_next(self: t.Any) -> str:
163+
return next(name_iter)
164+
165+
monkeypatch.setattr(RandomStrSequence, "__next__", mock_next)
166+
167+
# Create sessions that will cause collisions
168+
with server.new_session(collision_names[0]):
169+
with server.new_session(collision_names[1]):
170+
result = get_test_session_name(server=server)
171+
assert result == success_name
172+
assert not server.has_session(result)
173+
174+
175+
def test_get_test_window_name_doctest_examples(session: Session) -> None:
176+
"""Test the doctest examples for get_test_window_name."""
177+
# Test basic functionality
178+
result = get_test_window_name(session=session)
179+
assert result.startswith(TEST_SESSION_PREFIX)
180+
assert len(result) == len(TEST_SESSION_PREFIX) + 8
181+
182+
# Test uniqueness (from doctest example)
183+
result1 = get_test_window_name(session=session)
184+
result2 = get_test_window_name(session=session)
185+
assert result1 != result2
186+
187+
113188
def test_get_test_window_name_default_prefix(session: Session) -> None:
114189
"""Test get_test_window_name with default prefix."""
115190
result = get_test_window_name(session=session)
@@ -152,7 +227,43 @@ def mock_next(self: t.Any) -> str:
152227
assert not any(w.window_name == result for w in session.windows)
153228

154229

230+
def test_get_test_window_name_multiple_collisions(
231+
session: Session,
232+
monkeypatch: pytest.MonkeyPatch,
233+
) -> None:
234+
"""Test get_test_window_name with multiple collisions."""
235+
names = ["collision1", "collision2", "success"]
236+
collision_names = [TEST_SESSION_PREFIX + name for name in names[:-1]]
237+
success_name = TEST_SESSION_PREFIX + names[-1]
238+
name_iter = iter(names)
239+
240+
def mock_next(self: t.Any) -> str:
241+
return next(name_iter)
242+
243+
monkeypatch.setattr(RandomStrSequence, "__next__", mock_next)
244+
245+
# Create windows that will cause collisions
246+
for name in collision_names:
247+
session.new_window(window_name=name)
248+
249+
result = get_test_window_name(session=session)
250+
assert result == success_name
251+
assert not any(w.window_name == result for w in session.windows)
252+
253+
155254
def test_get_test_window_name_requires_prefix() -> None:
156255
"""Test that get_test_window_name requires a prefix."""
157256
with pytest.raises(AssertionError):
158257
get_test_window_name(session=t.cast("Session", object()), prefix=None)
258+
259+
260+
@pytest.mark.skipif(
261+
sys.version_info < (3, 11),
262+
reason="Self type only available in Python 3.11+",
263+
)
264+
def test_random_str_sequence_self_type() -> None:
265+
"""Test that RandomStrSequence works with Self type annotation."""
266+
rng = RandomStrSequence()
267+
iter_result = iter(rng)
268+
assert isinstance(iter_result, RandomStrSequence)
269+
assert iter_result is rng

0 commit comments

Comments
 (0)
0