1 ;;; test-org-footnote.el --- Tests for org-footnote.el
3 ;; Copyright (C) 2012, 2013, 2014 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
/delete
()
23 "Test `org-footnote-delete' specifications."
27 (org-test-with-temp-text "Paragraph[1]\n\n[1] Definition"
30 (org-trim (buffer-string)))))
31 ;; Remove multiple definitions and references.
33 (equal "Paragraph and another"
34 (org-test-with-temp-text
35 "Paragraph[1] and another[1]\n\n[1] def\n\n[1] def"
38 (org-trim (buffer-string)))))
39 ;; Delete inline footnotes and all references.
42 (org-test-with-temp-text "Para[fn:label:def] and[fn:label]"
45 (org-trim (buffer-string)))))
46 ;; Delete anonymous footnotes.
49 (org-test-with-temp-text "Para[fn::def]"
52 (org-trim (buffer-string)))))
53 ;; With an argument, delete footnote with specified label.
55 (equal "Paragraph[1] and another\n\n[1] def"
56 (let ((org-footnote-section nil
))
57 (org-test-with-temp-text
58 "Paragraph[1] and another[2]\n\n[1] def\n\n[2] def2"
59 (org-footnote-delete "2")
60 (org-trim (buffer-string))))))
61 ;; Error when no argument is specified at point is not at a footnote
64 (org-test-with-temp-text "Para[1]\n\n[1] Def"
65 (org-footnote-delete)))
66 ;; Correctly delete footnotes with multiple paragraphs.
68 (equal "Para\n\n\nOutside footnote."
69 (org-test-with-temp-text
70 "Para[1]\n\n[1] para1\n\npara2\n\n\nOutside footnote."
71 (org-footnote-delete "1")
72 (org-trim (buffer-string))))))
74 (ert-deftest test-org-footnote
/normalize-in-org
()
75 "Test specifications for `org-footnote-normalize' in an Org buffer."
76 ;; 1. With a non-nil `org-footnote-section'.
77 (let ((org-footnote-section "Footnotes")
78 (org-blank-before-new-entry '((heading . auto
))))
79 ;; 1.1. Normalize each type of footnote: standard, labelled,
80 ;; numbered, inline, anonymous.
81 (org-test-with-temp-text
82 "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
91 (org-footnote-normalize)
93 (equal (buffer-string)
94 "Paragraph[1][2][3][4][5]
110 ;; 1.2. When no footnote section is present, create it. Follow
111 ;; `org-blank-before-new-entry' specifications when doing so.
112 (org-test-with-temp-text "Paragraph[fn:1]\n\n[fn:1] Definition"
113 (org-footnote-normalize)
114 (should (equal (buffer-string)
115 "Paragraph[1]\n\n* Footnotes\n\n[1] Definition")))
116 (org-test-with-temp-text "Paragraph[fn:1]\n* Head1\n[fn:1] Definition"
117 (let ((org-blank-before-new-entry '((heading))))
118 (org-footnote-normalize))
119 (should (equal (buffer-string)
120 "Paragraph[1]\n* Head1\n* Footnotes\n\n[1] Definition")))
121 ;; 1.3. When the footnote section is misplaced, move it at the end
123 (org-test-with-temp-text "* Head1
128 (org-footnote-normalize)
130 (equal (buffer-string)
137 \[1] Definition 1"))))
138 ;; 2. With a nil `org-footnote-section'.
139 (let ((org-footnote-section nil
))
140 ;; 2.1. Normalize each type of footnote: standard, labelled,
141 ;; numbered, inline, anonymous.
142 (org-test-with-temp-text
143 "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
150 (org-footnote-normalize)
152 (equal (buffer-string)
153 "Paragraph[1][2][3][4][5]
165 ;; 2.2. Put each footnote definition at the end of the section
166 ;; containing its first reference.
167 (org-test-with-temp-text
174 (org-footnote-normalize)
176 (equal (buffer-string)
189 (ert-deftest test-org-footnote
/normalize-outside-org
()
190 "Test `org-footnote-normalize' specifications for buffers not in Org mode."
191 ;; 1. In a non-Org buffer, footnotes definitions are always put at
195 "Paragraph[1][2][3][4][5]
198 Some additional text.
209 (let ((org-footnote-tag-for-non-org-mode-files nil
))
211 (insert "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
220 Some additional text.")
221 (org-footnote-normalize)
223 ;; 2. With a special tag.
224 (let ((org-footnote-tag-for-non-org-mode-files "Footnotes:"))
225 ;; 2.1. The tag must be inserted before the footnotes, separated
226 ;; from the rest of the text with a blank line.
228 (insert "Paragraph[fn:1][fn::Anonymous]
233 Some additional text.")
234 (org-footnote-normalize)
236 (equal (buffer-string)
240 Some additional text.
247 ;; 2.2. Any tag already inserted in the buffer should be removed
248 ;; prior to footnotes insertion.
258 (org-footnote-normalize)
260 (equal (buffer-string)
268 ;; 3. As an exception, in `message-mode' buffer, if a signature is
269 ;; present, insert footnotes before it.n
270 (let ((org-footnote-tag-for-non-org-mode-files nil
))
272 (insert "Body[fn::def]
277 ;; Mimic `message-mode'.
278 (let ((major-mode 'message-mode
)
279 (message-cite-prefix-regexp "\\([ ]*[_.[:word:]]+>+\\|[ ]*[]>|]\\)+")
280 (message-signature-separator "^-- $"))
281 (flet ((message-point-in-header-p nil nil
))
282 (org-footnote-normalize)))
284 (equal (buffer-string)
294 (ert-deftest test-org-footnote
/sort
()
295 "Test footnotes definitions sorting."
296 (let ((org-footnote-section nil
))
297 (org-test-with-temp-text
298 "Text[fn:1][fn::inline][fn:2][fn:label]
305 (org-footnote-normalize 'sort
)
307 (equal (buffer-string)
308 "Text[fn:1][fn::inline][fn:2][fn:label]
318 (provide 'test-org-footnote
)
319 ;;; test-org-footnote.el ends here