test-org-footnote.el 11 KB

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