test_document.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2001, 2002 Zope Foundation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE.
  12. #
  13. ##############################################################################
  14. """Documentation tests.
  15. """
  16. import unittest
  17. class Test_asStructuredText(unittest.TestCase):
  18. def _callFUT(self, iface):
  19. from zope.interface.document import asStructuredText
  20. return asStructuredText(iface)
  21. def test_asStructuredText_no_docstring(self):
  22. from zope.interface import Interface
  23. EXPECTED = '\n\n'.join([
  24. "INoDocstring",
  25. " Attributes:",
  26. " Methods:",
  27. ""
  28. ])
  29. class INoDocstring(Interface):
  30. pass
  31. self.assertEqual(self._callFUT(INoDocstring), EXPECTED)
  32. def test_asStructuredText_empty_with_docstring(self):
  33. from zope.interface import Interface
  34. EXPECTED = '\n\n'.join([
  35. "IEmpty",
  36. " This is an empty interface.",
  37. " Attributes:",
  38. " Methods:",
  39. ""
  40. ])
  41. class IEmpty(Interface):
  42. """ This is an empty interface.
  43. """
  44. self.assertEqual(self._callFUT(IEmpty), EXPECTED)
  45. def test_asStructuredText_empty_with_multiline_docstring(self):
  46. from zope.interface import Interface
  47. EXPECTED = '\n'.join([
  48. "IEmpty",
  49. "",
  50. " This is an empty interface.",
  51. " ",
  52. (" It can be used to annotate any class or object, "
  53. "because it promises"),
  54. " nothing.",
  55. "",
  56. " Attributes:",
  57. "",
  58. " Methods:",
  59. "",
  60. ""
  61. ])
  62. class IEmpty(Interface):
  63. """ This is an empty interface.
  64. It can be used to annotate any class or object, because it promises
  65. nothing.
  66. """
  67. self.assertEqual(self._callFUT(IEmpty), EXPECTED)
  68. def test_asStructuredText_with_attribute_no_docstring(self):
  69. from zope.interface import Attribute
  70. from zope.interface import Interface
  71. EXPECTED = '\n\n'.join([
  72. "IHasAttribute",
  73. " This interface has an attribute.",
  74. " Attributes:",
  75. " an_attribute -- no documentation",
  76. " Methods:",
  77. ""
  78. ])
  79. class IHasAttribute(Interface):
  80. """ This interface has an attribute.
  81. """
  82. an_attribute = Attribute('an_attribute')
  83. self.assertEqual(self._callFUT(IHasAttribute), EXPECTED)
  84. def test_asStructuredText_with_attribute_with_docstring(self):
  85. from zope.interface import Attribute
  86. from zope.interface import Interface
  87. EXPECTED = '\n\n'.join([
  88. "IHasAttribute",
  89. " This interface has an attribute.",
  90. " Attributes:",
  91. " an_attribute -- This attribute is documented.",
  92. " Methods:",
  93. ""
  94. ])
  95. class IHasAttribute(Interface):
  96. """ This interface has an attribute.
  97. """
  98. an_attribute = Attribute('an_attribute',
  99. 'This attribute is documented.')
  100. self.assertEqual(self._callFUT(IHasAttribute), EXPECTED)
  101. def test_asStructuredText_with_method_no_args_no_docstring(self):
  102. from zope.interface import Interface
  103. EXPECTED = '\n\n'.join([
  104. "IHasMethod",
  105. " This interface has a method.",
  106. " Attributes:",
  107. " Methods:",
  108. " aMethod() -- no documentation",
  109. ""
  110. ])
  111. class IHasMethod(Interface):
  112. """ This interface has a method.
  113. """
  114. def aMethod():
  115. pass # pragma: no cover
  116. self.assertEqual(self._callFUT(IHasMethod), EXPECTED)
  117. def test_asStructuredText_with_method_positional_args_no_docstring(self):
  118. from zope.interface import Interface
  119. EXPECTED = '\n\n'.join([
  120. "IHasMethod",
  121. " This interface has a method.",
  122. " Attributes:",
  123. " Methods:",
  124. " aMethod(first, second) -- no documentation",
  125. ""
  126. ])
  127. class IHasMethod(Interface):
  128. """ This interface has a method.
  129. """
  130. def aMethod(first, second):
  131. pass # pragma: no cover
  132. self.assertEqual(self._callFUT(IHasMethod), EXPECTED)
  133. def test_asStructuredText_with_method_starargs_no_docstring(self):
  134. from zope.interface import Interface
  135. EXPECTED = '\n\n'.join([
  136. "IHasMethod",
  137. " This interface has a method.",
  138. " Attributes:",
  139. " Methods:",
  140. " aMethod(first, second, *rest) -- no documentation",
  141. ""
  142. ])
  143. class IHasMethod(Interface):
  144. """ This interface has a method.
  145. """
  146. def aMethod(first, second, *rest):
  147. pass # pragma: no cover
  148. self.assertEqual(self._callFUT(IHasMethod), EXPECTED)
  149. def test_asStructuredText_with_method_kwargs_no_docstring(self):
  150. from zope.interface import Interface
  151. EXPECTED = '\n\n'.join([
  152. "IHasMethod",
  153. " This interface has a method.",
  154. " Attributes:",
  155. " Methods:",
  156. " aMethod(first, second, **kw) -- no documentation",
  157. ""
  158. ])
  159. class IHasMethod(Interface):
  160. """ This interface has a method.
  161. """
  162. def aMethod(first, second, **kw):
  163. pass # pragma: no cover
  164. self.assertEqual(self._callFUT(IHasMethod), EXPECTED)
  165. def test_asStructuredText_with_method_with_docstring(self):
  166. from zope.interface import Interface
  167. EXPECTED = '\n\n'.join([
  168. "IHasMethod",
  169. " This interface has a method.",
  170. " Attributes:",
  171. " Methods:",
  172. " aMethod() -- This method is documented.",
  173. ""
  174. ])
  175. class IHasMethod(Interface):
  176. """ This interface has a method.
  177. """
  178. def aMethod():
  179. """This method is documented.
  180. """
  181. self.assertEqual(self._callFUT(IHasMethod), EXPECTED)
  182. def test_asStructuredText_derived_ignores_base(self):
  183. from zope.interface import Attribute
  184. from zope.interface import Interface
  185. EXPECTED = '\n\n'.join([
  186. "IDerived",
  187. " IDerived doc",
  188. " This interface extends:",
  189. " o IBase",
  190. " Attributes:",
  191. " attr1 -- no documentation",
  192. " attr2 -- attr2 doc",
  193. " Methods:",
  194. " method3() -- method3 doc",
  195. " method4() -- no documentation",
  196. " method5() -- method5 doc",
  197. "",
  198. ])
  199. class IBase(Interface):
  200. def method1():
  201. """docstring"""
  202. def method2():
  203. """docstring"""
  204. class IDerived(IBase):
  205. "IDerived doc"
  206. attr1 = Attribute('attr1')
  207. attr2 = Attribute('attr2', 'attr2 doc')
  208. def method3():
  209. "method3 doc"
  210. def method4():
  211. pass # pragma: no cover
  212. def method5():
  213. "method5 doc"
  214. self.assertEqual(self._callFUT(IDerived), EXPECTED)
  215. class Test_asReStructuredText(unittest.TestCase):
  216. def _callFUT(self, iface):
  217. from zope.interface.document import asReStructuredText
  218. return asReStructuredText(iface)
  219. def test_asReStructuredText_no_docstring(self):
  220. from zope.interface import Interface
  221. EXPECTED = '\n\n'.join([
  222. "``INoDocstring``",
  223. " Attributes:",
  224. " Methods:",
  225. ""
  226. ])
  227. class INoDocstring(Interface):
  228. pass
  229. self.assertEqual(self._callFUT(INoDocstring), EXPECTED)
  230. def test_asReStructuredText_empty_with_docstring(self):
  231. from zope.interface import Interface
  232. EXPECTED = '\n\n'.join([
  233. "``IEmpty``",
  234. " This is an empty interface.",
  235. " Attributes:",
  236. " Methods:",
  237. ""
  238. ])
  239. class IEmpty(Interface):
  240. """ This is an empty interface.
  241. """
  242. self.assertEqual(self._callFUT(IEmpty), EXPECTED)
  243. def test_asReStructuredText_empty_with_multiline_docstring(self):
  244. from zope.interface import Interface
  245. EXPECTED = '\n'.join([
  246. "``IEmpty``",
  247. "",
  248. " This is an empty interface.",
  249. " ",
  250. (" It can be used to annotate any class or object, "
  251. "because it promises"),
  252. " nothing.",
  253. "",
  254. " Attributes:",
  255. "",
  256. " Methods:",
  257. "",
  258. ""
  259. ])
  260. class IEmpty(Interface):
  261. """ This is an empty interface.
  262. It can be used to annotate any class or object, because it promises
  263. nothing.
  264. """
  265. self.assertEqual(self._callFUT(IEmpty), EXPECTED)
  266. def test_asReStructuredText_with_attribute_no_docstring(self):
  267. from zope.interface import Attribute
  268. from zope.interface import Interface
  269. EXPECTED = '\n\n'.join([
  270. "``IHasAttribute``",
  271. " This interface has an attribute.",
  272. " Attributes:",
  273. " ``an_attribute`` -- no documentation",
  274. " Methods:",
  275. ""
  276. ])
  277. class IHasAttribute(Interface):
  278. """ This interface has an attribute.
  279. """
  280. an_attribute = Attribute('an_attribute')
  281. self.assertEqual(self._callFUT(IHasAttribute), EXPECTED)
  282. def test_asReStructuredText_with_attribute_with_docstring(self):
  283. from zope.interface import Attribute
  284. from zope.interface import Interface
  285. EXPECTED = '\n\n'.join([
  286. "``IHasAttribute``",
  287. " This interface has an attribute.",
  288. " Attributes:",
  289. " ``an_attribute`` -- This attribute is documented.",
  290. " Methods:",
  291. ""
  292. ])
  293. class IHasAttribute(Interface):
  294. """ This interface has an attribute.
  295. """
  296. an_attribute = Attribute('an_attribute',
  297. 'This attribute is documented.')
  298. self.assertEqual(self._callFUT(IHasAttribute), EXPECTED)
  299. def test_asReStructuredText_with_method_no_args_no_docstring(self):
  300. from zope.interface import Interface
  301. EXPECTED = '\n\n'.join([
  302. "``IHasMethod``",
  303. " This interface has a method.",
  304. " Attributes:",
  305. " Methods:",
  306. " ``aMethod()`` -- no documentation",
  307. ""
  308. ])
  309. class IHasMethod(Interface):
  310. """ This interface has a method.
  311. """
  312. def aMethod():
  313. pass # pragma: no cover
  314. self.assertEqual(self._callFUT(IHasMethod), EXPECTED)
  315. def test_asReStructuredText_with_method_positional_args_no_docstring(self):
  316. from zope.interface import Interface
  317. EXPECTED = '\n\n'.join([
  318. "``IHasMethod``",
  319. " This interface has a method.",
  320. " Attributes:",
  321. " Methods:",
  322. " ``aMethod(first, second)`` -- no documentation",
  323. ""
  324. ])
  325. class IHasMethod(Interface):
  326. """ This interface has a method.
  327. """
  328. def aMethod(first, second):
  329. pass # pragma: no cover
  330. self.assertEqual(self._callFUT(IHasMethod), EXPECTED)
  331. def test_asReStructuredText_with_method_starargs_no_docstring(self):
  332. from zope.interface import Interface
  333. EXPECTED = '\n\n'.join([
  334. "``IHasMethod``",
  335. " This interface has a method.",
  336. " Attributes:",
  337. " Methods:",
  338. " ``aMethod(first, second, *rest)`` -- no documentation",
  339. ""
  340. ])
  341. class IHasMethod(Interface):
  342. """ This interface has a method.
  343. """
  344. def aMethod(first, second, *rest):
  345. pass # pragma: no cover
  346. self.assertEqual(self._callFUT(IHasMethod), EXPECTED)
  347. def test_asReStructuredText_with_method_kwargs_no_docstring(self):
  348. from zope.interface import Interface
  349. EXPECTED = '\n\n'.join([
  350. "``IHasMethod``",
  351. " This interface has a method.",
  352. " Attributes:",
  353. " Methods:",
  354. " ``aMethod(first, second, **kw)`` -- no documentation",
  355. ""
  356. ])
  357. class IHasMethod(Interface):
  358. """ This interface has a method.
  359. """
  360. def aMethod(first, second, **kw):
  361. pass # pragma: no cover
  362. self.assertEqual(self._callFUT(IHasMethod), EXPECTED)
  363. def test_asReStructuredText_with_method_with_docstring(self):
  364. from zope.interface import Interface
  365. EXPECTED = '\n\n'.join([
  366. "``IHasMethod``",
  367. " This interface has a method.",
  368. " Attributes:",
  369. " Methods:",
  370. " ``aMethod()`` -- This method is documented.",
  371. ""
  372. ])
  373. class IHasMethod(Interface):
  374. """ This interface has a method.
  375. """
  376. def aMethod():
  377. """This method is documented.
  378. """
  379. self.assertEqual(self._callFUT(IHasMethod), EXPECTED)
  380. def test_asReStructuredText_derived_ignores_base(self):
  381. from zope.interface import Attribute
  382. from zope.interface import Interface
  383. EXPECTED = '\n\n'.join([
  384. "``IDerived``",
  385. " IDerived doc",
  386. " This interface extends:",
  387. " o ``IBase``",
  388. " Attributes:",
  389. " ``attr1`` -- no documentation",
  390. " ``attr2`` -- attr2 doc",
  391. " Methods:",
  392. " ``method3()`` -- method3 doc",
  393. " ``method4()`` -- no documentation",
  394. " ``method5()`` -- method5 doc",
  395. "",
  396. ])
  397. class IBase(Interface):
  398. def method1():
  399. pass # pragma: no cover
  400. def method2():
  401. pass # pragma: no cover
  402. class IDerived(IBase):
  403. "IDerived doc"
  404. attr1 = Attribute('attr1')
  405. attr2 = Attribute('attr2', 'attr2 doc')
  406. def method3():
  407. "method3 doc"
  408. def method4():
  409. pass # pragma: no cover
  410. def method5():
  411. "method5 doc"
  412. self.assertEqual(self._callFUT(IDerived), EXPECTED)
  413. class Test__justify_and_indent(unittest.TestCase):
  414. def _callFUT(self, text, level, **kw):
  415. from zope.interface.document import _justify_and_indent
  416. return _justify_and_indent(text, level, **kw)
  417. def test_simple_level_0(self):
  418. LINES = ['Three blind mice', 'See how they run']
  419. text = '\n'.join(LINES)
  420. self.assertEqual(self._callFUT(text, 0), text)
  421. def test_simple_level_1(self):
  422. LINES = ['Three blind mice', 'See how they run']
  423. text = '\n'.join(LINES)
  424. self.assertEqual(self._callFUT(text, 1),
  425. '\n'.join([' ' + line for line in LINES]))
  426. def test_simple_level_2(self):
  427. LINES = ['Three blind mice', 'See how they run']
  428. text = '\n'.join(LINES)
  429. self.assertEqual(self._callFUT(text, 1),
  430. '\n'.join([' ' + line for line in LINES]))
  431. def test_simple_w_CRLF(self):
  432. LINES = ['Three blind mice', 'See how they run']
  433. text = '\r\n'.join(LINES)
  434. self.assertEqual(self._callFUT(text, 1),
  435. '\n'.join([' ' + line for line in LINES]))
  436. def test_with_munge(self):
  437. TEXT = ("This is a piece of text longer than 15 characters, \n"
  438. "and split across multiple lines.")
  439. EXPECTED = (" This is a piece\n"
  440. " of text longer\n"
  441. " than 15 characters,\n"
  442. " and split across\n"
  443. " multiple lines.\n"
  444. " ")
  445. self.assertEqual(self._callFUT(TEXT, 1, munge=1, width=15), EXPECTED)