org-bibtex: Fix type error in non-file buffers
[org-mode/org-tableheadings.git] / testing / lisp / test-org-footnote.el
blob4205d4375aeafb290f086f156f5c41efbc35fd62
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))))
102 ;; When creating a new footnote, move to its definition.
103 (should
104 (string=
105 "[fn:1] "
106 (org-test-with-temp-text "Text<point>"
107 (let ((org-footnote-auto-label t)
108 (org-footnote-auto-adjust nil))
109 (org-footnote-new))
110 (buffer-substring-no-properties (line-beginning-position) (point)))))
111 ;; Re-order and re-label footnotes properly when
112 ;; `org-footnote-auto-adjust' is non-nil.
113 (should
114 (string=
115 "[fn:1] 1\n\n[fn:2] \n\n[fn:3] 2\n"
116 (org-test-with-temp-text
117 "Text[fn:1]Text<point>Text[fn:2]\n\n[fn:1] 1\n\n[fn:2] 2"
118 (let ((org-footnote-auto-label t)
119 (org-footnote-auto-adjust t)
120 (org-footnote-section nil))
121 (org-footnote-new))
122 (buffer-substring-no-properties
123 (line-beginning-position -1)
124 (line-beginning-position 4))))))
126 (ert-deftest test-org-footnote/delete ()
127 "Test `org-footnote-delete' specifications."
128 ;; Regular test.
129 (should
130 (equal "Paragraph"
131 (org-test-with-temp-text "Paragraph[1]\n\n[1] Definition"
132 (search-forward "[")
133 (org-footnote-delete)
134 (org-trim (buffer-string)))))
135 ;; Remove multiple definitions and references.
136 (should
137 (equal "Paragraph and another"
138 (org-test-with-temp-text
139 "Paragraph[1] and another[1]\n\n[1] def\n\n[1] def"
140 (search-forward "[")
141 (org-footnote-delete)
142 (org-trim (buffer-string)))))
143 ;; Delete inline footnotes and all references.
144 (should
145 (equal "Para and"
146 (org-test-with-temp-text "Para[fn:label:def] and[fn:label]"
147 (search-forward "[")
148 (org-footnote-delete)
149 (org-trim (buffer-string)))))
150 ;; Delete anonymous footnotes.
151 (should
152 (equal "Para"
153 (org-test-with-temp-text "Para[fn::def]"
154 (search-forward "[")
155 (org-footnote-delete)
156 (org-trim (buffer-string)))))
157 ;; With an argument, delete footnote with specified label.
158 (should
159 (equal "Paragraph[1] and another\n\n[1] def"
160 (let ((org-footnote-section nil))
161 (org-test-with-temp-text
162 "Paragraph[1] and another[2]\n\n[1] def\n\n[2] def2"
163 (org-footnote-delete "2")
164 (org-trim (buffer-string))))))
165 ;; Error when no argument is specified at point is not at a footnote
166 ;; reference.
167 (should-error
168 (org-test-with-temp-text "Para[1]\n\n[1] Def"
169 (org-footnote-delete)))
170 ;; Correctly delete footnotes with multiple paragraphs.
171 (should
172 (equal "Para\n\n\nOutside footnote."
173 (org-test-with-temp-text
174 "Para[1]\n\n[1] para1\n\npara2\n\n\nOutside footnote."
175 (org-footnote-delete "1")
176 (org-trim (buffer-string))))))
178 (ert-deftest test-org-footnote/goto-definition ()
179 "Test `org-footnote-goto-definition' specifications."
180 ;; Error on unknown definitions.
181 (should-error
182 (org-test-with-temp-text "No footnote definition"
183 (org-footnote-goto-definition "fn:1")))
184 ;; Error when trying to reach a definition outside narrowed part of
185 ;; buffer.
186 (should-error
187 (org-test-with-temp-text "Some text<point>\n[fn:1] Definition."
188 (narrow-to-region (point-min) (point))
189 (org-footnote-goto-definition "fn:1")))
190 (should-error
191 (org-test-with-temp-text "[fn:1] Definition.\n<point>Some text"
192 (narrow-to-region (point) (point-max))
193 (org-footnote-goto-definition "fn:1")))
194 ;; Otherwise, move at the beginning of the definition, including
195 ;; anonymous footnotes.
196 (should
197 (equal
198 "Definition."
199 (org-test-with-temp-text "Some text\n[fn:1] Definition."
200 (org-footnote-goto-definition "fn:1")
201 (buffer-substring (point) (point-max)))))
202 (should
203 (equal
204 "definition]"
205 (org-test-with-temp-text "Some text[fn:label:definition]"
206 (org-footnote-goto-definition "fn:label")
207 (buffer-substring (point) (point-max))))))
209 (ert-deftest test-org-footnote/normalize-in-org ()
210 "Test specifications for `org-footnote-normalize' in an Org buffer."
211 ;; With a non-nil `org-footnote-section', normalize each type of
212 ;; footnote: standard, labelled, numbered, inline, anonymous.
213 (should
214 (equal "Paragraph[1][2][3][4][5]
216 * Footnotes
218 \[1] Standard
220 \[2] Labelled
222 \[3] Numbered
224 \[4] Inline
226 \[5] Anonymous
230 (let ((org-footnote-section "Footnotes")
231 (org-blank-before-new-entry '((heading . auto))))
232 (org-test-with-temp-text
233 "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
235 * Footnotes
237 \[fn:1] Standard
239 \[fn:label] Labelled
241 \[1] Numbered"
242 (org-footnote-normalize)
243 (buffer-string)))))
244 ;; When no footnote section is present, create it. Follow
245 ;; `org-blank-before-new-entry' specifications when doing so.
246 (should
247 (equal "Paragraph[1]\n\n* Footnotes\n\n[1] Definition"
248 (let ((org-footnote-section "Footnotes")
249 (org-blank-before-new-entry '((heading . auto))))
250 (org-test-with-temp-text "Paragraph[fn:1]\n\n[fn:1] Definition"
251 (org-footnote-normalize)
252 (buffer-string)))))
253 (should
254 (equal
255 "Paragraph[1]\n* Head1\n* Footnotes\n\n[1] Definition"
256 (let ((org-footnote-section "Footnotes")
257 (org-blank-before-new-entry '((heading))))
258 (org-test-with-temp-text "Paragraph[fn:1]\n* Head1\n[fn:1] Definition"
259 (org-footnote-normalize)
260 (buffer-string)))))
261 ;; When the footnote section is misplaced, move it at the end of
262 ;; the buffer.
263 (should
264 (equal
265 "* Head1
266 Body[1]
267 * Head2
269 * Footnotes
271 \[1] Definition 1"
272 (let ((org-footnote-section "Footnotes")
273 (org-blank-before-new-entry '((heading . auto))))
274 (org-test-with-temp-text "* Head1
275 Body[fn:1]
276 * Footnotes
277 \[fn:1] Definition 1
278 * Head2"
279 (org-footnote-normalize)
280 (buffer-string)))))
281 ;; With a nil `org-footnote-section', normalize each type of
282 ;; footnote: standard, labelled, numbered, inline, anonymous.
283 (should
284 (equal "Paragraph[1][2][3][4][5]
286 \[1] Standard
288 \[2] Labelled
290 \[3] Numbered
292 \[4] Inline
294 \[5] Anonymous
296 (let ((org-footnote-section nil))
297 (org-test-with-temp-text
298 "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
300 \[fn:1] Standard
302 \[fn:label] Labelled
304 \[1] Numbered"
305 (org-footnote-normalize)
306 (buffer-string)))))
307 ;; Also put each footnote definition at the end of the section
308 ;; containing its first reference.
309 (should
310 (equal "* Head 1
311 Text[1]
313 \[1] Def1
314 * Head 2
315 Text[1]
316 * Head 3
317 Text[2]
319 \[2] Def2
321 (let ((org-footnote-section nil))
322 (org-test-with-temp-text
323 "* Head 1
324 Text[fn:1:Def1]
325 * Head 2
326 Text[fn:1]
327 * Head 3
328 Text[fn:2:Def2]"
329 (org-footnote-normalize)
330 (buffer-string))))))
332 (ert-deftest test-org-footnote/normalize-outside-org ()
333 "Test `org-footnote-normalize' specifications for buffers not in Org mode."
334 ;; 1. In a non-Org buffer, footnotes definitions are always put at
335 ;; its end.
336 (should
337 (equal
338 "Paragraph[1][2][3][4][5]
341 Some additional text.
343 \[1] Standard
345 \[2] Labelled
347 \[3] Numbered
349 \[4] Inline
351 \[5] Anonymous"
352 (let ((org-footnote-tag-for-non-org-mode-files nil))
353 (with-temp-buffer
354 (insert "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
356 \[fn:1] Standard
358 \[fn:label] Labelled
360 \[1] Numbered
363 Some additional text.")
364 (org-footnote-normalize)
365 (buffer-string)))))
366 ;; 2. With a special tag.
367 (let ((org-footnote-tag-for-non-org-mode-files "Footnotes:"))
368 ;; 2.1. The tag must be inserted before the footnotes, separated
369 ;; from the rest of the text with a blank line.
370 (with-temp-buffer
371 (insert "Paragraph[fn:1][fn::Anonymous]
373 \[fn:1] Standard
376 Some additional text.")
377 (org-footnote-normalize)
378 (should
379 (equal (buffer-string)
380 "Paragraph[1][2]
383 Some additional text.
385 Footnotes:
387 \[1] Standard
389 \[2] Anonymous")))
390 ;; 2.2. Any tag already inserted in the buffer should be removed
391 ;; prior to footnotes insertion.
392 (with-temp-buffer
393 (insert "Text[fn:1]
394 Footnotes:
396 Additional text.
398 Footnotes:
400 \[fn:1] Definition")
401 (org-footnote-normalize)
402 (should
403 (equal (buffer-string)
404 "Text[1]
406 Additional text.
408 Footnotes:
410 \[1] Definition"))))
411 ;; 3. As an exception, in `message-mode' buffer, if a signature is
412 ;; present, insert footnotes before it.n
413 (let ((org-footnote-tag-for-non-org-mode-files nil))
414 (with-temp-buffer
415 (insert "Body[fn::def]
417 Fake signature
419 Signature")
420 ;; Mimic `message-mode'.
421 (let ((major-mode 'message-mode)
422 (message-cite-prefix-regexp "\\([ ]*[_.[:word:]]+>+\\|[ ]*[]>|]\\)+")
423 (message-signature-separator "^-- $"))
424 (flet ((message-point-in-header-p nil nil))
425 (org-footnote-normalize)))
426 (should
427 (equal (buffer-string)
428 "Body[1]
430 Fake signature
432 \[1] def
435 Signature")))))
437 (ert-deftest test-org-footnote/sort ()
438 "Test footnotes definitions sorting."
439 (let ((org-footnote-section nil))
440 (org-test-with-temp-text
441 "Text[fn:1][fn::inline][fn:2][fn:label]
443 \[fn:label] C
445 \[fn:1] A
447 \[fn:2] B"
448 (org-footnote-normalize 'sort)
449 (should
450 (equal (buffer-string)
451 "Text[fn:1][fn::inline][fn:2][fn:label]
453 \[fn:1] A
455 \[fn:2] B
457 \[fn:label] C
458 ")))))
461 (provide 'test-org-footnote)
462 ;;; test-org-footnote.el ends here