org-footnote: Allow new footnotes in headlines
[org-mode/org-tableheadings.git] / testing / lisp / test-org-footnote.el
blob1a10faa2195e85adfff1185bd3fa98f390a2c3be
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/>.
20 ;;; Code:
22 (ert-deftest test-org-footnote/new ()
23 "Test `org-footnote-new' specifications."
24 ;; `org-footnote-auto-label' is t.
25 (should
26 (string-match-p
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))
31 (org-footnote-new))
32 (buffer-string))))
33 ;; `org-footnote-auto-label' is `plain'.
34 (should
35 (string-match-p
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))
40 (org-footnote-new))
41 (buffer-string))))
42 ;; `org-footnote-auto-label' is `random'.
43 (should
44 (string-match-p
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))
49 (org-footnote-new))
50 (buffer-string))))
51 ;; Error at beginning of line.
52 (should-error
53 (org-test-with-temp-text "<point>Test"
54 (org-footnote-new)))
55 ;; Error at keywords.
56 (should-error
57 (org-test-with-temp-text "#+TIT<point>LE: value"
58 (org-footnote-new)))
59 (should-error
60 (org-test-with-temp-text "#+CAPTION: <point>\nParagraph"
61 (org-footnote-new)))
62 ;; Allow new footnotes in blank lines at the beginning of the
63 ;; document.
64 (should
65 (string-match-p
66 " \\[fn:1\\]"
67 (org-test-with-temp-text " <point>"
68 (let ((org-footnote-auto-label t)) (org-footnote-new))
69 (buffer-string))))
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))
73 (should
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))
76 (should-error
77 (org-test-with-temp-text "* H <point>:tag:" (org-footnote-new) t))
78 ;; Allow new footnotes within recursive objects, but not in links.
79 (should
80 (string-match-p
81 " \\*bold\\[fn:1\\]\\*"
82 (org-test-with-temp-text " *bold<point>*"
83 (let ((org-footnote-auto-label t)) (org-footnote-new))
84 (buffer-string))))
85 (should-error
86 (org-test-with-temp-text " [[http://orgmode.org][Org mode<point>]]"
87 (org-footnote-new)))
88 ;; Allow new footnotes in blank lines after an element or white
89 ;; spaces after an object.
90 (should
91 (string-match-p
92 " \\[fn:1\\]"
93 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nA\n#+END_EXAMPLE\n <point>"
94 (let ((org-footnote-auto-label t)) (org-footnote-new))
95 (buffer-string))))
96 (should
97 (string-match-p
98 " \\*bold\\*\\[fn:1\\]"
99 (org-test-with-temp-text " *bold*<point>"
100 (let ((org-footnote-auto-label t)) (org-footnote-new))
101 (buffer-string)))))
103 (ert-deftest test-org-footnote/delete ()
104 "Test `org-footnote-delete' specifications."
105 ;; Regular test.
106 (should
107 (equal "Paragraph"
108 (org-test-with-temp-text "Paragraph[1]\n\n[1] Definition"
109 (search-forward "[")
110 (org-footnote-delete)
111 (org-trim (buffer-string)))))
112 ;; Remove multiple definitions and references.
113 (should
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"
117 (search-forward "[")
118 (org-footnote-delete)
119 (org-trim (buffer-string)))))
120 ;; Delete inline footnotes and all references.
121 (should
122 (equal "Para and"
123 (org-test-with-temp-text "Para[fn:label:def] and[fn:label]"
124 (search-forward "[")
125 (org-footnote-delete)
126 (org-trim (buffer-string)))))
127 ;; Delete anonymous footnotes.
128 (should
129 (equal "Para"
130 (org-test-with-temp-text "Para[fn::def]"
131 (search-forward "[")
132 (org-footnote-delete)
133 (org-trim (buffer-string)))))
134 ;; With an argument, delete footnote with specified label.
135 (should
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
143 ;; reference.
144 (should-error
145 (org-test-with-temp-text "Para[1]\n\n[1] Def"
146 (org-footnote-delete)))
147 ;; Correctly delete footnotes with multiple paragraphs.
148 (should
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/normalize-in-org ()
156 "Test specifications for `org-footnote-normalize' in an Org buffer."
157 ;; 1. With a non-nil `org-footnote-section'.
158 (let ((org-footnote-section "Footnotes")
159 (org-blank-before-new-entry '((heading . auto))))
160 ;; 1.1. Normalize each type of footnote: standard, labelled,
161 ;; numbered, inline, anonymous.
162 (org-test-with-temp-text
163 "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
165 * Footnotes
167 \[fn:1] Standard
169 \[fn:label] Labelled
171 \[1] Numbered"
172 (org-footnote-normalize)
173 (should
174 (equal (buffer-string)
175 "Paragraph[1][2][3][4][5]
177 * Footnotes
179 \[1] Standard
181 \[2] Labelled
183 \[3] Numbered
185 \[4] Inline
187 \[5] Anonymous
190 ")))
191 ;; 1.2. When no footnote section is present, create it. Follow
192 ;; `org-blank-before-new-entry' specifications when doing so.
193 (org-test-with-temp-text "Paragraph[fn:1]\n\n[fn:1] Definition"
194 (org-footnote-normalize)
195 (should (equal (buffer-string)
196 "Paragraph[1]\n\n* Footnotes\n\n[1] Definition")))
197 (org-test-with-temp-text "Paragraph[fn:1]\n* Head1\n[fn:1] Definition"
198 (let ((org-blank-before-new-entry '((heading))))
199 (org-footnote-normalize))
200 (should (equal (buffer-string)
201 "Paragraph[1]\n* Head1\n* Footnotes\n\n[1] Definition")))
202 ;; 1.3. When the footnote section is misplaced, move it at the end
203 ;; of the buffer.
204 (org-test-with-temp-text "* Head1
205 Body[fn:1]
206 * Footnotes
207 \[fn:1] Definition 1
208 * Head2"
209 (org-footnote-normalize)
210 (should
211 (equal (buffer-string)
212 "* Head1
213 Body[1]
214 * Head2
216 * Footnotes
218 \[1] Definition 1"))))
219 ;; 2. With a nil `org-footnote-section'.
220 (let ((org-footnote-section nil))
221 ;; 2.1. Normalize each type of footnote: standard, labelled,
222 ;; numbered, inline, anonymous.
223 (org-test-with-temp-text
224 "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
226 \[fn:1] Standard
228 \[fn:label] Labelled
230 \[1] Numbered"
231 (org-footnote-normalize)
232 (should
233 (equal (buffer-string)
234 "Paragraph[1][2][3][4][5]
236 \[1] Standard
238 \[2] Labelled
240 \[3] Numbered
242 \[4] Inline
244 \[5] Anonymous
245 ")))
246 ;; 2.2. Put each footnote definition at the end of the section
247 ;; containing its first reference.
248 (org-test-with-temp-text
249 "* Head 1
250 Text[fn:1:Def1]
251 * Head 2
252 Text[fn:1]
253 * Head 3
254 Text[fn:2:Def2]"
255 (org-footnote-normalize)
256 (should
257 (equal (buffer-string)
258 "* Head 1
259 Text[1]
261 \[1] Def1
262 * Head 2
263 Text[1]
264 * Head 3
265 Text[2]
267 \[2] Def2
268 ")))))
270 (ert-deftest test-org-footnote/normalize-outside-org ()
271 "Test `org-footnote-normalize' specifications for buffers not in Org mode."
272 ;; 1. In a non-Org buffer, footnotes definitions are always put at
273 ;; its end.
274 (should
275 (equal
276 "Paragraph[1][2][3][4][5]
279 Some additional text.
281 \[1] Standard
283 \[2] Labelled
285 \[3] Numbered
287 \[4] Inline
289 \[5] Anonymous"
290 (let ((org-footnote-tag-for-non-org-mode-files nil))
291 (with-temp-buffer
292 (insert "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
294 \[fn:1] Standard
296 \[fn:label] Labelled
298 \[1] Numbered
301 Some additional text.")
302 (org-footnote-normalize)
303 (buffer-string)))))
304 ;; 2. With a special tag.
305 (let ((org-footnote-tag-for-non-org-mode-files "Footnotes:"))
306 ;; 2.1. The tag must be inserted before the footnotes, separated
307 ;; from the rest of the text with a blank line.
308 (with-temp-buffer
309 (insert "Paragraph[fn:1][fn::Anonymous]
311 \[fn:1] Standard
314 Some additional text.")
315 (org-footnote-normalize)
316 (should
317 (equal (buffer-string)
318 "Paragraph[1][2]
321 Some additional text.
323 Footnotes:
325 \[1] Standard
327 \[2] Anonymous")))
328 ;; 2.2. Any tag already inserted in the buffer should be removed
329 ;; prior to footnotes insertion.
330 (with-temp-buffer
331 (insert "Text[fn:1]
332 Footnotes:
334 Additional text.
336 Footnotes:
338 \[fn:1] Definition")
339 (org-footnote-normalize)
340 (should
341 (equal (buffer-string)
342 "Text[1]
344 Additional text.
346 Footnotes:
348 \[1] Definition"))))
349 ;; 3. As an exception, in `message-mode' buffer, if a signature is
350 ;; present, insert footnotes before it.n
351 (let ((org-footnote-tag-for-non-org-mode-files nil))
352 (with-temp-buffer
353 (insert "Body[fn::def]
355 Fake signature
357 Signature")
358 ;; Mimic `message-mode'.
359 (let ((major-mode 'message-mode)
360 (message-cite-prefix-regexp "\\([ ]*[_.[:word:]]+>+\\|[ ]*[]>|]\\)+")
361 (message-signature-separator "^-- $"))
362 (flet ((message-point-in-header-p nil nil))
363 (org-footnote-normalize)))
364 (should
365 (equal (buffer-string)
366 "Body[1]
368 Fake signature
370 \[1] def
373 Signature")))))
375 (ert-deftest test-org-footnote/sort ()
376 "Test footnotes definitions sorting."
377 (let ((org-footnote-section nil))
378 (org-test-with-temp-text
379 "Text[fn:1][fn::inline][fn:2][fn:label]
381 \[fn:label] C
383 \[fn:1] A
385 \[fn:2] B"
386 (org-footnote-normalize 'sort)
387 (should
388 (equal (buffer-string)
389 "Text[fn:1][fn::inline][fn:2][fn:label]
391 \[fn:1] A
393 \[fn:2] B
395 \[fn:label] C
396 ")))))
399 (provide 'test-org-footnote)
400 ;;; test-org-footnote.el ends here