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