|
| 1 | +"""Additional tests for terminal output handling in tools.py""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from wcgw.client.tools import ( |
| 7 | + Confirmation, |
| 8 | + _incremental_text, |
| 9 | + _is_int, |
| 10 | + ask_confirmation, |
| 11 | + get_incremental_output, |
| 12 | + render_terminal_output, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class TestTerminalOutputFull(unittest.TestCase): |
| 17 | + def setUp(self): |
| 18 | + """Set up test environment before each test case""" |
| 19 | + self.maxDiff = None |
| 20 | + |
| 21 | + def test_get_incremental_output_break_conditions(self): |
| 22 | + """Test break conditions in get_incremental_output""" |
| 23 | + # Test case 1: No match found - returns entire new output |
| 24 | + old_output = ["old"] |
| 25 | + new_output = ["diff1", "diff2"] |
| 26 | + result = get_incremental_output(old_output, new_output) |
| 27 | + self.assertEqual(result, new_output) # No match, return all |
| 28 | + |
| 29 | + # Test case 2: Last line matches but not complete sequence - returns entire new output |
| 30 | + old_output = ["start", "last"] |
| 31 | + new_output = [ |
| 32 | + "other", |
| 33 | + "last", |
| 34 | + "extra", |
| 35 | + ] # Matches last line but not full sequence |
| 36 | + result = get_incremental_output(old_output, new_output) |
| 37 | + self.assertEqual(result, new_output) # Incomplete match, return all |
| 38 | + |
| 39 | + # Test case 3: Empty old output - returns entire new output |
| 40 | + old_output = [] |
| 41 | + new_output = ["line1", "line2"] |
| 42 | + result = get_incremental_output(old_output, new_output) |
| 43 | + self.assertEqual(result, new_output) # Empty old, return all |
| 44 | + |
| 45 | + # Test case 4: Complete sequence match - returns only new content after match |
| 46 | + old_output = ["start", "middle"] |
| 47 | + new_output = ["start", "middle", "extra"] # Complete sequence matches |
| 48 | + result = get_incremental_output(old_output, new_output) |
| 49 | + self.assertEqual(result, ["extra"]) # Complete match, return only new content |
| 50 | + |
| 51 | + def test_incremental_terminal_output(self): |
| 52 | + """Test incremental terminal output handling""" |
| 53 | + # Test with completely empty old output |
| 54 | + result = _incremental_text("new text", "") |
| 55 | + self.assertEqual(result.rstrip(), "new text") |
| 56 | + |
| 57 | + # Test with new output matching end of old |
| 58 | + result = _incremental_text( |
| 59 | + "some existing text\nnew text", "some existing text\n" |
| 60 | + ) |
| 61 | + self.assertEqual(result.rstrip(), "new text") |
| 62 | + |
| 63 | + def test_ask_confirmation(self): |
| 64 | + """Test ask_confirmation function""" |
| 65 | + with patch("builtins.input", return_value="y"): |
| 66 | + result = ask_confirmation(Confirmation(prompt="Test prompt")) |
| 67 | + self.assertEqual(result, "Yes") |
| 68 | + |
| 69 | + with patch("builtins.input", return_value="n"): |
| 70 | + result = ask_confirmation(Confirmation(prompt="Test prompt")) |
| 71 | + self.assertEqual(result, "No") |
| 72 | + |
| 73 | + # Test with other input that should be treated as no |
| 74 | + with patch("builtins.input", return_value="anything"): |
| 75 | + result = ask_confirmation(Confirmation(prompt="Test prompt")) |
| 76 | + self.assertEqual(result, "No") |
| 77 | + |
| 78 | + def test_terminal_empty_output(self): |
| 79 | + """Test render_terminal_output with empty lines and whitespace""" |
| 80 | + # Test with completely empty output |
| 81 | + result = render_terminal_output("") |
| 82 | + self.assertEqual(result, []) |
| 83 | + |
| 84 | + # Test with non-empty followed by empty lines |
| 85 | + result = render_terminal_output("line1\nline2\n \n\n") |
| 86 | + result = [line.rstrip() for line in result] # Strip trailing spaces |
| 87 | + self.assertEqual(result, ["line1", "line2"]) # Empty lines at end are filtered |
| 88 | + |
| 89 | + # Test with content between empty lines |
| 90 | + result = render_terminal_output("\n\nline1\n\nline2\n") |
| 91 | + result = [line.rstrip() for line in result] |
| 92 | + self.assertEqual( |
| 93 | + result, ["", "", "line1", "", "line2"] |
| 94 | + ) # Empty at start kept, at end filtered |
| 95 | + |
| 96 | + # Test with trailing whitespace lines |
| 97 | + result = render_terminal_output("line1\n \n ") |
| 98 | + result = [line.rstrip() for line in result] |
| 99 | + self.assertEqual(result, ["line1"]) # Whitespace at end filtered |
| 100 | + |
| 101 | + def test_is_int_function(self): |
| 102 | + """Test _is_int function with various inputs""" |
| 103 | + self.assertTrue(_is_int("123")) # Valid integer |
| 104 | + self.assertTrue(_is_int("-123")) # Negative integer |
| 105 | + self.assertTrue(_is_int("0")) # Zero |
| 106 | + |
| 107 | + self.assertFalse(_is_int("abc")) # Letters |
| 108 | + self.assertFalse(_is_int("12.3")) # Float |
| 109 | + self.assertFalse(_is_int("")) # Empty string |
| 110 | + self.assertFalse(_is_int(" ")) # Whitespace |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + unittest.main() |
0 commit comments