Merge branch 'maint'
[org-mode/org-tableheadings.git] / testing / lisp / test-org.el
blobfe2111392e5d801504889ed1af6d1090079df8b5
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 ;; Template test file for Org tests
23 ;;; Code:
26 ;;; Comments
28 (ert-deftest test-org/toggle-comment ()
29 "Test `org-toggle-comment' specifications."
30 ;; Simple headline.
31 (should
32 (equal "* Test"
33 (org-test-with-temp-text "* COMMENT Test"
34 (org-toggle-comment)
35 (buffer-string))))
36 (should
37 (equal "* COMMENT Test"
38 (org-test-with-temp-text "* Test"
39 (org-toggle-comment)
40 (buffer-string))))
41 ;; Headline with a regular keyword.
42 (should
43 (equal "* TODO Test"
44 (org-test-with-temp-text "* TODO COMMENT Test"
45 (org-toggle-comment)
46 (buffer-string))))
47 (should
48 (equal "* TODO COMMENT Test"
49 (org-test-with-temp-text "* TODO Test"
50 (org-toggle-comment)
51 (buffer-string))))
52 ;; Empty headline.
53 (should
54 (equal "* "
55 (org-test-with-temp-text "* COMMENT"
56 (org-toggle-comment)
57 (buffer-string))))
58 (should
59 (equal "* COMMENT"
60 (org-test-with-temp-text "* "
61 (org-toggle-comment)
62 (buffer-string))))
63 ;; Headline with a single keyword.
64 (should
65 (equal "* TODO "
66 (org-test-with-temp-text "* TODO COMMENT"
67 (org-toggle-comment)
68 (buffer-string))))
69 (should
70 (equal "* TODO COMMENT"
71 (org-test-with-temp-text "* TODO"
72 (org-toggle-comment)
73 (buffer-string))))
74 ;; Headline with a keyword, a priority cookie and contents.
75 (should
76 (equal "* TODO [#A] Headline"
77 (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
78 (org-toggle-comment)
79 (buffer-string))))
80 (should
81 (equal "* TODO [#A] COMMENT Headline"
82 (org-test-with-temp-text "* TODO [#A] Headline"
83 (org-toggle-comment)
84 (buffer-string)))))
86 (ert-deftest test-org/comment-dwim ()
87 "Test `comment-dwim' behaviour in an Org buffer."
88 ;; No region selected, no comment on current line and line not
89 ;; empty: insert comment on line above.
90 (should
91 (equal "# \nComment"
92 (org-test-with-temp-text "Comment"
93 (call-interactively #'org-comment-dwim)
94 (buffer-string))))
95 ;; No region selected, no comment on current line and line empty:
96 ;; insert comment on this line.
97 (should
98 (equal "# \nParagraph"
99 (org-test-with-temp-text "\nParagraph"
100 (call-interactively #'org-comment-dwim)
101 (buffer-string))))
102 ;; No region selected, and a comment on this line: indent it.
103 (should
104 (equal "* Headline\n # Comment"
105 (org-test-with-temp-text "* Headline\n# <point>Comment"
106 (let ((org-adapt-indentation t))
107 (call-interactively #'org-comment-dwim))
108 (buffer-string))))
109 ;; Also recognize single # at column 0 as comments.
110 (should
111 (equal "# Comment"
112 (org-test-with-temp-text "# Comment"
113 (call-interactively #'org-comment-dwim)
114 (buffer-string))))
115 ;; Region selected and only comments and blank lines within it:
116 ;; un-comment all commented lines.
117 (should
118 (equal "Comment 1\n\nComment 2"
119 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
120 (transient-mark-mode 1)
121 (push-mark (point) t t)
122 (goto-char (point-max))
123 (call-interactively #'org-comment-dwim)
124 (buffer-string))))
125 ;; Region selected without comments: comment all lines if
126 ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
127 (should
128 (equal "# Comment 1\n\n# Comment 2"
129 (org-test-with-temp-text "Comment 1\n\nComment 2"
130 (transient-mark-mode 1)
131 (push-mark (point) t t)
132 (goto-char (point-max))
133 (let ((comment-empty-lines nil))
134 (call-interactively #'org-comment-dwim))
135 (buffer-string))))
136 (should
137 (equal "# Comment 1\n# \n# Comment 2"
138 (org-test-with-temp-text "Comment 1\n\nComment 2"
139 (transient-mark-mode 1)
140 (push-mark (point) t t)
141 (goto-char (point-max))
142 (let ((comment-empty-lines t))
143 (call-interactively #'org-comment-dwim))
144 (buffer-string))))
145 ;; In front of a keyword without region, insert a new comment.
146 (should
147 (equal "# \n#+KEYWORD: value"
148 (org-test-with-temp-text "#+KEYWORD: value"
149 (call-interactively #'org-comment-dwim)
150 (buffer-string))))
151 ;; Comment a heading
152 (should
153 (equal "* COMMENT Test"
154 (org-test-with-temp-text "* Test"
155 (call-interactively #'org-comment-dwim)
156 (buffer-string))))
157 ;; In a source block, use appropriate syntax.
158 (should
159 (equal " ;; "
160 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n<point>\n#+END_SRC"
161 (let ((org-edit-src-content-indentation 2))
162 (call-interactively #'org-comment-dwim))
163 (buffer-substring-no-properties (line-beginning-position)
164 (point)))))
165 (should
166 (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
167 (org-test-with-temp-text
168 "#+BEGIN_SRC emacs-lisp\n<point>a\nb\n#+END_SRC"
169 (transient-mark-mode 1)
170 (push-mark (point) t t)
171 (forward-line 2)
172 (let ((org-edit-src-content-indentation 2))
173 (call-interactively #'org-comment-dwim))
174 (buffer-string)))))
178 ;;; Date and time analysis
180 (ert-deftest test-org/org-read-date ()
181 "Test `org-read-date' specifications."
182 ;; Parse ISO date with abbreviated year and month.
183 (should (equal "2012-03-29 16:40"
184 (let ((org-time-was-given t))
185 (org-read-date t nil "12-3-29 16:40"))))
186 ;; Parse Europeans dates.
187 (should (equal "2012-03-29 16:40"
188 (let ((org-time-was-given t))
189 (org-read-date t nil "29.03.2012 16:40"))))
190 ;; Parse Europeans dates without year.
191 (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
192 (let ((org-time-was-given t))
193 (org-read-date t nil "29.03. 16:40"))))
194 ;; Relative date applied to current time if there is single
195 ;; plus/minus, or to default date when there are two of them.
196 (should
197 (equal
198 "2015-03-04"
199 (cl-letf (((symbol-function 'current-time)
200 (lambda ()
201 (apply #'encode-time (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 (cl-letf (((symbol-function 'current-time)
209 (lambda ()
210 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
211 (org-read-date
212 t nil "++1y" nil
213 (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
214 ;; When `org-read-date-prefer-future' is non-nil, prefer future
215 ;; dates (relatively to now) when incomplete. Otherwise, use
216 ;; default date.
217 (should
218 (equal
219 "2014-04-01"
220 (cl-letf (((symbol-function 'current-time)
221 (lambda ()
222 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
223 (let ((org-read-date-prefer-future t))
224 (org-read-date t nil "1")))))
225 (should
226 (equal
227 "2013-03-04"
228 (cl-letf (((symbol-function 'current-time)
229 (lambda ()
230 (apply #'encode-time (org-parse-time-string "2012-03-29")))))
231 (let ((org-read-date-prefer-future t))
232 (org-read-date t nil "3-4")))))
233 (should
234 (equal
235 "2012-03-04"
236 (cl-letf (((symbol-function 'current-time)
237 (lambda ()
238 (apply #'encode-time (org-parse-time-string "2012-03-29")))))
239 (let ((org-read-date-prefer-future nil))
240 (org-read-date t nil "3-4")))))
241 ;; When set to `org-read-date-prefer-future' is set to `time', read
242 ;; day is moved to tomorrow if specified hour is before current
243 ;; time. However, it only happens in no other part of the date is
244 ;; specified.
245 (should
246 (equal
247 "2012-03-30"
248 (cl-letf (((symbol-function 'current-time)
249 (lambda ()
250 (apply #'encode-time (org-parse-time-string "2012-03-29 16:40")))))
251 (let ((org-read-date-prefer-future 'time))
252 (org-read-date t nil "00:40" nil)))))
253 (should-not
254 (equal
255 "2012-03-30"
256 (cl-letf (((symbol-function 'current-time)
257 (lambda ()
258 (apply #'encode-time (org-parse-time-string "2012-03-29 16:40")))))
259 (let ((org-read-date-prefer-future 'time))
260 (org-read-date t nil "29 00:40" nil)))))
261 ;; Caveat: `org-read-date-prefer-future' always refers to current
262 ;; time, not default time, when they differ.
263 (should
264 (equal
265 "2014-04-01"
266 (cl-letf (((symbol-function 'current-time)
267 (lambda ()
268 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
269 (let ((org-read-date-prefer-future t))
270 (org-read-date
271 t nil "1" nil
272 (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
273 (should
274 (equal
275 "2014-03-25"
276 (cl-letf (((symbol-function 'current-time)
277 (lambda ()
278 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
279 (let ((org-read-date-prefer-future t))
280 (org-read-date
281 t nil "25" nil
282 (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
284 (ert-deftest test-org/org-parse-time-string ()
285 "Test `org-parse-time-string'."
286 (should (equal (org-parse-time-string "2012-03-29 16:40")
287 '(0 40 16 29 3 2012 nil nil nil)))
288 (should (equal (org-parse-time-string "[2012-03-29 16:40]")
289 '(0 40 16 29 3 2012 nil nil nil)))
290 (should (equal (org-parse-time-string "<2012-03-29 16:40>")
291 '(0 40 16 29 3 2012 nil nil nil)))
292 (should (equal (org-parse-time-string "<2012-03-29>")
293 '(0 0 0 29 3 2012 nil nil nil)))
294 (should (equal (org-parse-time-string "<2012-03-29>" t)
295 '(0 nil nil 29 3 2012 nil nil nil))))
297 (ert-deftest test-org/closest-date ()
298 "Test `org-closest-date' specifications."
299 (require 'calendar)
300 ;; Time stamps without a repeater are returned unchanged.
301 (should
302 (equal
303 '(3 29 2012)
304 (calendar-gregorian-from-absolute
305 (org-closest-date "<2012-03-29>" "<2014-03-04>" nil))))
306 ;; Time stamps with a null repeater are returned unchanged.
307 (should
308 (equal
309 '(3 29 2012)
310 (calendar-gregorian-from-absolute
311 (org-closest-date "<2012-03-29 +0d>" "<2014-03-04>" nil))))
312 ;; if PREFER is set to `past' always return a date before, or equal
313 ;; to CURRENT.
314 (should
315 (equal
316 '(3 1 2014)
317 (calendar-gregorian-from-absolute
318 (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'past))))
319 (should
320 (equal
321 '(3 4 2014)
322 (calendar-gregorian-from-absolute
323 (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'past))))
324 ;; if PREFER is set to `future' always return a date before, or equal
325 ;; to CURRENT.
326 (should
327 (equal
328 '(3 29 2014)
329 (calendar-gregorian-from-absolute
330 (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'future))))
331 (should
332 (equal
333 '(3 4 2014)
334 (calendar-gregorian-from-absolute
335 (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'future))))
336 ;; If PREFER is neither `past' nor `future', select closest date.
337 (should
338 (equal
339 '(3 1 2014)
340 (calendar-gregorian-from-absolute
341 (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" nil))))
342 (should
343 (equal
344 '(5 4 2014)
345 (calendar-gregorian-from-absolute
346 (org-closest-date "<2012-03-04 +1m>" "<2014-04-28>" nil))))
347 ;; Test "day" repeater.
348 (should
349 (equal '(3 8 2014)
350 (calendar-gregorian-from-absolute
351 (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'past))))
352 (should
353 (equal '(3 10 2014)
354 (calendar-gregorian-from-absolute
355 (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'future))))
356 ;; Test "month" repeater.
357 (should
358 (equal '(1 5 2015)
359 (calendar-gregorian-from-absolute
360 (org-closest-date "<2014-03-05 +2m>" "<2015-02-04>" 'past))))
361 (should
362 (equal '(3 29 2014)
363 (calendar-gregorian-from-absolute
364 (org-closest-date "<2012-03-29 +2m>" "<2014-03-04>" 'future))))
365 ;; Test "year" repeater.
366 (should
367 (equal '(3 5 2014)
368 (calendar-gregorian-from-absolute
369 (org-closest-date "<2014-03-05 +2y>" "<2015-02-04>" 'past))))
370 (should
371 (equal '(3 29 2014)
372 (calendar-gregorian-from-absolute
373 (org-closest-date "<2012-03-29 +2y>" "<2014-03-04>" 'future)))))
375 (ert-deftest test-org/deadline-close-p ()
376 "Test `org-deadline-close-p' specifications."
377 ;; Pretend that the current time is 2016-06-03 Fri 01:43
378 (cl-letf (((symbol-function 'current-time)
379 (lambda ()
380 (apply #'encode-time
381 (org-parse-time-string "2016-06-03 Fri 01:43")))))
382 ;; Timestamps are close if they are within `ndays' of lead time.
383 (org-test-with-temp-text "* Heading"
384 (should (org-deadline-close-p "2016-06-03 Fri" 0))
385 (should (org-deadline-close-p "2016-06-02 Thu" 0))
386 (should-not (org-deadline-close-p "2016-06-04 Sat" 0))
387 (should (org-deadline-close-p "2016-06-04 Sat" 1))
388 (should (org-deadline-close-p "2016-06-03 Fri 12:00" 0)))
389 ;; Read `ndays' from timestamp if argument not given.
390 (org-test-with-temp-text "* H"
391 (should (org-deadline-close-p "2016-06-04 Sat -1d"))
392 (should-not (org-deadline-close-p "2016-06-04 Sat -0d"))
393 (should (org-deadline-close-p "2016-06-10 Fri -1w"))
394 (should-not (org-deadline-close-p "2016-06-11 Sat -1w")))
395 ;; Prefer `ndays' argument over lead time in timestamp.
396 (org-test-with-temp-text "* H"
397 (should (org-deadline-close-p "2016-06-04 Sat -0d" 1))
398 (should-not (org-deadline-close-p "2016-06-04 Sat -0d" 0)))
399 ;; Completed tasks are never close.
400 (let ((org-todo-keywords '(("TODO" "|" "DONE"))))
401 (org-test-with-temp-text "* TODO Heading"
402 (should (org-deadline-close-p "2016-06-03")))
403 (org-test-with-temp-text "* DONE Heading"
404 (should-not (org-deadline-close-p "2016-06-03"))))))
407 ;;; Drawers
409 (ert-deftest test-org/insert-property-drawer ()
410 "Test `org-insert-property-drawer' specifications."
411 ;; Error before first headline.
412 (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
413 ;; Insert drawer right after headline if there is no planning line,
414 ;; or after it otherwise.
415 (should
416 (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
417 (org-test-with-temp-text "* H\nParagraph<point>"
418 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
419 (buffer-string))))
420 (should
421 (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
422 (org-test-with-temp-text
423 "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
424 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
425 (buffer-string))))
426 ;; Indent inserted drawer.
427 (should
428 (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
429 (org-test-with-temp-text "* H\nParagraph<point>"
430 (let ((org-adapt-indentation t)) (org-insert-property-drawer))
431 (buffer-string))))
432 ;; Handle insertion at eob.
433 (should
434 (equal "* H\n:PROPERTIES:\n:END:\n"
435 (org-test-with-temp-text "* H"
436 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
437 (buffer-string))))
438 ;; Skip inlinetasks before point.
439 (when (featurep 'org-inlinetask)
440 (should
441 (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
442 (org-test-with-temp-text
443 "* H\n*************** I\n*************** END\nP<point>"
444 (let ((org-adapt-indentation nil)
445 (org-inlinetask-min-level 15))
446 (org-insert-property-drawer))
447 (buffer-string)))))
448 ;; Correctly set drawer in an inlinetask.
449 (when (featurep 'org-inlinetask)
450 (should
451 (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
452 (org-test-with-temp-text
453 "* H\n*************** I\nP<point>\n*************** END"
454 (let ((org-adapt-indentation nil)
455 (org-inlinetask-min-level 15))
456 (org-insert-property-drawer))
457 (buffer-string))))))
460 ;;; Filling
462 (ert-deftest test-org/fill-element ()
463 "Test `org-fill-element' specifications."
464 ;; At an Org table, align it.
465 (should
466 (equal "| a |\n"
467 (org-test-with-temp-text "|a|"
468 (org-fill-element)
469 (buffer-string))))
470 (should
471 (equal "#+name: table\n| a |\n"
472 (org-test-with-temp-text "#+name: table\n| a |\n"
473 (org-fill-element)
474 (buffer-string))))
475 ;; At a paragraph, preserve line breaks.
476 (org-test-with-temp-text "some \\\\\nlong\ntext"
477 (let ((fill-column 20))
478 (org-fill-element)
479 (should (equal (buffer-string) "some \\\\\nlong text"))))
480 ;; Correctly fill a paragraph when point is at its very end.
481 (should
482 (equal "A B"
483 (org-test-with-temp-text "A\nB"
484 (let ((fill-column 20))
485 (goto-char (point-max))
486 (org-fill-element)
487 (buffer-string)))))
488 ;; Correctly fill the last paragraph of a greater element.
489 (should
490 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
491 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
492 (let ((fill-column 8))
493 (forward-line)
494 (end-of-line)
495 (org-fill-element)
496 (buffer-string)))))
497 ;; Correctly fill an element in a narrowed buffer.
498 (should
499 (equal "01234\n6"
500 (org-test-with-temp-text "01234 6789"
501 (let ((fill-column 5))
502 (narrow-to-region 1 8)
503 (org-fill-element)
504 (buffer-string)))))
505 ;; Handle `adaptive-fill-regexp' in paragraphs.
506 (should
507 (equal "> a b"
508 (org-test-with-temp-text "> a\n> b"
509 (let ((fill-column 5)
510 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
511 (org-fill-element)
512 (buffer-string)))))
513 ;; Special case: Fill first paragraph when point is at an item or
514 ;; a plain-list or a footnote reference.
515 (should
516 (equal "- A B"
517 (org-test-with-temp-text "- A\n B"
518 (let ((fill-column 20))
519 (org-fill-element)
520 (buffer-string)))))
521 (should
522 (equal "[fn:1] A B"
523 (org-test-with-temp-text "[fn:1] A\nB"
524 (let ((fill-column 20))
525 (org-fill-element)
526 (buffer-string)))))
527 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
528 (let ((fill-column 20))
529 (org-fill-element)
530 (should (equal (buffer-string)
531 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
532 ;; Fill contents of `comment-block' elements.
533 (should
534 (equal
535 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
536 (let ((fill-column 20))
537 (forward-line)
538 (org-fill-element)
539 (buffer-string)))
540 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
541 ;; Fill `comment' elements.
542 (should
543 (equal " # A B"
544 (org-test-with-temp-text " # A\n # B"
545 (let ((fill-column 20))
546 (org-fill-element)
547 (buffer-string)))))
548 ;; Do not mix consecutive comments when filling one of them.
549 (should
550 (equal "# A B\n\n# C"
551 (org-test-with-temp-text "# A\n# B\n\n# C"
552 (let ((fill-column 20))
553 (org-fill-element)
554 (buffer-string)))))
555 ;; Use commented empty lines as separators when filling comments.
556 (should
557 (equal "# A B\n#\n# C"
558 (org-test-with-temp-text "# A\n# B\n#\n# C"
559 (let ((fill-column 20))
560 (org-fill-element)
561 (buffer-string)))))
562 ;; Handle `adaptive-fill-regexp' in comments.
563 (should
564 (equal "# > a b"
565 (org-test-with-temp-text "# > a\n# > b"
566 (let ((fill-column 20)
567 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
568 (org-fill-element)
569 (buffer-string)))))
570 ;; Do nothing at affiliated keywords.
571 (should
572 (equal "#+NAME: para\nSome\ntext."
573 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
574 (let ((fill-column 20))
575 (org-fill-element)
576 (buffer-string)))))
577 ;; Do not move point after table when filling a table.
578 (should-not
579 (org-test-with-temp-text "| a | b |\n| c | d |\n"
580 (forward-char)
581 (org-fill-element)
582 (eobp)))
583 ;; Do not fill "n" macro, with or without arguments, followed by
584 ;; a dot or a closing parenthesis since it could be confused with
585 ;; a numbered bullet.
586 (should-not
587 (equal "123456789\n{{{n}}}."
588 (org-test-with-temp-text "123456789 {{{n}}}."
589 (let ((fill-column 10))
590 (org-fill-element)
591 (buffer-string)))))
592 (should-not
593 (equal "123456789\n{{{n}}}\)"
594 (org-test-with-temp-text "123456789 {{{n}}}\)"
595 (let ((fill-column 10))
596 (org-fill-element)
597 (buffer-string)))))
598 (should-not
599 (equal "123456789\n{{{n()}}}."
600 (org-test-with-temp-text "123456789 {{{n()}}}."
601 (let ((fill-column 10))
602 (org-fill-element)
603 (buffer-string)))))
604 (should-not
605 (equal "123456789\n{{{n(counter)}}}."
606 (org-test-with-temp-text "123456789 {{{n(counter)}}}."
607 (let ((fill-column 10))
608 (org-fill-element)
609 (buffer-string))))))
611 (ert-deftest test-org/auto-fill-function ()
612 "Test auto-filling features."
613 ;; Auto fill paragraph.
614 (should
615 (equal "12345\n7890"
616 (org-test-with-temp-text "12345 7890"
617 (let ((fill-column 5))
618 (end-of-line)
619 (org-auto-fill-function)
620 (buffer-string)))))
621 ;; Auto fill first paragraph in an item.
622 (should
623 (equal "- 12345\n 7890"
624 (org-test-with-temp-text "- 12345 7890"
625 (let ((fill-column 7))
626 (end-of-line)
627 (org-auto-fill-function)
628 (buffer-string)))))
629 ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
630 (should
631 (equal "> 12345\n 7890"
632 (org-test-with-temp-text "> 12345 7890"
633 (let ((fill-column 10)
634 (adaptive-fill-regexp "[ \t]*>+[ \t]*")
635 (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
636 (end-of-line)
637 (org-auto-fill-function)
638 (buffer-string)))))
639 (should
640 (equal "> 12345\n> 12345\n> 7890"
641 (org-test-with-temp-text "> 12345\n> 12345 7890"
642 (let ((fill-column 10)
643 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
644 (goto-char (point-max))
645 (org-auto-fill-function)
646 (buffer-string)))))
647 (should-not
648 (equal " 12345\n *12345\n *12345"
649 (org-test-with-temp-text " 12345\n *12345 12345"
650 (let ((fill-column 10)
651 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
652 (goto-char (point-max))
653 (org-auto-fill-function)
654 (buffer-string)))))
655 ;; Auto fill comments.
656 (should
657 (equal " # 12345\n # 7890"
658 (org-test-with-temp-text " # 12345 7890"
659 (let ((fill-column 10))
660 (end-of-line)
661 (org-auto-fill-function)
662 (buffer-string)))))
663 ;; A hash within a line isn't a comment.
664 (should-not
665 (equal "12345 # 7890\n# 1"
666 (org-test-with-temp-text "12345 # 7890 1"
667 (let ((fill-column 12))
668 (end-of-line)
669 (org-auto-fill-function)
670 (buffer-string)))))
671 ;; Correctly interpret empty prefix.
672 (should-not
673 (equal "# a\n# b\nRegular\n# paragraph"
674 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
675 (let ((fill-column 12))
676 (end-of-line 3)
677 (org-auto-fill-function)
678 (buffer-string)))))
679 ;; Comment block: auto fill contents.
680 (should
681 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
682 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
683 (let ((fill-column 5))
684 (forward-line)
685 (end-of-line)
686 (org-auto-fill-function)
687 (buffer-string)))))
688 (should
689 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
690 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
691 (let ((fill-column 5))
692 (forward-line)
693 (end-of-line)
694 (org-auto-fill-function)
695 (buffer-string)))))
696 ;; Do not fill if a new item could be created.
697 (should-not
698 (equal "12345\n- 90"
699 (org-test-with-temp-text "12345 - 90"
700 (let ((fill-column 5))
701 (end-of-line)
702 (org-auto-fill-function)
703 (buffer-string)))))
704 ;; Do not fill if a line break could be introduced.
705 (should-not
706 (equal "123\\\\\n7890"
707 (org-test-with-temp-text "123\\\\ 7890"
708 (let ((fill-column 6))
709 (end-of-line)
710 (org-auto-fill-function)
711 (buffer-string)))))
712 ;; Do not fill affiliated keywords.
713 (should-not
714 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
715 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
716 (let ((fill-column 20))
717 (end-of-line)
718 (org-auto-fill-function)
719 (buffer-string))))))
723 ;;; Indentation
725 (ert-deftest test-org/indent-line ()
726 "Test `org-indent-line' specifications."
727 ;; Do not indent diary sexps, footnote definitions or headlines.
728 (should
729 (zerop
730 (org-test-with-temp-text "%%(org-calendar-holiday)"
731 (org-indent-line)
732 (org-get-indentation))))
733 (should
734 (zerop
735 (org-test-with-temp-text "[fn:1] fn"
736 (let ((org-adapt-indentation t)) (org-indent-line))
737 (org-get-indentation))))
738 (should
739 (zerop
740 (org-test-with-temp-text "* H"
741 (org-indent-line)
742 (org-get-indentation))))
743 ;; Do not indent before first headline.
744 (should
745 (zerop
746 (org-test-with-temp-text ""
747 (org-indent-line)
748 (org-get-indentation))))
749 ;; Indent according to headline level otherwise, unless
750 ;; `org-adapt-indentation' is nil.
751 (should
752 (= 2
753 (org-test-with-temp-text "* H\n<point>A"
754 (let ((org-adapt-indentation t)) (org-indent-line))
755 (org-get-indentation))))
756 (should
757 (= 2
758 (org-test-with-temp-text "* H\n<point>\nA"
759 (let ((org-adapt-indentation t)) (org-indent-line))
760 (org-get-indentation))))
761 (should
762 (zerop
763 (org-test-with-temp-text "* H\n<point>A"
764 (let ((org-adapt-indentation nil)) (org-indent-line))
765 (org-get-indentation))))
766 ;; Indenting preserves point position.
767 (should
768 (org-test-with-temp-text "* H\nA<point>B"
769 (let ((org-adapt-indentation t)) (org-indent-line))
770 (looking-at "B")))
771 ;; Do not change indentation at an item or a LaTeX environment.
772 (should
773 (= 1
774 (org-test-with-temp-text "* H\n<point> - A"
775 (let ((org-adapt-indentation t)) (org-indent-line))
776 (org-get-indentation))))
777 (should
778 (= 1
779 (org-test-with-temp-text
780 "\\begin{equation}\n <point>1+1=2\n\\end{equation}"
781 (org-indent-line)
782 (org-get-indentation))))
783 ;; On blank lines at the end of a list, indent like last element
784 ;; within it if the line is still in the list. If the last element
785 ;; is an item, indent like its contents. Otherwise, indent like the
786 ;; whole list.
787 (should
788 (= 4
789 (org-test-with-temp-text "* H\n- A\n - AA\n<point>"
790 (let ((org-adapt-indentation t)) (org-indent-line))
791 (org-get-indentation))))
792 (should
793 (= 4
794 (org-test-with-temp-text "* H\n- A\n -\n\n<point>"
795 (let ((org-adapt-indentation t)) (org-indent-line))
796 (org-get-indentation))))
797 (should
798 (zerop
799 (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n<point>"
800 (let ((org-adapt-indentation t)) (org-indent-line))
801 (org-get-indentation))))
802 (should
803 (= 4
804 (org-test-with-temp-text "* H\n- A\n - \n<point>"
805 (let ((org-adapt-indentation t)) (org-indent-line))
806 (org-get-indentation))))
807 (should
808 (= 4
809 (org-test-with-temp-text
810 "* H\n - \n #+BEGIN_SRC emacs-lisp\n t\n #+END_SRC\n<point>"
811 (let ((org-adapt-indentation t)) (org-indent-line))
812 (org-get-indentation))))
813 (should
814 (= 2
815 (org-test-with-temp-text "- A\n B\n\n<point>"
816 (let ((org-adapt-indentation nil)) (org-indent-line))
817 (org-get-indentation))))
818 (should
819 (= 2
820 (org-test-with-temp-text
821 "- A\n \begin{cases} 1 + 1\n \end{cases}\n\n<point>"
822 (let ((org-adapt-indentation nil)) (org-indent-line))
823 (org-get-indentation))))
824 ;; Likewise, on a blank line at the end of a footnote definition,
825 ;; indent at column 0 if line belongs to the definition. Otherwise,
826 ;; indent like the definition itself.
827 (should
828 (zerop
829 (org-test-with-temp-text "* H\n[fn:1] Definition\n<point>"
830 (let ((org-adapt-indentation t)) (org-indent-line))
831 (org-get-indentation))))
832 (should
833 (zerop
834 (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n<point>"
835 (let ((org-adapt-indentation t)) (org-indent-line))
836 (org-get-indentation))))
837 ;; After the end of the contents of a greater element, indent like
838 ;; the beginning of the element.
839 (should
840 (= 1
841 (org-test-with-temp-text
842 " #+BEGIN_CENTER\n Contents\n<point>#+END_CENTER"
843 (org-indent-line)
844 (org-get-indentation))))
845 ;; On blank lines after a paragraph, indent like its last non-empty
846 ;; line.
847 (should
848 (= 1
849 (org-test-with-temp-text " Paragraph\n\n<point>"
850 (org-indent-line)
851 (org-get-indentation))))
852 ;; At the first line of an element, indent like previous element's
853 ;; first line, ignoring footnotes definitions and inline tasks, or
854 ;; according to parent.
855 (should
856 (= 2
857 (org-test-with-temp-text "A\n\n B\n\nC<point>"
858 (org-indent-line)
859 (org-get-indentation))))
860 (should
861 (= 1
862 (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC<point>"
863 (org-indent-line)
864 (org-get-indentation))))
865 (should
866 (= 1
867 (org-test-with-temp-text
868 " #+BEGIN_CENTER\n<point> Contents\n#+END_CENTER"
869 (org-indent-line)
870 (org-get-indentation))))
871 ;; Within code part of a source block, use language major mode if
872 ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
873 ;; according to line above.
874 (should
875 (= 6
876 (org-test-with-temp-text
877 "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
878 (let ((org-src-tab-acts-natively t)
879 (org-edit-src-content-indentation 0))
880 (org-indent-line))
881 (org-get-indentation))))
882 (should
883 (= 1
884 (org-test-with-temp-text
885 "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
886 (let ((org-src-tab-acts-natively nil)
887 (org-edit-src-content-indentation 0))
888 (org-indent-line))
889 (org-get-indentation))))
890 ;; Otherwise, indent like the first non-blank line above.
891 (should
892 (zerop
893 (org-test-with-temp-text
894 "#+BEGIN_CENTER\nline1\n\n<point> line2\n#+END_CENTER"
895 (org-indent-line)
896 (org-get-indentation))))
897 ;; Align node properties according to `org-property-format'. Handle
898 ;; nicely empty values.
899 (should
900 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
901 (org-test-with-temp-text
902 "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
903 (let ((org-property-format "%-10s %s")) (org-indent-line))
904 (buffer-string))))
905 (should
906 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
907 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
908 (let ((org-property-format "%-10s %s")) (org-indent-line))
909 (buffer-string)))))
911 (ert-deftest test-org/indent-region ()
912 "Test `org-indent-region' specifications."
913 ;; Indent paragraph.
914 (should
915 (equal "A\nB\nC"
916 (org-test-with-temp-text " A\nB\n C"
917 (org-indent-region (point-min) (point-max))
918 (buffer-string))))
919 ;; Indent greater elements along with their contents.
920 (should
921 (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
922 (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
923 (org-indent-region (point-min) (point-max))
924 (buffer-string))))
925 ;; Ignore contents of verse blocks. Only indent block delimiters.
926 (should
927 (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
928 (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
929 (org-indent-region (point-min) (point-max))
930 (buffer-string))))
931 (should
932 (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
933 (org-test-with-temp-text " #+BEGIN_VERSE\n A\n B\n #+END_VERSE"
934 (org-indent-region (point-min) (point-max))
935 (buffer-string))))
936 ;; Indent example blocks as a single block, unless indentation
937 ;; should be preserved. In this case only indent the block markers.
938 (should
939 (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
940 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
941 (org-indent-region (point-min) (point-max))
942 (buffer-string))))
943 (should
944 (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
945 (org-test-with-temp-text " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
946 (org-indent-region (point-min) (point-max))
947 (buffer-string))))
948 (should
949 (equal "#+BEGIN_EXAMPLE -i\n A\n B\n#+END_EXAMPLE"
950 (org-test-with-temp-text
951 " #+BEGIN_EXAMPLE -i\n A\n B\n #+END_EXAMPLE"
952 (org-indent-region (point-min) (point-max))
953 (buffer-string))))
954 (should
955 (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
956 (org-test-with-temp-text
957 " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
958 (let ((org-src-preserve-indentation t))
959 (org-indent-region (point-min) (point-max)))
960 (buffer-string))))
961 ;; Treat export blocks as a whole.
962 (should
963 (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
964 (org-test-with-temp-text "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
965 (org-indent-region (point-min) (point-max))
966 (buffer-string))))
967 (should
968 (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
969 (org-test-with-temp-text
970 " #+BEGIN_EXPORT latex\n A\n B\n #+END_EXPORT"
971 (org-indent-region (point-min) (point-max))
972 (buffer-string))))
973 ;; Indent according to mode if `org-src-tab-acts-natively' is
974 ;; non-nil. Otherwise, do not indent code at all.
975 (should
976 (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
977 (org-test-with-temp-text
978 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
979 (let ((org-src-tab-acts-natively t)
980 (org-edit-src-content-indentation 0))
981 (org-indent-region (point-min) (point-max)))
982 (buffer-string))))
983 (should
984 (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
985 (org-test-with-temp-text
986 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
987 (let ((org-src-tab-acts-natively nil)
988 (org-edit-src-content-indentation 0))
989 (org-indent-region (point-min) (point-max)))
990 (buffer-string))))
991 ;; Align node properties according to `org-property-format'. Handle
992 ;; nicely empty values.
993 (should
994 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
995 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
996 (let ((org-property-format "%-10s %s")
997 (org-adapt-indentation nil))
998 (org-indent-region (point) (point-max)))
999 (buffer-string))))
1000 (should
1001 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
1002 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
1003 (let ((org-property-format "%-10s %s")
1004 (org-adapt-indentation nil))
1005 (org-indent-region (point) (point-max)))
1006 (buffer-string))))
1007 ;; Indent plain lists.
1008 (should
1009 (equal "- A\n B\n - C\n\n D"
1010 (org-test-with-temp-text "- A\n B\n - C\n\n D"
1011 (org-indent-region (point-min) (point-max))
1012 (buffer-string))))
1013 (should
1014 (equal "- A\n\n- B"
1015 (org-test-with-temp-text " - A\n\n - B"
1016 (org-indent-region (point-min) (point-max))
1017 (buffer-string))))
1018 ;; Indent footnote definitions.
1019 (should
1020 (equal "[fn:1] Definition\n\nDefinition"
1021 (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
1022 (org-indent-region (point-min) (point-max))
1023 (buffer-string))))
1024 ;; Special case: Start indenting on a blank line.
1025 (should
1026 (equal "\nParagraph"
1027 (org-test-with-temp-text "\n Paragraph"
1028 (org-indent-region (point-min) (point-max))
1029 (buffer-string)))))
1033 ;;; Editing
1035 (ert-deftest test-org/delete-indentation ()
1036 "Test `org-delete-indentation' specifications."
1037 ;; Regular test.
1038 (should (equal "foo bar"
1039 (org-test-with-temp-text
1040 "foo \n bar<point>"
1041 (org-delete-indentation)
1042 (buffer-string))))
1043 ;; With optional argument.
1044 (should (equal "foo bar"
1045 (org-test-with-temp-text
1046 "foo<point> \n bar"
1047 (org-delete-indentation t)
1048 (buffer-string))))
1049 ;; At headline text should be appended to the headline text.
1050 (should
1051 (equal"* foo bar :tag:"
1052 (let (org-auto-align-tags)
1053 (org-test-with-temp-text
1054 "* foo :tag:\n bar<point>"
1055 (org-delete-indentation)
1056 (buffer-string)))))
1057 (should
1058 (equal "* foo bar :tag:"
1059 (let (org-auto-align-tags)
1060 (org-test-with-temp-text
1061 "* foo <point>:tag:\n bar"
1062 (org-delete-indentation t)
1063 (buffer-string))))))
1065 (ert-deftest test-org/return ()
1066 "Test `org-return' specifications."
1067 ;; Regular test.
1068 (should
1069 (equal "Para\ngraph"
1070 (org-test-with-temp-text "Para<point>graph"
1071 (org-return)
1072 (buffer-string))))
1073 ;; With optional argument, indent line.
1074 (should
1075 (equal " Para\n graph"
1076 (org-test-with-temp-text " Para<point>graph"
1077 (org-return t)
1078 (buffer-string))))
1079 ;; On a table, call `org-table-next-row'.
1080 (should
1081 (org-test-with-temp-text "| <point>a |\n| b |"
1082 (org-return)
1083 (looking-at-p "b")))
1084 ;; Open link or timestamp under point when `org-return-follows-link'
1085 ;; is non-nil.
1086 (should
1087 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
1088 (let ((org-return-follows-link t)
1089 (org-link-search-must-match-exact-headline nil))
1090 (org-return))
1091 (looking-at-p "<<target>>")))
1092 (should-not
1093 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
1094 (let ((org-return-follows-link nil)) (org-return))
1095 (looking-at-p "<<target>>")))
1096 (should
1097 (org-test-with-temp-text "* [[b][a<point>]]\n* b"
1098 (let ((org-return-follows-link t)) (org-return))
1099 (looking-at-p "* b")))
1100 (should
1101 (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
1102 (let ((org-return-follows-link t)
1103 (org-link-search-must-match-exact-headline nil))
1104 (org-return))
1105 (looking-at-p "<<target>>")))
1106 (should-not
1107 (org-test-with-temp-text "Link [[target]]<point> <<target>>"
1108 (let ((org-return-follows-link t)
1109 (org-link-search-must-match-exact-headline nil))
1110 (org-return))
1111 (looking-at-p "<<target>>")))
1112 ;; When `org-return-follows-link' is non-nil, tolerate links and
1113 ;; timestamps in comments, node properties, etc.
1114 (should
1115 (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
1116 (let ((org-return-follows-link t)
1117 (org-link-search-must-match-exact-headline nil))
1118 (org-return))
1119 (looking-at-p "<<target>>")))
1120 (should-not
1121 (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
1122 (let ((org-return-follows-link nil)) (org-return))
1123 (looking-at-p "<<target>>")))
1124 (should-not
1125 (org-test-with-temp-text "# Comment [[target]]<point>\n <<target>>"
1126 (let ((org-return-follows-link t)
1127 (org-link-search-must-match-exact-headline nil))
1128 (org-return))
1129 (looking-at-p "<<target>>")))
1130 ;; `org-return-follows-link' handle multi-line lines.
1131 (should
1132 (org-test-with-temp-text
1133 "[[target][This is a very\n long description<point>]]\n <<target>>"
1134 (let ((org-return-follows-link t)
1135 (org-link-search-must-match-exact-headline nil))
1136 (org-return))
1137 (looking-at-p "<<target>>")))
1138 (should-not
1139 (org-test-with-temp-text
1140 "[[target][This is a very\n long description]]<point>\n <<target>>"
1141 (let ((org-return-follows-link t)
1142 (org-link-search-must-match-exact-headline nil))
1143 (org-return))
1144 (looking-at-p "<<target>>")))
1145 ;; However, do not open link when point is in a table.
1146 (should
1147 (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
1148 (let ((org-return-follows-link t)) (org-return))
1149 (looking-at-p "between")))
1150 ;; Special case: in a list, when indenting, do not break structure.
1151 (should
1152 (equal "- A\n B"
1153 (org-test-with-temp-text "- A <point>B"
1154 (org-return t)
1155 (buffer-string))))
1156 (should
1157 (equal "- A\n\n- B"
1158 (org-test-with-temp-text "- A\n<point>- B"
1159 (org-return t)
1160 (buffer-string))))
1161 ;; On tags part of a headline, add a newline below it instead of
1162 ;; breaking it.
1163 (should
1164 (equal "* H :tag:\n"
1165 (org-test-with-temp-text "* H :<point>tag:"
1166 (org-return)
1167 (buffer-string))))
1168 ;; Before headline text, add a newline below it instead of breaking
1169 ;; it.
1170 (should
1171 (equal "* TODO H :tag:\n"
1172 (org-test-with-temp-text "* <point>TODO H :tag:"
1173 (org-return)
1174 (buffer-string))))
1175 (should
1176 (equal "* TODO [#B] H :tag:\n"
1177 (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
1178 (org-return)
1179 (buffer-string))))
1180 (should ;TODO are case-sensitive
1181 (equal "* \nTodo"
1182 (org-test-with-temp-text "* <point>Todo"
1183 (org-return)
1184 (buffer-string))))
1185 ;; At headline text, break headline text but preserve tags.
1186 (should
1187 (equal "* TODO [#B] foo :tag:\nbar"
1188 (let (org-auto-align-tags)
1189 (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
1190 (org-return)
1191 (buffer-string)))))
1192 ;; At bol of headline insert newline.
1193 (should
1194 (equal "\n* h"
1195 (org-test-with-temp-text "<point>* h"
1196 (org-return)
1197 (buffer-string))))
1198 ;; Refuse to leave invalid headline in buffer.
1199 (should
1200 (equal "* h\n"
1201 (org-test-with-temp-text "*<point> h"
1202 (org-return)
1203 (buffer-string))))
1204 ;; Before first column or after last one in a table, split the
1205 ;; table.
1206 (should
1207 (equal "| a |\n\n| b |"
1208 (org-test-with-temp-text "| a |\n<point>| b |"
1209 (org-return)
1210 (buffer-string))))
1211 (should
1212 (equal "| a |\n\n| b |"
1213 (org-test-with-temp-text "| a |<point>\n| b |"
1214 (org-return)
1215 (buffer-string))))
1216 ;; Do not auto-fill on hitting <RET> inside a property drawer.
1217 (should
1218 (equal "* Heading\n:PROPERTIES:\n:SOME_PROP: This is a very long property value that goes beyond the fill-column. But this is inside a property drawer, so the auto-filling should be disabled.\n\n:END:"
1219 (org-test-with-temp-text "* Heading\n:PROPERTIES:\n:SOME_PROP: This is a very long property value that goes beyond the fill-column. But this is inside a property drawer, so the auto-filling should be disabled.<point>\n:END:"
1220 (setq-local fill-column 10)
1221 (auto-fill-mode 1)
1222 (org-return)
1223 (buffer-string)))))
1225 (ert-deftest test-org/meta-return ()
1226 "Test M-RET (`org-meta-return') specifications."
1227 ;; In a table field insert a row above.
1228 (should
1229 (org-test-with-temp-text "| a |"
1230 (forward-char)
1231 (org-meta-return)
1232 (forward-line -1)
1233 (looking-at "| |$")))
1234 ;; In a paragraph change current line into a header.
1235 (should
1236 (org-test-with-temp-text "a"
1237 (org-meta-return)
1238 (beginning-of-line)
1239 (looking-at "\* a$")))
1240 ;; In an item insert an item, in this case above.
1241 (should
1242 (org-test-with-temp-text "- a"
1243 (org-meta-return)
1244 (beginning-of-line)
1245 (looking-at "- $")))
1246 ;; In a drawer and item insert an item, in this case above.
1247 (should
1248 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
1249 (forward-line)
1250 (org-meta-return)
1251 (beginning-of-line)
1252 (looking-at "- $"))))
1254 (ert-deftest test-org/insert-heading ()
1255 "Test `org-insert-heading' specifications."
1256 ;; In an empty buffer, insert a new headline.
1257 (should
1258 (equal "* "
1259 (org-test-with-temp-text ""
1260 (org-insert-heading)
1261 (buffer-string))))
1262 ;; At the beginning of a line, turn it into a headline.
1263 (should
1264 (equal "* P"
1265 (org-test-with-temp-text "<point>P"
1266 (org-insert-heading)
1267 (buffer-string))))
1268 ;; In the middle of a line, split the line if allowed, otherwise,
1269 ;; insert the headline at its end.
1270 (should
1271 (equal "Para\n* graph"
1272 (org-test-with-temp-text "Para<point>graph"
1273 (let ((org-M-RET-may-split-line '((default . t))))
1274 (org-insert-heading))
1275 (buffer-string))))
1276 (should
1277 (equal "Paragraph\n* "
1278 (org-test-with-temp-text "Para<point>graph"
1279 (let ((org-M-RET-may-split-line '((default . nil))))
1280 (org-insert-heading))
1281 (buffer-string))))
1282 ;; At the beginning of a headline, create one above.
1283 (should
1284 (equal "* \n* H"
1285 (org-test-with-temp-text "* H"
1286 (org-insert-heading)
1287 (buffer-string))))
1288 ;; In the middle of a headline, split it if allowed.
1289 (should
1290 (equal "* H\n* 1\n"
1291 (org-test-with-temp-text "* H<point>1"
1292 (let ((org-M-RET-may-split-line '((headline . t))))
1293 (org-insert-heading))
1294 (buffer-string))))
1295 (should
1296 (equal "* H1\n* \n"
1297 (org-test-with-temp-text "* H<point>1"
1298 (let ((org-M-RET-may-split-line '((headline . nil))))
1299 (org-insert-heading))
1300 (buffer-string))))
1301 ;; However, splitting cannot happen on TODO keywords, priorities or
1302 ;; tags.
1303 (should
1304 (equal "* TODO H1\n* \n"
1305 (org-test-with-temp-text "* TO<point>DO H1"
1306 (let ((org-M-RET-may-split-line '((headline . t))))
1307 (org-insert-heading))
1308 (buffer-string))))
1309 (should
1310 (equal "* [#A] H1\n* \n"
1311 (org-test-with-temp-text "* [#<point>A] H1"
1312 (let ((org-M-RET-may-split-line '((headline . t))))
1313 (org-insert-heading))
1314 (buffer-string))))
1315 (should
1316 (equal "* H1 :tag:\n* \n"
1317 (org-test-with-temp-text "* H1 :ta<point>g:"
1318 (let ((org-M-RET-may-split-line '((headline . t))))
1319 (org-insert-heading))
1320 (buffer-string))))
1321 ;; New headline level depends on the level of the headline above.
1322 (should
1323 (equal "** H\n** P"
1324 (org-test-with-temp-text "** H\n<point>P"
1325 (org-insert-heading)
1326 (buffer-string))))
1327 (should
1328 (equal "** H\nPara\n** graph"
1329 (org-test-with-temp-text "** H\nPara<point>graph"
1330 (let ((org-M-RET-may-split-line '((default . t))))
1331 (org-insert-heading))
1332 (buffer-string))))
1333 (should
1334 (equal "** \n** H"
1335 (org-test-with-temp-text "** H"
1336 (org-insert-heading)
1337 (buffer-string))))
1338 ;; When called with one universal argument, insert a new headline at
1339 ;; the end of the current subtree, independently on the position of
1340 ;; point.
1341 (should
1342 (equal
1343 "* H1\n** H2\n* \n"
1344 (org-test-with-temp-text "* H1\n** H2"
1345 (let ((org-insert-heading-respect-content nil))
1346 (org-insert-heading '(4)))
1347 (buffer-string))))
1348 (should
1349 (equal
1350 "* H1\n** H2\n* \n"
1351 (org-test-with-temp-text "* H<point>1\n** H2"
1352 (let ((org-insert-heading-respect-content nil))
1353 (org-insert-heading '(4)))
1354 (buffer-string))))
1355 ;; When called with two universal arguments, insert a new headline
1356 ;; at the end of the grandparent subtree.
1357 (should
1358 (equal "* H1\n** H3\n- item\n** H2\n** \n"
1359 (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
1360 (let ((org-insert-heading-respect-content nil))
1361 (org-insert-heading '(16)))
1362 (buffer-string))))
1363 ;; When optional TOP-LEVEL argument is non-nil, always insert
1364 ;; a level 1 heading.
1365 (should
1366 (equal "* H1\n** H2\n* \n"
1367 (org-test-with-temp-text "* H1\n** H2<point>"
1368 (org-insert-heading nil nil t)
1369 (buffer-string))))
1370 (should
1371 (equal "* H1\n- item\n* "
1372 (org-test-with-temp-text "* H1\n- item<point>"
1373 (org-insert-heading nil nil t)
1374 (buffer-string))))
1375 ;; Obey `org-blank-before-new-entry'.
1376 (should
1377 (equal "* H1\n\n* \n"
1378 (org-test-with-temp-text "* H1<point>"
1379 (let ((org-blank-before-new-entry '((heading . t))))
1380 (org-insert-heading))
1381 (buffer-string))))
1382 (should
1383 (equal "* H1\n* \n"
1384 (org-test-with-temp-text "* H1<point>"
1385 (let ((org-blank-before-new-entry '((heading . nil))))
1386 (org-insert-heading))
1387 (buffer-string))))
1388 (should
1389 (equal "* H1\n* H2\n* \n"
1390 (org-test-with-temp-text "* H1\n* H2<point>"
1391 (let ((org-blank-before-new-entry '((heading . auto))))
1392 (org-insert-heading))
1393 (buffer-string))))
1394 (should
1395 (equal "* H1\n\n* H2\n\n* \n"
1396 (org-test-with-temp-text "* H1\n\n* H2<point>"
1397 (let ((org-blank-before-new-entry '((heading . auto))))
1398 (org-insert-heading))
1399 (buffer-string))))
1400 ;; Corner case: correctly insert a headline after an empty one.
1401 (should
1402 (equal "* \n* \n"
1403 (org-test-with-temp-text "* <point>"
1404 (org-insert-heading)
1405 (buffer-string))))
1406 (should
1407 (org-test-with-temp-text "* <point>\n"
1408 (org-insert-heading)
1409 (looking-at-p "\n\\'")))
1410 ;; Do not insert spurious headlines when inserting a new headline.
1411 (should
1412 (equal "* H1\n* H2\n* \n"
1413 (org-test-with-temp-text "* H1\n* H2<point>\n"
1414 (org-insert-heading)
1415 (buffer-string))))
1416 ;; Preserve visibility at beginning of line. In particular, when
1417 ;; removing spurious blank lines, do not visually merge heading with
1418 ;; the line visible above.
1419 (should-not
1420 (org-test-with-temp-text "* H1<point>\nContents\n\n* H2\n"
1421 (org-overview)
1422 (let ((org-blank-before-new-entry '((heading . nil))))
1423 (org-insert-heading '(4)))
1424 (invisible-p (line-end-position 0))))
1425 ;; Properly handle empty lines when forcing a headline below current
1426 ;; one.
1427 (should
1428 (equal "* H1\n\n* H\n\n* \n"
1429 (org-test-with-temp-text "* H1\n\n* H<point>"
1430 (let ((org-blank-before-new-entry '((heading . t))))
1431 (org-insert-heading '(4))
1432 (buffer-string))))))
1434 (ert-deftest test-org/insert-todo-heading-respect-content ()
1435 "Test `org-insert-todo-heading-respect-content' specifications."
1436 ;; Create a TODO heading.
1437 (should
1438 (org-test-with-temp-text "* H1\n Body"
1439 (org-insert-todo-heading-respect-content)
1440 (nth 2 (org-heading-components))))
1441 ;; Add headline at the end of the first subtree
1442 (should
1443 (equal
1444 "* TODO \n"
1445 (org-test-with-temp-text "* H1\nH1Body<point>\n** H2\nH2Body"
1446 (org-insert-todo-heading-respect-content)
1447 (buffer-substring-no-properties (line-beginning-position) (point-max)))))
1448 ;; In a list, do not create a new item.
1449 (should
1450 (equal
1451 "* TODO \n"
1452 (org-test-with-temp-text "* H\n- an item\n- another one"
1453 (search-forward "an ")
1454 (org-insert-todo-heading-respect-content)
1455 (buffer-substring-no-properties (line-beginning-position) (point-max)))))
1456 ;; Use the same TODO keyword as current heading.
1457 (should
1458 (equal
1459 "* TODO \n"
1460 (org-test-with-temp-text "* TODO\n** WAITING\n"
1461 (org-insert-todo-heading-respect-content)
1462 (buffer-substring-no-properties (line-beginning-position) (point-max))))))
1464 (ert-deftest test-org/clone-with-time-shift ()
1465 "Test `org-clone-subtree-with-time-shift'."
1466 ;; Raise an error before first heading.
1467 (should-error
1468 (org-test-with-temp-text ""
1469 (org-clone-subtree-with-time-shift 1)))
1470 ;; Raise an error on invalid number of clones.
1471 (should-error
1472 (org-test-with-temp-text "* Clone me"
1473 (org-clone-subtree-with-time-shift -1)))
1474 ;; Clone non-repeating once.
1475 (should
1476 (equal "\
1477 * H1\n<2015-06-21>
1478 * H1\n<2015-06-23>
1480 (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
1481 (org-clone-subtree-with-time-shift 1 "+2d")
1482 (replace-regexp-in-string
1483 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1484 nil nil 1))))
1485 ;; Clone repeating once.
1486 (should
1487 (equal "\
1488 * H1\n<2015-06-21>
1489 * H1\n<2015-06-23>
1490 * H1\n<2015-06-25 +1w>
1492 (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
1493 (org-clone-subtree-with-time-shift 1 "+2d")
1494 (replace-regexp-in-string
1495 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1496 nil nil 1))))
1497 ;; Clone non-repeating zero times.
1498 (should
1499 (equal "\
1500 * H1\n<2015-06-21>
1502 (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
1503 (org-clone-subtree-with-time-shift 0 "+2d")
1504 (replace-regexp-in-string
1505 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1506 nil nil 1))))
1507 ;; Clone repeating "zero" times.
1508 (should
1509 (equal "\
1510 * H1\n<2015-06-21>
1511 * H1\n<2015-06-23 +1w>
1513 (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
1514 (org-clone-subtree-with-time-shift 0 "+2d")
1515 (replace-regexp-in-string
1516 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1517 nil nil 1))))
1518 ;; Clone with blank SHIFT argument.
1519 (should
1520 (string-prefix-p
1521 "* H <2012-03-29"
1522 (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
1523 (org-clone-subtree-with-time-shift 1 "")
1524 (buffer-substring-no-properties (line-beginning-position 2)
1525 (line-end-position 2)))))
1526 ;; Find time stamps before point. If SHIFT is not specified, ask
1527 ;; for a time shift.
1528 (should
1529 (string-prefix-p
1530 "* H <2012-03-30"
1531 (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
1532 (org-clone-subtree-with-time-shift 1 "+1d")
1533 (buffer-substring-no-properties (line-beginning-position 2)
1534 (line-end-position 2)))))
1535 (should
1536 (string-prefix-p
1537 "* H <2014-03-05"
1538 (org-test-with-temp-text "* H <2014-03-04 Tue><point>"
1539 (cl-letf (((symbol-function 'read-from-minibuffer)
1540 (lambda (&rest args) "+1d")))
1541 (org-clone-subtree-with-time-shift 1))
1542 (buffer-substring-no-properties (line-beginning-position 2)
1543 (line-end-position 2))))))
1546 ;;; Fixed-Width Areas
1548 (ert-deftest test-org/toggle-fixed-width ()
1549 "Test `org-toggle-fixed-width' specifications."
1550 ;; No region: Toggle on fixed-width marker in paragraphs.
1551 (should
1552 (equal ": A"
1553 (org-test-with-temp-text "A"
1554 (org-toggle-fixed-width)
1555 (buffer-string))))
1556 ;; No region: Toggle off fixed-width markers in fixed-width areas.
1557 (should
1558 (equal "A"
1559 (org-test-with-temp-text ": A"
1560 (org-toggle-fixed-width)
1561 (buffer-string))))
1562 ;; No region: Toggle on marker in blank lines after elements or just
1563 ;; after a headline.
1564 (should
1565 (equal "* H\n: "
1566 (org-test-with-temp-text "* H\n"
1567 (forward-line)
1568 (org-toggle-fixed-width)
1569 (buffer-string))))
1570 (should
1571 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
1572 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
1573 (goto-char (point-max))
1574 (org-toggle-fixed-width)
1575 (buffer-string))))
1576 ;; No region: Toggle on marker in front of one line elements (e.g.,
1577 ;; headlines, clocks)
1578 (should
1579 (equal ": * Headline"
1580 (org-test-with-temp-text "* Headline"
1581 (org-toggle-fixed-width)
1582 (buffer-string))))
1583 (should
1584 (equal ": #+KEYWORD: value"
1585 (org-test-with-temp-text "#+KEYWORD: value"
1586 (org-toggle-fixed-width)
1587 (buffer-string))))
1588 ;; No region: error in other situations.
1589 (should-error
1590 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
1591 (forward-line)
1592 (org-toggle-fixed-width)
1593 (buffer-string)))
1594 ;; No region: Indentation is preserved.
1595 (should
1596 (equal "- A\n : B"
1597 (org-test-with-temp-text "- A\n B"
1598 (forward-line)
1599 (org-toggle-fixed-width)
1600 (buffer-string))))
1601 ;; Region: If it contains only fixed-width elements and blank lines,
1602 ;; toggle off fixed-width markup.
1603 (should
1604 (equal "A\n\nB"
1605 (org-test-with-temp-text ": A\n\n: B"
1606 (transient-mark-mode 1)
1607 (push-mark (point) t t)
1608 (goto-char (point-max))
1609 (org-toggle-fixed-width)
1610 (buffer-string))))
1611 ;; Region: If it contains anything else, toggle on fixed-width but
1612 ;; not on fixed-width areas.
1613 (should
1614 (equal ": A\n: \n: B\n: \n: C"
1615 (org-test-with-temp-text "A\n\n: B\n\nC"
1616 (transient-mark-mode 1)
1617 (push-mark (point) t t)
1618 (goto-char (point-max))
1619 (org-toggle-fixed-width)
1620 (buffer-string))))
1621 ;; Region: Ignore blank lines at its end, unless it contains only
1622 ;; such lines.
1623 (should
1624 (equal ": A\n\n"
1625 (org-test-with-temp-text "A\n\n"
1626 (transient-mark-mode 1)
1627 (push-mark (point) t t)
1628 (goto-char (point-max))
1629 (org-toggle-fixed-width)
1630 (buffer-string))))
1631 (should
1632 (equal ": \n: \n"
1633 (org-test-with-temp-text "\n\n"
1634 (transient-mark-mode 1)
1635 (push-mark (point) t t)
1636 (goto-char (point-max))
1637 (org-toggle-fixed-width)
1638 (buffer-string)))))
1642 ;;; Headline
1644 (ert-deftest test-org/get-heading ()
1645 "Test `org-get-heading' specifications."
1646 ;; Return current heading, even if point is not on it.
1647 (should
1648 (equal "H"
1649 (org-test-with-temp-text "* H"
1650 (org-get-heading))))
1651 (should
1652 (equal "H"
1653 (org-test-with-temp-text "* H\nText<point>"
1654 (org-get-heading))))
1655 ;; Without any optional argument, return TODO keyword, priority
1656 ;; cookie, COMMENT keyword and tags.
1657 (should
1658 (equal "TODO H"
1659 (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
1660 (org-get-heading))))
1661 (should
1662 (equal "[#A] H"
1663 (org-test-with-temp-text "* [#A] H"
1664 (org-get-heading))))
1665 (should
1666 (equal "COMMENT H"
1667 (org-test-with-temp-text "* COMMENT H"
1668 (org-get-heading))))
1669 (should
1670 (equal "H :tag:"
1671 (org-test-with-temp-text "* H :tag:"
1672 (org-get-heading))))
1673 ;; With NO-TAGS argument, ignore tags.
1674 (should
1675 (equal "TODO H"
1676 (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
1677 (org-get-heading t))))
1678 (should
1679 (equal "H"
1680 (org-test-with-temp-text "* H :tag:"
1681 (org-get-heading t))))
1682 ;; With NO-TODO, ignore TODO keyword.
1683 (should
1684 (equal "H"
1685 (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
1686 (org-get-heading nil t))))
1687 (should
1688 (equal "H :tag:"
1689 (org-test-with-temp-text "* H :tag:"
1690 (org-get-heading nil t))))
1691 ;; TODO keywords are case-sensitive.
1692 (should
1693 (equal "Todo H"
1694 (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
1695 (org-get-heading nil t))))
1696 ;; With NO-PRIORITY, ignore priority.
1697 (should
1698 (equal "H"
1699 (org-test-with-temp-text "* [#A] H"
1700 (org-get-heading nil nil t))))
1701 (should
1702 (equal "H"
1703 (org-test-with-temp-text "* H"
1704 (org-get-heading nil nil t))))
1705 (should
1706 (equal "TODO H"
1707 (org-test-with-temp-text "* TODO [#A] H"
1708 (org-get-heading nil nil t))))
1709 ;; With NO-COMMENT, ignore COMMENT keyword.
1710 (should
1711 (equal "H"
1712 (org-test-with-temp-text "* COMMENT H"
1713 (org-get-heading nil nil nil t))))
1714 (should
1715 (equal "H"
1716 (org-test-with-temp-text "* H"
1717 (org-get-heading nil nil nil t))))
1718 (should
1719 (equal "TODO [#A] H"
1720 (org-test-with-temp-text "* TODO [#A] COMMENT H"
1721 (org-get-heading nil nil nil t))))
1722 ;; On an empty headline, return value is consistent.
1723 (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
1724 (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
1725 (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
1726 (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil t))))
1727 (should
1728 (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil nil t)))))
1730 (ert-deftest test-org/in-commented-heading-p ()
1731 "Test `org-in-commented-heading-p' specifications."
1732 ;; Commented headline.
1733 (should
1734 (org-test-with-temp-text "* COMMENT Headline\nBody"
1735 (goto-char (point-max))
1736 (org-in-commented-heading-p)))
1737 ;; Commented ancestor.
1738 (should
1739 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1740 (goto-char (point-max))
1741 (org-in-commented-heading-p)))
1742 ;; Comment keyword is case-sensitive.
1743 (should-not
1744 (org-test-with-temp-text "* Comment Headline\nBody"
1745 (goto-char (point-max))
1746 (org-in-commented-heading-p)))
1747 ;; Keyword is standalone.
1748 (should-not
1749 (org-test-with-temp-text "* COMMENTHeadline\nBody"
1750 (goto-char (point-max))
1751 (org-in-commented-heading-p)))
1752 ;; Optional argument.
1753 (should-not
1754 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1755 (goto-char (point-max))
1756 (org-in-commented-heading-p t))))
1758 (ert-deftest test-org/entry-blocked-p ()
1759 ;; Check other dependencies.
1760 (should
1761 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
1762 (let ((org-enforce-todo-dependencies t)
1763 (org-blocker-hook
1764 '(org-block-todo-from-children-or-siblings-or-parent)))
1765 (org-entry-blocked-p))))
1766 (should-not
1767 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
1768 (let ((org-enforce-todo-dependencies t)
1769 (org-blocker-hook
1770 '(org-block-todo-from-children-or-siblings-or-parent)))
1771 (org-entry-blocked-p))))
1772 ;; Entry without a TODO keyword or with a DONE keyword cannot be
1773 ;; blocked.
1774 (should-not
1775 (org-test-with-temp-text "* Blocked\n** TODO one"
1776 (let ((org-enforce-todo-dependencies t)
1777 (org-blocker-hook
1778 '(org-block-todo-from-children-or-siblings-or-parent)))
1779 (org-entry-blocked-p))))
1780 (should-not
1781 (org-test-with-temp-text "* DONE Blocked\n** TODO one"
1782 (let ((org-enforce-todo-dependencies t)
1783 (org-blocker-hook
1784 '(org-block-todo-from-children-or-siblings-or-parent)))
1785 (org-entry-blocked-p))))
1786 ;; Follow :ORDERED: specifications.
1787 (should
1788 (org-test-with-temp-text
1789 "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
1790 (let ((org-enforce-todo-dependencies t)
1791 (org-blocker-hook
1792 '(org-block-todo-from-children-or-siblings-or-parent)))
1793 (org-entry-blocked-p))))
1794 (should-not
1795 (org-test-with-temp-text
1796 "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
1797 (let ((org-enforce-todo-dependencies t)
1798 (org-blocker-hook
1799 '(org-block-todo-from-children-or-siblings-or-parent)))
1800 (org-entry-blocked-p)))))
1802 (ert-deftest test-org/get-outline-path ()
1803 "Test `org-get-outline-path' specifications."
1804 ;; Top-level headlines have no outline path.
1805 (should-not
1806 (org-test-with-temp-text "* H"
1807 (org-get-outline-path)))
1808 ;; Otherwise, outline path is the path leading to the headline.
1809 (should
1810 (equal '("H")
1811 (org-test-with-temp-text "* H\n** S<point>"
1812 (org-get-outline-path))))
1813 ;; Find path even when point is not on a headline.
1814 (should
1815 (equal '("H")
1816 (org-test-with-temp-text "* H\n** S\nText<point>"
1817 (org-get-outline-path))))
1818 ;; TODO keywords, tags and statistics cookies are ignored.
1819 (should
1820 (equal '("H")
1821 (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
1822 (org-get-outline-path))))
1823 ;; Links are replaced with their description or their path.
1824 (should
1825 (equal '("Org")
1826 (org-test-with-temp-text
1827 "* [[https://orgmode.org][Org]]\n** S<point>"
1828 (org-get-outline-path))))
1829 (should
1830 (equal '("https://orgmode.org")
1831 (org-test-with-temp-text
1832 "* [[https://orgmode.org]]\n** S<point>"
1833 (org-get-outline-path))))
1834 ;; When WITH-SELF is non-nil, include current heading.
1835 (should
1836 (equal '("H")
1837 (org-test-with-temp-text "* H"
1838 (org-get-outline-path t))))
1839 (should
1840 (equal '("H" "S")
1841 (org-test-with-temp-text "* H\n** S\nText<point>"
1842 (org-get-outline-path t))))
1843 ;; Using cache is transparent to the user.
1844 (should
1845 (equal '("H")
1846 (org-test-with-temp-text "* H\n** S<point>"
1847 (setq org-outline-path-cache nil)
1848 (org-get-outline-path nil t))))
1849 ;; Do not corrupt cache when finding outline path in distant part of
1850 ;; the buffer.
1851 (should
1852 (equal '("H2")
1853 (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
1854 (setq org-outline-path-cache nil)
1855 (org-get-outline-path nil t)
1856 (search-forward "S2")
1857 (org-get-outline-path nil t))))
1858 ;; Do not choke on empty headlines.
1859 (should
1860 (org-test-with-temp-text "* H\n** <point>"
1861 (org-get-outline-path)))
1862 (should
1863 (org-test-with-temp-text "* \n** H<point>"
1864 (org-get-outline-path))))
1866 (ert-deftest test-org/format-outline-path ()
1867 "Test `org-format-outline-path' specifications."
1868 (should
1869 (string= (org-format-outline-path (list "one" "two" "three"))
1870 "one/two/three"))
1871 ;; Empty path.
1872 (should
1873 (string= (org-format-outline-path '())
1874 ""))
1875 (should
1876 (string= (org-format-outline-path '(nil))
1877 ""))
1878 ;; Empty path and prefix.
1879 (should
1880 (string= (org-format-outline-path '() nil ">>")
1881 ">>"))
1882 ;; Trailing whitespace in headings.
1883 (should
1884 (string= (org-format-outline-path (list "one\t" "tw o " "three "))
1885 "one/tw o/three"))
1886 ;; Non-default prefix and separators.
1887 (should
1888 (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
1889 ">>|one|two|three"))
1890 ;; Truncate.
1891 (should
1892 (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
1893 "one/two/.."))
1894 ;; Give a very narrow width.
1895 (should
1896 (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
1897 "on"))
1898 ;; Give a prefix that extends beyond the width.
1899 (should
1900 (string= (org-format-outline-path (list "one" "two" "three" "four") 10
1901 ">>>>>>>>>>")
1902 ">>>>>>>>..")))
1904 (ert-deftest test-org/map-entries ()
1905 "Test `org-map-entries' specifications."
1906 ;; Full match.
1907 (should
1908 (equal '(1 11)
1909 (org-test-with-temp-text "* Level 1\n** Level 2"
1910 (org-map-entries #'point))))
1911 ;; Level match.
1912 (should
1913 (equal '(1)
1914 (org-test-with-temp-text "* Level 1\n** Level 2"
1915 (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
1916 (should
1917 (equal '(11)
1918 (org-test-with-temp-text "* Level 1\n** Level 2"
1919 (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
1920 ;; Tag match.
1921 (should
1922 (equal '(11)
1923 (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
1924 (org-map-entries #'point "yes"))))
1925 (should
1926 (equal '(14)
1927 (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
1928 (org-map-entries #'point "+yes-a"))))
1929 (should
1930 (equal '(11 23)
1931 (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
1932 (org-map-entries #'point "{yes?}"))))
1933 ;; Priority match.
1934 (should
1935 (equal '(1)
1936 (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
1937 (org-map-entries #'point "PRIORITY=\"A\""))))
1938 ;; Date match.
1939 (should
1940 (equal '(36)
1941 (org-test-with-temp-text "
1942 * H1
1943 SCHEDULED: <2012-03-29 thu.>
1944 * H2
1945 SCHEDULED: <2014-03-04 tue.>"
1946 (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
1947 (should
1948 (equal '(2)
1949 (org-test-with-temp-text "
1950 * H1
1951 SCHEDULED: <2012-03-29 thu.>
1952 * H2
1953 SCHEDULED: <2014-03-04 tue.>"
1954 (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
1955 ;; Regular property match.
1956 (should
1957 (equal '(2)
1958 (org-test-with-temp-text "
1959 * H1
1960 :PROPERTIES:
1961 :TEST: 1
1962 :END:
1963 * H2
1964 :PROPERTIES:
1965 :TEST: 2
1966 :END:"
1967 (org-map-entries #'point "TEST=1"))))
1968 ;; Multiple criteria.
1969 (should
1970 (equal '(23)
1971 (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
1972 (let (org-odd-levels-only
1973 (org-use-tag-inheritance nil))
1974 (org-map-entries #'point "yes+LEVEL=1")))))
1975 ;; "or" criteria.
1976 (should
1977 (equal '(12 24)
1978 (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
1979 (let (org-odd-levels-only)
1980 (org-map-entries #'point "LEVEL=2|no")))))
1981 (should
1982 (equal '(1 12)
1983 (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
1984 (let (org-odd-levels-only)
1985 (org-map-entries #'point "yes|no")))))
1986 ;; "and" criteria.
1987 (should
1988 (equal '(22)
1989 (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
1990 (let (org-odd-levels-only)
1991 (org-map-entries #'point "yes&no"))))))
1993 (ert-deftest test-org/edit-headline ()
1994 "Test `org-edit-headline' specifications."
1995 (should
1996 (equal "* B"
1997 (org-test-with-temp-text "* A"
1998 (org-edit-headline "B")
1999 (buffer-string))))
2000 ;; Handle empty headings.
2001 (should
2002 (equal "* "
2003 (org-test-with-temp-text "* A"
2004 (org-edit-headline "")
2005 (buffer-string))))
2006 (should
2007 (equal "* A"
2008 (org-test-with-temp-text "* "
2009 (org-edit-headline "A")
2010 (buffer-string))))
2011 ;; Handle TODO keywords and priority cookies.
2012 (should
2013 (equal "* TODO B"
2014 (org-test-with-temp-text "* TODO A"
2015 (org-edit-headline "B")
2016 (buffer-string))))
2017 (should
2018 (equal "* [#A] B"
2019 (org-test-with-temp-text "* [#A] A"
2020 (org-edit-headline "B")
2021 (buffer-string))))
2022 (should
2023 (equal "* TODO [#A] B"
2024 (org-test-with-temp-text "* TODO [#A] A"
2025 (org-edit-headline "B")
2026 (buffer-string))))
2027 ;; Handle tags.
2028 (equal "* B :tag:"
2029 (org-test-with-temp-text "* A :tag:"
2030 (let ((org-tags-column 4)) (org-edit-headline "B"))
2031 (buffer-string))))
2035 ;;; Keywords
2037 (ert-deftest test-org/set-regexps-and-options ()
2038 "Test `org-set-regexps-and-options' specifications."
2039 ;; TAGS keyword.
2040 (should
2041 (equal '(("A"))
2042 (let ((org-tag-alist '(("A")))
2043 (org-tag-persistent-alist nil))
2044 (org-test-with-temp-text ""
2045 (org-mode-restart)
2046 org-current-tag-alist))))
2047 (should
2048 (equal '(("B"))
2049 (let ((org-tag-alist '(("A")))
2050 (org-tag-persistent-alist nil))
2051 (org-test-with-temp-text "#+TAGS: B"
2052 (org-mode-restart)
2053 org-current-tag-alist))))
2054 (should
2055 (equal '(("C") ("B"))
2056 (let ((org-tag-alist '(("A")))
2057 (org-tag-persistent-alist '(("C"))))
2058 (org-test-with-temp-text "#+TAGS: B"
2059 (org-mode-restart)
2060 org-current-tag-alist))))
2061 (should
2062 (equal '(("B"))
2063 (let ((org-tag-alist '(("A")))
2064 (org-tag-persistent-alist '(("C"))))
2065 (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
2066 (org-mode-restart)
2067 org-current-tag-alist))))
2068 (should
2069 (equal '(("A" . ?a) ("B") ("C"))
2070 (let ((org-tag-persistant-alist nil))
2071 (org-test-with-temp-text "#+TAGS: A(a) B C"
2072 (org-mode-restart)
2073 org-current-tag-alist))))
2074 (should
2075 (equal '(("A") (:newline) ("B"))
2076 (let ((org-tag-persistent-alist nil))
2077 (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
2078 (org-mode-restart)
2079 org-current-tag-alist))))
2080 (should
2081 (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
2082 (let ((org-tag-persistent-alist nil))
2083 (org-test-with-temp-text "#+TAGS: { A B } C"
2084 (org-mode-restart)
2085 org-current-tag-alist))))
2086 (should
2087 (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
2088 (let ((org-tag-persistent-alist nil))
2089 (org-test-with-temp-text "#+TAGS: { A : B C }"
2090 (org-mode-restart)
2091 org-current-tag-alist))))
2092 (should
2093 (equal '(("A" "B" "C"))
2094 (let ((org-tag-persistent-alist nil))
2095 (org-test-with-temp-text "#+TAGS: { A : B C }"
2096 (org-mode-restart)
2097 org-tag-groups-alist))))
2098 (should
2099 (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
2100 (let ((org-tag-persistent-alist nil))
2101 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
2102 (org-mode-restart)
2103 org-current-tag-alist))))
2104 (should
2105 (equal '(("A" "B" "C"))
2106 (let ((org-tag-persistent-alist nil))
2107 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
2108 (org-mode-restart)
2109 org-tag-groups-alist))))
2110 ;; FILETAGS keyword.
2111 (should
2112 (equal '("A" "B" "C")
2113 (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
2114 (org-mode-restart)
2115 org-file-tags)))
2116 ;; PROPERTY keyword. Property names are case-insensitive.
2117 (should
2118 (equal "foo=1"
2119 (org-test-with-temp-text "#+PROPERTY: var foo=1"
2120 (org-mode-restart)
2121 (cdr (assoc "var" org-file-properties)))))
2122 (should
2123 (equal
2124 "foo=1 bar=2"
2125 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
2126 (org-mode-restart)
2127 (cdr (assoc "var" org-file-properties)))))
2128 (should
2129 (equal
2130 "foo=1 bar=2"
2131 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
2132 (org-mode-restart)
2133 (cdr (assoc "var" org-file-properties)))))
2134 ;; ARCHIVE keyword.
2135 (should
2136 (equal "%s_done::"
2137 (org-test-with-temp-text "#+ARCHIVE: %s_done::"
2138 (org-mode-restart)
2139 org-archive-location)))
2140 ;; CATEGORY keyword.
2141 (should
2142 (eq 'test
2143 (org-test-with-temp-text "#+CATEGORY: test"
2144 (org-mode-restart)
2145 org-category)))
2146 (should
2147 (equal "test"
2148 (org-test-with-temp-text "#+CATEGORY: test"
2149 (org-mode-restart)
2150 (cdr (assoc "CATEGORY" org-file-properties)))))
2151 ;; COLUMNS keyword.
2152 (should
2153 (equal "%25ITEM %TAGS %PRIORITY %TODO"
2154 (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
2155 (org-mode-restart)
2156 org-columns-default-format)))
2157 ;; CONSTANTS keyword. Constants names are case sensitive.
2158 (should
2159 (equal '("299792458." "3.14")
2160 (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
2161 (org-mode-restart)
2162 (mapcar
2163 (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
2164 '("c" "pi")))))
2165 (should
2166 (equal "3.14"
2167 (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
2168 (org-mode-restart)
2169 (cdr (assoc "pi" org-table-formula-constants-local)))))
2170 (should
2171 (equal "22/7"
2172 (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
2173 (org-mode-restart)
2174 (cdr (assoc "PI" org-table-formula-constants-local)))))
2175 ;; LINK keyword.
2176 (should
2177 (equal
2178 '("url1" "url2")
2179 (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
2180 (org-mode-restart)
2181 (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
2182 '("a" "b")))))
2183 ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
2184 (should
2185 (equal
2186 '(?X ?Z ?Y)
2187 (org-test-with-temp-text "#+PRIORITIES: X Z Y"
2188 (org-mode-restart)
2189 (list org-highest-priority org-lowest-priority org-default-priority))))
2190 (should
2191 (equal
2192 '(?A ?C ?B)
2193 (org-test-with-temp-text "#+PRIORITIES: X Z"
2194 (org-mode-restart)
2195 (list org-highest-priority org-lowest-priority org-default-priority))))
2196 ;; STARTUP keyword.
2197 (should
2198 (equal '(t t)
2199 (org-test-with-temp-text "#+STARTUP: fold odd"
2200 (org-mode-restart)
2201 (list org-startup-folded org-odd-levels-only))))
2202 ;; TODO keywords.
2203 (should
2204 (equal '(("A" "B") ("C"))
2205 (org-test-with-temp-text "#+TODO: A B | C"
2206 (org-mode-restart)
2207 (list org-not-done-keywords org-done-keywords))))
2208 (should
2209 (equal '(("A" "C") ("B" "D"))
2210 (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
2211 (org-mode-restart)
2212 (list org-not-done-keywords org-done-keywords))))
2213 (should
2214 (equal '(("A" "B") ("C"))
2215 (org-test-with-temp-text "#+TYP_TODO: A B | C"
2216 (org-mode-restart)
2217 (list org-not-done-keywords org-done-keywords))))
2218 (should
2219 (equal '((:startgroup) ("A" . ?a) (:endgroup))
2220 (org-test-with-temp-text "#+TODO: A(a)"
2221 (org-mode-restart)
2222 org-todo-key-alist)))
2223 (should
2224 (equal '(("D" note nil) ("C" time nil) ("B" note time))
2225 (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
2226 (org-mode-restart)
2227 org-todo-log-states)))
2228 ;; Enter SETUPFILE keyword.
2229 (should
2230 (equal "1"
2231 (org-test-with-temp-text
2232 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
2233 (org-mode-restart)
2234 (cdr (assoc "a" org-file-properties))))))
2238 ;;; Links
2240 ;;;; Coderefs
2242 (ert-deftest test-org/coderef ()
2243 "Test coderef links specifications."
2244 (should
2245 (org-test-with-temp-text "
2246 #+BEGIN_SRC emacs-lisp
2247 \(+ 1 1) (ref:sc)
2248 #+END_SRC
2249 \[[(sc)<point>]]"
2250 (org-open-at-point)
2251 (looking-at "(ref:sc)")))
2252 ;; Find coderef even with alternate label format.
2253 (should
2254 (org-test-with-temp-text "
2255 #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
2256 \(+ 1 1) {ref:sc}
2257 #+END_SRC
2258 \[[(sc)<point>]]"
2259 (org-open-at-point)
2260 (looking-at "{ref:sc}"))))
2262 ;;;; Custom ID
2264 (ert-deftest test-org/custom-id ()
2265 "Test custom ID links specifications."
2266 (should
2267 (org-test-with-temp-text
2268 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
2269 (org-open-at-point)
2270 (looking-at-p "\\* H1")))
2271 ;; Throw an error on false positives.
2272 (should-error
2273 (org-test-with-temp-text
2274 "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
2275 (org-open-at-point)
2276 (looking-at-p "\\* H1"))))
2278 ;;;; Fuzzy Links
2280 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
2281 ;; a named element (#+name: text) and to headlines (* Text).
2283 (ert-deftest test-org/fuzzy-links ()
2284 "Test fuzzy links specifications."
2285 ;; Fuzzy link goes in priority to a matching target.
2286 (should
2287 (org-test-with-temp-text
2288 "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
2289 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
2290 (looking-at "<<Test>>")))
2291 ;; Then fuzzy link points to an element with a given name.
2292 (should
2293 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
2294 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
2295 (looking-at "#\\+NAME: Test")))
2296 ;; A target still lead to a matching headline otherwise.
2297 (should
2298 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
2299 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
2300 (looking-at "\\* Head2")))
2301 ;; With a leading star in link, enforce heading match.
2302 (should
2303 (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
2304 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
2305 (looking-at "\\* Test")))
2306 ;; With a leading star in link, enforce exact heading match, even
2307 ;; with `org-link-search-must-match-exact-headline' set to nil.
2308 (should-error
2309 (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
2310 (let ((org-link-search-must-match-exact-headline nil))
2311 (org-open-at-point))))
2312 ;; Handle non-nil `org-link-search-must-match-exact-headline'.
2313 (should
2314 (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
2315 (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
2316 (looking-at "\\* Test")))
2317 (should
2318 (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
2319 (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
2320 (looking-at "\\* Test")))
2321 ;; Heading match should not care about spaces, cookies, TODO
2322 ;; keywords, priorities, and tags. However, TODO keywords are
2323 ;; case-sensitive.
2324 (should
2325 (let ((first-line
2326 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
2327 (org-test-with-temp-text
2328 (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
2329 (let ((org-link-search-must-match-exact-headline nil)
2330 (org-todo-regexp "TODO"))
2331 (org-open-at-point))
2332 (looking-at (regexp-quote first-line)))))
2333 (should-error
2334 (org-test-with-temp-text "** todo Test 1 2\nFoo Bar\n<point>[[*Test 1 2]]"
2335 (let ((org-link-search-must-match-exact-headline nil)
2336 (org-todo-regexp "TODO"))
2337 (org-open-at-point))))
2338 ;; Heading match should still be exact.
2339 (should-error
2340 (org-test-with-temp-text "
2341 ** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent:
2342 Foo Bar
2343 <point>[[*Test 1]]"
2344 (let ((org-link-search-must-match-exact-headline nil)
2345 (org-todo-regexp "TODO"))
2346 (org-open-at-point))))
2347 (should
2348 (org-test-with-temp-text "* Test 1 2 3\n** Test 1 2\n<point>[[*Test 1 2]]"
2349 (let ((org-link-search-must-match-exact-headline nil)
2350 (org-todo-regexp "TODO"))
2351 (org-open-at-point))
2352 (looking-at-p (regexp-quote "** Test 1 2"))))
2353 ;; Heading match ignores COMMENT keyword.
2354 (should
2355 (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
2356 (org-open-at-point)
2357 (looking-at "\\* COMMENT Test")))
2358 (should
2359 (org-test-with-temp-text "[[*Test]]\n* TODO COMMENT Test"
2360 (org-open-at-point)
2361 (looking-at "\\* TODO COMMENT Test")))
2362 ;; Correctly un-hexify fuzzy links.
2363 (should
2364 (org-test-with-temp-text "* With space\n[[*With%20space][With space<point>]]"
2365 (org-open-at-point)
2366 (bobp)))
2367 (should
2368 (org-test-with-temp-text "* [1]\n[[*%5B1%5D<point>]]"
2369 (org-open-at-point)
2370 (bobp)))
2371 ;; Match search strings containing newline characters, including
2372 ;; blank lines.
2373 (should
2374 (org-test-with-temp-text-in-file "Paragraph\n\nline1\nline2\n\n"
2375 (let ((file (buffer-file-name)))
2376 (goto-char (point-max))
2377 (insert (format "[[file:%s::line1 line2]]" file))
2378 (beginning-of-line)
2379 (let ((org-link-search-must-match-exact-headline nil))
2380 (org-open-at-point 0))
2381 (looking-at-p "line1"))))
2382 (should
2383 (org-test-with-temp-text-in-file "Paragraph\n\nline1\n\nline2\n\n"
2384 (let ((file (buffer-file-name)))
2385 (goto-char (point-max))
2386 (insert (format "[[file:%s::line1 line2]]" file))
2387 (beginning-of-line)
2388 (let ((org-link-search-must-match-exact-headline nil))
2389 (org-open-at-point 0))
2390 (looking-at-p "line1")))))
2392 ;;;; Link Escaping
2394 (ert-deftest test-org/org-link-escape-ascii-character ()
2395 "Escape an ascii character."
2396 (should
2397 (string=
2398 "%5B"
2399 (org-link-escape "["))))
2401 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
2402 "Escape an ascii control character."
2403 (should
2404 (string=
2405 "%09"
2406 (org-link-escape "\t"))))
2408 (ert-deftest test-org/org-link-escape-multibyte-character ()
2409 "Escape an unicode multibyte character."
2410 (should
2411 (string=
2412 "%E2%82%AC"
2413 (org-link-escape "€"))))
2415 (ert-deftest test-org/org-link-escape-custom-table ()
2416 "Escape string with custom character table."
2417 (should
2418 (string=
2419 "Foo%3A%42ar%0A"
2420 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
2422 (ert-deftest test-org/org-link-escape-custom-table-merge ()
2423 "Escape string with custom table merged with default table."
2424 (should
2425 (string=
2426 "%5BF%6F%6F%3A%42ar%0A%5D"
2427 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
2429 (ert-deftest test-org/org-link-unescape-ascii-character ()
2430 "Unescape an ascii character."
2431 (should
2432 (string=
2434 (org-link-unescape "%5B"))))
2436 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
2437 "Unescpae an ascii control character."
2438 (should
2439 (string=
2440 "\n"
2441 (org-link-unescape "%0A"))))
2443 (ert-deftest test-org/org-link-unescape-multibyte-character ()
2444 "Unescape unicode multibyte character."
2445 (should
2446 (string=
2447 "€"
2448 (org-link-unescape "%E2%82%AC"))))
2450 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
2451 "Unescape old style percent escaped character."
2452 (should
2453 (string=
2454 "àâçèéêîôùû"
2455 (decode-coding-string
2456 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
2458 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
2459 "Escape and unescape a URL that includes an escaped char.
2460 http://article.gmane.org/gmane.emacs.orgmode/21459/"
2461 (should
2462 (string=
2463 "http://some.host.com/form?&id=blah%2Bblah25"
2464 (org-link-unescape
2465 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
2467 ;;;; Open at point
2469 (ert-deftest test-org/open-at-point/keyword ()
2470 "Does `org-open-at-point' open link in a keyword line?"
2471 (should
2472 (org-test-with-temp-text
2473 "<<top>>\n#+KEYWORD: <point>[[top]]"
2474 (org-open-at-point) t))
2475 (should
2476 (org-test-with-temp-text
2477 "* H\n<<top>>\n#+KEYWORD: <point>[[top]]"
2478 (org-open-at-point) t)))
2480 (ert-deftest test-org/open-at-point/property ()
2481 "Does `org-open-at-point' open link in property drawer?"
2482 (should
2483 (org-test-with-temp-text
2484 "* Headline
2485 :PROPERTIES:
2486 :URL: <point>[[*Headline]]
2487 :END:"
2488 (org-open-at-point) t)))
2490 (ert-deftest test-org/open-at-point/comment ()
2491 "Does `org-open-at-point' open link in a commented line?"
2492 (should
2493 (org-test-with-temp-text
2494 "<<top>>\n# <point>[[top]]"
2495 (org-open-at-point) t))
2496 (should
2497 (org-test-with-temp-text
2498 "* H\n<<top>>\n# <point>[[top]]"
2499 (org-open-at-point) t)))
2501 (ert-deftest test-org/open-at-point/inline-image ()
2502 "Test `org-open-at-point' on nested links."
2503 (should
2504 (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
2505 (org-open-at-point)
2506 (bobp))))
2508 (ert-deftest test-org/open-at-point/radio-target ()
2509 "Test `org-open-at-point' on radio targets."
2510 (should
2511 (org-test-with-temp-text "<<<target>>> <point>target"
2512 (org-update-radio-target-regexp)
2513 (org-open-at-point)
2514 (eq (org-element-type (org-element-context)) 'radio-target))))
2516 (ert-deftest test-org/open-at-point/tag ()
2517 "Test `org-open-at-point' on tags."
2518 (should
2519 (org-test-with-temp-text "* H :<point>tag:"
2520 (catch :result
2521 (cl-letf (((symbol-function 'org-tags-view)
2522 (lambda (&rest args) (throw :result t))))
2523 (org-open-at-point)
2524 nil))))
2525 (should-not
2526 (org-test-with-temp-text-in-file "* H<point> :tag:"
2527 (catch :result
2528 (cl-letf (((symbol-function 'org-tags-view)
2529 (lambda (&rest args) (throw :result t))))
2530 (org-open-at-point)
2531 nil)))))
2533 ;;;; Stored links
2535 (ert-deftest test-org/store-link ()
2536 "Test `org-store-link' specifications."
2537 ;; On a headline, link to that headline. Use heading as the
2538 ;; description of the link.
2539 (should
2540 (let (org-store-link-props org-stored-links)
2541 (org-test-with-temp-text-in-file "* H1"
2542 (let ((file (buffer-file-name)))
2543 (equal (format "[[file:%s::*H1][H1]]" file)
2544 (org-store-link nil))))))
2545 ;; On a headline, remove any link from description.
2546 (should
2547 (let (org-store-link-props org-stored-links)
2548 (org-test-with-temp-text-in-file "* [[#l][d]]"
2549 (let ((file (buffer-file-name)))
2550 (equal (format "[[file:%s::*%s][d]]"
2551 file
2552 (org-link-escape "[[#l][d]]"))
2553 (org-store-link nil))))))
2554 (should
2555 (let (org-store-link-props org-stored-links)
2556 (org-test-with-temp-text-in-file "* [[l]]"
2557 (let ((file (buffer-file-name)))
2558 (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
2559 (org-store-link nil))))))
2560 (should
2561 (let (org-store-link-props org-stored-links)
2562 (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
2563 (let ((file (buffer-file-name)))
2564 (equal (format "[[file:%s::*%s][d1 d2]]"
2565 file
2566 (org-link-escape "[[l1][d1]] [[l2][d2]]"))
2567 (org-store-link nil))))))
2568 ;; On a named element, link to that element.
2569 (should
2570 (let (org-store-link-props org-stored-links)
2571 (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
2572 (let ((file (buffer-file-name)))
2573 (equal (format "[[file:%s::foo][foo]]" file)
2574 (org-store-link nil))))))
2575 ;; Store link to Org buffer, with context.
2576 (should
2577 (let ((org-stored-links nil)
2578 (org-id-link-to-org-use-id nil)
2579 (org-context-in-file-links t))
2580 (org-test-with-temp-text-in-file "* h1"
2581 (let ((file (buffer-file-name)))
2582 (equal (format "[[file:%s::*h1][h1]]" file)
2583 (org-store-link nil))))))
2584 ;; Store link to Org buffer, without context.
2585 (should
2586 (let ((org-stored-links nil)
2587 (org-id-link-to-org-use-id nil)
2588 (org-context-in-file-links nil))
2589 (org-test-with-temp-text-in-file "* h1"
2590 (let ((file (buffer-file-name)))
2591 (equal (format "[[file:%s][file:%s]]" file file)
2592 (org-store-link nil))))))
2593 ;; C-u prefix reverses `org-context-in-file-links' in Org buffer.
2594 (should
2595 (let ((org-stored-links nil)
2596 (org-id-link-to-org-use-id nil)
2597 (org-context-in-file-links nil))
2598 (org-test-with-temp-text-in-file "* h1"
2599 (let ((file (buffer-file-name)))
2600 (equal (format "[[file:%s::*h1][h1]]" file)
2601 (org-store-link '(4)))))))
2602 ;; A C-u C-u does *not* reverse `org-context-in-file-links' in Org
2603 ;; buffer.
2604 (should
2605 (let ((org-stored-links nil)
2606 (org-id-link-to-org-use-id nil)
2607 (org-context-in-file-links nil))
2608 (org-test-with-temp-text-in-file "* h1"
2609 (let ((file (buffer-file-name)))
2610 (equal (format "[[file:%s][file:%s]]" file file)
2611 (org-store-link '(16)))))))
2612 ;; Store file link to non-Org buffer, with context.
2613 (should
2614 (let ((org-stored-links nil)
2615 (org-context-in-file-links t))
2616 (org-test-with-temp-text-in-file "one\n<point>two"
2617 (fundamental-mode)
2618 (let ((file (buffer-file-name)))
2619 (equal (format "[[file:%s::one]]" file)
2620 (org-store-link nil))))))
2621 ;; Store file link to non-Org buffer, without context.
2622 (should
2623 (let ((org-stored-links nil)
2624 (org-context-in-file-links nil))
2625 (org-test-with-temp-text-in-file "one\n<point>two"
2626 (fundamental-mode)
2627 (let ((file (buffer-file-name)))
2628 (equal (format "[[file:%s][file:%s]]" file file)
2629 (org-store-link nil))))))
2630 ;; C-u prefix reverses `org-context-in-file-links' in non-Org
2631 ;; buffer.
2632 (should
2633 (let ((org-stored-links nil)
2634 (org-context-in-file-links nil))
2635 (org-test-with-temp-text-in-file "one\n<point>two"
2636 (fundamental-mode)
2637 (let ((file (buffer-file-name)))
2638 (equal (format "[[file:%s::one]]" file)
2639 (org-store-link '(4)))))))
2640 ;; A C-u C-u does *not* reverse `org-context-in-file-links' in
2641 ;; non-Org buffer.
2642 (should
2643 (let ((org-stored-links nil)
2644 (org-context-in-file-links nil))
2645 (org-test-with-temp-text-in-file "one\n<point>two"
2646 (fundamental-mode)
2647 (let ((file (buffer-file-name)))
2648 (equal (format "[[file:%s][file:%s]]" file file)
2649 (org-store-link '(16))))))))
2652 ;;; Node Properties
2654 (ert-deftest test-org/accumulated-properties-in-drawers ()
2655 "Ensure properties accumulate in subtree drawers."
2656 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
2657 (org-babel-next-src-block)
2658 (should (equal '(2 1) (org-babel-execute-src-block)))))
2660 (ert-deftest test-org/custom-properties ()
2661 "Test custom properties specifications."
2662 ;; Standard test.
2663 (should
2664 (let ((org-custom-properties '("FOO")))
2665 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
2666 (org-toggle-custom-properties-visibility)
2667 (org-invisible-p2))))
2668 ;; Properties are case-insensitive.
2669 (should
2670 (let ((org-custom-properties '("FOO")))
2671 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
2672 (org-toggle-custom-properties-visibility)
2673 (org-invisible-p2))))
2674 (should
2675 (let ((org-custom-properties '("foo")))
2676 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
2677 (org-toggle-custom-properties-visibility)
2678 (org-invisible-p2))))
2679 ;; Multiple custom properties in the same drawer.
2680 (should
2681 (let ((org-custom-properties '("FOO" "BAR")))
2682 (org-test-with-temp-text
2683 "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
2684 (org-toggle-custom-properties-visibility)
2685 (and (org-invisible-p2)
2686 (not (progn (forward-line) (org-invisible-p2)))
2687 (progn (forward-line) (org-invisible-p2))))))
2688 ;; Hide custom properties with an empty value.
2689 (should
2690 (let ((org-custom-properties '("FOO")))
2691 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
2692 (org-toggle-custom-properties-visibility)
2693 (org-invisible-p2))))
2694 ;; Do not hide fake properties.
2695 (should-not
2696 (let ((org-custom-properties '("FOO")))
2697 (org-test-with-temp-text ":FOO: val\n"
2698 (org-toggle-custom-properties-visibility)
2699 (org-invisible-p2))))
2700 (should-not
2701 (let ((org-custom-properties '("A")))
2702 (org-test-with-temp-text
2703 "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
2704 (org-toggle-custom-properties-visibility)
2705 (org-invisible-p2)))))
2709 ;;; Mark Region
2711 (ert-deftest test-org/mark-subtree ()
2712 "Test `org-mark-subtree' specifications."
2713 ;; Error when point is before first headline.
2714 (should-error
2715 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
2716 (progn (transient-mark-mode 1)
2717 (org-mark-subtree))))
2718 ;; Without argument, mark current subtree.
2719 (should
2720 (equal
2721 '(12 32)
2722 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
2723 (progn (transient-mark-mode 1)
2724 (forward-line 2)
2725 (org-mark-subtree)
2726 (list (region-beginning) (region-end))))))
2727 ;; With an argument, move ARG up.
2728 (should
2729 (equal
2730 '(1 32)
2731 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
2732 (progn (transient-mark-mode 1)
2733 (forward-line 2)
2734 (org-mark-subtree 1)
2735 (list (region-beginning) (region-end))))))
2736 ;; Do not get fooled by inlinetasks.
2737 (when (featurep 'org-inlinetask)
2738 (should
2739 (= 1
2740 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
2741 (progn (transient-mark-mode 1)
2742 (forward-line 1)
2743 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
2744 (region-beginning)))))))
2748 ;;; Miscellaneous
2750 (ert-deftest test-org/sort-entries ()
2751 "Test `org-sort-entries'."
2752 ;; Sort alphabetically.
2753 (should
2754 (equal "\n* abc\n* def\n* xyz\n"
2755 (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
2756 (org-sort-entries nil ?a)
2757 (buffer-string))))
2758 (should
2759 (equal "\n* xyz\n* def\n* abc\n"
2760 (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
2761 (org-sort-entries nil ?A)
2762 (buffer-string))))
2763 (should
2764 (equal "\n* \n* klm\n* xyz\n"
2765 (org-test-with-temp-text "\n* xyz\n* \n* klm\n"
2766 (org-sort-entries nil ?a)
2767 (buffer-string))))
2768 ;; Sort numerically.
2769 (should
2770 (equal "\n* 1\n* 2\n* 10\n"
2771 (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
2772 (org-sort-entries nil ?n)
2773 (buffer-string))))
2774 (should
2775 (equal "\n* 10\n* 2\n* 1\n"
2776 (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
2777 (org-sort-entries nil ?N)
2778 (buffer-string))))
2779 (should
2780 (equal "\n* \n* 1\n* 2\n"
2781 (org-test-with-temp-text "\n* 1\n* \n* 2\n"
2782 (org-sort-entries nil ?n)
2783 (buffer-string))))
2784 ;; Sort by custom function.
2785 (should
2786 (equal "\n* b\n* aa\n* ccc\n"
2787 (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
2788 (org-sort-entries nil ?f
2789 (lambda ()
2790 (length (buffer-substring (point-at-bol)
2791 (point-at-eol))))
2792 #'<)
2793 (buffer-string))))
2794 (should
2795 (equal "\n* ccc\n* aa\n* b\n"
2796 (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
2797 (org-sort-entries nil ?F
2798 (lambda ()
2799 (length (buffer-substring (point-at-bol)
2800 (point-at-eol))))
2801 #'<)
2802 (buffer-string))))
2803 ;; Sort by TODO keyword.
2804 (should
2805 (equal "\n* TODO h1\n* TODO h3\n* DONE h2\n"
2806 (org-test-with-temp-text
2807 "\n* TODO h1\n* DONE h2\n* TODO h3\n"
2808 (org-sort-entries nil ?o)
2809 (buffer-string))))
2810 (should
2811 (equal "\n* DONE h2\n* TODO h1\n* TODO h3\n"
2812 (org-test-with-temp-text
2813 "\n* TODO h1\n* DONE h2\n* TODO h3\n"
2814 (org-sort-entries nil ?O)
2815 (buffer-string))))
2816 ;; Sort by priority.
2817 (should
2818 (equal "\n* [#A] h2\n* [#B] h3\n* [#C] h1\n"
2819 (org-test-with-temp-text
2820 "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
2821 (org-sort-entries nil ?p)
2822 (buffer-string))))
2823 (should
2824 (equal "\n* [#C] h1\n* [#B] h3\n* [#A] h2\n"
2825 (org-test-with-temp-text
2826 "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
2827 (org-sort-entries nil ?P)
2828 (buffer-string))))
2829 ;; Sort by creation time.
2830 (should
2831 (equal "
2832 * h3
2833 [2017-05-08 Mon]
2834 * h2
2835 [2017-05-09 Tue]
2836 * h1
2837 [2018-05-09 Wed]
2839 (org-test-with-temp-text
2841 * h1
2842 [2018-05-09 Wed]
2843 * h2
2844 [2017-05-09 Tue]
2845 * h3
2846 [2017-05-08 Mon]
2848 (org-sort-entries nil ?c)
2849 (buffer-string))))
2851 ;; Sort by scheduled date.
2852 (should
2853 (equal "
2854 * TODO h4
2855 SCHEDULED: <2017-05-06 Sat>
2856 * TODO h3
2857 SCHEDULED: <2017-05-08 Mon>
2858 * TODO h2
2859 DEADLINE: <2017-05-09 Tue>
2860 * TODO h1
2861 DEADLINE: <2017-05-07 Sun>
2863 (org-test-with-temp-text
2865 * TODO h2
2866 DEADLINE: <2017-05-09 Tue>
2867 * TODO h1
2868 DEADLINE: <2017-05-07 Sun>
2869 * TODO h3
2870 SCHEDULED: <2017-05-08 Mon>
2871 * TODO h4
2872 SCHEDULED: <2017-05-06 Sat>
2874 (org-sort-entries nil ?s)
2875 (buffer-string))))
2876 ;; Sort by deadline date.
2877 (should
2878 (equal "
2879 * TODO h1
2880 DEADLINE: <2017-05-07 Sun>
2881 * TODO h2
2882 DEADLINE: <2017-05-09 Tue>
2883 * TODO h3
2884 SCHEDULED: <2017-05-08 Mon>
2885 * TODO h4
2886 SCHEDULED: <2017-05-06 Sat>
2888 (org-test-with-temp-text
2890 * TODO h2
2891 DEADLINE: <2017-05-09 Tue>
2892 * TODO h1
2893 DEADLINE: <2017-05-07 Sun>
2894 * TODO h3
2895 SCHEDULED: <2017-05-08 Mon>
2896 * TODO h4
2897 SCHEDULED: <2017-05-06 Sat>
2899 (org-sort-entries nil ?d)
2900 (buffer-string))))
2901 ;; Sort by any date/time
2902 (should
2903 (equal "
2904 * TODO h4
2905 SCHEDULED: <2017-05-06 Sat>
2906 * TODO h1
2907 DEADLINE: <2017-05-07 Sun>
2908 * TODO h3
2909 SCHEDULED: <2017-05-08 Mon>
2910 * TODO h2
2911 DEADLINE: <2017-05-09 Tue>
2913 (org-test-with-temp-text
2915 * TODO h2
2916 DEADLINE: <2017-05-09 Tue>
2917 * TODO h1
2918 DEADLINE: <2017-05-07 Sun>
2919 * TODO h3
2920 SCHEDULED: <2017-05-08 Mon>
2921 * TODO h4
2922 SCHEDULED: <2017-05-06 Sat>
2924 (org-sort-entries nil ?t)
2925 (buffer-string))))
2926 ;; Sort by clocking time.
2927 (should
2928 (equal "
2929 * clocked h2
2930 :LOGBOOK:
2931 CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
2932 CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
2933 :END:
2934 * clocked h1
2935 :LOGBOOK:
2936 CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
2937 CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
2938 :END:
2940 (org-test-with-temp-text
2942 * clocked h1
2943 :LOGBOOK:
2944 CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
2945 CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
2946 :END:
2947 * clocked h2
2948 :LOGBOOK:
2949 CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
2950 CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
2951 :END:
2953 (org-sort-entries nil ?k)
2954 (buffer-string))))
2955 ;; Preserve file local variables when sorting.
2956 (should
2957 (equal "\n* A\n* B\n# Local Variables:\n# foo: t\n# End:\n"
2958 (org-test-with-temp-text
2959 "\n* B\n* A\n# Local Variables:\n# foo: t\n# End:"
2960 (org-sort-entries nil ?a)
2961 (buffer-string)))))
2963 (ert-deftest test-org/string-collate-greaterp ()
2964 "Test `org-string-collate-greaterp' specifications."
2965 (should (org-string-collate-greaterp "def" "abc"))
2966 (should-not (org-string-collate-greaterp "abc" "def")))
2968 (ert-deftest test-org/file-contents ()
2969 "Test `org-file-contents' specifications."
2970 ;; Open files.
2971 (should
2972 (string= "#+BIND: variable value
2973 #+DESCRIPTION: l2
2974 #+LANGUAGE: en
2975 #+SELECT_TAGS: b
2976 #+TITLE: b
2977 #+PROPERTY: a 1
2978 " (org-file-contents (expand-file-name "setupfile3.org"
2979 (concat org-test-dir "examples/")))))
2980 ;; Throw error when trying to access an invalid file.
2981 (should-error (org-file-contents "this-file-must-not-exist"))
2982 ;; Try to access an invalid file, but do not throw an error.
2983 (should
2984 (progn (org-file-contents "this-file-must-not-exist" :noerror) t))
2985 ;; Open URL.
2986 (should
2987 (string= "foo"
2988 (let ((buffer (generate-new-buffer "url-retrieve-output")))
2989 (unwind-protect
2990 ;; Simulate successful retrieval of a URL.
2991 (cl-letf (((symbol-function 'url-retrieve-synchronously)
2992 (lambda (&rest_)
2993 (with-current-buffer buffer
2994 (insert "HTTP/1.1 200 OK\n\nfoo"))
2995 buffer)))
2996 (org-file-contents "http://some-valid-url"))
2997 (kill-buffer buffer)))))
2998 ;; Throw error when trying to access an invalid URL.
2999 (should-error
3000 (let ((buffer (generate-new-buffer "url-retrieve-output")))
3001 (unwind-protect
3002 ;; Simulate unsuccessful retrieval of a URL.
3003 (cl-letf (((symbol-function 'url-retrieve-synchronously)
3004 (lambda (&rest_)
3005 (with-current-buffer buffer
3006 (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
3007 buffer)))
3008 (org-file-contents "http://this-url-must-not-exist"))
3009 (kill-buffer buffer))))
3010 ;; Try to access an invalid URL, but do not throw an error.
3011 (should-error
3012 (let ((buffer (generate-new-buffer "url-retrieve-output")))
3013 (unwind-protect
3014 ;; Simulate unsuccessful retrieval of a URL.
3015 (cl-letf (((symbol-function 'url-retrieve-synchronously)
3016 (lambda (&rest_)
3017 (with-current-buffer buffer
3018 (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
3019 buffer)))
3020 (org-file-contents "http://this-url-must-not-exist"))
3021 (kill-buffer buffer))))
3022 (should
3023 (let ((buffer (generate-new-buffer "url-retrieve-output")))
3024 (unwind-protect
3025 ;; Simulate unsuccessful retrieval of a URL.
3026 (cl-letf (((symbol-function 'url-retrieve-synchronously)
3027 (lambda (&rest_)
3028 (with-current-buffer buffer
3029 (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
3030 buffer)))
3031 (org-file-contents "http://this-url-must-not-exist" :noerror))
3032 (kill-buffer buffer))
3033 t)))
3036 ;;; Navigation
3038 (ert-deftest test-org/forward-heading-same-level ()
3039 "Test `org-forward-heading-same-level' specifications."
3040 ;; Test navigation at top level, forward and backward.
3041 (should
3042 (equal "* H2"
3043 (org-test-with-temp-text "* H1\n* H2"
3044 (org-forward-heading-same-level 1)
3045 (buffer-substring-no-properties (point) (line-end-position)))))
3046 (should
3047 (equal "* H1"
3048 (org-test-with-temp-text "* H1\n<point>* H2"
3049 (org-forward-heading-same-level -1)
3050 (buffer-substring-no-properties (point) (line-end-position)))))
3051 ;; Test navigation in a sub-tree, forward and backward.
3052 (should
3053 (equal "* H2"
3054 (org-test-with-temp-text "* H1\n** H11\n** H12\n* H2"
3055 (org-forward-heading-same-level 1)
3056 (buffer-substring-no-properties (point) (line-end-position)))))
3057 (should
3058 (equal "* H1"
3059 (org-test-with-temp-text "* H1\n** H11\n** H12\n<point>* H2"
3060 (org-forward-heading-same-level -1)
3061 (buffer-substring-no-properties (point) (line-end-position)))))
3062 ;; Stop at first or last sub-heading.
3063 (should-not
3064 (equal "* H2"
3065 (org-test-with-temp-text "* H1\n** H11\n<point>** H12\n* H2"
3066 (org-forward-heading-same-level 1)
3067 (buffer-substring-no-properties (point) (line-end-position)))))
3068 (should-not
3069 (equal "* H2"
3070 (org-test-with-temp-text "* H1\n<point>** H11\n** H12\n* H2"
3071 (org-forward-heading-same-level -1)
3072 (buffer-substring-no-properties (point) (line-end-position)))))
3073 ;; Allow multiple moves.
3074 (should
3075 (equal "* H3"
3076 (org-test-with-temp-text "* H1\n* H2\n* H3"
3077 (org-forward-heading-same-level 2)
3078 (buffer-substring-no-properties (point) (line-end-position)))))
3079 (should
3080 (equal "* H1"
3081 (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
3082 (org-forward-heading-same-level -2)
3083 (buffer-substring-no-properties (point) (line-end-position)))))
3084 ;; Ignore spurious moves when first (or last) sibling is reached.
3085 (should
3086 (equal "** H3"
3087 (org-test-with-temp-text "* First\n<point>** H1\n** H2\n** H3\n* Last"
3088 (org-forward-heading-same-level 100)
3089 (buffer-substring-no-properties (point) (line-end-position)))))
3090 (should
3091 (equal "** H1"
3092 (org-test-with-temp-text "* First\n** H1\n** H2\n<point>** H3\n* Last"
3093 (org-forward-heading-same-level -100)
3094 (buffer-substring-no-properties (point) (line-end-position))))))
3096 (ert-deftest test-org/end-of-meta-data ()
3097 "Test `org-end-of-meta-data' specifications."
3098 ;; Skip planning line.
3099 (should
3100 (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
3101 (org-end-of-meta-data)
3102 (eobp)))
3103 ;; Skip properties drawer.
3104 (should
3105 (org-test-with-temp-text
3106 "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
3107 (org-end-of-meta-data)
3108 (eobp)))
3109 ;; Skip both.
3110 (should
3111 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
3112 (org-end-of-meta-data)
3113 (eobp)))
3114 ;; Nothing to skip, go to first line.
3115 (should
3116 (org-test-with-temp-text "* Headline\nContents"
3117 (org-end-of-meta-data)
3118 (looking-at "Contents")))
3119 ;; With option argument, skip empty lines, regular drawers and
3120 ;; clocking lines.
3121 (should
3122 (org-test-with-temp-text "* Headline\n\nContents"
3123 (org-end-of-meta-data t)
3124 (looking-at "Contents")))
3125 (should
3126 (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
3127 (org-end-of-meta-data t)
3128 (looking-at "Contents")))
3129 (should
3130 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
3131 (org-end-of-meta-data t)
3132 (looking-at "Contents")))
3133 ;; Special case: do not skip incomplete drawers.
3134 (should
3135 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
3136 (org-end-of-meta-data t)
3137 (looking-at ":LOGBOOK:")))
3138 ;; Special case: Be careful about consecutive headlines.
3139 (should-not
3140 (org-test-with-temp-text "* H1\n*H2\nContents"
3141 (org-end-of-meta-data t)
3142 (looking-at "Contents"))))
3144 (ert-deftest test-org/beginning-of-line ()
3145 "Test `org-beginning-of-line' specifications."
3146 ;; Move to beginning of line. If current line in invisible, move to
3147 ;; beginning of visible line instead.
3148 (should
3149 (org-test-with-temp-text "Some text\nSome other text<point>"
3150 (org-beginning-of-line)
3151 (bolp)))
3152 (should
3153 (org-test-with-temp-text "* H1\n** H2<point>"
3154 (org-overview)
3155 (org-beginning-of-line)
3156 (= (line-beginning-position) 1)))
3157 ;; With `visual-line-mode' active, move to beginning of visual line.
3158 (should-not
3159 (org-test-with-temp-text "A <point>long line of text\nSome other text"
3160 (visual-line-mode)
3161 (dotimes (i 1000) (insert "very "))
3162 (org-beginning-of-line)
3163 (bolp)))
3164 ;; In a wide headline, with `visual-line-mode', prefer going to the
3165 ;; beginning of a visual line than to the logical beginning of line,
3166 ;; even if special movement is active.
3167 (should-not
3168 (org-test-with-temp-text "* A <point>long headline"
3169 (visual-line-mode)
3170 (dotimes (i 1000) (insert "very "))
3171 (goto-char (point-max))
3172 (org-beginning-of-line)
3173 (bobp)))
3174 (should-not
3175 (org-test-with-temp-text "* A <point>long headline"
3176 (visual-line-mode)
3177 (dotimes (i 1000) (insert "very "))
3178 (goto-char (point-max))
3179 (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))
3180 (bobp)))
3181 ;; At an headline with special movement, first move at beginning of
3182 ;; title, then at the beginning of line, rinse, repeat.
3183 (should
3184 (org-test-with-temp-text "* TODO Headline<point>"
3185 (let ((org-special-ctrl-a/e t))
3186 (and (progn (org-beginning-of-line) (looking-at-p "Headline"))
3187 (progn (org-beginning-of-line) (bolp))
3188 (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
3189 (should
3190 (org-test-with-temp-text "* TODO [#A] Headline<point>"
3191 (let ((org-special-ctrl-a/e t))
3192 (org-beginning-of-line)
3193 (looking-at "Headline"))))
3194 (should
3195 (org-test-with-temp-text "* TODO [#A] Headline<point>"
3196 (let ((org-special-ctrl-a/e '(t . nil)))
3197 (org-beginning-of-line)
3198 (looking-at "Headline"))))
3199 (should-not
3200 (org-test-with-temp-text "* TODO [#A] Headline<point>"
3201 (let ((org-special-ctrl-a/e '(nil . nil)))
3202 (org-beginning-of-line)
3203 (looking-at "Headline"))))
3204 ;; At an headline with reversed movement, first move to beginning of
3205 ;; line, then to the beginning of title.
3206 (should
3207 (org-test-with-temp-text "* TODO Headline<point>"
3208 (let ((org-special-ctrl-a/e 'reversed)
3209 (this-command last-command))
3210 (and (progn (org-beginning-of-line) (bolp))
3211 (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
3212 (should
3213 (org-test-with-temp-text "* TODO Headline<point>"
3214 (let ((org-special-ctrl-a/e '(reversed . nil))
3215 (this-command last-command))
3216 (and (progn (org-beginning-of-line) (bolp))
3217 (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
3218 (should-not
3219 (org-test-with-temp-text "* TODO Headline<point>"
3220 (let ((org-special-ctrl-a/e '(t . nil))
3221 (this-command last-command))
3222 (and (progn (org-beginning-of-line) (bolp))
3223 (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
3224 ;; At an item with special movement, first move after to beginning
3225 ;; of title, then to the beginning of line, rinse, repeat.
3226 (should
3227 (org-test-with-temp-text "- [ ] Item<point>"
3228 (let ((org-special-ctrl-a/e t))
3229 (and (progn (org-beginning-of-line) (looking-at-p "Item"))
3230 (progn (org-beginning-of-line) (bolp))
3231 (progn (org-beginning-of-line) (looking-at-p "Item"))))))
3232 ;; At an item with reversed movement, first move to beginning of
3233 ;; line, then to the beginning of title.
3234 (should
3235 (org-test-with-temp-text "- [X] Item<point>"
3236 (let ((org-special-ctrl-a/e 'reversed)
3237 (this-command last-command))
3238 (and (progn (org-beginning-of-line) (bolp))
3239 (progn (org-beginning-of-line) (looking-at-p "Item"))))))
3240 ;; Leave point before invisible characters at column 0.
3241 (should
3242 (org-test-with-temp-text "[[https://orgmode.org]]<point>"
3243 (let ((org-special-ctrl-a/e nil))
3244 (org-beginning-of-line)
3245 (bolp))))
3246 (should
3247 (org-test-with-temp-text "[[https://orgmode.org]]<point>"
3248 (let ((org-special-ctrl-a/e t))
3249 (org-beginning-of-line)
3250 (bolp))))
3251 (should
3252 (org-test-with-temp-text "[[http<point>://orgmode.org]]"
3253 (visual-line-mode)
3254 (org-beginning-of-line)
3255 (bolp)))
3256 ;; Special case: Do not error when the buffer contains only a single
3257 ;; asterisk.
3258 (should
3259 (org-test-with-temp-text "*<point>"
3260 (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
3261 (should
3262 (org-test-with-temp-text "*<point>"
3263 (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
3265 (ert-deftest test-org/end-of-line ()
3266 "Test `org-end-of-line' specifications."
3267 ;; Standard test.
3268 (should
3269 (org-test-with-temp-text "Some text\nSome other text"
3270 (org-end-of-line)
3271 (eolp)))
3272 ;; With `visual-line-mode' active, move to end of visible line.
3273 ;; However, never go past ellipsis.
3274 (should-not
3275 (org-test-with-temp-text "A <point>long line of text\nSome other text"
3276 (visual-line-mode)
3277 (dotimes (i 1000) (insert "very "))
3278 (goto-char (point-min))
3279 (org-end-of-line)
3280 (eolp)))
3281 (should-not
3282 (org-test-with-temp-text "* A short headline\nSome contents"
3283 (visual-line-mode)
3284 (org-overview)
3285 (org-end-of-line)
3286 (eobp)))
3287 ;; In a wide headline, with `visual-line-mode', prefer going to end
3288 ;; of visible line if tags, or end of line, are farther.
3289 (should-not
3290 (org-test-with-temp-text "* A <point>long headline"
3291 (visual-line-mode)
3292 (dotimes (i 1000) (insert "very "))
3293 (goto-char (point-min))
3294 (org-end-of-line)
3295 (eolp)))
3296 (should-not
3297 (org-test-with-temp-text "* A <point>long headline :tag:"
3298 (visual-line-mode)
3299 (dotimes (i 1000) (insert "very "))
3300 (goto-char (point-min))
3301 (org-end-of-line)
3302 (eolp)))
3303 ;; At an headline without special movement, go to end of line.
3304 ;; However, never go past ellipsis.
3305 (should
3306 (org-test-with-temp-text "* Headline2b :tag:\n"
3307 (let ((org-special-ctrl-a/e nil))
3308 (and (progn (org-end-of-line) (eolp))
3309 (progn (org-end-of-line) (eolp))))))
3310 (should
3311 (org-test-with-temp-text "* Headline2b :tag:\n"
3312 (let ((org-special-ctrl-a/e '(t . nil)))
3313 (and (progn (org-end-of-line) (eolp))
3314 (progn (org-end-of-line) (eolp))))))
3315 (should
3316 (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
3317 (org-overview)
3318 (let ((org-special-ctrl-a/e nil))
3319 (org-end-of-line)
3320 (= 1 (line-beginning-position)))))
3321 ;; At an headline with special movement, first move before tags,
3322 ;; then at the end of line, rinse, repeat. However, never go past
3323 ;; ellipsis.
3324 (should
3325 (org-test-with-temp-text "* Headline1 :tag:\n"
3326 (let ((org-special-ctrl-a/e t))
3327 (and (progn (org-end-of-line) (looking-at-p " :tag:"))
3328 (progn (org-end-of-line) (eolp))
3329 (progn (org-end-of-line) (looking-at-p " :tag:"))))))
3330 (should
3331 (org-test-with-temp-text "* Headline1 :tag:\n"
3332 (let ((org-special-ctrl-a/e '(nil . t)))
3333 (and (progn (org-end-of-line) (looking-at-p " :tag:"))
3334 (progn (org-end-of-line) (eolp))
3335 (progn (org-end-of-line) (looking-at-p " :tag:"))))))
3336 (should-not
3337 (org-test-with-temp-text "* Headline1 :tag:\n"
3338 (let ((org-special-ctrl-a/e '(nil . nil)))
3339 (and (progn (org-end-of-line) (looking-at-p " :tag:"))
3340 (progn (org-end-of-line) (eolp))
3341 (progn (org-end-of-line) (looking-at-p " :tag:"))))))
3342 (should
3343 (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
3344 (org-overview)
3345 (let ((org-special-ctrl-a/e t))
3346 (org-end-of-line)
3347 (org-end-of-line)
3348 (= 1 (line-beginning-position)))))
3349 ;; At an headline, with reversed movement, first go to end of line,
3350 ;; then before tags. However, never go past ellipsis.
3351 (should
3352 (org-test-with-temp-text "* Headline3 :tag:\n"
3353 (let ((org-special-ctrl-a/e 'reversed)
3354 (this-command last-command))
3355 (and (progn (org-end-of-line) (eolp))
3356 (progn (org-end-of-line) (looking-at-p " :tag:"))))))
3357 (should
3358 (org-test-with-temp-text "* Headline3 :tag:\n"
3359 (let ((org-special-ctrl-a/e '(nil . reversed))
3360 (this-command last-command))
3361 (and (progn (org-end-of-line) (eolp))
3362 (progn (org-end-of-line) (looking-at-p " :tag:"))))))
3363 (should-not
3364 (org-test-with-temp-text "* Headline3 :tag:\n"
3365 (let ((org-special-ctrl-a/e '(nil . t))
3366 (this-command last-command))
3367 (and (progn (org-end-of-line) (eolp))
3368 (progn (org-end-of-line) (looking-at-p " :tag:"))))))
3369 (should
3370 (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
3371 (org-overview)
3372 (let ((org-special-ctrl-a/e 'reversed))
3373 (org-end-of-line)
3374 (= 1 (line-beginning-position)))))
3375 ;; At a block without hidden contents.
3376 (should
3377 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
3378 (progn (org-end-of-line) (eolp))))
3379 ;; At a block with hidden contents.
3380 (should-not
3381 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
3382 (let ((org-special-ctrl-a/e t))
3383 (org-hide-block-toggle)
3384 (org-end-of-line)
3385 (eobp))))
3386 ;; Get past invisible characters at the end of line.
3387 (should
3388 (org-test-with-temp-text "[[https://orgmode.org]]"
3389 (org-end-of-line)
3390 (eolp))))
3392 (ert-deftest test-org/open-line ()
3393 "Test `org-open-line' specifications."
3394 ;; Call `open-line' outside of tables.
3395 (should
3396 (equal "\nText"
3397 (org-test-with-temp-text "Text"
3398 (org-open-line 1)
3399 (buffer-string))))
3400 ;; At a table, create a row above.
3401 (should
3402 (equal "\n| |\n| a |"
3403 (org-test-with-temp-text "\n<point>| a |"
3404 (org-open-line 1)
3405 (buffer-string))))
3406 ;; At the very first character of the buffer, also call `open-line'.
3407 (should
3408 (equal "\n| a |"
3409 (org-test-with-temp-text "| a |"
3410 (org-open-line 1)
3411 (buffer-string))))
3412 ;; Narrowing does not count.
3413 (should
3414 (equal "Text\n| |\n| a |"
3415 (org-test-with-temp-text "Text\n<point>| a |"
3416 (narrow-to-region (point) (point-max))
3417 (org-open-line 1)
3418 (widen)
3419 (buffer-string)))))
3421 (ert-deftest test-org/forward-sentence ()
3422 "Test `org-forward-sentence' specifications."
3423 ;; At the end of a table cell, move to the end of the next one.
3424 (should
3425 (org-test-with-temp-text "| a<point> | b |"
3426 (org-forward-sentence)
3427 (looking-at " |$")))
3428 ;; Elsewhere in a cell, move to its end.
3429 (should
3430 (org-test-with-temp-text "| a<point>c | b |"
3431 (org-forward-sentence)
3432 (looking-at " | b |$")))
3433 ;; Otherwise, simply call `forward-sentence'.
3434 (should
3435 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
3436 (org-forward-sentence)
3437 (looking-at " Sentence 2.")))
3438 (should
3439 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
3440 (org-forward-sentence)
3441 (org-forward-sentence)
3442 (eobp)))
3443 ;; At the end of an element, jump to the next one, without stopping
3444 ;; on blank lines in-between.
3445 (should
3446 (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
3447 (org-forward-sentence)
3448 (eobp)))
3449 ;; Headlines are considered to be sentences by themselves, even if
3450 ;; they do not end with a full stop.
3451 (should
3452 (equal
3453 "* Headline"
3454 (org-test-with-temp-text "* <point>Headline\nSentence."
3455 (org-forward-sentence)
3456 (buffer-substring-no-properties (line-beginning-position) (point)))))
3457 (should
3458 (org-test-with-temp-text "* Headline<point>\nSentence."
3459 (org-forward-sentence)
3460 (eobp)))
3461 (should
3462 (org-test-with-temp-text "Sentence.<point>\n\n* Headline\n\nSentence 2."
3463 (org-forward-sentence)
3464 (and (org-at-heading-p) (eolp)))))
3466 (ert-deftest test-org/backward-sentence ()
3467 "Test `org-backward-sentence' specifications."
3468 ;; At the beginning of a table cell, move to the beginning of the
3469 ;; previous one.
3470 (should
3471 (org-test-with-temp-text "| a | <point>b |"
3472 (org-backward-sentence)
3473 (looking-at "a | b |$")))
3474 ;; Elsewhere in a cell, move to its beginning.
3475 (should
3476 (org-test-with-temp-text "| a | b<point>c |"
3477 (org-backward-sentence)
3478 (looking-at "bc |$")))
3479 ;; Otherwise, simply call `backward-sentence'.
3480 (should
3481 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
3482 (org-backward-sentence)
3483 (looking-at "Sentence 2.")))
3484 (should
3485 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
3486 (org-backward-sentence)
3487 (org-backward-sentence)
3488 (bobp)))
3489 ;; Make sure to hit the beginning of a sentence on the same line as
3490 ;; an item.
3491 (should
3492 (org-test-with-temp-text "- Line 1\n line <point>2."
3493 (org-backward-sentence)
3494 (looking-at "Line 1"))))
3496 (ert-deftest test-org/forward-paragraph ()
3497 "Test `org-forward-paragraph' specifications."
3498 ;; At end of buffer, do not return an error.
3499 (should
3500 (org-test-with-temp-text "Paragraph"
3501 (goto-char (point-max))
3502 (org-forward-paragraph)
3504 ;; Standard test.
3505 (should
3506 (org-test-with-temp-text "P1\n\nP2\n\nP3"
3507 (org-forward-paragraph)
3508 (looking-at "P2")))
3509 ;; Ignore depth.
3510 (should
3511 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
3512 (org-forward-paragraph)
3513 (looking-at "P1")))
3514 ;; Do not enter elements with invisible contents.
3515 (should
3516 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
3517 (org-hide-block-toggle)
3518 (org-forward-paragraph)
3519 (looking-at "P3")))
3520 ;; On an affiliated keyword, jump to the beginning of the element.
3521 (should
3522 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
3523 (org-forward-paragraph)
3524 (looking-at "Para")))
3525 ;; On an item or a footnote definition, move to the second element
3526 ;; inside, if any.
3527 (should
3528 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
3529 (org-forward-paragraph)
3530 (looking-at " Paragraph")))
3531 (should
3532 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
3533 (org-forward-paragraph)
3534 (looking-at "Paragraph")))
3535 ;; On an item, or a footnote definition, when the first line is
3536 ;; empty, move to the first item.
3537 (should
3538 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
3539 (org-forward-paragraph)
3540 (looking-at " Paragraph")))
3541 (should
3542 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
3543 (org-forward-paragraph)
3544 (looking-at "Paragraph")))
3545 ;; On a table (resp. a property drawer) do not move through table
3546 ;; rows (resp. node properties).
3547 (should
3548 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
3549 (org-forward-paragraph)
3550 (looking-at "Paragraph")))
3551 (should
3552 (org-test-with-temp-text
3553 "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
3554 (org-forward-paragraph)
3555 (looking-at "Paragraph")))
3556 ;; On a verse or source block, stop after blank lines.
3557 (should
3558 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
3559 (org-forward-paragraph)
3560 (looking-at "L2")))
3561 (should
3562 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
3563 (org-forward-paragraph)
3564 (looking-at "L2"))))
3566 (ert-deftest test-org/backward-paragraph ()
3567 "Test `org-backward-paragraph' specifications."
3568 ;; Do not error at beginning of buffer.
3569 (should
3570 (org-test-with-temp-text "Paragraph"
3571 (org-backward-paragraph)
3573 ;; Regular test.
3574 (should
3575 (org-test-with-temp-text "P1\n\nP2\n\nP3<point>"
3576 (org-backward-paragraph)
3577 (looking-at "P3")))
3578 (should
3579 (org-test-with-temp-text "P1\n\nP2\n\n<point>P3"
3580 (org-backward-paragraph)
3581 (looking-at-p "P2")))
3582 ;; Ignore depth.
3583 (should
3584 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\n<point>P3"
3585 (org-backward-paragraph)
3586 (looking-at-p "P2")))
3587 ;; Ignore invisible elements.
3588 (should
3589 (org-test-with-temp-text "* H1\n P1\n* H2"
3590 (org-cycle)
3591 (goto-char (point-max))
3592 (beginning-of-line)
3593 (org-backward-paragraph)
3594 (bobp)))
3595 ;; On an affiliated keyword, jump to the first one.
3596 (should
3597 (org-test-with-temp-text
3598 "P1\n#+name: n\n#+caption: c1\n#+caption: <point>c2\nP2"
3599 (org-backward-paragraph)
3600 (looking-at-p "#\\+name")))
3601 ;; On the second element in an item or a footnote definition, jump
3602 ;; to item or the definition.
3603 (should
3604 (org-test-with-temp-text "- line1\n\n<point> line2"
3605 (org-backward-paragraph)
3606 (looking-at-p "- line1")))
3607 (should
3608 (org-test-with-temp-text "[fn:1] line1\n\n<point> line2"
3609 (org-backward-paragraph)
3610 (looking-at-p "\\[fn:1\\] line1")))
3611 ;; On a table (resp. a property drawer), ignore table rows
3612 ;; (resp. node properties).
3613 (should
3614 (org-test-with-temp-text "| a | b |\n| c | d |\n<point>P1"
3615 (org-backward-paragraph)
3616 (bobp)))
3617 (should
3618 (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
3619 (org-backward-paragraph)
3620 (looking-at-p ":PROPERTIES:")))
3621 ;; On a comment, example, src and verse blocks, stop before blank
3622 ;; lines.
3623 (should
3624 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\n<point>L3\n#+END_VERSE"
3625 (org-backward-paragraph)
3626 (looking-at-p "L2")))
3627 (should
3628 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\n<point>L3#+END_SRC"
3629 (org-backward-paragraph)
3630 (looking-at-p "L2")))
3631 ;; In comment, example, export, src and verse blocks, stop below
3632 ;; opening line when called from within the block.
3633 (should
3634 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\nL2<point>\n#+END_VERSE"
3635 (org-backward-paragraph)
3636 (looking-at-p "L1")))
3637 (should
3638 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nL1\nL2<point>\n#+END_EXAMPLE"
3639 (org-backward-paragraph)
3640 (looking-at-p "L1")))
3641 ;; When called from the opening line itself, however, move to
3642 ;; beginning of block.
3643 (should
3644 (org-test-with-temp-text "#+BEGIN_<point>EXAMPLE\nL1\n#+END_EXAMPLE"
3645 (org-backward-paragraph)
3646 (bobp)))
3647 ;; Pathological case: on an empty heading, move to its beginning.
3648 (should
3649 (org-test-with-temp-text "* <point>H"
3650 (org-backward-paragraph)
3651 (bobp))))
3653 (ert-deftest test-org/forward-element ()
3654 "Test `org-forward-element' specifications."
3655 ;; 1. At EOB: should error.
3656 (org-test-with-temp-text "Some text\n"
3657 (goto-char (point-max))
3658 (should-error (org-forward-element)))
3659 ;; 2. Standard move: expected to ignore blank lines.
3660 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
3661 (org-forward-element)
3662 (should (looking-at (regexp-quote "Second paragraph."))))
3663 ;; 3. Headline tests.
3664 (org-test-with-temp-text "
3665 * Head 1
3666 ** Head 1.1
3667 *** Head 1.1.1
3668 ** Head 1.2"
3669 ;; 3.1. At an headline beginning: move to next headline at the
3670 ;; same level.
3671 (goto-line 3)
3672 (org-forward-element)
3673 (should (looking-at (regexp-quote "** Head 1.2")))
3674 ;; 3.2. At an headline beginning: move to parent headline if no
3675 ;; headline at the same level.
3676 (goto-line 3)
3677 (org-forward-element)
3678 (should (looking-at (regexp-quote "** Head 1.2"))))
3679 ;; 4. Greater element tests.
3680 (org-test-with-temp-text
3681 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
3682 ;; 4.1. At a greater element: expected to skip contents.
3683 (org-forward-element)
3684 (should (looking-at (regexp-quote "Outside.")))
3685 ;; 4.2. At the end of greater element contents: expected to skip
3686 ;; to the end of the greater element.
3687 (goto-line 2)
3688 (org-forward-element)
3689 (should (looking-at (regexp-quote "Outside."))))
3690 ;; 5. List tests.
3691 (org-test-with-temp-text "
3692 - item1
3694 - sub1
3696 - sub2
3698 - sub3
3700 Inner paragraph.
3702 - item2
3704 Outside."
3705 ;; 5.1. At list top point: expected to move to the element after
3706 ;; the list.
3707 (goto-line 2)
3708 (org-forward-element)
3709 (should (looking-at (regexp-quote "Outside.")))
3710 ;; 5.2. Special case: at the first line of a sub-list, but not at
3711 ;; beginning of line, move to next item.
3712 (goto-line 2)
3713 (forward-char)
3714 (org-forward-element)
3715 (should (looking-at "- item2"))
3716 (goto-line 4)
3717 (forward-char)
3718 (org-forward-element)
3719 (should (looking-at " - sub2"))
3720 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
3721 (goto-line 4)
3722 (org-forward-element)
3723 (should (looking-at (regexp-quote " Inner paragraph.")))
3724 ;; 5.4. At sub-list end: expected to move outside the sub-list.
3725 (goto-line 8)
3726 (org-forward-element)
3727 (should (looking-at (regexp-quote " Inner paragraph.")))
3728 ;; 5.5. At an item: expected to move to next item, if any.
3729 (goto-line 6)
3730 (org-forward-element)
3731 (should (looking-at " - sub3"))))
3733 (ert-deftest test-org/backward-element ()
3734 "Test `org-backward-element' specifications."
3735 ;; 1. Should error at BOB.
3736 (org-test-with-temp-text " \nParagraph."
3737 (should-error (org-backward-element)))
3738 ;; 2. Should move at BOB when called on the first element in buffer.
3739 (should
3740 (org-test-with-temp-text "\n#+TITLE: test"
3741 (progn (forward-line)
3742 (org-backward-element)
3743 (bobp))))
3744 ;; 3. Not at the beginning of an element: move at its beginning.
3745 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
3746 (goto-line 3)
3747 (end-of-line)
3748 (org-backward-element)
3749 (should (looking-at (regexp-quote "Paragraph2."))))
3750 ;; 4. Headline tests.
3751 (org-test-with-temp-text "
3752 * Head 1
3753 ** Head 1.1
3754 *** Head 1.1.1
3755 ** Head 1.2"
3756 ;; 4.1. At an headline beginning: move to previous headline at the
3757 ;; same level.
3758 (goto-line 5)
3759 (org-backward-element)
3760 (should (looking-at (regexp-quote "** Head 1.1")))
3761 ;; 4.2. At an headline beginning: move to parent headline if no
3762 ;; headline at the same level.
3763 (goto-line 3)
3764 (org-backward-element)
3765 (should (looking-at (regexp-quote "* Head 1")))
3766 ;; 4.3. At the first top-level headline: should error.
3767 (goto-line 2)
3768 (should-error (org-backward-element)))
3769 ;; 5. At beginning of first element inside a greater element:
3770 ;; expected to move to greater element's beginning.
3771 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
3772 (goto-line 3)
3773 (org-backward-element)
3774 (should (looking-at "#\\+BEGIN_CENTER")))
3775 ;; 6. At the beginning of the first element in a section: should
3776 ;; move back to headline, if any.
3777 (should
3778 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
3779 (progn (goto-char (point-max))
3780 (beginning-of-line)
3781 (org-backward-element)
3782 (org-at-heading-p))))
3783 ;; 7. List tests.
3784 (org-test-with-temp-text "
3785 - item1
3787 - sub1
3789 - sub2
3791 - sub3
3793 Inner paragraph.
3795 - item2
3798 Outside."
3799 ;; 7.1. At beginning of sub-list: expected to move to the
3800 ;; paragraph before it.
3801 (goto-line 4)
3802 (org-backward-element)
3803 (should (looking-at "item1"))
3804 ;; 7.2. At an item in a list: expected to move at previous item.
3805 (goto-line 8)
3806 (org-backward-element)
3807 (should (looking-at " - sub2"))
3808 (goto-line 12)
3809 (org-backward-element)
3810 (should (looking-at "- item1"))
3811 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
3812 ;; beginning.
3813 (goto-line 10)
3814 (org-backward-element)
3815 (should (looking-at " - sub1"))
3816 (goto-line 15)
3817 (org-backward-element)
3818 (should (looking-at "- item1"))
3819 ;; 7.4. At blank-lines before list end: expected to move to top
3820 ;; item.
3821 (goto-line 14)
3822 (org-backward-element)
3823 (should (looking-at "- item1"))))
3825 (ert-deftest test-org/up-element ()
3826 "Test `org-up-element' specifications."
3827 ;; 1. At BOB or with no surrounding element: should error.
3828 (org-test-with-temp-text "Paragraph."
3829 (should-error (org-up-element)))
3830 (org-test-with-temp-text "* Head1\n* Head2"
3831 (goto-line 2)
3832 (should-error (org-up-element)))
3833 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
3834 (goto-line 3)
3835 (should-error (org-up-element)))
3836 ;; 2. At an headline: move to parent headline.
3837 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
3838 (goto-line 3)
3839 (org-up-element)
3840 (should (looking-at "\\* Head1")))
3841 ;; 3. Inside a greater element: move to greater element beginning.
3842 (org-test-with-temp-text
3843 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
3844 (goto-line 3)
3845 (org-up-element)
3846 (should (looking-at "#\\+BEGIN_CENTER")))
3847 ;; 4. List tests.
3848 (org-test-with-temp-text "* Top
3849 - item1
3851 - sub1
3853 - sub2
3855 Paragraph within sub2.
3857 - item2"
3858 ;; 4.1. Within an item: move to the item beginning.
3859 (goto-line 8)
3860 (org-up-element)
3861 (should (looking-at " - sub2"))
3862 ;; 4.2. At an item in a sub-list: move to parent item.
3863 (goto-line 4)
3864 (org-up-element)
3865 (should (looking-at "- item1"))
3866 ;; 4.3. At an item in top list: move to beginning of whole list.
3867 (goto-line 10)
3868 (org-up-element)
3869 (should (looking-at "- item1"))
3870 ;; 4.4. Special case. At very top point: should move to parent of
3871 ;; list.
3872 (goto-line 2)
3873 (org-up-element)
3874 (should (looking-at "\\* Top"))))
3876 (ert-deftest test-org/down-element ()
3877 "Test `org-down-element' specifications."
3878 ;; Error when the element hasn't got a recursive type.
3879 (org-test-with-temp-text "Paragraph."
3880 (should-error (org-down-element)))
3881 ;; Error when the element has no contents
3882 (org-test-with-temp-text "* Headline"
3883 (should-error (org-down-element)))
3884 ;; When at a plain-list, move to first item.
3885 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
3886 (goto-line 2)
3887 (org-down-element)
3888 (should (looking-at " - Item 1.1")))
3889 (org-test-with-temp-text "#+NAME: list\n- Item 1"
3890 (org-down-element)
3891 (should (looking-at " Item 1")))
3892 ;; When at a table, move to first row
3893 (org-test-with-temp-text "#+NAME: table\n| a | b |"
3894 (org-down-element)
3895 (should (looking-at " a | b |")))
3896 ;; Otherwise, move inside the greater element.
3897 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
3898 (org-down-element)
3899 (should (looking-at "Paragraph"))))
3901 (ert-deftest test-org/drag-element-backward ()
3902 "Test `org-drag-element-backward' specifications."
3903 ;; Standard test.
3904 (should
3905 (equal
3906 "#+key2: val2\n#+key1: val1\n#+key3: val3"
3907 (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
3908 (org-drag-element-backward)
3909 (buffer-string))))
3910 (should
3911 (equal
3912 "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
3913 (org-test-with-temp-text
3914 "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
3915 (org-drag-element-backward)
3916 (buffer-string))))
3917 ;; Preserve blank lines.
3918 (should
3919 (equal "Paragraph 2\n\n\nPara1\n\nPara3"
3920 (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
3921 (org-drag-element-backward)
3922 (buffer-string))))
3923 ;; Preserve column.
3924 (should
3925 (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
3926 (org-drag-element-backward)
3927 (looking-at-p "2")))
3928 ;; Error when trying to move first element of buffer.
3929 (should-error
3930 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
3931 (org-drag-element-backward))
3932 :type 'user-error)
3933 ;; Error when trying to swap nested elements.
3934 (should-error
3935 (org-test-with-temp-text "#+BEGIN_CENTER\n<point>Test.\n#+END_CENTER"
3936 (org-drag-element-backward))
3937 :type 'user-error)
3938 ;; Error when trying to swap an headline element and a non-headline
3939 ;; element.
3940 (should-error
3941 (org-test-with-temp-text "Test.\n<point>* Head 1"
3942 (org-drag-element-backward))
3943 :type 'error)
3944 ;; Error when called before first element.
3945 (should-error
3946 (org-test-with-temp-text "\n<point>"
3947 (org-drag-element-backward))
3948 :type 'user-error)
3949 ;; Preserve visibility of elements and their contents.
3950 (should
3951 (equal '((63 . 82) (26 . 48))
3952 (org-test-with-temp-text "
3953 #+BEGIN_CENTER
3954 Text.
3955 #+END_CENTER
3956 - item 1
3957 #+BEGIN_QUOTE
3958 Text.
3959 #+END_QUOTE"
3960 (while (search-forward "BEGIN_" nil t) (org-cycle))
3961 (search-backward "- item 1")
3962 (org-drag-element-backward)
3963 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
3964 (overlays-in (point-min) (point-max))))))
3965 ;; Pathological case: handle call with point in blank lines right
3966 ;; after a headline.
3967 (should
3968 (equal "* H2\n* H1\nText\n\n"
3969 (org-test-with-temp-text "* H1\nText\n* H2\n\n<point>"
3970 (org-drag-element-backward)
3971 (buffer-string)))))
3973 (ert-deftest test-org/drag-element-forward ()
3974 "Test `org-drag-element-forward' specifications."
3975 ;; 1. Error when trying to move first element of buffer.
3976 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
3977 (goto-line 3)
3978 (should-error (org-drag-element-forward)))
3979 ;; 2. Error when trying to swap nested elements.
3980 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
3981 (forward-line)
3982 (should-error (org-drag-element-forward)))
3983 ;; 3. Error when trying to swap a non-headline element and an
3984 ;; headline.
3985 (org-test-with-temp-text "Test.\n* Head 1"
3986 (should-error (org-drag-element-forward)))
3987 ;; 4. Error when called before first element.
3988 (should-error
3989 (org-test-with-temp-text "\n"
3990 (forward-line)
3991 (org-drag-element-backward))
3992 :type 'user-error)
3993 ;; 5. Otherwise, swap elements, preserving column and blank lines
3994 ;; between elements.
3995 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
3996 (search-forward "graph")
3997 (org-drag-element-forward)
3998 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
3999 (should (looking-at " 1")))
4000 ;; 5. Preserve visibility of elements and their contents.
4001 (org-test-with-temp-text "
4002 #+BEGIN_CENTER
4003 Text.
4004 #+END_CENTER
4005 - item 1
4006 #+BEGIN_QUOTE
4007 Text.
4008 #+END_QUOTE"
4009 (while (search-forward "BEGIN_" nil t) (org-cycle))
4010 (search-backward "#+BEGIN_CENTER")
4011 (org-drag-element-forward)
4012 (should
4013 (equal
4014 '((63 . 82) (26 . 48))
4015 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
4016 (overlays-in (point-min) (point-max)))))))
4018 (ert-deftest test-org/next-block ()
4019 "Test `org-next-block' specifications."
4020 ;; Regular test.
4021 (should
4022 (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
4023 (org-next-block 1)
4024 (looking-at "#\\+BEGIN_CENTER")))
4025 ;; Ignore case.
4026 (should
4027 (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
4028 (let ((case-fold-search nil))
4029 (org-next-block 1)
4030 (looking-at "#\\+begin_center"))))
4031 ;; Ignore current line.
4032 (should
4033 (org-test-with-temp-text
4034 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
4035 (org-next-block 1)
4036 (looking-at "#\\+BEGIN_CENTER")))
4037 ;; Throw an error when no block is found.
4038 (should-error
4039 (org-test-with-temp-text "Paragraph"
4040 (org-next-block 1)))
4041 ;; With an argument, skip many blocks at once.
4042 (should
4043 (org-test-with-temp-text
4044 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
4045 (org-next-block 2)
4046 (looking-at "#\\+BEGIN_QUOTE")))
4047 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
4048 (should
4049 (org-test-with-temp-text
4050 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
4051 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
4052 (looking-at "#\\+BEGIN_QUOTE")))
4053 ;; Optional argument is also case-insensitive.
4054 (should
4055 (org-test-with-temp-text
4056 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
4057 (let ((case-fold-search nil))
4058 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
4059 (looking-at "#\\+begin_quote")))))
4061 (ert-deftest test-org/insert-template ()
4062 "Test `org-insert-structure-template'."
4063 ;; Test in empty buffer.
4064 (should
4065 (string= "#+begin_foo\n\n#+end_foo\n"
4066 (org-test-with-temp-text ""
4067 (org-insert-structure-template "foo")
4068 (buffer-string))))
4069 ;; Test with multiple lines in buffer.
4070 (should
4071 (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\nI'm a second paragraph"
4072 (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
4073 (org-mark-element)
4074 (org-insert-structure-template "foo")
4075 (buffer-string))))
4076 ;; Mark only the current line.
4077 (should
4078 (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
4079 (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
4080 (set-mark (point-min))
4081 (end-of-line)
4082 (activate-mark)
4083 (org-insert-structure-template "foo")
4084 (buffer-string))))
4085 ;; Middle of paragraph
4086 (should
4087 (string= "p1\n#+begin_foo\np2\n#+end_foo\np3"
4088 (org-test-with-temp-text "p1\n<point>p2\np3"
4089 (set-mark (line-beginning-position))
4090 (end-of-line)
4091 (activate-mark)
4092 (org-insert-structure-template "foo")
4093 (buffer-string))))
4094 ;; Test with text in buffer, no region, no final newline.
4095 (should
4096 (string= "#+begin_foo\nI'm a paragraph.\n#+end_foo\n"
4097 (org-test-with-temp-text "I'm a paragraph."
4098 (org-mark-element)
4099 (org-insert-structure-template "foo")
4100 (buffer-string))))
4101 ;; Test with text in buffer and region set.
4102 (should
4103 (string= "#+begin_foo\nI'm a paragraph\n\nI'm a second paragrah\n#+end_foo\n"
4104 (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragrah"
4105 (set-mark (point))
4106 (goto-char (point-max))
4107 (org-insert-structure-template "foo")
4108 (buffer-string))))
4109 ;; Test with example escaping.
4110 (should
4111 (string= "#+begin_example\n,* Heading\n#+end_example\n"
4112 (org-test-with-temp-text "* Heading"
4113 (org-mark-element)
4114 (org-insert-structure-template "example")
4115 (buffer-string))))
4116 ;; Test with indentation.
4117 (should
4118 (string= " #+begin_foo\n This is a paragraph\n #+end_foo\n"
4119 (org-test-with-temp-text " This is a paragraph"
4120 (org-mark-element)
4121 (org-insert-structure-template "foo")
4122 (buffer-string))))
4123 ;; Test point location.
4124 (should
4125 (eq (length "#\\+begin_foo\n")
4126 (org-test-with-temp-text ""
4127 (org-insert-structure-template "foo")
4128 (point))))
4129 (should
4130 (eq (length "#\\+begin_src ")
4131 (org-test-with-temp-text ""
4132 (org-insert-structure-template "src")
4133 (point)))))
4135 (ert-deftest test-org/previous-block ()
4136 "Test `org-previous-block' specifications."
4137 ;; Regular test.
4138 (should
4139 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
4140 (org-previous-block 1)
4141 (looking-at "#\\+BEGIN_CENTER")))
4142 ;; Ignore case.
4143 (should
4144 (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
4145 (let ((case-fold-search nil))
4146 (org-previous-block 1)
4147 (looking-at "#\\+begin_center"))))
4148 ;; Ignore current line.
4149 (should
4150 (org-test-with-temp-text
4151 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
4152 (org-previous-block 1)
4153 (looking-at "#\\+BEGIN_QUOTE")))
4154 ;; Throw an error when no block is found.
4155 (should-error
4156 (org-test-with-temp-text "Paragraph<point>"
4157 (org-previous-block 1)))
4158 ;; With an argument, skip many blocks at once.
4159 (should
4160 (org-test-with-temp-text
4161 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
4162 (org-previous-block 2)
4163 (looking-at "#\\+BEGIN_CENTER")))
4164 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
4165 (should
4166 (org-test-with-temp-text
4167 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
4168 (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
4169 (looking-at "#\\+BEGIN_QUOTE")))
4170 ;; Optional argument is also case-insensitive.
4171 (should
4172 (org-test-with-temp-text
4173 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
4174 (let ((case-fold-search nil))
4175 (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
4176 (looking-at "#\\+begin_quote")))))
4179 ;;; Outline structure
4181 (ert-deftest test-org/demote ()
4182 "Test `org-demote' specifications."
4183 ;; Add correct number of stars according to `org-odd-levels-only'.
4184 (should
4185 (= 2
4186 (org-test-with-temp-text "* H"
4187 (let ((org-odd-levels-only nil)) (org-demote))
4188 (org-current-level))))
4189 (should
4190 (= 3
4191 (org-test-with-temp-text "* H"
4192 (let ((org-odd-levels-only t)) (org-demote))
4193 (org-current-level))))
4194 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
4195 (should
4196 (org-test-with-temp-text "* H :tag:"
4197 (let ((org-tags-column 10)
4198 (org-auto-align-tags t)
4199 (org-odd-levels-only nil))
4200 (org-demote))
4201 (org-move-to-column 10)
4202 (looking-at-p ":tag:$")))
4203 (should-not
4204 (org-test-with-temp-text "* H :tag:"
4205 (let ((org-tags-column 10)
4206 (org-auto-align-tags nil)
4207 (org-odd-levels-only nil))
4208 (org-demote))
4209 (org-move-to-column 10)
4210 (looking-at-p ":tag:$")))
4211 ;; When `org-adapt-indentation' is non-nil, always indent planning
4212 ;; info and property drawers accordingly.
4213 (should
4214 (= 3
4215 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
4216 (let ((org-odd-levels-only nil)
4217 (org-adapt-indentation t))
4218 (org-demote))
4219 (forward-line)
4220 (org-get-indentation))))
4221 (should
4222 (= 3
4223 (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
4224 (let ((org-odd-levels-only nil)
4225 (org-adapt-indentation t))
4226 (org-demote))
4227 (forward-line)
4228 (org-get-indentation))))
4229 (should-not
4230 (= 3
4231 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
4232 (let ((org-odd-levels-only nil)
4233 (org-adapt-indentation nil))
4234 (org-demote))
4235 (forward-line)
4236 (org-get-indentation))))
4237 ;; When `org-adapt-indentation' is non-nil, shift all lines in
4238 ;; section accordingly. Ignore, however, footnote definitions and
4239 ;; inlinetasks boundaries.
4240 (should
4241 (= 3
4242 (org-test-with-temp-text "* H\n Paragraph"
4243 (let ((org-odd-levels-only nil)
4244 (org-adapt-indentation t))
4245 (org-demote))
4246 (forward-line)
4247 (org-get-indentation))))
4248 (should
4249 (= 2
4250 (org-test-with-temp-text "* H\n Paragraph"
4251 (let ((org-odd-levels-only nil)
4252 (org-adapt-indentation nil))
4253 (org-demote))
4254 (forward-line)
4255 (org-get-indentation))))
4256 (should
4257 (zerop
4258 (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
4259 (let ((org-odd-levels-only nil)
4260 (org-adapt-indentation t))
4261 (org-demote))
4262 (goto-char (point-max))
4263 (org-get-indentation))))
4264 (should
4265 (= 3
4266 (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
4267 (let ((org-odd-levels-only nil)
4268 (org-adapt-indentation t))
4269 (org-demote))
4270 (goto-char (point-max))
4271 (org-get-indentation))))
4272 (when (featurep 'org-inlinetask)
4273 (should
4274 (zerop
4275 (let ((org-inlinetask-min-level 5)
4276 (org-adapt-indentation t))
4277 (org-test-with-temp-text "* H\n***** I\n***** END"
4278 (org-demote)
4279 (forward-line)
4280 (org-get-indentation))))))
4281 (when (featurep 'org-inlinetask)
4282 (should
4283 (= 3
4284 (let ((org-inlinetask-min-level 5)
4285 (org-adapt-indentation t))
4286 (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
4287 (org-demote)
4288 (forward-line 2)
4289 (org-get-indentation))))))
4290 ;; Ignore contents of source blocks or example blocks when
4291 ;; indentation should be preserved (through
4292 ;; `org-src-preserve-indentation' or "-i" flag).
4293 (should-not
4294 (zerop
4295 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
4296 (let ((org-adapt-indentation t)
4297 (org-src-preserve-indentation nil))
4298 (org-demote))
4299 (forward-line 2)
4300 (org-get-indentation))))
4301 (should
4302 (zerop
4303 (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
4304 (let ((org-adapt-indentation t)
4305 (org-src-preserve-indentation t))
4306 (org-demote))
4307 (forward-line 2)
4308 (org-get-indentation))))
4309 (should
4310 (zerop
4311 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
4312 (let ((org-adapt-indentation t)
4313 (org-src-preserve-indentation t))
4314 (org-demote))
4315 (forward-line 2)
4316 (org-get-indentation))))
4317 (should
4318 (zerop
4319 (org-test-with-temp-text
4320 "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
4321 (let ((org-adapt-indentation t)
4322 (org-src-preserve-indentation nil))
4323 (org-demote))
4324 (forward-line 2)
4325 (org-get-indentation)))))
4327 (ert-deftest test-org/promote ()
4328 "Test `org-promote' specifications."
4329 ;; Return an error if headline is to be promoted to level 0, unless
4330 ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
4331 ;; headline becomes a comment.
4332 (should-error
4333 (org-test-with-temp-text "* H"
4334 (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
4335 (should
4336 (equal "# H"
4337 (org-test-with-temp-text "* H"
4338 (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
4339 (buffer-string))))
4340 ;; Remove correct number of stars according to
4341 ;; `org-odd-levels-only'.
4342 (should
4343 (= 2
4344 (org-test-with-temp-text "*** H"
4345 (let ((org-odd-levels-only nil)) (org-promote))
4346 (org-current-level))))
4347 (should
4348 (= 1
4349 (org-test-with-temp-text "*** H"
4350 (let ((org-odd-levels-only t)) (org-promote))
4351 (org-current-level))))
4352 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
4353 (should
4354 (org-test-with-temp-text "** H :tag:"
4355 (let ((org-tags-column 10)
4356 (org-auto-align-tags t)
4357 (org-odd-levels-only nil))
4358 (org-promote))
4359 (org-move-to-column 10)
4360 (looking-at-p ":tag:$")))
4361 (should-not
4362 (org-test-with-temp-text "** H :tag:"
4363 (let ((org-tags-column 10)
4364 (org-auto-align-tags nil)
4365 (org-odd-levels-only nil))
4366 (org-promote))
4367 (org-move-to-column 10)
4368 (looking-at-p ":tag:$")))
4369 ;; When `org-adapt-indentation' is non-nil, always indent planning
4370 ;; info and property drawers.
4371 (should
4372 (= 2
4373 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
4374 (let ((org-odd-levels-only nil)
4375 (org-adapt-indentation t))
4376 (org-promote))
4377 (forward-line)
4378 (org-get-indentation))))
4379 (should
4380 (= 2
4381 (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
4382 (let ((org-odd-levels-only nil)
4383 (org-adapt-indentation t))
4384 (org-promote))
4385 (forward-line)
4386 (org-get-indentation))))
4387 (should-not
4388 (= 2
4389 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
4390 (let ((org-odd-levels-only nil)
4391 (org-adapt-indentation nil))
4392 (org-promote))
4393 (forward-line)
4394 (org-get-indentation))))
4395 ;; When `org-adapt-indentation' is non-nil, shift all lines in
4396 ;; section accordingly. Ignore, however, footnote definitions and
4397 ;; inlinetasks boundaries.
4398 (should
4399 (= 2
4400 (org-test-with-temp-text "** H\n Paragraph"
4401 (let ((org-odd-levels-only nil)
4402 (org-adapt-indentation t))
4403 (org-promote))
4404 (forward-line)
4405 (org-get-indentation))))
4406 (should-not
4407 (= 2
4408 (org-test-with-temp-text "** H\n Paragraph"
4409 (let ((org-odd-levels-only nil)
4410 (org-adapt-indentation nil))
4411 (org-promote))
4412 (forward-line)
4413 (org-get-indentation))))
4414 (should
4415 (= 2
4416 (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
4417 (let ((org-odd-levels-only nil)
4418 (org-adapt-indentation t))
4419 (org-promote))
4420 (forward-line)
4421 (org-get-indentation))))
4422 (when (featurep 'org-inlinetask)
4423 (should
4424 (zerop
4425 (let ((org-inlinetask-min-level 5)
4426 (org-adapt-indentation t))
4427 (org-test-with-temp-text "** H\n***** I\n***** END"
4428 (org-promote)
4429 (forward-line)
4430 (org-get-indentation))))))
4431 (when (featurep 'org-inlinetask)
4432 (should
4433 (= 2
4434 (let ((org-inlinetask-min-level 5)
4435 (org-adapt-indentation t))
4436 (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
4437 (org-promote)
4438 (forward-line 2)
4439 (org-get-indentation))))))
4440 ;; Give up shifting if it would break document's structure
4441 ;; otherwise.
4442 (should
4443 (= 3
4444 (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
4445 (let ((org-odd-levels-only nil)
4446 (org-adapt-indentation t))
4447 (org-promote))
4448 (forward-line)
4449 (org-get-indentation))))
4450 (should
4451 (= 3
4452 (org-test-with-temp-text "** H\n Paragraph\n * list."
4453 (let ((org-odd-levels-only nil)
4454 (org-adapt-indentation t))
4455 (org-promote))
4456 (forward-line)
4457 (org-get-indentation))))
4458 ;; Ignore contents of source blocks or example blocks when
4459 ;; indentation should be preserved (through
4460 ;; `org-src-preserve-indentation' or "-i" flag).
4461 (should-not
4462 (zerop
4463 (org-test-with-temp-text
4464 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
4465 (let ((org-adapt-indentation t)
4466 (org-src-preserve-indentation nil)
4467 (org-odd-levels-only nil))
4468 (org-promote))
4469 (forward-line)
4470 (org-get-indentation))))
4471 (should
4472 (zerop
4473 (org-test-with-temp-text
4474 "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
4475 (let ((org-adapt-indentation t)
4476 (org-src-preserve-indentation t)
4477 (org-odd-levels-only nil))
4478 (org-promote))
4479 (forward-line)
4480 (org-get-indentation))))
4481 (should
4482 (zerop
4483 (org-test-with-temp-text
4484 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
4485 (let ((org-adapt-indentation t)
4486 (org-src-preserve-indentation t)
4487 (org-odd-levels-only nil))
4488 (org-promote))
4489 (forward-line)
4490 (org-get-indentation))))
4491 (should
4492 (zerop
4493 (org-test-with-temp-text
4494 "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
4495 (let ((org-adapt-indentation t)
4496 (org-src-preserve-indentation nil)
4497 (org-odd-levels-only nil))
4498 (org-promote))
4499 (forward-line)
4500 (org-get-indentation)))))
4502 (ert-deftest test-org/org-get-valid-level ()
4503 "Test function `org-get-valid-level' specifications."
4504 (let ((org-odd-levels-only nil))
4505 (should (equal 1 (org-get-valid-level 0 0)))
4506 (should (equal 1 (org-get-valid-level 0 1)))
4507 (should (equal 2 (org-get-valid-level 0 2)))
4508 (should (equal 3 (org-get-valid-level 0 3)))
4509 (should (equal 1 (org-get-valid-level 1 0)))
4510 (should (equal 2 (org-get-valid-level 1 1)))
4511 (should (equal 23 (org-get-valid-level 1 22)))
4512 (should (equal 1 (org-get-valid-level 1 -1)))
4513 (should (equal 1 (org-get-valid-level 2 -1))))
4514 (let ((org-odd-levels-only t))
4515 (should (equal 1 (org-get-valid-level 0 0)))
4516 (should (equal 1 (org-get-valid-level 0 1)))
4517 (should (equal 3 (org-get-valid-level 0 2)))
4518 (should (equal 5 (org-get-valid-level 0 3)))
4519 (should (equal 1 (org-get-valid-level 1 0)))
4520 (should (equal 3 (org-get-valid-level 1 1)))
4521 (should (equal 3 (org-get-valid-level 2 1)))
4522 (should (equal 5 (org-get-valid-level 3 1)))
4523 (should (equal 5 (org-get-valid-level 4 1)))
4524 (should (equal 43 (org-get-valid-level 1 21)))
4525 (should (equal 1 (org-get-valid-level 1 -1)))
4526 (should (equal 1 (org-get-valid-level 2 -1)))
4527 (should (equal 1 (org-get-valid-level 3 -1)))
4528 (should (equal 3 (org-get-valid-level 4 -1)))
4529 (should (equal 3 (org-get-valid-level 5 -1)))))
4532 ;;; Planning
4534 (ert-deftest test-org/at-planning-p ()
4535 "Test `org-at-planning-p' specifications."
4536 ;; Regular test.
4537 (should
4538 (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
4539 (org-at-planning-p)))
4540 (should-not
4541 (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
4542 (org-at-planning-p)))
4543 ;; Correctly find planning attached to inlinetasks.
4544 (when (featurep 'org-inlinetask)
4545 (should
4546 (org-test-with-temp-text
4547 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
4548 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
4549 (should-not
4550 (org-test-with-temp-text
4551 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
4552 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
4553 (should-not
4554 (org-test-with-temp-text
4555 "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
4556 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
4557 (should-not
4558 (org-test-with-temp-text
4559 "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
4560 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
4562 (ert-deftest test-org/add-planning-info ()
4563 "Test `org-add-planning-info'."
4564 ;; Create deadline when `org-adapt-indentation' is non-nil.
4565 (should
4566 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
4567 (org-test-with-temp-text "* H\nParagraph<point>"
4568 (let ((org-adapt-indentation t))
4569 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
4570 (replace-regexp-in-string
4571 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4572 nil nil 1))))
4573 ;; Create deadline when `org-adapt-indentation' is nil.
4574 (should
4575 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
4576 (org-test-with-temp-text "* H\nParagraph<point>"
4577 (let ((org-adapt-indentation nil))
4578 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
4579 (replace-regexp-in-string
4580 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4581 nil nil 1))))
4582 ;; Update deadline when `org-adapt-indentation' is non-nil.
4583 (should
4584 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
4585 (org-test-with-temp-text "\
4587 DEADLINE: <2015-06-24 Wed>
4588 Paragraph<point>"
4589 (let ((org-adapt-indentation t))
4590 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
4591 (replace-regexp-in-string
4592 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4593 nil nil 1))))
4594 ;; Update deadline when `org-adapt-indentation' is nil.
4595 (should
4596 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
4597 (org-test-with-temp-text "\
4599 DEADLINE: <2015-06-24 Wed>
4600 Paragraph<point>"
4601 (let ((org-adapt-indentation nil))
4602 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
4603 (replace-regexp-in-string
4604 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4605 nil nil 1))))
4606 ;; Schedule when `org-adapt-indentation' is non-nil.
4607 (should
4608 (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
4609 (org-test-with-temp-text "* H\nParagraph<point>"
4610 (let ((org-adapt-indentation t))
4611 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
4612 (replace-regexp-in-string
4613 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4614 nil nil 1))))
4615 ;; Schedule when `org-adapt-indentation' is nil.
4616 (should
4617 (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
4618 (org-test-with-temp-text "* H\nParagraph<point>"
4619 (let ((org-adapt-indentation nil))
4620 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
4621 (replace-regexp-in-string
4622 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4623 nil nil 1))))
4624 ;; Add deadline when scheduled.
4625 (should
4626 (equal "\
4628 DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
4629 Paragraph"
4630 (org-test-with-temp-text "\
4632 SCHEDULED: <2015-06-24 Wed>
4633 Paragraph<point>"
4634 (let ((org-adapt-indentation t))
4635 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
4636 (replace-regexp-in-string
4637 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4638 nil nil 1))))
4639 ;; Remove middle entry.
4640 (should
4641 (equal "\
4643 CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
4644 Paragraph"
4645 (org-test-with-temp-text "\
4647 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
4648 Paragraph<point>"
4649 (let ((org-adapt-indentation t))
4650 (org-add-planning-info nil nil 'deadline))
4651 (replace-regexp-in-string
4652 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
4653 nil nil 1))))
4654 ;; Remove last entry and then middle entry (order should not
4655 ;; matter).
4656 (should
4657 (equal "\
4659 CLOSED: [2015-06-24]
4660 Paragraph"
4661 (org-test-with-temp-text "\
4663 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
4664 Paragraph<point>"
4665 (let ((org-adapt-indentation t))
4666 (org-add-planning-info nil nil 'scheduled 'deadline))
4667 (replace-regexp-in-string
4668 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
4669 nil nil 1))))
4670 ;; Remove closed when `org-adapt-indentation' is non-nil.
4671 (should
4672 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
4673 (org-test-with-temp-text "\
4675 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
4676 Paragraph<point>"
4677 (let ((org-adapt-indentation t))
4678 (org-add-planning-info nil nil 'closed))
4679 (replace-regexp-in-string
4680 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4681 nil nil 1))))
4682 (should
4683 (equal "* H\n Paragraph"
4684 (org-test-with-temp-text "\
4686 CLOSED: [2015-06-25 Thu]
4687 Paragraph<point>"
4688 (let ((org-adapt-indentation t))
4689 (org-add-planning-info nil nil 'closed))
4690 (replace-regexp-in-string
4691 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4692 nil nil 1))))
4693 ;; Remove closed when `org-adapt-indentation' is nil.
4694 (should
4695 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
4696 (org-test-with-temp-text "\
4698 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
4699 Paragraph<point>"
4700 (let ((org-adapt-indentation nil))
4701 (org-add-planning-info nil nil 'closed))
4702 (replace-regexp-in-string
4703 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4704 nil nil 1))))
4705 (should
4706 (equal "* H\nParagraph"
4707 (org-test-with-temp-text "\
4709 CLOSED: [2015-06-25 Thu]
4710 Paragraph<point>"
4711 (let ((org-adapt-indentation nil))
4712 (org-add-planning-info nil nil 'closed))
4713 (replace-regexp-in-string
4714 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4715 nil nil 1))))
4716 ;; Remove closed entry and delete empty line.
4717 (should
4718 (equal "\
4720 Paragraph"
4721 (org-test-with-temp-text "\
4723 CLOSED: [2015-06-24 Wed]
4724 Paragraph<point>"
4725 (let ((org-adapt-indentation t))
4726 (org-add-planning-info nil nil 'closed))
4727 (replace-regexp-in-string
4728 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4729 nil nil 1))))
4730 ;; Remove one entry and update another.
4731 (should
4732 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
4733 (org-test-with-temp-text "\
4735 SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
4736 Paragraph<point>"
4737 (let ((org-adapt-indentation t))
4738 (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
4739 (replace-regexp-in-string
4740 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4741 nil nil 1)))))
4743 (ert-deftest test-org/deadline ()
4744 "Test `org-deadline' specifications."
4745 ;; Insert a new value or replace existing one.
4746 (should
4747 (equal "* H\nDEADLINE: <2012-03-29>\n"
4748 (org-test-with-temp-text "* H"
4749 (let ((org-adapt-indentation nil)
4750 (org-last-inserted-timestamp nil))
4751 (org-deadline nil "<2012-03-29 Tue>"))
4752 (replace-regexp-in-string
4753 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4754 nil nil 1))))
4755 (should
4756 (equal "* H\nDEADLINE: <2014-03-04>"
4757 (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
4758 (let ((org-adapt-indentation nil)
4759 (org-last-inserted-timestamp nil))
4760 (org-deadline nil "<2014-03-04 Thu>"))
4761 (replace-regexp-in-string
4762 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4763 nil nil 1))))
4764 ;; Accept delta time, e.g., "+2d".
4765 (should
4766 (equal "* H\nDEADLINE: <2015-03-04>\n"
4767 (cl-letf (((symbol-function 'current-time)
4768 (lambda (&rest args)
4769 (apply #'encode-time
4770 (org-parse-time-string "2014-03-04")))))
4771 (org-test-with-temp-text "* H"
4772 (let ((org-adapt-indentation nil)
4773 (org-last-inserted-timestamp nil))
4774 (org-deadline nil "+1y"))
4775 (replace-regexp-in-string
4776 "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
4777 ;; Preserve repeater.
4778 (should
4779 (equal "* H\nDEADLINE: <2012-03-29 +2y>\n"
4780 (org-test-with-temp-text "* H"
4781 (let ((org-adapt-indentation nil)
4782 (org-last-inserted-timestamp nil))
4783 (org-deadline nil "<2012-03-29 Tue +2y>"))
4784 (replace-regexp-in-string
4785 "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
4786 ;; Remove CLOSED keyword, if any.
4787 (should
4788 (equal "* H\nDEADLINE: <2012-03-29>"
4789 (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
4790 (let ((org-adapt-indentation nil)
4791 (org-last-inserted-timestamp nil))
4792 (org-deadline nil "<2012-03-29 Tue>"))
4793 (replace-regexp-in-string
4794 "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
4795 ;; With C-u argument, remove DEADLINE keyword.
4796 (should
4797 (equal "* H\n"
4798 (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
4799 (let ((org-adapt-indentation nil)
4800 (org-last-inserted-timestamp nil))
4801 (org-deadline '(4)))
4802 (buffer-string))))
4803 (should
4804 (equal "* H"
4805 (org-test-with-temp-text "* H"
4806 (let ((org-adapt-indentation nil)
4807 (org-last-inserted-timestamp nil))
4808 (org-deadline '(4)))
4809 (buffer-string))))
4810 ;; With C-u C-u argument, prompt for a delay cookie.
4811 (should
4812 (equal "* H\nDEADLINE: <2012-03-29 -705d>"
4813 (cl-letf (((symbol-function 'org-read-date)
4814 (lambda (&rest args)
4815 (apply #'encode-time
4816 (org-parse-time-string "2014-03-04")))))
4817 (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
4818 (let ((org-adapt-indentation nil)
4819 (org-last-inserted-timestamp nil))
4820 (org-deadline '(16)))
4821 (buffer-string)))))
4822 (should-error
4823 (cl-letf (((symbol-function 'org-read-date)
4824 (lambda (&rest args)
4825 (apply #'encode-time
4826 (org-parse-time-string "2014-03-04")))))
4827 (org-test-with-temp-text "* H"
4828 (let ((org-adapt-indentation nil)
4829 (org-last-inserted-timestamp nil))
4830 (org-deadline '(16)))
4831 (buffer-string))))
4832 ;; When a region is active and
4833 ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
4834 ;; same value in all headlines in region.
4835 (should
4836 (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
4837 (org-test-with-temp-text "* H1\n* H2"
4838 (let ((org-adapt-indentation nil)
4839 (org-last-inserted-timestamp nil)
4840 (org-loop-over-headlines-in-active-region t))
4841 (transient-mark-mode 1)
4842 (push-mark (point) t t)
4843 (goto-char (point-max))
4844 (org-deadline nil "2012-03-29"))
4845 (replace-regexp-in-string
4846 "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
4847 (should-not
4848 (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
4849 (org-test-with-temp-text "* H1\n* H2"
4850 (let ((org-adapt-indentation nil)
4851 (org-last-inserted-timestamp nil)
4852 (org-loop-over-headlines-in-active-region nil))
4853 (transient-mark-mode 1)
4854 (push-mark (point) t t)
4855 (goto-char (point-max))
4856 (org-deadline nil "2012-03-29"))
4857 (replace-regexp-in-string
4858 "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
4860 (ert-deftest test-org/schedule ()
4861 "Test `org-schedule' specifications."
4862 ;; Insert a new value or replace existing one.
4863 (should
4864 (equal "* H\nSCHEDULED: <2012-03-29>\n"
4865 (org-test-with-temp-text "* H"
4866 (let ((org-adapt-indentation nil)
4867 (org-last-inserted-timestamp nil))
4868 (org-schedule nil "<2012-03-29 Tue>"))
4869 (replace-regexp-in-string
4870 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4871 nil nil 1))))
4872 (should
4873 (equal "* H\nSCHEDULED: <2014-03-04>"
4874 (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
4875 (let ((org-adapt-indentation nil)
4876 (org-last-inserted-timestamp nil))
4877 (org-schedule nil "<2014-03-04 Thu>"))
4878 (replace-regexp-in-string
4879 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
4880 nil nil 1))))
4881 ;; Accept delta time, e.g., "+2d".
4882 (should
4883 (equal "* H\nSCHEDULED: <2015-03-04>\n"
4884 (cl-letf (((symbol-function 'current-time)
4885 (lambda (&rest args)
4886 (apply #'encode-time
4887 (org-parse-time-string "2014-03-04")))))
4888 (org-test-with-temp-text "* H"
4889 (let ((org-adapt-indentation nil)
4890 (org-last-inserted-timestamp nil))
4891 (org-schedule nil "+1y"))
4892 (replace-regexp-in-string
4893 "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
4894 ;; Preserve repeater.
4895 (should
4896 (equal "* H\nSCHEDULED: <2012-03-29 +2y>\n"
4897 (org-test-with-temp-text "* H"
4898 (let ((org-adapt-indentation nil)
4899 (org-last-inserted-timestamp nil))
4900 (org-schedule nil "<2012-03-29 Tue +2y>"))
4901 (replace-regexp-in-string
4902 "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
4903 ;; Remove CLOSED keyword, if any.
4904 (should
4905 (equal "* H\nSCHEDULED: <2012-03-29>"
4906 (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
4907 (let ((org-adapt-indentation nil)
4908 (org-last-inserted-timestamp nil))
4909 (org-schedule nil "<2012-03-29 Tue>"))
4910 (replace-regexp-in-string
4911 "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
4912 ;; With C-u argument, remove SCHEDULED keyword.
4913 (should
4914 (equal "* H\n"
4915 (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
4916 (let ((org-adapt-indentation nil)
4917 (org-last-inserted-timestamp nil))
4918 (org-schedule '(4)))
4919 (buffer-string))))
4920 (should
4921 (equal "* H"
4922 (org-test-with-temp-text "* H"
4923 (let ((org-adapt-indentation nil)
4924 (org-last-inserted-timestamp nil))
4925 (org-schedule '(4)))
4926 (buffer-string))))
4927 ;; With C-u C-u argument, prompt for a delay cookie.
4928 (should
4929 (equal "* H\nSCHEDULED: <2012-03-29 -705d>"
4930 (cl-letf (((symbol-function 'org-read-date)
4931 (lambda (&rest args)
4932 (apply #'encode-time
4933 (org-parse-time-string "2014-03-04")))))
4934 (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
4935 (let ((org-adapt-indentation nil)
4936 (org-last-inserted-timestamp nil))
4937 (org-schedule '(16)))
4938 (buffer-string)))))
4939 (should-error
4940 (cl-letf (((symbol-function 'org-read-date)
4941 (lambda (&rest args)
4942 (apply #'encode-time
4943 (org-parse-time-string "2014-03-04")))))
4944 (org-test-with-temp-text "* H"
4945 (let ((org-adapt-indentation nil)
4946 (org-last-inserted-timestamp nil))
4947 (org-schedule '(16)))
4948 (buffer-string))))
4949 ;; When a region is active and
4950 ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
4951 ;; same value in all headlines in region.
4952 (should
4953 (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
4954 (org-test-with-temp-text "* H1\n* H2"
4955 (let ((org-adapt-indentation nil)
4956 (org-last-inserted-timestamp nil)
4957 (org-loop-over-headlines-in-active-region t))
4958 (transient-mark-mode 1)
4959 (push-mark (point) t t)
4960 (goto-char (point-max))
4961 (org-schedule nil "2012-03-29"))
4962 (replace-regexp-in-string
4963 "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
4964 (should-not
4965 (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
4966 (org-test-with-temp-text "* H1\n* H2"
4967 (let ((org-adapt-indentation nil)
4968 (org-last-inserted-timestamp nil)
4969 (org-loop-over-headlines-in-active-region nil))
4970 (transient-mark-mode 1)
4971 (push-mark (point) t t)
4972 (goto-char (point-max))
4973 (org-schedule nil "2012-03-29"))
4974 (replace-regexp-in-string
4975 "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
4976 (should
4977 ;; check if a repeater survives re-scheduling.
4978 (string-match-p
4979 "\\* H\nSCHEDULED: <2017-02-01 [.A-Za-z]* \\+\\+7d>\n"
4980 (org-test-with-temp-text "* H\nSCHEDULED: <2017-01-19 ++7d>\n"
4981 (let ((org-adapt-indentation nil)
4982 (org-last-inserted-timestamp nil))
4983 (org-schedule nil "2017-02-01"))
4984 (buffer-string)))))
4987 ;;; Property API
4989 (ert-deftest test-org/buffer-property-keys ()
4990 "Test `org-buffer-property-keys' specifications."
4991 ;; Retrieve properties accross siblings.
4992 (should
4993 (equal '("A" "B")
4994 (org-test-with-temp-text "
4995 * H1
4996 :PROPERTIES:
4997 :A: 1
4998 :END:
4999 * H2
5000 :PROPERTIES:
5001 :B: 1
5002 :END:"
5003 (org-buffer-property-keys))))
5004 ;; Retrieve properties accross children.
5005 (should
5006 (equal '("A" "B")
5007 (org-test-with-temp-text "
5008 * H1
5009 :PROPERTIES:
5010 :A: 1
5011 :END:
5012 ** H2
5013 :PROPERTIES:
5014 :B: 1
5015 :END:"
5016 (org-buffer-property-keys))))
5017 ;; Retrieve muliple properties in the same drawer.
5018 (should
5019 (equal '("A" "B")
5020 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
5021 (org-buffer-property-keys))))
5022 ;; Ignore extension symbol in property name.
5023 (should
5024 (equal '("A")
5025 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
5026 (org-buffer-property-keys))))
5027 ;; With non-nil COLUMNS, extract property names from columns.
5028 (should
5029 (equal '("A" "B")
5030 (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
5031 (org-buffer-property-keys nil nil t))))
5032 (should
5033 (equal '("A" "B" "COLUMNS")
5034 (org-test-with-temp-text
5035 "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
5036 (org-buffer-property-keys nil nil t))))
5037 ;; In COLUMNS, ignore title and summary-type.
5038 (should
5039 (equal '("A")
5040 (org-test-with-temp-text "#+COLUMNS: %A(Foo)"
5041 (org-buffer-property-keys nil nil t))))
5042 (should
5043 (equal '("A")
5044 (org-test-with-temp-text "#+COLUMNS: %A{Foo}"
5045 (org-buffer-property-keys nil nil t))))
5046 (should
5047 (equal '("A")
5048 (org-test-with-temp-text "#+COLUMNS: %A(Foo){Bar}"
5049 (org-buffer-property-keys nil nil t)))))
5051 (ert-deftest test-org/property-values ()
5052 "Test `org-property-values' specifications."
5053 ;; Regular test.
5054 (should
5055 (equal '("2" "1")
5056 (org-test-with-temp-text
5057 "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
5058 (org-property-values "A"))))
5059 ;; Ignore empty values.
5060 (should-not
5061 (org-test-with-temp-text
5062 "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
5063 (org-property-values "A")))
5064 ;; Take into consideration extended values.
5065 (should
5066 (equal '("1 2")
5067 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
5068 (org-property-values "A")))))
5070 (ert-deftest test-org/find-property ()
5071 "Test `org-find-property' specifications."
5072 ;; Regular test.
5073 (should
5074 (= 1
5075 (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
5076 (org-find-property "prop"))))
5077 ;; Ignore false positives.
5078 (should
5079 (= 27
5080 (org-test-with-temp-text
5081 "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
5082 (org-find-property "A"))))
5083 ;; Return first entry found in buffer.
5084 (should
5085 (= 1
5086 (org-test-with-temp-text
5087 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
5088 (org-find-property "A"))))
5089 ;; Only search visible part of the buffer.
5090 (should
5091 (= 31
5092 (org-test-with-temp-text
5093 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
5094 (org-narrow-to-subtree)
5095 (org-find-property "A"))))
5096 ;; With optional argument, only find entries with a specific value.
5097 (should-not
5098 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
5099 (org-find-property "A" "2")))
5100 (should
5101 (= 31
5102 (org-test-with-temp-text
5103 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
5104 (org-find-property "A" "2"))))
5105 ;; Use "nil" for explicit nil values.
5106 (should
5107 (= 31
5108 (org-test-with-temp-text
5109 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
5110 (org-find-property "A" "nil")))))
5112 (ert-deftest test-org/entry-delete ()
5113 "Test `org-entry-delete' specifications."
5114 ;; Regular test.
5115 (should
5116 (string-match
5117 " *:PROPERTIES:\n *:B: +2\n *:END:"
5118 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
5119 (org-entry-delete (point) "A")
5120 (buffer-string))))
5121 ;; Also remove accumulated properties.
5122 (should-not
5123 (string-match
5124 ":A"
5125 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
5126 (org-entry-delete (point) "A")
5127 (buffer-string))))
5128 ;; When last property is removed, remove the property drawer.
5129 (should-not
5130 (string-match
5131 ":PROPERTIES:"
5132 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
5133 (org-entry-delete (point) "A")
5134 (buffer-string))))
5135 ;; Return a non-nil value when some property was removed.
5136 (should
5137 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
5138 (org-entry-delete (point) "A")))
5139 (should-not
5140 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
5141 (org-entry-delete (point) "C")))
5142 ;; Special properties cannot be located in a drawer. Allow to
5143 ;; remove them anyway, in case of user error.
5144 (should
5145 (org-test-with-temp-text "* H\n:PROPERTIES:\n:SCHEDULED: 1\n:END:"
5146 (org-entry-delete (point) "SCHEDULED"))))
5148 (ert-deftest test-org/entry-get ()
5149 "Test `org-entry-get' specifications."
5150 ;; Regular test.
5151 (should
5152 (equal "1"
5153 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
5154 (org-entry-get (point) "A"))))
5155 ;; Ignore case.
5156 (should
5157 (equal "1"
5158 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
5159 (org-entry-get (point) "a"))))
5160 ;; Handle extended values, both before and after base value.
5161 (should
5162 (equal "1 2 3"
5163 (org-test-with-temp-text
5164 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
5165 (org-entry-get (point) "A"))))
5166 ;; Empty values are returned as the empty string.
5167 (should
5168 (equal ""
5169 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
5170 (org-entry-get (point) "A"))))
5171 ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
5172 ;; otherwise, return nil.
5173 (should-not
5174 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
5175 (org-entry-get (point) "A")))
5176 (should
5177 (equal "nil"
5178 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
5179 (org-entry-get (point) "A" nil t))))
5180 ;; Return nil when no property is found, independently on the
5181 ;; LITERAL-NIL argument.
5182 (should-not
5183 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
5184 (org-entry-get (point) "B")))
5185 (should-not
5186 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
5187 (org-entry-get (point) "B" nil t)))
5188 ;; Handle inheritance, when allowed. Include extended values and
5189 ;; possibly global values.
5190 (should
5191 (equal
5193 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
5194 (org-entry-get (point) "A" t))))
5195 (should
5196 (equal
5198 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
5199 (let ((org-use-property-inheritance t))
5200 (org-entry-get (point) "A" 'selective)))))
5201 (should-not
5202 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
5203 (let ((org-use-property-inheritance nil))
5204 (org-entry-get (point) "A" 'selective))))
5205 (should
5206 (equal
5207 "1 2"
5208 (org-test-with-temp-text
5209 "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
5210 (org-entry-get (point-max) "A" t))))
5211 (should
5212 (equal "1"
5213 (org-test-with-temp-text
5214 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
5215 (org-mode-restart)
5216 (org-entry-get (point-max) "A" t))))
5217 (should
5218 (equal "0 1"
5219 (org-test-with-temp-text
5220 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
5221 (org-mode-restart)
5222 (org-entry-get (point-max) "A" t)))))
5224 (ert-deftest test-org/entry-properties ()
5225 "Test `org-entry-properties' specifications."
5226 ;; Get "ITEM" property.
5227 (should
5228 (equal "H"
5229 (org-test-with-temp-text "* TODO H"
5230 (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
5231 (should
5232 (equal "H"
5233 (org-test-with-temp-text "* TODO H"
5234 (cdr (assoc "ITEM" (org-entry-properties))))))
5235 ;; Get "TODO" property. TODO keywords are case sensitive.
5236 (should
5237 (equal "TODO"
5238 (org-test-with-temp-text "* TODO H"
5239 (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
5240 (should
5241 (equal "TODO"
5242 (org-test-with-temp-text "* TODO H"
5243 (cdr (assoc "TODO" (org-entry-properties))))))
5244 (should-not
5245 (org-test-with-temp-text "* H"
5246 (assoc "TODO" (org-entry-properties nil "TODO"))))
5247 (should-not
5248 (org-test-with-temp-text "* todo H"
5249 (assoc "TODO" (org-entry-properties nil "TODO"))))
5250 ;; Get "PRIORITY" property.
5251 (should
5252 (equal "A"
5253 (org-test-with-temp-text "* [#A] H"
5254 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
5255 (should
5256 (equal "A"
5257 (org-test-with-temp-text "* [#A] H"
5258 (cdr (assoc "PRIORITY" (org-entry-properties))))))
5259 (should
5260 (equal (char-to-string org-default-priority)
5261 (org-test-with-temp-text "* H"
5262 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
5263 ;; Get "FILE" property.
5264 (should
5265 (org-test-with-temp-text-in-file "* H\nParagraph"
5266 (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
5267 (buffer-file-name))))
5268 (should
5269 (org-test-with-temp-text-in-file "* H\nParagraph"
5270 (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
5271 (buffer-file-name))))
5272 (should-not
5273 (org-test-with-temp-text "* H\nParagraph"
5274 (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
5275 ;; Get "TAGS" property.
5276 (should
5277 (equal ":tag1:tag2:"
5278 (org-test-with-temp-text "* H :tag1:tag2:"
5279 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
5280 (should
5281 (equal ":tag1:tag2:"
5282 (org-test-with-temp-text "* H :tag1:tag2:"
5283 (cdr (assoc "TAGS" (org-entry-properties))))))
5284 (should-not
5285 (org-test-with-temp-text "* H"
5286 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
5287 ;; Get "ALLTAGS" property.
5288 (should
5289 (equal ":tag1:tag2:"
5290 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
5291 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
5292 (should
5293 (equal ":tag1:tag2:"
5294 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
5295 (cdr (assoc "ALLTAGS" (org-entry-properties))))))
5296 (should-not
5297 (org-test-with-temp-text "* H"
5298 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
5299 ;; Get "BLOCKED" property.
5300 (should
5301 (equal "t"
5302 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
5303 (let ((org-enforce-todo-dependencies t)
5304 (org-blocker-hook
5305 '(org-block-todo-from-children-or-siblings-or-parent)))
5306 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
5307 (should
5308 (equal ""
5309 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
5310 (let ((org-enforce-todo-dependencies t)
5311 (org-blocker-hook
5312 '(org-block-todo-from-children-or-siblings-or-parent)))
5313 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
5314 ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
5315 (should
5316 (equal
5317 "[2012-03-29 thu.]"
5318 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
5319 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
5320 (should
5321 (equal
5322 "[2012-03-29 thu.]"
5323 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
5324 (cdr (assoc "CLOSED" (org-entry-properties))))))
5325 (should-not
5326 (org-test-with-temp-text "* H"
5327 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
5328 (should
5329 (equal
5330 "<2014-03-04 tue.>"
5331 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
5332 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
5333 (should
5334 (equal
5335 "<2014-03-04 tue.>"
5336 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
5337 (cdr (assoc "DEADLINE" (org-entry-properties))))))
5338 (should-not
5339 (org-test-with-temp-text "* H"
5340 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
5341 (should
5342 (equal
5343 "<2014-03-04 tue.>"
5344 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
5345 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
5346 (should
5347 (equal
5348 "<2014-03-04 tue.>"
5349 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
5350 (cdr (assoc "SCHEDULED" (org-entry-properties))))))
5351 (should-not
5352 (org-test-with-temp-text "* H"
5353 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
5354 ;; Get "CATEGORY"
5355 (should
5356 (equal "cat"
5357 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
5358 (cdr (assoc "CATEGORY" (org-entry-properties))))))
5359 (should
5360 (equal "cat"
5361 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
5362 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
5363 (should
5364 (equal "cat"
5365 (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
5366 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
5367 (should
5368 (equal "cat2"
5369 (org-test-with-temp-text
5370 (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
5371 "\n"
5372 "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
5373 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
5374 ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
5375 (should
5376 (equal "<2012-03-29 thu.>"
5377 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
5378 (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
5379 (should
5380 (equal "[2012-03-29 thu.]"
5381 (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
5382 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
5383 (should
5384 (equal "<2012-03-29 thu.>"
5385 (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
5386 (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
5387 (should
5388 (equal "[2014-03-04 tue.]"
5389 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
5390 (cdr (assoc "TIMESTAMP_IA"
5391 (org-entry-properties nil "TIMESTAMP_IA"))))))
5392 (should-not
5393 (equal "<2012-03-29 thu.>"
5394 (org-test-with-temp-text "* Current\n* Next\n<2012-03-29 thu.>"
5395 (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
5396 ;; Get standard properties.
5397 (should
5398 (equal "1"
5399 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
5400 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
5401 ;; Handle extended properties.
5402 (should
5403 (equal "1 2 3"
5404 (org-test-with-temp-text
5405 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
5406 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
5407 (should
5408 (equal "1 2 3"
5409 (org-test-with-temp-text
5410 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
5411 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
5412 ;; Ignore forbidden (special) properties.
5413 (should-not
5414 (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
5415 (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
5417 (ert-deftest test-org/entry-put ()
5418 "Test `org-entry-put' specifications."
5419 ;; Error when not a string or nil.
5420 (should-error
5421 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
5422 (org-entry-put 1 "test" 2)))
5423 ;; Error when property name is invalid.
5424 (should-error
5425 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
5426 (org-entry-put 1 "no space" "value")))
5427 (should-error
5428 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
5429 (org-entry-put 1 "" "value")))
5430 ;; Set "TODO" property.
5431 (should
5432 (string-match (regexp-quote " TODO H")
5433 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
5434 (org-entry-put (point) "TODO" "TODO")
5435 (buffer-string))))
5436 (should
5437 (string-match (regexp-quote "* H")
5438 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
5439 (org-entry-put (point) "TODO" nil)
5440 (buffer-string))))
5441 ;; Set "PRIORITY" property.
5442 (should
5443 (equal "* [#A] H"
5444 (org-test-with-temp-text "* [#B] H"
5445 (org-entry-put (point) "PRIORITY" "A")
5446 (buffer-string))))
5447 (should
5448 (equal "* H"
5449 (org-test-with-temp-text "* [#B] H"
5450 (org-entry-put (point) "PRIORITY" nil)
5451 (buffer-string))))
5452 ;; Set "SCHEDULED" property.
5453 (should
5454 (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
5455 (org-test-with-temp-text "* H"
5456 (org-entry-put (point) "SCHEDULED" "2014-03-04")
5457 (buffer-string))))
5458 (should
5459 (string= "* H\n"
5460 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
5461 (org-entry-put (point) "SCHEDULED" nil)
5462 (buffer-string))))
5463 (should
5464 (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
5465 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
5466 (org-entry-put (point) "SCHEDULED" "earlier")
5467 (buffer-string))))
5468 (should
5469 (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
5470 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
5471 (org-entry-put (point) "SCHEDULED" "later")
5472 (buffer-string))))
5473 ;; Set "DEADLINE" property.
5474 (should
5475 (string-match "^ *DEADLINE: <2014-03-04 .*?>"
5476 (org-test-with-temp-text "* H"
5477 (org-entry-put (point) "DEADLINE" "2014-03-04")
5478 (buffer-string))))
5479 (should
5480 (string= "* H\n"
5481 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
5482 (org-entry-put (point) "DEADLINE" nil)
5483 (buffer-string))))
5484 (should
5485 (string-match "^ *DEADLINE: <2014-03-03 .*?>"
5486 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
5487 (org-entry-put (point) "DEADLINE" "earlier")
5488 (buffer-string))))
5489 (should
5490 (string-match "^ *DEADLINE: <2014-03-05 .*?>"
5491 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
5492 (org-entry-put (point) "DEADLINE" "later")
5493 (buffer-string))))
5494 ;; Set "CATEGORY" property
5495 (should
5496 (string-match "^ *:CATEGORY: cat"
5497 (org-test-with-temp-text "* H"
5498 (org-entry-put (point) "CATEGORY" "cat")
5499 (buffer-string))))
5500 ;; Regular properties, with or without pre-existing drawer.
5501 (should
5502 (string-match "^ *:A: +2$"
5503 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
5504 (org-entry-put (point) "A" "2")
5505 (buffer-string))))
5506 (should
5507 (string-match "^ *:A: +1$"
5508 (org-test-with-temp-text "* H"
5509 (org-entry-put (point) "A" "1")
5510 (buffer-string))))
5511 ;; Special case: two consecutive headlines.
5512 (should
5513 (string-match "\\* A\n *:PROPERTIES:"
5514 (org-test-with-temp-text "* A\n** B"
5515 (org-entry-put (point) "A" "1")
5516 (buffer-string)))))
5518 (ert-deftest test-org/refresh-properties ()
5519 "Test `org-refresh-properties' specifications."
5520 (should
5521 (equal "1"
5522 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
5523 (org-refresh-properties "A" 'org-test)
5524 (get-text-property (point) 'org-test))))
5525 (should-not
5526 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
5527 (org-refresh-properties "B" 'org-test)
5528 (get-text-property (point) 'org-test)))
5529 ;; Handle properties only defined with extension syntax, i.e.,
5530 ;; "PROPERTY+".
5531 (should
5532 (equal "1"
5533 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A+: 1\n:END:"
5534 (org-refresh-properties "A" 'org-test)
5535 (get-text-property (point) 'org-test))))
5536 ;; When property is inherited, add text property to the whole
5537 ;; sub-tree.
5538 (should
5539 (equal "1"
5540 (org-test-with-temp-text
5541 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n<point>** H2"
5542 (let ((org-use-property-inheritance t))
5543 (org-refresh-properties "A" 'org-test))
5544 (get-text-property (point) 'org-test))))
5545 ;; When property is inherited, use global value across the whole
5546 ;; buffer. However local values have precedence.
5547 (should-not
5548 (equal "1"
5549 (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
5550 (org-mode-restart)
5551 (let ((org-use-property-inheritance nil))
5552 (org-refresh-properties "A" 'org-test))
5553 (get-text-property (point) 'org-test))))
5554 (should
5555 (equal "1"
5556 (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
5557 (org-mode-restart)
5558 (let ((org-use-property-inheritance t))
5559 (org-refresh-properties "A" 'org-test))
5560 (get-text-property (point) 'org-test))))
5561 (should
5562 (equal "2"
5563 (org-test-with-temp-text
5564 "#+PROPERTY: A 1\n<point>* H\n:PROPERTIES:\n:A: 2\n:END:"
5565 (org-mode-restart)
5566 (let ((org-use-property-inheritance t))
5567 (org-refresh-properties "A" 'org-test))
5568 (get-text-property (point) 'org-test)))))
5571 ;;; Radio Targets
5573 (ert-deftest test-org/update-radio-target-regexp ()
5574 "Test `org-update-radio-target-regexp' specifications."
5575 ;; Properly update cache with no previous radio target regexp.
5576 (should
5577 (eq 'link
5578 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
5579 (save-excursion (goto-char (point-max)) (org-element-context))
5580 (insert "<<<")
5581 (search-forward "o")
5582 (insert ">>>")
5583 (org-update-radio-target-regexp)
5584 (goto-char (point-max))
5585 (org-element-type (org-element-context)))))
5586 ;; Properly update cache with previous radio target regexp.
5587 (should
5588 (eq 'link
5589 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
5590 (save-excursion (goto-char (point-max)) (org-element-context))
5591 (insert "<<<")
5592 (search-forward "o")
5593 (insert ">>>")
5594 (org-update-radio-target-regexp)
5595 (search-backward "r")
5596 (delete-char 5)
5597 (insert "new")
5598 (org-update-radio-target-regexp)
5599 (goto-char (point-max))
5600 (delete-region (line-beginning-position) (point))
5601 (insert "new")
5602 (org-element-type (org-element-context))))))
5605 ;;; Refile
5607 (ert-deftest test-org/refile-get-targets ()
5608 "Test `org-refile-get-targets' specifications."
5609 ;; :maxlevel includes all headings above specified value.
5610 (should
5611 (equal '("H1" "H2" "H3")
5612 (org-test-with-temp-text "* H1\n** H2\n*** H3"
5613 (let ((org-refile-use-outline-path nil)
5614 (org-refile-targets `((nil :maxlevel . 3))))
5615 (mapcar #'car (org-refile-get-targets))))))
5616 (should
5617 (equal '("H1" "H2")
5618 (org-test-with-temp-text "* H1\n** H2\n*** H3"
5619 (let ((org-refile-use-outline-path nil)
5620 (org-refile-targets `((nil :maxlevel . 2))))
5621 (mapcar #'car (org-refile-get-targets))))))
5622 ;; :level limits targets to headlines with the specified level.
5623 (should
5624 (equal '("H2")
5625 (org-test-with-temp-text "* H1\n** H2\n*** H3"
5626 (let ((org-refile-use-outline-path nil)
5627 (org-refile-targets `((nil :level . 2))))
5628 (mapcar #'car (org-refile-get-targets))))))
5629 ;; :tag limits targets to headlines with specified tag.
5630 (should
5631 (equal '("H1")
5632 (org-test-with-temp-text "* H1 :foo:\n** H2\n*** H3 :bar:"
5633 (let ((org-refile-use-outline-path nil)
5634 (org-refile-targets `((nil :tag . "foo"))))
5635 (mapcar #'car (org-refile-get-targets))))))
5636 ;; :todo limits targets to headlines with specified TODO keyword.
5637 (should
5638 (equal '("H2")
5639 (org-test-with-temp-text "* H1\n** TODO H2\n*** DONE H3"
5640 (let ((org-refile-use-outline-path nil)
5641 (org-refile-targets `((nil :todo . "TODO"))))
5642 (mapcar #'car (org-refile-get-targets))))))
5643 ;; :regexp filters targets matching provided regexp.
5644 (should
5645 (equal '("F2" "F3")
5646 (org-test-with-temp-text "* H1\n** F2\n*** F3"
5647 (let ((org-refile-use-outline-path nil)
5648 (org-refile-targets `((nil :regexp . "F"))))
5649 (mapcar #'car (org-refile-get-targets))))))
5650 ;; A nil `org-refile-targets' includes only top level headlines in
5651 ;; current buffer.
5652 (should
5653 (equal '("H1" "H2")
5654 (org-test-with-temp-text "* H1\n** S1\n* H2"
5655 (let ((org-refile-use-outline-path nil)
5656 (org-refile-targets nil))
5657 (mapcar #'car (org-refile-get-targets))))))
5658 ;; Return value is the union of the targets according to all the
5659 ;; defined rules. However, prevent duplicates.
5660 (should
5661 (equal '("F2" "F3" "H1")
5662 (org-test-with-temp-text "* TODO H1\n** F2\n*** F3"
5663 (let ((org-refile-use-outline-path nil)
5664 (org-refile-targets `((nil :regexp . "F")
5665 (nil :todo . "TODO"))))
5666 (mapcar #'car (org-refile-get-targets))))))
5667 (should
5668 (equal '("F2" "F3" "H1")
5669 (org-test-with-temp-text "* TODO H1\n** TODO F2\n*** F3"
5670 (let ((org-refile-use-outline-path nil)
5671 (org-refile-targets `((nil :regexp . "F")
5672 (nil :todo . "TODO"))))
5673 (mapcar #'car (org-refile-get-targets))))))
5674 ;; When `org-refile-use-outline-path' is non-nil, provide targets as
5675 ;; paths.
5676 (should
5677 (equal '("H1" "H1/H2" "H1/H2/H3")
5678 (org-test-with-temp-text "* H1\n** H2\n*** H3"
5679 (let ((org-refile-use-outline-path t)
5680 (org-refile-targets `((nil :maxlevel . 3))))
5681 (mapcar #'car (org-refile-get-targets))))))
5682 ;; When providing targets as paths, escape forward slashes in
5683 ;; headings with backslashes.
5684 (should
5685 (equal '("H1\\/foo")
5686 (org-test-with-temp-text "* H1/foo"
5687 (let ((org-refile-use-outline-path t)
5688 (org-refile-targets `((nil :maxlevel . 1))))
5689 (mapcar #'car (org-refile-get-targets))))))
5690 ;; When `org-refile-use-outline-path' is `file', include file name
5691 ;; without directory in targets.
5692 (should
5693 (org-test-with-temp-text-in-file "* H1"
5694 (let* ((filename (buffer-file-name))
5695 (org-refile-use-outline-path 'file)
5696 (org-refile-targets `(((,filename) :level . 1))))
5697 (member (file-name-nondirectory filename)
5698 (mapcar #'car (org-refile-get-targets))))))
5699 ;; When `org-refile-use-outline-path' is `full-file-path', include
5700 ;; full file name.
5701 (should
5702 (org-test-with-temp-text-in-file "* H1"
5703 (let* ((filename (file-truename (buffer-file-name)))
5704 (org-refile-use-outline-path 'full-file-path)
5705 (org-refile-targets `(((,filename) :level . 1))))
5706 (member filename (mapcar #'car (org-refile-get-targets))))))
5707 ;; When `org-refile-use-outline-path' is `buffer-name', include
5708 ;; buffer name.
5709 (should
5710 (org-test-with-temp-text "* H1"
5711 (let* ((org-refile-use-outline-path 'buffer-name)
5712 (org-refile-targets `((nil :level . 1))))
5713 (member (buffer-name) (mapcar #'car (org-refile-get-targets)))))))
5717 ;;; Sparse trees
5719 (ert-deftest test-org/match-sparse-tree ()
5720 "Test `org-match-sparse-tree' specifications."
5721 ;; Match tags.
5722 (should-not
5723 (org-test-with-temp-text "* H\n** H1 :tag:"
5724 (org-match-sparse-tree nil "tag")
5725 (search-forward "H1")
5726 (org-invisible-p2)))
5727 (should
5728 (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
5729 (org-match-sparse-tree nil "tag")
5730 (search-forward "H2")
5731 (org-invisible-p2)))
5732 ;; "-" operator for tags.
5733 (should-not
5734 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
5735 (org-match-sparse-tree nil "tag1-tag2")
5736 (search-forward "H1")
5737 (org-invisible-p2)))
5738 (should
5739 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
5740 (org-match-sparse-tree nil "tag1-tag2")
5741 (search-forward "H2")
5742 (org-invisible-p2)))
5743 ;; "&" operator for tags.
5744 (should
5745 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
5746 (org-match-sparse-tree nil "tag1&tag2")
5747 (search-forward "H1")
5748 (org-invisible-p2)))
5749 (should-not
5750 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
5751 (org-match-sparse-tree nil "tag1&tag2")
5752 (search-forward "H2")
5753 (org-invisible-p2)))
5754 ;; "|" operator for tags.
5755 (should-not
5756 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
5757 (org-match-sparse-tree nil "tag1|tag2")
5758 (search-forward "H1")
5759 (org-invisible-p2)))
5760 (should-not
5761 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
5762 (org-match-sparse-tree nil "tag1|tag2")
5763 (search-forward "H2")
5764 (org-invisible-p2)))
5765 ;; Regexp match on tags.
5766 (should-not
5767 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
5768 (org-match-sparse-tree nil "{^tag.*}")
5769 (search-forward "H1")
5770 (org-invisible-p2)))
5771 (should
5772 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
5773 (org-match-sparse-tree nil "{^tag.*}")
5774 (search-forward "H2")
5775 (org-invisible-p2)))
5776 ;; Match group tags.
5777 (should-not
5778 (org-test-with-temp-text
5779 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
5780 (org-match-sparse-tree nil "work")
5781 (search-forward "H1")
5782 (org-invisible-p2)))
5783 (should-not
5784 (org-test-with-temp-text
5785 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
5786 (org-match-sparse-tree nil "work")
5787 (search-forward "H2")
5788 (org-invisible-p2)))
5789 ;; Match group tags with hard brackets.
5790 (should-not
5791 (org-test-with-temp-text
5792 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
5793 (org-match-sparse-tree nil "work")
5794 (search-forward "H1")
5795 (org-invisible-p2)))
5796 (should-not
5797 (org-test-with-temp-text
5798 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
5799 (org-match-sparse-tree nil "work")
5800 (search-forward "H2")
5801 (org-invisible-p2)))
5802 ;; Match tags in hierarchies
5803 (should-not
5804 (org-test-with-temp-text
5805 "#+TAGS: [ Lev_1 : Lev_2 ]\n
5806 #+TAGS: [ Lev_2 : Lev_3 ]\n
5807 #+TAGS: { Lev_3 : Lev_4 }\n
5808 * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
5809 (org-match-sparse-tree nil "Lev_1")
5810 (search-forward "H4")
5811 (org-invisible-p2)))
5812 (should-not
5813 (org-test-with-temp-text
5814 "#+TAGS: [ Lev_1 : Lev_2 ]\n
5815 #+TAGS: [ Lev_2 : Lev_3 ]\n
5816 #+TAGS: { Lev_3 : Lev_4 }\n
5817 * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
5818 (org-match-sparse-tree nil "Lev_1+Lev_3")
5819 (search-forward "H4")
5820 (org-invisible-p2)))
5821 ;; Match regular expressions in tags
5822 (should-not
5823 (org-test-with-temp-text
5824 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
5825 (org-match-sparse-tree nil "Lev")
5826 (search-forward "H1")
5827 (org-invisible-p2)))
5828 (should
5829 (org-test-with-temp-text
5830 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
5831 (org-match-sparse-tree nil "Lev")
5832 (search-forward "H1")
5833 (org-invisible-p2)))
5834 ;; Match properties.
5835 (should
5836 (org-test-with-temp-text
5837 "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
5838 (org-match-sparse-tree nil "A=\"1\"")
5839 (search-forward "H2")
5840 (org-invisible-p2)))
5841 (should-not
5842 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
5843 (org-match-sparse-tree nil "A=\"1\"")
5844 (search-forward "H2")
5845 (org-invisible-p2)))
5846 ;; Case is not significant when matching properties.
5847 (should-not
5848 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
5849 (org-match-sparse-tree nil "a=\"1\"")
5850 (search-forward "H2")
5851 (org-invisible-p2)))
5852 (should-not
5853 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
5854 (org-match-sparse-tree nil "A=\"1\"")
5855 (search-forward "H2")
5856 (org-invisible-p2)))
5857 ;; Match special LEVEL property.
5858 (should-not
5859 (org-test-with-temp-text "* H\n** H1\n*** H2"
5860 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
5861 (search-forward "H1")
5862 (org-invisible-p2)))
5863 (should
5864 (org-test-with-temp-text "* H\n** H1\n*** H2"
5865 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
5866 (search-forward "H2")
5867 (org-invisible-p2)))
5868 ;; Comparison operators when matching properties.
5869 (should
5870 (org-test-with-temp-text
5871 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
5872 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
5873 (search-forward "H1")
5874 (org-invisible-p2)))
5875 (should-not
5876 (org-test-with-temp-text
5877 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
5878 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
5879 (search-forward "H2")
5880 (org-invisible-p2)))
5881 ;; Regexp match on properties values.
5882 (should-not
5883 (org-test-with-temp-text
5884 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
5885 (org-match-sparse-tree nil "A={f.*}")
5886 (search-forward "H1")
5887 (org-invisible-p2)))
5888 (should
5889 (org-test-with-temp-text
5890 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
5891 (org-match-sparse-tree nil "A={f.*}")
5892 (search-forward "H2")
5893 (org-invisible-p2)))
5894 ;; With an optional argument, limit match to TODO entries.
5895 (should-not
5896 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
5897 (org-match-sparse-tree t "tag")
5898 (search-forward "H1")
5899 (org-invisible-p2)))
5900 (should
5901 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
5902 (org-match-sparse-tree t "tag")
5903 (search-forward "H2")
5904 (org-invisible-p2))))
5906 (ert-deftest test-org/occur ()
5907 "Test `org-occur' specifications."
5908 ;; Count number of matches.
5909 (should
5910 (= 1
5911 (org-test-with-temp-text "* H\nA\n* H2"
5912 (org-occur "A"))))
5913 (should
5914 (= 2
5915 (org-test-with-temp-text "* H\nA\n* H2\nA"
5916 (org-occur "A"))))
5917 ;; Test CALLBACK optional argument.
5918 (should
5919 (= 0
5920 (org-test-with-temp-text "* H\nA\n* H2"
5921 (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
5922 (should
5923 (= 1
5924 (org-test-with-temp-text "* H\nA\n* H2\nA"
5925 (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
5926 ;; Case-fold searches according to `org-occur-case-fold-search'.
5927 (should
5928 (= 2
5929 (org-test-with-temp-text "Aa"
5930 (let ((org-occur-case-fold-search t)) (org-occur "A")))))
5931 (should
5932 (= 2
5933 (org-test-with-temp-text "Aa"
5934 (let ((org-occur-case-fold-search t)) (org-occur "a")))))
5935 (should
5936 (= 1
5937 (org-test-with-temp-text "Aa"
5938 (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
5939 (should
5940 (= 1
5941 (org-test-with-temp-text "Aa"
5942 (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
5943 (should
5944 (= 1
5945 (org-test-with-temp-text "Aa"
5946 (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
5947 (should
5948 (= 2
5949 (org-test-with-temp-text "Aa"
5950 (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
5953 ;;; Tags
5955 (ert-deftest test-org/tag-string-to-alist ()
5956 "Test `org-tag-string-to-alist' specifications."
5957 ;; Tag without selection key.
5958 (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
5959 ;; Tag with selection key.
5960 (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
5961 ;; Tag group.
5962 (should
5963 (equal
5964 (org-tag-string-to-alist "[ group : t1 t2 ]")
5965 '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
5966 ;; Mutually exclusive tags.
5967 (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
5968 '((:startgroup) ("tag1") ("tag2") (:endgroup))))
5969 (should
5970 (equal
5971 (org-tag-string-to-alist "{ group : tag1 tag2 }")
5972 '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
5974 (ert-deftest test-org/tag-alist-to-string ()
5975 "Test `org-tag-alist-to-string' specifications."
5976 (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
5977 (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
5978 (should
5979 (equal
5980 (org-tag-alist-to-string
5981 '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
5982 "[ group : t1 t2 ]"))
5983 (should
5984 (equal (org-tag-alist-to-string
5985 '((:startgroup) ("tag1") ("tag2") (:endgroup)))
5986 "{ tag1 tag2 }"))
5987 (should
5988 (equal
5989 (org-tag-alist-to-string
5990 '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
5991 "{ group : tag1 tag2 }")))
5993 (ert-deftest test-org/tag-alist-to-groups ()
5994 "Test `org-tag-alist-to-groups' specifications."
5995 (should
5996 (equal (org-tag-alist-to-groups
5997 '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
5998 '(("group" "t1" "t2"))))
5999 (should
6000 (equal
6001 (org-tag-alist-to-groups
6002 '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
6003 '(("group" "t1" "t2"))))
6004 (should-not
6005 (org-tag-alist-to-groups
6006 '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
6008 (ert-deftest test-org/tag-align ()
6009 "Test tags alignment."
6010 ;; Test aligning tags with different display width.
6011 (should
6012 ;; 12345678901234567890
6013 (equal "* Test :abc:"
6014 (org-test-with-temp-text "* Test :abc:"
6015 (let ((org-tags-column -20)
6016 (indent-tabs-mode nil))
6017 (org-fix-tags-on-the-fly))
6018 (buffer-string))))
6019 (should
6020 ;; 12345678901234567890
6021 (equal "* Test :日本語:"
6022 (org-test-with-temp-text "* Test :日本語:"
6023 (let ((org-tags-column -20)
6024 (indent-tabs-mode nil))
6025 (org-fix-tags-on-the-fly))
6026 (buffer-string))))
6027 ;; Make sure aligning tags do not skip invisible text.
6028 (should
6029 (equal "* [[linkx]] :tag:"
6030 (org-test-with-temp-text "* [[link<point>]] :tag:"
6031 (let ((org-tags-column 0))
6032 (org-fix-tags-on-the-fly)
6033 (insert "x")
6034 (buffer-string))))))
6036 (ert-deftest test-org/tags-at ()
6037 (should
6038 (equal '("foo" "bar")
6039 (org-test-with-temp-text
6040 "* T<point>est :foo:bar:"
6041 (org-get-tags-at)))))
6043 (ert-deftest test-org/set-tags ()
6044 "Test `org-set-tags' specifications."
6045 ;; Tags set via fast-tag-selection should be visible afterwards
6046 (should
6047 (let ((org-tag-alist '(("NEXT" . ?n)))
6048 (org-fast-tag-selection-single-key t))
6049 (cl-letf (((symbol-function 'read-char-exclusive) (lambda () ?n))
6050 ((symbol-function 'window-width) (lambda (&rest args) 100)))
6051 (org-test-with-temp-text "<point>* Headline\nAnd its content\n* And another headline\n\nWith some content"
6052 ;; Show only headlines
6053 (org-content)
6054 ;; Set NEXT tag on current entry
6055 (org-set-tags nil nil)
6056 ;; Move point to that NEXT tag
6057 (search-forward "NEXT") (backward-word)
6058 ;; And it should be visible (i.e. no overlays)
6059 (not (overlays-at (point))))))))
6061 (ert-deftest test-org/set-tags-to ()
6062 "Test `org-set-tags-to' specifications."
6063 ;; Throw an error on invalid data.
6064 (should-error
6065 (org-test-with-temp-text "* H"
6066 (org-set-tags-to 'foo)))
6067 ;; `nil', an empty, and a blank string remove all tags.
6068 (should
6069 (equal "* H"
6070 (org-test-with-temp-text "* H :tag1:tag2:"
6071 (org-set-tags-to nil)
6072 (buffer-string))))
6073 (should
6074 (equal "* H"
6075 (org-test-with-temp-text "* H :tag1:tag2:"
6076 (org-set-tags-to "")
6077 (buffer-string))))
6078 (should
6079 (equal "* H"
6080 (org-test-with-temp-text "* H :tag1:tag2:"
6081 (org-set-tags-to " ")
6082 (buffer-string))))
6083 ;; If there's nothing to remove, just bail out.
6084 (should
6085 (equal "* H"
6086 (org-test-with-temp-text "* H"
6087 (org-set-tags-to nil)
6088 (buffer-string))))
6089 (should
6090 (equal "* "
6091 (org-test-with-temp-text "* "
6092 (org-set-tags-to nil)
6093 (buffer-string))))
6094 ;; If DATA is a tag string, set current tags to it, even if it means
6095 ;; replacing old tags.
6096 (should
6097 (equal "* H :tag0:"
6098 (org-test-with-temp-text "* H :tag1:tag2:"
6099 (org-set-tags-to ":tag0:")
6100 (buffer-string))))
6101 (should
6102 (equal "* H :tag0:"
6103 (org-test-with-temp-text "* H"
6104 (org-set-tags-to ":tag0:")
6105 (buffer-string))))
6106 ;; If DATA is a list, set tags to this list, even if it means
6107 ;; replacing old tags.
6108 (should
6109 (equal "* H :tag0:"
6110 (org-test-with-temp-text "* H :tag1:tag2:"
6111 (org-set-tags-to '("tag0"))
6112 (buffer-string))))
6113 (should
6114 (equal "* H :tag0:"
6115 (org-test-with-temp-text "* H"
6116 (org-set-tags-to '("tag0"))
6117 (buffer-string))))
6118 ;; Special case: handle empty headlines.
6119 (should
6120 (equal "* :tag0:"
6121 (org-test-with-temp-text "* "
6122 (org-set-tags-to '("tag0"))
6123 (buffer-string)))))
6126 ;;; TODO keywords
6128 (ert-deftest test-org/auto-repeat-maybe ()
6129 "Test `org-auto-repeat-maybe' specifications."
6130 ;; Do not auto repeat when there is no valid time stamp with
6131 ;; a repeater in the entry.
6132 (should-not
6133 (string-prefix-p
6134 "* TODO H"
6135 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
6136 (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu>"
6137 (org-todo "DONE")
6138 (buffer-string)))))
6139 (should-not
6140 (string-prefix-p
6141 "* TODO H"
6142 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
6143 (org-test-with-temp-text "* TODO H\n# <2012-03-29 Thu>"
6144 (org-todo "DONE")
6145 (buffer-string)))))
6146 ;; When switching to DONE state, switch back to first TODO keyword
6147 ;; in sequence, or the same keyword if they have different types.
6148 (should
6149 (string-prefix-p
6150 "* TODO H"
6151 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
6152 (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
6153 (org-todo "DONE")
6154 (buffer-string)))))
6155 (should
6156 (string-prefix-p
6157 "* KWD1 H"
6158 (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
6159 (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
6160 (org-todo "DONE")
6161 (buffer-string)))))
6162 (should
6163 (string-prefix-p
6164 "* KWD2 H"
6165 (let ((org-todo-keywords '((type "KWD1" "KWD2" "DONE"))))
6166 (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
6167 (org-todo "DONE")
6168 (buffer-string)))))
6169 ;; If there was no TODO keyword in the first place, do not insert
6170 ;; any either.
6171 (should
6172 (string-prefix-p
6173 "* H"
6174 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
6175 (org-test-with-temp-text "* H\n<2012-03-29 Thu +2y>"
6176 (org-todo "DONE")
6177 (buffer-string)))))
6178 ;; Revert to REPEAT_TO_STATE, if set.
6179 (should
6180 (string-prefix-p
6181 "* KWD2 H"
6182 (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
6183 (org-test-with-temp-text
6184 "* KWD2 H
6185 :PROPERTIES:
6186 :REPEAT_TO_STATE: KWD2
6187 :END:
6188 <2012-03-29 Thu +2y>"
6189 (org-todo "DONE")
6190 (buffer-string)))))
6191 ;; When switching to DONE state, update base date. If there are
6192 ;; multiple repeated time stamps, update them all.
6193 (should
6194 (string-match-p
6195 "<2014-03-29 .* \\+2y>"
6196 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
6197 (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
6198 (org-todo "DONE")
6199 (buffer-string)))))
6200 (should
6201 (string-match-p
6202 "<2015-03-04 .* \\+1y>"
6203 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
6204 (org-test-with-temp-text
6205 "* TODO H\n<2012-03-29 Thu. +2y>\n<2014-03-04 Tue +1y>"
6206 (org-todo "DONE")
6207 (buffer-string)))))
6208 ;; Throw an error if repeater unit is the hour and no time is
6209 ;; provided in the time-stamp.
6210 (should-error
6211 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
6212 (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2h>"
6213 (org-todo "DONE")
6214 (buffer-string))))
6215 ;; Do not repeat commented time stamps.
6216 (should-not
6217 (string-prefix-p
6218 "<2015-03-04 .* \\+1y>"
6219 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
6220 (org-test-with-temp-text
6221 "* TODO H\n<2012-03-29 Thu +2y>\n# <2014-03-04 Tue +1y>"
6222 (org-todo "DONE")
6223 (buffer-string)))))
6224 (should-not
6225 (string-prefix-p
6226 "<2015-03-04 .* \\+1y>"
6227 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
6228 (org-test-with-temp-text
6229 "* TODO H
6230 <2012-03-29 Thu. +2y>
6231 #+BEGIN_EXAMPLE
6232 <2014-03-04 Tue +1y>
6233 #+END_EXAMPLE"
6234 (org-todo "DONE")
6235 (buffer-string)))))
6236 ;; When `org-log-repeat' is non-nil or there is a CLOCK in the
6237 ;; entry, record time of last repeat.
6238 (should-not
6239 (string-match-p
6240 ":LAST_REPEAT:"
6241 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
6242 (org-log-repeat nil))
6243 (cl-letf (((symbol-function 'org-add-log-setup)
6244 (lambda (&rest args) nil)))
6245 (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
6246 (org-todo "DONE")
6247 (buffer-string))))))
6248 (should
6249 (string-match-p
6250 ":LAST_REPEAT:"
6251 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
6252 (org-log-repeat t))
6253 (cl-letf (((symbol-function 'org-add-log-setup)
6254 (lambda (&rest args) nil)))
6255 (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
6256 (org-todo "DONE")
6257 (buffer-string))))))
6258 (should
6259 (string-match-p
6260 ":LAST_REPEAT:"
6261 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
6262 (cl-letf (((symbol-function 'org-add-log-setup)
6263 (lambda (&rest args) nil)))
6264 (org-test-with-temp-text
6265 "* TODO H\n<2012-03-29 Thu +2y>\nCLOCK: [2012-03-29 Thu 16:40]"
6266 (org-todo "DONE")
6267 (buffer-string))))))
6268 ;; When a SCHEDULED entry has no repeater, remove it upon repeating
6269 ;; the entry as it is no longer relevant.
6270 (should-not
6271 (string-match-p
6272 "^SCHEDULED:"
6273 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
6274 (org-test-with-temp-text
6275 "* TODO H\nSCHEDULED: <2014-03-04 Tue>\n<2012-03-29 Thu +2y>"
6276 (org-todo "DONE")
6277 (buffer-string)))))
6278 ;; Properly advance repeater even when a clock entry is specified
6279 ;; and `org-log-repeat' is nil.
6280 (should
6281 (string-match-p
6282 "SCHEDULED: <2014-03-29"
6283 (let ((org-log-repeat nil)
6284 (org-todo-keywords '((sequence "TODO" "DONE"))))
6285 (org-test-with-temp-text
6286 "* TODO H
6287 SCHEDULED: <2012-03-29 Thu +2y>
6288 CLOCK: [2012-03-29 Thu 10:00]--[2012-03-29 Thu 16:40] => 6:40"
6289 (org-todo "DONE")
6290 (buffer-string))))))
6293 ;;; Timestamps API
6295 (ert-deftest test-org/at-timestamp-p ()
6296 "Test `org-at-timestamp-p' specifications."
6297 (should
6298 (org-test-with-temp-text "<2012-03-29 Thu>"
6299 (org-at-timestamp-p)))
6300 (should-not
6301 (org-test-with-temp-text "2012-03-29 Thu"
6302 (org-at-timestamp-p)))
6303 ;; Test return values.
6304 (should
6305 (eq 'bracket
6306 (org-test-with-temp-text "<2012-03-29 Thu>"
6307 (org-at-timestamp-p))))
6308 (should
6309 (eq 'year
6310 (org-test-with-temp-text "<<point>2012-03-29 Thu>"
6311 (org-at-timestamp-p))))
6312 (should
6313 (eq 'month
6314 (org-test-with-temp-text "<2012-<point>03-29 Thu>"
6315 (org-at-timestamp-p))))
6316 (should
6317 (eq 'day
6318 (org-test-with-temp-text "<2012-03-<point>29 Thu>"
6319 (org-at-timestamp-p))))
6320 (should
6321 (eq 'day
6322 (org-test-with-temp-text "<2012-03-29 T<point>hu>"
6323 (org-at-timestamp-p))))
6324 (should
6325 (wholenump
6326 (org-test-with-temp-text "<2012-03-29 Thu +2<point>y>"
6327 (org-at-timestamp-p))))
6328 (should
6329 (eq 'bracket
6330 (org-test-with-temp-text "<2012-03-29 Thu<point>>"
6331 (org-at-timestamp-p))))
6332 (should
6333 (eq 'after
6334 (org-test-with-temp-text "<2012-03-29 Thu><point>»"
6335 (org-at-timestamp-p))))
6336 ;; Test `inactive' optional argument.
6337 (should
6338 (org-test-with-temp-text "[2012-03-29 Thu]"
6339 (org-at-timestamp-p 'inactive)))
6340 (should-not
6341 (org-test-with-temp-text "[2012-03-29 Thu]"
6342 (org-at-timestamp-p)))
6343 ;; When optional argument is `agenda', recognize time-stamps in
6344 ;; planning info line, property drawers and clocks.
6345 (should
6346 (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
6347 (org-at-timestamp-p 'agenda)))
6348 (should-not
6349 (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
6350 (org-at-timestamp-p)))
6351 (should
6352 (org-test-with-temp-text
6353 "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
6354 (org-at-timestamp-p 'agenda)))
6355 (should-not
6356 (org-test-with-temp-text
6357 "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
6358 (org-at-timestamp-p)))
6359 (should
6360 (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
6361 (let ((org-agenda-include-inactive-timestamps t))
6362 (org-at-timestamp-p 'agenda))))
6363 (should-not
6364 (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
6365 (let ((org-agenda-include-inactive-timestamps t))
6366 (org-at-timestamp-p))))
6367 (should-not
6368 (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
6369 (let ((org-agenda-include-inactive-timestamps t))
6370 (org-at-timestamp-p 'inactive))))
6371 ;; When optional argument is `lax', match any part of the document
6372 ;; with Org timestamp syntax.
6373 (should
6374 (org-test-with-temp-text "# <2012-03-29 Thu><point>"
6375 (org-at-timestamp-p 'lax)))
6376 (should-not
6377 (org-test-with-temp-text "# <2012-03-29 Thu><point>"
6378 (org-at-timestamp-p)))
6379 (should
6380 (org-test-with-temp-text ": <2012-03-29 Thu><point>"
6381 (org-at-timestamp-p 'lax)))
6382 (should-not
6383 (org-test-with-temp-text ": <2012-03-29 Thu><point>"
6384 (org-at-timestamp-p)))
6385 (should
6386 (org-test-with-temp-text
6387 "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
6388 (org-at-timestamp-p 'lax)))
6389 (should-not
6390 (org-test-with-temp-text
6391 "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
6392 (org-at-timestamp-p)))
6393 ;; Optional argument `lax' also matches inactive timestamps.
6394 (should
6395 (org-test-with-temp-text "# [2012-03-29 Thu]<point>"
6396 (org-at-timestamp-p 'lax))))
6398 (ert-deftest test-org/time-stamp ()
6399 "Test `org-time-stamp' specifications."
6400 ;; Insert chosen time stamp at point.
6401 (should
6402 (string-match
6403 "Te<2014-03-04 .*?>xt"
6404 (org-test-with-temp-text "Te<point>xt"
6405 (cl-letf (((symbol-function 'org-read-date)
6406 (lambda (&rest args)
6407 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
6408 (org-time-stamp nil)
6409 (buffer-string)))))
6410 ;; With a prefix argument, also insert time.
6411 (should
6412 (string-match
6413 "Te<2014-03-04 .*? 00:41>xt"
6414 (org-test-with-temp-text "Te<point>xt"
6415 (cl-letf (((symbol-function 'org-read-date)
6416 (lambda (&rest args)
6417 (apply #'encode-time
6418 (org-parse-time-string "2014-03-04 00:41")))))
6419 (org-time-stamp '(4))
6420 (buffer-string)))))
6421 ;; With two universal prefix arguments, insert an active timestamp
6422 ;; with the current time without prompting the user.
6423 (should
6424 (string-match
6425 "Te<2014-03-04 .*? 00:41>xt"
6426 (org-test-with-temp-text "Te<point>xt"
6427 (cl-letf (((symbol-function 'current-time)
6428 (lambda ()
6429 (apply #'encode-time
6430 (org-parse-time-string "2014-03-04 00:41")))))
6431 (org-time-stamp '(16))
6432 (buffer-string)))))
6433 ;; When optional argument is non-nil, insert an inactive timestamp.
6434 (should
6435 (string-match
6436 "Te\\[2014-03-04 .*?\\]xt"
6437 (org-test-with-temp-text "Te<point>xt"
6438 (cl-letf (((symbol-function 'org-read-date)
6439 (lambda (&rest args)
6440 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
6441 (org-time-stamp nil t)
6442 (buffer-string)))))
6443 ;; When called from a timestamp, replace existing one.
6444 (should
6445 (string-match
6446 "<2014-03-04 .*?>"
6447 (org-test-with-temp-text "<2012-03-29<point> thu.>"
6448 (cl-letf (((symbol-function 'org-read-date)
6449 (lambda (&rest args)
6450 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
6451 (org-time-stamp nil)
6452 (buffer-string)))))
6453 (should
6454 (string-match
6455 "<2014-03-04 .*?>--<2014-03-04 .*?>"
6456 (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
6457 (cl-letf (((symbol-function 'org-read-date)
6458 (lambda (&rest args)
6459 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
6460 (org-time-stamp nil)
6461 (buffer-string)))))
6462 ;; When replacing a timestamp, preserve repeater, if any.
6463 (should
6464 (string-match
6465 "<2014-03-04 .*? \\+2y>"
6466 (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
6467 (cl-letf (((symbol-function 'org-read-date)
6468 (lambda (&rest args)
6469 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
6470 (org-time-stamp nil)
6471 (buffer-string)))))
6472 ;; When called twice in a raw, build a date range.
6473 (should
6474 (string-match
6475 "<2012-03-29 .*?>--<2014-03-04 .*?>"
6476 (org-test-with-temp-text "<2012-03-29 thu.><point>"
6477 (cl-letf (((symbol-function 'org-read-date)
6478 (lambda (&rest args)
6479 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
6480 (let ((last-command 'org-time-stamp)
6481 (this-command 'org-time-stamp))
6482 (org-time-stamp nil))
6483 (buffer-string))))))
6485 (ert-deftest test-org/timestamp-has-time-p ()
6486 "Test `org-timestamp-has-time-p' specifications."
6487 ;; With time.
6488 (should
6489 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
6490 (org-timestamp-has-time-p (org-element-context))))
6491 ;; Without time.
6492 (should-not
6493 (org-test-with-temp-text "<2012-03-29 Thu>"
6494 (org-timestamp-has-time-p (org-element-context)))))
6496 (ert-deftest test-org/get-repeat ()
6497 "Test `org-get-repeat' specifications."
6498 (should
6499 (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40 +2y>"
6500 (org-get-repeat)))
6501 (should-not
6502 (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40>"
6503 (org-get-repeat)))
6504 ;; Return proper repeat string.
6505 (should
6506 (equal "+2y"
6507 (org-test-with-temp-text "* H\n<2014-03-04 Tue 16:40 +2y>"
6508 (org-get-repeat))))
6509 ;; Prevent false positive (commented or verbatim time stamps)
6510 (should-not
6511 (org-test-with-temp-text "* H\n# <2012-03-29 Thu 16:40>"
6512 (org-get-repeat)))
6513 (should-not
6514 (org-test-with-temp-text
6515 "* H\n#+BEGIN_EXAMPLE\n<2012-03-29 Thu 16:40>\n#+END_EXAMPLE"
6516 (org-get-repeat)))
6517 ;; Return nil when called before first heading.
6518 (should-not
6519 (org-test-with-temp-text "<2012-03-29 Thu 16:40 +2y>"
6520 (org-get-repeat)))
6521 ;; When called with an optional argument, extract repeater from that
6522 ;; string instead.
6523 (should (equal "+2y" (org-get-repeat "<2012-03-29 Thu 16:40 +2y>")))
6524 (should-not (org-get-repeat "<2012-03-29 Thu 16:40>")))
6526 (ert-deftest test-org/timestamp-format ()
6527 "Test `org-timestamp-format' specifications."
6528 ;; Regular test.
6529 (should
6530 (equal
6531 "2012-03-29 16:40"
6532 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
6533 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
6534 ;; Range end.
6535 (should
6536 (equal
6537 "2012-03-29"
6538 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
6539 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
6541 (ert-deftest test-org/timestamp-split-range ()
6542 "Test `org-timestamp-split-range' specifications."
6543 ;; Extract range start (active).
6544 (should
6545 (equal '(2012 3 29)
6546 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
6547 (let ((ts (org-timestamp-split-range (org-element-context))))
6548 (mapcar (lambda (p) (org-element-property p ts))
6549 '(:year-end :month-end :day-end))))))
6550 ;; Extract range start (inactive)
6551 (should
6552 (equal '(2012 3 29)
6553 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
6554 (let ((ts (org-timestamp-split-range (org-element-context))))
6555 (mapcar (lambda (p) (org-element-property p ts))
6556 '(:year-end :month-end :day-end))))))
6557 ;; Extract range end (active).
6558 (should
6559 (equal '(2012 3 30)
6560 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
6561 (let ((ts (org-timestamp-split-range
6562 (org-element-context) t)))
6563 (mapcar (lambda (p) (org-element-property p ts))
6564 '(:year-end :month-end :day-end))))))
6565 ;; Extract range end (inactive)
6566 (should
6567 (equal '(2012 3 30)
6568 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
6569 (let ((ts (org-timestamp-split-range
6570 (org-element-context) t)))
6571 (mapcar (lambda (p) (org-element-property p ts))
6572 '(:year-end :month-end :day-end))))))
6573 ;; Return the timestamp if not a range.
6574 (should
6575 (org-test-with-temp-text "[2012-03-29 Thu]"
6576 (let* ((ts-orig (org-element-context))
6577 (ts-copy (org-timestamp-split-range ts-orig)))
6578 (eq ts-orig ts-copy))))
6579 (should
6580 (org-test-with-temp-text "<%%(org-float t 4 2)>"
6581 (let* ((ts-orig (org-element-context))
6582 (ts-copy (org-timestamp-split-range ts-orig)))
6583 (eq ts-orig ts-copy)))))
6585 (ert-deftest test-org/timestamp-translate ()
6586 "Test `org-timestamp-translate' specifications."
6587 ;; Translate whole date range.
6588 (should
6589 (equal "<29>--<30>"
6590 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
6591 (let ((org-display-custom-times t)
6592 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
6593 (org-timestamp-translate (org-element-context))))))
6594 ;; Translate date range start.
6595 (should
6596 (equal "<29>"
6597 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
6598 (let ((org-display-custom-times t)
6599 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
6600 (org-timestamp-translate (org-element-context) 'start)))))
6601 ;; Translate date range end.
6602 (should
6603 (equal "<30>"
6604 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
6605 (let ((org-display-custom-times t)
6606 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
6607 (org-timestamp-translate (org-element-context) 'end)))))
6608 ;; Translate time range.
6609 (should
6610 (equal "<08>--<16>"
6611 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
6612 (let ((org-display-custom-times t)
6613 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
6614 (org-timestamp-translate (org-element-context))))))
6615 ;; Translate non-range timestamp.
6616 (should
6617 (equal "<29>"
6618 (org-test-with-temp-text "<2012-03-29 Thu>"
6619 (let ((org-display-custom-times t)
6620 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
6621 (org-timestamp-translate (org-element-context))))))
6622 ;; Do not change `diary' timestamps.
6623 (should
6624 (equal "<%%(org-float t 4 2)>"
6625 (org-test-with-temp-text "<%%(org-float t 4 2)>"
6626 (let ((org-display-custom-times t)
6627 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
6628 (org-timestamp-translate (org-element-context)))))))
6630 (ert-deftest test-org/timestamp-from-string ()
6631 "Test `org-timestamp-from-string' specifications."
6632 ;; Return nil if argument is not a valid Org timestamp.
6633 (should-not (org-timestamp-from-string ""))
6634 (should-not (org-timestamp-from-string nil))
6635 (should-not (org-timestamp-from-string "<2012-03-29"))
6636 ;; Otherwise, return a valid Org timestamp object.
6637 (should
6638 (equal "<2012-03-29 Thu>"
6639 (let ((system-time-locale "en_US"))
6640 (org-element-interpret-data
6641 (org-timestamp-from-string "<2012-03-29 Thu>")))))
6642 (should
6643 (equal "[2014-03-04 Tue]"
6644 (let ((system-time-locale "en_US"))
6645 (org-element-interpret-data
6646 (org-timestamp-from-string "[2014-03-04 Tue]"))))))
6648 (ert-deftest test-org/timestamp-from-time ()
6649 "Test `org-timestamp-from-time' specifications."
6650 ;; Standard test.
6651 (should
6652 (equal "<2012-03-29 Thu>"
6653 (let ((system-time-locale "en_US"))
6654 (org-element-interpret-data
6655 (org-timestamp-from-time
6656 (apply #'encode-time
6657 (org-parse-time-string "<2012-03-29 Thu 16:40>")))))))
6658 ;; When optional argument WITH-TIME is non-nil, provide time
6659 ;; information.
6660 (should
6661 (equal "<2012-03-29 Thu 16:40>"
6662 (let ((system-time-locale "en_US"))
6663 (org-element-interpret-data
6664 (org-timestamp-from-time
6665 (apply #'encode-time
6666 (org-parse-time-string "<2012-03-29 Thu 16:40>"))
6667 t)))))
6668 ;; When optional argument INACTIVE is non-nil, return an inactive
6669 ;; timestamp.
6670 (should
6671 (equal "[2012-03-29 Thu]"
6672 (let ((system-time-locale "en_US"))
6673 (org-element-interpret-data
6674 (org-timestamp-from-time
6675 (apply #'encode-time
6676 (org-parse-time-string "<2012-03-29 Thu 16:40>"))
6677 nil t))))))
6679 (ert-deftest test-org/timestamp-to-time ()
6680 "Test `org-timestamp-to-time' specifications."
6681 (should
6682 (equal "2014-03-04"
6683 (format-time-string
6684 "%Y-%m-%d"
6685 (org-timestamp-to-time
6686 (org-timestamp-from-string "<2014-03-04 Tue>")))))
6687 (should
6688 (equal "2014-03-04"
6689 (format-time-string
6690 "%Y-%m-%d"
6691 (org-timestamp-to-time
6692 (org-timestamp-from-string "[2014-03-04 Tue]")))))
6693 (should
6694 (equal "2012-03-29 08:30"
6695 (format-time-string
6696 "%Y-%m-%d %H:%M"
6697 (org-timestamp-to-time
6698 (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")))))
6699 (should
6700 (equal "2012-03-29"
6701 (format-time-string
6702 "%Y-%m-%d"
6703 (org-timestamp-to-time
6704 (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")))))
6705 (should
6706 (equal "2012-03-29"
6707 (format-time-string
6708 "%Y-%m-%d"
6709 (org-timestamp-to-time
6710 (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")))))
6711 ;; When optional argument END is non-nil, use end of date range or
6712 ;; time range.
6713 (should
6714 (equal "2012-03-29 16:40"
6715 (format-time-string
6716 "%Y-%m-%d %H:%M"
6717 (org-timestamp-to-time
6718 (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")
6719 t))))
6720 (should
6721 (equal "2014-03-04"
6722 (format-time-string
6723 "%Y-%m-%d"
6724 (org-timestamp-to-time
6725 (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")
6726 t))))
6727 (should
6728 (equal "2014-03-04"
6729 (format-time-string
6730 "%Y-%m-%d"
6731 (org-timestamp-to-time
6732 (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")
6733 t)))))
6736 ;;; Visibility
6738 (ert-deftest test-org/flag-drawer ()
6739 "Test `org-flag-drawer' specifications."
6740 ;; Hide drawer.
6741 (should
6742 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
6743 (org-flag-drawer t)
6744 (get-char-property (line-end-position) 'invisible)))
6745 ;; Show drawer.
6746 (should-not
6747 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
6748 (org-flag-drawer t)
6749 (org-flag-drawer nil)
6750 (get-char-property (line-end-position) 'invisible)))
6751 ;; Test optional argument.
6752 (should
6753 (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
6754 (let ((drawer (save-excursion (search-forward ":D2")
6755 (org-element-at-point))))
6756 (org-flag-drawer t drawer)
6757 (get-char-property (progn (search-forward ":D2") (line-end-position))
6758 'invisible))))
6759 (should-not
6760 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
6761 (let ((drawer (save-excursion (search-forward ":D2")
6762 (org-element-at-point))))
6763 (org-flag-drawer t drawer)
6764 (get-char-property (line-end-position) 'invisible))))
6765 ;; Do not hide fake drawers.
6766 (should-not
6767 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
6768 (forward-line 1)
6769 (org-flag-drawer t)
6770 (get-char-property (line-end-position) 'invisible)))
6771 ;; Do not hide incomplete drawers.
6772 (should-not
6773 (org-test-with-temp-text ":D:\nparagraph"
6774 (forward-line 1)
6775 (org-flag-drawer t)
6776 (get-char-property (line-end-position) 'invisible)))
6777 ;; Do not hide drawers when called from final blank lines.
6778 (should-not
6779 (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
6780 (goto-char (point-max))
6781 (org-flag-drawer t)
6782 (goto-char (point-min))
6783 (get-char-property (line-end-position) 'invisible)))
6784 ;; Don't leave point in an invisible part of the buffer when hiding
6785 ;; a drawer away.
6786 (should-not
6787 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
6788 (goto-char (point-max))
6789 (org-flag-drawer t)
6790 (get-char-property (point) 'invisible))))
6792 (ert-deftest test-org/hide-block-toggle ()
6793 "Test `org-hide-block-toggle' specifications."
6794 ;; Error when not at a block.
6795 (should-error
6796 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
6797 (org-hide-block-toggle 'off)
6798 (get-char-property (line-end-position) 'invisible)))
6799 ;; Hide block.
6800 (should
6801 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
6802 (org-hide-block-toggle)
6803 (get-char-property (line-end-position) 'invisible)))
6804 (should
6805 (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
6806 (org-hide-block-toggle)
6807 (get-char-property (line-end-position) 'invisible)))
6808 ;; Show block unconditionally when optional argument is `off'.
6809 (should-not
6810 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
6811 (org-hide-block-toggle)
6812 (org-hide-block-toggle 'off)
6813 (get-char-property (line-end-position) 'invisible)))
6814 (should-not
6815 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
6816 (org-hide-block-toggle 'off)
6817 (get-char-property (line-end-position) 'invisible)))
6818 ;; Hide block unconditionally when optional argument is non-nil.
6819 (should
6820 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
6821 (org-hide-block-toggle t)
6822 (get-char-property (line-end-position) 'invisible)))
6823 (should
6824 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
6825 (org-hide-block-toggle)
6826 (org-hide-block-toggle t)
6827 (get-char-property (line-end-position) 'invisible)))
6828 ;; Do not hide block when called from final blank lines.
6829 (should-not
6830 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
6831 (org-hide-block-toggle)
6832 (goto-char (point-min))
6833 (get-char-property (line-end-position) 'invisible)))
6834 ;; Don't leave point in an invisible part of the buffer when hiding
6835 ;; a block away.
6836 (should-not
6837 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
6838 (org-hide-block-toggle)
6839 (get-char-property (point) 'invisible))))
6841 (ert-deftest test-org/hide-block-toggle-maybe ()
6842 "Test `org-hide-block-toggle-maybe' specifications."
6843 (should
6844 (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
6845 (org-hide-block-toggle-maybe)))
6846 (should-not
6847 (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
6849 (ert-deftest test-org/show-set-visibility ()
6850 "Test `org-show-set-visibility' specifications."
6851 ;; Do not throw an error before first heading.
6852 (should
6853 (org-test-with-temp-text "Preamble\n* Headline"
6854 (org-show-set-visibility 'tree)
6856 ;; Test all visibility spans, both on headline and in entry.
6857 (let ((list-visible-lines
6858 (lambda (state headerp)
6859 (org-test-with-temp-text "* Grandmother (0)
6860 ** Uncle (1)
6861 *** Heir (2)
6862 ** Father (3)
6863 Ancestor text (4)
6864 *** Sister (5)
6865 Sibling text (6)
6866 *** Self (7)
6867 Match (8)
6868 **** First born (9)
6869 Child text (10)
6870 **** The other child (11)
6871 *** Brother (12)
6872 ** Aunt (13)
6874 (org-cycle t)
6875 (search-forward (if headerp "Self" "Match"))
6876 (org-show-set-visibility state)
6877 (goto-char (point-min))
6878 (let (result (line 0))
6879 (while (not (eobp))
6880 (unless (org-invisible-p2) (push line result))
6881 (incf line)
6882 (forward-line))
6883 (nreverse result))))))
6884 (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
6885 (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
6886 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
6887 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
6888 (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
6889 (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
6890 (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
6891 (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
6892 (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
6893 (should (equal '(0 1 3 5 7 8 9 11 12 13)
6894 (funcall list-visible-lines 'tree nil)))
6895 (should (equal '(0 1 3 4 5 7 12 13)
6896 (funcall list-visible-lines 'canonical t)))
6897 (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
6898 (funcall list-visible-lines 'canonical nil))))
6899 ;; When point is hidden in a drawer or a block, make sure to make it
6900 ;; visible.
6901 (should-not
6902 (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
6903 (org-hide-block-toggle)
6904 (search-forward "Text")
6905 (org-show-set-visibility 'minimal)
6906 (org-invisible-p2)))
6907 (should-not
6908 (org-test-with-temp-text ":DRAWER:\nText\n:END:"
6909 (org-flag-drawer t)
6910 (search-forward "Text")
6911 (org-show-set-visibility 'minimal)
6912 (org-invisible-p2)))
6913 (should-not
6914 (org-test-with-temp-text
6915 "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
6916 (org-flag-drawer t)
6917 (forward-line -1)
6918 (org-hide-block-toggle)
6919 (search-forward "Text")
6920 (org-show-set-visibility 'minimal)
6921 (org-invisible-p2))))
6923 (defun test-org/copy-visible ()
6924 "Test `org-copy-visible' specifications."
6925 (should
6926 (equal "Foo"
6927 (org-test-with-temp-text "Foo"
6928 (let ((kill-ring nil))
6929 (org-copy-visible (point-min) (point-max))
6930 (current-kill 0 t)))))
6931 ;; Skip invisible characters by text property.
6932 (should
6933 (equal "Foo"
6934 (org-test-with-temp-text #("F<hidden>oo" 1 7 (invisible t))
6935 (let ((kill-ring nil))
6936 (org-copy-visible (point-min) (point-max))
6937 (current-kill 0 t)))))
6938 ;; Skip invisible characters by overlay.
6939 (should
6940 (equal "Foo"
6941 (org-test-with-temp-text "F<hidden>oo"
6942 (let ((o (make-overlay 2 10)))
6943 (overlay-put o 'invisible t))
6944 (let ((kill-ring nil))
6945 (org-copy-visible (point-min) (point-max))
6946 (current-kill 0 t)))))
6947 ;; Handle invisible characters at the beginning and the end of the
6948 ;; buffer.
6949 (should
6950 (equal "Foo"
6951 (org-test-with-temp-text #("<hidden>Foo" 0 8 (invisible t))
6952 (let ((kill-ring nil))
6953 (org-copy-visible (point-min) (point-max))
6954 (current-kill 0 t)))))
6955 (should
6956 (equal "Foo"
6957 (org-test-with-temp-text #("Foo<hidden>" 3 11 (invisible t))
6958 (let ((kill-ring nil))
6959 (org-copy-visible (point-min) (point-max))
6960 (current-kill 0 t)))))
6961 ;; Handle multiple visible parts.
6962 (should
6963 (equal "abc"
6964 (org-test-with-temp-text
6965 #("aXbXc" 1 2 (invisible t) 3 4 (invisible t))
6966 (let ((kill-ring nil))
6967 (org-copy-visible (point-min) (point-max))
6968 (current-kill 0 t))))))
6970 (ert-deftest test-org/set-visibility-according-to-property ()
6971 "Test `org-set-visibility-according-to-property' specifications."
6972 ;; "folded" state.
6973 (should
6974 (org-test-with-temp-text
6977 :PROPERTIES:
6978 :VISIBILITY: folded
6979 :END:
6980 ** <point>b"
6981 (org-set-visibility-according-to-property)
6982 (invisible-p (point))))
6983 ;; "children" state.
6984 (should
6985 (org-test-with-temp-text
6988 :PROPERTIES:
6989 :VISIBILITY: children
6990 :END:
6991 ** b
6992 <point>Contents
6993 ** c"
6994 (org-set-visibility-according-to-property)
6995 (invisible-p (point))))
6996 (should
6997 (org-test-with-temp-text
7000 :PROPERTIES:
7001 :VISIBILITY: children
7002 :END:
7003 ** b
7004 Contents
7005 *** <point>c"
7006 (org-set-visibility-according-to-property)
7007 (invisible-p (point))))
7008 ;; "content" state.
7009 (should
7010 (org-test-with-temp-text
7013 :PROPERTIES:
7014 :VISIBILITY: content
7015 :END:
7016 ** b
7017 <point>Contents
7018 *** c"
7019 (org-set-visibility-according-to-property)
7020 (invisible-p (point))))
7021 (should
7022 (org-test-with-temp-text
7025 :PROPERTIES:
7026 :VISIBILITY: content
7027 :END:
7028 ** b
7029 Contents
7030 *** <point>c"
7031 (org-set-visibility-according-to-property)
7032 (not (invisible-p (point)))))
7033 ;; "showall" state.
7034 (should
7035 (org-test-with-temp-text
7038 :PROPERTIES:
7039 :VISIBILITY: showall
7040 :END:
7041 ** b
7042 <point>Contents
7043 *** c"
7044 (org-set-visibility-according-to-property)
7045 (not (invisible-p (point)))))
7046 (should
7047 (org-test-with-temp-text
7050 :PROPERTIES:
7051 :VISIBILITY: showall
7052 :END:
7053 ** b
7054 Contents
7055 *** <point>c"
7056 (org-set-visibility-according-to-property)
7057 (not (invisible-p (point))))))
7060 ;;; Yank and Kill
7062 (ert-deftest test-org/paste-subtree ()
7063 "Test `org-paste-subtree' specifications."
7064 ;; Return an error if text to yank is not a set of subtrees.
7065 (should-error (org-paste-subtree nil "Text"))
7066 ;; Adjust level according to current one.
7067 (should
7068 (equal "* H\n* Text\n"
7069 (org-test-with-temp-text "* H\n<point>"
7070 (org-paste-subtree nil "* Text")
7071 (buffer-string))))
7072 (should
7073 (equal "* H1\n** H2\n** Text\n"
7074 (org-test-with-temp-text "* H1\n** H2\n<point>"
7075 (org-paste-subtree nil "* Text")
7076 (buffer-string))))
7077 ;; When not on a heading, move to next heading before yanking.
7078 (should
7079 (equal "* H1\nParagraph\n* Text\n* H2"
7080 (org-test-with-temp-text "* H1\n<point>Paragraph\n* H2"
7081 (org-paste-subtree nil "* Text")
7082 (buffer-string))))
7083 ;; If point is between two headings, use the deepest level.
7084 (should
7085 (equal "* H1\n\n* Text\n* H2"
7086 (org-test-with-temp-text "* H1\n<point>\n* H2"
7087 (org-paste-subtree nil "* Text")
7088 (buffer-string))))
7089 (should
7090 (equal "** H1\n\n** Text\n* H2"
7091 (org-test-with-temp-text "** H1\n<point>\n* H2"
7092 (org-paste-subtree nil "* Text")
7093 (buffer-string))))
7094 (should
7095 (equal "* H1\n\n** Text\n** H2"
7096 (org-test-with-temp-text "* H1\n<point>\n** H2"
7097 (org-paste-subtree nil "* Text")
7098 (buffer-string))))
7099 ;; When on an empty heading, after the stars, deduce the new level
7100 ;; from the number of stars.
7101 (should
7102 (equal "*** Text\n"
7103 (org-test-with-temp-text "*** <point>"
7104 (org-paste-subtree nil "* Text")
7105 (buffer-string))))
7106 ;; Optional argument LEVEL forces a level for the subtree.
7107 (should
7108 (equal "* H\n*** Text\n"
7109 (org-test-with-temp-text "* H<point>"
7110 (org-paste-subtree 3 "* Text")
7111 (buffer-string)))))
7113 (provide 'test-org)
7115 ;;; test-org.el ends here