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