org.texi: Overwrite with the one generated from "org-manual.org"
[org-mode/org-tableheadings.git] / testing / lisp / test-org-footnote.el
blobf5877a86185749106225833b0416cfcfcac55da2
1 ;;; test-org-footnote.el --- Tests for org-footnote.el
3 ;; Copyright (C) 2012-2015 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <mail at nicolasgoaziou dot fr>
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 `random'.
34 (should
35 (string-match-p
36 "Test\\[fn:\\(.+?\\)\\]\n+\\[fn:\\1\\]"
37 (org-test-with-temp-text "Test<point>"
38 (let ((org-footnote-auto-label 'random)
39 (org-footnote-section nil))
40 (org-footnote-new))
41 (buffer-string))))
42 ;; Error at beginning of line.
43 (should-error
44 (org-test-with-temp-text "<point>Test"
45 (org-footnote-new)))
46 ;; Error at keywords.
47 (should-error
48 (org-test-with-temp-text "#+TIT<point>LE: value"
49 (org-footnote-new)))
50 (should-error
51 (org-test-with-temp-text "#+CAPTION: <point>\nParagraph"
52 (org-footnote-new)))
53 ;; Allow new footnotes in blank lines at the beginning of the
54 ;; document.
55 (should
56 (string-match-p
57 " \\[fn:1\\]"
58 (org-test-with-temp-text " <point>"
59 (let ((org-footnote-auto-label t)) (org-footnote-new))
60 (buffer-string))))
61 ;; In an headline or inlinetask, point must be either on the
62 ;; heading itself or on the blank lines below.
63 (should (org-test-with-temp-text "* H<point>" (org-footnote-new) t))
64 (should
65 (org-test-with-temp-text "* H\n <point>\nParagraph" (org-footnote-new) t))
66 (should-error (org-test-with-temp-text "*<point> H" (org-footnote-new) t))
67 (should-error
68 (org-test-with-temp-text "* H <point>:tag:" (org-footnote-new) t))
69 ;; Allow new footnotes within recursive objects, but not in links.
70 (should
71 (string-match-p
72 " \\*bold\\[fn:1\\]\\*"
73 (org-test-with-temp-text " *bold<point>*"
74 (let ((org-footnote-auto-label t)) (org-footnote-new))
75 (buffer-string))))
76 (should-error
77 (org-test-with-temp-text " [[https://orgmode.org][Org mode<point>]]"
78 (org-footnote-new)))
79 ;; Allow new footnotes in blank lines after an element or white
80 ;; spaces after an object.
81 (should
82 (string-match-p
83 " \\[fn:1\\]"
84 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nA\n#+END_EXAMPLE\n <point>"
85 (let ((org-footnote-auto-label t)) (org-footnote-new))
86 (buffer-string))))
87 (should
88 (string-match-p
89 " \\*bold\\*\\[fn:1\\]"
90 (org-test-with-temp-text " *bold*<point>"
91 (let ((org-footnote-auto-label t)) (org-footnote-new))
92 (buffer-string))))
93 ;; When creating a new footnote, move to its definition.
94 (should
95 (string=
96 "[fn:1] "
97 (org-test-with-temp-text "Text<point>"
98 (let ((org-footnote-auto-label t)
99 (org-footnote-auto-adjust nil))
100 (org-footnote-new))
101 (buffer-substring-no-properties (line-beginning-position) (point)))))
102 ;; Re-order and re-label footnotes properly when
103 ;; `org-footnote-auto-adjust' is non-nil.
104 (should
105 (string=
106 "[fn:1] 1\n\n[fn:2] \n\n[fn:3] 2\n"
107 (org-test-with-temp-text
108 "Text[fn:1]Text<point>Text[fn:2]\n\n[fn:1] 1\n\n[fn:2] 2"
109 (let ((org-footnote-auto-label t)
110 (org-footnote-auto-adjust t)
111 (org-footnote-section nil))
112 (org-footnote-new))
113 (buffer-substring-no-properties
114 (line-beginning-position -1)
115 (line-beginning-position 4)))))
116 ;; Do not alter file local variables when inserting new definition
117 ;; label.
118 (should
119 (equal "Paragraph[fn:1]
121 \[fn:1]
122 # Local Variables:
123 # foo: t
124 # End:"
125 (org-test-with-temp-text
126 "Paragraph<point>\n# Local Variables:\n# foo: t\n# End:"
127 (let ((org-footnote-section nil)) (org-footnote-new))
128 (buffer-string))))
129 (should
130 (equal "Paragraph[fn:1]
132 * Footnotes
134 \[fn:1]
135 # Local Variables:
136 # foo: t
137 # End:"
138 (org-test-with-temp-text
139 "Paragraph<point>\n# Local Variables:\n# foo: t\n# End:"
140 (let ((org-footnote-section "Footnotes")) (org-footnote-new))
141 (buffer-string)))))
143 (ert-deftest test-org-footnote/delete ()
144 "Test `org-footnote-delete' specifications."
145 ;; Regular test.
146 (should
147 (equal "Paragraph"
148 (org-test-with-temp-text "Paragraph<point>[fn:1]\n\n[fn:1] Definition"
149 (org-footnote-delete)
150 (org-trim (buffer-string)))))
151 ;; Remove multiple definitions and references.
152 (should
153 (equal "Paragraph and another"
154 (org-test-with-temp-text
155 "Paragraph<point>[fn:1] and another[fn:1]
157 \[fn:1] def
159 \[fn:1] def"
160 (org-footnote-delete)
161 (org-trim (buffer-string)))))
162 ;; Delete inline footnotes and all references.
163 (should
164 (equal "Para and"
165 (org-test-with-temp-text "Para<point>[fn:label:def] and[fn:label]"
166 (org-footnote-delete)
167 (org-trim (buffer-string)))))
168 ;; Delete anonymous footnotes.
169 (should
170 (equal "Para"
171 (let ((org-footnote-section nil))
172 (org-test-with-temp-text "Para<point>[fn::def]"
173 (org-footnote-delete)
174 (org-trim (buffer-string))))))
175 ;; With an argument, delete footnote with specified label.
176 (should
177 (equal "Paragraph[fn:1] and another\n\n[fn:1] def"
178 (let ((org-footnote-section nil))
179 (org-test-with-temp-text
180 "Paragraph[fn:1] and another[fn:2]\n\n[fn:1] def\n\n[fn:2] def2"
181 (org-footnote-delete "2")
182 (org-trim (buffer-string))))))
183 ;; Error when no argument is specified at point is not at a footnote
184 ;; reference.
185 (should-error
186 (org-test-with-temp-text "Para[fn:1]\n\n[fn:1] Def"
187 (org-footnote-delete)))
188 ;; Correctly delete footnotes with multiple paragraphs.
189 (should
190 (equal "Para\n\n\nOutside footnote."
191 (let ((org-footnote-section nil))
192 (org-test-with-temp-text
193 "Para[fn:1]\n\n[fn:1] para1\n\npara2\n\n\nOutside footnote."
194 (org-footnote-delete "1")
195 (org-trim (buffer-string))))))
196 ;; Remove blank lines above the footnote but preserve those after
197 ;; it.
198 (should
199 (equal "Text\n\n\nOther text."
200 (let ((org-footnote-section nil))
201 (org-test-with-temp-text
202 "Text[fn:1]\n\n[fn:1] Definition.\n\n\nOther text."
203 (org-footnote-delete "1")
204 (buffer-string)))))
205 ;; Preserve file local variables when deleting a footnote.
206 (should
207 (equal
208 "Paragraph\n# Local Variables:\n# foo: t\n# End:"
209 (org-test-with-temp-text
210 "Paragraph[fn:1]\n[fn:1] Def 1\n# Local Variables:\n# foo: t\n# End:"
211 (let ((org-footnote-section nil)) (org-footnote-delete "1"))
212 (buffer-string)))))
214 (ert-deftest test-org-footnote/goto-definition ()
215 "Test `org-footnote-goto-definition' specifications."
216 ;; Error on unknown definitions.
217 (should-error
218 (org-test-with-temp-text "No footnote definition"
219 (org-footnote-goto-definition "1")))
220 ;; Error when trying to reach a definition outside narrowed part of
221 ;; buffer.
222 (should-error
223 (org-test-with-temp-text "Some text<point>\n[fn:1] Definition."
224 (narrow-to-region (point-min) (point))
225 (org-footnote-goto-definition "1")))
226 (should-error
227 (org-test-with-temp-text "[fn:1] Definition.\n<point>Some text"
228 (narrow-to-region (point) (point-max))
229 (org-footnote-goto-definition "1")))
230 ;; Otherwise, move at the beginning of the definition, including
231 ;; anonymous footnotes.
232 (should
233 (equal
234 "Definition."
235 (org-test-with-temp-text "Some text\n[fn:1] Definition."
236 (org-footnote-goto-definition "1")
237 (buffer-substring (point) (point-max)))))
238 (should
239 (equal
240 "definition]"
241 (org-test-with-temp-text "Some text[fn:label:definition]"
242 (org-footnote-goto-definition "label")
243 (buffer-substring (point) (point-max))))))
245 (ert-deftest test-org-footnote/sort ()
246 "Test `org-footnote-sort' specifications."
247 ;; Reorder definitions with a nil `org-footnote-section'. In this
248 ;; case each definition is written at the end of the section
249 ;; containing its first reference.
250 (should
251 (equal
253 Text[fn:1][fn:2]
255 \[fn:1] Def 1
257 \[fn:2] Def 2
259 (org-test-with-temp-text "
260 Text[fn:1][fn:2]
262 \[fn:2] Def 2
264 \[fn:1] Def 1"
265 (let ((org-footnote-section nil)) (org-footnote-sort))
266 (buffer-string))))
267 (should
268 (equal
270 * H1
271 Text[fn:1]
273 \[fn:1] Def 1
274 * H2
275 Text[fn:2]
277 \[fn:2] Def 2
279 (org-test-with-temp-text "
280 * H1
281 Text[fn:1]
282 * H2
283 Text[fn:2]
285 \[fn:1] Def 1
287 \[fn:2] Def 2
289 (let ((org-footnote-section nil)) (org-footnote-sort))
290 (buffer-string))))
291 ;; Reorder definitions with a non-nil `org-footnote-section'.
292 (should
293 (equal
295 Text[fn:1][fn:2]
297 * Footnotes
299 \[fn:1] Def 1
301 \[fn:2] Def 2
303 (org-test-with-temp-text "
304 Text[fn:1][fn:2]
306 \[fn:2] Def 2
308 \[fn:1] Def 1"
309 (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
310 (buffer-string))))
311 ;; When `org-footnote-section' is non-nil, clear previous footnote
312 ;; sections.
313 (should
314 (equal
316 Text[fn:1]
318 * Headline
320 * Other headline
322 * Footnotes
324 \[fn:1] Def 1
326 (org-test-with-temp-text "
327 Text[fn:1]
329 * Footnotes
331 \[fn:1] Def 1
333 * Headline
335 ** Footnotes
337 * Other headline"
338 (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
339 (buffer-string))))
340 ;; Ignore anonymous footnotes.
341 (should
342 (equal
344 Text[fn:1][fn::inline][fn:2]
346 \[fn:1] Def 1
348 \[fn:2] Def 2
350 (org-test-with-temp-text
352 Text[fn:1][fn::inline][fn:2]
354 \[fn:2] Def 2
356 \[fn:1] Def 1"
357 (let ((org-footnote-section nil)) (org-footnote-sort))
358 (buffer-string))))
359 ;; Ignore inline footnotes.
360 (should
361 (equal
363 Text[fn:1][fn:label:inline][fn:2]
365 \[fn:1] Def 1
367 \[fn:2] Def 2
369 (org-test-with-temp-text
371 Text[fn:1][fn:label:inline][fn:2]
373 \[fn:2] Def 2
375 \[fn:1] Def 1"
376 (let ((org-footnote-section nil)) (org-footnote-sort))
377 (buffer-string))))
378 ;; Handle (deeply) nested footnotes.
379 (should
380 (equal
382 Text[fn:1][fn:3]
384 \[fn:1] Def 1[fn:2]
386 \[fn:2] Def 2
388 \[fn:3] Def 3
390 (org-test-with-temp-text "
391 Text[fn:1][fn:3]
393 \[fn:1] Def 1[fn:2]
395 \[fn:3] Def 3
397 \[fn:2] Def 2
399 (let ((org-footnote-section nil)) (org-footnote-sort))
400 (buffer-string))))
401 (should
402 (equal
404 Text[fn:1][fn:4]
406 \[fn:1] Def 1[fn:2]
408 \[fn:2] Def 2[fn:3]
410 \[fn:3] Def 3
412 \[fn:4] Def 4
414 (org-test-with-temp-text "
415 Text[fn:1][fn:4]
417 \[fn:1] Def 1[fn:2]
419 \[fn:3] Def 3
421 \[fn:2] Def 2[fn:3]
423 \[fn:4] Def 4
425 (let ((org-footnote-section nil)) (org-footnote-sort))
426 (buffer-string))))
427 ;; When multiple (nested) references are used, make sure to insert
428 ;; definition only once.
429 (should
430 (equal
432 * Section 1
434 Text[fn:1]
436 \[fn:1] Def 1
438 * Section 2
440 Text[fn:1]"
441 (org-test-with-temp-text
443 * Section 1
445 Text[fn:1]
447 \[fn:1] Def 1
449 * Section 2
451 Text[fn:1]"
452 (let ((org-footnote-section nil)) (org-footnote-sort))
453 (buffer-string))))
454 (should
455 (equal
457 Text[fn:1][fn:4]
459 \[fn:1] Def 1[fn:2][fn:3]
461 \[fn:2] Def 2[fn:3]
463 \[fn:3] Def 3
465 \[fn:4] Def 4
467 (org-test-with-temp-text "
468 Text[fn:1][fn:4]
470 \[fn:1] Def 1[fn:2][fn:3]
472 \[fn:3] Def 3
474 \[fn:2] Def 2[fn:3]
476 \[fn:4] Def 4
478 (let ((org-footnote-section nil)) (org-footnote-sort))
479 (buffer-string))))
480 ;; Insert un-referenced definitions at the end.
481 (should
482 (equal
483 "Text[fn:9]
485 \[fn:9] B
487 \[fn:1] A
489 (org-test-with-temp-text "Text[fn:9]\n\n[fn:1] A\n[fn:9] B"
490 (let ((org-footnote-section nil)) (org-footnote-sort))
491 (buffer-string))))
492 ;; When sorting, preserve file local variables.
493 (should
494 (equal "
495 Paragraph[fn:1][fn:2]
497 \[fn:1] Def 1
499 \[fn:2] Def 2
501 # Local Variables:
502 # foo: t
503 # End:"
504 (org-test-with-temp-text
506 Paragraph[fn:1][fn:2]
508 \[fn:2] Def 2
510 \[fn:1] Def 1
512 # Local Variables:
513 # foo: t
514 # End:"
515 (let ((org-footnote-section nil)) (org-footnote-sort))
516 (buffer-string)))))
518 (ert-deftest test-org-footnote/renumber-fn:N ()
519 "Test `org-footnote-renumber-fn:N' specifications."
520 ;; Renumber (inline) references and definitions.
521 (should
522 (equal
523 "Test[fn:1]"
524 (org-test-with-temp-text "Test[fn:99]"
525 (org-footnote-renumber-fn:N)
526 (buffer-string))))
527 (should
528 (equal
529 "Test[fn:1]\n\n[fn:1] 99"
530 (org-test-with-temp-text "Test[fn:99]\n\n[fn:99] 99"
531 (org-footnote-renumber-fn:N)
532 (buffer-string))))
533 (should
534 (equal
535 "Test[fn:1:99]"
536 (org-test-with-temp-text "Test[fn:99:99]"
537 (org-footnote-renumber-fn:N)
538 (buffer-string))))
539 ;; No-op if there's no numbered footnote.
540 (should
541 (equal
542 "Test[fn:label]\n\n[fn:label] Def"
543 (org-test-with-temp-text "Test[fn:label]\n\n[fn:label] Def"
544 (org-footnote-renumber-fn:N)
545 (buffer-string))))
546 ;; Definitions without a reference get the highest numbers.
547 (should
548 (equal
549 "Test[fn:1]\n[fn:1] 1\n[fn:2] 99"
550 (org-test-with-temp-text "Test[fn:1]\n[fn:1] 1\n[fn:99] 99"
551 (org-footnote-renumber-fn:N)
552 (buffer-string))))
553 ;; Sort labels in sequence. Anonymous footnotes are ignored.
554 (should
555 (equal
556 "Test[fn:1][fn:2:def][fn:3]"
557 (org-test-with-temp-text "Test[fn:4][fn:3:def][fn:2]"
558 (org-footnote-renumber-fn:N)
559 (buffer-string))))
560 (should
561 (equal
562 "Test[fn:1][fn::def][fn:2]"
563 (org-test-with-temp-text "Test[fn:4][fn::def][fn:2]"
564 (org-footnote-renumber-fn:N)
565 (buffer-string)))))
567 (ert-deftest test-org-footnote/normalize ()
568 "Test `org-footnote-normalize' specifications."
569 ;; Normalize regular, inline and anonymous references.
570 (should
571 (equal
572 "Test[fn:1]\n\n[fn:1] def\n"
573 (org-test-with-temp-text "Test[fn:label]\n[fn:label] def"
574 (let ((org-footnote-section nil)) (org-footnote-normalize))
575 (buffer-string))))
576 (should
577 (equal
578 "Test[fn:1]\n\n[fn:1] def\n"
579 (org-test-with-temp-text "Test[fn:label:def]"
580 (let ((org-footnote-section nil)) (org-footnote-normalize))
581 (buffer-string))))
582 (should
583 (equal
584 "Test[fn:1]\n\n[fn:1] def\n"
585 (org-test-with-temp-text "Test[fn::def]"
586 (let ((org-footnote-section nil)) (org-footnote-normalize))
587 (buffer-string))))
588 ;; Normalization includes sorting.
589 (should
590 (equal
591 "Test[fn:1][fn:2]\n\n[fn:1] def2\n\n[fn:2] def\n"
592 (org-test-with-temp-text "Test[fn:2][fn:1]\n\n[fn:2] def2\n[fn:1] def"
593 (let ((org-footnote-section nil)) (org-footnote-normalize))
594 (buffer-string))))
595 (should
596 (equal
597 "Test[fn:1][fn:2]\n\n[fn:1] def\n\n[fn:2] inline\n"
598 (org-test-with-temp-text "Test[fn:2][fn::inline]\n[fn:2] def\n"
599 (let ((org-footnote-section nil)) (org-footnote-normalize))
600 (buffer-string))))
601 (should
602 (equal
603 "Test[fn:1][fn:3]
605 \[fn:1] def[fn:2]
607 \[fn:2] inline
609 \[fn:3] last
611 (org-test-with-temp-text
612 "Test[fn:lab1][fn:lab2]\n[fn:lab1] def[fn::inline]\n[fn:lab2] last"
613 (let ((org-footnote-section nil)) (org-footnote-normalize))
614 (buffer-string))))
615 ;; When normalizing an inline reference, fill paragraph whenever the
616 ;; `org-footnote-fill-after-inline-note-extraction' is non-nil.
617 (should
618 (equal
619 "Test[fn:1] Next\n\n[fn:1] def\n"
620 (org-test-with-temp-text "Test[fn::def]\nNext"
621 (let ((org-footnote-section nil)
622 (org-footnote-fill-after-inline-note-extraction t))
623 (org-footnote-normalize))
624 (buffer-string))))
625 ;; Insert un-referenced definitions at the end.
626 (should
627 (equal
628 "Test[fn:1]\nNext\n\n[fn:1] def\n\n[fn:2] A\n"
629 (org-test-with-temp-text "Test[fn::def]\nNext\n[fn:unref] A"
630 (let ((org-footnote-section nil)) (org-footnote-normalize))
631 (buffer-string))))
632 ;; Preserve file local variables when normalizing.
633 (should
634 (equal "
635 Paragraph[fn:1][fn:2]
637 \[fn:1] Def 1
639 \[fn:2] Def 2
641 # Local Variables:
642 # foo: t
643 # End:"
644 (org-test-with-temp-text
646 Paragraph[fn:foo][fn:bar]
648 \[fn:bar] Def 2
650 \[fn:foo] Def 1
652 # Local Variables:
653 # foo: t
654 # End:"
655 (let ((org-footnote-section nil)) (org-footnote-normalize))
656 (buffer-string)))))
659 (provide 'test-org-footnote)
660 ;;; test-org-footnote.el ends here