1 ;;; test-org-footnote.el --- Tests for org-footnote.el
3 ;; Copyright (C) 2012-2015, 2019 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
/goto-previous-reference
()
246 "Test `org-footnote-goto-previous-reference' specifications."
247 ;; Error on unknown reference.
249 (org-test-with-temp-text "No footnote reference"
250 (org-footnote-goto-previous-reference "1")))
251 ;; Error when trying to reach a reference outside narrowed part of
254 (org-test-with-temp-text "Some text<point>\nReference[fn:1]."
255 (narrow-to-region (point-min) (point))
256 (org-footnote-goto-previous-reference "1")))
257 ;; Otherwise, move to closest reference from point.
259 (org-test-with-temp-text "First reference[fn:1]\nReference[fn:1].<point>"
260 (org-footnote-goto-previous-reference "1")
261 (= (line-end-position) (point-max))))
263 (org-test-with-temp-text "First reference[fn:1]\nReference[fn:1]."
264 (org-footnote-goto-previous-reference "1")
265 (= (line-beginning-position) (point-min)))))
267 (ert-deftest test-org-footnote
/sort
()
268 "Test `org-footnote-sort' specifications."
269 ;; Reorder definitions with a nil `org-footnote-section'. In this
270 ;; case each definition is written at the end of the section
271 ;; containing its first reference.
281 (org-test-with-temp-text "
287 (let ((org-footnote-section nil
)) (org-footnote-sort))
301 (org-test-with-temp-text "
311 (let ((org-footnote-section nil
)) (org-footnote-sort))
313 ;; Reorder definitions with a non-nil `org-footnote-section'.
325 (org-test-with-temp-text "
331 (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
333 ;; When `org-footnote-section' is non-nil, clear previous footnote
348 (org-test-with-temp-text "
360 (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
362 ;; Ignore anonymous footnotes.
366 Text[fn:1][fn::inline][fn:2]
372 (org-test-with-temp-text
374 Text[fn:1][fn::inline][fn:2]
379 (let ((org-footnote-section nil
)) (org-footnote-sort))
381 ;; Ignore inline footnotes.
385 Text[fn:1][fn:label:inline][fn:2]
391 (org-test-with-temp-text
393 Text[fn:1][fn:label:inline][fn:2]
398 (let ((org-footnote-section nil
)) (org-footnote-sort))
400 ;; Handle (deeply) nested footnotes.
412 (org-test-with-temp-text "
421 (let ((org-footnote-section nil
)) (org-footnote-sort))
436 (org-test-with-temp-text "
447 (let ((org-footnote-section nil
)) (org-footnote-sort))
449 ;; When multiple (nested) references are used, make sure to insert
450 ;; definition only once.
463 (org-test-with-temp-text
474 (let ((org-footnote-section nil
)) (org-footnote-sort))
481 \[fn:1] Def 1[fn:2][fn:3]
489 (org-test-with-temp-text "
492 \[fn:1] Def 1[fn:2][fn:3]
500 (let ((org-footnote-section nil
)) (org-footnote-sort))
502 ;; Insert un-referenced definitions at the end.
511 (org-test-with-temp-text "Text[fn:9]\n\n[fn:1] A\n[fn:9] B"
512 (let ((org-footnote-section nil
)) (org-footnote-sort))
514 ;; When sorting, preserve file local variables.
517 Paragraph[fn:1][fn:2]
526 (org-test-with-temp-text
528 Paragraph[fn:1][fn:2]
537 (let ((org-footnote-section nil
)) (org-footnote-sort))
540 (ert-deftest test-org-footnote
/renumber-fn
:N
()
541 "Test `org-footnote-renumber-fn:N' specifications."
542 ;; Renumber (inline) references and definitions.
546 (org-test-with-temp-text "Test[fn:99]"
547 (org-footnote-renumber-fn:N
)
551 "Test[fn:1]\n\n[fn:1] 99"
552 (org-test-with-temp-text "Test[fn:99]\n\n[fn:99] 99"
553 (org-footnote-renumber-fn:N
)
558 (org-test-with-temp-text "Test[fn:99:99]"
559 (org-footnote-renumber-fn:N
)
561 ;; No-op if there's no numbered footnote.
564 "Test[fn:label]\n\n[fn:label] Def"
565 (org-test-with-temp-text "Test[fn:label]\n\n[fn:label] Def"
566 (org-footnote-renumber-fn:N
)
568 ;; Definitions without a reference get the highest numbers.
571 "Test[fn:1]\n[fn:1] 1\n[fn:2] 99"
572 (org-test-with-temp-text "Test[fn:1]\n[fn:1] 1\n[fn:99] 99"
573 (org-footnote-renumber-fn:N
)
575 ;; Sort labels in sequence. Anonymous footnotes are ignored.
578 "Test[fn:1][fn:2:def][fn:3]"
579 (org-test-with-temp-text "Test[fn:4][fn:3:def][fn:2]"
580 (org-footnote-renumber-fn:N
)
584 "Test[fn:1][fn::def][fn:2]"
585 (org-test-with-temp-text "Test[fn:4][fn::def][fn:2]"
586 (org-footnote-renumber-fn:N
)
589 (ert-deftest test-org-footnote
/normalize
()
590 "Test `org-footnote-normalize' specifications."
591 ;; Normalize regular, inline and anonymous references.
594 "Test[fn:1]\n\n[fn:1] def\n"
595 (org-test-with-temp-text "Test[fn:label]\n[fn:label] def"
596 (let ((org-footnote-section nil
)) (org-footnote-normalize))
600 "Test[fn:1]\n\n[fn:1] def\n"
601 (org-test-with-temp-text "Test[fn:label:def]"
602 (let ((org-footnote-section nil
)) (org-footnote-normalize))
606 "Test[fn:1]\n\n[fn:1] def\n"
607 (org-test-with-temp-text "Test[fn::def]"
608 (let ((org-footnote-section nil
)) (org-footnote-normalize))
610 ;; Normalization includes sorting.
613 "Test[fn:1][fn:2]\n\n[fn:1] def2\n\n[fn:2] def\n"
614 (org-test-with-temp-text "Test[fn:2][fn:1]\n\n[fn:2] def2\n[fn:1] def"
615 (let ((org-footnote-section nil
)) (org-footnote-normalize))
619 "Test[fn:1][fn:2]\n\n[fn:1] def\n\n[fn:2] inline\n"
620 (org-test-with-temp-text "Test[fn:2][fn::inline]\n[fn:2] def\n"
621 (let ((org-footnote-section nil
)) (org-footnote-normalize))
633 (org-test-with-temp-text
634 "Test[fn:lab1][fn:lab2]\n[fn:lab1] def[fn::inline]\n[fn:lab2] last"
635 (let ((org-footnote-section nil
)) (org-footnote-normalize))
637 ;; When normalizing an inline reference, fill paragraph whenever the
638 ;; `org-footnote-fill-after-inline-note-extraction' is non-nil.
641 "Test[fn:1] Next\n\n[fn:1] def\n"
642 (org-test-with-temp-text "Test[fn::def]\nNext"
643 (let ((org-footnote-section nil
)
644 (org-footnote-fill-after-inline-note-extraction t
))
645 (org-footnote-normalize))
647 ;; Insert un-referenced definitions at the end.
650 "Test[fn:1]\nNext\n\n[fn:1] def\n\n[fn:2] A\n"
651 (org-test-with-temp-text "Test[fn::def]\nNext\n[fn:unref] A"
652 (let ((org-footnote-section nil
)) (org-footnote-normalize))
654 ;; Preserve file local variables when normalizing.
657 Paragraph[fn:1][fn:2]
666 (org-test-with-temp-text
668 Paragraph[fn:foo][fn:bar]
677 (let ((org-footnote-section nil
)) (org-footnote-normalize))
681 (provide 'test-org-footnote
)
682 ;;; test-org-footnote.el ends here