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