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