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