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