Jim Fulton | 7d42878 | 2004-10-13 14:15:32 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 2 | """A module to test whether doctest recognizes some 2.2 features, |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 3 | like static and class methods. |
4 | |||||
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 5 | >>> print('yup') # 1 |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 6 | yup |
Jim Fulton | 7d42878 | 2004-10-13 14:15:32 +0000 | [diff] [blame] | 7 | |
8 | We include some (random) encoded (utf-8) text in the text surrounding | ||||
9 | the example. It should be ignored: | ||||
10 | |||||
11 | ЉЊЈЁЂ | ||||
12 | |||||
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 13 | """ |
14 | |||||
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 15 | import sys |
16 | import unittest | ||||
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 17 | from test import support |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 18 | if sys.flags.optimize >= 2: |
19 | raise unittest.SkipTest("Cannot test docstrings with -O2") | ||||
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 20 | |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 21 | class C(object): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 22 | """Class C. |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 24 | >>> print(C()) # 2 |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 25 | 42 |
Jim Fulton | 7d42878 | 2004-10-13 14:15:32 +0000 | [diff] [blame] | 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 | |||||
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 33 | """ |
34 | |||||
35 | def __init__(self): | ||||
36 | """C.__init__. | ||||
37 | |||||
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 38 | >>> print(C()) # 3 |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 39 | 42 |
40 | """ | ||||
41 | |||||
42 | def __str__(self): | ||||
43 | """ | ||||
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 44 | >>> print(C()) # 4 |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 45 | 42 |
46 | """ | ||||
47 | return "42" | ||||
48 | |||||
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 49 | class D(object): |
50 | """A nested D class. | ||||
51 | |||||
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 52 | >>> print("In D!") # 5 |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 53 | In D! |
54 | """ | ||||
55 | |||||
56 | def nested(self): | ||||
57 | """ | ||||
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 58 | >>> print(3) # 6 |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 59 | 3 |
60 | """ | ||||
61 | |||||
62 | def getx(self): | ||||
63 | """ | ||||
64 | >>> c = C() # 7 | ||||
65 | >>> c.x = 12 # 8 | ||||
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 66 | >>> print(c.x) # 9 |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 67 | -12 |
68 | """ | ||||
69 | return -self._x | ||||
70 | |||||
71 | def setx(self, value): | ||||
72 | """ | ||||
73 | >>> c = C() # 10 | ||||
74 | >>> c.x = 12 # 11 | ||||
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 75 | >>> print(c.x) # 12 |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 76 | -12 |
77 | """ | ||||
78 | self._x = value | ||||
79 | |||||
80 | x = property(getx, setx, doc="""\ | ||||
81 | >>> c = C() # 13 | ||||
82 | >>> c.x = 12 # 14 | ||||
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 83 | >>> print(c.x) # 15 |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 84 | -12 |
85 | """) | ||||
86 | |||||
Guido van Rossum | 5a8a037 | 2005-01-16 00:25:31 +0000 | [diff] [blame] | 87 | @staticmethod |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 88 | def statm(): |
89 | """ | ||||
90 | A static method. | ||||
91 | |||||
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 92 | >>> print(C.statm()) # 16 |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 93 | 666 |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 94 | >>> print(C().statm()) # 17 |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 95 | 666 |
96 | """ | ||||
97 | return 666 | ||||
98 | |||||
Guido van Rossum | 5a8a037 | 2005-01-16 00:25:31 +0000 | [diff] [blame] | 99 | @classmethod |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 100 | def clsm(cls, val): |
101 | """ | ||||
102 | A class method. | ||||
103 | |||||
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 104 | >>> print(C.clsm(22)) # 18 |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 105 | 22 |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 106 | >>> print(C().clsm(23)) # 19 |
Tim Peters | 1b0e549 | 2001-10-03 04:15:28 +0000 | [diff] [blame] | 107 | 23 |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 108 | """ |
Tim Peters | 1b0e549 | 2001-10-03 04:15:28 +0000 | [diff] [blame] | 109 | return val |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 110 | |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 111 | def test_main(): |
Barry Warsaw | 408b6d3 | 2002-07-30 23:27:12 +0000 | [diff] [blame] | 112 | from test import test_doctest2 |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 113 | EXPECTED = 19 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 114 | f, t = support.run_doctest(test_doctest2) |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 115 | if t != EXPECTED: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 116 | raise support.TestFailed("expected %d tests to run, not %d" % |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 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() |