| 1 |
|
| 2 |
class AppTest(object): |
| 3 |
spaceconfig = {"objspace.usemodules._pypyjson": True} |
| 4 |
|
| 5 |
def test_check_strategy(self): |
| 6 |
import __pypy__ |
| 7 |
import _pypyjson |
| 8 |
|
| 9 |
d = _pypyjson.loads('{"a": 1}') |
| 10 |
assert __pypy__.strategy(d) == "JsonDictStrategy" |
| 11 |
d = _pypyjson.loads('{}') |
| 12 |
assert __pypy__.strategy(d) == "EmptyDictStrategy" |
| 13 |
|
| 14 |
def test_simple(self): |
| 15 |
import __pypy__ |
| 16 |
import _pypyjson |
| 17 |
|
| 18 |
d = _pypyjson.loads('{"a": 1, "b": "x"}') |
| 19 |
assert len(d) == 2 |
| 20 |
assert d[u"a"] == 1 |
| 21 |
assert d[u"b"] == u"x" |
| 22 |
assert u"c" not in d |
| 23 |
|
| 24 |
d[u"a"] = 5 |
| 25 |
assert d[u"a"] == 5 |
| 26 |
assert __pypy__.strategy(d) == "JsonDictStrategy" |
| 27 |
|
| 28 |
|
| 29 |
assert not 1 in d |
| 30 |
assert __pypy__.strategy(d) == "UnicodeDictStrategy" |
| 31 |
assert len(d) == 2 |
| 32 |
assert d[u"a"] == 5 |
| 33 |
assert d[u"b"] == u"x" |
| 34 |
assert u"c" not in d |
| 35 |
|
| 36 |
def test_setdefault(self): |
| 37 |
import __pypy__ |
| 38 |
import _pypyjson |
| 39 |
|
| 40 |
d = _pypyjson.loads('{"a": 1, "b": "x"}') |
| 41 |
assert d.setdefault(u"a", "blub") == 1 |
| 42 |
d.setdefault(u"x", 23) |
| 43 |
assert __pypy__.strategy(d) == "UnicodeDictStrategy" |
| 44 |
assert len(d) == 3 |
| 45 |
assert d == {u"a": 1, u"b": "x", u"x": 23} |
| 46 |
|
| 47 |
def test_delitem(self): |
| 48 |
import __pypy__ |
| 49 |
import _pypyjson |
| 50 |
|
| 51 |
d = _pypyjson.loads('{"a": 1, "b": "x"}') |
| 52 |
del d[u"a"] |
| 53 |
assert __pypy__.strategy(d) == "UnicodeDictStrategy" |
| 54 |
assert len(d) == 1 |
| 55 |
assert d == {u"b": "x"} |
| 56 |
|
| 57 |
def test_popitem(self): |
| 58 |
import __pypy__ |
| 59 |
import _pypyjson |
| 60 |
|
| 61 |
d = _pypyjson.loads('{"a": 1, "b": "x"}') |
| 62 |
k, v = d.popitem() |
| 63 |
assert __pypy__.strategy(d) == "UnicodeDictStrategy" |
| 64 |
if k == u"a": |
| 65 |
assert v == 1 |
| 66 |
assert len(d) == 1 |
| 67 |
assert d == {u"b": "x"} |
| 68 |
else: |
| 69 |
assert v == u"x" |
| 70 |
assert len(d) == 1 |
| 71 |
assert d == {u"a": 1} |
| 72 |
|
| 73 |
def test_keys_values_items(self): |
| 74 |
import _pypyjson |
| 75 |
|
| 76 |
d = _pypyjson.loads('{"a": 1, "b": "x"}') |
| 77 |
assert list(d.keys()) == [u"a", u"b"] |
| 78 |
assert list(d.values()) == [1, u"x"] |
| 79 |
assert list(d.items()) == [(u"a", 1), (u"b", u"x")] |
| 80 |
|
| 81 |
def test_dict_order_retained_when_switching_strategies(self): |
| 82 |
import _pypyjson |
| 83 |
import __pypy__ |
| 84 |
d = _pypyjson.loads('{"a": 1, "b": "x"}') |
| 85 |
assert list(d) == [u"a", u"b"] |
| 86 |
|
| 87 |
assert not 1 in d |
| 88 |
assert __pypy__.strategy(d) == "UnicodeDictStrategy" |
| 89 |
assert list(d) == [u"a", u"b"] |
| 90 |
|
| 91 |
def test_bug(self): |
| 92 |
import _pypyjson |
| 93 |
a = """ |
| 94 |
{ |
| 95 |
"top": { |
| 96 |
"k": "8", |
| 97 |
"k": "8", |
| 98 |
"boom": 1 |
| 99 |
} |
| 100 |
} |
| 101 |
""" |
| 102 |
d = _pypyjson.loads(a) |
| 103 |
str(d) |
| 104 |
repr(d) |
| 105 |
|
| 106 |
def test_objdict_bug(self): |
| 107 |
import _pypyjson |
| 108 |
a = """{"foo": "bar"}""" |
| 109 |
d = _pypyjson.loads(a) |
| 110 |
d['foo'] = 'x' |
| 111 |
|
| 112 |
class Obj(object): |
| 113 |
pass |
| 114 |
|
| 115 |
x = Obj() |
| 116 |
x.__dict__ = d |
| 117 |
|
| 118 |
x.foo = 'baz' |
| 119 |
|
| 120 |
d = _pypyjson.loads(a) |
| 121 |
x = Obj() |
| 122 |
x.__dict__ = d |
| 123 |
x.foo |
| 124 |
|
| 125 |
|
| 126 |
def test_copy(self): |
| 127 |
import _pypyjson |
| 128 |
d = _pypyjson.loads('{"a": 1}') |
| 129 |
d1 = d.copy() |
| 130 |
assert d1 == {"a": 1} |