ยปCore Development>Code coverage>Lib/test/test_augassign.py

Python code coverage for Lib/test/test_augassign.py

#countcontent
1n/a# Augmented assignment test.
2n/a
3n/aimport unittest
4n/a
5n/a
6n/aclass AugAssignTest(unittest.TestCase):
7n/a def testBasic(self):
8n/a x = 2
9n/a x += 1
10n/a x *= 2
11n/a x **= 2
12n/a x -= 8
13n/a x //= 5
14n/a x %= 3
15n/a x &= 2
16n/a x |= 5
17n/a x ^= 1
18n/a x /= 2
19n/a self.assertEqual(x, 3.0)
20n/a
21n/a def test_with_unpacking(self):
22n/a self.assertRaises(SyntaxError, compile, "x, b += 3", "<test>", "exec")
23n/a
24n/a def testInList(self):
25n/a x = [2]
26n/a x[0] += 1
27n/a x[0] *= 2
28n/a x[0] **= 2
29n/a x[0] -= 8
30n/a x[0] //= 5
31n/a x[0] %= 3
32n/a x[0] &= 2
33n/a x[0] |= 5
34n/a x[0] ^= 1
35n/a x[0] /= 2
36n/a self.assertEqual(x[0], 3.0)
37n/a
38n/a def testInDict(self):
39n/a x = {0: 2}
40n/a x[0] += 1
41n/a x[0] *= 2
42n/a x[0] **= 2
43n/a x[0] -= 8
44n/a x[0] //= 5
45n/a x[0] %= 3
46n/a x[0] &= 2
47n/a x[0] |= 5
48n/a x[0] ^= 1
49n/a x[0] /= 2
50n/a self.assertEqual(x[0], 3.0)
51n/a
52n/a def testSequences(self):
53n/a x = [1,2]
54n/a x += [3,4]
55n/a x *= 2
56n/a
57n/a self.assertEqual(x, [1, 2, 3, 4, 1, 2, 3, 4])
58n/a
59n/a x = [1, 2, 3]
60n/a y = x
61n/a x[1:2] *= 2
62n/a y[1:2] += [1]
63n/a
64n/a self.assertEqual(x, [1, 2, 1, 2, 3])
65n/a self.assertTrue(x is y)
66n/a
67n/a def testCustomMethods1(self):
68n/a
69n/a class aug_test:
70n/a def __init__(self, value):
71n/a self.val = value
72n/a def __radd__(self, val):
73n/a return self.val + val
74n/a def __add__(self, val):
75n/a return aug_test(self.val + val)
76n/a
77n/a class aug_test2(aug_test):
78n/a def __iadd__(self, val):
79n/a self.val = self.val + val
80n/a return self
81n/a
82n/a class aug_test3(aug_test):
83n/a def __iadd__(self, val):
84n/a return aug_test3(self.val + val)
85n/a
86n/a class aug_test4(aug_test3):
87n/a """Blocks inheritance, and fallback to __add__"""
88n/a __iadd__ = None
89n/a
90n/a x = aug_test(1)
91n/a y = x
92n/a x += 10
93n/a
94n/a self.assertIsInstance(x, aug_test)
95n/a self.assertTrue(y is not x)
96n/a self.assertEqual(x.val, 11)
97n/a
98n/a x = aug_test2(2)
99n/a y = x
100n/a x += 10
101n/a
102n/a self.assertTrue(y is x)
103n/a self.assertEqual(x.val, 12)
104n/a
105n/a x = aug_test3(3)
106n/a y = x
107n/a x += 10
108n/a
109n/a self.assertIsInstance(x, aug_test3)
110n/a self.assertTrue(y is not x)
111n/a self.assertEqual(x.val, 13)
112n/a
113n/a x = aug_test4(4)
114n/a with self.assertRaises(TypeError):
115n/a x += 10
116n/a
117n/a
118n/a def testCustomMethods2(test_self):
119n/a output = []
120n/a
121n/a class testall:
122n/a def __add__(self, val):
123n/a output.append("__add__ called")
124n/a def __radd__(self, val):
125n/a output.append("__radd__ called")
126n/a def __iadd__(self, val):
127n/a output.append("__iadd__ called")
128n/a return self
129n/a
130n/a def __sub__(self, val):
131n/a output.append("__sub__ called")
132n/a def __rsub__(self, val):
133n/a output.append("__rsub__ called")
134n/a def __isub__(self, val):
135n/a output.append("__isub__ called")
136n/a return self
137n/a
138n/a def __mul__(self, val):
139n/a output.append("__mul__ called")
140n/a def __rmul__(self, val):
141n/a output.append("__rmul__ called")
142n/a def __imul__(self, val):
143n/a output.append("__imul__ called")
144n/a return self
145n/a
146n/a def __matmul__(self, val):
147n/a output.append("__matmul__ called")
148n/a def __rmatmul__(self, val):
149n/a output.append("__rmatmul__ called")
150n/a def __imatmul__(self, val):
151n/a output.append("__imatmul__ called")
152n/a return self
153n/a
154n/a def __floordiv__(self, val):
155n/a output.append("__floordiv__ called")
156n/a return self
157n/a def __ifloordiv__(self, val):
158n/a output.append("__ifloordiv__ called")
159n/a return self
160n/a def __rfloordiv__(self, val):
161n/a output.append("__rfloordiv__ called")
162n/a return self
163n/a
164n/a def __truediv__(self, val):
165n/a output.append("__truediv__ called")
166n/a return self
167n/a def __rtruediv__(self, val):
168n/a output.append("__rtruediv__ called")
169n/a return self
170n/a def __itruediv__(self, val):
171n/a output.append("__itruediv__ called")
172n/a return self
173n/a
174n/a def __mod__(self, val):
175n/a output.append("__mod__ called")
176n/a def __rmod__(self, val):
177n/a output.append("__rmod__ called")
178n/a def __imod__(self, val):
179n/a output.append("__imod__ called")
180n/a return self
181n/a
182n/a def __pow__(self, val):
183n/a output.append("__pow__ called")
184n/a def __rpow__(self, val):
185n/a output.append("__rpow__ called")
186n/a def __ipow__(self, val):
187n/a output.append("__ipow__ called")
188n/a return self
189n/a
190n/a def __or__(self, val):
191n/a output.append("__or__ called")
192n/a def __ror__(self, val):
193n/a output.append("__ror__ called")
194n/a def __ior__(self, val):
195n/a output.append("__ior__ called")
196n/a return self
197n/a
198n/a def __and__(self, val):
199n/a output.append("__and__ called")
200n/a def __rand__(self, val):
201n/a output.append("__rand__ called")
202n/a def __iand__(self, val):
203n/a output.append("__iand__ called")
204n/a return self
205n/a
206n/a def __xor__(self, val):
207n/a output.append("__xor__ called")
208n/a def __rxor__(self, val):
209n/a output.append("__rxor__ called")
210n/a def __ixor__(self, val):
211n/a output.append("__ixor__ called")
212n/a return self
213n/a
214n/a def __rshift__(self, val):
215n/a output.append("__rshift__ called")
216n/a def __rrshift__(self, val):
217n/a output.append("__rrshift__ called")
218n/a def __irshift__(self, val):
219n/a output.append("__irshift__ called")
220n/a return self
221n/a
222n/a def __lshift__(self, val):
223n/a output.append("__lshift__ called")
224n/a def __rlshift__(self, val):
225n/a output.append("__rlshift__ called")
226n/a def __ilshift__(self, val):
227n/a output.append("__ilshift__ called")
228n/a return self
229n/a
230n/a x = testall()
231n/a x + 1
232n/a 1 + x
233n/a x += 1
234n/a
235n/a x - 1
236n/a 1 - x
237n/a x -= 1
238n/a
239n/a x * 1
240n/a 1 * x
241n/a x *= 1
242n/a
243n/a x @ 1
244n/a 1 @ x
245n/a x @= 1
246n/a
247n/a x / 1
248n/a 1 / x
249n/a x /= 1
250n/a
251n/a x // 1
252n/a 1 // x
253n/a x //= 1
254n/a
255n/a x % 1
256n/a 1 % x
257n/a x %= 1
258n/a
259n/a x ** 1
260n/a 1 ** x
261n/a x **= 1
262n/a
263n/a x | 1
264n/a 1 | x
265n/a x |= 1
266n/a
267n/a x & 1
268n/a 1 & x
269n/a x &= 1
270n/a
271n/a x ^ 1
272n/a 1 ^ x
273n/a x ^= 1
274n/a
275n/a x >> 1
276n/a 1 >> x
277n/a x >>= 1
278n/a
279n/a x << 1
280n/a 1 << x
281n/a x <<= 1
282n/a
283n/a test_self.assertEqual(output, '''\
284n/a__add__ called
285n/a__radd__ called
286n/a__iadd__ called
287n/a__sub__ called
288n/a__rsub__ called
289n/a__isub__ called
290n/a__mul__ called
291n/a__rmul__ called
292n/a__imul__ called
293n/a__matmul__ called
294n/a__rmatmul__ called
295n/a__imatmul__ called
296n/a__truediv__ called
297n/a__rtruediv__ called
298n/a__itruediv__ called
299n/a__floordiv__ called
300n/a__rfloordiv__ called
301n/a__ifloordiv__ called
302n/a__mod__ called
303n/a__rmod__ called
304n/a__imod__ called
305n/a__pow__ called
306n/a__rpow__ called
307n/a__ipow__ called
308n/a__or__ called
309n/a__ror__ called
310n/a__ior__ called
311n/a__and__ called
312n/a__rand__ called
313n/a__iand__ called
314n/a__xor__ called
315n/a__rxor__ called
316n/a__ixor__ called
317n/a__rshift__ called
318n/a__rrshift__ called
319n/a__irshift__ called
320n/a__lshift__ called
321n/a__rlshift__ called
322n/a__ilshift__ called
323n/a'''.splitlines())
324n/a
325n/aif __name__ == '__main__':
326n/a unittest.main()