| 1 |
|
| 2 |
|
| 3 |
from _collections import defaultdict |
| 4 |
from pytest import raises |
| 5 |
|
| 6 |
def test_basics(): |
| 7 |
d = defaultdict(list) |
| 8 |
assert d.default_factory is list |
| 9 |
assert defaultdict.default_factory.__get__(d) is list |
| 10 |
l = d[5] |
| 11 |
d[5].append(42) |
| 12 |
d[5].append(43) |
| 13 |
assert l == [42, 43] |
| 14 |
l2 = [] |
| 15 |
d[5] = l2 |
| 16 |
d[5].append(44) |
| 17 |
assert l == [42, 43] and l2 == [44] |
| 18 |
|
| 19 |
def test_module(): |
| 20 |
assert repr(defaultdict) in ( |
| 21 |
"<class 'collections.defaultdict'>", |
| 22 |
"<type 'collections.defaultdict'>") |
| 23 |
|
| 24 |
def test_keyerror_without_factory(): |
| 25 |
for d1 in [defaultdict(), defaultdict(None)]: |
| 26 |
for key in ['foo', (1,)]: |
| 27 |
try: |
| 28 |
d1[key] |
| 29 |
except KeyError as err: |
| 30 |
assert err.args[0] == key |
| 31 |
else: |
| 32 |
assert 0, "expected KeyError" |
| 33 |
|
| 34 |
def test_noncallable(): |
| 35 |
raises(TypeError, defaultdict, [('a', 5)]) |
| 36 |
d = defaultdict(None, [('a', 5)]) |
| 37 |
assert list(d.items()) == [('a', 5)] |
| 38 |
|
| 39 |
def test_kwds(): |
| 40 |
d = defaultdict(default_factory=5) |
| 41 |
assert list(d.keys()) == ['default_factory'] |
| 42 |
|
| 43 |
def test_copy(): |
| 44 |
def f(): |
| 45 |
return 42 |
| 46 |
d = defaultdict(f, {2: 3}) |
| 47 |
|
| 48 |
d1 = d.copy() |
| 49 |
assert type(d1) is defaultdict |
| 50 |
assert len(d1) == 1 |
| 51 |
assert d1[2] == 3 |
| 52 |
assert d1[3] == 42 |
| 53 |
|
| 54 |
import copy |
| 55 |
d2 = copy.deepcopy(d) |
| 56 |
assert type(d2) is defaultdict |
| 57 |
assert len(d2) == 1 |
| 58 |
assert d2[2] == 3 |
| 59 |
assert d2[3] == 42 |
| 60 |
|
| 61 |
def test_no_dict(): |
| 62 |
assert not hasattr(defaultdict(), '__dict__') |
| 63 |
|
| 64 |
def test_no_setattr(): |
| 65 |
class D(defaultdict): |
| 66 |
def __setattr__(self, attr, name): |
| 67 |
raise AssertionError |
| 68 |
d = D(int) |
| 69 |
assert d['5'] == 0 |
| 70 |
d['6'] += 3 |
| 71 |
assert d['6'] == 3 |
| 72 |
|
| 73 |
def test_default_factory(): |
| 74 |
f = lambda: 42 |
| 75 |
d = defaultdict(f) |
| 76 |
assert d.default_factory is f |
| 77 |
d.default_factory = lambda: 43 |
| 78 |
assert d['5'] == 43 |
| 79 |
|
| 80 |
def test_reduce(): |
| 81 |
d = defaultdict(None, {3: 4}) |
| 82 |
dict_iter = d.__reduce__()[4] |
| 83 |
assert type(dict_iter) is type(iter(d.items())) |
| 84 |
|
| 85 |
def test_rec_repr(): |
| 86 |
class X(defaultdict): |
| 87 |
def mydefault(self): |
| 88 |
pass |
| 89 |
d = X.__new__(X) |
| 90 |
d.__init__(d.mydefault) |
| 91 |
assert repr(d).endswith('X(..., {})>, {})') |
| 92 |
|
| 93 |
def test_subclass_repr(): |
| 94 |
class subclass(defaultdict): |
| 95 |
pass |
| 96 |
assert repr(subclass()) == 'subclass(None, {})' |
| 97 |
|
| 98 |
def test_generic_alias(): |
| 99 |
import _collections |
| 100 |
ga = _collections.defaultdict[int] |
| 101 |
assert ga.__origin__ is _collections.defaultdict |
| 102 |
assert ga.__args__ == (int, ) |
| 103 |
|
| 104 |
def test_union(): |
| 105 |
import _collections |
| 106 |
d1 = _collections.defaultdict(int) |
| 107 |
d1[1], d1[2] |
| 108 |
d2 = _collections.defaultdict(str) |
| 109 |
d2[2], d2[3] |
| 110 |
d = d1 | d2 |
| 111 |
assert d.default_factory is int |
| 112 |
assert d == {1: 0, 2: "", 3: ""} |
| 113 |
|
| 114 |
d = dict(d1) | d2 |
| 115 |
assert d.default_factory is str |
| 116 |
assert d == {1: 0, 2: "", 3: ""} |
| 117 |
|
| 118 |
with raises(TypeError): |
| 119 |
d1 | list(d2.items()) |
| 120 |
with raises(TypeError): |
| 121 |
list(d2.items()) | d1 |