org-habit: Fix 6652baa39db26df8a8ac5dbbe40f3de91bf1a6b1
[org-mode.git] / testing / lisp / test-org-footnote.el
blobe73dbf8d58a93bf2a9cf1af5e9c633e340454f7e
1 ;;; test-org-footnote.el --- Tests for org-footnote.el
3 ;; Copyright (C) 2012-2015 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
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 `plain'.
34 (should
35 (string-match-p
36 "Test\\[1\\]\n+\\[1\\]"
37 (org-test-with-temp-text "Test<point>"
38 (let ((org-footnote-auto-label 'plain)
39 (org-footnote-section nil))
40 (org-footnote-new))
41 (buffer-string))))
42 ;; `org-footnote-auto-label' is `random'.
43 (should
44 (string-match-p
45 "Test\\[fn:\\(.+?\\)\\]\n+\\[fn:\\1\\]"
46 (org-test-with-temp-text "Test<point>"
47 (let ((org-footnote-auto-label 'random)
48 (org-footnote-section nil))
49 (org-footnote-new))
50 (buffer-string))))
51 ;; Error at beginning of line.
52 (should-error
53 (org-test-with-temp-text "<point>Test"
54 (org-footnote-new)))
55 ;; Error at keywords.
56 (should-error
57 (org-test-with-temp-text "#+TIT<point>LE: value"
58 (org-footnote-new)))
59 (should-error
60 (org-test-with-temp-text "#+CAPTION: <point>\nParagraph"
61 (org-footnote-new)))
62 ;; Allow new footnotes in blank lines at the beginning of the
63 ;; document.
64 (should
65 (string-match-p
66 " \\[fn:1\\]"
67 (org-test-with-temp-text " <point>"
68 (let ((org-footnote-auto-label t)) (org-footnote-new))
69 (buffer-string))))
70 ;; Allow new footnotes within recursive objects, but not in links.
71 (should
72 (string-match-p
73 " \\*bold\\[fn:1\\]\\*"
74 (org-test-with-temp-text " *bold<point>*"
75 (let ((org-footnote-auto-label t)) (org-footnote-new))
76 (buffer-string))))
77 (should-error
78 (org-test-with-temp-text " [[http://orgmode.org][Org mode<point>]]"
79 (org-footnote-new)))
80 ;; Allow new footnotes in blank lines after an element or white
81 ;; spaces after an object.
82 (should
83 (string-match-p
84 " \\[fn:1\\]"
85 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nA\n#+END_EXAMPLE\n <point>"
86 (let ((org-footnote-auto-label t)) (org-footnote-new))
87 (buffer-string))))
88 (should
89 (string-match-p
90 " \\*bold\\*\\[fn:1\\]"
91 (org-test-with-temp-text " *bold*<point>"
92 (let ((org-footnote-auto-label t)) (org-footnote-new))
93 (buffer-string)))))
95 (ert-deftest test-org-footnote/delete ()
96 "Test `org-footnote-delete' specifications."
97 ;; Regular test.
98 (should
99 (equal "Paragraph"
100 (org-test-with-temp-text "Paragraph[1]\n\n[1] Definition"
101 (search-forward "[")
102 (org-footnote-delete)
103 (org-trim (buffer-string)))))
104 ;; Remove multiple definitions and references.
105 (should
106 (equal "Paragraph and another"
107 (org-test-with-temp-text
108 "Paragraph[1] and another[1]\n\n[1] def\n\n[1] def"
109 (search-forward "[")
110 (org-footnote-delete)
111 (org-trim (buffer-string)))))
112 ;; Delete inline footnotes and all references.
113 (should
114 (equal "Para and"
115 (org-test-with-temp-text "Para[fn:label:def] and[fn:label]"
116 (search-forward "[")
117 (org-footnote-delete)
118 (org-trim (buffer-string)))))
119 ;; Delete anonymous footnotes.
120 (should
121 (equal "Para"
122 (org-test-with-temp-text "Para[fn::def]"
123 (search-forward "[")
124 (org-footnote-delete)
125 (org-trim (buffer-string)))))
126 ;; With an argument, delete footnote with specified label.
127 (should
128 (equal "Paragraph[1] and another\n\n[1] def"
129 (let ((org-footnote-section nil))
130 (org-test-with-temp-text
131 "Paragraph[1] and another[2]\n\n[1] def\n\n[2] def2"
132 (org-footnote-delete "2")
133 (org-trim (buffer-string))))))
134 ;; Error when no argument is specified at point is not at a footnote
135 ;; reference.
136 (should-error
137 (org-test-with-temp-text "Para[1]\n\n[1] Def"
138 (org-footnote-delete)))
139 ;; Correctly delete footnotes with multiple paragraphs.
140 (should
141 (equal "Para\n\n\nOutside footnote."
142 (org-test-with-temp-text
143 "Para[1]\n\n[1] para1\n\npara2\n\n\nOutside footnote."
144 (org-footnote-delete "1")
145 (org-trim (buffer-string))))))
147 (ert-deftest test-org-footnote/normalize-in-org ()
148 "Test specifications for `org-footnote-normalize' in an Org buffer."
149 ;; 1. With a non-nil `org-footnote-section'.
150 (let ((org-footnote-section "Footnotes")
151 (org-blank-before-new-entry '((heading . auto))))
152 ;; 1.1. Normalize each type of footnote: standard, labelled,
153 ;; numbered, inline, anonymous.
154 (org-test-with-temp-text
155 "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
157 * Footnotes
159 \[fn:1] Standard
161 \[fn:label] Labelled
163 \[1] Numbered"
164 (org-footnote-normalize)
165 (should
166 (equal (buffer-string)
167 "Paragraph[1][2][3][4][5]
169 * Footnotes
171 \[1] Standard
173 \[2] Labelled
175 \[3] Numbered
177 \[4] Inline
179 \[5] Anonymous
182 ")))
183 ;; 1.2. When no footnote section is present, create it. Follow
184 ;; `org-blank-before-new-entry' specifications when doing so.
185 (org-test-with-temp-text "Paragraph[fn:1]\n\n[fn:1] Definition"
186 (org-footnote-normalize)
187 (should (equal (buffer-string)
188 "Paragraph[1]\n\n* Footnotes\n\n[1] Definition")))
189 (org-test-with-temp-text "Paragraph[fn:1]\n* Head1\n[fn:1] Definition"
190 (let ((org-blank-before-new-entry '((heading))))
191 (org-footnote-normalize))
192 (should (equal (buffer-string)
193 "Paragraph[1]\n* Head1\n* Footnotes\n\n[1] Definition")))
194 ;; 1.3. When the footnote section is misplaced, move it at the end
195 ;; of the buffer.
196 (org-test-with-temp-text "* Head1
197 Body[fn:1]
198 * Footnotes
199 \[fn:1] Definition 1
200 * Head2"
201 (org-footnote-normalize)
202 (should
203 (equal (buffer-string)
204 "* Head1
205 Body[1]
206 * Head2
208 * Footnotes
210 \[1] Definition 1"))))
211 ;; 2. With a nil `org-footnote-section'.
212 (let ((org-footnote-section nil))
213 ;; 2.1. Normalize each type of footnote: standard, labelled,
214 ;; numbered, inline, anonymous.
215 (org-test-with-temp-text
216 "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
218 \[fn:1] Standard
220 \[fn:label] Labelled
222 \[1] Numbered"
223 (org-footnote-normalize)
224 (should
225 (equal (buffer-string)
226 "Paragraph[1][2][3][4][5]
228 \[1] Standard
230 \[2] Labelled
232 \[3] Numbered
234 \[4] Inline
236 \[5] Anonymous
237 ")))
238 ;; 2.2. Put each footnote definition at the end of the section
239 ;; containing its first reference.
240 (org-test-with-temp-text
241 "* Head 1
242 Text[fn:1:Def1]
243 * Head 2
244 Text[fn:1]
245 * Head 3
246 Text[fn:2:Def2]"
247 (org-footnote-normalize)
248 (should
249 (equal (buffer-string)
250 "* Head 1
251 Text[1]
253 \[1] Def1
254 * Head 2
255 Text[1]
256 * Head 3
257 Text[2]
259 \[2] Def2
260 ")))))
262 (ert-deftest test-org-footnote/normalize-outside-org ()
263 "Test `org-footnote-normalize' specifications for buffers not in Org mode."
264 ;; 1. In a non-Org buffer, footnotes definitions are always put at
265 ;; its end.
266 (should
267 (equal
268 "Paragraph[1][2][3][4][5]
271 Some additional text.
273 \[1] Standard
275 \[2] Labelled
277 \[3] Numbered
279 \[4] Inline
281 \[5] Anonymous"
282 (let ((org-footnote-tag-for-non-org-mode-files nil))
283 (with-temp-buffer
284 (insert "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
286 \[fn:1] Standard
288 \[fn:label] Labelled
290 \[1] Numbered
293 Some additional text.")
294 (org-footnote-normalize)
295 (buffer-string)))))
296 ;; 2. With a special tag.
297 (let ((org-footnote-tag-for-non-org-mode-files "Footnotes:"))
298 ;; 2.1. The tag must be inserted before the footnotes, separated
299 ;; from the rest of the text with a blank line.
300 (with-temp-buffer
301 (insert "Paragraph[fn:1][fn::Anonymous]
303 \[fn:1] Standard
306 Some additional text.")
307 (org-footnote-normalize)
308 (should
309 (equal (buffer-string)
310 "Paragraph[1][2]
313 Some additional text.
315 Footnotes:
317 \[1] Standard
319 \[2] Anonymous")))
320 ;; 2.2. Any tag already inserted in the buffer should be removed
321 ;; prior to footnotes insertion.
322 (with-temp-buffer
323 (insert "Text[fn:1]
324 Footnotes:
326 Additional text.
328 Footnotes:
330 \[fn:1] Definition")
331 (org-footnote-normalize)
332 (should
333 (equal (buffer-string)
334 "Text[1]
336 Additional text.
338 Footnotes:
340 \[1] Definition"))))
341 ;; 3. As an exception, in `message-mode' buffer, if a signature is
342 ;; present, insert footnotes before it.n
343 (let ((org-footnote-tag-for-non-org-mode-files nil))
344 (with-temp-buffer
345 (insert "Body[fn::def]
347 Fake signature
349 Signature")
350 ;; Mimic `message-mode'.
351 (let ((major-mode 'message-mode)
352 (message-cite-prefix-regexp "\\([ ]*[_.[:word:]]+>+\\|[ ]*[]>|]\\)+")
353 (message-signature-separator "^-- $"))
354 (flet ((message-point-in-header-p nil nil))
355 (org-footnote-normalize)))
356 (should
357 (equal (buffer-string)
358 "Body[1]
360 Fake signature
362 \[1] def
365 Signature")))))
367 (ert-deftest test-org-footnote/sort ()
368 "Test footnotes definitions sorting."
369 (let ((org-footnote-section nil))
370 (org-test-with-temp-text
371 "Text[fn:1][fn::inline][fn:2][fn:label]
373 \[fn:label] C
375 \[fn:1] A
377 \[fn:2] B"
378 (org-footnote-normalize 'sort)
379 (should
380 (equal (buffer-string)
381 "Text[fn:1][fn::inline][fn:2][fn:label]
383 \[fn:1] A
385 \[fn:2] B
387 \[fn:label] C
388 ")))))
391 (provide 'test-org-footnote)
392 ;;; test-org-footnote.el ends here