blob: 8c043baf5d10963850e7240a75582016f4bba473 [file] [log] [blame]
Jim Fulton7d428782004-10-13 14:15:32 +00001# -*- coding: utf-8 -*-
2u"""A module to test whether doctest recognizes some 2.2 features,
Tim Peters17111f32001-10-03 04:08:26 +00003like static and class methods.
4
5>>> print 'yup' # 1
6yup
Jim Fulton7d428782004-10-13 14:15:32 +00007
8We include some (random) encoded (utf-8) text in the text surrounding
9the example. It should be ignored:
10
11ЉЊЈЁЂ
12
Tim Peters17111f32001-10-03 04:08:26 +000013"""
14
R. David Murrayf28fd242010-02-23 00:24:49 +000015import sys
16import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +000017from test import test_support
R. David Murrayf28fd242010-02-23 00:24:49 +000018if sys.flags.optimize >= 2:
19 raise unittest.SkipTest("Cannot test docstrings with -O2")
Tim Peters17111f32001-10-03 04:08:26 +000020
Tim Peters17111f32001-10-03 04:08:26 +000021class C(object):
Jim Fulton7d428782004-10-13 14:15:32 +000022 u"""Class C.
Tim Peters17111f32001-10-03 04:08:26 +000023
24 >>> print C() # 2
25 42
Jim Fulton7d428782004-10-13 14:15:32 +000026
27
28 We include some (random) encoded (utf-8) text in the text surrounding
29 the example. It should be ignored:
30
31 ЉЊЈЁЂ
32
Tim Peters17111f32001-10-03 04:08:26 +000033 """
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
Tim Peters17111f32001-10-03 04:08:26 +000049 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
Guido van Rossum5a8a0372005-01-16 00:25:31 +000087 @staticmethod
Tim Peters17111f32001-10-03 04:08:26 +000088 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
Guido van Rossum5a8a0372005-01-16 00:25:31 +000099 @classmethod
Tim Peters17111f32001-10-03 04:08:26 +0000100 def clsm(cls, val):
101 """
102 A class method.
103
104 >>> print C.clsm(22) # 18
105 22
Tim Peters1b0e5492001-10-03 04:15:28 +0000106 >>> print C().clsm(23) # 19
107 23
Tim Peters17111f32001-10-03 04:08:26 +0000108 """
Tim Peters1b0e5492001-10-03 04:15:28 +0000109 return val
Tim Peters17111f32001-10-03 04:08:26 +0000110
Tim Peters17111f32001-10-03 04:08:26 +0000111def test_main():
Barry Warsaw408b6d32002-07-30 23:27:12 +0000112 from test import test_doctest2
Tim Peters2f93e282001-10-04 05:27:00 +0000113 EXPECTED = 19
Tim Peters17111f32001-10-03 04:08:26 +0000114 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.
121from doctest import *
122
123if __name__ == '__main__':
124 test_main()