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/>.
22 (ert-deftest test-org-footnote
/new
()
23 "Test `org-footnote-new' specifications."
24 ;; `org-footnote-auto-label' is t.
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
))
33 ;; `org-footnote-auto-label' is `random'.
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
))
42 ;; Error at beginning of line.
44 (org-test-with-temp-text "<point>Test"
48 (org-test-with-temp-text "#+TIT<point>LE: value"
51 (org-test-with-temp-text "#+CAPTION: <point>\nParagraph"
53 ;; Allow new footnotes in blank lines at the beginning of the
58 (org-test-with-temp-text " <point>"
59 (let ((org-footnote-auto-label t
)) (org-footnote-new))
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
))
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
))
68 (org-test-with-temp-text "* H <point>:tag:" (org-footnote-new) t
))
69 ;; Allow new footnotes within recursive objects, but not in links.
72 " \\*bold\\[fn:1\\]\\*"
73 (org-test-with-temp-text " *bold<point>*"
74 (let ((org-footnote-auto-label t
)) (org-footnote-new))
77 (org-test-with-temp-text " [[https://orgmode.org][Org mode<point>]]"
79 ;; Allow new footnotes in blank lines after an element or white
80 ;; spaces after an object.
84 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nA\n#+END_EXAMPLE\n <point>"
85 (let ((org-footnote-auto-label t
)) (org-footnote-new))
89 " \\*bold\\*\\[fn:1\\]"
90 (org-test-with-temp-text " *bold*<point>"
91 (let ((org-footnote-auto-label t
)) (org-footnote-new))
93 ;; When creating a new footnote, move to its definition.
97 (org-test-with-temp-text "Text<point>"
98 (let ((org-footnote-auto-label t
)
99 (org-footnote-auto-adjust nil
))
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.
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
))
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
119 (equal "Paragraph[fn:1]
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))
130 (equal "Paragraph[fn:1]
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))
143 (ert-deftest test-org-footnote
/delete
()
144 "Test `org-footnote-delete' specifications."
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.
153 (equal "Paragraph and another"
154 (org-test-with-temp-text
155 "Paragraph<point>[fn:1] and another[fn:1]
160 (org-footnote-delete)
161 (org-trim (buffer-string)))))
162 ;; Delete inline footnotes and all references.
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.
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.
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
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.
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
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")
205 ;; Preserve file local variables when deleting a footnote.
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"))
214 (ert-deftest test-org-footnote
/goto-definition
()
215 "Test `org-footnote-goto-definition' specifications."
216 ;; Error on unknown definitions.
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
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")))
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.
235 (org-test-with-temp-text "Some text\n[fn:1] Definition."
236 (org-footnote-goto-definition "1")
237 (buffer-substring (point) (point-max)))))
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.
259 (org-test-with-temp-text "
265 (let ((org-footnote-section nil
)) (org-footnote-sort))
279 (org-test-with-temp-text "
289 (let ((org-footnote-section nil
)) (org-footnote-sort))
291 ;; Reorder definitions with a non-nil `org-footnote-section'.
303 (org-test-with-temp-text "
309 (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
311 ;; When `org-footnote-section' is non-nil, clear previous footnote
326 (org-test-with-temp-text "
338 (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
340 ;; Ignore anonymous footnotes.
344 Text[fn:1][fn::inline][fn:2]
350 (org-test-with-temp-text
352 Text[fn:1][fn::inline][fn:2]
357 (let ((org-footnote-section nil
)) (org-footnote-sort))
359 ;; Ignore inline footnotes.
363 Text[fn:1][fn:label:inline][fn:2]
369 (org-test-with-temp-text
371 Text[fn:1][fn:label:inline][fn:2]
376 (let ((org-footnote-section nil
)) (org-footnote-sort))
378 ;; Handle (deeply) nested footnotes.
390 (org-test-with-temp-text "
399 (let ((org-footnote-section nil
)) (org-footnote-sort))
414 (org-test-with-temp-text "
425 (let ((org-footnote-section nil
)) (org-footnote-sort))
427 ;; When multiple (nested) references are used, make sure to insert
428 ;; definition only once.
441 (org-test-with-temp-text
452 (let ((org-footnote-section nil
)) (org-footnote-sort))
459 \[fn:1] Def 1[fn:2][fn:3]
467 (org-test-with-temp-text "
470 \[fn:1] Def 1[fn:2][fn:3]
478 (let ((org-footnote-section nil
)) (org-footnote-sort))
480 ;; Insert un-referenced definitions at the end.
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))
492 ;; When sorting, preserve file local variables.
495 Paragraph[fn:1][fn:2]
504 (org-test-with-temp-text
506 Paragraph[fn:1][fn:2]
515 (let ((org-footnote-section nil
)) (org-footnote-sort))
518 (ert-deftest test-org-footnote
/renumber-fn
:N
()
519 "Test `org-footnote-renumber-fn:N' specifications."
520 ;; Renumber (inline) references and definitions.
524 (org-test-with-temp-text "Test[fn:99]"
525 (org-footnote-renumber-fn:N
)
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
)
536 (org-test-with-temp-text "Test[fn:99:99]"
537 (org-footnote-renumber-fn:N
)
539 ;; No-op if there's no numbered footnote.
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
)
546 ;; Definitions without a reference get the highest numbers.
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
)
553 ;; Sort labels in sequence. Anonymous footnotes are ignored.
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
)
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
)
567 (ert-deftest test-org-footnote
/normalize
()
568 "Test `org-footnote-normalize' specifications."
569 ;; Normalize regular, inline and anonymous references.
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))
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))
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))
588 ;; Normalization includes sorting.
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))
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))
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))
615 ;; When normalizing an inline reference, fill paragraph whenever the
616 ;; `org-footnote-fill-after-inline-note-extraction' is non-nil.
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))
625 ;; Insert un-referenced definitions at the end.
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))
632 ;; Preserve file local variables when normalizing.
635 Paragraph[fn:1][fn:2]
644 (org-test-with-temp-text
646 Paragraph[fn:foo][fn:bar]
655 (let ((org-footnote-section nil
)) (org-footnote-normalize))
659 (provide 'test-org-footnote
)
660 ;;; test-org-footnote.el ends here