blob: 347a143641071d87e45a63b42297960617ba2986 [file] [log] [blame]
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001"""A module to test whether doctest recognizes some 2.2 features,
Tim Peters17111f32001-10-03 04:08:26 +00002like static and class methods.
3
Guido van Rossum7131f842007-02-09 20:13:25 +00004>>> print('yup') # 1
Tim Peters17111f32001-10-03 04:08:26 +00005yup
Jim Fulton7d428782004-10-13 14:15:32 +00006
7We include some (random) encoded (utf-8) text in the text surrounding
8the example. It should be ignored:
9
10ЉЊЈЁЂ
11
Tim Peters17111f32001-10-03 04:08:26 +000012"""
13
R. David Murray378c0cf2010-02-24 01:46:21 +000014import sys
15import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +000016from test import support
R. David Murray378c0cf2010-02-24 01:46:21 +000017if sys.flags.optimize >= 2:
18 raise unittest.SkipTest("Cannot test docstrings with -O2")
Tim Peters17111f32001-10-03 04:08:26 +000019
Tim Peters17111f32001-10-03 04:08:26 +000020class C(object):
Guido van Rossumef87d6e2007-05-02 19:09:54 +000021 """Class C.
Tim Peters17111f32001-10-03 04:08:26 +000022
Guido van Rossum7131f842007-02-09 20:13:25 +000023 >>> print(C()) # 2
Tim Peters17111f32001-10-03 04:08:26 +000024 42
Jim Fulton7d428782004-10-13 14:15:32 +000025
26
27 We include some (random) encoded (utf-8) text in the text surrounding
28 the example. It should be ignored:
29
30 ЉЊЈЁЂ
31
Tim Peters17111f32001-10-03 04:08:26 +000032 """
33
34 def __init__(self):
35 """C.__init__.
36
Guido van Rossum7131f842007-02-09 20:13:25 +000037 >>> print(C()) # 3
Tim Peters17111f32001-10-03 04:08:26 +000038 42
39 """
40
41 def __str__(self):
42 """
Guido van Rossum7131f842007-02-09 20:13:25 +000043 >>> print(C()) # 4
Tim Peters17111f32001-10-03 04:08:26 +000044 42
45 """
46 return "42"
47
Tim Peters17111f32001-10-03 04:08:26 +000048 class D(object):
49 """A nested D class.
50
Guido van Rossum7131f842007-02-09 20:13:25 +000051 >>> print("In D!") # 5
Tim Peters17111f32001-10-03 04:08:26 +000052 In D!
53 """
54
55 def nested(self):
56 """
Guido van Rossum7131f842007-02-09 20:13:25 +000057 >>> print(3) # 6
Tim Peters17111f32001-10-03 04:08:26 +000058 3
59 """
60
61 def getx(self):
62 """
63 >>> c = C() # 7
64 >>> c.x = 12 # 8
Guido van Rossum7131f842007-02-09 20:13:25 +000065 >>> print(c.x) # 9
Tim Peters17111f32001-10-03 04:08:26 +000066 -12
67 """
68 return -self._x
69
70 def setx(self, value):
71 """
72 >>> c = C() # 10
73 >>> c.x = 12 # 11
Guido van Rossum7131f842007-02-09 20:13:25 +000074 >>> print(c.x) # 12
Tim Peters17111f32001-10-03 04:08:26 +000075 -12
76 """
77 self._x = value
78
79 x = property(getx, setx, doc="""\
80 >>> c = C() # 13
81 >>> c.x = 12 # 14
Guido van Rossum7131f842007-02-09 20:13:25 +000082 >>> print(c.x) # 15
Tim Peters17111f32001-10-03 04:08:26 +000083 -12
84 """)
85
Guido van Rossum5a8a0372005-01-16 00:25:31 +000086 @staticmethod
Tim Peters17111f32001-10-03 04:08:26 +000087 def statm():
88 """
89 A static method.
90
Guido van Rossum7131f842007-02-09 20:13:25 +000091 >>> print(C.statm()) # 16
Tim Peters17111f32001-10-03 04:08:26 +000092 666
Guido van Rossum7131f842007-02-09 20:13:25 +000093 >>> print(C().statm()) # 17
Tim Peters17111f32001-10-03 04:08:26 +000094 666
95 """
96 return 666
97
Guido van Rossum5a8a0372005-01-16 00:25:31 +000098 @classmethod
Tim Peters17111f32001-10-03 04:08:26 +000099 def clsm(cls, val):
100 """
101 A class method.
102
Guido van Rossum7131f842007-02-09 20:13:25 +0000103 >>> print(C.clsm(22)) # 18
Tim Peters17111f32001-10-03 04:08:26 +0000104 22
Guido van Rossum7131f842007-02-09 20:13:25 +0000105 >>> print(C().clsm(23)) # 19
Tim Peters1b0e5492001-10-03 04:15:28 +0000106 23
Tim Peters17111f32001-10-03 04:08:26 +0000107 """
Tim Peters1b0e5492001-10-03 04:15:28 +0000108 return val
Tim Peters17111f32001-10-03 04:08:26 +0000109
Tim Peters17111f32001-10-03 04:08:26 +0000110def test_main():
Barry Warsaw408b6d32002-07-30 23:27:12 +0000111 from test import test_doctest2
Tim Peters2f93e282001-10-04 05:27:00 +0000112 EXPECTED = 19
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000113 f, t = support.run_doctest(test_doctest2)
Tim Peters17111f32001-10-03 04:08:26 +0000114 if t != EXPECTED:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000115 raise support.TestFailed("expected %d tests to run, not %d" %
Tim Peters17111f32001-10-03 04:08:26 +0000116 (EXPECTED, t))
117
118# Pollute the namespace with a bunch of imported functions and classes,
119# to make sure they don't get tested.
120from doctest import *
121
122if __name__ == '__main__':
123 test_main()