3 ;; Copyright (c) ߚ David Maus
6 ;; Released under the GNU General Public License version 3
7 ;; see: http://www.gnu.org/licenses/gpl-3.0.html
11 ;; Template test file for Org-mode tests
14 (ert-deftest test-org
/org-link-escape-ascii-character
()
15 "Escape an ascii character."
19 (org-link-escape "["))))
21 (ert-deftest test-org
/org-link-escape-ascii-ctrl-character
()
22 "Escape an ascii control character."
26 (org-link-escape "\t"))))
28 (ert-deftest test-org
/org-link-escape-multibyte-character
()
29 "Escape an unicode multibyte character."
33 (org-link-escape "€"))))
35 (ert-deftest test-org
/org-link-escape-custom-table
()
36 "Escape string with custom character table."
40 (org-link-escape "Foo:Bar\n" '(?\
: ?\B
)))))
42 (ert-deftest test-org
/org-link-escape-custom-table-merge
()
43 "Escape string with custom table merged with default table."
46 "%5BF%6F%6F%3A%42ar%0A%5D"
47 (org-link-escape "[Foo:Bar\n]" '(?\
: ?\B ?\o
) t
))))
49 (ert-deftest test-org
/org-link-unescape-ascii-character
()
50 "Unescape an ascii character."
54 (org-link-unescape "%5B"))))
56 (ert-deftest test-org
/org-link-unescape-ascii-ctrl-character
()
57 "Unescpae an ascii control character."
61 (org-link-unescape "%0A"))))
63 (ert-deftest test-org
/org-link-unescape-multibyte-character
()
64 "Unescape unicode multibyte character."
68 (org-link-unescape "%E2%82%AC"))))
70 (ert-deftest test-org
/org-link-unescape-ascii-extended-char
()
71 "Unescape old style percent escaped character."
75 (decode-coding-string (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1
))))
77 (ert-deftest test-org
/org-link-escape-url-with-escaped-char
()
78 "Escape and unscape a URL that includes an escaped char.
79 http://article.gmane.org/gmane.emacs.orgmode/21459/"
82 "http://some.host.com/form?&id=blah%2Bblah25"
83 (org-link-unescape (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
85 (ert-deftest test-org
/accumulated-properties-in-drawers
()
86 "Ensure properties accumulate in subtree drawers."
87 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
88 (org-babel-next-src-block)
89 (should (equal '(2 1) (org-babel-execute-src-block)))))
97 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
98 ;; a target keyword (aka an invisible target: #+TARGET: text), to
99 ;; a named element (#+name: text) and to headlines (* Text).
101 (ert-deftest test-org
/fuzzy-links
()
102 "Test fuzzy links specifications."
103 ;; 1. Fuzzy link goes in priority to a matching target.
104 (org-test-with-temp-text
105 "#+TARGET: Test\n#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
108 (should (looking-at "<<Test>>")))
109 ;; 2. Fuzzy link should then go to a matching target keyword.
110 (org-test-with-temp-text
111 "#+NAME: Test\n|a|b|\n#+TARGET: Test\n* Test\n[[Test]]"
114 (should (looking-at "#\\+TARGET: Test")))
115 ;; 3. Then fuzzy link points to an element with a given name.
116 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
119 (should (looking-at "#\\+NAME: Test")))
120 ;; 4. A target still lead to a matching headline otherwise.
121 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
124 (should (looking-at "\\* Head2")))
125 ;; 5. With a leading star in link, enforce heading match.
126 (org-test-with-temp-text "#+TARGET: Test\n* Test\n<<Test>>\n[[*Test]]"
129 (should (looking-at "\\* Test"))))
135 (ert-deftest test-org
/macro-replace-all
()
136 "Test `org-macro-replace-all' specifications."
140 "#+MACRO: A B\n1 B 3"
141 (org-test-with-temp-text "#+MACRO: A B\n1 {{{A}}} 3"
142 (progn (org-macro-initialize-templates)
143 (org-macro-replace-all org-macro-templates
)
145 ;; Macro with arguments.
148 "#+MACRO: macro $1 $2\nsome text"
149 (org-test-with-temp-text "#+MACRO: macro $1 $2\n{{{macro(some,text)}}}"
150 (progn (org-macro-initialize-templates)
151 (org-macro-replace-all org-macro-templates
)
153 ;; Macro with "eval".
156 "#+MACRO: add (eval (+ $1 $2))\n3"
157 (org-test-with-temp-text "#+MACRO: add (eval (+ $1 $2))\n{{{add(1,2)}}}"
158 (progn (org-macro-initialize-templates)
159 (org-macro-replace-all org-macro-templates
)
164 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\ninner outer"
165 (org-test-with-temp-text
166 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\n{{{out}}}"
167 (progn (org-macro-initialize-templates)
168 (org-macro-replace-all org-macro-templates
)
175 (ert-deftest test-org
/fill-paragraph
()
176 "Test `org-fill-paragraph' specifications."
177 ;; At an Org table, align it.
178 (org-test-with-temp-text "|a|"
180 (should (equal (buffer-string) "| a |\n")))
181 ;; At a paragraph, preserve line breaks.
182 (org-test-with-temp-text "some \\\\\nlong\ntext"
183 (let ((fill-column 20))
185 (should (equal (buffer-string) "some \\\\\nlong text"))))
186 ;; Correctly fill a paragraph when point is at its very end.
189 (org-test-with-temp-text "A\nB"
190 (let ((fill-column 20))
191 (goto-char (point-max))
194 ;; Correctly fill the last paragraph of a greater element.
196 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
197 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
198 (let ((fill-column 8))
203 ;; Correctly fill an element in a narrowed buffer.
206 (org-test-with-temp-text "01234 6789"
207 (let ((fill-column 5))
208 (narrow-to-region 1 8)
211 ;; Special case: Fill first paragraph when point is at an item or
212 ;; a plain-list or a footnote reference.
215 (org-test-with-temp-text "- A\n B"
216 (let ((fill-column 20))
221 (org-test-with-temp-text "[fn:1] A\nB"
222 (let ((fill-column 20))
225 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
226 (let ((fill-column 20))
228 (should (equal (buffer-string)
229 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
230 ;; Fill contents of `comment-block' elements.
233 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
234 (let ((fill-column 20))
238 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
239 ;; Fill `comment' elements.
242 (org-test-with-temp-text " # A\n # B"
243 (let ((fill-column 20))
246 ;; Do nothing at affiliated keywords.
247 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
248 (let ((fill-column 20))
250 (should (equal (buffer-string) "#+NAME: para\nSome\ntext.")))))
252 (ert-deftest test-org
/auto-fill-function
()
253 "Test auto-filling features."
254 ;; Auto fill paragraph.
257 (org-test-with-temp-text "12345 7890"
258 (let ((fill-column 5))
260 (org-auto-fill-function)
262 ;; Auto fill first paragraph in an item.
264 (equal "- 12345\n 7890"
265 (org-test-with-temp-text "- 12345 7890"
266 (let ((fill-column 7))
268 (org-auto-fill-function)
270 ;; Auto fill comments.
272 (equal " # 12345\n # 7890"
273 (org-test-with-temp-text " # 12345 7890"
274 (let ((fill-column 10))
276 (org-auto-fill-function)
278 ;; Comment block: auto fill contents.
280 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
281 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
282 (let ((fill-column 5))
285 (org-auto-fill-function)
288 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
289 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
290 (let ((fill-column 5))
293 (org-auto-fill-function)
295 ;; Do not fill if a new item could be created.
298 (org-test-with-temp-text "12345 - 90"
299 (let ((fill-column 5))
301 (org-auto-fill-function)
303 ;; Do not fill if a line break could be introduced.
305 (equal "123\\\\\n7890"
306 (org-test-with-temp-text "123\\\\ 7890"
307 (let ((fill-column 6))
309 (org-auto-fill-function)
311 ;; Do not fill affiliated keywords.
313 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
314 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
315 (let ((fill-column 20))
317 (org-auto-fill-function)
324 (ert-deftest test-org
/comment-dwim
()
325 "Test `comment-dwim' behaviour in an Org buffer."
326 ;; No region selected, no comment on current line and line not
327 ;; empty: insert comment on line above.
330 (org-test-with-temp-text "Comment"
331 (progn (call-interactively 'comment-dwim
)
333 ;; No region selected, no comment on current line and line empty:
334 ;; insert comment on this line.
336 (equal "# \nParagraph"
337 (org-test-with-temp-text "\nParagraph"
338 (progn (call-interactively 'comment-dwim
)
340 ;; No region selected, and a comment on this line: indent it.
342 (equal "* Headline\n # Comment"
343 (org-test-with-temp-text "* Headline\n# Comment"
344 (progn (forward-line)
345 (let ((org-adapt-indentation t
))
346 (call-interactively 'comment-dwim
))
348 ;; Also recognize single # at column 0 as comments.
351 (org-test-with-temp-text "# Comment"
352 (progn (forward-line)
353 (call-interactively 'comment-dwim
)
355 ;; Region selected and only comments and blank lines within it:
356 ;; un-comment all commented lines.
358 (equal "Comment 1\n\nComment 2"
359 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
361 (transient-mark-mode 1)
362 (push-mark (point) t t
)
363 (goto-char (point-max))
364 (call-interactively 'comment-dwim
)
366 ;; Region selected without comments: comment all non-blank lines.
368 (equal "# Comment 1\n\n# Comment 2"
369 (org-test-with-temp-text "Comment 1\n\nComment 2"
371 (transient-mark-mode 1)
372 (push-mark (point) t t
)
373 (goto-char (point-max))
374 (call-interactively 'comment-dwim
)
376 ;; In front of a keyword without region, insert a new comment.
378 (equal "# \n#+KEYWORD: value"
379 (org-test-with-temp-text "#+KEYWORD: value"
380 (progn (call-interactively 'comment-dwim
)
387 (ert-deftest test-org
/mark-subtree
()
388 "Test `org-mark-subtree' specifications."
389 ;; Error when point is before first headline.
391 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
392 (progn (transient-mark-mode 1)
393 (org-mark-subtree))))
394 ;; Without argument, mark current subtree.
398 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
399 (progn (transient-mark-mode 1)
402 (list (region-beginning) (region-end))))))
403 ;; With an argument, move ARG up.
407 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
408 (progn (transient-mark-mode 1)
411 (list (region-beginning) (region-end))))))
412 ;; Do not get fooled with inlinetasks.
413 (when (featurep 'org-inlinetask
)
416 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
417 (progn (transient-mark-mode 1)
419 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
420 (region-beginning)))))))
426 (ert-deftest test-org
/forward-element
()
427 "Test `org-forward-element' specifications."
428 ;; 1. At EOB: should error.
429 (org-test-with-temp-text "Some text\n"
430 (goto-char (point-max))
431 (should-error (org-forward-element)))
432 ;; 2. Standard move: expected to ignore blank lines.
433 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
434 (org-forward-element)
435 (should (looking-at "Second paragraph.")))
436 ;; 3. Headline tests.
437 (org-test-with-temp-text "
442 ;; 3.1. At an headline beginning: move to next headline at the
445 (org-forward-element)
446 (should (looking-at "** Head 1.2"))
447 ;; 3.2. At an headline beginning: move to parent headline if no
448 ;; headline at the same level.
450 (org-forward-element)
451 (should (looking-at "** Head 1.2")))
452 ;; 4. Greater element tests.
453 (org-test-with-temp-text
454 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
455 ;; 4.1. At a greater element: expected to skip contents.
456 (org-forward-element)
457 (should (looking-at "Outside."))
458 ;; 4.2. At the end of greater element contents: expected to skip
459 ;; to the end of the greater element.
461 (org-forward-element)
462 (should (looking-at "Outside.")))
464 (org-test-with-temp-text "
478 ;; 5.1. At list top point: expected to move to the element after
481 (org-forward-element)
482 (should (looking-at "Outside."))
483 ;; 5.2. Special case: at the first line of a sub-list, but not at
484 ;; beginning of line, move to next item.
487 (org-forward-element)
488 (should (looking-at "- item2"))
491 (org-forward-element)
492 (should (looking-at " - sub2"))
493 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
495 (org-forward-element)
496 (should (looking-at " Inner paragraph."))
497 ;; 5.4. At sub-list end: expected to move outside the sub-list.
499 (org-forward-element)
500 (should (looking-at " Inner paragraph."))
501 ;; 5.5. At an item: expected to move to next item, if any.
503 (org-forward-element)
504 (should (looking-at " - sub3"))))
506 (ert-deftest test-org
/backward-element
()
507 "Test `org-backward-element' specifications."
508 ;; 1. Should error at BOB.
509 (org-test-with-temp-text " \nParagraph."
510 (should-error (org-backward-element)))
511 ;; 2. Should move at BOB when called on the first element in buffer.
513 (org-test-with-temp-text "\n#+TITLE: test"
514 (progn (forward-line)
515 (org-backward-element)
517 ;; 3. Not at the beginning of an element: move at its beginning.
518 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
521 (org-backward-element)
522 (should (looking-at "Paragraph2.")))
523 ;; 4. Headline tests.
524 (org-test-with-temp-text "
529 ;; 4.1. At an headline beginning: move to previous headline at the
532 (org-backward-element)
533 (should (looking-at "** Head 1.1"))
534 ;; 4.2. At an headline beginning: move to parent headline if no
535 ;; headline at the same level.
537 (org-backward-element)
538 (should (looking-at "* Head 1"))
539 ;; 4.3. At the first top-level headline: should error.
541 (should-error (org-backward-element)))
542 ;; 5. At beginning of first element inside a greater element:
543 ;; expected to move to greater element's beginning.
544 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
546 (org-backward-element)
547 (should (looking-at "#\\+BEGIN_CENTER")))
548 ;; 6. At the beginning of the first element in a section: should
549 ;; move back to headline, if any.
551 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
552 (progn (goto-char (point-max))
554 (org-backward-element)
555 (org-at-heading-p))))
557 (org-test-with-temp-text "
572 ;; 7.1. At beginning of sub-list: expected to move to the
573 ;; paragraph before it.
575 (org-backward-element)
576 (should (looking-at "item1"))
577 ;; 7.2. At an item in a list: expected to move at previous item.
579 (org-backward-element)
580 (should (looking-at " - sub2"))
582 (org-backward-element)
583 (should (looking-at "- item1"))
584 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
587 (org-backward-element)
588 (should (looking-at " - sub1"))
590 (org-backward-element)
591 (should (looking-at "- item1"))
592 ;; 7.4. At blank-lines before list end: expected to move to top
595 (org-backward-element)
596 (should (looking-at "- item1"))))
598 (ert-deftest test-org
/up-element
()
599 "Test `org-up-element' specifications."
600 ;; 1. At BOB or with no surrounding element: should error.
601 (org-test-with-temp-text "Paragraph."
602 (should-error (org-up-element)))
603 (org-test-with-temp-text "* Head1\n* Head2"
605 (should-error (org-up-element)))
606 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
608 (should-error (org-up-element)))
609 ;; 2. At an headline: move to parent headline.
610 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
613 (should (looking-at "\\* Head1")))
614 ;; 3. Inside a greater element: move to greater element beginning.
615 (org-test-with-temp-text
616 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
619 (should (looking-at "#\\+BEGIN_CENTER")))
621 (org-test-with-temp-text "* Top
628 Paragraph within sub2.
631 ;; 4.1. Within an item: move to the item beginning.
634 (should (looking-at " - sub2"))
635 ;; 4.2. At an item in a sub-list: move to parent item.
638 (should (looking-at "- item1"))
639 ;; 4.3. At an item in top list: move to beginning of whole list.
642 (should (looking-at "- item1"))
643 ;; 4.4. Special case. At very top point: should move to parent of
647 (should (looking-at "\\* Top"))))
649 (ert-deftest test-org
/down-element
()
650 "Test `org-down-element' specifications."
651 ;; Error when the element hasn't got a recursive type.
652 (org-test-with-temp-text "Paragraph."
653 (should-error (org-down-element)))
654 ;; Error when the element has no contents
655 (org-test-with-temp-text "* Headline"
656 (should-error (org-down-element)))
657 ;; When at a plain-list, move to first item.
658 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
661 (should (looking-at " - Item 1.1")))
662 (org-test-with-temp-text "#+NAME: list\n- Item 1"
664 (should (looking-at " Item 1")))
665 ;; When at a table, move to first row
666 (org-test-with-temp-text "#+NAME: table\n| a | b |"
668 (should (looking-at " a | b |")))
669 ;; Otherwise, move inside the greater element.
670 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
672 (should (looking-at "Paragraph"))))
674 (ert-deftest test-org
/drag-element-backward
()
675 "Test `org-drag-element-backward' specifications."
676 ;; 1. Error when trying to move first element of buffer.
677 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
678 (should-error (org-drag-element-backward)))
679 ;; 2. Error when trying to swap nested elements.
680 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
682 (should-error (org-drag-element-backward)))
683 ;; 3. Error when trying to swap an headline element and
684 ;; a non-headline element.
685 (org-test-with-temp-text "Test.\n* Head 1"
687 (should-error (org-drag-element-backward)))
688 ;; 4. Otherwise, swap elements, preserving column and blank lines
690 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
691 (search-forward "graph")
692 (org-drag-element-backward)
693 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
694 (should (looking-at " 2")))
695 ;; 5. Preserve visibility of elements and their contents.
696 (org-test-with-temp-text "
704 (while (search-forward "BEGIN_" nil t
) (org-cycle))
705 (search-backward "- item 1")
706 (org-drag-element-backward)
709 '((63 .
82) (26 .
48))
710 (mapcar (lambda (ov) (cons (overlay-start ov
) (overlay-end ov
)))
711 (overlays-in (point-min) (point-max)))))))
713 (ert-deftest test-org
/drag-element-forward
()
714 "Test `org-drag-element-forward' specifications."
715 ;; 1. Error when trying to move first element of buffer.
716 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
718 (should-error (org-drag-element-forward)))
719 ;; 2. Error when trying to swap nested elements.
720 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
722 (should-error (org-drag-element-forward)))
723 ;; 3. Error when trying to swap a non-headline element and an
725 (org-test-with-temp-text "Test.\n* Head 1"
726 (should-error (org-drag-element-forward)))
727 ;; 4. Otherwise, swap elements, preserving column and blank lines
729 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
730 (search-forward "graph")
731 (org-drag-element-forward)
732 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
733 (should (looking-at " 1")))
734 ;; 5. Preserve visibility of elements and their contents.
735 (org-test-with-temp-text "
743 (while (search-forward "BEGIN_" nil t
) (org-cycle))
744 (search-backward "#+BEGIN_CENTER")
745 (org-drag-element-forward)
748 '((63 .
82) (26 .
48))
749 (mapcar (lambda (ov) (cons (overlay-start ov
) (overlay-end ov
)))
750 (overlays-in (point-min) (point-max)))))))
755 ;;; test-org.el ends here