File: test_jsondict.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (130 lines) | stat: -rw-r--r-- 3,407 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

class AppTest(object):
    spaceconfig = {"objspace.usemodules._pypyjson": True}

    def test_check_strategy(self):
        import __pypy__
        import _pypyjson

        d = _pypyjson.loads('{"a": 1}')
        assert __pypy__.strategy(d) == "JsonDictStrategy"
        d = _pypyjson.loads('{}')
        assert __pypy__.strategy(d) == "EmptyDictStrategy"

    def test_simple(self):
        import __pypy__
        import _pypyjson

        d = _pypyjson.loads('{"a": 1, "b": "x"}')
        assert len(d) == 2
        assert d[u"a"] == 1
        assert d[u"b"] == u"x"
        assert u"c" not in d

        d[u"a"] = 5
        assert d[u"a"] == 5
        assert __pypy__.strategy(d) == "JsonDictStrategy"

        # devolve it
        assert not 1 in d
        assert __pypy__.strategy(d) == "UnicodeDictStrategy"
        assert len(d) == 2
        assert d[u"a"] == 5
        assert d[u"b"] == u"x"
        assert u"c" not in d

    def test_setdefault(self):
        import __pypy__
        import _pypyjson

        d = _pypyjson.loads('{"a": 1, "b": "x"}')
        assert d.setdefault(u"a", "blub") == 1
        d.setdefault(u"x", 23)
        assert __pypy__.strategy(d) == "UnicodeDictStrategy"
        assert len(d) == 3
        assert d == {u"a": 1, u"b": "x", u"x": 23}

    def test_delitem(self):
        import __pypy__
        import _pypyjson

        d = _pypyjson.loads('{"a": 1, "b": "x"}')
        del d[u"a"]
        assert __pypy__.strategy(d) == "UnicodeDictStrategy"
        assert len(d) == 1
        assert d == {u"b": "x"}

    def test_popitem(self):
        import __pypy__
        import _pypyjson

        d = _pypyjson.loads('{"a": 1, "b": "x"}')
        k, v = d.popitem()
        assert __pypy__.strategy(d) == "UnicodeDictStrategy"
        if k == u"a":
            assert v == 1
            assert len(d) == 1
            assert d == {u"b": "x"}
        else:
            assert v == u"x"
            assert len(d) == 1
            assert d == {u"a": 1}

    def test_keys_values_items(self):
        import _pypyjson

        d = _pypyjson.loads('{"a": 1, "b": "x"}')
        assert list(d.keys()) == [u"a", u"b"]
        assert list(d.values()) == [1, u"x"]
        assert list(d.items()) == [(u"a", 1), (u"b", u"x")]

    def test_dict_order_retained_when_switching_strategies(self):
        import _pypyjson
        import __pypy__
        d = _pypyjson.loads('{"a": 1, "b": "x"}')
        assert list(d) == [u"a", u"b"]
        # devolve
        assert not 1 in d
        assert __pypy__.strategy(d) == "UnicodeDictStrategy"
        assert list(d) == [u"a", u"b"]

    def test_bug(self):
        import _pypyjson
        a =  """
        {
          "top": {
            "k": "8",
            "k": "8",
            "boom": 1
          }
        }
        """
        d = _pypyjson.loads(a)
        str(d)
        repr(d)

    def test_objdict_bug(self):
        import _pypyjson
        a = """{"foo": "bar"}"""
        d = _pypyjson.loads(a)
        d['foo'] = 'x'

        class Obj(object):
            pass

        x = Obj()
        x.__dict__ = d

        x.foo = 'baz'  # used to segfault on pypy3

        d = _pypyjson.loads(a)
        x = Obj()
        x.__dict__ = d
        x.foo # used to segfault on pypy3


    def test_copy(self):
        import _pypyjson
        d = _pypyjson.loads('{"a": 1}')
        d1 = d.copy()
        assert d1 == {"a": 1}