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