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