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