Turn org-mode into Org or Org mode
[org-mode.git] / testing / lisp / test-org.el
blobf1849e31a35897fc77320b496ba05baa9ca27dc0
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/forward-sentence ()
2491 "Test `org-forward-sentence' specifications."
2492 ;; At the end of a table cell, move to the end of the next one.
2493 (should
2494 (org-test-with-temp-text "| a<point> | b |"
2495 (org-forward-sentence)
2496 (looking-at " |$")))
2497 ;; Elsewhere in a cell, move to its end.
2498 (should
2499 (org-test-with-temp-text "| a<point>c | b |"
2500 (org-forward-sentence)
2501 (looking-at " | b |$")))
2502 ;; Otherwise, simply call `forward-sentence'.
2503 (should
2504 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
2505 (org-forward-sentence)
2506 (looking-at " Sentence 2.")))
2507 (should
2508 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
2509 (org-forward-sentence)
2510 (org-forward-sentence)
2511 (eobp)))
2512 ;; At the end of an element, jump to the next one, without stopping
2513 ;; on blank lines in-between.
2514 (should
2515 (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
2516 (org-forward-sentence)
2517 (eobp))))
2519 (ert-deftest test-org/backward-sentence ()
2520 "Test `org-backward-sentence' specifications."
2521 ;; At the beginning of a table cell, move to the beginning of the
2522 ;; previous one.
2523 (should
2524 (org-test-with-temp-text "| a | <point>b |"
2525 (org-backward-sentence)
2526 (looking-at "a | b |$")))
2527 ;; Elsewhere in a cell, move to its beginning.
2528 (should
2529 (org-test-with-temp-text "| a | b<point>c |"
2530 (org-backward-sentence)
2531 (looking-at "bc |$")))
2532 ;; Otherwise, simply call `backward-sentence'.
2533 (should
2534 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
2535 (org-backward-sentence)
2536 (looking-at "Sentence 2.")))
2537 (should
2538 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
2539 (org-backward-sentence)
2540 (org-backward-sentence)
2541 (bobp)))
2542 ;; Make sure to hit the beginning of a sentence on the same line as
2543 ;; an item.
2544 (should
2545 (org-test-with-temp-text "- Line 1\n line <point>2."
2546 (org-backward-sentence)
2547 (looking-at "Line 1"))))
2549 (ert-deftest test-org/forward-paragraph ()
2550 "Test `org-forward-paragraph' specifications."
2551 ;; At end of buffer, return an error.
2552 (should-error
2553 (org-test-with-temp-text "Paragraph"
2554 (goto-char (point-max))
2555 (org-forward-paragraph)))
2556 ;; Standard test.
2557 (should
2558 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2559 (org-forward-paragraph)
2560 (looking-at "P2")))
2561 ;; Ignore depth.
2562 (should
2563 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
2564 (org-forward-paragraph)
2565 (looking-at "P1")))
2566 ;; Do not enter elements with invisible contents.
2567 (should
2568 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
2569 (org-hide-block-toggle)
2570 (org-forward-paragraph)
2571 (looking-at "P3")))
2572 ;; On an affiliated keyword, jump to the beginning of the element.
2573 (should
2574 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
2575 (org-forward-paragraph)
2576 (looking-at "Para")))
2577 ;; On an item or a footnote definition, move to the second element
2578 ;; inside, if any.
2579 (should
2580 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
2581 (org-forward-paragraph)
2582 (looking-at " Paragraph")))
2583 (should
2584 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
2585 (org-forward-paragraph)
2586 (looking-at "Paragraph")))
2587 ;; On an item, or a footnote definition, when the first line is
2588 ;; empty, move to the first item.
2589 (should
2590 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
2591 (org-forward-paragraph)
2592 (looking-at " Paragraph")))
2593 (should
2594 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
2595 (org-forward-paragraph)
2596 (looking-at "Paragraph")))
2597 ;; On a table (resp. a property drawer) do not move through table
2598 ;; rows (resp. node properties).
2599 (should
2600 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
2601 (org-forward-paragraph)
2602 (looking-at "Paragraph")))
2603 (should
2604 (org-test-with-temp-text
2605 "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
2606 (org-forward-paragraph)
2607 (looking-at "Paragraph")))
2608 ;; On a verse or source block, stop after blank lines.
2609 (should
2610 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
2611 (org-forward-paragraph)
2612 (looking-at "L2")))
2613 (should
2614 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
2615 (org-forward-paragraph)
2616 (looking-at "L2"))))
2618 (ert-deftest test-org/backward-paragraph ()
2619 "Test `org-backward-paragraph' specifications."
2620 ;; Error at beginning of buffer.
2621 (should-error
2622 (org-test-with-temp-text "Paragraph"
2623 (org-backward-paragraph)))
2624 ;; Regular test.
2625 (should
2626 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2627 (goto-char (point-max))
2628 (org-backward-paragraph)
2629 (looking-at "P3")))
2630 (should
2631 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2632 (goto-char (point-max))
2633 (beginning-of-line)
2634 (org-backward-paragraph)
2635 (looking-at "P2")))
2636 ;; Ignore depth.
2637 (should
2638 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
2639 (goto-char (point-max))
2640 (beginning-of-line)
2641 (org-backward-paragraph)
2642 (looking-at "P2")))
2643 ;; Ignore invisible elements.
2644 (should
2645 (org-test-with-temp-text "* H1\n P1\n* H2"
2646 (org-cycle)
2647 (goto-char (point-max))
2648 (beginning-of-line)
2649 (org-backward-paragraph)
2650 (bobp)))
2651 ;; On an affiliated keyword, jump to the first one.
2652 (should
2653 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
2654 (search-forward "c2")
2655 (org-backward-paragraph)
2656 (looking-at "#\\+name")))
2657 ;; On the second element in an item or a footnote definition, jump
2658 ;; to item or the definition.
2659 (should
2660 (org-test-with-temp-text "- line1\n\n line2"
2661 (goto-char (point-max))
2662 (beginning-of-line)
2663 (org-backward-paragraph)
2664 (looking-at "- line1")))
2665 (should
2666 (org-test-with-temp-text "[fn:1] line1\n\n line2"
2667 (goto-char (point-max))
2668 (beginning-of-line)
2669 (org-backward-paragraph)
2670 (looking-at "\\[fn:1\\] line1")))
2671 ;; On a table (resp. a property drawer), ignore table rows
2672 ;; (resp. node properties).
2673 (should
2674 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
2675 (goto-char (point-max))
2676 (beginning-of-line)
2677 (org-backward-paragraph)
2678 (bobp)))
2679 (should
2680 (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
2681 (org-backward-paragraph)
2682 (looking-at ":PROPERTIES:")))
2683 ;; On a source or verse block, stop before blank lines.
2684 (should
2685 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
2686 (search-forward "L3")
2687 (beginning-of-line)
2688 (org-backward-paragraph)
2689 (looking-at "L2")))
2690 (should
2691 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
2692 (search-forward "L3")
2693 (beginning-of-line)
2694 (org-backward-paragraph)
2695 (looking-at "L2"))))
2697 (ert-deftest test-org/forward-element ()
2698 "Test `org-forward-element' specifications."
2699 ;; 1. At EOB: should error.
2700 (org-test-with-temp-text "Some text\n"
2701 (goto-char (point-max))
2702 (should-error (org-forward-element)))
2703 ;; 2. Standard move: expected to ignore blank lines.
2704 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
2705 (org-forward-element)
2706 (should (looking-at (regexp-quote "Second paragraph."))))
2707 ;; 3. Headline tests.
2708 (org-test-with-temp-text "
2709 * Head 1
2710 ** Head 1.1
2711 *** Head 1.1.1
2712 ** Head 1.2"
2713 ;; 3.1. At an headline beginning: move to next headline at the
2714 ;; same level.
2715 (goto-line 3)
2716 (org-forward-element)
2717 (should (looking-at (regexp-quote "** Head 1.2")))
2718 ;; 3.2. At an headline beginning: move to parent headline if no
2719 ;; headline at the same level.
2720 (goto-line 3)
2721 (org-forward-element)
2722 (should (looking-at (regexp-quote "** Head 1.2"))))
2723 ;; 4. Greater element tests.
2724 (org-test-with-temp-text
2725 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
2726 ;; 4.1. At a greater element: expected to skip contents.
2727 (org-forward-element)
2728 (should (looking-at (regexp-quote "Outside.")))
2729 ;; 4.2. At the end of greater element contents: expected to skip
2730 ;; to the end of the greater element.
2731 (goto-line 2)
2732 (org-forward-element)
2733 (should (looking-at (regexp-quote "Outside."))))
2734 ;; 5. List tests.
2735 (org-test-with-temp-text "
2736 - item1
2738 - sub1
2740 - sub2
2742 - sub3
2744 Inner paragraph.
2746 - item2
2748 Outside."
2749 ;; 5.1. At list top point: expected to move to the element after
2750 ;; the list.
2751 (goto-line 2)
2752 (org-forward-element)
2753 (should (looking-at (regexp-quote "Outside.")))
2754 ;; 5.2. Special case: at the first line of a sub-list, but not at
2755 ;; beginning of line, move to next item.
2756 (goto-line 2)
2757 (forward-char)
2758 (org-forward-element)
2759 (should (looking-at "- item2"))
2760 (goto-line 4)
2761 (forward-char)
2762 (org-forward-element)
2763 (should (looking-at " - sub2"))
2764 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
2765 (goto-line 4)
2766 (org-forward-element)
2767 (should (looking-at (regexp-quote " Inner paragraph.")))
2768 ;; 5.4. At sub-list end: expected to move outside the sub-list.
2769 (goto-line 8)
2770 (org-forward-element)
2771 (should (looking-at (regexp-quote " Inner paragraph.")))
2772 ;; 5.5. At an item: expected to move to next item, if any.
2773 (goto-line 6)
2774 (org-forward-element)
2775 (should (looking-at " - sub3"))))
2777 (ert-deftest test-org/backward-element ()
2778 "Test `org-backward-element' specifications."
2779 ;; 1. Should error at BOB.
2780 (org-test-with-temp-text " \nParagraph."
2781 (should-error (org-backward-element)))
2782 ;; 2. Should move at BOB when called on the first element in buffer.
2783 (should
2784 (org-test-with-temp-text "\n#+TITLE: test"
2785 (progn (forward-line)
2786 (org-backward-element)
2787 (bobp))))
2788 ;; 3. Not at the beginning of an element: move at its beginning.
2789 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2790 (goto-line 3)
2791 (end-of-line)
2792 (org-backward-element)
2793 (should (looking-at (regexp-quote "Paragraph2."))))
2794 ;; 4. Headline tests.
2795 (org-test-with-temp-text "
2796 * Head 1
2797 ** Head 1.1
2798 *** Head 1.1.1
2799 ** Head 1.2"
2800 ;; 4.1. At an headline beginning: move to previous headline at the
2801 ;; same level.
2802 (goto-line 5)
2803 (org-backward-element)
2804 (should (looking-at (regexp-quote "** Head 1.1")))
2805 ;; 4.2. At an headline beginning: move to parent headline if no
2806 ;; headline at the same level.
2807 (goto-line 3)
2808 (org-backward-element)
2809 (should (looking-at (regexp-quote "* Head 1")))
2810 ;; 4.3. At the first top-level headline: should error.
2811 (goto-line 2)
2812 (should-error (org-backward-element)))
2813 ;; 5. At beginning of first element inside a greater element:
2814 ;; expected to move to greater element's beginning.
2815 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
2816 (goto-line 3)
2817 (org-backward-element)
2818 (should (looking-at "#\\+BEGIN_CENTER")))
2819 ;; 6. At the beginning of the first element in a section: should
2820 ;; move back to headline, if any.
2821 (should
2822 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
2823 (progn (goto-char (point-max))
2824 (beginning-of-line)
2825 (org-backward-element)
2826 (org-at-heading-p))))
2827 ;; 7. List tests.
2828 (org-test-with-temp-text "
2829 - item1
2831 - sub1
2833 - sub2
2835 - sub3
2837 Inner paragraph.
2839 - item2
2842 Outside."
2843 ;; 7.1. At beginning of sub-list: expected to move to the
2844 ;; paragraph before it.
2845 (goto-line 4)
2846 (org-backward-element)
2847 (should (looking-at "item1"))
2848 ;; 7.2. At an item in a list: expected to move at previous item.
2849 (goto-line 8)
2850 (org-backward-element)
2851 (should (looking-at " - sub2"))
2852 (goto-line 12)
2853 (org-backward-element)
2854 (should (looking-at "- item1"))
2855 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
2856 ;; beginning.
2857 (goto-line 10)
2858 (org-backward-element)
2859 (should (looking-at " - sub1"))
2860 (goto-line 15)
2861 (org-backward-element)
2862 (should (looking-at "- item1"))
2863 ;; 7.4. At blank-lines before list end: expected to move to top
2864 ;; item.
2865 (goto-line 14)
2866 (org-backward-element)
2867 (should (looking-at "- item1"))))
2869 (ert-deftest test-org/up-element ()
2870 "Test `org-up-element' specifications."
2871 ;; 1. At BOB or with no surrounding element: should error.
2872 (org-test-with-temp-text "Paragraph."
2873 (should-error (org-up-element)))
2874 (org-test-with-temp-text "* Head1\n* Head2"
2875 (goto-line 2)
2876 (should-error (org-up-element)))
2877 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2878 (goto-line 3)
2879 (should-error (org-up-element)))
2880 ;; 2. At an headline: move to parent headline.
2881 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
2882 (goto-line 3)
2883 (org-up-element)
2884 (should (looking-at "\\* Head1")))
2885 ;; 3. Inside a greater element: move to greater element beginning.
2886 (org-test-with-temp-text
2887 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
2888 (goto-line 3)
2889 (org-up-element)
2890 (should (looking-at "#\\+BEGIN_CENTER")))
2891 ;; 4. List tests.
2892 (org-test-with-temp-text "* Top
2893 - item1
2895 - sub1
2897 - sub2
2899 Paragraph within sub2.
2901 - item2"
2902 ;; 4.1. Within an item: move to the item beginning.
2903 (goto-line 8)
2904 (org-up-element)
2905 (should (looking-at " - sub2"))
2906 ;; 4.2. At an item in a sub-list: move to parent item.
2907 (goto-line 4)
2908 (org-up-element)
2909 (should (looking-at "- item1"))
2910 ;; 4.3. At an item in top list: move to beginning of whole list.
2911 (goto-line 10)
2912 (org-up-element)
2913 (should (looking-at "- item1"))
2914 ;; 4.4. Special case. At very top point: should move to parent of
2915 ;; list.
2916 (goto-line 2)
2917 (org-up-element)
2918 (should (looking-at "\\* Top"))))
2920 (ert-deftest test-org/down-element ()
2921 "Test `org-down-element' specifications."
2922 ;; Error when the element hasn't got a recursive type.
2923 (org-test-with-temp-text "Paragraph."
2924 (should-error (org-down-element)))
2925 ;; Error when the element has no contents
2926 (org-test-with-temp-text "* Headline"
2927 (should-error (org-down-element)))
2928 ;; When at a plain-list, move to first item.
2929 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
2930 (goto-line 2)
2931 (org-down-element)
2932 (should (looking-at " - Item 1.1")))
2933 (org-test-with-temp-text "#+NAME: list\n- Item 1"
2934 (org-down-element)
2935 (should (looking-at " Item 1")))
2936 ;; When at a table, move to first row
2937 (org-test-with-temp-text "#+NAME: table\n| a | b |"
2938 (org-down-element)
2939 (should (looking-at " a | b |")))
2940 ;; Otherwise, move inside the greater element.
2941 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
2942 (org-down-element)
2943 (should (looking-at "Paragraph"))))
2945 (ert-deftest test-org/drag-element-backward ()
2946 "Test `org-drag-element-backward' specifications."
2947 ;; Standard test.
2948 (should
2949 (equal
2950 "#+key2: val2\n#+key1: val1\n#+key3: val3"
2951 (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
2952 (org-drag-element-backward)
2953 (buffer-string))))
2954 (should
2955 (equal
2956 "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
2957 (org-test-with-temp-text
2958 "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
2959 (org-drag-element-backward)
2960 (buffer-string))))
2961 ;; Preserve blank lines.
2962 (should
2963 (equal "Paragraph 2\n\n\nPara1\n\nPara3"
2964 (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
2965 (org-drag-element-backward)
2966 (buffer-string))))
2967 ;; Preserve column.
2968 (should
2969 (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
2970 (org-drag-element-backward)
2971 (looking-at-p "2")))
2972 ;; Error when trying to move first element of buffer.
2973 (should-error
2974 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2975 (org-drag-element-backward)))
2976 ;; Error when trying to swap nested elements.
2977 (should-error
2978 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2979 (forward-line)
2980 (org-drag-element-backward)))
2981 ;; Error when trying to swap an headline element and a non-headline
2982 ;; element.
2983 (should-error
2984 (org-test-with-temp-text "Test.\n* Head 1"
2985 (forward-line)
2986 (org-drag-element-backward)))
2987 ;; Preserve visibility of elements and their contents.
2988 (should
2989 (equal '((63 . 82) (26 . 48))
2990 (org-test-with-temp-text "
2991 #+BEGIN_CENTER
2992 Text.
2993 #+END_CENTER
2994 - item 1
2995 #+BEGIN_QUOTE
2996 Text.
2997 #+END_QUOTE"
2998 (while (search-forward "BEGIN_" nil t) (org-cycle))
2999 (search-backward "- item 1")
3000 (org-drag-element-backward)
3001 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
3002 (overlays-in (point-min) (point-max)))))))
3004 (ert-deftest test-org/drag-element-forward ()
3005 "Test `org-drag-element-forward' specifications."
3006 ;; 1. Error when trying to move first element of buffer.
3007 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
3008 (goto-line 3)
3009 (should-error (org-drag-element-forward)))
3010 ;; 2. Error when trying to swap nested elements.
3011 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
3012 (forward-line)
3013 (should-error (org-drag-element-forward)))
3014 ;; 3. Error when trying to swap a non-headline element and an
3015 ;; headline.
3016 (org-test-with-temp-text "Test.\n* Head 1"
3017 (should-error (org-drag-element-forward)))
3018 ;; 4. Otherwise, swap elements, preserving column and blank lines
3019 ;; between elements.
3020 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
3021 (search-forward "graph")
3022 (org-drag-element-forward)
3023 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
3024 (should (looking-at " 1")))
3025 ;; 5. Preserve visibility of elements and their contents.
3026 (org-test-with-temp-text "
3027 #+BEGIN_CENTER
3028 Text.
3029 #+END_CENTER
3030 - item 1
3031 #+BEGIN_QUOTE
3032 Text.
3033 #+END_QUOTE"
3034 (while (search-forward "BEGIN_" nil t) (org-cycle))
3035 (search-backward "#+BEGIN_CENTER")
3036 (org-drag-element-forward)
3037 (should
3038 (equal
3039 '((63 . 82) (26 . 48))
3040 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
3041 (overlays-in (point-min) (point-max)))))))
3043 (ert-deftest test-org/next-block ()
3044 "Test `org-next-block' specifications."
3045 ;; Regular test.
3046 (should
3047 (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
3048 (org-next-block 1)
3049 (looking-at "#\\+BEGIN_CENTER")))
3050 ;; Ignore case.
3051 (should
3052 (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
3053 (let ((case-fold-search nil))
3054 (org-next-block 1)
3055 (looking-at "#\\+begin_center"))))
3056 ;; Ignore current line.
3057 (should
3058 (org-test-with-temp-text
3059 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
3060 (org-next-block 1)
3061 (looking-at "#\\+BEGIN_CENTER")))
3062 ;; Throw an error when no block is found.
3063 (should-error
3064 (org-test-with-temp-text "Paragraph"
3065 (org-next-block 1)))
3066 ;; With an argument, skip many blocks at once.
3067 (should
3068 (org-test-with-temp-text
3069 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
3070 (org-next-block 2)
3071 (looking-at "#\\+BEGIN_QUOTE")))
3072 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
3073 (should
3074 (org-test-with-temp-text
3075 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
3076 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
3077 (looking-at "#\\+BEGIN_QUOTE")))
3078 ;; Optional argument is also case-insensitive.
3079 (should
3080 (org-test-with-temp-text
3081 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
3082 (let ((case-fold-search nil))
3083 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
3084 (looking-at "#\\+begin_quote")))))
3086 (ert-deftest test-org/previous-block ()
3087 "Test `org-previous-block' specifications."
3088 ;; Regular test.
3089 (should
3090 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
3091 (org-previous-block 1)
3092 (looking-at "#\\+BEGIN_CENTER")))
3093 ;; Ignore case.
3094 (should
3095 (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
3096 (let ((case-fold-search nil))
3097 (org-previous-block 1)
3098 (looking-at "#\\+begin_center"))))
3099 ;; Ignore current line.
3100 (should
3101 (org-test-with-temp-text
3102 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
3103 (org-previous-block 1)
3104 (looking-at "#\\+BEGIN_QUOTE")))
3105 ;; Throw an error when no block is found.
3106 (should-error
3107 (org-test-with-temp-text "Paragraph<point>"
3108 (org-previous-block 1)))
3109 ;; With an argument, skip many blocks at once.
3110 (should
3111 (org-test-with-temp-text
3112 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
3113 (org-previous-block 2)
3114 (looking-at "#\\+BEGIN_CENTER")))
3115 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
3116 (should
3117 (org-test-with-temp-text
3118 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
3119 (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
3120 (looking-at "#\\+BEGIN_QUOTE")))
3121 ;; Optional argument is also case-insensitive.
3122 (should
3123 (org-test-with-temp-text
3124 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
3125 (let ((case-fold-search nil))
3126 (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
3127 (looking-at "#\\+begin_quote")))))
3130 ;;; Outline structure
3132 (ert-deftest test-org/demote ()
3133 "Test `org-demote' specifications."
3134 ;; Add correct number of stars according to `org-odd-levels-only'.
3135 (should
3136 (= 2
3137 (org-test-with-temp-text "* H"
3138 (let ((org-odd-levels-only nil)) (org-demote))
3139 (org-current-level))))
3140 (should
3141 (= 3
3142 (org-test-with-temp-text "* H"
3143 (let ((org-odd-levels-only t)) (org-demote))
3144 (org-current-level))))
3145 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
3146 (should
3147 (org-test-with-temp-text "* H :tag:"
3148 (let ((org-tags-column 10)
3149 (org-auto-align-tags t)
3150 (org-odd-levels-only nil))
3151 (org-demote))
3152 (org-move-to-column 10)
3153 (looking-at-p ":tag:$")))
3154 (should-not
3155 (org-test-with-temp-text "* H :tag:"
3156 (let ((org-tags-column 10)
3157 (org-auto-align-tags nil)
3158 (org-odd-levels-only nil))
3159 (org-demote))
3160 (org-move-to-column 10)
3161 (looking-at-p ":tag:$")))
3162 ;; When `org-adapt-indentation' is non-nil, always indent planning
3163 ;; info and property drawers accordingly.
3164 (should
3165 (= 3
3166 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
3167 (let ((org-odd-levels-only nil)
3168 (org-adapt-indentation t))
3169 (org-demote))
3170 (forward-line)
3171 (org-get-indentation))))
3172 (should
3173 (= 3
3174 (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
3175 (let ((org-odd-levels-only nil)
3176 (org-adapt-indentation t))
3177 (org-demote))
3178 (forward-line)
3179 (org-get-indentation))))
3180 (should-not
3181 (= 3
3182 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
3183 (let ((org-odd-levels-only nil)
3184 (org-adapt-indentation nil))
3185 (org-demote))
3186 (forward-line)
3187 (org-get-indentation))))
3188 ;; When `org-adapt-indentation' is non-nil, shift all lines in
3189 ;; section accordingly. Ignore, however, footnote definitions and
3190 ;; inlinetasks boundaries.
3191 (should
3192 (= 3
3193 (org-test-with-temp-text "* H\n Paragraph"
3194 (let ((org-odd-levels-only nil)
3195 (org-adapt-indentation t))
3196 (org-demote))
3197 (forward-line)
3198 (org-get-indentation))))
3199 (should
3200 (= 2
3201 (org-test-with-temp-text "* H\n Paragraph"
3202 (let ((org-odd-levels-only nil)
3203 (org-adapt-indentation nil))
3204 (org-demote))
3205 (forward-line)
3206 (org-get-indentation))))
3207 (should
3208 (zerop
3209 (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
3210 (let ((org-odd-levels-only nil)
3211 (org-adapt-indentation t))
3212 (org-demote))
3213 (goto-char (point-max))
3214 (org-get-indentation))))
3215 (should
3216 (= 3
3217 (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
3218 (let ((org-odd-levels-only nil)
3219 (org-adapt-indentation t))
3220 (org-demote))
3221 (goto-char (point-max))
3222 (org-get-indentation))))
3223 (when (featurep 'org-inlinetask)
3224 (should
3225 (zerop
3226 (let ((org-inlinetask-min-level 5)
3227 (org-adapt-indentation t))
3228 (org-test-with-temp-text "* H\n***** I\n***** END"
3229 (org-demote)
3230 (forward-line)
3231 (org-get-indentation))))))
3232 (when (featurep 'org-inlinetask)
3233 (should
3234 (= 3
3235 (let ((org-inlinetask-min-level 5)
3236 (org-adapt-indentation t))
3237 (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
3238 (org-demote)
3239 (forward-line 2)
3240 (org-get-indentation))))))
3241 ;; Ignore contents of source blocks or example blocks when
3242 ;; indentation should be preserved (through
3243 ;; `org-src-preserve-indentation' or "-i" flag).
3244 (should-not
3245 (zerop
3246 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
3247 (let ((org-adapt-indentation t)
3248 (org-src-preserve-indentation nil))
3249 (org-demote))
3250 (forward-line 2)
3251 (org-get-indentation))))
3252 (should
3253 (zerop
3254 (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
3255 (let ((org-adapt-indentation t)
3256 (org-src-preserve-indentation t))
3257 (org-demote))
3258 (forward-line 2)
3259 (org-get-indentation))))
3260 (should
3261 (zerop
3262 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
3263 (let ((org-adapt-indentation t)
3264 (org-src-preserve-indentation t))
3265 (org-demote))
3266 (forward-line 2)
3267 (org-get-indentation))))
3268 (should
3269 (zerop
3270 (org-test-with-temp-text
3271 "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
3272 (let ((org-adapt-indentation t)
3273 (org-src-preserve-indentation nil))
3274 (org-demote))
3275 (forward-line 2)
3276 (org-get-indentation)))))
3278 (ert-deftest test-org/promote ()
3279 "Test `org-promote' specifications."
3280 ;; Return an error if headline is to be promoted to level 0, unless
3281 ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
3282 ;; headline becomes a comment.
3283 (should-error
3284 (org-test-with-temp-text "* H"
3285 (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
3286 (should
3287 (equal "# H"
3288 (org-test-with-temp-text "* H"
3289 (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
3290 (buffer-string))))
3291 ;; Remove correct number of stars according to
3292 ;; `org-odd-levels-only'.
3293 (should
3294 (= 2
3295 (org-test-with-temp-text "*** H"
3296 (let ((org-odd-levels-only nil)) (org-promote))
3297 (org-current-level))))
3298 (should
3299 (= 1
3300 (org-test-with-temp-text "*** H"
3301 (let ((org-odd-levels-only t)) (org-promote))
3302 (org-current-level))))
3303 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
3304 (should
3305 (org-test-with-temp-text "** H :tag:"
3306 (let ((org-tags-column 10)
3307 (org-auto-align-tags t)
3308 (org-odd-levels-only nil))
3309 (org-promote))
3310 (org-move-to-column 10)
3311 (looking-at-p ":tag:$")))
3312 (should-not
3313 (org-test-with-temp-text "** H :tag:"
3314 (let ((org-tags-column 10)
3315 (org-auto-align-tags nil)
3316 (org-odd-levels-only nil))
3317 (org-promote))
3318 (org-move-to-column 10)
3319 (looking-at-p ":tag:$")))
3320 ;; When `org-adapt-indentation' is non-nil, always indent planning
3321 ;; info and property drawers.
3322 (should
3323 (= 2
3324 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
3325 (let ((org-odd-levels-only nil)
3326 (org-adapt-indentation t))
3327 (org-promote))
3328 (forward-line)
3329 (org-get-indentation))))
3330 (should
3331 (= 2
3332 (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
3333 (let ((org-odd-levels-only nil)
3334 (org-adapt-indentation t))
3335 (org-promote))
3336 (forward-line)
3337 (org-get-indentation))))
3338 (should-not
3339 (= 2
3340 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
3341 (let ((org-odd-levels-only nil)
3342 (org-adapt-indentation nil))
3343 (org-promote))
3344 (forward-line)
3345 (org-get-indentation))))
3346 ;; When `org-adapt-indentation' is non-nil, shift all lines in
3347 ;; section accordingly. Ignore, however, footnote definitions and
3348 ;; inlinetasks boundaries.
3349 (should
3350 (= 2
3351 (org-test-with-temp-text "** H\n Paragraph"
3352 (let ((org-odd-levels-only nil)
3353 (org-adapt-indentation t))
3354 (org-promote))
3355 (forward-line)
3356 (org-get-indentation))))
3357 (should-not
3358 (= 2
3359 (org-test-with-temp-text "** H\n Paragraph"
3360 (let ((org-odd-levels-only nil)
3361 (org-adapt-indentation nil))
3362 (org-promote))
3363 (forward-line)
3364 (org-get-indentation))))
3365 (should
3366 (= 2
3367 (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
3368 (let ((org-odd-levels-only nil)
3369 (org-adapt-indentation t))
3370 (org-promote))
3371 (forward-line)
3372 (org-get-indentation))))
3373 (when (featurep 'org-inlinetask)
3374 (should
3375 (zerop
3376 (let ((org-inlinetask-min-level 5)
3377 (org-adapt-indentation t))
3378 (org-test-with-temp-text "** H\n***** I\n***** END"
3379 (org-promote)
3380 (forward-line)
3381 (org-get-indentation))))))
3382 (when (featurep 'org-inlinetask)
3383 (should
3384 (= 2
3385 (let ((org-inlinetask-min-level 5)
3386 (org-adapt-indentation t))
3387 (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
3388 (org-promote)
3389 (forward-line 2)
3390 (org-get-indentation))))))
3391 ;; Give up shifting if it would break document's structure
3392 ;; otherwise.
3393 (should
3394 (= 3
3395 (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
3396 (let ((org-odd-levels-only nil)
3397 (org-adapt-indentation t))
3398 (org-promote))
3399 (forward-line)
3400 (org-get-indentation))))
3401 (should
3402 (= 3
3403 (org-test-with-temp-text "** H\n Paragraph\n * list."
3404 (let ((org-odd-levels-only nil)
3405 (org-adapt-indentation t))
3406 (org-promote))
3407 (forward-line)
3408 (org-get-indentation))))
3409 ;; Ignore contents of source blocks or example blocks when
3410 ;; indentation should be preserved (through
3411 ;; `org-src-preserve-indentation' or "-i" flag).
3412 (should-not
3413 (zerop
3414 (org-test-with-temp-text
3415 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
3416 (let ((org-adapt-indentation t)
3417 (org-src-preserve-indentation nil)
3418 (org-odd-levels-only nil))
3419 (org-promote))
3420 (forward-line)
3421 (org-get-indentation))))
3422 (should
3423 (zerop
3424 (org-test-with-temp-text
3425 "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
3426 (let ((org-adapt-indentation t)
3427 (org-src-preserve-indentation t)
3428 (org-odd-levels-only nil))
3429 (org-promote))
3430 (forward-line)
3431 (org-get-indentation))))
3432 (should
3433 (zerop
3434 (org-test-with-temp-text
3435 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
3436 (let ((org-adapt-indentation t)
3437 (org-src-preserve-indentation t)
3438 (org-odd-levels-only nil))
3439 (org-promote))
3440 (forward-line)
3441 (org-get-indentation))))
3442 (should
3443 (zerop
3444 (org-test-with-temp-text
3445 "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
3446 (let ((org-adapt-indentation t)
3447 (org-src-preserve-indentation nil)
3448 (org-odd-levels-only nil))
3449 (org-promote))
3450 (forward-line)
3451 (org-get-indentation)))))
3454 ;;; Planning
3456 (ert-deftest test-org/at-planning-p ()
3457 "Test `org-at-planning-p' specifications."
3458 ;; Regular test.
3459 (should
3460 (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
3461 (org-at-planning-p)))
3462 (should-not
3463 (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
3464 (org-at-planning-p)))
3465 ;; Correctly find planning attached to inlinetasks.
3466 (when (featurep 'org-inlinetask)
3467 (should
3468 (org-test-with-temp-text
3469 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
3470 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
3471 (should-not
3472 (org-test-with-temp-text
3473 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
3474 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
3475 (should-not
3476 (org-test-with-temp-text
3477 "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
3478 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
3479 (should-not
3480 (org-test-with-temp-text
3481 "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
3482 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
3484 (ert-deftest test-org/add-planning-info ()
3485 "Test `org-add-planning-info'."
3486 ;; Create deadline when `org-adapt-indentation' is non-nil.
3487 (should
3488 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3489 (org-test-with-temp-text "* H\nParagraph<point>"
3490 (let ((org-adapt-indentation t))
3491 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3492 (replace-regexp-in-string
3493 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3494 nil nil 1))))
3495 ;; Create deadline when `org-adapt-indentation' is nil.
3496 (should
3497 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3498 (org-test-with-temp-text "* H\nParagraph<point>"
3499 (let ((org-adapt-indentation nil))
3500 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3501 (replace-regexp-in-string
3502 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3503 nil nil 1))))
3504 ;; Update deadline when `org-adapt-indentation' is non-nil.
3505 (should
3506 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3507 (org-test-with-temp-text "\
3509 DEADLINE: <2015-06-24 Wed>
3510 Paragraph<point>"
3511 (let ((org-adapt-indentation t))
3512 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3513 (replace-regexp-in-string
3514 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3515 nil nil 1))))
3516 ;; Update deadline when `org-adapt-indentation' is nil.
3517 (should
3518 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3519 (org-test-with-temp-text "\
3521 DEADLINE: <2015-06-24 Wed>
3522 Paragraph<point>"
3523 (let ((org-adapt-indentation nil))
3524 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3525 (replace-regexp-in-string
3526 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3527 nil nil 1))))
3528 ;; Schedule when `org-adapt-indentation' is non-nil.
3529 (should
3530 (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
3531 (org-test-with-temp-text "* H\nParagraph<point>"
3532 (let ((org-adapt-indentation t))
3533 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
3534 (replace-regexp-in-string
3535 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3536 nil nil 1))))
3537 ;; Schedule when `org-adapt-indentation' is nil.
3538 (should
3539 (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
3540 (org-test-with-temp-text "* H\nParagraph<point>"
3541 (let ((org-adapt-indentation nil))
3542 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
3543 (replace-regexp-in-string
3544 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3545 nil nil 1))))
3546 ;; Add deadline when scheduled.
3547 (should
3548 (equal "\
3550 DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
3551 Paragraph"
3552 (org-test-with-temp-text "\
3554 SCHEDULED: <2015-06-24 Wed>
3555 Paragraph<point>"
3556 (let ((org-adapt-indentation t))
3557 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3558 (replace-regexp-in-string
3559 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3560 nil nil 1))))
3561 ;; Remove middle entry.
3562 (should
3563 (equal "\
3565 CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
3566 Paragraph"
3567 (org-test-with-temp-text "\
3569 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
3570 Paragraph<point>"
3571 (let ((org-adapt-indentation t))
3572 (org-add-planning-info nil nil 'deadline))
3573 (replace-regexp-in-string
3574 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
3575 nil nil 1))))
3576 ;; Remove last entry and then middle entry (order should not
3577 ;; matter).
3578 (should
3579 (equal "\
3581 CLOSED: [2015-06-24]
3582 Paragraph"
3583 (org-test-with-temp-text "\
3585 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
3586 Paragraph<point>"
3587 (let ((org-adapt-indentation t))
3588 (org-add-planning-info nil nil 'scheduled 'deadline))
3589 (replace-regexp-in-string
3590 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
3591 nil nil 1))))
3592 ;; Remove closed when `org-adapt-indentation' is non-nil.
3593 (should
3594 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3595 (org-test-with-temp-text "\
3597 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
3598 Paragraph<point>"
3599 (let ((org-adapt-indentation t))
3600 (org-add-planning-info nil nil 'closed))
3601 (replace-regexp-in-string
3602 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3603 nil nil 1))))
3604 (should
3605 (equal "* H\n Paragraph"
3606 (org-test-with-temp-text "\
3608 CLOSED: [2015-06-25 Thu]
3609 Paragraph<point>"
3610 (let ((org-adapt-indentation t))
3611 (org-add-planning-info nil nil 'closed))
3612 (replace-regexp-in-string
3613 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3614 nil nil 1))))
3615 ;; Remove closed when `org-adapt-indentation' is nil.
3616 (should
3617 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3618 (org-test-with-temp-text "\
3620 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
3621 Paragraph<point>"
3622 (let ((org-adapt-indentation nil))
3623 (org-add-planning-info nil nil 'closed))
3624 (replace-regexp-in-string
3625 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3626 nil nil 1))))
3627 (should
3628 (equal "* H\nParagraph"
3629 (org-test-with-temp-text "\
3631 CLOSED: [2015-06-25 Thu]
3632 Paragraph<point>"
3633 (let ((org-adapt-indentation nil))
3634 (org-add-planning-info nil nil 'closed))
3635 (replace-regexp-in-string
3636 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3637 nil nil 1))))
3638 ;; Remove closed entry and delete empty line.
3639 (should
3640 (equal "\
3642 Paragraph"
3643 (org-test-with-temp-text "\
3645 CLOSED: [2015-06-24 Wed]
3646 Paragraph<point>"
3647 (let ((org-adapt-indentation t))
3648 (org-add-planning-info nil nil 'closed))
3649 (replace-regexp-in-string
3650 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3651 nil nil 1))))
3652 ;; Remove one entry and update another.
3653 (should
3654 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3655 (org-test-with-temp-text "\
3657 SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
3658 Paragraph<point>"
3659 (let ((org-adapt-indentation t))
3660 (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
3661 (replace-regexp-in-string
3662 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3663 nil nil 1)))))
3666 ;;; Property API
3668 (ert-deftest test-org/buffer-property-keys ()
3669 "Test `org-buffer-property-keys' specifications."
3670 ;; Retrieve properties accross siblings.
3671 (should
3672 (equal '("A" "B")
3673 (org-test-with-temp-text "
3674 * H1
3675 :PROPERTIES:
3676 :A: 1
3677 :END:
3678 * H2
3679 :PROPERTIES:
3680 :B: 1
3681 :END:"
3682 (org-buffer-property-keys))))
3683 ;; Retrieve properties accross children.
3684 (should
3685 (equal '("A" "B")
3686 (org-test-with-temp-text "
3687 * H1
3688 :PROPERTIES:
3689 :A: 1
3690 :END:
3691 ** H2
3692 :PROPERTIES:
3693 :B: 1
3694 :END:"
3695 (org-buffer-property-keys))))
3696 ;; Retrieve muliple properties in the same drawer.
3697 (should
3698 (equal '("A" "B")
3699 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3700 (org-buffer-property-keys))))
3701 ;; Ignore extension symbol in property name.
3702 (should
3703 (equal '("A")
3704 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
3705 (org-buffer-property-keys))))
3706 ;; With non-nil COLUMNS, extract property names from columns.
3707 (should
3708 (equal '("A" "B")
3709 (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
3710 (org-buffer-property-keys nil nil t))))
3711 (should
3712 (equal '("A" "B" "COLUMNS")
3713 (org-test-with-temp-text
3714 "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
3715 (org-buffer-property-keys nil nil t))))
3716 ;; With non-nil IGNORE-MALFORMED malformed property drawers are silently ignored.
3717 (should
3718 (equal '("A")
3719 (org-test-with-temp-text
3720 "* a\n:PROPERTIES:\n:A: 1\n:END:\n* b\n:PROPERTIES:\nsome junk here\n:END:\n"
3721 (org-buffer-property-keys nil nil nil t)))))
3723 (ert-deftest test-org/property-values ()
3724 "Test `org-property-values' specifications."
3725 ;; Regular test.
3726 (should
3727 (equal '("2" "1")
3728 (org-test-with-temp-text
3729 "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
3730 (org-property-values "A"))))
3731 ;; Ignore empty values.
3732 (should-not
3733 (org-test-with-temp-text
3734 "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
3735 (org-property-values "A")))
3736 ;; Take into consideration extended values.
3737 (should
3738 (equal '("1 2")
3739 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
3740 (org-property-values "A")))))
3742 (ert-deftest test-org/find-property ()
3743 "Test `org-find-property' specifications."
3744 ;; Regular test.
3745 (should
3746 (= 1
3747 (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
3748 (org-find-property "prop"))))
3749 ;; Ignore false positives.
3750 (should
3751 (= 27
3752 (org-test-with-temp-text
3753 "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
3754 (org-find-property "A"))))
3755 ;; Return first entry found in buffer.
3756 (should
3757 (= 1
3758 (org-test-with-temp-text
3759 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
3760 (org-find-property "A"))))
3761 ;; Only search visible part of the buffer.
3762 (should
3763 (= 31
3764 (org-test-with-temp-text
3765 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
3766 (org-narrow-to-subtree)
3767 (org-find-property "A"))))
3768 ;; With optional argument, only find entries with a specific value.
3769 (should-not
3770 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3771 (org-find-property "A" "2")))
3772 (should
3773 (= 31
3774 (org-test-with-temp-text
3775 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
3776 (org-find-property "A" "2"))))
3777 ;; Use "nil" for explicit nil values.
3778 (should
3779 (= 31
3780 (org-test-with-temp-text
3781 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
3782 (org-find-property "A" "nil")))))
3784 (ert-deftest test-org/entry-delete ()
3785 "Test `org-entry-delete' specifications."
3786 ;; Regular test.
3787 (should
3788 (string-match
3789 " *:PROPERTIES:\n *:B: +2\n *:END:"
3790 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3791 (org-entry-delete (point) "A")
3792 (buffer-string))))
3793 ;; Also remove accumulated properties.
3794 (should-not
3795 (string-match
3796 ":A"
3797 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
3798 (org-entry-delete (point) "A")
3799 (buffer-string))))
3800 ;; When last property is removed, remove the property drawer.
3801 (should-not
3802 (string-match
3803 ":PROPERTIES:"
3804 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
3805 (org-entry-delete (point) "A")
3806 (buffer-string))))
3807 ;; Return a non-nil value when some property was removed.
3808 (should
3809 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3810 (org-entry-delete (point) "A")))
3811 (should-not
3812 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3813 (org-entry-delete (point) "C"))))
3815 (ert-deftest test-org/entry-get ()
3816 "Test `org-entry-get' specifications."
3817 ;; Regular test.
3818 (should
3819 (equal "1"
3820 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3821 (org-entry-get (point) "A"))))
3822 ;; Ignore case.
3823 (should
3824 (equal "1"
3825 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3826 (org-entry-get (point) "a"))))
3827 ;; Handle extended values, both before and after base value.
3828 (should
3829 (equal "1 2 3"
3830 (org-test-with-temp-text
3831 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
3832 (org-entry-get (point) "A"))))
3833 ;; Empty values are returned as the empty string.
3834 (should
3835 (equal ""
3836 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
3837 (org-entry-get (point) "A"))))
3838 ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
3839 ;; otherwise, return nil.
3840 (should-not
3841 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
3842 (org-entry-get (point) "A")))
3843 (should
3844 (equal "nil"
3845 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
3846 (org-entry-get (point) "A" nil t))))
3847 ;; Return nil when no property is found, independently on the
3848 ;; LITERAL-NIL argument.
3849 (should-not
3850 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3851 (org-entry-get (point) "B")))
3852 (should-not
3853 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3854 (org-entry-get (point) "B" nil t)))
3855 ;; Handle inheritance, when allowed. Include extended values and
3856 ;; possibly global values.
3857 (should
3858 (equal
3860 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3861 (org-entry-get (point) "A" t))))
3862 (should
3863 (equal
3865 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3866 (let ((org-use-property-inheritance t))
3867 (org-entry-get (point) "A" 'selective)))))
3868 (should-not
3869 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3870 (let ((org-use-property-inheritance nil))
3871 (org-entry-get (point) "A" 'selective))))
3872 (should
3873 (equal
3874 "1 2"
3875 (org-test-with-temp-text
3876 "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
3877 (org-entry-get (point-max) "A" t))))
3878 (should
3879 (equal "1"
3880 (org-test-with-temp-text
3881 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
3882 (org-mode-restart)
3883 (org-entry-get (point-max) "A" t))))
3884 (should
3885 (equal "0 1"
3886 (org-test-with-temp-text
3887 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
3888 (org-mode-restart)
3889 (org-entry-get (point-max) "A" t)))))
3891 (ert-deftest test-org/entry-properties ()
3892 "Test `org-entry-properties' specifications."
3893 ;; Get "ITEM" property.
3894 (should
3895 (equal "H"
3896 (org-test-with-temp-text "* TODO H"
3897 (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
3898 (should
3899 (equal "H"
3900 (org-test-with-temp-text "* TODO H"
3901 (cdr (assoc "ITEM" (org-entry-properties))))))
3902 ;; Get "TODO" property. TODO keywords are case sensitive.
3903 (should
3904 (equal "TODO"
3905 (org-test-with-temp-text "* TODO H"
3906 (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
3907 (should
3908 (equal "TODO"
3909 (org-test-with-temp-text "* TODO H"
3910 (cdr (assoc "TODO" (org-entry-properties))))))
3911 (should-not
3912 (org-test-with-temp-text "* H"
3913 (assoc "TODO" (org-entry-properties nil "TODO"))))
3914 (should-not
3915 (org-test-with-temp-text "* todo H"
3916 (assoc "TODO" (org-entry-properties nil "TODO"))))
3917 ;; Get "PRIORITY" property.
3918 (should
3919 (equal "A"
3920 (org-test-with-temp-text "* [#A] H"
3921 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
3922 (should
3923 (equal "A"
3924 (org-test-with-temp-text "* [#A] H"
3925 (cdr (assoc "PRIORITY" (org-entry-properties))))))
3926 (should
3927 (equal (char-to-string org-default-priority)
3928 (org-test-with-temp-text "* H"
3929 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
3930 ;; Get "FILE" property.
3931 (should
3932 (org-test-with-temp-text-in-file "* H\nParagraph"
3933 (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
3934 (buffer-file-name))))
3935 (should
3936 (org-test-with-temp-text-in-file "* H\nParagraph"
3937 (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
3938 (buffer-file-name))))
3939 (should-not
3940 (org-test-with-temp-text "* H\nParagraph"
3941 (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
3942 ;; Get "TAGS" property.
3943 (should
3944 (equal ":tag1:tag2:"
3945 (org-test-with-temp-text "* H :tag1:tag2:"
3946 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
3947 (should
3948 (equal ":tag1:tag2:"
3949 (org-test-with-temp-text "* H :tag1:tag2:"
3950 (cdr (assoc "TAGS" (org-entry-properties))))))
3951 (should-not
3952 (org-test-with-temp-text "* H"
3953 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
3954 ;; Get "ALLTAGS" property.
3955 (should
3956 (equal ":tag1:tag2:"
3957 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
3958 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
3959 (should
3960 (equal ":tag1:tag2:"
3961 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
3962 (cdr (assoc "ALLTAGS" (org-entry-properties))))))
3963 (should-not
3964 (org-test-with-temp-text "* H"
3965 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
3966 ;; Get "BLOCKED" property.
3967 (should
3968 (equal "t"
3969 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
3970 (let ((org-enforce-todo-dependencies t)
3971 (org-blocker-hook
3972 '(org-block-todo-from-children-or-siblings-or-parent)))
3973 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
3974 (should
3975 (equal ""
3976 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
3977 (let ((org-enforce-todo-dependencies t)
3978 (org-blocker-hook
3979 '(org-block-todo-from-children-or-siblings-or-parent)))
3980 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
3981 ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
3982 (should
3983 (equal
3984 "[2012-03-29 thu.]"
3985 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
3986 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
3987 (should
3988 (equal
3989 "[2012-03-29 thu.]"
3990 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
3991 (cdr (assoc "CLOSED" (org-entry-properties))))))
3992 (should-not
3993 (org-test-with-temp-text "* H"
3994 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
3995 (should
3996 (equal
3997 "<2014-03-04 tue.>"
3998 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3999 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
4000 (should
4001 (equal
4002 "<2014-03-04 tue.>"
4003 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
4004 (cdr (assoc "DEADLINE" (org-entry-properties))))))
4005 (should-not
4006 (org-test-with-temp-text "* H"
4007 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
4008 (should
4009 (equal
4010 "<2014-03-04 tue.>"
4011 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
4012 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
4013 (should
4014 (equal
4015 "<2014-03-04 tue.>"
4016 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
4017 (cdr (assoc "SCHEDULED" (org-entry-properties))))))
4018 (should-not
4019 (org-test-with-temp-text "* H"
4020 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
4021 ;; Get "CATEGORY"
4022 (should
4023 (equal "cat"
4024 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
4025 (cdr (assoc "CATEGORY" (org-entry-properties))))))
4026 (should
4027 (equal "cat"
4028 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
4029 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
4030 (should
4031 (equal "cat"
4032 (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
4033 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
4034 (should
4035 (equal "cat2"
4036 (org-test-with-temp-text
4037 (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
4038 "\n"
4039 "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
4040 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
4041 ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
4042 (should
4043 (equal "<2012-03-29 thu.>"
4044 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
4045 (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
4046 (should
4047 (equal "[2012-03-29 thu.]"
4048 (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
4049 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
4050 (should
4051 (equal "<2012-03-29 thu.>"
4052 (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
4053 (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
4054 (should
4055 (equal "[2014-03-04 tue.]"
4056 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
4057 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties nil "TIMESTAMP_IA"))))))
4058 ;; Get standard properties.
4059 (should
4060 (equal "1"
4061 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
4062 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
4063 ;; Handle extended properties.
4064 (should
4065 (equal "1 2 3"
4066 (org-test-with-temp-text
4067 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
4068 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
4069 (should
4070 (equal "1 2 3"
4071 (org-test-with-temp-text
4072 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
4073 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
4074 ;; Ignore forbidden (special) properties.
4075 (should-not
4076 (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
4077 (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
4079 (ert-deftest test-org/entry-put ()
4080 "Test `org-entry-put' specifications."
4081 ;; Error when not a string or nil.
4082 (should-error
4083 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
4084 (org-entry-put 1 "test" 2)))
4085 ;; Error when property name is invalid.
4086 (should-error
4087 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
4088 (org-entry-put 1 "no space" "value")))
4089 (should-error
4090 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
4091 (org-entry-put 1 "" "value")))
4092 ;; Set "TODO" property.
4093 (should
4094 (string-match (regexp-quote " TODO H")
4095 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
4096 (org-entry-put (point) "TODO" "TODO")
4097 (buffer-string))))
4098 (should
4099 (string-match (regexp-quote "* H")
4100 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
4101 (org-entry-put (point) "TODO" nil)
4102 (buffer-string))))
4103 ;; Set "PRIORITY" property.
4104 (should
4105 (equal "* [#A] H"
4106 (org-test-with-temp-text "* [#B] H"
4107 (org-entry-put (point) "PRIORITY" "A")
4108 (buffer-string))))
4109 (should
4110 (equal "* H"
4111 (org-test-with-temp-text "* [#B] H"
4112 (org-entry-put (point) "PRIORITY" nil)
4113 (buffer-string))))
4114 ;; Set "SCHEDULED" property.
4115 (should
4116 (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
4117 (org-test-with-temp-text "* H"
4118 (org-entry-put (point) "SCHEDULED" "2014-03-04")
4119 (buffer-string))))
4120 (should
4121 (string= "* H\n"
4122 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
4123 (org-entry-put (point) "SCHEDULED" nil)
4124 (buffer-string))))
4125 (should
4126 (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
4127 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
4128 (org-entry-put (point) "SCHEDULED" "earlier")
4129 (buffer-string))))
4130 (should
4131 (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
4132 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
4133 (org-entry-put (point) "SCHEDULED" "later")
4134 (buffer-string))))
4135 ;; Set "DEADLINE" property.
4136 (should
4137 (string-match "^ *DEADLINE: <2014-03-04 .*?>"
4138 (org-test-with-temp-text "* H"
4139 (org-entry-put (point) "DEADLINE" "2014-03-04")
4140 (buffer-string))))
4141 (should
4142 (string= "* H\n"
4143 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
4144 (org-entry-put (point) "DEADLINE" nil)
4145 (buffer-string))))
4146 (should
4147 (string-match "^ *DEADLINE: <2014-03-03 .*?>"
4148 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
4149 (org-entry-put (point) "DEADLINE" "earlier")
4150 (buffer-string))))
4151 (should
4152 (string-match "^ *DEADLINE: <2014-03-05 .*?>"
4153 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
4154 (org-entry-put (point) "DEADLINE" "later")
4155 (buffer-string))))
4156 ;; Set "CATEGORY" property
4157 (should
4158 (string-match "^ *:CATEGORY: cat"
4159 (org-test-with-temp-text "* H"
4160 (org-entry-put (point) "CATEGORY" "cat")
4161 (buffer-string))))
4162 ;; Regular properties, with or without pre-existing drawer.
4163 (should
4164 (string-match "^ *:A: +2$"
4165 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
4166 (org-entry-put (point) "A" "2")
4167 (buffer-string))))
4168 (should
4169 (string-match "^ *:A: +1$"
4170 (org-test-with-temp-text "* H"
4171 (org-entry-put (point) "A" "1")
4172 (buffer-string))))
4173 ;; Special case: two consecutive headlines.
4174 (should
4175 (string-match "\\* A\n *:PROPERTIES:"
4176 (org-test-with-temp-text "* A\n** B"
4177 (org-entry-put (point) "A" "1")
4178 (buffer-string)))))
4181 ;;; Radio Targets
4183 (ert-deftest test-org/update-radio-target-regexp ()
4184 "Test `org-update-radio-target-regexp' specifications."
4185 ;; Properly update cache with no previous radio target regexp.
4186 (should
4187 (eq 'link
4188 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
4189 (save-excursion (goto-char (point-max)) (org-element-context))
4190 (insert "<<<")
4191 (search-forward "o")
4192 (insert ">>>")
4193 (org-update-radio-target-regexp)
4194 (goto-char (point-max))
4195 (org-element-type (org-element-context)))))
4196 ;; Properly update cache with previous radio target regexp.
4197 (should
4198 (eq 'link
4199 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
4200 (save-excursion (goto-char (point-max)) (org-element-context))
4201 (insert "<<<")
4202 (search-forward "o")
4203 (insert ">>>")
4204 (org-update-radio-target-regexp)
4205 (search-backward "r")
4206 (delete-char 5)
4207 (insert "new")
4208 (org-update-radio-target-regexp)
4209 (goto-char (point-max))
4210 (delete-region (line-beginning-position) (point))
4211 (insert "new")
4212 (org-element-type (org-element-context))))))
4215 ;;; Sparse trees
4217 (ert-deftest test-org/match-sparse-tree ()
4218 "Test `org-match-sparse-tree' specifications."
4219 ;; Match tags.
4220 (should-not
4221 (org-test-with-temp-text "* H\n** H1 :tag:"
4222 (org-match-sparse-tree nil "tag")
4223 (search-forward "H1")
4224 (org-invisible-p2)))
4225 (should
4226 (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
4227 (org-match-sparse-tree nil "tag")
4228 (search-forward "H2")
4229 (org-invisible-p2)))
4230 ;; "-" operator for tags.
4231 (should-not
4232 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
4233 (org-match-sparse-tree nil "tag1-tag2")
4234 (search-forward "H1")
4235 (org-invisible-p2)))
4236 (should
4237 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
4238 (org-match-sparse-tree nil "tag1-tag2")
4239 (search-forward "H2")
4240 (org-invisible-p2)))
4241 ;; "&" operator for tags.
4242 (should
4243 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
4244 (org-match-sparse-tree nil "tag1&tag2")
4245 (search-forward "H1")
4246 (org-invisible-p2)))
4247 (should-not
4248 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
4249 (org-match-sparse-tree nil "tag1&tag2")
4250 (search-forward "H2")
4251 (org-invisible-p2)))
4252 ;; "|" operator for tags.
4253 (should-not
4254 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
4255 (org-match-sparse-tree nil "tag1|tag2")
4256 (search-forward "H1")
4257 (org-invisible-p2)))
4258 (should-not
4259 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
4260 (org-match-sparse-tree nil "tag1|tag2")
4261 (search-forward "H2")
4262 (org-invisible-p2)))
4263 ;; Regexp match on tags.
4264 (should-not
4265 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
4266 (org-match-sparse-tree nil "{^tag.*}")
4267 (search-forward "H1")
4268 (org-invisible-p2)))
4269 (should
4270 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
4271 (org-match-sparse-tree nil "{^tag.*}")
4272 (search-forward "H2")
4273 (org-invisible-p2)))
4274 ;; Match group tags.
4275 (should-not
4276 (org-test-with-temp-text
4277 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
4278 (org-match-sparse-tree nil "work")
4279 (search-forward "H1")
4280 (org-invisible-p2)))
4281 (should-not
4282 (org-test-with-temp-text
4283 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
4284 (org-match-sparse-tree nil "work")
4285 (search-forward "H2")
4286 (org-invisible-p2)))
4287 ;; Match group tags with hard brackets.
4288 (should-not
4289 (org-test-with-temp-text
4290 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
4291 (org-match-sparse-tree nil "work")
4292 (search-forward "H1")
4293 (org-invisible-p2)))
4294 (should-not
4295 (org-test-with-temp-text
4296 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
4297 (org-match-sparse-tree nil "work")
4298 (search-forward "H2")
4299 (org-invisible-p2)))
4300 ;; Match tags in hierarchies
4301 (should-not
4302 (org-test-with-temp-text
4303 "#+TAGS: [ Lev_1 : Lev_2 ]\n
4304 #+TAGS: [ Lev_2 : Lev_3 ]\n
4305 #+TAGS: { Lev_3 : Lev_4 }\n
4306 * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
4307 (org-match-sparse-tree nil "Lev_1")
4308 (search-forward "H4")
4309 (org-invisible-p2)))
4310 ;; Match regular expressions in tags
4311 (should-not
4312 (org-test-with-temp-text
4313 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
4314 (org-match-sparse-tree nil "Lev")
4315 (search-forward "H1")
4316 (org-invisible-p2)))
4317 (should
4318 (org-test-with-temp-text
4319 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
4320 (org-match-sparse-tree nil "Lev")
4321 (search-forward "H1")
4322 (org-invisible-p2)))
4323 ;; Match properties.
4324 (should
4325 (org-test-with-temp-text
4326 "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
4327 (org-match-sparse-tree nil "A=\"1\"")
4328 (search-forward "H2")
4329 (org-invisible-p2)))
4330 (should-not
4331 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
4332 (org-match-sparse-tree nil "A=\"1\"")
4333 (search-forward "H2")
4334 (org-invisible-p2)))
4335 ;; Case is not significant when matching properties.
4336 (should-not
4337 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
4338 (org-match-sparse-tree nil "a=\"1\"")
4339 (search-forward "H2")
4340 (org-invisible-p2)))
4341 (should-not
4342 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
4343 (org-match-sparse-tree nil "A=\"1\"")
4344 (search-forward "H2")
4345 (org-invisible-p2)))
4346 ;; Match special LEVEL property.
4347 (should-not
4348 (org-test-with-temp-text "* H\n** H1\n*** H2"
4349 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
4350 (search-forward "H1")
4351 (org-invisible-p2)))
4352 (should
4353 (org-test-with-temp-text "* H\n** H1\n*** H2"
4354 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
4355 (search-forward "H2")
4356 (org-invisible-p2)))
4357 ;; Comparison operators when matching properties.
4358 (should
4359 (org-test-with-temp-text
4360 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
4361 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
4362 (search-forward "H1")
4363 (org-invisible-p2)))
4364 (should-not
4365 (org-test-with-temp-text
4366 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
4367 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
4368 (search-forward "H2")
4369 (org-invisible-p2)))
4370 ;; Regexp match on properties values.
4371 (should-not
4372 (org-test-with-temp-text
4373 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
4374 (org-match-sparse-tree nil "A={f.*}")
4375 (search-forward "H1")
4376 (org-invisible-p2)))
4377 (should
4378 (org-test-with-temp-text
4379 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
4380 (org-match-sparse-tree nil "A={f.*}")
4381 (search-forward "H2")
4382 (org-invisible-p2)))
4383 ;; With an optional argument, limit match to TODO entries.
4384 (should-not
4385 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
4386 (org-match-sparse-tree t "tag")
4387 (search-forward "H1")
4388 (org-invisible-p2)))
4389 (should
4390 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
4391 (org-match-sparse-tree t "tag")
4392 (search-forward "H2")
4393 (org-invisible-p2))))
4395 (ert-deftest test-org/occur ()
4396 "Test `org-occur' specifications."
4397 ;; Count number of matches.
4398 (should
4399 (= 1
4400 (org-test-with-temp-text "* H\nA\n* H2"
4401 (org-occur "A"))))
4402 (should
4403 (= 2
4404 (org-test-with-temp-text "* H\nA\n* H2\nA"
4405 (org-occur "A"))))
4406 ;; Test CALLBACK optional argument.
4407 (should
4408 (= 0
4409 (org-test-with-temp-text "* H\nA\n* H2"
4410 (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
4411 (should
4412 (= 1
4413 (org-test-with-temp-text "* H\nA\n* H2\nA"
4414 (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
4415 ;; Case-fold searches according to `org-occur-case-fold-search'.
4416 (should
4417 (= 2
4418 (org-test-with-temp-text "Aa"
4419 (let ((org-occur-case-fold-search t)) (org-occur "A")))))
4420 (should
4421 (= 2
4422 (org-test-with-temp-text "Aa"
4423 (let ((org-occur-case-fold-search t)) (org-occur "a")))))
4424 (should
4425 (= 1
4426 (org-test-with-temp-text "Aa"
4427 (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
4428 (should
4429 (= 1
4430 (org-test-with-temp-text "Aa"
4431 (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
4432 (should
4433 (= 1
4434 (org-test-with-temp-text "Aa"
4435 (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
4436 (should
4437 (= 2
4438 (org-test-with-temp-text "Aa"
4439 (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
4442 ;;; Tags
4444 (ert-deftest test-org/tag-string-to-alist ()
4445 "Test `org-tag-string-to-alist' specifications."
4446 ;; Tag without selection key.
4447 (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
4448 ;; Tag with selection key.
4449 (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
4450 ;; Tag group.
4451 (should
4452 (equal
4453 (org-tag-string-to-alist "[ group : t1 t2 ]")
4454 '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
4455 ;; Mutually exclusive tags.
4456 (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
4457 '((:startgroup) ("tag1") ("tag2") (:endgroup))))
4458 (should
4459 (equal
4460 (org-tag-string-to-alist "{ group : tag1 tag2 }")
4461 '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
4463 (ert-deftest test-org/tag-alist-to-string ()
4464 "Test `org-tag-alist-to-string' specifications."
4465 (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
4466 (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
4467 (should
4468 (equal
4469 (org-tag-alist-to-string
4470 '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
4471 "[ group : t1 t2 ]"))
4472 (should
4473 (equal (org-tag-alist-to-string
4474 '((:startgroup) ("tag1") ("tag2") (:endgroup)))
4475 "{ tag1 tag2 }"))
4476 (should
4477 (equal
4478 (org-tag-alist-to-string
4479 '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
4480 "{ group : tag1 tag2 }")))
4482 (ert-deftest test-org/tag-alist-to-groups ()
4483 "Test `org-tag-alist-to-groups' specifications."
4484 (should
4485 (equal (org-tag-alist-to-groups
4486 '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
4487 '(("group" "t1" "t2"))))
4488 (should
4489 (equal
4490 (org-tag-alist-to-groups
4491 '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
4492 '(("group" "t1" "t2"))))
4493 (should-not
4494 (org-tag-alist-to-groups
4495 '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
4497 (ert-deftest test-org/tag-align ()
4498 "Test `org-align-tags-here' with different display width."
4499 (should
4500 ;; 12345678901234567890
4501 (equal "* Test :abc:"
4502 (org-test-with-temp-text "* Test :abc:"
4503 (let ((org-tags-column -20)
4504 (indent-tabs-mode nil))
4505 (org-fix-tags-on-the-fly))
4506 (buffer-string))))
4507 (should
4508 ;; 12345678901234567890
4509 (equal "* Test :日本語:"
4510 (org-test-with-temp-text "* Test :日本語:"
4511 (let ((org-tags-column -20)
4512 (indent-tabs-mode nil))
4513 (org-fix-tags-on-the-fly))
4514 (buffer-string)))))
4516 (ert-deftest test-org/tags-at ()
4517 (should
4518 (equal '("foo" "bar")
4519 (org-test-with-temp-text
4520 "* T<point>est :foo:bar:"
4521 (org-get-tags-at)))))
4524 ;;; Timestamps API
4526 (ert-deftest test-org/time-stamp ()
4527 "Test `org-time-stamp' specifications."
4528 ;; Insert chosen time stamp at point.
4529 (should
4530 (string-match
4531 "Te<2014-03-04 .*?>xt"
4532 (org-test-with-temp-text "Te<point>xt"
4533 (cl-letf (((symbol-function 'org-read-date)
4534 (lambda (&rest args)
4535 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
4536 (org-time-stamp nil)
4537 (buffer-string)))))
4538 ;; With a prefix argument, also insert time.
4539 (should
4540 (string-match
4541 "Te<2014-03-04 .*? 00:41>xt"
4542 (org-test-with-temp-text "Te<point>xt"
4543 (cl-letf (((symbol-function 'org-read-date)
4544 (lambda (&rest args)
4545 (apply #'encode-time
4546 (org-parse-time-string "2014-03-04 00:41")))))
4547 (org-time-stamp '(4))
4548 (buffer-string)))))
4549 ;; With two universal prefix arguments, insert an active timestamp
4550 ;; with the current time without prompting the user.
4551 (should
4552 (string-match
4553 "Te<2014-03-04 .*? 00:41>xt"
4554 (org-test-with-temp-text "Te<point>xt"
4555 (cl-letf (((symbol-function 'current-time)
4556 (lambda ()
4557 (apply #'encode-time
4558 (org-parse-time-string "2014-03-04 00:41")))))
4559 (org-time-stamp '(16))
4560 (buffer-string)))))
4561 ;; When optional argument is non-nil, insert an inactive timestamp.
4562 (should
4563 (string-match
4564 "Te\\[2014-03-04 .*?\\]xt"
4565 (org-test-with-temp-text "Te<point>xt"
4566 (cl-letf (((symbol-function 'org-read-date)
4567 (lambda (&rest args)
4568 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
4569 (org-time-stamp nil t)
4570 (buffer-string)))))
4571 ;; When called from a timestamp, replace existing one.
4572 (should
4573 (string-match
4574 "<2014-03-04 .*?>"
4575 (org-test-with-temp-text "<2012-03-29<point> thu.>"
4576 (cl-letf (((symbol-function 'org-read-date)
4577 (lambda (&rest args)
4578 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
4579 (org-time-stamp nil)
4580 (buffer-string)))))
4581 (should
4582 (string-match
4583 "<2014-03-04 .*?>--<2014-03-04 .*?>"
4584 (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
4585 (cl-letf (((symbol-function 'org-read-date)
4586 (lambda (&rest args)
4587 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
4588 (org-time-stamp nil)
4589 (buffer-string)))))
4590 ;; When replacing a timestamp, preserve repeater, if any.
4591 (should
4592 (string-match
4593 "<2014-03-04 .*? \\+2y>"
4594 (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
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)
4599 (buffer-string)))))
4600 ;; When called twice in a raw, build a date range.
4601 (should
4602 (string-match
4603 "<2012-03-29 .*?>--<2014-03-04 .*?>"
4604 (org-test-with-temp-text "<2012-03-29 thu.><point>"
4605 (cl-letf (((symbol-function 'org-read-date)
4606 (lambda (&rest args)
4607 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
4608 (let ((last-command 'org-time-stamp)
4609 (this-command 'org-time-stamp))
4610 (org-time-stamp nil))
4611 (buffer-string))))))
4613 (ert-deftest test-org/timestamp-has-time-p ()
4614 "Test `org-timestamp-has-time-p' specifications."
4615 ;; With time.
4616 (should
4617 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
4618 (org-timestamp-has-time-p (org-element-context))))
4619 ;; Without time.
4620 (should-not
4621 (org-test-with-temp-text "<2012-03-29 Thu>"
4622 (org-timestamp-has-time-p (org-element-context)))))
4624 (ert-deftest test-org/timestamp-format ()
4625 "Test `org-timestamp-format' specifications."
4626 ;; Regular test.
4627 (should
4628 (equal
4629 "2012-03-29 16:40"
4630 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
4631 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
4632 ;; Range end.
4633 (should
4634 (equal
4635 "2012-03-29"
4636 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
4637 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
4639 (ert-deftest test-org/timestamp-split-range ()
4640 "Test `org-timestamp-split-range' specifications."
4641 ;; Extract range start (active).
4642 (should
4643 (equal '(2012 3 29)
4644 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4645 (let ((ts (org-timestamp-split-range (org-element-context))))
4646 (mapcar (lambda (p) (org-element-property p ts))
4647 '(:year-end :month-end :day-end))))))
4648 ;; Extract range start (inactive)
4649 (should
4650 (equal '(2012 3 29)
4651 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
4652 (let ((ts (org-timestamp-split-range (org-element-context))))
4653 (mapcar (lambda (p) (org-element-property p ts))
4654 '(:year-end :month-end :day-end))))))
4655 ;; Extract range end (active).
4656 (should
4657 (equal '(2012 3 30)
4658 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4659 (let ((ts (org-timestamp-split-range
4660 (org-element-context) t)))
4661 (mapcar (lambda (p) (org-element-property p ts))
4662 '(:year-end :month-end :day-end))))))
4663 ;; Extract range end (inactive)
4664 (should
4665 (equal '(2012 3 30)
4666 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
4667 (let ((ts (org-timestamp-split-range
4668 (org-element-context) t)))
4669 (mapcar (lambda (p) (org-element-property p ts))
4670 '(:year-end :month-end :day-end))))))
4671 ;; Return the timestamp if not a range.
4672 (should
4673 (org-test-with-temp-text "[2012-03-29 Thu]"
4674 (let* ((ts-orig (org-element-context))
4675 (ts-copy (org-timestamp-split-range ts-orig)))
4676 (eq ts-orig ts-copy))))
4677 (should
4678 (org-test-with-temp-text "<%%(org-float t 4 2)>"
4679 (let* ((ts-orig (org-element-context))
4680 (ts-copy (org-timestamp-split-range ts-orig)))
4681 (eq ts-orig ts-copy)))))
4683 (ert-deftest test-org/timestamp-translate ()
4684 "Test `org-timestamp-translate' specifications."
4685 ;; Translate whole date range.
4686 (should
4687 (equal "<29>--<30>"
4688 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4689 (let ((org-display-custom-times t)
4690 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4691 (org-timestamp-translate (org-element-context))))))
4692 ;; Translate date range start.
4693 (should
4694 (equal "<29>"
4695 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4696 (let ((org-display-custom-times t)
4697 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4698 (org-timestamp-translate (org-element-context) 'start)))))
4699 ;; Translate date range end.
4700 (should
4701 (equal "<30>"
4702 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4703 (let ((org-display-custom-times t)
4704 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4705 (org-timestamp-translate (org-element-context) 'end)))))
4706 ;; Translate time range.
4707 (should
4708 (equal "<08>--<16>"
4709 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
4710 (let ((org-display-custom-times t)
4711 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
4712 (org-timestamp-translate (org-element-context))))))
4713 ;; Translate non-range timestamp.
4714 (should
4715 (equal "<29>"
4716 (org-test-with-temp-text "<2012-03-29 Thu>"
4717 (let ((org-display-custom-times t)
4718 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4719 (org-timestamp-translate (org-element-context))))))
4720 ;; Do not change `diary' timestamps.
4721 (should
4722 (equal "<%%(org-float t 4 2)>"
4723 (org-test-with-temp-text "<%%(org-float t 4 2)>"
4724 (let ((org-display-custom-times t)
4725 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4726 (org-timestamp-translate (org-element-context)))))))
4730 ;;; Visibility
4732 (ert-deftest test-org/flag-drawer ()
4733 "Test `org-flag-drawer' specifications."
4734 ;; Hide drawer.
4735 (should
4736 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4737 (org-flag-drawer t)
4738 (get-char-property (line-end-position) 'invisible)))
4739 ;; Show drawer.
4740 (should-not
4741 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4742 (org-flag-drawer t)
4743 (org-flag-drawer nil)
4744 (get-char-property (line-end-position) 'invisible)))
4745 ;; Test optional argument.
4746 (should
4747 (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
4748 (let ((drawer (save-excursion (search-forward ":D2")
4749 (org-element-at-point))))
4750 (org-flag-drawer t drawer)
4751 (get-char-property (progn (search-forward ":D2") (line-end-position))
4752 'invisible))))
4753 (should-not
4754 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
4755 (let ((drawer (save-excursion (search-forward ":D2")
4756 (org-element-at-point))))
4757 (org-flag-drawer t drawer)
4758 (get-char-property (line-end-position) 'invisible))))
4759 ;; Do not hide fake drawers.
4760 (should-not
4761 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
4762 (forward-line 1)
4763 (org-flag-drawer t)
4764 (get-char-property (line-end-position) 'invisible)))
4765 ;; Do not hide incomplete drawers.
4766 (should-not
4767 (org-test-with-temp-text ":D:\nparagraph"
4768 (forward-line 1)
4769 (org-flag-drawer t)
4770 (get-char-property (line-end-position) 'invisible)))
4771 ;; Do not hide drawers when called from final blank lines.
4772 (should-not
4773 (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
4774 (goto-char (point-max))
4775 (org-flag-drawer t)
4776 (goto-char (point-min))
4777 (get-char-property (line-end-position) 'invisible)))
4778 ;; Don't leave point in an invisible part of the buffer when hiding
4779 ;; a drawer away.
4780 (should-not
4781 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4782 (goto-char (point-max))
4783 (org-flag-drawer t)
4784 (get-char-property (point) 'invisible))))
4786 (ert-deftest test-org/hide-block-toggle ()
4787 "Test `org-hide-block-toggle' specifications."
4788 ;; Error when not at a block.
4789 (should-error
4790 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
4791 (org-hide-block-toggle 'off)
4792 (get-char-property (line-end-position) 'invisible)))
4793 ;; Hide block.
4794 (should
4795 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
4796 (org-hide-block-toggle)
4797 (get-char-property (line-end-position) 'invisible)))
4798 (should
4799 (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
4800 (org-hide-block-toggle)
4801 (get-char-property (line-end-position) 'invisible)))
4802 ;; Show block unconditionally when optional argument is `off'.
4803 (should-not
4804 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4805 (org-hide-block-toggle)
4806 (org-hide-block-toggle 'off)
4807 (get-char-property (line-end-position) 'invisible)))
4808 (should-not
4809 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4810 (org-hide-block-toggle 'off)
4811 (get-char-property (line-end-position) 'invisible)))
4812 ;; Hide block unconditionally when optional argument is non-nil.
4813 (should
4814 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4815 (org-hide-block-toggle t)
4816 (get-char-property (line-end-position) 'invisible)))
4817 (should
4818 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4819 (org-hide-block-toggle)
4820 (org-hide-block-toggle t)
4821 (get-char-property (line-end-position) 'invisible)))
4822 ;; Do not hide block when called from final blank lines.
4823 (should-not
4824 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
4825 (org-hide-block-toggle)
4826 (goto-char (point-min))
4827 (get-char-property (line-end-position) 'invisible)))
4828 ;; Don't leave point in an invisible part of the buffer when hiding
4829 ;; a block away.
4830 (should-not
4831 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
4832 (org-hide-block-toggle)
4833 (get-char-property (point) 'invisible))))
4835 (ert-deftest test-org/hide-block-toggle-maybe ()
4836 "Test `org-hide-block-toggle-maybe' specifications."
4837 (should
4838 (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
4839 (org-hide-block-toggle-maybe)))
4840 (should-not
4841 (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
4843 (ert-deftest test-org/show-set-visibility ()
4844 "Test `org-show-set-visibility' specifications."
4845 ;; Do not throw an error before first heading.
4846 (should
4847 (org-test-with-temp-text "Preamble\n* Headline"
4848 (org-show-set-visibility 'tree)
4850 ;; Test all visibility spans, both on headline and in entry.
4851 (let ((list-visible-lines
4852 (lambda (state headerp)
4853 (org-test-with-temp-text "* Grandmother (0)
4854 ** Uncle (1)
4855 *** Heir (2)
4856 ** Father (3)
4857 Ancestor text (4)
4858 *** Sister (5)
4859 Sibling text (6)
4860 *** Self (7)
4861 Match (8)
4862 **** First born (9)
4863 Child text (10)
4864 **** The other child (11)
4865 *** Brother (12)
4866 ** Aunt (13)
4868 (org-cycle t)
4869 (search-forward (if headerp "Self" "Match"))
4870 (org-show-set-visibility state)
4871 (goto-char (point-min))
4872 (let (result (line 0))
4873 (while (not (eobp))
4874 (unless (org-invisible-p2) (push line result))
4875 (incf line)
4876 (forward-line))
4877 (nreverse result))))))
4878 (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
4879 (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
4880 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
4881 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
4882 (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
4883 (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
4884 (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
4885 (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
4886 (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
4887 (should (equal '(0 1 3 5 7 8 9 11 12 13)
4888 (funcall list-visible-lines 'tree nil)))
4889 (should (equal '(0 1 3 4 5 7 12 13)
4890 (funcall list-visible-lines 'canonical t)))
4891 (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
4892 (funcall list-visible-lines 'canonical nil))))
4893 ;; When point is hidden in a drawer or a block, make sure to make it
4894 ;; visible.
4895 (should-not
4896 (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
4897 (org-hide-block-toggle)
4898 (search-forward "Text")
4899 (org-show-set-visibility 'minimal)
4900 (org-invisible-p2)))
4901 (should-not
4902 (org-test-with-temp-text ":DRAWER:\nText\n:END:"
4903 (org-flag-drawer t)
4904 (search-forward "Text")
4905 (org-show-set-visibility 'minimal)
4906 (org-invisible-p2)))
4907 (should-not
4908 (org-test-with-temp-text
4909 "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
4910 (org-flag-drawer t)
4911 (forward-line -1)
4912 (org-hide-block-toggle)
4913 (search-forward "Text")
4914 (org-show-set-visibility 'minimal)
4915 (org-invisible-p2))))
4918 (provide 'test-org)
4920 ;;; test-org.el ends here