Fix `C-e' with visible lines and arguments
[org-mode/org-tableheadings.git] / testing / lisp / test-org.el
blob08ce4d8b19a20eed8e28b0f0d26b37ef15da85c1
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 ;; At the beginning of a headline, create one above.
1173 (should
1174 (equal "* \n* H"
1175 (org-test-with-temp-text "* H"
1176 (org-insert-heading)
1177 (buffer-string))))
1178 ;; In the middle of a headline, split it if allowed.
1179 (should
1180 (equal "* H\n* 1"
1181 (org-test-with-temp-text "* H<point>1"
1182 (let ((org-M-RET-may-split-line '((headline . t))))
1183 (org-insert-heading))
1184 (buffer-string))))
1185 (should
1186 (equal "* H1\n* "
1187 (org-test-with-temp-text "* H<point>1"
1188 (let ((org-M-RET-may-split-line '((headline . nil))))
1189 (org-insert-heading))
1190 (buffer-string))))
1191 ;; However, splitting cannot happen on TODO keywords, priorities or
1192 ;; tags.
1193 (should
1194 (equal "* TODO H1\n* "
1195 (org-test-with-temp-text "* TO<point>DO H1"
1196 (let ((org-M-RET-may-split-line '((headline . t))))
1197 (org-insert-heading))
1198 (buffer-string))))
1199 (should
1200 (equal "* [#A] H1\n* "
1201 (org-test-with-temp-text "* [#<point>A] H1"
1202 (let ((org-M-RET-may-split-line '((headline . t))))
1203 (org-insert-heading))
1204 (buffer-string))))
1205 (should
1206 (equal "* H1 :tag:\n* "
1207 (org-test-with-temp-text "* H1 :ta<point>g:"
1208 (let ((org-M-RET-may-split-line '((headline . t))))
1209 (org-insert-heading))
1210 (buffer-string))))
1211 ;; When on a list, insert an item instead, unless called with an
1212 ;; universal argument or if list is invisible. In this case, create
1213 ;; a new headline after contents.
1214 (should
1215 (equal "* H\n- item\n- "
1216 (org-test-with-temp-text "* H\n- item<point>"
1217 (let ((org-insert-heading-respect-content nil))
1218 (org-insert-heading))
1219 (buffer-string))))
1220 (should
1221 (equal "* H\n- item\n- item 2\n* "
1222 (org-test-with-temp-text "* H\n- item<point>\n- item 2"
1223 (let ((org-insert-heading-respect-content nil))
1224 (org-insert-heading '(4)))
1225 (buffer-string))))
1226 (should
1227 (equal "* H\n- item\n* "
1228 (org-test-with-temp-text "* H\n- item"
1229 (org-cycle)
1230 (goto-char (point-max))
1231 (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
1232 (buffer-string))))
1233 ;; Preserve list visibility when inserting an item.
1234 (should
1235 (equal
1236 '(outline outline)
1237 (org-test-with-temp-text "- A\n - B\n- C\n - D"
1238 (let ((org-cycle-include-plain-lists t))
1239 (org-cycle)
1240 (forward-line 2)
1241 (org-cycle)
1242 (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
1243 (list (get-char-property (line-beginning-position 0) 'invisible)
1244 (get-char-property (line-end-position 2) 'invisible))))))
1245 ;; When called with one universal argument, insert a new headline at
1246 ;; the end of the current subtree, independently on the position of
1247 ;; point.
1248 (should
1249 (equal
1250 "* H1\n** H2\n* "
1251 (org-test-with-temp-text "* H1\n** H2"
1252 (let ((org-insert-heading-respect-content nil))
1253 (org-insert-heading '(4)))
1254 (buffer-string))))
1255 (should
1256 (equal
1257 "* H1\n** H2\n* "
1258 (org-test-with-temp-text "* H<point>1\n** H2"
1259 (let ((org-insert-heading-respect-content nil))
1260 (org-insert-heading '(4)))
1261 (buffer-string))))
1262 ;; When called with two universal arguments, insert a new headline
1263 ;; at the end of the grandparent subtree.
1264 (should
1265 (equal "* H1\n** H3\n- item\n** H2\n** "
1266 (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
1267 (let ((org-insert-heading-respect-content nil))
1268 (org-insert-heading '(16)))
1269 (buffer-string))))
1270 ;; When optional TOP-LEVEL argument is non-nil, always insert
1271 ;; a level 1 heading.
1272 (should
1273 (equal "* H1\n** H2\n* "
1274 (org-test-with-temp-text "* H1\n** H2<point>"
1275 (org-insert-heading nil nil t)
1276 (buffer-string))))
1277 (should
1278 (equal "* H1\n- item\n* "
1279 (org-test-with-temp-text "* H1\n- item<point>"
1280 (org-insert-heading nil nil t)
1281 (buffer-string))))
1282 ;; Corner case: correctly insert a headline after an empty one.
1283 (should
1284 (equal "* \n* "
1285 (org-test-with-temp-text "* <point>"
1286 (org-insert-heading)
1287 (buffer-string)))))
1289 (ert-deftest test-org/insert-todo-heading-respect-content ()
1290 "Test `org-insert-todo-heading-respect-content' specifications."
1291 ;; Create a TODO heading.
1292 (should
1293 (org-test-with-temp-text "* H1\n Body"
1294 (org-insert-todo-heading-respect-content)
1295 (nth 2 (org-heading-components))))
1296 ;; Add headline at the end of the first subtree
1297 (should
1298 (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
1299 (search-forward "H1Body")
1300 (org-insert-todo-heading-respect-content)
1301 (and (eobp) (org-at-heading-p))))
1302 ;; In a list, do not create a new item.
1303 (should
1304 (org-test-with-temp-text "* H\n- an item\n- another one"
1305 (search-forward "an ")
1306 (org-insert-todo-heading-respect-content)
1307 (and (eobp) (org-at-heading-p)))))
1309 (ert-deftest test-org/clone-with-time-shift ()
1310 "Test `org-clone-subtree-with-time-shift'."
1311 ;; Clone non-repeating once.
1312 (should
1313 (equal "\
1314 * H1\n<2015-06-21>
1315 * H1\n<2015-06-23>
1317 (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
1318 (org-clone-subtree-with-time-shift 1 "+2d")
1319 (replace-regexp-in-string
1320 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1321 nil nil 1))))
1322 ;; Clone repeating once.
1323 (should
1324 (equal "\
1325 * H1\n<2015-06-21>
1326 * H1\n<2015-06-23>
1327 * H1\n<2015-06-25 +1w>
1329 (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
1330 (org-clone-subtree-with-time-shift 1 "+2d")
1331 (replace-regexp-in-string
1332 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1333 nil nil 1))))
1334 ;; Clone non-repeating zero times.
1335 (should
1336 (equal "\
1337 * H1\n<2015-06-21>
1339 (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
1340 (org-clone-subtree-with-time-shift 0 "+2d")
1341 (replace-regexp-in-string
1342 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1343 nil nil 1))))
1344 ;; Clone repeating "zero" times.
1345 (should
1346 (equal "\
1347 * H1\n<2015-06-21>
1348 * H1\n<2015-06-23 +1w>
1350 (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
1351 (org-clone-subtree-with-time-shift 0 "+2d")
1352 (replace-regexp-in-string
1353 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1354 nil nil 1)))))
1357 ;;; Fixed-Width Areas
1359 (ert-deftest test-org/toggle-fixed-width ()
1360 "Test `org-toggle-fixed-width' specifications."
1361 ;; No region: Toggle on fixed-width marker in paragraphs.
1362 (should
1363 (equal ": A"
1364 (org-test-with-temp-text "A"
1365 (org-toggle-fixed-width)
1366 (buffer-string))))
1367 ;; No region: Toggle off fixed-width markers in fixed-width areas.
1368 (should
1369 (equal "A"
1370 (org-test-with-temp-text ": A"
1371 (org-toggle-fixed-width)
1372 (buffer-string))))
1373 ;; No region: Toggle on marker in blank lines after elements or just
1374 ;; after a headline.
1375 (should
1376 (equal "* H\n: "
1377 (org-test-with-temp-text "* H\n"
1378 (forward-line)
1379 (org-toggle-fixed-width)
1380 (buffer-string))))
1381 (should
1382 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
1383 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
1384 (goto-char (point-max))
1385 (org-toggle-fixed-width)
1386 (buffer-string))))
1387 ;; No region: Toggle on marker in front of one line elements (e.g.,
1388 ;; headlines, clocks)
1389 (should
1390 (equal ": * Headline"
1391 (org-test-with-temp-text "* Headline"
1392 (org-toggle-fixed-width)
1393 (buffer-string))))
1394 (should
1395 (equal ": #+KEYWORD: value"
1396 (org-test-with-temp-text "#+KEYWORD: value"
1397 (org-toggle-fixed-width)
1398 (buffer-string))))
1399 ;; No region: error in other situations.
1400 (should-error
1401 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
1402 (forward-line)
1403 (org-toggle-fixed-width)
1404 (buffer-string)))
1405 ;; No region: Indentation is preserved.
1406 (should
1407 (equal "- A\n : B"
1408 (org-test-with-temp-text "- A\n B"
1409 (forward-line)
1410 (org-toggle-fixed-width)
1411 (buffer-string))))
1412 ;; Region: If it contains only fixed-width elements and blank lines,
1413 ;; toggle off fixed-width markup.
1414 (should
1415 (equal "A\n\nB"
1416 (org-test-with-temp-text ": A\n\n: B"
1417 (transient-mark-mode 1)
1418 (push-mark (point) t t)
1419 (goto-char (point-max))
1420 (org-toggle-fixed-width)
1421 (buffer-string))))
1422 ;; Region: If it contains anything else, toggle on fixed-width but
1423 ;; not on fixed-width areas.
1424 (should
1425 (equal ": A\n: \n: B\n: \n: C"
1426 (org-test-with-temp-text "A\n\n: B\n\nC"
1427 (transient-mark-mode 1)
1428 (push-mark (point) t t)
1429 (goto-char (point-max))
1430 (org-toggle-fixed-width)
1431 (buffer-string))))
1432 ;; Region: Ignore blank lines at its end, unless it contains only
1433 ;; such lines.
1434 (should
1435 (equal ": A\n\n"
1436 (org-test-with-temp-text "A\n\n"
1437 (transient-mark-mode 1)
1438 (push-mark (point) t t)
1439 (goto-char (point-max))
1440 (org-toggle-fixed-width)
1441 (buffer-string))))
1442 (should
1443 (equal ": \n: \n"
1444 (org-test-with-temp-text "\n\n"
1445 (transient-mark-mode 1)
1446 (push-mark (point) t t)
1447 (goto-char (point-max))
1448 (org-toggle-fixed-width)
1449 (buffer-string)))))
1453 ;;; Headline
1455 (ert-deftest test-org/get-heading ()
1456 "Test `org-get-heading' specifications."
1457 ;; Return current heading, even if point is not on it.
1458 (should
1459 (equal "H"
1460 (org-test-with-temp-text "* H"
1461 (org-get-heading))))
1462 (should
1463 (equal "H"
1464 (org-test-with-temp-text "* H\nText<point>"
1465 (org-get-heading))))
1466 ;; Without any optional argument, return TODO keywords and tags.
1467 (should
1468 (equal "TODO H"
1469 (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
1470 (org-get-heading))))
1471 (should
1472 (equal "H :tag:"
1473 (org-test-with-temp-text "* H :tag:"
1474 (org-get-heading))))
1475 ;; With NO-TAGS argument, ignore tags.
1476 (should
1477 (equal "TODO H"
1478 (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
1479 (org-get-heading t))))
1480 (should
1481 (equal "H"
1482 (org-test-with-temp-text "* H :tag:"
1483 (org-get-heading t))))
1484 ;; With NO-TODO, ignore TODO keyword.
1485 (should
1486 (equal "H"
1487 (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
1488 (org-get-heading nil t))))
1489 (should
1490 (equal "H :tag:"
1491 (org-test-with-temp-text "* H :tag:"
1492 (org-get-heading nil t))))
1493 ;; TODO keywords are case-sensitive.
1494 (should
1495 (equal "Todo H"
1496 (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
1497 (org-get-heading nil t))))
1498 ;; On an empty headline, return value is consistent.
1499 (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
1500 (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
1501 (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
1502 (should (equal "" (org-test-with-temp-text "* " (org-get-heading t t)))))
1504 (ert-deftest test-org/in-commented-heading-p ()
1505 "Test `org-in-commented-heading-p' specifications."
1506 ;; Commented headline.
1507 (should
1508 (org-test-with-temp-text "* COMMENT Headline\nBody"
1509 (goto-char (point-max))
1510 (org-in-commented-heading-p)))
1511 ;; Commented ancestor.
1512 (should
1513 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1514 (goto-char (point-max))
1515 (org-in-commented-heading-p)))
1516 ;; Comment keyword is case-sensitive.
1517 (should-not
1518 (org-test-with-temp-text "* Comment Headline\nBody"
1519 (goto-char (point-max))
1520 (org-in-commented-heading-p)))
1521 ;; Keyword is standalone.
1522 (should-not
1523 (org-test-with-temp-text "* COMMENTHeadline\nBody"
1524 (goto-char (point-max))
1525 (org-in-commented-heading-p)))
1526 ;; Optional argument.
1527 (should-not
1528 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1529 (goto-char (point-max))
1530 (org-in-commented-heading-p t))))
1532 (ert-deftest test-org/entry-blocked-p ()
1533 ;; Check other dependencies.
1534 (should
1535 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
1536 (let ((org-enforce-todo-dependencies t)
1537 (org-blocker-hook
1538 '(org-block-todo-from-children-or-siblings-or-parent)))
1539 (org-entry-blocked-p))))
1540 (should-not
1541 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
1542 (let ((org-enforce-todo-dependencies t)
1543 (org-blocker-hook
1544 '(org-block-todo-from-children-or-siblings-or-parent)))
1545 (org-entry-blocked-p))))
1546 ;; Entry without a TODO keyword or with a DONE keyword cannot be
1547 ;; blocked.
1548 (should-not
1549 (org-test-with-temp-text "* Blocked\n** TODO one"
1550 (let ((org-enforce-todo-dependencies t)
1551 (org-blocker-hook
1552 '(org-block-todo-from-children-or-siblings-or-parent)))
1553 (org-entry-blocked-p))))
1554 (should-not
1555 (org-test-with-temp-text "* DONE Blocked\n** TODO one"
1556 (let ((org-enforce-todo-dependencies t)
1557 (org-blocker-hook
1558 '(org-block-todo-from-children-or-siblings-or-parent)))
1559 (org-entry-blocked-p))))
1560 ;; Follow :ORDERED: specifications.
1561 (should
1562 (org-test-with-temp-text
1563 "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
1564 (let ((org-enforce-todo-dependencies t)
1565 (org-blocker-hook
1566 '(org-block-todo-from-children-or-siblings-or-parent)))
1567 (org-entry-blocked-p))))
1568 (should-not
1569 (org-test-with-temp-text
1570 "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
1571 (let ((org-enforce-todo-dependencies t)
1572 (org-blocker-hook
1573 '(org-block-todo-from-children-or-siblings-or-parent)))
1574 (org-entry-blocked-p)))))
1576 (ert-deftest test-org/get-outline-path ()
1577 "Test `org-get-outline-path' specifications."
1578 ;; Top-level headlines have no outline path.
1579 (should-not
1580 (org-test-with-temp-text "* H"
1581 (org-get-outline-path)))
1582 ;; Otherwise, outline path is the path leading to the headline.
1583 (should
1584 (equal '("H")
1585 (org-test-with-temp-text "* H\n** S<point>"
1586 (org-get-outline-path))))
1587 ;; Find path even when point is not on a headline.
1588 (should
1589 (equal '("H")
1590 (org-test-with-temp-text "* H\n** S\nText<point>"
1591 (org-get-outline-path))))
1592 ;; TODO keywords, tags and statistics cookies are ignored.
1593 (should
1594 (equal '("H")
1595 (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
1596 (org-get-outline-path))))
1597 ;; Links are replaced with their description or their path.
1598 (should
1599 (equal '("Org")
1600 (org-test-with-temp-text
1601 "* [[http://orgmode.org][Org]]\n** S<point>"
1602 (org-get-outline-path))))
1603 (should
1604 (equal '("http://orgmode.org")
1605 (org-test-with-temp-text
1606 "* [[http://orgmode.org]]\n** S<point>"
1607 (org-get-outline-path))))
1608 ;; When WITH-SELF is non-nil, include current heading.
1609 (should
1610 (equal '("H")
1611 (org-test-with-temp-text "* H"
1612 (org-get-outline-path t))))
1613 (should
1614 (equal '("H" "S")
1615 (org-test-with-temp-text "* H\n** S\nText<point>"
1616 (org-get-outline-path t))))
1617 ;; Using cache is transparent to the user.
1618 (should
1619 (equal '("H")
1620 (org-test-with-temp-text "* H\n** S<point>"
1621 (setq org-outline-path-cache nil)
1622 (org-get-outline-path nil t))))
1623 ;; Do not corrupt cache when finding outline path in distant part of
1624 ;; the buffer.
1625 (should
1626 (equal '("H2")
1627 (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
1628 (setq org-outline-path-cache nil)
1629 (org-get-outline-path nil t)
1630 (search-forward "S2")
1631 (org-get-outline-path nil t))))
1632 ;; Do not choke on empty headlines.
1633 (should
1634 (org-test-with-temp-text "* H\n** <point>"
1635 (org-get-outline-path)))
1636 (should
1637 (org-test-with-temp-text "* \n** H<point>"
1638 (org-get-outline-path))))
1640 (ert-deftest test-org/format-outline-path ()
1641 "Test `org-format-outline-path' specifications."
1642 (should
1643 (string= (org-format-outline-path (list "one" "two" "three"))
1644 "one/two/three"))
1645 ;; Empty path.
1646 (should
1647 (string= (org-format-outline-path '())
1648 ""))
1649 (should
1650 (string= (org-format-outline-path '(nil))
1651 ""))
1652 ;; Empty path and prefix.
1653 (should
1654 (string= (org-format-outline-path '() nil ">>")
1655 ">>"))
1656 ;; Trailing whitespace in headings.
1657 (should
1658 (string= (org-format-outline-path (list "one\t" "tw o " "three "))
1659 "one/tw o/three"))
1660 ;; Non-default prefix and separators.
1661 (should
1662 (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
1663 ">>|one|two|three"))
1664 ;; Truncate.
1665 (should
1666 (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
1667 "one/two/.."))
1668 ;; Give a very narrow width.
1669 (should
1670 (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
1671 "on"))
1672 ;; Give a prefix that extends beyond the width.
1673 (should
1674 (string= (org-format-outline-path (list "one" "two" "three" "four") 10
1675 ">>>>>>>>>>")
1676 ">>>>>>>>..")))
1678 (ert-deftest test-org/map-entries ()
1679 "Test `org-map-entries' specifications."
1680 ;; Full match.
1681 (should
1682 (equal '(1 11)
1683 (org-test-with-temp-text "* Level 1\n** Level 2"
1684 (org-map-entries #'point))))
1685 ;; Level match.
1686 (should
1687 (equal '(1)
1688 (org-test-with-temp-text "* Level 1\n** Level 2"
1689 (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
1690 (should
1691 (equal '(11)
1692 (org-test-with-temp-text "* Level 1\n** Level 2"
1693 (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
1694 ;; Tag match.
1695 (should
1696 (equal '(11)
1697 (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
1698 (org-map-entries #'point "yes"))))
1699 (should
1700 (equal '(14)
1701 (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
1702 (org-map-entries #'point "+yes-a"))))
1703 (should
1704 (equal '(11 23)
1705 (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
1706 (org-map-entries #'point "{yes?}"))))
1707 ;; Priority match.
1708 (should
1709 (equal '(1)
1710 (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
1711 (org-map-entries #'point "PRIORITY=\"A\""))))
1712 ;; Date match.
1713 (should
1714 (equal '(36)
1715 (org-test-with-temp-text "
1716 * H1
1717 SCHEDULED: <2012-03-29 thu.>
1718 * H2
1719 SCHEDULED: <2014-03-04 tue.>"
1720 (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
1721 (should
1722 (equal '(2)
1723 (org-test-with-temp-text "
1724 * H1
1725 SCHEDULED: <2012-03-29 thu.>
1726 * H2
1727 SCHEDULED: <2014-03-04 tue.>"
1728 (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
1729 ;; Regular property match.
1730 (should
1731 (equal '(2)
1732 (org-test-with-temp-text "
1733 * H1
1734 :PROPERTIES:
1735 :TEST: 1
1736 :END:
1737 * H2
1738 :PROPERTIES:
1739 :TEST: 2
1740 :END:"
1741 (org-map-entries #'point "TEST=1"))))
1742 ;; Multiple criteria.
1743 (should
1744 (equal '(23)
1745 (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
1746 (let (org-odd-levels-only
1747 (org-use-tag-inheritance nil))
1748 (org-map-entries #'point "yes+LEVEL=1")))))
1749 ;; "or" criteria.
1750 (should
1751 (equal '(12 24)
1752 (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
1753 (let (org-odd-levels-only)
1754 (org-map-entries #'point "LEVEL=2|no")))))
1755 (should
1756 (equal '(1 12)
1757 (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
1758 (let (org-odd-levels-only)
1759 (org-map-entries #'point "yes|no")))))
1760 ;; "and" criteria.
1761 (should
1762 (equal '(22)
1763 (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
1764 (let (org-odd-levels-only)
1765 (org-map-entries #'point "yes&no"))))))
1767 (ert-deftest test-org/edit-headline ()
1768 "Test `org-edit-headline' specifications."
1769 (should
1770 (equal "* B"
1771 (org-test-with-temp-text "* A"
1772 (org-edit-headline "B")
1773 (buffer-string))))
1774 ;; Handle empty headings.
1775 (should
1776 (equal "* "
1777 (org-test-with-temp-text "* A"
1778 (org-edit-headline "")
1779 (buffer-string))))
1780 (should
1781 (equal "* A"
1782 (org-test-with-temp-text "* "
1783 (org-edit-headline "A")
1784 (buffer-string))))
1785 ;; Handle TODO keywords and priority cookies.
1786 (should
1787 (equal "* TODO B"
1788 (org-test-with-temp-text "* TODO A"
1789 (org-edit-headline "B")
1790 (buffer-string))))
1791 (should
1792 (equal "* [#A] B"
1793 (org-test-with-temp-text "* [#A] A"
1794 (org-edit-headline "B")
1795 (buffer-string))))
1796 (should
1797 (equal "* TODO [#A] B"
1798 (org-test-with-temp-text "* TODO [#A] A"
1799 (org-edit-headline "B")
1800 (buffer-string))))
1801 ;; Handle tags.
1802 (equal "* B :tag:"
1803 (org-test-with-temp-text "* A :tag:"
1804 (let ((org-tags-column 4)) (org-edit-headline "B"))
1805 (buffer-string))))
1809 ;;; Keywords
1811 (ert-deftest test-org/set-regexps-and-options ()
1812 "Test `org-set-regexps-and-options' specifications."
1813 ;; TAGS keyword.
1814 (should
1815 (equal '(("A"))
1816 (let ((org-tag-alist '(("A")))
1817 (org-tag-persistent-alist nil))
1818 (org-test-with-temp-text ""
1819 (org-mode-restart)
1820 org-current-tag-alist))))
1821 (should
1822 (equal '(("B"))
1823 (let ((org-tag-alist '(("A")))
1824 (org-tag-persistent-alist nil))
1825 (org-test-with-temp-text "#+TAGS: B"
1826 (org-mode-restart)
1827 org-current-tag-alist))))
1828 (should
1829 (equal '(("C") ("B"))
1830 (let ((org-tag-alist '(("A")))
1831 (org-tag-persistent-alist '(("C"))))
1832 (org-test-with-temp-text "#+TAGS: B"
1833 (org-mode-restart)
1834 org-current-tag-alist))))
1835 (should
1836 (equal '(("B"))
1837 (let ((org-tag-alist '(("A")))
1838 (org-tag-persistent-alist '(("C"))))
1839 (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
1840 (org-mode-restart)
1841 org-current-tag-alist))))
1842 (should
1843 (equal '(("A" . ?a) ("B") ("C"))
1844 (let ((org-tag-persistant-alist nil))
1845 (org-test-with-temp-text "#+TAGS: A(a) B C"
1846 (org-mode-restart)
1847 org-current-tag-alist))))
1848 (should
1849 (equal '(("A") (:newline) ("B"))
1850 (let ((org-tag-persistent-alist nil))
1851 (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
1852 (org-mode-restart)
1853 org-current-tag-alist))))
1854 (should
1855 (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
1856 (let ((org-tag-persistent-alist nil))
1857 (org-test-with-temp-text "#+TAGS: { A B } C"
1858 (org-mode-restart)
1859 org-current-tag-alist))))
1860 (should
1861 (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
1862 (let ((org-tag-persistent-alist nil))
1863 (org-test-with-temp-text "#+TAGS: { A : B C }"
1864 (org-mode-restart)
1865 org-current-tag-alist))))
1866 (should
1867 (equal '(("A" "B" "C"))
1868 (let ((org-tag-persistent-alist nil))
1869 (org-test-with-temp-text "#+TAGS: { A : B C }"
1870 (org-mode-restart)
1871 org-tag-groups-alist))))
1872 (should
1873 (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
1874 (let ((org-tag-persistent-alist nil))
1875 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1876 (org-mode-restart)
1877 org-current-tag-alist))))
1878 (should
1879 (equal '(("A" "B" "C"))
1880 (let ((org-tag-persistent-alist nil))
1881 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1882 (org-mode-restart)
1883 org-tag-groups-alist))))
1884 ;; FILETAGS keyword.
1885 (should
1886 (equal '("A" "B" "C")
1887 (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
1888 (org-mode-restart)
1889 org-file-tags)))
1890 ;; PROPERTY keyword. Property names are case-insensitive.
1891 (should
1892 (equal "foo=1"
1893 (org-test-with-temp-text "#+PROPERTY: var foo=1"
1894 (org-mode-restart)
1895 (cdr (assoc "var" org-file-properties)))))
1896 (should
1897 (equal
1898 "foo=1 bar=2"
1899 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
1900 (org-mode-restart)
1901 (cdr (assoc "var" org-file-properties)))))
1902 (should
1903 (equal
1904 "foo=1 bar=2"
1905 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
1906 (org-mode-restart)
1907 (cdr (assoc "var" org-file-properties)))))
1908 ;; ARCHIVE keyword.
1909 (should
1910 (equal "%s_done::"
1911 (org-test-with-temp-text "#+ARCHIVE: %s_done::"
1912 (org-mode-restart)
1913 org-archive-location)))
1914 ;; CATEGORY keyword.
1915 (should
1916 (eq 'test
1917 (org-test-with-temp-text "#+CATEGORY: test"
1918 (org-mode-restart)
1919 org-category)))
1920 (should
1921 (equal "test"
1922 (org-test-with-temp-text "#+CATEGORY: test"
1923 (org-mode-restart)
1924 (cdr (assoc "CATEGORY" org-file-properties)))))
1925 ;; COLUMNS keyword.
1926 (should
1927 (equal "%25ITEM %TAGS %PRIORITY %TODO"
1928 (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
1929 (org-mode-restart)
1930 org-columns-default-format)))
1931 ;; CONSTANTS keyword. Constants names are case sensitive.
1932 (should
1933 (equal '("299792458." "3.14")
1934 (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
1935 (org-mode-restart)
1936 (mapcar
1937 (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
1938 '("c" "pi")))))
1939 (should
1940 (equal "3.14"
1941 (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
1942 (org-mode-restart)
1943 (cdr (assoc "pi" org-table-formula-constants-local)))))
1944 (should
1945 (equal "22/7"
1946 (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
1947 (org-mode-restart)
1948 (cdr (assoc "PI" org-table-formula-constants-local)))))
1949 ;; LINK keyword.
1950 (should
1951 (equal
1952 '("url1" "url2")
1953 (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
1954 (org-mode-restart)
1955 (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
1956 '("a" "b")))))
1957 ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
1958 (should
1959 (equal
1960 '(?X ?Z ?Y)
1961 (org-test-with-temp-text "#+PRIORITIES: X Z Y"
1962 (org-mode-restart)
1963 (list org-highest-priority org-lowest-priority org-default-priority))))
1964 (should
1965 (equal
1966 '(?A ?C ?B)
1967 (org-test-with-temp-text "#+PRIORITIES: X Z"
1968 (org-mode-restart)
1969 (list org-highest-priority org-lowest-priority org-default-priority))))
1970 ;; STARTUP keyword.
1971 (should
1972 (equal '(t t)
1973 (org-test-with-temp-text "#+STARTUP: fold odd"
1974 (org-mode-restart)
1975 (list org-startup-folded org-odd-levels-only))))
1976 ;; TODO keywords.
1977 (should
1978 (equal '(("A" "B") ("C"))
1979 (org-test-with-temp-text "#+TODO: A B | C"
1980 (org-mode-restart)
1981 (list org-not-done-keywords org-done-keywords))))
1982 (should
1983 (equal '(("A" "C") ("B" "D"))
1984 (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
1985 (org-mode-restart)
1986 (list org-not-done-keywords org-done-keywords))))
1987 (should
1988 (equal '(("A" "B") ("C"))
1989 (org-test-with-temp-text "#+TYP_TODO: A B | C"
1990 (org-mode-restart)
1991 (list org-not-done-keywords org-done-keywords))))
1992 (should
1993 (equal '((:startgroup) ("A" . ?a) (:endgroup))
1994 (org-test-with-temp-text "#+TODO: A(a)"
1995 (org-mode-restart)
1996 org-todo-key-alist)))
1997 (should
1998 (equal '(("D" note nil) ("C" time nil) ("B" note time))
1999 (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
2000 (org-mode-restart)
2001 org-todo-log-states)))
2002 ;; Enter SETUPFILE keyword.
2003 (should
2004 (equal "1"
2005 (org-test-with-temp-text
2006 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
2007 (org-mode-restart)
2008 (cdr (assoc "a" org-file-properties))))))
2012 ;;; Links
2014 ;;;; Coderefs
2016 (ert-deftest test-org/coderef ()
2017 "Test coderef links specifications."
2018 (should
2019 (org-test-with-temp-text "
2020 #+BEGIN_SRC emacs-lisp
2021 \(+ 1 1) (ref:sc)
2022 #+END_SRC
2023 \[[(sc)<point>]]"
2024 (org-open-at-point)
2025 (looking-at "(ref:sc)")))
2026 ;; Find coderef even with alternate label format.
2027 (should
2028 (org-test-with-temp-text "
2029 #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
2030 \(+ 1 1) {ref:sc}
2031 #+END_SRC
2032 \[[(sc)<point>]]"
2033 (org-open-at-point)
2034 (looking-at "{ref:sc}"))))
2036 ;;;; Custom ID
2038 (ert-deftest test-org/custom-id ()
2039 "Test custom ID links specifications."
2040 (should
2041 (org-test-with-temp-text
2042 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
2043 (org-open-at-point)
2044 (looking-at-p "\\* H1")))
2045 ;; Throw an error on false positives.
2046 (should-error
2047 (org-test-with-temp-text
2048 "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
2049 (org-open-at-point)
2050 (looking-at-p "\\* H1"))))
2052 ;;;; Fuzzy Links
2054 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
2055 ;; a named element (#+name: text) and to headlines (* Text).
2057 (ert-deftest test-org/fuzzy-links ()
2058 "Test fuzzy links specifications."
2059 ;; Fuzzy link goes in priority to a matching target.
2060 (should
2061 (org-test-with-temp-text
2062 "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
2063 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
2064 (looking-at "<<Test>>")))
2065 ;; Then fuzzy link points to an element with a given name.
2066 (should
2067 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
2068 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
2069 (looking-at "#\\+NAME: Test")))
2070 ;; A target still lead to a matching headline otherwise.
2071 (should
2072 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
2073 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
2074 (looking-at "\\* Head2")))
2075 ;; With a leading star in link, enforce heading match.
2076 (should
2077 (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
2078 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
2079 (looking-at "\\* Test")))
2080 ;; With a leading star in link, enforce exact heading match, even
2081 ;; with `org-link-search-must-match-exact-headline' set to nil.
2082 (should-error
2083 (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
2084 (let ((org-link-search-must-match-exact-headline nil))
2085 (org-open-at-point))))
2086 ;; Handle non-nil `org-link-search-must-match-exact-headline'.
2087 (should
2088 (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
2089 (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
2090 (looking-at "\\* Test")))
2091 (should
2092 (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
2093 (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
2094 (looking-at "\\* Test")))
2095 ;; Heading match should not care about spaces, cookies, TODO
2096 ;; keywords, priorities, and tags.
2097 (should
2098 (let ((first-line
2099 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
2100 (org-test-with-temp-text
2101 (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
2102 (let ((org-link-search-must-match-exact-headline nil)
2103 (org-todo-regexp "TODO"))
2104 (org-open-at-point))
2105 (looking-at (regexp-quote first-line)))))
2106 ;; Heading match should still be exact.
2107 (should-error
2108 (let ((first-line
2109 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
2110 (org-test-with-temp-text
2111 (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
2112 (let ((org-link-search-must-match-exact-headline nil)
2113 (org-todo-regexp "TODO"))
2114 (org-open-at-point)))))
2115 ;; Heading match ignores COMMENT keyword.
2116 (should
2117 (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
2118 (org-open-at-point)
2119 (looking-at "\\* COMMENT Test")))
2120 ;; Correctly un-hexify fuzzy links.
2121 (should
2122 (org-test-with-temp-text "* With space\n[[*With%20space][With space<point>]]"
2123 (org-open-at-point)
2124 (bobp))))
2126 ;;;; Link Escaping
2128 (ert-deftest test-org/org-link-escape-ascii-character ()
2129 "Escape an ascii character."
2130 (should
2131 (string=
2132 "%5B"
2133 (org-link-escape "["))))
2135 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
2136 "Escape an ascii control character."
2137 (should
2138 (string=
2139 "%09"
2140 (org-link-escape "\t"))))
2142 (ert-deftest test-org/org-link-escape-multibyte-character ()
2143 "Escape an unicode multibyte character."
2144 (should
2145 (string=
2146 "%E2%82%AC"
2147 (org-link-escape "€"))))
2149 (ert-deftest test-org/org-link-escape-custom-table ()
2150 "Escape string with custom character table."
2151 (should
2152 (string=
2153 "Foo%3A%42ar%0A"
2154 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
2156 (ert-deftest test-org/org-link-escape-custom-table-merge ()
2157 "Escape string with custom table merged with default table."
2158 (should
2159 (string=
2160 "%5BF%6F%6F%3A%42ar%0A%5D"
2161 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
2163 (ert-deftest test-org/org-link-unescape-ascii-character ()
2164 "Unescape an ascii character."
2165 (should
2166 (string=
2168 (org-link-unescape "%5B"))))
2170 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
2171 "Unescpae an ascii control character."
2172 (should
2173 (string=
2174 "\n"
2175 (org-link-unescape "%0A"))))
2177 (ert-deftest test-org/org-link-unescape-multibyte-character ()
2178 "Unescape unicode multibyte character."
2179 (should
2180 (string=
2181 "€"
2182 (org-link-unescape "%E2%82%AC"))))
2184 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
2185 "Unescape old style percent escaped character."
2186 (should
2187 (string=
2188 "àâçèéêîôùû"
2189 (decode-coding-string
2190 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
2192 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
2193 "Escape and unescape a URL that includes an escaped char.
2194 http://article.gmane.org/gmane.emacs.orgmode/21459/"
2195 (should
2196 (string=
2197 "http://some.host.com/form?&id=blah%2Bblah25"
2198 (org-link-unescape
2199 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
2201 ;;;; Open at point
2203 (ert-deftest test-org/open-at-point-in-keyword ()
2204 "Does `org-open-at-point' open link in a keyword line?"
2205 (should
2206 (org-test-with-temp-text
2207 "<<top>>\n#+KEYWORD: <point>[[top]]"
2208 (org-open-at-point) t)))
2210 (ert-deftest test-org/open-at-point-in-property ()
2211 "Does `org-open-at-point' open link in property drawer?"
2212 (should
2213 (org-test-with-temp-text
2214 "* Headline
2215 :PROPERTIES:
2216 :URL: <point>[[*Headline]]
2217 :END:"
2218 (org-open-at-point) t)))
2220 (ert-deftest test-org/open-at-point-in-comment ()
2221 "Does `org-open-at-point' open link in a commented line?"
2222 (should
2223 (org-test-with-temp-text
2224 "<<top>>\n# <point>[[top]]"
2225 (org-open-at-point) t)))
2227 (ert-deftest test-org/open-at-point/inline-image ()
2228 "Test `org-open-at-point' on nested links."
2229 (should
2230 (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
2231 (org-open-at-point)
2232 (bobp))))
2234 (ert-deftest test-org/open-at-point/radio-target ()
2235 "Test `org-open-at-point' on radio targets."
2236 (should
2237 (org-test-with-temp-text "<<<target>>> <point>target"
2238 (org-update-radio-target-regexp)
2239 (org-open-at-point)
2240 (eq (org-element-type (org-element-context)) 'radio-target))))
2242 ;;;; Stored links
2244 (ert-deftest test-org/store-link ()
2245 "Test `org-store-link' specifications."
2246 ;; On a headline, link to that headline. Use heading as the
2247 ;; description of the link.
2248 (should
2249 (let (org-store-link-props org-stored-links)
2250 (org-test-with-temp-text-in-file "* H1"
2251 (let ((file (buffer-file-name)))
2252 (equal (format "[[file:%s::*H1][H1]]" file)
2253 (org-store-link nil))))))
2254 ;; On a headline, remove any link from description.
2255 (should
2256 (let (org-store-link-props org-stored-links)
2257 (org-test-with-temp-text-in-file "* [[#l][d]]"
2258 (let ((file (buffer-file-name)))
2259 (equal (format "[[file:%s::*%s][d]]"
2260 file
2261 (org-link-escape "[[#l][d]]"))
2262 (org-store-link nil))))))
2263 (should
2264 (let (org-store-link-props org-stored-links)
2265 (org-test-with-temp-text-in-file "* [[l]]"
2266 (let ((file (buffer-file-name)))
2267 (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
2268 (org-store-link nil))))))
2269 (should
2270 (let (org-store-link-props org-stored-links)
2271 (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
2272 (let ((file (buffer-file-name)))
2273 (equal (format "[[file:%s::*%s][d1 d2]]"
2274 file
2275 (org-link-escape "[[l1][d1]] [[l2][d2]]"))
2276 (org-store-link nil))))))
2277 ;; On a named element, link to that element.
2278 (should
2279 (let (org-store-link-props org-stored-links)
2280 (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
2281 (let ((file (buffer-file-name)))
2282 (equal (format "[[file:%s::foo][foo]]" file)
2283 (org-store-link nil)))))))
2286 ;;; Node Properties
2288 (ert-deftest test-org/accumulated-properties-in-drawers ()
2289 "Ensure properties accumulate in subtree drawers."
2290 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
2291 (org-babel-next-src-block)
2292 (should (equal '(2 1) (org-babel-execute-src-block)))))
2294 (ert-deftest test-org/custom-properties ()
2295 "Test custom properties specifications."
2296 ;; Standard test.
2297 (should
2298 (let ((org-custom-properties '("FOO")))
2299 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
2300 (org-toggle-custom-properties-visibility)
2301 (org-invisible-p2))))
2302 ;; Properties are case-insensitive.
2303 (should
2304 (let ((org-custom-properties '("FOO")))
2305 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
2306 (org-toggle-custom-properties-visibility)
2307 (org-invisible-p2))))
2308 (should
2309 (let ((org-custom-properties '("foo")))
2310 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
2311 (org-toggle-custom-properties-visibility)
2312 (org-invisible-p2))))
2313 ;; Multiple custom properties in the same drawer.
2314 (should
2315 (let ((org-custom-properties '("FOO" "BAR")))
2316 (org-test-with-temp-text
2317 "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
2318 (org-toggle-custom-properties-visibility)
2319 (and (org-invisible-p2)
2320 (not (progn (forward-line) (org-invisible-p2)))
2321 (progn (forward-line) (org-invisible-p2))))))
2322 ;; Hide custom properties with an empty value.
2323 (should
2324 (let ((org-custom-properties '("FOO")))
2325 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
2326 (org-toggle-custom-properties-visibility)
2327 (org-invisible-p2))))
2328 ;; Do not hide fake properties.
2329 (should-not
2330 (let ((org-custom-properties '("FOO")))
2331 (org-test-with-temp-text ":FOO: val\n"
2332 (org-toggle-custom-properties-visibility)
2333 (org-invisible-p2))))
2334 (should-not
2335 (let ((org-custom-properties '("A")))
2336 (org-test-with-temp-text
2337 "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
2338 (org-toggle-custom-properties-visibility)
2339 (org-invisible-p2)))))
2343 ;;; Mark Region
2345 (ert-deftest test-org/mark-subtree ()
2346 "Test `org-mark-subtree' specifications."
2347 ;; Error when point is before first headline.
2348 (should-error
2349 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
2350 (progn (transient-mark-mode 1)
2351 (org-mark-subtree))))
2352 ;; Without argument, mark current subtree.
2353 (should
2354 (equal
2355 '(12 32)
2356 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
2357 (progn (transient-mark-mode 1)
2358 (forward-line 2)
2359 (org-mark-subtree)
2360 (list (region-beginning) (region-end))))))
2361 ;; With an argument, move ARG up.
2362 (should
2363 (equal
2364 '(1 32)
2365 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
2366 (progn (transient-mark-mode 1)
2367 (forward-line 2)
2368 (org-mark-subtree 1)
2369 (list (region-beginning) (region-end))))))
2370 ;; Do not get fooled by inlinetasks.
2371 (when (featurep 'org-inlinetask)
2372 (should
2373 (= 1
2374 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
2375 (progn (transient-mark-mode 1)
2376 (forward-line 1)
2377 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
2378 (region-beginning)))))))
2382 ;;; Miscellaneous
2384 (ert-deftest test-org/in-regexp ()
2385 "Test `org-in-regexp' specifications."
2386 ;; Standard tests.
2387 (should
2388 (org-test-with-temp-text "xx ab<point>c xx"
2389 (org-in-regexp "abc")))
2390 (should-not
2391 (org-test-with-temp-text "xx abc <point>xx"
2392 (org-in-regexp "abc")))
2393 ;; Return non-nil even with multiple matching regexps in the same
2394 ;; line.
2395 (should
2396 (org-test-with-temp-text "abc xx ab<point>c xx"
2397 (org-in-regexp "abc")))
2398 ;; With optional argument NLINES, check extra lines around point.
2399 (should-not
2400 (org-test-with-temp-text "A\nB<point>\nC"
2401 (org-in-regexp "A\nB\nC")))
2402 (should
2403 (org-test-with-temp-text "A\nB<point>\nC"
2404 (org-in-regexp "A\nB\nC" 1)))
2405 (should-not
2406 (org-test-with-temp-text "A\nB\nC<point>"
2407 (org-in-regexp "A\nB\nC" 1)))
2408 ;; When optional argument VISUALLY is non-nil, return nil if at
2409 ;; regexp boundaries.
2410 (should
2411 (org-test-with-temp-text "xx abc<point> xx"
2412 (org-in-regexp "abc")))
2413 (should-not
2414 (org-test-with-temp-text "xx abc<point> xx"
2415 (org-in-regexp "abc" nil t))))
2418 ;;; Navigation
2420 (ert-deftest test-org/end-of-meta-data ()
2421 "Test `org-end-of-meta-data' specifications."
2422 ;; Skip planning line.
2423 (should
2424 (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
2425 (org-end-of-meta-data)
2426 (eobp)))
2427 ;; Skip properties drawer.
2428 (should
2429 (org-test-with-temp-text
2430 "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
2431 (org-end-of-meta-data)
2432 (eobp)))
2433 ;; Skip both.
2434 (should
2435 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
2436 (org-end-of-meta-data)
2437 (eobp)))
2438 ;; Nothing to skip, go to first line.
2439 (should
2440 (org-test-with-temp-text "* Headline\nContents"
2441 (org-end-of-meta-data)
2442 (looking-at "Contents")))
2443 ;; With option argument, skip empty lines, regular drawers and
2444 ;; clocking lines.
2445 (should
2446 (org-test-with-temp-text "* Headline\n\nContents"
2447 (org-end-of-meta-data t)
2448 (looking-at "Contents")))
2449 (should
2450 (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
2451 (org-end-of-meta-data t)
2452 (looking-at "Contents")))
2453 (should
2454 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
2455 (org-end-of-meta-data t)
2456 (looking-at "Contents")))
2457 ;; Special case: do not skip incomplete drawers.
2458 (should
2459 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
2460 (org-end-of-meta-data t)
2461 (looking-at ":LOGBOOK:")))
2462 ;; Special case: Be careful about consecutive headlines.
2463 (should-not
2464 (org-test-with-temp-text "* H1\n*H2\nContents"
2465 (org-end-of-meta-data t)
2466 (looking-at "Contents"))))
2468 (ert-deftest test-org/beginning-of-line ()
2469 "Test `org-beginning-of-line' specifications."
2470 ;; Standard test.
2471 (should
2472 (org-test-with-temp-text "Some text\nSome other text<point>"
2473 (progn (org-beginning-of-line) (bolp))))
2474 ;; Standard test with `visual-line-mode'.
2475 (should-not
2476 (org-test-with-temp-text "A <point>long line of text\nSome other text"
2477 (progn (visual-line-mode)
2478 (dotimes (i 1000) (insert "very "))
2479 (org-beginning-of-line)
2480 (bolp))))
2481 ;; At an headline with special movement.
2482 (should
2483 (org-test-with-temp-text "* TODO Headline<point>"
2484 (let ((org-special-ctrl-a/e t))
2485 (and (progn (org-beginning-of-line) (looking-at "Headline"))
2486 (progn (org-beginning-of-line) (bolp))
2487 (progn (org-beginning-of-line) (looking-at "Headline"))))))
2488 ;; Leave point before invisible characters at column 0.
2489 (should
2490 (org-test-with-temp-text "[[http://orgmode.org]]<point>"
2491 (let ((org-special-ctrl-a/e nil))
2492 (org-beginning-of-line)
2493 (bolp))))
2494 (should
2495 (org-test-with-temp-text "[[http://orgmode.org]]<point>"
2496 (let ((org-special-ctrl-a/e t))
2497 (org-beginning-of-line)
2498 (bolp))))
2499 ;; Special case: Do not error when the buffer contains only a single
2500 ;; asterisk.
2501 (should
2502 (org-test-with-temp-text "*<point>"
2503 (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
2504 (should
2505 (org-test-with-temp-text "*<point>"
2506 (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
2508 (ert-deftest test-org/end-of-line ()
2509 "Test `org-end-of-line' specifications."
2510 ;; Standard test.
2511 (should
2512 (org-test-with-temp-text "Some text\nSome other text"
2513 (progn (org-end-of-line) (eolp))))
2514 ;; With `visual-line-mode' active, move to end of visible line.
2515 ;; However, never go past ellipsis.
2516 (should-not
2517 (org-test-with-temp-text "A <point>long line of text\nSome other text"
2518 (visual-line-mode)
2519 (dotimes (i 1000) (insert "very "))
2520 (goto-char (point-min))
2521 (org-end-of-line)
2522 (eolp)))
2523 (should-not
2524 (org-test-with-temp-text "* A short headline\nSome contents"
2525 (visual-line-mode)
2526 (org-overview)
2527 (org-end-of-line)
2528 (eobp)))
2529 ;; In a wide headline, with `visual-line-mode', prefer going to end
2530 ;; of visible line if tags, or end of line, are farther.
2531 (should-not
2532 (org-test-with-temp-text "* A <point>long headline"
2533 (visual-line-mode)
2534 (dotimes (i 1000) (insert "very "))
2535 (goto-char (point-min))
2536 (org-end-of-line)
2537 (eolp)))
2538 (should-not
2539 (org-test-with-temp-text "* A <point>long headline :tag:"
2540 (visual-line-mode)
2541 (dotimes (i 1000) (insert "very "))
2542 (goto-char (point-min))
2543 (org-end-of-line)
2544 (eolp)))
2545 ;; At an headline without special movement, go to end of line.
2546 ;; However, never go past ellipsis.
2547 (should
2548 (org-test-with-temp-text "* Headline2b :tag:\n"
2549 (let ((org-special-ctrl-a/e nil))
2550 (and (progn (org-end-of-line) (eolp))
2551 (progn (org-end-of-line) (eolp))))))
2552 (should
2553 (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
2554 (org-overview)
2555 (let ((org-special-ctrl-a/e nil))
2556 (org-end-of-line)
2557 (= 1 (line-beginning-position)))))
2558 ;; At an headline with special movement, first move before tags,
2559 ;; then at the end of line, rinse, repeat. However, never go past
2560 ;; ellipsis.
2561 (should
2562 (org-test-with-temp-text "* Headline1 :tag:\n"
2563 (let ((org-special-ctrl-a/e t))
2564 (and (progn (org-end-of-line) (looking-at " :tag:"))
2565 (progn (org-end-of-line) (eolp))
2566 (progn (org-end-of-line) (looking-at " :tag:"))))))
2567 (should
2568 (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
2569 (org-overview)
2570 (let ((org-special-ctrl-a/e t))
2571 (org-end-of-line)
2572 (org-end-of-line)
2573 (= 1 (line-beginning-position)))))
2574 ;; At an headline, with reversed movement, first go to end of line,
2575 ;; then before tags. However, never go past ellipsis.
2576 (should
2577 (org-test-with-temp-text "* Headline3 :tag:\n"
2578 (let ((org-special-ctrl-a/e 'reversed)
2579 (this-command last-command))
2580 (and (progn (org-end-of-line) (eolp))
2581 (progn (org-end-of-line) (looking-at " :tag:"))))))
2582 (should
2583 (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
2584 (org-overview)
2585 (let ((org-special-ctrl-a/e 'reversed))
2586 (org-end-of-line)
2587 (= 1 (line-beginning-position)))))
2588 ;; At a block without hidden contents.
2589 (should
2590 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
2591 (progn (org-end-of-line) (eolp))))
2592 ;; At a block with hidden contents.
2593 (should-not
2594 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
2595 (let ((org-special-ctrl-a/e t))
2596 (org-hide-block-toggle)
2597 (org-end-of-line)
2598 (eobp))))
2599 ;; Get past invisible characters at the end of line.
2600 (should
2601 (org-test-with-temp-text "[[http://orgmode.org]]"
2602 (org-end-of-line)
2603 (eolp))))
2605 (ert-deftest test-org/open-line ()
2606 "Test `org-open-line' specifications."
2607 ;; Call `open-line' outside of tables.
2608 (should
2609 (equal "\nText"
2610 (org-test-with-temp-text "Text"
2611 (org-open-line 1)
2612 (buffer-string))))
2613 ;; At a table, create a row above.
2614 (should
2615 (equal "\n| |\n| a |"
2616 (org-test-with-temp-text "\n<point>| a |"
2617 (org-open-line 1)
2618 (buffer-string))))
2619 ;; At the very first character of the buffer, also call `open-line'.
2620 (should
2621 (equal "\n| a |"
2622 (org-test-with-temp-text "| a |"
2623 (org-open-line 1)
2624 (buffer-string))))
2625 ;; Narrowing does not count.
2626 (should
2627 (equal "Text\n| |\n| a |"
2628 (org-test-with-temp-text "Text\n<point>| a |"
2629 (narrow-to-region (point) (point-max))
2630 (org-open-line 1)
2631 (widen)
2632 (buffer-string)))))
2634 (ert-deftest test-org/forward-sentence ()
2635 "Test `org-forward-sentence' specifications."
2636 ;; At the end of a table cell, move to the end of the next one.
2637 (should
2638 (org-test-with-temp-text "| a<point> | b |"
2639 (org-forward-sentence)
2640 (looking-at " |$")))
2641 ;; Elsewhere in a cell, move to its end.
2642 (should
2643 (org-test-with-temp-text "| a<point>c | b |"
2644 (org-forward-sentence)
2645 (looking-at " | b |$")))
2646 ;; Otherwise, simply call `forward-sentence'.
2647 (should
2648 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
2649 (org-forward-sentence)
2650 (looking-at " Sentence 2.")))
2651 (should
2652 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
2653 (org-forward-sentence)
2654 (org-forward-sentence)
2655 (eobp)))
2656 ;; At the end of an element, jump to the next one, without stopping
2657 ;; on blank lines in-between.
2658 (should
2659 (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
2660 (org-forward-sentence)
2661 (eobp))))
2663 (ert-deftest test-org/backward-sentence ()
2664 "Test `org-backward-sentence' specifications."
2665 ;; At the beginning of a table cell, move to the beginning of the
2666 ;; previous one.
2667 (should
2668 (org-test-with-temp-text "| a | <point>b |"
2669 (org-backward-sentence)
2670 (looking-at "a | b |$")))
2671 ;; Elsewhere in a cell, move to its beginning.
2672 (should
2673 (org-test-with-temp-text "| a | b<point>c |"
2674 (org-backward-sentence)
2675 (looking-at "bc |$")))
2676 ;; Otherwise, simply call `backward-sentence'.
2677 (should
2678 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
2679 (org-backward-sentence)
2680 (looking-at "Sentence 2.")))
2681 (should
2682 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
2683 (org-backward-sentence)
2684 (org-backward-sentence)
2685 (bobp)))
2686 ;; Make sure to hit the beginning of a sentence on the same line as
2687 ;; an item.
2688 (should
2689 (org-test-with-temp-text "- Line 1\n line <point>2."
2690 (org-backward-sentence)
2691 (looking-at "Line 1"))))
2693 (ert-deftest test-org/forward-paragraph ()
2694 "Test `org-forward-paragraph' specifications."
2695 ;; At end of buffer, return an error.
2696 (should-error
2697 (org-test-with-temp-text "Paragraph"
2698 (goto-char (point-max))
2699 (org-forward-paragraph)))
2700 ;; Standard test.
2701 (should
2702 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2703 (org-forward-paragraph)
2704 (looking-at "P2")))
2705 ;; Ignore depth.
2706 (should
2707 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
2708 (org-forward-paragraph)
2709 (looking-at "P1")))
2710 ;; Do not enter elements with invisible contents.
2711 (should
2712 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
2713 (org-hide-block-toggle)
2714 (org-forward-paragraph)
2715 (looking-at "P3")))
2716 ;; On an affiliated keyword, jump to the beginning of the element.
2717 (should
2718 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
2719 (org-forward-paragraph)
2720 (looking-at "Para")))
2721 ;; On an item or a footnote definition, move to the second element
2722 ;; inside, if any.
2723 (should
2724 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
2725 (org-forward-paragraph)
2726 (looking-at " Paragraph")))
2727 (should
2728 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
2729 (org-forward-paragraph)
2730 (looking-at "Paragraph")))
2731 ;; On an item, or a footnote definition, when the first line is
2732 ;; empty, move to the first item.
2733 (should
2734 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
2735 (org-forward-paragraph)
2736 (looking-at " Paragraph")))
2737 (should
2738 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
2739 (org-forward-paragraph)
2740 (looking-at "Paragraph")))
2741 ;; On a table (resp. a property drawer) do not move through table
2742 ;; rows (resp. node properties).
2743 (should
2744 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
2745 (org-forward-paragraph)
2746 (looking-at "Paragraph")))
2747 (should
2748 (org-test-with-temp-text
2749 "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
2750 (org-forward-paragraph)
2751 (looking-at "Paragraph")))
2752 ;; On a verse or source block, stop after blank lines.
2753 (should
2754 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
2755 (org-forward-paragraph)
2756 (looking-at "L2")))
2757 (should
2758 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
2759 (org-forward-paragraph)
2760 (looking-at "L2"))))
2762 (ert-deftest test-org/backward-paragraph ()
2763 "Test `org-backward-paragraph' specifications."
2764 ;; Error at beginning of buffer.
2765 (should-error
2766 (org-test-with-temp-text "Paragraph"
2767 (org-backward-paragraph)))
2768 ;; Regular test.
2769 (should
2770 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2771 (goto-char (point-max))
2772 (org-backward-paragraph)
2773 (looking-at "P3")))
2774 (should
2775 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2776 (goto-char (point-max))
2777 (beginning-of-line)
2778 (org-backward-paragraph)
2779 (looking-at "P2")))
2780 ;; Ignore depth.
2781 (should
2782 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
2783 (goto-char (point-max))
2784 (beginning-of-line)
2785 (org-backward-paragraph)
2786 (looking-at "P2")))
2787 ;; Ignore invisible elements.
2788 (should
2789 (org-test-with-temp-text "* H1\n P1\n* H2"
2790 (org-cycle)
2791 (goto-char (point-max))
2792 (beginning-of-line)
2793 (org-backward-paragraph)
2794 (bobp)))
2795 ;; On an affiliated keyword, jump to the first one.
2796 (should
2797 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
2798 (search-forward "c2")
2799 (org-backward-paragraph)
2800 (looking-at "#\\+name")))
2801 ;; On the second element in an item or a footnote definition, jump
2802 ;; to item or the definition.
2803 (should
2804 (org-test-with-temp-text "- line1\n\n line2"
2805 (goto-char (point-max))
2806 (beginning-of-line)
2807 (org-backward-paragraph)
2808 (looking-at "- line1")))
2809 (should
2810 (org-test-with-temp-text "[fn:1] line1\n\n line2"
2811 (goto-char (point-max))
2812 (beginning-of-line)
2813 (org-backward-paragraph)
2814 (looking-at "\\[fn:1\\] line1")))
2815 ;; On a table (resp. a property drawer), ignore table rows
2816 ;; (resp. node properties).
2817 (should
2818 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
2819 (goto-char (point-max))
2820 (beginning-of-line)
2821 (org-backward-paragraph)
2822 (bobp)))
2823 (should
2824 (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
2825 (org-backward-paragraph)
2826 (looking-at ":PROPERTIES:")))
2827 ;; On a source or verse block, stop before blank lines.
2828 (should
2829 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
2830 (search-forward "L3")
2831 (beginning-of-line)
2832 (org-backward-paragraph)
2833 (looking-at "L2")))
2834 (should
2835 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
2836 (search-forward "L3")
2837 (beginning-of-line)
2838 (org-backward-paragraph)
2839 (looking-at "L2"))))
2841 (ert-deftest test-org/forward-element ()
2842 "Test `org-forward-element' specifications."
2843 ;; 1. At EOB: should error.
2844 (org-test-with-temp-text "Some text\n"
2845 (goto-char (point-max))
2846 (should-error (org-forward-element)))
2847 ;; 2. Standard move: expected to ignore blank lines.
2848 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
2849 (org-forward-element)
2850 (should (looking-at (regexp-quote "Second paragraph."))))
2851 ;; 3. Headline tests.
2852 (org-test-with-temp-text "
2853 * Head 1
2854 ** Head 1.1
2855 *** Head 1.1.1
2856 ** Head 1.2"
2857 ;; 3.1. At an headline beginning: move to next headline at the
2858 ;; same level.
2859 (goto-line 3)
2860 (org-forward-element)
2861 (should (looking-at (regexp-quote "** Head 1.2")))
2862 ;; 3.2. At an headline beginning: move to parent headline if no
2863 ;; headline at the same level.
2864 (goto-line 3)
2865 (org-forward-element)
2866 (should (looking-at (regexp-quote "** Head 1.2"))))
2867 ;; 4. Greater element tests.
2868 (org-test-with-temp-text
2869 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
2870 ;; 4.1. At a greater element: expected to skip contents.
2871 (org-forward-element)
2872 (should (looking-at (regexp-quote "Outside.")))
2873 ;; 4.2. At the end of greater element contents: expected to skip
2874 ;; to the end of the greater element.
2875 (goto-line 2)
2876 (org-forward-element)
2877 (should (looking-at (regexp-quote "Outside."))))
2878 ;; 5. List tests.
2879 (org-test-with-temp-text "
2880 - item1
2882 - sub1
2884 - sub2
2886 - sub3
2888 Inner paragraph.
2890 - item2
2892 Outside."
2893 ;; 5.1. At list top point: expected to move to the element after
2894 ;; the list.
2895 (goto-line 2)
2896 (org-forward-element)
2897 (should (looking-at (regexp-quote "Outside.")))
2898 ;; 5.2. Special case: at the first line of a sub-list, but not at
2899 ;; beginning of line, move to next item.
2900 (goto-line 2)
2901 (forward-char)
2902 (org-forward-element)
2903 (should (looking-at "- item2"))
2904 (goto-line 4)
2905 (forward-char)
2906 (org-forward-element)
2907 (should (looking-at " - sub2"))
2908 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
2909 (goto-line 4)
2910 (org-forward-element)
2911 (should (looking-at (regexp-quote " Inner paragraph.")))
2912 ;; 5.4. At sub-list end: expected to move outside the sub-list.
2913 (goto-line 8)
2914 (org-forward-element)
2915 (should (looking-at (regexp-quote " Inner paragraph.")))
2916 ;; 5.5. At an item: expected to move to next item, if any.
2917 (goto-line 6)
2918 (org-forward-element)
2919 (should (looking-at " - sub3"))))
2921 (ert-deftest test-org/backward-element ()
2922 "Test `org-backward-element' specifications."
2923 ;; 1. Should error at BOB.
2924 (org-test-with-temp-text " \nParagraph."
2925 (should-error (org-backward-element)))
2926 ;; 2. Should move at BOB when called on the first element in buffer.
2927 (should
2928 (org-test-with-temp-text "\n#+TITLE: test"
2929 (progn (forward-line)
2930 (org-backward-element)
2931 (bobp))))
2932 ;; 3. Not at the beginning of an element: move at its beginning.
2933 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2934 (goto-line 3)
2935 (end-of-line)
2936 (org-backward-element)
2937 (should (looking-at (regexp-quote "Paragraph2."))))
2938 ;; 4. Headline tests.
2939 (org-test-with-temp-text "
2940 * Head 1
2941 ** Head 1.1
2942 *** Head 1.1.1
2943 ** Head 1.2"
2944 ;; 4.1. At an headline beginning: move to previous headline at the
2945 ;; same level.
2946 (goto-line 5)
2947 (org-backward-element)
2948 (should (looking-at (regexp-quote "** Head 1.1")))
2949 ;; 4.2. At an headline beginning: move to parent headline if no
2950 ;; headline at the same level.
2951 (goto-line 3)
2952 (org-backward-element)
2953 (should (looking-at (regexp-quote "* Head 1")))
2954 ;; 4.3. At the first top-level headline: should error.
2955 (goto-line 2)
2956 (should-error (org-backward-element)))
2957 ;; 5. At beginning of first element inside a greater element:
2958 ;; expected to move to greater element's beginning.
2959 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
2960 (goto-line 3)
2961 (org-backward-element)
2962 (should (looking-at "#\\+BEGIN_CENTER")))
2963 ;; 6. At the beginning of the first element in a section: should
2964 ;; move back to headline, if any.
2965 (should
2966 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
2967 (progn (goto-char (point-max))
2968 (beginning-of-line)
2969 (org-backward-element)
2970 (org-at-heading-p))))
2971 ;; 7. List tests.
2972 (org-test-with-temp-text "
2973 - item1
2975 - sub1
2977 - sub2
2979 - sub3
2981 Inner paragraph.
2983 - item2
2986 Outside."
2987 ;; 7.1. At beginning of sub-list: expected to move to the
2988 ;; paragraph before it.
2989 (goto-line 4)
2990 (org-backward-element)
2991 (should (looking-at "item1"))
2992 ;; 7.2. At an item in a list: expected to move at previous item.
2993 (goto-line 8)
2994 (org-backward-element)
2995 (should (looking-at " - sub2"))
2996 (goto-line 12)
2997 (org-backward-element)
2998 (should (looking-at "- item1"))
2999 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
3000 ;; beginning.
3001 (goto-line 10)
3002 (org-backward-element)
3003 (should (looking-at " - sub1"))
3004 (goto-line 15)
3005 (org-backward-element)
3006 (should (looking-at "- item1"))
3007 ;; 7.4. At blank-lines before list end: expected to move to top
3008 ;; item.
3009 (goto-line 14)
3010 (org-backward-element)
3011 (should (looking-at "- item1"))))
3013 (ert-deftest test-org/up-element ()
3014 "Test `org-up-element' specifications."
3015 ;; 1. At BOB or with no surrounding element: should error.
3016 (org-test-with-temp-text "Paragraph."
3017 (should-error (org-up-element)))
3018 (org-test-with-temp-text "* Head1\n* Head2"
3019 (goto-line 2)
3020 (should-error (org-up-element)))
3021 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
3022 (goto-line 3)
3023 (should-error (org-up-element)))
3024 ;; 2. At an headline: move to parent headline.
3025 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
3026 (goto-line 3)
3027 (org-up-element)
3028 (should (looking-at "\\* Head1")))
3029 ;; 3. Inside a greater element: move to greater element beginning.
3030 (org-test-with-temp-text
3031 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
3032 (goto-line 3)
3033 (org-up-element)
3034 (should (looking-at "#\\+BEGIN_CENTER")))
3035 ;; 4. List tests.
3036 (org-test-with-temp-text "* Top
3037 - item1
3039 - sub1
3041 - sub2
3043 Paragraph within sub2.
3045 - item2"
3046 ;; 4.1. Within an item: move to the item beginning.
3047 (goto-line 8)
3048 (org-up-element)
3049 (should (looking-at " - sub2"))
3050 ;; 4.2. At an item in a sub-list: move to parent item.
3051 (goto-line 4)
3052 (org-up-element)
3053 (should (looking-at "- item1"))
3054 ;; 4.3. At an item in top list: move to beginning of whole list.
3055 (goto-line 10)
3056 (org-up-element)
3057 (should (looking-at "- item1"))
3058 ;; 4.4. Special case. At very top point: should move to parent of
3059 ;; list.
3060 (goto-line 2)
3061 (org-up-element)
3062 (should (looking-at "\\* Top"))))
3064 (ert-deftest test-org/down-element ()
3065 "Test `org-down-element' specifications."
3066 ;; Error when the element hasn't got a recursive type.
3067 (org-test-with-temp-text "Paragraph."
3068 (should-error (org-down-element)))
3069 ;; Error when the element has no contents
3070 (org-test-with-temp-text "* Headline"
3071 (should-error (org-down-element)))
3072 ;; When at a plain-list, move to first item.
3073 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
3074 (goto-line 2)
3075 (org-down-element)
3076 (should (looking-at " - Item 1.1")))
3077 (org-test-with-temp-text "#+NAME: list\n- Item 1"
3078 (org-down-element)
3079 (should (looking-at " Item 1")))
3080 ;; When at a table, move to first row
3081 (org-test-with-temp-text "#+NAME: table\n| a | b |"
3082 (org-down-element)
3083 (should (looking-at " a | b |")))
3084 ;; Otherwise, move inside the greater element.
3085 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
3086 (org-down-element)
3087 (should (looking-at "Paragraph"))))
3089 (ert-deftest test-org/drag-element-backward ()
3090 "Test `org-drag-element-backward' specifications."
3091 ;; Standard test.
3092 (should
3093 (equal
3094 "#+key2: val2\n#+key1: val1\n#+key3: val3"
3095 (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
3096 (org-drag-element-backward)
3097 (buffer-string))))
3098 (should
3099 (equal
3100 "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
3101 (org-test-with-temp-text
3102 "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
3103 (org-drag-element-backward)
3104 (buffer-string))))
3105 ;; Preserve blank lines.
3106 (should
3107 (equal "Paragraph 2\n\n\nPara1\n\nPara3"
3108 (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
3109 (org-drag-element-backward)
3110 (buffer-string))))
3111 ;; Preserve column.
3112 (should
3113 (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
3114 (org-drag-element-backward)
3115 (looking-at-p "2")))
3116 ;; Error when trying to move first element of buffer.
3117 (should-error
3118 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
3119 (org-drag-element-backward)))
3120 ;; Error when trying to swap nested elements.
3121 (should-error
3122 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
3123 (forward-line)
3124 (org-drag-element-backward)))
3125 ;; Error when trying to swap an headline element and a non-headline
3126 ;; element.
3127 (should-error
3128 (org-test-with-temp-text "Test.\n* Head 1"
3129 (forward-line)
3130 (org-drag-element-backward)))
3131 ;; Preserve visibility of elements and their contents.
3132 (should
3133 (equal '((63 . 82) (26 . 48))
3134 (org-test-with-temp-text "
3135 #+BEGIN_CENTER
3136 Text.
3137 #+END_CENTER
3138 - item 1
3139 #+BEGIN_QUOTE
3140 Text.
3141 #+END_QUOTE"
3142 (while (search-forward "BEGIN_" nil t) (org-cycle))
3143 (search-backward "- item 1")
3144 (org-drag-element-backward)
3145 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
3146 (overlays-in (point-min) (point-max)))))))
3148 (ert-deftest test-org/drag-element-forward ()
3149 "Test `org-drag-element-forward' specifications."
3150 ;; 1. Error when trying to move first element of buffer.
3151 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
3152 (goto-line 3)
3153 (should-error (org-drag-element-forward)))
3154 ;; 2. Error when trying to swap nested elements.
3155 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
3156 (forward-line)
3157 (should-error (org-drag-element-forward)))
3158 ;; 3. Error when trying to swap a non-headline element and an
3159 ;; headline.
3160 (org-test-with-temp-text "Test.\n* Head 1"
3161 (should-error (org-drag-element-forward)))
3162 ;; 4. Otherwise, swap elements, preserving column and blank lines
3163 ;; between elements.
3164 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
3165 (search-forward "graph")
3166 (org-drag-element-forward)
3167 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
3168 (should (looking-at " 1")))
3169 ;; 5. Preserve visibility of elements and their contents.
3170 (org-test-with-temp-text "
3171 #+BEGIN_CENTER
3172 Text.
3173 #+END_CENTER
3174 - item 1
3175 #+BEGIN_QUOTE
3176 Text.
3177 #+END_QUOTE"
3178 (while (search-forward "BEGIN_" nil t) (org-cycle))
3179 (search-backward "#+BEGIN_CENTER")
3180 (org-drag-element-forward)
3181 (should
3182 (equal
3183 '((63 . 82) (26 . 48))
3184 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
3185 (overlays-in (point-min) (point-max)))))))
3187 (ert-deftest test-org/next-block ()
3188 "Test `org-next-block' specifications."
3189 ;; Regular test.
3190 (should
3191 (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
3192 (org-next-block 1)
3193 (looking-at "#\\+BEGIN_CENTER")))
3194 ;; Ignore case.
3195 (should
3196 (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
3197 (let ((case-fold-search nil))
3198 (org-next-block 1)
3199 (looking-at "#\\+begin_center"))))
3200 ;; Ignore current line.
3201 (should
3202 (org-test-with-temp-text
3203 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
3204 (org-next-block 1)
3205 (looking-at "#\\+BEGIN_CENTER")))
3206 ;; Throw an error when no block is found.
3207 (should-error
3208 (org-test-with-temp-text "Paragraph"
3209 (org-next-block 1)))
3210 ;; With an argument, skip many blocks at once.
3211 (should
3212 (org-test-with-temp-text
3213 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
3214 (org-next-block 2)
3215 (looking-at "#\\+BEGIN_QUOTE")))
3216 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
3217 (should
3218 (org-test-with-temp-text
3219 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
3220 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
3221 (looking-at "#\\+BEGIN_QUOTE")))
3222 ;; Optional argument is also case-insensitive.
3223 (should
3224 (org-test-with-temp-text
3225 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
3226 (let ((case-fold-search nil))
3227 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
3228 (looking-at "#\\+begin_quote")))))
3230 (ert-deftest test-org/previous-block ()
3231 "Test `org-previous-block' specifications."
3232 ;; Regular test.
3233 (should
3234 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
3235 (org-previous-block 1)
3236 (looking-at "#\\+BEGIN_CENTER")))
3237 ;; Ignore case.
3238 (should
3239 (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
3240 (let ((case-fold-search nil))
3241 (org-previous-block 1)
3242 (looking-at "#\\+begin_center"))))
3243 ;; Ignore current line.
3244 (should
3245 (org-test-with-temp-text
3246 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
3247 (org-previous-block 1)
3248 (looking-at "#\\+BEGIN_QUOTE")))
3249 ;; Throw an error when no block is found.
3250 (should-error
3251 (org-test-with-temp-text "Paragraph<point>"
3252 (org-previous-block 1)))
3253 ;; With an argument, skip many blocks at once.
3254 (should
3255 (org-test-with-temp-text
3256 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
3257 (org-previous-block 2)
3258 (looking-at "#\\+BEGIN_CENTER")))
3259 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
3260 (should
3261 (org-test-with-temp-text
3262 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
3263 (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
3264 (looking-at "#\\+BEGIN_QUOTE")))
3265 ;; Optional argument is also case-insensitive.
3266 (should
3267 (org-test-with-temp-text
3268 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
3269 (let ((case-fold-search nil))
3270 (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
3271 (looking-at "#\\+begin_quote")))))
3274 ;;; Outline structure
3276 (ert-deftest test-org/demote ()
3277 "Test `org-demote' specifications."
3278 ;; Add correct number of stars according to `org-odd-levels-only'.
3279 (should
3280 (= 2
3281 (org-test-with-temp-text "* H"
3282 (let ((org-odd-levels-only nil)) (org-demote))
3283 (org-current-level))))
3284 (should
3285 (= 3
3286 (org-test-with-temp-text "* H"
3287 (let ((org-odd-levels-only t)) (org-demote))
3288 (org-current-level))))
3289 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
3290 (should
3291 (org-test-with-temp-text "* H :tag:"
3292 (let ((org-tags-column 10)
3293 (org-auto-align-tags t)
3294 (org-odd-levels-only nil))
3295 (org-demote))
3296 (org-move-to-column 10)
3297 (looking-at-p ":tag:$")))
3298 (should-not
3299 (org-test-with-temp-text "* H :tag:"
3300 (let ((org-tags-column 10)
3301 (org-auto-align-tags nil)
3302 (org-odd-levels-only nil))
3303 (org-demote))
3304 (org-move-to-column 10)
3305 (looking-at-p ":tag:$")))
3306 ;; When `org-adapt-indentation' is non-nil, always indent planning
3307 ;; info and property drawers accordingly.
3308 (should
3309 (= 3
3310 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
3311 (let ((org-odd-levels-only nil)
3312 (org-adapt-indentation t))
3313 (org-demote))
3314 (forward-line)
3315 (org-get-indentation))))
3316 (should
3317 (= 3
3318 (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
3319 (let ((org-odd-levels-only nil)
3320 (org-adapt-indentation t))
3321 (org-demote))
3322 (forward-line)
3323 (org-get-indentation))))
3324 (should-not
3325 (= 3
3326 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
3327 (let ((org-odd-levels-only nil)
3328 (org-adapt-indentation nil))
3329 (org-demote))
3330 (forward-line)
3331 (org-get-indentation))))
3332 ;; When `org-adapt-indentation' is non-nil, shift all lines in
3333 ;; section accordingly. Ignore, however, footnote definitions and
3334 ;; inlinetasks boundaries.
3335 (should
3336 (= 3
3337 (org-test-with-temp-text "* H\n Paragraph"
3338 (let ((org-odd-levels-only nil)
3339 (org-adapt-indentation t))
3340 (org-demote))
3341 (forward-line)
3342 (org-get-indentation))))
3343 (should
3344 (= 2
3345 (org-test-with-temp-text "* H\n Paragraph"
3346 (let ((org-odd-levels-only nil)
3347 (org-adapt-indentation nil))
3348 (org-demote))
3349 (forward-line)
3350 (org-get-indentation))))
3351 (should
3352 (zerop
3353 (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
3354 (let ((org-odd-levels-only nil)
3355 (org-adapt-indentation t))
3356 (org-demote))
3357 (goto-char (point-max))
3358 (org-get-indentation))))
3359 (should
3360 (= 3
3361 (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
3362 (let ((org-odd-levels-only nil)
3363 (org-adapt-indentation t))
3364 (org-demote))
3365 (goto-char (point-max))
3366 (org-get-indentation))))
3367 (when (featurep 'org-inlinetask)
3368 (should
3369 (zerop
3370 (let ((org-inlinetask-min-level 5)
3371 (org-adapt-indentation t))
3372 (org-test-with-temp-text "* H\n***** I\n***** END"
3373 (org-demote)
3374 (forward-line)
3375 (org-get-indentation))))))
3376 (when (featurep 'org-inlinetask)
3377 (should
3378 (= 3
3379 (let ((org-inlinetask-min-level 5)
3380 (org-adapt-indentation t))
3381 (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
3382 (org-demote)
3383 (forward-line 2)
3384 (org-get-indentation))))))
3385 ;; Ignore contents of source blocks or example blocks when
3386 ;; indentation should be preserved (through
3387 ;; `org-src-preserve-indentation' or "-i" flag).
3388 (should-not
3389 (zerop
3390 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
3391 (let ((org-adapt-indentation t)
3392 (org-src-preserve-indentation nil))
3393 (org-demote))
3394 (forward-line 2)
3395 (org-get-indentation))))
3396 (should
3397 (zerop
3398 (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
3399 (let ((org-adapt-indentation t)
3400 (org-src-preserve-indentation t))
3401 (org-demote))
3402 (forward-line 2)
3403 (org-get-indentation))))
3404 (should
3405 (zerop
3406 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
3407 (let ((org-adapt-indentation t)
3408 (org-src-preserve-indentation t))
3409 (org-demote))
3410 (forward-line 2)
3411 (org-get-indentation))))
3412 (should
3413 (zerop
3414 (org-test-with-temp-text
3415 "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
3416 (let ((org-adapt-indentation t)
3417 (org-src-preserve-indentation nil))
3418 (org-demote))
3419 (forward-line 2)
3420 (org-get-indentation)))))
3422 (ert-deftest test-org/promote ()
3423 "Test `org-promote' specifications."
3424 ;; Return an error if headline is to be promoted to level 0, unless
3425 ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
3426 ;; headline becomes a comment.
3427 (should-error
3428 (org-test-with-temp-text "* H"
3429 (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
3430 (should
3431 (equal "# H"
3432 (org-test-with-temp-text "* H"
3433 (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
3434 (buffer-string))))
3435 ;; Remove correct number of stars according to
3436 ;; `org-odd-levels-only'.
3437 (should
3438 (= 2
3439 (org-test-with-temp-text "*** H"
3440 (let ((org-odd-levels-only nil)) (org-promote))
3441 (org-current-level))))
3442 (should
3443 (= 1
3444 (org-test-with-temp-text "*** H"
3445 (let ((org-odd-levels-only t)) (org-promote))
3446 (org-current-level))))
3447 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
3448 (should
3449 (org-test-with-temp-text "** H :tag:"
3450 (let ((org-tags-column 10)
3451 (org-auto-align-tags t)
3452 (org-odd-levels-only nil))
3453 (org-promote))
3454 (org-move-to-column 10)
3455 (looking-at-p ":tag:$")))
3456 (should-not
3457 (org-test-with-temp-text "** H :tag:"
3458 (let ((org-tags-column 10)
3459 (org-auto-align-tags nil)
3460 (org-odd-levels-only nil))
3461 (org-promote))
3462 (org-move-to-column 10)
3463 (looking-at-p ":tag:$")))
3464 ;; When `org-adapt-indentation' is non-nil, always indent planning
3465 ;; info and property drawers.
3466 (should
3467 (= 2
3468 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
3469 (let ((org-odd-levels-only nil)
3470 (org-adapt-indentation t))
3471 (org-promote))
3472 (forward-line)
3473 (org-get-indentation))))
3474 (should
3475 (= 2
3476 (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
3477 (let ((org-odd-levels-only nil)
3478 (org-adapt-indentation t))
3479 (org-promote))
3480 (forward-line)
3481 (org-get-indentation))))
3482 (should-not
3483 (= 2
3484 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
3485 (let ((org-odd-levels-only nil)
3486 (org-adapt-indentation nil))
3487 (org-promote))
3488 (forward-line)
3489 (org-get-indentation))))
3490 ;; When `org-adapt-indentation' is non-nil, shift all lines in
3491 ;; section accordingly. Ignore, however, footnote definitions and
3492 ;; inlinetasks boundaries.
3493 (should
3494 (= 2
3495 (org-test-with-temp-text "** H\n Paragraph"
3496 (let ((org-odd-levels-only nil)
3497 (org-adapt-indentation t))
3498 (org-promote))
3499 (forward-line)
3500 (org-get-indentation))))
3501 (should-not
3502 (= 2
3503 (org-test-with-temp-text "** H\n Paragraph"
3504 (let ((org-odd-levels-only nil)
3505 (org-adapt-indentation nil))
3506 (org-promote))
3507 (forward-line)
3508 (org-get-indentation))))
3509 (should
3510 (= 2
3511 (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
3512 (let ((org-odd-levels-only nil)
3513 (org-adapt-indentation t))
3514 (org-promote))
3515 (forward-line)
3516 (org-get-indentation))))
3517 (when (featurep 'org-inlinetask)
3518 (should
3519 (zerop
3520 (let ((org-inlinetask-min-level 5)
3521 (org-adapt-indentation t))
3522 (org-test-with-temp-text "** H\n***** I\n***** END"
3523 (org-promote)
3524 (forward-line)
3525 (org-get-indentation))))))
3526 (when (featurep 'org-inlinetask)
3527 (should
3528 (= 2
3529 (let ((org-inlinetask-min-level 5)
3530 (org-adapt-indentation t))
3531 (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
3532 (org-promote)
3533 (forward-line 2)
3534 (org-get-indentation))))))
3535 ;; Give up shifting if it would break document's structure
3536 ;; otherwise.
3537 (should
3538 (= 3
3539 (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
3540 (let ((org-odd-levels-only nil)
3541 (org-adapt-indentation t))
3542 (org-promote))
3543 (forward-line)
3544 (org-get-indentation))))
3545 (should
3546 (= 3
3547 (org-test-with-temp-text "** H\n Paragraph\n * list."
3548 (let ((org-odd-levels-only nil)
3549 (org-adapt-indentation t))
3550 (org-promote))
3551 (forward-line)
3552 (org-get-indentation))))
3553 ;; Ignore contents of source blocks or example blocks when
3554 ;; indentation should be preserved (through
3555 ;; `org-src-preserve-indentation' or "-i" flag).
3556 (should-not
3557 (zerop
3558 (org-test-with-temp-text
3559 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
3560 (let ((org-adapt-indentation t)
3561 (org-src-preserve-indentation nil)
3562 (org-odd-levels-only nil))
3563 (org-promote))
3564 (forward-line)
3565 (org-get-indentation))))
3566 (should
3567 (zerop
3568 (org-test-with-temp-text
3569 "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
3570 (let ((org-adapt-indentation t)
3571 (org-src-preserve-indentation t)
3572 (org-odd-levels-only nil))
3573 (org-promote))
3574 (forward-line)
3575 (org-get-indentation))))
3576 (should
3577 (zerop
3578 (org-test-with-temp-text
3579 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
3580 (let ((org-adapt-indentation t)
3581 (org-src-preserve-indentation t)
3582 (org-odd-levels-only nil))
3583 (org-promote))
3584 (forward-line)
3585 (org-get-indentation))))
3586 (should
3587 (zerop
3588 (org-test-with-temp-text
3589 "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
3590 (let ((org-adapt-indentation t)
3591 (org-src-preserve-indentation nil)
3592 (org-odd-levels-only nil))
3593 (org-promote))
3594 (forward-line)
3595 (org-get-indentation)))))
3598 ;;; Planning
3600 (ert-deftest test-org/at-planning-p ()
3601 "Test `org-at-planning-p' specifications."
3602 ;; Regular test.
3603 (should
3604 (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
3605 (org-at-planning-p)))
3606 (should-not
3607 (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
3608 (org-at-planning-p)))
3609 ;; Correctly find planning attached to inlinetasks.
3610 (when (featurep 'org-inlinetask)
3611 (should
3612 (org-test-with-temp-text
3613 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
3614 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
3615 (should-not
3616 (org-test-with-temp-text
3617 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
3618 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
3619 (should-not
3620 (org-test-with-temp-text
3621 "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
3622 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
3623 (should-not
3624 (org-test-with-temp-text
3625 "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
3626 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
3628 (ert-deftest test-org/add-planning-info ()
3629 "Test `org-add-planning-info'."
3630 ;; Create deadline when `org-adapt-indentation' is non-nil.
3631 (should
3632 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3633 (org-test-with-temp-text "* H\nParagraph<point>"
3634 (let ((org-adapt-indentation t))
3635 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3636 (replace-regexp-in-string
3637 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3638 nil nil 1))))
3639 ;; Create deadline when `org-adapt-indentation' is nil.
3640 (should
3641 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3642 (org-test-with-temp-text "* H\nParagraph<point>"
3643 (let ((org-adapt-indentation nil))
3644 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3645 (replace-regexp-in-string
3646 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3647 nil nil 1))))
3648 ;; Update deadline when `org-adapt-indentation' is non-nil.
3649 (should
3650 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3651 (org-test-with-temp-text "\
3653 DEADLINE: <2015-06-24 Wed>
3654 Paragraph<point>"
3655 (let ((org-adapt-indentation t))
3656 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3657 (replace-regexp-in-string
3658 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3659 nil nil 1))))
3660 ;; Update deadline when `org-adapt-indentation' is nil.
3661 (should
3662 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3663 (org-test-with-temp-text "\
3665 DEADLINE: <2015-06-24 Wed>
3666 Paragraph<point>"
3667 (let ((org-adapt-indentation nil))
3668 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3669 (replace-regexp-in-string
3670 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3671 nil nil 1))))
3672 ;; Schedule when `org-adapt-indentation' is non-nil.
3673 (should
3674 (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
3675 (org-test-with-temp-text "* H\nParagraph<point>"
3676 (let ((org-adapt-indentation t))
3677 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
3678 (replace-regexp-in-string
3679 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3680 nil nil 1))))
3681 ;; Schedule when `org-adapt-indentation' is nil.
3682 (should
3683 (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
3684 (org-test-with-temp-text "* H\nParagraph<point>"
3685 (let ((org-adapt-indentation nil))
3686 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
3687 (replace-regexp-in-string
3688 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3689 nil nil 1))))
3690 ;; Add deadline when scheduled.
3691 (should
3692 (equal "\
3694 DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
3695 Paragraph"
3696 (org-test-with-temp-text "\
3698 SCHEDULED: <2015-06-24 Wed>
3699 Paragraph<point>"
3700 (let ((org-adapt-indentation t))
3701 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3702 (replace-regexp-in-string
3703 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3704 nil nil 1))))
3705 ;; Remove middle entry.
3706 (should
3707 (equal "\
3709 CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
3710 Paragraph"
3711 (org-test-with-temp-text "\
3713 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
3714 Paragraph<point>"
3715 (let ((org-adapt-indentation t))
3716 (org-add-planning-info nil nil 'deadline))
3717 (replace-regexp-in-string
3718 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
3719 nil nil 1))))
3720 ;; Remove last entry and then middle entry (order should not
3721 ;; matter).
3722 (should
3723 (equal "\
3725 CLOSED: [2015-06-24]
3726 Paragraph"
3727 (org-test-with-temp-text "\
3729 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
3730 Paragraph<point>"
3731 (let ((org-adapt-indentation t))
3732 (org-add-planning-info nil nil 'scheduled 'deadline))
3733 (replace-regexp-in-string
3734 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
3735 nil nil 1))))
3736 ;; Remove closed when `org-adapt-indentation' is non-nil.
3737 (should
3738 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3739 (org-test-with-temp-text "\
3741 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
3742 Paragraph<point>"
3743 (let ((org-adapt-indentation t))
3744 (org-add-planning-info nil nil 'closed))
3745 (replace-regexp-in-string
3746 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3747 nil nil 1))))
3748 (should
3749 (equal "* H\n Paragraph"
3750 (org-test-with-temp-text "\
3752 CLOSED: [2015-06-25 Thu]
3753 Paragraph<point>"
3754 (let ((org-adapt-indentation t))
3755 (org-add-planning-info nil nil 'closed))
3756 (replace-regexp-in-string
3757 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3758 nil nil 1))))
3759 ;; Remove closed when `org-adapt-indentation' is nil.
3760 (should
3761 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3762 (org-test-with-temp-text "\
3764 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
3765 Paragraph<point>"
3766 (let ((org-adapt-indentation nil))
3767 (org-add-planning-info nil nil 'closed))
3768 (replace-regexp-in-string
3769 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3770 nil nil 1))))
3771 (should
3772 (equal "* H\nParagraph"
3773 (org-test-with-temp-text "\
3775 CLOSED: [2015-06-25 Thu]
3776 Paragraph<point>"
3777 (let ((org-adapt-indentation nil))
3778 (org-add-planning-info nil nil 'closed))
3779 (replace-regexp-in-string
3780 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3781 nil nil 1))))
3782 ;; Remove closed entry and delete empty line.
3783 (should
3784 (equal "\
3786 Paragraph"
3787 (org-test-with-temp-text "\
3789 CLOSED: [2015-06-24 Wed]
3790 Paragraph<point>"
3791 (let ((org-adapt-indentation t))
3792 (org-add-planning-info nil nil 'closed))
3793 (replace-regexp-in-string
3794 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3795 nil nil 1))))
3796 ;; Remove one entry and update another.
3797 (should
3798 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3799 (org-test-with-temp-text "\
3801 SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
3802 Paragraph<point>"
3803 (let ((org-adapt-indentation t))
3804 (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
3805 (replace-regexp-in-string
3806 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3807 nil nil 1)))))
3810 ;;; Property API
3812 (ert-deftest test-org/buffer-property-keys ()
3813 "Test `org-buffer-property-keys' specifications."
3814 ;; Retrieve properties accross siblings.
3815 (should
3816 (equal '("A" "B")
3817 (org-test-with-temp-text "
3818 * H1
3819 :PROPERTIES:
3820 :A: 1
3821 :END:
3822 * H2
3823 :PROPERTIES:
3824 :B: 1
3825 :END:"
3826 (org-buffer-property-keys))))
3827 ;; Retrieve properties accross children.
3828 (should
3829 (equal '("A" "B")
3830 (org-test-with-temp-text "
3831 * H1
3832 :PROPERTIES:
3833 :A: 1
3834 :END:
3835 ** H2
3836 :PROPERTIES:
3837 :B: 1
3838 :END:"
3839 (org-buffer-property-keys))))
3840 ;; Retrieve muliple properties in the same drawer.
3841 (should
3842 (equal '("A" "B")
3843 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3844 (org-buffer-property-keys))))
3845 ;; Ignore extension symbol in property name.
3846 (should
3847 (equal '("A")
3848 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
3849 (org-buffer-property-keys))))
3850 ;; With non-nil COLUMNS, extract property names from columns.
3851 (should
3852 (equal '("A" "B")
3853 (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
3854 (org-buffer-property-keys nil nil t))))
3855 (should
3856 (equal '("A" "B" "COLUMNS")
3857 (org-test-with-temp-text
3858 "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
3859 (org-buffer-property-keys nil nil t))))
3860 ;; With non-nil IGNORE-MALFORMED malformed property drawers are silently ignored.
3861 (should
3862 (equal '("A")
3863 (org-test-with-temp-text
3864 "* a\n:PROPERTIES:\n:A: 1\n:END:\n* b\n:PROPERTIES:\nsome junk here\n:END:\n"
3865 (org-buffer-property-keys nil nil nil t)))))
3867 (ert-deftest test-org/property-values ()
3868 "Test `org-property-values' specifications."
3869 ;; Regular test.
3870 (should
3871 (equal '("2" "1")
3872 (org-test-with-temp-text
3873 "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
3874 (org-property-values "A"))))
3875 ;; Ignore empty values.
3876 (should-not
3877 (org-test-with-temp-text
3878 "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
3879 (org-property-values "A")))
3880 ;; Take into consideration extended values.
3881 (should
3882 (equal '("1 2")
3883 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
3884 (org-property-values "A")))))
3886 (ert-deftest test-org/find-property ()
3887 "Test `org-find-property' specifications."
3888 ;; Regular test.
3889 (should
3890 (= 1
3891 (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
3892 (org-find-property "prop"))))
3893 ;; Ignore false positives.
3894 (should
3895 (= 27
3896 (org-test-with-temp-text
3897 "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
3898 (org-find-property "A"))))
3899 ;; Return first entry found in buffer.
3900 (should
3901 (= 1
3902 (org-test-with-temp-text
3903 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
3904 (org-find-property "A"))))
3905 ;; Only search visible part of the buffer.
3906 (should
3907 (= 31
3908 (org-test-with-temp-text
3909 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
3910 (org-narrow-to-subtree)
3911 (org-find-property "A"))))
3912 ;; With optional argument, only find entries with a specific value.
3913 (should-not
3914 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3915 (org-find-property "A" "2")))
3916 (should
3917 (= 31
3918 (org-test-with-temp-text
3919 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
3920 (org-find-property "A" "2"))))
3921 ;; Use "nil" for explicit nil values.
3922 (should
3923 (= 31
3924 (org-test-with-temp-text
3925 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
3926 (org-find-property "A" "nil")))))
3928 (ert-deftest test-org/entry-delete ()
3929 "Test `org-entry-delete' specifications."
3930 ;; Regular test.
3931 (should
3932 (string-match
3933 " *:PROPERTIES:\n *:B: +2\n *:END:"
3934 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3935 (org-entry-delete (point) "A")
3936 (buffer-string))))
3937 ;; Also remove accumulated properties.
3938 (should-not
3939 (string-match
3940 ":A"
3941 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
3942 (org-entry-delete (point) "A")
3943 (buffer-string))))
3944 ;; When last property is removed, remove the property drawer.
3945 (should-not
3946 (string-match
3947 ":PROPERTIES:"
3948 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
3949 (org-entry-delete (point) "A")
3950 (buffer-string))))
3951 ;; Return a non-nil value when some property was removed.
3952 (should
3953 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3954 (org-entry-delete (point) "A")))
3955 (should-not
3956 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3957 (org-entry-delete (point) "C"))))
3959 (ert-deftest test-org/entry-get ()
3960 "Test `org-entry-get' specifications."
3961 ;; Regular test.
3962 (should
3963 (equal "1"
3964 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3965 (org-entry-get (point) "A"))))
3966 ;; Ignore case.
3967 (should
3968 (equal "1"
3969 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3970 (org-entry-get (point) "a"))))
3971 ;; Handle extended values, both before and after base value.
3972 (should
3973 (equal "1 2 3"
3974 (org-test-with-temp-text
3975 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
3976 (org-entry-get (point) "A"))))
3977 ;; Empty values are returned as the empty string.
3978 (should
3979 (equal ""
3980 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
3981 (org-entry-get (point) "A"))))
3982 ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
3983 ;; otherwise, return nil.
3984 (should-not
3985 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
3986 (org-entry-get (point) "A")))
3987 (should
3988 (equal "nil"
3989 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
3990 (org-entry-get (point) "A" nil t))))
3991 ;; Return nil when no property is found, independently on the
3992 ;; LITERAL-NIL argument.
3993 (should-not
3994 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3995 (org-entry-get (point) "B")))
3996 (should-not
3997 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3998 (org-entry-get (point) "B" nil t)))
3999 ;; Handle inheritance, when allowed. Include extended values and
4000 ;; possibly global values.
4001 (should
4002 (equal
4004 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
4005 (org-entry-get (point) "A" t))))
4006 (should
4007 (equal
4009 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
4010 (let ((org-use-property-inheritance t))
4011 (org-entry-get (point) "A" 'selective)))))
4012 (should-not
4013 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
4014 (let ((org-use-property-inheritance nil))
4015 (org-entry-get (point) "A" 'selective))))
4016 (should
4017 (equal
4018 "1 2"
4019 (org-test-with-temp-text
4020 "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
4021 (org-entry-get (point-max) "A" t))))
4022 (should
4023 (equal "1"
4024 (org-test-with-temp-text
4025 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
4026 (org-mode-restart)
4027 (org-entry-get (point-max) "A" t))))
4028 (should
4029 (equal "0 1"
4030 (org-test-with-temp-text
4031 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
4032 (org-mode-restart)
4033 (org-entry-get (point-max) "A" t)))))
4035 (ert-deftest test-org/entry-properties ()
4036 "Test `org-entry-properties' specifications."
4037 ;; Get "ITEM" property.
4038 (should
4039 (equal "H"
4040 (org-test-with-temp-text "* TODO H"
4041 (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
4042 (should
4043 (equal "H"
4044 (org-test-with-temp-text "* TODO H"
4045 (cdr (assoc "ITEM" (org-entry-properties))))))
4046 ;; Get "TODO" property. TODO keywords are case sensitive.
4047 (should
4048 (equal "TODO"
4049 (org-test-with-temp-text "* TODO H"
4050 (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
4051 (should
4052 (equal "TODO"
4053 (org-test-with-temp-text "* TODO H"
4054 (cdr (assoc "TODO" (org-entry-properties))))))
4055 (should-not
4056 (org-test-with-temp-text "* H"
4057 (assoc "TODO" (org-entry-properties nil "TODO"))))
4058 (should-not
4059 (org-test-with-temp-text "* todo H"
4060 (assoc "TODO" (org-entry-properties nil "TODO"))))
4061 ;; Get "PRIORITY" property.
4062 (should
4063 (equal "A"
4064 (org-test-with-temp-text "* [#A] H"
4065 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
4066 (should
4067 (equal "A"
4068 (org-test-with-temp-text "* [#A] H"
4069 (cdr (assoc "PRIORITY" (org-entry-properties))))))
4070 (should
4071 (equal (char-to-string org-default-priority)
4072 (org-test-with-temp-text "* H"
4073 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
4074 ;; Get "FILE" property.
4075 (should
4076 (org-test-with-temp-text-in-file "* H\nParagraph"
4077 (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
4078 (buffer-file-name))))
4079 (should
4080 (org-test-with-temp-text-in-file "* H\nParagraph"
4081 (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
4082 (buffer-file-name))))
4083 (should-not
4084 (org-test-with-temp-text "* H\nParagraph"
4085 (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
4086 ;; Get "TAGS" property.
4087 (should
4088 (equal ":tag1:tag2:"
4089 (org-test-with-temp-text "* H :tag1:tag2:"
4090 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
4091 (should
4092 (equal ":tag1:tag2:"
4093 (org-test-with-temp-text "* H :tag1:tag2:"
4094 (cdr (assoc "TAGS" (org-entry-properties))))))
4095 (should-not
4096 (org-test-with-temp-text "* H"
4097 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
4098 ;; Get "ALLTAGS" property.
4099 (should
4100 (equal ":tag1:tag2:"
4101 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
4102 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
4103 (should
4104 (equal ":tag1:tag2:"
4105 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
4106 (cdr (assoc "ALLTAGS" (org-entry-properties))))))
4107 (should-not
4108 (org-test-with-temp-text "* H"
4109 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
4110 ;; Get "BLOCKED" property.
4111 (should
4112 (equal "t"
4113 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
4114 (let ((org-enforce-todo-dependencies t)
4115 (org-blocker-hook
4116 '(org-block-todo-from-children-or-siblings-or-parent)))
4117 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
4118 (should
4119 (equal ""
4120 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
4121 (let ((org-enforce-todo-dependencies t)
4122 (org-blocker-hook
4123 '(org-block-todo-from-children-or-siblings-or-parent)))
4124 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
4125 ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
4126 (should
4127 (equal
4128 "[2012-03-29 thu.]"
4129 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
4130 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
4131 (should
4132 (equal
4133 "[2012-03-29 thu.]"
4134 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
4135 (cdr (assoc "CLOSED" (org-entry-properties))))))
4136 (should-not
4137 (org-test-with-temp-text "* H"
4138 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
4139 (should
4140 (equal
4141 "<2014-03-04 tue.>"
4142 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
4143 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
4144 (should
4145 (equal
4146 "<2014-03-04 tue.>"
4147 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
4148 (cdr (assoc "DEADLINE" (org-entry-properties))))))
4149 (should-not
4150 (org-test-with-temp-text "* H"
4151 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
4152 (should
4153 (equal
4154 "<2014-03-04 tue.>"
4155 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
4156 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
4157 (should
4158 (equal
4159 "<2014-03-04 tue.>"
4160 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
4161 (cdr (assoc "SCHEDULED" (org-entry-properties))))))
4162 (should-not
4163 (org-test-with-temp-text "* H"
4164 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
4165 ;; Get "CATEGORY"
4166 (should
4167 (equal "cat"
4168 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
4169 (cdr (assoc "CATEGORY" (org-entry-properties))))))
4170 (should
4171 (equal "cat"
4172 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
4173 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
4174 (should
4175 (equal "cat"
4176 (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
4177 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
4178 (should
4179 (equal "cat2"
4180 (org-test-with-temp-text
4181 (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
4182 "\n"
4183 "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
4184 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
4185 ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
4186 (should
4187 (equal "<2012-03-29 thu.>"
4188 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
4189 (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
4190 (should
4191 (equal "[2012-03-29 thu.]"
4192 (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
4193 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
4194 (should
4195 (equal "<2012-03-29 thu.>"
4196 (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
4197 (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
4198 (should
4199 (equal "[2014-03-04 tue.]"
4200 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
4201 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties nil "TIMESTAMP_IA"))))))
4202 ;; Get standard properties.
4203 (should
4204 (equal "1"
4205 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
4206 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
4207 ;; Handle extended properties.
4208 (should
4209 (equal "1 2 3"
4210 (org-test-with-temp-text
4211 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
4212 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
4213 (should
4214 (equal "1 2 3"
4215 (org-test-with-temp-text
4216 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
4217 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
4218 ;; Ignore forbidden (special) properties.
4219 (should-not
4220 (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
4221 (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
4223 (ert-deftest test-org/entry-put ()
4224 "Test `org-entry-put' specifications."
4225 ;; Error when not a string or nil.
4226 (should-error
4227 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
4228 (org-entry-put 1 "test" 2)))
4229 ;; Error when property name is invalid.
4230 (should-error
4231 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
4232 (org-entry-put 1 "no space" "value")))
4233 (should-error
4234 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
4235 (org-entry-put 1 "" "value")))
4236 ;; Set "TODO" property.
4237 (should
4238 (string-match (regexp-quote " TODO H")
4239 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
4240 (org-entry-put (point) "TODO" "TODO")
4241 (buffer-string))))
4242 (should
4243 (string-match (regexp-quote "* H")
4244 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
4245 (org-entry-put (point) "TODO" nil)
4246 (buffer-string))))
4247 ;; Set "PRIORITY" property.
4248 (should
4249 (equal "* [#A] H"
4250 (org-test-with-temp-text "* [#B] H"
4251 (org-entry-put (point) "PRIORITY" "A")
4252 (buffer-string))))
4253 (should
4254 (equal "* H"
4255 (org-test-with-temp-text "* [#B] H"
4256 (org-entry-put (point) "PRIORITY" nil)
4257 (buffer-string))))
4258 ;; Set "SCHEDULED" property.
4259 (should
4260 (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
4261 (org-test-with-temp-text "* H"
4262 (org-entry-put (point) "SCHEDULED" "2014-03-04")
4263 (buffer-string))))
4264 (should
4265 (string= "* H\n"
4266 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
4267 (org-entry-put (point) "SCHEDULED" nil)
4268 (buffer-string))))
4269 (should
4270 (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
4271 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
4272 (org-entry-put (point) "SCHEDULED" "earlier")
4273 (buffer-string))))
4274 (should
4275 (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
4276 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
4277 (org-entry-put (point) "SCHEDULED" "later")
4278 (buffer-string))))
4279 ;; Set "DEADLINE" property.
4280 (should
4281 (string-match "^ *DEADLINE: <2014-03-04 .*?>"
4282 (org-test-with-temp-text "* H"
4283 (org-entry-put (point) "DEADLINE" "2014-03-04")
4284 (buffer-string))))
4285 (should
4286 (string= "* H\n"
4287 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
4288 (org-entry-put (point) "DEADLINE" nil)
4289 (buffer-string))))
4290 (should
4291 (string-match "^ *DEADLINE: <2014-03-03 .*?>"
4292 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
4293 (org-entry-put (point) "DEADLINE" "earlier")
4294 (buffer-string))))
4295 (should
4296 (string-match "^ *DEADLINE: <2014-03-05 .*?>"
4297 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
4298 (org-entry-put (point) "DEADLINE" "later")
4299 (buffer-string))))
4300 ;; Set "CATEGORY" property
4301 (should
4302 (string-match "^ *:CATEGORY: cat"
4303 (org-test-with-temp-text "* H"
4304 (org-entry-put (point) "CATEGORY" "cat")
4305 (buffer-string))))
4306 ;; Regular properties, with or without pre-existing drawer.
4307 (should
4308 (string-match "^ *:A: +2$"
4309 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
4310 (org-entry-put (point) "A" "2")
4311 (buffer-string))))
4312 (should
4313 (string-match "^ *:A: +1$"
4314 (org-test-with-temp-text "* H"
4315 (org-entry-put (point) "A" "1")
4316 (buffer-string))))
4317 ;; Special case: two consecutive headlines.
4318 (should
4319 (string-match "\\* A\n *:PROPERTIES:"
4320 (org-test-with-temp-text "* A\n** B"
4321 (org-entry-put (point) "A" "1")
4322 (buffer-string)))))
4325 ;;; Radio Targets
4327 (ert-deftest test-org/update-radio-target-regexp ()
4328 "Test `org-update-radio-target-regexp' specifications."
4329 ;; Properly update cache with no previous radio target regexp.
4330 (should
4331 (eq 'link
4332 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
4333 (save-excursion (goto-char (point-max)) (org-element-context))
4334 (insert "<<<")
4335 (search-forward "o")
4336 (insert ">>>")
4337 (org-update-radio-target-regexp)
4338 (goto-char (point-max))
4339 (org-element-type (org-element-context)))))
4340 ;; Properly update cache with previous radio target regexp.
4341 (should
4342 (eq 'link
4343 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
4344 (save-excursion (goto-char (point-max)) (org-element-context))
4345 (insert "<<<")
4346 (search-forward "o")
4347 (insert ">>>")
4348 (org-update-radio-target-regexp)
4349 (search-backward "r")
4350 (delete-char 5)
4351 (insert "new")
4352 (org-update-radio-target-regexp)
4353 (goto-char (point-max))
4354 (delete-region (line-beginning-position) (point))
4355 (insert "new")
4356 (org-element-type (org-element-context))))))
4359 ;;; Sparse trees
4361 (ert-deftest test-org/match-sparse-tree ()
4362 "Test `org-match-sparse-tree' specifications."
4363 ;; Match tags.
4364 (should-not
4365 (org-test-with-temp-text "* H\n** H1 :tag:"
4366 (org-match-sparse-tree nil "tag")
4367 (search-forward "H1")
4368 (org-invisible-p2)))
4369 (should
4370 (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
4371 (org-match-sparse-tree nil "tag")
4372 (search-forward "H2")
4373 (org-invisible-p2)))
4374 ;; "-" operator for tags.
4375 (should-not
4376 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
4377 (org-match-sparse-tree nil "tag1-tag2")
4378 (search-forward "H1")
4379 (org-invisible-p2)))
4380 (should
4381 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
4382 (org-match-sparse-tree nil "tag1-tag2")
4383 (search-forward "H2")
4384 (org-invisible-p2)))
4385 ;; "&" operator for tags.
4386 (should
4387 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
4388 (org-match-sparse-tree nil "tag1&tag2")
4389 (search-forward "H1")
4390 (org-invisible-p2)))
4391 (should-not
4392 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
4393 (org-match-sparse-tree nil "tag1&tag2")
4394 (search-forward "H2")
4395 (org-invisible-p2)))
4396 ;; "|" operator for tags.
4397 (should-not
4398 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
4399 (org-match-sparse-tree nil "tag1|tag2")
4400 (search-forward "H1")
4401 (org-invisible-p2)))
4402 (should-not
4403 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
4404 (org-match-sparse-tree nil "tag1|tag2")
4405 (search-forward "H2")
4406 (org-invisible-p2)))
4407 ;; Regexp match on tags.
4408 (should-not
4409 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
4410 (org-match-sparse-tree nil "{^tag.*}")
4411 (search-forward "H1")
4412 (org-invisible-p2)))
4413 (should
4414 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
4415 (org-match-sparse-tree nil "{^tag.*}")
4416 (search-forward "H2")
4417 (org-invisible-p2)))
4418 ;; Match group tags.
4419 (should-not
4420 (org-test-with-temp-text
4421 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
4422 (org-match-sparse-tree nil "work")
4423 (search-forward "H1")
4424 (org-invisible-p2)))
4425 (should-not
4426 (org-test-with-temp-text
4427 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
4428 (org-match-sparse-tree nil "work")
4429 (search-forward "H2")
4430 (org-invisible-p2)))
4431 ;; Match group tags with hard brackets.
4432 (should-not
4433 (org-test-with-temp-text
4434 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
4435 (org-match-sparse-tree nil "work")
4436 (search-forward "H1")
4437 (org-invisible-p2)))
4438 (should-not
4439 (org-test-with-temp-text
4440 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
4441 (org-match-sparse-tree nil "work")
4442 (search-forward "H2")
4443 (org-invisible-p2)))
4444 ;; Match tags in hierarchies
4445 (should-not
4446 (org-test-with-temp-text
4447 "#+TAGS: [ Lev_1 : Lev_2 ]\n
4448 #+TAGS: [ Lev_2 : Lev_3 ]\n
4449 #+TAGS: { Lev_3 : Lev_4 }\n
4450 * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
4451 (org-match-sparse-tree nil "Lev_1")
4452 (search-forward "H4")
4453 (org-invisible-p2)))
4454 ;; Match regular expressions in tags
4455 (should-not
4456 (org-test-with-temp-text
4457 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
4458 (org-match-sparse-tree nil "Lev")
4459 (search-forward "H1")
4460 (org-invisible-p2)))
4461 (should
4462 (org-test-with-temp-text
4463 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
4464 (org-match-sparse-tree nil "Lev")
4465 (search-forward "H1")
4466 (org-invisible-p2)))
4467 ;; Match properties.
4468 (should
4469 (org-test-with-temp-text
4470 "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
4471 (org-match-sparse-tree nil "A=\"1\"")
4472 (search-forward "H2")
4473 (org-invisible-p2)))
4474 (should-not
4475 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
4476 (org-match-sparse-tree nil "A=\"1\"")
4477 (search-forward "H2")
4478 (org-invisible-p2)))
4479 ;; Case is not significant when matching properties.
4480 (should-not
4481 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
4482 (org-match-sparse-tree nil "a=\"1\"")
4483 (search-forward "H2")
4484 (org-invisible-p2)))
4485 (should-not
4486 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
4487 (org-match-sparse-tree nil "A=\"1\"")
4488 (search-forward "H2")
4489 (org-invisible-p2)))
4490 ;; Match special LEVEL property.
4491 (should-not
4492 (org-test-with-temp-text "* H\n** H1\n*** H2"
4493 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
4494 (search-forward "H1")
4495 (org-invisible-p2)))
4496 (should
4497 (org-test-with-temp-text "* H\n** H1\n*** H2"
4498 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
4499 (search-forward "H2")
4500 (org-invisible-p2)))
4501 ;; Comparison operators when matching properties.
4502 (should
4503 (org-test-with-temp-text
4504 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
4505 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
4506 (search-forward "H1")
4507 (org-invisible-p2)))
4508 (should-not
4509 (org-test-with-temp-text
4510 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
4511 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
4512 (search-forward "H2")
4513 (org-invisible-p2)))
4514 ;; Regexp match on properties values.
4515 (should-not
4516 (org-test-with-temp-text
4517 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
4518 (org-match-sparse-tree nil "A={f.*}")
4519 (search-forward "H1")
4520 (org-invisible-p2)))
4521 (should
4522 (org-test-with-temp-text
4523 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
4524 (org-match-sparse-tree nil "A={f.*}")
4525 (search-forward "H2")
4526 (org-invisible-p2)))
4527 ;; With an optional argument, limit match to TODO entries.
4528 (should-not
4529 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
4530 (org-match-sparse-tree t "tag")
4531 (search-forward "H1")
4532 (org-invisible-p2)))
4533 (should
4534 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
4535 (org-match-sparse-tree t "tag")
4536 (search-forward "H2")
4537 (org-invisible-p2))))
4539 (ert-deftest test-org/occur ()
4540 "Test `org-occur' specifications."
4541 ;; Count number of matches.
4542 (should
4543 (= 1
4544 (org-test-with-temp-text "* H\nA\n* H2"
4545 (org-occur "A"))))
4546 (should
4547 (= 2
4548 (org-test-with-temp-text "* H\nA\n* H2\nA"
4549 (org-occur "A"))))
4550 ;; Test CALLBACK optional argument.
4551 (should
4552 (= 0
4553 (org-test-with-temp-text "* H\nA\n* H2"
4554 (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
4555 (should
4556 (= 1
4557 (org-test-with-temp-text "* H\nA\n* H2\nA"
4558 (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
4559 ;; Case-fold searches according to `org-occur-case-fold-search'.
4560 (should
4561 (= 2
4562 (org-test-with-temp-text "Aa"
4563 (let ((org-occur-case-fold-search t)) (org-occur "A")))))
4564 (should
4565 (= 2
4566 (org-test-with-temp-text "Aa"
4567 (let ((org-occur-case-fold-search t)) (org-occur "a")))))
4568 (should
4569 (= 1
4570 (org-test-with-temp-text "Aa"
4571 (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
4572 (should
4573 (= 1
4574 (org-test-with-temp-text "Aa"
4575 (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
4576 (should
4577 (= 1
4578 (org-test-with-temp-text "Aa"
4579 (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
4580 (should
4581 (= 2
4582 (org-test-with-temp-text "Aa"
4583 (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
4586 ;;; Tags
4588 (ert-deftest test-org/tag-string-to-alist ()
4589 "Test `org-tag-string-to-alist' specifications."
4590 ;; Tag without selection key.
4591 (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
4592 ;; Tag with selection key.
4593 (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
4594 ;; Tag group.
4595 (should
4596 (equal
4597 (org-tag-string-to-alist "[ group : t1 t2 ]")
4598 '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
4599 ;; Mutually exclusive tags.
4600 (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
4601 '((:startgroup) ("tag1") ("tag2") (:endgroup))))
4602 (should
4603 (equal
4604 (org-tag-string-to-alist "{ group : tag1 tag2 }")
4605 '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
4607 (ert-deftest test-org/tag-alist-to-string ()
4608 "Test `org-tag-alist-to-string' specifications."
4609 (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
4610 (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
4611 (should
4612 (equal
4613 (org-tag-alist-to-string
4614 '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
4615 "[ group : t1 t2 ]"))
4616 (should
4617 (equal (org-tag-alist-to-string
4618 '((:startgroup) ("tag1") ("tag2") (:endgroup)))
4619 "{ tag1 tag2 }"))
4620 (should
4621 (equal
4622 (org-tag-alist-to-string
4623 '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
4624 "{ group : tag1 tag2 }")))
4626 (ert-deftest test-org/tag-alist-to-groups ()
4627 "Test `org-tag-alist-to-groups' specifications."
4628 (should
4629 (equal (org-tag-alist-to-groups
4630 '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
4631 '(("group" "t1" "t2"))))
4632 (should
4633 (equal
4634 (org-tag-alist-to-groups
4635 '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
4636 '(("group" "t1" "t2"))))
4637 (should-not
4638 (org-tag-alist-to-groups
4639 '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
4641 (ert-deftest test-org/tag-align ()
4642 "Test tags alignment."
4643 ;; Test aligning tags with different display width.
4644 (should
4645 ;; 12345678901234567890
4646 (equal "* Test :abc:"
4647 (org-test-with-temp-text "* Test :abc:"
4648 (let ((org-tags-column -20)
4649 (indent-tabs-mode nil))
4650 (org-fix-tags-on-the-fly))
4651 (buffer-string))))
4652 (should
4653 ;; 12345678901234567890
4654 (equal "* Test :日本語:"
4655 (org-test-with-temp-text "* Test :日本語:"
4656 (let ((org-tags-column -20)
4657 (indent-tabs-mode nil))
4658 (org-fix-tags-on-the-fly))
4659 (buffer-string))))
4660 ;; Make sure aligning tags do not skip invisible text.
4661 (should
4662 (equal "* [[linkx]] :tag:"
4663 (org-test-with-temp-text "* [[link<point>]] :tag:"
4664 (let ((org-tags-column 0))
4665 (org-fix-tags-on-the-fly)
4666 (insert "x")
4667 (buffer-string))))))
4669 (ert-deftest test-org/tags-at ()
4670 (should
4671 (equal '("foo" "bar")
4672 (org-test-with-temp-text
4673 "* T<point>est :foo:bar:"
4674 (org-get-tags-at)))))
4677 ;;; Timestamps API
4679 (ert-deftest test-org/time-stamp ()
4680 "Test `org-time-stamp' specifications."
4681 ;; Insert chosen time stamp at point.
4682 (should
4683 (string-match
4684 "Te<2014-03-04 .*?>xt"
4685 (org-test-with-temp-text "Te<point>xt"
4686 (cl-letf (((symbol-function 'org-read-date)
4687 (lambda (&rest args)
4688 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
4689 (org-time-stamp nil)
4690 (buffer-string)))))
4691 ;; With a prefix argument, also insert time.
4692 (should
4693 (string-match
4694 "Te<2014-03-04 .*? 00:41>xt"
4695 (org-test-with-temp-text "Te<point>xt"
4696 (cl-letf (((symbol-function 'org-read-date)
4697 (lambda (&rest args)
4698 (apply #'encode-time
4699 (org-parse-time-string "2014-03-04 00:41")))))
4700 (org-time-stamp '(4))
4701 (buffer-string)))))
4702 ;; With two universal prefix arguments, insert an active timestamp
4703 ;; with the current time without prompting the user.
4704 (should
4705 (string-match
4706 "Te<2014-03-04 .*? 00:41>xt"
4707 (org-test-with-temp-text "Te<point>xt"
4708 (cl-letf (((symbol-function 'current-time)
4709 (lambda ()
4710 (apply #'encode-time
4711 (org-parse-time-string "2014-03-04 00:41")))))
4712 (org-time-stamp '(16))
4713 (buffer-string)))))
4714 ;; When optional argument is non-nil, insert an inactive timestamp.
4715 (should
4716 (string-match
4717 "Te\\[2014-03-04 .*?\\]xt"
4718 (org-test-with-temp-text "Te<point>xt"
4719 (cl-letf (((symbol-function 'org-read-date)
4720 (lambda (&rest args)
4721 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
4722 (org-time-stamp nil t)
4723 (buffer-string)))))
4724 ;; When called from a timestamp, replace existing one.
4725 (should
4726 (string-match
4727 "<2014-03-04 .*?>"
4728 (org-test-with-temp-text "<2012-03-29<point> thu.>"
4729 (cl-letf (((symbol-function 'org-read-date)
4730 (lambda (&rest args)
4731 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
4732 (org-time-stamp nil)
4733 (buffer-string)))))
4734 (should
4735 (string-match
4736 "<2014-03-04 .*?>--<2014-03-04 .*?>"
4737 (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
4738 (cl-letf (((symbol-function 'org-read-date)
4739 (lambda (&rest args)
4740 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
4741 (org-time-stamp nil)
4742 (buffer-string)))))
4743 ;; When replacing a timestamp, preserve repeater, if any.
4744 (should
4745 (string-match
4746 "<2014-03-04 .*? \\+2y>"
4747 (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
4748 (cl-letf (((symbol-function 'org-read-date)
4749 (lambda (&rest args)
4750 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
4751 (org-time-stamp nil)
4752 (buffer-string)))))
4753 ;; When called twice in a raw, build a date range.
4754 (should
4755 (string-match
4756 "<2012-03-29 .*?>--<2014-03-04 .*?>"
4757 (org-test-with-temp-text "<2012-03-29 thu.><point>"
4758 (cl-letf (((symbol-function 'org-read-date)
4759 (lambda (&rest args)
4760 (apply #'encode-time (org-parse-time-string "2014-03-04")))))
4761 (let ((last-command 'org-time-stamp)
4762 (this-command 'org-time-stamp))
4763 (org-time-stamp nil))
4764 (buffer-string))))))
4766 (ert-deftest test-org/timestamp-has-time-p ()
4767 "Test `org-timestamp-has-time-p' specifications."
4768 ;; With time.
4769 (should
4770 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
4771 (org-timestamp-has-time-p (org-element-context))))
4772 ;; Without time.
4773 (should-not
4774 (org-test-with-temp-text "<2012-03-29 Thu>"
4775 (org-timestamp-has-time-p (org-element-context)))))
4777 (ert-deftest test-org/timestamp-format ()
4778 "Test `org-timestamp-format' specifications."
4779 ;; Regular test.
4780 (should
4781 (equal
4782 "2012-03-29 16:40"
4783 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
4784 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
4785 ;; Range end.
4786 (should
4787 (equal
4788 "2012-03-29"
4789 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
4790 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
4792 (ert-deftest test-org/timestamp-split-range ()
4793 "Test `org-timestamp-split-range' specifications."
4794 ;; Extract range start (active).
4795 (should
4796 (equal '(2012 3 29)
4797 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4798 (let ((ts (org-timestamp-split-range (org-element-context))))
4799 (mapcar (lambda (p) (org-element-property p ts))
4800 '(:year-end :month-end :day-end))))))
4801 ;; Extract range start (inactive)
4802 (should
4803 (equal '(2012 3 29)
4804 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
4805 (let ((ts (org-timestamp-split-range (org-element-context))))
4806 (mapcar (lambda (p) (org-element-property p ts))
4807 '(:year-end :month-end :day-end))))))
4808 ;; Extract range end (active).
4809 (should
4810 (equal '(2012 3 30)
4811 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4812 (let ((ts (org-timestamp-split-range
4813 (org-element-context) t)))
4814 (mapcar (lambda (p) (org-element-property p ts))
4815 '(:year-end :month-end :day-end))))))
4816 ;; Extract range end (inactive)
4817 (should
4818 (equal '(2012 3 30)
4819 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
4820 (let ((ts (org-timestamp-split-range
4821 (org-element-context) t)))
4822 (mapcar (lambda (p) (org-element-property p ts))
4823 '(:year-end :month-end :day-end))))))
4824 ;; Return the timestamp if not a range.
4825 (should
4826 (org-test-with-temp-text "[2012-03-29 Thu]"
4827 (let* ((ts-orig (org-element-context))
4828 (ts-copy (org-timestamp-split-range ts-orig)))
4829 (eq ts-orig ts-copy))))
4830 (should
4831 (org-test-with-temp-text "<%%(org-float t 4 2)>"
4832 (let* ((ts-orig (org-element-context))
4833 (ts-copy (org-timestamp-split-range ts-orig)))
4834 (eq ts-orig ts-copy)))))
4836 (ert-deftest test-org/timestamp-translate ()
4837 "Test `org-timestamp-translate' specifications."
4838 ;; Translate whole date range.
4839 (should
4840 (equal "<29>--<30>"
4841 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4842 (let ((org-display-custom-times t)
4843 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4844 (org-timestamp-translate (org-element-context))))))
4845 ;; Translate date range start.
4846 (should
4847 (equal "<29>"
4848 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4849 (let ((org-display-custom-times t)
4850 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4851 (org-timestamp-translate (org-element-context) 'start)))))
4852 ;; Translate date range end.
4853 (should
4854 (equal "<30>"
4855 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4856 (let ((org-display-custom-times t)
4857 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4858 (org-timestamp-translate (org-element-context) 'end)))))
4859 ;; Translate time range.
4860 (should
4861 (equal "<08>--<16>"
4862 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
4863 (let ((org-display-custom-times t)
4864 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
4865 (org-timestamp-translate (org-element-context))))))
4866 ;; Translate non-range timestamp.
4867 (should
4868 (equal "<29>"
4869 (org-test-with-temp-text "<2012-03-29 Thu>"
4870 (let ((org-display-custom-times t)
4871 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4872 (org-timestamp-translate (org-element-context))))))
4873 ;; Do not change `diary' timestamps.
4874 (should
4875 (equal "<%%(org-float t 4 2)>"
4876 (org-test-with-temp-text "<%%(org-float t 4 2)>"
4877 (let ((org-display-custom-times t)
4878 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4879 (org-timestamp-translate (org-element-context)))))))
4883 ;;; Visibility
4885 (ert-deftest test-org/flag-drawer ()
4886 "Test `org-flag-drawer' specifications."
4887 ;; Hide drawer.
4888 (should
4889 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4890 (org-flag-drawer t)
4891 (get-char-property (line-end-position) 'invisible)))
4892 ;; Show drawer.
4893 (should-not
4894 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4895 (org-flag-drawer t)
4896 (org-flag-drawer nil)
4897 (get-char-property (line-end-position) 'invisible)))
4898 ;; Test optional argument.
4899 (should
4900 (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
4901 (let ((drawer (save-excursion (search-forward ":D2")
4902 (org-element-at-point))))
4903 (org-flag-drawer t drawer)
4904 (get-char-property (progn (search-forward ":D2") (line-end-position))
4905 'invisible))))
4906 (should-not
4907 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
4908 (let ((drawer (save-excursion (search-forward ":D2")
4909 (org-element-at-point))))
4910 (org-flag-drawer t drawer)
4911 (get-char-property (line-end-position) 'invisible))))
4912 ;; Do not hide fake drawers.
4913 (should-not
4914 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
4915 (forward-line 1)
4916 (org-flag-drawer t)
4917 (get-char-property (line-end-position) 'invisible)))
4918 ;; Do not hide incomplete drawers.
4919 (should-not
4920 (org-test-with-temp-text ":D:\nparagraph"
4921 (forward-line 1)
4922 (org-flag-drawer t)
4923 (get-char-property (line-end-position) 'invisible)))
4924 ;; Do not hide drawers when called from final blank lines.
4925 (should-not
4926 (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
4927 (goto-char (point-max))
4928 (org-flag-drawer t)
4929 (goto-char (point-min))
4930 (get-char-property (line-end-position) 'invisible)))
4931 ;; Don't leave point in an invisible part of the buffer when hiding
4932 ;; a drawer away.
4933 (should-not
4934 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4935 (goto-char (point-max))
4936 (org-flag-drawer t)
4937 (get-char-property (point) 'invisible))))
4939 (ert-deftest test-org/hide-block-toggle ()
4940 "Test `org-hide-block-toggle' specifications."
4941 ;; Error when not at a block.
4942 (should-error
4943 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
4944 (org-hide-block-toggle 'off)
4945 (get-char-property (line-end-position) 'invisible)))
4946 ;; Hide block.
4947 (should
4948 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
4949 (org-hide-block-toggle)
4950 (get-char-property (line-end-position) 'invisible)))
4951 (should
4952 (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
4953 (org-hide-block-toggle)
4954 (get-char-property (line-end-position) 'invisible)))
4955 ;; Show block unconditionally when optional argument is `off'.
4956 (should-not
4957 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4958 (org-hide-block-toggle)
4959 (org-hide-block-toggle 'off)
4960 (get-char-property (line-end-position) 'invisible)))
4961 (should-not
4962 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4963 (org-hide-block-toggle 'off)
4964 (get-char-property (line-end-position) 'invisible)))
4965 ;; Hide block unconditionally when optional argument is non-nil.
4966 (should
4967 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4968 (org-hide-block-toggle t)
4969 (get-char-property (line-end-position) 'invisible)))
4970 (should
4971 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4972 (org-hide-block-toggle)
4973 (org-hide-block-toggle t)
4974 (get-char-property (line-end-position) 'invisible)))
4975 ;; Do not hide block when called from final blank lines.
4976 (should-not
4977 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
4978 (org-hide-block-toggle)
4979 (goto-char (point-min))
4980 (get-char-property (line-end-position) 'invisible)))
4981 ;; Don't leave point in an invisible part of the buffer when hiding
4982 ;; a block away.
4983 (should-not
4984 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
4985 (org-hide-block-toggle)
4986 (get-char-property (point) 'invisible))))
4988 (ert-deftest test-org/hide-block-toggle-maybe ()
4989 "Test `org-hide-block-toggle-maybe' specifications."
4990 (should
4991 (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
4992 (org-hide-block-toggle-maybe)))
4993 (should-not
4994 (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
4996 (ert-deftest test-org/show-set-visibility ()
4997 "Test `org-show-set-visibility' specifications."
4998 ;; Do not throw an error before first heading.
4999 (should
5000 (org-test-with-temp-text "Preamble\n* Headline"
5001 (org-show-set-visibility 'tree)
5003 ;; Test all visibility spans, both on headline and in entry.
5004 (let ((list-visible-lines
5005 (lambda (state headerp)
5006 (org-test-with-temp-text "* Grandmother (0)
5007 ** Uncle (1)
5008 *** Heir (2)
5009 ** Father (3)
5010 Ancestor text (4)
5011 *** Sister (5)
5012 Sibling text (6)
5013 *** Self (7)
5014 Match (8)
5015 **** First born (9)
5016 Child text (10)
5017 **** The other child (11)
5018 *** Brother (12)
5019 ** Aunt (13)
5021 (org-cycle t)
5022 (search-forward (if headerp "Self" "Match"))
5023 (org-show-set-visibility state)
5024 (goto-char (point-min))
5025 (let (result (line 0))
5026 (while (not (eobp))
5027 (unless (org-invisible-p2) (push line result))
5028 (incf line)
5029 (forward-line))
5030 (nreverse result))))))
5031 (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
5032 (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
5033 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
5034 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
5035 (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
5036 (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
5037 (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
5038 (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
5039 (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
5040 (should (equal '(0 1 3 5 7 8 9 11 12 13)
5041 (funcall list-visible-lines 'tree nil)))
5042 (should (equal '(0 1 3 4 5 7 12 13)
5043 (funcall list-visible-lines 'canonical t)))
5044 (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
5045 (funcall list-visible-lines 'canonical nil))))
5046 ;; When point is hidden in a drawer or a block, make sure to make it
5047 ;; visible.
5048 (should-not
5049 (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
5050 (org-hide-block-toggle)
5051 (search-forward "Text")
5052 (org-show-set-visibility 'minimal)
5053 (org-invisible-p2)))
5054 (should-not
5055 (org-test-with-temp-text ":DRAWER:\nText\n:END:"
5056 (org-flag-drawer t)
5057 (search-forward "Text")
5058 (org-show-set-visibility 'minimal)
5059 (org-invisible-p2)))
5060 (should-not
5061 (org-test-with-temp-text
5062 "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
5063 (org-flag-drawer t)
5064 (forward-line -1)
5065 (org-hide-block-toggle)
5066 (search-forward "Text")
5067 (org-show-set-visibility 'minimal)
5068 (org-invisible-p2))))
5071 (provide 'test-org)
5073 ;;; test-org.el ends here