/Lib/test/test_doctest2.py

http://unladen-swallow.googlecode.com/ · Python · 127 lines · 67 code · 14 blank · 46 comment · 4 complexity · 71d68d27a3d2cc462131ff8fcba4191a MD5 · raw file

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