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