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
)
103 ;;; Date and time analysis
105 (ert-deftest test-org
/org-read-date
()
106 "Test `org-read-date' specifications."
107 ;; Parse ISO date with abbreviated year and month.
108 (should (equal "2012-03-29 16:40"
109 (let ((org-time-was-given t
))
110 (org-read-date t nil
"12-3-29 16:40"))))
111 ;; Parse Europeans dates.
112 (should (equal "2012-03-29 16:40"
113 (let ((org-time-was-given t
))
114 (org-read-date t nil
"29.03.2012 16:40"))))
115 ;; Parse Europeans dates without year.
116 (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
117 (let ((org-time-was-given t
))
118 (org-read-date t nil
"29.03. 16:40")))))
120 (ert-deftest test-org
/org-parse-time-string
()
121 "Test `org-parse-time-string'."
122 (should (equal (org-parse-time-string "2012-03-29 16:40")
123 '(0 40 16 29 3 2012 nil nil nil
)))
124 (should (equal (org-parse-time-string "[2012-03-29 16:40]")
125 '(0 40 16 29 3 2012 nil nil nil
)))
126 (should (equal (org-parse-time-string "<2012-03-29 16:40>")
127 '(0 40 16 29 3 2012 nil nil nil
)))
128 (should (equal (org-parse-time-string "<2012-03-29>")
129 '(0 0 0 29 3 2012 nil nil nil
)))
130 (should (equal (org-parse-time-string "<2012-03-29>" t
)
131 '(0 nil nil
29 3 2012 nil nil nil
))))
136 (ert-deftest test-org
/fill-paragraph
()
137 "Test `org-fill-paragraph' specifications."
138 ;; At an Org table, align it.
141 (org-test-with-temp-text "|a|"
145 (equal "#+name: table\n| a |\n"
146 (org-test-with-temp-text "#+name: table\n| a |"
149 ;; At a paragraph, preserve line breaks.
150 (org-test-with-temp-text "some \\\\\nlong\ntext"
151 (let ((fill-column 20))
153 (should (equal (buffer-string) "some \\\\\nlong text"))))
154 ;; Correctly fill a paragraph when point is at its very end.
157 (org-test-with-temp-text "A\nB"
158 (let ((fill-column 20))
159 (goto-char (point-max))
162 ;; Correctly fill the last paragraph of a greater element.
164 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
165 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
166 (let ((fill-column 8))
171 ;; Correctly fill an element in a narrowed buffer.
174 (org-test-with-temp-text "01234 6789"
175 (let ((fill-column 5))
176 (narrow-to-region 1 8)
179 ;; Handle `adaptive-fill-regexp' in paragraphs.
182 (org-test-with-temp-text "> a\n> b"
183 (let ((fill-column 5)
184 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
187 ;; Special case: Fill first paragraph when point is at an item or
188 ;; a plain-list or a footnote reference.
191 (org-test-with-temp-text "- A\n B"
192 (let ((fill-column 20))
197 (org-test-with-temp-text "[fn:1] A\nB"
198 (let ((fill-column 20))
201 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
202 (let ((fill-column 20))
204 (should (equal (buffer-string)
205 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
206 ;; Fill contents of `comment-block' elements.
209 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
210 (let ((fill-column 20))
214 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
215 ;; Fill `comment' elements.
218 (org-test-with-temp-text " # A\n # B"
219 (let ((fill-column 20))
222 ;; Do not mix consecutive comments when filling one of them.
224 (equal "# A B\n\n# C"
225 (org-test-with-temp-text "# A\n# B\n\n# C"
226 (let ((fill-column 20))
229 ;; Use commented empty lines as separators when filling comments.
231 (equal "# A B\n#\n# C"
232 (org-test-with-temp-text "# A\n# B\n#\n# C"
233 (let ((fill-column 20))
236 ;; Handle `adaptive-fill-regexp' in comments.
239 (org-test-with-temp-text "# > a\n# > b"
240 (let ((fill-column 20)
241 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
244 ;; Do nothing at affiliated keywords.
245 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
246 (let ((fill-column 20))
248 (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
249 ;; Do not move point after table when filling a table.
251 (org-test-with-temp-text "| a | b |\n| c | d |\n"
256 (ert-deftest test-org
/auto-fill-function
()
257 "Test auto-filling features."
258 ;; Auto fill paragraph.
261 (org-test-with-temp-text "12345 7890"
262 (let ((fill-column 5))
264 (org-auto-fill-function)
266 ;; Auto fill first paragraph in an item.
268 (equal "- 12345\n 7890"
269 (org-test-with-temp-text "- 12345 7890"
270 (let ((fill-column 7))
272 (org-auto-fill-function)
274 ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
276 (equal "> 12345\n 7890"
277 (org-test-with-temp-text "> 12345 7890"
278 (let ((fill-column 10)
279 (adaptive-fill-regexp "[ \t]*>+[ \t]*")
280 (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
282 (org-auto-fill-function)
285 (equal "> 12345\n> 12345\n> 7890"
286 (org-test-with-temp-text "> 12345\n> 12345 7890"
287 (let ((fill-column 10)
288 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
289 (goto-char (point-max))
290 (org-auto-fill-function)
293 (equal " 12345\n *12345\n *12345"
294 (org-test-with-temp-text " 12345\n *12345 12345"
295 (let ((fill-column 10)
296 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
297 (goto-char (point-max))
298 (org-auto-fill-function)
300 ;; Auto fill comments.
302 (equal " # 12345\n # 7890"
303 (org-test-with-temp-text " # 12345 7890"
304 (let ((fill-column 10))
306 (org-auto-fill-function)
308 ;; A hash within a line isn't a comment.
310 (equal "12345 # 7890\n# 1"
311 (org-test-with-temp-text "12345 # 7890 1"
312 (let ((fill-column 12))
314 (org-auto-fill-function)
316 ;; Correctly interpret empty prefix.
318 (equal "# a\n# b\nRegular\n# paragraph"
319 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
320 (let ((fill-column 12))
322 (org-auto-fill-function)
324 ;; Comment block: auto fill contents.
326 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
327 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
328 (let ((fill-column 5))
331 (org-auto-fill-function)
334 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
335 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
336 (let ((fill-column 5))
339 (org-auto-fill-function)
341 ;; Do not fill if a new item could be created.
344 (org-test-with-temp-text "12345 - 90"
345 (let ((fill-column 5))
347 (org-auto-fill-function)
349 ;; Do not fill if a line break could be introduced.
351 (equal "123\\\\\n7890"
352 (org-test-with-temp-text "123\\\\ 7890"
353 (let ((fill-column 6))
355 (org-auto-fill-function)
357 ;; Do not fill affiliated keywords.
359 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
360 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
361 (let ((fill-column 20))
363 (org-auto-fill-function)
372 (ert-deftest test-org
/meta-return
()
373 "Test M-RET (`org-meta-return')."
374 ;; In a table field insert a row above.
376 (org-test-with-temp-text "| a |"
380 (looking-at "| |$")))
381 ;; In a paragraph change current line into a header.
383 (org-test-with-temp-text "a"
386 (looking-at "\* a$")))
387 ;; In an item insert an item, in this case above.
389 (org-test-with-temp-text "- a"
393 ;; In a drawer and item insert an item, in this case above.
395 (let ((org-drawers '("MYDRAWER")))
396 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
400 (looking-at "- $")))))
402 (ert-deftest test-org
/insert-heading
()
403 "Test `org-insert-heading' specifications."
404 ;; FIXME: Test coverage is incomplete yet.
406 ;; In an empty buffer, insert a new headline.
409 (org-test-with-temp-text ""
412 ;; At the beginning of a line, turn it into a headline
415 (org-test-with-temp-text "<point>P"
418 ;; In the middle of a line, split the line if allowed, otherwise,
419 ;; insert the headline at its end.
421 (equal "Para\n* graph"
422 (org-test-with-temp-text "Para<point>graph"
423 (let ((org-M-RET-may-split-line '((default . t
))))
424 (org-insert-heading))
427 (equal "Paragraph\n* "
428 (org-test-with-temp-text "Para<point>graph"
429 (let ((org-M-RET-may-split-line '((default . nil
))))
430 (org-insert-heading))
433 (ert-deftest test-org
/insert-todo-heading-respect-content
()
434 "Test `org-insert-todo-heading-respect-content' specifications."
435 ;; Create a TODO heading.
437 (org-test-with-temp-text "* H1\n Body"
438 (org-insert-todo-heading-respect-content)
439 (nth 2 (org-heading-components))))
440 ;; Add headline at the end of the first subtree
442 (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
443 (search-forward "H1Body")
444 (org-insert-todo-heading-respect-content)
445 (and (eobp) (org-at-heading-p))))
446 ;; In a list, do not create a new item.
448 (org-test-with-temp-text "* H\n- an item\n- another one"
449 (search-forward "an ")
450 (org-insert-todo-heading-respect-content)
451 (and (eobp) (org-at-heading-p)))))
459 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
460 ;; a named element (#+name: text) and to headlines (* Text).
462 (ert-deftest test-org
/fuzzy-links
()
463 "Test fuzzy links specifications."
464 ;; 1. Fuzzy link goes in priority to a matching target.
466 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
469 (looking-at "<<Test>>")))
470 ;; 2. Then fuzzy link points to an element with a given name.
472 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
475 (looking-at "#\\+NAME: Test")))
476 ;; 3. A target still lead to a matching headline otherwise.
478 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
481 (looking-at "\\* Head2")))
482 ;; 4. With a leading star in link, enforce heading match.
484 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
487 (looking-at "\\* Test"))))
492 (ert-deftest test-org
/org-link-escape-ascii-character
()
493 "Escape an ascii character."
497 (org-link-escape "["))))
499 (ert-deftest test-org
/org-link-escape-ascii-ctrl-character
()
500 "Escape an ascii control character."
504 (org-link-escape "\t"))))
506 (ert-deftest test-org
/org-link-escape-multibyte-character
()
507 "Escape an unicode multibyte character."
511 (org-link-escape "€"))))
513 (ert-deftest test-org
/org-link-escape-custom-table
()
514 "Escape string with custom character table."
518 (org-link-escape "Foo:Bar\n" '(?\
: ?\B
)))))
520 (ert-deftest test-org
/org-link-escape-custom-table-merge
()
521 "Escape string with custom table merged with default table."
524 "%5BF%6F%6F%3A%42ar%0A%5D"
525 (org-link-escape "[Foo:Bar\n]" '(?\
: ?\B ?\o
) t
))))
527 (ert-deftest test-org
/org-link-unescape-ascii-character
()
528 "Unescape an ascii character."
532 (org-link-unescape "%5B"))))
534 (ert-deftest test-org
/org-link-unescape-ascii-ctrl-character
()
535 "Unescpae an ascii control character."
539 (org-link-unescape "%0A"))))
541 (ert-deftest test-org
/org-link-unescape-multibyte-character
()
542 "Unescape unicode multibyte character."
546 (org-link-unescape "%E2%82%AC"))))
548 (ert-deftest test-org
/org-link-unescape-ascii-extended-char
()
549 "Unescape old style percent escaped character."
552 "à âçèéêîôùû"
553 (decode-coding-string
554 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1
))))
556 (ert-deftest test-org
/org-link-escape-url-with-escaped-char
()
557 "Escape and unescape a URL that includes an escaped char.
558 http://article.gmane.org/gmane.emacs.orgmode/21459/"
561 "http://some.host.com/form?&id=blah%2Bblah25"
563 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
565 (ert-deftest test-org
/org-link-escape-chars-browser
()
566 "Escape a URL to pass to `browse-url'."
569 "http://some.host.com/search?q=%22Org%20mode%22"
570 (org-link-escape "http://some.host.com/search?q=\"Org mode\""
571 org-link-escape-chars-browser
))))
577 (ert-deftest test-org
/accumulated-properties-in-drawers
()
578 "Ensure properties accumulate in subtree drawers."
579 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
580 (org-babel-next-src-block)
581 (should (equal '(2 1) (org-babel-execute-src-block)))))
587 (ert-deftest test-org
/mark-subtree
()
588 "Test `org-mark-subtree' specifications."
589 ;; Error when point is before first headline.
591 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
592 (progn (transient-mark-mode 1)
593 (org-mark-subtree))))
594 ;; Without argument, mark current subtree.
598 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
599 (progn (transient-mark-mode 1)
602 (list (region-beginning) (region-end))))))
603 ;; With an argument, move ARG up.
607 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
608 (progn (transient-mark-mode 1)
611 (list (region-beginning) (region-end))))))
612 ;; Do not get fooled by inlinetasks.
613 (when (featurep 'org-inlinetask
)
616 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
617 (progn (transient-mark-mode 1)
619 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
620 (region-beginning)))))))
626 (ert-deftest test-org
/beginning-of-line
()
627 "Test `org-beginning-of-line' specifications."
630 (org-test-with-temp-text "Some text\nSome other text"
631 (progn (org-beginning-of-line) (bolp))))
632 ;; Standard test with `visual-line-mode'.
634 (org-test-with-temp-text "A long line of text\nSome other text"
635 (progn (visual-line-mode)
637 (dotimes (i 1000) (insert "very "))
638 (org-beginning-of-line)
640 ;; At an headline with special movement.
642 (org-test-with-temp-text "* TODO Headline"
643 (let ((org-special-ctrl-a/e t
))
645 (and (progn (org-beginning-of-line) (looking-at "Headline"))
646 (progn (org-beginning-of-line) (bolp))
647 (progn (org-beginning-of-line) (looking-at "Headline")))))))
649 (ert-deftest test-org
/end-of-line
()
650 "Test `org-end-of-line' specifications."
653 (org-test-with-temp-text "Some text\nSome other text"
654 (progn (org-end-of-line) (eolp))))
655 ;; Standard test with `visual-line-mode'.
657 (org-test-with-temp-text "A long line of text\nSome other text"
658 (progn (visual-line-mode)
660 (dotimes (i 1000) (insert "very "))
661 (goto-char (point-min))
664 ;; At an headline with special movement.
666 (org-test-with-temp-text "* Headline1 :tag:\n"
667 (let ((org-special-ctrl-a/e t
))
668 (and (progn (org-end-of-line) (looking-at " :tag:"))
669 (progn (org-end-of-line) (eolp))
670 (progn (org-end-of-line) (looking-at " :tag:"))))))
671 ;; At an headline without special movement.
673 (org-test-with-temp-text "* Headline2 :tag:\n"
674 (let ((org-special-ctrl-a/e nil
))
675 (and (progn (org-end-of-line) (eolp))
676 (progn (org-end-of-line) (eolp))))))
677 ;; At an headline, with reversed movement.
679 (org-test-with-temp-text "* Headline3 :tag:\n"
680 (let ((org-special-ctrl-a/e
'reversed
)
681 (this-command last-command
))
682 (and (progn (org-end-of-line) (eolp))
683 (progn (org-end-of-line) (looking-at " :tag:"))))))
684 ;; At a block without hidden contents.
686 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
687 (progn (org-end-of-line) (eolp))))
688 ;; At a block with hidden contents.
690 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
691 (let ((org-special-ctrl-a/e t
))
692 (org-hide-block-toggle)
696 (ert-deftest test-org
/forward-paragraph
()
697 "Test `org-forward-paragraph' specifications."
698 ;; At end of buffer, return an error.
700 (org-test-with-temp-text "Paragraph"
701 (goto-char (point-max))
702 (org-forward-paragraph)))
705 (org-test-with-temp-text "P1\n\nP2\n\nP3"
706 (org-forward-paragraph)
710 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
711 (org-forward-paragraph)
713 ;; Do not enter elements with invisible contents.
715 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
716 (org-hide-block-toggle)
717 (org-forward-paragraph)
719 ;; On an affiliated keyword, jump to the beginning of the element.
721 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
722 (org-forward-paragraph)
723 (looking-at "Para")))
724 ;; On an item or a footnote definition, move to the second element
727 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
728 (org-forward-paragraph)
729 (looking-at " Paragraph")))
731 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
732 (org-forward-paragraph)
733 (looking-at "Paragraph")))
734 ;; On an item, or a footnote definition, when the first line is
735 ;; empty, move to the first item.
737 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
738 (org-forward-paragraph)
739 (looking-at " Paragraph")))
741 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
742 (org-forward-paragraph)
743 (looking-at "Paragraph")))
744 ;; On a table (resp. a property drawer) do not move through table
745 ;; rows (resp. node properties).
747 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
748 (org-forward-paragraph)
749 (looking-at "Paragraph")))
751 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
752 (org-forward-paragraph)
753 (looking-at "Paragraph")))
754 ;; On a verse or source block, stop after blank lines.
756 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
757 (org-forward-paragraph)
760 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
761 (org-forward-paragraph)
764 (ert-deftest test-org
/backward-paragraph
()
765 "Test `org-backward-paragraph' specifications."
766 ;; Error at beginning of buffer.
768 (org-test-with-temp-text "Paragraph"
769 (org-backward-paragraph)))
772 (org-test-with-temp-text "P1\n\nP2\n\nP3"
773 (goto-char (point-max))
774 (org-backward-paragraph)
777 (org-test-with-temp-text "P1\n\nP2\n\nP3"
778 (goto-char (point-max))
780 (org-backward-paragraph)
784 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
785 (goto-char (point-max))
787 (org-backward-paragraph)
789 ;; Ignore invisible elements.
791 (org-test-with-temp-text "* H1\n P1\n* H2"
793 (goto-char (point-max))
795 (org-backward-paragraph)
797 ;; On an affiliated keyword, jump to the first one.
799 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
800 (search-forward "c2")
801 (org-backward-paragraph)
802 (looking-at "#\\+name")))
803 ;; On the second element in an item or a footnote definition, jump
804 ;; to item or the definition.
806 (org-test-with-temp-text "- line1\n\n line2"
807 (goto-char (point-max))
809 (org-backward-paragraph)
810 (looking-at "- line1")))
812 (org-test-with-temp-text "[fn:1] line1\n\n line2"
813 (goto-char (point-max))
815 (org-backward-paragraph)
816 (looking-at "\\[fn:1\\] line1")))
817 ;; On a table (resp. a property drawer), ignore table rows
818 ;; (resp. node properties).
820 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
821 (goto-char (point-max))
823 (org-backward-paragraph)
826 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
827 (goto-char (point-max))
829 (org-backward-paragraph)
831 ;; On a source or verse block, stop before blank lines.
833 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
834 (search-forward "L3")
836 (org-backward-paragraph)
839 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
840 (search-forward "L3")
842 (org-backward-paragraph)
845 (ert-deftest test-org
/forward-element
()
846 "Test `org-forward-element' specifications."
847 ;; 1. At EOB: should error.
848 (org-test-with-temp-text "Some text\n"
849 (goto-char (point-max))
850 (should-error (org-forward-element)))
851 ;; 2. Standard move: expected to ignore blank lines.
852 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
853 (org-forward-element)
854 (should (looking-at (regexp-quote "Second paragraph."))))
855 ;; 3. Headline tests.
856 (org-test-with-temp-text "
861 ;; 3.1. At an headline beginning: move to next headline at the
864 (org-forward-element)
865 (should (looking-at (regexp-quote "** Head 1.2")))
866 ;; 3.2. At an headline beginning: move to parent headline if no
867 ;; headline at the same level.
869 (org-forward-element)
870 (should (looking-at (regexp-quote "** Head 1.2"))))
871 ;; 4. Greater element tests.
872 (org-test-with-temp-text
873 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
874 ;; 4.1. At a greater element: expected to skip contents.
875 (org-forward-element)
876 (should (looking-at (regexp-quote "Outside.")))
877 ;; 4.2. At the end of greater element contents: expected to skip
878 ;; to the end of the greater element.
880 (org-forward-element)
881 (should (looking-at (regexp-quote "Outside."))))
883 (org-test-with-temp-text "
897 ;; 5.1. At list top point: expected to move to the element after
900 (org-forward-element)
901 (should (looking-at (regexp-quote "Outside.")))
902 ;; 5.2. Special case: at the first line of a sub-list, but not at
903 ;; beginning of line, move to next item.
906 (org-forward-element)
907 (should (looking-at "- item2"))
910 (org-forward-element)
911 (should (looking-at " - sub2"))
912 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
914 (org-forward-element)
915 (should (looking-at (regexp-quote " Inner paragraph.")))
916 ;; 5.4. At sub-list end: expected to move outside the sub-list.
918 (org-forward-element)
919 (should (looking-at (regexp-quote " Inner paragraph.")))
920 ;; 5.5. At an item: expected to move to next item, if any.
922 (org-forward-element)
923 (should (looking-at " - sub3"))))
925 (ert-deftest test-org
/backward-element
()
926 "Test `org-backward-element' specifications."
927 ;; 1. Should error at BOB.
928 (org-test-with-temp-text " \nParagraph."
929 (should-error (org-backward-element)))
930 ;; 2. Should move at BOB when called on the first element in buffer.
932 (org-test-with-temp-text "\n#+TITLE: test"
933 (progn (forward-line)
934 (org-backward-element)
936 ;; 3. Not at the beginning of an element: move at its beginning.
937 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
940 (org-backward-element)
941 (should (looking-at (regexp-quote "Paragraph2."))))
942 ;; 4. Headline tests.
943 (org-test-with-temp-text "
948 ;; 4.1. At an headline beginning: move to previous headline at the
951 (org-backward-element)
952 (should (looking-at (regexp-quote "** Head 1.1")))
953 ;; 4.2. At an headline beginning: move to parent headline if no
954 ;; headline at the same level.
956 (org-backward-element)
957 (should (looking-at (regexp-quote "* Head 1")))
958 ;; 4.3. At the first top-level headline: should error.
960 (should-error (org-backward-element)))
961 ;; 5. At beginning of first element inside a greater element:
962 ;; expected to move to greater element's beginning.
963 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
965 (org-backward-element)
966 (should (looking-at "#\\+BEGIN_CENTER")))
967 ;; 6. At the beginning of the first element in a section: should
968 ;; move back to headline, if any.
970 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
971 (progn (goto-char (point-max))
973 (org-backward-element)
974 (org-at-heading-p))))
976 (org-test-with-temp-text "
991 ;; 7.1. At beginning of sub-list: expected to move to the
992 ;; paragraph before it.
994 (org-backward-element)
995 (should (looking-at "item1"))
996 ;; 7.2. At an item in a list: expected to move at previous item.
998 (org-backward-element)
999 (should (looking-at " - sub2"))
1001 (org-backward-element)
1002 (should (looking-at "- item1"))
1003 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
1006 (org-backward-element)
1007 (should (looking-at " - sub1"))
1009 (org-backward-element)
1010 (should (looking-at "- item1"))
1011 ;; 7.4. At blank-lines before list end: expected to move to top
1014 (org-backward-element)
1015 (should (looking-at "- item1"))))
1017 (ert-deftest test-org
/up-element
()
1018 "Test `org-up-element' specifications."
1019 ;; 1. At BOB or with no surrounding element: should error.
1020 (org-test-with-temp-text "Paragraph."
1021 (should-error (org-up-element)))
1022 (org-test-with-temp-text "* Head1\n* Head2"
1024 (should-error (org-up-element)))
1025 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
1027 (should-error (org-up-element)))
1028 ;; 2. At an headline: move to parent headline.
1029 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
1032 (should (looking-at "\\* Head1")))
1033 ;; 3. Inside a greater element: move to greater element beginning.
1034 (org-test-with-temp-text
1035 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
1038 (should (looking-at "#\\+BEGIN_CENTER")))
1040 (org-test-with-temp-text "* Top
1047 Paragraph within sub2.
1050 ;; 4.1. Within an item: move to the item beginning.
1053 (should (looking-at " - sub2"))
1054 ;; 4.2. At an item in a sub-list: move to parent item.
1057 (should (looking-at "- item1"))
1058 ;; 4.3. At an item in top list: move to beginning of whole list.
1061 (should (looking-at "- item1"))
1062 ;; 4.4. Special case. At very top point: should move to parent of
1066 (should (looking-at "\\* Top"))))
1068 (ert-deftest test-org
/down-element
()
1069 "Test `org-down-element' specifications."
1070 ;; Error when the element hasn't got a recursive type.
1071 (org-test-with-temp-text "Paragraph."
1072 (should-error (org-down-element)))
1073 ;; Error when the element has no contents
1074 (org-test-with-temp-text "* Headline"
1075 (should-error (org-down-element)))
1076 ;; When at a plain-list, move to first item.
1077 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
1080 (should (looking-at " - Item 1.1")))
1081 (org-test-with-temp-text "#+NAME: list\n- Item 1"
1083 (should (looking-at " Item 1")))
1084 ;; When at a table, move to first row
1085 (org-test-with-temp-text "#+NAME: table\n| a | b |"
1087 (should (looking-at " a | b |")))
1088 ;; Otherwise, move inside the greater element.
1089 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
1091 (should (looking-at "Paragraph"))))
1093 (ert-deftest test-org
/drag-element-backward
()
1094 "Test `org-drag-element-backward' specifications."
1095 ;; 1. Error when trying to move first element of buffer.
1096 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
1097 (should-error (org-drag-element-backward)))
1098 ;; 2. Error when trying to swap nested elements.
1099 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
1101 (should-error (org-drag-element-backward)))
1102 ;; 3. Error when trying to swap an headline element and
1103 ;; a non-headline element.
1104 (org-test-with-temp-text "Test.\n* Head 1"
1106 (should-error (org-drag-element-backward)))
1107 ;; 4. Otherwise, swap elements, preserving column and blank lines
1108 ;; between elements.
1109 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
1110 (search-forward "graph")
1111 (org-drag-element-backward)
1112 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
1113 (should (looking-at " 2")))
1114 ;; 5. Preserve visibility of elements and their contents.
1115 (org-test-with-temp-text "
1123 (while (search-forward "BEGIN_" nil t
) (org-cycle))
1124 (search-backward "- item 1")
1125 (org-drag-element-backward)
1128 '((63 .
82) (26 .
48))
1129 (mapcar (lambda (ov) (cons (overlay-start ov
) (overlay-end ov
)))
1130 (overlays-in (point-min) (point-max)))))))
1132 (ert-deftest test-org
/drag-element-forward
()
1133 "Test `org-drag-element-forward' specifications."
1134 ;; 1. Error when trying to move first element of buffer.
1135 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
1137 (should-error (org-drag-element-forward)))
1138 ;; 2. Error when trying to swap nested elements.
1139 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
1141 (should-error (org-drag-element-forward)))
1142 ;; 3. Error when trying to swap a non-headline element and an
1144 (org-test-with-temp-text "Test.\n* Head 1"
1145 (should-error (org-drag-element-forward)))
1146 ;; 4. Otherwise, swap elements, preserving column and blank lines
1147 ;; between elements.
1148 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
1149 (search-forward "graph")
1150 (org-drag-element-forward)
1151 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
1152 (should (looking-at " 1")))
1153 ;; 5. Preserve visibility of elements and their contents.
1154 (org-test-with-temp-text "
1162 (while (search-forward "BEGIN_" nil t
) (org-cycle))
1163 (search-backward "#+BEGIN_CENTER")
1164 (org-drag-element-forward)
1167 '((63 .
82) (26 .
48))
1168 (mapcar (lambda (ov) (cons (overlay-start ov
) (overlay-end ov
)))
1169 (overlays-in (point-min) (point-max)))))))
1175 (ert-deftest test-org
/timestamp-has-time-p
()
1176 "Test `org-timestamp-has-time-p' specifications."
1179 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
1180 (org-timestamp-has-time-p (org-element-context))))
1183 (org-test-with-temp-text "<2012-03-29 Thu>"
1184 (org-timestamp-has-time-p (org-element-context)))))
1186 (ert-deftest test-org
/timestamp-format
()
1187 "Test `org-timestamp-format' specifications."
1192 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
1193 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
1198 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
1199 (org-timestamp-format (org-element-context) "%Y-%m-%d" t
)))))
1201 (ert-deftest test-org
/timestamp-split-range
()
1202 "Test `org-timestamp-split-range' specifications."
1203 ;; Extract range start (active).
1206 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1207 (let ((ts (org-timestamp-split-range (org-element-context))))
1208 (mapcar (lambda (p) (org-element-property p ts
))
1209 '(:year-end
:month-end
:day-end
))))))
1210 ;; Extract range start (inactive)
1213 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1214 (let ((ts (org-timestamp-split-range (org-element-context))))
1215 (mapcar (lambda (p) (org-element-property p ts
))
1216 '(:year-end
:month-end
:day-end
))))))
1217 ;; Extract range end (active).
1220 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1221 (let ((ts (org-timestamp-split-range
1222 (org-element-context) t
)))
1223 (mapcar (lambda (p) (org-element-property p ts
))
1224 '(:year-end
:month-end
:day-end
))))))
1225 ;; Extract range end (inactive)
1228 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1229 (let ((ts (org-timestamp-split-range
1230 (org-element-context) t
)))
1231 (mapcar (lambda (p) (org-element-property p ts
))
1232 '(:year-end
:month-end
:day-end
))))))
1233 ;; Return the timestamp if not a range.
1235 (org-test-with-temp-text "[2012-03-29 Thu]"
1236 (let* ((ts-orig (org-element-context))
1237 (ts-copy (org-timestamp-split-range ts-orig
)))
1238 (eq ts-orig ts-copy
))))
1240 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1241 (let* ((ts-orig (org-element-context))
1242 (ts-copy (org-timestamp-split-range ts-orig
)))
1243 (eq ts-orig ts-copy
))))
1244 ;; Check that parent is the same when a range was split.
1246 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1247 (let* ((ts-orig (org-element-context))
1248 (ts-copy (org-timestamp-split-range ts-orig
)))
1249 (eq (org-element-property :parent ts-orig
)
1250 (org-element-property :parent ts-copy
))))))
1252 (ert-deftest test-org
/timestamp-translate
()
1253 "Test `org-timestamp-translate' specifications."
1254 ;; Translate whole date range.
1257 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1258 (let ((org-display-custom-times t
)
1259 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1260 (org-timestamp-translate (org-element-context))))))
1261 ;; Translate date range start.
1264 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1265 (let ((org-display-custom-times t
)
1266 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1267 (org-timestamp-translate (org-element-context) 'start
)))))
1268 ;; Translate date range end.
1271 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1272 (let ((org-display-custom-times t
)
1273 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1274 (org-timestamp-translate (org-element-context) 'end
)))))
1275 ;; Translate time range.
1278 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
1279 (let ((org-display-custom-times t
)
1280 (org-time-stamp-custom-formats '("<%d>" .
"<%H>")))
1281 (org-timestamp-translate (org-element-context))))))
1282 ;; Translate non-range timestamp.
1285 (org-test-with-temp-text "<2012-03-29 Thu>"
1286 (let ((org-display-custom-times t
)
1287 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1288 (org-timestamp-translate (org-element-context))))))
1289 ;; Do not change `diary' timestamps.
1291 (equal "<%%(org-float t 4 2)>"
1292 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1293 (let ((org-display-custom-times t
)
1294 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1295 (org-timestamp-translate (org-element-context)))))))
1299 ;;; Targets and Radio Targets
1301 (ert-deftest test-org
/all-targets
()
1302 "Test `org-all-targets' specifications."
1303 ;; Without an argument.
1305 (equal '("radio-target" "target")
1306 (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
1307 (org-all-targets))))
1309 (equal '("radio-target")
1310 (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
1313 (equal '("radio-target")
1314 (org-test-with-temp-text "<<target>> <<<radio-target>>>"
1315 (org-all-targets t
)))))
1320 ;;; test-org.el ends here