@@ -171,6 +171,45 @@ def mock_next(self: t.Any) -> str:
171171 assert not server .has_session (result )
172172
173173
174+ def test_get_test_session_name_loop_behavior (
175+ server : Server ,
176+ ) -> None :
177+ """Test the loop behavior in get_test_session_name using real sessions."""
178+ # Get a first session name
179+ first_name = get_test_session_name (server = server )
180+
181+ # Create this session to trigger the loop behavior
182+ with server .new_session (first_name ):
183+ # Now when we call get_test_session_name again, it should
184+ # give us a different name since the first one is taken
185+ second_name = get_test_session_name (server = server )
186+
187+ # Verify we got a different name
188+ assert first_name != second_name
189+
190+ # Verify the first name exists as a session
191+ assert server .has_session (first_name )
192+
193+ # Verify the second name doesn't exist yet
194+ assert not server .has_session (second_name )
195+
196+ # Create a second session with the second name
197+ with server .new_session (second_name ):
198+ # Now get a third name, to trigger another iteration
199+ third_name = get_test_session_name (server = server )
200+
201+ # Verify all names are different
202+ assert first_name != third_name
203+ assert second_name != third_name
204+
205+ # Verify the first two names exist as sessions
206+ assert server .has_session (first_name )
207+ assert server .has_session (second_name )
208+
209+ # Verify the third name doesn't exist yet
210+ assert not server .has_session (third_name )
211+
212+
174213def test_get_test_window_name_doctest_examples (session : Session ) -> None :
175214 """Test the doctest examples for get_test_window_name."""
176215 # Test basic functionality
@@ -250,6 +289,55 @@ def mock_next(self: t.Any) -> str:
250289 assert not any (w .window_name == result for w in session .windows )
251290
252291
292+ def test_get_test_window_name_loop_behavior (
293+ session : Session ,
294+ ) -> None :
295+ """Test the loop behavior in get_test_window_name using real windows."""
296+ # Get a window name first
297+ first_name = get_test_window_name (session = session )
298+
299+ # Create this window
300+ window = session .new_window (window_name = first_name )
301+ try :
302+ # Now when we call get_test_window_name again, it should
303+ # give us a different name since the first one is taken
304+ second_name = get_test_window_name (session = session )
305+
306+ # Verify we got a different name
307+ assert first_name != second_name
308+
309+ # Verify the first name exists as a window
310+ assert any (w .window_name == first_name for w in session .windows )
311+
312+ # Verify the second name doesn't exist yet
313+ assert not any (w .window_name == second_name for w in session .windows )
314+
315+ # Create a second window with the second name
316+ window2 = session .new_window (window_name = second_name )
317+ try :
318+ # Now get a third name, to trigger another iteration
319+ third_name = get_test_window_name (session = session )
320+
321+ # Verify all names are different
322+ assert first_name != third_name
323+ assert second_name != third_name
324+
325+ # Verify the first two names exist as windows
326+ assert any (w .window_name == first_name for w in session .windows )
327+ assert any (w .window_name == second_name for w in session .windows )
328+
329+ # Verify the third name doesn't exist yet
330+ assert not any (w .window_name == third_name for w in session .windows )
331+ finally :
332+ # Clean up the second window
333+ if window2 :
334+ window2 .kill ()
335+ finally :
336+ # Clean up
337+ if window :
338+ window .kill ()
339+
340+
253341def test_get_test_window_name_requires_prefix () -> None :
254342 """Test that get_test_window_name requires a prefix."""
255343 with pytest .raises (AssertionError ):
@@ -327,83 +415,23 @@ def test_namer_initialization() -> None:
327415 assert namer .characters == "abcdefghijklmnopqrstuvwxyz0123456789_"
328416
329417
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)
418+ def test_random_str_sequence_iter_next_methods () -> None :
419+ """Test both __iter__ and __next__ methods directly."""
420+ # Initialize the sequence
390421 rng = RandomStrSequence ()
422+
423+ # Test __iter__ method
391424 iter_result = iter (rng )
392425 assert iter_result is rng
393426
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
427+ # Test __next__ method directly multiple times
428+ results = []
429+ for _ in range (5 ):
430+ next_result = next (rng )
431+ results .append (next_result )
432+ assert isinstance (next_result , str )
433+ assert len (next_result ) == 8
434+ assert all (c in rng .characters for c in next_result )
407435
408- assert callable ( get_test_session_name )
409- assert callable ( get_test_window_name )
436+ # Verify all results are unique
437+ assert len ( set ( results )) == len ( results )
0 commit comments