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
|
class AppTestBasic:
spaceconfig = dict(usemodules=['_collections'])
def test_basics(self):
from _collections import defaultdict
d = defaultdict(list)
assert d.default_factory is list
assert defaultdict.default_factory.__get__(d) is list
l = d[5]
d[5].append(42)
d[5].append(43)
assert l == [42, 43]
l2 = []
d[5] = l2
d[5].append(44)
assert l == [42, 43] and l2 == [44]
def test_module(self):
from _collections import defaultdict
assert repr(defaultdict) in (
"<class 'collections.defaultdict'>", # on PyPy
"<type 'collections.defaultdict'>") # on CPython
def test_keyerror_without_factory(self):
from _collections import defaultdict
for d1 in [defaultdict(), defaultdict(None)]:
for key in ['foo', (1,)]:
try:
d1[key]
except KeyError as err:
assert err.args[0] == key
else:
assert 0, "expected KeyError"
def test_noncallable(self):
from _collections import defaultdict
raises(TypeError, defaultdict, [('a', 5)])
d = defaultdict(None, [('a', 5)])
assert list(d.items()) == [('a', 5)]
def test_kwds(self):
from _collections import defaultdict
d = defaultdict(default_factory=5)
assert list(d.keys()) == ['default_factory']
def test_copy(self):
import _collections
def f():
return 42
d = _collections.defaultdict(f, {2: 3})
#
d1 = d.copy()
assert type(d1) is _collections.defaultdict
assert len(d1) == 1
assert d1[2] == 3
assert d1[3] == 42
#
import copy
d2 = copy.deepcopy(d)
assert type(d2) is _collections.defaultdict
assert len(d2) == 1
assert d2[2] == 3
assert d2[3] == 42
def test_no_dict(self):
import _collections
assert not hasattr(_collections.defaultdict(), '__dict__')
def test_no_setattr(self):
import _collections
class D(_collections.defaultdict):
def __setattr__(self, attr, name):
raise AssertionError
d = D(int)
assert d['5'] == 0
d['6'] += 3
assert d['6'] == 3
def test_default_factory(self):
import _collections
f = lambda: 42
d = _collections.defaultdict(f)
assert d.default_factory is f
d.default_factory = lambda: 43
assert d['5'] == 43
def test_reduce(self):
import _collections
d = _collections.defaultdict(None, {3: 4})
dict_iter = d.__reduce__()[4]
assert type(dict_iter) is type(iter(d.items()))
def test_rec_repr(self):
import _collections
class X(_collections.defaultdict):
def mydefault(self):
pass
d = X.__new__(X)
d.__init__(d.mydefault)
assert repr(d).endswith('defaultdict(..., {})>, {})')
|