Debian Codegraph @

pypy/module/_pypyjson/test/apptest__pypyjson.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # spaceconfig = {"usemodules": ["_pypyjson"]}
3 """App-level tests for _pypyjson.make_encoder / W_Encoder."""
4 import _pypyjson
5 import json.encoder as je
6
7 # fast_encode_mode constants (must match interp_encoder.py)
8 FAST_ENCODE_ASCII = 0
9 FAST_ENCODE_UNICODE = 1
10 FAST_ENCODE_CUSTOM = 2
11
12
13 def _make_encoder(ensure_ascii=True, sort_keys=False, skipkeys=False,
14 allow_nan=True):
15 encoder = je.encode_basestring_ascii if ensure_ascii else je.encode_basestring
16 return _pypyjson.make_encoder(
17 None, je.JSONEncoder().default, encoder, None,
18 ': ', ', ', sort_keys, skipkeys, allow_nan)
19
20
21 def test_fast_encode_mode_ascii():
22 # make_encoder must identify encode_basestring_ascii (mode 0).
23 # Non-ASCII chars must be escaped as \uXXXX.
24 enc = _make_encoder(ensure_ascii=True)
25 assert enc.fast_encode_mode == FAST_ENCODE_ASCII
26 assert ''.join(enc(u'\u03b1', 0)) == '"\\u03b1"'
27
28
29 def test_fast_encode_mode_unicode():
30 # make_encoder must identify encode_basestring (mode 1).
31 # Non-ASCII chars must pass through unchanged.
32 enc = _make_encoder(ensure_ascii=False)
33 assert enc.fast_encode_mode == FAST_ENCODE_UNICODE
34 assert ''.join(enc(u'\u03b1', 0)) == u'"\u03b1"'
35
36
37 def test_fast_encode_mode_custom():
38 # An unrecognised callable sets mode 2 and is called per string.
39 called = []
40 def my_encoder(s):
41 called.append(s)
42 return '"custom"'
43 enc = _pypyjson.make_encoder(
44 None, je.JSONEncoder().default, my_encoder, None,
45 ': ', ', ', False, False, True)
46 assert enc.fast_encode_mode == FAST_ENCODE_CUSTOM
47 assert ''.join(enc(u'hello', 0)) == '"custom"'
48 assert called == [u'hello']
49
50
51 def test_encode_truefalse_sort_keys():
52 # Bool keys must sort by their JSON string form ("false" < "true").
53 enc = _make_encoder(sort_keys=True)
54 assert ''.join(enc({True: False, False: True}, 0)) == '{"false": true, "true": false}'
55
56
57 def test_encode_sort_keys_by_value():
58 # Mixed numeric keys must sort by original value, not by JSON string form.
59 # "false" > "6" lexicographically, but False(0) < 2 < 4.0 < 6 numerically.
60 enc = _make_encoder(sort_keys=True)
61 result = ''.join(enc({2: 3.0, 4.0: 5, False: 1, 6: True}, 0))
62 assert result == '{"false": 1, "2": 3.0, "4.0": 5, "6": true}', result
63
64
65 def test_encode_int_subclass_uses_base_int_repr():
66 class Intish(int):
67 def __str__(self):
68 return 'not-json :-('
69
70 def __repr__(self):
71 return 'also not-json :-('
72
73 enc = _make_encoder()
74 val = Intish(7)
75 result = ''.join(enc({'val': val, val: 'key'}, 0))
76 assert result == '{"val": 7, "7": "key"}'
77
78
79 def test_encode_mutated_list():
80 # Mutations to a list during default() must be visible (lazy iteration,
81 # matching CPython's enumerate() behaviour).
82 a = [object()] * 10
83 def crasher(obj):
84 del a[-1]
85 enc = _pypyjson.make_encoder(
86 None, crasher, je.encode_basestring_ascii, None,
87 ': ', ', ', False, False, True)
88 assert ''.join(enc(a, 0)) == '[null, null, null, null, null]'
89
90
91 def test_encode_non_ascii_unicode():
92 # ensure_ascii=False must pass non-ASCII chars through unchanged.
93 enc = _make_encoder(ensure_ascii=False)
94 u = u'\u03b1\u03a9'
95 assert ''.join(enc([u], 0)) == u'["' + u + u'"]'