|
17 | 17 | """ |
18 | 18 |
|
19 | 19 | from decimal import Decimal |
| 20 | +from pathlib import Path, PurePosixPath, PureWindowsPath |
20 | 21 |
|
21 | 22 | from toon_format import decode, encode |
22 | 23 |
|
@@ -416,3 +417,66 @@ def test_roundtrip_numeric_precision(self): |
416 | 417 | # All numbers should round-trip with fidelity |
417 | 418 | for key, value in original.items(): |
418 | 419 | assert decoded[key] == value, f"Mismatch for {key}: {decoded[key]} != {value}" |
| 420 | + |
| 421 | + |
| 422 | +class TestPathNormalization: |
| 423 | + """Test pathlib.Path normalization to strings.""" |
| 424 | + |
| 425 | + def test_path_to_string(self): |
| 426 | + """pathlib.Path should be converted to string.""" |
| 427 | + data = {"file": Path("/tmp/test.txt")} |
| 428 | + result = encode(data) |
| 429 | + decoded = decode(result) |
| 430 | + |
| 431 | + assert decoded["file"] == "/tmp/test.txt" |
| 432 | + |
| 433 | + def test_relative_path(self): |
| 434 | + """Relative paths should be preserved.""" |
| 435 | + data = {"rel": Path("./relative/path.txt")} |
| 436 | + result = encode(data) |
| 437 | + decoded = decode(result) |
| 438 | + |
| 439 | + # Path normalization may vary, but should be a string |
| 440 | + assert isinstance(decoded["rel"], str) |
| 441 | + assert "relative" in decoded["rel"] |
| 442 | + assert "path.txt" in decoded["rel"] |
| 443 | + |
| 444 | + def test_pure_path(self): |
| 445 | + """PurePath objects should also be normalized.""" |
| 446 | + data = { |
| 447 | + "posix": PurePosixPath("/usr/bin/python"), |
| 448 | + "windows": PureWindowsPath("C:\\Windows\\System32"), |
| 449 | + } |
| 450 | + result = encode(data) |
| 451 | + decoded = decode(result) |
| 452 | + |
| 453 | + assert decoded["posix"] == "/usr/bin/python" |
| 454 | + assert decoded["windows"] == "C:\\Windows\\System32" |
| 455 | + |
| 456 | + def test_path_in_array(self): |
| 457 | + """Path objects in arrays should be normalized.""" |
| 458 | + data = {"paths": [Path("/tmp/a"), Path("/tmp/b"), Path("/tmp/c")]} |
| 459 | + result = encode(data) |
| 460 | + decoded = decode(result) |
| 461 | + |
| 462 | + assert decoded["paths"] == ["/tmp/a", "/tmp/b", "/tmp/c"] |
| 463 | + |
| 464 | + def test_path_in_nested_structure(self): |
| 465 | + """Path objects in nested structures should be normalized.""" |
| 466 | + data = { |
| 467 | + "project": { |
| 468 | + "root": Path("/home/user/project"), |
| 469 | + "src": Path("/home/user/project/src"), |
| 470 | + "files": [ |
| 471 | + {"name": "main.py", "path": Path("/home/user/project/src/main.py")}, |
| 472 | + {"name": "test.py", "path": Path("/home/user/project/src/test.py")}, |
| 473 | + ], |
| 474 | + } |
| 475 | + } |
| 476 | + result = encode(data) |
| 477 | + decoded = decode(result) |
| 478 | + |
| 479 | + assert decoded["project"]["root"] == "/home/user/project" |
| 480 | + assert decoded["project"]["src"] == "/home/user/project/src" |
| 481 | + assert decoded["project"]["files"][0]["path"] == "/home/user/project/src/main.py" |
| 482 | + assert decoded["project"]["files"][1]["path"] == "/home/user/project/src/test.py" |
0 commit comments