Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_defaultdict.TestDefaultDict Class Reference

List of all members.

Public Member Functions

def test_basic
def test_missing
def test_repr
def test_print
def test_copy
def test_shallow_copy
def test_deep_copy
def test_keyerror_without_factory
def test_recursive_repr
def test_pickleing

Public Attributes

 default_factory

Detailed Description

Definition at line 15 of file test_defaultdict.py.


Member Function Documentation

Definition at line 17 of file test_defaultdict.py.

00017 
00018     def test_basic(self):
00019         d1 = defaultdict()
00020         self.assertEqual(d1.default_factory, None)
00021         d1.default_factory = list
00022         d1[12].append(42)
00023         self.assertEqual(d1, {12: [42]})
00024         d1[12].append(24)
00025         self.assertEqual(d1, {12: [42, 24]})
00026         d1[13]
00027         d1[14]
00028         self.assertEqual(d1, {12: [42, 24], 13: [], 14: []})
00029         self.assertTrue(d1[12] is not d1[13] is not d1[14])
00030         d2 = defaultdict(list, foo=1, bar=2)
00031         self.assertEqual(d2.default_factory, list)
00032         self.assertEqual(d2, {"foo": 1, "bar": 2})
00033         self.assertEqual(d2["foo"], 1)
00034         self.assertEqual(d2["bar"], 2)
00035         self.assertEqual(d2[42], [])
00036         self.assertIn("foo", d2)
00037         self.assertIn("foo", d2.keys())
00038         self.assertIn("bar", d2)
00039         self.assertIn("bar", d2.keys())
00040         self.assertIn(42, d2)
00041         self.assertIn(42, d2.keys())
00042         self.assertNotIn(12, d2)
00043         self.assertNotIn(12, d2.keys())
00044         d2.default_factory = None
00045         self.assertEqual(d2.default_factory, None)
00046         try:
00047             d2[15]
00048         except KeyError as err:
00049             self.assertEqual(err.args, (15,))
00050         else:
00051             self.fail("d2[15] didn't raise KeyError")
00052         self.assertRaises(TypeError, defaultdict, 1)

Here is the call graph for this function:

Definition at line 97 of file test_defaultdict.py.

00097 
00098     def test_copy(self):
00099         d1 = defaultdict()
00100         d2 = d1.copy()
00101         self.assertEqual(type(d2), defaultdict)
00102         self.assertEqual(d2.default_factory, None)
00103         self.assertEqual(d2, {})
00104         d1.default_factory = list
00105         d3 = d1.copy()
00106         self.assertEqual(type(d3), defaultdict)
00107         self.assertEqual(d3.default_factory, list)
00108         self.assertEqual(d3, {})
00109         d1[42]
00110         d4 = d1.copy()
00111         self.assertEqual(type(d4), defaultdict)
00112         self.assertEqual(d4.default_factory, list)
00113         self.assertEqual(d4, {42: []})
00114         d4[12]
00115         self.assertEqual(d4, {42: [], 12: []})
00116 
00117         # Issue 6637: Copy fails for empty default dict
00118         d = defaultdict()
00119         d['a'] = 42
00120         e = d.copy()
00121         self.assertEqual(e['a'], 42)

Here is the call graph for this function:

Definition at line 132 of file test_defaultdict.py.

00132 
00133     def test_deep_copy(self):
00134         d1 = defaultdict(foobar, {1: [1]})
00135         d2 = copy.deepcopy(d1)
00136         self.assertEqual(d2.default_factory, foobar)
00137         self.assertEqual(d2, d1)
00138         self.assertTrue(d1[1] is not d2[1])
00139         d1.default_factory = list
00140         d2 = copy.deepcopy(d1)
00141         self.assertEqual(d2.default_factory, list)
00142         self.assertEqual(d2, d1)

Here is the call graph for this function:

Definition at line 143 of file test_defaultdict.py.

00143 
00144     def test_keyerror_without_factory(self):
00145         d1 = defaultdict()
00146         try:
00147             d1[(1,)]
00148         except KeyError as err:
00149             self.assertEqual(err.args[0], (1,))
00150         else:
00151             self.fail("expected KeyError")

Here is the call graph for this function:

Definition at line 53 of file test_defaultdict.py.

00053 
00054     def test_missing(self):
00055         d1 = defaultdict()
00056         self.assertRaises(KeyError, d1.__missing__, 42)
00057         d1.default_factory = list
00058         self.assertEqual(d1.__missing__(42), [])

Here is the call graph for this function:

Definition at line 175 of file test_defaultdict.py.

00175 
00176     def test_pickleing(self):
00177         d = defaultdict(int)
00178         d[1]
00179         for proto in range(pickle.HIGHEST_PROTOCOL + 1):
00180             s = pickle.dumps(d, proto)
00181             o = pickle.loads(s)
00182             self.assertEqual(d, o)

Here is the call graph for this function:

Definition at line 76 of file test_defaultdict.py.

00076 
00077     def test_print(self):
00078         d1 = defaultdict()
00079         def foo(): return 42
00080         d2 = defaultdict(foo, {1: 2})
00081         # NOTE: We can't use tempfile.[Named]TemporaryFile since this
00082         # code must exercise the tp_print C code, which only gets
00083         # invoked for *real* files.
00084         tfn = tempfile.mktemp()
00085         try:
00086             f = open(tfn, "w+")
00087             try:
00088                 print(d1, file=f)
00089                 print(d2, file=f)
00090                 f.seek(0)
00091                 self.assertEqual(f.readline(), repr(d1) + "\n")
00092                 self.assertEqual(f.readline(), repr(d2) + "\n")
00093             finally:
00094                 f.close()
00095         finally:
00096             os.remove(tfn)

Here is the call graph for this function:

Definition at line 152 of file test_defaultdict.py.

00152 
00153     def test_recursive_repr(self):
00154         # Issue2045: stack overflow when default_factory is a bound method
00155         class sub(defaultdict):
00156             def __init__(self):
00157                 self.default_factory = self._factory
00158             def _factory(self):
00159                 return []
00160         d = sub()
00161         self.assertTrue(repr(d).startswith(
00162             "defaultdict(<bound method sub._factory of defaultdict(..."))
00163 
00164         # NOTE: printing a subclass of a builtin type does not call its
00165         # tp_print slot. So this part is essentially the same test as above.
00166         tfn = tempfile.mktemp()
00167         try:
00168             f = open(tfn, "w+")
00169             try:
00170                 print(d, file=f)
00171             finally:
00172                 f.close()
00173         finally:
00174             os.remove(tfn)

Here is the call graph for this function:

Definition at line 59 of file test_defaultdict.py.

00059 
00060     def test_repr(self):
00061         d1 = defaultdict()
00062         self.assertEqual(d1.default_factory, None)
00063         self.assertEqual(repr(d1), "defaultdict(None, {})")
00064         self.assertEqual(eval(repr(d1)), d1)
00065         d1[11] = 41
00066         self.assertEqual(repr(d1), "defaultdict(None, {11: 41})")
00067         d2 = defaultdict(int)
00068         self.assertEqual(d2.default_factory, int)
00069         d2[12] = 42
00070         self.assertEqual(repr(d2), "defaultdict(<class 'int'>, {12: 42})")
00071         def foo(): return 43
00072         d3 = defaultdict(foo)
00073         self.assertTrue(d3.default_factory is foo)
00074         d3[13]
00075         self.assertEqual(repr(d3), "defaultdict(%s, {13: 43})" % repr(foo))

Here is the call graph for this function:

Definition at line 122 of file test_defaultdict.py.

00122 
00123     def test_shallow_copy(self):
00124         d1 = defaultdict(foobar, {1: 1})
00125         d2 = copy.copy(d1)
00126         self.assertEqual(d2.default_factory, foobar)
00127         self.assertEqual(d2, d1)
00128         d1.default_factory = list
00129         d2 = copy.copy(d1)
00130         self.assertEqual(d2.default_factory, list)
00131         self.assertEqual(d2, d1)

Here is the call graph for this function:


Member Data Documentation

Definition at line 156 of file test_defaultdict.py.


The documentation for this class was generated from the following file: