1 # Augmented assignment test.
3 from test
.test_support
import run_unittest
, check_py3k_warnings
7 class AugAssignTest(unittest
.TestCase
):
22 self
.assertEquals(x
, 3)
24 # new-style division (with -Qnew)
25 self
.assertEquals(x
, 3.0)
27 def test_with_unpacking(self
):
28 self
.assertRaises(SyntaxError, compile, "x, b += 3", "<test>", "exec")
43 self
.assertEquals(x
[0], 3)
45 self
.assertEquals(x
[0], 3.0)
60 self
.assertEquals(x
[0], 3)
62 self
.assertEquals(x
[0], 3.0)
64 def testSequences(self
):
69 self
.assertEquals(x
, [1, 2, 3, 4, 1, 2, 3, 4])
76 self
.assertEquals(x
, [1, 2, 1, 2, 3])
77 self
.assertTrue(x
is y
)
79 def testCustomMethods1(self
):
82 def __init__(self
, value
):
84 def __radd__(self
, val
):
86 def __add__(self
, val
):
87 return aug_test(self
.val
+ val
)
89 class aug_test2(aug_test
):
90 def __iadd__(self
, val
):
91 self
.val
= self
.val
+ val
94 class aug_test3(aug_test
):
95 def __iadd__(self
, val
):
96 return aug_test3(self
.val
+ val
)
102 self
.assertIsInstance(x
, aug_test
)
103 self
.assertTrue(y
is not x
)
104 self
.assertEquals(x
.val
, 11)
110 self
.assertTrue(y
is x
)
111 self
.assertEquals(x
.val
, 12)
117 self
.assertIsInstance(x
, aug_test3
)
118 self
.assertTrue(y
is not x
)
119 self
.assertEquals(x
.val
, 13)
122 def testCustomMethods2(test_self
):
126 def __add__(self
, val
):
127 output
.append("__add__ called")
128 def __radd__(self
, val
):
129 output
.append("__radd__ called")
130 def __iadd__(self
, val
):
131 output
.append("__iadd__ called")
134 def __sub__(self
, val
):
135 output
.append("__sub__ called")
136 def __rsub__(self
, val
):
137 output
.append("__rsub__ called")
138 def __isub__(self
, val
):
139 output
.append("__isub__ called")
142 def __mul__(self
, val
):
143 output
.append("__mul__ called")
144 def __rmul__(self
, val
):
145 output
.append("__rmul__ called")
146 def __imul__(self
, val
):
147 output
.append("__imul__ called")
150 def __div__(self
, val
):
151 output
.append("__div__ called")
152 def __rdiv__(self
, val
):
153 output
.append("__rdiv__ called")
154 def __idiv__(self
, val
):
155 output
.append("__idiv__ called")
158 def __floordiv__(self
, val
):
159 output
.append("__floordiv__ called")
161 def __ifloordiv__(self
, val
):
162 output
.append("__ifloordiv__ called")
164 def __rfloordiv__(self
, val
):
165 output
.append("__rfloordiv__ called")
168 def __truediv__(self
, val
):
169 output
.append("__truediv__ called")
171 def __itruediv__(self
, val
):
172 output
.append("__itruediv__ called")
175 def __mod__(self
, val
):
176 output
.append("__mod__ called")
177 def __rmod__(self
, val
):
178 output
.append("__rmod__ called")
179 def __imod__(self
, val
):
180 output
.append("__imod__ called")
183 def __pow__(self
, val
):
184 output
.append("__pow__ called")
185 def __rpow__(self
, val
):
186 output
.append("__rpow__ called")
187 def __ipow__(self
, val
):
188 output
.append("__ipow__ called")
191 def __or__(self
, val
):
192 output
.append("__or__ called")
193 def __ror__(self
, val
):
194 output
.append("__ror__ called")
195 def __ior__(self
, val
):
196 output
.append("__ior__ called")
199 def __and__(self
, val
):
200 output
.append("__and__ called")
201 def __rand__(self
, val
):
202 output
.append("__rand__ called")
203 def __iand__(self
, val
):
204 output
.append("__iand__ called")
207 def __xor__(self
, val
):
208 output
.append("__xor__ called")
209 def __rxor__(self
, val
):
210 output
.append("__rxor__ called")
211 def __ixor__(self
, val
):
212 output
.append("__ixor__ called")
215 def __rshift__(self
, val
):
216 output
.append("__rshift__ called")
217 def __rrshift__(self
, val
):
218 output
.append("__rrshift__ called")
219 def __irshift__(self
, val
):
220 output
.append("__irshift__ called")
223 def __lshift__(self
, val
):
224 output
.append("__lshift__ called")
225 def __rlshift__(self
, val
):
226 output
.append("__rlshift__ called")
227 def __ilshift__(self
, val
):
228 output
.append("__ilshift__ called")
249 # True division is in effect, so "/" doesn't map to __div__ etc;
250 # but the canned expected-output file requires that those get called.
287 test_self
.assertEquals(output
, '''\
327 with
check_py3k_warnings(("classic int division", DeprecationWarning)):
328 run_unittest(AugAssignTest
)
330 if __name__
== '__main__':