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