ox-latex: Remove unused function
[org-mode.git] / testing / lisp / test-org.el
blob259dc04c8b890eeffc59488d7cd0b1d61e6360a5
1 ;;; test-org.el --- tests for org.el
3 ;; Copyright (c) David Maus
4 ;; Authors: David Maus
6 ;; This file is not part of GNU Emacs.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Comments:
23 ;; Template test file for Org-mode tests
25 ;;; Code:
28 ;;; Comments
30 (ert-deftest test-org/comment-dwim ()
31 "Test `comment-dwim' behaviour in an Org buffer."
32 ;; No region selected, no comment on current line and line not
33 ;; empty: insert comment on line above.
34 (should
35 (equal "# \nComment"
36 (org-test-with-temp-text "Comment"
37 (progn (call-interactively 'comment-dwim)
38 (buffer-string)))))
39 ;; No region selected, no comment on current line and line empty:
40 ;; insert comment on this line.
41 (should
42 (equal "# \nParagraph"
43 (org-test-with-temp-text "\nParagraph"
44 (progn (call-interactively 'comment-dwim)
45 (buffer-string)))))
46 ;; No region selected, and a comment on this line: indent it.
47 (should
48 (equal "* Headline\n # Comment"
49 (org-test-with-temp-text "* Headline\n# Comment"
50 (progn (forward-line)
51 (let ((org-adapt-indentation t))
52 (call-interactively 'comment-dwim))
53 (buffer-string)))))
54 ;; Also recognize single # at column 0 as comments.
55 (should
56 (equal "# Comment"
57 (org-test-with-temp-text "# Comment"
58 (progn (forward-line)
59 (call-interactively 'comment-dwim)
60 (buffer-string)))))
61 ;; Region selected and only comments and blank lines within it:
62 ;; un-comment all commented lines.
63 (should
64 (equal "Comment 1\n\nComment 2"
65 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
66 (progn
67 (transient-mark-mode 1)
68 (push-mark (point) t t)
69 (goto-char (point-max))
70 (call-interactively 'comment-dwim)
71 (buffer-string)))))
72 ;; Region selected without comments: comment all non-blank lines.
73 (should
74 (equal "# Comment 1\n\n# Comment 2"
75 (org-test-with-temp-text "Comment 1\n\nComment 2"
76 (progn
77 (transient-mark-mode 1)
78 (push-mark (point) t t)
79 (goto-char (point-max))
80 (call-interactively 'comment-dwim)
81 (buffer-string)))))
82 ;; In front of a keyword without region, insert a new comment.
83 (should
84 (equal "# \n#+KEYWORD: value"
85 (org-test-with-temp-text "#+KEYWORD: value"
86 (progn (call-interactively 'comment-dwim)
87 (buffer-string))))))
91 ;;; Date and time analysis
93 (ert-deftest test-org/org-read-date ()
94 "Test `org-read-date' specifications."
95 ;; Parse ISO date with abbreviated year and month.
96 (should (equal "2012-03-29 16:40"
97 (let ((org-time-was-given t))
98 (org-read-date t nil "12-3-29 16:40"))))
99 ;; Parse Europeans dates.
100 (should (equal "2012-03-29 16:40"
101 (let ((org-time-was-given t))
102 (org-read-date t nil "29.03.2012 16:40"))))
103 ;; Parse Europeans dates without year.
104 (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
105 (let ((org-time-was-given t))
106 (org-read-date t nil "29.03. 16:40")))))
108 (ert-deftest test-org/org-parse-time-string ()
109 "Test `org-parse-time-string'."
110 (should (equal (org-parse-time-string "2012-03-29 16:40")
111 '(0 40 16 29 3 2012 nil nil nil)))
112 (should (equal (org-parse-time-string "[2012-03-29 16:40]")
113 '(0 40 16 29 3 2012 nil nil nil)))
114 (should (equal (org-parse-time-string "<2012-03-29 16:40>")
115 '(0 40 16 29 3 2012 nil nil nil)))
116 (should (equal (org-parse-time-string "<2012-03-29>")
117 '(0 0 0 29 3 2012 nil nil nil)))
118 (should (equal (org-parse-time-string "<2012-03-29>" t)
119 '(0 nil nil 29 3 2012 nil nil nil))))
122 ;;; Filling
124 (ert-deftest test-org/fill-paragraph ()
125 "Test `org-fill-paragraph' specifications."
126 ;; At an Org table, align it.
127 (should
128 (equal "| a |\n"
129 (org-test-with-temp-text "|a|"
130 (org-fill-paragraph)
131 (buffer-string))))
132 (should
133 (equal "#+name: table\n| a |\n"
134 (org-test-with-temp-text "#+name: table\n| a |"
135 (org-fill-paragraph)
136 (buffer-string))))
137 ;; At a paragraph, preserve line breaks.
138 (org-test-with-temp-text "some \\\\\nlong\ntext"
139 (let ((fill-column 20))
140 (org-fill-paragraph)
141 (should (equal (buffer-string) "some \\\\\nlong text"))))
142 ;; Correctly fill a paragraph when point is at its very end.
143 (should
144 (equal "A B"
145 (org-test-with-temp-text "A\nB"
146 (let ((fill-column 20))
147 (goto-char (point-max))
148 (org-fill-paragraph)
149 (buffer-string)))))
150 ;; Correctly fill the last paragraph of a greater element.
151 (should
152 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
153 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
154 (let ((fill-column 8))
155 (forward-line)
156 (end-of-line)
157 (org-fill-paragraph)
158 (buffer-string)))))
159 ;; Correctly fill an element in a narrowed buffer.
160 (should
161 (equal "01234\n6"
162 (org-test-with-temp-text "01234 6789"
163 (let ((fill-column 5))
164 (narrow-to-region 1 8)
165 (org-fill-paragraph)
166 (buffer-string)))))
167 ;; Special case: Fill first paragraph when point is at an item or
168 ;; a plain-list or a footnote reference.
169 (should
170 (equal "- A B"
171 (org-test-with-temp-text "- A\n B"
172 (let ((fill-column 20))
173 (org-fill-paragraph)
174 (buffer-string)))))
175 (should
176 (equal "[fn:1] A B"
177 (org-test-with-temp-text "[fn:1] A\nB"
178 (let ((fill-column 20))
179 (org-fill-paragraph)
180 (buffer-string)))))
181 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
182 (let ((fill-column 20))
183 (org-fill-paragraph)
184 (should (equal (buffer-string)
185 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
186 ;; Fill contents of `comment-block' elements.
187 (should
188 (equal
189 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
190 (let ((fill-column 20))
191 (forward-line)
192 (org-fill-paragraph)
193 (buffer-string)))
194 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
195 ;; Fill `comment' elements.
196 (should
197 (equal " # A B"
198 (org-test-with-temp-text " # A\n # B"
199 (let ((fill-column 20))
200 (org-fill-paragraph)
201 (buffer-string)))))
202 ;; Do nothing at affiliated keywords.
203 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
204 (let ((fill-column 20))
205 (org-fill-paragraph)
206 (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
207 ;; Do not move point after table when filling a table.
208 (should-not
209 (org-test-with-temp-text "| a | b |\n| c | d |\n"
210 (forward-char)
211 (org-fill-paragraph)
212 (eobp))))
214 (ert-deftest test-org/auto-fill-function ()
215 "Test auto-filling features."
216 ;; Auto fill paragraph.
217 (should
218 (equal "12345\n7890"
219 (org-test-with-temp-text "12345 7890"
220 (let ((fill-column 5))
221 (end-of-line)
222 (org-auto-fill-function)
223 (buffer-string)))))
224 ;; Auto fill first paragraph in an item.
225 (should
226 (equal "- 12345\n 7890"
227 (org-test-with-temp-text "- 12345 7890"
228 (let ((fill-column 7))
229 (end-of-line)
230 (org-auto-fill-function)
231 (buffer-string)))))
232 ;; Auto fill comments.
233 (should
234 (equal " # 12345\n # 7890"
235 (org-test-with-temp-text " # 12345 7890"
236 (let ((fill-column 10))
237 (end-of-line)
238 (org-auto-fill-function)
239 (buffer-string)))))
240 ;; A hash within a line isn't a comment.
241 (should-not
242 (equal "12345 # 7890\n# 1"
243 (org-test-with-temp-text "12345 # 7890 1"
244 (let ((fill-column 12))
245 (end-of-line)
246 (org-auto-fill-function)
247 (buffer-string)))))
248 ;; Correctly interpret empty prefix.
249 (should-not
250 (equal "# a\n# b\nRegular\n# paragraph"
251 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
252 (let ((fill-column 12))
253 (end-of-line 3)
254 (org-auto-fill-function)
255 (buffer-string)))))
256 ;; Comment block: auto fill contents.
257 (should
258 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
259 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
260 (let ((fill-column 5))
261 (forward-line)
262 (end-of-line)
263 (org-auto-fill-function)
264 (buffer-string)))))
265 (should
266 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
267 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
268 (let ((fill-column 5))
269 (forward-line)
270 (end-of-line)
271 (org-auto-fill-function)
272 (buffer-string)))))
273 ;; Do not fill if a new item could be created.
274 (should-not
275 (equal "12345\n- 90"
276 (org-test-with-temp-text "12345 - 90"
277 (let ((fill-column 5))
278 (end-of-line)
279 (org-auto-fill-function)
280 (buffer-string)))))
281 ;; Do not fill if a line break could be introduced.
282 (should-not
283 (equal "123\\\\\n7890"
284 (org-test-with-temp-text "123\\\\ 7890"
285 (let ((fill-column 6))
286 (end-of-line)
287 (org-auto-fill-function)
288 (buffer-string)))))
289 ;; Do not fill affiliated keywords.
290 (should-not
291 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
292 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
293 (let ((fill-column 20))
294 (end-of-line)
295 (org-auto-fill-function)
296 (buffer-string))))))
300 ;;; Links
302 ;;;; Fuzzy Links
304 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
305 ;; a named element (#+name: text) and to headlines (* Text).
307 (ert-deftest test-org/fuzzy-links ()
308 "Test fuzzy links specifications."
309 ;; 1. Fuzzy link goes in priority to a matching target.
310 (should
311 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
312 (goto-line 5)
313 (org-open-at-point)
314 (looking-at "<<Test>>")))
315 ;; 2. Then fuzzy link points to an element with a given name.
316 (should
317 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
318 (goto-line 5)
319 (org-open-at-point)
320 (looking-at "#\\+NAME: Test")))
321 ;; 3. A target still lead to a matching headline otherwise.
322 (should
323 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
324 (goto-line 4)
325 (org-open-at-point)
326 (looking-at "\\* Head2")))
327 ;; 4. With a leading star in link, enforce heading match.
328 (should
329 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
330 (goto-line 3)
331 (org-open-at-point)
332 (looking-at "\\* Test"))))
335 ;;;; Link Escaping
337 (ert-deftest test-org/org-link-escape-ascii-character ()
338 "Escape an ascii character."
339 (should
340 (string=
341 "%5B"
342 (org-link-escape "["))))
344 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
345 "Escape an ascii control character."
346 (should
347 (string=
348 "%09"
349 (org-link-escape "\t"))))
351 (ert-deftest test-org/org-link-escape-multibyte-character ()
352 "Escape an unicode multibyte character."
353 (should
354 (string=
355 "%E2%82%AC"
356 (org-link-escape "€"))))
358 (ert-deftest test-org/org-link-escape-custom-table ()
359 "Escape string with custom character table."
360 (should
361 (string=
362 "Foo%3A%42ar%0A"
363 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
365 (ert-deftest test-org/org-link-escape-custom-table-merge ()
366 "Escape string with custom table merged with default table."
367 (should
368 (string=
369 "%5BF%6F%6F%3A%42ar%0A%5D"
370 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
372 (ert-deftest test-org/org-link-unescape-ascii-character ()
373 "Unescape an ascii character."
374 (should
375 (string=
377 (org-link-unescape "%5B"))))
379 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
380 "Unescpae an ascii control character."
381 (should
382 (string=
383 "\n"
384 (org-link-unescape "%0A"))))
386 (ert-deftest test-org/org-link-unescape-multibyte-character ()
387 "Unescape unicode multibyte character."
388 (should
389 (string=
390 "€"
391 (org-link-unescape "%E2%82%AC"))))
393 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
394 "Unescape old style percent escaped character."
395 (should
396 (string=
397 "àâçèéêîôùû"
398 (decode-coding-string (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
400 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
401 "Escape and unscape a URL that includes an escaped char.
402 http://article.gmane.org/gmane.emacs.orgmode/21459/"
403 (should
404 (string=
405 "http://some.host.com/form?&id=blah%2Bblah25"
406 (org-link-unescape (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
410 ;;; Node Properties
412 (ert-deftest test-org/accumulated-properties-in-drawers ()
413 "Ensure properties accumulate in subtree drawers."
414 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
415 (org-babel-next-src-block)
416 (should (equal '(2 1) (org-babel-execute-src-block)))))
420 ;;; Mark Region
422 (ert-deftest test-org/mark-subtree ()
423 "Test `org-mark-subtree' specifications."
424 ;; Error when point is before first headline.
425 (should-error
426 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
427 (progn (transient-mark-mode 1)
428 (org-mark-subtree))))
429 ;; Without argument, mark current subtree.
430 (should
431 (equal
432 '(12 32)
433 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
434 (progn (transient-mark-mode 1)
435 (forward-line 2)
436 (org-mark-subtree)
437 (list (region-beginning) (region-end))))))
438 ;; With an argument, move ARG up.
439 (should
440 (equal
441 '(1 32)
442 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
443 (progn (transient-mark-mode 1)
444 (forward-line 2)
445 (org-mark-subtree 1)
446 (list (region-beginning) (region-end))))))
447 ;; Do not get fooled by inlinetasks.
448 (when (featurep 'org-inlinetask)
449 (should
450 (= 1
451 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
452 (progn (transient-mark-mode 1)
453 (forward-line 1)
454 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
455 (region-beginning)))))))
459 ;;; Navigation
461 (ert-deftest test-org/beginning-of-line ()
462 "Test `org-beginning-of-line' specifications."
463 ;; Standard test.
464 (should
465 (org-test-with-temp-text "Some text\nSome other text"
466 (progn (org-beginning-of-line) (bolp))))
467 ;; Standard test with `visual-line-mode'.
468 (should-not
469 (org-test-with-temp-text "A long line of text\nSome other text"
470 (progn (visual-line-mode)
471 (forward-char 2)
472 (dotimes (i 1000) (insert "very "))
473 (org-beginning-of-line)
474 (bolp))))
475 ;; At an headline with special movement.
476 (should
477 (org-test-with-temp-text "* TODO Headline"
478 (let ((org-special-ctrl-a/e t))
479 (org-end-of-line)
480 (and (progn (org-beginning-of-line) (looking-at "Headline"))
481 (progn (org-beginning-of-line) (bolp))
482 (progn (org-beginning-of-line) (looking-at "Headline")))))))
484 (ert-deftest test-org/end-of-line ()
485 "Test `org-end-of-line' specifications."
486 ;; Standard test.
487 (should
488 (org-test-with-temp-text "Some text\nSome other text"
489 (progn (org-end-of-line) (eolp))))
490 ;; Standard test with `visual-line-mode'.
491 (should-not
492 (org-test-with-temp-text "A long line of text\nSome other text"
493 (progn (visual-line-mode)
494 (forward-char 2)
495 (dotimes (i 1000) (insert "very "))
496 (goto-char (point-min))
497 (org-end-of-line)
498 (eolp))))
499 ;; At an headline with special movement.
500 (should
501 (org-test-with-temp-text "* Headline1 :tag:\n"
502 (let ((org-special-ctrl-a/e t))
503 (and (progn (org-end-of-line) (looking-at " :tag:"))
504 (progn (org-end-of-line) (eolp))
505 (progn (org-end-of-line) (looking-at " :tag:"))))))
506 ;; At an headline without special movement.
507 (should
508 (org-test-with-temp-text "* Headline2 :tag:\n"
509 (let ((org-special-ctrl-a/e nil))
510 (and (progn (org-end-of-line) (eolp))
511 (progn (org-end-of-line) (eolp))))))
512 ;; At an headline, with reversed movement.
513 (should
514 (org-test-with-temp-text "* Headline3 :tag:\n"
515 (let ((org-special-ctrl-a/e 'reversed)
516 (this-command last-command))
517 (and (progn (org-end-of-line) (eolp))
518 (progn (org-end-of-line) (looking-at " :tag:"))))))
519 ;; At a block without hidden contents.
520 (should
521 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
522 (progn (org-end-of-line) (eolp))))
523 ;; At a block with hidden contents.
524 (should-not
525 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
526 (let ((org-special-ctrl-a/e t))
527 (org-hide-block-toggle)
528 (org-end-of-line)
529 (eobp)))))
531 (ert-deftest test-org/forward-element ()
532 "Test `org-forward-element' specifications."
533 ;; 1. At EOB: should error.
534 (org-test-with-temp-text "Some text\n"
535 (goto-char (point-max))
536 (should-error (org-forward-element)))
537 ;; 2. Standard move: expected to ignore blank lines.
538 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
539 (org-forward-element)
540 (should (looking-at (regexp-quote "Second paragraph."))))
541 ;; 3. Headline tests.
542 (org-test-with-temp-text "
543 * Head 1
544 ** Head 1.1
545 *** Head 1.1.1
546 ** Head 1.2"
547 ;; 3.1. At an headline beginning: move to next headline at the
548 ;; same level.
549 (goto-line 3)
550 (org-forward-element)
551 (should (looking-at (regexp-quote "** Head 1.2")))
552 ;; 3.2. At an headline beginning: move to parent headline if no
553 ;; headline at the same level.
554 (goto-line 3)
555 (org-forward-element)
556 (should (looking-at (regexp-quote "** Head 1.2"))))
557 ;; 4. Greater element tests.
558 (org-test-with-temp-text
559 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
560 ;; 4.1. At a greater element: expected to skip contents.
561 (org-forward-element)
562 (should (looking-at (regexp-quote "Outside.")))
563 ;; 4.2. At the end of greater element contents: expected to skip
564 ;; to the end of the greater element.
565 (goto-line 2)
566 (org-forward-element)
567 (should (looking-at (regexp-quote "Outside."))))
568 ;; 5. List tests.
569 (org-test-with-temp-text "
570 - item1
572 - sub1
574 - sub2
576 - sub3
578 Inner paragraph.
580 - item2
582 Outside."
583 ;; 5.1. At list top point: expected to move to the element after
584 ;; the list.
585 (goto-line 2)
586 (org-forward-element)
587 (should (looking-at (regexp-quote "Outside.")))
588 ;; 5.2. Special case: at the first line of a sub-list, but not at
589 ;; beginning of line, move to next item.
590 (goto-line 2)
591 (forward-char)
592 (org-forward-element)
593 (should (looking-at "- item2"))
594 (goto-line 4)
595 (forward-char)
596 (org-forward-element)
597 (should (looking-at " - sub2"))
598 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
599 (goto-line 4)
600 (org-forward-element)
601 (should (looking-at (regexp-quote " Inner paragraph.")))
602 ;; 5.4. At sub-list end: expected to move outside the sub-list.
603 (goto-line 8)
604 (org-forward-element)
605 (should (looking-at (regexp-quote " Inner paragraph.")))
606 ;; 5.5. At an item: expected to move to next item, if any.
607 (goto-line 6)
608 (org-forward-element)
609 (should (looking-at " - sub3"))))
611 (ert-deftest test-org/backward-element ()
612 "Test `org-backward-element' specifications."
613 ;; 1. Should error at BOB.
614 (org-test-with-temp-text " \nParagraph."
615 (should-error (org-backward-element)))
616 ;; 2. Should move at BOB when called on the first element in buffer.
617 (should
618 (org-test-with-temp-text "\n#+TITLE: test"
619 (progn (forward-line)
620 (org-backward-element)
621 (bobp))))
622 ;; 3. Not at the beginning of an element: move at its beginning.
623 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
624 (goto-line 3)
625 (end-of-line)
626 (org-backward-element)
627 (should (looking-at (regexp-quote "Paragraph2."))))
628 ;; 4. Headline tests.
629 (org-test-with-temp-text "
630 * Head 1
631 ** Head 1.1
632 *** Head 1.1.1
633 ** Head 1.2"
634 ;; 4.1. At an headline beginning: move to previous headline at the
635 ;; same level.
636 (goto-line 5)
637 (org-backward-element)
638 (should (looking-at (regexp-quote "** Head 1.1")))
639 ;; 4.2. At an headline beginning: move to parent headline if no
640 ;; headline at the same level.
641 (goto-line 3)
642 (org-backward-element)
643 (should (looking-at (regexp-quote "* Head 1")))
644 ;; 4.3. At the first top-level headline: should error.
645 (goto-line 2)
646 (should-error (org-backward-element)))
647 ;; 5. At beginning of first element inside a greater element:
648 ;; expected to move to greater element's beginning.
649 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
650 (goto-line 3)
651 (org-backward-element)
652 (should (looking-at "#\\+BEGIN_CENTER")))
653 ;; 6. At the beginning of the first element in a section: should
654 ;; move back to headline, if any.
655 (should
656 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
657 (progn (goto-char (point-max))
658 (beginning-of-line)
659 (org-backward-element)
660 (org-at-heading-p))))
661 ;; 7. List tests.
662 (org-test-with-temp-text "
663 - item1
665 - sub1
667 - sub2
669 - sub3
671 Inner paragraph.
673 - item2
676 Outside."
677 ;; 7.1. At beginning of sub-list: expected to move to the
678 ;; paragraph before it.
679 (goto-line 4)
680 (org-backward-element)
681 (should (looking-at "item1"))
682 ;; 7.2. At an item in a list: expected to move at previous item.
683 (goto-line 8)
684 (org-backward-element)
685 (should (looking-at " - sub2"))
686 (goto-line 12)
687 (org-backward-element)
688 (should (looking-at "- item1"))
689 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
690 ;; beginning.
691 (goto-line 10)
692 (org-backward-element)
693 (should (looking-at " - sub1"))
694 (goto-line 15)
695 (org-backward-element)
696 (should (looking-at "- item1"))
697 ;; 7.4. At blank-lines before list end: expected to move to top
698 ;; item.
699 (goto-line 14)
700 (org-backward-element)
701 (should (looking-at "- item1"))))
703 (ert-deftest test-org/up-element ()
704 "Test `org-up-element' specifications."
705 ;; 1. At BOB or with no surrounding element: should error.
706 (org-test-with-temp-text "Paragraph."
707 (should-error (org-up-element)))
708 (org-test-with-temp-text "* Head1\n* Head2"
709 (goto-line 2)
710 (should-error (org-up-element)))
711 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
712 (goto-line 3)
713 (should-error (org-up-element)))
714 ;; 2. At an headline: move to parent headline.
715 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
716 (goto-line 3)
717 (org-up-element)
718 (should (looking-at "\\* Head1")))
719 ;; 3. Inside a greater element: move to greater element beginning.
720 (org-test-with-temp-text
721 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
722 (goto-line 3)
723 (org-up-element)
724 (should (looking-at "#\\+BEGIN_CENTER")))
725 ;; 4. List tests.
726 (org-test-with-temp-text "* Top
727 - item1
729 - sub1
731 - sub2
733 Paragraph within sub2.
735 - item2"
736 ;; 4.1. Within an item: move to the item beginning.
737 (goto-line 8)
738 (org-up-element)
739 (should (looking-at " - sub2"))
740 ;; 4.2. At an item in a sub-list: move to parent item.
741 (goto-line 4)
742 (org-up-element)
743 (should (looking-at "- item1"))
744 ;; 4.3. At an item in top list: move to beginning of whole list.
745 (goto-line 10)
746 (org-up-element)
747 (should (looking-at "- item1"))
748 ;; 4.4. Special case. At very top point: should move to parent of
749 ;; list.
750 (goto-line 2)
751 (org-up-element)
752 (should (looking-at "\\* Top"))))
754 (ert-deftest test-org/down-element ()
755 "Test `org-down-element' specifications."
756 ;; Error when the element hasn't got a recursive type.
757 (org-test-with-temp-text "Paragraph."
758 (should-error (org-down-element)))
759 ;; Error when the element has no contents
760 (org-test-with-temp-text "* Headline"
761 (should-error (org-down-element)))
762 ;; When at a plain-list, move to first item.
763 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
764 (goto-line 2)
765 (org-down-element)
766 (should (looking-at " - Item 1.1")))
767 (org-test-with-temp-text "#+NAME: list\n- Item 1"
768 (org-down-element)
769 (should (looking-at " Item 1")))
770 ;; When at a table, move to first row
771 (org-test-with-temp-text "#+NAME: table\n| a | b |"
772 (org-down-element)
773 (should (looking-at " a | b |")))
774 ;; Otherwise, move inside the greater element.
775 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
776 (org-down-element)
777 (should (looking-at "Paragraph"))))
779 (ert-deftest test-org/drag-element-backward ()
780 "Test `org-drag-element-backward' specifications."
781 ;; 1. Error when trying to move first element of buffer.
782 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
783 (should-error (org-drag-element-backward)))
784 ;; 2. Error when trying to swap nested elements.
785 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
786 (forward-line)
787 (should-error (org-drag-element-backward)))
788 ;; 3. Error when trying to swap an headline element and
789 ;; a non-headline element.
790 (org-test-with-temp-text "Test.\n* Head 1"
791 (forward-line)
792 (should-error (org-drag-element-backward)))
793 ;; 4. Otherwise, swap elements, preserving column and blank lines
794 ;; between elements.
795 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
796 (search-forward "graph")
797 (org-drag-element-backward)
798 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
799 (should (looking-at " 2")))
800 ;; 5. Preserve visibility of elements and their contents.
801 (org-test-with-temp-text "
802 #+BEGIN_CENTER
803 Text.
804 #+END_CENTER
805 - item 1
806 #+BEGIN_QUOTE
807 Text.
808 #+END_QUOTE"
809 (while (search-forward "BEGIN_" nil t) (org-cycle))
810 (search-backward "- item 1")
811 (org-drag-element-backward)
812 (should
813 (equal
814 '((63 . 82) (26 . 48))
815 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
816 (overlays-in (point-min) (point-max)))))))
818 (ert-deftest test-org/drag-element-forward ()
819 "Test `org-drag-element-forward' specifications."
820 ;; 1. Error when trying to move first element of buffer.
821 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
822 (goto-line 3)
823 (should-error (org-drag-element-forward)))
824 ;; 2. Error when trying to swap nested elements.
825 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
826 (forward-line)
827 (should-error (org-drag-element-forward)))
828 ;; 3. Error when trying to swap a non-headline element and an
829 ;; headline.
830 (org-test-with-temp-text "Test.\n* Head 1"
831 (should-error (org-drag-element-forward)))
832 ;; 4. Otherwise, swap elements, preserving column and blank lines
833 ;; between elements.
834 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
835 (search-forward "graph")
836 (org-drag-element-forward)
837 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
838 (should (looking-at " 1")))
839 ;; 5. Preserve visibility of elements and their contents.
840 (org-test-with-temp-text "
841 #+BEGIN_CENTER
842 Text.
843 #+END_CENTER
844 - item 1
845 #+BEGIN_QUOTE
846 Text.
847 #+END_QUOTE"
848 (while (search-forward "BEGIN_" nil t) (org-cycle))
849 (search-backward "#+BEGIN_CENTER")
850 (org-drag-element-forward)
851 (should
852 (equal
853 '((63 . 82) (26 . 48))
854 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
855 (overlays-in (point-min) (point-max)))))))
859 ;;; Planning
861 (ert-deftest test-org/timestamp-has-time-p ()
862 "Test `org-timestamp-has-time-p' specifications."
863 ;; With time.
864 (should
865 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
866 (org-timestamp-has-time-p (org-element-context))))
867 ;; Without time.
868 (should-not
869 (org-test-with-temp-text "<2012-03-29 Thu>"
870 (org-timestamp-has-time-p (org-element-context)))))
872 (ert-deftest test-org/timestamp-format ()
873 "Test `org-timestamp-format' specifications."
874 ;; Regular test.
875 (should
876 (equal
877 "2012-03-29 16:40"
878 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
879 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
880 ;; Range end.
881 (should
882 (equal
883 "2012-03-29"
884 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
885 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
887 (ert-deftest test-org/timestamp-split-range ()
888 "Test `org-timestamp-split-range' specifications."
889 ;; Extract range start (active).
890 (should
891 (equal '(2012 3 29)
892 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
893 (let ((ts (org-timestamp-split-range (org-element-context))))
894 (mapcar (lambda (p) (org-element-property p ts))
895 '(:year-end :month-end :day-end))))))
896 ;; Extract range start (inactive)
897 (should
898 (equal '(2012 3 29)
899 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
900 (let ((ts (org-timestamp-split-range (org-element-context))))
901 (mapcar (lambda (p) (org-element-property p ts))
902 '(:year-end :month-end :day-end))))))
903 ;; Extract range end (active).
904 (should
905 (equal '(2012 3 30)
906 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
907 (let ((ts (org-timestamp-split-range
908 (org-element-context) t)))
909 (mapcar (lambda (p) (org-element-property p ts))
910 '(:year-end :month-end :day-end))))))
911 ;; Extract range end (inactive)
912 (should
913 (equal '(2012 3 30)
914 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
915 (let ((ts (org-timestamp-split-range
916 (org-element-context) t)))
917 (mapcar (lambda (p) (org-element-property p ts))
918 '(:year-end :month-end :day-end))))))
919 ;; Return the timestamp if not a range.
920 (should
921 (org-test-with-temp-text "[2012-03-29 Thu]"
922 (let* ((ts-orig (org-element-context))
923 (ts-copy (org-timestamp-split-range ts-orig)))
924 (eq ts-orig ts-copy))))
925 (should
926 (org-test-with-temp-text "<%%(org-float t 4 2)>"
927 (let* ((ts-orig (org-element-context))
928 (ts-copy (org-timestamp-split-range ts-orig)))
929 (eq ts-orig ts-copy))))
930 ;; Check that parent is the same when a range was split.
931 (should
932 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
933 (let* ((ts-orig (org-element-context))
934 (ts-copy (org-timestamp-split-range ts-orig)))
935 (eq (org-element-property :parent ts-orig)
936 (org-element-property :parent ts-copy))))))
938 (ert-deftest test-org/timestamp-translate ()
939 "Test `org-timestamp-translate' specifications."
940 ;; Translate whole date range.
941 (should
942 (equal "<29>--<30>"
943 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
944 (let ((org-display-custom-times t)
945 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
946 (org-timestamp-translate (org-element-context))))))
947 ;; Translate date range start.
948 (should
949 (equal "<29>"
950 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
951 (let ((org-display-custom-times t)
952 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
953 (org-timestamp-translate (org-element-context) 'start)))))
954 ;; Translate date range end.
955 (should
956 (equal "<30>"
957 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
958 (let ((org-display-custom-times t)
959 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
960 (org-timestamp-translate (org-element-context) 'end)))))
961 ;; Translate time range.
962 (should
963 (equal "<08>--<16>"
964 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
965 (let ((org-display-custom-times t)
966 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
967 (org-timestamp-translate (org-element-context))))))
968 ;; Translate non-range timestamp.
969 (should
970 (equal "<29>"
971 (org-test-with-temp-text "<2012-03-29 Thu>"
972 (let ((org-display-custom-times t)
973 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
974 (org-timestamp-translate (org-element-context))))))
975 ;; Do not change `diary' timestamps.
976 (should
977 (equal "<%%(org-float t 4 2)>"
978 (org-test-with-temp-text "<%%(org-float t 4 2)>"
979 (let ((org-display-custom-times t)
980 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
981 (org-timestamp-translate (org-element-context)))))))
985 ;;; Targets and Radio Targets
987 (ert-deftest test-org/all-targets ()
988 "Test `org-all-targets' specifications."
989 ;; Without an argument.
990 (should
991 (equal '("radio-target" "target")
992 (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
993 (org-all-targets))))
994 (should
995 (equal '("radio-target")
996 (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
997 ;; With argument.
998 (should
999 (equal '("radio-target")
1000 (org-test-with-temp-text "<<target>> <<<radio-target>>>"
1001 (org-all-targets t)))))
1004 (provide 'test-org)
1006 ;;; test-org.el ends here