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