test_doccer.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ''' Some tests for the documenting decorator and support functions '''
  2. import sys
  3. import pytest
  4. from numpy.testing import assert_equal, suppress_warnings
  5. from scipy._lib import doccer
  6. # python -OO strips docstrings
  7. DOCSTRINGS_STRIPPED = sys.flags.optimize > 1
  8. docstring = \
  9. """Docstring
  10. %(strtest1)s
  11. %(strtest2)s
  12. %(strtest3)s
  13. """
  14. param_doc1 = \
  15. """Another test
  16. with some indent"""
  17. param_doc2 = \
  18. """Another test, one line"""
  19. param_doc3 = \
  20. """ Another test
  21. with some indent"""
  22. doc_dict = {'strtest1':param_doc1,
  23. 'strtest2':param_doc2,
  24. 'strtest3':param_doc3}
  25. filled_docstring = \
  26. """Docstring
  27. Another test
  28. with some indent
  29. Another test, one line
  30. Another test
  31. with some indent
  32. """
  33. def test_unindent():
  34. with suppress_warnings() as sup:
  35. sup.filter(category=DeprecationWarning)
  36. assert_equal(doccer.unindent_string(param_doc1), param_doc1)
  37. assert_equal(doccer.unindent_string(param_doc2), param_doc2)
  38. assert_equal(doccer.unindent_string(param_doc3), param_doc1)
  39. def test_unindent_dict():
  40. with suppress_warnings() as sup:
  41. sup.filter(category=DeprecationWarning)
  42. d2 = doccer.unindent_dict(doc_dict)
  43. assert_equal(d2['strtest1'], doc_dict['strtest1'])
  44. assert_equal(d2['strtest2'], doc_dict['strtest2'])
  45. assert_equal(d2['strtest3'], doc_dict['strtest1'])
  46. def test_docformat():
  47. with suppress_warnings() as sup:
  48. sup.filter(category=DeprecationWarning)
  49. udd = doccer.unindent_dict(doc_dict)
  50. formatted = doccer.docformat(docstring, udd)
  51. assert_equal(formatted, filled_docstring)
  52. single_doc = 'Single line doc %(strtest1)s'
  53. formatted = doccer.docformat(single_doc, doc_dict)
  54. # Note - initial indent of format string does not
  55. # affect subsequent indent of inserted parameter
  56. assert_equal(formatted, """Single line doc Another test
  57. with some indent""")
  58. @pytest.mark.skipif(DOCSTRINGS_STRIPPED, reason="docstrings stripped")
  59. def test_decorator():
  60. with suppress_warnings() as sup:
  61. sup.filter(category=DeprecationWarning)
  62. # with unindentation of parameters
  63. decorator = doccer.filldoc(doc_dict, True)
  64. @decorator
  65. def func():
  66. """ Docstring
  67. %(strtest3)s
  68. """
  69. assert_equal(func.__doc__, """ Docstring
  70. Another test
  71. with some indent
  72. """)
  73. # without unindentation of parameters
  74. decorator = doccer.filldoc(doc_dict, False)
  75. @decorator
  76. def func():
  77. """ Docstring
  78. %(strtest3)s
  79. """
  80. assert_equal(func.__doc__, """ Docstring
  81. Another test
  82. with some indent
  83. """)
  84. @pytest.mark.skipif(DOCSTRINGS_STRIPPED, reason="docstrings stripped")
  85. def test_inherit_docstring_from():
  86. with suppress_warnings() as sup:
  87. sup.filter(category=DeprecationWarning)
  88. class Foo:
  89. def func(self):
  90. '''Do something useful.'''
  91. return
  92. def func2(self):
  93. '''Something else.'''
  94. class Bar(Foo):
  95. @doccer.inherit_docstring_from(Foo)
  96. def func(self):
  97. '''%(super)sABC'''
  98. return
  99. @doccer.inherit_docstring_from(Foo)
  100. def func2(self):
  101. # No docstring.
  102. return
  103. assert_equal(Bar.func.__doc__, Foo.func.__doc__ + 'ABC')
  104. assert_equal(Bar.func2.__doc__, Foo.func2.__doc__)
  105. bar = Bar()
  106. assert_equal(bar.func.__doc__, Foo.func.__doc__ + 'ABC')
  107. assert_equal(bar.func2.__doc__, Foo.func2.__doc__)