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)))))
451 ;;; Fixed-Width Areas
453 (ert-deftest test-org
/toggle-fixed-with
()
454 "Test `org-toggle-fixed-width' specifications."
455 ;; No region: Toggle on fixed-width marker in paragraphs.
458 (org-test-with-temp-text "A"
459 (org-toggle-fixed-width)
461 ;; No region: Toggle off fixed-width markers in fixed-width areas.
464 (org-test-with-temp-text ": A"
465 (org-toggle-fixed-width)
467 ;; No region: Toggle on marker in blank lines after elements or just
471 (org-test-with-temp-text "* H\n"
473 (org-toggle-fixed-width)
476 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
477 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
478 (goto-char (point-max))
479 (org-toggle-fixed-width)
481 ;; No region: Toggle on marker in front of one line elements (e.g.,
482 ;; headlines, clocks)
484 (equal ": * Headline"
485 (org-test-with-temp-text "* Headline"
486 (org-toggle-fixed-width)
489 (equal ": #+KEYWORD: value"
490 (org-test-with-temp-text "#+KEYWORD: value"
491 (org-toggle-fixed-width)
493 ;; No region: error in other situations.
495 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
497 (org-toggle-fixed-width)
499 ;; No region: Indentation is preserved.
502 (org-test-with-temp-text "- A\n B"
504 (org-toggle-fixed-width)
506 ;; Region: If it contains only fixed-width elements and blank lines,
507 ;; toggle off fixed-width markup.
510 (org-test-with-temp-text ": A\n\n: B"
511 (transient-mark-mode 1)
512 (push-mark (point) t t
)
513 (goto-char (point-max))
514 (org-toggle-fixed-width)
516 ;; Region: If it contains anything else, toggle on fixed-width but
517 ;; not on fixed-width areas.
519 (equal ": A\n: \n: B\n: \n: C"
520 (org-test-with-temp-text "A\n\n: B\n\nC"
521 (transient-mark-mode 1)
522 (push-mark (point) t t
)
523 (goto-char (point-max))
524 (org-toggle-fixed-width)
526 ;; Region: Ignore blank lines at its end, unless it contains only
530 (org-test-with-temp-text "A\n\n"
531 (transient-mark-mode 1)
532 (push-mark (point) t t
)
533 (goto-char (point-max))
534 (org-toggle-fixed-width)
538 (org-test-with-temp-text "\n\n"
539 (transient-mark-mode 1)
540 (push-mark (point) t t
)
541 (goto-char (point-max))
542 (org-toggle-fixed-width)
551 (ert-deftest test-org
/custom-id
()
552 "Test custom ID links specifications."
554 (org-test-with-temp-text
555 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
556 (goto-char (1- (point-max)))
558 (org-looking-at-p "\\* H1"))))
562 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
563 ;; a named element (#+name: text) and to headlines (* Text).
565 (ert-deftest test-org
/fuzzy-links
()
566 "Test fuzzy links specifications."
567 ;; 1. Fuzzy link goes in priority to a matching target.
569 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
572 (looking-at "<<Test>>")))
573 ;; 2. Then fuzzy link points to an element with a given name.
575 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
578 (looking-at "#\\+NAME: Test")))
579 ;; 3. A target still lead to a matching headline otherwise.
581 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
584 (looking-at "\\* Head2")))
585 ;; 4. With a leading star in link, enforce heading match.
587 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
590 (looking-at "\\* Test"))))
595 (ert-deftest test-org
/org-link-escape-ascii-character
()
596 "Escape an ascii character."
600 (org-link-escape "["))))
602 (ert-deftest test-org
/org-link-escape-ascii-ctrl-character
()
603 "Escape an ascii control character."
607 (org-link-escape "\t"))))
609 (ert-deftest test-org
/org-link-escape-multibyte-character
()
610 "Escape an unicode multibyte character."
614 (org-link-escape "€"))))
616 (ert-deftest test-org
/org-link-escape-custom-table
()
617 "Escape string with custom character table."
621 (org-link-escape "Foo:Bar\n" '(?\
: ?\B
)))))
623 (ert-deftest test-org
/org-link-escape-custom-table-merge
()
624 "Escape string with custom table merged with default table."
627 "%5BF%6F%6F%3A%42ar%0A%5D"
628 (org-link-escape "[Foo:Bar\n]" '(?\
: ?\B ?\o
) t
))))
630 (ert-deftest test-org
/org-link-unescape-ascii-character
()
631 "Unescape an ascii character."
635 (org-link-unescape "%5B"))))
637 (ert-deftest test-org
/org-link-unescape-ascii-ctrl-character
()
638 "Unescpae an ascii control character."
642 (org-link-unescape "%0A"))))
644 (ert-deftest test-org
/org-link-unescape-multibyte-character
()
645 "Unescape unicode multibyte character."
649 (org-link-unescape "%E2%82%AC"))))
651 (ert-deftest test-org
/org-link-unescape-ascii-extended-char
()
652 "Unescape old style percent escaped character."
655 "à âçèéêîôùû"
656 (decode-coding-string
657 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1
))))
659 (ert-deftest test-org
/org-link-escape-url-with-escaped-char
()
660 "Escape and unescape a URL that includes an escaped char.
661 http://article.gmane.org/gmane.emacs.orgmode/21459/"
664 "http://some.host.com/form?&id=blah%2Bblah25"
666 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
668 (ert-deftest test-org
/org-link-escape-chars-browser
()
669 "Test of the constant `org-link-escape-chars-browser'.
670 See there why this test is a candidate to be removed once Org
671 drops support for Emacs 24.1 and 24.2."
674 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
675 "%22Release%208.2%22&idxname=emacs-orgmode")
676 (org-link-escape-browser ; Do not replace with `url-encode-url',
677 ; see docstring above.
678 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
679 "\"Release 8.2\"&idxname=emacs-orgmode")))))
685 (ert-deftest test-org
/accumulated-properties-in-drawers
()
686 "Ensure properties accumulate in subtree drawers."
687 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
688 (org-babel-next-src-block)
689 (should (equal '(2 1) (org-babel-execute-src-block)))))
695 (ert-deftest test-org
/mark-subtree
()
696 "Test `org-mark-subtree' specifications."
697 ;; Error when point is before first headline.
699 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
700 (progn (transient-mark-mode 1)
701 (org-mark-subtree))))
702 ;; Without argument, mark current subtree.
706 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
707 (progn (transient-mark-mode 1)
710 (list (region-beginning) (region-end))))))
711 ;; With an argument, move ARG up.
715 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
716 (progn (transient-mark-mode 1)
719 (list (region-beginning) (region-end))))))
720 ;; Do not get fooled by inlinetasks.
721 (when (featurep 'org-inlinetask
)
724 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
725 (progn (transient-mark-mode 1)
727 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
728 (region-beginning)))))))
734 (ert-deftest test-org
/beginning-of-line
()
735 "Test `org-beginning-of-line' specifications."
738 (org-test-with-temp-text "Some text\nSome other text"
739 (progn (org-beginning-of-line) (bolp))))
740 ;; Standard test with `visual-line-mode'.
742 (org-test-with-temp-text "A long line of text\nSome other text"
743 (progn (visual-line-mode)
745 (dotimes (i 1000) (insert "very "))
746 (org-beginning-of-line)
748 ;; At an headline with special movement.
750 (org-test-with-temp-text "* TODO Headline"
751 (let ((org-special-ctrl-a/e t
))
753 (and (progn (org-beginning-of-line) (looking-at "Headline"))
754 (progn (org-beginning-of-line) (bolp))
755 (progn (org-beginning-of-line) (looking-at "Headline")))))))
757 (ert-deftest test-org
/end-of-line
()
758 "Test `org-end-of-line' specifications."
761 (org-test-with-temp-text "Some text\nSome other text"
762 (progn (org-end-of-line) (eolp))))
763 ;; Standard test with `visual-line-mode'.
765 (org-test-with-temp-text "A long line of text\nSome other text"
766 (progn (visual-line-mode)
768 (dotimes (i 1000) (insert "very "))
769 (goto-char (point-min))
772 ;; At an headline with special movement.
774 (org-test-with-temp-text "* Headline1 :tag:\n"
775 (let ((org-special-ctrl-a/e t
))
776 (and (progn (org-end-of-line) (looking-at " :tag:"))
777 (progn (org-end-of-line) (eolp))
778 (progn (org-end-of-line) (looking-at " :tag:"))))))
779 ;; At an headline without special movement.
781 (org-test-with-temp-text "* Headline2 :tag:\n"
782 (let ((org-special-ctrl-a/e nil
))
783 (and (progn (org-end-of-line) (eolp))
784 (progn (org-end-of-line) (eolp))))))
785 ;; At an headline, with reversed movement.
787 (org-test-with-temp-text "* Headline3 :tag:\n"
788 (let ((org-special-ctrl-a/e
'reversed
)
789 (this-command last-command
))
790 (and (progn (org-end-of-line) (eolp))
791 (progn (org-end-of-line) (looking-at " :tag:"))))))
792 ;; At a block without hidden contents.
794 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
795 (progn (org-end-of-line) (eolp))))
796 ;; At a block with hidden contents.
798 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
799 (let ((org-special-ctrl-a/e t
))
800 (org-hide-block-toggle)
804 (ert-deftest test-org
/forward-paragraph
()
805 "Test `org-forward-paragraph' specifications."
806 ;; At end of buffer, return an error.
808 (org-test-with-temp-text "Paragraph"
809 (goto-char (point-max))
810 (org-forward-paragraph)))
813 (org-test-with-temp-text "P1\n\nP2\n\nP3"
814 (org-forward-paragraph)
818 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
819 (org-forward-paragraph)
821 ;; Do not enter elements with invisible contents.
823 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
824 (org-hide-block-toggle)
825 (org-forward-paragraph)
827 ;; On an affiliated keyword, jump to the beginning of the element.
829 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
830 (org-forward-paragraph)
831 (looking-at "Para")))
832 ;; On an item or a footnote definition, move to the second element
835 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
836 (org-forward-paragraph)
837 (looking-at " Paragraph")))
839 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
840 (org-forward-paragraph)
841 (looking-at "Paragraph")))
842 ;; On an item, or a footnote definition, when the first line is
843 ;; empty, move to the first item.
845 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
846 (org-forward-paragraph)
847 (looking-at " Paragraph")))
849 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
850 (org-forward-paragraph)
851 (looking-at "Paragraph")))
852 ;; On a table (resp. a property drawer) do not move through table
853 ;; rows (resp. node properties).
855 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
856 (org-forward-paragraph)
857 (looking-at "Paragraph")))
859 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
860 (org-forward-paragraph)
861 (looking-at "Paragraph")))
862 ;; On a verse or source block, stop after blank lines.
864 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
865 (org-forward-paragraph)
868 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
869 (org-forward-paragraph)
872 (ert-deftest test-org
/backward-paragraph
()
873 "Test `org-backward-paragraph' specifications."
874 ;; Error at beginning of buffer.
876 (org-test-with-temp-text "Paragraph"
877 (org-backward-paragraph)))
880 (org-test-with-temp-text "P1\n\nP2\n\nP3"
881 (goto-char (point-max))
882 (org-backward-paragraph)
885 (org-test-with-temp-text "P1\n\nP2\n\nP3"
886 (goto-char (point-max))
888 (org-backward-paragraph)
892 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
893 (goto-char (point-max))
895 (org-backward-paragraph)
897 ;; Ignore invisible elements.
899 (org-test-with-temp-text "* H1\n P1\n* H2"
901 (goto-char (point-max))
903 (org-backward-paragraph)
905 ;; On an affiliated keyword, jump to the first one.
907 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
908 (search-forward "c2")
909 (org-backward-paragraph)
910 (looking-at "#\\+name")))
911 ;; On the second element in an item or a footnote definition, jump
912 ;; to item or the definition.
914 (org-test-with-temp-text "- line1\n\n line2"
915 (goto-char (point-max))
917 (org-backward-paragraph)
918 (looking-at "- line1")))
920 (org-test-with-temp-text "[fn:1] line1\n\n line2"
921 (goto-char (point-max))
923 (org-backward-paragraph)
924 (looking-at "\\[fn:1\\] line1")))
925 ;; On a table (resp. a property drawer), ignore table rows
926 ;; (resp. node properties).
928 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
929 (goto-char (point-max))
931 (org-backward-paragraph)
934 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
935 (goto-char (point-max))
937 (org-backward-paragraph)
939 ;; On a source or verse block, stop before blank lines.
941 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
942 (search-forward "L3")
944 (org-backward-paragraph)
947 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
948 (search-forward "L3")
950 (org-backward-paragraph)
953 (ert-deftest test-org
/forward-element
()
954 "Test `org-forward-element' specifications."
955 ;; 1. At EOB: should error.
956 (org-test-with-temp-text "Some text\n"
957 (goto-char (point-max))
958 (should-error (org-forward-element)))
959 ;; 2. Standard move: expected to ignore blank lines.
960 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
961 (org-forward-element)
962 (should (looking-at (regexp-quote "Second paragraph."))))
963 ;; 3. Headline tests.
964 (org-test-with-temp-text "
969 ;; 3.1. At an headline beginning: move to next headline at the
972 (org-forward-element)
973 (should (looking-at (regexp-quote "** Head 1.2")))
974 ;; 3.2. At an headline beginning: move to parent headline if no
975 ;; headline at the same level.
977 (org-forward-element)
978 (should (looking-at (regexp-quote "** Head 1.2"))))
979 ;; 4. Greater element tests.
980 (org-test-with-temp-text
981 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
982 ;; 4.1. At a greater element: expected to skip contents.
983 (org-forward-element)
984 (should (looking-at (regexp-quote "Outside.")))
985 ;; 4.2. At the end of greater element contents: expected to skip
986 ;; to the end of the greater element.
988 (org-forward-element)
989 (should (looking-at (regexp-quote "Outside."))))
991 (org-test-with-temp-text "
1005 ;; 5.1. At list top point: expected to move to the element after
1008 (org-forward-element)
1009 (should (looking-at (regexp-quote "Outside.")))
1010 ;; 5.2. Special case: at the first line of a sub-list, but not at
1011 ;; beginning of line, move to next item.
1014 (org-forward-element)
1015 (should (looking-at "- item2"))
1018 (org-forward-element)
1019 (should (looking-at " - sub2"))
1020 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
1022 (org-forward-element)
1023 (should (looking-at (regexp-quote " Inner paragraph.")))
1024 ;; 5.4. At sub-list end: expected to move outside the sub-list.
1026 (org-forward-element)
1027 (should (looking-at (regexp-quote " Inner paragraph.")))
1028 ;; 5.5. At an item: expected to move to next item, if any.
1030 (org-forward-element)
1031 (should (looking-at " - sub3"))))
1033 (ert-deftest test-org
/backward-element
()
1034 "Test `org-backward-element' specifications."
1035 ;; 1. Should error at BOB.
1036 (org-test-with-temp-text " \nParagraph."
1037 (should-error (org-backward-element)))
1038 ;; 2. Should move at BOB when called on the first element in buffer.
1040 (org-test-with-temp-text "\n#+TITLE: test"
1041 (progn (forward-line)
1042 (org-backward-element)
1044 ;; 3. Not at the beginning of an element: move at its beginning.
1045 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
1048 (org-backward-element)
1049 (should (looking-at (regexp-quote "Paragraph2."))))
1050 ;; 4. Headline tests.
1051 (org-test-with-temp-text "
1056 ;; 4.1. At an headline beginning: move to previous headline at the
1059 (org-backward-element)
1060 (should (looking-at (regexp-quote "** Head 1.1")))
1061 ;; 4.2. At an headline beginning: move to parent headline if no
1062 ;; headline at the same level.
1064 (org-backward-element)
1065 (should (looking-at (regexp-quote "* Head 1")))
1066 ;; 4.3. At the first top-level headline: should error.
1068 (should-error (org-backward-element)))
1069 ;; 5. At beginning of first element inside a greater element:
1070 ;; expected to move to greater element's beginning.
1071 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
1073 (org-backward-element)
1074 (should (looking-at "#\\+BEGIN_CENTER")))
1075 ;; 6. At the beginning of the first element in a section: should
1076 ;; move back to headline, if any.
1078 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
1079 (progn (goto-char (point-max))
1081 (org-backward-element)
1082 (org-at-heading-p))))
1084 (org-test-with-temp-text "
1099 ;; 7.1. At beginning of sub-list: expected to move to the
1100 ;; paragraph before it.
1102 (org-backward-element)
1103 (should (looking-at "item1"))
1104 ;; 7.2. At an item in a list: expected to move at previous item.
1106 (org-backward-element)
1107 (should (looking-at " - sub2"))
1109 (org-backward-element)
1110 (should (looking-at "- item1"))
1111 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
1114 (org-backward-element)
1115 (should (looking-at " - sub1"))
1117 (org-backward-element)
1118 (should (looking-at "- item1"))
1119 ;; 7.4. At blank-lines before list end: expected to move to top
1122 (org-backward-element)
1123 (should (looking-at "- item1"))))
1125 (ert-deftest test-org
/up-element
()
1126 "Test `org-up-element' specifications."
1127 ;; 1. At BOB or with no surrounding element: should error.
1128 (org-test-with-temp-text "Paragraph."
1129 (should-error (org-up-element)))
1130 (org-test-with-temp-text "* Head1\n* Head2"
1132 (should-error (org-up-element)))
1133 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
1135 (should-error (org-up-element)))
1136 ;; 2. At an headline: move to parent headline.
1137 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
1140 (should (looking-at "\\* Head1")))
1141 ;; 3. Inside a greater element: move to greater element beginning.
1142 (org-test-with-temp-text
1143 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
1146 (should (looking-at "#\\+BEGIN_CENTER")))
1148 (org-test-with-temp-text "* Top
1155 Paragraph within sub2.
1158 ;; 4.1. Within an item: move to the item beginning.
1161 (should (looking-at " - sub2"))
1162 ;; 4.2. At an item in a sub-list: move to parent item.
1165 (should (looking-at "- item1"))
1166 ;; 4.3. At an item in top list: move to beginning of whole list.
1169 (should (looking-at "- item1"))
1170 ;; 4.4. Special case. At very top point: should move to parent of
1174 (should (looking-at "\\* Top"))))
1176 (ert-deftest test-org
/down-element
()
1177 "Test `org-down-element' specifications."
1178 ;; Error when the element hasn't got a recursive type.
1179 (org-test-with-temp-text "Paragraph."
1180 (should-error (org-down-element)))
1181 ;; Error when the element has no contents
1182 (org-test-with-temp-text "* Headline"
1183 (should-error (org-down-element)))
1184 ;; When at a plain-list, move to first item.
1185 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
1188 (should (looking-at " - Item 1.1")))
1189 (org-test-with-temp-text "#+NAME: list\n- Item 1"
1191 (should (looking-at " Item 1")))
1192 ;; When at a table, move to first row
1193 (org-test-with-temp-text "#+NAME: table\n| a | b |"
1195 (should (looking-at " a | b |")))
1196 ;; Otherwise, move inside the greater element.
1197 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
1199 (should (looking-at "Paragraph"))))
1201 (ert-deftest test-org
/drag-element-backward
()
1202 "Test `org-drag-element-backward' specifications."
1203 ;; 1. Error when trying to move first element of buffer.
1204 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
1205 (should-error (org-drag-element-backward)))
1206 ;; 2. Error when trying to swap nested elements.
1207 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
1209 (should-error (org-drag-element-backward)))
1210 ;; 3. Error when trying to swap an headline element and
1211 ;; a non-headline element.
1212 (org-test-with-temp-text "Test.\n* Head 1"
1214 (should-error (org-drag-element-backward)))
1215 ;; 4. Otherwise, swap elements, preserving column and blank lines
1216 ;; between elements.
1217 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
1218 (search-forward "graph")
1219 (org-drag-element-backward)
1220 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
1221 (should (looking-at " 2")))
1222 ;; 5. Preserve visibility of elements and their contents.
1223 (org-test-with-temp-text "
1231 (while (search-forward "BEGIN_" nil t
) (org-cycle))
1232 (search-backward "- item 1")
1233 (org-drag-element-backward)
1236 '((63 .
82) (26 .
48))
1237 (mapcar (lambda (ov) (cons (overlay-start ov
) (overlay-end ov
)))
1238 (overlays-in (point-min) (point-max)))))))
1240 (ert-deftest test-org
/drag-element-forward
()
1241 "Test `org-drag-element-forward' specifications."
1242 ;; 1. Error when trying to move first element of buffer.
1243 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
1245 (should-error (org-drag-element-forward)))
1246 ;; 2. Error when trying to swap nested elements.
1247 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
1249 (should-error (org-drag-element-forward)))
1250 ;; 3. Error when trying to swap a non-headline element and an
1252 (org-test-with-temp-text "Test.\n* Head 1"
1253 (should-error (org-drag-element-forward)))
1254 ;; 4. Otherwise, swap elements, preserving column and blank lines
1255 ;; between elements.
1256 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
1257 (search-forward "graph")
1258 (org-drag-element-forward)
1259 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
1260 (should (looking-at " 1")))
1261 ;; 5. Preserve visibility of elements and their contents.
1262 (org-test-with-temp-text "
1270 (while (search-forward "BEGIN_" nil t
) (org-cycle))
1271 (search-backward "#+BEGIN_CENTER")
1272 (org-drag-element-forward)
1275 '((63 .
82) (26 .
48))
1276 (mapcar (lambda (ov) (cons (overlay-start ov
) (overlay-end ov
)))
1277 (overlays-in (point-min) (point-max)))))))
1283 (ert-deftest test-org
/timestamp-has-time-p
()
1284 "Test `org-timestamp-has-time-p' specifications."
1287 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
1288 (org-timestamp-has-time-p (org-element-context))))
1291 (org-test-with-temp-text "<2012-03-29 Thu>"
1292 (org-timestamp-has-time-p (org-element-context)))))
1294 (ert-deftest test-org
/timestamp-format
()
1295 "Test `org-timestamp-format' specifications."
1300 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
1301 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
1306 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
1307 (org-timestamp-format (org-element-context) "%Y-%m-%d" t
)))))
1309 (ert-deftest test-org
/timestamp-split-range
()
1310 "Test `org-timestamp-split-range' specifications."
1311 ;; Extract range start (active).
1314 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1315 (let ((ts (org-timestamp-split-range (org-element-context))))
1316 (mapcar (lambda (p) (org-element-property p ts
))
1317 '(:year-end
:month-end
:day-end
))))))
1318 ;; Extract range start (inactive)
1321 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1322 (let ((ts (org-timestamp-split-range (org-element-context))))
1323 (mapcar (lambda (p) (org-element-property p ts
))
1324 '(:year-end
:month-end
:day-end
))))))
1325 ;; Extract range end (active).
1328 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1329 (let ((ts (org-timestamp-split-range
1330 (org-element-context) t
)))
1331 (mapcar (lambda (p) (org-element-property p ts
))
1332 '(:year-end
:month-end
:day-end
))))))
1333 ;; Extract range end (inactive)
1336 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1337 (let ((ts (org-timestamp-split-range
1338 (org-element-context) t
)))
1339 (mapcar (lambda (p) (org-element-property p ts
))
1340 '(:year-end
:month-end
:day-end
))))))
1341 ;; Return the timestamp if not a range.
1343 (org-test-with-temp-text "[2012-03-29 Thu]"
1344 (let* ((ts-orig (org-element-context))
1345 (ts-copy (org-timestamp-split-range ts-orig
)))
1346 (eq ts-orig ts-copy
))))
1348 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1349 (let* ((ts-orig (org-element-context))
1350 (ts-copy (org-timestamp-split-range ts-orig
)))
1351 (eq ts-orig ts-copy
))))
1352 ;; Check that parent is the same when a range was split.
1354 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1355 (let* ((ts-orig (org-element-context))
1356 (ts-copy (org-timestamp-split-range ts-orig
)))
1357 (eq (org-element-property :parent ts-orig
)
1358 (org-element-property :parent ts-copy
))))))
1360 (ert-deftest test-org
/timestamp-translate
()
1361 "Test `org-timestamp-translate' specifications."
1362 ;; Translate whole date range.
1365 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1366 (let ((org-display-custom-times t
)
1367 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1368 (org-timestamp-translate (org-element-context))))))
1369 ;; Translate date range start.
1372 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1373 (let ((org-display-custom-times t
)
1374 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1375 (org-timestamp-translate (org-element-context) 'start
)))))
1376 ;; Translate date range end.
1379 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1380 (let ((org-display-custom-times t
)
1381 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1382 (org-timestamp-translate (org-element-context) 'end
)))))
1383 ;; Translate time range.
1386 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
1387 (let ((org-display-custom-times t
)
1388 (org-time-stamp-custom-formats '("<%d>" .
"<%H>")))
1389 (org-timestamp-translate (org-element-context))))))
1390 ;; Translate non-range timestamp.
1393 (org-test-with-temp-text "<2012-03-29 Thu>"
1394 (let ((org-display-custom-times t
)
1395 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1396 (org-timestamp-translate (org-element-context))))))
1397 ;; Do not change `diary' timestamps.
1399 (equal "<%%(org-float t 4 2)>"
1400 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1401 (let ((org-display-custom-times t
)
1402 (org-time-stamp-custom-formats '("<%d>" .
"<%d>")))
1403 (org-timestamp-translate (org-element-context)))))))
1407 ;;; Targets and Radio Targets
1409 (ert-deftest test-org
/all-targets
()
1410 "Test `org-all-targets' specifications."
1411 ;; Without an argument.
1413 (equal '("radio-target" "target")
1414 (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
1415 (org-all-targets))))
1417 (equal '("radio-target")
1418 (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
1421 (equal '("radio-target")
1422 (org-test-with-temp-text "<<target>> <<<radio-target>>>"
1423 (org-all-targets t
)))))
1428 (ert-deftest test-org
/flag-drawer
()
1429 "Test `org-flag-drawer' specifications."
1432 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
1434 (get-char-property (line-end-position) 'invisible
)))
1437 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
1439 (org-flag-drawer nil
)
1440 (get-char-property (line-end-position) 'invisible
)))
1441 ;; Test optional argument.
1443 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
1444 (let ((drawer (save-excursion (search-forward ":D2")
1445 (org-element-at-point))))
1446 (org-flag-drawer t drawer
)
1447 (get-char-property (progn (search-forward ":D2") (line-end-position))
1450 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
1451 (let ((drawer (save-excursion (search-forward ":D2")
1452 (org-element-at-point))))
1453 (org-flag-drawer t drawer
)
1454 (get-char-property (line-end-position) 'invisible
))))
1455 ;; Do not hide fake drawers.
1457 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
1460 (get-char-property (line-end-position) 'invisible
)))
1461 ;; Do not hide incomplete drawers.
1463 (org-test-with-temp-text ":D:\nparagraph"
1466 (get-char-property (line-end-position) 'invisible
))))
1471 ;;; test-org.el ends here