test_ipdoctest.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. def doctest_simple():
  9. """ipdoctest must handle simple inputs
  10. In [1]: 1
  11. Out[1]: 1
  12. In [2]: print(1)
  13. 1
  14. """
  15. def doctest_multiline1():
  16. """The ipdoctest machinery must handle multiline examples gracefully.
  17. In [2]: for i in range(4):
  18. ...: print(i)
  19. ...:
  20. 0
  21. 1
  22. 2
  23. 3
  24. """
  25. def doctest_multiline2():
  26. """Multiline examples that define functions and print output.
  27. In [7]: def f(x):
  28. ...: return x+1
  29. ...:
  30. In [8]: f(1)
  31. Out[8]: 2
  32. In [9]: def g(x):
  33. ...: print('x is:',x)
  34. ...:
  35. In [10]: g(1)
  36. x is: 1
  37. In [11]: g('hello')
  38. x is: hello
  39. """
  40. def doctest_multiline3():
  41. """Multiline examples with blank lines.
  42. In [12]: def h(x):
  43. ....: if x>1:
  44. ....: return x**2
  45. ....: # To leave a blank line in the input, you must mark it
  46. ....: # with a comment character:
  47. ....: #
  48. ....: # otherwise the doctest parser gets confused.
  49. ....: else:
  50. ....: return -1
  51. ....:
  52. In [13]: h(5)
  53. Out[13]: 25
  54. In [14]: h(1)
  55. Out[14]: -1
  56. In [15]: h(0)
  57. Out[15]: -1
  58. """
  59. def doctest_builtin_underscore():
  60. """Defining builtins._ should not break anything outside the doctest
  61. while also should be working as expected inside the doctest.
  62. In [1]: import builtins
  63. In [2]: builtins._ = 42
  64. In [3]: builtins._
  65. Out[3]: 42
  66. In [4]: _
  67. Out[4]: 42
  68. """