@@ -272,6 +272,72 @@ def test_no_strict_option(self, tmp_path):
272272 result = main ()
273273 assert result == 0
274274
275+ def test_check_valid_json_returns_zero_without_output (self , tmp_path ):
276+ """Check mode should validate JSON input and not emit output."""
277+ input_file = tmp_path / "input.json"
278+ input_file .write_text ('{"ok": true}' )
279+
280+ with patch ("sys.stdout" , new_callable = StringIO ) as mock_stdout :
281+ with patch ("sys.stderr" , new_callable = StringIO ) as mock_stderr :
282+ with patch ("sys.argv" , ["toon" , str (input_file ), "--check" ]):
283+ result = main ()
284+ assert result == 0
285+ assert mock_stdout .getvalue () == ""
286+ assert mock_stderr .getvalue () == ""
287+
288+ def test_check_invalid_json_returns_error (self , tmp_path ):
289+ """Check mode should fail for invalid JSON when encoding."""
290+ input_file = tmp_path / "input.json"
291+ input_file .write_text ('{"broken": invalid}' )
292+
293+ with patch ("sys.stdout" , new_callable = StringIO ) as mock_stdout :
294+ with patch ("sys.stderr" , new_callable = StringIO ) as mock_stderr :
295+ with patch ("sys.argv" , ["toon" , str (input_file ), "--check" ]):
296+ result = main ()
297+ assert result == 1
298+ assert mock_stdout .getvalue () == ""
299+ assert "Error during encode" in mock_stderr .getvalue ()
300+
301+ def test_check_valid_toon_returns_zero_without_output (self , tmp_path ):
302+ """Check mode should validate TOON input and not emit output."""
303+ input_file = tmp_path / "input.toon"
304+ input_file .write_text ("name: Alice\n age: 30" )
305+
306+ with patch ("sys.stdout" , new_callable = StringIO ) as mock_stdout :
307+ with patch ("sys.stderr" , new_callable = StringIO ) as mock_stderr :
308+ with patch ("sys.argv" , ["toon" , str (input_file ), "--check" ]):
309+ result = main ()
310+ assert result == 0
311+ assert mock_stdout .getvalue () == ""
312+ assert mock_stderr .getvalue () == ""
313+
314+ def test_check_invalid_toon_returns_error (self , tmp_path ):
315+ """Check mode should fail for invalid TOON when decoding."""
316+ input_file = tmp_path / "input.toon"
317+ input_file .write_text ("items[2]: 1" )
318+
319+ with patch ("sys.stdout" , new_callable = StringIO ) as mock_stdout :
320+ with patch ("sys.stderr" , new_callable = StringIO ) as mock_stderr :
321+ with patch ("sys.argv" , ["toon" , str (input_file ), "--check" ]):
322+ result = main ()
323+ assert result == 1
324+ assert mock_stdout .getvalue () == ""
325+ assert "Error during decode" in mock_stderr .getvalue ()
326+
327+ def test_error_check_and_output_together (self , tmp_path ):
328+ """Check mode cannot be combined with output path."""
329+ input_file = tmp_path / "input.json"
330+ input_file .write_text ('{"test": true}' )
331+ output_file = tmp_path / "output.toon"
332+
333+ with patch ("sys.stderr" , new_callable = StringIO ) as mock_stderr :
334+ with patch (
335+ "sys.argv" , ["toon" , str (input_file ), "--check" , "-o" , str (output_file )]
336+ ):
337+ result = main ()
338+ assert result == 1
339+ assert "Cannot specify both --check and --output" in mock_stderr .getvalue ()
340+
275341 def test_decode_indent_option_affects_output (self , tmp_path ):
276342 """Ensure --indent controls the JSON formatting."""
277343 input_file = tmp_path / "input.toon"
0 commit comments