org-format-outline-path: Ignore nil path elements
[org-mode/org-tableheadings.git] / testing / lisp / test-org.el
blobb4dff701c6d47358c067c9bdc2047eff8661a172
1 ;;; test-org.el --- tests for org.el
3 ;; Copyright (c) David Maus
4 ;; Authors: David Maus
6 ;; This file is not part of GNU Emacs.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Comments:
23 ;; Template test file for Org-mode tests
25 ;;; Code:
28 ;;; Comments
30 (ert-deftest test-org/toggle-comment ()
31 "Test `org-toggle-comment' specifications."
32 ;; Simple headline.
33 (should
34 (equal "* Test"
35 (org-test-with-temp-text "* COMMENT Test"
36 (org-toggle-comment)
37 (buffer-string))))
38 (should
39 (equal "* COMMENT Test"
40 (org-test-with-temp-text "* Test"
41 (org-toggle-comment)
42 (buffer-string))))
43 ;; Headline with a regular keyword.
44 (should
45 (equal "* TODO Test"
46 (org-test-with-temp-text "* TODO COMMENT Test"
47 (org-toggle-comment)
48 (buffer-string))))
49 (should
50 (equal "* TODO COMMENT Test"
51 (org-test-with-temp-text "* TODO Test"
52 (org-toggle-comment)
53 (buffer-string))))
54 ;; Empty headline.
55 (should
56 (equal "* "
57 (org-test-with-temp-text "* COMMENT"
58 (org-toggle-comment)
59 (buffer-string))))
60 (should
61 (equal "* COMMENT"
62 (org-test-with-temp-text "* "
63 (org-toggle-comment)
64 (buffer-string))))
65 ;; Headline with a single keyword.
66 (should
67 (equal "* TODO "
68 (org-test-with-temp-text "* TODO COMMENT"
69 (org-toggle-comment)
70 (buffer-string))))
71 (should
72 (equal "* TODO COMMENT"
73 (org-test-with-temp-text "* TODO"
74 (org-toggle-comment)
75 (buffer-string))))
76 ;; Headline with a keyword, a priority cookie and contents.
77 (should
78 (equal "* TODO [#A] Headline"
79 (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
80 (org-toggle-comment)
81 (buffer-string))))
82 (should
83 (equal "* TODO [#A] COMMENT Headline"
84 (org-test-with-temp-text "* TODO [#A] Headline"
85 (org-toggle-comment)
86 (buffer-string)))))
88 (ert-deftest test-org/comment-dwim ()
89 "Test `comment-dwim' behaviour in an Org buffer."
90 ;; No region selected, no comment on current line and line not
91 ;; empty: insert comment on line above.
92 (should
93 (equal "# \nComment"
94 (org-test-with-temp-text "Comment"
95 (progn (call-interactively 'comment-dwim)
96 (buffer-string)))))
97 ;; No region selected, no comment on current line and line empty:
98 ;; insert comment on this line.
99 (should
100 (equal "# \nParagraph"
101 (org-test-with-temp-text "\nParagraph"
102 (progn (call-interactively 'comment-dwim)
103 (buffer-string)))))
104 ;; No region selected, and a comment on this line: indent it.
105 (should
106 (equal "* Headline\n # Comment"
107 (org-test-with-temp-text "* Headline\n# Comment"
108 (progn (forward-line)
109 (let ((org-adapt-indentation t))
110 (call-interactively 'comment-dwim))
111 (buffer-string)))))
112 ;; Also recognize single # at column 0 as comments.
113 (should
114 (equal "# Comment"
115 (org-test-with-temp-text "# Comment"
116 (progn (forward-line)
117 (call-interactively 'comment-dwim)
118 (buffer-string)))))
119 ;; Region selected and only comments and blank lines within it:
120 ;; un-comment all commented lines.
121 (should
122 (equal "Comment 1\n\nComment 2"
123 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
124 (progn
125 (transient-mark-mode 1)
126 (push-mark (point) t t)
127 (goto-char (point-max))
128 (call-interactively 'comment-dwim)
129 (buffer-string)))))
130 ;; Region selected without comments: comment all lines if
131 ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
132 (should
133 (equal "# Comment 1\n\n# Comment 2"
134 (org-test-with-temp-text "Comment 1\n\nComment 2"
135 (progn
136 (transient-mark-mode 1)
137 (push-mark (point) t t)
138 (goto-char (point-max))
139 (let ((comment-empty-lines nil))
140 (call-interactively 'comment-dwim))
141 (buffer-string)))))
142 (should
143 (equal "# Comment 1\n# \n# Comment 2"
144 (org-test-with-temp-text "Comment 1\n\nComment 2"
145 (progn
146 (transient-mark-mode 1)
147 (push-mark (point) t t)
148 (goto-char (point-max))
149 (let ((comment-empty-lines t))
150 (call-interactively 'comment-dwim))
151 (buffer-string)))))
152 ;; In front of a keyword without region, insert a new comment.
153 (should
154 (equal "# \n#+KEYWORD: value"
155 (org-test-with-temp-text "#+KEYWORD: value"
156 (progn (call-interactively 'comment-dwim)
157 (buffer-string)))))
158 ;; In a source block, use appropriate syntax.
159 (should
160 (equal " ;; "
161 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n\n#+END_SRC"
162 (forward-line)
163 (let ((org-edit-src-content-indentation 2))
164 (call-interactively 'comment-dwim))
165 (buffer-substring-no-properties (line-beginning-position) (point)))))
166 (should
167 (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
168 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\na\nb\n#+END_SRC"
169 (forward-line)
170 (transient-mark-mode 1)
171 (push-mark (point) t t)
172 (forward-line 2)
173 (let ((org-edit-src-content-indentation 2))
174 (call-interactively 'comment-dwim))
175 (buffer-string)))))
179 ;;; Date and time analysis
181 (ert-deftest test-org/org-read-date ()
182 "Test `org-read-date' specifications."
183 ;; Parse ISO date with abbreviated year and month.
184 (should (equal "2012-03-29 16:40"
185 (let ((org-time-was-given t))
186 (org-read-date t nil "12-3-29 16:40"))))
187 ;; Parse Europeans dates.
188 (should (equal "2012-03-29 16:40"
189 (let ((org-time-was-given t))
190 (org-read-date t nil "29.03.2012 16:40"))))
191 ;; Parse Europeans dates without year.
192 (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
193 (let ((org-time-was-given t))
194 (org-read-date t nil "29.03. 16:40"))))
195 ;; Relative date applied to current time if there is single
196 ;; plus/minus, or to default date when there are two of them.
197 (should
198 (equal
199 "2015-03-04"
200 (flet ((current-time () (apply #'encode-time
201 (org-parse-time-string "2014-03-04"))))
202 (org-read-date
203 t nil "+1y" nil
204 (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
205 (should
206 (equal
207 "2013-03-29"
208 (flet ((current-time () (apply #'encode-time
209 (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 (flet ((current-time () (apply #'encode-time
220 (org-parse-time-string "2014-03-04"))))
221 (let ((org-read-date-prefer-future t))
222 (org-read-date t nil "1")))))
223 (should
224 (equal
225 "2013-03-04"
226 (flet ((current-time () (apply #'encode-time
227 (org-parse-time-string "2012-03-29"))))
228 (let ((org-read-date-prefer-future t))
229 (org-read-date t nil "3-4")))))
230 (should
231 (equal
232 "2012-03-04"
233 (flet ((current-time () (apply #'encode-time
234 (org-parse-time-string "2012-03-29"))))
235 (let ((org-read-date-prefer-future nil))
236 (org-read-date t nil "3-4")))))
237 ;; When set to `org-read-date-prefer-future' is set to `time', read
238 ;; day is moved to tomorrow if specified hour is before current
239 ;; time. However, it only happens in no other part of the date is
240 ;; specified.
241 (should
242 (equal
243 "2012-03-30"
244 (flet ((current-time () (apply #'encode-time
245 (org-parse-time-string "2012-03-29 16:40"))))
246 (let ((org-read-date-prefer-future 'time))
247 (org-read-date t nil "00:40" nil)))))
248 (should-not
249 (equal
250 "2012-03-30"
251 (flet ((current-time () (apply #'encode-time
252 (org-parse-time-string "2012-03-29 16:40"))))
253 (let ((org-read-date-prefer-future 'time))
254 (org-read-date t nil "29 00:40" nil)))))
255 ;; Caveat: `org-read-date-prefer-future' always refers to current
256 ;; time, not default time, when they differ.
257 (should
258 (equal
259 "2014-04-01"
260 (flet ((current-time
261 () (apply #'encode-time (org-parse-time-string "2014-03-04"))))
262 (let ((org-read-date-prefer-future t))
263 (org-read-date
264 t nil "1" nil
265 (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
266 (should
267 (equal
268 "2014-03-25"
269 (flet ((current-time
270 () (apply #'encode-time (org-parse-time-string "2014-03-04"))))
271 (let ((org-read-date-prefer-future t))
272 (org-read-date
273 t nil "25" nil
274 (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
276 (ert-deftest test-org/org-parse-time-string ()
277 "Test `org-parse-time-string'."
278 (should (equal (org-parse-time-string "2012-03-29 16:40")
279 '(0 40 16 29 3 2012 nil nil nil)))
280 (should (equal (org-parse-time-string "[2012-03-29 16:40]")
281 '(0 40 16 29 3 2012 nil nil nil)))
282 (should (equal (org-parse-time-string "<2012-03-29 16:40>")
283 '(0 40 16 29 3 2012 nil nil nil)))
284 (should (equal (org-parse-time-string "<2012-03-29>")
285 '(0 0 0 29 3 2012 nil nil nil)))
286 (should (equal (org-parse-time-string "<2012-03-29>" t)
287 '(0 nil nil 29 3 2012 nil nil nil))))
290 ;;; Drawers
292 (ert-deftest test-org/insert-property-drawer ()
293 "Test `org-insert-property-drawer' specifications."
294 ;; Error before first headline.
295 (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
296 ;; Insert drawer right after headline if there is no planning line,
297 ;; or after it otherwise.
298 (should
299 (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
300 (org-test-with-temp-text "* H\nParagraph<point>"
301 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
302 (buffer-string))))
303 (should
304 (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
305 (org-test-with-temp-text
306 "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
307 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
308 (buffer-string))))
309 ;; Indent inserted drawer.
310 (should
311 (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
312 (org-test-with-temp-text "* H\nParagraph<point>"
313 (let ((org-adapt-indentation t)) (org-insert-property-drawer))
314 (buffer-string))))
315 ;; Handle insertion at eob.
316 (should
317 (equal "* H\n:PROPERTIES:\n:END:\n"
318 (org-test-with-temp-text "* H"
319 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
320 (buffer-string))))
321 ;; Skip inlinetasks before point.
322 (when (featurep 'org-inlinetask)
323 (should
324 (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
325 (org-test-with-temp-text
326 "* H\n*************** I\n*************** END\nP<point>"
327 (let ((org-adapt-indentation nil)
328 (org-inlinetask-min-level 15))
329 (org-insert-property-drawer))
330 (buffer-string)))))
331 ;; Correctly set drawer in an inlinetask.
332 (when (featurep 'org-inlinetask)
333 (should
334 (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
335 (org-test-with-temp-text
336 "* H\n*************** I\nP<point>\n*************** END"
337 (let ((org-adapt-indentation nil)
338 (org-inlinetask-min-level 15))
339 (org-insert-property-drawer))
340 (buffer-string))))))
343 ;;; Filling
345 (ert-deftest test-org/fill-paragraph ()
346 "Test `org-fill-paragraph' specifications."
347 ;; At an Org table, align it.
348 (should
349 (equal "| a |\n"
350 (org-test-with-temp-text "|a|"
351 (org-fill-paragraph)
352 (buffer-string))))
353 (should
354 (equal "#+name: table\n| a |\n"
355 (org-test-with-temp-text "#+name: table\n| a |\n"
356 (org-fill-paragraph)
357 (buffer-string))))
358 ;; At a paragraph, preserve line breaks.
359 (org-test-with-temp-text "some \\\\\nlong\ntext"
360 (let ((fill-column 20))
361 (org-fill-paragraph)
362 (should (equal (buffer-string) "some \\\\\nlong text"))))
363 ;; Correctly fill a paragraph when point is at its very end.
364 (should
365 (equal "A B"
366 (org-test-with-temp-text "A\nB"
367 (let ((fill-column 20))
368 (goto-char (point-max))
369 (org-fill-paragraph)
370 (buffer-string)))))
371 ;; Correctly fill the last paragraph of a greater element.
372 (should
373 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
374 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
375 (let ((fill-column 8))
376 (forward-line)
377 (end-of-line)
378 (org-fill-paragraph)
379 (buffer-string)))))
380 ;; Correctly fill an element in a narrowed buffer.
381 (should
382 (equal "01234\n6"
383 (org-test-with-temp-text "01234 6789"
384 (let ((fill-column 5))
385 (narrow-to-region 1 8)
386 (org-fill-paragraph)
387 (buffer-string)))))
388 ;; Handle `adaptive-fill-regexp' in paragraphs.
389 (should
390 (equal "> a b"
391 (org-test-with-temp-text "> a\n> b"
392 (let ((fill-column 5)
393 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
394 (org-fill-paragraph)
395 (buffer-string)))))
396 ;; Special case: Fill first paragraph when point is at an item or
397 ;; a plain-list or a footnote reference.
398 (should
399 (equal "- A B"
400 (org-test-with-temp-text "- A\n B"
401 (let ((fill-column 20))
402 (org-fill-paragraph)
403 (buffer-string)))))
404 (should
405 (equal "[fn:1] A B"
406 (org-test-with-temp-text "[fn:1] A\nB"
407 (let ((fill-column 20))
408 (org-fill-paragraph)
409 (buffer-string)))))
410 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
411 (let ((fill-column 20))
412 (org-fill-paragraph)
413 (should (equal (buffer-string)
414 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
415 ;; Fill contents of `comment-block' elements.
416 (should
417 (equal
418 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
419 (let ((fill-column 20))
420 (forward-line)
421 (org-fill-paragraph)
422 (buffer-string)))
423 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
424 ;; Fill `comment' elements.
425 (should
426 (equal " # A B"
427 (org-test-with-temp-text " # A\n # B"
428 (let ((fill-column 20))
429 (org-fill-paragraph)
430 (buffer-string)))))
431 ;; Do not mix consecutive comments when filling one of them.
432 (should
433 (equal "# A B\n\n# C"
434 (org-test-with-temp-text "# A\n# B\n\n# C"
435 (let ((fill-column 20))
436 (org-fill-paragraph)
437 (buffer-string)))))
438 ;; Use commented empty lines as separators when filling comments.
439 (should
440 (equal "# A B\n#\n# C"
441 (org-test-with-temp-text "# A\n# B\n#\n# C"
442 (let ((fill-column 20))
443 (org-fill-paragraph)
444 (buffer-string)))))
445 ;; Handle `adaptive-fill-regexp' in comments.
446 (should
447 (equal "# > a b"
448 (org-test-with-temp-text "# > a\n# > b"
449 (let ((fill-column 20)
450 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
451 (org-fill-paragraph)
452 (buffer-string)))))
453 ;; Do nothing at affiliated keywords.
454 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
455 (let ((fill-column 20))
456 (org-fill-paragraph)
457 (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
458 ;; Do not move point after table when filling a table.
459 (should-not
460 (org-test-with-temp-text "| a | b |\n| c | d |\n"
461 (forward-char)
462 (org-fill-paragraph)
463 (eobp))))
465 (ert-deftest test-org/auto-fill-function ()
466 "Test auto-filling features."
467 ;; Auto fill paragraph.
468 (should
469 (equal "12345\n7890"
470 (org-test-with-temp-text "12345 7890"
471 (let ((fill-column 5))
472 (end-of-line)
473 (org-auto-fill-function)
474 (buffer-string)))))
475 ;; Auto fill first paragraph in an item.
476 (should
477 (equal "- 12345\n 7890"
478 (org-test-with-temp-text "- 12345 7890"
479 (let ((fill-column 7))
480 (end-of-line)
481 (org-auto-fill-function)
482 (buffer-string)))))
483 ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
484 (should
485 (equal "> 12345\n 7890"
486 (org-test-with-temp-text "> 12345 7890"
487 (let ((fill-column 10)
488 (adaptive-fill-regexp "[ \t]*>+[ \t]*")
489 (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
490 (end-of-line)
491 (org-auto-fill-function)
492 (buffer-string)))))
493 (should
494 (equal "> 12345\n> 12345\n> 7890"
495 (org-test-with-temp-text "> 12345\n> 12345 7890"
496 (let ((fill-column 10)
497 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
498 (goto-char (point-max))
499 (org-auto-fill-function)
500 (buffer-string)))))
501 (should-not
502 (equal " 12345\n *12345\n *12345"
503 (org-test-with-temp-text " 12345\n *12345 12345"
504 (let ((fill-column 10)
505 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
506 (goto-char (point-max))
507 (org-auto-fill-function)
508 (buffer-string)))))
509 ;; Auto fill comments.
510 (should
511 (equal " # 12345\n # 7890"
512 (org-test-with-temp-text " # 12345 7890"
513 (let ((fill-column 10))
514 (end-of-line)
515 (org-auto-fill-function)
516 (buffer-string)))))
517 ;; A hash within a line isn't a comment.
518 (should-not
519 (equal "12345 # 7890\n# 1"
520 (org-test-with-temp-text "12345 # 7890 1"
521 (let ((fill-column 12))
522 (end-of-line)
523 (org-auto-fill-function)
524 (buffer-string)))))
525 ;; Correctly interpret empty prefix.
526 (should-not
527 (equal "# a\n# b\nRegular\n# paragraph"
528 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
529 (let ((fill-column 12))
530 (end-of-line 3)
531 (org-auto-fill-function)
532 (buffer-string)))))
533 ;; Comment block: auto fill contents.
534 (should
535 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
536 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
537 (let ((fill-column 5))
538 (forward-line)
539 (end-of-line)
540 (org-auto-fill-function)
541 (buffer-string)))))
542 (should
543 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
544 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
545 (let ((fill-column 5))
546 (forward-line)
547 (end-of-line)
548 (org-auto-fill-function)
549 (buffer-string)))))
550 ;; Do not fill if a new item could be created.
551 (should-not
552 (equal "12345\n- 90"
553 (org-test-with-temp-text "12345 - 90"
554 (let ((fill-column 5))
555 (end-of-line)
556 (org-auto-fill-function)
557 (buffer-string)))))
558 ;; Do not fill if a line break could be introduced.
559 (should-not
560 (equal "123\\\\\n7890"
561 (org-test-with-temp-text "123\\\\ 7890"
562 (let ((fill-column 6))
563 (end-of-line)
564 (org-auto-fill-function)
565 (buffer-string)))))
566 ;; Do not fill affiliated keywords.
567 (should-not
568 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
569 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
570 (let ((fill-column 20))
571 (end-of-line)
572 (org-auto-fill-function)
573 (buffer-string))))))
577 ;;; Indentation
579 (ert-deftest test-org/indent-line ()
580 "Test `org-indent-line' specifications."
581 ;; Do not indent diary sexps, footnote definitions or headlines.
582 (should
583 (zerop
584 (org-test-with-temp-text "%%(org-calendar-holiday)"
585 (org-indent-line)
586 (org-get-indentation))))
587 (should
588 (zerop
589 (org-test-with-temp-text "[fn:1] fn"
590 (let ((org-adapt-indentation t)) (org-indent-line))
591 (org-get-indentation))))
592 (should
593 (zerop
594 (org-test-with-temp-text "* H"
595 (org-indent-line)
596 (org-get-indentation))))
597 ;; Do not indent before first headline.
598 (should
599 (zerop
600 (org-test-with-temp-text ""
601 (org-indent-line)
602 (org-get-indentation))))
603 ;; Indent according to headline level otherwise, unless
604 ;; `org-adapt-indentation' is nil.
605 (should
606 (= 2
607 (org-test-with-temp-text "* H\nA"
608 (forward-line)
609 (let ((org-adapt-indentation t)) (org-indent-line))
610 (org-get-indentation))))
611 (should
612 (= 2
613 (org-test-with-temp-text "* H\n\nA"
614 (forward-line)
615 (let ((org-adapt-indentation t)) (org-indent-line))
616 (org-get-indentation))))
617 (should
618 (zerop
619 (org-test-with-temp-text "* H\nA"
620 (forward-line)
621 (let ((org-adapt-indentation nil)) (org-indent-line))
622 (org-get-indentation))))
623 ;; Indenting preserves point position.
624 (should
625 (org-test-with-temp-text "* H\nAB"
626 (forward-line)
627 (forward-char)
628 (let ((org-adapt-indentation t)) (org-indent-line))
629 (looking-at "B")))
630 ;; Do not change indentation at an item.
631 (should
632 (= 1
633 (org-test-with-temp-text "* H\n - A"
634 (forward-line)
635 (let ((org-adapt-indentation t)) (org-indent-line))
636 (org-get-indentation))))
637 ;; On blank lines at the end of a list, indent like last element
638 ;; within it if the line is still in the list. If the last element
639 ;; is an item, indent like its contents. Otherwise, indent like the
640 ;; whole list.
641 (should
642 (= 4
643 (org-test-with-temp-text "* H\n- A\n - AA\n"
644 (goto-char (point-max))
645 (let ((org-adapt-indentation t)) (org-indent-line))
646 (org-get-indentation))))
647 (should
648 (zerop
649 (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
650 (goto-char (point-max))
651 (let ((org-adapt-indentation t)) (org-indent-line))
652 (org-get-indentation))))
653 (should
654 (= 4
655 (org-test-with-temp-text "* H\n- A\n - \n"
656 (goto-char (point-max))
657 (let ((org-adapt-indentation t)) (org-indent-line))
658 (org-get-indentation))))
659 ;; Likewise, on a blank line at the end of a footnote definition,
660 ;; indent at column 0 if line belongs to the definition. Otherwise,
661 ;; indent like the definition itself.
662 (should
663 (zerop
664 (org-test-with-temp-text "* H\n[fn:1] Definition\n"
665 (goto-char (point-max))
666 (let ((org-adapt-indentation t)) (org-indent-line))
667 (org-get-indentation))))
668 (should
669 (zerop
670 (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
671 (goto-char (point-max))
672 (let ((org-adapt-indentation t)) (org-indent-line))
673 (org-get-indentation))))
674 ;; After the end of the contents of a greater element, indent like
675 ;; the beginning of the element.
676 (should
677 (= 1
678 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
679 (forward-line 2)
680 (org-indent-line)
681 (org-get-indentation))))
682 ;; On blank lines after a paragraph, indent like its last non-empty
683 ;; line.
684 (should
685 (= 1
686 (org-test-with-temp-text " Paragraph\n\n<point>"
687 (org-indent-line)
688 (org-get-indentation))))
689 ;; At the first line of an element, indent like previous element's
690 ;; first line, ignoring footnotes definitions and inline tasks, or
691 ;; according to parent.
692 (should
693 (= 2
694 (org-test-with-temp-text "A\n\n B\n\nC"
695 (goto-char (point-max))
696 (org-indent-line)
697 (org-get-indentation))))
698 (should
699 (= 1
700 (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
701 (goto-char (point-max))
702 (org-indent-line)
703 (org-get-indentation))))
704 (should
705 (= 1
706 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
707 (forward-line 1)
708 (org-indent-line)
709 (org-get-indentation))))
710 ;; Within code part of a source block, use language major mode if
711 ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
712 ;; according to line above.
713 (should
714 (= 6
715 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
716 (forward-line 2)
717 (let ((org-src-tab-acts-natively t)
718 (org-edit-src-content-indentation 0))
719 (org-indent-line))
720 (org-get-indentation))))
721 (should
722 (= 1
723 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
724 (forward-line 2)
725 (let ((org-src-tab-acts-natively nil)
726 (org-edit-src-content-indentation 0))
727 (org-indent-line))
728 (org-get-indentation))))
729 ;; Otherwise, indent like the first non-blank line above.
730 (should
731 (zerop
732 (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
733 (forward-line 3)
734 (org-indent-line)
735 (org-get-indentation))))
736 ;; Align node properties according to `org-property-format'. Handle
737 ;; nicely empty values.
738 (should
739 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
740 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
741 (let ((org-property-format "%-10s %s")) (org-indent-line))
742 (buffer-string))))
743 (should
744 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
745 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
746 (let ((org-property-format "%-10s %s")) (org-indent-line))
747 (buffer-string)))))
749 (ert-deftest test-org/indent-region ()
750 "Test `org-indent-region' specifications."
751 ;; Indent paragraph.
752 (should
753 (equal "A\nB\nC"
754 (org-test-with-temp-text " A\nB\n C"
755 (org-indent-region (point-min) (point-max))
756 (buffer-string))))
757 ;; Indent greater elements along with their contents.
758 (should
759 (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
760 (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
761 (org-indent-region (point-min) (point-max))
762 (buffer-string))))
763 ;; Ignore contents of verse blocks and example blocks.
764 (should
765 (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
766 (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
767 (org-indent-region (point-min) (point-max))
768 (buffer-string))))
769 (should
770 (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
771 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
772 (org-indent-region (point-min) (point-max))
773 (buffer-string))))
774 ;; Indent according to mode if `org-src-tab-acts-natively' is
775 ;; non-nil. Otherwise, do not indent code at all.
776 (should
777 (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
778 (org-test-with-temp-text
779 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
780 (let ((org-src-tab-acts-natively t)
781 (org-edit-src-content-indentation 0))
782 (org-indent-region (point-min) (point-max)))
783 (buffer-string))))
784 (should
785 (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
786 (org-test-with-temp-text
787 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
788 (let ((org-src-tab-acts-natively nil)
789 (org-edit-src-content-indentation 0))
790 (org-indent-region (point-min) (point-max)))
791 (buffer-string))))
792 ;; Align node properties according to `org-property-format'. Handle
793 ;; nicely empty values.
794 (should
795 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
796 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
797 (let ((org-property-format "%-10s %s")
798 (org-adapt-indentation nil))
799 (org-indent-region (point) (point-max)))
800 (buffer-string))))
801 (should
802 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
803 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
804 (let ((org-property-format "%-10s %s")
805 (org-adapt-indentation nil))
806 (org-indent-region (point) (point-max)))
807 (buffer-string))))
808 ;; Indent plain lists.
809 (should
810 (equal "- A\n B\n - C\n\n D"
811 (org-test-with-temp-text "- A\n B\n - C\n\n D"
812 (org-indent-region (point-min) (point-max))
813 (buffer-string))))
814 (should
815 (equal "- A\n\n- B"
816 (org-test-with-temp-text " - A\n\n - B"
817 (org-indent-region (point-min) (point-max))
818 (buffer-string))))
819 ;; Indent footnote definitions.
820 (should
821 (equal "[fn:1] Definition\n\nDefinition"
822 (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
823 (org-indent-region (point-min) (point-max))
824 (buffer-string))))
825 ;; Special case: Start indenting on a blank line.
826 (should
827 (equal "\nParagraph"
828 (org-test-with-temp-text "\n Paragraph"
829 (org-indent-region (point-min) (point-max))
830 (buffer-string)))))
834 ;;; Editing
836 (ert-deftest test-org/delete-indentation ()
837 "Test `org-delete-indentation' specifications."
838 ;; Regular test.
839 (should (equal "foo bar"
840 (org-test-with-temp-text
841 "foo \n bar<point>"
842 (org-delete-indentation)
843 (buffer-string))))
844 ;; With optional argument.
845 (should (equal "foo bar"
846 (org-test-with-temp-text
847 "foo<point> \n bar"
848 (org-delete-indentation t)
849 (buffer-string))))
850 ;; At headline text should be appended to the headline text.
851 (should
852 (equal"* foo bar :tag:"
853 (let (org-auto-align-tags)
854 (org-test-with-temp-text
855 "* foo :tag:\n bar<point>"
856 (org-delete-indentation)
857 (buffer-string)))))
858 (should
859 (equal "* foo bar :tag:"
860 (let (org-auto-align-tags)
861 (org-test-with-temp-text
862 "* foo <point>:tag:\n bar"
863 (org-delete-indentation t)
864 (buffer-string))))))
866 (ert-deftest test-org/return ()
867 "Test `org-return' specifications."
868 ;; Regular test.
869 (should
870 (equal "Para\ngraph"
871 (org-test-with-temp-text "Para<point>graph"
872 (org-return)
873 (buffer-string))))
874 ;; With optional argument, indent line.
875 (should
876 (equal " Para\n graph"
877 (org-test-with-temp-text " Para<point>graph"
878 (org-return t)
879 (buffer-string))))
880 ;; On a table, call `org-table-next-row'.
881 (should
882 (org-test-with-temp-text "| <point>a |\n| b |"
883 (org-return)
884 (org-looking-at-p "b")))
885 ;; Open link or timestamp under point when `org-return-follows-link'
886 ;; is non-nil.
887 (should
888 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
889 (let ((org-return-follows-link t)
890 (org-link-search-must-match-exact-headline nil))
891 (org-return))
892 (org-looking-at-p "<<target>>")))
893 (should-not
894 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
895 (let ((org-return-follows-link nil)) (org-return))
896 (org-looking-at-p "<<target>>")))
897 (should
898 (org-test-with-temp-text "* [[b][a<point>]]\n* b"
899 (let ((org-return-follows-link t)) (org-return))
900 (org-looking-at-p "* b")))
901 (should
902 (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
903 (let ((org-return-follows-link t)
904 (org-link-search-must-match-exact-headline nil))
905 (org-return))
906 (org-looking-at-p "<<target>>")))
907 ;; When `org-return-follows-link' is non-nil, tolerate links and
908 ;; timestamps in comments, node properties, etc.
909 (should
910 (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
911 (let ((org-return-follows-link t)
912 (org-link-search-must-match-exact-headline nil))
913 (org-return))
914 (org-looking-at-p "<<target>>")))
915 ;; However, do not open link when point is in a table.
916 (should
917 (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
918 (let ((org-return-follows-link t)) (org-return))
919 (org-looking-at-p "between")))
920 ;; Special case: in a list, when indenting, do not break structure.
921 (should
922 (equal "- A\n B"
923 (org-test-with-temp-text "- A <point>B"
924 (org-return t)
925 (buffer-string))))
926 (should
927 (equal "- A\n\n- B"
928 (org-test-with-temp-text "- A\n<point>- B"
929 (org-return t)
930 (buffer-string))))
931 ;; On tags part of a headline, add a newline below it instead of
932 ;; breaking it.
933 (should
934 (equal "* H :tag:\n"
935 (org-test-with-temp-text "* H :<point>tag:"
936 (org-return)
937 (buffer-string))))
938 ;; Before headline text, add a newline below it instead of breaking
939 ;; it.
940 (should
941 (equal "* TODO H :tag:\n"
942 (org-test-with-temp-text "* <point>TODO H :tag:"
943 (org-return)
944 (buffer-string))))
945 (should
946 (equal "* TODO [#B] H :tag:\n"
947 (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
948 (org-return)
949 (buffer-string))))
950 ;; At headline text, break headline text but preserve tags.
951 (should
952 (equal "* TODO [#B] foo :tag:\nbar"
953 (let (org-auto-align-tags)
954 (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
955 (org-return)
956 (buffer-string)))))
957 ;; At bol of headline insert newline.
958 (should
959 (equal "\n* h"
960 (org-test-with-temp-text "<point>* h"
961 (org-return)
962 (buffer-string))))
963 ;; Refuse to leave invalid headline in buffer.
964 (should
965 (equal "* h\n"
966 (org-test-with-temp-text "*<point> h"
967 (org-return)
968 (buffer-string)))))
970 (ert-deftest test-org/meta-return ()
971 "Test M-RET (`org-meta-return') specifications."
972 ;; In a table field insert a row above.
973 (should
974 (org-test-with-temp-text "| a |"
975 (forward-char)
976 (org-meta-return)
977 (forward-line -1)
978 (looking-at "| |$")))
979 ;; In a paragraph change current line into a header.
980 (should
981 (org-test-with-temp-text "a"
982 (org-meta-return)
983 (beginning-of-line)
984 (looking-at "\* a$")))
985 ;; In an item insert an item, in this case above.
986 (should
987 (org-test-with-temp-text "- a"
988 (org-meta-return)
989 (beginning-of-line)
990 (looking-at "- $")))
991 ;; In a drawer and item insert an item, in this case above.
992 (should
993 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
994 (forward-line)
995 (org-meta-return)
996 (beginning-of-line)
997 (looking-at "- $"))))
999 (ert-deftest test-org/insert-heading ()
1000 "Test `org-insert-heading' specifications."
1001 ;; In an empty buffer, insert a new headline.
1002 (should
1003 (equal "* "
1004 (org-test-with-temp-text ""
1005 (org-insert-heading)
1006 (buffer-string))))
1007 ;; At the beginning of a line, turn it into a headline
1008 (should
1009 (equal "* P"
1010 (org-test-with-temp-text "<point>P"
1011 (org-insert-heading)
1012 (buffer-string))))
1013 ;; In the middle of a line, split the line if allowed, otherwise,
1014 ;; insert the headline at its end.
1015 (should
1016 (equal "Para\n* graph"
1017 (org-test-with-temp-text "Para<point>graph"
1018 (let ((org-M-RET-may-split-line '((default . t))))
1019 (org-insert-heading))
1020 (buffer-string))))
1021 (should
1022 (equal "Paragraph\n* "
1023 (org-test-with-temp-text "Para<point>graph"
1024 (let ((org-M-RET-may-split-line '((default . nil))))
1025 (org-insert-heading))
1026 (buffer-string))))
1027 ;; When on a list, insert an item instead, unless called with an
1028 ;; universal argument or if list is invisible. In this case, create
1029 ;; a new headline after contents.
1030 (should
1031 (equal "* H\n- item\n- "
1032 (org-test-with-temp-text "* H\n- item<point>"
1033 (let ((org-insert-heading-respect-content nil))
1034 (org-insert-heading))
1035 (buffer-string))))
1036 (should
1037 (equal "* H\n- item\n- item 2\n* "
1038 (org-test-with-temp-text "* H\n- item<point>\n- item 2"
1039 (let ((org-insert-heading-respect-content nil))
1040 (org-insert-heading '(4)))
1041 (buffer-string))))
1042 (should
1043 (equal "* H\n- item\n* "
1044 (org-test-with-temp-text "* H\n- item"
1045 (org-cycle)
1046 (goto-char (point-max))
1047 (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
1048 (buffer-string))))
1049 ;; When called with two universal arguments, insert a new headline
1050 ;; at the end of the grandparent subtree.
1051 (should
1052 (equal "* H1\n** H3\n- item\n** H2\n** "
1053 (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
1054 (let ((org-insert-heading-respect-content nil))
1055 (org-insert-heading '(16)))
1056 (buffer-string))))
1057 ;; When optional TOP-LEVEL argument is non-nil, always insert
1058 ;; a level 1 heading.
1059 (should
1060 (equal "* H1\n** H2\n* "
1061 (org-test-with-temp-text "* H1\n** H2<point>"
1062 (org-insert-heading nil nil t)
1063 (buffer-string))))
1064 (should
1065 (equal "* H1\n- item\n* "
1066 (org-test-with-temp-text "* H1\n- item<point>"
1067 (org-insert-heading nil nil t)
1068 (buffer-string))))
1069 ;; Corner case: correctly insert a headline after an empty one.
1070 (should
1071 (equal "* \n* "
1072 (org-test-with-temp-text "* <point>"
1073 (org-insert-heading)
1074 (buffer-string)))))
1076 (ert-deftest test-org/insert-todo-heading-respect-content ()
1077 "Test `org-insert-todo-heading-respect-content' specifications."
1078 ;; Create a TODO heading.
1079 (should
1080 (org-test-with-temp-text "* H1\n Body"
1081 (org-insert-todo-heading-respect-content)
1082 (nth 2 (org-heading-components))))
1083 ;; Add headline at the end of the first subtree
1084 (should
1085 (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
1086 (search-forward "H1Body")
1087 (org-insert-todo-heading-respect-content)
1088 (and (eobp) (org-at-heading-p))))
1089 ;; In a list, do not create a new item.
1090 (should
1091 (org-test-with-temp-text "* H\n- an item\n- another one"
1092 (search-forward "an ")
1093 (org-insert-todo-heading-respect-content)
1094 (and (eobp) (org-at-heading-p)))))
1096 (ert-deftest test-org/clone-with-time-shift ()
1097 "Test `org-clone-subtree-with-time-shift'."
1098 ;; Clone non-repeating once.
1099 (should
1100 (equal "\
1101 * H1\n<2015-06-21>
1102 * H1\n<2015-06-23>
1104 (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
1105 (org-clone-subtree-with-time-shift 1 "+2d")
1106 (replace-regexp-in-string
1107 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1108 nil nil 1))))
1109 ;; Clone repeating once.
1110 (should
1111 (equal "\
1112 * H1\n<2015-06-21>
1113 * H1\n<2015-06-23>
1114 * H1\n<2015-06-25 +1w>
1116 (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
1117 (org-clone-subtree-with-time-shift 1 "+2d")
1118 (replace-regexp-in-string
1119 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1120 nil nil 1))))
1121 ;; Clone non-repeating zero times.
1122 (should
1123 (equal "\
1124 * H1\n<2015-06-21>
1126 (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
1127 (org-clone-subtree-with-time-shift 0 "+2d")
1128 (replace-regexp-in-string
1129 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1130 nil nil 1))))
1131 ;; Clone repeating "zero" times.
1132 (should
1133 (equal "\
1134 * H1\n<2015-06-21>
1135 * H1\n<2015-06-23 +1w>
1137 (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
1138 (org-clone-subtree-with-time-shift 0 "+2d")
1139 (replace-regexp-in-string
1140 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1141 nil nil 1)))))
1144 ;;; Fixed-Width Areas
1146 (ert-deftest test-org/toggle-fixed-width ()
1147 "Test `org-toggle-fixed-width' specifications."
1148 ;; No region: Toggle on fixed-width marker in paragraphs.
1149 (should
1150 (equal ": A"
1151 (org-test-with-temp-text "A"
1152 (org-toggle-fixed-width)
1153 (buffer-string))))
1154 ;; No region: Toggle off fixed-width markers in fixed-width areas.
1155 (should
1156 (equal "A"
1157 (org-test-with-temp-text ": A"
1158 (org-toggle-fixed-width)
1159 (buffer-string))))
1160 ;; No region: Toggle on marker in blank lines after elements or just
1161 ;; after a headline.
1162 (should
1163 (equal "* H\n: "
1164 (org-test-with-temp-text "* H\n"
1165 (forward-line)
1166 (org-toggle-fixed-width)
1167 (buffer-string))))
1168 (should
1169 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
1170 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
1171 (goto-char (point-max))
1172 (org-toggle-fixed-width)
1173 (buffer-string))))
1174 ;; No region: Toggle on marker in front of one line elements (e.g.,
1175 ;; headlines, clocks)
1176 (should
1177 (equal ": * Headline"
1178 (org-test-with-temp-text "* Headline"
1179 (org-toggle-fixed-width)
1180 (buffer-string))))
1181 (should
1182 (equal ": #+KEYWORD: value"
1183 (org-test-with-temp-text "#+KEYWORD: value"
1184 (org-toggle-fixed-width)
1185 (buffer-string))))
1186 ;; No region: error in other situations.
1187 (should-error
1188 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
1189 (forward-line)
1190 (org-toggle-fixed-width)
1191 (buffer-string)))
1192 ;; No region: Indentation is preserved.
1193 (should
1194 (equal "- A\n : B"
1195 (org-test-with-temp-text "- A\n B"
1196 (forward-line)
1197 (org-toggle-fixed-width)
1198 (buffer-string))))
1199 ;; Region: If it contains only fixed-width elements and blank lines,
1200 ;; toggle off fixed-width markup.
1201 (should
1202 (equal "A\n\nB"
1203 (org-test-with-temp-text ": A\n\n: B"
1204 (transient-mark-mode 1)
1205 (push-mark (point) t t)
1206 (goto-char (point-max))
1207 (org-toggle-fixed-width)
1208 (buffer-string))))
1209 ;; Region: If it contains anything else, toggle on fixed-width but
1210 ;; not on fixed-width areas.
1211 (should
1212 (equal ": A\n: \n: B\n: \n: C"
1213 (org-test-with-temp-text "A\n\n: B\n\nC"
1214 (transient-mark-mode 1)
1215 (push-mark (point) t t)
1216 (goto-char (point-max))
1217 (org-toggle-fixed-width)
1218 (buffer-string))))
1219 ;; Region: Ignore blank lines at its end, unless it contains only
1220 ;; such lines.
1221 (should
1222 (equal ": A\n\n"
1223 (org-test-with-temp-text "A\n\n"
1224 (transient-mark-mode 1)
1225 (push-mark (point) t t)
1226 (goto-char (point-max))
1227 (org-toggle-fixed-width)
1228 (buffer-string))))
1229 (should
1230 (equal ": \n: \n"
1231 (org-test-with-temp-text "\n\n"
1232 (transient-mark-mode 1)
1233 (push-mark (point) t t)
1234 (goto-char (point-max))
1235 (org-toggle-fixed-width)
1236 (buffer-string)))))
1240 ;;; Headline
1242 (ert-deftest test-org/in-commented-heading-p ()
1243 "Test `org-in-commented-heading-p' specifications."
1244 ;; Commented headline.
1245 (should
1246 (org-test-with-temp-text "* COMMENT Headline\nBody"
1247 (goto-char (point-max))
1248 (org-in-commented-heading-p)))
1249 ;; Commented ancestor.
1250 (should
1251 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1252 (goto-char (point-max))
1253 (org-in-commented-heading-p)))
1254 ;; Comment keyword is case-sensitive.
1255 (should-not
1256 (org-test-with-temp-text "* Comment Headline\nBody"
1257 (goto-char (point-max))
1258 (org-in-commented-heading-p)))
1259 ;; Keyword is standalone.
1260 (should-not
1261 (org-test-with-temp-text "* COMMENTHeadline\nBody"
1262 (goto-char (point-max))
1263 (org-in-commented-heading-p)))
1264 ;; Optional argument.
1265 (should-not
1266 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1267 (goto-char (point-max))
1268 (org-in-commented-heading-p t))))
1270 (ert-deftest test-org/entry-blocked-p ()
1271 ;; Check other dependencies.
1272 (should
1273 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
1274 (let ((org-enforce-todo-dependencies t)
1275 (org-blocker-hook
1276 '(org-block-todo-from-children-or-siblings-or-parent)))
1277 (org-entry-blocked-p))))
1278 (should-not
1279 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
1280 (let ((org-enforce-todo-dependencies t)
1281 (org-blocker-hook
1282 '(org-block-todo-from-children-or-siblings-or-parent)))
1283 (org-entry-blocked-p))))
1284 ;; Entry without a TODO keyword or with a DONE keyword cannot be
1285 ;; blocked.
1286 (should-not
1287 (org-test-with-temp-text "* Blocked\n** TODO one"
1288 (let ((org-enforce-todo-dependencies t)
1289 (org-blocker-hook
1290 '(org-block-todo-from-children-or-siblings-or-parent)))
1291 (org-entry-blocked-p))))
1292 (should-not
1293 (org-test-with-temp-text "* DONE Blocked\n** TODO one"
1294 (let ((org-enforce-todo-dependencies t)
1295 (org-blocker-hook
1296 '(org-block-todo-from-children-or-siblings-or-parent)))
1297 (org-entry-blocked-p))))
1298 ;; Follow :ORDERED: specifications.
1299 (should
1300 (org-test-with-temp-text
1301 "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
1302 (let ((org-enforce-todo-dependencies t)
1303 (org-blocker-hook
1304 '(org-block-todo-from-children-or-siblings-or-parent)))
1305 (org-entry-blocked-p))))
1306 (should-not
1307 (org-test-with-temp-text
1308 "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
1309 (let ((org-enforce-todo-dependencies t)
1310 (org-blocker-hook
1311 '(org-block-todo-from-children-or-siblings-or-parent)))
1312 (org-entry-blocked-p)))))
1314 (ert-deftest test-org/format-outline-path ()
1315 (should
1316 (string= (org-format-outline-path (list "one" "two" "three"))
1317 "one/two/three"))
1318 ;; Empty path.
1319 (should
1320 (string= (org-format-outline-path '())
1321 ""))
1322 (should
1323 (string= (org-format-outline-path '(nil))
1324 ""))
1325 ;; Empty path and prefix.
1326 (should
1327 (string= (org-format-outline-path '() nil ">>")
1328 ">>"))
1329 ;; Trailing whitespace in headings.
1330 (should
1331 (string= (org-format-outline-path (list "one\t" "tw o " "three "))
1332 "one/tw o/three"))
1333 ;; Non-default prefix and separators.
1334 (should
1335 (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
1336 ">>|one|two|three"))
1337 ;; Truncate.
1338 (should
1339 (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
1340 "one/two/.."))
1341 ;; Give a very narrow width.
1342 (should
1343 (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
1344 "on"))
1345 ;; Give a prefix that extends beyond the width.
1346 (should
1347 (string= (org-format-outline-path (list "one" "two" "three" "four") 10
1348 ">>>>>>>>>>")
1349 ">>>>>>>>..")))
1352 ;;; Keywords
1354 (ert-deftest test-org/set-regexps-and-options ()
1355 "Test `org-set-regexps-and-options' specifications."
1356 ;; TAGS keyword.
1357 (should
1358 (equal '(("A" . ?a) ("B") ("C"))
1359 (org-test-with-temp-text "#+TAGS: A(a) B C"
1360 (org-mode-restart)
1361 org-tag-alist)))
1362 (should
1363 (equal '(("A") (:newline) ("B"))
1364 (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
1365 (org-mode-restart)
1366 org-tag-alist)))
1367 (should
1368 (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
1369 (org-test-with-temp-text "#+TAGS: { A B } C"
1370 (org-mode-restart)
1371 org-tag-alist)))
1372 (should
1373 (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
1374 (org-test-with-temp-text "#+TAGS: { A : B C }"
1375 (org-mode-restart)
1376 org-tag-alist)))
1377 (should
1378 (equal '(("A" "B" "C"))
1379 (org-test-with-temp-text "#+TAGS: { A : B C }"
1380 (org-mode-restart)
1381 org-tag-groups-alist)))
1382 (should
1383 (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
1384 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1385 (org-mode-restart)
1386 org-tag-alist)))
1387 (should
1388 (equal '(("A" "B" "C"))
1389 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1390 (org-mode-restart)
1391 org-tag-groups-alist)))
1392 ;; FILETAGS keyword.
1393 (should
1394 (equal '("A" "B" "C")
1395 (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
1396 (org-mode-restart)
1397 org-file-tags)))
1398 ;; PROPERTY keyword. Property names are case-insensitive.
1399 (should
1400 (equal "foo=1"
1401 (org-test-with-temp-text "#+PROPERTY: var foo=1"
1402 (org-mode-restart)
1403 (cdr (assoc "var" org-file-properties)))))
1404 (should
1405 (equal
1406 "foo=1 bar=2"
1407 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
1408 (org-mode-restart)
1409 (cdr (assoc "var" org-file-properties)))))
1410 (should
1411 (equal
1412 "foo=1 bar=2"
1413 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
1414 (org-mode-restart)
1415 (cdr (assoc "var" org-file-properties)))))
1416 ;; ARCHIVE keyword.
1417 (should
1418 (equal "%s_done::"
1419 (org-test-with-temp-text "#+ARCHIVE: %s_done::"
1420 (org-mode-restart)
1421 org-archive-location)))
1422 ;; CATEGORY keyword.
1423 (should
1424 (eq 'test
1425 (org-test-with-temp-text "#+CATEGORY: test"
1426 (org-mode-restart)
1427 org-category)))
1428 (should
1429 (equal "test"
1430 (org-test-with-temp-text "#+CATEGORY: test"
1431 (org-mode-restart)
1432 (cdr (assoc "CATEGORY" org-file-properties)))))
1433 ;; COLUMNS keyword.
1434 (should
1435 (equal "%25ITEM %TAGS %PRIORITY %TODO"
1436 (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
1437 (org-mode-restart)
1438 org-columns-default-format)))
1439 ;; CONSTANTS keyword. Constants names are case sensitive.
1440 (should
1441 (equal '("299792458." "3.14")
1442 (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
1443 (org-mode-restart)
1444 (mapcar
1445 (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
1446 '("c" "pi")))))
1447 (should
1448 (equal "3.14"
1449 (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
1450 (org-mode-restart)
1451 (cdr (assoc "pi" org-table-formula-constants-local)))))
1452 (should
1453 (equal "22/7"
1454 (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
1455 (org-mode-restart)
1456 (cdr (assoc "PI" org-table-formula-constants-local)))))
1457 ;; LINK keyword.
1458 (should
1459 (equal
1460 '("url1" "url2")
1461 (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
1462 (org-mode-restart)
1463 (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
1464 '("a" "b")))))
1465 ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
1466 (should
1467 (equal
1468 '(?X ?Z ?Y)
1469 (org-test-with-temp-text "#+PRIORITIES: X Z Y"
1470 (org-mode-restart)
1471 (list org-highest-priority org-lowest-priority org-default-priority))))
1472 (should
1473 (equal
1474 '(?A ?C ?B)
1475 (org-test-with-temp-text "#+PRIORITIES: X Z"
1476 (org-mode-restart)
1477 (list org-highest-priority org-lowest-priority org-default-priority))))
1478 ;; STARTUP keyword.
1479 (should
1480 (equal '(t t)
1481 (org-test-with-temp-text "#+STARTUP: fold odd"
1482 (org-mode-restart)
1483 (list org-startup-folded org-odd-levels-only))))
1484 ;; TODO keywords.
1485 (should
1486 (equal '(("A" "B") ("C"))
1487 (org-test-with-temp-text "#+TODO: A B | C"
1488 (org-mode-restart)
1489 (list org-not-done-keywords org-done-keywords))))
1490 (should
1491 (equal '(("A" "C") ("B" "D"))
1492 (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
1493 (org-mode-restart)
1494 (list org-not-done-keywords org-done-keywords))))
1495 (should
1496 (equal '(("A" "B") ("C"))
1497 (org-test-with-temp-text "#+TYP_TODO: A B | C"
1498 (org-mode-restart)
1499 (list org-not-done-keywords org-done-keywords))))
1500 (should
1501 (equal '((:startgroup) ("A" . ?a) (:endgroup))
1502 (org-test-with-temp-text "#+TODO: A(a)"
1503 (org-mode-restart)
1504 org-todo-key-alist)))
1505 (should
1506 (equal '(("D" note nil) ("C" time nil) ("B" note time))
1507 (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
1508 (org-mode-restart)
1509 org-todo-log-states)))
1510 ;; Enter SETUPFILE keyword.
1511 (should
1512 (equal "1"
1513 (org-test-with-temp-text
1514 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
1515 (org-mode-restart)
1516 (cdr (assoc "a" org-file-properties))))))
1520 ;;; Links
1522 ;;;; Coderefs
1524 (ert-deftest test-org/coderef ()
1525 "Test coderef links specifications."
1526 (should
1527 (org-test-with-temp-text "
1528 #+BEGIN_SRC emacs-lisp
1529 \(+ 1 1) (ref:sc)
1530 #+END_SRC
1531 \[[(sc)]]<point>"
1532 (org-open-at-point)
1533 (looking-at "(ref:sc)")))
1534 ;; Find coderef even with alternate label format.
1535 (should
1536 (org-test-with-temp-text "
1537 #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
1538 \(+ 1 1) {ref:sc}
1539 #+END_SRC
1540 \[[(sc)]]<point>"
1541 (org-open-at-point)
1542 (looking-at "{ref:sc}"))))
1544 ;;;; Custom ID
1546 (ert-deftest test-org/custom-id ()
1547 "Test custom ID links specifications."
1548 (should
1549 (org-test-with-temp-text
1550 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
1551 (org-open-at-point)
1552 (org-looking-at-p "\\* H1")))
1553 ;; Throw an error on false positives.
1554 (should-error
1555 (org-test-with-temp-text
1556 "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
1557 (org-open-at-point)
1558 (org-looking-at-p "\\* H1"))))
1560 ;;;; Fuzzy Links
1562 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
1563 ;; a named element (#+name: text) and to headlines (* Text).
1565 (ert-deftest test-org/fuzzy-links ()
1566 "Test fuzzy links specifications."
1567 ;; Fuzzy link goes in priority to a matching target.
1568 (should
1569 (org-test-with-temp-text
1570 "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
1571 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1572 (looking-at "<<Test>>")))
1573 ;; Then fuzzy link points to an element with a given name.
1574 (should
1575 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
1576 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1577 (looking-at "#\\+NAME: Test")))
1578 ;; A target still lead to a matching headline otherwise.
1579 (should
1580 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
1581 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1582 (looking-at "\\* Head2")))
1583 ;; With a leading star in link, enforce heading match.
1584 (should
1585 (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
1586 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1587 (looking-at "\\* Test")))
1588 ;; With a leading star in link, enforce exact heading match, even
1589 ;; with `org-link-search-must-match-exact-headline' set to nil.
1590 (should-error
1591 (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
1592 (let ((org-link-search-must-match-exact-headline nil))
1593 (org-open-at-point))))
1594 ;; Handle non-nil `org-link-search-must-match-exact-headline'.
1595 (should
1596 (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
1597 (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
1598 (looking-at "\\* Test")))
1599 (should
1600 (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
1601 (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
1602 (looking-at "\\* Test")))
1603 ;; Heading match should not care about spaces, cookies, TODO
1604 ;; keywords, priorities, and tags.
1605 (should
1606 (let ((first-line
1607 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1608 (org-test-with-temp-text
1609 (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
1610 (let ((org-link-search-must-match-exact-headline nil)
1611 (org-todo-regexp "TODO"))
1612 (org-open-at-point))
1613 (looking-at (regexp-quote first-line)))))
1614 ;; Heading match should still be exact.
1615 (should-error
1616 (let ((first-line
1617 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1618 (org-test-with-temp-text
1619 (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
1620 (let ((org-link-search-must-match-exact-headline nil)
1621 (org-todo-regexp "TODO"))
1622 (org-open-at-point)))))
1623 ;; Heading match ignores COMMENT keyword.
1624 (should
1625 (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
1626 (org-open-at-point)
1627 (looking-at "\\* COMMENT Test")))
1628 ;; Correctly un-hexify fuzzy links.
1629 (should
1630 (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
1631 (goto-char (point-max))
1632 (org-open-at-point)
1633 (bobp))))
1635 ;;;; Link Escaping
1637 (ert-deftest test-org/org-link-escape-ascii-character ()
1638 "Escape an ascii character."
1639 (should
1640 (string=
1641 "%5B"
1642 (org-link-escape "["))))
1644 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
1645 "Escape an ascii control character."
1646 (should
1647 (string=
1648 "%09"
1649 (org-link-escape "\t"))))
1651 (ert-deftest test-org/org-link-escape-multibyte-character ()
1652 "Escape an unicode multibyte character."
1653 (should
1654 (string=
1655 "%E2%82%AC"
1656 (org-link-escape "€"))))
1658 (ert-deftest test-org/org-link-escape-custom-table ()
1659 "Escape string with custom character table."
1660 (should
1661 (string=
1662 "Foo%3A%42ar%0A"
1663 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
1665 (ert-deftest test-org/org-link-escape-custom-table-merge ()
1666 "Escape string with custom table merged with default table."
1667 (should
1668 (string=
1669 "%5BF%6F%6F%3A%42ar%0A%5D"
1670 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
1672 (ert-deftest test-org/org-link-unescape-ascii-character ()
1673 "Unescape an ascii character."
1674 (should
1675 (string=
1677 (org-link-unescape "%5B"))))
1679 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
1680 "Unescpae an ascii control character."
1681 (should
1682 (string=
1683 "\n"
1684 (org-link-unescape "%0A"))))
1686 (ert-deftest test-org/org-link-unescape-multibyte-character ()
1687 "Unescape unicode multibyte character."
1688 (should
1689 (string=
1690 "€"
1691 (org-link-unescape "%E2%82%AC"))))
1693 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
1694 "Unescape old style percent escaped character."
1695 (should
1696 (string=
1697 "àâçèéêîôùû"
1698 (decode-coding-string
1699 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
1701 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
1702 "Escape and unescape a URL that includes an escaped char.
1703 http://article.gmane.org/gmane.emacs.orgmode/21459/"
1704 (should
1705 (string=
1706 "http://some.host.com/form?&id=blah%2Bblah25"
1707 (org-link-unescape
1708 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
1710 (ert-deftest test-org/org-link-escape-chars-browser ()
1711 "Test of the constant `org-link-escape-chars-browser'.
1712 See there why this test is a candidate to be removed once Org
1713 drops support for Emacs 24.1 and 24.2."
1714 (should
1715 (string=
1716 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1717 "%22Release%208.2%22&idxname=emacs-orgmode")
1718 (org-link-escape-browser ; Do not replace with `url-encode-url',
1719 ; see docstring above.
1720 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1721 "\"Release 8.2\"&idxname=emacs-orgmode")))))
1723 ;;;; Open at point
1725 (ert-deftest test-org/open-at-point-in-keyword ()
1726 "Does `org-open-at-point' open link in a keyword line?"
1727 (should
1728 (org-test-with-temp-text
1729 "#+KEYWORD: <point>[[info:emacs#Top]]"
1730 (org-open-at-point) t)))
1732 (ert-deftest test-org/open-at-point-in-property ()
1733 "Does `org-open-at-point' open link in property drawer?"
1734 (should
1735 (org-test-with-temp-text
1736 "* Headline
1737 :PROPERTIES:
1738 :URL: <point>[[info:emacs#Top]]
1739 :END:"
1740 (org-open-at-point) t)))
1742 (ert-deftest test-org/open-at-point-in-comment ()
1743 "Does `org-open-at-point' open link in a commented line?"
1744 (should
1745 (org-test-with-temp-text
1746 "# <point>[[info:emacs#Top]]"
1747 (org-open-at-point) t)))
1749 (ert-deftest test-org/open-at-point/info ()
1750 "Test `org-open-at-point' on info links."
1751 (should
1752 (org-test-with-temp-text
1753 "<point>[[info:emacs#Top]]"
1754 (org-open-at-point)
1755 (and (switch-to-buffer "*info*")
1756 (prog1
1757 (looking-at "\nThe Emacs Editor")
1758 (kill-buffer))))))
1760 (ert-deftest test-org/open-at-point/inline-image ()
1761 "Test `org-open-at-point' on nested links."
1762 (should
1763 (org-test-with-temp-text "[[info:org#Top][info:<point>emacs#Top]]"
1764 (org-open-at-point)
1765 (prog1 (with-current-buffer "*info*" (looking-at "\nOrg Mode Manual"))
1766 (kill-buffer "*info*")))))
1768 (ert-deftest test-org/open-at-point/radio-target ()
1769 "Test `org-open-at-point' on radio targets."
1770 (should
1771 (org-test-with-temp-text "<<<target>>> <point>target"
1772 (org-update-radio-target-regexp)
1773 (org-open-at-point)
1774 (eq (org-element-type (org-element-context)) 'radio-target))))
1777 ;;; Node Properties
1779 (ert-deftest test-org/accumulated-properties-in-drawers ()
1780 "Ensure properties accumulate in subtree drawers."
1781 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
1782 (org-babel-next-src-block)
1783 (should (equal '(2 1) (org-babel-execute-src-block)))))
1785 (ert-deftest test-org/custom-properties ()
1786 "Test custom properties specifications."
1787 ;; Standard test.
1788 (should
1789 (let ((org-custom-properties '("FOO")))
1790 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1791 (org-toggle-custom-properties-visibility)
1792 (org-invisible-p2))))
1793 ;; Properties are case-insensitive.
1794 (should
1795 (let ((org-custom-properties '("FOO")))
1796 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
1797 (org-toggle-custom-properties-visibility)
1798 (org-invisible-p2))))
1799 (should
1800 (let ((org-custom-properties '("foo")))
1801 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1802 (org-toggle-custom-properties-visibility)
1803 (org-invisible-p2))))
1804 ;; Multiple custom properties in the same drawer.
1805 (should
1806 (let ((org-custom-properties '("FOO" "BAR")))
1807 (org-test-with-temp-text
1808 "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
1809 (org-toggle-custom-properties-visibility)
1810 (and (org-invisible-p2)
1811 (not (progn (forward-line) (org-invisible-p2)))
1812 (progn (forward-line) (org-invisible-p2))))))
1813 ;; Hide custom properties with an empty value.
1814 (should
1815 (let ((org-custom-properties '("FOO")))
1816 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
1817 (org-toggle-custom-properties-visibility)
1818 (org-invisible-p2))))
1819 ;; Do not hide fake properties.
1820 (should-not
1821 (let ((org-custom-properties '("FOO")))
1822 (org-test-with-temp-text ":FOO: val\n"
1823 (org-toggle-custom-properties-visibility)
1824 (org-invisible-p2))))
1825 (should-not
1826 (let ((org-custom-properties '("A")))
1827 (org-test-with-temp-text
1828 "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
1829 (org-toggle-custom-properties-visibility)
1830 (org-invisible-p2)))))
1834 ;;; Mark Region
1836 (ert-deftest test-org/mark-subtree ()
1837 "Test `org-mark-subtree' specifications."
1838 ;; Error when point is before first headline.
1839 (should-error
1840 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
1841 (progn (transient-mark-mode 1)
1842 (org-mark-subtree))))
1843 ;; Without argument, mark current subtree.
1844 (should
1845 (equal
1846 '(12 32)
1847 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1848 (progn (transient-mark-mode 1)
1849 (forward-line 2)
1850 (org-mark-subtree)
1851 (list (region-beginning) (region-end))))))
1852 ;; With an argument, move ARG up.
1853 (should
1854 (equal
1855 '(1 32)
1856 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1857 (progn (transient-mark-mode 1)
1858 (forward-line 2)
1859 (org-mark-subtree 1)
1860 (list (region-beginning) (region-end))))))
1861 ;; Do not get fooled by inlinetasks.
1862 (when (featurep 'org-inlinetask)
1863 (should
1864 (= 1
1865 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
1866 (progn (transient-mark-mode 1)
1867 (forward-line 1)
1868 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
1869 (region-beginning)))))))
1873 ;;; Navigation
1875 (ert-deftest test-org/end-of-meta-data ()
1876 "Test `org-end-of-meta-data' specifications."
1877 ;; Skip planning line.
1878 (should
1879 (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
1880 (org-end-of-meta-data)
1881 (eobp)))
1882 ;; Skip properties drawer.
1883 (should
1884 (org-test-with-temp-text
1885 "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
1886 (org-end-of-meta-data)
1887 (eobp)))
1888 ;; Skip both.
1889 (should
1890 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
1891 (org-end-of-meta-data)
1892 (eobp)))
1893 ;; Nothing to skip, go to first line.
1894 (should
1895 (org-test-with-temp-text "* Headline\nContents"
1896 (org-end-of-meta-data)
1897 (looking-at "Contents")))
1898 ;; With option argument, skip empty lines, regular drawers and
1899 ;; clocking lines.
1900 (should
1901 (org-test-with-temp-text "* Headline\n\nContents"
1902 (org-end-of-meta-data t)
1903 (looking-at "Contents")))
1904 (should
1905 (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
1906 (org-end-of-meta-data t)
1907 (looking-at "Contents")))
1908 (should
1909 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
1910 (org-end-of-meta-data t)
1911 (looking-at "Contents")))
1912 ;; Special case: do not skip incomplete drawers.
1913 (should
1914 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
1915 (org-end-of-meta-data t)
1916 (looking-at ":LOGBOOK:")))
1917 ;; Special case: Be careful about consecutive headlines.
1918 (should-not
1919 (org-test-with-temp-text "* H1\n*H2\nContents"
1920 (org-end-of-meta-data t)
1921 (looking-at "Contents"))))
1923 (ert-deftest test-org/beginning-of-line ()
1924 "Test `org-beginning-of-line' specifications."
1925 ;; Standard test.
1926 (should
1927 (org-test-with-temp-text "Some text\nSome other text"
1928 (progn (org-beginning-of-line) (bolp))))
1929 ;; Standard test with `visual-line-mode'.
1930 (should-not
1931 (org-test-with-temp-text "A long line of text\nSome other text"
1932 (progn (visual-line-mode)
1933 (forward-char 2)
1934 (dotimes (i 1000) (insert "very "))
1935 (org-beginning-of-line)
1936 (bolp))))
1937 ;; At an headline with special movement.
1938 (should
1939 (org-test-with-temp-text "* TODO Headline"
1940 (let ((org-special-ctrl-a/e t))
1941 (org-end-of-line)
1942 (and (progn (org-beginning-of-line) (looking-at "Headline"))
1943 (progn (org-beginning-of-line) (bolp))
1944 (progn (org-beginning-of-line) (looking-at "Headline"))))))
1945 ;; Special case: Do not error when the buffer contains only a single
1946 ;; asterisk.
1947 (should
1948 (org-test-with-temp-text "*<point>"
1949 (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))))
1950 (should
1951 (org-test-with-temp-text "*<point>"
1952 (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line)))))
1954 (ert-deftest test-org/end-of-line ()
1955 "Test `org-end-of-line' specifications."
1956 ;; Standard test.
1957 (should
1958 (org-test-with-temp-text "Some text\nSome other text"
1959 (progn (org-end-of-line) (eolp))))
1960 ;; Standard test with `visual-line-mode'.
1961 (should-not
1962 (org-test-with-temp-text "A long line of text\nSome other text"
1963 (progn (visual-line-mode)
1964 (forward-char 2)
1965 (dotimes (i 1000) (insert "very "))
1966 (goto-char (point-min))
1967 (org-end-of-line)
1968 (eolp))))
1969 ;; At an headline with special movement.
1970 (should
1971 (org-test-with-temp-text "* Headline1 :tag:\n"
1972 (let ((org-special-ctrl-a/e t))
1973 (and (progn (org-end-of-line) (looking-at " :tag:"))
1974 (progn (org-end-of-line) (eolp))
1975 (progn (org-end-of-line) (looking-at " :tag:"))))))
1976 ;; At an headline without special movement.
1977 (should
1978 (org-test-with-temp-text "* Headline2 :tag:\n"
1979 (let ((org-special-ctrl-a/e nil))
1980 (and (progn (org-end-of-line) (eolp))
1981 (progn (org-end-of-line) (eolp))))))
1982 ;; At an headline, with reversed movement.
1983 (should
1984 (org-test-with-temp-text "* Headline3 :tag:\n"
1985 (let ((org-special-ctrl-a/e 'reversed)
1986 (this-command last-command))
1987 (and (progn (org-end-of-line) (eolp))
1988 (progn (org-end-of-line) (looking-at " :tag:"))))))
1989 ;; At a block without hidden contents.
1990 (should
1991 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1992 (progn (org-end-of-line) (eolp))))
1993 ;; At a block with hidden contents.
1994 (should-not
1995 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1996 (let ((org-special-ctrl-a/e t))
1997 (org-hide-block-toggle)
1998 (org-end-of-line)
1999 (eobp)))))
2001 (ert-deftest test-org/forward-sentence ()
2002 "Test `org-forward-sentence' specifications."
2003 ;; At the end of a table cell, move to the end of the next one.
2004 (should
2005 (org-test-with-temp-text "| a<point> | b |"
2006 (org-forward-sentence)
2007 (looking-at " |$")))
2008 ;; Elsewhere in a cell, move to its end.
2009 (should
2010 (org-test-with-temp-text "| a<point>c | b |"
2011 (org-forward-sentence)
2012 (looking-at " | b |$")))
2013 ;; Otherwise, simply call `forward-sentence'.
2014 (should
2015 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
2016 (org-forward-sentence)
2017 (looking-at " Sentence 2.")))
2018 (should
2019 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
2020 (org-forward-sentence)
2021 (org-forward-sentence)
2022 (eobp)))
2023 ;; At the end of an element, jump to the next one, without stopping
2024 ;; on blank lines in-between.
2025 (should
2026 (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
2027 (org-forward-sentence)
2028 (eobp))))
2030 (ert-deftest test-org/backward-sentence ()
2031 "Test `org-backward-sentence' specifications."
2032 ;; At the beginning of a table cell, move to the beginning of the
2033 ;; previous one.
2034 (should
2035 (org-test-with-temp-text "| a | <point>b |"
2036 (org-backward-sentence)
2037 (looking-at "a | b |$")))
2038 ;; Elsewhere in a cell, move to its beginning.
2039 (should
2040 (org-test-with-temp-text "| a | b<point>c |"
2041 (org-backward-sentence)
2042 (looking-at "bc |$")))
2043 ;; Otherwise, simply call `backward-sentence'.
2044 (should
2045 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
2046 (org-backward-sentence)
2047 (looking-at "Sentence 2.")))
2048 (should
2049 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
2050 (org-backward-sentence)
2051 (org-backward-sentence)
2052 (bobp)))
2053 ;; Make sure to hit the beginning of a sentence on the same line as
2054 ;; an item.
2055 (should
2056 (org-test-with-temp-text "- Line 1\n line <point>2."
2057 (org-backward-sentence)
2058 (looking-at "Line 1"))))
2060 (ert-deftest test-org/forward-paragraph ()
2061 "Test `org-forward-paragraph' specifications."
2062 ;; At end of buffer, return an error.
2063 (should-error
2064 (org-test-with-temp-text "Paragraph"
2065 (goto-char (point-max))
2066 (org-forward-paragraph)))
2067 ;; Standard test.
2068 (should
2069 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2070 (org-forward-paragraph)
2071 (looking-at "P2")))
2072 ;; Ignore depth.
2073 (should
2074 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
2075 (org-forward-paragraph)
2076 (looking-at "P1")))
2077 ;; Do not enter elements with invisible contents.
2078 (should
2079 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
2080 (org-hide-block-toggle)
2081 (org-forward-paragraph)
2082 (looking-at "P3")))
2083 ;; On an affiliated keyword, jump to the beginning of the element.
2084 (should
2085 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
2086 (org-forward-paragraph)
2087 (looking-at "Para")))
2088 ;; On an item or a footnote definition, move to the second element
2089 ;; inside, if any.
2090 (should
2091 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
2092 (org-forward-paragraph)
2093 (looking-at " Paragraph")))
2094 (should
2095 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
2096 (org-forward-paragraph)
2097 (looking-at "Paragraph")))
2098 ;; On an item, or a footnote definition, when the first line is
2099 ;; empty, move to the first item.
2100 (should
2101 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
2102 (org-forward-paragraph)
2103 (looking-at " Paragraph")))
2104 (should
2105 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
2106 (org-forward-paragraph)
2107 (looking-at "Paragraph")))
2108 ;; On a table (resp. a property drawer) do not move through table
2109 ;; rows (resp. node properties).
2110 (should
2111 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
2112 (org-forward-paragraph)
2113 (looking-at "Paragraph")))
2114 (should
2115 (org-test-with-temp-text
2116 "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
2117 (org-forward-paragraph)
2118 (looking-at "Paragraph")))
2119 ;; On a verse or source block, stop after blank lines.
2120 (should
2121 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
2122 (org-forward-paragraph)
2123 (looking-at "L2")))
2124 (should
2125 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
2126 (org-forward-paragraph)
2127 (looking-at "L2"))))
2129 (ert-deftest test-org/backward-paragraph ()
2130 "Test `org-backward-paragraph' specifications."
2131 ;; Error at beginning of buffer.
2132 (should-error
2133 (org-test-with-temp-text "Paragraph"
2134 (org-backward-paragraph)))
2135 ;; Regular test.
2136 (should
2137 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2138 (goto-char (point-max))
2139 (org-backward-paragraph)
2140 (looking-at "P3")))
2141 (should
2142 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2143 (goto-char (point-max))
2144 (beginning-of-line)
2145 (org-backward-paragraph)
2146 (looking-at "P2")))
2147 ;; Ignore depth.
2148 (should
2149 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
2150 (goto-char (point-max))
2151 (beginning-of-line)
2152 (org-backward-paragraph)
2153 (looking-at "P2")))
2154 ;; Ignore invisible elements.
2155 (should
2156 (org-test-with-temp-text "* H1\n P1\n* H2"
2157 (org-cycle)
2158 (goto-char (point-max))
2159 (beginning-of-line)
2160 (org-backward-paragraph)
2161 (bobp)))
2162 ;; On an affiliated keyword, jump to the first one.
2163 (should
2164 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
2165 (search-forward "c2")
2166 (org-backward-paragraph)
2167 (looking-at "#\\+name")))
2168 ;; On the second element in an item or a footnote definition, jump
2169 ;; to item or the definition.
2170 (should
2171 (org-test-with-temp-text "- line1\n\n line2"
2172 (goto-char (point-max))
2173 (beginning-of-line)
2174 (org-backward-paragraph)
2175 (looking-at "- line1")))
2176 (should
2177 (org-test-with-temp-text "[fn:1] line1\n\n line2"
2178 (goto-char (point-max))
2179 (beginning-of-line)
2180 (org-backward-paragraph)
2181 (looking-at "\\[fn:1\\] line1")))
2182 ;; On a table (resp. a property drawer), ignore table rows
2183 ;; (resp. node properties).
2184 (should
2185 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
2186 (goto-char (point-max))
2187 (beginning-of-line)
2188 (org-backward-paragraph)
2189 (bobp)))
2190 (should
2191 (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
2192 (org-backward-paragraph)
2193 (looking-at ":PROPERTIES:")))
2194 ;; On a source or verse block, stop before blank lines.
2195 (should
2196 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
2197 (search-forward "L3")
2198 (beginning-of-line)
2199 (org-backward-paragraph)
2200 (looking-at "L2")))
2201 (should
2202 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
2203 (search-forward "L3")
2204 (beginning-of-line)
2205 (org-backward-paragraph)
2206 (looking-at "L2"))))
2208 (ert-deftest test-org/forward-element ()
2209 "Test `org-forward-element' specifications."
2210 ;; 1. At EOB: should error.
2211 (org-test-with-temp-text "Some text\n"
2212 (goto-char (point-max))
2213 (should-error (org-forward-element)))
2214 ;; 2. Standard move: expected to ignore blank lines.
2215 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
2216 (org-forward-element)
2217 (should (looking-at (regexp-quote "Second paragraph."))))
2218 ;; 3. Headline tests.
2219 (org-test-with-temp-text "
2220 * Head 1
2221 ** Head 1.1
2222 *** Head 1.1.1
2223 ** Head 1.2"
2224 ;; 3.1. At an headline beginning: move to next headline at the
2225 ;; same level.
2226 (goto-line 3)
2227 (org-forward-element)
2228 (should (looking-at (regexp-quote "** Head 1.2")))
2229 ;; 3.2. At an headline beginning: move to parent headline if no
2230 ;; headline at the same level.
2231 (goto-line 3)
2232 (org-forward-element)
2233 (should (looking-at (regexp-quote "** Head 1.2"))))
2234 ;; 4. Greater element tests.
2235 (org-test-with-temp-text
2236 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
2237 ;; 4.1. At a greater element: expected to skip contents.
2238 (org-forward-element)
2239 (should (looking-at (regexp-quote "Outside.")))
2240 ;; 4.2. At the end of greater element contents: expected to skip
2241 ;; to the end of the greater element.
2242 (goto-line 2)
2243 (org-forward-element)
2244 (should (looking-at (regexp-quote "Outside."))))
2245 ;; 5. List tests.
2246 (org-test-with-temp-text "
2247 - item1
2249 - sub1
2251 - sub2
2253 - sub3
2255 Inner paragraph.
2257 - item2
2259 Outside."
2260 ;; 5.1. At list top point: expected to move to the element after
2261 ;; the list.
2262 (goto-line 2)
2263 (org-forward-element)
2264 (should (looking-at (regexp-quote "Outside.")))
2265 ;; 5.2. Special case: at the first line of a sub-list, but not at
2266 ;; beginning of line, move to next item.
2267 (goto-line 2)
2268 (forward-char)
2269 (org-forward-element)
2270 (should (looking-at "- item2"))
2271 (goto-line 4)
2272 (forward-char)
2273 (org-forward-element)
2274 (should (looking-at " - sub2"))
2275 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
2276 (goto-line 4)
2277 (org-forward-element)
2278 (should (looking-at (regexp-quote " Inner paragraph.")))
2279 ;; 5.4. At sub-list end: expected to move outside the sub-list.
2280 (goto-line 8)
2281 (org-forward-element)
2282 (should (looking-at (regexp-quote " Inner paragraph.")))
2283 ;; 5.5. At an item: expected to move to next item, if any.
2284 (goto-line 6)
2285 (org-forward-element)
2286 (should (looking-at " - sub3"))))
2288 (ert-deftest test-org/backward-element ()
2289 "Test `org-backward-element' specifications."
2290 ;; 1. Should error at BOB.
2291 (org-test-with-temp-text " \nParagraph."
2292 (should-error (org-backward-element)))
2293 ;; 2. Should move at BOB when called on the first element in buffer.
2294 (should
2295 (org-test-with-temp-text "\n#+TITLE: test"
2296 (progn (forward-line)
2297 (org-backward-element)
2298 (bobp))))
2299 ;; 3. Not at the beginning of an element: move at its beginning.
2300 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2301 (goto-line 3)
2302 (end-of-line)
2303 (org-backward-element)
2304 (should (looking-at (regexp-quote "Paragraph2."))))
2305 ;; 4. Headline tests.
2306 (org-test-with-temp-text "
2307 * Head 1
2308 ** Head 1.1
2309 *** Head 1.1.1
2310 ** Head 1.2"
2311 ;; 4.1. At an headline beginning: move to previous headline at the
2312 ;; same level.
2313 (goto-line 5)
2314 (org-backward-element)
2315 (should (looking-at (regexp-quote "** Head 1.1")))
2316 ;; 4.2. At an headline beginning: move to parent headline if no
2317 ;; headline at the same level.
2318 (goto-line 3)
2319 (org-backward-element)
2320 (should (looking-at (regexp-quote "* Head 1")))
2321 ;; 4.3. At the first top-level headline: should error.
2322 (goto-line 2)
2323 (should-error (org-backward-element)))
2324 ;; 5. At beginning of first element inside a greater element:
2325 ;; expected to move to greater element's beginning.
2326 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
2327 (goto-line 3)
2328 (org-backward-element)
2329 (should (looking-at "#\\+BEGIN_CENTER")))
2330 ;; 6. At the beginning of the first element in a section: should
2331 ;; move back to headline, if any.
2332 (should
2333 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
2334 (progn (goto-char (point-max))
2335 (beginning-of-line)
2336 (org-backward-element)
2337 (org-at-heading-p))))
2338 ;; 7. List tests.
2339 (org-test-with-temp-text "
2340 - item1
2342 - sub1
2344 - sub2
2346 - sub3
2348 Inner paragraph.
2350 - item2
2353 Outside."
2354 ;; 7.1. At beginning of sub-list: expected to move to the
2355 ;; paragraph before it.
2356 (goto-line 4)
2357 (org-backward-element)
2358 (should (looking-at "item1"))
2359 ;; 7.2. At an item in a list: expected to move at previous item.
2360 (goto-line 8)
2361 (org-backward-element)
2362 (should (looking-at " - sub2"))
2363 (goto-line 12)
2364 (org-backward-element)
2365 (should (looking-at "- item1"))
2366 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
2367 ;; beginning.
2368 (goto-line 10)
2369 (org-backward-element)
2370 (should (looking-at " - sub1"))
2371 (goto-line 15)
2372 (org-backward-element)
2373 (should (looking-at "- item1"))
2374 ;; 7.4. At blank-lines before list end: expected to move to top
2375 ;; item.
2376 (goto-line 14)
2377 (org-backward-element)
2378 (should (looking-at "- item1"))))
2380 (ert-deftest test-org/up-element ()
2381 "Test `org-up-element' specifications."
2382 ;; 1. At BOB or with no surrounding element: should error.
2383 (org-test-with-temp-text "Paragraph."
2384 (should-error (org-up-element)))
2385 (org-test-with-temp-text "* Head1\n* Head2"
2386 (goto-line 2)
2387 (should-error (org-up-element)))
2388 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2389 (goto-line 3)
2390 (should-error (org-up-element)))
2391 ;; 2. At an headline: move to parent headline.
2392 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
2393 (goto-line 3)
2394 (org-up-element)
2395 (should (looking-at "\\* Head1")))
2396 ;; 3. Inside a greater element: move to greater element beginning.
2397 (org-test-with-temp-text
2398 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
2399 (goto-line 3)
2400 (org-up-element)
2401 (should (looking-at "#\\+BEGIN_CENTER")))
2402 ;; 4. List tests.
2403 (org-test-with-temp-text "* Top
2404 - item1
2406 - sub1
2408 - sub2
2410 Paragraph within sub2.
2412 - item2"
2413 ;; 4.1. Within an item: move to the item beginning.
2414 (goto-line 8)
2415 (org-up-element)
2416 (should (looking-at " - sub2"))
2417 ;; 4.2. At an item in a sub-list: move to parent item.
2418 (goto-line 4)
2419 (org-up-element)
2420 (should (looking-at "- item1"))
2421 ;; 4.3. At an item in top list: move to beginning of whole list.
2422 (goto-line 10)
2423 (org-up-element)
2424 (should (looking-at "- item1"))
2425 ;; 4.4. Special case. At very top point: should move to parent of
2426 ;; list.
2427 (goto-line 2)
2428 (org-up-element)
2429 (should (looking-at "\\* Top"))))
2431 (ert-deftest test-org/down-element ()
2432 "Test `org-down-element' specifications."
2433 ;; Error when the element hasn't got a recursive type.
2434 (org-test-with-temp-text "Paragraph."
2435 (should-error (org-down-element)))
2436 ;; Error when the element has no contents
2437 (org-test-with-temp-text "* Headline"
2438 (should-error (org-down-element)))
2439 ;; When at a plain-list, move to first item.
2440 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
2441 (goto-line 2)
2442 (org-down-element)
2443 (should (looking-at " - Item 1.1")))
2444 (org-test-with-temp-text "#+NAME: list\n- Item 1"
2445 (org-down-element)
2446 (should (looking-at " Item 1")))
2447 ;; When at a table, move to first row
2448 (org-test-with-temp-text "#+NAME: table\n| a | b |"
2449 (org-down-element)
2450 (should (looking-at " a | b |")))
2451 ;; Otherwise, move inside the greater element.
2452 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
2453 (org-down-element)
2454 (should (looking-at "Paragraph"))))
2456 (ert-deftest test-org/drag-element-backward ()
2457 "Test `org-drag-element-backward' specifications."
2458 ;; Standard test.
2459 (should
2460 (equal
2461 "#+key2: val2\n#+key1: val1\n#+key3: val3"
2462 (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
2463 (org-drag-element-backward)
2464 (buffer-string))))
2465 (should
2466 (equal
2467 "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
2468 (org-test-with-temp-text
2469 "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
2470 (org-drag-element-backward)
2471 (buffer-string))))
2472 ;; Preserve blank lines.
2473 (should
2474 (equal "Paragraph 2\n\n\nPara1\n\nPara3"
2475 (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
2476 (org-drag-element-backward)
2477 (buffer-string))))
2478 ;; Preserve column.
2479 (should
2480 (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
2481 (org-drag-element-backward)
2482 (org-looking-at-p "2")))
2483 ;; Error when trying to move first element of buffer.
2484 (should-error
2485 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2486 (org-drag-element-backward)))
2487 ;; Error when trying to swap nested elements.
2488 (should-error
2489 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2490 (forward-line)
2491 (org-drag-element-backward)))
2492 ;; Error when trying to swap an headline element and a non-headline
2493 ;; element.
2494 (should-error
2495 (org-test-with-temp-text "Test.\n* Head 1"
2496 (forward-line)
2497 (org-drag-element-backward)))
2498 ;; Preserve visibility of elements and their contents.
2499 (should
2500 (equal '((63 . 82) (26 . 48))
2501 (org-test-with-temp-text "
2502 #+BEGIN_CENTER
2503 Text.
2504 #+END_CENTER
2505 - item 1
2506 #+BEGIN_QUOTE
2507 Text.
2508 #+END_QUOTE"
2509 (while (search-forward "BEGIN_" nil t) (org-cycle))
2510 (search-backward "- item 1")
2511 (org-drag-element-backward)
2512 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2513 (overlays-in (point-min) (point-max)))))))
2515 (ert-deftest test-org/drag-element-forward ()
2516 "Test `org-drag-element-forward' specifications."
2517 ;; 1. Error when trying to move first element of buffer.
2518 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2519 (goto-line 3)
2520 (should-error (org-drag-element-forward)))
2521 ;; 2. Error when trying to swap nested elements.
2522 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2523 (forward-line)
2524 (should-error (org-drag-element-forward)))
2525 ;; 3. Error when trying to swap a non-headline element and an
2526 ;; headline.
2527 (org-test-with-temp-text "Test.\n* Head 1"
2528 (should-error (org-drag-element-forward)))
2529 ;; 4. Otherwise, swap elements, preserving column and blank lines
2530 ;; between elements.
2531 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
2532 (search-forward "graph")
2533 (org-drag-element-forward)
2534 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
2535 (should (looking-at " 1")))
2536 ;; 5. Preserve visibility of elements and their contents.
2537 (org-test-with-temp-text "
2538 #+BEGIN_CENTER
2539 Text.
2540 #+END_CENTER
2541 - item 1
2542 #+BEGIN_QUOTE
2543 Text.
2544 #+END_QUOTE"
2545 (while (search-forward "BEGIN_" nil t) (org-cycle))
2546 (search-backward "#+BEGIN_CENTER")
2547 (org-drag-element-forward)
2548 (should
2549 (equal
2550 '((63 . 82) (26 . 48))
2551 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2552 (overlays-in (point-min) (point-max)))))))
2554 (ert-deftest test-org/next-block ()
2555 "Test `org-next-block' specifications."
2556 ;; Regular test.
2557 (should
2558 (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
2559 (org-next-block 1)
2560 (looking-at "#\\+BEGIN_CENTER")))
2561 ;; Ignore case.
2562 (should
2563 (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
2564 (let ((case-fold-search nil))
2565 (org-next-block 1)
2566 (looking-at "#\\+begin_center"))))
2567 ;; Ignore current line.
2568 (should
2569 (org-test-with-temp-text
2570 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
2571 (org-next-block 1)
2572 (looking-at "#\\+BEGIN_CENTER")))
2573 ;; Throw an error when no block is found.
2574 (should-error
2575 (org-test-with-temp-text "Paragraph"
2576 (org-next-block 1)))
2577 ;; With an argument, skip many blocks at once.
2578 (should
2579 (org-test-with-temp-text
2580 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
2581 (org-next-block 2)
2582 (looking-at "#\\+BEGIN_QUOTE")))
2583 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
2584 (should
2585 (org-test-with-temp-text
2586 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
2587 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
2588 (looking-at "#\\+BEGIN_QUOTE")))
2589 ;; Optional argument is also case-insensitive.
2590 (should
2591 (org-test-with-temp-text
2592 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
2593 (let ((case-fold-search nil))
2594 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
2595 (looking-at "#\\+begin_quote")))))
2597 (ert-deftest test-org/previous-block ()
2598 "Test `org-previous-block' specifications."
2599 ;; Regular test.
2600 (should
2601 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
2602 (org-previous-block 1)
2603 (looking-at "#\\+BEGIN_CENTER")))
2604 ;; Ignore case.
2605 (should
2606 (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
2607 (let ((case-fold-search nil))
2608 (org-previous-block 1)
2609 (looking-at "#\\+begin_center"))))
2610 ;; Ignore current line.
2611 (should
2612 (org-test-with-temp-text
2613 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
2614 (org-previous-block 1)
2615 (looking-at "#\\+BEGIN_QUOTE")))
2616 ;; Throw an error when no block is found.
2617 (should-error
2618 (org-test-with-temp-text "Paragraph<point>"
2619 (org-previous-block 1)))
2620 ;; With an argument, skip many blocks at once.
2621 (should
2622 (org-test-with-temp-text
2623 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
2624 (org-previous-block 2)
2625 (looking-at "#\\+BEGIN_CENTER")))
2626 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
2627 (should
2628 (org-test-with-temp-text
2629 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
2630 (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
2631 (looking-at "#\\+BEGIN_QUOTE")))
2632 ;; Optional argument is also case-insensitive.
2633 (should
2634 (org-test-with-temp-text
2635 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
2636 (let ((case-fold-search nil))
2637 (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
2638 (looking-at "#\\+begin_quote")))))
2641 ;;; Outline structure
2643 (ert-deftest test-org/demote ()
2644 "Test `org-demote' specifications."
2645 ;; Add correct number of stars according to `org-odd-levels-only'.
2646 (should
2647 (= 2
2648 (org-test-with-temp-text "* H"
2649 (let ((org-odd-levels-only nil)) (org-demote))
2650 (org-current-level))))
2651 (should
2652 (= 3
2653 (org-test-with-temp-text "* H"
2654 (let ((org-odd-levels-only t)) (org-demote))
2655 (org-current-level))))
2656 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2657 (should
2658 (org-test-with-temp-text "* H :tag:"
2659 (let ((org-tags-column 10)
2660 (org-auto-align-tags t)
2661 (org-odd-levels-only nil))
2662 (org-demote))
2663 (org-move-to-column 10)
2664 (org-looking-at-p ":tag:$")))
2665 (should-not
2666 (org-test-with-temp-text "* H :tag:"
2667 (let ((org-tags-column 10)
2668 (org-auto-align-tags nil)
2669 (org-odd-levels-only nil))
2670 (org-demote))
2671 (org-move-to-column 10)
2672 (org-looking-at-p ":tag:$")))
2673 ;; When `org-adapt-indentation' is non-nil, always indent planning
2674 ;; info and property drawers accordingly.
2675 (should
2676 (= 3
2677 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2678 (let ((org-odd-levels-only nil)
2679 (org-adapt-indentation t))
2680 (org-demote))
2681 (forward-line)
2682 (org-get-indentation))))
2683 (should
2684 (= 3
2685 (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2686 (let ((org-odd-levels-only nil)
2687 (org-adapt-indentation t))
2688 (org-demote))
2689 (forward-line)
2690 (org-get-indentation))))
2691 (should-not
2692 (= 3
2693 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2694 (let ((org-odd-levels-only nil)
2695 (org-adapt-indentation nil))
2696 (org-demote))
2697 (forward-line)
2698 (org-get-indentation))))
2699 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2700 ;; section accordingly. Ignore, however, footnote definitions and
2701 ;; inlinetasks boundaries.
2702 (should
2703 (= 3
2704 (org-test-with-temp-text "* H\n Paragraph"
2705 (let ((org-odd-levels-only nil)
2706 (org-adapt-indentation t))
2707 (org-demote))
2708 (forward-line)
2709 (org-get-indentation))))
2710 (should
2711 (= 2
2712 (org-test-with-temp-text "* H\n Paragraph"
2713 (let ((org-odd-levels-only nil)
2714 (org-adapt-indentation nil))
2715 (org-demote))
2716 (forward-line)
2717 (org-get-indentation))))
2718 (should
2719 (zerop
2720 (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
2721 (let ((org-odd-levels-only nil)
2722 (org-adapt-indentation t))
2723 (org-demote))
2724 (goto-char (point-max))
2725 (org-get-indentation))))
2726 (should
2727 (= 3
2728 (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
2729 (let ((org-odd-levels-only nil)
2730 (org-adapt-indentation t))
2731 (org-demote))
2732 (goto-char (point-max))
2733 (org-get-indentation))))
2734 (when (featurep 'org-inlinetask)
2735 (should
2736 (zerop
2737 (let ((org-inlinetask-min-level 5)
2738 (org-adapt-indentation t))
2739 (org-test-with-temp-text "* H\n***** I\n***** END"
2740 (org-demote)
2741 (forward-line)
2742 (org-get-indentation))))))
2743 (when (featurep 'org-inlinetask)
2744 (should
2745 (= 3
2746 (let ((org-inlinetask-min-level 5)
2747 (org-adapt-indentation t))
2748 (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
2749 (org-demote)
2750 (forward-line 2)
2751 (org-get-indentation))))))
2752 ;; Ignore contents of source blocks or example blocks when
2753 ;; indentation should be preserved (through
2754 ;; `org-src-preserve-indentation' or "-i" flag).
2755 (should-not
2756 (zerop
2757 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2758 (let ((org-adapt-indentation t)
2759 (org-src-preserve-indentation nil))
2760 (org-demote))
2761 (forward-line 2)
2762 (org-get-indentation))))
2763 (should
2764 (zerop
2765 (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2766 (let ((org-adapt-indentation t)
2767 (org-src-preserve-indentation t))
2768 (org-demote))
2769 (forward-line 2)
2770 (org-get-indentation))))
2771 (should
2772 (zerop
2773 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2774 (let ((org-adapt-indentation t)
2775 (org-src-preserve-indentation t))
2776 (org-demote))
2777 (forward-line 2)
2778 (org-get-indentation))))
2779 (should
2780 (zerop
2781 (org-test-with-temp-text
2782 "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
2783 (let ((org-adapt-indentation t)
2784 (org-src-preserve-indentation nil))
2785 (org-demote))
2786 (forward-line 2)
2787 (org-get-indentation)))))
2789 (ert-deftest test-org/promote ()
2790 "Test `org-promote' specifications."
2791 ;; Return an error if headline is to be promoted to level 0, unless
2792 ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
2793 ;; headline becomes a comment.
2794 (should-error
2795 (org-test-with-temp-text "* H"
2796 (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
2797 (should
2798 (equal "# H"
2799 (org-test-with-temp-text "* H"
2800 (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
2801 (buffer-string))))
2802 ;; Remove correct number of stars according to
2803 ;; `org-odd-levels-only'.
2804 (should
2805 (= 2
2806 (org-test-with-temp-text "*** H"
2807 (let ((org-odd-levels-only nil)) (org-promote))
2808 (org-current-level))))
2809 (should
2810 (= 1
2811 (org-test-with-temp-text "*** H"
2812 (let ((org-odd-levels-only t)) (org-promote))
2813 (org-current-level))))
2814 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2815 (should
2816 (org-test-with-temp-text "** H :tag:"
2817 (let ((org-tags-column 10)
2818 (org-auto-align-tags t)
2819 (org-odd-levels-only nil))
2820 (org-promote))
2821 (org-move-to-column 10)
2822 (org-looking-at-p ":tag:$")))
2823 (should-not
2824 (org-test-with-temp-text "** H :tag:"
2825 (let ((org-tags-column 10)
2826 (org-auto-align-tags nil)
2827 (org-odd-levels-only nil))
2828 (org-promote))
2829 (org-move-to-column 10)
2830 (org-looking-at-p ":tag:$")))
2831 ;; When `org-adapt-indentation' is non-nil, always indent planning
2832 ;; info and property drawers.
2833 (should
2834 (= 2
2835 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2836 (let ((org-odd-levels-only nil)
2837 (org-adapt-indentation t))
2838 (org-promote))
2839 (forward-line)
2840 (org-get-indentation))))
2841 (should
2842 (= 2
2843 (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2844 (let ((org-odd-levels-only nil)
2845 (org-adapt-indentation t))
2846 (org-promote))
2847 (forward-line)
2848 (org-get-indentation))))
2849 (should-not
2850 (= 2
2851 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2852 (let ((org-odd-levels-only nil)
2853 (org-adapt-indentation nil))
2854 (org-promote))
2855 (forward-line)
2856 (org-get-indentation))))
2857 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2858 ;; section accordingly. Ignore, however, footnote definitions and
2859 ;; inlinetasks boundaries.
2860 (should
2861 (= 2
2862 (org-test-with-temp-text "** H\n Paragraph"
2863 (let ((org-odd-levels-only nil)
2864 (org-adapt-indentation t))
2865 (org-promote))
2866 (forward-line)
2867 (org-get-indentation))))
2868 (should-not
2869 (= 2
2870 (org-test-with-temp-text "** H\n Paragraph"
2871 (let ((org-odd-levels-only nil)
2872 (org-adapt-indentation nil))
2873 (org-promote))
2874 (forward-line)
2875 (org-get-indentation))))
2876 (should
2877 (= 2
2878 (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
2879 (let ((org-odd-levels-only nil)
2880 (org-adapt-indentation t))
2881 (org-promote))
2882 (forward-line)
2883 (org-get-indentation))))
2884 (when (featurep 'org-inlinetask)
2885 (should
2886 (zerop
2887 (let ((org-inlinetask-min-level 5)
2888 (org-adapt-indentation t))
2889 (org-test-with-temp-text "** H\n***** I\n***** END"
2890 (org-promote)
2891 (forward-line)
2892 (org-get-indentation))))))
2893 (when (featurep 'org-inlinetask)
2894 (should
2895 (= 2
2896 (let ((org-inlinetask-min-level 5)
2897 (org-adapt-indentation t))
2898 (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
2899 (org-promote)
2900 (forward-line 2)
2901 (org-get-indentation))))))
2902 ;; Give up shifting if it would break document's structure
2903 ;; otherwise.
2904 (should
2905 (= 3
2906 (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
2907 (let ((org-odd-levels-only nil)
2908 (org-adapt-indentation t))
2909 (org-promote))
2910 (forward-line)
2911 (org-get-indentation))))
2912 (should
2913 (= 3
2914 (org-test-with-temp-text "** H\n Paragraph\n * list."
2915 (let ((org-odd-levels-only nil)
2916 (org-adapt-indentation t))
2917 (org-promote))
2918 (forward-line)
2919 (org-get-indentation))))
2920 ;; Ignore contents of source blocks or example blocks when
2921 ;; indentation should be preserved (through
2922 ;; `org-src-preserve-indentation' or "-i" flag).
2923 (should-not
2924 (zerop
2925 (org-test-with-temp-text
2926 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2927 (let ((org-adapt-indentation t)
2928 (org-src-preserve-indentation nil)
2929 (org-odd-levels-only nil))
2930 (org-promote))
2931 (forward-line)
2932 (org-get-indentation))))
2933 (should
2934 (zerop
2935 (org-test-with-temp-text
2936 "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
2937 (let ((org-adapt-indentation t)
2938 (org-src-preserve-indentation t)
2939 (org-odd-levels-only nil))
2940 (org-promote))
2941 (forward-line)
2942 (org-get-indentation))))
2943 (should
2944 (zerop
2945 (org-test-with-temp-text
2946 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2947 (let ((org-adapt-indentation t)
2948 (org-src-preserve-indentation t)
2949 (org-odd-levels-only nil))
2950 (org-promote))
2951 (forward-line)
2952 (org-get-indentation))))
2953 (should
2954 (zerop
2955 (org-test-with-temp-text
2956 "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
2957 (let ((org-adapt-indentation t)
2958 (org-src-preserve-indentation nil)
2959 (org-odd-levels-only nil))
2960 (org-promote))
2961 (forward-line)
2962 (org-get-indentation)))))
2965 ;;; Planning
2967 (ert-deftest test-org/at-planning-p ()
2968 "Test `org-at-planning-p' specifications."
2969 ;; Regular test.
2970 (should
2971 (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
2972 (org-at-planning-p)))
2973 (should-not
2974 (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
2975 (org-at-planning-p)))
2976 ;; Correctly find planning attached to inlinetasks.
2977 (when (featurep 'org-inlinetask)
2978 (should
2979 (org-test-with-temp-text
2980 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
2981 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2982 (should-not
2983 (org-test-with-temp-text
2984 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
2985 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2986 (should-not
2987 (org-test-with-temp-text
2988 "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
2989 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2990 (should-not
2991 (org-test-with-temp-text
2992 "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
2993 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
2995 (ert-deftest test-org/add-planning-info ()
2996 "Test `org-add-planning-info'."
2997 ;; Create deadline when `org-adapt-indentation' is non-nil.
2998 (should
2999 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3000 (org-test-with-temp-text "* H\nParagraph<point>"
3001 (let ((org-adapt-indentation t))
3002 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3003 (replace-regexp-in-string
3004 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3005 nil nil 1))))
3006 ;; Create deadline when `org-adapt-indentation' is nil.
3007 (should
3008 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3009 (org-test-with-temp-text "* H\nParagraph<point>"
3010 (let ((org-adapt-indentation nil))
3011 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3012 (replace-regexp-in-string
3013 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3014 nil nil 1))))
3015 ;; Update deadline when `org-adapt-indentation' is non-nil.
3016 (should
3017 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3018 (org-test-with-temp-text "\
3020 DEADLINE: <2015-06-24 Wed>
3021 Paragraph<point>"
3022 (let ((org-adapt-indentation t))
3023 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3024 (replace-regexp-in-string
3025 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3026 nil nil 1))))
3027 ;; Update deadline when `org-adapt-indentation' is nil.
3028 (should
3029 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3030 (org-test-with-temp-text "\
3032 DEADLINE: <2015-06-24 Wed>
3033 Paragraph<point>"
3034 (let ((org-adapt-indentation nil))
3035 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3036 (replace-regexp-in-string
3037 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3038 nil nil 1))))
3039 ;; Schedule when `org-adapt-indentation' is non-nil.
3040 (should
3041 (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
3042 (org-test-with-temp-text "* H\nParagraph<point>"
3043 (let ((org-adapt-indentation t))
3044 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
3045 (replace-regexp-in-string
3046 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3047 nil nil 1))))
3048 ;; Schedule when `org-adapt-indentation' is nil.
3049 (should
3050 (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
3051 (org-test-with-temp-text "* H\nParagraph<point>"
3052 (let ((org-adapt-indentation nil))
3053 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
3054 (replace-regexp-in-string
3055 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3056 nil nil 1))))
3057 ;; Add deadline when scheduled.
3058 (should
3059 (equal "\
3061 DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
3062 Paragraph"
3063 (org-test-with-temp-text "\
3065 SCHEDULED: <2015-06-24 Wed>
3066 Paragraph<point>"
3067 (let ((org-adapt-indentation t))
3068 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3069 (replace-regexp-in-string
3070 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3071 nil nil 1))))
3072 ;; Remove middle entry.
3073 (should
3074 (equal "\
3076 CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
3077 Paragraph"
3078 (org-test-with-temp-text "\
3080 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
3081 Paragraph<point>"
3082 (let ((org-adapt-indentation t))
3083 (org-add-planning-info nil nil 'deadline))
3084 (replace-regexp-in-string
3085 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
3086 nil nil 1))))
3087 ;; Remove last entry and then middle entry (order should not
3088 ;; matter).
3089 (should
3090 (equal "\
3092 CLOSED: [2015-06-24]
3093 Paragraph"
3094 (org-test-with-temp-text "\
3096 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
3097 Paragraph<point>"
3098 (let ((org-adapt-indentation t))
3099 (org-add-planning-info nil nil 'scheduled 'deadline))
3100 (replace-regexp-in-string
3101 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
3102 nil nil 1))))
3103 ;; Remove closed when `org-adapt-indentation' is non-nil.
3104 (should
3105 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3106 (org-test-with-temp-text "\
3108 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
3109 Paragraph<point>"
3110 (let ((org-adapt-indentation t))
3111 (org-add-planning-info nil nil 'closed))
3112 (replace-regexp-in-string
3113 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3114 nil nil 1))))
3115 (should
3116 (equal "* H\n Paragraph"
3117 (org-test-with-temp-text "\
3119 CLOSED: [2015-06-25 Thu]
3120 Paragraph<point>"
3121 (let ((org-adapt-indentation t))
3122 (org-add-planning-info nil nil 'closed))
3123 (replace-regexp-in-string
3124 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3125 nil nil 1))))
3126 ;; Remove closed when `org-adapt-indentation' is nil.
3127 (should
3128 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3129 (org-test-with-temp-text "\
3131 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
3132 Paragraph<point>"
3133 (let ((org-adapt-indentation nil))
3134 (org-add-planning-info nil nil 'closed))
3135 (replace-regexp-in-string
3136 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3137 nil nil 1))))
3138 (should
3139 (equal "* H\nParagraph"
3140 (org-test-with-temp-text "\
3142 CLOSED: [2015-06-25 Thu]
3143 Paragraph<point>"
3144 (let ((org-adapt-indentation nil))
3145 (org-add-planning-info nil nil 'closed))
3146 (replace-regexp-in-string
3147 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3148 nil nil 1))))
3149 ;; Remove closed entry and delete empty line.
3150 (should
3151 (equal "\
3153 Paragraph"
3154 (org-test-with-temp-text "\
3156 CLOSED: [2015-06-24 Wed]
3157 Paragraph<point>"
3158 (let ((org-adapt-indentation t))
3159 (org-add-planning-info nil nil 'closed))
3160 (replace-regexp-in-string
3161 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3162 nil nil 1))))
3163 ;; Remove one entry and update another.
3164 (should
3165 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3166 (org-test-with-temp-text "\
3168 SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
3169 Paragraph<point>"
3170 (let ((org-adapt-indentation t))
3171 (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
3172 (replace-regexp-in-string
3173 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3174 nil nil 1)))))
3177 ;;; Property API
3179 (ert-deftest test-org/buffer-property-keys ()
3180 "Test `org-buffer-property-keys' specifications."
3181 ;; Retrieve properties accross siblings.
3182 (should
3183 (equal '("A" "B")
3184 (org-test-with-temp-text "
3185 * H1
3186 :PROPERTIES:
3187 :A: 1
3188 :END:
3189 * H2
3190 :PROPERTIES:
3191 :B: 1
3192 :END:"
3193 (org-buffer-property-keys))))
3194 ;; Retrieve properties accross children.
3195 (should
3196 (equal '("A" "B")
3197 (org-test-with-temp-text "
3198 * H1
3199 :PROPERTIES:
3200 :A: 1
3201 :END:
3202 ** H2
3203 :PROPERTIES:
3204 :B: 1
3205 :END:"
3206 (org-buffer-property-keys))))
3207 ;; Retrieve muliple properties in the same drawer.
3208 (should
3209 (equal '("A" "B")
3210 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3211 (org-buffer-property-keys))))
3212 ;; Ignore extension symbol in property name.
3213 (should
3214 (equal '("A")
3215 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
3216 (org-buffer-property-keys))))
3217 ;; With non-nil COLUMNS, extract property names from columns.
3218 (should
3219 (equal '("A" "B")
3220 (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
3221 (org-buffer-property-keys nil nil t))))
3222 (should
3223 (equal '("A" "B" "COLUMNS")
3224 (org-test-with-temp-text
3225 "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
3226 (org-buffer-property-keys nil nil t)))))
3228 (ert-deftest test-org/property-values ()
3229 "Test `org-property-values' specifications."
3230 ;; Regular test.
3231 (should
3232 (equal '("2" "1")
3233 (org-test-with-temp-text
3234 "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
3235 (org-property-values "A"))))
3236 ;; Ignore empty values.
3237 (should-not
3238 (org-test-with-temp-text
3239 "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
3240 (org-property-values "A")))
3241 ;; Take into consideration extended values.
3242 (should
3243 (equal '("1 2")
3244 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
3245 (org-property-values "A")))))
3247 (ert-deftest test-org/find-property ()
3248 "Test `org-find-property' specifications."
3249 ;; Regular test.
3250 (should
3251 (= 1
3252 (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
3253 (org-find-property "prop"))))
3254 ;; Ignore false positives.
3255 (should
3256 (= 27
3257 (org-test-with-temp-text
3258 "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
3259 (org-find-property "A"))))
3260 ;; Return first entry found in buffer.
3261 (should
3262 (= 1
3263 (org-test-with-temp-text
3264 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
3265 (org-find-property "A"))))
3266 ;; Only search visible part of the buffer.
3267 (should
3268 (= 31
3269 (org-test-with-temp-text
3270 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
3271 (org-narrow-to-subtree)
3272 (org-find-property "A"))))
3273 ;; With optional argument, only find entries with a specific value.
3274 (should-not
3275 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3276 (org-find-property "A" "2")))
3277 (should
3278 (= 31
3279 (org-test-with-temp-text
3280 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
3281 (org-find-property "A" "2"))))
3282 ;; Use "nil" for explicit nil values.
3283 (should
3284 (= 31
3285 (org-test-with-temp-text
3286 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
3287 (org-find-property "A" "nil")))))
3289 (ert-deftest test-org/entry-delete ()
3290 "Test `org-entry-delete' specifications."
3291 ;; Regular test.
3292 (should
3293 (string-match
3294 " *:PROPERTIES:\n *:B: +2\n *:END:"
3295 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3296 (org-entry-delete (point) "A")
3297 (buffer-string))))
3298 ;; Also remove accumulated properties.
3299 (should-not
3300 (string-match
3301 ":A"
3302 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
3303 (org-entry-delete (point) "A")
3304 (buffer-string))))
3305 ;; When last property is removed, remove the property drawer.
3306 (should-not
3307 (string-match
3308 ":PROPERTIES:"
3309 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
3310 (org-entry-delete (point) "A")
3311 (buffer-string))))
3312 ;; Return a non-nil value when some property was removed.
3313 (should
3314 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3315 (org-entry-delete (point) "A")))
3316 (should-not
3317 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3318 (org-entry-delete (point) "C"))))
3320 (ert-deftest test-org/entry-get ()
3321 "Test `org-entry-get' specifications."
3322 ;; Regular test.
3323 (should
3324 (equal "1"
3325 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3326 (org-entry-get (point) "A"))))
3327 ;; Ignore case.
3328 (should
3329 (equal "1"
3330 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3331 (org-entry-get (point) "a"))))
3332 ;; Handle extended values, both before and after base value.
3333 (should
3334 (equal "1 2 3"
3335 (org-test-with-temp-text
3336 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
3337 (org-entry-get (point) "A"))))
3338 ;; Empty values are returned as the empty string.
3339 (should
3340 (equal ""
3341 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
3342 (org-entry-get (point) "A"))))
3343 ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
3344 ;; otherwise, return nil.
3345 (should-not
3346 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
3347 (org-entry-get (point) "A")))
3348 (should
3349 (equal "nil"
3350 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
3351 (org-entry-get (point) "A" nil t))))
3352 ;; Return nil when no property is found, independently on the
3353 ;; LITERAL-NIL argument.
3354 (should-not
3355 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3356 (org-entry-get (point) "B")))
3357 (should-not
3358 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3359 (org-entry-get (point) "B" nil t)))
3360 ;; Handle inheritance, when allowed. Include extended values and
3361 ;; possibly global values.
3362 (should
3363 (equal
3365 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3366 (org-entry-get (point) "A" t))))
3367 (should
3368 (equal
3370 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3371 (let ((org-use-property-inheritance t))
3372 (org-entry-get (point) "A" 'selective)))))
3373 (should-not
3374 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3375 (let ((org-use-property-inheritance nil))
3376 (org-entry-get (point) "A" 'selective))))
3377 (should
3378 (equal
3379 "1 2"
3380 (org-test-with-temp-text
3381 "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
3382 (org-entry-get (point-max) "A" t))))
3383 (should
3384 (equal "1"
3385 (org-test-with-temp-text
3386 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
3387 (org-mode-restart)
3388 (org-entry-get (point-max) "A" t))))
3389 (should
3390 (equal "0 1"
3391 (org-test-with-temp-text
3392 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
3393 (org-mode-restart)
3394 (org-entry-get (point-max) "A" t)))))
3396 (ert-deftest test-org/entry-properties ()
3397 "Test `org-entry-properties' specifications."
3398 ;; Get "ITEM" property.
3399 (should
3400 (equal "* H"
3401 (org-test-with-temp-text "* TODO H"
3402 (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
3403 (should
3404 (equal "* H"
3405 (org-test-with-temp-text "* TODO H"
3406 (cdr (assoc "ITEM" (org-entry-properties))))))
3407 ;; Get "TODO" property. TODO keywords are case sensitive.
3408 (should
3409 (equal "TODO"
3410 (org-test-with-temp-text "* TODO H"
3411 (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
3412 (should
3413 (equal "TODO"
3414 (org-test-with-temp-text "* TODO H"
3415 (cdr (assoc "TODO" (org-entry-properties))))))
3416 (should-not
3417 (org-test-with-temp-text "* H"
3418 (assoc "TODO" (org-entry-properties nil "TODO"))))
3419 (should-not
3420 (org-test-with-temp-text "* todo H"
3421 (assoc "TODO" (org-entry-properties nil "TODO"))))
3422 ;; Get "PRIORITY" property.
3423 (should
3424 (equal "A"
3425 (org-test-with-temp-text "* [#A] H"
3426 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
3427 (should
3428 (equal "A"
3429 (org-test-with-temp-text "* [#A] H"
3430 (cdr (assoc "PRIORITY" (org-entry-properties))))))
3431 (should
3432 (equal (char-to-string org-default-priority)
3433 (org-test-with-temp-text "* H"
3434 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
3435 ;; Get "FILE" property.
3436 (should
3437 (org-test-with-temp-text-in-file "* H\nParagraph"
3438 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
3439 (buffer-file-name))))
3440 (should
3441 (org-test-with-temp-text-in-file "* H\nParagraph"
3442 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
3443 (buffer-file-name))))
3444 (should-not
3445 (org-test-with-temp-text "* H\nParagraph"
3446 (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
3447 ;; Get "TAGS" property.
3448 (should
3449 (equal ":tag1:tag2:"
3450 (org-test-with-temp-text "* H :tag1:tag2:"
3451 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
3452 (should
3453 (equal ":tag1:tag2:"
3454 (org-test-with-temp-text "* H :tag1:tag2:"
3455 (cdr (assoc "TAGS" (org-entry-properties))))))
3456 (should-not
3457 (org-test-with-temp-text "* H"
3458 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
3459 ;; Get "ALLTAGS" property.
3460 (should
3461 (equal ":tag1:tag2:"
3462 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
3463 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
3464 (should
3465 (equal ":tag1:tag2:"
3466 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
3467 (cdr (assoc "ALLTAGS" (org-entry-properties))))))
3468 (should-not
3469 (org-test-with-temp-text "* H"
3470 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
3471 ;; Get "BLOCKED" property.
3472 (should
3473 (equal "t"
3474 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
3475 (let ((org-enforce-todo-dependencies t)
3476 (org-blocker-hook
3477 '(org-block-todo-from-children-or-siblings-or-parent)))
3478 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
3479 (should
3480 (equal ""
3481 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
3482 (let ((org-enforce-todo-dependencies t)
3483 (org-blocker-hook
3484 '(org-block-todo-from-children-or-siblings-or-parent)))
3485 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
3486 ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
3487 (should
3488 (equal
3489 "[2012-03-29 thu.]"
3490 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
3491 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
3492 (should
3493 (equal
3494 "[2012-03-29 thu.]"
3495 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
3496 (cdr (assoc "CLOSED" (org-entry-properties))))))
3497 (should-not
3498 (org-test-with-temp-text "* H"
3499 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
3500 (should
3501 (equal
3502 "<2014-03-04 tue.>"
3503 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3504 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
3505 (should
3506 (equal
3507 "<2014-03-04 tue.>"
3508 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3509 (cdr (assoc "DEADLINE" (org-entry-properties))))))
3510 (should-not
3511 (org-test-with-temp-text "* H"
3512 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
3513 (should
3514 (equal
3515 "<2014-03-04 tue.>"
3516 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3517 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
3518 (should
3519 (equal
3520 "<2014-03-04 tue.>"
3521 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3522 (cdr (assoc "SCHEDULED" (org-entry-properties))))))
3523 (should-not
3524 (org-test-with-temp-text "* H"
3525 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
3526 ;; Get "CATEGORY"
3527 (should
3528 (equal "cat"
3529 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
3530 (cdr (assoc "CATEGORY" (org-entry-properties))))))
3531 (should
3532 (equal "cat"
3533 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
3534 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3535 (should
3536 (equal "cat"
3537 (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
3538 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3539 (should
3540 (equal "cat2"
3541 (org-test-with-temp-text
3542 (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
3543 "\n"
3544 "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
3545 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3546 ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
3547 (should
3548 (equal "<2012-03-29 thu.>"
3549 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
3550 (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
3551 (should
3552 (equal "[2012-03-29 thu.]"
3553 (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
3554 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
3555 (should
3556 (equal "<2012-03-29 thu.>"
3557 (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
3558 (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
3559 (should
3560 (equal "[2014-03-04 tue.]"
3561 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
3562 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties nil "TIMESTAMP_IA"))))))
3563 ;; Get standard properties.
3564 (should
3565 (equal "1"
3566 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3567 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3568 ;; Handle extended properties.
3569 (should
3570 (equal "1 2 3"
3571 (org-test-with-temp-text
3572 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
3573 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3574 (should
3575 (equal "1 2 3"
3576 (org-test-with-temp-text
3577 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
3578 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3579 ;; Ignore forbidden (special) properties.
3580 (should-not
3581 (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
3582 (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
3584 (ert-deftest test-org/entry-put ()
3585 "Test `org-entry-put' specifications."
3586 ;; Error when not a string or nil.
3587 (should-error
3588 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
3589 (org-entry-put 1 "test" 2)))
3590 ;; Set "TODO" property.
3591 (should
3592 (string-match (regexp-quote " TODO H")
3593 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
3594 (org-entry-put (point) "TODO" "TODO")
3595 (buffer-string))))
3596 (should
3597 (string-match (regexp-quote "* H")
3598 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
3599 (org-entry-put (point) "TODO" nil)
3600 (buffer-string))))
3601 ;; Set "PRIORITY" property.
3602 (should
3603 (equal "* [#A] H"
3604 (org-test-with-temp-text "* [#B] H"
3605 (org-entry-put (point) "PRIORITY" "A")
3606 (buffer-string))))
3607 (should
3608 (equal "* H"
3609 (org-test-with-temp-text "* [#B] H"
3610 (org-entry-put (point) "PRIORITY" nil)
3611 (buffer-string))))
3612 ;; Set "SCHEDULED" property.
3613 (should
3614 (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
3615 (org-test-with-temp-text "* H"
3616 (org-entry-put (point) "SCHEDULED" "2014-03-04")
3617 (buffer-string))))
3618 (should
3619 (string= "* H\n"
3620 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3621 (org-entry-put (point) "SCHEDULED" nil)
3622 (buffer-string))))
3623 (should
3624 (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
3625 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3626 (org-entry-put (point) "SCHEDULED" "earlier")
3627 (buffer-string))))
3628 (should
3629 (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
3630 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3631 (org-entry-put (point) "SCHEDULED" "later")
3632 (buffer-string))))
3633 ;; Set "DEADLINE" property.
3634 (should
3635 (string-match "^ *DEADLINE: <2014-03-04 .*?>"
3636 (org-test-with-temp-text "* H"
3637 (org-entry-put (point) "DEADLINE" "2014-03-04")
3638 (buffer-string))))
3639 (should
3640 (string= "* H\n"
3641 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3642 (org-entry-put (point) "DEADLINE" nil)
3643 (buffer-string))))
3644 (should
3645 (string-match "^ *DEADLINE: <2014-03-03 .*?>"
3646 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3647 (org-entry-put (point) "DEADLINE" "earlier")
3648 (buffer-string))))
3649 (should
3650 (string-match "^ *DEADLINE: <2014-03-05 .*?>"
3651 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3652 (org-entry-put (point) "DEADLINE" "later")
3653 (buffer-string))))
3654 ;; Set "CATEGORY" property
3655 (should
3656 (string-match "^ *:CATEGORY: cat"
3657 (org-test-with-temp-text "* H"
3658 (org-entry-put (point) "CATEGORY" "cat")
3659 (buffer-string))))
3660 ;; Regular properties, with or without pre-existing drawer.
3661 (should
3662 (string-match "^ *:A: +2$"
3663 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3664 (org-entry-put (point) "A" "2")
3665 (buffer-string))))
3666 (should
3667 (string-match "^ *:A: +1$"
3668 (org-test-with-temp-text "* H"
3669 (org-entry-put (point) "A" "1")
3670 (buffer-string))))
3671 ;; Special case: two consecutive headlines.
3672 (should
3673 (string-match "\\* A\n *:PROPERTIES:"
3674 (org-test-with-temp-text "* A\n** B"
3675 (org-entry-put (point) "A" "1")
3676 (buffer-string)))))
3679 ;;; Radio Targets
3681 (ert-deftest test-org/update-radio-target-regexp ()
3682 "Test `org-update-radio-target-regexp' specifications."
3683 ;; Properly update cache with no previous radio target regexp.
3684 (should
3685 (eq 'link
3686 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3687 (save-excursion (goto-char (point-max)) (org-element-context))
3688 (insert "<<<")
3689 (search-forward "o")
3690 (insert ">>>")
3691 (org-update-radio-target-regexp)
3692 (goto-char (point-max))
3693 (org-element-type (org-element-context)))))
3694 ;; Properly update cache with previous radio target regexp.
3695 (should
3696 (eq 'link
3697 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3698 (save-excursion (goto-char (point-max)) (org-element-context))
3699 (insert "<<<")
3700 (search-forward "o")
3701 (insert ">>>")
3702 (org-update-radio-target-regexp)
3703 (search-backward "r")
3704 (delete-char 5)
3705 (insert "new")
3706 (org-update-radio-target-regexp)
3707 (goto-char (point-max))
3708 (delete-region (line-beginning-position) (point))
3709 (insert "new")
3710 (org-element-type (org-element-context))))))
3713 ;;; Sparse trees
3715 (ert-deftest test-org/match-sparse-tree ()
3716 "Test `org-match-sparse-tree' specifications."
3717 ;; Match tags.
3718 (should-not
3719 (org-test-with-temp-text "* H\n** H1 :tag:"
3720 (org-match-sparse-tree nil "tag")
3721 (search-forward "H1")
3722 (org-invisible-p2)))
3723 (should
3724 (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
3725 (org-match-sparse-tree nil "tag")
3726 (search-forward "H2")
3727 (org-invisible-p2)))
3728 ;; "-" operator for tags.
3729 (should-not
3730 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3731 (org-match-sparse-tree nil "tag1-tag2")
3732 (search-forward "H1")
3733 (org-invisible-p2)))
3734 (should
3735 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3736 (org-match-sparse-tree nil "tag1-tag2")
3737 (search-forward "H2")
3738 (org-invisible-p2)))
3739 ;; "&" operator for tags.
3740 (should
3741 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3742 (org-match-sparse-tree nil "tag1&tag2")
3743 (search-forward "H1")
3744 (org-invisible-p2)))
3745 (should-not
3746 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3747 (org-match-sparse-tree nil "tag1&tag2")
3748 (search-forward "H2")
3749 (org-invisible-p2)))
3750 ;; "|" operator for tags.
3751 (should-not
3752 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3753 (org-match-sparse-tree nil "tag1|tag2")
3754 (search-forward "H1")
3755 (org-invisible-p2)))
3756 (should-not
3757 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3758 (org-match-sparse-tree nil "tag1|tag2")
3759 (search-forward "H2")
3760 (org-invisible-p2)))
3761 ;; Regexp match on tags.
3762 (should-not
3763 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3764 (org-match-sparse-tree nil "{^tag.*}")
3765 (search-forward "H1")
3766 (org-invisible-p2)))
3767 (should
3768 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3769 (org-match-sparse-tree nil "{^tag.*}")
3770 (search-forward "H2")
3771 (org-invisible-p2)))
3772 ;; Match group tags.
3773 (should-not
3774 (org-test-with-temp-text
3775 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3776 (org-match-sparse-tree nil "work")
3777 (search-forward "H1")
3778 (org-invisible-p2)))
3779 (should-not
3780 (org-test-with-temp-text
3781 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3782 (org-match-sparse-tree nil "work")
3783 (search-forward "H2")
3784 (org-invisible-p2)))
3785 ;; Match group tags with hard brackets.
3786 (should-not
3787 (org-test-with-temp-text
3788 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
3789 (org-match-sparse-tree nil "work")
3790 (search-forward "H1")
3791 (org-invisible-p2)))
3792 (should-not
3793 (org-test-with-temp-text
3794 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
3795 (org-match-sparse-tree nil "work")
3796 (search-forward "H2")
3797 (org-invisible-p2)))
3798 ;; Match tags in hierarchies
3799 (should-not
3800 (org-test-with-temp-text
3801 "#+TAGS: [ Lev_1 : Lev_2 ]\n
3802 #+TAGS: [ Lev_2 : Lev_3 ]\n
3803 #+TAGS: { Lev_3 : Lev_4 }\n
3804 * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
3805 (org-match-sparse-tree nil "Lev_1")
3806 (search-forward "H4")
3807 (org-invisible-p2)))
3808 ;; Match regular expressions in tags
3809 (should-not
3810 (org-test-with-temp-text
3811 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
3812 (org-match-sparse-tree nil "Lev")
3813 (search-forward "H1")
3814 (org-invisible-p2)))
3815 (should
3816 (org-test-with-temp-text
3817 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
3818 (org-match-sparse-tree nil "Lev")
3819 (search-forward "H1")
3820 (org-invisible-p2)))
3821 ;; Match properties.
3822 (should
3823 (org-test-with-temp-text
3824 "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
3825 (org-match-sparse-tree nil "A=\"1\"")
3826 (search-forward "H2")
3827 (org-invisible-p2)))
3828 (should-not
3829 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3830 (org-match-sparse-tree nil "A=\"1\"")
3831 (search-forward "H2")
3832 (org-invisible-p2)))
3833 ;; Case is not significant when matching properties.
3834 (should-not
3835 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3836 (org-match-sparse-tree nil "a=\"1\"")
3837 (search-forward "H2")
3838 (org-invisible-p2)))
3839 (should-not
3840 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
3841 (org-match-sparse-tree nil "A=\"1\"")
3842 (search-forward "H2")
3843 (org-invisible-p2)))
3844 ;; Match special LEVEL property.
3845 (should-not
3846 (org-test-with-temp-text "* H\n** H1\n*** H2"
3847 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3848 (search-forward "H1")
3849 (org-invisible-p2)))
3850 (should
3851 (org-test-with-temp-text "* H\n** H1\n*** H2"
3852 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3853 (search-forward "H2")
3854 (org-invisible-p2)))
3855 ;; Comparison operators when matching properties.
3856 (should
3857 (org-test-with-temp-text
3858 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3859 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3860 (search-forward "H1")
3861 (org-invisible-p2)))
3862 (should-not
3863 (org-test-with-temp-text
3864 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3865 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3866 (search-forward "H2")
3867 (org-invisible-p2)))
3868 ;; Regexp match on properties values.
3869 (should-not
3870 (org-test-with-temp-text
3871 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3872 (org-match-sparse-tree nil "A={f.*}")
3873 (search-forward "H1")
3874 (org-invisible-p2)))
3875 (should
3876 (org-test-with-temp-text
3877 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3878 (org-match-sparse-tree nil "A={f.*}")
3879 (search-forward "H2")
3880 (org-invisible-p2)))
3881 ;; With an optional argument, limit match to TODO entries.
3882 (should-not
3883 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3884 (org-match-sparse-tree t "tag")
3885 (search-forward "H1")
3886 (org-invisible-p2)))
3887 (should
3888 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3889 (org-match-sparse-tree t "tag")
3890 (search-forward "H2")
3891 (org-invisible-p2))))
3894 ;;; Timestamps API
3896 (ert-deftest test-org/time-stamp ()
3897 "Test `org-time-stamp' specifications."
3898 ;; Insert chosen time stamp at point.
3899 (should
3900 (string-match
3901 "Te<2014-03-04 .*?>xt"
3902 (org-test-with-temp-text "Te<point>xt"
3903 (flet ((org-read-date
3904 (&rest args)
3905 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3906 (org-time-stamp nil)
3907 (buffer-string)))))
3908 ;; With a prefix argument, also insert time.
3909 (should
3910 (string-match
3911 "Te<2014-03-04 .*? 00:41>xt"
3912 (org-test-with-temp-text "Te<point>xt"
3913 (flet ((org-read-date
3914 (&rest args)
3915 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
3916 (org-time-stamp '(4))
3917 (buffer-string)))))
3918 ;; With two universal prefix arguments, insert an active timestamp
3919 ;; with the current time without prompting the user.
3920 (should
3921 (string-match
3922 "Te<2014-03-04 .*? 00:41>xt"
3923 (org-test-with-temp-text "Te<point>xt"
3924 (flet ((current-time
3926 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
3927 (org-time-stamp '(16))
3928 (buffer-string)))))
3929 ;; When optional argument is non-nil, insert an inactive timestamp.
3930 (should
3931 (string-match
3932 "Te\\[2014-03-04 .*?\\]xt"
3933 (org-test-with-temp-text "Te<point>xt"
3934 (flet ((org-read-date
3935 (&rest args)
3936 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3937 (org-time-stamp nil t)
3938 (buffer-string)))))
3939 ;; When called from a timestamp, replace existing one.
3940 (should
3941 (string-match
3942 "<2014-03-04 .*?>"
3943 (org-test-with-temp-text "<2012-03-29<point> thu.>"
3944 (flet ((org-read-date
3945 (&rest args)
3946 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3947 (org-time-stamp nil)
3948 (buffer-string)))))
3949 (should
3950 (string-match
3951 "<2014-03-04 .*?>--<2014-03-04 .*?>"
3952 (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
3953 (flet ((org-read-date
3954 (&rest args)
3955 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3956 (org-time-stamp nil)
3957 (buffer-string)))))
3958 ;; When replacing a timestamp, preserve repeater, if any.
3959 (should
3960 (string-match
3961 "<2014-03-04 .*? \\+2y>"
3962 (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
3963 (flet ((org-read-date
3964 (&rest args)
3965 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3966 (org-time-stamp nil)
3967 (buffer-string)))))
3968 ;; When called twice in a raw, build a date range.
3969 (should
3970 (string-match
3971 "<2012-03-29 .*?>--<2014-03-04 .*?>"
3972 (org-test-with-temp-text "<2012-03-29 thu.><point>"
3973 (flet ((org-read-date
3974 (&rest args)
3975 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3976 (let ((last-command 'org-time-stamp)
3977 (this-command 'org-time-stamp))
3978 (org-time-stamp nil))
3979 (buffer-string))))))
3981 (ert-deftest test-org/timestamp-has-time-p ()
3982 "Test `org-timestamp-has-time-p' specifications."
3983 ;; With time.
3984 (should
3985 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
3986 (org-timestamp-has-time-p (org-element-context))))
3987 ;; Without time.
3988 (should-not
3989 (org-test-with-temp-text "<2012-03-29 Thu>"
3990 (org-timestamp-has-time-p (org-element-context)))))
3992 (ert-deftest test-org/timestamp-format ()
3993 "Test `org-timestamp-format' specifications."
3994 ;; Regular test.
3995 (should
3996 (equal
3997 "2012-03-29 16:40"
3998 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
3999 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
4000 ;; Range end.
4001 (should
4002 (equal
4003 "2012-03-29"
4004 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
4005 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
4007 (ert-deftest test-org/timestamp-split-range ()
4008 "Test `org-timestamp-split-range' specifications."
4009 ;; Extract range start (active).
4010 (should
4011 (equal '(2012 3 29)
4012 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4013 (let ((ts (org-timestamp-split-range (org-element-context))))
4014 (mapcar (lambda (p) (org-element-property p ts))
4015 '(:year-end :month-end :day-end))))))
4016 ;; Extract range start (inactive)
4017 (should
4018 (equal '(2012 3 29)
4019 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
4020 (let ((ts (org-timestamp-split-range (org-element-context))))
4021 (mapcar (lambda (p) (org-element-property p ts))
4022 '(:year-end :month-end :day-end))))))
4023 ;; Extract range end (active).
4024 (should
4025 (equal '(2012 3 30)
4026 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4027 (let ((ts (org-timestamp-split-range
4028 (org-element-context) t)))
4029 (mapcar (lambda (p) (org-element-property p ts))
4030 '(:year-end :month-end :day-end))))))
4031 ;; Extract range end (inactive)
4032 (should
4033 (equal '(2012 3 30)
4034 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
4035 (let ((ts (org-timestamp-split-range
4036 (org-element-context) t)))
4037 (mapcar (lambda (p) (org-element-property p ts))
4038 '(:year-end :month-end :day-end))))))
4039 ;; Return the timestamp if not a range.
4040 (should
4041 (org-test-with-temp-text "[2012-03-29 Thu]"
4042 (let* ((ts-orig (org-element-context))
4043 (ts-copy (org-timestamp-split-range ts-orig)))
4044 (eq ts-orig ts-copy))))
4045 (should
4046 (org-test-with-temp-text "<%%(org-float t 4 2)>"
4047 (let* ((ts-orig (org-element-context))
4048 (ts-copy (org-timestamp-split-range ts-orig)))
4049 (eq ts-orig ts-copy)))))
4051 (ert-deftest test-org/timestamp-translate ()
4052 "Test `org-timestamp-translate' specifications."
4053 ;; Translate whole date range.
4054 (should
4055 (equal "<29>--<30>"
4056 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4057 (let ((org-display-custom-times t)
4058 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4059 (org-timestamp-translate (org-element-context))))))
4060 ;; Translate date range start.
4061 (should
4062 (equal "<29>"
4063 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4064 (let ((org-display-custom-times t)
4065 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4066 (org-timestamp-translate (org-element-context) 'start)))))
4067 ;; Translate date range end.
4068 (should
4069 (equal "<30>"
4070 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4071 (let ((org-display-custom-times t)
4072 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4073 (org-timestamp-translate (org-element-context) 'end)))))
4074 ;; Translate time range.
4075 (should
4076 (equal "<08>--<16>"
4077 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
4078 (let ((org-display-custom-times t)
4079 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
4080 (org-timestamp-translate (org-element-context))))))
4081 ;; Translate non-range timestamp.
4082 (should
4083 (equal "<29>"
4084 (org-test-with-temp-text "<2012-03-29 Thu>"
4085 (let ((org-display-custom-times t)
4086 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4087 (org-timestamp-translate (org-element-context))))))
4088 ;; Do not change `diary' timestamps.
4089 (should
4090 (equal "<%%(org-float t 4 2)>"
4091 (org-test-with-temp-text "<%%(org-float t 4 2)>"
4092 (let ((org-display-custom-times t)
4093 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4094 (org-timestamp-translate (org-element-context)))))))
4098 ;;; Visibility
4100 (ert-deftest test-org/flag-drawer ()
4101 "Test `org-flag-drawer' specifications."
4102 ;; Hide drawer.
4103 (should
4104 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4105 (org-flag-drawer t)
4106 (get-char-property (line-end-position) 'invisible)))
4107 ;; Show drawer.
4108 (should-not
4109 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4110 (org-flag-drawer t)
4111 (org-flag-drawer nil)
4112 (get-char-property (line-end-position) 'invisible)))
4113 ;; Test optional argument.
4114 (should
4115 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
4116 (let ((drawer (save-excursion (search-forward ":D2")
4117 (org-element-at-point))))
4118 (org-flag-drawer t drawer)
4119 (get-char-property (progn (search-forward ":D2") (line-end-position))
4120 'invisible))))
4121 (should-not
4122 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
4123 (let ((drawer (save-excursion (search-forward ":D2")
4124 (org-element-at-point))))
4125 (org-flag-drawer t drawer)
4126 (get-char-property (line-end-position) 'invisible))))
4127 ;; Do not hide fake drawers.
4128 (should-not
4129 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
4130 (forward-line 1)
4131 (org-flag-drawer t)
4132 (get-char-property (line-end-position) 'invisible)))
4133 ;; Do not hide incomplete drawers.
4134 (should-not
4135 (org-test-with-temp-text ":D:\nparagraph"
4136 (forward-line 1)
4137 (org-flag-drawer t)
4138 (get-char-property (line-end-position) 'invisible)))
4139 ;; Do not hide drawers when called from final blank lines.
4140 (should-not
4141 (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
4142 (goto-char (point-max))
4143 (org-flag-drawer t)
4144 (goto-char (point-min))
4145 (get-char-property (line-end-position) 'invisible)))
4146 ;; Don't leave point in an invisible part of the buffer when hiding
4147 ;; a drawer away.
4148 (should-not
4149 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4150 (goto-char (point-max))
4151 (org-flag-drawer t)
4152 (get-char-property (point) 'invisible))))
4154 (ert-deftest test-org/hide-block-toggle ()
4155 "Test `org-hide-block-toggle' specifications."
4156 ;; Error when not at a block.
4157 (should-error
4158 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
4159 (org-hide-block-toggle 'off)
4160 (get-char-property (line-end-position) 'invisible)))
4161 ;; Hide block.
4162 (should
4163 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
4164 (org-hide-block-toggle)
4165 (get-char-property (line-end-position) 'invisible)))
4166 (should
4167 (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
4168 (org-hide-block-toggle)
4169 (get-char-property (line-end-position) 'invisible)))
4170 ;; Show block unconditionally when optional argument is `off'.
4171 (should-not
4172 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4173 (org-hide-block-toggle)
4174 (org-hide-block-toggle 'off)
4175 (get-char-property (line-end-position) 'invisible)))
4176 (should-not
4177 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4178 (org-hide-block-toggle 'off)
4179 (get-char-property (line-end-position) 'invisible)))
4180 ;; Hide block unconditionally when optional argument is non-nil.
4181 (should
4182 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4183 (org-hide-block-toggle t)
4184 (get-char-property (line-end-position) 'invisible)))
4185 (should
4186 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4187 (org-hide-block-toggle)
4188 (org-hide-block-toggle t)
4189 (get-char-property (line-end-position) 'invisible)))
4190 ;; Do not hide block when called from final blank lines.
4191 (should-not
4192 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
4193 (org-hide-block-toggle)
4194 (goto-char (point-min))
4195 (get-char-property (line-end-position) 'invisible)))
4196 ;; Don't leave point in an invisible part of the buffer when hiding
4197 ;; a block away.
4198 (should-not
4199 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
4200 (org-hide-block-toggle)
4201 (get-char-property (point) 'invisible))))
4203 (ert-deftest test-org/hide-block-toggle-maybe ()
4204 "Test `org-hide-block-toggle-maybe' specifications."
4205 (should
4206 (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
4207 (org-hide-block-toggle-maybe)))
4208 (should-not
4209 (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
4211 (ert-deftest test-org/show-set-visibility ()
4212 "Test `org-show-set-visibility' specifications."
4213 ;; Do not throw an error before first heading.
4214 (should
4215 (org-test-with-temp-text "Preamble\n* Headline"
4216 (org-show-set-visibility 'tree)
4218 ;; Test all visibility spans, both on headline and in entry.
4219 (let ((list-visible-lines
4220 (lambda (state headerp)
4221 (org-test-with-temp-text "* Grandmother (0)
4222 ** Uncle (1)
4223 *** Heir (2)
4224 ** Father (3)
4225 Ancestor text (4)
4226 *** Sister (5)
4227 Sibling text (6)
4228 *** Self (7)
4229 Match (8)
4230 **** First born (9)
4231 Child text (10)
4232 **** The other child (11)
4233 *** Brother (12)
4234 ** Aunt (13)
4236 (org-cycle t)
4237 (search-forward (if headerp "Self" "Match"))
4238 (org-show-set-visibility state)
4239 (goto-char (point-min))
4240 (let (result (line 0))
4241 (while (not (eobp))
4242 (unless (org-invisible-p2) (push line result))
4243 (incf line)
4244 (forward-line))
4245 (nreverse result))))))
4246 (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
4247 (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
4248 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
4249 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
4250 (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
4251 (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
4252 (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
4253 (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
4254 (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
4255 (should (equal '(0 1 3 5 7 8 9 11 12 13)
4256 (funcall list-visible-lines 'tree nil)))
4257 (should (equal '(0 1 3 4 5 7 12 13)
4258 (funcall list-visible-lines 'canonical t)))
4259 (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
4260 (funcall list-visible-lines 'canonical nil)))))
4263 (provide 'test-org)
4265 ;;; test-org.el ends here