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