test-org-footnote.el 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. ;;; test-org-footnote.el --- Tests for org-footnote.el
  2. ;; Copyright (C) 2012, 2013, 2014 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (ert-deftest test-org-footnote/delete ()
  16. "Test `org-footnote-delete' specifications."
  17. ;; Regular test.
  18. (should
  19. (equal "Paragraph"
  20. (org-test-with-temp-text "Paragraph[1]\n\n[1] Definition"
  21. (search-forward "[")
  22. (org-footnote-delete)
  23. (org-trim (buffer-string)))))
  24. ;; Remove multiple definitions and references.
  25. (should
  26. (equal "Paragraph and another"
  27. (org-test-with-temp-text
  28. "Paragraph[1] and another[1]\n\n[1] def\n\n[1] def"
  29. (search-forward "[")
  30. (org-footnote-delete)
  31. (org-trim (buffer-string)))))
  32. ;; Delete inline footnotes and all references.
  33. (should
  34. (equal "Para and"
  35. (org-test-with-temp-text "Para[fn:label:def] and[fn:label]"
  36. (search-forward "[")
  37. (org-footnote-delete)
  38. (org-trim (buffer-string)))))
  39. ;; Delete anonymous footnotes.
  40. (should
  41. (equal "Para"
  42. (org-test-with-temp-text "Para[fn::def]"
  43. (search-forward "[")
  44. (org-footnote-delete)
  45. (org-trim (buffer-string)))))
  46. ;; With an argument, delete footnote with specified label.
  47. (should
  48. (equal "Paragraph[1] and another\n\n[1] def"
  49. (let ((org-footnote-section nil))
  50. (org-test-with-temp-text
  51. "Paragraph[1] and another[2]\n\n[1] def\n\n[2] def2"
  52. (org-footnote-delete "2")
  53. (org-trim (buffer-string))))))
  54. ;; Error when no argument is specified at point is not at a footnote
  55. ;; reference.
  56. (should-error
  57. (org-test-with-temp-text "Para[1]\n\n[1] Def"
  58. (org-footnote-delete)))
  59. ;; Correctly delete footnotes with multiple paragraphs.
  60. (should
  61. (equal "Para\n\n\nOutside footnote."
  62. (org-test-with-temp-text
  63. "Para[1]\n\n[1] para1\n\npara2\n\n\nOutside footnote."
  64. (org-footnote-delete "1")
  65. (org-trim (buffer-string))))))
  66. (ert-deftest test-org-footnote/normalize-in-org ()
  67. "Test specifications for `org-footnote-normalize' in an Org buffer."
  68. ;; 1. With a non-nil `org-footnote-section'.
  69. (let ((org-footnote-section "Footnotes")
  70. (org-blank-before-new-entry '((heading . auto))))
  71. ;; 1.1. Normalize each type of footnote: standard, labelled,
  72. ;; numbered, inline, anonymous.
  73. (org-test-with-temp-text
  74. "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
  75. * Footnotes
  76. \[fn:1] Standard
  77. \[fn:label] Labelled
  78. \[1] Numbered"
  79. (org-footnote-normalize)
  80. (should
  81. (equal (buffer-string)
  82. "Paragraph[1][2][3][4][5]
  83. * Footnotes
  84. \[1] Standard
  85. \[2] Labelled
  86. \[3] Numbered
  87. \[4] Inline
  88. \[5] Anonymous
  89. ")))
  90. ;; 1.2. When no footnote section is present, create it. Follow
  91. ;; `org-blank-before-new-entry' specifications when doing so.
  92. (org-test-with-temp-text "Paragraph[fn:1]\n\n[fn:1] Definition"
  93. (org-footnote-normalize)
  94. (should (equal (buffer-string)
  95. "Paragraph[1]\n\n* Footnotes\n\n[1] Definition")))
  96. (org-test-with-temp-text "Paragraph[fn:1]\n* Head1\n[fn:1] Definition"
  97. (let ((org-blank-before-new-entry '((heading))))
  98. (org-footnote-normalize))
  99. (should (equal (buffer-string)
  100. "Paragraph[1]\n* Head1\n* Footnotes\n\n[1] Definition")))
  101. ;; 1.3. When the footnote section is misplaced, move it at the end
  102. ;; of the buffer.
  103. (org-test-with-temp-text "* Head1
  104. Body[fn:1]
  105. * Footnotes
  106. \[fn:1] Definition 1
  107. * Head2"
  108. (org-footnote-normalize)
  109. (should
  110. (equal (buffer-string)
  111. "* Head1
  112. Body[1]
  113. * Head2
  114. * Footnotes
  115. \[1] Definition 1"))))
  116. ;; 2. With a nil `org-footnote-section'.
  117. (let ((org-footnote-section nil))
  118. ;; 2.1. Normalize each type of footnote: standard, labelled,
  119. ;; numbered, inline, anonymous.
  120. (org-test-with-temp-text
  121. "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
  122. \[fn:1] Standard
  123. \[fn:label] Labelled
  124. \[1] Numbered"
  125. (org-footnote-normalize)
  126. (should
  127. (equal (buffer-string)
  128. "Paragraph[1][2][3][4][5]
  129. \[1] Standard
  130. \[2] Labelled
  131. \[3] Numbered
  132. \[4] Inline
  133. \[5] Anonymous
  134. ")))
  135. ;; 2.2. Put each footnote definition at the end of the section
  136. ;; containing its first reference.
  137. (org-test-with-temp-text
  138. "* Head 1
  139. Text[fn:1:Def1]
  140. * Head 2
  141. Text[fn:1]
  142. * Head 3
  143. Text[fn:2:Def2]"
  144. (org-footnote-normalize)
  145. (should
  146. (equal (buffer-string)
  147. "* Head 1
  148. Text[1]
  149. \[1] Def1
  150. * Head 2
  151. Text[1]
  152. * Head 3
  153. Text[2]
  154. \[2] Def2
  155. ")))))
  156. (ert-deftest test-org-footnote/normalize-outside-org ()
  157. "Test `org-footnote-normalize' specifications for buffers not in Org mode."
  158. ;; 1. In a non-Org buffer, footnotes definitions are always put at
  159. ;; its end.
  160. (should
  161. (equal
  162. "Paragraph[1][2][3][4][5]
  163. Some additional text.
  164. \[1] Standard
  165. \[2] Labelled
  166. \[3] Numbered
  167. \[4] Inline
  168. \[5] Anonymous"
  169. (let ((org-footnote-tag-for-non-org-mode-files nil))
  170. (with-temp-buffer
  171. (insert "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
  172. \[fn:1] Standard
  173. \[fn:label] Labelled
  174. \[1] Numbered
  175. Some additional text.")
  176. (org-footnote-normalize)
  177. (buffer-string)))))
  178. ;; 2. With a special tag.
  179. (let ((org-footnote-tag-for-non-org-mode-files "Footnotes:"))
  180. ;; 2.1. The tag must be inserted before the footnotes, separated
  181. ;; from the rest of the text with a blank line.
  182. (with-temp-buffer
  183. (insert "Paragraph[fn:1][fn::Anonymous]
  184. \[fn:1] Standard
  185. Some additional text.")
  186. (org-footnote-normalize)
  187. (should
  188. (equal (buffer-string)
  189. "Paragraph[1][2]
  190. Some additional text.
  191. Footnotes:
  192. \[1] Standard
  193. \[2] Anonymous")))
  194. ;; 2.2. Any tag already inserted in the buffer should be removed
  195. ;; prior to footnotes insertion.
  196. (with-temp-buffer
  197. (insert "Text[fn:1]
  198. Footnotes:
  199. Additional text.
  200. Footnotes:
  201. \[fn:1] Definition")
  202. (org-footnote-normalize)
  203. (should
  204. (equal (buffer-string)
  205. "Text[1]
  206. Additional text.
  207. Footnotes:
  208. \[1] Definition"))))
  209. ;; 3. As an exception, in `message-mode' buffer, if a signature is
  210. ;; present, insert footnotes before it.n
  211. (let ((org-footnote-tag-for-non-org-mode-files nil))
  212. (with-temp-buffer
  213. (insert "Body[fn::def]
  214. --
  215. Fake signature
  216. --
  217. Signature")
  218. ;; Mimic `message-mode'.
  219. (let ((major-mode 'message-mode)
  220. (message-cite-prefix-regexp "\\([ ]*[_.[:word:]]+>+\\|[ ]*[]>|]\\)+")
  221. (message-signature-separator "^-- $"))
  222. (flet ((message-point-in-header-p nil nil))
  223. (org-footnote-normalize)))
  224. (should
  225. (equal (buffer-string)
  226. "Body[1]
  227. --
  228. Fake signature
  229. \[1] def
  230. --
  231. Signature")))))
  232. (ert-deftest test-org-footnote/sort ()
  233. "Test footnotes definitions sorting."
  234. (let ((org-footnote-section nil))
  235. (org-test-with-temp-text
  236. "Text[fn:1][fn::inline][fn:2][fn:label]
  237. \[fn:label] C
  238. \[fn:1] A
  239. \[fn:2] B"
  240. (org-footnote-normalize 'sort)
  241. (should
  242. (equal (buffer-string)
  243. "Text[fn:1][fn::inline][fn:2][fn:label]
  244. \[fn:1] A
  245. \[fn:2] B
  246. \[fn:label] C
  247. ")))))
  248. (provide 'test-org-footnote)
  249. ;;; test-org-footnote.el ends here