move sections
[python/dscho.git] / Lib / test / test_doctest2.py
blob8c043baf5d10963850e7240a75582016f4bba473
1 # -*- coding: utf-8 -*-
2 u"""A module to test whether doctest recognizes some 2.2 features,
3 like static and class methods.
5 >>> print 'yup' # 1
6 yup
8 We include some (random) encoded (utf-8) text in the text surrounding
9 the example. It should be ignored:
11 ЉЊЈЁЂ
13 """
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")
21 class C(object):
22 u"""Class C.
24 >>> print C() # 2
28 We include some (random) encoded (utf-8) text in the text surrounding
29 the example. It should be ignored:
31 ЉЊЈЁЂ
33 """
35 def __init__(self):
36 """C.__init__.
38 >>> print C() # 3
40 """
42 def __str__(self):
43 """
44 >>> print C() # 4
46 """
47 return "42"
49 class D(object):
50 """A nested D class.
52 >>> print "In D!" # 5
53 In D!
54 """
56 def nested(self):
57 """
58 >>> print 3 # 6
60 """
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
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
80 x = property(getx, setx, doc="""\
81 >>> c = C() # 13
82 >>> c.x = 12 # 14
83 >>> print c.x # 15
84 -12
85 """)
87 @staticmethod
88 def statm():
89 """
90 A static method.
92 >>> print C.statm() # 16
93 666
94 >>> print C().statm() # 17
95 666
96 """
97 return 666
99 @classmethod
100 def clsm(cls, val):
102 A class method.
104 >>> print C.clsm(22) # 18
106 >>> print C().clsm(23) # 19
109 return val
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))
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 *
123 if __name__ == '__main__':
124 test_main()