test-org-footnote.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. ;;; test-org-footnote.el --- Tests for org-footnote.el
  2. ;; Copyright (C) 2012-2015 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/new ()
  16. "Test `org-footnote-new' specifications."
  17. ;; `org-footnote-auto-label' is t.
  18. (should
  19. (string-match-p
  20. "Test\\[fn:1\\]\n+\\[fn:1\\]"
  21. (org-test-with-temp-text "Test<point>"
  22. (let ((org-footnote-auto-label t)
  23. (org-footnote-section nil))
  24. (org-footnote-new))
  25. (buffer-string))))
  26. ;; `org-footnote-auto-label' is `plain'.
  27. (should
  28. (string-match-p
  29. "Test\\[1\\]\n+\\[1\\]"
  30. (org-test-with-temp-text "Test<point>"
  31. (let ((org-footnote-auto-label 'plain)
  32. (org-footnote-section nil))
  33. (org-footnote-new))
  34. (buffer-string))))
  35. ;; `org-footnote-auto-label' is `random'.
  36. (should
  37. (string-match-p
  38. "Test\\[fn:\\(.+?\\)\\]\n+\\[fn:\\1\\]"
  39. (org-test-with-temp-text "Test<point>"
  40. (let ((org-footnote-auto-label 'random)
  41. (org-footnote-section nil))
  42. (org-footnote-new))
  43. (buffer-string))))
  44. ;; Error at beginning of line.
  45. (should-error
  46. (org-test-with-temp-text "<point>Test"
  47. (org-footnote-new)))
  48. ;; Error at keywords.
  49. (should-error
  50. (org-test-with-temp-text "#+TIT<point>LE: value"
  51. (org-footnote-new)))
  52. (should-error
  53. (org-test-with-temp-text "#+CAPTION: <point>\nParagraph"
  54. (org-footnote-new)))
  55. ;; Allow new footnotes in blank lines at the beginning of the
  56. ;; document.
  57. (should
  58. (string-match-p
  59. " \\[fn:1\\]"
  60. (org-test-with-temp-text " <point>"
  61. (let ((org-footnote-auto-label t)) (org-footnote-new))
  62. (buffer-string))))
  63. ;; In an headline or inlinetask, point must be either on the
  64. ;; heading itself or on the blank lines below.
  65. (should (org-test-with-temp-text "* H<point>" (org-footnote-new) t))
  66. (should
  67. (org-test-with-temp-text "* H\n <point>\nParagraph" (org-footnote-new) t))
  68. (should-error (org-test-with-temp-text "*<point> H" (org-footnote-new) t))
  69. (should-error
  70. (org-test-with-temp-text "* H <point>:tag:" (org-footnote-new) t))
  71. ;; Allow new footnotes within recursive objects, but not in links.
  72. (should
  73. (string-match-p
  74. " \\*bold\\[fn:1\\]\\*"
  75. (org-test-with-temp-text " *bold<point>*"
  76. (let ((org-footnote-auto-label t)) (org-footnote-new))
  77. (buffer-string))))
  78. (should-error
  79. (org-test-with-temp-text " [[http://orgmode.org][Org mode<point>]]"
  80. (org-footnote-new)))
  81. ;; Allow new footnotes in blank lines after an element or white
  82. ;; spaces after an object.
  83. (should
  84. (string-match-p
  85. " \\[fn:1\\]"
  86. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nA\n#+END_EXAMPLE\n <point>"
  87. (let ((org-footnote-auto-label t)) (org-footnote-new))
  88. (buffer-string))))
  89. (should
  90. (string-match-p
  91. " \\*bold\\*\\[fn:1\\]"
  92. (org-test-with-temp-text " *bold*<point>"
  93. (let ((org-footnote-auto-label t)) (org-footnote-new))
  94. (buffer-string)))))
  95. (ert-deftest test-org-footnote/delete ()
  96. "Test `org-footnote-delete' specifications."
  97. ;; Regular test.
  98. (should
  99. (equal "Paragraph"
  100. (org-test-with-temp-text "Paragraph[1]\n\n[1] Definition"
  101. (search-forward "[")
  102. (org-footnote-delete)
  103. (org-trim (buffer-string)))))
  104. ;; Remove multiple definitions and references.
  105. (should
  106. (equal "Paragraph and another"
  107. (org-test-with-temp-text
  108. "Paragraph[1] and another[1]\n\n[1] def\n\n[1] def"
  109. (search-forward "[")
  110. (org-footnote-delete)
  111. (org-trim (buffer-string)))))
  112. ;; Delete inline footnotes and all references.
  113. (should
  114. (equal "Para and"
  115. (org-test-with-temp-text "Para[fn:label:def] and[fn:label]"
  116. (search-forward "[")
  117. (org-footnote-delete)
  118. (org-trim (buffer-string)))))
  119. ;; Delete anonymous footnotes.
  120. (should
  121. (equal "Para"
  122. (org-test-with-temp-text "Para[fn::def]"
  123. (search-forward "[")
  124. (org-footnote-delete)
  125. (org-trim (buffer-string)))))
  126. ;; With an argument, delete footnote with specified label.
  127. (should
  128. (equal "Paragraph[1] and another\n\n[1] def"
  129. (let ((org-footnote-section nil))
  130. (org-test-with-temp-text
  131. "Paragraph[1] and another[2]\n\n[1] def\n\n[2] def2"
  132. (org-footnote-delete "2")
  133. (org-trim (buffer-string))))))
  134. ;; Error when no argument is specified at point is not at a footnote
  135. ;; reference.
  136. (should-error
  137. (org-test-with-temp-text "Para[1]\n\n[1] Def"
  138. (org-footnote-delete)))
  139. ;; Correctly delete footnotes with multiple paragraphs.
  140. (should
  141. (equal "Para\n\n\nOutside footnote."
  142. (org-test-with-temp-text
  143. "Para[1]\n\n[1] para1\n\npara2\n\n\nOutside footnote."
  144. (org-footnote-delete "1")
  145. (org-trim (buffer-string))))))
  146. (ert-deftest test-org-footnote/normalize-in-org ()
  147. "Test specifications for `org-footnote-normalize' in an Org buffer."
  148. ;; 1. With a non-nil `org-footnote-section'.
  149. (let ((org-footnote-section "Footnotes")
  150. (org-blank-before-new-entry '((heading . auto))))
  151. ;; 1.1. Normalize each type of footnote: standard, labelled,
  152. ;; numbered, inline, anonymous.
  153. (org-test-with-temp-text
  154. "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
  155. * Footnotes
  156. \[fn:1] Standard
  157. \[fn:label] Labelled
  158. \[1] Numbered"
  159. (org-footnote-normalize)
  160. (should
  161. (equal (buffer-string)
  162. "Paragraph[1][2][3][4][5]
  163. * Footnotes
  164. \[1] Standard
  165. \[2] Labelled
  166. \[3] Numbered
  167. \[4] Inline
  168. \[5] Anonymous
  169. ")))
  170. ;; 1.2. When no footnote section is present, create it. Follow
  171. ;; `org-blank-before-new-entry' specifications when doing so.
  172. (org-test-with-temp-text "Paragraph[fn:1]\n\n[fn:1] Definition"
  173. (org-footnote-normalize)
  174. (should (equal (buffer-string)
  175. "Paragraph[1]\n\n* Footnotes\n\n[1] Definition")))
  176. (org-test-with-temp-text "Paragraph[fn:1]\n* Head1\n[fn:1] Definition"
  177. (let ((org-blank-before-new-entry '((heading))))
  178. (org-footnote-normalize))
  179. (should (equal (buffer-string)
  180. "Paragraph[1]\n* Head1\n* Footnotes\n\n[1] Definition")))
  181. ;; 1.3. When the footnote section is misplaced, move it at the end
  182. ;; of the buffer.
  183. (org-test-with-temp-text "* Head1
  184. Body[fn:1]
  185. * Footnotes
  186. \[fn:1] Definition 1
  187. * Head2"
  188. (org-footnote-normalize)
  189. (should
  190. (equal (buffer-string)
  191. "* Head1
  192. Body[1]
  193. * Head2
  194. * Footnotes
  195. \[1] Definition 1"))))
  196. ;; 2. With a nil `org-footnote-section'.
  197. (let ((org-footnote-section nil))
  198. ;; 2.1. Normalize each type of footnote: standard, labelled,
  199. ;; numbered, inline, anonymous.
  200. (org-test-with-temp-text
  201. "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
  202. \[fn:1] Standard
  203. \[fn:label] Labelled
  204. \[1] Numbered"
  205. (org-footnote-normalize)
  206. (should
  207. (equal (buffer-string)
  208. "Paragraph[1][2][3][4][5]
  209. \[1] Standard
  210. \[2] Labelled
  211. \[3] Numbered
  212. \[4] Inline
  213. \[5] Anonymous
  214. ")))
  215. ;; 2.2. Put each footnote definition at the end of the section
  216. ;; containing its first reference.
  217. (org-test-with-temp-text
  218. "* Head 1
  219. Text[fn:1:Def1]
  220. * Head 2
  221. Text[fn:1]
  222. * Head 3
  223. Text[fn:2:Def2]"
  224. (org-footnote-normalize)
  225. (should
  226. (equal (buffer-string)
  227. "* Head 1
  228. Text[1]
  229. \[1] Def1
  230. * Head 2
  231. Text[1]
  232. * Head 3
  233. Text[2]
  234. \[2] Def2
  235. ")))))
  236. (ert-deftest test-org-footnote/normalize-outside-org ()
  237. "Test `org-footnote-normalize' specifications for buffers not in Org mode."
  238. ;; 1. In a non-Org buffer, footnotes definitions are always put at
  239. ;; its end.
  240. (should
  241. (equal
  242. "Paragraph[1][2][3][4][5]
  243. Some additional text.
  244. \[1] Standard
  245. \[2] Labelled
  246. \[3] Numbered
  247. \[4] Inline
  248. \[5] Anonymous"
  249. (let ((org-footnote-tag-for-non-org-mode-files nil))
  250. (with-temp-buffer
  251. (insert "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
  252. \[fn:1] Standard
  253. \[fn:label] Labelled
  254. \[1] Numbered
  255. Some additional text.")
  256. (org-footnote-normalize)
  257. (buffer-string)))))
  258. ;; 2. With a special tag.
  259. (let ((org-footnote-tag-for-non-org-mode-files "Footnotes:"))
  260. ;; 2.1. The tag must be inserted before the footnotes, separated
  261. ;; from the rest of the text with a blank line.
  262. (with-temp-buffer
  263. (insert "Paragraph[fn:1][fn::Anonymous]
  264. \[fn:1] Standard
  265. Some additional text.")
  266. (org-footnote-normalize)
  267. (should
  268. (equal (buffer-string)
  269. "Paragraph[1][2]
  270. Some additional text.
  271. Footnotes:
  272. \[1] Standard
  273. \[2] Anonymous")))
  274. ;; 2.2. Any tag already inserted in the buffer should be removed
  275. ;; prior to footnotes insertion.
  276. (with-temp-buffer
  277. (insert "Text[fn:1]
  278. Footnotes:
  279. Additional text.
  280. Footnotes:
  281. \[fn:1] Definition")
  282. (org-footnote-normalize)
  283. (should
  284. (equal (buffer-string)
  285. "Text[1]
  286. Additional text.
  287. Footnotes:
  288. \[1] Definition"))))
  289. ;; 3. As an exception, in `message-mode' buffer, if a signature is
  290. ;; present, insert footnotes before it.n
  291. (let ((org-footnote-tag-for-non-org-mode-files nil))
  292. (with-temp-buffer
  293. (insert "Body[fn::def]
  294. --
  295. Fake signature
  296. --
  297. Signature")
  298. ;; Mimic `message-mode'.
  299. (let ((major-mode 'message-mode)
  300. (message-cite-prefix-regexp "\\([ ]*[_.[:word:]]+>+\\|[ ]*[]>|]\\)+")
  301. (message-signature-separator "^-- $"))
  302. (flet ((message-point-in-header-p nil nil))
  303. (org-footnote-normalize)))
  304. (should
  305. (equal (buffer-string)
  306. "Body[1]
  307. --
  308. Fake signature
  309. \[1] def
  310. --
  311. Signature")))))
  312. (ert-deftest test-org-footnote/sort ()
  313. "Test footnotes definitions sorting."
  314. (let ((org-footnote-section nil))
  315. (org-test-with-temp-text
  316. "Text[fn:1][fn::inline][fn:2][fn:label]
  317. \[fn:label] C
  318. \[fn:1] A
  319. \[fn:2] B"
  320. (org-footnote-normalize 'sort)
  321. (should
  322. (equal (buffer-string)
  323. "Text[fn:1][fn::inline][fn:2][fn:label]
  324. \[fn:1] A
  325. \[fn:2] B
  326. \[fn:label] C
  327. ")))))
  328. (provide 'test-org-footnote)
  329. ;;; test-org-footnote.el ends here