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