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