test_doctest2.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. u"""A module to test whether doctest recognizes some 2.2 features,
  3. like static and class methods.
  4. >>> print 'yup' # 1
  5. yup
  6. We include some (random) encoded (utf-8) text in the text surrounding
  7. the example. It should be ignored:
  8. ЉЊЈЁЂ
  9. """
  10. import sys
  11. import unittest
  12. from test import test_support
  13. if sys.flags.optimize >= 2:
  14. raise unittest.SkipTest("Cannot test docstrings with -O2")
  15. class C(object):
  16. u"""Class C.
  17. >>> print C() # 2
  18. 42
  19. We include some (random) encoded (utf-8) text in the text surrounding
  20. the example. It should be ignored:
  21. ЉЊЈЁЂ
  22. """
  23. def __init__(self):
  24. """C.__init__.
  25. >>> print C() # 3
  26. 42
  27. """
  28. def __str__(self):
  29. """
  30. >>> print C() # 4
  31. 42
  32. """
  33. return "42"
  34. class D(object):
  35. """A nested D class.
  36. >>> print "In D!" # 5
  37. In D!
  38. """
  39. def nested(self):
  40. """
  41. >>> print 3 # 6
  42. 3
  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. def setx(self, value):
  53. """
  54. >>> c = C() # 10
  55. >>> c.x = 12 # 11
  56. >>> print c.x # 12
  57. -12
  58. """
  59. self._x = value
  60. x = property(getx, setx, doc="""\
  61. >>> c = C() # 13
  62. >>> c.x = 12 # 14
  63. >>> print c.x # 15
  64. -12
  65. """)
  66. @staticmethod
  67. def statm():
  68. """
  69. A static method.
  70. >>> print C.statm() # 16
  71. 666
  72. >>> print C().statm() # 17
  73. 666
  74. """
  75. return 666
  76. @classmethod
  77. def clsm(cls, val):
  78. """
  79. A class method.
  80. >>> print C.clsm(22) # 18
  81. 22
  82. >>> print C().clsm(23) # 19
  83. 23
  84. """
  85. return val
  86. def test_main():
  87. from test import test_doctest2
  88. EXPECTED = 19
  89. f, t = test_support.run_doctest(test_doctest2)
  90. if t != EXPECTED:
  91. raise test_support.TestFailed("expected %d tests to run, not %d" %
  92. (EXPECTED, t))
  93. # Pollute the namespace with a bunch of imported functions and classes,
  94. # to make sure they don't get tested.
  95. from doctest import *
  96. if __name__ == '__main__':
  97. test_main()