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