|
| 1 | +import importlib |
| 2 | +import sys |
1 | 3 | from pathlib import Path |
2 | 4 |
|
3 | 5 | import pytest |
@@ -98,3 +100,45 @@ def test_exceptions(): |
98 | 100 | match=r"Invalid value 'EolStyle\.CRLF' for option comment_policy", |
99 | 101 | ): |
100 | 102 | _ = FracturedJsonOptions(comment_policy=EolStyle.CRLF) |
| 103 | + formatter = Formatter() |
| 104 | + with pytest.raises(TypeError, match="json_text must be a str"): |
| 105 | + formatter.reformat(None) # type: ignore[arg-type] |
| 106 | + |
| 107 | + with pytest.raises(TypeError, match="json_text must be a str"): |
| 108 | + formatter.minify(b"{}") # type: ignore[arg-type] |
| 109 | + |
| 110 | + with pytest.raises(TypeError, match="Must be callable"): |
| 111 | + formatter.string_length_func = 123 # type: ignore[assignment] |
| 112 | + |
| 113 | + |
| 114 | +def test_dll_missing(path_is_file_fails): # noqa: ARG001 |
| 115 | + if "fractured_json" in sys.modules: |
| 116 | + del sys.modules["fractured_json"] |
| 117 | + |
| 118 | + with pytest.raises(FileNotFoundError) as exc: |
| 119 | + importlib.import_module("fractured_json") |
| 120 | + |
| 121 | + assert "FracturedJson.dll not found" in str(exc.value) |
| 122 | + |
| 123 | + |
| 124 | +def test_load_runtime_fails(pythonnet_load_raises): # noqa: ARG001 |
| 125 | + if "fractured_json" in sys.modules: |
| 126 | + del sys.modules["fractured_json"] |
| 127 | + |
| 128 | + with pytest.raises(RuntimeError) as exc: |
| 129 | + importlib.import_module("fractured_json") |
| 130 | + |
| 131 | + assert "Failed to load pythonnet runtime" in str(exc.value) |
| 132 | + assert "coreclr" in str(exc.value) |
| 133 | + |
| 134 | + |
| 135 | +def test_string_length_property(): |
| 136 | + formatter = Formatter() |
| 137 | + |
| 138 | + def double_len(s: str) -> int: |
| 139 | + return len(s) * 2 |
| 140 | + |
| 141 | + formatter.string_length_func = double_len |
| 142 | + getter = formatter.string_length_func |
| 143 | + assert callable(getter) |
| 144 | + assert getter("abc") == 6 |
0 commit comments