1import doctest
2import unittest
3
4
5doctests = """
6
7Unpack tuple
8
9    >>> t = (1, 2, 3)
10    >>> a, b, c = t
11    >>> a == 1 and b == 2 and c == 3
12    True
13
14Unpack list
15
16    >>> l = [4, 5, 6]
17    >>> a, b, c = l
18    >>> a == 4 and b == 5 and c == 6
19    True
20
21Unpack implied tuple
22
23    >>> a, b, c = 7, 8, 9
24    >>> a == 7 and b == 8 and c == 9
25    True
26
27Unpack string... fun!
28
29    >>> a, b, c = 'one'
30    >>> a == 'o' and b == 'n' and c == 'e'
31    True
32
33Unpack generic sequence
34
35    >>> class Seq:
36    ...     def __getitem__(self, i):
37    ...         if i >= 0 and i < 3: return i
38    ...         raise IndexError
39    ...
40    >>> a, b, c = Seq()
41    >>> a == 0 and b == 1 and c == 2
42    True
43
44Single element unpacking, with extra syntax
45
46    >>> st = (99,)
47    >>> sl = [100]
48    >>> a, = st
49    >>> a
50    99
51    >>> b, = sl
52    >>> b
53    100
54
55Now for some failures
56
57Unpacking non-sequence
58
59    >>> a, b, c = 7
60    Traceback (most recent call last):
61      ...
62    TypeError: cannot unpack non-iterable int object
63
64Unpacking tuple of wrong size
65
66    >>> a, b = t
67    Traceback (most recent call last):
68      ...
69    ValueError: too many values to unpack (expected 2)
70
71Unpacking tuple of wrong size
72
73    >>> a, b = l
74    Traceback (most recent call last):
75      ...
76    ValueError: too many values to unpack (expected 2)
77
78Unpacking sequence too short
79
80    >>> a, b, c, d = Seq()
81    Traceback (most recent call last):
82      ...
83    ValueError: not enough values to unpack (expected 4, got 3)
84
85Unpacking sequence too long
86
87    >>> a, b = Seq()
88    Traceback (most recent call last):
89      ...
90    ValueError: too many values to unpack (expected 2)
91
92Unpacking a sequence where the test for too long raises a different kind of
93error
94
95    >>> class BozoError(Exception):
96    ...     pass
97    ...
98    >>> class BadSeq:
99    ...     def __getitem__(self, i):
100    ...         if i >= 0 and i < 3:
101    ...             return i
102    ...         elif i == 3:
103    ...             raise BozoError
104    ...         else:
105    ...             raise IndexError
106    ...
107
108Trigger code while not expecting an IndexError (unpack sequence too long, wrong
109error)
110
111    >>> a, b, c, d, e = BadSeq()
112    Traceback (most recent call last):
113      ...
114    test.test_unpack.BozoError
115
116Trigger code while expecting an IndexError (unpack sequence too short, wrong
117error)
118
119    >>> a, b, c = BadSeq()
120    Traceback (most recent call last):
121      ...
122    test.test_unpack.BozoError
123
124Allow unpacking empty iterables
125
126    >>> () = []
127    >>> [] = ()
128    >>> [] = []
129    >>> () = ()
130
131Unpacking non-iterables should raise TypeError
132
133    >>> () = 42
134    Traceback (most recent call last):
135      ...
136    TypeError: cannot unpack non-iterable int object
137
138Unpacking to an empty iterable should raise ValueError
139
140    >>> () = [42]
141    Traceback (most recent call last):
142      ...
143    ValueError: too many values to unpack (expected 0)
144
145"""
146
147__test__ = {'doctests' : doctests}
148
149def load_tests(loader, tests, pattern):
150    tests.addTest(doctest.DocTestSuite())
151    return tests
152
153
154if __name__ == "__main__":
155    unittest.main()
156