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

Python code coverage for Lib/test/test_class.py

#countcontent
1n/a"Test the functionality of Python classes implementing operators."
2n/a
3n/aimport unittest
4n/a
5n/a
6n/atestmeths = [
7n/a
8n/a# Binary operations
9n/a "add",
10n/a "radd",
11n/a "sub",
12n/a "rsub",
13n/a "mul",
14n/a "rmul",
15n/a "matmul",
16n/a "rmatmul",
17n/a "truediv",
18n/a "rtruediv",
19n/a "floordiv",
20n/a "rfloordiv",
21n/a "mod",
22n/a "rmod",
23n/a "divmod",
24n/a "rdivmod",
25n/a "pow",
26n/a "rpow",
27n/a "rshift",
28n/a "rrshift",
29n/a "lshift",
30n/a "rlshift",
31n/a "and",
32n/a "rand",
33n/a "or",
34n/a "ror",
35n/a "xor",
36n/a "rxor",
37n/a
38n/a# List/dict operations
39n/a "contains",
40n/a "getitem",
41n/a "setitem",
42n/a "delitem",
43n/a
44n/a# Unary operations
45n/a "neg",
46n/a "pos",
47n/a "abs",
48n/a
49n/a# generic operations
50n/a "init",
51n/a ]
52n/a
53n/a# These need to return something other than None
54n/a# "hash",
55n/a# "str",
56n/a# "repr",
57n/a# "int",
58n/a# "float",
59n/a
60n/a# These are separate because they can influence the test of other methods.
61n/a# "getattr",
62n/a# "setattr",
63n/a# "delattr",
64n/a
65n/acallLst = []
66n/adef trackCall(f):
67n/a def track(*args, **kwargs):
68n/a callLst.append((f.__name__, args))
69n/a return f(*args, **kwargs)
70n/a return track
71n/a
72n/astatictests = """
73n/a@trackCall
74n/adef __hash__(self, *args):
75n/a return hash(id(self))
76n/a
77n/a@trackCall
78n/adef __str__(self, *args):
79n/a return "AllTests"
80n/a
81n/a@trackCall
82n/adef __repr__(self, *args):
83n/a return "AllTests"
84n/a
85n/a@trackCall
86n/adef __int__(self, *args):
87n/a return 1
88n/a
89n/a@trackCall
90n/adef __index__(self, *args):
91n/a return 1
92n/a
93n/a@trackCall
94n/adef __float__(self, *args):
95n/a return 1.0
96n/a
97n/a@trackCall
98n/adef __eq__(self, *args):
99n/a return True
100n/a
101n/a@trackCall
102n/adef __ne__(self, *args):
103n/a return False
104n/a
105n/a@trackCall
106n/adef __lt__(self, *args):
107n/a return False
108n/a
109n/a@trackCall
110n/adef __le__(self, *args):
111n/a return True
112n/a
113n/a@trackCall
114n/adef __gt__(self, *args):
115n/a return False
116n/a
117n/a@trackCall
118n/adef __ge__(self, *args):
119n/a return True
120n/a"""
121n/a
122n/a# Synthesize all the other AllTests methods from the names in testmeths.
123n/a
124n/amethod_template = """\
125n/a@trackCall
126n/adef __%s__(self, *args):
127n/a pass
128n/a"""
129n/a
130n/ad = {}
131n/aexec(statictests, globals(), d)
132n/afor method in testmeths:
133n/a exec(method_template % method, globals(), d)
134n/aAllTests = type("AllTests", (object,), d)
135n/adel d, statictests, method, method_template
136n/a
137n/aclass ClassTests(unittest.TestCase):
138n/a def setUp(self):
139n/a callLst[:] = []
140n/a
141n/a def assertCallStack(self, expected_calls):
142n/a actualCallList = callLst[:] # need to copy because the comparison below will add
143n/a # additional calls to callLst
144n/a if expected_calls != actualCallList:
145n/a self.fail("Expected call list:\n %s\ndoes not match actual call list\n %s" %
146n/a (expected_calls, actualCallList))
147n/a
148n/a def testInit(self):
149n/a foo = AllTests()
150n/a self.assertCallStack([("__init__", (foo,))])
151n/a
152n/a def testBinaryOps(self):
153n/a testme = AllTests()
154n/a # Binary operations
155n/a
156n/a callLst[:] = []
157n/a testme + 1
158n/a self.assertCallStack([("__add__", (testme, 1))])
159n/a
160n/a callLst[:] = []
161n/a 1 + testme
162n/a self.assertCallStack([("__radd__", (testme, 1))])
163n/a
164n/a callLst[:] = []
165n/a testme - 1
166n/a self.assertCallStack([("__sub__", (testme, 1))])
167n/a
168n/a callLst[:] = []
169n/a 1 - testme
170n/a self.assertCallStack([("__rsub__", (testme, 1))])
171n/a
172n/a callLst[:] = []
173n/a testme * 1
174n/a self.assertCallStack([("__mul__", (testme, 1))])
175n/a
176n/a callLst[:] = []
177n/a 1 * testme
178n/a self.assertCallStack([("__rmul__", (testme, 1))])
179n/a
180n/a callLst[:] = []
181n/a testme @ 1
182n/a self.assertCallStack([("__matmul__", (testme, 1))])
183n/a
184n/a callLst[:] = []
185n/a 1 @ testme
186n/a self.assertCallStack([("__rmatmul__", (testme, 1))])
187n/a
188n/a callLst[:] = []
189n/a testme / 1
190n/a self.assertCallStack([("__truediv__", (testme, 1))])
191n/a
192n/a
193n/a callLst[:] = []
194n/a 1 / testme
195n/a self.assertCallStack([("__rtruediv__", (testme, 1))])
196n/a
197n/a callLst[:] = []
198n/a testme // 1
199n/a self.assertCallStack([("__floordiv__", (testme, 1))])
200n/a
201n/a
202n/a callLst[:] = []
203n/a 1 // testme
204n/a self.assertCallStack([("__rfloordiv__", (testme, 1))])
205n/a
206n/a callLst[:] = []
207n/a testme % 1
208n/a self.assertCallStack([("__mod__", (testme, 1))])
209n/a
210n/a callLst[:] = []
211n/a 1 % testme
212n/a self.assertCallStack([("__rmod__", (testme, 1))])
213n/a
214n/a
215n/a callLst[:] = []
216n/a divmod(testme,1)
217n/a self.assertCallStack([("__divmod__", (testme, 1))])
218n/a
219n/a callLst[:] = []
220n/a divmod(1, testme)
221n/a self.assertCallStack([("__rdivmod__", (testme, 1))])
222n/a
223n/a callLst[:] = []
224n/a testme ** 1
225n/a self.assertCallStack([("__pow__", (testme, 1))])
226n/a
227n/a callLst[:] = []
228n/a 1 ** testme
229n/a self.assertCallStack([("__rpow__", (testme, 1))])
230n/a
231n/a callLst[:] = []
232n/a testme >> 1
233n/a self.assertCallStack([("__rshift__", (testme, 1))])
234n/a
235n/a callLst[:] = []
236n/a 1 >> testme
237n/a self.assertCallStack([("__rrshift__", (testme, 1))])
238n/a
239n/a callLst[:] = []
240n/a testme << 1
241n/a self.assertCallStack([("__lshift__", (testme, 1))])
242n/a
243n/a callLst[:] = []
244n/a 1 << testme
245n/a self.assertCallStack([("__rlshift__", (testme, 1))])
246n/a
247n/a callLst[:] = []
248n/a testme & 1
249n/a self.assertCallStack([("__and__", (testme, 1))])
250n/a
251n/a callLst[:] = []
252n/a 1 & testme
253n/a self.assertCallStack([("__rand__", (testme, 1))])
254n/a
255n/a callLst[:] = []
256n/a testme | 1
257n/a self.assertCallStack([("__or__", (testme, 1))])
258n/a
259n/a callLst[:] = []
260n/a 1 | testme
261n/a self.assertCallStack([("__ror__", (testme, 1))])
262n/a
263n/a callLst[:] = []
264n/a testme ^ 1
265n/a self.assertCallStack([("__xor__", (testme, 1))])
266n/a
267n/a callLst[:] = []
268n/a 1 ^ testme
269n/a self.assertCallStack([("__rxor__", (testme, 1))])
270n/a
271n/a def testListAndDictOps(self):
272n/a testme = AllTests()
273n/a
274n/a # List/dict operations
275n/a
276n/a class Empty: pass
277n/a
278n/a try:
279n/a 1 in Empty()
280n/a self.fail('failed, should have raised TypeError')
281n/a except TypeError:
282n/a pass
283n/a
284n/a callLst[:] = []
285n/a 1 in testme
286n/a self.assertCallStack([('__contains__', (testme, 1))])
287n/a
288n/a callLst[:] = []
289n/a testme[1]
290n/a self.assertCallStack([('__getitem__', (testme, 1))])
291n/a
292n/a callLst[:] = []
293n/a testme[1] = 1
294n/a self.assertCallStack([('__setitem__', (testme, 1, 1))])
295n/a
296n/a callLst[:] = []
297n/a del testme[1]
298n/a self.assertCallStack([('__delitem__', (testme, 1))])
299n/a
300n/a callLst[:] = []
301n/a testme[:42]
302n/a self.assertCallStack([('__getitem__', (testme, slice(None, 42)))])
303n/a
304n/a callLst[:] = []
305n/a testme[:42] = "The Answer"
306n/a self.assertCallStack([('__setitem__', (testme, slice(None, 42),
307n/a "The Answer"))])
308n/a
309n/a callLst[:] = []
310n/a del testme[:42]
311n/a self.assertCallStack([('__delitem__', (testme, slice(None, 42)))])
312n/a
313n/a callLst[:] = []
314n/a testme[2:1024:10]
315n/a self.assertCallStack([('__getitem__', (testme, slice(2, 1024, 10)))])
316n/a
317n/a callLst[:] = []
318n/a testme[2:1024:10] = "A lot"
319n/a self.assertCallStack([('__setitem__', (testme, slice(2, 1024, 10),
320n/a "A lot"))])
321n/a callLst[:] = []
322n/a del testme[2:1024:10]
323n/a self.assertCallStack([('__delitem__', (testme, slice(2, 1024, 10)))])
324n/a
325n/a callLst[:] = []
326n/a testme[:42, ..., :24:, 24, 100]
327n/a self.assertCallStack([('__getitem__', (testme, (slice(None, 42, None),
328n/a Ellipsis,
329n/a slice(None, 24, None),
330n/a 24, 100)))])
331n/a callLst[:] = []
332n/a testme[:42, ..., :24:, 24, 100] = "Strange"
333n/a self.assertCallStack([('__setitem__', (testme, (slice(None, 42, None),
334n/a Ellipsis,
335n/a slice(None, 24, None),
336n/a 24, 100), "Strange"))])
337n/a callLst[:] = []
338n/a del testme[:42, ..., :24:, 24, 100]
339n/a self.assertCallStack([('__delitem__', (testme, (slice(None, 42, None),
340n/a Ellipsis,
341n/a slice(None, 24, None),
342n/a 24, 100)))])
343n/a
344n/a def testUnaryOps(self):
345n/a testme = AllTests()
346n/a
347n/a callLst[:] = []
348n/a -testme
349n/a self.assertCallStack([('__neg__', (testme,))])
350n/a callLst[:] = []
351n/a +testme
352n/a self.assertCallStack([('__pos__', (testme,))])
353n/a callLst[:] = []
354n/a abs(testme)
355n/a self.assertCallStack([('__abs__', (testme,))])
356n/a callLst[:] = []
357n/a int(testme)
358n/a self.assertCallStack([('__int__', (testme,))])
359n/a callLst[:] = []
360n/a float(testme)
361n/a self.assertCallStack([('__float__', (testme,))])
362n/a callLst[:] = []
363n/a oct(testme)
364n/a self.assertCallStack([('__index__', (testme,))])
365n/a callLst[:] = []
366n/a hex(testme)
367n/a self.assertCallStack([('__index__', (testme,))])
368n/a
369n/a
370n/a def testMisc(self):
371n/a testme = AllTests()
372n/a
373n/a callLst[:] = []
374n/a hash(testme)
375n/a self.assertCallStack([('__hash__', (testme,))])
376n/a
377n/a callLst[:] = []
378n/a repr(testme)
379n/a self.assertCallStack([('__repr__', (testme,))])
380n/a
381n/a callLst[:] = []
382n/a str(testme)
383n/a self.assertCallStack([('__str__', (testme,))])
384n/a
385n/a callLst[:] = []
386n/a testme == 1
387n/a self.assertCallStack([('__eq__', (testme, 1))])
388n/a
389n/a callLst[:] = []
390n/a testme < 1
391n/a self.assertCallStack([('__lt__', (testme, 1))])
392n/a
393n/a callLst[:] = []
394n/a testme > 1
395n/a self.assertCallStack([('__gt__', (testme, 1))])
396n/a
397n/a callLst[:] = []
398n/a testme != 1
399n/a self.assertCallStack([('__ne__', (testme, 1))])
400n/a
401n/a callLst[:] = []
402n/a 1 == testme
403n/a self.assertCallStack([('__eq__', (1, testme))])
404n/a
405n/a callLst[:] = []
406n/a 1 < testme
407n/a self.assertCallStack([('__gt__', (1, testme))])
408n/a
409n/a callLst[:] = []
410n/a 1 > testme
411n/a self.assertCallStack([('__lt__', (1, testme))])
412n/a
413n/a callLst[:] = []
414n/a 1 != testme
415n/a self.assertCallStack([('__ne__', (1, testme))])
416n/a
417n/a
418n/a def testGetSetAndDel(self):
419n/a # Interfering tests
420n/a class ExtraTests(AllTests):
421n/a @trackCall
422n/a def __getattr__(self, *args):
423n/a return "SomeVal"
424n/a
425n/a @trackCall
426n/a def __setattr__(self, *args):
427n/a pass
428n/a
429n/a @trackCall
430n/a def __delattr__(self, *args):
431n/a pass
432n/a
433n/a testme = ExtraTests()
434n/a
435n/a callLst[:] = []
436n/a testme.spam
437n/a self.assertCallStack([('__getattr__', (testme, "spam"))])
438n/a
439n/a callLst[:] = []
440n/a testme.eggs = "spam, spam, spam and ham"
441n/a self.assertCallStack([('__setattr__', (testme, "eggs",
442n/a "spam, spam, spam and ham"))])
443n/a
444n/a callLst[:] = []
445n/a del testme.cardinal
446n/a self.assertCallStack([('__delattr__', (testme, "cardinal"))])
447n/a
448n/a def testDel(self):
449n/a x = []
450n/a
451n/a class DelTest:
452n/a def __del__(self):
453n/a x.append("crab people, crab people")
454n/a testme = DelTest()
455n/a del testme
456n/a import gc
457n/a gc.collect()
458n/a self.assertEqual(["crab people, crab people"], x)
459n/a
460n/a def testBadTypeReturned(self):
461n/a # return values of some method are type-checked
462n/a class BadTypeClass:
463n/a def __int__(self):
464n/a return None
465n/a __float__ = __int__
466n/a __complex__ = __int__
467n/a __str__ = __int__
468n/a __repr__ = __int__
469n/a __bytes__ = __int__
470n/a __bool__ = __int__
471n/a __index__ = __int__
472n/a def index(x):
473n/a return [][x]
474n/a
475n/a for f in [float, complex, str, repr, bytes, bin, oct, hex, bool, index]:
476n/a self.assertRaises(TypeError, f, BadTypeClass())
477n/a
478n/a def testHashStuff(self):
479n/a # Test correct errors from hash() on objects with comparisons but
480n/a # no __hash__
481n/a
482n/a class C0:
483n/a pass
484n/a
485n/a hash(C0()) # This should work; the next two should raise TypeError
486n/a
487n/a class C2:
488n/a def __eq__(self, other): return 1
489n/a
490n/a self.assertRaises(TypeError, hash, C2())
491n/a
492n/a
493n/a def testSFBug532646(self):
494n/a # Test for SF bug 532646
495n/a
496n/a class A:
497n/a pass
498n/a A.__call__ = A()
499n/a a = A()
500n/a
501n/a try:
502n/a a() # This should not segfault
503n/a except RecursionError:
504n/a pass
505n/a else:
506n/a self.fail("Failed to raise RecursionError")
507n/a
508n/a def testForExceptionsRaisedInInstanceGetattr2(self):
509n/a # Tests for exceptions raised in instance_getattr2().
510n/a
511n/a def booh(self):
512n/a raise AttributeError("booh")
513n/a
514n/a class A:
515n/a a = property(booh)
516n/a try:
517n/a A().a # Raised AttributeError: A instance has no attribute 'a'
518n/a except AttributeError as x:
519n/a if str(x) != "booh":
520n/a self.fail("attribute error for A().a got masked: %s" % x)
521n/a
522n/a class E:
523n/a __eq__ = property(booh)
524n/a E() == E() # In debug mode, caused a C-level assert() to fail
525n/a
526n/a class I:
527n/a __init__ = property(booh)
528n/a try:
529n/a # In debug mode, printed XXX undetected error and
530n/a # raises AttributeError
531n/a I()
532n/a except AttributeError as x:
533n/a pass
534n/a else:
535n/a self.fail("attribute error for I.__init__ got masked")
536n/a
537n/a def testHashComparisonOfMethods(self):
538n/a # Test comparison and hash of methods
539n/a class A:
540n/a def __init__(self, x):
541n/a self.x = x
542n/a def f(self):
543n/a pass
544n/a def g(self):
545n/a pass
546n/a def __eq__(self, other):
547n/a return self.x == other.x
548n/a def __hash__(self):
549n/a return self.x
550n/a class B(A):
551n/a pass
552n/a
553n/a a1 = A(1)
554n/a a2 = A(2)
555n/a self.assertEqual(a1.f, a1.f)
556n/a self.assertNotEqual(a1.f, a2.f)
557n/a self.assertNotEqual(a1.f, a1.g)
558n/a self.assertEqual(a1.f, A(1).f)
559n/a self.assertEqual(hash(a1.f), hash(a1.f))
560n/a self.assertEqual(hash(a1.f), hash(A(1).f))
561n/a
562n/a self.assertNotEqual(A.f, a1.f)
563n/a self.assertNotEqual(A.f, A.g)
564n/a self.assertEqual(B.f, A.f)
565n/a self.assertEqual(hash(B.f), hash(A.f))
566n/a
567n/a # the following triggers a SystemError in 2.4
568n/a a = A(hash(A.f)^(-1))
569n/a hash(a.f)
570n/a
571n/aif __name__ == '__main__':
572n/a unittest.main()