1 ;;; test-org-footnote.el --- Tests for org-footnote.el
3 ;; Copyright (C) 2012-2015 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 (ert-deftest test-org-footnote
/new
()
23 "Test `org-footnote-new' specifications."
24 ;; `org-footnote-auto-label' is t.
27 "Test\\[fn:1\\]\n+\\[fn:1\\]"
28 (org-test-with-temp-text "Test<point>"
29 (let ((org-footnote-auto-label t
)
30 (org-footnote-section nil
))
33 ;; `org-footnote-auto-label' is `plain'.
36 "Test\\[1\\]\n+\\[1\\]"
37 (org-test-with-temp-text "Test<point>"
38 (let ((org-footnote-auto-label 'plain
)
39 (org-footnote-section nil
))
42 ;; `org-footnote-auto-label' is `random'.
45 "Test\\[fn:\\(.+?\\)\\]\n+\\[fn:\\1\\]"
46 (org-test-with-temp-text "Test<point>"
47 (let ((org-footnote-auto-label 'random
)
48 (org-footnote-section nil
))
51 ;; Error at beginning of line.
53 (org-test-with-temp-text "<point>Test"
57 (org-test-with-temp-text "#+TIT<point>LE: value"
60 (org-test-with-temp-text "#+CAPTION: <point>\nParagraph"
62 ;; Allow new footnotes in blank lines at the beginning of the
67 (org-test-with-temp-text " <point>"
68 (let ((org-footnote-auto-label t
)) (org-footnote-new))
70 ;; Allow new footnotes within recursive objects, but not in links.
73 " \\*bold\\[fn:1\\]\\*"
74 (org-test-with-temp-text " *bold<point>*"
75 (let ((org-footnote-auto-label t
)) (org-footnote-new))
78 (org-test-with-temp-text " [[http://orgmode.org][Org mode<point>]]"
80 ;; Allow new footnotes in blank lines after an element or white
81 ;; spaces after an object.
85 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nA\n#+END_EXAMPLE\n <point>"
86 (let ((org-footnote-auto-label t
)) (org-footnote-new))
90 " \\*bold\\*\\[fn:1\\]"
91 (org-test-with-temp-text " *bold*<point>"
92 (let ((org-footnote-auto-label t
)) (org-footnote-new))
95 (ert-deftest test-org-footnote
/delete
()
96 "Test `org-footnote-delete' specifications."
100 (org-test-with-temp-text "Paragraph[1]\n\n[1] Definition"
102 (org-footnote-delete)
103 (org-trim (buffer-string)))))
104 ;; Remove multiple definitions and references.
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"
110 (org-footnote-delete)
111 (org-trim (buffer-string)))))
112 ;; Delete inline footnotes and all references.
115 (org-test-with-temp-text "Para[fn:label:def] and[fn:label]"
117 (org-footnote-delete)
118 (org-trim (buffer-string)))))
119 ;; Delete anonymous footnotes.
122 (org-test-with-temp-text "Para[fn::def]"
124 (org-footnote-delete)
125 (org-trim (buffer-string)))))
126 ;; With an argument, delete footnote with specified label.
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
137 (org-test-with-temp-text "Para[1]\n\n[1] Def"
138 (org-footnote-delete)))
139 ;; Correctly delete footnotes with multiple paragraphs.
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))))))
147 (ert-deftest test-org-footnote
/normalize-in-org
()
148 "Test specifications for `org-footnote-normalize' in an Org buffer."
149 ;; 1. With a non-nil `org-footnote-section'.
150 (let ((org-footnote-section "Footnotes")
151 (org-blank-before-new-entry '((heading . auto
))))
152 ;; 1.1. Normalize each type of footnote: standard, labelled,
153 ;; numbered, inline, anonymous.
154 (org-test-with-temp-text
155 "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
164 (org-footnote-normalize)
166 (equal (buffer-string)
167 "Paragraph[1][2][3][4][5]
183 ;; 1.2. When no footnote section is present, create it. Follow
184 ;; `org-blank-before-new-entry' specifications when doing so.
185 (org-test-with-temp-text "Paragraph[fn:1]\n\n[fn:1] Definition"
186 (org-footnote-normalize)
187 (should (equal (buffer-string)
188 "Paragraph[1]\n\n* Footnotes\n\n[1] Definition")))
189 (org-test-with-temp-text "Paragraph[fn:1]\n* Head1\n[fn:1] Definition"
190 (let ((org-blank-before-new-entry '((heading))))
191 (org-footnote-normalize))
192 (should (equal (buffer-string)
193 "Paragraph[1]\n* Head1\n* Footnotes\n\n[1] Definition")))
194 ;; 1.3. When the footnote section is misplaced, move it at the end
196 (org-test-with-temp-text "* Head1
201 (org-footnote-normalize)
203 (equal (buffer-string)
210 \[1] Definition 1"))))
211 ;; 2. With a nil `org-footnote-section'.
212 (let ((org-footnote-section nil
))
213 ;; 2.1. Normalize each type of footnote: standard, labelled,
214 ;; numbered, inline, anonymous.
215 (org-test-with-temp-text
216 "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
223 (org-footnote-normalize)
225 (equal (buffer-string)
226 "Paragraph[1][2][3][4][5]
238 ;; 2.2. Put each footnote definition at the end of the section
239 ;; containing its first reference.
240 (org-test-with-temp-text
247 (org-footnote-normalize)
249 (equal (buffer-string)
262 (ert-deftest test-org-footnote
/normalize-outside-org
()
263 "Test `org-footnote-normalize' specifications for buffers not in Org mode."
264 ;; 1. In a non-Org buffer, footnotes definitions are always put at
268 "Paragraph[1][2][3][4][5]
271 Some additional text.
282 (let ((org-footnote-tag-for-non-org-mode-files nil
))
284 (insert "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
293 Some additional text.")
294 (org-footnote-normalize)
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.
301 (insert "Paragraph[fn:1][fn::Anonymous]
306 Some additional text.")
307 (org-footnote-normalize)
309 (equal (buffer-string)
313 Some additional text.
320 ;; 2.2. Any tag already inserted in the buffer should be removed
321 ;; prior to footnotes insertion.
331 (org-footnote-normalize)
333 (equal (buffer-string)
341 ;; 3. As an exception, in `message-mode' buffer, if a signature is
342 ;; present, insert footnotes before it.n
343 (let ((org-footnote-tag-for-non-org-mode-files nil
))
345 (insert "Body[fn::def]
350 ;; Mimic `message-mode'.
351 (let ((major-mode 'message-mode
)
352 (message-cite-prefix-regexp "\\([ ]*[_.[:word:]]+>+\\|[ ]*[]>|]\\)+")
353 (message-signature-separator "^-- $"))
354 (flet ((message-point-in-header-p nil nil
))
355 (org-footnote-normalize)))
357 (equal (buffer-string)
367 (ert-deftest test-org-footnote
/sort
()
368 "Test footnotes definitions sorting."
369 (let ((org-footnote-section nil
))
370 (org-test-with-temp-text
371 "Text[fn:1][fn::inline][fn:2][fn:label]
378 (org-footnote-normalize 'sort
)
380 (equal (buffer-string)
381 "Text[fn:1][fn::inline][fn:2][fn:label]
391 (provide 'test-org-footnote
)
392 ;;; test-org-footnote.el ends here