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 " [[http://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))))))
117 (ert-deftest test-org-footnote
/delete
()
118 "Test `org-footnote-delete' specifications."
122 (org-test-with-temp-text "Paragraph<point>[fn:1]\n\n[fn:1] Definition"
123 (org-footnote-delete)
124 (org-trim (buffer-string)))))
125 ;; Remove multiple definitions and references.
127 (equal "Paragraph and another"
128 (org-test-with-temp-text
129 "Paragraph<point>[fn:1] and another[fn:1]
134 (org-footnote-delete)
135 (org-trim (buffer-string)))))
136 ;; Delete inline footnotes and all references.
139 (org-test-with-temp-text "Para<point>[fn:label:def] and[fn:label]"
140 (org-footnote-delete)
141 (org-trim (buffer-string)))))
142 ;; Delete anonymous footnotes.
145 (let ((org-footnote-section nil
))
146 (org-test-with-temp-text "Para<point>[fn::def]"
147 (org-footnote-delete)
148 (org-trim (buffer-string))))))
149 ;; With an argument, delete footnote with specified label.
151 (equal "Paragraph[fn:1] and another\n\n[fn:1] def"
152 (let ((org-footnote-section nil
))
153 (org-test-with-temp-text
154 "Paragraph[fn:1] and another[fn:2]\n\n[fn:1] def\n\n[fn:2] def2"
155 (org-footnote-delete "2")
156 (org-trim (buffer-string))))))
157 ;; Error when no argument is specified at point is not at a footnote
160 (org-test-with-temp-text "Para[fn:1]\n\n[fn:1] Def"
161 (org-footnote-delete)))
162 ;; Correctly delete footnotes with multiple paragraphs.
164 (equal "Para\n\n\nOutside footnote."
165 (let ((org-footnote-section nil
))
166 (org-test-with-temp-text
167 "Para[fn:1]\n\n[fn:1] para1\n\npara2\n\n\nOutside footnote."
168 (org-footnote-delete "1")
169 (org-trim (buffer-string)))))))
171 (ert-deftest test-org-footnote
/goto-definition
()
172 "Test `org-footnote-goto-definition' specifications."
173 ;; Error on unknown definitions.
175 (org-test-with-temp-text "No footnote definition"
176 (org-footnote-goto-definition "1")))
177 ;; Error when trying to reach a definition outside narrowed part of
180 (org-test-with-temp-text "Some text<point>\n[fn:1] Definition."
181 (narrow-to-region (point-min) (point))
182 (org-footnote-goto-definition "1")))
184 (org-test-with-temp-text "[fn:1] Definition.\n<point>Some text"
185 (narrow-to-region (point) (point-max))
186 (org-footnote-goto-definition "1")))
187 ;; Otherwise, move at the beginning of the definition, including
188 ;; anonymous footnotes.
192 (org-test-with-temp-text "Some text\n[fn:1] Definition."
193 (org-footnote-goto-definition "1")
194 (buffer-substring (point) (point-max)))))
198 (org-test-with-temp-text "Some text[fn:label:definition]"
199 (org-footnote-goto-definition "label")
200 (buffer-substring (point) (point-max))))))
202 (ert-deftest test-org-footnote
/sort
()
203 "Test `org-footnote-sort' specifications."
204 ;; Reorder definitions with a nil `org-footnote-section'. In this
205 ;; case each definition is written at the end of the section
206 ;; containing its first reference.
216 (org-test-with-temp-text "
222 (let ((org-footnote-section nil
)) (org-footnote-sort))
236 (org-test-with-temp-text "
246 (let ((org-footnote-section nil
)) (org-footnote-sort))
248 ;; Reorder definitions with a non-nil `org-footnote-section'.
260 (org-test-with-temp-text "
266 (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
268 ;; When `org-footnote-section' is non-nil, clear previous footnote
283 (org-test-with-temp-text "
295 (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
297 ;; Ignore anonymous footnotes.
301 Text[fn:1][fn::inline][fn:2]
307 (org-test-with-temp-text
309 Text[fn:1][fn::inline][fn:2]
314 (let ((org-footnote-section nil
)) (org-footnote-sort))
316 ;; Ignore inline footnotes.
320 Text[fn:1][fn:label:inline][fn:2]
326 (org-test-with-temp-text
328 Text[fn:1][fn:label:inline][fn:2]
333 (let ((org-footnote-section nil
)) (org-footnote-sort))
335 ;; Handle (deeply) nested footnotes.
347 (org-test-with-temp-text "
356 (let ((org-footnote-section nil
)) (org-footnote-sort))
371 (org-test-with-temp-text "
382 (let ((org-footnote-section nil
)) (org-footnote-sort))
384 ;; When multiple (nested) references are used, make sure to insert
385 ;; definition only once.
398 (org-test-with-temp-text
409 (let ((org-footnote-section nil
)) (org-footnote-sort))
416 \[fn:1] Def 1[fn:2][fn:3]
424 (org-test-with-temp-text "
427 \[fn:1] Def 1[fn:2][fn:3]
435 (let ((org-footnote-section nil
)) (org-footnote-sort))
437 ;; Insert un-referenced definitions at the end.
446 (org-test-with-temp-text "Text[fn:9]\n\n[fn:1] A\n[fn:9] B"
447 (let ((org-footnote-section nil
)) (org-footnote-sort))
450 (ert-deftest test-org-footnote
/renumber-fn
:N
()
451 "Test `org-footnote-renumber-fn:N' specifications."
452 ;; Renumber (inline) references and definitions.
456 (org-test-with-temp-text "Test[fn:99]"
457 (org-footnote-renumber-fn:N
)
461 "Test[fn:1]\n\n[fn:1] 99"
462 (org-test-with-temp-text "Test[fn:99]\n\n[fn:99] 99"
463 (org-footnote-renumber-fn:N
)
468 (org-test-with-temp-text "Test[fn:99:99]"
469 (org-footnote-renumber-fn:N
)
471 ;; No-op if there's no numbered footnote.
474 "Test[fn:label]\n\n[fn:label] Def"
475 (org-test-with-temp-text "Test[fn:label]\n\n[fn:label] Def"
476 (org-footnote-renumber-fn:N
)
478 ;; Definitions without a reference get the highest numbers.
481 "Test[fn:1]\n[fn:1] 1\n[fn:2] 99"
482 (org-test-with-temp-text "Test[fn:1]\n[fn:1] 1\n[fn:99] 99"
483 (org-footnote-renumber-fn:N
)
485 ;; Sort labels in sequence. Anonymous footnotes are ignored.
488 "Test[fn:1][fn:2:def][fn:3]"
489 (org-test-with-temp-text "Test[fn:4][fn:3:def][fn:2]"
490 (org-footnote-renumber-fn:N
)
494 "Test[fn:1][fn::def][fn:2]"
495 (org-test-with-temp-text "Test[fn:4][fn::def][fn:2]"
496 (org-footnote-renumber-fn:N
)
499 (ert-deftest test-org-footnote
/normalize
()
500 "Test `org-footnote-normalize' specifications."
501 ;; Normalize regular, inline and anonymous references.
504 "Test[fn:1]\n\n[fn:1] def\n"
505 (org-test-with-temp-text "Test[fn:label]\n[fn:label] def"
506 (let ((org-footnote-section nil
)) (org-footnote-normalize))
510 "Test[fn:1]\n\n[fn:1] def\n"
511 (org-test-with-temp-text "Test[fn:label:def]"
512 (let ((org-footnote-section nil
)) (org-footnote-normalize))
516 "Test[fn:1]\n\n[fn:1] def\n"
517 (org-test-with-temp-text "Test[fn::def]"
518 (let ((org-footnote-section nil
)) (org-footnote-normalize))
520 ;; Normalization includes sorting.
523 "Test[fn:1][fn:2]\n\n[fn:1] def2\n\n[fn:2] def\n"
524 (org-test-with-temp-text "Test[fn:2][fn:1]\n\n[fn:2] def2\n[fn:1] def"
525 (let ((org-footnote-section nil
)) (org-footnote-normalize))
529 "Test[fn:1][fn:2]\n\n[fn:1] def\n\n[fn:2] inline\n"
530 (org-test-with-temp-text "Test[fn:2][fn::inline]\n[fn:2] def\n"
531 (let ((org-footnote-section nil
)) (org-footnote-normalize))
543 (org-test-with-temp-text
544 "Test[fn:lab1][fn:lab2]\n[fn:lab1] def[fn::inline]\n[fn:lab2] last"
545 (let ((org-footnote-section nil
)) (org-footnote-normalize))
547 ;; When normalizing an inline reference, fill paragraph whenever the
548 ;; `org-footnote-fill-after-inline-note-extraction' is non-nil.
551 "Test[fn:1] Next\n\n[fn:1] def\n"
552 (org-test-with-temp-text "Test[fn::def]\nNext"
553 (let ((org-footnote-section nil
)
554 (org-footnote-fill-after-inline-note-extraction t
))
555 (org-footnote-normalize))
557 ;; Insert un-referenced definitions at the end.
560 "Test[fn:1]\nNext\n\n[fn:1] def\n\n[fn:2] A\n"
561 (org-test-with-temp-text "Test[fn::def]\nNext\n[fn:unref] A"
562 (let ((org-footnote-section nil
)) (org-footnote-normalize))
566 (provide 'test-org-footnote
)
567 ;;; test-org-footnote.el ends here