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