1 ;;; test-org.el --- tests for org.el
3 ;; Copyright (c) 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/>.
23 ;; Template test file for Org-mode tests
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.
36 (org-test-with-temp-text "Comment"
37 (progn (call-interactively 'comment-dwim
)
39 ;; No region selected, no comment on current line and line empty:
40 ;; insert comment on this line.
42 (equal "# \nParagraph"
43 (org-test-with-temp-text "\nParagraph"
44 (progn (call-interactively 'comment-dwim
)
46 ;; No region selected, and a comment on this line: indent it.
48 (equal "* Headline\n # Comment"
49 (org-test-with-temp-text "* Headline\n# Comment"
51 (let ((org-adapt-indentation t
))
52 (call-interactively 'comment-dwim
))
54 ;; Also recognize single # at column 0 as comments.
57 (org-test-with-temp-text "# Comment"
59 (call-interactively 'comment-dwim
)
61 ;; Region selected and only comments and blank lines within it:
62 ;; un-comment all commented lines.
64 (equal "Comment 1\n\nComment 2"
65 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
67 (transient-mark-mode 1)
68 (push-mark (point) t t
)
69 (goto-char (point-max))
70 (call-interactively 'comment-dwim
)
72 ;; Region selected without comments: comment all lines if
73 ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
75 (equal "# Comment 1\n\n# Comment 2"
76 (org-test-with-temp-text "Comment 1\n\nComment 2"
78 (transient-mark-mode 1)
79 (push-mark (point) t t
)
80 (goto-char (point-max))
81 (let ((comment-empty-lines nil
))
82 (call-interactively 'comment-dwim
))
85 (equal "# Comment 1\n# \n# Comment 2"
86 (org-test-with-temp-text "Comment 1\n\nComment 2"
88 (transient-mark-mode 1)
89 (push-mark (point) t t
)
90 (goto-char (point-max))
91 (let ((comment-empty-lines t
))
92 (call-interactively 'comment-dwim
))
94 ;; In front of a keyword without region, insert a new comment.
96 (equal "# \n#+KEYWORD: value"
97 (org-test-with-temp-text "#+KEYWORD: value"
98 (progn (call-interactively 'comment-dwim
)
100 ;; In a source block, use appropriate syntax.
103 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n\n#+END_SRC"
105 (let ((org-edit-src-content-indentation 2))
106 (call-interactively 'comment-dwim
))
107 (buffer-substring-no-properties (line-beginning-position) (point)))))
109 (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
110 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\na\nb\n#+END_SRC"
112 (transient-mark-mode 1)
113 (push-mark (point) t t
)
115 (let ((org-edit-src-content-indentation 2))
116 (call-interactively 'comment-dwim
))
121 ;;; Date and time analysis
123 (ert-deftest test-org
/org-read-date
()
124 "Test `org-read-date' specifications."
125 ;; Parse ISO date with abbreviated year and month.
126 (should (equal "2012-03-29 16:40"
127 (let ((org-time-was-given t
))
128 (org-read-date t nil
"12-3-29 16:40"))))
129 ;; Parse Europeans dates.
130 (should (equal "2012-03-29 16:40"
131 (let ((org-time-was-given t
))
132 (org-read-date t nil
"29.03.2012 16:40"))))
133 ;; Parse Europeans dates without year.
134 (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
135 (let ((org-time-was-given t
))
136 (org-read-date t nil
"29.03. 16:40")))))
138 (ert-deftest test-org
/org-parse-time-string
()
139 "Test `org-parse-time-string'."
140 (should (equal (org-parse-time-string "2012-03-29 16:40")
141 '(0 40 16 29 3 2012 nil nil nil
)))
142 (should (equal (org-parse-time-string "[2012-03-29 16:40]")
143 '(0 40 16 29 3 2012 nil nil nil
)))
144 (should (equal (org-parse-time-string "<2012-03-29 16:40>")
145 '(0 40 16 29 3 2012 nil nil nil
)))
146 (should (equal (org-parse-time-string "<2012-03-29>")
147 '(0 0 0 29 3 2012 nil nil nil
)))
148 (should (equal (org-parse-time-string "<2012-03-29>" t
)
149 '(0 nil nil
29 3 2012 nil nil nil
))))
154 (ert-deftest test-org
/fill-paragraph
()
155 "Test `org-fill-paragraph' specifications."
156 ;; At an Org table, align it.
159 (org-test-with-temp-text "|a|"
163 (equal "#+name: table\n| a |\n"
164 (org-test-with-temp-text "#+name: table\n| a |"
167 ;; At a paragraph, preserve line breaks.
168 (org-test-with-temp-text "some \\\\\nlong\ntext"
169 (let ((fill-column 20))
171 (should (equal (buffer-string) "some \\\\\nlong text"))))
172 ;; Correctly fill a paragraph when point is at its very end.
175 (org-test-with-temp-text "A\nB"
176 (let ((fill-column 20))
177 (goto-char (point-max))
180 ;; Correctly fill the last paragraph of a greater element.
182 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
183 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
184 (let ((fill-column 8))
189 ;; Correctly fill an element in a narrowed buffer.
192 (org-test-with-temp-text "01234 6789"
193 (let ((fill-column 5))
194 (narrow-to-region 1 8)
197 ;; Handle `adaptive-fill-regexp' in paragraphs.
200 (org-test-with-temp-text "> a\n> b"
201 (let ((fill-column 5)
202 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
205 ;; Special case: Fill first paragraph when point is at an item or
206 ;; a plain-list or a footnote reference.
209 (org-test-with-temp-text "- A\n B"
210 (let ((fill-column 20))
215 (org-test-with-temp-text "[fn:1] A\nB"
216 (let ((fill-column 20))
219 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
220 (let ((fill-column 20))
222 (should (equal (buffer-string)
223 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
224 ;; Fill contents of `comment-block' elements.
227 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
228 (let ((fill-column 20))
232 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
233 ;; Fill `comment' elements.
236 (org-test-with-temp-text " # A\n # B"
237 (let ((fill-column 20))
240 ;; Do not mix consecutive comments when filling one of them.
242 (equal "# A B\n\n# C"
243 (org-test-with-temp-text "# A\n# B\n\n# C"
244 (let ((fill-column 20))
247 ;; Use commented empty lines as separators when filling comments.
249 (equal "# A B\n#\n# C"
250 (org-test-with-temp-text "# A\n# B\n#\n# C"
251 (let ((fill-column 20))
254 ;; Handle `adaptive-fill-regexp' in comments.
257 (org-test-with-temp-text "# > a\n# > b"
258 (let ((fill-column 20)
259 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
262 ;; Do nothing at affiliated keywords.
263 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
264 (let ((fill-column 20))
266 (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
267 ;; Do not move point after table when filling a table.
269 (org-test-with-temp-text "| a | b |\n| c | d |\n"
274 (ert-deftest test-org
/auto-fill-function
()
275 "Test auto-filling features."
276 ;; Auto fill paragraph.
279 (org-test-with-temp-text "12345 7890"
280 (let ((fill-column 5))
282 (org-auto-fill-function)
284 ;; Auto fill first paragraph in an item.
286 (equal "- 12345\n 7890"
287 (org-test-with-temp-text "- 12345 7890"
288 (let ((fill-column 7))
290 (org-auto-fill-function)
292 ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
294 (equal "> 12345\n 7890"
295 (org-test-with-temp-text "> 12345 7890"
296 (let ((fill-column 10)
297 (adaptive-fill-regexp "[ \t]*>+[ \t]*")
298 (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
300 (org-auto-fill-function)
303 (equal "> 12345\n> 12345\n> 7890"
304 (org-test-with-temp-text "> 12345\n> 12345 7890"
305 (let ((fill-column 10)
306 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
307 (goto-char (point-max))
308 (org-auto-fill-function)
311 (equal " 12345\n *12345\n *12345"
312 (org-test-with-temp-text " 12345\n *12345 12345"
313 (let ((fill-column 10)
314 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
315 (goto-char (point-max))
316 (org-auto-fill-function)
318 ;; Auto fill comments.
320 (equal " # 12345\n # 7890"
321 (org-test-with-temp-text " # 12345 7890"
322 (let ((fill-column 10))
324 (org-auto-fill-function)
326 ;; A hash within a line isn't a comment.
328 (equal "12345 # 7890\n# 1"
329 (org-test-with-temp-text "12345 # 7890 1"
330 (let ((fill-column 12))
332 (org-auto-fill-function)
334 ;; Correctly interpret empty prefix.
336 (equal "# a\n# b\nRegular\n# paragraph"
337 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
338 (let ((fill-column 12))
340 (org-auto-fill-function)
342 ;; Comment block: auto fill contents.
344 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
345 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
346 (let ((fill-column 5))
349 (org-auto-fill-function)
352 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
353 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
354 (let ((fill-column 5))
357 (org-auto-fill-function)
359 ;; Do not fill if a new item could be created.
362 (org-test-with-temp-text "12345 - 90"
363 (let ((fill-column 5))
365 (org-auto-fill-function)
367 ;; Do not fill if a line break could be introduced.
369 (equal "123\\\\\n7890"
370 (org-test-with-temp-text "123\\\\ 7890"
371 (let ((fill-column 6))
373 (org-auto-fill-function)
375 ;; Do not fill affiliated keywords.
377 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
378 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
379 (let ((fill-column 20))
381 (org-auto-fill-function)
390 (ert-deftest test-org
/meta-return
()
391 "Test M-RET (`org-meta-return')."
392 ;; In a table field insert a row above.
394 (org-test-with-temp-text "| a |"
398 (looking-at "| |$")))
399 ;; In a paragraph change current line into a header.
401 (org-test-with-temp-text "a"
404 (looking-at "\* a$")))
405 ;; In an item insert an item, in this case above.
407 (org-test-with-temp-text "- a"
411 ;; In a drawer and paragraph insert an empty line, in this case above.
413 (org-test-with-temp-text ":MYDRAWER:\na\n:END:"
418 ;; In a drawer and item insert an item, in this case above.
420 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
424 (looking-at "- $"))))
426 (ert-deftest test-org
/insert-todo-heading-respect-content
()
427 "Test `org-insert-todo-heading-respect-content' specifications."
428 ;; Create a TODO heading.
430 (org-test-with-temp-text "* H1\n Body"
431 (org-insert-todo-heading-respect-content)
432 (nth 2 (org-heading-components))))
433 ;; Add headline after body of current subtree.
435 (org-test-with-temp-text "* H1\nBody"
436 (org-insert-todo-heading-respect-content)
439 (org-test-with-temp-text "* H1\n** H2\nBody"
440 (org-insert-todo-heading-respect-content)
442 ;; In a list, do not create a new item.
444 (org-test-with-temp-text "* H\n- an item\n- another one"
445 (search-forward "an ")
446 (org-insert-todo-heading-respect-content)
447 (and (eobp) (org-at-heading-p)))))
455 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
456 ;; a named element (#+name: text) and to headlines (* Text).
458 (ert-deftest test-org
/fuzzy-links
()
459 "Test fuzzy links specifications."
460 ;; 1. Fuzzy link goes in priority to a matching target.
462 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
465 (looking-at "<<Test>>")))
466 ;; 2. Then fuzzy link points to an element with a given name.
468 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
471 (looking-at "#\\+NAME: Test")))
472 ;; 3. A target still lead to a matching headline otherwise.
474 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
477 (looking-at "\\* Head2")))
478 ;; 4. With a leading star in link, enforce heading match.
480 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
483 (looking-at "\\* Test"))))
488 (ert-deftest test-org
/org-link-escape-ascii-character
()
489 "Escape an ascii character."
493 (org-link-escape "["))))
495 (ert-deftest test-org
/org-link-escape-ascii-ctrl-character
()
496 "Escape an ascii control character."
500 (org-link-escape "\t"))))
502 (ert-deftest test-org
/org-link-escape-multibyte-character
()
503 "Escape an unicode multibyte character."
507 (org-link-escape "€"))))
509 (ert-deftest test-org
/org-link-escape-custom-table
()
510 "Escape string with custom character table."
514 (org-link-escape "Foo:Bar\n" '(?\
: ?\B
)))))
516 (ert-deftest test-org
/org-link-escape-custom-table-merge
()
517 "Escape string with custom table merged with default table."
520 "%5BF%6F%6F%3A%42ar%0A%5D"
521 (org-link-escape "[Foo:Bar\n]" '(?\
: ?\B ?\o
) t
))))
523 (ert-deftest test-org
/org-link-unescape-ascii-character
()
524 "Unescape an ascii character."
528 (org-link-unescape "%5B"))))
530 (ert-deftest test-org
/org-link-unescape-ascii-ctrl-character
()
531 "Unescpae an ascii control character."
535 (org-link-unescape "%0A"))))
537 (ert-deftest test-org
/org-link-unescape-multibyte-character
()
538 "Unescape unicode multibyte character."
542 (org-link-unescape "%E2%82%AC"))))
544 (ert-deftest test-org
/org-link-unescape-ascii-extended-char
()
545 "Unescape old style percent escaped character."
548 "à âçèéêîôùû"
549 (decode-coding-string
550 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1
))))
552 (ert-deftest test-org
/org-link-escape-url-with-escaped-char
()
553 "Escape and unescape a URL that includes an escaped char.
554 http://article.gmane.org/gmane.emacs.orgmode/21459/"
557 "http://some.host.com/form?&id=blah%2Bblah25"
559 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
561 (ert-deftest test-org
/org-link-escape-chars-browser
()
562 "Escape a URL to pass to `browse-url'."
565 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
566 "%22Release%208.2%22&idxname=emacs-orgmode")
567 (org-link-escape-browser
568 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
569 "\"Release 8.2\"&idxname=emacs-orgmode")))))
575 (ert-deftest test-org
/accumulated-properties-in-drawers
()
576 "Ensure properties accumulate in subtree drawers."
577 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
578 (org-babel-next-src-block)
579 (should (equal '(2 1) (org-babel-execute-src-block)))))
585 (ert-deftest test-org
/mark-subtree
()
586 "Test `org-mark-subtree' specifications."
587 ;; Error when point is before first headline.
589 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
590 (progn (transient-mark-mode 1)
591 (org-mark-subtree))))
592 ;; Without argument, mark current subtree.
596 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
597 (progn (transient-mark-mode 1)
600 (list (region-beginning) (region-end))))))
601 ;; With an argument, move ARG up.
605 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
606 (progn (transient-mark-mode 1)
609 (list (region-beginning) (region-end))))))
610 ;; Do not get fooled by inlinetasks.
611 (when (featurep 'org-inlinetask
)
614 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
615 (progn (transient-mark-mode 1)
617 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
618 (region-beginning)))))))
624 (ert-deftest test-org
/beginning-of-line
()
625 "Test `org-beginning-of-line' specifications."
628 (org-test-with-temp-text "Some text\nSome other text"
629 (progn (org-beginning-of-line) (bolp))))
630 ;; Standard test with `visual-line-mode'.
632 (org-test-with-temp-text "A long line of text\nSome other text"
633 (progn (visual-line-mode)
635 (dotimes (i 1000) (insert "very "))
636 (org-beginning-of-line)
638 ;; At an headline with special movement.
640 (org-test-with-temp-text "* TODO Headline"
641 (let ((org-special-ctrl-a/e t
))
643 (and (progn (org-beginning-of-line) (looking-at "Headline"))
644 (progn (org-beginning-of-line) (bolp))
645 (progn (org-beginning-of-line) (looking-at "Headline")))))))
647 (ert-deftest test-org
/end-of-line
()
648 "Test `org-end-of-line' specifications."
651 (org-test-with-temp-text "Some text\nSome other text"
652 (progn (org-end-of-line) (eolp))))
653 ;; Standard test with `visual-line-mode'.
655 (org-test-with-temp-text "A long line of text\nSome other text"
656 (progn (visual-line-mode)
658 (dotimes (i 1000) (insert "very "))
659 (goto-char (point-min))
662 ;; At an headline with special movement.
664 (org-test-with-temp-text "* Headline1 :tag:\n"
665 (let ((org-special-ctrl-a/e t
))
666 (and (progn (org-end-of-line) (looking-at " :tag:"))
667 (progn (org-end-of-line) (eolp))
668 (progn (org-end-of-line) (looking-at " :tag:"))))))
669 ;; At an headline without special movement.
671 (org-test-with-temp-text "* Headline2 :tag:\n"
672 (let ((org-special-ctrl-a/e nil
))
673 (and (progn (org-end-of-line) (eolp))
674 (progn (org-end-of-line) (eolp))))))
675 ;; At an headline, with reversed movement.
677 (org-test-with-temp-text "* Headline3 :tag:\n"
678 (let ((org-special-ctrl-a/e
'reversed
)
679 (this-command last-command
))
680 (and (progn (org-end-of-line) (eolp))
681 (progn (org-end-of-line) (looking-at " :tag:"))))))
682 ;; At a block without hidden contents.
684 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
685 (progn (org-end-of-line) (eolp))))
686 ;; At a block with hidden contents.
688 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
689 (let ((org-special-ctrl-a/e t
))
690 (org-hide-block-toggle)
694 (ert-deftest test-org
/forward-paragraph
()
695 "Test `org-forward-paragraph' specifications."
696 ;; At end of buffer, return an error.
698 (org-test-with-temp-text "Paragraph"
699 (goto-char (point-max))
700 (org-forward-paragraph)))
703 (org-test-with-temp-text "P1\n\nP2\n\nP3"
704 (org-forward-paragraph)
708 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
709 (org-forward-paragraph)
711 ;; Do not enter elements with invisible contents.
713 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
714 (org-hide-block-toggle)
715 (org-forward-paragraph)
717 ;; On an affiliated keyword, jump to the beginning of the element.
719 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
720 (org-forward-paragraph)
721 (looking-at "Para")))
722 ;; On an item or a footnote definition, move to the second element
725 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
726 (org-forward-paragraph)
727 (looking-at " Paragraph")))
729 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
730 (org-forward-paragraph)
731 (looking-at "Paragraph")))
732 ;; On an item, or a footnote definition, when the first line is
733 ;; empty, move to the first item.
735 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
736 (org-forward-paragraph)
737 (looking-at " Paragraph")))
739 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
740 (org-forward-paragraph)
741 (looking-at "Paragraph")))
742 ;; On a table (resp. a property drawer) do not move through table
743 ;; rows (resp. node properties).
745 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
746 (org-forward-paragraph)
747 (looking-at "Paragraph")))
749 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
750 (org-forward-paragraph)
751 (looking-at "Paragraph")))
752 ;; On a verse or source block, stop after blank lines.
754 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
755 (org-forward-paragraph)
758 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
759 (org-forward-paragraph)
762 (ert-deftest test-org
/backward-paragraph
()
763 "Test `org-backward-paragraph' specifications."
764 ;; Error at beginning of buffer.
766 (org-test-with-temp-text "Paragraph"
767 (org-backward-paragraph)))
770 (org-test-with-temp-text "P1\n\nP2\n\nP3"
771 (goto-char (point-max))
772 (org-backward-paragraph)
775 (org-test-with-temp-text "P1\n\nP2\n\nP3"
776 (goto-char (point-max))
778 (org-backward-paragraph)
782 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
783 (goto-char (point-max))
785 (org-backward-paragraph)
787 ;; Ignore invisible elements.
789 (org-test-with-temp-text "* H1\n P1\n* H2"
791 (goto-char (point-max))
793 (org-backward-paragraph)
795 ;; On an affiliated keyword, jump to the first one.
797 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
798 (search-forward "c2")
799 (org-backward-paragraph)
800 (looking-at "#\\+name")))
801 ;; On the second element in an item or a footnote definition, jump
802 ;; to item or the definition.
804 (org-test-with-temp-text "- line1\n\n line2"
805 (goto-char (point-max))
807 (org-backward-paragraph)
808 (looking-at "- line1")))
810 (org-test-with-temp-text "[fn:1] line1\n\n line2"
811 (goto-char (point-max))
813 (org-backward-paragraph)
814 (looking-at "\\[fn:1\\] line1")))
815 ;; On a table (resp. a property drawer), ignore table rows
816 ;; (resp. node properties).
818 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
819 (goto-char (point-max))
821 (org-backward-paragraph)
824 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
825 (goto-char (point-max))
827 (org-backward-paragraph)
829 ;; On a source or verse block, stop before blank lines.
831 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
832 (search-forward "L3")
834 (org-backward-paragraph)
837 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
838 (search-forward "L3")
840 (org-backward-paragraph)
843 (ert-deftest test-org
/forward-element
()
844 "Test `org-forward-element' specifications."
845 ;; 1. At EOB: should error.
846 (org-test-with-temp-text "Some text\n"
847 (goto-char (point-max))
848 (should-error (org-forward-element)))
849 ;; 2. Standard move: expected to ignore blank lines.
850 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
851 (org-forward-element)
852 (should (looking-at (regexp-quote "Second paragraph."))))
853 ;; 3. Headline tests.
854 (org-test-with-temp-text "
859 ;; 3.1. At an headline beginning: move to next headline at the
862 (org-forward-element)
863 (should (looking-at (regexp-quote "** Head 1.2")))
864 ;; 3.2. At an headline beginning: move to parent headline if no
865 ;; headline at the same level.
867 (org-forward-element)
868 (should (looking-at (regexp-quote "** Head 1.2"))))
869 ;; 4. Greater element tests.
870 (org-test-with-temp-text
871 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
872 ;; 4.1. At a greater element: expected to skip contents.
873 (org-forward-element)
874 (should (looking-at (regexp-quote "Outside.")))
875 ;; 4.2. At the end of greater element contents: expected to skip
876 ;; to the end of the greater element.
878 (org-forward-element)
879 (should (looking-at (regexp-quote "Outside."))))
881 (org-test-with-temp-text "
895 ;; 5.1. At list top point: expected to move to the element after
898 (org-forward-element)
899 (should (looking-at (regexp-quote "Outside.")))
900 ;; 5.2. Special case: at the first line of a sub-list, but not at
901 ;; beginning of line, move to next item.
904 (org-forward-element)
905 (should (looking-at "- item2"))
908 (org-forward-element)
909 (should (looking-at " - sub2"))
910 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
912 (org-forward-element)
913 (should (looking-at (regexp-quote " Inner paragraph.")))
914 ;; 5.4. At sub-list end: expected to move outside the sub-list.
916 (org-forward-element)
917 (should (looking-at (regexp-quote " Inner paragraph.")))
918 ;; 5.5. At an item: expected to move to next item, if any.
920 (org-forward-element)
921 (should (looking-at " - sub3"))))
923 (ert-deftest test-org
/backward-element
()
924 "Test `org-backward-element' specifications."
925 ;; 1. Should error at BOB.
926 (org-test-with-temp-text " \nParagraph."
927 (should-error (org-backward-element)))
928 ;; 2. Should move at BOB when called on the first element in buffer.
930 (org-test-with-temp-text "\n#+TITLE: test"
931 (progn (forward-line)
932 (org-backward-element)
934 ;; 3. Not at the beginning of an element: move at its beginning.
935 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
938 (org-backward-element)
939 (should (looking-at (regexp-quote "Paragraph2."))))
940 ;; 4. Headline tests.
941 (org-test-with-temp-text "
946 ;; 4.1. At an headline beginning: move to previous headline at the
949 (org-backward-element)
950 (should (looking-at (regexp-quote "** Head 1.1")))
951 ;; 4.2. At an headline beginning: move to parent headline if no
952 ;; headline at the same level.
954 (org-backward-element)
955 (should (looking-at (regexp-quote "* Head 1")))
956 ;; 4.3. At the first top-level headline: should error.
958 (should-error (org-backward-element)))
959 ;; 5. At beginning of first element inside a greater element:
960 ;; expected to move to greater element's beginning.
961 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
963 (org-backward-element)
964 (should (looking-at "#\\+BEGIN_CENTER")))
965 ;; 6. At the beginning of the first element in a section: should
966 ;; move back to headline, if any.
968 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
969 (progn (goto-char (point-max))
971 (org-backward-element)
972 (org-at-heading-p))))
974 (org-test-with-temp-text "
989 ;; 7.1. At beginning of sub-list: expected to move to the
990 ;; paragraph before it.
992 (org-backward-element)
993 (should (looking-at "item1"))
994 ;; 7.2. At an item in a list: expected to move at previous item.
996 (org-backward-element)
997 (should (looking-at " - sub2"))
999 (org-backward-element)
1000 (should (looking-at "- item1"))
1001 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
1004 (org-backward-element)
1005 (should (looking-at " - sub1"))
1007 (org-backward-element)
1008 (should (looking-at "- item1"))
1009 ;; 7.4. At blank-lines before list end: expected to move to top
1012 (org-backward-element)
1013 (should (looking-at "- item1"))))
1015 (ert-deftest test-org
/up-element
()
1016 "Test `org-up-element' specifications."
1017 ;; 1. At BOB or with no surrounding element: should error.
1018 (org-test-with-temp-text "Paragraph."
1019 (should-error (org-up-element)))
1020 (org-test-with-temp-text "* Head1\n* Head2"
1022 (should-error (org-up-element)))
1023 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
1025 (should-error (org-up-element)))
1026 ;; 2. At an headline: move to parent headline.
1027 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
1030 (should (looking-at "\\* Head1")))
1031 ;; 3. Inside a greater element: move to greater element beginning.
1032 (org-test-with-temp-text
1033 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
1036 (should (looking-at "#\\+BEGIN_CENTER")))
1038 (org-test-with-temp-text "* Top
1045 Paragraph within sub2.
1048 ;; 4.1. Within an item: move to the item beginning.
1051 (should (looking-at " - sub2"))
1052 ;; 4.2. At an item in a sub-list: move to parent item.
1055 (should (looking-at "- item1"))
1056 ;; 4.3. At an item in top list: move to beginning of whole list.
1059 (should (looking-at "- item1"))
1060 ;; 4.4. Special case. At very top point: should move to parent of
1064 (should (looking-at "\\* Top"))))
1066 (ert-deftest test-org
/down-element
()
1067 "Test `org-down-element' specifications."
1068 ;; Error when the element hasn't got a recursive type.
1069 (org-test-with-temp-text "Paragraph."
1070 (should-error (org-down-element)))
1071 ;; Error when the element has no contents
1072 (org-test-with-temp-text "* Headline"
1073 (should-error (org-down-element)))
1074 ;; When at a plain-list, move to first item.
1075 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
1078 (should (looking-at " - Item 1.1")))
1079 (org-test-with-temp-text "#+NAME: list\n- Item 1"
1081 (should (looking-at " Item 1")))
1082 ;; When at a table, move to first row
1083 (org-test-with-temp-text "#+NAME: table\n| a | b |"
1085 (should (looking-at " a | b |")))
1086 ;; Otherwise, move inside the greater element.
1087 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
1089 (should (looking-at "Paragraph"))))
1091 (ert-deftest test-org
/drag-element-backward
()
1092 "Test `org-drag-element-backward' specifications."
1093 ;; 1. Error when trying to move first element of buffer.
1094 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
1095 (should-error (org-drag-element-backward)))
1096 ;; 2. Error when trying to swap nested elements.
1097 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
1099 (should-error (org-drag-element-backward)))
1100 ;; 3. Error when trying to swap an headline element and
1101 ;; a non-headline element.
1102 (org-test-with-temp-text "Test.\n* Head 1"
1104 (should-error (org-drag-element-backward)))
1105 ;; 4. Otherwise, swap elements, preserving column and blank lines
1106 ;; between elements.
1107 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
1108 (search-forward "graph")
1109 (org-drag-element-backward)
1110 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
1111 (should (looking-at " 2")))
1112 ;; 5. Preserve visibility of elements and their contents.
1113 (org-test-with-temp-text "
1121 (while (search-forward "BEGIN_" nil t
) (org-cycle))
1122 (search-backward "- item 1")
1123 (org-drag-element-backward)
1126 '((63 .
82) (26 .
48))
1127 (mapcar (lambda (ov) (cons (overlay-start ov
) (overlay-end ov
)))
1128 (overlays-in (point-min) (point-max)))))))
1130 (ert-deftest test-org
/drag-element-forward
()
1131 "Test `org-drag-element-forward' specifications."
1132 ;; 1. Error when trying to move first element of buffer.
1133 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
1135 (should-error (org-drag-element-forward)))
1136 ;; 2. Error when trying to swap nested elements.
1137 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
1139 (should-error (org-drag-element-forward)))
1140 ;; 3. Error when trying to swap a non-headline element and an
1142 (org-test-with-temp-text "Test.\n* Head 1"
1143 (should-error (org-drag-element-forward)))
1144 ;; 4. Otherwise, swap elements, preserving column and blank lines
1145 ;; between elements.
1146 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
1147 (search-forward "graph")
1148 (org-drag-element-forward)
1149 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
1150 (should (looking-at " 1")))
1151 ;; 5. Preserve visibility of elements and their contents.
1152 (org-test-with-temp-text "
1160 (while (search-forward "BEGIN_" nil t
) (org-cycle))
1161 (search-backward "#+BEGIN_CENTER")
1162 (org-drag-element-forward)
1165 '((63 .
82) (26 .
48))
1166 (mapcar (lambda (ov) (cons (overlay-start ov
) (overlay-end ov
)))
1167 (overlays-in (point-min) (point-max)))))))
1173 (ert-deftest test-org
/timestamp-has-time-p
()
1174 "Test `org-timestamp-has-time-p' specifications."
1177 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
1178 (org-timestamp-has-time-p (org-element-context))))
1181 (org-test-with-temp-text "<2012-03-29 Thu>"
1182 (org-timestamp-has-time-p (org-element-context)))))
1184 (ert-deftest test-org
/timestamp-format
()
1185 "Test `org-timestamp-format' specifications."
1190 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
1191 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
1196 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
1197 (org-timestamp-format (org-element-context) "%Y-%m-%d" t
)))))
1199 (ert-deftest test-org
/timestamp-split-range
()
1200 "Test `org-timestamp-split-range' specifications."
1201 ;; Extract range start (active).
1204 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1205 (let ((ts (org-timestamp-split-range (org-element-context))))
1206 (mapcar (lambda (p) (org-element-property p ts
))
1207 '(:year-end
:month-end
:day-end
))))))
1208 ;; Extract range start (inactive)
1211 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1212 (let ((ts (org-timestamp-split-range (org-element-context))))
1213 (mapcar (lambda (p) (org-element-property p ts
))
1214 '(:year-end
:month-end
:day-end
))))))
1215 ;; Extract range end (active).
1218 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1219 (let ((ts (org-timestamp-split-range
1220 (org-element-context) t
)))
1221 (mapcar (lambda (p) (org-element-property p ts
))
1222 '(:year-end
:month-end
:day-end
))))))
1223 ;; Extract range end (inactive)
1226 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1227 (let ((ts (org-timestamp-split-range
1228 (org-element-context) t
)))
1229 (mapcar (lambda (p) (org-element-property p ts
))
1230 '(:year-end
:month-end
:day-end
))))))
1231 ;; Return the timestamp if not a range.
1233 (org-test-with-temp-text "[2012-03-29 Thu]"
1234 (let* ((ts-orig (org-element-context))
1235 (ts-copy (org-timestamp-split-range ts-orig
)))
1236 (eq ts-orig ts-copy
))))
1238 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1239 (let* ((ts-orig (org-element-context))
1240 (ts-copy (org-timestamp-split-range ts-orig
)))
1241 (eq ts-orig ts-copy
))))
1242 ;; Check that parent is the same when a range was split.
1244 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1245 (let* ((ts-orig (org-element-context))
1246 (ts-copy (org-timestamp-split-range ts-orig
)))
1247 (eq (org-element-property :parent ts-orig
)
1248 (org-element-property :parent ts-copy
))))))
1250 (ert-deftest test-org
/timestamp-translate
()
1251 "Test `org-timestamp-translate' specifications."
1252 ;; Translate whole date range.
1255 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1256 (let ((org-display-custom-times t
)
1257 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1258 (org-timestamp-translate (org-element-context))))))
1259 ;; Translate date range start.
1262 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1263 (let ((org-display-custom-times t
)
1264 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1265 (org-timestamp-translate (org-element-context) 'start
)))))
1266 ;; Translate date range end.
1269 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1270 (let ((org-display-custom-times t
)
1271 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1272 (org-timestamp-translate (org-element-context) 'end
)))))
1273 ;; Translate time range.
1276 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
1277 (let ((org-display-custom-times t
)
1278 (org-time-stamp-custom-formats '("<%d>" .
"<%H>")))
1279 (org-timestamp-translate (org-element-context))))))
1280 ;; Translate non-range timestamp.
1283 (org-test-with-temp-text "<2012-03-29 Thu>"
1284 (let ((org-display-custom-times t
)
1285 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1286 (org-timestamp-translate (org-element-context))))))
1287 ;; Do not change `diary' timestamps.
1289 (equal "<%%(org-float t 4 2)>"
1290 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1291 (let ((org-display-custom-times t
)
1292 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1293 (org-timestamp-translate (org-element-context)))))))
1297 ;;; Targets and Radio Targets
1299 (ert-deftest test-org
/all-targets
()
1300 "Test `org-all-targets' specifications."
1301 ;; Without an argument.
1303 (equal '("radio-target" "target")
1304 (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
1305 (org-all-targets))))
1307 (equal '("radio-target")
1308 (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
1311 (equal '("radio-target")
1312 (org-test-with-temp-text "<<target>> <<<radio-target>>>"
1313 (org-all-targets t
)))))
1318 (ert-deftest test-org
/flag-drawer
()
1319 "Test `org-flag-drawer' specifications."
1322 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
1324 (get-char-property (line-end-position) 'invisible
)))
1327 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
1329 (org-flag-drawer nil
)
1330 (get-char-property (line-end-position) 'invisible
)))
1331 ;; Test optional argument.
1333 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
1334 (let ((drawer (save-excursion (search-forward ":D2")
1335 (org-element-at-point))))
1336 (org-flag-drawer t drawer
)
1337 (get-char-property (progn (search-forward ":D2") (line-end-position))
1340 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
1341 (let ((drawer (save-excursion (search-forward ":D2")
1342 (org-element-at-point))))
1343 (org-flag-drawer t drawer
)
1344 (get-char-property (line-end-position) 'invisible
))))
1345 ;; Do not hide fake drawers.
1347 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
1350 (get-char-property (line-end-position) 'invisible
)))
1351 ;; Do not hide incomplete drawers.
1353 (org-test-with-temp-text ":D:\nparagraph"
1356 (get-char-property (line-end-position) 'invisible
))))
1361 ;;; test-org.el ends here