test_doccer.py 3.1 KB

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