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