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))))))
170 ;; Remove blank lines above the footnote but preserve those after
173 (equal "Text\n\n\nOther text."
174 (let ((org-footnote-section nil
))
175 (org-test-with-temp-text
176 "Text[fn:1]\n\n[fn:1] Definition.\n\n\nOther text."
177 (org-footnote-delete "1")
180 (ert-deftest test-org-footnote
/goto-definition
()
181 "Test `org-footnote-goto-definition' specifications."
182 ;; Error on unknown definitions.
184 (org-test-with-temp-text "No footnote definition"
185 (org-footnote-goto-definition "1")))
186 ;; Error when trying to reach a definition outside narrowed part of
189 (org-test-with-temp-text "Some text<point>\n[fn:1] Definition."
190 (narrow-to-region (point-min) (point))
191 (org-footnote-goto-definition "1")))
193 (org-test-with-temp-text "[fn:1] Definition.\n<point>Some text"
194 (narrow-to-region (point) (point-max))
195 (org-footnote-goto-definition "1")))
196 ;; Otherwise, move at the beginning of the definition, including
197 ;; anonymous footnotes.
201 (org-test-with-temp-text "Some text\n[fn:1] Definition."
202 (org-footnote-goto-definition "1")
203 (buffer-substring (point) (point-max)))))
207 (org-test-with-temp-text "Some text[fn:label:definition]"
208 (org-footnote-goto-definition "label")
209 (buffer-substring (point) (point-max))))))
211 (ert-deftest test-org-footnote
/sort
()
212 "Test `org-footnote-sort' specifications."
213 ;; Reorder definitions with a nil `org-footnote-section'. In this
214 ;; case each definition is written at the end of the section
215 ;; containing its first reference.
225 (org-test-with-temp-text "
231 (let ((org-footnote-section nil
)) (org-footnote-sort))
245 (org-test-with-temp-text "
255 (let ((org-footnote-section nil
)) (org-footnote-sort))
257 ;; Reorder definitions with a non-nil `org-footnote-section'.
269 (org-test-with-temp-text "
275 (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
277 ;; When `org-footnote-section' is non-nil, clear previous footnote
292 (org-test-with-temp-text "
304 (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
306 ;; Ignore anonymous footnotes.
310 Text[fn:1][fn::inline][fn:2]
316 (org-test-with-temp-text
318 Text[fn:1][fn::inline][fn:2]
323 (let ((org-footnote-section nil
)) (org-footnote-sort))
325 ;; Ignore inline footnotes.
329 Text[fn:1][fn:label:inline][fn:2]
335 (org-test-with-temp-text
337 Text[fn:1][fn:label:inline][fn:2]
342 (let ((org-footnote-section nil
)) (org-footnote-sort))
344 ;; Handle (deeply) nested footnotes.
356 (org-test-with-temp-text "
365 (let ((org-footnote-section nil
)) (org-footnote-sort))
380 (org-test-with-temp-text "
391 (let ((org-footnote-section nil
)) (org-footnote-sort))
393 ;; When multiple (nested) references are used, make sure to insert
394 ;; definition only once.
407 (org-test-with-temp-text
418 (let ((org-footnote-section nil
)) (org-footnote-sort))
425 \[fn:1] Def 1[fn:2][fn:3]
433 (org-test-with-temp-text "
436 \[fn:1] Def 1[fn:2][fn:3]
444 (let ((org-footnote-section nil
)) (org-footnote-sort))
446 ;; Insert un-referenced definitions at the end.
455 (org-test-with-temp-text "Text[fn:9]\n\n[fn:1] A\n[fn:9] B"
456 (let ((org-footnote-section nil
)) (org-footnote-sort))
459 (ert-deftest test-org-footnote
/renumber-fn
:N
()
460 "Test `org-footnote-renumber-fn:N' specifications."
461 ;; Renumber (inline) references and definitions.
465 (org-test-with-temp-text "Test[fn:99]"
466 (org-footnote-renumber-fn:N
)
470 "Test[fn:1]\n\n[fn:1] 99"
471 (org-test-with-temp-text "Test[fn:99]\n\n[fn:99] 99"
472 (org-footnote-renumber-fn:N
)
477 (org-test-with-temp-text "Test[fn:99:99]"
478 (org-footnote-renumber-fn:N
)
480 ;; No-op if there's no numbered footnote.
483 "Test[fn:label]\n\n[fn:label] Def"
484 (org-test-with-temp-text "Test[fn:label]\n\n[fn:label] Def"
485 (org-footnote-renumber-fn:N
)
487 ;; Definitions without a reference get the highest numbers.
490 "Test[fn:1]\n[fn:1] 1\n[fn:2] 99"
491 (org-test-with-temp-text "Test[fn:1]\n[fn:1] 1\n[fn:99] 99"
492 (org-footnote-renumber-fn:N
)
494 ;; Sort labels in sequence. Anonymous footnotes are ignored.
497 "Test[fn:1][fn:2:def][fn:3]"
498 (org-test-with-temp-text "Test[fn:4][fn:3:def][fn:2]"
499 (org-footnote-renumber-fn:N
)
503 "Test[fn:1][fn::def][fn:2]"
504 (org-test-with-temp-text "Test[fn:4][fn::def][fn:2]"
505 (org-footnote-renumber-fn:N
)
508 (ert-deftest test-org-footnote
/normalize
()
509 "Test `org-footnote-normalize' specifications."
510 ;; Normalize regular, inline and anonymous references.
513 "Test[fn:1]\n\n[fn:1] def\n"
514 (org-test-with-temp-text "Test[fn:label]\n[fn:label] def"
515 (let ((org-footnote-section nil
)) (org-footnote-normalize))
519 "Test[fn:1]\n\n[fn:1] def\n"
520 (org-test-with-temp-text "Test[fn:label:def]"
521 (let ((org-footnote-section nil
)) (org-footnote-normalize))
525 "Test[fn:1]\n\n[fn:1] def\n"
526 (org-test-with-temp-text "Test[fn::def]"
527 (let ((org-footnote-section nil
)) (org-footnote-normalize))
529 ;; Normalization includes sorting.
532 "Test[fn:1][fn:2]\n\n[fn:1] def2\n\n[fn:2] def\n"
533 (org-test-with-temp-text "Test[fn:2][fn:1]\n\n[fn:2] def2\n[fn:1] def"
534 (let ((org-footnote-section nil
)) (org-footnote-normalize))
538 "Test[fn:1][fn:2]\n\n[fn:1] def\n\n[fn:2] inline\n"
539 (org-test-with-temp-text "Test[fn:2][fn::inline]\n[fn:2] def\n"
540 (let ((org-footnote-section nil
)) (org-footnote-normalize))
552 (org-test-with-temp-text
553 "Test[fn:lab1][fn:lab2]\n[fn:lab1] def[fn::inline]\n[fn:lab2] last"
554 (let ((org-footnote-section nil
)) (org-footnote-normalize))
556 ;; When normalizing an inline reference, fill paragraph whenever the
557 ;; `org-footnote-fill-after-inline-note-extraction' is non-nil.
560 "Test[fn:1] Next\n\n[fn:1] def\n"
561 (org-test-with-temp-text "Test[fn::def]\nNext"
562 (let ((org-footnote-section nil
)
563 (org-footnote-fill-after-inline-note-extraction t
))
564 (org-footnote-normalize))
566 ;; Insert un-referenced definitions at the end.
569 "Test[fn:1]\nNext\n\n[fn:1] def\n\n[fn:2] A\n"
570 (org-test-with-temp-text "Test[fn::def]\nNext\n[fn:unref] A"
571 (let ((org-footnote-section nil
)) (org-footnote-normalize))
575 (provide 'test-org-footnote
)
576 ;;; test-org-footnote.el ends here