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