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