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