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