8000 test(random): Add comprehensive tests for random test utilities · tmux-python/libtmux@8b78910 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b78910

Browse files
committed
test(random): Add comprehensive tests for random test utilities
why: Ensure test utilities for random string generation and naming work correctly what: - Add tests for RandomStrSequence with default and custom characters - Test iterator protocol and uniqueness guarantees - Test session/window name generation with real tmux server - Use string.ascii_uppercase for predictable character set - Verify prefix requirements and name collisions refs: Uses global server/session fixtures for real tmux testing
1 parent 0c9231c commit 8b78910

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

tests/test/test_random.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""Tests for libtmux's random test utilities."""
2+
3+
from __future__ import annotations
4+
5+
import string
6+
import typing as t
7+
8+
import pytest
9+
10+
from libtmux.test.random import (
11+
RandomStrSequence,
12+
get_test_session_name,
13+
get_test_window_name,
14+
)
15+
16+
if t.TYPE_CHECKING:
17+
from libtmux.server import Server
18+
from libtmux.session import Session
19+
20+
21+
def test_random_str_sequence_default() -> None:
22+
"""Test RandomStrSequence with default characters."""
23+
rng = RandomStrSequence()
24+
result = next(rng)
25+
26+
assert isinstance(result, str)
27+
assert len(result) == 8
28+
assert all(c in "abcdefghijklmnopqrstuvwxyz0123456789_" for c in result)
29+
30+
31+
def test_random_str_sequence_custom_chars() -> None:
32+
"""Test RandomStrSequence with custom characters."""
33+
custom_chars = string.ascii_uppercase # Enough characters for sampling
34+
rng = RandomStrSequence(characters=custom_chars)
35+
result = next(rng)
36+
37+
assert isinstance(result, str)
38+
assert len(result) == 8
39+
assert all(c in custom_chars for c in result)
40+
41+
42+
def test_random_str_sequence_uniqueness() -> None:
43+
"""Test that RandomStrSequence generates unique strings."""
44+
rng = RandomStrSequence()
45+
results = [next(rng) for _ in range(100)]
46+
47+
# Check uniqueness
48+
assert len(set(results)) == len(results)
49+
50+
51+
def test_random_str_sequence_iterator() -> None:
52+
"""Test that RandomStrSequence is a proper iterator."""
53+
rng = RandomStrSequence()
54+
assert iter(rng) is rng
55+
56+
57+
def test_get_test_session_name(server: Server) -> None:
58+
"""Test get_test_session_name function."""
59+
result = get_test_session_name(server=server)
60+
61+
assert isinstance(result, str)
62+
assert result.startswith("libtmux_") # Uses TEST_SESSION_PREFIX
63+
assert len(result) == 16 # prefix(8) + random(8)
64+
assert not server.has_session(result)
65+
66+
67+
def test_get_test_window_name(session: Session) -> None:
68+
"""Test get_test_window_name function."""
69+
result = get_test_window_name(session=session)
70+
71+
assert isinstance(result, str)
72+
assert result.startswith("libtmux_") # Uses TEST_SESSION_PREFIX
73+
assert len(result) == 16 # prefix(8) + random(8)
74+
assert not any(w.window_name == result for w in session.windows)
75+
76+
77+
def test_get_test_window_name_requires_prefix() -> None:
78+
"""Test that get_test_window_name requires a prefix."""
79+
with pytest.raises(AssertionError):
80+
get_test_window_name(session=t.cast("Session", object()), prefix=None)

0 commit comments

Comments
 (0)
0