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