Fix `org-entry-properties' with default priority
[org-mode.git] / testing / lisp / test-org.el
blobd41e3a349324d9d137af00f0f67027e468899709
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 (zerop
649 (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
650 (goto-char (point-max))
651 (let ((org-adapt-indentation t)) (org-indent-line))
652 (org-get-indentation))))
653 (should
654 (= 4
655 (org-test-with-temp-text "* H\n- A\n - \n"
656 (goto-char (point-max))
657 (let ((org-adapt-indentation t)) (org-indent-line))
658 (org-get-indentation))))
659 ;; Likewise, on a blank line at the end of a footnote definition,
660 ;; indent at column 0 if line belongs to the definition. Otherwise,
661 ;; indent like the definition itself.
662 (should
663 (zerop
664 (org-test-with-temp-text "* H\n[fn:1] Definition\n"
665 (goto-char (point-max))
666 (let ((org-adapt-indentation t)) (org-indent-line))
667 (org-get-indentation))))
668 (should
669 (zerop
670 (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
671 (goto-char (point-max))
672 (let ((org-adapt-indentation t)) (org-indent-line))
673 (org-get-indentation))))
674 ;; After the end of the contents of a greater element, indent like
675 ;; the beginning of the element.
676 (should
677 (= 1
678 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
679 (forward-line 2)
680 (org-indent-line)
681 (org-get-indentation))))
682 ;; On blank lines after a paragraph, indent like its last non-empty
683 ;; line.
684 (should
685 (= 1
686 (org-test-with-temp-text " Paragraph\n\n<point>"
687 (org-indent-line)
688 (org-get-indentation))))
689 ;; At the first line of an element, indent like previous element's
690 ;; first line, ignoring footnotes definitions and inline tasks, or
691 ;; according to parent.
692 (should
693 (= 2
694 (org-test-with-temp-text "A\n\n B\n\nC"
695 (goto-char (point-max))
696 (org-indent-line)
697 (org-get-indentation))))
698 (should
699 (= 1
700 (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
701 (goto-char (point-max))
702 (org-indent-line)
703 (org-get-indentation))))
704 (should
705 (= 1
706 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
707 (forward-line 1)
708 (org-indent-line)
709 (org-get-indentation))))
710 ;; Within code part of a source block, use language major mode if
711 ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
712 ;; according to line above.
713 (should
714 (= 6
715 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
716 (forward-line 2)
717 (let ((org-src-tab-acts-natively t)
718 (org-edit-src-content-indentation 0))
719 (org-indent-line))
720 (org-get-indentation))))
721 (should
722 (= 1
723 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
724 (forward-line 2)
725 (let ((org-src-tab-acts-natively nil)
726 (org-edit-src-content-indentation 0))
727 (org-indent-line))
728 (org-get-indentation))))
729 ;; Otherwise, indent like the first non-blank line above.
730 (should
731 (zerop
732 (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
733 (forward-line 3)
734 (org-indent-line)
735 (org-get-indentation))))
736 ;; Align node properties according to `org-property-format'. Handle
737 ;; nicely empty values.
738 (should
739 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
740 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
741 (let ((org-property-format "%-10s %s")) (org-indent-line))
742 (buffer-string))))
743 (should
744 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
745 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
746 (let ((org-property-format "%-10s %s")) (org-indent-line))
747 (buffer-string)))))
749 (ert-deftest test-org/indent-region ()
750 "Test `org-indent-region' specifications."
751 ;; Indent paragraph.
752 (should
753 (equal "A\nB\nC"
754 (org-test-with-temp-text " A\nB\n C"
755 (org-indent-region (point-min) (point-max))
756 (buffer-string))))
757 ;; Indent greater elements along with their contents.
758 (should
759 (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
760 (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
761 (org-indent-region (point-min) (point-max))
762 (buffer-string))))
763 ;; Ignore contents of verse blocks and example blocks.
764 (should
765 (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
766 (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
767 (org-indent-region (point-min) (point-max))
768 (buffer-string))))
769 (should
770 (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
771 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
772 (org-indent-region (point-min) (point-max))
773 (buffer-string))))
774 ;; Indent according to mode if `org-src-tab-acts-natively' is
775 ;; non-nil. Otherwise, do not indent code at all.
776 (should
777 (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
778 (org-test-with-temp-text
779 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
780 (let ((org-src-tab-acts-natively t)
781 (org-edit-src-content-indentation 0))
782 (org-indent-region (point-min) (point-max)))
783 (buffer-string))))
784 (should
785 (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
786 (org-test-with-temp-text
787 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
788 (let ((org-src-tab-acts-natively nil)
789 (org-edit-src-content-indentation 0))
790 (org-indent-region (point-min) (point-max)))
791 (buffer-string))))
792 ;; Align node properties according to `org-property-format'. Handle
793 ;; nicely empty values.
794 (should
795 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
796 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
797 (let ((org-property-format "%-10s %s")
798 (org-adapt-indentation nil))
799 (org-indent-region (point) (point-max)))
800 (buffer-string))))
801 (should
802 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
803 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
804 (let ((org-property-format "%-10s %s")
805 (org-adapt-indentation nil))
806 (org-indent-region (point) (point-max)))
807 (buffer-string))))
808 ;; Indent plain lists.
809 (should
810 (equal "- A\n B\n - C\n\n D"
811 (org-test-with-temp-text "- A\n B\n - C\n\n D"
812 (org-indent-region (point-min) (point-max))
813 (buffer-string))))
814 (should
815 (equal "- A\n\n- B"
816 (org-test-with-temp-text " - A\n\n - B"
817 (org-indent-region (point-min) (point-max))
818 (buffer-string))))
819 ;; Indent footnote definitions.
820 (should
821 (equal "[fn:1] Definition\n\nDefinition"
822 (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
823 (org-indent-region (point-min) (point-max))
824 (buffer-string))))
825 ;; Special case: Start indenting on a blank line.
826 (should
827 (equal "\nParagraph"
828 (org-test-with-temp-text "\n Paragraph"
829 (org-indent-region (point-min) (point-max))
830 (buffer-string)))))
834 ;;; Editing
836 (ert-deftest test-org/delete-indentation ()
837 "Test `org-delete-indentation' specifications."
838 ;; Regular test.
839 (should (equal "foo bar"
840 (org-test-with-temp-text
841 "foo \n bar<point>"
842 (org-delete-indentation)
843 (buffer-string))))
844 ;; With optional argument.
845 (should (equal "foo bar"
846 (org-test-with-temp-text
847 "foo<point> \n bar"
848 (org-delete-indentation t)
849 (buffer-string))))
850 ;; At headline text should be appended to the headline text.
851 (should
852 (equal"* foo bar :tag:"
853 (let (org-auto-align-tags)
854 (org-test-with-temp-text
855 "* foo :tag:\n bar<point>"
856 (org-delete-indentation)
857 (buffer-string)))))
858 (should
859 (equal "* foo bar :tag:"
860 (let (org-auto-align-tags)
861 (org-test-with-temp-text
862 "* foo <point>:tag:\n bar"
863 (org-delete-indentation t)
864 (buffer-string))))))
866 (ert-deftest test-org/return ()
867 "Test `org-return' specifications."
868 ;; Regular test.
869 (should
870 (equal "Para\ngraph"
871 (org-test-with-temp-text "Para<point>graph"
872 (org-return)
873 (buffer-string))))
874 ;; With optional argument, indent line.
875 (should
876 (equal " Para\n graph"
877 (org-test-with-temp-text " Para<point>graph"
878 (org-return t)
879 (buffer-string))))
880 ;; On a table, call `org-table-next-row'.
881 (should
882 (org-test-with-temp-text "| <point>a |\n| b |"
883 (org-return)
884 (org-looking-at-p "b")))
885 ;; Open link or timestamp under point when `org-return-follows-link'
886 ;; is non-nil.
887 (should
888 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
889 (let ((org-return-follows-link t)
890 (org-link-search-must-match-exact-headline nil)) (org-return))
891 (org-looking-at-p "<<target>>")))
892 (should-not
893 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
894 (let ((org-return-follows-link nil)) (org-return))
895 (org-looking-at-p "<<target>>")))
896 ;; Link in heading should also be opened when
897 ;; `org-return-follows-link` is non-nil.
898 (should
899 (org-test-with-temp-text "* [[b][a<point>]]\n* b"
900 (let ((org-return-follows-link t)) (org-return))
901 (org-looking-at-p "* b")))
902 ;; However, do not open link when point is in a table.
903 (should
904 (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
905 (let ((org-return-follows-link t)) (org-return))
906 (org-looking-at-p "between")))
907 ;; Special case: in a list, when indenting, do not break structure.
908 (should
909 (equal "- A\n B"
910 (org-test-with-temp-text "- A <point>B"
911 (org-return t)
912 (buffer-string))))
913 (should
914 (equal "- A\n\n- B"
915 (org-test-with-temp-text "- A\n<point>- B"
916 (org-return t)
917 (buffer-string))))
918 ;; On tags part of a headline, add a newline below it instead of
919 ;; breaking it.
920 (should
921 (equal "* H :tag:\n"
922 (org-test-with-temp-text "* H :<point>tag:"
923 (org-return)
924 (buffer-string))))
925 ;; Before headline text, add a newline below it instead of breaking
926 ;; it.
927 (should
928 (equal "* TODO H :tag:\n"
929 (org-test-with-temp-text "* <point>TODO H :tag:"
930 (org-return)
931 (buffer-string))))
932 (should
933 (equal "* TODO [#B] H :tag:\n"
934 (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
935 (org-return)
936 (buffer-string))))
937 ;; At headline text, break headline text but preserve tags.
938 (should
939 (equal "* TODO [#B] foo :tag:\nbar"
940 (let (org-auto-align-tags)
941 (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
942 (org-return)
943 (buffer-string)))))
944 ;; At bol of headline insert newline.
945 (should
946 (equal "\n* h"
947 (org-test-with-temp-text "<point>* h"
948 (org-return)
949 (buffer-string))))
950 ;; Refuse to leave invalid headline in buffer.
951 (should
952 (equal "* h\n"
953 (org-test-with-temp-text "*<point> h"
954 (org-return)
955 (buffer-string)))))
957 (ert-deftest test-org/meta-return ()
958 "Test M-RET (`org-meta-return') specifications."
959 ;; In a table field insert a row above.
960 (should
961 (org-test-with-temp-text "| a |"
962 (forward-char)
963 (org-meta-return)
964 (forward-line -1)
965 (looking-at "| |$")))
966 ;; In a paragraph change current line into a header.
967 (should
968 (org-test-with-temp-text "a"
969 (org-meta-return)
970 (beginning-of-line)
971 (looking-at "\* a$")))
972 ;; In an item insert an item, in this case above.
973 (should
974 (org-test-with-temp-text "- a"
975 (org-meta-return)
976 (beginning-of-line)
977 (looking-at "- $")))
978 ;; In a drawer and item insert an item, in this case above.
979 (should
980 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
981 (forward-line)
982 (org-meta-return)
983 (beginning-of-line)
984 (looking-at "- $"))))
986 (ert-deftest test-org/insert-heading ()
987 "Test `org-insert-heading' specifications."
988 ;; In an empty buffer, insert a new headline.
989 (should
990 (equal "* "
991 (org-test-with-temp-text ""
992 (org-insert-heading)
993 (buffer-string))))
994 ;; At the beginning of a line, turn it into a headline
995 (should
996 (equal "* P"
997 (org-test-with-temp-text "<point>P"
998 (org-insert-heading)
999 (buffer-string))))
1000 ;; In the middle of a line, split the line if allowed, otherwise,
1001 ;; insert the headline at its end.
1002 (should
1003 (equal "Para\n* graph"
1004 (org-test-with-temp-text "Para<point>graph"
1005 (let ((org-M-RET-may-split-line '((default . t))))
1006 (org-insert-heading))
1007 (buffer-string))))
1008 (should
1009 (equal "Paragraph\n* "
1010 (org-test-with-temp-text "Para<point>graph"
1011 (let ((org-M-RET-may-split-line '((default . nil))))
1012 (org-insert-heading))
1013 (buffer-string))))
1014 ;; When on a list, insert an item instead, unless called with an
1015 ;; universal argument or if list is invisible. In this case, create
1016 ;; a new headline after contents.
1017 (should
1018 (equal "* H\n- item\n- "
1019 (org-test-with-temp-text "* H\n- item<point>"
1020 (let ((org-insert-heading-respect-content nil))
1021 (org-insert-heading))
1022 (buffer-string))))
1023 (should
1024 (equal "* H\n- item\n- item 2\n* "
1025 (org-test-with-temp-text "* H\n- item<point>\n- item 2"
1026 (let ((org-insert-heading-respect-content nil))
1027 (org-insert-heading '(4)))
1028 (buffer-string))))
1029 (should
1030 (equal "* H\n- item\n* "
1031 (org-test-with-temp-text "* H\n- item"
1032 (org-cycle)
1033 (goto-char (point-max))
1034 (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
1035 (buffer-string))))
1036 ;; When called with two universal arguments, insert a new headline
1037 ;; at the end of the grandparent subtree.
1038 (should
1039 (equal "* H1\n** H3\n- item\n** H2\n** "
1040 (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
1041 (let ((org-insert-heading-respect-content nil))
1042 (org-insert-heading '(16)))
1043 (buffer-string))))
1044 ;; When optional TOP-LEVEL argument is non-nil, always insert
1045 ;; a level 1 heading.
1046 (should
1047 (equal "* H1\n** H2\n* "
1048 (org-test-with-temp-text "* H1\n** H2<point>"
1049 (org-insert-heading nil nil t)
1050 (buffer-string))))
1051 (should
1052 (equal "* H1\n- item\n* "
1053 (org-test-with-temp-text "* H1\n- item<point>"
1054 (org-insert-heading nil nil t)
1055 (buffer-string))))
1056 ;; Corner case: correctly insert a headline after an empty one.
1057 (should
1058 (equal "* \n* "
1059 (org-test-with-temp-text "* <point>"
1060 (org-insert-heading)
1061 (buffer-string)))))
1063 (ert-deftest test-org/insert-todo-heading-respect-content ()
1064 "Test `org-insert-todo-heading-respect-content' specifications."
1065 ;; Create a TODO heading.
1066 (should
1067 (org-test-with-temp-text "* H1\n Body"
1068 (org-insert-todo-heading-respect-content)
1069 (nth 2 (org-heading-components))))
1070 ;; Add headline at the end of the first subtree
1071 (should
1072 (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
1073 (search-forward "H1Body")
1074 (org-insert-todo-heading-respect-content)
1075 (and (eobp) (org-at-heading-p))))
1076 ;; In a list, do not create a new item.
1077 (should
1078 (org-test-with-temp-text "* H\n- an item\n- another one"
1079 (search-forward "an ")
1080 (org-insert-todo-heading-respect-content)
1081 (and (eobp) (org-at-heading-p)))))
1083 (ert-deftest test-org/clone-with-time-shift ()
1084 "Test `org-clone-subtree-with-time-shift'."
1085 ;; Clone non-repeating once.
1086 (should
1087 (equal "\
1088 * H1\n<2015-06-21>
1089 * H1\n<2015-06-23>
1091 (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
1092 (org-clone-subtree-with-time-shift 1 "+2d")
1093 (replace-regexp-in-string
1094 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1095 nil nil 1))))
1096 ;; Clone repeating once.
1097 (should
1098 (equal "\
1099 * H1\n<2015-06-21>
1100 * H1\n<2015-06-23>
1101 * H1\n<2015-06-25 +1w>
1103 (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
1104 (org-clone-subtree-with-time-shift 1 "+2d")
1105 (replace-regexp-in-string
1106 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1107 nil nil 1))))
1108 ;; Clone non-repeating zero times.
1109 (should
1110 (equal "\
1111 * H1\n<2015-06-21>
1113 (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
1114 (org-clone-subtree-with-time-shift 0 "+2d")
1115 (replace-regexp-in-string
1116 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1117 nil nil 1))))
1118 ;; Clone repeating "zero" times.
1119 (should
1120 (equal "\
1121 * H1\n<2015-06-21>
1122 * H1\n<2015-06-23 +1w>
1124 (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
1125 (org-clone-subtree-with-time-shift 0 "+2d")
1126 (replace-regexp-in-string
1127 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1128 nil nil 1)))))
1131 ;;; Fixed-Width Areas
1133 (ert-deftest test-org/toggle-fixed-width ()
1134 "Test `org-toggle-fixed-width' specifications."
1135 ;; No region: Toggle on fixed-width marker in paragraphs.
1136 (should
1137 (equal ": A"
1138 (org-test-with-temp-text "A"
1139 (org-toggle-fixed-width)
1140 (buffer-string))))
1141 ;; No region: Toggle off fixed-width markers in fixed-width areas.
1142 (should
1143 (equal "A"
1144 (org-test-with-temp-text ": A"
1145 (org-toggle-fixed-width)
1146 (buffer-string))))
1147 ;; No region: Toggle on marker in blank lines after elements or just
1148 ;; after a headline.
1149 (should
1150 (equal "* H\n: "
1151 (org-test-with-temp-text "* H\n"
1152 (forward-line)
1153 (org-toggle-fixed-width)
1154 (buffer-string))))
1155 (should
1156 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
1157 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
1158 (goto-char (point-max))
1159 (org-toggle-fixed-width)
1160 (buffer-string))))
1161 ;; No region: Toggle on marker in front of one line elements (e.g.,
1162 ;; headlines, clocks)
1163 (should
1164 (equal ": * Headline"
1165 (org-test-with-temp-text "* Headline"
1166 (org-toggle-fixed-width)
1167 (buffer-string))))
1168 (should
1169 (equal ": #+KEYWORD: value"
1170 (org-test-with-temp-text "#+KEYWORD: value"
1171 (org-toggle-fixed-width)
1172 (buffer-string))))
1173 ;; No region: error in other situations.
1174 (should-error
1175 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
1176 (forward-line)
1177 (org-toggle-fixed-width)
1178 (buffer-string)))
1179 ;; No region: Indentation is preserved.
1180 (should
1181 (equal "- A\n : B"
1182 (org-test-with-temp-text "- A\n B"
1183 (forward-line)
1184 (org-toggle-fixed-width)
1185 (buffer-string))))
1186 ;; Region: If it contains only fixed-width elements and blank lines,
1187 ;; toggle off fixed-width markup.
1188 (should
1189 (equal "A\n\nB"
1190 (org-test-with-temp-text ": A\n\n: B"
1191 (transient-mark-mode 1)
1192 (push-mark (point) t t)
1193 (goto-char (point-max))
1194 (org-toggle-fixed-width)
1195 (buffer-string))))
1196 ;; Region: If it contains anything else, toggle on fixed-width but
1197 ;; not on fixed-width areas.
1198 (should
1199 (equal ": A\n: \n: B\n: \n: C"
1200 (org-test-with-temp-text "A\n\n: B\n\nC"
1201 (transient-mark-mode 1)
1202 (push-mark (point) t t)
1203 (goto-char (point-max))
1204 (org-toggle-fixed-width)
1205 (buffer-string))))
1206 ;; Region: Ignore blank lines at its end, unless it contains only
1207 ;; such lines.
1208 (should
1209 (equal ": A\n\n"
1210 (org-test-with-temp-text "A\n\n"
1211 (transient-mark-mode 1)
1212 (push-mark (point) t t)
1213 (goto-char (point-max))
1214 (org-toggle-fixed-width)
1215 (buffer-string))))
1216 (should
1217 (equal ": \n: \n"
1218 (org-test-with-temp-text "\n\n"
1219 (transient-mark-mode 1)
1220 (push-mark (point) t t)
1221 (goto-char (point-max))
1222 (org-toggle-fixed-width)
1223 (buffer-string)))))
1227 ;;; Headline
1229 (ert-deftest test-org/in-commented-heading-p ()
1230 "Test `org-in-commented-heading-p' specifications."
1231 ;; Commented headline.
1232 (should
1233 (org-test-with-temp-text "* COMMENT Headline\nBody"
1234 (goto-char (point-max))
1235 (org-in-commented-heading-p)))
1236 ;; Commented ancestor.
1237 (should
1238 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1239 (goto-char (point-max))
1240 (org-in-commented-heading-p)))
1241 ;; Comment keyword is case-sensitive.
1242 (should-not
1243 (org-test-with-temp-text "* Comment Headline\nBody"
1244 (goto-char (point-max))
1245 (org-in-commented-heading-p)))
1246 ;; Keyword is standalone.
1247 (should-not
1248 (org-test-with-temp-text "* COMMENTHeadline\nBody"
1249 (goto-char (point-max))
1250 (org-in-commented-heading-p)))
1251 ;; Optional argument.
1252 (should-not
1253 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1254 (goto-char (point-max))
1255 (org-in-commented-heading-p t))))
1257 (ert-deftest test-org/entry-blocked-p ()
1258 ;; Check other dependencies.
1259 (should
1260 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
1261 (let ((org-enforce-todo-dependencies t)
1262 (org-blocker-hook
1263 '(org-block-todo-from-children-or-siblings-or-parent)))
1264 (org-entry-blocked-p))))
1265 (should-not
1266 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
1267 (let ((org-enforce-todo-dependencies t)
1268 (org-blocker-hook
1269 '(org-block-todo-from-children-or-siblings-or-parent)))
1270 (org-entry-blocked-p))))
1271 ;; Entry without a TODO keyword or with a DONE keyword cannot be
1272 ;; blocked.
1273 (should-not
1274 (org-test-with-temp-text "* Blocked\n** TODO one"
1275 (let ((org-enforce-todo-dependencies t)
1276 (org-blocker-hook
1277 '(org-block-todo-from-children-or-siblings-or-parent)))
1278 (org-entry-blocked-p))))
1279 (should-not
1280 (org-test-with-temp-text "* DONE Blocked\n** TODO one"
1281 (let ((org-enforce-todo-dependencies t)
1282 (org-blocker-hook
1283 '(org-block-todo-from-children-or-siblings-or-parent)))
1284 (org-entry-blocked-p))))
1285 ;; Follow :ORDERED: specifications.
1286 (should
1287 (org-test-with-temp-text
1288 "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
1289 (let ((org-enforce-todo-dependencies t)
1290 (org-blocker-hook
1291 '(org-block-todo-from-children-or-siblings-or-parent)))
1292 (org-entry-blocked-p))))
1293 (should-not
1294 (org-test-with-temp-text
1295 "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
1296 (let ((org-enforce-todo-dependencies t)
1297 (org-blocker-hook
1298 '(org-block-todo-from-children-or-siblings-or-parent)))
1299 (org-entry-blocked-p)))))
1302 ;;; Keywords
1304 (ert-deftest test-org/set-regexps-and-options ()
1305 "Test `org-set-regexps-and-options' specifications."
1306 ;; TAGS keyword.
1307 (should
1308 (equal '(("A" . ?a) ("B") ("C"))
1309 (org-test-with-temp-text "#+TAGS: A(a) B C"
1310 (org-mode-restart)
1311 org-tag-alist)))
1312 (should
1313 (equal '(("A") (:newline) ("B"))
1314 (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
1315 (org-mode-restart)
1316 org-tag-alist)))
1317 (should
1318 (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
1319 (org-test-with-temp-text "#+TAGS: { A B } C"
1320 (org-mode-restart)
1321 org-tag-alist)))
1322 (should
1323 (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
1324 (org-test-with-temp-text "#+TAGS: { A : B C }"
1325 (org-mode-restart)
1326 org-tag-alist)))
1327 (should
1328 (equal '(("A" "B" "C"))
1329 (org-test-with-temp-text "#+TAGS: { A : B C }"
1330 (org-mode-restart)
1331 org-tag-groups-alist)))
1332 (should
1333 (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
1334 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1335 (org-mode-restart)
1336 org-tag-alist)))
1337 (should
1338 (equal '(("A" "B" "C"))
1339 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1340 (org-mode-restart)
1341 org-tag-groups-alist)))
1342 ;; FILETAGS keyword.
1343 (should
1344 (equal '("A" "B" "C")
1345 (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
1346 (org-mode-restart)
1347 org-file-tags)))
1348 ;; PROPERTY keyword. Property names are case-insensitive.
1349 (should
1350 (equal "foo=1"
1351 (org-test-with-temp-text "#+PROPERTY: var foo=1"
1352 (org-mode-restart)
1353 (cdr (assoc "var" org-file-properties)))))
1354 (should
1355 (equal
1356 "foo=1 bar=2"
1357 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
1358 (org-mode-restart)
1359 (cdr (assoc "var" org-file-properties)))))
1360 (should
1361 (equal
1362 "foo=1 bar=2"
1363 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
1364 (org-mode-restart)
1365 (cdr (assoc "var" org-file-properties)))))
1366 ;; ARCHIVE keyword.
1367 (should
1368 (equal "%s_done::"
1369 (org-test-with-temp-text "#+ARCHIVE: %s_done::"
1370 (org-mode-restart)
1371 org-archive-location)))
1372 ;; CATEGORY keyword.
1373 (should
1374 (eq 'test
1375 (org-test-with-temp-text "#+CATEGORY: test"
1376 (org-mode-restart)
1377 org-category)))
1378 (should
1379 (equal "test"
1380 (org-test-with-temp-text "#+CATEGORY: test"
1381 (org-mode-restart)
1382 (cdr (assoc "CATEGORY" org-file-properties)))))
1383 ;; COLUMNS keyword.
1384 (should
1385 (equal "%25ITEM %TAGS %PRIORITY %TODO"
1386 (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
1387 (org-mode-restart)
1388 org-columns-default-format)))
1389 ;; CONSTANTS keyword. Constants names are case sensitive.
1390 (should
1391 (equal '("299792458." "3.14")
1392 (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
1393 (org-mode-restart)
1394 (mapcar
1395 (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
1396 '("c" "pi")))))
1397 (should
1398 (equal "3.14"
1399 (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
1400 (org-mode-restart)
1401 (cdr (assoc "pi" org-table-formula-constants-local)))))
1402 (should
1403 (equal "22/7"
1404 (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
1405 (org-mode-restart)
1406 (cdr (assoc "PI" org-table-formula-constants-local)))))
1407 ;; LINK keyword.
1408 (should
1409 (equal
1410 '("url1" "url2")
1411 (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
1412 (org-mode-restart)
1413 (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
1414 '("a" "b")))))
1415 ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
1416 (should
1417 (equal
1418 '(?X ?Z ?Y)
1419 (org-test-with-temp-text "#+PRIORITIES: X Z Y"
1420 (org-mode-restart)
1421 (list org-highest-priority org-lowest-priority org-default-priority))))
1422 (should
1423 (equal
1424 '(?A ?C ?B)
1425 (org-test-with-temp-text "#+PRIORITIES: X Z"
1426 (org-mode-restart)
1427 (list org-highest-priority org-lowest-priority org-default-priority))))
1428 ;; STARTUP keyword.
1429 (should
1430 (equal '(t t)
1431 (org-test-with-temp-text "#+STARTUP: fold odd"
1432 (org-mode-restart)
1433 (list org-startup-folded org-odd-levels-only))))
1434 ;; TODO keywords.
1435 (should
1436 (equal '(("A" "B") ("C"))
1437 (org-test-with-temp-text "#+TODO: A B | C"
1438 (org-mode-restart)
1439 (list org-not-done-keywords org-done-keywords))))
1440 (should
1441 (equal '(("A" "C") ("B" "D"))
1442 (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
1443 (org-mode-restart)
1444 (list org-not-done-keywords org-done-keywords))))
1445 (should
1446 (equal '(("A" "B") ("C"))
1447 (org-test-with-temp-text "#+TYP_TODO: A B | C"
1448 (org-mode-restart)
1449 (list org-not-done-keywords org-done-keywords))))
1450 (should
1451 (equal '((:startgroup) ("A" . ?a) (:endgroup))
1452 (org-test-with-temp-text "#+TODO: A(a)"
1453 (org-mode-restart)
1454 org-todo-key-alist)))
1455 (should
1456 (equal '(("D" note nil) ("C" time nil) ("B" note time))
1457 (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
1458 (org-mode-restart)
1459 org-todo-log-states)))
1460 ;; Enter SETUPFILE keyword.
1461 (should
1462 (equal "1"
1463 (org-test-with-temp-text
1464 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
1465 (org-mode-restart)
1466 (cdr (assoc "a" org-file-properties))))))
1470 ;;; Links
1472 ;;;; Coderefs
1474 (ert-deftest test-org/coderef ()
1475 "Test coderef links specifications."
1476 (should
1477 (org-test-with-temp-text "
1478 #+BEGIN_SRC emacs-lisp
1479 \(+ 1 1) (ref:sc)
1480 #+END_SRC
1481 \[[(sc)]]<point>"
1482 (org-open-at-point)
1483 (looking-at "(ref:sc)")))
1484 ;; Find coderef even with alternate label format.
1485 (should
1486 (org-test-with-temp-text "
1487 #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
1488 \(+ 1 1) {ref:sc}
1489 #+END_SRC
1490 \[[(sc)]]<point>"
1491 (org-open-at-point)
1492 (looking-at "{ref:sc}"))))
1494 ;;;; Custom ID
1496 (ert-deftest test-org/custom-id ()
1497 "Test custom ID links specifications."
1498 (should
1499 (org-test-with-temp-text
1500 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
1501 (org-open-at-point)
1502 (org-looking-at-p "\\* H1")))
1503 ;; Throw an error on false positives.
1504 (should-error
1505 (org-test-with-temp-text
1506 "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
1507 (org-open-at-point)
1508 (org-looking-at-p "\\* H1"))))
1510 ;;;; Fuzzy Links
1512 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
1513 ;; a named element (#+name: text) and to headlines (* Text).
1515 (ert-deftest test-org/fuzzy-links ()
1516 "Test fuzzy links specifications."
1517 ;; Fuzzy link goes in priority to a matching target.
1518 (should
1519 (org-test-with-temp-text
1520 "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
1521 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1522 (looking-at "<<Test>>")))
1523 ;; Then fuzzy link points to an element with a given name.
1524 (should
1525 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
1526 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1527 (looking-at "#\\+NAME: Test")))
1528 ;; A target still lead to a matching headline otherwise.
1529 (should
1530 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
1531 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1532 (looking-at "\\* Head2")))
1533 ;; With a leading star in link, enforce heading match.
1534 (should
1535 (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
1536 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1537 (looking-at "\\* Test")))
1538 ;; With a leading star in link, enforce exact heading match, even
1539 ;; with `org-link-search-must-match-exact-headline' set to nil.
1540 (should-error
1541 (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
1542 (let ((org-link-search-must-match-exact-headline nil))
1543 (org-open-at-point))))
1544 ;; Handle non-nil `org-link-search-must-match-exact-headline'.
1545 (should
1546 (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
1547 (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
1548 (looking-at "\\* Test")))
1549 (should
1550 (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
1551 (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
1552 (looking-at "\\* Test")))
1553 ;; Heading match should not care about spaces, cookies, TODO
1554 ;; keywords, priorities, and tags.
1555 (should
1556 (let ((first-line
1557 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1558 (org-test-with-temp-text
1559 (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
1560 (let ((org-link-search-must-match-exact-headline nil)
1561 (org-todo-regexp "TODO"))
1562 (org-open-at-point))
1563 (looking-at (regexp-quote first-line)))))
1564 ;; Heading match should still be exact.
1565 (should-error
1566 (let ((first-line
1567 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1568 (org-test-with-temp-text
1569 (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
1570 (let ((org-link-search-must-match-exact-headline nil)
1571 (org-todo-regexp "TODO"))
1572 (org-open-at-point)))))
1573 ;; Heading match ignores COMMENT keyword.
1574 (should
1575 (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
1576 (org-open-at-point)
1577 (looking-at "\\* COMMENT Test")))
1578 ;; Correctly un-hexify fuzzy links.
1579 (should
1580 (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
1581 (goto-char (point-max))
1582 (org-open-at-point)
1583 (bobp))))
1585 ;;;; Link Escaping
1587 (ert-deftest test-org/org-link-escape-ascii-character ()
1588 "Escape an ascii character."
1589 (should
1590 (string=
1591 "%5B"
1592 (org-link-escape "["))))
1594 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
1595 "Escape an ascii control character."
1596 (should
1597 (string=
1598 "%09"
1599 (org-link-escape "\t"))))
1601 (ert-deftest test-org/org-link-escape-multibyte-character ()
1602 "Escape an unicode multibyte character."
1603 (should
1604 (string=
1605 "%E2%82%AC"
1606 (org-link-escape "€"))))
1608 (ert-deftest test-org/org-link-escape-custom-table ()
1609 "Escape string with custom character table."
1610 (should
1611 (string=
1612 "Foo%3A%42ar%0A"
1613 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
1615 (ert-deftest test-org/org-link-escape-custom-table-merge ()
1616 "Escape string with custom table merged with default table."
1617 (should
1618 (string=
1619 "%5BF%6F%6F%3A%42ar%0A%5D"
1620 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
1622 (ert-deftest test-org/org-link-unescape-ascii-character ()
1623 "Unescape an ascii character."
1624 (should
1625 (string=
1627 (org-link-unescape "%5B"))))
1629 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
1630 "Unescpae an ascii control character."
1631 (should
1632 (string=
1633 "\n"
1634 (org-link-unescape "%0A"))))
1636 (ert-deftest test-org/org-link-unescape-multibyte-character ()
1637 "Unescape unicode multibyte character."
1638 (should
1639 (string=
1640 "€"
1641 (org-link-unescape "%E2%82%AC"))))
1643 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
1644 "Unescape old style percent escaped character."
1645 (should
1646 (string=
1647 "àâçèéêîôùû"
1648 (decode-coding-string
1649 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
1651 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
1652 "Escape and unescape a URL that includes an escaped char.
1653 http://article.gmane.org/gmane.emacs.orgmode/21459/"
1654 (should
1655 (string=
1656 "http://some.host.com/form?&id=blah%2Bblah25"
1657 (org-link-unescape
1658 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
1660 (ert-deftest test-org/org-link-escape-chars-browser ()
1661 "Test of the constant `org-link-escape-chars-browser'.
1662 See there why this test is a candidate to be removed once Org
1663 drops support for Emacs 24.1 and 24.2."
1664 (should
1665 (string=
1666 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1667 "%22Release%208.2%22&idxname=emacs-orgmode")
1668 (org-link-escape-browser ; Do not replace with `url-encode-url',
1669 ; see docstring above.
1670 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1671 "\"Release 8.2\"&idxname=emacs-orgmode")))))
1673 ;;;; Open at point
1675 (ert-deftest test-org/open-at-point-in-keyword ()
1676 "Does `org-open-at-point' open link in a keyword line?"
1677 (should
1678 (org-test-with-temp-text
1679 "#+KEYWORD: <point>[[info:emacs#Top]]"
1680 (org-open-at-point) t)))
1682 (ert-deftest test-org/open-at-point-in-property ()
1683 "Does `org-open-at-point' open link in property drawer?"
1684 (should
1685 (org-test-with-temp-text
1686 "* Headline
1687 :PROPERTIES:
1688 :URL: <point>[[info:emacs#Top]]
1689 :END:"
1690 (org-open-at-point) t)))
1692 (ert-deftest test-org/open-at-point-in-comment ()
1693 "Does `org-open-at-point' open link in a commented line?"
1694 (should
1695 (org-test-with-temp-text
1696 "# <point>[[info:emacs#Top]]"
1697 (org-open-at-point) t)))
1699 (ert-deftest test-org/open-at-point/info ()
1700 "Test `org-open-at-point' on info links."
1701 (should
1702 (org-test-with-temp-text
1703 "<point>[[info:emacs#Top]]"
1704 (org-open-at-point)
1705 (and (switch-to-buffer "*info*")
1706 (prog1
1707 (looking-at "\nThe Emacs Editor")
1708 (kill-buffer))))))
1710 (ert-deftest test-org/open-at-point/inline-image ()
1711 "Test `org-open-at-point' on nested links."
1712 (should
1713 (org-test-with-temp-text "[[info:org#Top][info:<point>emacs#Top]]"
1714 (org-open-at-point)
1715 (prog1 (with-current-buffer "*info*" (looking-at "\nOrg Mode Manual"))
1716 (kill-buffer "*info*")))))
1718 (ert-deftest test-org/open-at-point/radio-target ()
1719 "Test `org-open-at-point' on radio targets."
1720 (should
1721 (org-test-with-temp-text "<<<target>>> <point>target"
1722 (org-update-radio-target-regexp)
1723 (org-open-at-point)
1724 (eq (org-element-type (org-element-context)) 'radio-target))))
1727 ;;; Node Properties
1729 (ert-deftest test-org/accumulated-properties-in-drawers ()
1730 "Ensure properties accumulate in subtree drawers."
1731 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
1732 (org-babel-next-src-block)
1733 (should (equal '(2 1) (org-babel-execute-src-block)))))
1735 (ert-deftest test-org/custom-properties ()
1736 "Test custom properties specifications."
1737 ;; Standard test.
1738 (should
1739 (let ((org-custom-properties '("FOO")))
1740 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1741 (org-toggle-custom-properties-visibility)
1742 (org-invisible-p2))))
1743 ;; Properties are case-insensitive.
1744 (should
1745 (let ((org-custom-properties '("FOO")))
1746 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
1747 (org-toggle-custom-properties-visibility)
1748 (org-invisible-p2))))
1749 (should
1750 (let ((org-custom-properties '("foo")))
1751 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1752 (org-toggle-custom-properties-visibility)
1753 (org-invisible-p2))))
1754 ;; Multiple custom properties in the same drawer.
1755 (should
1756 (let ((org-custom-properties '("FOO" "BAR")))
1757 (org-test-with-temp-text
1758 "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
1759 (org-toggle-custom-properties-visibility)
1760 (and (org-invisible-p2)
1761 (not (progn (forward-line) (org-invisible-p2)))
1762 (progn (forward-line) (org-invisible-p2))))))
1763 ;; Hide custom properties with an empty value.
1764 (should
1765 (let ((org-custom-properties '("FOO")))
1766 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
1767 (org-toggle-custom-properties-visibility)
1768 (org-invisible-p2))))
1769 ;; Do not hide fake properties.
1770 (should-not
1771 (let ((org-custom-properties '("FOO")))
1772 (org-test-with-temp-text ":FOO: val\n"
1773 (org-toggle-custom-properties-visibility)
1774 (org-invisible-p2))))
1775 (should-not
1776 (let ((org-custom-properties '("A")))
1777 (org-test-with-temp-text
1778 "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
1779 (org-toggle-custom-properties-visibility)
1780 (org-invisible-p2)))))
1784 ;;; Mark Region
1786 (ert-deftest test-org/mark-subtree ()
1787 "Test `org-mark-subtree' specifications."
1788 ;; Error when point is before first headline.
1789 (should-error
1790 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
1791 (progn (transient-mark-mode 1)
1792 (org-mark-subtree))))
1793 ;; Without argument, mark current subtree.
1794 (should
1795 (equal
1796 '(12 32)
1797 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1798 (progn (transient-mark-mode 1)
1799 (forward-line 2)
1800 (org-mark-subtree)
1801 (list (region-beginning) (region-end))))))
1802 ;; With an argument, move ARG up.
1803 (should
1804 (equal
1805 '(1 32)
1806 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1807 (progn (transient-mark-mode 1)
1808 (forward-line 2)
1809 (org-mark-subtree 1)
1810 (list (region-beginning) (region-end))))))
1811 ;; Do not get fooled by inlinetasks.
1812 (when (featurep 'org-inlinetask)
1813 (should
1814 (= 1
1815 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
1816 (progn (transient-mark-mode 1)
1817 (forward-line 1)
1818 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
1819 (region-beginning)))))))
1823 ;;; Navigation
1825 (ert-deftest test-org/end-of-meta-data ()
1826 "Test `org-end-of-meta-data' specifications."
1827 ;; Skip planning line.
1828 (should
1829 (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
1830 (org-end-of-meta-data)
1831 (eobp)))
1832 ;; Skip properties drawer.
1833 (should
1834 (org-test-with-temp-text
1835 "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
1836 (org-end-of-meta-data)
1837 (eobp)))
1838 ;; Skip both.
1839 (should
1840 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
1841 (org-end-of-meta-data)
1842 (eobp)))
1843 ;; Nothing to skip, go to first line.
1844 (should
1845 (org-test-with-temp-text "* Headline\nContents"
1846 (org-end-of-meta-data)
1847 (looking-at "Contents")))
1848 ;; With option argument, skip empty lines, regular drawers and
1849 ;; clocking lines.
1850 (should
1851 (org-test-with-temp-text "* Headline\n\nContents"
1852 (org-end-of-meta-data t)
1853 (looking-at "Contents")))
1854 (should
1855 (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
1856 (org-end-of-meta-data t)
1857 (looking-at "Contents")))
1858 (should
1859 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
1860 (org-end-of-meta-data t)
1861 (looking-at "Contents")))
1862 ;; Special case: do not skip incomplete drawers.
1863 (should
1864 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
1865 (org-end-of-meta-data t)
1866 (looking-at ":LOGBOOK:")))
1867 ;; Special case: Be careful about consecutive headlines.
1868 (should-not
1869 (org-test-with-temp-text "* H1\n*H2\nContents"
1870 (org-end-of-meta-data t)
1871 (looking-at "Contents"))))
1873 (ert-deftest test-org/beginning-of-line ()
1874 "Test `org-beginning-of-line' specifications."
1875 ;; Standard test.
1876 (should
1877 (org-test-with-temp-text "Some text\nSome other text"
1878 (progn (org-beginning-of-line) (bolp))))
1879 ;; Standard test with `visual-line-mode'.
1880 (should-not
1881 (org-test-with-temp-text "A long line of text\nSome other text"
1882 (progn (visual-line-mode)
1883 (forward-char 2)
1884 (dotimes (i 1000) (insert "very "))
1885 (org-beginning-of-line)
1886 (bolp))))
1887 ;; At an headline with special movement.
1888 (should
1889 (org-test-with-temp-text "* TODO Headline"
1890 (let ((org-special-ctrl-a/e t))
1891 (org-end-of-line)
1892 (and (progn (org-beginning-of-line) (looking-at "Headline"))
1893 (progn (org-beginning-of-line) (bolp))
1894 (progn (org-beginning-of-line) (looking-at "Headline"))))))
1895 ;; Special case: Do not error when the buffer contains only a single
1896 ;; asterisk.
1897 (should
1898 (org-test-with-temp-text "*<point>"
1899 (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))))
1900 (should
1901 (org-test-with-temp-text "*<point>"
1902 (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line)))))
1904 (ert-deftest test-org/end-of-line ()
1905 "Test `org-end-of-line' specifications."
1906 ;; Standard test.
1907 (should
1908 (org-test-with-temp-text "Some text\nSome other text"
1909 (progn (org-end-of-line) (eolp))))
1910 ;; Standard test with `visual-line-mode'.
1911 (should-not
1912 (org-test-with-temp-text "A long line of text\nSome other text"
1913 (progn (visual-line-mode)
1914 (forward-char 2)
1915 (dotimes (i 1000) (insert "very "))
1916 (goto-char (point-min))
1917 (org-end-of-line)
1918 (eolp))))
1919 ;; At an headline with special movement.
1920 (should
1921 (org-test-with-temp-text "* Headline1 :tag:\n"
1922 (let ((org-special-ctrl-a/e t))
1923 (and (progn (org-end-of-line) (looking-at " :tag:"))
1924 (progn (org-end-of-line) (eolp))
1925 (progn (org-end-of-line) (looking-at " :tag:"))))))
1926 ;; At an headline without special movement.
1927 (should
1928 (org-test-with-temp-text "* Headline2 :tag:\n"
1929 (let ((org-special-ctrl-a/e nil))
1930 (and (progn (org-end-of-line) (eolp))
1931 (progn (org-end-of-line) (eolp))))))
1932 ;; At an headline, with reversed movement.
1933 (should
1934 (org-test-with-temp-text "* Headline3 :tag:\n"
1935 (let ((org-special-ctrl-a/e 'reversed)
1936 (this-command last-command))
1937 (and (progn (org-end-of-line) (eolp))
1938 (progn (org-end-of-line) (looking-at " :tag:"))))))
1939 ;; At a block without hidden contents.
1940 (should
1941 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1942 (progn (org-end-of-line) (eolp))))
1943 ;; At a block with hidden contents.
1944 (should-not
1945 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1946 (let ((org-special-ctrl-a/e t))
1947 (org-hide-block-toggle)
1948 (org-end-of-line)
1949 (eobp)))))
1951 (ert-deftest test-org/forward-sentence ()
1952 "Test `org-forward-sentence' specifications."
1953 ;; At the end of a table cell, move to the end of the next one.
1954 (should
1955 (org-test-with-temp-text "| a<point> | b |"
1956 (org-forward-sentence)
1957 (looking-at " |$")))
1958 ;; Elsewhere in a cell, move to its end.
1959 (should
1960 (org-test-with-temp-text "| a<point>c | b |"
1961 (org-forward-sentence)
1962 (looking-at " | b |$")))
1963 ;; Otherwise, simply call `forward-sentence'.
1964 (should
1965 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
1966 (org-forward-sentence)
1967 (looking-at " Sentence 2.")))
1968 (should
1969 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
1970 (org-forward-sentence)
1971 (org-forward-sentence)
1972 (eobp)))
1973 ;; At the end of an element, jump to the next one, without stopping
1974 ;; on blank lines in-between.
1975 (should
1976 (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
1977 (org-forward-sentence)
1978 (eobp))))
1980 (ert-deftest test-org/backward-sentence ()
1981 "Test `org-backward-sentence' specifications."
1982 ;; At the beginning of a table cell, move to the beginning of the
1983 ;; previous one.
1984 (should
1985 (org-test-with-temp-text "| a | <point>b |"
1986 (org-backward-sentence)
1987 (looking-at "a | b |$")))
1988 ;; Elsewhere in a cell, move to its beginning.
1989 (should
1990 (org-test-with-temp-text "| a | b<point>c |"
1991 (org-backward-sentence)
1992 (looking-at "bc |$")))
1993 ;; Otherwise, simply call `backward-sentence'.
1994 (should
1995 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
1996 (org-backward-sentence)
1997 (looking-at "Sentence 2.")))
1998 (should
1999 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
2000 (org-backward-sentence)
2001 (org-backward-sentence)
2002 (bobp)))
2003 ;; Make sure to hit the beginning of a sentence on the same line as
2004 ;; an item.
2005 (should
2006 (org-test-with-temp-text "- Line 1\n line <point>2."
2007 (org-backward-sentence)
2008 (looking-at "Line 1"))))
2010 (ert-deftest test-org/forward-paragraph ()
2011 "Test `org-forward-paragraph' specifications."
2012 ;; At end of buffer, return an error.
2013 (should-error
2014 (org-test-with-temp-text "Paragraph"
2015 (goto-char (point-max))
2016 (org-forward-paragraph)))
2017 ;; Standard test.
2018 (should
2019 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2020 (org-forward-paragraph)
2021 (looking-at "P2")))
2022 ;; Ignore depth.
2023 (should
2024 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
2025 (org-forward-paragraph)
2026 (looking-at "P1")))
2027 ;; Do not enter elements with invisible contents.
2028 (should
2029 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
2030 (org-hide-block-toggle)
2031 (org-forward-paragraph)
2032 (looking-at "P3")))
2033 ;; On an affiliated keyword, jump to the beginning of the element.
2034 (should
2035 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
2036 (org-forward-paragraph)
2037 (looking-at "Para")))
2038 ;; On an item or a footnote definition, move to the second element
2039 ;; inside, if any.
2040 (should
2041 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
2042 (org-forward-paragraph)
2043 (looking-at " Paragraph")))
2044 (should
2045 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
2046 (org-forward-paragraph)
2047 (looking-at "Paragraph")))
2048 ;; On an item, or a footnote definition, when the first line is
2049 ;; empty, move to the first item.
2050 (should
2051 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
2052 (org-forward-paragraph)
2053 (looking-at " Paragraph")))
2054 (should
2055 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
2056 (org-forward-paragraph)
2057 (looking-at "Paragraph")))
2058 ;; On a table (resp. a property drawer) do not move through table
2059 ;; rows (resp. node properties).
2060 (should
2061 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
2062 (org-forward-paragraph)
2063 (looking-at "Paragraph")))
2064 (should
2065 (org-test-with-temp-text
2066 "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
2067 (org-forward-paragraph)
2068 (looking-at "Paragraph")))
2069 ;; On a verse or source block, stop after blank lines.
2070 (should
2071 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
2072 (org-forward-paragraph)
2073 (looking-at "L2")))
2074 (should
2075 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
2076 (org-forward-paragraph)
2077 (looking-at "L2"))))
2079 (ert-deftest test-org/backward-paragraph ()
2080 "Test `org-backward-paragraph' specifications."
2081 ;; Error at beginning of buffer.
2082 (should-error
2083 (org-test-with-temp-text "Paragraph"
2084 (org-backward-paragraph)))
2085 ;; Regular test.
2086 (should
2087 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2088 (goto-char (point-max))
2089 (org-backward-paragraph)
2090 (looking-at "P3")))
2091 (should
2092 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2093 (goto-char (point-max))
2094 (beginning-of-line)
2095 (org-backward-paragraph)
2096 (looking-at "P2")))
2097 ;; Ignore depth.
2098 (should
2099 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
2100 (goto-char (point-max))
2101 (beginning-of-line)
2102 (org-backward-paragraph)
2103 (looking-at "P2")))
2104 ;; Ignore invisible elements.
2105 (should
2106 (org-test-with-temp-text "* H1\n P1\n* H2"
2107 (org-cycle)
2108 (goto-char (point-max))
2109 (beginning-of-line)
2110 (org-backward-paragraph)
2111 (bobp)))
2112 ;; On an affiliated keyword, jump to the first one.
2113 (should
2114 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
2115 (search-forward "c2")
2116 (org-backward-paragraph)
2117 (looking-at "#\\+name")))
2118 ;; On the second element in an item or a footnote definition, jump
2119 ;; to item or the definition.
2120 (should
2121 (org-test-with-temp-text "- line1\n\n line2"
2122 (goto-char (point-max))
2123 (beginning-of-line)
2124 (org-backward-paragraph)
2125 (looking-at "- line1")))
2126 (should
2127 (org-test-with-temp-text "[fn:1] line1\n\n line2"
2128 (goto-char (point-max))
2129 (beginning-of-line)
2130 (org-backward-paragraph)
2131 (looking-at "\\[fn:1\\] line1")))
2132 ;; On a table (resp. a property drawer), ignore table rows
2133 ;; (resp. node properties).
2134 (should
2135 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
2136 (goto-char (point-max))
2137 (beginning-of-line)
2138 (org-backward-paragraph)
2139 (bobp)))
2140 (should
2141 (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
2142 (org-backward-paragraph)
2143 (looking-at ":PROPERTIES:")))
2144 ;; On a source or verse block, stop before blank lines.
2145 (should
2146 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
2147 (search-forward "L3")
2148 (beginning-of-line)
2149 (org-backward-paragraph)
2150 (looking-at "L2")))
2151 (should
2152 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
2153 (search-forward "L3")
2154 (beginning-of-line)
2155 (org-backward-paragraph)
2156 (looking-at "L2"))))
2158 (ert-deftest test-org/forward-element ()
2159 "Test `org-forward-element' specifications."
2160 ;; 1. At EOB: should error.
2161 (org-test-with-temp-text "Some text\n"
2162 (goto-char (point-max))
2163 (should-error (org-forward-element)))
2164 ;; 2. Standard move: expected to ignore blank lines.
2165 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
2166 (org-forward-element)
2167 (should (looking-at (regexp-quote "Second paragraph."))))
2168 ;; 3. Headline tests.
2169 (org-test-with-temp-text "
2170 * Head 1
2171 ** Head 1.1
2172 *** Head 1.1.1
2173 ** Head 1.2"
2174 ;; 3.1. At an headline beginning: move to next headline at the
2175 ;; same level.
2176 (goto-line 3)
2177 (org-forward-element)
2178 (should (looking-at (regexp-quote "** Head 1.2")))
2179 ;; 3.2. At an headline beginning: move to parent headline if no
2180 ;; headline at the same level.
2181 (goto-line 3)
2182 (org-forward-element)
2183 (should (looking-at (regexp-quote "** Head 1.2"))))
2184 ;; 4. Greater element tests.
2185 (org-test-with-temp-text
2186 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
2187 ;; 4.1. At a greater element: expected to skip contents.
2188 (org-forward-element)
2189 (should (looking-at (regexp-quote "Outside.")))
2190 ;; 4.2. At the end of greater element contents: expected to skip
2191 ;; to the end of the greater element.
2192 (goto-line 2)
2193 (org-forward-element)
2194 (should (looking-at (regexp-quote "Outside."))))
2195 ;; 5. List tests.
2196 (org-test-with-temp-text "
2197 - item1
2199 - sub1
2201 - sub2
2203 - sub3
2205 Inner paragraph.
2207 - item2
2209 Outside."
2210 ;; 5.1. At list top point: expected to move to the element after
2211 ;; the list.
2212 (goto-line 2)
2213 (org-forward-element)
2214 (should (looking-at (regexp-quote "Outside.")))
2215 ;; 5.2. Special case: at the first line of a sub-list, but not at
2216 ;; beginning of line, move to next item.
2217 (goto-line 2)
2218 (forward-char)
2219 (org-forward-element)
2220 (should (looking-at "- item2"))
2221 (goto-line 4)
2222 (forward-char)
2223 (org-forward-element)
2224 (should (looking-at " - sub2"))
2225 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
2226 (goto-line 4)
2227 (org-forward-element)
2228 (should (looking-at (regexp-quote " Inner paragraph.")))
2229 ;; 5.4. At sub-list end: expected to move outside the sub-list.
2230 (goto-line 8)
2231 (org-forward-element)
2232 (should (looking-at (regexp-quote " Inner paragraph.")))
2233 ;; 5.5. At an item: expected to move to next item, if any.
2234 (goto-line 6)
2235 (org-forward-element)
2236 (should (looking-at " - sub3"))))
2238 (ert-deftest test-org/backward-element ()
2239 "Test `org-backward-element' specifications."
2240 ;; 1. Should error at BOB.
2241 (org-test-with-temp-text " \nParagraph."
2242 (should-error (org-backward-element)))
2243 ;; 2. Should move at BOB when called on the first element in buffer.
2244 (should
2245 (org-test-with-temp-text "\n#+TITLE: test"
2246 (progn (forward-line)
2247 (org-backward-element)
2248 (bobp))))
2249 ;; 3. Not at the beginning of an element: move at its beginning.
2250 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2251 (goto-line 3)
2252 (end-of-line)
2253 (org-backward-element)
2254 (should (looking-at (regexp-quote "Paragraph2."))))
2255 ;; 4. Headline tests.
2256 (org-test-with-temp-text "
2257 * Head 1
2258 ** Head 1.1
2259 *** Head 1.1.1
2260 ** Head 1.2"
2261 ;; 4.1. At an headline beginning: move to previous headline at the
2262 ;; same level.
2263 (goto-line 5)
2264 (org-backward-element)
2265 (should (looking-at (regexp-quote "** Head 1.1")))
2266 ;; 4.2. At an headline beginning: move to parent headline if no
2267 ;; headline at the same level.
2268 (goto-line 3)
2269 (org-backward-element)
2270 (should (looking-at (regexp-quote "* Head 1")))
2271 ;; 4.3. At the first top-level headline: should error.
2272 (goto-line 2)
2273 (should-error (org-backward-element)))
2274 ;; 5. At beginning of first element inside a greater element:
2275 ;; expected to move to greater element's beginning.
2276 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
2277 (goto-line 3)
2278 (org-backward-element)
2279 (should (looking-at "#\\+BEGIN_CENTER")))
2280 ;; 6. At the beginning of the first element in a section: should
2281 ;; move back to headline, if any.
2282 (should
2283 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
2284 (progn (goto-char (point-max))
2285 (beginning-of-line)
2286 (org-backward-element)
2287 (org-at-heading-p))))
2288 ;; 7. List tests.
2289 (org-test-with-temp-text "
2290 - item1
2292 - sub1
2294 - sub2
2296 - sub3
2298 Inner paragraph.
2300 - item2
2303 Outside."
2304 ;; 7.1. At beginning of sub-list: expected to move to the
2305 ;; paragraph before it.
2306 (goto-line 4)
2307 (org-backward-element)
2308 (should (looking-at "item1"))
2309 ;; 7.2. At an item in a list: expected to move at previous item.
2310 (goto-line 8)
2311 (org-backward-element)
2312 (should (looking-at " - sub2"))
2313 (goto-line 12)
2314 (org-backward-element)
2315 (should (looking-at "- item1"))
2316 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
2317 ;; beginning.
2318 (goto-line 10)
2319 (org-backward-element)
2320 (should (looking-at " - sub1"))
2321 (goto-line 15)
2322 (org-backward-element)
2323 (should (looking-at "- item1"))
2324 ;; 7.4. At blank-lines before list end: expected to move to top
2325 ;; item.
2326 (goto-line 14)
2327 (org-backward-element)
2328 (should (looking-at "- item1"))))
2330 (ert-deftest test-org/up-element ()
2331 "Test `org-up-element' specifications."
2332 ;; 1. At BOB or with no surrounding element: should error.
2333 (org-test-with-temp-text "Paragraph."
2334 (should-error (org-up-element)))
2335 (org-test-with-temp-text "* Head1\n* Head2"
2336 (goto-line 2)
2337 (should-error (org-up-element)))
2338 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2339 (goto-line 3)
2340 (should-error (org-up-element)))
2341 ;; 2. At an headline: move to parent headline.
2342 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
2343 (goto-line 3)
2344 (org-up-element)
2345 (should (looking-at "\\* Head1")))
2346 ;; 3. Inside a greater element: move to greater element beginning.
2347 (org-test-with-temp-text
2348 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
2349 (goto-line 3)
2350 (org-up-element)
2351 (should (looking-at "#\\+BEGIN_CENTER")))
2352 ;; 4. List tests.
2353 (org-test-with-temp-text "* Top
2354 - item1
2356 - sub1
2358 - sub2
2360 Paragraph within sub2.
2362 - item2"
2363 ;; 4.1. Within an item: move to the item beginning.
2364 (goto-line 8)
2365 (org-up-element)
2366 (should (looking-at " - sub2"))
2367 ;; 4.2. At an item in a sub-list: move to parent item.
2368 (goto-line 4)
2369 (org-up-element)
2370 (should (looking-at "- item1"))
2371 ;; 4.3. At an item in top list: move to beginning of whole list.
2372 (goto-line 10)
2373 (org-up-element)
2374 (should (looking-at "- item1"))
2375 ;; 4.4. Special case. At very top point: should move to parent of
2376 ;; list.
2377 (goto-line 2)
2378 (org-up-element)
2379 (should (looking-at "\\* Top"))))
2381 (ert-deftest test-org/down-element ()
2382 "Test `org-down-element' specifications."
2383 ;; Error when the element hasn't got a recursive type.
2384 (org-test-with-temp-text "Paragraph."
2385 (should-error (org-down-element)))
2386 ;; Error when the element has no contents
2387 (org-test-with-temp-text "* Headline"
2388 (should-error (org-down-element)))
2389 ;; When at a plain-list, move to first item.
2390 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
2391 (goto-line 2)
2392 (org-down-element)
2393 (should (looking-at " - Item 1.1")))
2394 (org-test-with-temp-text "#+NAME: list\n- Item 1"
2395 (org-down-element)
2396 (should (looking-at " Item 1")))
2397 ;; When at a table, move to first row
2398 (org-test-with-temp-text "#+NAME: table\n| a | b |"
2399 (org-down-element)
2400 (should (looking-at " a | b |")))
2401 ;; Otherwise, move inside the greater element.
2402 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
2403 (org-down-element)
2404 (should (looking-at "Paragraph"))))
2406 (ert-deftest test-org/drag-element-backward ()
2407 "Test `org-drag-element-backward' specifications."
2408 ;; Standard test.
2409 (should
2410 (equal
2411 "#+key2: val2\n#+key1: val1\n#+key3: val3"
2412 (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
2413 (org-drag-element-backward)
2414 (buffer-string))))
2415 (should
2416 (equal
2417 "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
2418 (org-test-with-temp-text
2419 "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
2420 (org-drag-element-backward)
2421 (buffer-string))))
2422 ;; Preserve blank lines.
2423 (should
2424 (equal "Paragraph 2\n\n\nPara1\n\nPara3"
2425 (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
2426 (org-drag-element-backward)
2427 (buffer-string))))
2428 ;; Preserve column.
2429 (should
2430 (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
2431 (org-drag-element-backward)
2432 (org-looking-at-p "2")))
2433 ;; Error when trying to move first element of buffer.
2434 (should-error
2435 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2436 (org-drag-element-backward)))
2437 ;; Error when trying to swap nested elements.
2438 (should-error
2439 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2440 (forward-line)
2441 (org-drag-element-backward)))
2442 ;; Error when trying to swap an headline element and a non-headline
2443 ;; element.
2444 (should-error
2445 (org-test-with-temp-text "Test.\n* Head 1"
2446 (forward-line)
2447 (org-drag-element-backward)))
2448 ;; Preserve visibility of elements and their contents.
2449 (should
2450 (equal '((63 . 82) (26 . 48))
2451 (org-test-with-temp-text "
2452 #+BEGIN_CENTER
2453 Text.
2454 #+END_CENTER
2455 - item 1
2456 #+BEGIN_QUOTE
2457 Text.
2458 #+END_QUOTE"
2459 (while (search-forward "BEGIN_" nil t) (org-cycle))
2460 (search-backward "- item 1")
2461 (org-drag-element-backward)
2462 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2463 (overlays-in (point-min) (point-max)))))))
2465 (ert-deftest test-org/drag-element-forward ()
2466 "Test `org-drag-element-forward' specifications."
2467 ;; 1. Error when trying to move first element of buffer.
2468 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2469 (goto-line 3)
2470 (should-error (org-drag-element-forward)))
2471 ;; 2. Error when trying to swap nested elements.
2472 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2473 (forward-line)
2474 (should-error (org-drag-element-forward)))
2475 ;; 3. Error when trying to swap a non-headline element and an
2476 ;; headline.
2477 (org-test-with-temp-text "Test.\n* Head 1"
2478 (should-error (org-drag-element-forward)))
2479 ;; 4. Otherwise, swap elements, preserving column and blank lines
2480 ;; between elements.
2481 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
2482 (search-forward "graph")
2483 (org-drag-element-forward)
2484 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
2485 (should (looking-at " 1")))
2486 ;; 5. Preserve visibility of elements and their contents.
2487 (org-test-with-temp-text "
2488 #+BEGIN_CENTER
2489 Text.
2490 #+END_CENTER
2491 - item 1
2492 #+BEGIN_QUOTE
2493 Text.
2494 #+END_QUOTE"
2495 (while (search-forward "BEGIN_" nil t) (org-cycle))
2496 (search-backward "#+BEGIN_CENTER")
2497 (org-drag-element-forward)
2498 (should
2499 (equal
2500 '((63 . 82) (26 . 48))
2501 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2502 (overlays-in (point-min) (point-max)))))))
2504 (ert-deftest test-org/next-block ()
2505 "Test `org-next-block' specifications."
2506 ;; Regular test.
2507 (should
2508 (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
2509 (org-next-block 1)
2510 (looking-at "#\\+BEGIN_CENTER")))
2511 ;; Ignore case.
2512 (should
2513 (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
2514 (let ((case-fold-search nil))
2515 (org-next-block 1)
2516 (looking-at "#\\+begin_center"))))
2517 ;; Ignore current line.
2518 (should
2519 (org-test-with-temp-text
2520 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
2521 (org-next-block 1)
2522 (looking-at "#\\+BEGIN_CENTER")))
2523 ;; Throw an error when no block is found.
2524 (should-error
2525 (org-test-with-temp-text "Paragraph"
2526 (org-next-block 1)))
2527 ;; With an argument, skip many blocks at once.
2528 (should
2529 (org-test-with-temp-text
2530 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
2531 (org-next-block 2)
2532 (looking-at "#\\+BEGIN_QUOTE")))
2533 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
2534 (should
2535 (org-test-with-temp-text
2536 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
2537 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
2538 (looking-at "#\\+BEGIN_QUOTE")))
2539 ;; Optional argument is also case-insensitive.
2540 (should
2541 (org-test-with-temp-text
2542 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
2543 (let ((case-fold-search nil))
2544 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
2545 (looking-at "#\\+begin_quote")))))
2547 (ert-deftest test-org/previous-block ()
2548 "Test `org-previous-block' specifications."
2549 ;; Regular test.
2550 (should
2551 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
2552 (org-previous-block 1)
2553 (looking-at "#\\+BEGIN_CENTER")))
2554 ;; Ignore case.
2555 (should
2556 (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
2557 (let ((case-fold-search nil))
2558 (org-previous-block 1)
2559 (looking-at "#\\+begin_center"))))
2560 ;; Ignore current line.
2561 (should
2562 (org-test-with-temp-text
2563 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
2564 (org-previous-block 1)
2565 (looking-at "#\\+BEGIN_QUOTE")))
2566 ;; Throw an error when no block is found.
2567 (should-error
2568 (org-test-with-temp-text "Paragraph<point>"
2569 (org-previous-block 1)))
2570 ;; With an argument, skip many blocks at once.
2571 (should
2572 (org-test-with-temp-text
2573 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
2574 (org-previous-block 2)
2575 (looking-at "#\\+BEGIN_CENTER")))
2576 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
2577 (should
2578 (org-test-with-temp-text
2579 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
2580 (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
2581 (looking-at "#\\+BEGIN_QUOTE")))
2582 ;; Optional argument is also case-insensitive.
2583 (should
2584 (org-test-with-temp-text
2585 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
2586 (let ((case-fold-search nil))
2587 (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
2588 (looking-at "#\\+begin_quote")))))
2591 ;;; Outline structure
2593 (ert-deftest test-org/demote ()
2594 "Test `org-demote' specifications."
2595 ;; Add correct number of stars according to `org-odd-levels-only'.
2596 (should
2597 (= 2
2598 (org-test-with-temp-text "* H"
2599 (let ((org-odd-levels-only nil)) (org-demote))
2600 (org-current-level))))
2601 (should
2602 (= 3
2603 (org-test-with-temp-text "* H"
2604 (let ((org-odd-levels-only t)) (org-demote))
2605 (org-current-level))))
2606 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2607 (should
2608 (org-test-with-temp-text "* H :tag:"
2609 (let ((org-tags-column 10)
2610 (org-auto-align-tags t)
2611 (org-odd-levels-only nil))
2612 (org-demote))
2613 (org-move-to-column 10)
2614 (org-looking-at-p ":tag:$")))
2615 (should-not
2616 (org-test-with-temp-text "* H :tag:"
2617 (let ((org-tags-column 10)
2618 (org-auto-align-tags nil)
2619 (org-odd-levels-only nil))
2620 (org-demote))
2621 (org-move-to-column 10)
2622 (org-looking-at-p ":tag:$")))
2623 ;; When `org-adapt-indentation' is non-nil, always indent planning
2624 ;; info and property drawers accordingly.
2625 (should
2626 (= 3
2627 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2628 (let ((org-odd-levels-only nil)
2629 (org-adapt-indentation t))
2630 (org-demote))
2631 (forward-line)
2632 (org-get-indentation))))
2633 (should
2634 (= 3
2635 (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2636 (let ((org-odd-levels-only nil)
2637 (org-adapt-indentation t))
2638 (org-demote))
2639 (forward-line)
2640 (org-get-indentation))))
2641 (should-not
2642 (= 3
2643 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2644 (let ((org-odd-levels-only nil)
2645 (org-adapt-indentation nil))
2646 (org-demote))
2647 (forward-line)
2648 (org-get-indentation))))
2649 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2650 ;; section accordingly. Ignore, however, footnote definitions and
2651 ;; inlinetasks boundaries.
2652 (should
2653 (= 3
2654 (org-test-with-temp-text "* H\n Paragraph"
2655 (let ((org-odd-levels-only nil)
2656 (org-adapt-indentation t))
2657 (org-demote))
2658 (forward-line)
2659 (org-get-indentation))))
2660 (should
2661 (= 2
2662 (org-test-with-temp-text "* H\n Paragraph"
2663 (let ((org-odd-levels-only nil)
2664 (org-adapt-indentation nil))
2665 (org-demote))
2666 (forward-line)
2667 (org-get-indentation))))
2668 (should
2669 (zerop
2670 (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
2671 (let ((org-odd-levels-only nil)
2672 (org-adapt-indentation t))
2673 (org-demote))
2674 (goto-char (point-max))
2675 (org-get-indentation))))
2676 (should
2677 (= 3
2678 (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
2679 (let ((org-odd-levels-only nil)
2680 (org-adapt-indentation t))
2681 (org-demote))
2682 (goto-char (point-max))
2683 (org-get-indentation))))
2684 (when (featurep 'org-inlinetask)
2685 (should
2686 (zerop
2687 (let ((org-inlinetask-min-level 5)
2688 (org-adapt-indentation t))
2689 (org-test-with-temp-text "* H\n***** I\n***** END"
2690 (org-demote)
2691 (forward-line)
2692 (org-get-indentation))))))
2693 (when (featurep 'org-inlinetask)
2694 (should
2695 (= 3
2696 (let ((org-inlinetask-min-level 5)
2697 (org-adapt-indentation t))
2698 (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
2699 (org-demote)
2700 (forward-line 2)
2701 (org-get-indentation))))))
2702 ;; Ignore contents of source blocks or example blocks when
2703 ;; indentation should be preserved (through
2704 ;; `org-src-preserve-indentation' or "-i" flag).
2705 (should-not
2706 (zerop
2707 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2708 (let ((org-adapt-indentation t)
2709 (org-src-preserve-indentation nil))
2710 (org-demote))
2711 (forward-line 2)
2712 (org-get-indentation))))
2713 (should
2714 (zerop
2715 (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2716 (let ((org-adapt-indentation t)
2717 (org-src-preserve-indentation t))
2718 (org-demote))
2719 (forward-line 2)
2720 (org-get-indentation))))
2721 (should
2722 (zerop
2723 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2724 (let ((org-adapt-indentation t)
2725 (org-src-preserve-indentation t))
2726 (org-demote))
2727 (forward-line 2)
2728 (org-get-indentation))))
2729 (should
2730 (zerop
2731 (org-test-with-temp-text
2732 "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
2733 (let ((org-adapt-indentation t)
2734 (org-src-preserve-indentation nil))
2735 (org-demote))
2736 (forward-line 2)
2737 (org-get-indentation)))))
2739 (ert-deftest test-org/promote ()
2740 "Test `org-promote' specifications."
2741 ;; Return an error if headline is to be promoted to level 0, unless
2742 ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
2743 ;; headline becomes a comment.
2744 (should-error
2745 (org-test-with-temp-text "* H"
2746 (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
2747 (should
2748 (equal "# H"
2749 (org-test-with-temp-text "* H"
2750 (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
2751 (buffer-string))))
2752 ;; Remove correct number of stars according to
2753 ;; `org-odd-levels-only'.
2754 (should
2755 (= 2
2756 (org-test-with-temp-text "*** H"
2757 (let ((org-odd-levels-only nil)) (org-promote))
2758 (org-current-level))))
2759 (should
2760 (= 1
2761 (org-test-with-temp-text "*** H"
2762 (let ((org-odd-levels-only t)) (org-promote))
2763 (org-current-level))))
2764 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2765 (should
2766 (org-test-with-temp-text "** H :tag:"
2767 (let ((org-tags-column 10)
2768 (org-auto-align-tags t)
2769 (org-odd-levels-only nil))
2770 (org-promote))
2771 (org-move-to-column 10)
2772 (org-looking-at-p ":tag:$")))
2773 (should-not
2774 (org-test-with-temp-text "** H :tag:"
2775 (let ((org-tags-column 10)
2776 (org-auto-align-tags nil)
2777 (org-odd-levels-only nil))
2778 (org-promote))
2779 (org-move-to-column 10)
2780 (org-looking-at-p ":tag:$")))
2781 ;; When `org-adapt-indentation' is non-nil, always indent planning
2782 ;; info and property drawers.
2783 (should
2784 (= 2
2785 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2786 (let ((org-odd-levels-only nil)
2787 (org-adapt-indentation t))
2788 (org-promote))
2789 (forward-line)
2790 (org-get-indentation))))
2791 (should
2792 (= 2
2793 (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2794 (let ((org-odd-levels-only nil)
2795 (org-adapt-indentation t))
2796 (org-promote))
2797 (forward-line)
2798 (org-get-indentation))))
2799 (should-not
2800 (= 2
2801 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2802 (let ((org-odd-levels-only nil)
2803 (org-adapt-indentation nil))
2804 (org-promote))
2805 (forward-line)
2806 (org-get-indentation))))
2807 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2808 ;; section accordingly. Ignore, however, footnote definitions and
2809 ;; inlinetasks boundaries.
2810 (should
2811 (= 2
2812 (org-test-with-temp-text "** H\n Paragraph"
2813 (let ((org-odd-levels-only nil)
2814 (org-adapt-indentation t))
2815 (org-promote))
2816 (forward-line)
2817 (org-get-indentation))))
2818 (should-not
2819 (= 2
2820 (org-test-with-temp-text "** H\n Paragraph"
2821 (let ((org-odd-levels-only nil)
2822 (org-adapt-indentation nil))
2823 (org-promote))
2824 (forward-line)
2825 (org-get-indentation))))
2826 (should
2827 (= 2
2828 (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
2829 (let ((org-odd-levels-only nil)
2830 (org-adapt-indentation t))
2831 (org-promote))
2832 (forward-line)
2833 (org-get-indentation))))
2834 (when (featurep 'org-inlinetask)
2835 (should
2836 (zerop
2837 (let ((org-inlinetask-min-level 5)
2838 (org-adapt-indentation t))
2839 (org-test-with-temp-text "** H\n***** I\n***** END"
2840 (org-promote)
2841 (forward-line)
2842 (org-get-indentation))))))
2843 (when (featurep 'org-inlinetask)
2844 (should
2845 (= 2
2846 (let ((org-inlinetask-min-level 5)
2847 (org-adapt-indentation t))
2848 (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
2849 (org-promote)
2850 (forward-line 2)
2851 (org-get-indentation))))))
2852 ;; Give up shifting if it would break document's structure
2853 ;; otherwise.
2854 (should
2855 (= 3
2856 (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
2857 (let ((org-odd-levels-only nil)
2858 (org-adapt-indentation t))
2859 (org-promote))
2860 (forward-line)
2861 (org-get-indentation))))
2862 (should
2863 (= 3
2864 (org-test-with-temp-text "** H\n Paragraph\n * list."
2865 (let ((org-odd-levels-only nil)
2866 (org-adapt-indentation t))
2867 (org-promote))
2868 (forward-line)
2869 (org-get-indentation))))
2870 ;; Ignore contents of source blocks or example blocks when
2871 ;; indentation should be preserved (through
2872 ;; `org-src-preserve-indentation' or "-i" flag).
2873 (should-not
2874 (zerop
2875 (org-test-with-temp-text
2876 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2877 (let ((org-adapt-indentation t)
2878 (org-src-preserve-indentation nil)
2879 (org-odd-levels-only nil))
2880 (org-promote))
2881 (forward-line)
2882 (org-get-indentation))))
2883 (should
2884 (zerop
2885 (org-test-with-temp-text
2886 "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
2887 (let ((org-adapt-indentation t)
2888 (org-src-preserve-indentation t)
2889 (org-odd-levels-only nil))
2890 (org-promote))
2891 (forward-line)
2892 (org-get-indentation))))
2893 (should
2894 (zerop
2895 (org-test-with-temp-text
2896 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2897 (let ((org-adapt-indentation t)
2898 (org-src-preserve-indentation t)
2899 (org-odd-levels-only nil))
2900 (org-promote))
2901 (forward-line)
2902 (org-get-indentation))))
2903 (should
2904 (zerop
2905 (org-test-with-temp-text
2906 "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
2907 (let ((org-adapt-indentation t)
2908 (org-src-preserve-indentation nil)
2909 (org-odd-levels-only nil))
2910 (org-promote))
2911 (forward-line)
2912 (org-get-indentation)))))
2915 ;;; Planning
2917 (ert-deftest test-org/at-planning-p ()
2918 "Test `org-at-planning-p' specifications."
2919 ;; Regular test.
2920 (should
2921 (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
2922 (org-at-planning-p)))
2923 (should-not
2924 (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
2925 (org-at-planning-p)))
2926 ;; Correctly find planning attached to inlinetasks.
2927 (when (featurep 'org-inlinetask)
2928 (should
2929 (org-test-with-temp-text
2930 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
2931 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2932 (should-not
2933 (org-test-with-temp-text
2934 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
2935 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2936 (should-not
2937 (org-test-with-temp-text
2938 "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
2939 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2940 (should-not
2941 (org-test-with-temp-text
2942 "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
2943 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
2945 (ert-deftest test-org/add-planning-info ()
2946 "Test `org-add-planning-info'."
2947 ;; Create deadline when `org-adapt-indentation' is non-nil.
2948 (should
2949 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
2950 (org-test-with-temp-text "* H\nParagraph<point>"
2951 (let ((org-adapt-indentation t))
2952 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
2953 (replace-regexp-in-string
2954 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
2955 nil nil 1))))
2956 ;; Create deadline when `org-adapt-indentation' is nil.
2957 (should
2958 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
2959 (org-test-with-temp-text "* H\nParagraph<point>"
2960 (let ((org-adapt-indentation nil))
2961 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
2962 (replace-regexp-in-string
2963 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
2964 nil nil 1))))
2965 ;; Update deadline when `org-adapt-indentation' is non-nil.
2966 (should
2967 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
2968 (org-test-with-temp-text "\
2970 DEADLINE: <2015-06-24 Wed>
2971 Paragraph<point>"
2972 (let ((org-adapt-indentation t))
2973 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
2974 (replace-regexp-in-string
2975 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
2976 nil nil 1))))
2977 ;; Update deadline when `org-adapt-indentation' is nil.
2978 (should
2979 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
2980 (org-test-with-temp-text "\
2982 DEADLINE: <2015-06-24 Wed>
2983 Paragraph<point>"
2984 (let ((org-adapt-indentation nil))
2985 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
2986 (replace-regexp-in-string
2987 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
2988 nil nil 1))))
2989 ;; Schedule when `org-adapt-indentation' is non-nil.
2990 (should
2991 (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
2992 (org-test-with-temp-text "* H\nParagraph<point>"
2993 (let ((org-adapt-indentation t))
2994 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
2995 (replace-regexp-in-string
2996 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
2997 nil nil 1))))
2998 ;; Schedule when `org-adapt-indentation' is nil.
2999 (should
3000 (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
3001 (org-test-with-temp-text "* H\nParagraph<point>"
3002 (let ((org-adapt-indentation nil))
3003 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
3004 (replace-regexp-in-string
3005 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3006 nil nil 1))))
3007 ;; Add deadline when scheduled.
3008 (should
3009 (equal "\
3011 DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
3012 Paragraph"
3013 (org-test-with-temp-text "\
3015 SCHEDULED: <2015-06-24 Wed>
3016 Paragraph<point>"
3017 (let ((org-adapt-indentation t))
3018 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3019 (replace-regexp-in-string
3020 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3021 nil nil 1))))
3022 ;; Remove middle entry.
3023 (should
3024 (equal "\
3026 CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
3027 Paragraph"
3028 (org-test-with-temp-text "\
3030 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
3031 Paragraph<point>"
3032 (let ((org-adapt-indentation t))
3033 (org-add-planning-info nil nil 'deadline))
3034 (replace-regexp-in-string
3035 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
3036 nil nil 1))))
3037 ;; Remove last entry and then middle entry (order should not
3038 ;; matter).
3039 (should
3040 (equal "\
3042 CLOSED: [2015-06-24]
3043 Paragraph"
3044 (org-test-with-temp-text "\
3046 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
3047 Paragraph<point>"
3048 (let ((org-adapt-indentation t))
3049 (org-add-planning-info nil nil 'scheduled 'deadline))
3050 (replace-regexp-in-string
3051 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
3052 nil nil 1))))
3053 ;; Remove closed when `org-adapt-indentation' is non-nil.
3054 (should
3055 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3056 (org-test-with-temp-text "\
3058 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
3059 Paragraph<point>"
3060 (let ((org-adapt-indentation t))
3061 (org-add-planning-info nil nil 'closed))
3062 (replace-regexp-in-string
3063 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3064 nil nil 1))))
3065 ;; Remove closed when `org-adapt-indentation' is nil.
3066 (should
3067 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3068 (org-test-with-temp-text "\
3070 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
3071 Paragraph<point>"
3072 (let ((org-adapt-indentation nil))
3073 (org-add-planning-info nil nil 'closed))
3074 (replace-regexp-in-string
3075 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3076 nil nil 1))))
3077 ;; Remove closed entry and delete empty line.
3078 (should
3079 (equal "\
3081 Paragraph"
3082 (org-test-with-temp-text "\
3084 CLOSED: [2015-06-24 Wed]
3085 Paragraph<point>"
3086 (let ((org-adapt-indentation t))
3087 (org-add-planning-info nil nil 'closed))
3088 (replace-regexp-in-string
3089 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3090 nil nil 1))))
3091 ;; Remove one entry and update another.
3092 (should
3093 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3094 (org-test-with-temp-text "\
3096 SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
3097 Paragraph<point>"
3098 (let ((org-adapt-indentation t))
3099 (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
3100 (replace-regexp-in-string
3101 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3102 nil nil 1)))))
3105 ;;; Property API
3107 (ert-deftest test-org/buffer-property-keys ()
3108 "Test `org-buffer-property-keys' specifications."
3109 ;; Retrieve properties accross siblings.
3110 (should
3111 (equal '("A" "B")
3112 (org-test-with-temp-text "
3113 * H1
3114 :PROPERTIES:
3115 :A: 1
3116 :END:
3117 * H2
3118 :PROPERTIES:
3119 :B: 1
3120 :END:"
3121 (org-buffer-property-keys))))
3122 ;; Retrieve properties accross children.
3123 (should
3124 (equal '("A" "B")
3125 (org-test-with-temp-text "
3126 * H1
3127 :PROPERTIES:
3128 :A: 1
3129 :END:
3130 ** H2
3131 :PROPERTIES:
3132 :B: 1
3133 :END:"
3134 (org-buffer-property-keys))))
3135 ;; Retrieve muliple properties in the same drawer.
3136 (should
3137 (equal '("A" "B")
3138 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3139 (org-buffer-property-keys))))
3140 ;; Ignore extension symbol in property name.
3141 (should
3142 (equal '("A")
3143 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
3144 (org-buffer-property-keys))))
3145 ;; With non-nil COLUMNS, extract property names from columns.
3146 (should
3147 (equal '("A" "B")
3148 (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
3149 (org-buffer-property-keys nil nil t))))
3150 (should
3151 (equal '("A" "B" "COLUMNS")
3152 (org-test-with-temp-text
3153 "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
3154 (org-buffer-property-keys nil nil t)))))
3156 (ert-deftest test-org/property-values ()
3157 "Test `org-property-values' specifications."
3158 ;; Regular test.
3159 (should
3160 (equal '("2" "1")
3161 (org-test-with-temp-text
3162 "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
3163 (org-property-values "A"))))
3164 ;; Ignore empty values.
3165 (should-not
3166 (org-test-with-temp-text
3167 "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
3168 (org-property-values "A")))
3169 ;; Take into consideration extended values.
3170 (should
3171 (equal '("1 2")
3172 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
3173 (org-property-values "A")))))
3175 (ert-deftest test-org/find-property ()
3176 "Test `org-find-property' specifications."
3177 ;; Regular test.
3178 (should
3179 (= 1
3180 (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
3181 (org-find-property "prop"))))
3182 ;; Ignore false positives.
3183 (should
3184 (= 27
3185 (org-test-with-temp-text
3186 "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
3187 (org-find-property "A"))))
3188 ;; Return first entry found in buffer.
3189 (should
3190 (= 1
3191 (org-test-with-temp-text
3192 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
3193 (org-find-property "A"))))
3194 ;; Only search visible part of the buffer.
3195 (should
3196 (= 31
3197 (org-test-with-temp-text
3198 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
3199 (org-narrow-to-subtree)
3200 (org-find-property "A"))))
3201 ;; With optional argument, only find entries with a specific value.
3202 (should-not
3203 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3204 (org-find-property "A" "2")))
3205 (should
3206 (= 31
3207 (org-test-with-temp-text
3208 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
3209 (org-find-property "A" "2"))))
3210 ;; Use "nil" for explicit nil values.
3211 (should
3212 (= 31
3213 (org-test-with-temp-text
3214 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
3215 (org-find-property "A" "nil")))))
3217 (ert-deftest test-org/entry-delete ()
3218 "Test `org-entry-delete' specifications."
3219 ;; Regular test.
3220 (should
3221 (string-match
3222 " *:PROPERTIES:\n *:B: +2\n *:END:"
3223 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3224 (org-entry-delete (point) "A")
3225 (buffer-string))))
3226 ;; Also remove accumulated properties.
3227 (should-not
3228 (string-match
3229 ":A"
3230 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
3231 (org-entry-delete (point) "A")
3232 (buffer-string))))
3233 ;; When last property is removed, remove the property drawer.
3234 (should-not
3235 (string-match
3236 ":PROPERTIES:"
3237 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
3238 (org-entry-delete (point) "A")
3239 (buffer-string))))
3240 ;; Return a non-nil value when some property was removed.
3241 (should
3242 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3243 (org-entry-delete (point) "A")))
3244 (should-not
3245 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3246 (org-entry-delete (point) "C"))))
3248 (ert-deftest test-org/entry-get ()
3249 "Test `org-entry-get' specifications."
3250 ;; Regular test.
3251 (should
3252 (equal "1"
3253 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3254 (org-entry-get (point) "A"))))
3255 ;; Ignore case.
3256 (should
3257 (equal "1"
3258 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3259 (org-entry-get (point) "a"))))
3260 ;; Handle extended values, both before and after base value.
3261 (should
3262 (equal "1 2 3"
3263 (org-test-with-temp-text
3264 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
3265 (org-entry-get (point) "A"))))
3266 ;; Empty values are returned as the empty string.
3267 (should
3268 (equal ""
3269 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
3270 (org-entry-get (point) "A"))))
3271 ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
3272 ;; otherwise, return nil.
3273 (should-not
3274 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
3275 (org-entry-get (point) "A")))
3276 (should
3277 (equal "nil"
3278 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
3279 (org-entry-get (point) "A" nil t))))
3280 ;; Return nil when no property is found, independently on the
3281 ;; LITERAL-NIL argument.
3282 (should-not
3283 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3284 (org-entry-get (point) "B")))
3285 (should-not
3286 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3287 (org-entry-get (point) "B" nil t)))
3288 ;; Handle inheritance, when allowed. Include extended values and
3289 ;; possibly global values.
3290 (should
3291 (equal
3293 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3294 (org-entry-get (point) "A" t))))
3295 (should
3296 (equal
3298 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3299 (let ((org-use-property-inheritance t))
3300 (org-entry-get (point) "A" 'selective)))))
3301 (should-not
3302 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3303 (let ((org-use-property-inheritance nil))
3304 (org-entry-get (point) "A" 'selective))))
3305 (should
3306 (equal
3307 "1 2"
3308 (org-test-with-temp-text
3309 "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
3310 (org-entry-get (point-max) "A" t))))
3311 (should
3312 (equal "1"
3313 (org-test-with-temp-text
3314 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
3315 (org-mode-restart)
3316 (org-entry-get (point-max) "A" t))))
3317 (should
3318 (equal "0 1"
3319 (org-test-with-temp-text
3320 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
3321 (org-mode-restart)
3322 (org-entry-get (point-max) "A" t)))))
3324 (ert-deftest test-org/entry-properties ()
3325 "Test `org-entry-properties' specifications."
3326 ;; Get "ITEM" property.
3327 (should
3328 (equal "* H"
3329 (org-test-with-temp-text "* TODO H"
3330 (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
3331 (should
3332 (equal "* H"
3333 (org-test-with-temp-text "* TODO H"
3334 (cdr (assoc "ITEM" (org-entry-properties))))))
3335 ;; Get "TODO" property. TODO keywords are case sensitive.
3336 (should
3337 (equal "TODO"
3338 (org-test-with-temp-text "* TODO H"
3339 (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
3340 (should
3341 (equal "TODO"
3342 (org-test-with-temp-text "* TODO H"
3343 (cdr (assoc "TODO" (org-entry-properties))))))
3344 (should-not
3345 (org-test-with-temp-text "* H"
3346 (assoc "TODO" (org-entry-properties nil "TODO"))))
3347 (should-not
3348 (org-test-with-temp-text "* todo H"
3349 (assoc "TODO" (org-entry-properties nil "TODO"))))
3350 ;; Get "PRIORITY" property.
3351 (should
3352 (equal "A"
3353 (org-test-with-temp-text "* [#A] H"
3354 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
3355 (should
3356 (equal "A"
3357 (org-test-with-temp-text "* [#A] H"
3358 (cdr (assoc "PRIORITY" (org-entry-properties))))))
3359 (should
3360 (equal (char-to-string org-default-priority)
3361 (org-test-with-temp-text "* H"
3362 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
3363 ;; Get "FILE" property.
3364 (should
3365 (org-test-with-temp-text-in-file "* H\nParagraph"
3366 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
3367 (buffer-file-name))))
3368 (should
3369 (org-test-with-temp-text-in-file "* H\nParagraph"
3370 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
3371 (buffer-file-name))))
3372 (should-not
3373 (org-test-with-temp-text "* H\nParagraph"
3374 (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
3375 ;; Get "TAGS" property.
3376 (should
3377 (equal ":tag1:tag2:"
3378 (org-test-with-temp-text "* H :tag1:tag2:"
3379 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
3380 (should
3381 (equal ":tag1:tag2:"
3382 (org-test-with-temp-text "* H :tag1:tag2:"
3383 (cdr (assoc "TAGS" (org-entry-properties))))))
3384 (should-not
3385 (org-test-with-temp-text "* H"
3386 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
3387 ;; Get "ALLTAGS" property.
3388 (should
3389 (equal ":tag1:tag2:"
3390 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
3391 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
3392 (should
3393 (equal ":tag1:tag2:"
3394 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
3395 (cdr (assoc "ALLTAGS" (org-entry-properties))))))
3396 (should-not
3397 (org-test-with-temp-text "* H"
3398 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
3399 ;; Get "BLOCKED" property.
3400 (should
3401 (equal "t"
3402 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
3403 (let ((org-enforce-todo-dependencies t)
3404 (org-blocker-hook
3405 '(org-block-todo-from-children-or-siblings-or-parent)))
3406 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
3407 (should
3408 (equal ""
3409 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
3410 (let ((org-enforce-todo-dependencies t)
3411 (org-blocker-hook
3412 '(org-block-todo-from-children-or-siblings-or-parent)))
3413 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
3414 ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
3415 (should
3416 (equal
3417 "[2012-03-29 thu.]"
3418 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
3419 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
3420 (should
3421 (equal
3422 "[2012-03-29 thu.]"
3423 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
3424 (cdr (assoc "CLOSED" (org-entry-properties))))))
3425 (should-not
3426 (org-test-with-temp-text "* H"
3427 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
3428 (should
3429 (equal
3430 "<2014-03-04 tue.>"
3431 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3432 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
3433 (should
3434 (equal
3435 "<2014-03-04 tue.>"
3436 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3437 (cdr (assoc "DEADLINE" (org-entry-properties))))))
3438 (should-not
3439 (org-test-with-temp-text "* H"
3440 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
3441 (should
3442 (equal
3443 "<2014-03-04 tue.>"
3444 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3445 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
3446 (should
3447 (equal
3448 "<2014-03-04 tue.>"
3449 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3450 (cdr (assoc "SCHEDULED" (org-entry-properties))))))
3451 (should-not
3452 (org-test-with-temp-text "* H"
3453 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
3454 ;; Get "CATEGORY"
3455 (should
3456 (equal "cat"
3457 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
3458 (cdr (assoc "CATEGORY" (org-entry-properties))))))
3459 (should
3460 (equal "cat"
3461 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
3462 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3463 (should
3464 (equal "cat"
3465 (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
3466 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3467 (should
3468 (equal "cat2"
3469 (org-test-with-temp-text
3470 (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
3471 "\n"
3472 "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
3473 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3474 ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
3475 (should
3476 (equal "<2012-03-29 thu.>"
3477 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
3478 (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
3479 (should
3480 (equal "[2012-03-29 thu.]"
3481 (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
3482 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
3483 (should
3484 (equal "<2012-03-29 thu.>"
3485 (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
3486 (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
3487 (should
3488 (equal "[2014-03-04 tue.]"
3489 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
3490 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties nil "TIMESTAMP_IA"))))))
3491 ;; Get standard properties.
3492 (should
3493 (equal "1"
3494 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3495 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3496 ;; Handle extended properties.
3497 (should
3498 (equal "1 2 3"
3499 (org-test-with-temp-text
3500 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
3501 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3502 (should
3503 (equal "1 2 3"
3504 (org-test-with-temp-text
3505 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
3506 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3507 ;; Ignore forbidden (special) properties.
3508 (should-not
3509 (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
3510 (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
3512 (ert-deftest test-org/entry-put ()
3513 "Test `org-entry-put' specifications."
3514 ;; Error when not a string or nil.
3515 (should-error
3516 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
3517 (org-entry-put 1 "test" 2)))
3518 ;; Set "TODO" property.
3519 (should
3520 (string-match (regexp-quote " TODO H")
3521 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
3522 (org-entry-put (point) "TODO" "TODO")
3523 (buffer-string))))
3524 (should
3525 (string-match (regexp-quote "* H")
3526 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
3527 (org-entry-put (point) "TODO" nil)
3528 (buffer-string))))
3529 ;; Set "PRIORITY" property.
3530 (should
3531 (equal "* [#A] H"
3532 (org-test-with-temp-text "* [#B] H"
3533 (org-entry-put (point) "PRIORITY" "A")
3534 (buffer-string))))
3535 (should
3536 (equal "* H"
3537 (org-test-with-temp-text "* [#B] H"
3538 (org-entry-put (point) "PRIORITY" nil)
3539 (buffer-string))))
3540 ;; Set "SCHEDULED" property.
3541 (should
3542 (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
3543 (org-test-with-temp-text "* H"
3544 (org-entry-put (point) "SCHEDULED" "2014-03-04")
3545 (buffer-string))))
3546 (should
3547 (string= "* H\n"
3548 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3549 (org-entry-put (point) "SCHEDULED" nil)
3550 (buffer-string))))
3551 (should
3552 (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
3553 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3554 (org-entry-put (point) "SCHEDULED" "earlier")
3555 (buffer-string))))
3556 (should
3557 (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
3558 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3559 (org-entry-put (point) "SCHEDULED" "later")
3560 (buffer-string))))
3561 ;; Set "DEADLINE" property.
3562 (should
3563 (string-match "^ *DEADLINE: <2014-03-04 .*?>"
3564 (org-test-with-temp-text "* H"
3565 (org-entry-put (point) "DEADLINE" "2014-03-04")
3566 (buffer-string))))
3567 (should
3568 (string= "* H\n"
3569 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3570 (org-entry-put (point) "DEADLINE" nil)
3571 (buffer-string))))
3572 (should
3573 (string-match "^ *DEADLINE: <2014-03-03 .*?>"
3574 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3575 (org-entry-put (point) "DEADLINE" "earlier")
3576 (buffer-string))))
3577 (should
3578 (string-match "^ *DEADLINE: <2014-03-05 .*?>"
3579 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3580 (org-entry-put (point) "DEADLINE" "later")
3581 (buffer-string))))
3582 ;; Set "CATEGORY" property
3583 (should
3584 (string-match "^ *:CATEGORY: cat"
3585 (org-test-with-temp-text "* H"
3586 (org-entry-put (point) "CATEGORY" "cat")
3587 (buffer-string))))
3588 ;; Regular properties, with or without pre-existing drawer.
3589 (should
3590 (string-match "^ *:A: +2$"
3591 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3592 (org-entry-put (point) "A" "2")
3593 (buffer-string))))
3594 (should
3595 (string-match "^ *:A: +1$"
3596 (org-test-with-temp-text "* H"
3597 (org-entry-put (point) "A" "1")
3598 (buffer-string))))
3599 ;; Special case: two consecutive headlines.
3600 (should
3601 (string-match "\\* A\n *:PROPERTIES:"
3602 (org-test-with-temp-text "* A\n** B"
3603 (org-entry-put (point) "A" "1")
3604 (buffer-string)))))
3607 ;;; Radio Targets
3609 (ert-deftest test-org/update-radio-target-regexp ()
3610 "Test `org-update-radio-target-regexp' specifications."
3611 ;; Properly update cache with no previous radio target regexp.
3612 (should
3613 (eq 'link
3614 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3615 (save-excursion (goto-char (point-max)) (org-element-context))
3616 (insert "<<<")
3617 (search-forward "o")
3618 (insert ">>>")
3619 (org-update-radio-target-regexp)
3620 (goto-char (point-max))
3621 (org-element-type (org-element-context)))))
3622 ;; Properly update cache with previous radio target regexp.
3623 (should
3624 (eq 'link
3625 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3626 (save-excursion (goto-char (point-max)) (org-element-context))
3627 (insert "<<<")
3628 (search-forward "o")
3629 (insert ">>>")
3630 (org-update-radio-target-regexp)
3631 (search-backward "r")
3632 (delete-char 5)
3633 (insert "new")
3634 (org-update-radio-target-regexp)
3635 (goto-char (point-max))
3636 (delete-region (line-beginning-position) (point))
3637 (insert "new")
3638 (org-element-type (org-element-context))))))
3641 ;;; Sparse trees
3643 (ert-deftest test-org/match-sparse-tree ()
3644 "Test `org-match-sparse-tree' specifications."
3645 ;; Match tags.
3646 (should-not
3647 (org-test-with-temp-text "* H\n** H1 :tag:"
3648 (org-match-sparse-tree nil "tag")
3649 (search-forward "H1")
3650 (org-invisible-p2)))
3651 (should
3652 (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
3653 (org-match-sparse-tree nil "tag")
3654 (search-forward "H2")
3655 (org-invisible-p2)))
3656 ;; "-" operator for tags.
3657 (should-not
3658 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3659 (org-match-sparse-tree nil "tag1-tag2")
3660 (search-forward "H1")
3661 (org-invisible-p2)))
3662 (should
3663 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3664 (org-match-sparse-tree nil "tag1-tag2")
3665 (search-forward "H2")
3666 (org-invisible-p2)))
3667 ;; "&" operator for tags.
3668 (should
3669 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3670 (org-match-sparse-tree nil "tag1&tag2")
3671 (search-forward "H1")
3672 (org-invisible-p2)))
3673 (should-not
3674 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3675 (org-match-sparse-tree nil "tag1&tag2")
3676 (search-forward "H2")
3677 (org-invisible-p2)))
3678 ;; "|" operator for tags.
3679 (should-not
3680 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3681 (org-match-sparse-tree nil "tag1|tag2")
3682 (search-forward "H1")
3683 (org-invisible-p2)))
3684 (should-not
3685 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3686 (org-match-sparse-tree nil "tag1|tag2")
3687 (search-forward "H2")
3688 (org-invisible-p2)))
3689 ;; Regexp match on tags.
3690 (should-not
3691 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3692 (org-match-sparse-tree nil "{^tag.*}")
3693 (search-forward "H1")
3694 (org-invisible-p2)))
3695 (should
3696 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3697 (org-match-sparse-tree nil "{^tag.*}")
3698 (search-forward "H2")
3699 (org-invisible-p2)))
3700 ;; Match group tags.
3701 (should-not
3702 (org-test-with-temp-text
3703 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3704 (org-match-sparse-tree nil "work")
3705 (search-forward "H1")
3706 (org-invisible-p2)))
3707 (should-not
3708 (org-test-with-temp-text
3709 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3710 (org-match-sparse-tree nil "work")
3711 (search-forward "H2")
3712 (org-invisible-p2)))
3713 ;; Match group tags with hard brackets.
3714 (should-not
3715 (org-test-with-temp-text
3716 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
3717 (org-match-sparse-tree nil "work")
3718 (search-forward "H1")
3719 (org-invisible-p2)))
3720 (should-not
3721 (org-test-with-temp-text
3722 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
3723 (org-match-sparse-tree nil "work")
3724 (search-forward "H2")
3725 (org-invisible-p2)))
3726 ;; Match tags in hierarchies
3727 (should-not
3728 (org-test-with-temp-text
3729 "#+TAGS: [ Lev_1 : Lev_2 ]\n
3730 #+TAGS: [ Lev_2 : Lev_3 ]\n
3731 #+TAGS: { Lev_3 : Lev_4 }\n
3732 * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
3733 (org-match-sparse-tree nil "Lev_1")
3734 (search-forward "H4")
3735 (org-invisible-p2)))
3736 ;; Match regular expressions in tags
3737 (should-not
3738 (org-test-with-temp-text
3739 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
3740 (org-match-sparse-tree nil "Lev")
3741 (search-forward "H1")
3742 (org-invisible-p2)))
3743 (should
3744 (org-test-with-temp-text
3745 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
3746 (org-match-sparse-tree nil "Lev")
3747 (search-forward "H1")
3748 (org-invisible-p2)))
3749 ;; Match properties.
3750 (should
3751 (org-test-with-temp-text
3752 "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
3753 (org-match-sparse-tree nil "A=\"1\"")
3754 (search-forward "H2")
3755 (org-invisible-p2)))
3756 (should-not
3757 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3758 (org-match-sparse-tree nil "A=\"1\"")
3759 (search-forward "H2")
3760 (org-invisible-p2)))
3761 ;; Case is not significant when matching properties.
3762 (should-not
3763 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3764 (org-match-sparse-tree nil "a=\"1\"")
3765 (search-forward "H2")
3766 (org-invisible-p2)))
3767 (should-not
3768 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
3769 (org-match-sparse-tree nil "A=\"1\"")
3770 (search-forward "H2")
3771 (org-invisible-p2)))
3772 ;; Match special LEVEL property.
3773 (should-not
3774 (org-test-with-temp-text "* H\n** H1\n*** H2"
3775 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3776 (search-forward "H1")
3777 (org-invisible-p2)))
3778 (should
3779 (org-test-with-temp-text "* H\n** H1\n*** H2"
3780 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3781 (search-forward "H2")
3782 (org-invisible-p2)))
3783 ;; Comparison operators when matching properties.
3784 (should
3785 (org-test-with-temp-text
3786 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3787 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3788 (search-forward "H1")
3789 (org-invisible-p2)))
3790 (should-not
3791 (org-test-with-temp-text
3792 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3793 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3794 (search-forward "H2")
3795 (org-invisible-p2)))
3796 ;; Regexp match on properties values.
3797 (should-not
3798 (org-test-with-temp-text
3799 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3800 (org-match-sparse-tree nil "A={f.*}")
3801 (search-forward "H1")
3802 (org-invisible-p2)))
3803 (should
3804 (org-test-with-temp-text
3805 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3806 (org-match-sparse-tree nil "A={f.*}")
3807 (search-forward "H2")
3808 (org-invisible-p2)))
3809 ;; With an optional argument, limit match to TODO entries.
3810 (should-not
3811 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3812 (org-match-sparse-tree t "tag")
3813 (search-forward "H1")
3814 (org-invisible-p2)))
3815 (should
3816 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3817 (org-match-sparse-tree t "tag")
3818 (search-forward "H2")
3819 (org-invisible-p2))))
3822 ;;; Timestamps API
3824 (ert-deftest test-org/time-stamp ()
3825 "Test `org-time-stamp' specifications."
3826 ;; Insert chosen time stamp at point.
3827 (should
3828 (string-match
3829 "Te<2014-03-04 .*?>xt"
3830 (org-test-with-temp-text "Te<point>xt"
3831 (flet ((org-read-date
3832 (&rest args)
3833 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3834 (org-time-stamp nil)
3835 (buffer-string)))))
3836 ;; With a prefix argument, also insert time.
3837 (should
3838 (string-match
3839 "Te<2014-03-04 .*? 00:41>xt"
3840 (org-test-with-temp-text "Te<point>xt"
3841 (flet ((org-read-date
3842 (&rest args)
3843 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
3844 (org-time-stamp '(4))
3845 (buffer-string)))))
3846 ;; With two universal prefix arguments, insert an active timestamp
3847 ;; with the current time without prompting the user.
3848 (should
3849 (string-match
3850 "Te<2014-03-04 .*? 00:41>xt"
3851 (org-test-with-temp-text "Te<point>xt"
3852 (flet ((current-time
3854 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
3855 (org-time-stamp '(16))
3856 (buffer-string)))))
3857 ;; When optional argument is non-nil, insert an inactive timestamp.
3858 (should
3859 (string-match
3860 "Te\\[2014-03-04 .*?\\]xt"
3861 (org-test-with-temp-text "Te<point>xt"
3862 (flet ((org-read-date
3863 (&rest args)
3864 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3865 (org-time-stamp nil t)
3866 (buffer-string)))))
3867 ;; When called from a timestamp, replace existing one.
3868 (should
3869 (string-match
3870 "<2014-03-04 .*?>"
3871 (org-test-with-temp-text "<2012-03-29<point> thu.>"
3872 (flet ((org-read-date
3873 (&rest args)
3874 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3875 (org-time-stamp nil)
3876 (buffer-string)))))
3877 (should
3878 (string-match
3879 "<2014-03-04 .*?>--<2014-03-04 .*?>"
3880 (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
3881 (flet ((org-read-date
3882 (&rest args)
3883 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3884 (org-time-stamp nil)
3885 (buffer-string)))))
3886 ;; When replacing a timestamp, preserve repeater, if any.
3887 (should
3888 (string-match
3889 "<2014-03-04 .*? \\+2y>"
3890 (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
3891 (flet ((org-read-date
3892 (&rest args)
3893 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3894 (org-time-stamp nil)
3895 (buffer-string)))))
3896 ;; When called twice in a raw, build a date range.
3897 (should
3898 (string-match
3899 "<2012-03-29 .*?>--<2014-03-04 .*?>"
3900 (org-test-with-temp-text "<2012-03-29 thu.><point>"
3901 (flet ((org-read-date
3902 (&rest args)
3903 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3904 (let ((last-command 'org-time-stamp)
3905 (this-command 'org-time-stamp))
3906 (org-time-stamp nil))
3907 (buffer-string))))))
3909 (ert-deftest test-org/timestamp-has-time-p ()
3910 "Test `org-timestamp-has-time-p' specifications."
3911 ;; With time.
3912 (should
3913 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
3914 (org-timestamp-has-time-p (org-element-context))))
3915 ;; Without time.
3916 (should-not
3917 (org-test-with-temp-text "<2012-03-29 Thu>"
3918 (org-timestamp-has-time-p (org-element-context)))))
3920 (ert-deftest test-org/timestamp-format ()
3921 "Test `org-timestamp-format' specifications."
3922 ;; Regular test.
3923 (should
3924 (equal
3925 "2012-03-29 16:40"
3926 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
3927 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
3928 ;; Range end.
3929 (should
3930 (equal
3931 "2012-03-29"
3932 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
3933 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
3935 (ert-deftest test-org/timestamp-split-range ()
3936 "Test `org-timestamp-split-range' specifications."
3937 ;; Extract range start (active).
3938 (should
3939 (equal '(2012 3 29)
3940 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3941 (let ((ts (org-timestamp-split-range (org-element-context))))
3942 (mapcar (lambda (p) (org-element-property p ts))
3943 '(:year-end :month-end :day-end))))))
3944 ;; Extract range start (inactive)
3945 (should
3946 (equal '(2012 3 29)
3947 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
3948 (let ((ts (org-timestamp-split-range (org-element-context))))
3949 (mapcar (lambda (p) (org-element-property p ts))
3950 '(:year-end :month-end :day-end))))))
3951 ;; Extract range end (active).
3952 (should
3953 (equal '(2012 3 30)
3954 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3955 (let ((ts (org-timestamp-split-range
3956 (org-element-context) t)))
3957 (mapcar (lambda (p) (org-element-property p ts))
3958 '(:year-end :month-end :day-end))))))
3959 ;; Extract range end (inactive)
3960 (should
3961 (equal '(2012 3 30)
3962 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
3963 (let ((ts (org-timestamp-split-range
3964 (org-element-context) t)))
3965 (mapcar (lambda (p) (org-element-property p ts))
3966 '(:year-end :month-end :day-end))))))
3967 ;; Return the timestamp if not a range.
3968 (should
3969 (org-test-with-temp-text "[2012-03-29 Thu]"
3970 (let* ((ts-orig (org-element-context))
3971 (ts-copy (org-timestamp-split-range ts-orig)))
3972 (eq ts-orig ts-copy))))
3973 (should
3974 (org-test-with-temp-text "<%%(org-float t 4 2)>"
3975 (let* ((ts-orig (org-element-context))
3976 (ts-copy (org-timestamp-split-range ts-orig)))
3977 (eq ts-orig ts-copy)))))
3979 (ert-deftest test-org/timestamp-translate ()
3980 "Test `org-timestamp-translate' specifications."
3981 ;; Translate whole date range.
3982 (should
3983 (equal "<29>--<30>"
3984 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3985 (let ((org-display-custom-times t)
3986 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3987 (org-timestamp-translate (org-element-context))))))
3988 ;; Translate date range start.
3989 (should
3990 (equal "<29>"
3991 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3992 (let ((org-display-custom-times t)
3993 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3994 (org-timestamp-translate (org-element-context) 'start)))))
3995 ;; Translate date range end.
3996 (should
3997 (equal "<30>"
3998 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3999 (let ((org-display-custom-times t)
4000 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4001 (org-timestamp-translate (org-element-context) 'end)))))
4002 ;; Translate time range.
4003 (should
4004 (equal "<08>--<16>"
4005 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
4006 (let ((org-display-custom-times t)
4007 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
4008 (org-timestamp-translate (org-element-context))))))
4009 ;; Translate non-range timestamp.
4010 (should
4011 (equal "<29>"
4012 (org-test-with-temp-text "<2012-03-29 Thu>"
4013 (let ((org-display-custom-times t)
4014 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4015 (org-timestamp-translate (org-element-context))))))
4016 ;; Do not change `diary' timestamps.
4017 (should
4018 (equal "<%%(org-float t 4 2)>"
4019 (org-test-with-temp-text "<%%(org-float t 4 2)>"
4020 (let ((org-display-custom-times t)
4021 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4022 (org-timestamp-translate (org-element-context)))))))
4026 ;;; Visibility
4028 (ert-deftest test-org/flag-drawer ()
4029 "Test `org-flag-drawer' specifications."
4030 ;; Hide drawer.
4031 (should
4032 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4033 (org-flag-drawer t)
4034 (get-char-property (line-end-position) 'invisible)))
4035 ;; Show drawer.
4036 (should-not
4037 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4038 (org-flag-drawer t)
4039 (org-flag-drawer nil)
4040 (get-char-property (line-end-position) 'invisible)))
4041 ;; Test optional argument.
4042 (should
4043 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
4044 (let ((drawer (save-excursion (search-forward ":D2")
4045 (org-element-at-point))))
4046 (org-flag-drawer t drawer)
4047 (get-char-property (progn (search-forward ":D2") (line-end-position))
4048 'invisible))))
4049 (should-not
4050 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
4051 (let ((drawer (save-excursion (search-forward ":D2")
4052 (org-element-at-point))))
4053 (org-flag-drawer t drawer)
4054 (get-char-property (line-end-position) 'invisible))))
4055 ;; Do not hide fake drawers.
4056 (should-not
4057 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
4058 (forward-line 1)
4059 (org-flag-drawer t)
4060 (get-char-property (line-end-position) 'invisible)))
4061 ;; Do not hide incomplete drawers.
4062 (should-not
4063 (org-test-with-temp-text ":D:\nparagraph"
4064 (forward-line 1)
4065 (org-flag-drawer t)
4066 (get-char-property (line-end-position) 'invisible)))
4067 ;; Do not hide drawers when called from final blank lines.
4068 (should-not
4069 (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
4070 (goto-char (point-max))
4071 (org-flag-drawer t)
4072 (goto-char (point-min))
4073 (get-char-property (line-end-position) 'invisible)))
4074 ;; Don't leave point in an invisible part of the buffer when hiding
4075 ;; a drawer away.
4076 (should-not
4077 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4078 (goto-char (point-max))
4079 (org-flag-drawer t)
4080 (get-char-property (point) 'invisible))))
4082 (ert-deftest test-org/hide-block-toggle ()
4083 "Test `org-hide-block-toggle' specifications."
4084 ;; Error when not at a block.
4085 (should-error
4086 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
4087 (org-hide-block-toggle 'off)
4088 (get-char-property (line-end-position) 'invisible)))
4089 ;; Hide block.
4090 (should
4091 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
4092 (org-hide-block-toggle)
4093 (get-char-property (line-end-position) 'invisible)))
4094 (should
4095 (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
4096 (org-hide-block-toggle)
4097 (get-char-property (line-end-position) 'invisible)))
4098 ;; Show block unconditionally when optional argument is `off'.
4099 (should-not
4100 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4101 (org-hide-block-toggle)
4102 (org-hide-block-toggle 'off)
4103 (get-char-property (line-end-position) 'invisible)))
4104 (should-not
4105 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4106 (org-hide-block-toggle 'off)
4107 (get-char-property (line-end-position) 'invisible)))
4108 ;; Hide block unconditionally when optional argument is non-nil.
4109 (should
4110 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4111 (org-hide-block-toggle t)
4112 (get-char-property (line-end-position) 'invisible)))
4113 (should
4114 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4115 (org-hide-block-toggle)
4116 (org-hide-block-toggle t)
4117 (get-char-property (line-end-position) 'invisible)))
4118 ;; Do not hide block when called from final blank lines.
4119 (should-not
4120 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
4121 (org-hide-block-toggle)
4122 (goto-char (point-min))
4123 (get-char-property (line-end-position) 'invisible)))
4124 ;; Don't leave point in an invisible part of the buffer when hiding
4125 ;; a block away.
4126 (should-not
4127 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
4128 (org-hide-block-toggle)
4129 (get-char-property (point) 'invisible))))
4131 (ert-deftest test-org/hide-block-toggle-maybe ()
4132 "Test `org-hide-block-toggle-maybe' specifications."
4133 (should
4134 (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
4135 (org-hide-block-toggle-maybe)))
4136 (should-not
4137 (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
4139 (ert-deftest test-org/show-set-visibility ()
4140 "Test `org-show-set-visibility' specifications."
4141 ;; Do not throw an error before first heading.
4142 (should
4143 (org-test-with-temp-text "Preamble\n* Headline"
4144 (org-show-set-visibility 'tree)
4146 ;; Test all visibility spans, both on headline and in entry.
4147 (let ((list-visible-lines
4148 (lambda (state headerp)
4149 (org-test-with-temp-text "* Grandmother (0)
4150 ** Uncle (1)
4151 *** Heir (2)
4152 ** Father (3)
4153 Ancestor text (4)
4154 *** Sister (5)
4155 Sibling text (6)
4156 *** Self (7)
4157 Match (8)
4158 **** First born (9)
4159 Child text (10)
4160 **** The other child (11)
4161 *** Brother (12)
4162 ** Aunt (13)
4164 (org-cycle t)
4165 (search-forward (if headerp "Self" "Match"))
4166 (org-show-set-visibility state)
4167 (goto-char (point-min))
4168 (let (result (line 0))
4169 (while (not (eobp))
4170 (unless (org-invisible-p2) (push line result))
4171 (incf line)
4172 (forward-line))
4173 (nreverse result))))))
4174 (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
4175 (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
4176 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
4177 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
4178 (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
4179 (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
4180 (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
4181 (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
4182 (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
4183 (should (equal '(0 1 3 5 7 8 9 11 12 13)
4184 (funcall list-visible-lines 'tree nil)))
4185 (should (equal '(0 1 3 4 5 7 12 13)
4186 (funcall list-visible-lines 'canonical t)))
4187 (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
4188 (funcall list-visible-lines 'canonical nil)))))
4191 (provide 'test-org)
4193 ;;; test-org.el ends here