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