|
| 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