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