»Core Development>Code coverage>Lib/test/test_doctest2.py

Python code coverage for Lib/test/test_doctest2.py

#countcontent
1n/a"""A module to test whether doctest recognizes some 2.2 features,
2n/alike static and class methods.
3n/a
4n/a>>> print('yup') # 1
5n/ayup
6n/a
7n/aWe include some (random) encoded (utf-8) text in the text surrounding
8n/athe example. It should be ignored:
9n/a
10n/aЉЊЈЁЂ
11n/a
12n/a"""
13n/a
14n/aimport sys
15n/aimport unittest
16n/afrom test import support
17n/aif sys.flags.optimize >= 2:
18n/a raise unittest.SkipTest("Cannot test docstrings with -O2")
19n/a
20n/aclass C(object):
21n/a """Class C.
22n/a
23n/a >>> print(C()) # 2
24n/a 42
25n/a
26n/a
27n/a We include some (random) encoded (utf-8) text in the text surrounding
28n/a the example. It should be ignored:
29n/a
30n/a ЉЊЈЁЂ
31n/a
32n/a """
33n/a
34n/a def __init__(self):
35n/a """C.__init__.
36n/a
37n/a >>> print(C()) # 3
38n/a 42
39n/a """
40n/a
41n/a def __str__(self):
42n/a """
43n/a >>> print(C()) # 4
44n/a 42
45n/a """
46n/a return "42"
47n/a
48n/a class D(object):
49n/a """A nested D class.
50n/a
51n/a >>> print("In D!") # 5
52n/a In D!
53n/a """
54n/a
55n/a def nested(self):
56n/a """
57n/a >>> print(3) # 6
58n/a 3
59n/a """
60n/a
61n/a def getx(self):
62n/a """
63n/a >>> c = C() # 7
64n/a >>> c.x = 12 # 8
65n/a >>> print(c.x) # 9
66n/a -12
67n/a """
68n/a return -self._x
69n/a
70n/a def setx(self, value):
71n/a """
72n/a >>> c = C() # 10
73n/a >>> c.x = 12 # 11
74n/a >>> print(c.x) # 12
75n/a -12
76n/a """
77n/a self._x = value
78n/a
79n/a x = property(getx, setx, doc="""\
80n/a >>> c = C() # 13
81n/a >>> c.x = 12 # 14
82n/a >>> print(c.x) # 15
83n/a -12
84n/a """)
85n/a
86n/a @staticmethod
87n/a def statm():
88n/a """
89n/a A static method.
90n/a
91n/a >>> print(C.statm()) # 16
92n/a 666
93n/a >>> print(C().statm()) # 17
94n/a 666
95n/a """
96n/a return 666
97n/a
98n/a @classmethod
99n/a def clsm(cls, val):
100n/a """
101n/a A class method.
102n/a
103n/a >>> print(C.clsm(22)) # 18
104n/a 22
105n/a >>> print(C().clsm(23)) # 19
106n/a 23
107n/a """
108n/a return val
109n/a
110n/adef test_main():
111n/a from test import test_doctest2
112n/a EXPECTED = 19
113n/a f, t = support.run_doctest(test_doctest2)
114n/a if t != EXPECTED:
115n/a raise support.TestFailed("expected %d tests to run, not %d" %
116n/a (EXPECTED, t))
117n/a
118n/a# Pollute the namespace with a bunch of imported functions and classes,
119n/a# to make sure they don't get tested.
120n/afrom doctest import *
121n/a
122n/aif __name__ == '__main__':
123n/a test_main()