test-org-footnote.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/goto-definition ()
  147. "Test `org-footnote-goto-definition' specifications."
  148. ;; Error on unknown definitions.
  149. (should-error
  150. (org-test-with-temp-text "No footnote definition"
  151. (org-footnote-goto-definition "fn:1")))
  152. ;; Error when trying to reach a definition outside narrowed part of
  153. ;; buffer.
  154. (should-error
  155. (org-test-with-temp-text "Some text<point>\n[fn:1] Definition."
  156. (narrow-to-region (point-min) (point))
  157. (org-footnote-goto-definition "fn:1")))
  158. (should-error
  159. (org-test-with-temp-text "[fn:1] Definition.\n<point>Some text"
  160. (narrow-to-region (point) (point-max))
  161. (org-footnote-goto-definition "fn:1")))
  162. ;; Otherwise, move at the beginning of the definition, including
  163. ;; anonymous footnotes.
  164. (should
  165. (equal
  166. " Definition."
  167. (org-test-with-temp-text "Some text\n[fn:1] Definition."
  168. (org-footnote-goto-definition "fn:1")
  169. (buffer-substring (point) (point-max)))))
  170. (should
  171. (equal
  172. "definition]"
  173. (org-test-with-temp-text "Some text[fn:label:definition]"
  174. (org-footnote-goto-definition "fn:label")
  175. (buffer-substring (point) (point-max))))))
  176. (ert-deftest test-org-footnote/normalize-in-org ()
  177. "Test specifications for `org-footnote-normalize' in an Org buffer."
  178. ;; With a non-nil `org-footnote-section', normalize each type of
  179. ;; footnote: standard, labelled, numbered, inline, anonymous.
  180. (should
  181. (equal "Paragraph[1][2][3][4][5]
  182. * Footnotes
  183. \[1] Standard
  184. \[2] Labelled
  185. \[3] Numbered
  186. \[4] Inline
  187. \[5] Anonymous
  188. "
  189. (let ((org-footnote-section "Footnotes")
  190. (org-blank-before-new-entry '((heading . auto))))
  191. (org-test-with-temp-text
  192. "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
  193. * Footnotes
  194. \[fn:1] Standard
  195. \[fn:label] Labelled
  196. \[1] Numbered"
  197. (org-footnote-normalize)
  198. (buffer-string)))))
  199. ;; When no footnote section is present, create it. Follow
  200. ;; `org-blank-before-new-entry' specifications when doing so.
  201. (should
  202. (equal "Paragraph[1]\n\n* Footnotes\n\n[1] Definition"
  203. (let ((org-footnote-section "Footnotes")
  204. (org-blank-before-new-entry '((heading . auto))))
  205. (org-test-with-temp-text "Paragraph[fn:1]\n\n[fn:1] Definition"
  206. (org-footnote-normalize)
  207. (buffer-string)))))
  208. (should
  209. (equal
  210. "Paragraph[1]\n* Head1\n* Footnotes\n\n[1] Definition"
  211. (let ((org-footnote-section "Footnotes")
  212. (org-blank-before-new-entry '((heading))))
  213. (org-test-with-temp-text "Paragraph[fn:1]\n* Head1\n[fn:1] Definition"
  214. (org-footnote-normalize)
  215. (buffer-string)))))
  216. ;; When the footnote section is misplaced, move it at the end of
  217. ;; the buffer.
  218. (should
  219. (equal
  220. "* Head1
  221. Body[1]
  222. * Head2
  223. * Footnotes
  224. \[1] Definition 1"
  225. (let ((org-footnote-section "Footnotes")
  226. (org-blank-before-new-entry '((heading . auto))))
  227. (org-test-with-temp-text "* Head1
  228. Body[fn:1]
  229. * Footnotes
  230. \[fn:1] Definition 1
  231. * Head2"
  232. (org-footnote-normalize)
  233. (buffer-string)))))
  234. ;; With a nil `org-footnote-section', normalize each type of
  235. ;; footnote: standard, labelled, numbered, inline, anonymous.
  236. (should
  237. (equal "Paragraph[1][2][3][4][5]
  238. \[1] Standard
  239. \[2] Labelled
  240. \[3] Numbered
  241. \[4] Inline
  242. \[5] Anonymous
  243. "
  244. (let ((org-footnote-section nil))
  245. (org-test-with-temp-text
  246. "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
  247. \[fn:1] Standard
  248. \[fn:label] Labelled
  249. \[1] Numbered"
  250. (org-footnote-normalize)
  251. (buffer-string)))))
  252. ;; Also put each footnote definition at the end of the section
  253. ;; containing its first reference.
  254. (should
  255. (equal "* Head 1
  256. Text[1]
  257. \[1] Def1
  258. * Head 2
  259. Text[1]
  260. * Head 3
  261. Text[2]
  262. \[2] Def2
  263. "
  264. (let ((org-footnote-section nil))
  265. (org-test-with-temp-text
  266. "* Head 1
  267. Text[fn:1:Def1]
  268. * Head 2
  269. Text[fn:1]
  270. * Head 3
  271. Text[fn:2:Def2]"
  272. (org-footnote-normalize)
  273. (buffer-string))))))
  274. (ert-deftest test-org-footnote/normalize-outside-org ()
  275. "Test `org-footnote-normalize' specifications for buffers not in Org mode."
  276. ;; 1. In a non-Org buffer, footnotes definitions are always put at
  277. ;; its end.
  278. (should
  279. (equal
  280. "Paragraph[1][2][3][4][5]
  281. Some additional text.
  282. \[1] Standard
  283. \[2] Labelled
  284. \[3] Numbered
  285. \[4] Inline
  286. \[5] Anonymous"
  287. (let ((org-footnote-tag-for-non-org-mode-files nil))
  288. (with-temp-buffer
  289. (insert "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
  290. \[fn:1] Standard
  291. \[fn:label] Labelled
  292. \[1] Numbered
  293. Some additional text.")
  294. (org-footnote-normalize)
  295. (buffer-string)))))
  296. ;; 2. With a special tag.
  297. (let ((org-footnote-tag-for-non-org-mode-files "Footnotes:"))
  298. ;; 2.1. The tag must be inserted before the footnotes, separated
  299. ;; from the rest of the text with a blank line.
  300. (with-temp-buffer
  301. (insert "Paragraph[fn:1][fn::Anonymous]
  302. \[fn:1] Standard
  303. Some additional text.")
  304. (org-footnote-normalize)
  305. (should
  306. (equal (buffer-string)
  307. "Paragraph[1][2]
  308. Some additional text.
  309. Footnotes:
  310. \[1] Standard
  311. \[2] Anonymous")))
  312. ;; 2.2. Any tag already inserted in the buffer should be removed
  313. ;; prior to footnotes insertion.
  314. (with-temp-buffer
  315. (insert "Text[fn:1]
  316. Footnotes:
  317. Additional text.
  318. Footnotes:
  319. \[fn:1] Definition")
  320. (org-footnote-normalize)
  321. (should
  322. (equal (buffer-string)
  323. "Text[1]
  324. Additional text.
  325. Footnotes:
  326. \[1] Definition"))))
  327. ;; 3. As an exception, in `message-mode' buffer, if a signature is
  328. ;; present, insert footnotes before it.n
  329. (let ((org-footnote-tag-for-non-org-mode-files nil))
  330. (with-temp-buffer
  331. (insert "Body[fn::def]
  332. --
  333. Fake signature
  334. --
  335. Signature")
  336. ;; Mimic `message-mode'.
  337. (let ((major-mode 'message-mode)
  338. (message-cite-prefix-regexp "\\([ ]*[_.[:word:]]+>+\\|[ ]*[]>|]\\)+")
  339. (message-signature-separator "^-- $"))
  340. (flet ((message-point-in-header-p nil nil))
  341. (org-footnote-normalize)))
  342. (should
  343. (equal (buffer-string)
  344. "Body[1]
  345. --
  346. Fake signature
  347. \[1] def
  348. --
  349. Signature")))))
  350. (ert-deftest test-org-footnote/sort ()
  351. "Test footnotes definitions sorting."
  352. (let ((org-footnote-section nil))
  353. (org-test-with-temp-text
  354. "Text[fn:1][fn::inline][fn:2][fn:label]
  355. \[fn:label] C
  356. \[fn:1] A
  357. \[fn:2] B"
  358. (org-footnote-normalize 'sort)
  359. (should
  360. (equal (buffer-string)
  361. "Text[fn:1][fn::inline][fn:2][fn:label]
  362. \[fn:1] A
  363. \[fn:2] B
  364. \[fn:label] C
  365. ")))))
  366. (provide 'test-org-footnote)
  367. ;;; test-org-footnote.el ends here