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