| 1 |
|
| 2 |
import pytest |
| 3 |
from pypy.module._pypyjson.interp_decoder import JSONDecoder, Terminator, MapBase |
| 4 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 5 |
|
| 6 |
|
| 7 |
class TestJson(object): |
| 8 |
def test_skip_whitespace(self): |
| 9 |
s = ' hello ' |
| 10 |
dec = JSONDecoder(self.space, s) |
| 11 |
assert dec.pos == 0 |
| 12 |
assert dec.skip_whitespace(0) == 3 |
| 13 |
assert dec.skip_whitespace(3) == 3 |
| 14 |
assert dec.skip_whitespace(8) == len(s) |
| 15 |
dec.close() |
| 16 |
|
| 17 |
def test_json_map(self): |
| 18 |
m = Terminator(self.space) |
| 19 |
w_a = self.space.newutf8("a", 1) |
| 20 |
w_b = self.space.newutf8("b", 1) |
| 21 |
w_c = self.space.newutf8("c", 1) |
| 22 |
m1 = m.get_next(w_a, '"a"', 0, 3, m) |
| 23 |
assert m1.w_key == w_a |
| 24 |
assert m1.nextmap_first is None |
| 25 |
assert m1.key_repr == '"a"' |
| 26 |
assert m1.key_repr_cmp('"a": 123', 0) |
| 27 |
assert not m1.key_repr_cmp('b": 123', 0) |
| 28 |
assert m.nextmap_first.w_key == w_a |
| 29 |
|
| 30 |
m2 = m.get_next(w_a, '"a"', 0, 3, m) |
| 31 |
assert m2 is m1 |
| 32 |
|
| 33 |
m3 = m.get_next(w_b, '"b"', 0, 3, m) |
| 34 |
assert m3.w_key == w_b |
| 35 |
assert m3.nextmap_first is None |
| 36 |
assert m3.key_repr == '"b"' |
| 37 |
assert m.nextmap_first is m1 |
| 38 |
|
| 39 |
m4 = m3.get_next(w_c, '"c"', 0, 3, m) |
| 40 |
assert m4.w_key == w_c |
| 41 |
assert m4.nextmap_first is None |
| 42 |
assert m4.key_repr == '"c"' |
| 43 |
assert m3.nextmap_first is m4 |
| 44 |
|
| 45 |
def test_json_map_get_index(self): |
| 46 |
m = Terminator(self.space) |
| 47 |
w_a = self.space.newutf8("a", 1) |
| 48 |
w_b = self.space.newutf8("b", 1) |
| 49 |
w_c = self.space.newutf8("c", 1) |
| 50 |
m1 = m.get_next(w_a, 'a"', 0, 2, m) |
| 51 |
assert m1.get_index(w_a) == 0 |
| 52 |
assert m1.get_index(w_b) == -1 |
| 53 |
|
| 54 |
m2 = m.get_next(w_b, 'b"', 0, 2, m) |
| 55 |
assert m2.get_index(w_b) == 0 |
| 56 |
assert m2.get_index(w_a) == -1 |
| 57 |
|
| 58 |
m3 = m2.get_next(w_c, 'c"', 0, 2, m) |
| 59 |
assert m3.get_index(w_b) == 0 |
| 60 |
assert m3.get_index(w_c) == 1 |
| 61 |
assert m3.get_index(w_a) == -1 |
| 62 |
|
| 63 |
def test_jsonmap_fill_dict(self): |
| 64 |
from collections import OrderedDict |
| 65 |
m = Terminator(self.space) |
| 66 |
space = self.space |
| 67 |
w_a = space.newutf8("a", 1) |
| 68 |
w_b = space.newutf8("b", 1) |
| 69 |
w_c = space.newutf8("c", 1) |
| 70 |
m1 = m.get_next(w_a, 'a"', 0, 2, m) |
| 71 |
m2 = m1.get_next(w_b, 'b"', 0, 2, m) |
| 72 |
m3 = m2.get_next(w_c, 'c"', 0, 2, m) |
| 73 |
d = OrderedDict() |
| 74 |
m3.fill_dict(d, [space.w_None, space.w_None, space.w_None]) |
| 75 |
assert list(d) == [w_a, w_b, w_c] |
| 76 |
|
| 77 |
def test_repeated_key_get_next(self): |
| 78 |
m = Terminator(self.space) |
| 79 |
w_a = self.space.newutf8("a", 1) |
| 80 |
w_b = self.space.newutf8("b", 1) |
| 81 |
w_c = self.space.newutf8("c", 1) |
| 82 |
m1 = m.get_next(w_a, '"a"', 0, 3, m) |
| 83 |
m1 = m1.get_next(w_b, '"b"', 0, 3, m) |
| 84 |
m1 = m1.get_next(w_c, '"c"', 0, 3, m) |
| 85 |
m2 = m1.get_next(w_a, '"a"', 0, 3, m) |
| 86 |
assert m2 is None |
| 87 |
|
| 88 |
|
| 89 |
def test_decode_key_map(self): |
| 90 |
m = Terminator(self.space) |
| 91 |
m_diff = Terminator(self.space) |
| 92 |
for s1 in ["abc", "1001" * 10, u"ä".encode("utf-8")]: |
| 93 |
s = ' "%s" "%s" "%s"' % (s1, s1, s1) |
| 94 |
dec = JSONDecoder(self.space, s) |
| 95 |
assert dec.pos == 0 |
| 96 |
m1 = dec.decode_key_map(dec.skip_whitespace(0), m) |
| 97 |
assert m1.w_key._utf8 == s1 |
| 98 |
assert m1.key_repr == '"%s"' % s1 |
| 99 |
|
| 100 |
|
| 101 |
m2 = dec.decode_key_map(dec.skip_whitespace(dec.pos), m_diff) |
| 102 |
assert m1.w_key is m2.w_key |
| 103 |
|
| 104 |
|
| 105 |
m3 = dec.decode_key_map(dec.skip_whitespace(dec.pos), m_diff) |
| 106 |
assert m3 is m2 |
| 107 |
dec.close() |
| 108 |
|
| 109 |
def test_decode_string_caching(self): |
| 110 |
for s1 in ["abc", u"ä".encode("utf-8")]: |
| 111 |
s = '"%s" "%s" "%s"' % (s1, s1, s1) |
| 112 |
dec = JSONDecoder(self.space, s) |
| 113 |
dec.MIN_SIZE_FOR_STRING_CACHE = 0 |
| 114 |
assert dec.pos == 0 |
| 115 |
w_x = dec.decode_string(1) |
| 116 |
w_y = dec.decode_string(dec.skip_whitespace(dec.pos) + 1) |
| 117 |
assert w_x is not w_y |
| 118 |
|
| 119 |
w_z = dec.decode_string(dec.skip_whitespace(dec.pos) + 1) |
| 120 |
assert w_z is w_y |
| 121 |
dec.close() |
| 122 |
|
| 123 |
def _make_some_maps(self): |
| 124 |
|
| 125 |
|
| 126 |
w_a = self.space.newutf8("a", 1) |
| 127 |
w_b = self.space.newutf8("b", 1) |
| 128 |
w_c = self.space.newutf8("c", 1) |
| 129 |
w_d = self.space.newutf8("d", 1) |
| 130 |
base = Terminator(self.space) |
| 131 |
base.instantiation_count = 6 |
| 132 |
m1 = base.get_next(w_a, 'a"', 0, 2, base) |
| 133 |
m2 = m1.get_next(w_b, 'b"', 0, 2, base) |
| 134 |
m3 = m2.get_next(w_c, 'c"', 0, 2, base) |
| 135 |
m4 = m2.get_next(w_d, 'd"', 0, 2, base) |
| 136 |
return base, m1, m2, m3, m4 |
| 137 |
|
| 138 |
|
| 139 |
def test_fringe_to_useful(self): |
| 140 |
base, m1, m2, m3, m4 = self._make_some_maps() |
| 141 |
base.instantiation_count = 6 |
| 142 |
assert m1.state == MapBase.FRINGE |
| 143 |
m1.instantiation_count = 6 |
| 144 |
|
| 145 |
assert m2.state == MapBase.PRELIMINARY |
| 146 |
m2.instantiation_count = 6 |
| 147 |
|
| 148 |
assert m3.state == MapBase.PRELIMINARY |
| 149 |
m3.instantiation_count = 2 |
| 150 |
assert m2.nextmap_first is m3 |
| 151 |
|
| 152 |
assert m4.state == MapBase.PRELIMINARY |
| 153 |
m4.instantiation_count = 4 |
| 154 |
|
| 155 |
m1.mark_useful(base) |
| 156 |
assert m1.state == MapBase.USEFUL |
| 157 |
assert m2.state == MapBase.USEFUL |
| 158 |
assert m3.state == MapBase.FRINGE |
| 159 |
assert m4.state == MapBase.USEFUL |
| 160 |
assert m2.nextmap_first is m4 |
| 161 |
|
| 162 |
assert m1.number_of_leaves == 2 |
| 163 |
base._check_invariants() |
| 164 |
|
| 165 |
def test_number_of_leaves(self): |
| 166 |
w_x = self.space.newutf8("x", 1) |
| 167 |
base, m1, m2, m3, m4 = self._make_some_maps() |
| 168 |
assert base.number_of_leaves == 2 |
| 169 |
assert m1.number_of_leaves == 2 |
| 170 |
assert m2.number_of_leaves == 2 |
| 171 |
assert m3.number_of_leaves == 1 |
| 172 |
assert m4.number_of_leaves == 1 |
| 173 |
m5 = m2.get_next(w_x, 'x"', 0, 2, base) |
| 174 |
assert base.number_of_leaves == 3 |
| 175 |
assert m1.number_of_leaves == 3 |
| 176 |
assert m2.number_of_leaves == 3 |
| 177 |
assert m5.number_of_leaves == 1 |
| 178 |
|
| 179 |
def test_number_of_leaves_after_mark_blocked(self): |
| 180 |
w_x = self.space.newutf8("x", 1) |
| 181 |
base, m1, m2, m3, m4 = self._make_some_maps() |
| 182 |
m5 = m2.get_next(w_x, 'x"', 0, 2, base) |
| 183 |
assert base.number_of_leaves == 3 |
| 184 |
m2.mark_blocked(base) |
| 185 |
assert base.number_of_leaves == 1 |
| 186 |
|
| 187 |
def test_mark_useful_cleans_fringe(self): |
| 188 |
base, m1, m2, m3, m4 = self._make_some_maps() |
| 189 |
base.instantiation_count = 6 |
| 190 |
assert m1.state == MapBase.FRINGE |
| 191 |
m1.instantiation_count = 6 |
| 192 |
m2.instantiation_count = 6 |
| 193 |
m3.instantiation_count = 2 |
| 194 |
m4.instantiation_count = 4 |
| 195 |
assert base.current_fringe == {m1: None} |
| 196 |
|
| 197 |
m1.mark_useful(base) |
| 198 |
assert base.current_fringe == {m3: None} |
| 199 |
|
| 200 |
def test_cleanup_fringe(self): |
| 201 |
w_a = self.space.newutf8("a", 1) |
| 202 |
w_b = self.space.newutf8("b", 1) |
| 203 |
w_c = self.space.newutf8("c", 1) |
| 204 |
w_d = self.space.newutf8("d", 1) |
| 205 |
base = Terminator(self.space) |
| 206 |
base.instantiation_count = 6 |
| 207 |
m1 = base.get_next(w_a, 'a"', 0, 2, base) |
| 208 |
m2 = base.get_next(w_b, 'b"', 0, 2, base) |
| 209 |
m3 = base.get_next(w_c, 'c"', 0, 2, base) |
| 210 |
m4 = base.get_next(w_d, 'd"', 0, 2, base) |
| 211 |
m5 = m4.get_next(w_a, 'a"', 0, 2, base) |
| 212 |
base.instantiation_count = 7 |
| 213 |
m1.instantiation_count = 2 |
| 214 |
m2.instantiation_count = 2 |
| 215 |
m3.instantiation_count = 2 |
| 216 |
m4.instantiation_count = 1 |
| 217 |
m5.instantiation_count = 1 |
| 218 |
assert base.current_fringe == dict.fromkeys([m1, m2, m3, m4]) |
| 219 |
|
| 220 |
base.cleanup_fringe() |
| 221 |
assert base.current_fringe == dict.fromkeys([m1, m2, m3]) |
| 222 |
assert m4.state == MapBase.BLOCKED |
| 223 |
assert m4.nextmap_first is None |
| 224 |
assert m4.nextmap_all is None |
| 225 |
assert m5.state == MapBase.BLOCKED |
| 226 |
assert m5.nextmap_first is None |
| 227 |
assert m5.nextmap_all is None |
| 228 |
|
| 229 |
def test_deal_with_blocked(self): |
| 230 |
w_a = self.space.newutf8("a", 1) |
| 231 |
w_b = self.space.newutf8("b", 1) |
| 232 |
w_c = self.space.newutf8("c", 1) |
| 233 |
space = self.space |
| 234 |
s = '{"a": 1, "b": 2, "c": 3}' |
| 235 |
dec = JSONDecoder(space, s) |
| 236 |
dec.startmap = base = Terminator(space) |
| 237 |
m1 = base.get_next(w_a, 'a"', 0, 2, base) |
| 238 |
m2 = m1.get_next(w_b, 'b"', 0, 2, base) |
| 239 |
m2.mark_blocked(base) |
| 240 |
w_res = dec.decode_object(1) |
| 241 |
assert space.int_w(space.len(w_res)) == 3 |
| 242 |
assert space.int_w(space.getitem(w_res, w_a)) == 1 |
| 243 |
assert space.int_w(space.getitem(w_res, w_b)) == 2 |
| 244 |
assert space.int_w(space.getitem(w_res, w_c)) == 3 |
| 245 |
dec.close() |
| 246 |
|
| 247 |
def test_deal_with_blocked_number_of_leaves(self): |
| 248 |
w_a = self.space.newutf8("a", 1) |
| 249 |
w_b = self.space.newutf8("b", 1) |
| 250 |
w_x = self.space.newutf8("x", 1) |
| 251 |
w_u = self.space.newutf8("u", 1) |
| 252 |
space = self.space |
| 253 |
base = Terminator(space) |
| 254 |
m1 = base.get_next(w_a, 'a"', 0, 2, base) |
| 255 |
m2 = m1.get_next(w_b, 'b"', 0, 2, base) |
| 256 |
m2.get_next(w_x, 'x"', 0, 2, base) |
| 257 |
m2.get_next(w_u, 'u"', 0, 2, base) |
| 258 |
assert base.number_of_leaves == 2 |
| 259 |
m2.mark_blocked(base) |
| 260 |
assert base.number_of_leaves == 1 |
| 261 |
|
| 262 |
def test_instatiation_count(self): |
| 263 |
m = Terminator(self.space) |
| 264 |
dec = JSONDecoder(self.space, '"abc" "def"') |
| 265 |
m1 = dec.decode_key_map(dec.skip_whitespace(0), m) |
| 266 |
m2 = dec.decode_key_map(dec.skip_whitespace(6), m1) |
| 267 |
m1 = dec.decode_key_map(dec.skip_whitespace(0), m) |
| 268 |
m2 = dec.decode_key_map(dec.skip_whitespace(6), m1) |
| 269 |
m1 = dec.decode_key_map(dec.skip_whitespace(0), m) |
| 270 |
|
| 271 |
assert m1.instantiation_count == 3 |
| 272 |
assert m2.instantiation_count == 2 |
| 273 |
dec.close() |
| 274 |
|
| 275 |
|
| 276 |
class AppTest(object): |
| 277 |
spaceconfig = {"usemodules": ['_pypyjson']} |
| 278 |
|
| 279 |
def test_raise_on_bytes(self): |
| 280 |
import _pypyjson |
| 281 |
raises(TypeError, _pypyjson.loads, b"42") |
| 282 |
|
| 283 |
|
| 284 |
def test_decode_constants(self): |
| 285 |
import _pypyjson |
| 286 |
assert _pypyjson.loads('null') is None |
| 287 |
raises(ValueError, _pypyjson.loads, 'nul') |
| 288 |
raises(ValueError, _pypyjson.loads, 'nu') |
| 289 |
raises(ValueError, _pypyjson.loads, 'n') |
| 290 |
raises(ValueError, _pypyjson.loads, 'nuXX') |
| 291 |
|
| 292 |
assert _pypyjson.loads('true') is True |
| 293 |
raises(ValueError, _pypyjson.loads, 'tru') |
| 294 |
raises(ValueError, _pypyjson.loads, 'tr') |
| 295 |
raises(ValueError, _pypyjson.loads, 't') |
| 296 |
raises(ValueError, _pypyjson.loads, 'trXX') |
| 297 |
|
| 298 |
assert _pypyjson.loads('false') is False |
| 299 |
raises(ValueError, _pypyjson.loads, 'fals') |
| 300 |
raises(ValueError, _pypyjson.loads, 'fal') |
| 301 |
raises(ValueError, _pypyjson.loads, 'fa') |
| 302 |
raises(ValueError, _pypyjson.loads, 'f') |
| 303 |
raises(ValueError, _pypyjson.loads, 'falXX') |
| 304 |
|
| 305 |
|
| 306 |
def test_decode_string(self): |
| 307 |
import _pypyjson |
| 308 |
res = _pypyjson.loads('"hello"') |
| 309 |
assert res == 'hello' |
| 310 |
assert type(res) is str |
| 311 |
|
| 312 |
def test_decode_string_utf8(self): |
| 313 |
import _pypyjson |
| 314 |
s = 'àèìòù' |
| 315 |
raises(ValueError, _pypyjson.loads, '"%s"' % s.encode('utf-8')) |
| 316 |
|
| 317 |
def test_skip_whitespace(self): |
| 318 |
import _pypyjson |
| 319 |
s = ' "hello" ' |
| 320 |
assert _pypyjson.loads(s) == 'hello' |
| 321 |
s = ' "hello" extra' |
| 322 |
raises(ValueError, "_pypyjson.loads(s)") |
| 323 |
|
| 324 |
def test_unterminated_string(self): |
| 325 |
import _pypyjson |
| 326 |
s = '"hello' |
| 327 |
raises(ValueError, "_pypyjson.loads(s)") |
| 328 |
|
| 329 |
def test_escape_sequence(self): |
| 330 |
import _pypyjson |
| 331 |
assert _pypyjson.loads(r'"\\"') == '\\' |
| 332 |
assert _pypyjson.loads(r'"\""') == '"' |
| 333 |
assert _pypyjson.loads(r'"\/"') == '/' |
| 334 |
assert _pypyjson.loads(r'"\b"') == '\b' |
| 335 |
assert _pypyjson.loads(r'"\f"') == '\f' |
| 336 |
assert _pypyjson.loads(r'"\n"') == '\n' |
| 337 |
assert _pypyjson.loads(r'"\r"') == '\r' |
| 338 |
assert _pypyjson.loads(r'"\t"') == '\t' |
| 339 |
|
| 340 |
def test_escape_sequence_in_the_middle(self): |
| 341 |
import _pypyjson |
| 342 |
s = r'"hello\nworld"' |
| 343 |
assert _pypyjson.loads(s) == "hello\nworld" |
| 344 |
|
| 345 |
def test_unterminated_string_after_escape_sequence(self): |
| 346 |
import _pypyjson |
| 347 |
s = r'"hello\nworld' |
| 348 |
raises(ValueError, "_pypyjson.loads(s)") |
| 349 |
|
| 350 |
def test_escape_sequence_unicode(self): |
| 351 |
import _pypyjson |
| 352 |
s = r'"\u1234"' |
| 353 |
assert _pypyjson.loads(s) == '\u1234' |
| 354 |
|
| 355 |
def test_escape_sequence_mixed_with_unicode(self): |
| 356 |
import _pypyjson |
| 357 |
assert _pypyjson.loads(r'"abc\\' + u'ä"') == u'abc\\ä' |
| 358 |
assert _pypyjson.loads(r'"abc\"' + u'ä"') == u'abc"ä' |
| 359 |
assert _pypyjson.loads(r'"def\u1234' + u'ä"') == u'def\u1234ä' |
| 360 |
|
| 361 |
def test_invalid_utf_8(self): |
| 362 |
import _pypyjson |
| 363 |
s = '"\xe0"' |
| 364 |
assert _pypyjson.loads(s) == 'à' |
| 365 |
|
| 366 |
def test_decode_numeric(self): |
| 367 |
import sys |
| 368 |
import _pypyjson |
| 369 |
def check(s, val): |
| 370 |
res = _pypyjson.loads(s) |
| 371 |
assert type(res) is type(val) |
| 372 |
assert res == val |
| 373 |
|
| 374 |
check('42', 42) |
| 375 |
check('-42', -42) |
| 376 |
check('42.123', 42.123) |
| 377 |
check('42E0', 42.0) |
| 378 |
check('42E3', 42000.0) |
| 379 |
check('42E-1', 4.2) |
| 380 |
check('42E+1', 420.0) |
| 381 |
check('42.123E3', 42123.0) |
| 382 |
check('0', 0) |
| 383 |
check('-0', 0) |
| 384 |
check('0.123', 0.123) |
| 385 |
check('0E3', 0.0) |
| 386 |
check('5E0001', 50.0) |
| 387 |
check(str(1 << 32), 1 << 32) |
| 388 |
check(str(1 << 64), 1 << 64) |
| 389 |
|
| 390 |
x = str(sys.maxsize+1) + '.123' |
| 391 |
check(x, float(x)) |
| 392 |
x = str(sys.maxsize+1) + 'E1' |
| 393 |
check(x, float(x)) |
| 394 |
x = str(sys.maxsize+1) + 'E-1' |
| 395 |
check(x, float(x)) |
| 396 |
|
| 397 |
check('1E400', float('inf')) |
| 398 |
|
| 399 |
check('Infinity', float('inf')) |
| 400 |
check('-Infinity', float('-inf')) |
| 401 |
|
| 402 |
def test_nan(self): |
| 403 |
import math |
| 404 |
import _pypyjson |
| 405 |
res = _pypyjson.loads('NaN') |
| 406 |
assert math.isnan(res) |
| 407 |
|
| 408 |
def test_decode_numeric_invalid(self): |
| 409 |
import _pypyjson |
| 410 |
def error(s): |
| 411 |
raises(ValueError, _pypyjson.loads, s) |
| 412 |
|
| 413 |
error(' 42 abc') |
| 414 |
error('.123') |
| 415 |
error('+123') |
| 416 |
error('12.') |
| 417 |
error('12.-3') |
| 418 |
error('12E') |
| 419 |
error('12E-') |
| 420 |
error('0123') |
| 421 |
|
| 422 |
def test_decode_object(self): |
| 423 |
import _pypyjson |
| 424 |
assert _pypyjson.loads('{}') == {} |
| 425 |
assert _pypyjson.loads('{ }') == {} |
| 426 |
|
| 427 |
s = '{"hello": "world", "aaa": "bbb"}' |
| 428 |
assert _pypyjson.loads(s) == {'hello': 'world', |
| 429 |
'aaa': 'bbb'} |
| 430 |
assert _pypyjson.loads(s) == {'hello': 'world', |
| 431 |
'aaa': 'bbb'} |
| 432 |
raises(ValueError, _pypyjson.loads, '{"key"') |
| 433 |
raises(ValueError, _pypyjson.loads, '{"key": 42') |
| 434 |
|
| 435 |
assert _pypyjson.loads('{"neighborhood": ""}') == { |
| 436 |
"neighborhood": ""} |
| 437 |
|
| 438 |
def test_decode_object_nonstring_key(self): |
| 439 |
import _pypyjson |
| 440 |
raises(ValueError, "_pypyjson.loads('{42: 43}')") |
| 441 |
|
| 442 |
def test_decode_array(self): |
| 443 |
import _pypyjson |
| 444 |
assert _pypyjson.loads('[]') == [] |
| 445 |
assert _pypyjson.loads('[ ]') == [] |
| 446 |
assert _pypyjson.loads('[1]') == [1] |
| 447 |
assert _pypyjson.loads('[1, 2]') == [1, 2] |
| 448 |
raises(ValueError, "_pypyjson.loads('[1: 2]')") |
| 449 |
raises(ValueError, "_pypyjson.loads('[1, 2')") |
| 450 |
raises(ValueError, """_pypyjson.loads('["extra comma",]')""") |
| 451 |
|
| 452 |
def test_unicode_surrogate_pair(self): |
| 453 |
import _pypyjson |
| 454 |
expected = 'z\U0001d120x' |
| 455 |
res = _pypyjson.loads('"z\\ud834\\udd20x"') |
| 456 |
assert res == expected |
| 457 |
|
| 458 |
def test_unicode_not_a_surrogate_pair(self): |
| 459 |
import _pypyjson |
| 460 |
res = _pypyjson.loads('"z\\ud800\\ud800x"') |
| 461 |
assert list(res) == [u'z', u'\ud800', u'\ud800', u'x'] |
| 462 |
res = _pypyjson.loads('"z\\udbff\\uffffx"') |
| 463 |
assert list(res) == [u'z', u'\udbff', u'\uffff', u'x'] |
| 464 |
res = _pypyjson.loads('"z\\ud800\\ud834\\udd20x"') |
| 465 |
assert res == u'z\ud800\U0001d120x' |
| 466 |
res = _pypyjson.loads('"z\\udc00\\udc00x"') |
| 467 |
assert list(res) == [u'z', u'\udc00', u'\udc00', u'x'] |
| 468 |
|
| 469 |
def test_lone_surrogate(self): |
| 470 |
import _pypyjson |
| 471 |
json = '{"a":"\\uD83D"}' |
| 472 |
res = _pypyjson.loads(json) |
| 473 |
assert res == {u'a': u'\ud83d'} |
| 474 |
|
| 475 |
def test_cache_keys(self): |
| 476 |
import _pypyjson |
| 477 |
json = '[{"a": 1}, {"a": 2}]' |
| 478 |
res = _pypyjson.loads(json) |
| 479 |
assert res == [{u'a': 1}, {u'a': 2}] |
| 480 |
|
| 481 |
def test_huge_map(self): |
| 482 |
import _pypyjson |
| 483 |
import __pypy__ |
| 484 |
s = '{' + ",".join('"%s": %s' % (i, i) for i in range(200)) + '}' |
| 485 |
res = _pypyjson.loads(s) |
| 486 |
assert len(res) == 200 |
| 487 |
assert __pypy__.strategy(res) == "UnicodeDictStrategy" |
| 488 |
|
| 489 |
def test_tab_in_string_should_fail(self): |
| 490 |
import _pypyjson |
| 491 |
|
| 492 |
s = '["\ttab\tcharacter\tin\tstring\t"]' |
| 493 |
raises(ValueError, "_pypyjson.loads(s)") |
| 494 |
|
| 495 |
def test_raw_encode_basestring_ascii(self): |
| 496 |
import _pypyjson |
| 497 |
def check(s): |
| 498 |
s = _pypyjson.raw_encode_basestring_ascii(s) |
| 499 |
assert type(s) is str |
| 500 |
return s |
| 501 |
assert check("") == "" |
| 502 |
assert check(u"") == "" |
| 503 |
assert check("abc ") == "abc " |
| 504 |
assert check(u"abc ") == "abc " |
| 505 |
assert check("\xc0") == "\\u00c0" |
| 506 |
assert check("\xc2\x84") == "\\u00c2\\u0084" |
| 507 |
assert check(u"\ud808\udf45") == "\\ud808\\udf45" |
| 508 |
assert check(u"\U00012345") == "\\ud808\\udf45" |
| 509 |
assert check("a\"c") == "a\\\"c" |
| 510 |
assert check("\\\"\b\f\n\r\t") == '\\\\\\"\\b\\f\\n\\r\\t' |
| 511 |
assert check("\x07") == "\\u0007" |
| 512 |
with raises(TypeError): |
| 513 |
check(b'abc') |
| 514 |
|
| 515 |
def test_error_position(self): |
| 516 |
import _pypyjson |
| 517 |
test_cases = [ |
| 518 |
('[,', "Unexpected ','", 1), |
| 519 |
('{"spam":[}', "Unexpected '}'", 9), |
| 520 |
('[42:', "Unexpected ':' when decoding array", 3), |
| 521 |
('[42 "spam"', "Unexpected '\"' when decoding array", 4), |
| 522 |
('[42,]', "Unexpected ']'", 4), |
| 523 |
('{"spam":[42}', "Unexpected '}' when decoding array", 11), |
| 524 |
('["]', 'Unterminated string starting at', 1), |
| 525 |
('["spam":', "Unexpected ':' when decoding array", 7), |
| 526 |
('[{]', "Key name must be string at char", 2), |
| 527 |
('{"a": 1 "b": 2}', "Unexpected '\"' when decoding object", 8), |
| 528 |
('"\\X"', "Invalid \\escape: X (char 1)", 1), |
| 529 |
('"\\ "', "Invalid \\escape: (char 1)", 1), |
| 530 |
('"\\', "Invalid \\escape: (char 1)", 1), |
| 531 |
] |
| 532 |
for inputtext, errmsg, errpos in test_cases: |
| 533 |
exc = raises(ValueError, _pypyjson.loads, inputtext) |
| 534 |
print(exc.value.args, (errmsg, inputtext, errpos)) |
| 535 |
assert exc.value.args == (errmsg, inputtext, errpos) |
| 536 |
|
| 537 |
def test_keys_reuse(self): |
| 538 |
import _pypyjson |
| 539 |
s = '[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]' |
| 540 |
rval = _pypyjson.loads(s) |
| 541 |
(a, b), (c, d) = sorted(rval[0]), sorted(rval[1]) |
| 542 |
assert a is c |
| 543 |
assert b is d |
| 544 |
|
| 545 |
def test_custom_error_class(self): |
| 546 |
import _pypyjson |
| 547 |
class MyError(Exception): |
| 548 |
pass |
| 549 |
exc = raises(MyError, _pypyjson.loads, 'nul', MyError) |
| 550 |
assert exc.value.args == ('Error when decoding null', 'nul', 1) |
| 551 |
|
| 552 |
def test_repeated_key(self): |
| 553 |
import _pypyjson |
| 554 |
a = '{"abc": "4", "k": 1, "k": 2}' |
| 555 |
d = _pypyjson.loads(a) |
| 556 |
assert d == {u"abc": u"4", u"k": 2} |
| 557 |
a = '{"abc": "4", "k": 1, "k": 1.5, "c": null, "k": 2}' |
| 558 |
d = _pypyjson.loads(a) |
| 559 |
assert d == {u"abc": u"4", u"c": None, u"k": 2} |
| 560 |
|
| 561 |
|