Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 1 | # Augmented assignment test. |
| 2 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3 | import unittest |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 4 | |
| 5 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6 | class 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 Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 19 | self.assertEqual(x, 3.0) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 20 | |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 21 | def test_with_unpacking(self): |
| 22 | self.assertRaises(SyntaxError, compile, "x, b += 3", "<test>", "exec") |
| 23 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 24 | 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 Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 36 | self.assertEqual(x[0], 3.0) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 37 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 38 | 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 Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 50 | self.assertEqual(x[0], 3.0) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 51 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 52 | def testSequences(self): |
| 53 | x = [1,2] |
| 54 | x += [3,4] |
| 55 | x *= 2 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 56 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 57 | self.assertEqual(x, [1, 2, 3, 4, 1, 2, 3, 4]) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 58 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 59 | x = [1, 2, 3] |
| 60 | y = x |
| 61 | x[1:2] *= 2 |
| 62 | y[1:2] += [1] |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 63 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 64 | self.assertEqual(x, [1, 2, 1, 2, 3]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 65 | self.assertTrue(x is y) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 66 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 67 | def testCustomMethods1(self): |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 68 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 69 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 76 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 77 | class aug_test2(aug_test): |
| 78 | def __iadd__(self, val): |
| 79 | self.val = self.val + val |
| 80 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 81 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 82 | class aug_test3(aug_test): |
| 83 | def __iadd__(self, val): |
| 84 | return aug_test3(self.val + val) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | 97c1adf | 2016-08-18 09:22:23 -0700 | [diff] [blame] | 86 | class aug_test4(aug_test3): |
| 87 | """Blocks inheritance, and fallback to __add__""" |
| 88 | __iadd__ = None |
| 89 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 90 | x = aug_test(1) |
| 91 | y = x |
| 92 | x += 10 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 93 | |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 94 | self.assertIsInstance(x, aug_test) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 95 | self.assertTrue(y is not x) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 96 | self.assertEqual(x.val, 11) |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 97 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 98 | x = aug_test2(2) |
| 99 | y = x |
| 100 | x += 10 |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 101 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 102 | self.assertTrue(y is x) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 103 | self.assertEqual(x.val, 12) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 104 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 105 | x = aug_test3(3) |
| 106 | y = x |
| 107 | x += 10 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 108 | |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 109 | self.assertIsInstance(x, aug_test3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 110 | self.assertTrue(y is not x) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 111 | self.assertEqual(x.val, 13) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | 97c1adf | 2016-08-18 09:22:23 -0700 | [diff] [blame] | 113 | x = aug_test4(4) |
| 114 | with self.assertRaises(TypeError): |
| 115 | x += 10 |
| 116 | |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 117 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 118 | def testCustomMethods2(test_self): |
| 119 | output = [] |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 120 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 121 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 129 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 130 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 137 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 138 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 145 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 146 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 154 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 163 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 164 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 173 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 174 | 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 Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 181 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 182 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 189 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 190 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 197 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 198 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 205 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 206 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 213 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 214 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 221 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 222 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 229 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 230 | 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 Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 243 | x @ 1 |
| 244 | 1 @ x |
| 245 | x @= 1 |
| 246 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | |
| 275 | x >> 1 |
| 276 | 1 >> x |
| 277 | x >>= 1 |
| 278 | |
| 279 | x << 1 |
| 280 | 1 << x |
| 281 | x <<= 1 |
| 282 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 283 | test_self.assertEqual(output, '''\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 284 | __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 Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 293 | __matmul__ called |
| 294 | __rmatmul__ called |
| 295 | __imatmul__ called |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 296 | __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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 325 | if __name__ == '__main__': |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 326 | unittest.main() |