test_ipdoctest.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """Tests for the ipdoctest machinery itself.
  2. Note: in a file named test_X, functions whose only test is their docstring (as
  3. a doctest) and which have no test functionality of their own, should be called
  4. 'doctest_foo' instead of 'test_foo', otherwise they get double-counted (the
  5. empty function call is counted as a test, which just inflates tests numbers
  6. artificially).
  7. """
  8. from IPython.utils.py3compat import doctest_refactor_print
  9. @doctest_refactor_print
  10. def doctest_simple():
  11. """ipdoctest must handle simple inputs
  12. In [1]: 1
  13. Out[1]: 1
  14. In [2]: print 1
  15. 1
  16. """
  17. @doctest_refactor_print
  18. def doctest_multiline1():
  19. """The ipdoctest machinery must handle multiline examples gracefully.
  20. In [2]: for i in range(4):
  21. ...: print i
  22. ...:
  23. 0
  24. 1
  25. 2
  26. 3
  27. """
  28. @doctest_refactor_print
  29. def doctest_multiline2():
  30. """Multiline examples that define functions and print output.
  31. In [7]: def f(x):
  32. ...: return x+1
  33. ...:
  34. In [8]: f(1)
  35. Out[8]: 2
  36. In [9]: def g(x):
  37. ...: print 'x is:',x
  38. ...:
  39. In [10]: g(1)
  40. x is: 1
  41. In [11]: g('hello')
  42. x is: hello
  43. """
  44. def doctest_multiline3():
  45. """Multiline examples with blank lines.
  46. In [12]: def h(x):
  47. ....: if x>1:
  48. ....: return x**2
  49. ....: # To leave a blank line in the input, you must mark it
  50. ....: # with a comment character:
  51. ....: #
  52. ....: # otherwise the doctest parser gets confused.
  53. ....: else:
  54. ....: return -1
  55. ....:
  56. In [13]: h(5)
  57. Out[13]: 25
  58. In [14]: h(1)
  59. Out[14]: -1
  60. In [15]: h(0)
  61. Out[15]: -1
  62. """