Update copyright years.
[org-mode.git] / testing / lisp / test-org-footnote.el
blobbe76119bcdaefa599aab0eab16bc6ff11ca02313
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/>.
20 ;;; Code:
22 (ert-deftest test-org-footnote/delete ()
23 "Test `org-footnote-delete' specifications."
24 ;; Regular test.
25 (should
26 (equal "Paragraph"
27 (org-test-with-temp-text "Paragraph[1]\n\n[1] Definition"
28 (search-forward "[")
29 (org-footnote-delete)
30 (org-trim (buffer-string)))))
31 ;; Remove multiple definitions and references.
32 (should
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"
36 (search-forward "[")
37 (org-footnote-delete)
38 (org-trim (buffer-string)))))
39 ;; Delete inline footnotes and all references.
40 (should
41 (equal "Para and"
42 (org-test-with-temp-text "Para[fn:label:def] and[fn:label]"
43 (search-forward "[")
44 (org-footnote-delete)
45 (org-trim (buffer-string)))))
46 ;; Delete anonymous footnotes.
47 (should
48 (equal "Para"
49 (org-test-with-temp-text "Para[fn::def]"
50 (search-forward "[")
51 (org-footnote-delete)
52 (org-trim (buffer-string)))))
53 ;; With an argument, delete footnote with specified label.
54 (should
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
62 ;; reference.
63 (should-error
64 (org-test-with-temp-text "Para[1]\n\n[1] Def"
65 (org-footnote-delete)))
66 ;; Correctly delete footnotes with multiple paragraphs.
67 (should
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]
84 * Footnotes
86 \[fn:1] Standard
88 \[fn:label] Labelled
90 \[1] Numbered"
91 (org-footnote-normalize)
92 (should
93 (equal (buffer-string)
94 "Paragraph[1][2][3][4][5]
96 * Footnotes
98 \[1] Standard
100 \[2] Labelled
102 \[3] Numbered
104 \[4] Inline
106 \[5] Anonymous
109 ")))
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
122 ;; of the buffer.
123 (org-test-with-temp-text "* Head1
124 Body[fn:1]
125 * Footnotes
126 \[fn:1] Definition 1
127 * Head2"
128 (org-footnote-normalize)
129 (should
130 (equal (buffer-string)
131 "* Head1
132 Body[1]
133 * Head2
135 * Footnotes
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]
145 \[fn:1] Standard
147 \[fn:label] Labelled
149 \[1] Numbered"
150 (org-footnote-normalize)
151 (should
152 (equal (buffer-string)
153 "Paragraph[1][2][3][4][5]
155 \[1] Standard
157 \[2] Labelled
159 \[3] Numbered
161 \[4] Inline
163 \[5] Anonymous
164 ")))
165 ;; 2.2. Put each footnote definition at the end of the section
166 ;; containing its first reference.
167 (org-test-with-temp-text
168 "* Head 1
169 Text[fn:1:Def1]
170 * Head 2
171 Text[fn:1]
172 * Head 3
173 Text[fn:2:Def2]"
174 (org-footnote-normalize)
175 (should
176 (equal (buffer-string)
177 "* Head 1
178 Text[1]
180 \[1] Def1
181 * Head 2
182 Text[1]
183 * Head 3
184 Text[2]
186 \[2] Def2
187 ")))))
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
192 ;; its end.
193 (should
194 (equal
195 "Paragraph[1][2][3][4][5]
198 Some additional text.
200 \[1] Standard
202 \[2] Labelled
204 \[3] Numbered
206 \[4] Inline
208 \[5] Anonymous"
209 (let ((org-footnote-tag-for-non-org-mode-files nil))
210 (with-temp-buffer
211 (insert "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
213 \[fn:1] Standard
215 \[fn:label] Labelled
217 \[1] Numbered
220 Some additional text.")
221 (org-footnote-normalize)
222 (buffer-string)))))
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.
227 (with-temp-buffer
228 (insert "Paragraph[fn:1][fn::Anonymous]
230 \[fn:1] Standard
233 Some additional text.")
234 (org-footnote-normalize)
235 (should
236 (equal (buffer-string)
237 "Paragraph[1][2]
240 Some additional text.
242 Footnotes:
244 \[1] Standard
246 \[2] Anonymous")))
247 ;; 2.2. Any tag already inserted in the buffer should be removed
248 ;; prior to footnotes insertion.
249 (with-temp-buffer
250 (insert "Text[fn:1]
251 Footnotes:
253 Additional text.
255 Footnotes:
257 \[fn:1] Definition")
258 (org-footnote-normalize)
259 (should
260 (equal (buffer-string)
261 "Text[1]
263 Additional text.
265 Footnotes:
267 \[1] Definition"))))
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))
271 (with-temp-buffer
272 (insert "Body[fn::def]
274 Fake signature
276 Signature")
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)))
283 (should
284 (equal (buffer-string)
285 "Body[1]
287 Fake signature
289 \[1] def
292 Signature")))))
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]
300 \[fn:label] C
302 \[fn:1] A
304 \[fn:2] B"
305 (org-footnote-normalize 'sort)
306 (should
307 (equal (buffer-string)
308 "Text[fn:1][fn::inline][fn:2][fn:label]
310 \[fn:1] A
312 \[fn:2] B
314 \[fn:label] C
315 ")))))
318 (provide 'test-org-footnote)
319 ;;; test-org-footnote.el ends here