blob: 5930d9e7a29fa2a2ee0d46d1f1f041b27703475c [file] [log] [blame]
Thomas Wouters104a7bc2000-08-24 20:14:10 +00001# Augmented assignment test.
2
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003import unittest
Thomas Wouters104a7bc2000-08-24 20:14:10 +00004
5
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006class AugAssignTest(unittest.TestCase):
7 def testBasic(self):
8 x = 2
9 x += 1
10 x *= 2
11 x **= 2
12 x -= 8
13 x //= 5
14 x %= 3
15 x &= 2
16 x |= 5
17 x ^= 1
18 x /= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +000019 self.assertEqual(x, 3.0)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000020
Benjamin Petersonbd27aef2009-10-03 20:27:13 +000021 def test_with_unpacking(self):
22 self.assertRaises(SyntaxError, compile, "x, b += 3", "<test>", "exec")
23
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000024 def testInList(self):
25 x = [2]
26 x[0] += 1
27 x[0] *= 2
28 x[0] **= 2
29 x[0] -= 8
30 x[0] //= 5
31 x[0] %= 3
32 x[0] &= 2
33 x[0] |= 5
34 x[0] ^= 1
35 x[0] /= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +000036 self.assertEqual(x[0], 3.0)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000037
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038 def testInDict(self):
39 x = {0: 2}
40 x[0] += 1
41 x[0] *= 2
42 x[0] **= 2
43 x[0] -= 8
44 x[0] //= 5
45 x[0] %= 3
46 x[0] &= 2
47 x[0] |= 5
48 x[0] ^= 1
49 x[0] /= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +000050 self.assertEqual(x[0], 3.0)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000051
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000052 def testSequences(self):
53 x = [1,2]
54 x += [3,4]
55 x *= 2
Thomas Wouters104a7bc2000-08-24 20:14:10 +000056
Ezio Melottib3aedd42010-11-20 19:04:17 +000057 self.assertEqual(x, [1, 2, 3, 4, 1, 2, 3, 4])
Thomas Wouters104a7bc2000-08-24 20:14:10 +000058
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000059 x = [1, 2, 3]
60 y = x
61 x[1:2] *= 2
62 y[1:2] += [1]
Thomas Wouters104a7bc2000-08-24 20:14:10 +000063
Ezio Melottib3aedd42010-11-20 19:04:17 +000064 self.assertEqual(x, [1, 2, 1, 2, 3])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000065 self.assertTrue(x is y)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000066
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000067 def testCustomMethods1(self):
Thomas Wouters104a7bc2000-08-24 20:14:10 +000068
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000069 class aug_test:
70 def __init__(self, value):
71 self.val = value
72 def __radd__(self, val):
73 return self.val + val
74 def __add__(self, val):
75 return aug_test(self.val + val)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000076
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000077 class aug_test2(aug_test):
78 def __iadd__(self, val):
79 self.val = self.val + val
80 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +000081
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000082 class aug_test3(aug_test):
83 def __iadd__(self, val):
84 return aug_test3(self.val + val)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000085
Guido van Rossum97c1adf2016-08-18 09:22:23 -070086 class aug_test4(aug_test3):
87 """Blocks inheritance, and fallback to __add__"""
88 __iadd__ = None
89
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000090 x = aug_test(1)
91 y = x
92 x += 10
Thomas Wouters104a7bc2000-08-24 20:14:10 +000093
Ezio Melottie9615932010-01-24 19:26:24 +000094 self.assertIsInstance(x, aug_test)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000095 self.assertTrue(y is not x)
Ezio Melottib3aedd42010-11-20 19:04:17 +000096 self.assertEqual(x.val, 11)
Jeremy Hylton4de8df92001-08-29 17:50:27 +000097
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000098 x = aug_test2(2)
99 y = x
100 x += 10
Jeremy Hylton4de8df92001-08-29 17:50:27 +0000101
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000102 self.assertTrue(y is x)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000103 self.assertEqual(x.val, 12)
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000104
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000105 x = aug_test3(3)
106 y = x
107 x += 10
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000108
Ezio Melottie9615932010-01-24 19:26:24 +0000109 self.assertIsInstance(x, aug_test3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000110 self.assertTrue(y is not x)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000111 self.assertEqual(x.val, 13)
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000112
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700113 x = aug_test4(4)
114 with self.assertRaises(TypeError):
115 x += 10
116
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000117
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000118 def testCustomMethods2(test_self):
119 output = []
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000120
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000121 class testall:
122 def __add__(self, val):
123 output.append("__add__ called")
124 def __radd__(self, val):
125 output.append("__radd__ called")
126 def __iadd__(self, val):
127 output.append("__iadd__ called")
128 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000129
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130 def __sub__(self, val):
131 output.append("__sub__ called")
132 def __rsub__(self, val):
133 output.append("__rsub__ called")
134 def __isub__(self, val):
135 output.append("__isub__ called")
136 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000137
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000138 def __mul__(self, val):
139 output.append("__mul__ called")
140 def __rmul__(self, val):
141 output.append("__rmul__ called")
142 def __imul__(self, val):
143 output.append("__imul__ called")
144 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000145
Benjamin Petersond51374e2014-04-09 23:55:56 -0400146 def __matmul__(self, val):
147 output.append("__matmul__ called")
148 def __rmatmul__(self, val):
149 output.append("__rmatmul__ called")
150 def __imatmul__(self, val):
151 output.append("__imatmul__ called")
152 return self
153
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000154 def __floordiv__(self, val):
155 output.append("__floordiv__ called")
156 return self
157 def __ifloordiv__(self, val):
158 output.append("__ifloordiv__ called")
159 return self
160 def __rfloordiv__(self, val):
161 output.append("__rfloordiv__ called")
162 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000163
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000164 def __truediv__(self, val):
165 output.append("__truediv__ called")
166 return self
167 def __rtruediv__(self, val):
168 output.append("__rtruediv__ called")
169 return self
170 def __itruediv__(self, val):
171 output.append("__itruediv__ called")
172 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000173
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000174 def __mod__(self, val):
175 output.append("__mod__ called")
176 def __rmod__(self, val):
177 output.append("__rmod__ called")
178 def __imod__(self, val):
179 output.append("__imod__ called")
180 return self
Jeremy Hylton4de8df92001-08-29 17:50:27 +0000181
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000182 def __pow__(self, val):
183 output.append("__pow__ called")
184 def __rpow__(self, val):
185 output.append("__rpow__ called")
186 def __ipow__(self, val):
187 output.append("__ipow__ called")
188 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000189
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000190 def __or__(self, val):
191 output.append("__or__ called")
192 def __ror__(self, val):
193 output.append("__ror__ called")
194 def __ior__(self, val):
195 output.append("__ior__ called")
196 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000197
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198 def __and__(self, val):
199 output.append("__and__ called")
200 def __rand__(self, val):
201 output.append("__rand__ called")
202 def __iand__(self, val):
203 output.append("__iand__ called")
204 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000205
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000206 def __xor__(self, val):
207 output.append("__xor__ called")
208 def __rxor__(self, val):
209 output.append("__rxor__ called")
210 def __ixor__(self, val):
211 output.append("__ixor__ called")
212 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000213
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000214 def __rshift__(self, val):
215 output.append("__rshift__ called")
216 def __rrshift__(self, val):
217 output.append("__rrshift__ called")
218 def __irshift__(self, val):
219 output.append("__irshift__ called")
220 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000221
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000222 def __lshift__(self, val):
223 output.append("__lshift__ called")
224 def __rlshift__(self, val):
225 output.append("__rlshift__ called")
226 def __ilshift__(self, val):
227 output.append("__ilshift__ called")
228 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000229
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000230 x = testall()
231 x + 1
232 1 + x
233 x += 1
234
235 x - 1
236 1 - x
237 x -= 1
238
239 x * 1
240 1 * x
241 x *= 1
242
Benjamin Petersond51374e2014-04-09 23:55:56 -0400243 x @ 1
244 1 @ x
245 x @= 1
246
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000247 x / 1
248 1 / x
249 x /= 1
250
251 x // 1
252 1 // x
253 x //= 1
254
255 x % 1
256 1 % x
257 x %= 1
258
259 x ** 1
260 1 ** x
261 x **= 1
262
263 x | 1
264 1 | x
265 x |= 1
266
267 x & 1
268 1 & x
269 x &= 1
270
271 x ^ 1
272 1 ^ x
273 x ^= 1
274
275 x >> 1
276 1 >> x
277 x >>= 1
278
279 x << 1
280 1 << x
281 x <<= 1
282
Ezio Melottib3aedd42010-11-20 19:04:17 +0000283 test_self.assertEqual(output, '''\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000284__add__ called
285__radd__ called
286__iadd__ called
287__sub__ called
288__rsub__ called
289__isub__ called
290__mul__ called
291__rmul__ called
292__imul__ called
Benjamin Petersond51374e2014-04-09 23:55:56 -0400293__matmul__ called
294__rmatmul__ called
295__imatmul__ called
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000296__truediv__ called
297__rtruediv__ called
298__itruediv__ called
299__floordiv__ called
300__rfloordiv__ called
301__ifloordiv__ called
302__mod__ called
303__rmod__ called
304__imod__ called
305__pow__ called
306__rpow__ called
307__ipow__ called
308__or__ called
309__ror__ called
310__ior__ called
311__and__ called
312__rand__ called
313__iand__ called
314__xor__ called
315__rxor__ called
316__ixor__ called
317__rshift__ called
318__rrshift__ called
319__irshift__ called
320__lshift__ called
321__rlshift__ called
322__ilshift__ called
323'''.splitlines())
324
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000325if __name__ == '__main__':
Zachary Ware38c707e2015-04-13 15:00:43 -0500326 unittest.main()