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