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 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
373 ;; a named element (#+name: text) and to headlines (* Text).
375 (ert-deftest test-org
/fuzzy-links
()
376 "Test fuzzy links specifications."
377 ;; 1. Fuzzy link goes in priority to a matching target.
379 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
382 (looking-at "<<Test>>")))
383 ;; 2. Then fuzzy link points to an element with a given name.
385 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
388 (looking-at "#\\+NAME: Test")))
389 ;; 3. A target still lead to a matching headline otherwise.
391 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
394 (looking-at "\\* Head2")))
395 ;; 4. With a leading star in link, enforce heading match.
397 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
400 (looking-at "\\* Test"))))
405 (ert-deftest test-org
/org-link-escape-ascii-character
()
406 "Escape an ascii character."
410 (org-link-escape "["))))
412 (ert-deftest test-org
/org-link-escape-ascii-ctrl-character
()
413 "Escape an ascii control character."
417 (org-link-escape "\t"))))
419 (ert-deftest test-org
/org-link-escape-multibyte-character
()
420 "Escape an unicode multibyte character."
424 (org-link-escape "€"))))
426 (ert-deftest test-org
/org-link-escape-custom-table
()
427 "Escape string with custom character table."
431 (org-link-escape "Foo:Bar\n" '(?\
: ?\B
)))))
433 (ert-deftest test-org
/org-link-escape-custom-table-merge
()
434 "Escape string with custom table merged with default table."
437 "%5BF%6F%6F%3A%42ar%0A%5D"
438 (org-link-escape "[Foo:Bar\n]" '(?\
: ?\B ?\o
) t
))))
440 (ert-deftest test-org
/org-link-unescape-ascii-character
()
441 "Unescape an ascii character."
445 (org-link-unescape "%5B"))))
447 (ert-deftest test-org
/org-link-unescape-ascii-ctrl-character
()
448 "Unescpae an ascii control character."
452 (org-link-unescape "%0A"))))
454 (ert-deftest test-org
/org-link-unescape-multibyte-character
()
455 "Unescape unicode multibyte character."
459 (org-link-unescape "%E2%82%AC"))))
461 (ert-deftest test-org
/org-link-unescape-ascii-extended-char
()
462 "Unescape old style percent escaped character."
465 "à âçèéêîôùû"
466 (decode-coding-string
467 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1
))))
469 (ert-deftest test-org
/org-link-escape-url-with-escaped-char
()
470 "Escape and unescape a URL that includes an escaped char.
471 http://article.gmane.org/gmane.emacs.orgmode/21459/"
474 "http://some.host.com/form?&id=blah%2Bblah25"
476 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
478 (ert-deftest test-org
/org-link-escape-chars-browser
()
479 "Escape a URL to pass to `browse-url'."
482 "http://some.host.com/search?q=%22Org%20mode%22"
483 (org-link-escape "http://some.host.com/search?q=\"Org mode\""
484 org-link-escape-chars-browser
))))
490 (ert-deftest test-org
/accumulated-properties-in-drawers
()
491 "Ensure properties accumulate in subtree drawers."
492 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
493 (org-babel-next-src-block)
494 (should (equal '(2 1) (org-babel-execute-src-block)))))
500 (ert-deftest test-org
/mark-subtree
()
501 "Test `org-mark-subtree' specifications."
502 ;; Error when point is before first headline.
504 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
505 (progn (transient-mark-mode 1)
506 (org-mark-subtree))))
507 ;; Without argument, mark current subtree.
511 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
512 (progn (transient-mark-mode 1)
515 (list (region-beginning) (region-end))))))
516 ;; With an argument, move ARG up.
520 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
521 (progn (transient-mark-mode 1)
524 (list (region-beginning) (region-end))))))
525 ;; Do not get fooled by inlinetasks.
526 (when (featurep 'org-inlinetask
)
529 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
530 (progn (transient-mark-mode 1)
532 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
533 (region-beginning)))))))
539 (ert-deftest test-org
/beginning-of-line
()
540 "Test `org-beginning-of-line' specifications."
543 (org-test-with-temp-text "Some text\nSome other text"
544 (progn (org-beginning-of-line) (bolp))))
545 ;; Standard test with `visual-line-mode'.
547 (org-test-with-temp-text "A long line of text\nSome other text"
548 (progn (visual-line-mode)
550 (dotimes (i 1000) (insert "very "))
551 (org-beginning-of-line)
553 ;; At an headline with special movement.
555 (org-test-with-temp-text "* TODO Headline"
556 (let ((org-special-ctrl-a/e t
))
558 (and (progn (org-beginning-of-line) (looking-at "Headline"))
559 (progn (org-beginning-of-line) (bolp))
560 (progn (org-beginning-of-line) (looking-at "Headline")))))))
562 (ert-deftest test-org
/end-of-line
()
563 "Test `org-end-of-line' specifications."
566 (org-test-with-temp-text "Some text\nSome other text"
567 (progn (org-end-of-line) (eolp))))
568 ;; Standard test with `visual-line-mode'.
570 (org-test-with-temp-text "A long line of text\nSome other text"
571 (progn (visual-line-mode)
573 (dotimes (i 1000) (insert "very "))
574 (goto-char (point-min))
577 ;; At an headline with special movement.
579 (org-test-with-temp-text "* Headline1 :tag:\n"
580 (let ((org-special-ctrl-a/e t
))
581 (and (progn (org-end-of-line) (looking-at " :tag:"))
582 (progn (org-end-of-line) (eolp))
583 (progn (org-end-of-line) (looking-at " :tag:"))))))
584 ;; At an headline without special movement.
586 (org-test-with-temp-text "* Headline2 :tag:\n"
587 (let ((org-special-ctrl-a/e nil
))
588 (and (progn (org-end-of-line) (eolp))
589 (progn (org-end-of-line) (eolp))))))
590 ;; At an headline, with reversed movement.
592 (org-test-with-temp-text "* Headline3 :tag:\n"
593 (let ((org-special-ctrl-a/e
'reversed
)
594 (this-command last-command
))
595 (and (progn (org-end-of-line) (eolp))
596 (progn (org-end-of-line) (looking-at " :tag:"))))))
597 ;; At a block without hidden contents.
599 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
600 (progn (org-end-of-line) (eolp))))
601 ;; At a block with hidden contents.
603 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
604 (let ((org-special-ctrl-a/e t
))
605 (org-hide-block-toggle)
609 (ert-deftest test-org
/forward-element
()
610 "Test `org-forward-element' specifications."
611 ;; 1. At EOB: should error.
612 (org-test-with-temp-text "Some text\n"
613 (goto-char (point-max))
614 (should-error (org-forward-element)))
615 ;; 2. Standard move: expected to ignore blank lines.
616 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
617 (org-forward-element)
618 (should (looking-at (regexp-quote "Second paragraph."))))
619 ;; 3. Headline tests.
620 (org-test-with-temp-text "
625 ;; 3.1. At an headline beginning: move to next headline at the
628 (org-forward-element)
629 (should (looking-at (regexp-quote "** Head 1.2")))
630 ;; 3.2. At an headline beginning: move to parent headline if no
631 ;; headline at the same level.
633 (org-forward-element)
634 (should (looking-at (regexp-quote "** Head 1.2"))))
635 ;; 4. Greater element tests.
636 (org-test-with-temp-text
637 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
638 ;; 4.1. At a greater element: expected to skip contents.
639 (org-forward-element)
640 (should (looking-at (regexp-quote "Outside.")))
641 ;; 4.2. At the end of greater element contents: expected to skip
642 ;; to the end of the greater element.
644 (org-forward-element)
645 (should (looking-at (regexp-quote "Outside."))))
647 (org-test-with-temp-text "
661 ;; 5.1. At list top point: expected to move to the element after
664 (org-forward-element)
665 (should (looking-at (regexp-quote "Outside.")))
666 ;; 5.2. Special case: at the first line of a sub-list, but not at
667 ;; beginning of line, move to next item.
670 (org-forward-element)
671 (should (looking-at "- item2"))
674 (org-forward-element)
675 (should (looking-at " - sub2"))
676 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
678 (org-forward-element)
679 (should (looking-at (regexp-quote " Inner paragraph.")))
680 ;; 5.4. At sub-list end: expected to move outside the sub-list.
682 (org-forward-element)
683 (should (looking-at (regexp-quote " Inner paragraph.")))
684 ;; 5.5. At an item: expected to move to next item, if any.
686 (org-forward-element)
687 (should (looking-at " - sub3"))))
689 (ert-deftest test-org
/backward-element
()
690 "Test `org-backward-element' specifications."
691 ;; 1. Should error at BOB.
692 (org-test-with-temp-text " \nParagraph."
693 (should-error (org-backward-element)))
694 ;; 2. Should move at BOB when called on the first element in buffer.
696 (org-test-with-temp-text "\n#+TITLE: test"
697 (progn (forward-line)
698 (org-backward-element)
700 ;; 3. Not at the beginning of an element: move at its beginning.
701 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
704 (org-backward-element)
705 (should (looking-at (regexp-quote "Paragraph2."))))
706 ;; 4. Headline tests.
707 (org-test-with-temp-text "
712 ;; 4.1. At an headline beginning: move to previous headline at the
715 (org-backward-element)
716 (should (looking-at (regexp-quote "** Head 1.1")))
717 ;; 4.2. At an headline beginning: move to parent headline if no
718 ;; headline at the same level.
720 (org-backward-element)
721 (should (looking-at (regexp-quote "* Head 1")))
722 ;; 4.3. At the first top-level headline: should error.
724 (should-error (org-backward-element)))
725 ;; 5. At beginning of first element inside a greater element:
726 ;; expected to move to greater element's beginning.
727 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
729 (org-backward-element)
730 (should (looking-at "#\\+BEGIN_CENTER")))
731 ;; 6. At the beginning of the first element in a section: should
732 ;; move back to headline, if any.
734 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
735 (progn (goto-char (point-max))
737 (org-backward-element)
738 (org-at-heading-p))))
740 (org-test-with-temp-text "
755 ;; 7.1. At beginning of sub-list: expected to move to the
756 ;; paragraph before it.
758 (org-backward-element)
759 (should (looking-at "item1"))
760 ;; 7.2. At an item in a list: expected to move at previous item.
762 (org-backward-element)
763 (should (looking-at " - sub2"))
765 (org-backward-element)
766 (should (looking-at "- item1"))
767 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
770 (org-backward-element)
771 (should (looking-at " - sub1"))
773 (org-backward-element)
774 (should (looking-at "- item1"))
775 ;; 7.4. At blank-lines before list end: expected to move to top
778 (org-backward-element)
779 (should (looking-at "- item1"))))
781 (ert-deftest test-org
/up-element
()
782 "Test `org-up-element' specifications."
783 ;; 1. At BOB or with no surrounding element: should error.
784 (org-test-with-temp-text "Paragraph."
785 (should-error (org-up-element)))
786 (org-test-with-temp-text "* Head1\n* Head2"
788 (should-error (org-up-element)))
789 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
791 (should-error (org-up-element)))
792 ;; 2. At an headline: move to parent headline.
793 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
796 (should (looking-at "\\* Head1")))
797 ;; 3. Inside a greater element: move to greater element beginning.
798 (org-test-with-temp-text
799 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
802 (should (looking-at "#\\+BEGIN_CENTER")))
804 (org-test-with-temp-text "* Top
811 Paragraph within sub2.
814 ;; 4.1. Within an item: move to the item beginning.
817 (should (looking-at " - sub2"))
818 ;; 4.2. At an item in a sub-list: move to parent item.
821 (should (looking-at "- item1"))
822 ;; 4.3. At an item in top list: move to beginning of whole list.
825 (should (looking-at "- item1"))
826 ;; 4.4. Special case. At very top point: should move to parent of
830 (should (looking-at "\\* Top"))))
832 (ert-deftest test-org
/down-element
()
833 "Test `org-down-element' specifications."
834 ;; Error when the element hasn't got a recursive type.
835 (org-test-with-temp-text "Paragraph."
836 (should-error (org-down-element)))
837 ;; Error when the element has no contents
838 (org-test-with-temp-text "* Headline"
839 (should-error (org-down-element)))
840 ;; When at a plain-list, move to first item.
841 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
844 (should (looking-at " - Item 1.1")))
845 (org-test-with-temp-text "#+NAME: list\n- Item 1"
847 (should (looking-at " Item 1")))
848 ;; When at a table, move to first row
849 (org-test-with-temp-text "#+NAME: table\n| a | b |"
851 (should (looking-at " a | b |")))
852 ;; Otherwise, move inside the greater element.
853 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
855 (should (looking-at "Paragraph"))))
857 (ert-deftest test-org
/drag-element-backward
()
858 "Test `org-drag-element-backward' specifications."
859 ;; 1. Error when trying to move first element of buffer.
860 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
861 (should-error (org-drag-element-backward)))
862 ;; 2. Error when trying to swap nested elements.
863 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
865 (should-error (org-drag-element-backward)))
866 ;; 3. Error when trying to swap an headline element and
867 ;; a non-headline element.
868 (org-test-with-temp-text "Test.\n* Head 1"
870 (should-error (org-drag-element-backward)))
871 ;; 4. Otherwise, swap elements, preserving column and blank lines
873 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
874 (search-forward "graph")
875 (org-drag-element-backward)
876 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
877 (should (looking-at " 2")))
878 ;; 5. Preserve visibility of elements and their contents.
879 (org-test-with-temp-text "
887 (while (search-forward "BEGIN_" nil t
) (org-cycle))
888 (search-backward "- item 1")
889 (org-drag-element-backward)
892 '((63 .
82) (26 .
48))
893 (mapcar (lambda (ov) (cons (overlay-start ov
) (overlay-end ov
)))
894 (overlays-in (point-min) (point-max)))))))
896 (ert-deftest test-org
/drag-element-forward
()
897 "Test `org-drag-element-forward' specifications."
898 ;; 1. Error when trying to move first element of buffer.
899 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
901 (should-error (org-drag-element-forward)))
902 ;; 2. Error when trying to swap nested elements.
903 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
905 (should-error (org-drag-element-forward)))
906 ;; 3. Error when trying to swap a non-headline element and an
908 (org-test-with-temp-text "Test.\n* Head 1"
909 (should-error (org-drag-element-forward)))
910 ;; 4. Otherwise, swap elements, preserving column and blank lines
912 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
913 (search-forward "graph")
914 (org-drag-element-forward)
915 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
916 (should (looking-at " 1")))
917 ;; 5. Preserve visibility of elements and their contents.
918 (org-test-with-temp-text "
926 (while (search-forward "BEGIN_" nil t
) (org-cycle))
927 (search-backward "#+BEGIN_CENTER")
928 (org-drag-element-forward)
931 '((63 .
82) (26 .
48))
932 (mapcar (lambda (ov) (cons (overlay-start ov
) (overlay-end ov
)))
933 (overlays-in (point-min) (point-max)))))))
939 (ert-deftest test-org
/timestamp-has-time-p
()
940 "Test `org-timestamp-has-time-p' specifications."
943 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
944 (org-timestamp-has-time-p (org-element-context))))
947 (org-test-with-temp-text "<2012-03-29 Thu>"
948 (org-timestamp-has-time-p (org-element-context)))))
950 (ert-deftest test-org
/timestamp-format
()
951 "Test `org-timestamp-format' specifications."
956 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
957 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
962 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
963 (org-timestamp-format (org-element-context) "%Y-%m-%d" t
)))))
965 (ert-deftest test-org
/timestamp-split-range
()
966 "Test `org-timestamp-split-range' specifications."
967 ;; Extract range start (active).
970 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
971 (let ((ts (org-timestamp-split-range (org-element-context))))
972 (mapcar (lambda (p) (org-element-property p ts
))
973 '(:year-end
:month-end
:day-end
))))))
974 ;; Extract range start (inactive)
977 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
978 (let ((ts (org-timestamp-split-range (org-element-context))))
979 (mapcar (lambda (p) (org-element-property p ts
))
980 '(:year-end
:month-end
:day-end
))))))
981 ;; Extract range end (active).
984 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
985 (let ((ts (org-timestamp-split-range
986 (org-element-context) t
)))
987 (mapcar (lambda (p) (org-element-property p ts
))
988 '(:year-end
:month-end
:day-end
))))))
989 ;; Extract range end (inactive)
992 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
993 (let ((ts (org-timestamp-split-range
994 (org-element-context) t
)))
995 (mapcar (lambda (p) (org-element-property p ts
))
996 '(:year-end
:month-end
:day-end
))))))
997 ;; Return the timestamp if not a range.
999 (org-test-with-temp-text "[2012-03-29 Thu]"
1000 (let* ((ts-orig (org-element-context))
1001 (ts-copy (org-timestamp-split-range ts-orig
)))
1002 (eq ts-orig ts-copy
))))
1004 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1005 (let* ((ts-orig (org-element-context))
1006 (ts-copy (org-timestamp-split-range ts-orig
)))
1007 (eq ts-orig ts-copy
))))
1008 ;; Check that parent is the same when a range was split.
1010 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1011 (let* ((ts-orig (org-element-context))
1012 (ts-copy (org-timestamp-split-range ts-orig
)))
1013 (eq (org-element-property :parent ts-orig
)
1014 (org-element-property :parent ts-copy
))))))
1016 (ert-deftest test-org
/timestamp-translate
()
1017 "Test `org-timestamp-translate' specifications."
1018 ;; Translate whole date range.
1021 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1022 (let ((org-display-custom-times t
)
1023 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1024 (org-timestamp-translate (org-element-context))))))
1025 ;; Translate date range start.
1028 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1029 (let ((org-display-custom-times t
)
1030 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1031 (org-timestamp-translate (org-element-context) 'start
)))))
1032 ;; Translate date range end.
1035 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1036 (let ((org-display-custom-times t
)
1037 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1038 (org-timestamp-translate (org-element-context) 'end
)))))
1039 ;; Translate time range.
1042 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
1043 (let ((org-display-custom-times t
)
1044 (org-time-stamp-custom-formats '("<%d>" .
"<%H>")))
1045 (org-timestamp-translate (org-element-context))))))
1046 ;; Translate non-range timestamp.
1049 (org-test-with-temp-text "<2012-03-29 Thu>"
1050 (let ((org-display-custom-times t
)
1051 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1052 (org-timestamp-translate (org-element-context))))))
1053 ;; Do not change `diary' timestamps.
1055 (equal "<%%(org-float t 4 2)>"
1056 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1057 (let ((org-display-custom-times t
)
1058 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1059 (org-timestamp-translate (org-element-context)))))))
1063 ;;; Targets and Radio Targets
1065 (ert-deftest test-org
/all-targets
()
1066 "Test `org-all-targets' specifications."
1067 ;; Without an argument.
1069 (equal '("radio-target" "target")
1070 (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
1071 (org-all-targets))))
1073 (equal '("radio-target")
1074 (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
1077 (equal '("radio-target")
1078 (org-test-with-temp-text "<<target>> <<<radio-target>>>"
1079 (org-all-targets t
)))))
1084 ;;; test-org.el ends here