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