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
/fill-paragraph
()
136 "Test `org-fill-paragraph' specifications."
137 ;; At an Org table, align it.
138 (org-test-with-temp-text "|a|"
140 (should (equal (buffer-string) "| a |\n")))
141 ;; At a paragraph, preserve line breaks.
142 (org-test-with-temp-text "some \\\\\nlong\ntext"
143 (let ((fill-column 20))
145 (should (equal (buffer-string) "some \\\\\nlong text"))))
146 ;; Correctly fill a paragraph when point is at its very end.
149 (org-test-with-temp-text "A\nB"
150 (let ((fill-column 20))
151 (goto-char (point-max))
154 ;; Correctly fill the last paragraph of a greater element.
156 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
157 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
158 (let ((fill-column 8))
163 ;; Correctly fill an element in a narrowed buffer.
166 (org-test-with-temp-text "01234 6789"
167 (let ((fill-column 5))
168 (narrow-to-region 1 8)
171 ;; Special case: Fill first paragraph when point is at an item or
172 ;; a plain-list or a footnote reference.
175 (org-test-with-temp-text "- A\n B"
176 (let ((fill-column 20))
181 (org-test-with-temp-text "[fn:1] A\nB"
182 (let ((fill-column 20))
185 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
186 (let ((fill-column 20))
188 (should (equal (buffer-string)
189 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
190 ;; Fill contents of `comment-block' elements.
193 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
194 (let ((fill-column 20))
198 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
199 ;; Fill `comment' elements.
202 (org-test-with-temp-text " # A\n # B"
203 (let ((fill-column 20))
206 ;; Do nothing at affiliated keywords.
207 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
208 (let ((fill-column 20))
210 (should (equal (buffer-string) "#+NAME: para\nSome\ntext.")))))
212 (ert-deftest test-org
/auto-fill-function
()
213 "Test auto-filling features."
214 ;; Auto fill paragraph.
217 (org-test-with-temp-text "12345 7890"
218 (let ((fill-column 5))
220 (org-auto-fill-function)
222 ;; Auto fill first paragraph in an item.
224 (equal "- 12345\n 7890"
225 (org-test-with-temp-text "- 12345 7890"
226 (let ((fill-column 7))
228 (org-auto-fill-function)
230 ;; Auto fill comments.
232 (equal " # 12345\n # 7890"
233 (org-test-with-temp-text " # 12345 7890"
234 (let ((fill-column 10))
236 (org-auto-fill-function)
238 ;; Comment block: auto fill contents.
240 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
241 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
242 (let ((fill-column 5))
245 (org-auto-fill-function)
248 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
249 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
250 (let ((fill-column 5))
253 (org-auto-fill-function)
255 ;; Do not fill if a new item could be created.
258 (org-test-with-temp-text "12345 - 90"
259 (let ((fill-column 5))
261 (org-auto-fill-function)
263 ;; Do not fill if a line break could be introduced.
265 (equal "123\\\\\n7890"
266 (org-test-with-temp-text "123\\\\ 7890"
267 (let ((fill-column 6))
269 (org-auto-fill-function)
271 ;; Do not fill affiliated keywords.
273 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
274 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
275 (let ((fill-column 20))
277 (org-auto-fill-function)
284 (ert-deftest test-org
/comment-dwim
()
285 "Test `comment-dwim' behaviour in an Org buffer."
286 ;; No region selected, no comment on current line and line not
287 ;; empty: insert comment on line above.
290 (org-test-with-temp-text "Comment"
291 (progn (call-interactively 'comment-dwim
)
293 ;; No region selected, no comment on current line and line empty:
294 ;; insert comment on this line.
296 (equal "# \nParagraph"
297 (org-test-with-temp-text "\nParagraph"
298 (progn (call-interactively 'comment-dwim
)
300 ;; No region selected, and a comment on this line: indent it.
302 (equal "* Headline\n # Comment"
303 (org-test-with-temp-text "* Headline\n# Comment"
304 (progn (forward-line)
305 (let ((org-adapt-indentation t
))
306 (call-interactively 'comment-dwim
))
308 ;; Also recognize single # at column 0 as comments.
311 (org-test-with-temp-text "# Comment"
312 (progn (forward-line)
313 (call-interactively 'comment-dwim
)
315 ;; Region selected and only comments and blank lines within it:
316 ;; un-comment all commented lines.
318 (equal "Comment 1\n\nComment 2"
319 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
321 (transient-mark-mode 1)
322 (push-mark (point) t t
)
323 (goto-char (point-max))
324 (call-interactively 'comment-dwim
)
326 ;; Region selected without comments: comment all non-blank lines.
328 (equal "# Comment 1\n\n# Comment 2"
329 (org-test-with-temp-text "Comment 1\n\nComment 2"
331 (transient-mark-mode 1)
332 (push-mark (point) t t
)
333 (goto-char (point-max))
334 (call-interactively 'comment-dwim
)
336 ;; In front of a keyword without region, insert a new comment.
338 (equal "# \n#+KEYWORD: value"
339 (org-test-with-temp-text "#+KEYWORD: value"
340 (progn (call-interactively 'comment-dwim
)
347 (ert-deftest test-org
/mark-subtree
()
348 "Test `org-mark-subtree' specifications."
349 ;; Error when point is before first headline.
351 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
352 (progn (transient-mark-mode 1)
353 (org-mark-subtree))))
354 ;; Without argument, mark current subtree.
358 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
359 (progn (transient-mark-mode 1)
362 (list (region-beginning) (region-end))))))
363 ;; With an argument, move ARG up.
367 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
368 (progn (transient-mark-mode 1)
371 (list (region-beginning) (region-end))))))
372 ;; Do not get fooled with inlinetasks.
373 (when (featurep 'org-inlinetask
)
376 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
377 (progn (transient-mark-mode 1)
379 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
380 (region-beginning)))))))
386 (ert-deftest test-org
/forward-element
()
387 "Test `org-forward-element' specifications."
388 ;; 1. At EOB: should error.
389 (org-test-with-temp-text "Some text\n"
390 (goto-char (point-max))
391 (should-error (org-forward-element)))
392 ;; 2. Standard move: expected to ignore blank lines.
393 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
394 (org-forward-element)
395 (should (looking-at "Second paragraph.")))
396 ;; 3. Headline tests.
397 (org-test-with-temp-text "
402 ;; 3.1. At an headline beginning: move to next headline at the
405 (org-forward-element)
406 (should (looking-at "** Head 1.2"))
407 ;; 3.2. At an headline beginning: move to parent headline if no
408 ;; headline at the same level.
410 (org-forward-element)
411 (should (looking-at "** Head 1.2")))
412 ;; 4. Greater element tests.
413 (org-test-with-temp-text
414 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
415 ;; 4.1. At a greater element: expected to skip contents.
416 (org-forward-element)
417 (should (looking-at "Outside."))
418 ;; 4.2. At the end of greater element contents: expected to skip
419 ;; to the end of the greater element.
421 (org-forward-element)
422 (should (looking-at "Outside.")))
424 (org-test-with-temp-text "
438 ;; 5.1. At list top point: expected to move to the element after
441 (org-forward-element)
442 (should (looking-at "Outside."))
443 ;; 5.2. Special case: at the first line of a sub-list, but not at
444 ;; beginning of line, move to next item.
447 (org-forward-element)
448 (should (looking-at "- item2"))
451 (org-forward-element)
452 (should (looking-at " - sub2"))
453 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
455 (org-forward-element)
456 (should (looking-at " Inner paragraph."))
457 ;; 5.4. At sub-list end: expected to move outside the sub-list.
459 (org-forward-element)
460 (should (looking-at " Inner paragraph."))
461 ;; 5.5. At an item: expected to move to next item, if any.
463 (org-forward-element)
464 (should (looking-at " - sub3"))))
466 (ert-deftest test-org
/backward-element
()
467 "Test `org-backward-element' specifications."
468 ;; 1. Should error at BOB.
469 (org-test-with-temp-text " \nParagraph."
470 (should-error (org-backward-element)))
471 ;; 2. Should move at BOB when called on the first element in buffer.
473 (org-test-with-temp-text "\n#+TITLE: test"
474 (progn (forward-line)
475 (org-backward-element)
477 ;; 3. Not at the beginning of an element: move at its beginning.
478 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
481 (org-backward-element)
482 (should (looking-at "Paragraph2.")))
483 ;; 4. Headline tests.
484 (org-test-with-temp-text "
489 ;; 4.1. At an headline beginning: move to previous headline at the
492 (org-backward-element)
493 (should (looking-at "** Head 1.1"))
494 ;; 4.2. At an headline beginning: move to parent headline if no
495 ;; headline at the same level.
497 (org-backward-element)
498 (should (looking-at "* Head 1"))
499 ;; 4.3. At the first top-level headline: should error.
501 (should-error (org-backward-element)))
502 ;; 5. At beginning of first element inside a greater element:
503 ;; expected to move to greater element's beginning.
504 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER."
506 (org-backward-element)
507 (should (looking-at "#\\+BEGIN_CENTER")))
508 ;; 6. At the beginning of the first element in a section: should
509 ;; move back to headline, if any.
511 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
512 (progn (goto-char (point-max))
514 (org-backward-element)
515 (org-at-heading-p))))
517 (org-test-with-temp-text "
532 ;; 7.1. At beginning of sub-list: expected to move to the
533 ;; paragraph before it.
535 (org-backward-element)
536 (should (looking-at "item1"))
537 ;; 7.2. At an item in a list: expected to move at previous item.
539 (org-backward-element)
540 (should (looking-at " - sub2"))
542 (org-backward-element)
543 (should (looking-at "- item1"))
544 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
547 (org-backward-element)
548 (should (looking-at " - sub1"))
550 (org-backward-element)
551 (should (looking-at "- item1"))
552 ;; 7.4. At blank-lines before list end: expected to move to top
555 (org-backward-element)
556 (should (looking-at "- item1"))))
558 (ert-deftest test-org
/up-element
()
559 "Test `org-up-element' specifications."
560 ;; 1. At BOB or with no surrounding element: should error.
561 (org-test-with-temp-text "Paragraph."
562 (should-error (org-up-element)))
563 (org-test-with-temp-text "* Head1\n* Head2"
565 (should-error (org-up-element)))
566 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
568 (should-error (org-up-element)))
569 ;; 2. At an headline: move to parent headline.
570 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
573 (should (looking-at "\\* Head1")))
574 ;; 3. Inside a greater element: move to greater element beginning.
575 (org-test-with-temp-text
576 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
579 (should (looking-at "#\\+BEGIN_CENTER")))
581 (org-test-with-temp-text "* Top
588 Paragraph within sub2.
591 ;; 4.1. Within an item: move to the item beginning.
594 (should (looking-at " - sub2"))
595 ;; 4.2. At an item in a sub-list: move to parent item.
598 (should (looking-at "- item1"))
599 ;; 4.3. At an item in top list: move to beginning of whole list.
602 (should (looking-at "- item1"))
603 ;; 4.4. Special case. At very top point: should move to parent of
607 (should (looking-at "\\* Top"))))
609 (ert-deftest test-org
/down-element
()
610 "Test `org-down-element' specifications."
611 ;; Error when the element hasn't got a recursive type.
612 (org-test-with-temp-text "Paragraph."
613 (should-error (org-down-element)))
614 ;; Error when the element has no contents
615 (org-test-with-temp-text "* Headline"
616 (should-error (org-down-element)))
617 ;; When at a plain-list, move to first item.
618 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
621 (should (looking-at " - Item 1.1")))
622 (org-test-with-temp-text "#+NAME: list\n- Item 1"
624 (should (looking-at " Item 1")))
625 ;; When at a table, move to first row
626 (org-test-with-temp-text "#+NAME: table\n| a | b |"
628 (should (looking-at " a | b |")))
629 ;; Otherwise, move inside the greater element.
630 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
632 (should (looking-at "Paragraph"))))
634 (ert-deftest test-org
/drag-element-backward
()
635 "Test `org-drag-element-backward' specifications."
636 ;; 1. Error when trying to move first element of buffer.
637 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
638 (should-error (org-drag-element-backward)))
639 ;; 2. Error when trying to swap nested elements.
640 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
642 (should-error (org-drag-element-backward)))
643 ;; 3. Error when trying to swap an headline element and
644 ;; a non-headline element.
645 (org-test-with-temp-text "Test.\n* Head 1"
647 (should-error (org-drag-element-backward)))
648 ;; 4. Otherwise, swap elements, preserving column and blank lines
650 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
651 (search-forward "graph")
652 (org-drag-element-backward)
653 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
654 (should (looking-at " 2")))
655 ;; 5. Preserve visibility of elements and their contents.
656 (org-test-with-temp-text "
664 (while (search-forward "BEGIN_" nil t
) (org-cycle))
665 (search-backward "- item 1")
666 (org-drag-element-backward)
669 '((63 .
82) (26 .
48))
670 (mapcar (lambda (ov) (cons (overlay-start ov
) (overlay-end ov
)))
671 (overlays-in (point-min) (point-max)))))))
673 (ert-deftest test-org
/drag-element-forward
()
674 "Test `org-drag-element-forward' specifications."
675 ;; 1. Error when trying to move first element of buffer.
676 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
678 (should-error (org-drag-element-forward)))
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-forward)))
683 ;; 3. Error when trying to swap a non-headline element and an
685 (org-test-with-temp-text "Test.\n* Head 1"
686 (should-error (org-drag-element-forward)))
687 ;; 4. Otherwise, swap elements, preserving column and blank lines
689 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
690 (search-forward "graph")
691 (org-drag-element-forward)
692 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
693 (should (looking-at " 1")))
694 ;; 5. Preserve visibility of elements and their contents.
695 (org-test-with-temp-text "
703 (while (search-forward "BEGIN_" nil t
) (org-cycle))
704 (search-backward "#+BEGIN_CENTER")
705 (org-drag-element-forward)
708 '((63 .
82) (26 .
48))
709 (mapcar (lambda (ov) (cons (overlay-start ov
) (overlay-end ov
)))
710 (overlays-in (point-min) (point-max)))))))
715 ;;; test-org.el ends here