test_sphinxext.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. """Tests for the sphinx extension
  13. """
  14. import importlib.metadata as importlib_metadata
  15. from stevedore import extension
  16. from stevedore import sphinxext
  17. from stevedore.tests import utils
  18. def _make_ext(name, docstring):
  19. def inner():
  20. pass
  21. inner.__doc__ = docstring
  22. m1 = importlib_metadata.EntryPoint(
  23. name, '{}_module:{}'.format(name, name), 'group',
  24. )
  25. return extension.Extension(name, m1, inner, None)
  26. class TestSphinxExt(utils.TestCase):
  27. def setUp(self):
  28. super(TestSphinxExt, self).setUp()
  29. self.exts = [
  30. _make_ext('test1', 'One-line docstring'),
  31. _make_ext('test2', 'Multi-line docstring\n\nAnother para'),
  32. ]
  33. self.em = extension.ExtensionManager.make_test_instance(self.exts)
  34. def test_simple_list(self):
  35. results = list(sphinxext._simple_list(self.em))
  36. self.assertEqual(
  37. [
  38. ('* test1 -- One-line docstring', 'test1_module'),
  39. ('* test2 -- Multi-line docstring', 'test2_module'),
  40. ],
  41. results,
  42. )
  43. def test_simple_list_no_docstring(self):
  44. ext = [_make_ext('nodoc', None)]
  45. em = extension.ExtensionManager.make_test_instance(ext)
  46. results = list(sphinxext._simple_list(em))
  47. self.assertEqual(
  48. [
  49. ('* nodoc -- ', 'nodoc_module'),
  50. ],
  51. results,
  52. )
  53. def test_detailed_list(self):
  54. results = list(sphinxext._detailed_list(self.em))
  55. self.assertEqual(
  56. [
  57. ('test1', 'test1_module'),
  58. ('-----', 'test1_module'),
  59. ('\n', 'test1_module'),
  60. ('One-line docstring', 'test1_module'),
  61. ('\n', 'test1_module'),
  62. ('test2', 'test2_module'),
  63. ('-----', 'test2_module'),
  64. ('\n', 'test2_module'),
  65. ('Multi-line docstring\n\nAnother para', 'test2_module'),
  66. ('\n', 'test2_module'),
  67. ],
  68. results,
  69. )
  70. def test_detailed_list_format(self):
  71. results = list(sphinxext._detailed_list(self.em, over='+', under='+'))
  72. self.assertEqual(
  73. [
  74. ('+++++', 'test1_module'),
  75. ('test1', 'test1_module'),
  76. ('+++++', 'test1_module'),
  77. ('\n', 'test1_module'),
  78. ('One-line docstring', 'test1_module'),
  79. ('\n', 'test1_module'),
  80. ('+++++', 'test2_module'),
  81. ('test2', 'test2_module'),
  82. ('+++++', 'test2_module'),
  83. ('\n', 'test2_module'),
  84. ('Multi-line docstring\n\nAnother para', 'test2_module'),
  85. ('\n', 'test2_module'),
  86. ],
  87. results,
  88. )
  89. def test_detailed_list_no_docstring(self):
  90. ext = [_make_ext('nodoc', None)]
  91. em = extension.ExtensionManager.make_test_instance(ext)
  92. results = list(sphinxext._detailed_list(em))
  93. self.assertEqual(
  94. [
  95. ('nodoc', 'nodoc_module'),
  96. ('-----', 'nodoc_module'),
  97. ('\n', 'nodoc_module'),
  98. (('.. warning:: No documentation found for '
  99. 'nodoc in nodoc_module:nodoc'),
  100. 'nodoc_module'),
  101. ('\n', 'nodoc_module'),
  102. ],
  103. results,
  104. )