blob: 5093e9d0f36420a6ce429cf0d5757ce890619808 [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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 x = aug_test(1)
87 y = x
88 x += 10
Thomas Wouters104a7bc2000-08-24 20:14:10 +000089
Ezio Melottie9615932010-01-24 19:26:24 +000090 self.assertIsInstance(x, aug_test)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000091 self.assertTrue(y is not x)
Ezio Melottib3aedd42010-11-20 19:04:17 +000092 self.assertEqual(x.val, 11)
Jeremy Hylton4de8df92001-08-29 17:50:27 +000093
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000094 x = aug_test2(2)
95 y = x
96 x += 10
Jeremy Hylton4de8df92001-08-29 17:50:27 +000097
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000098 self.assertTrue(y is x)
Ezio Melottib3aedd42010-11-20 19:04:17 +000099 self.assertEqual(x.val, 12)
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000100
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000101 x = aug_test3(3)
102 y = x
103 x += 10
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000104
Ezio Melottie9615932010-01-24 19:26:24 +0000105 self.assertIsInstance(x, aug_test3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000106 self.assertTrue(y is not x)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000107 self.assertEqual(x.val, 13)
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000108
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000109
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000110 def testCustomMethods2(test_self):
111 output = []
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000112
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113 class testall:
114 def __add__(self, val):
115 output.append("__add__ called")
116 def __radd__(self, val):
117 output.append("__radd__ called")
118 def __iadd__(self, val):
119 output.append("__iadd__ called")
120 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000121
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000122 def __sub__(self, val):
123 output.append("__sub__ called")
124 def __rsub__(self, val):
125 output.append("__rsub__ called")
126 def __isub__(self, val):
127 output.append("__isub__ called")
128 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000129
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130 def __mul__(self, val):
131 output.append("__mul__ called")
132 def __rmul__(self, val):
133 output.append("__rmul__ called")
134 def __imul__(self, val):
135 output.append("__imul__ called")
136 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000137
Benjamin Petersond51374e2014-04-09 23:55:56 -0400138 def __matmul__(self, val):
139 output.append("__matmul__ called")
140 def __rmatmul__(self, val):
141 output.append("__rmatmul__ called")
142 def __imatmul__(self, val):
143 output.append("__imatmul__ called")
144 return self
145
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000146 def __floordiv__(self, val):
147 output.append("__floordiv__ called")
148 return self
149 def __ifloordiv__(self, val):
150 output.append("__ifloordiv__ called")
151 return self
152 def __rfloordiv__(self, val):
153 output.append("__rfloordiv__ called")
154 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000155
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000156 def __truediv__(self, val):
157 output.append("__truediv__ called")
158 return self
159 def __rtruediv__(self, val):
160 output.append("__rtruediv__ called")
161 return self
162 def __itruediv__(self, val):
163 output.append("__itruediv__ called")
164 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000165
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000166 def __mod__(self, val):
167 output.append("__mod__ called")
168 def __rmod__(self, val):
169 output.append("__rmod__ called")
170 def __imod__(self, val):
171 output.append("__imod__ called")
172 return self
Jeremy Hylton4de8df92001-08-29 17:50:27 +0000173
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000174 def __pow__(self, val):
175 output.append("__pow__ called")
176 def __rpow__(self, val):
177 output.append("__rpow__ called")
178 def __ipow__(self, val):
179 output.append("__ipow__ called")
180 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000181
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000182 def __or__(self, val):
183 output.append("__or__ called")
184 def __ror__(self, val):
185 output.append("__ror__ called")
186 def __ior__(self, val):
187 output.append("__ior__ called")
188 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000189
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000190 def __and__(self, val):
191 output.append("__and__ called")
192 def __rand__(self, val):
193 output.append("__rand__ called")
194 def __iand__(self, val):
195 output.append("__iand__ called")
196 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000197
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198 def __xor__(self, val):
199 output.append("__xor__ called")
200 def __rxor__(self, val):
201 output.append("__rxor__ called")
202 def __ixor__(self, val):
203 output.append("__ixor__ called")
204 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000205
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000206 def __rshift__(self, val):
207 output.append("__rshift__ called")
208 def __rrshift__(self, val):
209 output.append("__rrshift__ called")
210 def __irshift__(self, val):
211 output.append("__irshift__ called")
212 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000213
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000214 def __lshift__(self, val):
215 output.append("__lshift__ called")
216 def __rlshift__(self, val):
217 output.append("__rlshift__ called")
218 def __ilshift__(self, val):
219 output.append("__ilshift__ called")
220 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000221
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000222 x = testall()
223 x + 1
224 1 + x
225 x += 1
226
227 x - 1
228 1 - x
229 x -= 1
230
231 x * 1
232 1 * x
233 x *= 1
234
Benjamin Petersond51374e2014-04-09 23:55:56 -0400235 x @ 1
236 1 @ x
237 x @= 1
238
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000239 x / 1
240 1 / x
241 x /= 1
242
243 x // 1
244 1 // x
245 x //= 1
246
247 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
Ezio Melottib3aedd42010-11-20 19:04:17 +0000275 test_self.assertEqual(output, '''\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000276__add__ called
277__radd__ called
278__iadd__ called
279__sub__ called
280__rsub__ called
281__isub__ called
282__mul__ called
283__rmul__ called
284__imul__ called
Benjamin Petersond51374e2014-04-09 23:55:56 -0400285__matmul__ called
286__rmatmul__ called
287__imatmul__ called
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000288__truediv__ called
289__rtruediv__ called
290__itruediv__ called
291__floordiv__ called
292__rfloordiv__ called
293__ifloordiv__ called
294__mod__ called
295__rmod__ called
296__imod__ called
297__pow__ called
298__rpow__ called
299__ipow__ called
300__or__ called
301__ror__ called
302__ior__ called
303__and__ called
304__rand__ called
305__iand__ called
306__xor__ called
307__rxor__ called
308__ixor__ called
309__rshift__ called
310__rrshift__ called
311__irshift__ called
312__lshift__ called
313__rlshift__ called
314__ilshift__ called
315'''.splitlines())
316
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000317if __name__ == '__main__':
Zachary Ware38c707e2015-04-13 15:00:43 -0500318 unittest.main()