Coverage for tests/test_main.py: 100%
120 statements
« prev ^ index » next coverage.py v7.10.4, created at 2025-08-22 15:57 -0700
« prev ^ index » next coverage.py v7.10.4, created at 2025-08-22 15:57 -0700
1import sys
2from unittest.mock import patch
4from fguess import main
7class TestMainFunction:
8 """Test the main CLI function."""
10 def test_main_with_command_line_argument_with_results(self, capsys):
11 """Test main function with command line argument that has results."""
12 with patch.object(sys, 'argv', ['fguess.py', '42']):
13 main()
15 captured = capsys.readouterr()
16 assert "Input: '42'" in captured.out
17 assert "-" * 40 in captured.out
18 assert "→" in captured.out # Should have format results
19 assert "variable =" in captured.out # Should show test values
21 def test_main_with_command_line_argument_no_results(self, capsys):
22 """Test main function with command line argument that has no results."""
23 with patch.object(sys, 'argv', ['fguess.py', 'hello']):
24 main()
26 captured = capsys.readouterr()
27 assert "Input: 'hello'" in captured.out
28 assert "-" * 40 in captured.out
29 assert "No format specifications found" in captured.out
31 @patch('builtins.input', return_value='123.45')
32 def test_main_interactive_mode_with_results(self, mock_input, capsys):
33 """Test main function in interactive mode with results."""
34 with patch.object(sys, 'argv', ['fguess.py']):
35 main()
37 captured = capsys.readouterr()
38 assert "Enter a formatted string (or 'quit' to exit):" in captured.out
39 assert "Input: '123.45'" in captured.out
40 assert "-" * 40 in captured.out
41 assert "→" in captured.out # Should have format results
43 @patch('builtins.input', return_value='quit')
44 def test_main_interactive_mode_quit(self, mock_input, capsys):
45 """Test main function in interactive mode with quit command."""
46 with patch.object(sys, 'argv', ['fguess.py']):
47 main()
49 captured = capsys.readouterr()
50 assert "Enter a formatted string (or 'quit' to exit):" in captured.out
51 # Should not have any other output after quit
52 assert "Input:" not in captured.out
53 assert "→" not in captured.out
55 @patch('builtins.input', return_value='QUIT') # Test case-insensitive
56 def test_main_interactive_mode_quit_case_insensitive(self, mock_input, capsys):
57 """Test that quit command is case-insensitive."""
58 with patch.object(sys, 'argv', ['fguess.py']):
59 main()
61 captured = capsys.readouterr()
62 assert "Enter a formatted string (or 'quit' to exit):" in captured.out
63 assert "Input:" not in captured.out
65 @patch('builtins.input', return_value='nonexistent')
66 def test_main_interactive_mode_no_results(self, mock_input, capsys):
67 """Test main function in interactive mode with no format results."""
68 with patch.object(sys, 'argv', ['fguess.py']):
69 main()
71 captured = capsys.readouterr()
72 assert "Enter a formatted string (or 'quit' to exit):" in captured.out
73 assert "Input: 'nonexistent'" in captured.out
74 assert "No format specifications found" in captured.out
76 def test_main_duplicate_formats_deduplication(self, capsys):
77 """Test that duplicate formats are properly deduplicated in output."""
78 # Use an input that might generate duplicate formats in the internal logic
79 with patch.object(sys, 'argv', ['fguess.py', '42']):
80 main()
82 captured = capsys.readouterr()
83 # Count how many times a specific format appears
84 lines = captured.out.split('\n')
85 format_lines = [line for line in lines if '→' in line]
87 # Should not have duplicate format lines (deduplication should work)
88 assert len(format_lines) == len(set(format_lines))
90 def test_main_output_formatting(self, capsys):
91 """Test the specific formatting of the main function output."""
92 with patch.object(sys, 'argv', ['fguess.py', '$123.45']):
93 main()
95 captured = capsys.readouterr()
96 lines = captured.out.split('\n')
98 # Check that we have the expected structure
99 input_line = next((line for line in lines if line.startswith("Input:")), None)
100 assert input_line == "Input: '$123.45'"
102 separator_line = next((line for line in lines if line == "-" * 40), None)
103 assert separator_line is not None
105 # Should have format result lines with proper spacing
106 format_lines = [line for line in lines if '→' in line]
107 assert len(format_lines) > 0
109 for format_line in format_lines:
110 # Check format: "type → format_spec"
111 parts = format_line.split('→')
112 assert len(parts) == 2
113 type_part = parts[0].strip()
114 assert type_part in ['str', 'int', 'float', 'datetime']
116 def test_main_test_value_generation(self, capsys):
117 """Test that test values are generated and displayed correctly."""
118 with patch.object(sys, 'argv', ['fguess.py', '0x42']):
119 main()
121 captured = capsys.readouterr()
123 # Should have lines with test values
124 example_lines = [line for line in captured.out.split('\n') if 'e.g., variable =' in line]
125 assert len(example_lines) > 0
127 # Should show the actual test value
128 assert any('66' in line for line in example_lines) # 0x42 = 66
130 def test_main_empty_input(self, capsys):
131 """Test main function with empty input."""
132 with patch.object(sys, 'argv', ['fguess.py', '']):
133 main()
135 captured = capsys.readouterr()
136 assert "Input: ''" in captured.out
137 assert "No format specifications found" in captured.out
139 @patch('builtins.input', return_value='')
140 def test_main_interactive_empty_input(self, mock_input, capsys):
141 """Test main function in interactive mode with empty input."""
142 with patch.object(sys, 'argv', ['fguess.py']):
143 main()
145 captured = capsys.readouterr()
146 assert "Enter a formatted string (or 'quit' to exit):" in captured.out
147 assert "Input: ''" in captured.out
148 assert "No format specifications found" in captured.out
150 def test_main_datetime_format_display(self, capsys):
151 """Test that datetime formats are displayed correctly."""
152 with patch.object(sys, 'argv', ['fguess.py', '2030-01-24']):
153 main()
155 captured = capsys.readouterr()
157 # Should have datetime format
158 assert 'datetime' in captured.out
159 assert '%Y-%m-%d' in captured.out
160 # Should show datetime test value
161 assert 'datetime.datetime' in captured.out or '2030' in captured.out
163 def test_main_multiple_format_types(self, capsys):
164 """Test display when multiple format types are available."""
165 with patch.object(sys, 'argv', ['fguess.py', ' 42 ']): # Should have int and str formats
166 main()
168 captured = capsys.readouterr()
170 # Should have multiple format types
171 format_lines = [line for line in captured.out.split('\n') if '→' in line]
172 format_types = set()
174 for line in format_lines:
175 type_part = line.split('→')[0].strip()
176 format_types.add(type_part)
178 # Should have at least int and str formats
179 assert len(format_types) >= 2
180 assert 'int' in format_types or 'str' in format_types
182 def test_main_special_characters_in_input(self, capsys):
183 """Test main function with special characters that need proper display."""
184 special_input = "Price: $1,234.56€"
185 with patch.object(sys, 'argv', ['fguess.py', special_input]):
186 main()
188 captured = capsys.readouterr()
190 # Input should be displayed correctly with special characters
191 assert f"Input: '{special_input}'" in captured.out
193 # Should handle Unicode characters properly
194 assert '€' in captured.out