Skip to content

Commit e1a73bd

Browse files
committed
test: improve coverage for random test utilities
1 parent e345a21 commit e1a73bd

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

tests/test/test_random.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,105 @@ def test_logger_configured(caplog: pytest.LogCaptureFixture) -> None:
305305

306306
assert "Test debug message" in caplog.text
307307
assert "Test info message" in caplog.text
308+
309+
310+
def test_next_method_directly() -> None:
311+
"""Test directly calling __next__ method on RandomStrSequence."""
312+
rng = RandomStrSequence()
313+
result = next(rng)
314+
assert isinstance(result, str)
315+
assert len(result) == 8
316+
assert all(c in rng.characters for c in result)
317+
318+
319+
def test_namer_initialization() -> None:
320+
"""Test that the namer global instance is initialized correctly."""
321+
# Since namer is a global instance from the random module,
322+
# we want to ensure it's properly initialized
323+
from libtmux.test.random import namer as direct_namer
324+
325+
assert namer is direct_namer
326+
assert isinstance(namer, RandomStrSequence)
327+
assert namer.characters == "abcdefghijklmnopqrstuvwxyz0123456789_"
328+
329+
330+
def test_get_test_session_name_loop_behavior(
331+
server: Server,
332+
monkeypatch: pytest.MonkeyPatch,
333+
) -> None:
334+
"""Test the loop behavior in get_test_session_name."""
335+
# Create two existing sessions with predictable names
336+
test_name_1 = f"{TEST_SESSION_PREFIX}test1"
337+
test_name_2 = f"{TEST_SESSION_PREFIX}test2"
338+
test_name_3 = f"{TEST_SESSION_PREFIX}test3"
339+
340+
# Set up the random sequence to return specific values
341+
name_sequence = iter(["test1", "test2", "test3"])
342+
343+
def mock_next(self: t.Any) -> str:
344+
return next(name_sequence)
345+
346+
monkeypatch.setattr(RandomStrSequence, "__next__", mock_next)
347+
348+
# Create two sessions that will match our first two random names
349+
with server.new_session(test_name_1), server.new_session(test_name_2):
350+
# This should skip the first two names and use the third one
351+
result = get_test_session_name(server=server)
352+
assert result == test_name_3
353+
assert not server.has_session(result)
354+
355+
356+
def test_get_test_window_name_loop_behavior(
357+
session: Session,
358+
monkeypatch: pytest.MonkeyPatch,
359+
) -> None:
360+
"""Test the loop behavior in get_test_window_name."""
361+
# Create two existing windows with predictable names
362+
test_name_1 = f"{TEST_SESSION_PREFIX}test1"
363+
test_name_2 = f"{TEST_SESSION_PREFIX}test2"
364+
test_name_3 = f"{TEST_SESSION_PREFIX}test3"
365+
366+
# Set up the random sequence to return specific values
367+
name_sequence = iter(["test1", "test2", "test3"])
368+
369+
def mock_next(self: t.Any) -> str:
370+
return next(name_sequence)
371+
372+
monkeypatch.setattr(RandomStrSequence, "__next__", mock_next)
373+
374+
# Create two windows that will match our first two random names
375+
session.new_window(window_name=test_name_1)
376+
session.new_window(window_name=test_name_2)
377+
378+
# This should skip the first two names and use the third one
379+
result = get_test_window_name(session=session)
380+
assert result == test_name_3
381+
assert not any(w.window_name == result for w in session.windows)
382+
383+
384+
def test_random_str_sequence_explicit_coverage() -> None:
385+
"""Test to explicitly cover certain methods and lines."""
386+
# This test is designed to improve coverage by directly accessing
387+
# specific methods and attributes
388+
389+
# Test RandomStrSequence.__iter__ (line 47)
390+
rng = RandomStrSequence()
391+
iter_result = iter(rng)
392+
assert iter_result is rng
393+
394+
# Test RandomStrSequence.__next__ (line 51)
395+
next_result = next(rng)
396+
assert isinstance(next_result, str)
397+
assert len(next_result) == 8
398+
399+
# Test the global namer instance (line 56)
400+
from libtmux.test.random import namer
401+
402+
assert isinstance(namer, RandomStrSequence)
403+
404+
# Force module to load get_test_session_name and
405+
# get_test_window_name functions (lines 59, 94)
406+
from libtmux.test.random import get_test_session_name, get_test_window_name
407+
408+
assert callable(get_test_session_name)
409+
assert callable(get_test_window_name)

0 commit comments

Comments
 (0)