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 ;; In an headline or inlinetask, point must be either on the
71 ;; heading itself or on the blank lines below.
72 (should (org-test-with-temp-text "* H<point>" (org-footnote-new) t
))
74 (org-test-with-temp-text "* H\n <point>\nParagraph" (org-footnote-new) t
))
75 (should-error (org-test-with-temp-text "*<point> H" (org-footnote-new) t
))
77 (org-test-with-temp-text "* H <point>:tag:" (org-footnote-new) t
))
78 ;; Allow new footnotes within recursive objects, but not in links.
81 " \\*bold\\[fn:1\\]\\*"
82 (org-test-with-temp-text " *bold<point>*"
83 (let ((org-footnote-auto-label t
)) (org-footnote-new))
86 (org-test-with-temp-text " [[http://orgmode.org][Org mode<point>]]"
88 ;; Allow new footnotes in blank lines after an element or white
89 ;; spaces after an object.
93 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nA\n#+END_EXAMPLE\n <point>"
94 (let ((org-footnote-auto-label t
)) (org-footnote-new))
98 " \\*bold\\*\\[fn:1\\]"
99 (org-test-with-temp-text " *bold*<point>"
100 (let ((org-footnote-auto-label t
)) (org-footnote-new))
103 (ert-deftest test-org-footnote
/delete
()
104 "Test `org-footnote-delete' specifications."
108 (org-test-with-temp-text "Paragraph[1]\n\n[1] Definition"
110 (org-footnote-delete)
111 (org-trim (buffer-string)))))
112 ;; Remove multiple definitions and references.
114 (equal "Paragraph and another"
115 (org-test-with-temp-text
116 "Paragraph[1] and another[1]\n\n[1] def\n\n[1] def"
118 (org-footnote-delete)
119 (org-trim (buffer-string)))))
120 ;; Delete inline footnotes and all references.
123 (org-test-with-temp-text "Para[fn:label:def] and[fn:label]"
125 (org-footnote-delete)
126 (org-trim (buffer-string)))))
127 ;; Delete anonymous footnotes.
130 (org-test-with-temp-text "Para[fn::def]"
132 (org-footnote-delete)
133 (org-trim (buffer-string)))))
134 ;; With an argument, delete footnote with specified label.
136 (equal "Paragraph[1] and another\n\n[1] def"
137 (let ((org-footnote-section nil
))
138 (org-test-with-temp-text
139 "Paragraph[1] and another[2]\n\n[1] def\n\n[2] def2"
140 (org-footnote-delete "2")
141 (org-trim (buffer-string))))))
142 ;; Error when no argument is specified at point is not at a footnote
145 (org-test-with-temp-text "Para[1]\n\n[1] Def"
146 (org-footnote-delete)))
147 ;; Correctly delete footnotes with multiple paragraphs.
149 (equal "Para\n\n\nOutside footnote."
150 (org-test-with-temp-text
151 "Para[1]\n\n[1] para1\n\npara2\n\n\nOutside footnote."
152 (org-footnote-delete "1")
153 (org-trim (buffer-string))))))
155 (ert-deftest test-org-footnote
/goto-definition
()
156 "Test `org-footnote-goto-definition' specifications."
157 ;; Error on unknown definitions.
159 (org-test-with-temp-text "No footnote definition"
160 (org-footnote-goto-definition "fn:1")))
161 ;; Error when trying to reach a definition outside narrowed part of
164 (org-test-with-temp-text "Some text<point>\n[fn:1] Definition."
165 (narrow-to-region (point-min) (point))
166 (org-footnote-goto-definition "fn:1")))
167 ;; Otherwise, move at the beginning of the definition, including
168 ;; anonymous footnotes.
172 (org-test-with-temp-text "Some text\n[fn:1] Definition."
173 (org-footnote-goto-definition "fn:1")
174 (buffer-substring (point) (point-max)))))
178 (org-test-with-temp-text "Some text[fn:label:definition]"
179 (org-footnote-goto-definition "fn:label")
180 (buffer-substring (point) (point-max))))))
182 (ert-deftest test-org-footnote
/normalize-in-org
()
183 "Test specifications for `org-footnote-normalize' in an Org buffer."
184 ;; 1. With a non-nil `org-footnote-section'.
185 (let ((org-footnote-section "Footnotes")
186 (org-blank-before-new-entry '((heading . auto
))))
187 ;; 1.1. Normalize each type of footnote: standard, labelled,
188 ;; numbered, inline, anonymous.
189 (org-test-with-temp-text
190 "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
199 (org-footnote-normalize)
201 (equal (buffer-string)
202 "Paragraph[1][2][3][4][5]
218 ;; 1.2. When no footnote section is present, create it. Follow
219 ;; `org-blank-before-new-entry' specifications when doing so.
220 (org-test-with-temp-text "Paragraph[fn:1]\n\n[fn:1] Definition"
221 (org-footnote-normalize)
222 (should (equal (buffer-string)
223 "Paragraph[1]\n\n* Footnotes\n\n[1] Definition")))
224 (org-test-with-temp-text "Paragraph[fn:1]\n* Head1\n[fn:1] Definition"
225 (let ((org-blank-before-new-entry '((heading))))
226 (org-footnote-normalize))
227 (should (equal (buffer-string)
228 "Paragraph[1]\n* Head1\n* Footnotes\n\n[1] Definition")))
229 ;; 1.3. When the footnote section is misplaced, move it at the end
231 (org-test-with-temp-text "* Head1
236 (org-footnote-normalize)
238 (equal (buffer-string)
245 \[1] Definition 1"))))
246 ;; 2. With a nil `org-footnote-section'.
247 (let ((org-footnote-section nil
))
248 ;; 2.1. Normalize each type of footnote: standard, labelled,
249 ;; numbered, inline, anonymous.
250 (org-test-with-temp-text
251 "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
258 (org-footnote-normalize)
260 (equal (buffer-string)
261 "Paragraph[1][2][3][4][5]
273 ;; 2.2. Put each footnote definition at the end of the section
274 ;; containing its first reference.
275 (org-test-with-temp-text
282 (org-footnote-normalize)
284 (equal (buffer-string)
297 (ert-deftest test-org-footnote
/normalize-outside-org
()
298 "Test `org-footnote-normalize' specifications for buffers not in Org mode."
299 ;; 1. In a non-Org buffer, footnotes definitions are always put at
303 "Paragraph[1][2][3][4][5]
306 Some additional text.
317 (let ((org-footnote-tag-for-non-org-mode-files nil
))
319 (insert "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
328 Some additional text.")
329 (org-footnote-normalize)
331 ;; 2. With a special tag.
332 (let ((org-footnote-tag-for-non-org-mode-files "Footnotes:"))
333 ;; 2.1. The tag must be inserted before the footnotes, separated
334 ;; from the rest of the text with a blank line.
336 (insert "Paragraph[fn:1][fn::Anonymous]
341 Some additional text.")
342 (org-footnote-normalize)
344 (equal (buffer-string)
348 Some additional text.
355 ;; 2.2. Any tag already inserted in the buffer should be removed
356 ;; prior to footnotes insertion.
366 (org-footnote-normalize)
368 (equal (buffer-string)
376 ;; 3. As an exception, in `message-mode' buffer, if a signature is
377 ;; present, insert footnotes before it.n
378 (let ((org-footnote-tag-for-non-org-mode-files nil
))
380 (insert "Body[fn::def]
385 ;; Mimic `message-mode'.
386 (let ((major-mode 'message-mode
)
387 (message-cite-prefix-regexp "\\([ ]*[_.[:word:]]+>+\\|[ ]*[]>|]\\)+")
388 (message-signature-separator "^-- $"))
389 (flet ((message-point-in-header-p nil nil
))
390 (org-footnote-normalize)))
392 (equal (buffer-string)
402 (ert-deftest test-org-footnote
/sort
()
403 "Test footnotes definitions sorting."
404 (let ((org-footnote-section nil
))
405 (org-test-with-temp-text
406 "Text[fn:1][fn::inline][fn:2][fn:label]
413 (org-footnote-normalize 'sort
)
415 (equal (buffer-string)
416 "Text[fn:1][fn::inline][fn:2][fn:label]
426 (provide 'test-org-footnote
)
427 ;;; test-org-footnote.el ends here