Improve `completion-at-point' for `org-contacts.el' in mail
[org-mode.git] / testing / lisp / test-org.el
blobdac5fd264a2647feaa28134bef8621ce86d12970
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.")))))
201 (ert-deftest test-org/auto-fill-function ()
202 "Test auto-filling features."
203 ;; Auto fill paragraph.
204 (should
205 (equal "12345\n7890"
206 (org-test-with-temp-text "12345 7890"
207 (let ((fill-column 5))
208 (end-of-line)
209 (org-auto-fill-function)
210 (buffer-string)))))
211 ;; Auto fill first paragraph in an item.
212 (should
213 (equal "- 12345\n 7890"
214 (org-test-with-temp-text "- 12345 7890"
215 (let ((fill-column 7))
216 (end-of-line)
217 (org-auto-fill-function)
218 (buffer-string)))))
219 ;; Auto fill comments.
220 (should
221 (equal " # 12345\n # 7890"
222 (org-test-with-temp-text " # 12345 7890"
223 (let ((fill-column 10))
224 (end-of-line)
225 (org-auto-fill-function)
226 (buffer-string)))))
227 ;; A hash within a line isn't a comment.
228 (should-not
229 (equal "12345 # 7890\n# 1"
230 (org-test-with-temp-text "12345 # 7890 1"
231 (let ((fill-column 12))
232 (end-of-line)
233 (org-auto-fill-function)
234 (buffer-string)))))
235 ;; Correctly interpret empty prefix.
236 (should-not
237 (equal "# a\n# b\nRegular\n# paragraph"
238 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
239 (let ((fill-column 12))
240 (end-of-line 3)
241 (org-auto-fill-function)
242 (buffer-string)))))
243 ;; Comment block: auto fill contents.
244 (should
245 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
246 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
247 (let ((fill-column 5))
248 (forward-line)
249 (end-of-line)
250 (org-auto-fill-function)
251 (buffer-string)))))
252 (should
253 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
254 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
255 (let ((fill-column 5))
256 (forward-line)
257 (end-of-line)
258 (org-auto-fill-function)
259 (buffer-string)))))
260 ;; Do not fill if a new item could be created.
261 (should-not
262 (equal "12345\n- 90"
263 (org-test-with-temp-text "12345 - 90"
264 (let ((fill-column 5))
265 (end-of-line)
266 (org-auto-fill-function)
267 (buffer-string)))))
268 ;; Do not fill if a line break could be introduced.
269 (should-not
270 (equal "123\\\\\n7890"
271 (org-test-with-temp-text "123\\\\ 7890"
272 (let ((fill-column 6))
273 (end-of-line)
274 (org-auto-fill-function)
275 (buffer-string)))))
276 ;; Do not fill affiliated keywords.
277 (should-not
278 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
279 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
280 (let ((fill-column 20))
281 (end-of-line)
282 (org-auto-fill-function)
283 (buffer-string))))))
287 ;;; Links
289 ;;;; Fuzzy Links
291 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
292 ;; a target keyword (aka an invisible target: #+TARGET: text), to
293 ;; a named element (#+name: text) and to headlines (* Text).
295 (ert-deftest test-org/fuzzy-links ()
296 "Test fuzzy links specifications."
297 ;; 1. Fuzzy link goes in priority to a matching target.
298 (org-test-with-temp-text
299 "#+TARGET: Test\n#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
300 (goto-line 6)
301 (org-open-at-point)
302 (should (looking-at "<<Test>>")))
303 ;; 2. Fuzzy link should then go to a matching target keyword.
304 (org-test-with-temp-text
305 "#+NAME: Test\n|a|b|\n#+TARGET: Test\n* Test\n[[Test]]"
306 (goto-line 5)
307 (org-open-at-point)
308 (should (looking-at "#\\+TARGET: Test")))
309 ;; 3. Then fuzzy link points to an element with a given name.
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 (should (looking-at "#\\+NAME: Test")))
314 ;; 4. A target still lead to a matching headline otherwise.
315 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
316 (goto-line 4)
317 (org-open-at-point)
318 (should (looking-at "\\* Head2")))
319 ;; 5. With a leading star in link, enforce heading match.
320 (org-test-with-temp-text "#+TARGET: Test\n* Test\n<<Test>>\n[[*Test]]"
321 (goto-line 4)
322 (org-open-at-point)
323 (should (looking-at "\\* Test"))))
326 ;;;; Link Escaping
328 (ert-deftest test-org/org-link-escape-ascii-character ()
329 "Escape an ascii character."
330 (should
331 (string=
332 "%5B"
333 (org-link-escape "["))))
335 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
336 "Escape an ascii control character."
337 (should
338 (string=
339 "%09"
340 (org-link-escape "\t"))))
342 (ert-deftest test-org/org-link-escape-multibyte-character ()
343 "Escape an unicode multibyte character."
344 (should
345 (string=
346 "%E2%82%AC"
347 (org-link-escape "€"))))
349 (ert-deftest test-org/org-link-escape-custom-table ()
350 "Escape string with custom character table."
351 (should
352 (string=
353 "Foo%3A%42ar%0A"
354 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
356 (ert-deftest test-org/org-link-escape-custom-table-merge ()
357 "Escape string with custom table merged with default table."
358 (should
359 (string=
360 "%5BF%6F%6F%3A%42ar%0A%5D"
361 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
363 (ert-deftest test-org/org-link-unescape-ascii-character ()
364 "Unescape an ascii character."
365 (should
366 (string=
368 (org-link-unescape "%5B"))))
370 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
371 "Unescpae an ascii control character."
372 (should
373 (string=
374 "\n"
375 (org-link-unescape "%0A"))))
377 (ert-deftest test-org/org-link-unescape-multibyte-character ()
378 "Unescape unicode multibyte character."
379 (should
380 (string=
381 "€"
382 (org-link-unescape "%E2%82%AC"))))
384 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
385 "Unescape old style percent escaped character."
386 (should
387 (string=
388 "àâçèéêîôùû"
389 (decode-coding-string (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
391 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
392 "Escape and unscape a URL that includes an escaped char.
393 http://article.gmane.org/gmane.emacs.orgmode/21459/"
394 (should
395 (string=
396 "http://some.host.com/form?&id=blah%2Bblah25"
397 (org-link-unescape (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
401 ;;; Macros
403 (ert-deftest test-org/macro-replace-all ()
404 "Test `org-macro-replace-all' specifications."
405 ;; Standard test.
406 (should
407 (equal
408 "#+MACRO: A B\n1 B 3"
409 (org-test-with-temp-text "#+MACRO: A B\n1 {{{A}}} 3"
410 (progn (org-macro-initialize-templates)
411 (org-macro-replace-all org-macro-templates)
412 (buffer-string)))))
413 ;; Macro with arguments.
414 (should
415 (equal
416 "#+MACRO: macro $1 $2\nsome text"
417 (org-test-with-temp-text "#+MACRO: macro $1 $2\n{{{macro(some,text)}}}"
418 (progn (org-macro-initialize-templates)
419 (org-macro-replace-all org-macro-templates)
420 (buffer-string)))))
421 ;; Macro with "eval".
422 (should
423 (equal
424 "#+MACRO: add (eval (+ $1 $2))\n3"
425 (org-test-with-temp-text "#+MACRO: add (eval (+ $1 $2))\n{{{add(1,2)}}}"
426 (progn (org-macro-initialize-templates)
427 (org-macro-replace-all org-macro-templates)
428 (buffer-string)))))
429 ;; Nested macros.
430 (should
431 (equal
432 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\ninner outer"
433 (org-test-with-temp-text
434 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\n{{{out}}}"
435 (progn (org-macro-initialize-templates)
436 (org-macro-replace-all org-macro-templates)
437 (buffer-string)))))
438 ;; Error out when macro expansion is circular.
439 (should-error
440 (org-test-with-temp-text
441 "#+MACRO: mac1 {{{mac2}}}\n#+MACRO: mac2 {{{mac1}}}\n{{{mac1}}}"
442 (org-macro-initialize-templates)
443 (org-macro-replace-all org-macro-templates))))
447 ;;; Node Properties
449 (ert-deftest test-org/accumulated-properties-in-drawers ()
450 "Ensure properties accumulate in subtree drawers."
451 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
452 (org-babel-next-src-block)
453 (should (equal '(2 1) (org-babel-execute-src-block)))))
457 ;;; Mark Region
459 (ert-deftest test-org/mark-subtree ()
460 "Test `org-mark-subtree' specifications."
461 ;; Error when point is before first headline.
462 (should-error
463 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
464 (progn (transient-mark-mode 1)
465 (org-mark-subtree))))
466 ;; Without argument, mark current subtree.
467 (should
468 (equal
469 '(12 32)
470 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
471 (progn (transient-mark-mode 1)
472 (forward-line 2)
473 (org-mark-subtree)
474 (list (region-beginning) (region-end))))))
475 ;; With an argument, move ARG up.
476 (should
477 (equal
478 '(1 32)
479 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
480 (progn (transient-mark-mode 1)
481 (forward-line 2)
482 (org-mark-subtree 1)
483 (list (region-beginning) (region-end))))))
484 ;; Do not get fooled by inlinetasks.
485 (when (featurep 'org-inlinetask)
486 (should
487 (= 1
488 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
489 (progn (transient-mark-mode 1)
490 (forward-line 1)
491 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
492 (region-beginning)))))))
496 ;;; Navigation
498 (ert-deftest test-org/beginning-of-line ()
499 "Test `org-beginning-of-line' specifications."
500 ;; Standard test.
501 (should
502 (org-test-with-temp-text "Some text\nSome other text"
503 (progn (org-beginning-of-line) (bolp))))
504 ;; Standard test with `visual-line-mode'.
505 (should-not
506 (org-test-with-temp-text "A long line of text\nSome other text"
507 (progn (visual-line-mode)
508 (forward-char 2)
509 (dotimes (i 1000) (insert "very "))
510 (org-beginning-of-line)
511 (bolp))))
512 ;; At an headline with special movement.
513 (should
514 (org-test-with-temp-text "* TODO Headline"
515 (let ((org-special-ctrl-a/e t))
516 (org-end-of-line)
517 (and (progn (org-beginning-of-line) (looking-at "Headline"))
518 (progn (org-beginning-of-line) (bolp))
519 (progn (org-beginning-of-line) (looking-at "Headline")))))))
521 (ert-deftest test-org/end-of-line ()
522 "Test `org-end-of-line' specifications."
523 ;; Standard test.
524 (should
525 (org-test-with-temp-text "Some text\nSome other text"
526 (progn (org-end-of-line) (eolp))))
527 ;; Standard test with `visual-line-mode'.
528 (should-not
529 (org-test-with-temp-text "A long line of text\nSome other text"
530 (progn (visual-line-mode)
531 (forward-char 2)
532 (dotimes (i 1000) (insert "very "))
533 (goto-char (point-min))
534 (org-end-of-line)
535 (eolp))))
536 ;; At an headline with special movement.
537 (should
538 (org-test-with-temp-text "* Headline1 :tag:\n"
539 (let ((org-special-ctrl-a/e t))
540 (and (progn (org-end-of-line) (looking-at " :tag:"))
541 (progn (org-end-of-line) (eolp))
542 (progn (org-end-of-line) (looking-at " :tag:"))))))
543 ;; At an headline without special movement.
544 (should
545 (org-test-with-temp-text "* Headline2 :tag:\n"
546 (let ((org-special-ctrl-a/e nil))
547 (and (progn (org-end-of-line) (eolp))
548 (progn (org-end-of-line) (eolp))))))
549 ;; At an headline, with reversed movement.
550 (should
551 (org-test-with-temp-text "* Headline3 :tag:\n"
552 (let ((org-special-ctrl-a/e 'reversed)
553 (this-command last-command))
554 (and (progn (org-end-of-line) (eolp))
555 (progn (org-end-of-line) (looking-at " :tag:"))))))
556 ;; At a block without hidden contents.
557 (should
558 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
559 (progn (org-end-of-line) (eolp))))
560 ;; At a block with hidden contents.
561 (should-not
562 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
563 (let ((org-special-ctrl-a/e t))
564 (org-hide-block-toggle)
565 (org-end-of-line)
566 (eobp)))))
568 (ert-deftest test-org/forward-element ()
569 "Test `org-forward-element' specifications."
570 ;; 1. At EOB: should error.
571 (org-test-with-temp-text "Some text\n"
572 (goto-char (point-max))
573 (should-error (org-forward-element)))
574 ;; 2. Standard move: expected to ignore blank lines.
575 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
576 (org-forward-element)
577 (should (looking-at (regexp-quote "Second paragraph."))))
578 ;; 3. Headline tests.
579 (org-test-with-temp-text "
580 * Head 1
581 ** Head 1.1
582 *** Head 1.1.1
583 ** Head 1.2"
584 ;; 3.1. At an headline beginning: move to next headline at the
585 ;; same level.
586 (goto-line 3)
587 (org-forward-element)
588 (should (looking-at (regexp-quote "** Head 1.2")))
589 ;; 3.2. At an headline beginning: move to parent headline if no
590 ;; headline at the same level.
591 (goto-line 3)
592 (org-forward-element)
593 (should (looking-at (regexp-quote "** Head 1.2"))))
594 ;; 4. Greater element tests.
595 (org-test-with-temp-text
596 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
597 ;; 4.1. At a greater element: expected to skip contents.
598 (org-forward-element)
599 (should (looking-at (regexp-quote "Outside.")))
600 ;; 4.2. At the end of greater element contents: expected to skip
601 ;; to the end of the greater element.
602 (goto-line 2)
603 (org-forward-element)
604 (should (looking-at (regexp-quote "Outside."))))
605 ;; 5. List tests.
606 (org-test-with-temp-text "
607 - item1
609 - sub1
611 - sub2
613 - sub3
615 Inner paragraph.
617 - item2
619 Outside."
620 ;; 5.1. At list top point: expected to move to the element after
621 ;; the list.
622 (goto-line 2)
623 (org-forward-element)
624 (should (looking-at (regexp-quote "Outside.")))
625 ;; 5.2. Special case: at the first line of a sub-list, but not at
626 ;; beginning of line, move to next item.
627 (goto-line 2)
628 (forward-char)
629 (org-forward-element)
630 (should (looking-at "- item2"))
631 (goto-line 4)
632 (forward-char)
633 (org-forward-element)
634 (should (looking-at " - sub2"))
635 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
636 (goto-line 4)
637 (org-forward-element)
638 (should (looking-at (regexp-quote " Inner paragraph.")))
639 ;; 5.4. At sub-list end: expected to move outside the sub-list.
640 (goto-line 8)
641 (org-forward-element)
642 (should (looking-at (regexp-quote " Inner paragraph.")))
643 ;; 5.5. At an item: expected to move to next item, if any.
644 (goto-line 6)
645 (org-forward-element)
646 (should (looking-at " - sub3"))))
648 (ert-deftest test-org/backward-element ()
649 "Test `org-backward-element' specifications."
650 ;; 1. Should error at BOB.
651 (org-test-with-temp-text " \nParagraph."
652 (should-error (org-backward-element)))
653 ;; 2. Should move at BOB when called on the first element in buffer.
654 (should
655 (org-test-with-temp-text "\n#+TITLE: test"
656 (progn (forward-line)
657 (org-backward-element)
658 (bobp))))
659 ;; 3. Not at the beginning of an element: move at its beginning.
660 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
661 (goto-line 3)
662 (end-of-line)
663 (org-backward-element)
664 (should (looking-at (regexp-quote "Paragraph2."))))
665 ;; 4. Headline tests.
666 (org-test-with-temp-text "
667 * Head 1
668 ** Head 1.1
669 *** Head 1.1.1
670 ** Head 1.2"
671 ;; 4.1. At an headline beginning: move to previous headline at the
672 ;; same level.
673 (goto-line 5)
674 (org-backward-element)
675 (should (looking-at (regexp-quote "** Head 1.1")))
676 ;; 4.2. At an headline beginning: move to parent headline if no
677 ;; headline at the same level.
678 (goto-line 3)
679 (org-backward-element)
680 (should (looking-at (regexp-quote "* Head 1")))
681 ;; 4.3. At the first top-level headline: should error.
682 (goto-line 2)
683 (should-error (org-backward-element)))
684 ;; 5. At beginning of first element inside a greater element:
685 ;; expected to move to greater element's beginning.
686 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
687 (goto-line 3)
688 (org-backward-element)
689 (should (looking-at "#\\+BEGIN_CENTER")))
690 ;; 6. At the beginning of the first element in a section: should
691 ;; move back to headline, if any.
692 (should
693 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
694 (progn (goto-char (point-max))
695 (beginning-of-line)
696 (org-backward-element)
697 (org-at-heading-p))))
698 ;; 7. List tests.
699 (org-test-with-temp-text "
700 - item1
702 - sub1
704 - sub2
706 - sub3
708 Inner paragraph.
710 - item2
713 Outside."
714 ;; 7.1. At beginning of sub-list: expected to move to the
715 ;; paragraph before it.
716 (goto-line 4)
717 (org-backward-element)
718 (should (looking-at "item1"))
719 ;; 7.2. At an item in a list: expected to move at previous item.
720 (goto-line 8)
721 (org-backward-element)
722 (should (looking-at " - sub2"))
723 (goto-line 12)
724 (org-backward-element)
725 (should (looking-at "- item1"))
726 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
727 ;; beginning.
728 (goto-line 10)
729 (org-backward-element)
730 (should (looking-at " - sub1"))
731 (goto-line 15)
732 (org-backward-element)
733 (should (looking-at "- item1"))
734 ;; 7.4. At blank-lines before list end: expected to move to top
735 ;; item.
736 (goto-line 14)
737 (org-backward-element)
738 (should (looking-at "- item1"))))
740 (ert-deftest test-org/up-element ()
741 "Test `org-up-element' specifications."
742 ;; 1. At BOB or with no surrounding element: should error.
743 (org-test-with-temp-text "Paragraph."
744 (should-error (org-up-element)))
745 (org-test-with-temp-text "* Head1\n* Head2"
746 (goto-line 2)
747 (should-error (org-up-element)))
748 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
749 (goto-line 3)
750 (should-error (org-up-element)))
751 ;; 2. At an headline: move to parent headline.
752 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
753 (goto-line 3)
754 (org-up-element)
755 (should (looking-at "\\* Head1")))
756 ;; 3. Inside a greater element: move to greater element beginning.
757 (org-test-with-temp-text
758 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
759 (goto-line 3)
760 (org-up-element)
761 (should (looking-at "#\\+BEGIN_CENTER")))
762 ;; 4. List tests.
763 (org-test-with-temp-text "* Top
764 - item1
766 - sub1
768 - sub2
770 Paragraph within sub2.
772 - item2"
773 ;; 4.1. Within an item: move to the item beginning.
774 (goto-line 8)
775 (org-up-element)
776 (should (looking-at " - sub2"))
777 ;; 4.2. At an item in a sub-list: move to parent item.
778 (goto-line 4)
779 (org-up-element)
780 (should (looking-at "- item1"))
781 ;; 4.3. At an item in top list: move to beginning of whole list.
782 (goto-line 10)
783 (org-up-element)
784 (should (looking-at "- item1"))
785 ;; 4.4. Special case. At very top point: should move to parent of
786 ;; list.
787 (goto-line 2)
788 (org-up-element)
789 (should (looking-at "\\* Top"))))
791 (ert-deftest test-org/down-element ()
792 "Test `org-down-element' specifications."
793 ;; Error when the element hasn't got a recursive type.
794 (org-test-with-temp-text "Paragraph."
795 (should-error (org-down-element)))
796 ;; Error when the element has no contents
797 (org-test-with-temp-text "* Headline"
798 (should-error (org-down-element)))
799 ;; When at a plain-list, move to first item.
800 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
801 (goto-line 2)
802 (org-down-element)
803 (should (looking-at " - Item 1.1")))
804 (org-test-with-temp-text "#+NAME: list\n- Item 1"
805 (org-down-element)
806 (should (looking-at " Item 1")))
807 ;; When at a table, move to first row
808 (org-test-with-temp-text "#+NAME: table\n| a | b |"
809 (org-down-element)
810 (should (looking-at " a | b |")))
811 ;; Otherwise, move inside the greater element.
812 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
813 (org-down-element)
814 (should (looking-at "Paragraph"))))
816 (ert-deftest test-org/drag-element-backward ()
817 "Test `org-drag-element-backward' specifications."
818 ;; 1. Error when trying to move first element of buffer.
819 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
820 (should-error (org-drag-element-backward)))
821 ;; 2. Error when trying to swap nested elements.
822 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
823 (forward-line)
824 (should-error (org-drag-element-backward)))
825 ;; 3. Error when trying to swap an headline element and
826 ;; a non-headline element.
827 (org-test-with-temp-text "Test.\n* Head 1"
828 (forward-line)
829 (should-error (org-drag-element-backward)))
830 ;; 4. Otherwise, swap elements, preserving column and blank lines
831 ;; between elements.
832 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
833 (search-forward "graph")
834 (org-drag-element-backward)
835 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
836 (should (looking-at " 2")))
837 ;; 5. Preserve visibility of elements and their contents.
838 (org-test-with-temp-text "
839 #+BEGIN_CENTER
840 Text.
841 #+END_CENTER
842 - item 1
843 #+BEGIN_QUOTE
844 Text.
845 #+END_QUOTE"
846 (while (search-forward "BEGIN_" nil t) (org-cycle))
847 (search-backward "- item 1")
848 (org-drag-element-backward)
849 (should
850 (equal
851 '((63 . 82) (26 . 48))
852 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
853 (overlays-in (point-min) (point-max)))))))
855 (ert-deftest test-org/drag-element-forward ()
856 "Test `org-drag-element-forward' specifications."
857 ;; 1. Error when trying to move first element of buffer.
858 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
859 (goto-line 3)
860 (should-error (org-drag-element-forward)))
861 ;; 2. Error when trying to swap nested elements.
862 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
863 (forward-line)
864 (should-error (org-drag-element-forward)))
865 ;; 3. Error when trying to swap a non-headline element and an
866 ;; headline.
867 (org-test-with-temp-text "Test.\n* Head 1"
868 (should-error (org-drag-element-forward)))
869 ;; 4. Otherwise, swap elements, preserving column and blank lines
870 ;; between elements.
871 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
872 (search-forward "graph")
873 (org-drag-element-forward)
874 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
875 (should (looking-at " 1")))
876 ;; 5. Preserve visibility of elements and their contents.
877 (org-test-with-temp-text "
878 #+BEGIN_CENTER
879 Text.
880 #+END_CENTER
881 - item 1
882 #+BEGIN_QUOTE
883 Text.
884 #+END_QUOTE"
885 (while (search-forward "BEGIN_" nil t) (org-cycle))
886 (search-backward "#+BEGIN_CENTER")
887 (org-drag-element-forward)
888 (should
889 (equal
890 '((63 . 82) (26 . 48))
891 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
892 (overlays-in (point-min) (point-max)))))))
896 ;;; Planning
898 (ert-deftest test-org/timestamp-has-time-p ()
899 "Test `org-timestamp-has-time-p' specifications."
900 ;; With time.
901 (should
902 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
903 (org-timestamp-has-time-p (org-element-context))))
904 ;; Without time.
905 (should-not
906 (org-test-with-temp-text "<2012-03-29 Thu>"
907 (org-timestamp-has-time-p (org-element-context)))))
909 (ert-deftest test-org/timestamp-format ()
910 "Test `org-timestamp-format' specifications."
911 ;; Regular test.
912 (should
913 (equal
914 "2012-03-29 16:40"
915 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
916 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
917 ;; Range end.
918 (should
919 (equal
920 "2012-03-29"
921 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
922 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
924 (ert-deftest test-org/timestamp-split-range ()
925 "Test `org-timestamp-split-range' specifications."
926 ;; Extract range start (active).
927 (should
928 (equal '(2012 3 29)
929 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
930 (let ((ts (org-timestamp-split-range (org-element-context))))
931 (mapcar (lambda (p) (org-element-property p ts))
932 '(:year-end :month-end :day-end))))))
933 ;; Extract range start (inactive)
934 (should
935 (equal '(2012 3 29)
936 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
937 (let ((ts (org-timestamp-split-range (org-element-context))))
938 (mapcar (lambda (p) (org-element-property p ts))
939 '(:year-end :month-end :day-end))))))
940 ;; Extract range end (active).
941 (should
942 (equal '(2012 3 30)
943 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
944 (let ((ts (org-timestamp-split-range
945 (org-element-context) t)))
946 (mapcar (lambda (p) (org-element-property p ts))
947 '(:year-end :month-end :day-end))))))
948 ;; Extract range end (inactive)
949 (should
950 (equal '(2012 3 30)
951 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
952 (let ((ts (org-timestamp-split-range
953 (org-element-context) t)))
954 (mapcar (lambda (p) (org-element-property p ts))
955 '(:year-end :month-end :day-end))))))
956 ;; Return the timestamp if not a range.
957 (should
958 (org-test-with-temp-text "[2012-03-29 Thu]"
959 (let* ((ts-orig (org-element-context))
960 (ts-copy (org-timestamp-split-range ts-orig)))
961 (eq ts-orig ts-copy))))
962 (should
963 (org-test-with-temp-text "<%%(org-float t 4 2)>"
964 (let* ((ts-orig (org-element-context))
965 (ts-copy (org-timestamp-split-range ts-orig)))
966 (eq ts-orig ts-copy))))
967 ;; Check that parent is the same when a range was split.
968 (should
969 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
970 (let* ((ts-orig (org-element-context))
971 (ts-copy (org-timestamp-split-range ts-orig)))
972 (eq (org-element-property :parent ts-orig)
973 (org-element-property :parent ts-copy))))))
975 (ert-deftest test-org/timestamp-translate ()
976 "Test `org-timestamp-translate' specifications."
977 ;; Translate whole date range.
978 (should
979 (equal "<29>--<30>"
980 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
981 (let ((org-display-custom-times t)
982 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
983 (org-timestamp-translate (org-element-context))))))
984 ;; Translate date range start.
985 (should
986 (equal "<29>"
987 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
988 (let ((org-display-custom-times t)
989 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
990 (org-timestamp-translate (org-element-context) 'start)))))
991 ;; Translate date range end.
992 (should
993 (equal "<30>"
994 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
995 (let ((org-display-custom-times t)
996 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
997 (org-timestamp-translate (org-element-context) 'end)))))
998 ;; Translate time range.
999 (should
1000 (equal "<08>--<16>"
1001 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
1002 (let ((org-display-custom-times t)
1003 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
1004 (org-timestamp-translate (org-element-context))))))
1005 ;; Translate non-range timestamp.
1006 (should
1007 (equal "<29>"
1008 (org-test-with-temp-text "<2012-03-29 Thu>"
1009 (let ((org-display-custom-times t)
1010 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1011 (org-timestamp-translate (org-element-context))))))
1012 ;; Do not change `diary' timestamps.
1013 (should
1014 (equal "<%%(org-float t 4 2)>"
1015 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1016 (let ((org-display-custom-times t)
1017 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1018 (org-timestamp-translate (org-element-context)))))))
1022 ;;; Targets and Radio Targets
1024 (ert-deftest test-org/all-targets ()
1025 "Test `org-all-targets' specifications."
1026 ;; Without an argument.
1027 (should
1028 (equal '("radio-target" "target")
1029 (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
1030 (org-all-targets))))
1031 (should
1032 (equal '("radio-target")
1033 (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
1034 ;; With argument.
1035 (should
1036 (equal '("radio-target")
1037 (org-test-with-temp-text "<<target>> <<<radio-target>>>"
1038 (org-all-targets t)))))
1041 (provide 'test-org)
1043 ;;; test-org.el ends here