Ditched '_find_SET()', since it was a no-value-added wrapper around
[python/dscho.git] / Lib / dos-8x3 / test_unp.py
blob1bfd47840b9101ae4f1423c403510dcf46e407dc
1 from test_support import *
3 t = (1, 2, 3)
4 l = [4, 5, 6]
6 class Seq:
7 def __getitem__(self, i):
8 if i >= 0 and i < 3: return i
9 raise IndexError
11 a = -1
12 b = -1
13 c = -1
15 # unpack tuple
16 if verbose:
17 print 'unpack tuple'
18 a, b, c = t
19 if a <> 1 or b <> 2 or c <> 3:
20 raise TestFailed
22 # unpack list
23 if verbose:
24 print 'unpack list'
25 a, b, c = l
26 if a <> 4 or b <> 5 or c <> 6:
27 raise TestFailed
29 # unpack implied tuple
30 if verbose:
31 print 'unpack implied tuple'
32 a, b, c = 7, 8, 9
33 if a <> 7 or b <> 8 or c <> 9:
34 raise TestFailed
36 # unpack string... fun!
37 if verbose:
38 print 'unpack string'
39 a, b, c = 'one'
40 if a <> 'o' or b <> 'n' or c <> 'e':
41 raise TestFailed
43 # unpack generic sequence
44 if verbose:
45 print 'unpack sequence'
46 a, b, c = Seq()
47 if a <> 0 or b <> 1 or c <> 2:
48 raise TestFailed
50 # now for some failures
52 # unpacking non-sequence
53 if verbose:
54 print 'unpack non-sequence'
55 try:
56 a, b, c = 7
57 raise TestFailed
58 except TypeError:
59 pass
62 # unpacking tuple of wrong size
63 if verbose:
64 print 'unpack tuple wrong size'
65 try:
66 a, b = t
67 raise TestFailed
68 except ValueError:
69 pass
71 # unpacking list of wrong size
72 if verbose:
73 print 'unpack list wrong size'
74 try:
75 a, b = l
76 raise TestFailed
77 except ValueError:
78 pass
81 # unpacking sequence too short
82 if verbose:
83 print 'unpack sequence too short'
84 try:
85 a, b, c, d = Seq()
86 raise TestFailed
87 except ValueError:
88 pass
91 # unpacking sequence too long
92 if verbose:
93 print 'unpack sequence too long'
94 try:
95 a, b = Seq()
96 raise TestFailed
97 except ValueError:
98 pass
101 # unpacking a sequence where the test for too long raises a different
102 # kind of error
103 BozoError = 'BozoError'
105 class BadSeq:
106 def __getitem__(self, i):
107 if i >= 0 and i < 3:
108 return i
109 elif i == 3:
110 raise BozoError
111 else:
112 raise IndexError
115 # trigger code while not expecting an IndexError
116 if verbose:
117 print 'unpack sequence too long, wrong error'
118 try:
119 a, b, c, d, e = BadSeq()
120 raise TestFailed
121 except BozoError:
122 pass
124 # trigger code while expecting an IndexError
125 if verbose:
126 print 'unpack sequence too short, wrong error'
127 try:
128 a, b, c = BadSeq()
129 raise TestFailed
130 except BozoError:
131 pass