LEFT | RIGHT |
1 # Augmented assignment test. | 1 # Augmented assignment test. |
2 | 2 |
3 import unittest | 3 import unittest |
4 | 4 |
5 | 5 |
6 class AugAssignTest(unittest.TestCase): | 6 class AugAssignTest(unittest.TestCase): |
7 def testBasic(self): | 7 def testBasic(self): |
8 x = 2 | 8 x = 2 |
9 x += 1 | 9 x += 1 |
10 x *= 2 | 10 x *= 2 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 class aug_test2(aug_test): | 77 class aug_test2(aug_test): |
78 def __iadd__(self, val): | 78 def __iadd__(self, val): |
79 self.val = self.val + val | 79 self.val = self.val + val |
80 return self | 80 return self |
81 | 81 |
82 class aug_test3(aug_test): | 82 class aug_test3(aug_test): |
83 def __iadd__(self, val): | 83 def __iadd__(self, val): |
84 return aug_test3(self.val + val) | 84 return aug_test3(self.val + val) |
85 | 85 |
86 class aug_test4(aug_test3): | 86 class aug_test4(aug_test3): |
87 "Blocks inheritance, and fallback to __add__" | 87 """Blocks inheritance, and fallback to __add__""" |
88 __iadd__ = None | 88 __iadd__ = None |
89 | 89 |
90 x = aug_test(1) | 90 x = aug_test(1) |
91 y = x | 91 y = x |
92 x += 10 | 92 x += 10 |
93 | 93 |
94 self.assertIsInstance(x, aug_test) | 94 self.assertIsInstance(x, aug_test) |
95 self.assertTrue(y is not x) | 95 self.assertTrue(y is not x) |
96 self.assertEqual(x.val, 11) | 96 self.assertEqual(x.val, 11) |
97 | 97 |
98 x = aug_test2(2) | 98 x = aug_test2(2) |
99 y = x | 99 y = x |
100 x += 10 | 100 x += 10 |
101 | 101 |
102 self.assertTrue(y is x) | 102 self.assertTrue(y is x) |
103 self.assertEqual(x.val, 12) | 103 self.assertEqual(x.val, 12) |
104 | 104 |
105 x = aug_test3(3) | 105 x = aug_test3(3) |
106 y = x | 106 y = x |
107 x += 10 | 107 x += 10 |
108 | 108 |
109 self.assertIsInstance(x, aug_test3) | 109 self.assertIsInstance(x, aug_test3) |
110 self.assertTrue(y is not x) | 110 self.assertTrue(y is not x) |
111 self.assertEqual(x.val, 13) | 111 self.assertEqual(x.val, 13) |
112 | 112 |
113 x = aug_test4(4) | 113 x = aug_test4(4) |
114 with self.assertRaises(TypeError): | 114 with self.assertRaises(TypeError): |
115 x += 10 | 115 x += 10 |
| 116 |
116 | 117 |
117 def testCustomMethods2(test_self): | 118 def testCustomMethods2(test_self): |
118 output = [] | 119 output = [] |
119 | 120 |
120 class testall: | 121 class testall: |
121 def __add__(self, val): | 122 def __add__(self, val): |
122 output.append("__add__ called") | 123 output.append("__add__ called") |
123 def __radd__(self, val): | 124 def __radd__(self, val): |
124 output.append("__radd__ called") | 125 output.append("__radd__ called") |
125 def __iadd__(self, val): | 126 def __iadd__(self, val): |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 __rshift__ called | 317 __rshift__ called |
317 __rrshift__ called | 318 __rrshift__ called |
318 __irshift__ called | 319 __irshift__ called |
319 __lshift__ called | 320 __lshift__ called |
320 __rlshift__ called | 321 __rlshift__ called |
321 __ilshift__ called | 322 __ilshift__ called |
322 '''.splitlines()) | 323 '''.splitlines()) |
323 | 324 |
324 if __name__ == '__main__': | 325 if __name__ == '__main__': |
325 unittest.main() | 326 unittest.main() |
LEFT | RIGHT |