| 1 |
import pytest |
| 2 |
import sys |
| 3 |
|
| 4 |
from DisplayCAL import demjson_compat |
| 5 |
from DisplayCAL.demjson_compat import decode, encode |
| 6 |
|
| 7 |
|
| 8 |
@pytest.fixture(scope="function") |
| 9 |
def set_debug_mode(): |
| 10 |
# Set debug mode for the duration of the test |
| 11 |
original_debug = demjson_compat.DEBUG |
| 12 |
demjson_compat.DEBUG = True |
| 13 |
yield |
| 14 |
# Restore original debug mode after the test |
| 15 |
demjson_compat.DEBUG = original_debug |
| 16 |
|
| 17 |
|
| 18 |
def test_decode_simple_json(): |
| 19 |
assert decode('{"a": 1, "b": 2}') == {"a": 1, "b": 2} |
| 20 |
|
| 21 |
|
| 22 |
def test_decode_simple_json_with_debug(capsys, set_debug_mode): |
| 23 |
assert decode('{"a": 1, "b": 2}') == {"a": 1, "b": 2} |
| 24 |
captured = capsys.readouterr() |
| 25 |
assert 'JSON: {"a": 1, "b": 2}\n' in captured.out |
| 26 |
|
| 27 |
|
| 28 |
def test_decode_with_single_line_comment(): |
| 29 |
json_with_comment = """ |
| 30 |
{ |
| 31 |
// this is a comment |
| 32 |
"a": 1, |
| 33 |
"b": 2 // another comment |
| 34 |
} |
| 35 |
""" |
| 36 |
assert decode(json_with_comment) == {"a": 1, "b": 2} |
| 37 |
|
| 38 |
|
| 39 |
def test_decode_with_multiline_comment(): |
| 40 |
json_with_comment = """ |
| 41 |
{ |
| 42 |
/* this is a |
| 43 |
multiline comment */ |
| 44 |
"a": 1, |
| 45 |
"b": 2 |
| 46 |
} |
| 47 |
""" |
| 48 |
assert decode(json_with_comment) == {"a": 1, "b": 2} |
| 49 |
|
| 50 |
|
| 51 |
def test_decode_with_comment_inside_string(): |
| 52 |
json_with_comment = """ |
| 53 |
{ |
| 54 |
"a": "// not a comment", |
| 55 |
"b": 2 |
| 56 |
} |
| 57 |
""" |
| 58 |
assert decode(json_with_comment) == {"a": "// not a comment", "b": 2} |
| 59 |
|
| 60 |
|
| 61 |
def test_decode_with_multiline_comment_inside_string(): |
| 62 |
json_with_comment = """ |
| 63 |
{ |
| 64 |
"a": "/* not a comment */", |
| 65 |
"b": 2 |
| 66 |
} |
| 67 |
""" |
| 68 |
assert decode(json_with_comment) == {"a": "/* not a comment */", "b": 2} |
| 69 |
|
| 70 |
|
| 71 |
def test_decode_with_escape_characters(): |
| 72 |
json_with_comment = """ |
| 73 |
{ |
| 74 |
"a": "value with new line\\n", |
| 75 |
"b": 2 // another comment |
| 76 |
} |
| 77 |
""" |
| 78 |
assert decode(json_with_comment) == {"a": "value with new line\n", "b": 2} |
| 79 |
|
| 80 |
|
| 81 |
def test_decode_with_strict_true(): |
| 82 |
# strict=True disables comment removal, so comments should cause an error |
| 83 |
json_with_comment = """ |
| 84 |
{ |
| 85 |
// comment |
| 86 |
"a": 1 |
| 87 |
} |
| 88 |
""" |
| 89 |
with pytest.raises(Exception): |
| 90 |
decode(json_with_comment, strict=True) |
| 91 |
|
| 92 |
|
| 93 |
def test_decode_with_encoding_utf8(): |
| 94 |
json_str = '{"a": "ü"}' |
| 95 |
# Should decode correctly with or without encoding specified |
| 96 |
assert decode(json_str, encoding="utf-8") == {"a": "ü"} |
| 97 |
|
| 98 |
|
| 99 |
def test_decode_ignores_extra_kwargs(): |
| 100 |
assert decode('{"a": 1}', foo="bar") == {"a": 1} |
| 101 |
|
| 102 |
|
| 103 |
def test_decode_empty_object(): |
| 104 |
assert decode("{}") == {} |
| 105 |
|
| 106 |
|
| 107 |
def test_decode_array(): |
| 108 |
assert decode("[1, 2, 3]") == [1, 2, 3] |
| 109 |
|
| 110 |
|
| 111 |
def test_decode_with_nested_comments(): |
| 112 |
json_with_comment = """ |
| 113 |
{ |
| 114 |
/* outer comment |
| 115 |
// inner single-line |
| 116 |
still in comment */ |
| 117 |
"a": 1 |
| 118 |
} |
| 119 |
""" |
| 120 |
assert decode(json_with_comment) == {"a": 1} |
| 121 |
|
| 122 |
|
| 123 |
def test_encode_simple_object(): |
| 124 |
obj = {"a": 1, "b": 2} |
| 125 |
result = encode(obj) |
| 126 |
# Compact encoding, keys order may vary |
| 127 |
assert result.replace(" ", "") in ('{"a":1,"b":2}', '{"b":2,"a":1}') |
| 128 |
|
| 129 |
|
| 130 |
def test_encode_pretty_printed(): |
| 131 |
obj = {"a": 1, "b": 2} |
| 132 |
result = encode(obj, compactly=False) |
| 133 |
# Should contain newlines and indentation |
| 134 |
assert "\n" in result and " " in result |
| 135 |
assert '"a": 1' in result or '"b": 2' in result |
| 136 |
|
| 137 |
|
| 138 |
def test_encode_escape_unicode_true(): |
| 139 |
obj = {"a": "ü"} |
| 140 |
result = encode(obj, escape_unicode=True) |
| 141 |
assert r"\u00fc" in result |
| 142 |
|
| 143 |
|
| 144 |
def test_encode_escape_unicode_false(): |
| 145 |
obj = {"a": "ü"} |
| 146 |
result = encode(obj, escape_unicode=False) |
| 147 |
assert "ü" in result |
| 148 |
|
| 149 |
|
| 150 |
def test_encode_with_encoding_utf8(): |
| 151 |
obj = {"a": "ü"} |
| 152 |
result = encode(obj, encoding="utf-8") |
| 153 |
# Should be a str containing the unicode character or its escape |
| 154 |
assert isinstance(result, str) |
| 155 |
assert "ü" in result or r"\u00fc" in result |
| 156 |
|
| 157 |
|
| 158 |
def test_encode_compactly_true_and_false(): |
| 159 |
obj = {"a": 1, "b": 2} |
| 160 |
compact = encode(obj, compactly=True) |
| 161 |
pretty = encode(obj, compactly=False) |
| 162 |
assert len(compact) < len(pretty) |
| 163 |
|
| 164 |
|
| 165 |
def test_encode_array(): |
| 166 |
arr = [1, 2, 3] |
| 167 |
result = encode(arr) |
| 168 |
assert result == "[1,2,3]" |
| 169 |
|
| 170 |
|
| 171 |
def test_encode_nested_object(): |
| 172 |
obj = {"a": {"b": [1, 2, 3]}, "c": "test"} |
| 173 |
result = encode(obj) |
| 174 |
assert '"b":[1,2,3]' in result.replace(" ", "") |
| 175 |
assert '"c":"test"' in result.replace(" ", "") |
| 176 |
|
| 177 |
|
| 178 |
def test_encode_ignores_strict(): |
| 179 |
obj = {"a": 1} |
| 180 |
result = encode(obj, strict=True) |
| 181 |
assert result.replace(" ", "") in ('{"a":1}',) |
| 182 |
|
| 183 |
|
| 184 |
def test_encode_empty_object(): |
| 185 |
assert encode({}) == "{}" |
| 186 |
|
| 187 |
|
| 188 |
def test_encode_empty_array(): |
| 189 |
assert encode([]) == "[]" |