|
2 | 2 |
|
3 | 3 | from __future__ import annotations
|
4 | 4 |
|
| 5 | +import logging |
5 | 6 | import string
|
| 7 | +import sys |
6 | 8 | import typing as t
|
7 | 9 |
|
8 | 10 | import pytest
|
|
12 | 14 | RandomStrSequence,
|
13 | 15 | get_test_session_name,
|
14 | 16 | get_test_window_name,
|
| 17 | + logger, |
| 18 | + namer, |
15 | 19 | )
|
16 | 20 |
|
17 | 21 | if t.TYPE_CHECKING:
|
18 | 22 | from libtmux.server import Server
|
19 | 23 | from libtmux.session import Session
|
20 | 24 |
|
21 | 25 |
|
| 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 | + |
22 | 32 | def test_random_str_sequence_default() -> None:
|
23 | 33 | """Test RandomStrSequence with default characters."""
|
24 | 34 | rng = RandomStrSequence()
|
@@ -68,6 +78,35 @@ def test_random_str_sequence_doctest_examples() -> None:
|
68 | 78 | assert isinstance(next(rng), str)
|
69 | 79 |
|
70 | 80 |
|
| 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 | + |
71 | 110 | def test_get_test_session_name_default_prefix(server: Server) -> None:
|
72 | 111 | """Test get_test_session_name with default prefix."""
|
73 | 112 | result = get_test_session_name(server=server)
|
@@ -110,6 +149,42 @@ def mock_next(self: t.Any) -> str:
|
110 | 149 | assert not server.has_session(result)
|
111 | 150 |
|
112 | 151 |
|
| 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 | + |
113 | 188 | def test_get_test_window_name_default_prefix(session: Session) -> None:
|
114 | 189 | """Test get_test_window_name with default prefix."""
|
115 | 190 | result = get_test_window_name(session=session)
|
@@ -152,7 +227,43 @@ def mock_next(self: t.Any) -> str:
|
152 | 227 | assert not any(w.window_name == result for w in session.windows)
|
153 | 228 |
|
154 | 229 |
|
| 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 | + |
155 | 254 | def test_get_test_window_name_requires_prefix() -> None:
|
156 | 255 | """Test that get_test_window_name requires a prefix."""
|
157 | 256 | with pytest.raises(AssertionError):
|
158 | 257 | 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