Fix indentation bug
[org-mode.git] / testing / lisp / test-org.el
blob0c0a506c81eb352ed7cd4fbdae81fa65b091e793
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/toggle-comment ()
31 "Test `org-toggle-comment' specifications."
32 ;; Simple headline.
33 (should
34 (equal "* Test"
35 (org-test-with-temp-text "* COMMENT Test"
36 (org-toggle-comment)
37 (buffer-string))))
38 (should
39 (equal "* COMMENT Test"
40 (org-test-with-temp-text "* Test"
41 (org-toggle-comment)
42 (buffer-string))))
43 ;; Headline with a regular keyword.
44 (should
45 (equal "* TODO Test"
46 (org-test-with-temp-text "* TODO COMMENT Test"
47 (org-toggle-comment)
48 (buffer-string))))
49 (should
50 (equal "* TODO COMMENT Test"
51 (org-test-with-temp-text "* TODO Test"
52 (org-toggle-comment)
53 (buffer-string))))
54 ;; Empty headline.
55 (should
56 (equal "* "
57 (org-test-with-temp-text "* COMMENT"
58 (org-toggle-comment)
59 (buffer-string))))
60 (should
61 (equal "* COMMENT"
62 (org-test-with-temp-text "* "
63 (org-toggle-comment)
64 (buffer-string))))
65 ;; Headline with a single keyword.
66 (should
67 (equal "* TODO "
68 (org-test-with-temp-text "* TODO COMMENT"
69 (org-toggle-comment)
70 (buffer-string))))
71 (should
72 (equal "* TODO COMMENT"
73 (org-test-with-temp-text "* TODO"
74 (org-toggle-comment)
75 (buffer-string))))
76 ;; Headline with a keyword, a priority cookie and contents.
77 (should
78 (equal "* TODO [#A] Headline"
79 (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
80 (org-toggle-comment)
81 (buffer-string))))
82 (should
83 (equal "* TODO [#A] COMMENT Headline"
84 (org-test-with-temp-text "* TODO [#A] Headline"
85 (org-toggle-comment)
86 (buffer-string)))))
88 (ert-deftest test-org/comment-dwim ()
89 "Test `comment-dwim' behaviour in an Org buffer."
90 ;; No region selected, no comment on current line and line not
91 ;; empty: insert comment on line above.
92 (should
93 (equal "# \nComment"
94 (org-test-with-temp-text "Comment"
95 (progn (call-interactively 'comment-dwim)
96 (buffer-string)))))
97 ;; No region selected, no comment on current line and line empty:
98 ;; insert comment on this line.
99 (should
100 (equal "# \nParagraph"
101 (org-test-with-temp-text "\nParagraph"
102 (progn (call-interactively 'comment-dwim)
103 (buffer-string)))))
104 ;; No region selected, and a comment on this line: indent it.
105 (should
106 (equal "* Headline\n # Comment"
107 (org-test-with-temp-text "* Headline\n# Comment"
108 (progn (forward-line)
109 (let ((org-adapt-indentation t))
110 (call-interactively 'comment-dwim))
111 (buffer-string)))))
112 ;; Also recognize single # at column 0 as comments.
113 (should
114 (equal "# Comment"
115 (org-test-with-temp-text "# Comment"
116 (progn (forward-line)
117 (call-interactively 'comment-dwim)
118 (buffer-string)))))
119 ;; Region selected and only comments and blank lines within it:
120 ;; un-comment all commented lines.
121 (should
122 (equal "Comment 1\n\nComment 2"
123 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
124 (progn
125 (transient-mark-mode 1)
126 (push-mark (point) t t)
127 (goto-char (point-max))
128 (call-interactively 'comment-dwim)
129 (buffer-string)))))
130 ;; Region selected without comments: comment all lines if
131 ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
132 (should
133 (equal "# Comment 1\n\n# Comment 2"
134 (org-test-with-temp-text "Comment 1\n\nComment 2"
135 (progn
136 (transient-mark-mode 1)
137 (push-mark (point) t t)
138 (goto-char (point-max))
139 (let ((comment-empty-lines nil))
140 (call-interactively 'comment-dwim))
141 (buffer-string)))))
142 (should
143 (equal "# Comment 1\n# \n# Comment 2"
144 (org-test-with-temp-text "Comment 1\n\nComment 2"
145 (progn
146 (transient-mark-mode 1)
147 (push-mark (point) t t)
148 (goto-char (point-max))
149 (let ((comment-empty-lines t))
150 (call-interactively 'comment-dwim))
151 (buffer-string)))))
152 ;; In front of a keyword without region, insert a new comment.
153 (should
154 (equal "# \n#+KEYWORD: value"
155 (org-test-with-temp-text "#+KEYWORD: value"
156 (progn (call-interactively 'comment-dwim)
157 (buffer-string)))))
158 ;; In a source block, use appropriate syntax.
159 (should
160 (equal " ;; "
161 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n\n#+END_SRC"
162 (forward-line)
163 (let ((org-edit-src-content-indentation 2))
164 (call-interactively 'comment-dwim))
165 (buffer-substring-no-properties (line-beginning-position) (point)))))
166 (should
167 (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
168 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\na\nb\n#+END_SRC"
169 (forward-line)
170 (transient-mark-mode 1)
171 (push-mark (point) t t)
172 (forward-line 2)
173 (let ((org-edit-src-content-indentation 2))
174 (call-interactively 'comment-dwim))
175 (buffer-string)))))
179 ;;; Date and time analysis
181 (ert-deftest test-org/org-read-date ()
182 "Test `org-read-date' specifications."
183 ;; Parse ISO date with abbreviated year and month.
184 (should (equal "2012-03-29 16:40"
185 (let ((org-time-was-given t))
186 (org-read-date t nil "12-3-29 16:40"))))
187 ;; Parse Europeans dates.
188 (should (equal "2012-03-29 16:40"
189 (let ((org-time-was-given t))
190 (org-read-date t nil "29.03.2012 16:40"))))
191 ;; Parse Europeans dates without year.
192 (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
193 (let ((org-time-was-given t))
194 (org-read-date t nil "29.03. 16:40"))))
195 ;; Relative date applied to current time if there is single
196 ;; plus/minus, or to default date when there are two of them.
197 (should
198 (equal
199 "2015-03-04"
200 (flet ((current-time () (apply #'encode-time
201 (org-parse-time-string "2014-03-04"))))
202 (org-read-date
203 t nil "+1y" nil
204 (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
205 (should
206 (equal
207 "2013-03-29"
208 (flet ((current-time () (apply #'encode-time
209 (org-parse-time-string "2014-03-04"))))
210 (org-read-date
211 t nil "++1y" nil
212 (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
213 ;; When `org-read-date-prefer-future' is non-nil, prefer future
214 ;; dates (relatively to now) when incomplete. Otherwise, use
215 ;; default date.
216 (should
217 (equal
218 "2014-04-01"
219 (flet ((current-time () (apply #'encode-time
220 (org-parse-time-string "2014-03-04"))))
221 (let ((org-read-date-prefer-future t))
222 (org-read-date t nil "1")))))
223 (should
224 (equal
225 "2013-03-04"
226 (flet ((current-time () (apply #'encode-time
227 (org-parse-time-string "2012-03-29"))))
228 (let ((org-read-date-prefer-future t))
229 (org-read-date t nil "3-4")))))
230 (should
231 (equal
232 "2012-03-04"
233 (flet ((current-time () (apply #'encode-time
234 (org-parse-time-string "2012-03-29"))))
235 (let ((org-read-date-prefer-future nil))
236 (org-read-date t nil "3-4")))))
237 ;; When set to `org-read-date-prefer-future' is set to `time', read
238 ;; day is moved to tomorrow if specified hour is before current
239 ;; time. However, it only happens in no other part of the date is
240 ;; specified.
241 (should
242 (equal
243 "2012-03-30"
244 (flet ((current-time () (apply #'encode-time
245 (org-parse-time-string "2012-03-29 16:40"))))
246 (let ((org-read-date-prefer-future 'time))
247 (org-read-date t nil "00:40" nil)))))
248 (should-not
249 (equal
250 "2012-03-30"
251 (flet ((current-time () (apply #'encode-time
252 (org-parse-time-string "2012-03-29 16:40"))))
253 (let ((org-read-date-prefer-future 'time))
254 (org-read-date t nil "29 00:40" nil)))))
255 ;; Caveat: `org-read-date-prefer-future' always refers to current
256 ;; time, not default time, when they differ.
257 (should
258 (equal
259 "2014-04-01"
260 (flet ((current-time
261 () (apply #'encode-time (org-parse-time-string "2014-03-04"))))
262 (let ((org-read-date-prefer-future t))
263 (org-read-date
264 t nil "1" nil
265 (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
266 (should
267 (equal
268 "2014-03-25"
269 (flet ((current-time
270 () (apply #'encode-time (org-parse-time-string "2014-03-04"))))
271 (let ((org-read-date-prefer-future t))
272 (org-read-date
273 t nil "25" nil
274 (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
276 (ert-deftest test-org/org-parse-time-string ()
277 "Test `org-parse-time-string'."
278 (should (equal (org-parse-time-string "2012-03-29 16:40")
279 '(0 40 16 29 3 2012 nil nil nil)))
280 (should (equal (org-parse-time-string "[2012-03-29 16:40]")
281 '(0 40 16 29 3 2012 nil nil nil)))
282 (should (equal (org-parse-time-string "<2012-03-29 16:40>")
283 '(0 40 16 29 3 2012 nil nil nil)))
284 (should (equal (org-parse-time-string "<2012-03-29>")
285 '(0 0 0 29 3 2012 nil nil nil)))
286 (should (equal (org-parse-time-string "<2012-03-29>" t)
287 '(0 nil nil 29 3 2012 nil nil nil))))
290 ;;; Drawers
292 (ert-deftest test-org/insert-property-drawer ()
293 "Test `org-insert-property-drawer' specifications."
294 ;; Error before first headline.
295 (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
296 ;; Insert drawer right after headline if there is no planning line,
297 ;; or after it otherwise.
298 (should
299 (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
300 (org-test-with-temp-text "* H\nParagraph<point>"
301 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
302 (buffer-string))))
303 (should
304 (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
305 (org-test-with-temp-text
306 "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
307 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
308 (buffer-string))))
309 ;; Indent inserted drawer.
310 (should
311 (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
312 (org-test-with-temp-text "* H\nParagraph<point>"
313 (let ((org-adapt-indentation t)) (org-insert-property-drawer))
314 (buffer-string))))
315 ;; Handle insertion at eob.
316 (should
317 (equal "* H\n:PROPERTIES:\n:END:\n"
318 (org-test-with-temp-text "* H"
319 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
320 (buffer-string))))
321 ;; Skip inlinetasks before point.
322 (when (featurep 'org-inlinetask)
323 (should
324 (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
325 (org-test-with-temp-text
326 "* H\n*************** I\n*************** END\nP<point>"
327 (let ((org-adapt-indentation nil)
328 (org-inlinetask-min-level 15))
329 (org-insert-property-drawer))
330 (buffer-string)))))
331 ;; Correctly set drawer in an inlinetask.
332 (when (featurep 'org-inlinetask)
333 (should
334 (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
335 (org-test-with-temp-text
336 "* H\n*************** I\nP<point>\n*************** END"
337 (let ((org-adapt-indentation nil)
338 (org-inlinetask-min-level 15))
339 (org-insert-property-drawer))
340 (buffer-string))))))
343 ;;; Filling
345 (ert-deftest test-org/fill-paragraph ()
346 "Test `org-fill-paragraph' specifications."
347 ;; At an Org table, align it.
348 (should
349 (equal "| a |\n"
350 (org-test-with-temp-text "|a|"
351 (org-fill-paragraph)
352 (buffer-string))))
353 (should
354 (equal "#+name: table\n| a |\n"
355 (org-test-with-temp-text "#+name: table\n| a |\n"
356 (org-fill-paragraph)
357 (buffer-string))))
358 ;; At a paragraph, preserve line breaks.
359 (org-test-with-temp-text "some \\\\\nlong\ntext"
360 (let ((fill-column 20))
361 (org-fill-paragraph)
362 (should (equal (buffer-string) "some \\\\\nlong text"))))
363 ;; Correctly fill a paragraph when point is at its very end.
364 (should
365 (equal "A B"
366 (org-test-with-temp-text "A\nB"
367 (let ((fill-column 20))
368 (goto-char (point-max))
369 (org-fill-paragraph)
370 (buffer-string)))))
371 ;; Correctly fill the last paragraph of a greater element.
372 (should
373 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
374 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
375 (let ((fill-column 8))
376 (forward-line)
377 (end-of-line)
378 (org-fill-paragraph)
379 (buffer-string)))))
380 ;; Correctly fill an element in a narrowed buffer.
381 (should
382 (equal "01234\n6"
383 (org-test-with-temp-text "01234 6789"
384 (let ((fill-column 5))
385 (narrow-to-region 1 8)
386 (org-fill-paragraph)
387 (buffer-string)))))
388 ;; Handle `adaptive-fill-regexp' in paragraphs.
389 (should
390 (equal "> a b"
391 (org-test-with-temp-text "> a\n> b"
392 (let ((fill-column 5)
393 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
394 (org-fill-paragraph)
395 (buffer-string)))))
396 ;; Special case: Fill first paragraph when point is at an item or
397 ;; a plain-list or a footnote reference.
398 (should
399 (equal "- A B"
400 (org-test-with-temp-text "- A\n B"
401 (let ((fill-column 20))
402 (org-fill-paragraph)
403 (buffer-string)))))
404 (should
405 (equal "[fn:1] A B"
406 (org-test-with-temp-text "[fn:1] A\nB"
407 (let ((fill-column 20))
408 (org-fill-paragraph)
409 (buffer-string)))))
410 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
411 (let ((fill-column 20))
412 (org-fill-paragraph)
413 (should (equal (buffer-string)
414 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
415 ;; Fill contents of `comment-block' elements.
416 (should
417 (equal
418 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
419 (let ((fill-column 20))
420 (forward-line)
421 (org-fill-paragraph)
422 (buffer-string)))
423 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
424 ;; Fill `comment' elements.
425 (should
426 (equal " # A B"
427 (org-test-with-temp-text " # A\n # B"
428 (let ((fill-column 20))
429 (org-fill-paragraph)
430 (buffer-string)))))
431 ;; Do not mix consecutive comments when filling one of them.
432 (should
433 (equal "# A B\n\n# C"
434 (org-test-with-temp-text "# A\n# B\n\n# C"
435 (let ((fill-column 20))
436 (org-fill-paragraph)
437 (buffer-string)))))
438 ;; Use commented empty lines as separators when filling comments.
439 (should
440 (equal "# A B\n#\n# C"
441 (org-test-with-temp-text "# A\n# B\n#\n# C"
442 (let ((fill-column 20))
443 (org-fill-paragraph)
444 (buffer-string)))))
445 ;; Handle `adaptive-fill-regexp' in comments.
446 (should
447 (equal "# > a b"
448 (org-test-with-temp-text "# > a\n# > b"
449 (let ((fill-column 20)
450 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
451 (org-fill-paragraph)
452 (buffer-string)))))
453 ;; Do nothing at affiliated keywords.
454 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
455 (let ((fill-column 20))
456 (org-fill-paragraph)
457 (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
458 ;; Do not move point after table when filling a table.
459 (should-not
460 (org-test-with-temp-text "| a | b |\n| c | d |\n"
461 (forward-char)
462 (org-fill-paragraph)
463 (eobp))))
465 (ert-deftest test-org/auto-fill-function ()
466 "Test auto-filling features."
467 ;; Auto fill paragraph.
468 (should
469 (equal "12345\n7890"
470 (org-test-with-temp-text "12345 7890"
471 (let ((fill-column 5))
472 (end-of-line)
473 (org-auto-fill-function)
474 (buffer-string)))))
475 ;; Auto fill first paragraph in an item.
476 (should
477 (equal "- 12345\n 7890"
478 (org-test-with-temp-text "- 12345 7890"
479 (let ((fill-column 7))
480 (end-of-line)
481 (org-auto-fill-function)
482 (buffer-string)))))
483 ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
484 (should
485 (equal "> 12345\n 7890"
486 (org-test-with-temp-text "> 12345 7890"
487 (let ((fill-column 10)
488 (adaptive-fill-regexp "[ \t]*>+[ \t]*")
489 (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
490 (end-of-line)
491 (org-auto-fill-function)
492 (buffer-string)))))
493 (should
494 (equal "> 12345\n> 12345\n> 7890"
495 (org-test-with-temp-text "> 12345\n> 12345 7890"
496 (let ((fill-column 10)
497 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
498 (goto-char (point-max))
499 (org-auto-fill-function)
500 (buffer-string)))))
501 (should-not
502 (equal " 12345\n *12345\n *12345"
503 (org-test-with-temp-text " 12345\n *12345 12345"
504 (let ((fill-column 10)
505 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
506 (goto-char (point-max))
507 (org-auto-fill-function)
508 (buffer-string)))))
509 ;; Auto fill comments.
510 (should
511 (equal " # 12345\n # 7890"
512 (org-test-with-temp-text " # 12345 7890"
513 (let ((fill-column 10))
514 (end-of-line)
515 (org-auto-fill-function)
516 (buffer-string)))))
517 ;; A hash within a line isn't a comment.
518 (should-not
519 (equal "12345 # 7890\n# 1"
520 (org-test-with-temp-text "12345 # 7890 1"
521 (let ((fill-column 12))
522 (end-of-line)
523 (org-auto-fill-function)
524 (buffer-string)))))
525 ;; Correctly interpret empty prefix.
526 (should-not
527 (equal "# a\n# b\nRegular\n# paragraph"
528 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
529 (let ((fill-column 12))
530 (end-of-line 3)
531 (org-auto-fill-function)
532 (buffer-string)))))
533 ;; Comment block: auto fill contents.
534 (should
535 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
536 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
537 (let ((fill-column 5))
538 (forward-line)
539 (end-of-line)
540 (org-auto-fill-function)
541 (buffer-string)))))
542 (should
543 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
544 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
545 (let ((fill-column 5))
546 (forward-line)
547 (end-of-line)
548 (org-auto-fill-function)
549 (buffer-string)))))
550 ;; Do not fill if a new item could be created.
551 (should-not
552 (equal "12345\n- 90"
553 (org-test-with-temp-text "12345 - 90"
554 (let ((fill-column 5))
555 (end-of-line)
556 (org-auto-fill-function)
557 (buffer-string)))))
558 ;; Do not fill if a line break could be introduced.
559 (should-not
560 (equal "123\\\\\n7890"
561 (org-test-with-temp-text "123\\\\ 7890"
562 (let ((fill-column 6))
563 (end-of-line)
564 (org-auto-fill-function)
565 (buffer-string)))))
566 ;; Do not fill affiliated keywords.
567 (should-not
568 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
569 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
570 (let ((fill-column 20))
571 (end-of-line)
572 (org-auto-fill-function)
573 (buffer-string))))))
577 ;;; Indentation
579 (ert-deftest test-org/indent-line ()
580 "Test `org-indent-line' specifications."
581 ;; Do not indent diary sexps, footnote definitions or headlines.
582 (should
583 (zerop
584 (org-test-with-temp-text "%%(org-calendar-holiday)"
585 (org-indent-line)
586 (org-get-indentation))))
587 (should
588 (zerop
589 (org-test-with-temp-text "[fn:1] fn"
590 (let ((org-adapt-indentation t)) (org-indent-line))
591 (org-get-indentation))))
592 (should
593 (zerop
594 (org-test-with-temp-text "* H"
595 (org-indent-line)
596 (org-get-indentation))))
597 ;; Do not indent before first headline.
598 (should
599 (zerop
600 (org-test-with-temp-text ""
601 (org-indent-line)
602 (org-get-indentation))))
603 ;; Indent according to headline level otherwise, unless
604 ;; `org-adapt-indentation' is nil.
605 (should
606 (= 2
607 (org-test-with-temp-text "* H\nA"
608 (forward-line)
609 (let ((org-adapt-indentation t)) (org-indent-line))
610 (org-get-indentation))))
611 (should
612 (= 2
613 (org-test-with-temp-text "* H\n\nA"
614 (forward-line)
615 (let ((org-adapt-indentation t)) (org-indent-line))
616 (org-get-indentation))))
617 (should
618 (zerop
619 (org-test-with-temp-text "* H\nA"
620 (forward-line)
621 (let ((org-adapt-indentation nil)) (org-indent-line))
622 (org-get-indentation))))
623 ;; Indenting preserves point position.
624 (should
625 (org-test-with-temp-text "* H\nAB"
626 (forward-line)
627 (forward-char)
628 (let ((org-adapt-indentation t)) (org-indent-line))
629 (looking-at "B")))
630 ;; Do not change indentation at an item.
631 (should
632 (= 1
633 (org-test-with-temp-text "* H\n - A"
634 (forward-line)
635 (let ((org-adapt-indentation t)) (org-indent-line))
636 (org-get-indentation))))
637 ;; On blank lines at the end of a list, indent like last element
638 ;; within it if the line is still in the list. If the last element
639 ;; is an item, indent like its contents. Otherwise, indent like the
640 ;; whole list.
641 (should
642 (= 4
643 (org-test-with-temp-text "* H\n- A\n - AA\n"
644 (goto-char (point-max))
645 (let ((org-adapt-indentation t)) (org-indent-line))
646 (org-get-indentation))))
647 (should
648 (= 4
649 (org-test-with-temp-text "* H\n- A\n -\n\n<point>"
650 (let ((org-adapt-indentation t)) (org-indent-line))
651 (org-get-indentation))))
652 (should
653 (zerop
654 (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
655 (goto-char (point-max))
656 (let ((org-adapt-indentation t)) (org-indent-line))
657 (org-get-indentation))))
658 (should
659 (= 4
660 (org-test-with-temp-text "* H\n- A\n - \n"
661 (goto-char (point-max))
662 (let ((org-adapt-indentation t)) (org-indent-line))
663 (org-get-indentation))))
664 (should
665 (= 2
666 (org-test-with-temp-text "- A\n B\n\n<point>"
667 (let ((org-adapt-indentation nil)) (org-indent-line))
668 (org-get-indentation))))
669 ;; Likewise, on a blank line at the end of a footnote definition,
670 ;; indent at column 0 if line belongs to the definition. Otherwise,
671 ;; indent like the definition itself.
672 (should
673 (zerop
674 (org-test-with-temp-text "* H\n[fn:1] Definition\n"
675 (goto-char (point-max))
676 (let ((org-adapt-indentation t)) (org-indent-line))
677 (org-get-indentation))))
678 (should
679 (zerop
680 (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
681 (goto-char (point-max))
682 (let ((org-adapt-indentation t)) (org-indent-line))
683 (org-get-indentation))))
684 ;; After the end of the contents of a greater element, indent like
685 ;; the beginning of the element.
686 (should
687 (= 1
688 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
689 (forward-line 2)
690 (org-indent-line)
691 (org-get-indentation))))
692 ;; On blank lines after a paragraph, indent like its last non-empty
693 ;; line.
694 (should
695 (= 1
696 (org-test-with-temp-text " Paragraph\n\n<point>"
697 (org-indent-line)
698 (org-get-indentation))))
699 ;; At the first line of an element, indent like previous element's
700 ;; first line, ignoring footnotes definitions and inline tasks, or
701 ;; according to parent.
702 (should
703 (= 2
704 (org-test-with-temp-text "A\n\n B\n\nC"
705 (goto-char (point-max))
706 (org-indent-line)
707 (org-get-indentation))))
708 (should
709 (= 1
710 (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
711 (goto-char (point-max))
712 (org-indent-line)
713 (org-get-indentation))))
714 (should
715 (= 1
716 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
717 (forward-line 1)
718 (org-indent-line)
719 (org-get-indentation))))
720 ;; Within code part of a source block, use language major mode if
721 ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
722 ;; according to line above.
723 (should
724 (= 6
725 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
726 (forward-line 2)
727 (let ((org-src-tab-acts-natively t)
728 (org-edit-src-content-indentation 0))
729 (org-indent-line))
730 (org-get-indentation))))
731 (should
732 (= 1
733 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
734 (forward-line 2)
735 (let ((org-src-tab-acts-natively nil)
736 (org-edit-src-content-indentation 0))
737 (org-indent-line))
738 (org-get-indentation))))
739 ;; Otherwise, indent like the first non-blank line above.
740 (should
741 (zerop
742 (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
743 (forward-line 3)
744 (org-indent-line)
745 (org-get-indentation))))
746 ;; Align node properties according to `org-property-format'. Handle
747 ;; nicely empty values.
748 (should
749 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
750 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
751 (let ((org-property-format "%-10s %s")) (org-indent-line))
752 (buffer-string))))
753 (should
754 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
755 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
756 (let ((org-property-format "%-10s %s")) (org-indent-line))
757 (buffer-string)))))
759 (ert-deftest test-org/indent-region ()
760 "Test `org-indent-region' specifications."
761 ;; Indent paragraph.
762 (should
763 (equal "A\nB\nC"
764 (org-test-with-temp-text " A\nB\n C"
765 (org-indent-region (point-min) (point-max))
766 (buffer-string))))
767 ;; Indent greater elements along with their contents.
768 (should
769 (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
770 (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
771 (org-indent-region (point-min) (point-max))
772 (buffer-string))))
773 ;; Ignore contents of verse blocks and example blocks.
774 (should
775 (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
776 (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
777 (org-indent-region (point-min) (point-max))
778 (buffer-string))))
779 (should
780 (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
781 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
782 (org-indent-region (point-min) (point-max))
783 (buffer-string))))
784 ;; Indent according to mode if `org-src-tab-acts-natively' is
785 ;; non-nil. Otherwise, do not indent code at all.
786 (should
787 (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
788 (org-test-with-temp-text
789 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
790 (let ((org-src-tab-acts-natively t)
791 (org-edit-src-content-indentation 0))
792 (org-indent-region (point-min) (point-max)))
793 (buffer-string))))
794 (should
795 (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
796 (org-test-with-temp-text
797 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
798 (let ((org-src-tab-acts-natively nil)
799 (org-edit-src-content-indentation 0))
800 (org-indent-region (point-min) (point-max)))
801 (buffer-string))))
802 ;; Align node properties according to `org-property-format'. Handle
803 ;; nicely empty values.
804 (should
805 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
806 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
807 (let ((org-property-format "%-10s %s")
808 (org-adapt-indentation nil))
809 (org-indent-region (point) (point-max)))
810 (buffer-string))))
811 (should
812 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
813 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
814 (let ((org-property-format "%-10s %s")
815 (org-adapt-indentation nil))
816 (org-indent-region (point) (point-max)))
817 (buffer-string))))
818 ;; Indent plain lists.
819 (should
820 (equal "- A\n B\n - C\n\n D"
821 (org-test-with-temp-text "- A\n B\n - C\n\n D"
822 (org-indent-region (point-min) (point-max))
823 (buffer-string))))
824 (should
825 (equal "- A\n\n- B"
826 (org-test-with-temp-text " - A\n\n - B"
827 (org-indent-region (point-min) (point-max))
828 (buffer-string))))
829 ;; Indent footnote definitions.
830 (should
831 (equal "[fn:1] Definition\n\nDefinition"
832 (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
833 (org-indent-region (point-min) (point-max))
834 (buffer-string))))
835 ;; Special case: Start indenting on a blank line.
836 (should
837 (equal "\nParagraph"
838 (org-test-with-temp-text "\n Paragraph"
839 (org-indent-region (point-min) (point-max))
840 (buffer-string)))))
844 ;;; Editing
846 (ert-deftest test-org/delete-indentation ()
847 "Test `org-delete-indentation' specifications."
848 ;; Regular test.
849 (should (equal "foo bar"
850 (org-test-with-temp-text
851 "foo \n bar<point>"
852 (org-delete-indentation)
853 (buffer-string))))
854 ;; With optional argument.
855 (should (equal "foo bar"
856 (org-test-with-temp-text
857 "foo<point> \n bar"
858 (org-delete-indentation t)
859 (buffer-string))))
860 ;; At headline text should be appended to the headline text.
861 (should
862 (equal"* foo bar :tag:"
863 (let (org-auto-align-tags)
864 (org-test-with-temp-text
865 "* foo :tag:\n bar<point>"
866 (org-delete-indentation)
867 (buffer-string)))))
868 (should
869 (equal "* foo bar :tag:"
870 (let (org-auto-align-tags)
871 (org-test-with-temp-text
872 "* foo <point>:tag:\n bar"
873 (org-delete-indentation t)
874 (buffer-string))))))
876 (ert-deftest test-org/return ()
877 "Test `org-return' specifications."
878 ;; Regular test.
879 (should
880 (equal "Para\ngraph"
881 (org-test-with-temp-text "Para<point>graph"
882 (org-return)
883 (buffer-string))))
884 ;; With optional argument, indent line.
885 (should
886 (equal " Para\n graph"
887 (org-test-with-temp-text " Para<point>graph"
888 (org-return t)
889 (buffer-string))))
890 ;; On a table, call `org-table-next-row'.
891 (should
892 (org-test-with-temp-text "| <point>a |\n| b |"
893 (org-return)
894 (org-looking-at-p "b")))
895 ;; Open link or timestamp under point when `org-return-follows-link'
896 ;; is non-nil.
897 (should
898 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
899 (let ((org-return-follows-link t)
900 (org-link-search-must-match-exact-headline nil))
901 (org-return))
902 (org-looking-at-p "<<target>>")))
903 (should-not
904 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
905 (let ((org-return-follows-link nil)) (org-return))
906 (org-looking-at-p "<<target>>")))
907 (should
908 (org-test-with-temp-text "* [[b][a<point>]]\n* b"
909 (let ((org-return-follows-link t)) (org-return))
910 (org-looking-at-p "* b")))
911 (should
912 (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
913 (let ((org-return-follows-link t)
914 (org-link-search-must-match-exact-headline nil))
915 (org-return))
916 (org-looking-at-p "<<target>>")))
917 ;; When `org-return-follows-link' is non-nil, tolerate links and
918 ;; timestamps in comments, node properties, etc.
919 (should
920 (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
921 (let ((org-return-follows-link t)
922 (org-link-search-must-match-exact-headline nil))
923 (org-return))
924 (org-looking-at-p "<<target>>")))
925 ;; However, do not open link when point is in a table.
926 (should
927 (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
928 (let ((org-return-follows-link t)) (org-return))
929 (org-looking-at-p "between")))
930 ;; Special case: in a list, when indenting, do not break structure.
931 (should
932 (equal "- A\n B"
933 (org-test-with-temp-text "- A <point>B"
934 (org-return t)
935 (buffer-string))))
936 (should
937 (equal "- A\n\n- B"
938 (org-test-with-temp-text "- A\n<point>- B"
939 (org-return t)
940 (buffer-string))))
941 ;; On tags part of a headline, add a newline below it instead of
942 ;; breaking it.
943 (should
944 (equal "* H :tag:\n"
945 (org-test-with-temp-text "* H :<point>tag:"
946 (org-return)
947 (buffer-string))))
948 ;; Before headline text, add a newline below it instead of breaking
949 ;; it.
950 (should
951 (equal "* TODO H :tag:\n"
952 (org-test-with-temp-text "* <point>TODO H :tag:"
953 (org-return)
954 (buffer-string))))
955 (should
956 (equal "* TODO [#B] H :tag:\n"
957 (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
958 (org-return)
959 (buffer-string))))
960 ;; At headline text, break headline text but preserve tags.
961 (should
962 (equal "* TODO [#B] foo :tag:\nbar"
963 (let (org-auto-align-tags)
964 (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
965 (org-return)
966 (buffer-string)))))
967 ;; At bol of headline insert newline.
968 (should
969 (equal "\n* h"
970 (org-test-with-temp-text "<point>* h"
971 (org-return)
972 (buffer-string))))
973 ;; Refuse to leave invalid headline in buffer.
974 (should
975 (equal "* h\n"
976 (org-test-with-temp-text "*<point> h"
977 (org-return)
978 (buffer-string)))))
980 (ert-deftest test-org/meta-return ()
981 "Test M-RET (`org-meta-return') specifications."
982 ;; In a table field insert a row above.
983 (should
984 (org-test-with-temp-text "| a |"
985 (forward-char)
986 (org-meta-return)
987 (forward-line -1)
988 (looking-at "| |$")))
989 ;; In a paragraph change current line into a header.
990 (should
991 (org-test-with-temp-text "a"
992 (org-meta-return)
993 (beginning-of-line)
994 (looking-at "\* a$")))
995 ;; In an item insert an item, in this case above.
996 (should
997 (org-test-with-temp-text "- a"
998 (org-meta-return)
999 (beginning-of-line)
1000 (looking-at "- $")))
1001 ;; In a drawer and item insert an item, in this case above.
1002 (should
1003 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
1004 (forward-line)
1005 (org-meta-return)
1006 (beginning-of-line)
1007 (looking-at "- $"))))
1009 (ert-deftest test-org/insert-heading ()
1010 "Test `org-insert-heading' specifications."
1011 ;; In an empty buffer, insert a new headline.
1012 (should
1013 (equal "* "
1014 (org-test-with-temp-text ""
1015 (org-insert-heading)
1016 (buffer-string))))
1017 ;; At the beginning of a line, turn it into a headline
1018 (should
1019 (equal "* P"
1020 (org-test-with-temp-text "<point>P"
1021 (org-insert-heading)
1022 (buffer-string))))
1023 ;; In the middle of a line, split the line if allowed, otherwise,
1024 ;; insert the headline at its end.
1025 (should
1026 (equal "Para\n* graph"
1027 (org-test-with-temp-text "Para<point>graph"
1028 (let ((org-M-RET-may-split-line '((default . t))))
1029 (org-insert-heading))
1030 (buffer-string))))
1031 (should
1032 (equal "Paragraph\n* "
1033 (org-test-with-temp-text "Para<point>graph"
1034 (let ((org-M-RET-may-split-line '((default . nil))))
1035 (org-insert-heading))
1036 (buffer-string))))
1037 ;; When on a list, insert an item instead, unless called with an
1038 ;; universal argument or if list is invisible. In this case, create
1039 ;; a new headline after contents.
1040 (should
1041 (equal "* H\n- item\n- "
1042 (org-test-with-temp-text "* H\n- item<point>"
1043 (let ((org-insert-heading-respect-content nil))
1044 (org-insert-heading))
1045 (buffer-string))))
1046 (should
1047 (equal "* H\n- item\n- item 2\n* "
1048 (org-test-with-temp-text "* H\n- item<point>\n- item 2"
1049 (let ((org-insert-heading-respect-content nil))
1050 (org-insert-heading '(4)))
1051 (buffer-string))))
1052 (should
1053 (equal "* H\n- item\n* "
1054 (org-test-with-temp-text "* H\n- item"
1055 (org-cycle)
1056 (goto-char (point-max))
1057 (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
1058 (buffer-string))))
1059 ;; When called with two universal arguments, insert a new headline
1060 ;; at the end of the grandparent subtree.
1061 (should
1062 (equal "* H1\n** H3\n- item\n** H2\n** "
1063 (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
1064 (let ((org-insert-heading-respect-content nil))
1065 (org-insert-heading '(16)))
1066 (buffer-string))))
1067 ;; When optional TOP-LEVEL argument is non-nil, always insert
1068 ;; a level 1 heading.
1069 (should
1070 (equal "* H1\n** H2\n* "
1071 (org-test-with-temp-text "* H1\n** H2<point>"
1072 (org-insert-heading nil nil t)
1073 (buffer-string))))
1074 (should
1075 (equal "* H1\n- item\n* "
1076 (org-test-with-temp-text "* H1\n- item<point>"
1077 (org-insert-heading nil nil t)
1078 (buffer-string))))
1079 ;; Corner case: correctly insert a headline after an empty one.
1080 (should
1081 (equal "* \n* "
1082 (org-test-with-temp-text "* <point>"
1083 (org-insert-heading)
1084 (buffer-string)))))
1086 (ert-deftest test-org/insert-todo-heading-respect-content ()
1087 "Test `org-insert-todo-heading-respect-content' specifications."
1088 ;; Create a TODO heading.
1089 (should
1090 (org-test-with-temp-text "* H1\n Body"
1091 (org-insert-todo-heading-respect-content)
1092 (nth 2 (org-heading-components))))
1093 ;; Add headline at the end of the first subtree
1094 (should
1095 (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
1096 (search-forward "H1Body")
1097 (org-insert-todo-heading-respect-content)
1098 (and (eobp) (org-at-heading-p))))
1099 ;; In a list, do not create a new item.
1100 (should
1101 (org-test-with-temp-text "* H\n- an item\n- another one"
1102 (search-forward "an ")
1103 (org-insert-todo-heading-respect-content)
1104 (and (eobp) (org-at-heading-p)))))
1106 (ert-deftest test-org/clone-with-time-shift ()
1107 "Test `org-clone-subtree-with-time-shift'."
1108 ;; Clone non-repeating once.
1109 (should
1110 (equal "\
1111 * H1\n<2015-06-21>
1112 * H1\n<2015-06-23>
1114 (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
1115 (org-clone-subtree-with-time-shift 1 "+2d")
1116 (replace-regexp-in-string
1117 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1118 nil nil 1))))
1119 ;; Clone repeating once.
1120 (should
1121 (equal "\
1122 * H1\n<2015-06-21>
1123 * H1\n<2015-06-23>
1124 * H1\n<2015-06-25 +1w>
1126 (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
1127 (org-clone-subtree-with-time-shift 1 "+2d")
1128 (replace-regexp-in-string
1129 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1130 nil nil 1))))
1131 ;; Clone non-repeating zero times.
1132 (should
1133 (equal "\
1134 * H1\n<2015-06-21>
1136 (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
1137 (org-clone-subtree-with-time-shift 0 "+2d")
1138 (replace-regexp-in-string
1139 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1140 nil nil 1))))
1141 ;; Clone repeating "zero" times.
1142 (should
1143 (equal "\
1144 * H1\n<2015-06-21>
1145 * H1\n<2015-06-23 +1w>
1147 (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
1148 (org-clone-subtree-with-time-shift 0 "+2d")
1149 (replace-regexp-in-string
1150 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1151 nil nil 1)))))
1154 ;;; Fixed-Width Areas
1156 (ert-deftest test-org/toggle-fixed-width ()
1157 "Test `org-toggle-fixed-width' specifications."
1158 ;; No region: Toggle on fixed-width marker in paragraphs.
1159 (should
1160 (equal ": A"
1161 (org-test-with-temp-text "A"
1162 (org-toggle-fixed-width)
1163 (buffer-string))))
1164 ;; No region: Toggle off fixed-width markers in fixed-width areas.
1165 (should
1166 (equal "A"
1167 (org-test-with-temp-text ": A"
1168 (org-toggle-fixed-width)
1169 (buffer-string))))
1170 ;; No region: Toggle on marker in blank lines after elements or just
1171 ;; after a headline.
1172 (should
1173 (equal "* H\n: "
1174 (org-test-with-temp-text "* H\n"
1175 (forward-line)
1176 (org-toggle-fixed-width)
1177 (buffer-string))))
1178 (should
1179 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
1180 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
1181 (goto-char (point-max))
1182 (org-toggle-fixed-width)
1183 (buffer-string))))
1184 ;; No region: Toggle on marker in front of one line elements (e.g.,
1185 ;; headlines, clocks)
1186 (should
1187 (equal ": * Headline"
1188 (org-test-with-temp-text "* Headline"
1189 (org-toggle-fixed-width)
1190 (buffer-string))))
1191 (should
1192 (equal ": #+KEYWORD: value"
1193 (org-test-with-temp-text "#+KEYWORD: value"
1194 (org-toggle-fixed-width)
1195 (buffer-string))))
1196 ;; No region: error in other situations.
1197 (should-error
1198 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
1199 (forward-line)
1200 (org-toggle-fixed-width)
1201 (buffer-string)))
1202 ;; No region: Indentation is preserved.
1203 (should
1204 (equal "- A\n : B"
1205 (org-test-with-temp-text "- A\n B"
1206 (forward-line)
1207 (org-toggle-fixed-width)
1208 (buffer-string))))
1209 ;; Region: If it contains only fixed-width elements and blank lines,
1210 ;; toggle off fixed-width markup.
1211 (should
1212 (equal "A\n\nB"
1213 (org-test-with-temp-text ": A\n\n: B"
1214 (transient-mark-mode 1)
1215 (push-mark (point) t t)
1216 (goto-char (point-max))
1217 (org-toggle-fixed-width)
1218 (buffer-string))))
1219 ;; Region: If it contains anything else, toggle on fixed-width but
1220 ;; not on fixed-width areas.
1221 (should
1222 (equal ": A\n: \n: B\n: \n: C"
1223 (org-test-with-temp-text "A\n\n: B\n\nC"
1224 (transient-mark-mode 1)
1225 (push-mark (point) t t)
1226 (goto-char (point-max))
1227 (org-toggle-fixed-width)
1228 (buffer-string))))
1229 ;; Region: Ignore blank lines at its end, unless it contains only
1230 ;; such lines.
1231 (should
1232 (equal ": A\n\n"
1233 (org-test-with-temp-text "A\n\n"
1234 (transient-mark-mode 1)
1235 (push-mark (point) t t)
1236 (goto-char (point-max))
1237 (org-toggle-fixed-width)
1238 (buffer-string))))
1239 (should
1240 (equal ": \n: \n"
1241 (org-test-with-temp-text "\n\n"
1242 (transient-mark-mode 1)
1243 (push-mark (point) t t)
1244 (goto-char (point-max))
1245 (org-toggle-fixed-width)
1246 (buffer-string)))))
1250 ;;; Headline
1252 (ert-deftest test-org/in-commented-heading-p ()
1253 "Test `org-in-commented-heading-p' specifications."
1254 ;; Commented headline.
1255 (should
1256 (org-test-with-temp-text "* COMMENT Headline\nBody"
1257 (goto-char (point-max))
1258 (org-in-commented-heading-p)))
1259 ;; Commented ancestor.
1260 (should
1261 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1262 (goto-char (point-max))
1263 (org-in-commented-heading-p)))
1264 ;; Comment keyword is case-sensitive.
1265 (should-not
1266 (org-test-with-temp-text "* Comment Headline\nBody"
1267 (goto-char (point-max))
1268 (org-in-commented-heading-p)))
1269 ;; Keyword is standalone.
1270 (should-not
1271 (org-test-with-temp-text "* COMMENTHeadline\nBody"
1272 (goto-char (point-max))
1273 (org-in-commented-heading-p)))
1274 ;; Optional argument.
1275 (should-not
1276 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1277 (goto-char (point-max))
1278 (org-in-commented-heading-p t))))
1280 (ert-deftest test-org/entry-blocked-p ()
1281 ;; Check other dependencies.
1282 (should
1283 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
1284 (let ((org-enforce-todo-dependencies t)
1285 (org-blocker-hook
1286 '(org-block-todo-from-children-or-siblings-or-parent)))
1287 (org-entry-blocked-p))))
1288 (should-not
1289 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
1290 (let ((org-enforce-todo-dependencies t)
1291 (org-blocker-hook
1292 '(org-block-todo-from-children-or-siblings-or-parent)))
1293 (org-entry-blocked-p))))
1294 ;; Entry without a TODO keyword or with a DONE keyword cannot be
1295 ;; blocked.
1296 (should-not
1297 (org-test-with-temp-text "* Blocked\n** TODO one"
1298 (let ((org-enforce-todo-dependencies t)
1299 (org-blocker-hook
1300 '(org-block-todo-from-children-or-siblings-or-parent)))
1301 (org-entry-blocked-p))))
1302 (should-not
1303 (org-test-with-temp-text "* DONE Blocked\n** TODO one"
1304 (let ((org-enforce-todo-dependencies t)
1305 (org-blocker-hook
1306 '(org-block-todo-from-children-or-siblings-or-parent)))
1307 (org-entry-blocked-p))))
1308 ;; Follow :ORDERED: specifications.
1309 (should
1310 (org-test-with-temp-text
1311 "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
1312 (let ((org-enforce-todo-dependencies t)
1313 (org-blocker-hook
1314 '(org-block-todo-from-children-or-siblings-or-parent)))
1315 (org-entry-blocked-p))))
1316 (should-not
1317 (org-test-with-temp-text
1318 "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
1319 (let ((org-enforce-todo-dependencies t)
1320 (org-blocker-hook
1321 '(org-block-todo-from-children-or-siblings-or-parent)))
1322 (org-entry-blocked-p)))))
1324 (ert-deftest test-org/get-outline-path ()
1325 "Test `org-get-outline-path' specifications."
1326 ;; Top-level headlines have no outline path.
1327 (should-not
1328 (org-test-with-temp-text "* H"
1329 (org-get-outline-path)))
1330 ;; Otherwise, outline path is the path leading to the headline.
1331 (should
1332 (equal '("H")
1333 (org-test-with-temp-text "* H\n** S<point>"
1334 (org-get-outline-path))))
1335 ;; Find path even when point is not on a headline.
1336 (should
1337 (equal '("H")
1338 (org-test-with-temp-text "* H\n** S\nText<point>"
1339 (org-get-outline-path))))
1340 ;; TODO keywords, tags and statistics cookies are ignored.
1341 (should
1342 (equal '("H")
1343 (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
1344 (org-get-outline-path))))
1345 ;; Links are replaced with their description or their path.
1346 (should
1347 (equal '("Org")
1348 (org-test-with-temp-text
1349 "* [[http://orgmode.org][Org]]\n** S<point>"
1350 (org-get-outline-path))))
1351 (should
1352 (equal '("http://orgmode.org")
1353 (org-test-with-temp-text
1354 "* [[http://orgmode.org]]\n** S<point>"
1355 (org-get-outline-path))))
1356 ;; When WITH-SELF is non-nil, include current heading.
1357 (should
1358 (equal '("H")
1359 (org-test-with-temp-text "* H"
1360 (org-get-outline-path t))))
1361 (should
1362 (equal '("H" "S")
1363 (org-test-with-temp-text "* H\n** S\nText<point>"
1364 (org-get-outline-path t))))
1365 ;; Using cache is transparent to the user.
1366 (should
1367 (equal '("H")
1368 (org-test-with-temp-text "* H\n** S<point>"
1369 (setq org-outline-path-cache nil)
1370 (org-get-outline-path nil t))))
1371 ;; Do not corrupt cache when finding outline path in distant part of
1372 ;; the buffer.
1373 (should
1374 (equal '("H2")
1375 (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
1376 (setq org-outline-path-cache nil)
1377 (org-get-outline-path nil t)
1378 (search-forward "S2")
1379 (org-get-outline-path nil t))))
1380 ;; Do not choke on empty headlines.
1381 (should
1382 (org-test-with-temp-text "* H\n** <point>"
1383 (org-get-outline-path)))
1384 (should
1385 (org-test-with-temp-text "* \n** H<point>"
1386 (org-get-outline-path))))
1388 (ert-deftest test-org/format-outline-path ()
1389 "Test `org-format-outline-path' specifications."
1390 (should
1391 (string= (org-format-outline-path (list "one" "two" "three"))
1392 "one/two/three"))
1393 ;; Empty path.
1394 (should
1395 (string= (org-format-outline-path '())
1396 ""))
1397 (should
1398 (string= (org-format-outline-path '(nil))
1399 ""))
1400 ;; Empty path and prefix.
1401 (should
1402 (string= (org-format-outline-path '() nil ">>")
1403 ">>"))
1404 ;; Trailing whitespace in headings.
1405 (should
1406 (string= (org-format-outline-path (list "one\t" "tw o " "three "))
1407 "one/tw o/three"))
1408 ;; Non-default prefix and separators.
1409 (should
1410 (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
1411 ">>|one|two|three"))
1412 ;; Truncate.
1413 (should
1414 (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
1415 "one/two/.."))
1416 ;; Give a very narrow width.
1417 (should
1418 (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
1419 "on"))
1420 ;; Give a prefix that extends beyond the width.
1421 (should
1422 (string= (org-format-outline-path (list "one" "two" "three" "four") 10
1423 ">>>>>>>>>>")
1424 ">>>>>>>>..")))
1427 ;;; Keywords
1429 (ert-deftest test-org/set-regexps-and-options ()
1430 "Test `org-set-regexps-and-options' specifications."
1431 ;; TAGS keyword.
1432 (should
1433 (equal '(("A" . ?a) ("B") ("C"))
1434 (org-test-with-temp-text "#+TAGS: A(a) B C"
1435 (org-mode-restart)
1436 org-tag-alist)))
1437 (should
1438 (equal '(("A") (:newline) ("B"))
1439 (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
1440 (org-mode-restart)
1441 org-tag-alist)))
1442 (should
1443 (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
1444 (org-test-with-temp-text "#+TAGS: { A B } C"
1445 (org-mode-restart)
1446 org-tag-alist)))
1447 (should
1448 (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
1449 (org-test-with-temp-text "#+TAGS: { A : B C }"
1450 (org-mode-restart)
1451 org-tag-alist)))
1452 (should
1453 (equal '(("A" "B" "C"))
1454 (org-test-with-temp-text "#+TAGS: { A : B C }"
1455 (org-mode-restart)
1456 org-tag-groups-alist)))
1457 (should
1458 (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
1459 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1460 (org-mode-restart)
1461 org-tag-alist)))
1462 (should
1463 (equal '(("A" "B" "C"))
1464 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1465 (org-mode-restart)
1466 org-tag-groups-alist)))
1467 ;; FILETAGS keyword.
1468 (should
1469 (equal '("A" "B" "C")
1470 (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
1471 (org-mode-restart)
1472 org-file-tags)))
1473 ;; PROPERTY keyword. Property names are case-insensitive.
1474 (should
1475 (equal "foo=1"
1476 (org-test-with-temp-text "#+PROPERTY: var foo=1"
1477 (org-mode-restart)
1478 (cdr (assoc "var" org-file-properties)))))
1479 (should
1480 (equal
1481 "foo=1 bar=2"
1482 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
1483 (org-mode-restart)
1484 (cdr (assoc "var" org-file-properties)))))
1485 (should
1486 (equal
1487 "foo=1 bar=2"
1488 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
1489 (org-mode-restart)
1490 (cdr (assoc "var" org-file-properties)))))
1491 ;; ARCHIVE keyword.
1492 (should
1493 (equal "%s_done::"
1494 (org-test-with-temp-text "#+ARCHIVE: %s_done::"
1495 (org-mode-restart)
1496 org-archive-location)))
1497 ;; CATEGORY keyword.
1498 (should
1499 (eq 'test
1500 (org-test-with-temp-text "#+CATEGORY: test"
1501 (org-mode-restart)
1502 org-category)))
1503 (should
1504 (equal "test"
1505 (org-test-with-temp-text "#+CATEGORY: test"
1506 (org-mode-restart)
1507 (cdr (assoc "CATEGORY" org-file-properties)))))
1508 ;; COLUMNS keyword.
1509 (should
1510 (equal "%25ITEM %TAGS %PRIORITY %TODO"
1511 (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
1512 (org-mode-restart)
1513 org-columns-default-format)))
1514 ;; CONSTANTS keyword. Constants names are case sensitive.
1515 (should
1516 (equal '("299792458." "3.14")
1517 (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
1518 (org-mode-restart)
1519 (mapcar
1520 (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
1521 '("c" "pi")))))
1522 (should
1523 (equal "3.14"
1524 (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
1525 (org-mode-restart)
1526 (cdr (assoc "pi" org-table-formula-constants-local)))))
1527 (should
1528 (equal "22/7"
1529 (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
1530 (org-mode-restart)
1531 (cdr (assoc "PI" org-table-formula-constants-local)))))
1532 ;; LINK keyword.
1533 (should
1534 (equal
1535 '("url1" "url2")
1536 (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
1537 (org-mode-restart)
1538 (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
1539 '("a" "b")))))
1540 ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
1541 (should
1542 (equal
1543 '(?X ?Z ?Y)
1544 (org-test-with-temp-text "#+PRIORITIES: X Z Y"
1545 (org-mode-restart)
1546 (list org-highest-priority org-lowest-priority org-default-priority))))
1547 (should
1548 (equal
1549 '(?A ?C ?B)
1550 (org-test-with-temp-text "#+PRIORITIES: X Z"
1551 (org-mode-restart)
1552 (list org-highest-priority org-lowest-priority org-default-priority))))
1553 ;; STARTUP keyword.
1554 (should
1555 (equal '(t t)
1556 (org-test-with-temp-text "#+STARTUP: fold odd"
1557 (org-mode-restart)
1558 (list org-startup-folded org-odd-levels-only))))
1559 ;; TODO keywords.
1560 (should
1561 (equal '(("A" "B") ("C"))
1562 (org-test-with-temp-text "#+TODO: A B | C"
1563 (org-mode-restart)
1564 (list org-not-done-keywords org-done-keywords))))
1565 (should
1566 (equal '(("A" "C") ("B" "D"))
1567 (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
1568 (org-mode-restart)
1569 (list org-not-done-keywords org-done-keywords))))
1570 (should
1571 (equal '(("A" "B") ("C"))
1572 (org-test-with-temp-text "#+TYP_TODO: A B | C"
1573 (org-mode-restart)
1574 (list org-not-done-keywords org-done-keywords))))
1575 (should
1576 (equal '((:startgroup) ("A" . ?a) (:endgroup))
1577 (org-test-with-temp-text "#+TODO: A(a)"
1578 (org-mode-restart)
1579 org-todo-key-alist)))
1580 (should
1581 (equal '(("D" note nil) ("C" time nil) ("B" note time))
1582 (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
1583 (org-mode-restart)
1584 org-todo-log-states)))
1585 ;; Enter SETUPFILE keyword.
1586 (should
1587 (equal "1"
1588 (org-test-with-temp-text
1589 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
1590 (org-mode-restart)
1591 (cdr (assoc "a" org-file-properties))))))
1595 ;;; Links
1597 ;;;; Coderefs
1599 (ert-deftest test-org/coderef ()
1600 "Test coderef links specifications."
1601 (should
1602 (org-test-with-temp-text "
1603 #+BEGIN_SRC emacs-lisp
1604 \(+ 1 1) (ref:sc)
1605 #+END_SRC
1606 \[[(sc)]]<point>"
1607 (org-open-at-point)
1608 (looking-at "(ref:sc)")))
1609 ;; Find coderef even with alternate label format.
1610 (should
1611 (org-test-with-temp-text "
1612 #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
1613 \(+ 1 1) {ref:sc}
1614 #+END_SRC
1615 \[[(sc)]]<point>"
1616 (org-open-at-point)
1617 (looking-at "{ref:sc}"))))
1619 ;;;; Custom ID
1621 (ert-deftest test-org/custom-id ()
1622 "Test custom ID links specifications."
1623 (should
1624 (org-test-with-temp-text
1625 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
1626 (org-open-at-point)
1627 (org-looking-at-p "\\* H1")))
1628 ;; Throw an error on false positives.
1629 (should-error
1630 (org-test-with-temp-text
1631 "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
1632 (org-open-at-point)
1633 (org-looking-at-p "\\* H1"))))
1635 ;;;; Fuzzy Links
1637 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
1638 ;; a named element (#+name: text) and to headlines (* Text).
1640 (ert-deftest test-org/fuzzy-links ()
1641 "Test fuzzy links specifications."
1642 ;; Fuzzy link goes in priority to a matching target.
1643 (should
1644 (org-test-with-temp-text
1645 "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
1646 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1647 (looking-at "<<Test>>")))
1648 ;; Then fuzzy link points to an element with a given name.
1649 (should
1650 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
1651 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1652 (looking-at "#\\+NAME: Test")))
1653 ;; A target still lead to a matching headline otherwise.
1654 (should
1655 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
1656 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1657 (looking-at "\\* Head2")))
1658 ;; With a leading star in link, enforce heading match.
1659 (should
1660 (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
1661 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1662 (looking-at "\\* Test")))
1663 ;; With a leading star in link, enforce exact heading match, even
1664 ;; with `org-link-search-must-match-exact-headline' set to nil.
1665 (should-error
1666 (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
1667 (let ((org-link-search-must-match-exact-headline nil))
1668 (org-open-at-point))))
1669 ;; Handle non-nil `org-link-search-must-match-exact-headline'.
1670 (should
1671 (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
1672 (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
1673 (looking-at "\\* Test")))
1674 (should
1675 (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
1676 (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
1677 (looking-at "\\* Test")))
1678 ;; Heading match should not care about spaces, cookies, TODO
1679 ;; keywords, priorities, and tags.
1680 (should
1681 (let ((first-line
1682 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1683 (org-test-with-temp-text
1684 (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
1685 (let ((org-link-search-must-match-exact-headline nil)
1686 (org-todo-regexp "TODO"))
1687 (org-open-at-point))
1688 (looking-at (regexp-quote first-line)))))
1689 ;; Heading match should still be exact.
1690 (should-error
1691 (let ((first-line
1692 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1693 (org-test-with-temp-text
1694 (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
1695 (let ((org-link-search-must-match-exact-headline nil)
1696 (org-todo-regexp "TODO"))
1697 (org-open-at-point)))))
1698 ;; Heading match ignores COMMENT keyword.
1699 (should
1700 (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
1701 (org-open-at-point)
1702 (looking-at "\\* COMMENT Test")))
1703 ;; Correctly un-hexify fuzzy links.
1704 (should
1705 (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
1706 (goto-char (point-max))
1707 (org-open-at-point)
1708 (bobp))))
1710 ;;;; Link Escaping
1712 (ert-deftest test-org/org-link-escape-ascii-character ()
1713 "Escape an ascii character."
1714 (should
1715 (string=
1716 "%5B"
1717 (org-link-escape "["))))
1719 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
1720 "Escape an ascii control character."
1721 (should
1722 (string=
1723 "%09"
1724 (org-link-escape "\t"))))
1726 (ert-deftest test-org/org-link-escape-multibyte-character ()
1727 "Escape an unicode multibyte character."
1728 (should
1729 (string=
1730 "%E2%82%AC"
1731 (org-link-escape "€"))))
1733 (ert-deftest test-org/org-link-escape-custom-table ()
1734 "Escape string with custom character table."
1735 (should
1736 (string=
1737 "Foo%3A%42ar%0A"
1738 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
1740 (ert-deftest test-org/org-link-escape-custom-table-merge ()
1741 "Escape string with custom table merged with default table."
1742 (should
1743 (string=
1744 "%5BF%6F%6F%3A%42ar%0A%5D"
1745 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
1747 (ert-deftest test-org/org-link-unescape-ascii-character ()
1748 "Unescape an ascii character."
1749 (should
1750 (string=
1752 (org-link-unescape "%5B"))))
1754 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
1755 "Unescpae an ascii control character."
1756 (should
1757 (string=
1758 "\n"
1759 (org-link-unescape "%0A"))))
1761 (ert-deftest test-org/org-link-unescape-multibyte-character ()
1762 "Unescape unicode multibyte character."
1763 (should
1764 (string=
1765 "€"
1766 (org-link-unescape "%E2%82%AC"))))
1768 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
1769 "Unescape old style percent escaped character."
1770 (should
1771 (string=
1772 "àâçèéêîôùû"
1773 (decode-coding-string
1774 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
1776 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
1777 "Escape and unescape a URL that includes an escaped char.
1778 http://article.gmane.org/gmane.emacs.orgmode/21459/"
1779 (should
1780 (string=
1781 "http://some.host.com/form?&id=blah%2Bblah25"
1782 (org-link-unescape
1783 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
1785 (ert-deftest test-org/org-link-escape-chars-browser ()
1786 "Test of the constant `org-link-escape-chars-browser'.
1787 See there why this test is a candidate to be removed once Org
1788 drops support for Emacs 24.1 and 24.2."
1789 (should
1790 (string=
1791 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1792 "%22Release%208.2%22&idxname=emacs-orgmode")
1793 (org-link-escape-browser ; Do not replace with `url-encode-url',
1794 ; see docstring above.
1795 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1796 "\"Release 8.2\"&idxname=emacs-orgmode")))))
1798 ;;;; Open at point
1800 (ert-deftest test-org/open-at-point-in-keyword ()
1801 "Does `org-open-at-point' open link in a keyword line?"
1802 (should
1803 (org-test-with-temp-text
1804 "#+KEYWORD: <point>[[info:emacs#Top]]"
1805 (org-open-at-point) t)))
1807 (ert-deftest test-org/open-at-point-in-property ()
1808 "Does `org-open-at-point' open link in property drawer?"
1809 (should
1810 (org-test-with-temp-text
1811 "* Headline
1812 :PROPERTIES:
1813 :URL: <point>[[info:emacs#Top]]
1814 :END:"
1815 (org-open-at-point) t)))
1817 (ert-deftest test-org/open-at-point-in-comment ()
1818 "Does `org-open-at-point' open link in a commented line?"
1819 (should
1820 (org-test-with-temp-text
1821 "# <point>[[info:emacs#Top]]"
1822 (org-open-at-point) t)))
1824 (ert-deftest test-org/open-at-point/info ()
1825 "Test `org-open-at-point' on info links."
1826 (should
1827 (org-test-with-temp-text
1828 "<point>[[info:emacs#Top]]"
1829 (org-open-at-point)
1830 (and (switch-to-buffer "*info*")
1831 (prog1
1832 (looking-at "\nThe Emacs Editor")
1833 (kill-buffer))))))
1835 (ert-deftest test-org/open-at-point/inline-image ()
1836 "Test `org-open-at-point' on nested links."
1837 (should
1838 (org-test-with-temp-text "[[info:org#Top][info:<point>emacs#Top]]"
1839 (org-open-at-point)
1840 (prog1 (with-current-buffer "*info*" (looking-at "\nOrg Mode Manual"))
1841 (kill-buffer "*info*")))))
1843 (ert-deftest test-org/open-at-point/radio-target ()
1844 "Test `org-open-at-point' on radio targets."
1845 (should
1846 (org-test-with-temp-text "<<<target>>> <point>target"
1847 (org-update-radio-target-regexp)
1848 (org-open-at-point)
1849 (eq (org-element-type (org-element-context)) 'radio-target))))
1852 ;;; Node Properties
1854 (ert-deftest test-org/accumulated-properties-in-drawers ()
1855 "Ensure properties accumulate in subtree drawers."
1856 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
1857 (org-babel-next-src-block)
1858 (should (equal '(2 1) (org-babel-execute-src-block)))))
1860 (ert-deftest test-org/custom-properties ()
1861 "Test custom properties specifications."
1862 ;; Standard test.
1863 (should
1864 (let ((org-custom-properties '("FOO")))
1865 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1866 (org-toggle-custom-properties-visibility)
1867 (org-invisible-p2))))
1868 ;; Properties are case-insensitive.
1869 (should
1870 (let ((org-custom-properties '("FOO")))
1871 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
1872 (org-toggle-custom-properties-visibility)
1873 (org-invisible-p2))))
1874 (should
1875 (let ((org-custom-properties '("foo")))
1876 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1877 (org-toggle-custom-properties-visibility)
1878 (org-invisible-p2))))
1879 ;; Multiple custom properties in the same drawer.
1880 (should
1881 (let ((org-custom-properties '("FOO" "BAR")))
1882 (org-test-with-temp-text
1883 "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
1884 (org-toggle-custom-properties-visibility)
1885 (and (org-invisible-p2)
1886 (not (progn (forward-line) (org-invisible-p2)))
1887 (progn (forward-line) (org-invisible-p2))))))
1888 ;; Hide custom properties with an empty value.
1889 (should
1890 (let ((org-custom-properties '("FOO")))
1891 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
1892 (org-toggle-custom-properties-visibility)
1893 (org-invisible-p2))))
1894 ;; Do not hide fake properties.
1895 (should-not
1896 (let ((org-custom-properties '("FOO")))
1897 (org-test-with-temp-text ":FOO: val\n"
1898 (org-toggle-custom-properties-visibility)
1899 (org-invisible-p2))))
1900 (should-not
1901 (let ((org-custom-properties '("A")))
1902 (org-test-with-temp-text
1903 "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
1904 (org-toggle-custom-properties-visibility)
1905 (org-invisible-p2)))))
1909 ;;; Mark Region
1911 (ert-deftest test-org/mark-subtree ()
1912 "Test `org-mark-subtree' specifications."
1913 ;; Error when point is before first headline.
1914 (should-error
1915 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
1916 (progn (transient-mark-mode 1)
1917 (org-mark-subtree))))
1918 ;; Without argument, mark current subtree.
1919 (should
1920 (equal
1921 '(12 32)
1922 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1923 (progn (transient-mark-mode 1)
1924 (forward-line 2)
1925 (org-mark-subtree)
1926 (list (region-beginning) (region-end))))))
1927 ;; With an argument, move ARG up.
1928 (should
1929 (equal
1930 '(1 32)
1931 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1932 (progn (transient-mark-mode 1)
1933 (forward-line 2)
1934 (org-mark-subtree 1)
1935 (list (region-beginning) (region-end))))))
1936 ;; Do not get fooled by inlinetasks.
1937 (when (featurep 'org-inlinetask)
1938 (should
1939 (= 1
1940 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
1941 (progn (transient-mark-mode 1)
1942 (forward-line 1)
1943 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
1944 (region-beginning)))))))
1948 ;;; Navigation
1950 (ert-deftest test-org/end-of-meta-data ()
1951 "Test `org-end-of-meta-data' specifications."
1952 ;; Skip planning line.
1953 (should
1954 (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
1955 (org-end-of-meta-data)
1956 (eobp)))
1957 ;; Skip properties drawer.
1958 (should
1959 (org-test-with-temp-text
1960 "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
1961 (org-end-of-meta-data)
1962 (eobp)))
1963 ;; Skip both.
1964 (should
1965 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
1966 (org-end-of-meta-data)
1967 (eobp)))
1968 ;; Nothing to skip, go to first line.
1969 (should
1970 (org-test-with-temp-text "* Headline\nContents"
1971 (org-end-of-meta-data)
1972 (looking-at "Contents")))
1973 ;; With option argument, skip empty lines, regular drawers and
1974 ;; clocking lines.
1975 (should
1976 (org-test-with-temp-text "* Headline\n\nContents"
1977 (org-end-of-meta-data t)
1978 (looking-at "Contents")))
1979 (should
1980 (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
1981 (org-end-of-meta-data t)
1982 (looking-at "Contents")))
1983 (should
1984 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
1985 (org-end-of-meta-data t)
1986 (looking-at "Contents")))
1987 ;; Special case: do not skip incomplete drawers.
1988 (should
1989 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
1990 (org-end-of-meta-data t)
1991 (looking-at ":LOGBOOK:")))
1992 ;; Special case: Be careful about consecutive headlines.
1993 (should-not
1994 (org-test-with-temp-text "* H1\n*H2\nContents"
1995 (org-end-of-meta-data t)
1996 (looking-at "Contents"))))
1998 (ert-deftest test-org/beginning-of-line ()
1999 "Test `org-beginning-of-line' specifications."
2000 ;; Standard test.
2001 (should
2002 (org-test-with-temp-text "Some text\nSome other text"
2003 (progn (org-beginning-of-line) (bolp))))
2004 ;; Standard test with `visual-line-mode'.
2005 (should-not
2006 (org-test-with-temp-text "A long line of text\nSome other text"
2007 (progn (visual-line-mode)
2008 (forward-char 2)
2009 (dotimes (i 1000) (insert "very "))
2010 (org-beginning-of-line)
2011 (bolp))))
2012 ;; At an headline with special movement.
2013 (should
2014 (org-test-with-temp-text "* TODO Headline"
2015 (let ((org-special-ctrl-a/e t))
2016 (org-end-of-line)
2017 (and (progn (org-beginning-of-line) (looking-at "Headline"))
2018 (progn (org-beginning-of-line) (bolp))
2019 (progn (org-beginning-of-line) (looking-at "Headline"))))))
2020 ;; Special case: Do not error when the buffer contains only a single
2021 ;; asterisk.
2022 (should
2023 (org-test-with-temp-text "*<point>"
2024 (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))))
2025 (should
2026 (org-test-with-temp-text "*<point>"
2027 (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line)))))
2029 (ert-deftest test-org/end-of-line ()
2030 "Test `org-end-of-line' specifications."
2031 ;; Standard test.
2032 (should
2033 (org-test-with-temp-text "Some text\nSome other text"
2034 (progn (org-end-of-line) (eolp))))
2035 ;; Standard test with `visual-line-mode'.
2036 (should-not
2037 (org-test-with-temp-text "A long line of text\nSome other text"
2038 (progn (visual-line-mode)
2039 (forward-char 2)
2040 (dotimes (i 1000) (insert "very "))
2041 (goto-char (point-min))
2042 (org-end-of-line)
2043 (eolp))))
2044 ;; At an headline with special movement.
2045 (should
2046 (org-test-with-temp-text "* Headline1 :tag:\n"
2047 (let ((org-special-ctrl-a/e t))
2048 (and (progn (org-end-of-line) (looking-at " :tag:"))
2049 (progn (org-end-of-line) (eolp))
2050 (progn (org-end-of-line) (looking-at " :tag:"))))))
2051 ;; At an headline without special movement.
2052 (should
2053 (org-test-with-temp-text "* Headline2 :tag:\n"
2054 (let ((org-special-ctrl-a/e nil))
2055 (and (progn (org-end-of-line) (eolp))
2056 (progn (org-end-of-line) (eolp))))))
2057 ;; At an headline, with reversed movement.
2058 (should
2059 (org-test-with-temp-text "* Headline3 :tag:\n"
2060 (let ((org-special-ctrl-a/e 'reversed)
2061 (this-command last-command))
2062 (and (progn (org-end-of-line) (eolp))
2063 (progn (org-end-of-line) (looking-at " :tag:"))))))
2064 ;; At a block without hidden contents.
2065 (should
2066 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
2067 (progn (org-end-of-line) (eolp))))
2068 ;; At a block with hidden contents.
2069 (should-not
2070 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
2071 (let ((org-special-ctrl-a/e t))
2072 (org-hide-block-toggle)
2073 (org-end-of-line)
2074 (eobp)))))
2076 (ert-deftest test-org/forward-sentence ()
2077 "Test `org-forward-sentence' specifications."
2078 ;; At the end of a table cell, move to the end of the next one.
2079 (should
2080 (org-test-with-temp-text "| a<point> | b |"
2081 (org-forward-sentence)
2082 (looking-at " |$")))
2083 ;; Elsewhere in a cell, move to its end.
2084 (should
2085 (org-test-with-temp-text "| a<point>c | b |"
2086 (org-forward-sentence)
2087 (looking-at " | b |$")))
2088 ;; Otherwise, simply call `forward-sentence'.
2089 (should
2090 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
2091 (org-forward-sentence)
2092 (looking-at " Sentence 2.")))
2093 (should
2094 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
2095 (org-forward-sentence)
2096 (org-forward-sentence)
2097 (eobp)))
2098 ;; At the end of an element, jump to the next one, without stopping
2099 ;; on blank lines in-between.
2100 (should
2101 (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
2102 (org-forward-sentence)
2103 (eobp))))
2105 (ert-deftest test-org/backward-sentence ()
2106 "Test `org-backward-sentence' specifications."
2107 ;; At the beginning of a table cell, move to the beginning of the
2108 ;; previous one.
2109 (should
2110 (org-test-with-temp-text "| a | <point>b |"
2111 (org-backward-sentence)
2112 (looking-at "a | b |$")))
2113 ;; Elsewhere in a cell, move to its beginning.
2114 (should
2115 (org-test-with-temp-text "| a | b<point>c |"
2116 (org-backward-sentence)
2117 (looking-at "bc |$")))
2118 ;; Otherwise, simply call `backward-sentence'.
2119 (should
2120 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
2121 (org-backward-sentence)
2122 (looking-at "Sentence 2.")))
2123 (should
2124 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
2125 (org-backward-sentence)
2126 (org-backward-sentence)
2127 (bobp)))
2128 ;; Make sure to hit the beginning of a sentence on the same line as
2129 ;; an item.
2130 (should
2131 (org-test-with-temp-text "- Line 1\n line <point>2."
2132 (org-backward-sentence)
2133 (looking-at "Line 1"))))
2135 (ert-deftest test-org/forward-paragraph ()
2136 "Test `org-forward-paragraph' specifications."
2137 ;; At end of buffer, return an error.
2138 (should-error
2139 (org-test-with-temp-text "Paragraph"
2140 (goto-char (point-max))
2141 (org-forward-paragraph)))
2142 ;; Standard test.
2143 (should
2144 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2145 (org-forward-paragraph)
2146 (looking-at "P2")))
2147 ;; Ignore depth.
2148 (should
2149 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
2150 (org-forward-paragraph)
2151 (looking-at "P1")))
2152 ;; Do not enter elements with invisible contents.
2153 (should
2154 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
2155 (org-hide-block-toggle)
2156 (org-forward-paragraph)
2157 (looking-at "P3")))
2158 ;; On an affiliated keyword, jump to the beginning of the element.
2159 (should
2160 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
2161 (org-forward-paragraph)
2162 (looking-at "Para")))
2163 ;; On an item or a footnote definition, move to the second element
2164 ;; inside, if any.
2165 (should
2166 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
2167 (org-forward-paragraph)
2168 (looking-at " Paragraph")))
2169 (should
2170 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
2171 (org-forward-paragraph)
2172 (looking-at "Paragraph")))
2173 ;; On an item, or a footnote definition, when the first line is
2174 ;; empty, move to the first item.
2175 (should
2176 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
2177 (org-forward-paragraph)
2178 (looking-at " Paragraph")))
2179 (should
2180 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
2181 (org-forward-paragraph)
2182 (looking-at "Paragraph")))
2183 ;; On a table (resp. a property drawer) do not move through table
2184 ;; rows (resp. node properties).
2185 (should
2186 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
2187 (org-forward-paragraph)
2188 (looking-at "Paragraph")))
2189 (should
2190 (org-test-with-temp-text
2191 "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
2192 (org-forward-paragraph)
2193 (looking-at "Paragraph")))
2194 ;; On a verse or source block, stop after blank lines.
2195 (should
2196 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
2197 (org-forward-paragraph)
2198 (looking-at "L2")))
2199 (should
2200 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
2201 (org-forward-paragraph)
2202 (looking-at "L2"))))
2204 (ert-deftest test-org/backward-paragraph ()
2205 "Test `org-backward-paragraph' specifications."
2206 ;; Error at beginning of buffer.
2207 (should-error
2208 (org-test-with-temp-text "Paragraph"
2209 (org-backward-paragraph)))
2210 ;; Regular test.
2211 (should
2212 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2213 (goto-char (point-max))
2214 (org-backward-paragraph)
2215 (looking-at "P3")))
2216 (should
2217 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2218 (goto-char (point-max))
2219 (beginning-of-line)
2220 (org-backward-paragraph)
2221 (looking-at "P2")))
2222 ;; Ignore depth.
2223 (should
2224 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
2225 (goto-char (point-max))
2226 (beginning-of-line)
2227 (org-backward-paragraph)
2228 (looking-at "P2")))
2229 ;; Ignore invisible elements.
2230 (should
2231 (org-test-with-temp-text "* H1\n P1\n* H2"
2232 (org-cycle)
2233 (goto-char (point-max))
2234 (beginning-of-line)
2235 (org-backward-paragraph)
2236 (bobp)))
2237 ;; On an affiliated keyword, jump to the first one.
2238 (should
2239 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
2240 (search-forward "c2")
2241 (org-backward-paragraph)
2242 (looking-at "#\\+name")))
2243 ;; On the second element in an item or a footnote definition, jump
2244 ;; to item or the definition.
2245 (should
2246 (org-test-with-temp-text "- line1\n\n line2"
2247 (goto-char (point-max))
2248 (beginning-of-line)
2249 (org-backward-paragraph)
2250 (looking-at "- line1")))
2251 (should
2252 (org-test-with-temp-text "[fn:1] line1\n\n line2"
2253 (goto-char (point-max))
2254 (beginning-of-line)
2255 (org-backward-paragraph)
2256 (looking-at "\\[fn:1\\] line1")))
2257 ;; On a table (resp. a property drawer), ignore table rows
2258 ;; (resp. node properties).
2259 (should
2260 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
2261 (goto-char (point-max))
2262 (beginning-of-line)
2263 (org-backward-paragraph)
2264 (bobp)))
2265 (should
2266 (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
2267 (org-backward-paragraph)
2268 (looking-at ":PROPERTIES:")))
2269 ;; On a source or verse block, stop before blank lines.
2270 (should
2271 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
2272 (search-forward "L3")
2273 (beginning-of-line)
2274 (org-backward-paragraph)
2275 (looking-at "L2")))
2276 (should
2277 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
2278 (search-forward "L3")
2279 (beginning-of-line)
2280 (org-backward-paragraph)
2281 (looking-at "L2"))))
2283 (ert-deftest test-org/forward-element ()
2284 "Test `org-forward-element' specifications."
2285 ;; 1. At EOB: should error.
2286 (org-test-with-temp-text "Some text\n"
2287 (goto-char (point-max))
2288 (should-error (org-forward-element)))
2289 ;; 2. Standard move: expected to ignore blank lines.
2290 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
2291 (org-forward-element)
2292 (should (looking-at (regexp-quote "Second paragraph."))))
2293 ;; 3. Headline tests.
2294 (org-test-with-temp-text "
2295 * Head 1
2296 ** Head 1.1
2297 *** Head 1.1.1
2298 ** Head 1.2"
2299 ;; 3.1. At an headline beginning: move to next headline at the
2300 ;; same level.
2301 (goto-line 3)
2302 (org-forward-element)
2303 (should (looking-at (regexp-quote "** Head 1.2")))
2304 ;; 3.2. At an headline beginning: move to parent headline if no
2305 ;; headline at the same level.
2306 (goto-line 3)
2307 (org-forward-element)
2308 (should (looking-at (regexp-quote "** Head 1.2"))))
2309 ;; 4. Greater element tests.
2310 (org-test-with-temp-text
2311 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
2312 ;; 4.1. At a greater element: expected to skip contents.
2313 (org-forward-element)
2314 (should (looking-at (regexp-quote "Outside.")))
2315 ;; 4.2. At the end of greater element contents: expected to skip
2316 ;; to the end of the greater element.
2317 (goto-line 2)
2318 (org-forward-element)
2319 (should (looking-at (regexp-quote "Outside."))))
2320 ;; 5. List tests.
2321 (org-test-with-temp-text "
2322 - item1
2324 - sub1
2326 - sub2
2328 - sub3
2330 Inner paragraph.
2332 - item2
2334 Outside."
2335 ;; 5.1. At list top point: expected to move to the element after
2336 ;; the list.
2337 (goto-line 2)
2338 (org-forward-element)
2339 (should (looking-at (regexp-quote "Outside.")))
2340 ;; 5.2. Special case: at the first line of a sub-list, but not at
2341 ;; beginning of line, move to next item.
2342 (goto-line 2)
2343 (forward-char)
2344 (org-forward-element)
2345 (should (looking-at "- item2"))
2346 (goto-line 4)
2347 (forward-char)
2348 (org-forward-element)
2349 (should (looking-at " - sub2"))
2350 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
2351 (goto-line 4)
2352 (org-forward-element)
2353 (should (looking-at (regexp-quote " Inner paragraph.")))
2354 ;; 5.4. At sub-list end: expected to move outside the sub-list.
2355 (goto-line 8)
2356 (org-forward-element)
2357 (should (looking-at (regexp-quote " Inner paragraph.")))
2358 ;; 5.5. At an item: expected to move to next item, if any.
2359 (goto-line 6)
2360 (org-forward-element)
2361 (should (looking-at " - sub3"))))
2363 (ert-deftest test-org/backward-element ()
2364 "Test `org-backward-element' specifications."
2365 ;; 1. Should error at BOB.
2366 (org-test-with-temp-text " \nParagraph."
2367 (should-error (org-backward-element)))
2368 ;; 2. Should move at BOB when called on the first element in buffer.
2369 (should
2370 (org-test-with-temp-text "\n#+TITLE: test"
2371 (progn (forward-line)
2372 (org-backward-element)
2373 (bobp))))
2374 ;; 3. Not at the beginning of an element: move at its beginning.
2375 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2376 (goto-line 3)
2377 (end-of-line)
2378 (org-backward-element)
2379 (should (looking-at (regexp-quote "Paragraph2."))))
2380 ;; 4. Headline tests.
2381 (org-test-with-temp-text "
2382 * Head 1
2383 ** Head 1.1
2384 *** Head 1.1.1
2385 ** Head 1.2"
2386 ;; 4.1. At an headline beginning: move to previous headline at the
2387 ;; same level.
2388 (goto-line 5)
2389 (org-backward-element)
2390 (should (looking-at (regexp-quote "** Head 1.1")))
2391 ;; 4.2. At an headline beginning: move to parent headline if no
2392 ;; headline at the same level.
2393 (goto-line 3)
2394 (org-backward-element)
2395 (should (looking-at (regexp-quote "* Head 1")))
2396 ;; 4.3. At the first top-level headline: should error.
2397 (goto-line 2)
2398 (should-error (org-backward-element)))
2399 ;; 5. At beginning of first element inside a greater element:
2400 ;; expected to move to greater element's beginning.
2401 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
2402 (goto-line 3)
2403 (org-backward-element)
2404 (should (looking-at "#\\+BEGIN_CENTER")))
2405 ;; 6. At the beginning of the first element in a section: should
2406 ;; move back to headline, if any.
2407 (should
2408 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
2409 (progn (goto-char (point-max))
2410 (beginning-of-line)
2411 (org-backward-element)
2412 (org-at-heading-p))))
2413 ;; 7. List tests.
2414 (org-test-with-temp-text "
2415 - item1
2417 - sub1
2419 - sub2
2421 - sub3
2423 Inner paragraph.
2425 - item2
2428 Outside."
2429 ;; 7.1. At beginning of sub-list: expected to move to the
2430 ;; paragraph before it.
2431 (goto-line 4)
2432 (org-backward-element)
2433 (should (looking-at "item1"))
2434 ;; 7.2. At an item in a list: expected to move at previous item.
2435 (goto-line 8)
2436 (org-backward-element)
2437 (should (looking-at " - sub2"))
2438 (goto-line 12)
2439 (org-backward-element)
2440 (should (looking-at "- item1"))
2441 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
2442 ;; beginning.
2443 (goto-line 10)
2444 (org-backward-element)
2445 (should (looking-at " - sub1"))
2446 (goto-line 15)
2447 (org-backward-element)
2448 (should (looking-at "- item1"))
2449 ;; 7.4. At blank-lines before list end: expected to move to top
2450 ;; item.
2451 (goto-line 14)
2452 (org-backward-element)
2453 (should (looking-at "- item1"))))
2455 (ert-deftest test-org/up-element ()
2456 "Test `org-up-element' specifications."
2457 ;; 1. At BOB or with no surrounding element: should error.
2458 (org-test-with-temp-text "Paragraph."
2459 (should-error (org-up-element)))
2460 (org-test-with-temp-text "* Head1\n* Head2"
2461 (goto-line 2)
2462 (should-error (org-up-element)))
2463 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2464 (goto-line 3)
2465 (should-error (org-up-element)))
2466 ;; 2. At an headline: move to parent headline.
2467 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
2468 (goto-line 3)
2469 (org-up-element)
2470 (should (looking-at "\\* Head1")))
2471 ;; 3. Inside a greater element: move to greater element beginning.
2472 (org-test-with-temp-text
2473 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
2474 (goto-line 3)
2475 (org-up-element)
2476 (should (looking-at "#\\+BEGIN_CENTER")))
2477 ;; 4. List tests.
2478 (org-test-with-temp-text "* Top
2479 - item1
2481 - sub1
2483 - sub2
2485 Paragraph within sub2.
2487 - item2"
2488 ;; 4.1. Within an item: move to the item beginning.
2489 (goto-line 8)
2490 (org-up-element)
2491 (should (looking-at " - sub2"))
2492 ;; 4.2. At an item in a sub-list: move to parent item.
2493 (goto-line 4)
2494 (org-up-element)
2495 (should (looking-at "- item1"))
2496 ;; 4.3. At an item in top list: move to beginning of whole list.
2497 (goto-line 10)
2498 (org-up-element)
2499 (should (looking-at "- item1"))
2500 ;; 4.4. Special case. At very top point: should move to parent of
2501 ;; list.
2502 (goto-line 2)
2503 (org-up-element)
2504 (should (looking-at "\\* Top"))))
2506 (ert-deftest test-org/down-element ()
2507 "Test `org-down-element' specifications."
2508 ;; Error when the element hasn't got a recursive type.
2509 (org-test-with-temp-text "Paragraph."
2510 (should-error (org-down-element)))
2511 ;; Error when the element has no contents
2512 (org-test-with-temp-text "* Headline"
2513 (should-error (org-down-element)))
2514 ;; When at a plain-list, move to first item.
2515 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
2516 (goto-line 2)
2517 (org-down-element)
2518 (should (looking-at " - Item 1.1")))
2519 (org-test-with-temp-text "#+NAME: list\n- Item 1"
2520 (org-down-element)
2521 (should (looking-at " Item 1")))
2522 ;; When at a table, move to first row
2523 (org-test-with-temp-text "#+NAME: table\n| a | b |"
2524 (org-down-element)
2525 (should (looking-at " a | b |")))
2526 ;; Otherwise, move inside the greater element.
2527 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
2528 (org-down-element)
2529 (should (looking-at "Paragraph"))))
2531 (ert-deftest test-org/drag-element-backward ()
2532 "Test `org-drag-element-backward' specifications."
2533 ;; Standard test.
2534 (should
2535 (equal
2536 "#+key2: val2\n#+key1: val1\n#+key3: val3"
2537 (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
2538 (org-drag-element-backward)
2539 (buffer-string))))
2540 (should
2541 (equal
2542 "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
2543 (org-test-with-temp-text
2544 "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
2545 (org-drag-element-backward)
2546 (buffer-string))))
2547 ;; Preserve blank lines.
2548 (should
2549 (equal "Paragraph 2\n\n\nPara1\n\nPara3"
2550 (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
2551 (org-drag-element-backward)
2552 (buffer-string))))
2553 ;; Preserve column.
2554 (should
2555 (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
2556 (org-drag-element-backward)
2557 (org-looking-at-p "2")))
2558 ;; Error when trying to move first element of buffer.
2559 (should-error
2560 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2561 (org-drag-element-backward)))
2562 ;; Error when trying to swap nested elements.
2563 (should-error
2564 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2565 (forward-line)
2566 (org-drag-element-backward)))
2567 ;; Error when trying to swap an headline element and a non-headline
2568 ;; element.
2569 (should-error
2570 (org-test-with-temp-text "Test.\n* Head 1"
2571 (forward-line)
2572 (org-drag-element-backward)))
2573 ;; Preserve visibility of elements and their contents.
2574 (should
2575 (equal '((63 . 82) (26 . 48))
2576 (org-test-with-temp-text "
2577 #+BEGIN_CENTER
2578 Text.
2579 #+END_CENTER
2580 - item 1
2581 #+BEGIN_QUOTE
2582 Text.
2583 #+END_QUOTE"
2584 (while (search-forward "BEGIN_" nil t) (org-cycle))
2585 (search-backward "- item 1")
2586 (org-drag-element-backward)
2587 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2588 (overlays-in (point-min) (point-max)))))))
2590 (ert-deftest test-org/drag-element-forward ()
2591 "Test `org-drag-element-forward' specifications."
2592 ;; 1. Error when trying to move first element of buffer.
2593 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2594 (goto-line 3)
2595 (should-error (org-drag-element-forward)))
2596 ;; 2. Error when trying to swap nested elements.
2597 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2598 (forward-line)
2599 (should-error (org-drag-element-forward)))
2600 ;; 3. Error when trying to swap a non-headline element and an
2601 ;; headline.
2602 (org-test-with-temp-text "Test.\n* Head 1"
2603 (should-error (org-drag-element-forward)))
2604 ;; 4. Otherwise, swap elements, preserving column and blank lines
2605 ;; between elements.
2606 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
2607 (search-forward "graph")
2608 (org-drag-element-forward)
2609 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
2610 (should (looking-at " 1")))
2611 ;; 5. Preserve visibility of elements and their contents.
2612 (org-test-with-temp-text "
2613 #+BEGIN_CENTER
2614 Text.
2615 #+END_CENTER
2616 - item 1
2617 #+BEGIN_QUOTE
2618 Text.
2619 #+END_QUOTE"
2620 (while (search-forward "BEGIN_" nil t) (org-cycle))
2621 (search-backward "#+BEGIN_CENTER")
2622 (org-drag-element-forward)
2623 (should
2624 (equal
2625 '((63 . 82) (26 . 48))
2626 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2627 (overlays-in (point-min) (point-max)))))))
2629 (ert-deftest test-org/next-block ()
2630 "Test `org-next-block' specifications."
2631 ;; Regular test.
2632 (should
2633 (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
2634 (org-next-block 1)
2635 (looking-at "#\\+BEGIN_CENTER")))
2636 ;; Ignore case.
2637 (should
2638 (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
2639 (let ((case-fold-search nil))
2640 (org-next-block 1)
2641 (looking-at "#\\+begin_center"))))
2642 ;; Ignore current line.
2643 (should
2644 (org-test-with-temp-text
2645 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
2646 (org-next-block 1)
2647 (looking-at "#\\+BEGIN_CENTER")))
2648 ;; Throw an error when no block is found.
2649 (should-error
2650 (org-test-with-temp-text "Paragraph"
2651 (org-next-block 1)))
2652 ;; With an argument, skip many blocks at once.
2653 (should
2654 (org-test-with-temp-text
2655 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
2656 (org-next-block 2)
2657 (looking-at "#\\+BEGIN_QUOTE")))
2658 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
2659 (should
2660 (org-test-with-temp-text
2661 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
2662 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
2663 (looking-at "#\\+BEGIN_QUOTE")))
2664 ;; Optional argument is also case-insensitive.
2665 (should
2666 (org-test-with-temp-text
2667 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
2668 (let ((case-fold-search nil))
2669 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
2670 (looking-at "#\\+begin_quote")))))
2672 (ert-deftest test-org/previous-block ()
2673 "Test `org-previous-block' specifications."
2674 ;; Regular test.
2675 (should
2676 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
2677 (org-previous-block 1)
2678 (looking-at "#\\+BEGIN_CENTER")))
2679 ;; Ignore case.
2680 (should
2681 (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
2682 (let ((case-fold-search nil))
2683 (org-previous-block 1)
2684 (looking-at "#\\+begin_center"))))
2685 ;; Ignore current line.
2686 (should
2687 (org-test-with-temp-text
2688 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
2689 (org-previous-block 1)
2690 (looking-at "#\\+BEGIN_QUOTE")))
2691 ;; Throw an error when no block is found.
2692 (should-error
2693 (org-test-with-temp-text "Paragraph<point>"
2694 (org-previous-block 1)))
2695 ;; With an argument, skip many blocks at once.
2696 (should
2697 (org-test-with-temp-text
2698 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
2699 (org-previous-block 2)
2700 (looking-at "#\\+BEGIN_CENTER")))
2701 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
2702 (should
2703 (org-test-with-temp-text
2704 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
2705 (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
2706 (looking-at "#\\+BEGIN_QUOTE")))
2707 ;; Optional argument is also case-insensitive.
2708 (should
2709 (org-test-with-temp-text
2710 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
2711 (let ((case-fold-search nil))
2712 (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
2713 (looking-at "#\\+begin_quote")))))
2716 ;;; Outline structure
2718 (ert-deftest test-org/demote ()
2719 "Test `org-demote' specifications."
2720 ;; Add correct number of stars according to `org-odd-levels-only'.
2721 (should
2722 (= 2
2723 (org-test-with-temp-text "* H"
2724 (let ((org-odd-levels-only nil)) (org-demote))
2725 (org-current-level))))
2726 (should
2727 (= 3
2728 (org-test-with-temp-text "* H"
2729 (let ((org-odd-levels-only t)) (org-demote))
2730 (org-current-level))))
2731 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2732 (should
2733 (org-test-with-temp-text "* H :tag:"
2734 (let ((org-tags-column 10)
2735 (org-auto-align-tags t)
2736 (org-odd-levels-only nil))
2737 (org-demote))
2738 (org-move-to-column 10)
2739 (org-looking-at-p ":tag:$")))
2740 (should-not
2741 (org-test-with-temp-text "* H :tag:"
2742 (let ((org-tags-column 10)
2743 (org-auto-align-tags nil)
2744 (org-odd-levels-only nil))
2745 (org-demote))
2746 (org-move-to-column 10)
2747 (org-looking-at-p ":tag:$")))
2748 ;; When `org-adapt-indentation' is non-nil, always indent planning
2749 ;; info and property drawers accordingly.
2750 (should
2751 (= 3
2752 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2753 (let ((org-odd-levels-only nil)
2754 (org-adapt-indentation t))
2755 (org-demote))
2756 (forward-line)
2757 (org-get-indentation))))
2758 (should
2759 (= 3
2760 (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2761 (let ((org-odd-levels-only nil)
2762 (org-adapt-indentation t))
2763 (org-demote))
2764 (forward-line)
2765 (org-get-indentation))))
2766 (should-not
2767 (= 3
2768 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2769 (let ((org-odd-levels-only nil)
2770 (org-adapt-indentation nil))
2771 (org-demote))
2772 (forward-line)
2773 (org-get-indentation))))
2774 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2775 ;; section accordingly. Ignore, however, footnote definitions and
2776 ;; inlinetasks boundaries.
2777 (should
2778 (= 3
2779 (org-test-with-temp-text "* H\n Paragraph"
2780 (let ((org-odd-levels-only nil)
2781 (org-adapt-indentation t))
2782 (org-demote))
2783 (forward-line)
2784 (org-get-indentation))))
2785 (should
2786 (= 2
2787 (org-test-with-temp-text "* H\n Paragraph"
2788 (let ((org-odd-levels-only nil)
2789 (org-adapt-indentation nil))
2790 (org-demote))
2791 (forward-line)
2792 (org-get-indentation))))
2793 (should
2794 (zerop
2795 (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
2796 (let ((org-odd-levels-only nil)
2797 (org-adapt-indentation t))
2798 (org-demote))
2799 (goto-char (point-max))
2800 (org-get-indentation))))
2801 (should
2802 (= 3
2803 (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
2804 (let ((org-odd-levels-only nil)
2805 (org-adapt-indentation t))
2806 (org-demote))
2807 (goto-char (point-max))
2808 (org-get-indentation))))
2809 (when (featurep 'org-inlinetask)
2810 (should
2811 (zerop
2812 (let ((org-inlinetask-min-level 5)
2813 (org-adapt-indentation t))
2814 (org-test-with-temp-text "* H\n***** I\n***** END"
2815 (org-demote)
2816 (forward-line)
2817 (org-get-indentation))))))
2818 (when (featurep 'org-inlinetask)
2819 (should
2820 (= 3
2821 (let ((org-inlinetask-min-level 5)
2822 (org-adapt-indentation t))
2823 (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
2824 (org-demote)
2825 (forward-line 2)
2826 (org-get-indentation))))))
2827 ;; Ignore contents of source blocks or example blocks when
2828 ;; indentation should be preserved (through
2829 ;; `org-src-preserve-indentation' or "-i" flag).
2830 (should-not
2831 (zerop
2832 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2833 (let ((org-adapt-indentation t)
2834 (org-src-preserve-indentation nil))
2835 (org-demote))
2836 (forward-line 2)
2837 (org-get-indentation))))
2838 (should
2839 (zerop
2840 (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2841 (let ((org-adapt-indentation t)
2842 (org-src-preserve-indentation t))
2843 (org-demote))
2844 (forward-line 2)
2845 (org-get-indentation))))
2846 (should
2847 (zerop
2848 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2849 (let ((org-adapt-indentation t)
2850 (org-src-preserve-indentation t))
2851 (org-demote))
2852 (forward-line 2)
2853 (org-get-indentation))))
2854 (should
2855 (zerop
2856 (org-test-with-temp-text
2857 "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
2858 (let ((org-adapt-indentation t)
2859 (org-src-preserve-indentation nil))
2860 (org-demote))
2861 (forward-line 2)
2862 (org-get-indentation)))))
2864 (ert-deftest test-org/promote ()
2865 "Test `org-promote' specifications."
2866 ;; Return an error if headline is to be promoted to level 0, unless
2867 ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
2868 ;; headline becomes a comment.
2869 (should-error
2870 (org-test-with-temp-text "* H"
2871 (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
2872 (should
2873 (equal "# H"
2874 (org-test-with-temp-text "* H"
2875 (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
2876 (buffer-string))))
2877 ;; Remove correct number of stars according to
2878 ;; `org-odd-levels-only'.
2879 (should
2880 (= 2
2881 (org-test-with-temp-text "*** H"
2882 (let ((org-odd-levels-only nil)) (org-promote))
2883 (org-current-level))))
2884 (should
2885 (= 1
2886 (org-test-with-temp-text "*** H"
2887 (let ((org-odd-levels-only t)) (org-promote))
2888 (org-current-level))))
2889 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2890 (should
2891 (org-test-with-temp-text "** H :tag:"
2892 (let ((org-tags-column 10)
2893 (org-auto-align-tags t)
2894 (org-odd-levels-only nil))
2895 (org-promote))
2896 (org-move-to-column 10)
2897 (org-looking-at-p ":tag:$")))
2898 (should-not
2899 (org-test-with-temp-text "** H :tag:"
2900 (let ((org-tags-column 10)
2901 (org-auto-align-tags nil)
2902 (org-odd-levels-only nil))
2903 (org-promote))
2904 (org-move-to-column 10)
2905 (org-looking-at-p ":tag:$")))
2906 ;; When `org-adapt-indentation' is non-nil, always indent planning
2907 ;; info and property drawers.
2908 (should
2909 (= 2
2910 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2911 (let ((org-odd-levels-only nil)
2912 (org-adapt-indentation t))
2913 (org-promote))
2914 (forward-line)
2915 (org-get-indentation))))
2916 (should
2917 (= 2
2918 (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2919 (let ((org-odd-levels-only nil)
2920 (org-adapt-indentation t))
2921 (org-promote))
2922 (forward-line)
2923 (org-get-indentation))))
2924 (should-not
2925 (= 2
2926 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2927 (let ((org-odd-levels-only nil)
2928 (org-adapt-indentation nil))
2929 (org-promote))
2930 (forward-line)
2931 (org-get-indentation))))
2932 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2933 ;; section accordingly. Ignore, however, footnote definitions and
2934 ;; inlinetasks boundaries.
2935 (should
2936 (= 2
2937 (org-test-with-temp-text "** H\n Paragraph"
2938 (let ((org-odd-levels-only nil)
2939 (org-adapt-indentation t))
2940 (org-promote))
2941 (forward-line)
2942 (org-get-indentation))))
2943 (should-not
2944 (= 2
2945 (org-test-with-temp-text "** H\n Paragraph"
2946 (let ((org-odd-levels-only nil)
2947 (org-adapt-indentation nil))
2948 (org-promote))
2949 (forward-line)
2950 (org-get-indentation))))
2951 (should
2952 (= 2
2953 (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
2954 (let ((org-odd-levels-only nil)
2955 (org-adapt-indentation t))
2956 (org-promote))
2957 (forward-line)
2958 (org-get-indentation))))
2959 (when (featurep 'org-inlinetask)
2960 (should
2961 (zerop
2962 (let ((org-inlinetask-min-level 5)
2963 (org-adapt-indentation t))
2964 (org-test-with-temp-text "** H\n***** I\n***** END"
2965 (org-promote)
2966 (forward-line)
2967 (org-get-indentation))))))
2968 (when (featurep 'org-inlinetask)
2969 (should
2970 (= 2
2971 (let ((org-inlinetask-min-level 5)
2972 (org-adapt-indentation t))
2973 (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
2974 (org-promote)
2975 (forward-line 2)
2976 (org-get-indentation))))))
2977 ;; Give up shifting if it would break document's structure
2978 ;; otherwise.
2979 (should
2980 (= 3
2981 (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
2982 (let ((org-odd-levels-only nil)
2983 (org-adapt-indentation t))
2984 (org-promote))
2985 (forward-line)
2986 (org-get-indentation))))
2987 (should
2988 (= 3
2989 (org-test-with-temp-text "** H\n Paragraph\n * list."
2990 (let ((org-odd-levels-only nil)
2991 (org-adapt-indentation t))
2992 (org-promote))
2993 (forward-line)
2994 (org-get-indentation))))
2995 ;; Ignore contents of source blocks or example blocks when
2996 ;; indentation should be preserved (through
2997 ;; `org-src-preserve-indentation' or "-i" flag).
2998 (should-not
2999 (zerop
3000 (org-test-with-temp-text
3001 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
3002 (let ((org-adapt-indentation t)
3003 (org-src-preserve-indentation nil)
3004 (org-odd-levels-only nil))
3005 (org-promote))
3006 (forward-line)
3007 (org-get-indentation))))
3008 (should
3009 (zerop
3010 (org-test-with-temp-text
3011 "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
3012 (let ((org-adapt-indentation t)
3013 (org-src-preserve-indentation t)
3014 (org-odd-levels-only nil))
3015 (org-promote))
3016 (forward-line)
3017 (org-get-indentation))))
3018 (should
3019 (zerop
3020 (org-test-with-temp-text
3021 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
3022 (let ((org-adapt-indentation t)
3023 (org-src-preserve-indentation t)
3024 (org-odd-levels-only nil))
3025 (org-promote))
3026 (forward-line)
3027 (org-get-indentation))))
3028 (should
3029 (zerop
3030 (org-test-with-temp-text
3031 "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
3032 (let ((org-adapt-indentation t)
3033 (org-src-preserve-indentation nil)
3034 (org-odd-levels-only nil))
3035 (org-promote))
3036 (forward-line)
3037 (org-get-indentation)))))
3040 ;;; Planning
3042 (ert-deftest test-org/at-planning-p ()
3043 "Test `org-at-planning-p' specifications."
3044 ;; Regular test.
3045 (should
3046 (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
3047 (org-at-planning-p)))
3048 (should-not
3049 (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
3050 (org-at-planning-p)))
3051 ;; Correctly find planning attached to inlinetasks.
3052 (when (featurep 'org-inlinetask)
3053 (should
3054 (org-test-with-temp-text
3055 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
3056 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
3057 (should-not
3058 (org-test-with-temp-text
3059 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
3060 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
3061 (should-not
3062 (org-test-with-temp-text
3063 "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
3064 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
3065 (should-not
3066 (org-test-with-temp-text
3067 "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
3068 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
3070 (ert-deftest test-org/add-planning-info ()
3071 "Test `org-add-planning-info'."
3072 ;; Create deadline when `org-adapt-indentation' is non-nil.
3073 (should
3074 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3075 (org-test-with-temp-text "* H\nParagraph<point>"
3076 (let ((org-adapt-indentation t))
3077 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3078 (replace-regexp-in-string
3079 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3080 nil nil 1))))
3081 ;; Create deadline when `org-adapt-indentation' is nil.
3082 (should
3083 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3084 (org-test-with-temp-text "* H\nParagraph<point>"
3085 (let ((org-adapt-indentation nil))
3086 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3087 (replace-regexp-in-string
3088 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3089 nil nil 1))))
3090 ;; Update deadline when `org-adapt-indentation' is non-nil.
3091 (should
3092 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3093 (org-test-with-temp-text "\
3095 DEADLINE: <2015-06-24 Wed>
3096 Paragraph<point>"
3097 (let ((org-adapt-indentation t))
3098 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3099 (replace-regexp-in-string
3100 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3101 nil nil 1))))
3102 ;; Update deadline when `org-adapt-indentation' is nil.
3103 (should
3104 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3105 (org-test-with-temp-text "\
3107 DEADLINE: <2015-06-24 Wed>
3108 Paragraph<point>"
3109 (let ((org-adapt-indentation nil))
3110 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3111 (replace-regexp-in-string
3112 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3113 nil nil 1))))
3114 ;; Schedule when `org-adapt-indentation' is non-nil.
3115 (should
3116 (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
3117 (org-test-with-temp-text "* H\nParagraph<point>"
3118 (let ((org-adapt-indentation t))
3119 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
3120 (replace-regexp-in-string
3121 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3122 nil nil 1))))
3123 ;; Schedule when `org-adapt-indentation' is nil.
3124 (should
3125 (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
3126 (org-test-with-temp-text "* H\nParagraph<point>"
3127 (let ((org-adapt-indentation nil))
3128 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
3129 (replace-regexp-in-string
3130 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3131 nil nil 1))))
3132 ;; Add deadline when scheduled.
3133 (should
3134 (equal "\
3136 DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
3137 Paragraph"
3138 (org-test-with-temp-text "\
3140 SCHEDULED: <2015-06-24 Wed>
3141 Paragraph<point>"
3142 (let ((org-adapt-indentation t))
3143 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3144 (replace-regexp-in-string
3145 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3146 nil nil 1))))
3147 ;; Remove middle entry.
3148 (should
3149 (equal "\
3151 CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
3152 Paragraph"
3153 (org-test-with-temp-text "\
3155 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
3156 Paragraph<point>"
3157 (let ((org-adapt-indentation t))
3158 (org-add-planning-info nil nil 'deadline))
3159 (replace-regexp-in-string
3160 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
3161 nil nil 1))))
3162 ;; Remove last entry and then middle entry (order should not
3163 ;; matter).
3164 (should
3165 (equal "\
3167 CLOSED: [2015-06-24]
3168 Paragraph"
3169 (org-test-with-temp-text "\
3171 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
3172 Paragraph<point>"
3173 (let ((org-adapt-indentation t))
3174 (org-add-planning-info nil nil 'scheduled 'deadline))
3175 (replace-regexp-in-string
3176 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
3177 nil nil 1))))
3178 ;; Remove closed when `org-adapt-indentation' is non-nil.
3179 (should
3180 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3181 (org-test-with-temp-text "\
3183 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
3184 Paragraph<point>"
3185 (let ((org-adapt-indentation t))
3186 (org-add-planning-info nil nil 'closed))
3187 (replace-regexp-in-string
3188 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3189 nil nil 1))))
3190 (should
3191 (equal "* H\n Paragraph"
3192 (org-test-with-temp-text "\
3194 CLOSED: [2015-06-25 Thu]
3195 Paragraph<point>"
3196 (let ((org-adapt-indentation t))
3197 (org-add-planning-info nil nil 'closed))
3198 (replace-regexp-in-string
3199 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3200 nil nil 1))))
3201 ;; Remove closed when `org-adapt-indentation' is nil.
3202 (should
3203 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3204 (org-test-with-temp-text "\
3206 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
3207 Paragraph<point>"
3208 (let ((org-adapt-indentation nil))
3209 (org-add-planning-info nil nil 'closed))
3210 (replace-regexp-in-string
3211 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3212 nil nil 1))))
3213 (should
3214 (equal "* H\nParagraph"
3215 (org-test-with-temp-text "\
3217 CLOSED: [2015-06-25 Thu]
3218 Paragraph<point>"
3219 (let ((org-adapt-indentation nil))
3220 (org-add-planning-info nil nil 'closed))
3221 (replace-regexp-in-string
3222 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3223 nil nil 1))))
3224 ;; Remove closed entry and delete empty line.
3225 (should
3226 (equal "\
3228 Paragraph"
3229 (org-test-with-temp-text "\
3231 CLOSED: [2015-06-24 Wed]
3232 Paragraph<point>"
3233 (let ((org-adapt-indentation t))
3234 (org-add-planning-info nil nil 'closed))
3235 (replace-regexp-in-string
3236 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3237 nil nil 1))))
3238 ;; Remove one entry and update another.
3239 (should
3240 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3241 (org-test-with-temp-text "\
3243 SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
3244 Paragraph<point>"
3245 (let ((org-adapt-indentation t))
3246 (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
3247 (replace-regexp-in-string
3248 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3249 nil nil 1)))))
3252 ;;; Property API
3254 (ert-deftest test-org/buffer-property-keys ()
3255 "Test `org-buffer-property-keys' specifications."
3256 ;; Retrieve properties accross siblings.
3257 (should
3258 (equal '("A" "B")
3259 (org-test-with-temp-text "
3260 * H1
3261 :PROPERTIES:
3262 :A: 1
3263 :END:
3264 * H2
3265 :PROPERTIES:
3266 :B: 1
3267 :END:"
3268 (org-buffer-property-keys))))
3269 ;; Retrieve properties accross children.
3270 (should
3271 (equal '("A" "B")
3272 (org-test-with-temp-text "
3273 * H1
3274 :PROPERTIES:
3275 :A: 1
3276 :END:
3277 ** H2
3278 :PROPERTIES:
3279 :B: 1
3280 :END:"
3281 (org-buffer-property-keys))))
3282 ;; Retrieve muliple properties in the same drawer.
3283 (should
3284 (equal '("A" "B")
3285 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3286 (org-buffer-property-keys))))
3287 ;; Ignore extension symbol in property name.
3288 (should
3289 (equal '("A")
3290 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
3291 (org-buffer-property-keys))))
3292 ;; With non-nil COLUMNS, extract property names from columns.
3293 (should
3294 (equal '("A" "B")
3295 (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
3296 (org-buffer-property-keys nil nil t))))
3297 (should
3298 (equal '("A" "B" "COLUMNS")
3299 (org-test-with-temp-text
3300 "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
3301 (org-buffer-property-keys nil nil t)))))
3303 (ert-deftest test-org/property-values ()
3304 "Test `org-property-values' specifications."
3305 ;; Regular test.
3306 (should
3307 (equal '("2" "1")
3308 (org-test-with-temp-text
3309 "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
3310 (org-property-values "A"))))
3311 ;; Ignore empty values.
3312 (should-not
3313 (org-test-with-temp-text
3314 "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
3315 (org-property-values "A")))
3316 ;; Take into consideration extended values.
3317 (should
3318 (equal '("1 2")
3319 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
3320 (org-property-values "A")))))
3322 (ert-deftest test-org/find-property ()
3323 "Test `org-find-property' specifications."
3324 ;; Regular test.
3325 (should
3326 (= 1
3327 (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
3328 (org-find-property "prop"))))
3329 ;; Ignore false positives.
3330 (should
3331 (= 27
3332 (org-test-with-temp-text
3333 "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
3334 (org-find-property "A"))))
3335 ;; Return first entry found in buffer.
3336 (should
3337 (= 1
3338 (org-test-with-temp-text
3339 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
3340 (org-find-property "A"))))
3341 ;; Only search visible part of the buffer.
3342 (should
3343 (= 31
3344 (org-test-with-temp-text
3345 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
3346 (org-narrow-to-subtree)
3347 (org-find-property "A"))))
3348 ;; With optional argument, only find entries with a specific value.
3349 (should-not
3350 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3351 (org-find-property "A" "2")))
3352 (should
3353 (= 31
3354 (org-test-with-temp-text
3355 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
3356 (org-find-property "A" "2"))))
3357 ;; Use "nil" for explicit nil values.
3358 (should
3359 (= 31
3360 (org-test-with-temp-text
3361 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
3362 (org-find-property "A" "nil")))))
3364 (ert-deftest test-org/entry-delete ()
3365 "Test `org-entry-delete' specifications."
3366 ;; Regular test.
3367 (should
3368 (string-match
3369 " *:PROPERTIES:\n *:B: +2\n *:END:"
3370 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3371 (org-entry-delete (point) "A")
3372 (buffer-string))))
3373 ;; Also remove accumulated properties.
3374 (should-not
3375 (string-match
3376 ":A"
3377 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
3378 (org-entry-delete (point) "A")
3379 (buffer-string))))
3380 ;; When last property is removed, remove the property drawer.
3381 (should-not
3382 (string-match
3383 ":PROPERTIES:"
3384 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
3385 (org-entry-delete (point) "A")
3386 (buffer-string))))
3387 ;; Return a non-nil value when some property was removed.
3388 (should
3389 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3390 (org-entry-delete (point) "A")))
3391 (should-not
3392 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3393 (org-entry-delete (point) "C"))))
3395 (ert-deftest test-org/entry-get ()
3396 "Test `org-entry-get' specifications."
3397 ;; Regular test.
3398 (should
3399 (equal "1"
3400 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3401 (org-entry-get (point) "A"))))
3402 ;; Ignore case.
3403 (should
3404 (equal "1"
3405 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3406 (org-entry-get (point) "a"))))
3407 ;; Handle extended values, both before and after base value.
3408 (should
3409 (equal "1 2 3"
3410 (org-test-with-temp-text
3411 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
3412 (org-entry-get (point) "A"))))
3413 ;; Empty values are returned as the empty string.
3414 (should
3415 (equal ""
3416 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
3417 (org-entry-get (point) "A"))))
3418 ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
3419 ;; otherwise, return nil.
3420 (should-not
3421 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
3422 (org-entry-get (point) "A")))
3423 (should
3424 (equal "nil"
3425 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
3426 (org-entry-get (point) "A" nil t))))
3427 ;; Return nil when no property is found, independently on the
3428 ;; LITERAL-NIL argument.
3429 (should-not
3430 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3431 (org-entry-get (point) "B")))
3432 (should-not
3433 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3434 (org-entry-get (point) "B" nil t)))
3435 ;; Handle inheritance, when allowed. Include extended values and
3436 ;; possibly global values.
3437 (should
3438 (equal
3440 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3441 (org-entry-get (point) "A" t))))
3442 (should
3443 (equal
3445 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3446 (let ((org-use-property-inheritance t))
3447 (org-entry-get (point) "A" 'selective)))))
3448 (should-not
3449 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3450 (let ((org-use-property-inheritance nil))
3451 (org-entry-get (point) "A" 'selective))))
3452 (should
3453 (equal
3454 "1 2"
3455 (org-test-with-temp-text
3456 "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
3457 (org-entry-get (point-max) "A" t))))
3458 (should
3459 (equal "1"
3460 (org-test-with-temp-text
3461 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
3462 (org-mode-restart)
3463 (org-entry-get (point-max) "A" t))))
3464 (should
3465 (equal "0 1"
3466 (org-test-with-temp-text
3467 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
3468 (org-mode-restart)
3469 (org-entry-get (point-max) "A" t)))))
3471 (ert-deftest test-org/entry-properties ()
3472 "Test `org-entry-properties' specifications."
3473 ;; Get "ITEM" property.
3474 (should
3475 (equal "* H"
3476 (org-test-with-temp-text "* TODO H"
3477 (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
3478 (should
3479 (equal "* H"
3480 (org-test-with-temp-text "* TODO H"
3481 (cdr (assoc "ITEM" (org-entry-properties))))))
3482 ;; Get "TODO" property. TODO keywords are case sensitive.
3483 (should
3484 (equal "TODO"
3485 (org-test-with-temp-text "* TODO H"
3486 (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
3487 (should
3488 (equal "TODO"
3489 (org-test-with-temp-text "* TODO H"
3490 (cdr (assoc "TODO" (org-entry-properties))))))
3491 (should-not
3492 (org-test-with-temp-text "* H"
3493 (assoc "TODO" (org-entry-properties nil "TODO"))))
3494 (should-not
3495 (org-test-with-temp-text "* todo H"
3496 (assoc "TODO" (org-entry-properties nil "TODO"))))
3497 ;; Get "PRIORITY" property.
3498 (should
3499 (equal "A"
3500 (org-test-with-temp-text "* [#A] H"
3501 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
3502 (should
3503 (equal "A"
3504 (org-test-with-temp-text "* [#A] H"
3505 (cdr (assoc "PRIORITY" (org-entry-properties))))))
3506 (should
3507 (equal (char-to-string org-default-priority)
3508 (org-test-with-temp-text "* H"
3509 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
3510 ;; Get "FILE" property.
3511 (should
3512 (org-test-with-temp-text-in-file "* H\nParagraph"
3513 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
3514 (buffer-file-name))))
3515 (should
3516 (org-test-with-temp-text-in-file "* H\nParagraph"
3517 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
3518 (buffer-file-name))))
3519 (should-not
3520 (org-test-with-temp-text "* H\nParagraph"
3521 (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
3522 ;; Get "TAGS" property.
3523 (should
3524 (equal ":tag1:tag2:"
3525 (org-test-with-temp-text "* H :tag1:tag2:"
3526 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
3527 (should
3528 (equal ":tag1:tag2:"
3529 (org-test-with-temp-text "* H :tag1:tag2:"
3530 (cdr (assoc "TAGS" (org-entry-properties))))))
3531 (should-not
3532 (org-test-with-temp-text "* H"
3533 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
3534 ;; Get "ALLTAGS" property.
3535 (should
3536 (equal ":tag1:tag2:"
3537 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
3538 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
3539 (should
3540 (equal ":tag1:tag2:"
3541 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
3542 (cdr (assoc "ALLTAGS" (org-entry-properties))))))
3543 (should-not
3544 (org-test-with-temp-text "* H"
3545 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
3546 ;; Get "BLOCKED" property.
3547 (should
3548 (equal "t"
3549 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
3550 (let ((org-enforce-todo-dependencies t)
3551 (org-blocker-hook
3552 '(org-block-todo-from-children-or-siblings-or-parent)))
3553 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
3554 (should
3555 (equal ""
3556 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
3557 (let ((org-enforce-todo-dependencies t)
3558 (org-blocker-hook
3559 '(org-block-todo-from-children-or-siblings-or-parent)))
3560 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
3561 ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
3562 (should
3563 (equal
3564 "[2012-03-29 thu.]"
3565 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
3566 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
3567 (should
3568 (equal
3569 "[2012-03-29 thu.]"
3570 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
3571 (cdr (assoc "CLOSED" (org-entry-properties))))))
3572 (should-not
3573 (org-test-with-temp-text "* H"
3574 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
3575 (should
3576 (equal
3577 "<2014-03-04 tue.>"
3578 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3579 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
3580 (should
3581 (equal
3582 "<2014-03-04 tue.>"
3583 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3584 (cdr (assoc "DEADLINE" (org-entry-properties))))))
3585 (should-not
3586 (org-test-with-temp-text "* H"
3587 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
3588 (should
3589 (equal
3590 "<2014-03-04 tue.>"
3591 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3592 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
3593 (should
3594 (equal
3595 "<2014-03-04 tue.>"
3596 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3597 (cdr (assoc "SCHEDULED" (org-entry-properties))))))
3598 (should-not
3599 (org-test-with-temp-text "* H"
3600 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
3601 ;; Get "CATEGORY"
3602 (should
3603 (equal "cat"
3604 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
3605 (cdr (assoc "CATEGORY" (org-entry-properties))))))
3606 (should
3607 (equal "cat"
3608 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
3609 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3610 (should
3611 (equal "cat"
3612 (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
3613 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3614 (should
3615 (equal "cat2"
3616 (org-test-with-temp-text
3617 (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
3618 "\n"
3619 "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
3620 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3621 ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
3622 (should
3623 (equal "<2012-03-29 thu.>"
3624 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
3625 (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
3626 (should
3627 (equal "[2012-03-29 thu.]"
3628 (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
3629 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
3630 (should
3631 (equal "<2012-03-29 thu.>"
3632 (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
3633 (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
3634 (should
3635 (equal "[2014-03-04 tue.]"
3636 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
3637 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties nil "TIMESTAMP_IA"))))))
3638 ;; Get standard properties.
3639 (should
3640 (equal "1"
3641 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3642 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3643 ;; Handle extended properties.
3644 (should
3645 (equal "1 2 3"
3646 (org-test-with-temp-text
3647 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
3648 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3649 (should
3650 (equal "1 2 3"
3651 (org-test-with-temp-text
3652 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
3653 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3654 ;; Ignore forbidden (special) properties.
3655 (should-not
3656 (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
3657 (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
3659 (ert-deftest test-org/entry-put ()
3660 "Test `org-entry-put' specifications."
3661 ;; Error when not a string or nil.
3662 (should-error
3663 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
3664 (org-entry-put 1 "test" 2)))
3665 ;; Error when property name is invalid.
3666 (should-error
3667 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
3668 (org-entry-put 1 "no space" "value")))
3669 (should-error
3670 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
3671 (org-entry-put 1 "" "value")))
3672 ;; Set "TODO" property.
3673 (should
3674 (string-match (regexp-quote " TODO H")
3675 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
3676 (org-entry-put (point) "TODO" "TODO")
3677 (buffer-string))))
3678 (should
3679 (string-match (regexp-quote "* H")
3680 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
3681 (org-entry-put (point) "TODO" nil)
3682 (buffer-string))))
3683 ;; Set "PRIORITY" property.
3684 (should
3685 (equal "* [#A] H"
3686 (org-test-with-temp-text "* [#B] H"
3687 (org-entry-put (point) "PRIORITY" "A")
3688 (buffer-string))))
3689 (should
3690 (equal "* H"
3691 (org-test-with-temp-text "* [#B] H"
3692 (org-entry-put (point) "PRIORITY" nil)
3693 (buffer-string))))
3694 ;; Set "SCHEDULED" property.
3695 (should
3696 (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
3697 (org-test-with-temp-text "* H"
3698 (org-entry-put (point) "SCHEDULED" "2014-03-04")
3699 (buffer-string))))
3700 (should
3701 (string= "* H\n"
3702 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3703 (org-entry-put (point) "SCHEDULED" nil)
3704 (buffer-string))))
3705 (should
3706 (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
3707 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3708 (org-entry-put (point) "SCHEDULED" "earlier")
3709 (buffer-string))))
3710 (should
3711 (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
3712 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3713 (org-entry-put (point) "SCHEDULED" "later")
3714 (buffer-string))))
3715 ;; Set "DEADLINE" property.
3716 (should
3717 (string-match "^ *DEADLINE: <2014-03-04 .*?>"
3718 (org-test-with-temp-text "* H"
3719 (org-entry-put (point) "DEADLINE" "2014-03-04")
3720 (buffer-string))))
3721 (should
3722 (string= "* H\n"
3723 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3724 (org-entry-put (point) "DEADLINE" nil)
3725 (buffer-string))))
3726 (should
3727 (string-match "^ *DEADLINE: <2014-03-03 .*?>"
3728 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3729 (org-entry-put (point) "DEADLINE" "earlier")
3730 (buffer-string))))
3731 (should
3732 (string-match "^ *DEADLINE: <2014-03-05 .*?>"
3733 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3734 (org-entry-put (point) "DEADLINE" "later")
3735 (buffer-string))))
3736 ;; Set "CATEGORY" property
3737 (should
3738 (string-match "^ *:CATEGORY: cat"
3739 (org-test-with-temp-text "* H"
3740 (org-entry-put (point) "CATEGORY" "cat")
3741 (buffer-string))))
3742 ;; Regular properties, with or without pre-existing drawer.
3743 (should
3744 (string-match "^ *:A: +2$"
3745 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3746 (org-entry-put (point) "A" "2")
3747 (buffer-string))))
3748 (should
3749 (string-match "^ *:A: +1$"
3750 (org-test-with-temp-text "* H"
3751 (org-entry-put (point) "A" "1")
3752 (buffer-string))))
3753 ;; Special case: two consecutive headlines.
3754 (should
3755 (string-match "\\* A\n *:PROPERTIES:"
3756 (org-test-with-temp-text "* A\n** B"
3757 (org-entry-put (point) "A" "1")
3758 (buffer-string)))))
3761 ;;; Radio Targets
3763 (ert-deftest test-org/update-radio-target-regexp ()
3764 "Test `org-update-radio-target-regexp' specifications."
3765 ;; Properly update cache with no previous radio target regexp.
3766 (should
3767 (eq 'link
3768 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3769 (save-excursion (goto-char (point-max)) (org-element-context))
3770 (insert "<<<")
3771 (search-forward "o")
3772 (insert ">>>")
3773 (org-update-radio-target-regexp)
3774 (goto-char (point-max))
3775 (org-element-type (org-element-context)))))
3776 ;; Properly update cache with previous radio target regexp.
3777 (should
3778 (eq 'link
3779 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3780 (save-excursion (goto-char (point-max)) (org-element-context))
3781 (insert "<<<")
3782 (search-forward "o")
3783 (insert ">>>")
3784 (org-update-radio-target-regexp)
3785 (search-backward "r")
3786 (delete-char 5)
3787 (insert "new")
3788 (org-update-radio-target-regexp)
3789 (goto-char (point-max))
3790 (delete-region (line-beginning-position) (point))
3791 (insert "new")
3792 (org-element-type (org-element-context))))))
3795 ;;; Sparse trees
3797 (ert-deftest test-org/match-sparse-tree ()
3798 "Test `org-match-sparse-tree' specifications."
3799 ;; Match tags.
3800 (should-not
3801 (org-test-with-temp-text "* H\n** H1 :tag:"
3802 (org-match-sparse-tree nil "tag")
3803 (search-forward "H1")
3804 (org-invisible-p2)))
3805 (should
3806 (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
3807 (org-match-sparse-tree nil "tag")
3808 (search-forward "H2")
3809 (org-invisible-p2)))
3810 ;; "-" operator for tags.
3811 (should-not
3812 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3813 (org-match-sparse-tree nil "tag1-tag2")
3814 (search-forward "H1")
3815 (org-invisible-p2)))
3816 (should
3817 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3818 (org-match-sparse-tree nil "tag1-tag2")
3819 (search-forward "H2")
3820 (org-invisible-p2)))
3821 ;; "&" operator for tags.
3822 (should
3823 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3824 (org-match-sparse-tree nil "tag1&tag2")
3825 (search-forward "H1")
3826 (org-invisible-p2)))
3827 (should-not
3828 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3829 (org-match-sparse-tree nil "tag1&tag2")
3830 (search-forward "H2")
3831 (org-invisible-p2)))
3832 ;; "|" operator for tags.
3833 (should-not
3834 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3835 (org-match-sparse-tree nil "tag1|tag2")
3836 (search-forward "H1")
3837 (org-invisible-p2)))
3838 (should-not
3839 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3840 (org-match-sparse-tree nil "tag1|tag2")
3841 (search-forward "H2")
3842 (org-invisible-p2)))
3843 ;; Regexp match on tags.
3844 (should-not
3845 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3846 (org-match-sparse-tree nil "{^tag.*}")
3847 (search-forward "H1")
3848 (org-invisible-p2)))
3849 (should
3850 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3851 (org-match-sparse-tree nil "{^tag.*}")
3852 (search-forward "H2")
3853 (org-invisible-p2)))
3854 ;; Match group tags.
3855 (should-not
3856 (org-test-with-temp-text
3857 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3858 (org-match-sparse-tree nil "work")
3859 (search-forward "H1")
3860 (org-invisible-p2)))
3861 (should-not
3862 (org-test-with-temp-text
3863 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3864 (org-match-sparse-tree nil "work")
3865 (search-forward "H2")
3866 (org-invisible-p2)))
3867 ;; Match group tags with hard brackets.
3868 (should-not
3869 (org-test-with-temp-text
3870 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
3871 (org-match-sparse-tree nil "work")
3872 (search-forward "H1")
3873 (org-invisible-p2)))
3874 (should-not
3875 (org-test-with-temp-text
3876 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
3877 (org-match-sparse-tree nil "work")
3878 (search-forward "H2")
3879 (org-invisible-p2)))
3880 ;; Match tags in hierarchies
3881 (should-not
3882 (org-test-with-temp-text
3883 "#+TAGS: [ Lev_1 : Lev_2 ]\n
3884 #+TAGS: [ Lev_2 : Lev_3 ]\n
3885 #+TAGS: { Lev_3 : Lev_4 }\n
3886 * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
3887 (org-match-sparse-tree nil "Lev_1")
3888 (search-forward "H4")
3889 (org-invisible-p2)))
3890 ;; Match regular expressions in tags
3891 (should-not
3892 (org-test-with-temp-text
3893 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
3894 (org-match-sparse-tree nil "Lev")
3895 (search-forward "H1")
3896 (org-invisible-p2)))
3897 (should
3898 (org-test-with-temp-text
3899 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
3900 (org-match-sparse-tree nil "Lev")
3901 (search-forward "H1")
3902 (org-invisible-p2)))
3903 ;; Match properties.
3904 (should
3905 (org-test-with-temp-text
3906 "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
3907 (org-match-sparse-tree nil "A=\"1\"")
3908 (search-forward "H2")
3909 (org-invisible-p2)))
3910 (should-not
3911 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3912 (org-match-sparse-tree nil "A=\"1\"")
3913 (search-forward "H2")
3914 (org-invisible-p2)))
3915 ;; Case is not significant when matching properties.
3916 (should-not
3917 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3918 (org-match-sparse-tree nil "a=\"1\"")
3919 (search-forward "H2")
3920 (org-invisible-p2)))
3921 (should-not
3922 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
3923 (org-match-sparse-tree nil "A=\"1\"")
3924 (search-forward "H2")
3925 (org-invisible-p2)))
3926 ;; Match special LEVEL property.
3927 (should-not
3928 (org-test-with-temp-text "* H\n** H1\n*** H2"
3929 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3930 (search-forward "H1")
3931 (org-invisible-p2)))
3932 (should
3933 (org-test-with-temp-text "* H\n** H1\n*** H2"
3934 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3935 (search-forward "H2")
3936 (org-invisible-p2)))
3937 ;; Comparison operators when matching properties.
3938 (should
3939 (org-test-with-temp-text
3940 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3941 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3942 (search-forward "H1")
3943 (org-invisible-p2)))
3944 (should-not
3945 (org-test-with-temp-text
3946 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3947 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3948 (search-forward "H2")
3949 (org-invisible-p2)))
3950 ;; Regexp match on properties values.
3951 (should-not
3952 (org-test-with-temp-text
3953 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3954 (org-match-sparse-tree nil "A={f.*}")
3955 (search-forward "H1")
3956 (org-invisible-p2)))
3957 (should
3958 (org-test-with-temp-text
3959 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3960 (org-match-sparse-tree nil "A={f.*}")
3961 (search-forward "H2")
3962 (org-invisible-p2)))
3963 ;; With an optional argument, limit match to TODO entries.
3964 (should-not
3965 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3966 (org-match-sparse-tree t "tag")
3967 (search-forward "H1")
3968 (org-invisible-p2)))
3969 (should
3970 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3971 (org-match-sparse-tree t "tag")
3972 (search-forward "H2")
3973 (org-invisible-p2))))
3976 ;;; Timestamps API
3978 (ert-deftest test-org/time-stamp ()
3979 "Test `org-time-stamp' specifications."
3980 ;; Insert chosen time stamp at point.
3981 (should
3982 (string-match
3983 "Te<2014-03-04 .*?>xt"
3984 (org-test-with-temp-text "Te<point>xt"
3985 (flet ((org-read-date
3986 (&rest args)
3987 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3988 (org-time-stamp nil)
3989 (buffer-string)))))
3990 ;; With a prefix argument, also insert time.
3991 (should
3992 (string-match
3993 "Te<2014-03-04 .*? 00:41>xt"
3994 (org-test-with-temp-text "Te<point>xt"
3995 (flet ((org-read-date
3996 (&rest args)
3997 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
3998 (org-time-stamp '(4))
3999 (buffer-string)))))
4000 ;; With two universal prefix arguments, insert an active timestamp
4001 ;; with the current time without prompting the user.
4002 (should
4003 (string-match
4004 "Te<2014-03-04 .*? 00:41>xt"
4005 (org-test-with-temp-text "Te<point>xt"
4006 (flet ((current-time
4008 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
4009 (org-time-stamp '(16))
4010 (buffer-string)))))
4011 ;; When optional argument is non-nil, insert an inactive timestamp.
4012 (should
4013 (string-match
4014 "Te\\[2014-03-04 .*?\\]xt"
4015 (org-test-with-temp-text "Te<point>xt"
4016 (flet ((org-read-date
4017 (&rest args)
4018 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
4019 (org-time-stamp nil t)
4020 (buffer-string)))))
4021 ;; When called from a timestamp, replace existing one.
4022 (should
4023 (string-match
4024 "<2014-03-04 .*?>"
4025 (org-test-with-temp-text "<2012-03-29<point> thu.>"
4026 (flet ((org-read-date
4027 (&rest args)
4028 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
4029 (org-time-stamp nil)
4030 (buffer-string)))))
4031 (should
4032 (string-match
4033 "<2014-03-04 .*?>--<2014-03-04 .*?>"
4034 (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
4035 (flet ((org-read-date
4036 (&rest args)
4037 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
4038 (org-time-stamp nil)
4039 (buffer-string)))))
4040 ;; When replacing a timestamp, preserve repeater, if any.
4041 (should
4042 (string-match
4043 "<2014-03-04 .*? \\+2y>"
4044 (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
4045 (flet ((org-read-date
4046 (&rest args)
4047 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
4048 (org-time-stamp nil)
4049 (buffer-string)))))
4050 ;; When called twice in a raw, build a date range.
4051 (should
4052 (string-match
4053 "<2012-03-29 .*?>--<2014-03-04 .*?>"
4054 (org-test-with-temp-text "<2012-03-29 thu.><point>"
4055 (flet ((org-read-date
4056 (&rest args)
4057 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
4058 (let ((last-command 'org-time-stamp)
4059 (this-command 'org-time-stamp))
4060 (org-time-stamp nil))
4061 (buffer-string))))))
4063 (ert-deftest test-org/timestamp-has-time-p ()
4064 "Test `org-timestamp-has-time-p' specifications."
4065 ;; With time.
4066 (should
4067 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
4068 (org-timestamp-has-time-p (org-element-context))))
4069 ;; Without time.
4070 (should-not
4071 (org-test-with-temp-text "<2012-03-29 Thu>"
4072 (org-timestamp-has-time-p (org-element-context)))))
4074 (ert-deftest test-org/timestamp-format ()
4075 "Test `org-timestamp-format' specifications."
4076 ;; Regular test.
4077 (should
4078 (equal
4079 "2012-03-29 16:40"
4080 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
4081 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
4082 ;; Range end.
4083 (should
4084 (equal
4085 "2012-03-29"
4086 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
4087 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
4089 (ert-deftest test-org/timestamp-split-range ()
4090 "Test `org-timestamp-split-range' specifications."
4091 ;; Extract range start (active).
4092 (should
4093 (equal '(2012 3 29)
4094 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4095 (let ((ts (org-timestamp-split-range (org-element-context))))
4096 (mapcar (lambda (p) (org-element-property p ts))
4097 '(:year-end :month-end :day-end))))))
4098 ;; Extract range start (inactive)
4099 (should
4100 (equal '(2012 3 29)
4101 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
4102 (let ((ts (org-timestamp-split-range (org-element-context))))
4103 (mapcar (lambda (p) (org-element-property p ts))
4104 '(:year-end :month-end :day-end))))))
4105 ;; Extract range end (active).
4106 (should
4107 (equal '(2012 3 30)
4108 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4109 (let ((ts (org-timestamp-split-range
4110 (org-element-context) t)))
4111 (mapcar (lambda (p) (org-element-property p ts))
4112 '(:year-end :month-end :day-end))))))
4113 ;; Extract range end (inactive)
4114 (should
4115 (equal '(2012 3 30)
4116 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
4117 (let ((ts (org-timestamp-split-range
4118 (org-element-context) t)))
4119 (mapcar (lambda (p) (org-element-property p ts))
4120 '(:year-end :month-end :day-end))))))
4121 ;; Return the timestamp if not a range.
4122 (should
4123 (org-test-with-temp-text "[2012-03-29 Thu]"
4124 (let* ((ts-orig (org-element-context))
4125 (ts-copy (org-timestamp-split-range ts-orig)))
4126 (eq ts-orig ts-copy))))
4127 (should
4128 (org-test-with-temp-text "<%%(org-float t 4 2)>"
4129 (let* ((ts-orig (org-element-context))
4130 (ts-copy (org-timestamp-split-range ts-orig)))
4131 (eq ts-orig ts-copy)))))
4133 (ert-deftest test-org/timestamp-translate ()
4134 "Test `org-timestamp-translate' specifications."
4135 ;; Translate whole date range.
4136 (should
4137 (equal "<29>--<30>"
4138 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4139 (let ((org-display-custom-times t)
4140 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4141 (org-timestamp-translate (org-element-context))))))
4142 ;; Translate date range start.
4143 (should
4144 (equal "<29>"
4145 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4146 (let ((org-display-custom-times t)
4147 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4148 (org-timestamp-translate (org-element-context) 'start)))))
4149 ;; Translate date range end.
4150 (should
4151 (equal "<30>"
4152 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4153 (let ((org-display-custom-times t)
4154 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4155 (org-timestamp-translate (org-element-context) 'end)))))
4156 ;; Translate time range.
4157 (should
4158 (equal "<08>--<16>"
4159 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
4160 (let ((org-display-custom-times t)
4161 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
4162 (org-timestamp-translate (org-element-context))))))
4163 ;; Translate non-range timestamp.
4164 (should
4165 (equal "<29>"
4166 (org-test-with-temp-text "<2012-03-29 Thu>"
4167 (let ((org-display-custom-times t)
4168 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4169 (org-timestamp-translate (org-element-context))))))
4170 ;; Do not change `diary' timestamps.
4171 (should
4172 (equal "<%%(org-float t 4 2)>"
4173 (org-test-with-temp-text "<%%(org-float t 4 2)>"
4174 (let ((org-display-custom-times t)
4175 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4176 (org-timestamp-translate (org-element-context)))))))
4180 ;;; Visibility
4182 (ert-deftest test-org/flag-drawer ()
4183 "Test `org-flag-drawer' specifications."
4184 ;; Hide drawer.
4185 (should
4186 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4187 (org-flag-drawer t)
4188 (get-char-property (line-end-position) 'invisible)))
4189 ;; Show drawer.
4190 (should-not
4191 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4192 (org-flag-drawer t)
4193 (org-flag-drawer nil)
4194 (get-char-property (line-end-position) 'invisible)))
4195 ;; Test optional argument.
4196 (should
4197 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
4198 (let ((drawer (save-excursion (search-forward ":D2")
4199 (org-element-at-point))))
4200 (org-flag-drawer t drawer)
4201 (get-char-property (progn (search-forward ":D2") (line-end-position))
4202 'invisible))))
4203 (should-not
4204 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
4205 (let ((drawer (save-excursion (search-forward ":D2")
4206 (org-element-at-point))))
4207 (org-flag-drawer t drawer)
4208 (get-char-property (line-end-position) 'invisible))))
4209 ;; Do not hide fake drawers.
4210 (should-not
4211 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
4212 (forward-line 1)
4213 (org-flag-drawer t)
4214 (get-char-property (line-end-position) 'invisible)))
4215 ;; Do not hide incomplete drawers.
4216 (should-not
4217 (org-test-with-temp-text ":D:\nparagraph"
4218 (forward-line 1)
4219 (org-flag-drawer t)
4220 (get-char-property (line-end-position) 'invisible)))
4221 ;; Do not hide drawers when called from final blank lines.
4222 (should-not
4223 (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
4224 (goto-char (point-max))
4225 (org-flag-drawer t)
4226 (goto-char (point-min))
4227 (get-char-property (line-end-position) 'invisible)))
4228 ;; Don't leave point in an invisible part of the buffer when hiding
4229 ;; a drawer away.
4230 (should-not
4231 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4232 (goto-char (point-max))
4233 (org-flag-drawer t)
4234 (get-char-property (point) 'invisible))))
4236 (ert-deftest test-org/hide-block-toggle ()
4237 "Test `org-hide-block-toggle' specifications."
4238 ;; Error when not at a block.
4239 (should-error
4240 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
4241 (org-hide-block-toggle 'off)
4242 (get-char-property (line-end-position) 'invisible)))
4243 ;; Hide block.
4244 (should
4245 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
4246 (org-hide-block-toggle)
4247 (get-char-property (line-end-position) 'invisible)))
4248 (should
4249 (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
4250 (org-hide-block-toggle)
4251 (get-char-property (line-end-position) 'invisible)))
4252 ;; Show block unconditionally when optional argument is `off'.
4253 (should-not
4254 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4255 (org-hide-block-toggle)
4256 (org-hide-block-toggle 'off)
4257 (get-char-property (line-end-position) 'invisible)))
4258 (should-not
4259 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4260 (org-hide-block-toggle 'off)
4261 (get-char-property (line-end-position) 'invisible)))
4262 ;; Hide block unconditionally when optional argument is non-nil.
4263 (should
4264 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4265 (org-hide-block-toggle t)
4266 (get-char-property (line-end-position) 'invisible)))
4267 (should
4268 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4269 (org-hide-block-toggle)
4270 (org-hide-block-toggle t)
4271 (get-char-property (line-end-position) 'invisible)))
4272 ;; Do not hide block when called from final blank lines.
4273 (should-not
4274 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
4275 (org-hide-block-toggle)
4276 (goto-char (point-min))
4277 (get-char-property (line-end-position) 'invisible)))
4278 ;; Don't leave point in an invisible part of the buffer when hiding
4279 ;; a block away.
4280 (should-not
4281 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
4282 (org-hide-block-toggle)
4283 (get-char-property (point) 'invisible))))
4285 (ert-deftest test-org/hide-block-toggle-maybe ()
4286 "Test `org-hide-block-toggle-maybe' specifications."
4287 (should
4288 (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
4289 (org-hide-block-toggle-maybe)))
4290 (should-not
4291 (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
4293 (ert-deftest test-org/show-set-visibility ()
4294 "Test `org-show-set-visibility' specifications."
4295 ;; Do not throw an error before first heading.
4296 (should
4297 (org-test-with-temp-text "Preamble\n* Headline"
4298 (org-show-set-visibility 'tree)
4300 ;; Test all visibility spans, both on headline and in entry.
4301 (let ((list-visible-lines
4302 (lambda (state headerp)
4303 (org-test-with-temp-text "* Grandmother (0)
4304 ** Uncle (1)
4305 *** Heir (2)
4306 ** Father (3)
4307 Ancestor text (4)
4308 *** Sister (5)
4309 Sibling text (6)
4310 *** Self (7)
4311 Match (8)
4312 **** First born (9)
4313 Child text (10)
4314 **** The other child (11)
4315 *** Brother (12)
4316 ** Aunt (13)
4318 (org-cycle t)
4319 (search-forward (if headerp "Self" "Match"))
4320 (org-show-set-visibility state)
4321 (goto-char (point-min))
4322 (let (result (line 0))
4323 (while (not (eobp))
4324 (unless (org-invisible-p2) (push line result))
4325 (incf line)
4326 (forward-line))
4327 (nreverse result))))))
4328 (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
4329 (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
4330 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
4331 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
4332 (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
4333 (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
4334 (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
4335 (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
4336 (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
4337 (should (equal '(0 1 3 5 7 8 9 11 12 13)
4338 (funcall list-visible-lines 'tree nil)))
4339 (should (equal '(0 1 3 4 5 7 12 13)
4340 (funcall list-visible-lines 'canonical t)))
4341 (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
4342 (funcall list-visible-lines 'canonical nil)))))
4345 (provide 'test-org)
4347 ;;; test-org.el ends here