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