blob: 8500ed557859ae9826bab3943029daa18083d7c1 [file] [log] [blame]
Jim Fulton7d428782004-10-13 14:15:32 +00001# -*- coding: utf-8 -*-
Guido van Rossumef87d6e2007-05-02 19:09:54 +00002"""A module to test whether doctest recognizes some 2.2 features,
Tim Peters17111f32001-10-03 04:08:26 +00003like static and class methods.
4
Guido van Rossum7131f842007-02-09 20:13:25 +00005>>> print('yup') # 1
Tim Peters17111f32001-10-03 04:08:26 +00006yup
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 Murray378c0cf2010-02-24 01:46:21 +000015import sys
16import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +000017from test import support
R. David Murray378c0cf2010-02-24 01:46:21 +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):
Guido van Rossumef87d6e2007-05-02 19:09:54 +000022 """Class C.
Tim Peters17111f32001-10-03 04:08:26 +000023
Guido van Rossum7131f842007-02-09 20:13:25 +000024 >>> print(C()) # 2
Tim Peters17111f32001-10-03 04:08:26 +000025 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
Guido van Rossum7131f842007-02-09 20:13:25 +000038 >>> print(C()) # 3
Tim Peters17111f32001-10-03 04:08:26 +000039 42
40 """
41
42 def __str__(self):
43 """
Guido van Rossum7131f842007-02-09 20:13:25 +000044 >>> print(C()) # 4
Tim Peters17111f32001-10-03 04:08:26 +000045 42
46 """
47 return "42"
48
Tim Peters17111f32001-10-03 04:08:26 +000049 class D(object):
50 """A nested D class.
51
Guido van Rossum7131f842007-02-09 20:13:25 +000052 >>> print("In D!") # 5
Tim Peters17111f32001-10-03 04:08:26 +000053 In D!
54 """
55
56 def nested(self):
57 """
Guido van Rossum7131f842007-02-09 20:13:25 +000058 >>> print(3) # 6
Tim Peters17111f32001-10-03 04:08:26 +000059 3
60 """
61
62 def getx(self):
63 """
64 >>> c = C() # 7
65 >>> c.x = 12 # 8
Guido van Rossum7131f842007-02-09 20:13:25 +000066 >>> print(c.x) # 9
Tim Peters17111f32001-10-03 04:08:26 +000067 -12
68 """
69 return -self._x
70
71 def setx(self, value):
72 """
73 >>> c = C() # 10
74 >>> c.x = 12 # 11
Guido van Rossum7131f842007-02-09 20:13:25 +000075 >>> print(c.x) # 12
Tim Peters17111f32001-10-03 04:08:26 +000076 -12
77 """
78 self._x = value
79
80 x = property(getx, setx, doc="""\
81 >>> c = C() # 13
82 >>> c.x = 12 # 14
Guido van Rossum7131f842007-02-09 20:13:25 +000083 >>> print(c.x) # 15
Tim Peters17111f32001-10-03 04:08:26 +000084 -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
Guido van Rossum7131f842007-02-09 20:13:25 +000092 >>> print(C.statm()) # 16
Tim Peters17111f32001-10-03 04:08:26 +000093 666
Guido van Rossum7131f842007-02-09 20:13:25 +000094 >>> print(C().statm()) # 17
Tim Peters17111f32001-10-03 04:08:26 +000095 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
Guido van Rossum7131f842007-02-09 20:13:25 +0000104 >>> print(C.clsm(22)) # 18
Tim Peters17111f32001-10-03 04:08:26 +0000105 22
Guido van Rossum7131f842007-02-09 20:13:25 +0000106 >>> print(C().clsm(23)) # 19
Tim Peters1b0e5492001-10-03 04:15:28 +0000107 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
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000114 f, t = support.run_doctest(test_doctest2)
Tim Peters17111f32001-10-03 04:08:26 +0000115 if t != EXPECTED:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000116 raise support.TestFailed("expected %d tests to run, not %d" %
Tim Peters17111f32001-10-03 04:08:26 +0000117 (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()