1212from apywire .__main__ import main
1313
1414
15- def test_cli_version_short_flag () -> None :
16- """Test CLI version display with -v flag."""
15+ @pytest .mark .parametrize (
16+ "flag" ,
17+ ["-v" , "--version" , "-h" , "--help" ],
18+ ids = ["v" , "version" , "h" , "help" ],
19+ )
20+ def test_cli_basic_flags_exit_zero (flag : str ) -> None :
21+ """Test that basic CLI flags exit with code 0."""
1722 with pytest .raises (SystemExit ) as exc_info :
18- main (["-v" ])
19-
20- # argparse with action="version" exits with code 0
21- assert exc_info .value .code == 0
22-
23-
24- def test_cli_version_long_flag () -> None :
25- """Test CLI version display with --version flag."""
26- with pytest .raises (SystemExit ) as exc_info :
27- main (["--version" ])
28-
29- # argparse with action="version" exits with code 0
30- assert exc_info .value .code == 0
31-
32-
33- def test_cli_help_flag () -> None :
34- """Test CLI help display with -h flag."""
35- with pytest .raises (SystemExit ) as exc_info :
36- main (["-h" ])
37-
38- # argparse with action="help" exits with code 0
39- assert exc_info .value .code == 0
40-
41-
42- def test_cli_help_long_flag () -> None :
43- """Test CLI help display with --help flag."""
44- with pytest .raises (SystemExit ) as exc_info :
45- main (["--help" ])
46-
47- # argparse with action="help" exits with code 0
23+ main ([flag ])
4824 assert exc_info .value .code == 0
4925
5026
@@ -54,33 +30,15 @@ def test_cli_no_arguments() -> None:
5430 assert result == 0
5531
5632
57- def test_cli_version_output_contains_package_name () -> None :
58- """Test that version output contains 'apywire'."""
33+ @pytest .mark .parametrize ("flag" , ["-v" , "--version" ], ids = ["v" , "version" ])
34+ def test_cli_version_output_format (flag : str ) -> None :
35+ """Test that version output contains package name and version."""
5936 from importlib .metadata import version
6037
6138 expected_version = version ("apywire" )
62-
63- # Capture stdout since argparse writes version to stdout
6439 with pytest .raises (SystemExit ) as exc_info :
6540 with patch ("sys.stdout" , new_callable = StringIO ) as mock_stdout :
66- main (["--version" ])
67-
68- assert exc_info .value .code == 0
69- stdout_output = mock_stdout .getvalue ()
70- assert "apywire" in stdout_output
71- assert expected_version in stdout_output
72-
73-
74- def test_cli_version_short_output_contains_package_name () -> None :
75- """Test that -v output contains 'apywire'."""
76- from importlib .metadata import version
77-
78- expected_version = version ("apywire" )
79-
80- # Capture stdout since argparse writes version to stdout
81- with pytest .raises (SystemExit ) as exc_info :
82- with patch ("sys.stdout" , new_callable = StringIO ) as mock_stdout :
83- main (["-v" ])
41+ main ([flag ])
8442
8543 assert exc_info .value .code == 0
8644 stdout_output = mock_stdout .getvalue ()
@@ -170,40 +128,25 @@ def test_cli_module_execution() -> None:
170128 assert "apywire" in result .stdout # argparse outputs to stdout
171129
172130
173- def test_cli_generate_json () -> None :
174- """Test generate command with JSON format."""
175- with patch ("sys.stdout" , new_callable = StringIO ) as mock_stdout :
176- result = main (
177- ["generate" , "--format" , "json" , "collections.OrderedDict d" ]
178- )
179-
180- assert result == 0
181- output = mock_stdout .getvalue ()
182- assert "collections.OrderedDict d" in output
183-
184-
185- def test_cli_generate_ini () -> None :
186- """Test generate command with INI format."""
187- with patch ("sys.stdout" , new_callable = StringIO ) as mock_stdout :
188- result = main (
189- ["generate" , "--format" , "ini" , "collections.OrderedDict d" ]
190- )
191-
192- assert result == 0
193- output = mock_stdout .getvalue ()
194- assert "[collections.OrderedDict d]" in output
195-
196-
197- def test_cli_generate_toml () -> None :
198- """Test generate command with TOML format."""
131+ @pytest .mark .parametrize (
132+ "fmt, expected_marker" ,
133+ [
134+ ("json" , "collections.OrderedDict d" ),
135+ ("ini" , "[collections.OrderedDict d]" ),
136+ ("toml" , '["collections.OrderedDict d"]' ),
137+ ],
138+ ids = ["json" , "ini" , "toml" ],
139+ )
140+ def test_cli_generate_formats (fmt : str , expected_marker : str ) -> None :
141+ """Test generate command with different output formats."""
199142 with patch ("sys.stdout" , new_callable = StringIO ) as mock_stdout :
200143 result = main (
201- ["generate" , "--format" , "toml" , "collections.OrderedDict d" ]
144+ ["generate" , "--format" , fmt , "collections.OrderedDict d" ]
202145 )
203146
204147 assert result == 0
205148 output = mock_stdout .getvalue ()
206- assert '["collections.OrderedDict d"]' in output
149+ assert expected_marker in output
207150
208151
209152def test_cli_generate_multiple_entries () -> None :
@@ -225,43 +168,27 @@ def test_cli_generate_multiple_entries() -> None:
225168 assert "collections.OrderedDict b" in output
226169
227170
228- def test_cli_compile_json_stdin () -> None :
229- """Test compile command with JSON from stdin."""
230- json_input = '{"collections.OrderedDict d": {}}'
231- with patch ("sys.stdin" , StringIO (json_input )):
171+ @pytest .mark .parametrize (
172+ "fmt, input_data" ,
173+ [
174+ ("json" , '{"collections.OrderedDict d": {}}' ),
175+ ("ini" , "[collections.OrderedDict d]\n " ),
176+ ("toml" , '["collections.OrderedDict d"]\n ' ),
177+ ],
178+ ids = ["json" , "ini" , "toml" ],
179+ )
180+ def test_cli_compile_stdin_formats (fmt : str , input_data : str ) -> None :
181+ """Test compile command with different input formats from stdin."""
182+ with patch ("sys.stdin" , StringIO (input_data )):
232183 with patch ("sys.stdout" , new_callable = StringIO ) as mock_stdout :
233- result = main (["compile" , "--format" , "json" , "-" ])
184+ result = main (["compile" , "--format" , fmt , "-" ])
234185
235186 assert result == 0
236187 output = mock_stdout .getvalue ()
237188 assert "class Compiled:" in output
238189 assert "def d(self):" in output
239190
240191
241- def test_cli_compile_ini_stdin () -> None :
242- """Test compile command with INI from stdin."""
243- ini_input = "[collections.OrderedDict d]\n "
244- with patch ("sys.stdin" , StringIO (ini_input )):
245- with patch ("sys.stdout" , new_callable = StringIO ) as mock_stdout :
246- result = main (["compile" , "--format" , "ini" , "-" ])
247-
248- assert result == 0
249- output = mock_stdout .getvalue ()
250- assert "class Compiled:" in output
251-
252-
253- def test_cli_compile_toml_stdin () -> None :
254- """Test compile command with TOML from stdin."""
255- toml_input = '["collections.OrderedDict d"]\n '
256- with patch ("sys.stdin" , StringIO (toml_input )):
257- with patch ("sys.stdout" , new_callable = StringIO ) as mock_stdout :
258- result = main (["compile" , "--format" , "toml" , "-" ])
259-
260- assert result == 0
261- output = mock_stdout .getvalue ()
262- assert "class Compiled:" in output
263-
264-
265192def test_cli_compile_with_aio_flag () -> None :
266193 """Test compile command with --aio flag."""
267194 json_input = '{"collections.OrderedDict d": {}}'
@@ -310,43 +237,26 @@ def test_cli_compile_from_file() -> None:
310237 os .unlink (temp_file )
311238
312239
313- def test_cli_compile_json_parsing_error () -> None :
314- """Test CLI error handling for invalid JSON input."""
315- json_input = '{"invalid": json content}'
316-
317- with patch ("sys.stdin" , StringIO (json_input )):
318- with patch ("sys.stderr" , new_callable = StringIO ) as mock_stderr :
319- result = main (["compile" , "--format" , "json" , "-" ])
320-
321- assert result == 1 # Should return error code
322- stderr_output = mock_stderr .getvalue ()
323- assert "Error parsing JSON content:" in stderr_output
324-
325-
326- def test_cli_compile_toml_parsing_error () -> None :
327- """Test CLI error handling for invalid TOML input."""
328- toml_input = '["invalid toml content'
329-
330- with patch ("sys.stdin" , StringIO (toml_input )):
331- with patch ("sys.stderr" , new_callable = StringIO ) as mock_stderr :
332- result = main (["compile" , "--format" , "toml" , "-" ])
333-
334- assert result == 1 # Should return error code
335- stderr_output = mock_stderr .getvalue ()
336- assert "Error parsing TOML content:" in stderr_output
337-
338-
339- def test_cli_compile_ini_parsing_error () -> None :
340- """Test CLI error handling for invalid INI input."""
341- ini_input = "[invalid section\n key = value"
342-
343- with patch ("sys.stdin" , StringIO (ini_input )):
240+ @pytest .mark .parametrize (
241+ "fmt, input_data, expected_err" ,
242+ [
243+ ("json" , '{"invalid": json content}' , "Error parsing JSON content:" ),
244+ ("toml" , '["invalid toml content' , "Error parsing TOML content:" ),
245+ ("ini" , "[invalid section\n key = value" , "Error parsing INI content:" ),
246+ ],
247+ ids = ["json" , "toml" , "ini" ],
248+ )
249+ def test_cli_compile_parsing_errors (
250+ fmt : str , input_data : str , expected_err : str
251+ ) -> None :
252+ """Test CLI error handling for invalid input formats."""
253+ with patch ("sys.stdin" , StringIO (input_data )):
344254 with patch ("sys.stderr" , new_callable = StringIO ) as mock_stderr :
345- result = main (["compile" , "--format" , "ini" , "-" ])
255+ result = main (["compile" , "--format" , fmt , "-" ])
346256
347- assert result == 1 # Should return error code
257+ assert result == 1
348258 stderr_output = mock_stderr .getvalue ()
349- assert "Error parsing INI content:" in stderr_output
259+ assert expected_err in stderr_output
350260
351261
352262def test_cli_generate_toml_write_error () -> None :
0 commit comments