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