org.texi (Closing items): Document `org-closed-keep-when-no-todo'
[org-mode.git] / testing / lisp / test-org.el
blob36f92d9c6afa2d9ffb5ae08c0ac3c98e501d0bce
1 ;;; test-org.el --- tests for org.el
3 ;; Copyright (c) David Maus
4 ;; Authors: 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/>.
21 ;;; Comments:
23 ;; Template test file for Org-mode tests
25 ;;; Code:
28 ;;; Comments
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.
34 (should
35 (equal "# \nComment"
36 (org-test-with-temp-text "Comment"
37 (progn (call-interactively 'comment-dwim)
38 (buffer-string)))))
39 ;; No region selected, no comment on current line and line empty:
40 ;; insert comment on this line.
41 (should
42 (equal "# \nParagraph"
43 (org-test-with-temp-text "\nParagraph"
44 (progn (call-interactively 'comment-dwim)
45 (buffer-string)))))
46 ;; No region selected, and a comment on this line: indent it.
47 (should
48 (equal "* Headline\n # Comment"
49 (org-test-with-temp-text "* Headline\n# Comment"
50 (progn (forward-line)
51 (let ((org-adapt-indentation t))
52 (call-interactively 'comment-dwim))
53 (buffer-string)))))
54 ;; Also recognize single # at column 0 as comments.
55 (should
56 (equal "# Comment"
57 (org-test-with-temp-text "# Comment"
58 (progn (forward-line)
59 (call-interactively 'comment-dwim)
60 (buffer-string)))))
61 ;; Region selected and only comments and blank lines within it:
62 ;; un-comment all commented lines.
63 (should
64 (equal "Comment 1\n\nComment 2"
65 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
66 (progn
67 (transient-mark-mode 1)
68 (push-mark (point) t t)
69 (goto-char (point-max))
70 (call-interactively 'comment-dwim)
71 (buffer-string)))))
72 ;; Region selected without comments: comment all non-blank lines.
73 (should
74 (equal "# Comment 1\n\n# Comment 2"
75 (org-test-with-temp-text "Comment 1\n\nComment 2"
76 (progn
77 (transient-mark-mode 1)
78 (push-mark (point) t t)
79 (goto-char (point-max))
80 (call-interactively 'comment-dwim)
81 (buffer-string)))))
82 ;; In front of a keyword without region, insert a new comment.
83 (should
84 (equal "# \n#+KEYWORD: value"
85 (org-test-with-temp-text "#+KEYWORD: value"
86 (progn (call-interactively 'comment-dwim)
87 (buffer-string))))))
91 ;;; Date and time analysis
93 (ert-deftest test-org/org-read-date ()
94 "Test `org-read-date' specifications."
95 ;; Parse ISO date with abbreviated year and month.
96 (should (equal "2012-03-29 16:40"
97 (let ((org-time-was-given t))
98 (org-read-date t nil "12-3-29 16:40"))))
99 ;; Parse Europeans dates.
100 (should (equal "2012-03-29 16:40"
101 (let ((org-time-was-given t))
102 (org-read-date t nil "29.03.2012 16:40"))))
103 ;; Parse Europeans dates without year.
104 (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
105 (let ((org-time-was-given t))
106 (org-read-date t nil "29.03. 16:40")))))
108 (ert-deftest test-org/org-parse-time-string ()
109 "Test `org-parse-time-string'."
110 (should (equal (org-parse-time-string "2012-03-29 16:40")
111 '(0 40 16 29 3 2012 nil nil nil)))
112 (should (equal (org-parse-time-string "[2012-03-29 16:40]")
113 '(0 40 16 29 3 2012 nil nil nil)))
114 (should (equal (org-parse-time-string "<2012-03-29 16:40>")
115 '(0 40 16 29 3 2012 nil nil nil)))
116 (should (equal (org-parse-time-string "<2012-03-29>")
117 '(0 0 0 29 3 2012 nil nil nil)))
118 (should (equal (org-parse-time-string "<2012-03-29>" t)
119 '(0 nil nil 29 3 2012 nil nil nil))))
122 ;;; Filling
124 (ert-deftest test-org/fill-paragraph ()
125 "Test `org-fill-paragraph' specifications."
126 ;; At an Org table, align it.
127 (org-test-with-temp-text "|a|"
128 (org-fill-paragraph)
129 (should (equal (buffer-string) "| a |\n")))
130 ;; At a paragraph, preserve line breaks.
131 (org-test-with-temp-text "some \\\\\nlong\ntext"
132 (let ((fill-column 20))
133 (org-fill-paragraph)
134 (should (equal (buffer-string) "some \\\\\nlong text"))))
135 ;; Correctly fill a paragraph when point is at its very end.
136 (should
137 (equal "A B"
138 (org-test-with-temp-text "A\nB"
139 (let ((fill-column 20))
140 (goto-char (point-max))
141 (org-fill-paragraph)
142 (buffer-string)))))
143 ;; Correctly fill the last paragraph of a greater element.
144 (should
145 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
146 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
147 (let ((fill-column 8))
148 (forward-line)
149 (end-of-line)
150 (org-fill-paragraph)
151 (buffer-string)))))
152 ;; Correctly fill an element in a narrowed buffer.
153 (should
154 (equal "01234\n6"
155 (org-test-with-temp-text "01234 6789"
156 (let ((fill-column 5))
157 (narrow-to-region 1 8)
158 (org-fill-paragraph)
159 (buffer-string)))))
160 ;; Special case: Fill first paragraph when point is at an item or
161 ;; a plain-list or a footnote reference.
162 (should
163 (equal "- A B"
164 (org-test-with-temp-text "- A\n B"
165 (let ((fill-column 20))
166 (org-fill-paragraph)
167 (buffer-string)))))
168 (should
169 (equal "[fn:1] A B"
170 (org-test-with-temp-text "[fn:1] A\nB"
171 (let ((fill-column 20))
172 (org-fill-paragraph)
173 (buffer-string)))))
174 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
175 (let ((fill-column 20))
176 (org-fill-paragraph)
177 (should (equal (buffer-string)
178 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
179 ;; Fill contents of `comment-block' elements.
180 (should
181 (equal
182 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
183 (let ((fill-column 20))
184 (forward-line)
185 (org-fill-paragraph)
186 (buffer-string)))
187 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
188 ;; Fill `comment' elements.
189 (should
190 (equal " # A B"
191 (org-test-with-temp-text " # A\n # B"
192 (let ((fill-column 20))
193 (org-fill-paragraph)
194 (buffer-string)))))
195 ;; Do nothing at affiliated keywords.
196 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
197 (let ((fill-column 20))
198 (org-fill-paragraph)
199 (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
200 ;; Do not move point after table when filling a table.
201 (should-not
202 (org-test-with-temp-text "| a | b |\n| c | d |\n"
203 (forward-char)
204 (org-fill-paragraph)
205 (eobp))))
207 (ert-deftest test-org/auto-fill-function ()
208 "Test auto-filling features."
209 ;; Auto fill paragraph.
210 (should
211 (equal "12345\n7890"
212 (org-test-with-temp-text "12345 7890"
213 (let ((fill-column 5))
214 (end-of-line)
215 (org-auto-fill-function)
216 (buffer-string)))))
217 ;; Auto fill first paragraph in an item.
218 (should
219 (equal "- 12345\n 7890"
220 (org-test-with-temp-text "- 12345 7890"
221 (let ((fill-column 7))
222 (end-of-line)
223 (org-auto-fill-function)
224 (buffer-string)))))
225 ;; Auto fill comments.
226 (should
227 (equal " # 12345\n # 7890"
228 (org-test-with-temp-text " # 12345 7890"
229 (let ((fill-column 10))
230 (end-of-line)
231 (org-auto-fill-function)
232 (buffer-string)))))
233 ;; A hash within a line isn't a comment.
234 (should-not
235 (equal "12345 # 7890\n# 1"
236 (org-test-with-temp-text "12345 # 7890 1"
237 (let ((fill-column 12))
238 (end-of-line)
239 (org-auto-fill-function)
240 (buffer-string)))))
241 ;; Correctly interpret empty prefix.
242 (should-not
243 (equal "# a\n# b\nRegular\n# paragraph"
244 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
245 (let ((fill-column 12))
246 (end-of-line 3)
247 (org-auto-fill-function)
248 (buffer-string)))))
249 ;; Comment block: auto fill contents.
250 (should
251 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
252 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
253 (let ((fill-column 5))
254 (forward-line)
255 (end-of-line)
256 (org-auto-fill-function)
257 (buffer-string)))))
258 (should
259 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
260 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
261 (let ((fill-column 5))
262 (forward-line)
263 (end-of-line)
264 (org-auto-fill-function)
265 (buffer-string)))))
266 ;; Do not fill if a new item could be created.
267 (should-not
268 (equal "12345\n- 90"
269 (org-test-with-temp-text "12345 - 90"
270 (let ((fill-column 5))
271 (end-of-line)
272 (org-auto-fill-function)
273 (buffer-string)))))
274 ;; Do not fill if a line break could be introduced.
275 (should-not
276 (equal "123\\\\\n7890"
277 (org-test-with-temp-text "123\\\\ 7890"
278 (let ((fill-column 6))
279 (end-of-line)
280 (org-auto-fill-function)
281 (buffer-string)))))
282 ;; Do not fill affiliated keywords.
283 (should-not
284 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
285 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
286 (let ((fill-column 20))
287 (end-of-line)
288 (org-auto-fill-function)
289 (buffer-string))))))
293 ;;; Links
295 ;;;; Fuzzy Links
297 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
298 ;; a named element (#+name: text) and to headlines (* Text).
300 (ert-deftest test-org/fuzzy-links ()
301 "Test fuzzy links specifications."
302 ;; 1. Fuzzy link goes in priority to a matching target.
303 (should
304 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
305 (goto-line 5)
306 (org-open-at-point)
307 (looking-at "<<Test>>")))
308 ;; 2. Then fuzzy link points to an element with a given name.
309 (should
310 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
311 (goto-line 5)
312 (org-open-at-point)
313 (looking-at "#\\+NAME: Test")))
314 ;; 3. A target still lead to a matching headline otherwise.
315 (should
316 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
317 (goto-line 4)
318 (org-open-at-point)
319 (looking-at "\\* Head2")))
320 ;; 4. With a leading star in link, enforce heading match.
321 (should
322 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
323 (goto-line 3)
324 (org-open-at-point)
325 (looking-at "\\* Test"))))
328 ;;;; Link Escaping
330 (ert-deftest test-org/org-link-escape-ascii-character ()
331 "Escape an ascii character."
332 (should
333 (string=
334 "%5B"
335 (org-link-escape "["))))
337 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
338 "Escape an ascii control character."
339 (should
340 (string=
341 "%09"
342 (org-link-escape "\t"))))
344 (ert-deftest test-org/org-link-escape-multibyte-character ()
345 "Escape an unicode multibyte character."
346 (should
347 (string=
348 "%E2%82%AC"
349 (org-link-escape "€"))))
351 (ert-deftest test-org/org-link-escape-custom-table ()
352 "Escape string with custom character table."
353 (should
354 (string=
355 "Foo%3A%42ar%0A"
356 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
358 (ert-deftest test-org/org-link-escape-custom-table-merge ()
359 "Escape string with custom table merged with default table."
360 (should
361 (string=
362 "%5BF%6F%6F%3A%42ar%0A%5D"
363 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
365 (ert-deftest test-org/org-link-unescape-ascii-character ()
366 "Unescape an ascii character."
367 (should
368 (string=
370 (org-link-unescape "%5B"))))
372 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
373 "Unescpae an ascii control character."
374 (should
375 (string=
376 "\n"
377 (org-link-unescape "%0A"))))
379 (ert-deftest test-org/org-link-unescape-multibyte-character ()
380 "Unescape unicode multibyte character."
381 (should
382 (string=
383 "€"
384 (org-link-unescape "%E2%82%AC"))))
386 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
387 "Unescape old style percent escaped character."
388 (should
389 (string=
390 "àâçèéêîôùû"
391 (decode-coding-string (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
393 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
394 "Escape and unscape a URL that includes an escaped char.
395 http://article.gmane.org/gmane.emacs.orgmode/21459/"
396 (should
397 (string=
398 "http://some.host.com/form?&id=blah%2Bblah25"
399 (org-link-unescape (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
403 ;;; Node Properties
405 (ert-deftest test-org/accumulated-properties-in-drawers ()
406 "Ensure properties accumulate in subtree drawers."
407 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
408 (org-babel-next-src-block)
409 (should (equal '(2 1) (org-babel-execute-src-block)))))
413 ;;; Mark Region
415 (ert-deftest test-org/mark-subtree ()
416 "Test `org-mark-subtree' specifications."
417 ;; Error when point is before first headline.
418 (should-error
419 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
420 (progn (transient-mark-mode 1)
421 (org-mark-subtree))))
422 ;; Without argument, mark current subtree.
423 (should
424 (equal
425 '(12 32)
426 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
427 (progn (transient-mark-mode 1)
428 (forward-line 2)
429 (org-mark-subtree)
430 (list (region-beginning) (region-end))))))
431 ;; With an argument, move ARG up.
432 (should
433 (equal
434 '(1 32)
435 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
436 (progn (transient-mark-mode 1)
437 (forward-line 2)
438 (org-mark-subtree 1)
439 (list (region-beginning) (region-end))))))
440 ;; Do not get fooled by inlinetasks.
441 (when (featurep 'org-inlinetask)
442 (should
443 (= 1
444 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
445 (progn (transient-mark-mode 1)
446 (forward-line 1)
447 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
448 (region-beginning)))))))
452 ;;; Navigation
454 (ert-deftest test-org/beginning-of-line ()
455 "Test `org-beginning-of-line' specifications."
456 ;; Standard test.
457 (should
458 (org-test-with-temp-text "Some text\nSome other text"
459 (progn (org-beginning-of-line) (bolp))))
460 ;; Standard test with `visual-line-mode'.
461 (should-not
462 (org-test-with-temp-text "A long line of text\nSome other text"
463 (progn (visual-line-mode)
464 (forward-char 2)
465 (dotimes (i 1000) (insert "very "))
466 (org-beginning-of-line)
467 (bolp))))
468 ;; At an headline with special movement.
469 (should
470 (org-test-with-temp-text "* TODO Headline"
471 (let ((org-special-ctrl-a/e t))
472 (org-end-of-line)
473 (and (progn (org-beginning-of-line) (looking-at "Headline"))
474 (progn (org-beginning-of-line) (bolp))
475 (progn (org-beginning-of-line) (looking-at "Headline")))))))
477 (ert-deftest test-org/end-of-line ()
478 "Test `org-end-of-line' specifications."
479 ;; Standard test.
480 (should
481 (org-test-with-temp-text "Some text\nSome other text"
482 (progn (org-end-of-line) (eolp))))
483 ;; Standard test with `visual-line-mode'.
484 (should-not
485 (org-test-with-temp-text "A long line of text\nSome other text"
486 (progn (visual-line-mode)
487 (forward-char 2)
488 (dotimes (i 1000) (insert "very "))
489 (goto-char (point-min))
490 (org-end-of-line)
491 (eolp))))
492 ;; At an headline with special movement.
493 (should
494 (org-test-with-temp-text "* Headline1 :tag:\n"
495 (let ((org-special-ctrl-a/e t))
496 (and (progn (org-end-of-line) (looking-at " :tag:"))
497 (progn (org-end-of-line) (eolp))
498 (progn (org-end-of-line) (looking-at " :tag:"))))))
499 ;; At an headline without special movement.
500 (should
501 (org-test-with-temp-text "* Headline2 :tag:\n"
502 (let ((org-special-ctrl-a/e nil))
503 (and (progn (org-end-of-line) (eolp))
504 (progn (org-end-of-line) (eolp))))))
505 ;; At an headline, with reversed movement.
506 (should
507 (org-test-with-temp-text "* Headline3 :tag:\n"
508 (let ((org-special-ctrl-a/e 'reversed)
509 (this-command last-command))
510 (and (progn (org-end-of-line) (eolp))
511 (progn (org-end-of-line) (looking-at " :tag:"))))))
512 ;; At a block without hidden contents.
513 (should
514 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
515 (progn (org-end-of-line) (eolp))))
516 ;; At a block with hidden contents.
517 (should-not
518 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
519 (let ((org-special-ctrl-a/e t))
520 (org-hide-block-toggle)
521 (org-end-of-line)
522 (eobp)))))
524 (ert-deftest test-org/forward-element ()
525 "Test `org-forward-element' specifications."
526 ;; 1. At EOB: should error.
527 (org-test-with-temp-text "Some text\n"
528 (goto-char (point-max))
529 (should-error (org-forward-element)))
530 ;; 2. Standard move: expected to ignore blank lines.
531 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
532 (org-forward-element)
533 (should (looking-at (regexp-quote "Second paragraph."))))
534 ;; 3. Headline tests.
535 (org-test-with-temp-text "
536 * Head 1
537 ** Head 1.1
538 *** Head 1.1.1
539 ** Head 1.2"
540 ;; 3.1. At an headline beginning: move to next headline at the
541 ;; same level.
542 (goto-line 3)
543 (org-forward-element)
544 (should (looking-at (regexp-quote "** Head 1.2")))
545 ;; 3.2. At an headline beginning: move to parent headline if no
546 ;; headline at the same level.
547 (goto-line 3)
548 (org-forward-element)
549 (should (looking-at (regexp-quote "** Head 1.2"))))
550 ;; 4. Greater element tests.
551 (org-test-with-temp-text
552 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
553 ;; 4.1. At a greater element: expected to skip contents.
554 (org-forward-element)
555 (should (looking-at (regexp-quote "Outside.")))
556 ;; 4.2. At the end of greater element contents: expected to skip
557 ;; to the end of the greater element.
558 (goto-line 2)
559 (org-forward-element)
560 (should (looking-at (regexp-quote "Outside."))))
561 ;; 5. List tests.
562 (org-test-with-temp-text "
563 - item1
565 - sub1
567 - sub2
569 - sub3
571 Inner paragraph.
573 - item2
575 Outside."
576 ;; 5.1. At list top point: expected to move to the element after
577 ;; the list.
578 (goto-line 2)
579 (org-forward-element)
580 (should (looking-at (regexp-quote "Outside.")))
581 ;; 5.2. Special case: at the first line of a sub-list, but not at
582 ;; beginning of line, move to next item.
583 (goto-line 2)
584 (forward-char)
585 (org-forward-element)
586 (should (looking-at "- item2"))
587 (goto-line 4)
588 (forward-char)
589 (org-forward-element)
590 (should (looking-at " - sub2"))
591 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
592 (goto-line 4)
593 (org-forward-element)
594 (should (looking-at (regexp-quote " Inner paragraph.")))
595 ;; 5.4. At sub-list end: expected to move outside the sub-list.
596 (goto-line 8)
597 (org-forward-element)
598 (should (looking-at (regexp-quote " Inner paragraph.")))
599 ;; 5.5. At an item: expected to move to next item, if any.
600 (goto-line 6)
601 (org-forward-element)
602 (should (looking-at " - sub3"))))
604 (ert-deftest test-org/backward-element ()
605 "Test `org-backward-element' specifications."
606 ;; 1. Should error at BOB.
607 (org-test-with-temp-text " \nParagraph."
608 (should-error (org-backward-element)))
609 ;; 2. Should move at BOB when called on the first element in buffer.
610 (should
611 (org-test-with-temp-text "\n#+TITLE: test"
612 (progn (forward-line)
613 (org-backward-element)
614 (bobp))))
615 ;; 3. Not at the beginning of an element: move at its beginning.
616 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
617 (goto-line 3)
618 (end-of-line)
619 (org-backward-element)
620 (should (looking-at (regexp-quote "Paragraph2."))))
621 ;; 4. Headline tests.
622 (org-test-with-temp-text "
623 * Head 1
624 ** Head 1.1
625 *** Head 1.1.1
626 ** Head 1.2"
627 ;; 4.1. At an headline beginning: move to previous headline at the
628 ;; same level.
629 (goto-line 5)
630 (org-backward-element)
631 (should (looking-at (regexp-quote "** Head 1.1")))
632 ;; 4.2. At an headline beginning: move to parent headline if no
633 ;; headline at the same level.
634 (goto-line 3)
635 (org-backward-element)
636 (should (looking-at (regexp-quote "* Head 1")))
637 ;; 4.3. At the first top-level headline: should error.
638 (goto-line 2)
639 (should-error (org-backward-element)))
640 ;; 5. At beginning of first element inside a greater element:
641 ;; expected to move to greater element's beginning.
642 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
643 (goto-line 3)
644 (org-backward-element)
645 (should (looking-at "#\\+BEGIN_CENTER")))
646 ;; 6. At the beginning of the first element in a section: should
647 ;; move back to headline, if any.
648 (should
649 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
650 (progn (goto-char (point-max))
651 (beginning-of-line)
652 (org-backward-element)
653 (org-at-heading-p))))
654 ;; 7. List tests.
655 (org-test-with-temp-text "
656 - item1
658 - sub1
660 - sub2
662 - sub3
664 Inner paragraph.
666 - item2
669 Outside."
670 ;; 7.1. At beginning of sub-list: expected to move to the
671 ;; paragraph before it.
672 (goto-line 4)
673 (org-backward-element)
674 (should (looking-at "item1"))
675 ;; 7.2. At an item in a list: expected to move at previous item.
676 (goto-line 8)
677 (org-backward-element)
678 (should (looking-at " - sub2"))
679 (goto-line 12)
680 (org-backward-element)
681 (should (looking-at "- item1"))
682 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
683 ;; beginning.
684 (goto-line 10)
685 (org-backward-element)
686 (should (looking-at " - sub1"))
687 (goto-line 15)
688 (org-backward-element)
689 (should (looking-at "- item1"))
690 ;; 7.4. At blank-lines before list end: expected to move to top
691 ;; item.
692 (goto-line 14)
693 (org-backward-element)
694 (should (looking-at "- item1"))))
696 (ert-deftest test-org/up-element ()
697 "Test `org-up-element' specifications."
698 ;; 1. At BOB or with no surrounding element: should error.
699 (org-test-with-temp-text "Paragraph."
700 (should-error (org-up-element)))
701 (org-test-with-temp-text "* Head1\n* Head2"
702 (goto-line 2)
703 (should-error (org-up-element)))
704 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
705 (goto-line 3)
706 (should-error (org-up-element)))
707 ;; 2. At an headline: move to parent headline.
708 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
709 (goto-line 3)
710 (org-up-element)
711 (should (looking-at "\\* Head1")))
712 ;; 3. Inside a greater element: move to greater element beginning.
713 (org-test-with-temp-text
714 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
715 (goto-line 3)
716 (org-up-element)
717 (should (looking-at "#\\+BEGIN_CENTER")))
718 ;; 4. List tests.
719 (org-test-with-temp-text "* Top
720 - item1
722 - sub1
724 - sub2
726 Paragraph within sub2.
728 - item2"
729 ;; 4.1. Within an item: move to the item beginning.
730 (goto-line 8)
731 (org-up-element)
732 (should (looking-at " - sub2"))
733 ;; 4.2. At an item in a sub-list: move to parent item.
734 (goto-line 4)
735 (org-up-element)
736 (should (looking-at "- item1"))
737 ;; 4.3. At an item in top list: move to beginning of whole list.
738 (goto-line 10)
739 (org-up-element)
740 (should (looking-at "- item1"))
741 ;; 4.4. Special case. At very top point: should move to parent of
742 ;; list.
743 (goto-line 2)
744 (org-up-element)
745 (should (looking-at "\\* Top"))))
747 (ert-deftest test-org/down-element ()
748 "Test `org-down-element' specifications."
749 ;; Error when the element hasn't got a recursive type.
750 (org-test-with-temp-text "Paragraph."
751 (should-error (org-down-element)))
752 ;; Error when the element has no contents
753 (org-test-with-temp-text "* Headline"
754 (should-error (org-down-element)))
755 ;; When at a plain-list, move to first item.
756 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
757 (goto-line 2)
758 (org-down-element)
759 (should (looking-at " - Item 1.1")))
760 (org-test-with-temp-text "#+NAME: list\n- Item 1"
761 (org-down-element)
762 (should (looking-at " Item 1")))
763 ;; When at a table, move to first row
764 (org-test-with-temp-text "#+NAME: table\n| a | b |"
765 (org-down-element)
766 (should (looking-at " a | b |")))
767 ;; Otherwise, move inside the greater element.
768 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
769 (org-down-element)
770 (should (looking-at "Paragraph"))))
772 (ert-deftest test-org/drag-element-backward ()
773 "Test `org-drag-element-backward' specifications."
774 ;; 1. Error when trying to move first element of buffer.
775 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
776 (should-error (org-drag-element-backward)))
777 ;; 2. Error when trying to swap nested elements.
778 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
779 (forward-line)
780 (should-error (org-drag-element-backward)))
781 ;; 3. Error when trying to swap an headline element and
782 ;; a non-headline element.
783 (org-test-with-temp-text "Test.\n* Head 1"
784 (forward-line)
785 (should-error (org-drag-element-backward)))
786 ;; 4. Otherwise, swap elements, preserving column and blank lines
787 ;; between elements.
788 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
789 (search-forward "graph")
790 (org-drag-element-backward)
791 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
792 (should (looking-at " 2")))
793 ;; 5. Preserve visibility of elements and their contents.
794 (org-test-with-temp-text "
795 #+BEGIN_CENTER
796 Text.
797 #+END_CENTER
798 - item 1
799 #+BEGIN_QUOTE
800 Text.
801 #+END_QUOTE"
802 (while (search-forward "BEGIN_" nil t) (org-cycle))
803 (search-backward "- item 1")
804 (org-drag-element-backward)
805 (should
806 (equal
807 '((63 . 82) (26 . 48))
808 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
809 (overlays-in (point-min) (point-max)))))))
811 (ert-deftest test-org/drag-element-forward ()
812 "Test `org-drag-element-forward' specifications."
813 ;; 1. Error when trying to move first element of buffer.
814 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
815 (goto-line 3)
816 (should-error (org-drag-element-forward)))
817 ;; 2. Error when trying to swap nested elements.
818 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
819 (forward-line)
820 (should-error (org-drag-element-forward)))
821 ;; 3. Error when trying to swap a non-headline element and an
822 ;; headline.
823 (org-test-with-temp-text "Test.\n* Head 1"
824 (should-error (org-drag-element-forward)))
825 ;; 4. Otherwise, swap elements, preserving column and blank lines
826 ;; between elements.
827 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
828 (search-forward "graph")
829 (org-drag-element-forward)
830 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
831 (should (looking-at " 1")))
832 ;; 5. Preserve visibility of elements and their contents.
833 (org-test-with-temp-text "
834 #+BEGIN_CENTER
835 Text.
836 #+END_CENTER
837 - item 1
838 #+BEGIN_QUOTE
839 Text.
840 #+END_QUOTE"
841 (while (search-forward "BEGIN_" nil t) (org-cycle))
842 (search-backward "#+BEGIN_CENTER")
843 (org-drag-element-forward)
844 (should
845 (equal
846 '((63 . 82) (26 . 48))
847 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
848 (overlays-in (point-min) (point-max)))))))
852 ;;; Planning
854 (ert-deftest test-org/timestamp-has-time-p ()
855 "Test `org-timestamp-has-time-p' specifications."
856 ;; With time.
857 (should
858 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
859 (org-timestamp-has-time-p (org-element-context))))
860 ;; Without time.
861 (should-not
862 (org-test-with-temp-text "<2012-03-29 Thu>"
863 (org-timestamp-has-time-p (org-element-context)))))
865 (ert-deftest test-org/timestamp-format ()
866 "Test `org-timestamp-format' specifications."
867 ;; Regular test.
868 (should
869 (equal
870 "2012-03-29 16:40"
871 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
872 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
873 ;; Range end.
874 (should
875 (equal
876 "2012-03-29"
877 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
878 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
880 (ert-deftest test-org/timestamp-split-range ()
881 "Test `org-timestamp-split-range' specifications."
882 ;; Extract range start (active).
883 (should
884 (equal '(2012 3 29)
885 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
886 (let ((ts (org-timestamp-split-range (org-element-context))))
887 (mapcar (lambda (p) (org-element-property p ts))
888 '(:year-end :month-end :day-end))))))
889 ;; Extract range start (inactive)
890 (should
891 (equal '(2012 3 29)
892 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
893 (let ((ts (org-timestamp-split-range (org-element-context))))
894 (mapcar (lambda (p) (org-element-property p ts))
895 '(:year-end :month-end :day-end))))))
896 ;; Extract range end (active).
897 (should
898 (equal '(2012 3 30)
899 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
900 (let ((ts (org-timestamp-split-range
901 (org-element-context) t)))
902 (mapcar (lambda (p) (org-element-property p ts))
903 '(:year-end :month-end :day-end))))))
904 ;; Extract range end (inactive)
905 (should
906 (equal '(2012 3 30)
907 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
908 (let ((ts (org-timestamp-split-range
909 (org-element-context) t)))
910 (mapcar (lambda (p) (org-element-property p ts))
911 '(:year-end :month-end :day-end))))))
912 ;; Return the timestamp if not a range.
913 (should
914 (org-test-with-temp-text "[2012-03-29 Thu]"
915 (let* ((ts-orig (org-element-context))
916 (ts-copy (org-timestamp-split-range ts-orig)))
917 (eq ts-orig ts-copy))))
918 (should
919 (org-test-with-temp-text "<%%(org-float t 4 2)>"
920 (let* ((ts-orig (org-element-context))
921 (ts-copy (org-timestamp-split-range ts-orig)))
922 (eq ts-orig ts-copy))))
923 ;; Check that parent is the same when a range was split.
924 (should
925 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
926 (let* ((ts-orig (org-element-context))
927 (ts-copy (org-timestamp-split-range ts-orig)))
928 (eq (org-element-property :parent ts-orig)
929 (org-element-property :parent ts-copy))))))
931 (ert-deftest test-org/timestamp-translate ()
932 "Test `org-timestamp-translate' specifications."
933 ;; Translate whole date range.
934 (should
935 (equal "<29>--<30>"
936 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
937 (let ((org-display-custom-times t)
938 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
939 (org-timestamp-translate (org-element-context))))))
940 ;; Translate date range start.
941 (should
942 (equal "<29>"
943 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
944 (let ((org-display-custom-times t)
945 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
946 (org-timestamp-translate (org-element-context) 'start)))))
947 ;; Translate date range end.
948 (should
949 (equal "<30>"
950 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
951 (let ((org-display-custom-times t)
952 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
953 (org-timestamp-translate (org-element-context) 'end)))))
954 ;; Translate time range.
955 (should
956 (equal "<08>--<16>"
957 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
958 (let ((org-display-custom-times t)
959 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
960 (org-timestamp-translate (org-element-context))))))
961 ;; Translate non-range timestamp.
962 (should
963 (equal "<29>"
964 (org-test-with-temp-text "<2012-03-29 Thu>"
965 (let ((org-display-custom-times t)
966 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
967 (org-timestamp-translate (org-element-context))))))
968 ;; Do not change `diary' timestamps.
969 (should
970 (equal "<%%(org-float t 4 2)>"
971 (org-test-with-temp-text "<%%(org-float t 4 2)>"
972 (let ((org-display-custom-times t)
973 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
974 (org-timestamp-translate (org-element-context)))))))
978 ;;; Targets and Radio Targets
980 (ert-deftest test-org/all-targets ()
981 "Test `org-all-targets' specifications."
982 ;; Without an argument.
983 (should
984 (equal '("radio-target" "target")
985 (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
986 (org-all-targets))))
987 (should
988 (equal '("radio-target")
989 (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
990 ;; With argument.
991 (should
992 (equal '("radio-target")
993 (org-test-with-temp-text "<<target>> <<<radio-target>>>"
994 (org-all-targets t)))))
997 (provide 'test-org)
999 ;;; test-org.el ends here