Skip to content

Commit c7bdbcf

Browse files
committed
Fix exception handling for unterminated strings and linting issues
- Fix bug where ToonDecodeError from decode_object was incorrectly caught when checking if input is a key-value pair, causing unterminated strings to be treated as root primitives instead of raising an error - Refactor to use try/except/else pattern consistent with codebase style - Fix linting issues in test_api.py: - Fix import sorting - Fix line length violations - Remove trailing whitespace from blank lines - Apply code formatting
1 parent f4e54ed commit c7bdbcf

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

src/toon_format/decoder.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,16 @@ def decode(input_str: str, options: Optional[DecodeOptions] = None) -> Union[Jso
315315
# Check if it's not a key-value line
316316
try:
317317
split_key_value(line_content)
318-
# It's a key-value, so root object
319-
result = decode_object(lines, 0, 0, strict)
320318
except ToonDecodeError:
321319
# Not a key-value, check if it's a header
322320
if header_info is None:
323321
# Single primitive
324322
result = parse_primitive(line_content)
325323
else:
326324
result = decode_object(lines, 0, 0, strict)
325+
else:
326+
# It's a key-value, so root object
327+
result = decode_object(lines, 0, 0, strict)
327328
else:
328329
# Otherwise, root object
329330
result = decode_object(lines, 0, 0, strict)

tests/test_api.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
import pytest
1717

18+
from tests.test_spec_fixtures import get_all_decode_fixtures
1819
from toon_format import ToonDecodeError, decode, encode
1920
from toon_format.types import DecodeOptions, EncodeOptions
20-
from tests.test_spec_fixtures import get_all_decode_fixtures
2121

2222

2323
class TestEncodeAPI:
@@ -293,38 +293,43 @@ def test_roundtrip_with_length_marker(self):
293293

294294

295295
# TODO: Add targeted unit tests for decode()'s json_indent feature here.
296-
# See Issue #10. For now, comprehensive tests are in TestDecodeJSONIndentationWithSpecFixtures.
297-
@pytest.mark.skip(reason="Placeholder for targeted decode() JSON indentation tests. See TODO above.")
296+
# See Issue #10. For now, comprehensive tests are in
297+
# TestDecodeJSONIndentationWithSpecFixtures.
298+
@pytest.mark.skip(
299+
reason="Placeholder for targeted decode() JSON indentation tests. See TODO above."
300+
)
298301
class TestDecodeJSONIndentation:
299302
"""Test decode() JSON indentation feature (Issue #10).
300-
301-
Comprehensive tests for the json_indent feature are in TestDecodeJSONIndentationWithSpecFixtures,
302-
which validates against official TOON specification fixtures.
303+
304+
Comprehensive tests for the json_indent feature are in
305+
TestDecodeJSONIndentationWithSpecFixtures, which validates against official
306+
TOON specification fixtures.
303307
"""
308+
304309
pass
305310

306311

307312
def _get_sample_decode_fixtures() -> List[tuple]:
308313
"""Get a sample of decode test cases from fixture files for json_indent testing.
309-
314+
310315
Selects a few representative test cases from the official TOON spec fixtures.
311316
"""
312317
all_fixtures = get_all_decode_fixtures()
313-
318+
314319
# Select a few representative test cases from different fixture categories
315320
selected_files = {"primitives.json", "arrays-primitive.json", "objects.json"}
316321
test_cases = []
317-
322+
318323
for test_id, test_data, fixture_name in all_fixtures:
319324
if f"{fixture_name}.json" in selected_files and len(test_cases) < 9:
320325
test_cases.append((test_id, test_data))
321-
326+
322327
return test_cases
323328

324329

325330
class TestDecodeJSONIndentationWithSpecFixtures:
326331
"""Test json_indent feature against spec fixtures to ensure comprehensive coverage.
327-
332+
328333
These tests validate that the json_indent feature works correctly with various
329334
TOON format patterns defined in the official specification fixtures.
330335
"""
@@ -358,9 +363,7 @@ def test_json_indent_produces_valid_json(self, test_id: str, test_data: Dict[str
358363
)
359364

360365
@pytest.mark.parametrize("test_id,test_data", _get_sample_decode_fixtures())
361-
def test_json_indent_with_different_indent_sizes(
362-
self, test_id: str, test_data: Dict[str, Any]
363-
):
366+
def test_json_indent_with_different_indent_sizes(self, test_id: str, test_data: Dict[str, Any]):
364367
"""Verify that json_indent respects different indent sizes."""
365368
input_str = test_data["input"]
366369
expected = test_data.get("expected")
@@ -384,7 +387,9 @@ def test_json_indent_with_different_indent_sizes(
384387
if "\n" in result_2 and "\n" in result_4:
385388
# Multi-line results should differ in formatting
386389
# (indentation characters will be different)
387-
assert result_2 != result_4, "Different indent sizes should produce different formatting"
390+
assert result_2 != result_4, (
391+
"Different indent sizes should produce different formatting"
392+
)
388393

389394
def test_json_indent_consistency_with_plain_decode(self):
390395
"""Verify that json_indent=None produces same data as plain decode."""

0 commit comments

Comments
 (0)