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