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