Fix property inheritance with extended values
[org-mode/org-tableheadings.git] / testing / lisp / test-org.el
blobbd486d190d8411a1c614183da88a964c2c506df2
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 |"
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)) (org-return))
890 (org-looking-at-p "<<target>>")))
891 (should-not
892 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
893 (let ((org-return-follows-link nil)) (org-return))
894 (org-looking-at-p "<<target>>")))
895 ;; However, do not open link when point is in a table.
896 (should
897 (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
898 (let ((org-return-follows-link t)) (org-return))
899 (org-looking-at-p "between")))
900 ;; Special case: in a list, when indenting, do not break structure.
901 (should
902 (equal "- A\n B"
903 (org-test-with-temp-text "- A <point>B"
904 (org-return t)
905 (buffer-string))))
906 (should
907 (equal "- A\n\n- B"
908 (org-test-with-temp-text "- A\n<point>- B"
909 (org-return t)
910 (buffer-string))))
911 ;; On tags part of a headline, add a newline below it instead of
912 ;; breaking it.
913 (should
914 (equal "* H :tag:\n"
915 (org-test-with-temp-text "* H :<point>tag:"
916 (org-return)
917 (buffer-string))))
918 ;; Before headline text, add a newline below it instead of breaking
919 ;; it.
920 (should
921 (equal "* TODO H :tag:\n"
922 (org-test-with-temp-text "* <point>TODO H :tag:"
923 (org-return)
924 (buffer-string))))
925 (should
926 (equal "* TODO [#B] H :tag:\n"
927 (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
928 (org-return)
929 (buffer-string))))
930 ;; At headline text, break headline text but preserve tags.
931 (should
932 (equal "* TODO [#B] foo :tag:\nbar"
933 (let (org-auto-align-tags)
934 (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
935 (org-return)
936 (buffer-string)))))
937 ;; At bol of headline insert newline.
938 (should
939 (equal "\n* h"
940 (org-test-with-temp-text "<point>* h"
941 (org-return)
942 (buffer-string)))))
944 (ert-deftest test-org/meta-return ()
945 "Test M-RET (`org-meta-return') specifications."
946 ;; In a table field insert a row above.
947 (should
948 (org-test-with-temp-text "| a |"
949 (forward-char)
950 (org-meta-return)
951 (forward-line -1)
952 (looking-at "| |$")))
953 ;; In a paragraph change current line into a header.
954 (should
955 (org-test-with-temp-text "a"
956 (org-meta-return)
957 (beginning-of-line)
958 (looking-at "\* a$")))
959 ;; In an item insert an item, in this case above.
960 (should
961 (org-test-with-temp-text "- a"
962 (org-meta-return)
963 (beginning-of-line)
964 (looking-at "- $")))
965 ;; In a drawer and item insert an item, in this case above.
966 (should
967 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
968 (forward-line)
969 (org-meta-return)
970 (beginning-of-line)
971 (looking-at "- $"))))
973 (ert-deftest test-org/insert-heading ()
974 "Test `org-insert-heading' specifications."
975 ;; In an empty buffer, insert a new headline.
976 (should
977 (equal "* "
978 (org-test-with-temp-text ""
979 (org-insert-heading)
980 (buffer-string))))
981 ;; At the beginning of a line, turn it into a headline
982 (should
983 (equal "* P"
984 (org-test-with-temp-text "<point>P"
985 (org-insert-heading)
986 (buffer-string))))
987 ;; In the middle of a line, split the line if allowed, otherwise,
988 ;; insert the headline at its end.
989 (should
990 (equal "Para\n* graph"
991 (org-test-with-temp-text "Para<point>graph"
992 (let ((org-M-RET-may-split-line '((default . t))))
993 (org-insert-heading))
994 (buffer-string))))
995 (should
996 (equal "Paragraph\n* "
997 (org-test-with-temp-text "Para<point>graph"
998 (let ((org-M-RET-may-split-line '((default . nil))))
999 (org-insert-heading))
1000 (buffer-string))))
1001 ;; When on a list, insert an item instead, unless called with an
1002 ;; universal argument or if list is invisible. In this case, create
1003 ;; a new headline after contents.
1004 (should
1005 (equal "* H\n- item\n- "
1006 (org-test-with-temp-text "* H\n- item<point>"
1007 (let ((org-insert-heading-respect-content nil))
1008 (org-insert-heading))
1009 (buffer-string))))
1010 (should
1011 (equal "* H\n- item\n- item 2\n* "
1012 (org-test-with-temp-text "* H\n- item<point>\n- item 2"
1013 (let ((org-insert-heading-respect-content nil))
1014 (org-insert-heading '(4)))
1015 (buffer-string))))
1016 (should
1017 (equal "* H\n- item\n* "
1018 (org-test-with-temp-text "* H\n- item"
1019 (org-cycle)
1020 (goto-char (point-max))
1021 (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
1022 (buffer-string))))
1023 ;; When called with two universal arguments, insert a new headline
1024 ;; at the end of the grandparent subtree.
1025 (should
1026 (equal "* H1\n** H3\n- item\n** H2\n** "
1027 (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
1028 (let ((org-insert-heading-respect-content nil))
1029 (org-insert-heading '(16)))
1030 (buffer-string))))
1031 ;; When optional TOP-LEVEL argument is non-nil, always insert
1032 ;; a level 1 heading.
1033 (should
1034 (equal "* H1\n** H2\n* "
1035 (org-test-with-temp-text "* H1\n** H2<point>"
1036 (org-insert-heading nil nil t)
1037 (buffer-string))))
1038 (should
1039 (equal "* H1\n- item\n* "
1040 (org-test-with-temp-text "* H1\n- item<point>"
1041 (org-insert-heading nil nil t)
1042 (buffer-string))))
1043 ;; Corner case: correctly insert a headline after an empty one.
1044 (should
1045 (equal "* \n* "
1046 (org-test-with-temp-text "* <point>"
1047 (org-insert-heading)
1048 (buffer-string)))))
1050 (ert-deftest test-org/insert-todo-heading-respect-content ()
1051 "Test `org-insert-todo-heading-respect-content' specifications."
1052 ;; Create a TODO heading.
1053 (should
1054 (org-test-with-temp-text "* H1\n Body"
1055 (org-insert-todo-heading-respect-content)
1056 (nth 2 (org-heading-components))))
1057 ;; Add headline at the end of the first subtree
1058 (should
1059 (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
1060 (search-forward "H1Body")
1061 (org-insert-todo-heading-respect-content)
1062 (and (eobp) (org-at-heading-p))))
1063 ;; In a list, do not create a new item.
1064 (should
1065 (org-test-with-temp-text "* H\n- an item\n- another one"
1066 (search-forward "an ")
1067 (org-insert-todo-heading-respect-content)
1068 (and (eobp) (org-at-heading-p)))))
1070 (ert-deftest test-org/clone-with-time-shift ()
1071 "Test `org-clone-subtree-with-time-shift'."
1072 ;; Clone non-repeating once.
1073 (should
1074 (equal "\
1075 * H1\n<2015-06-21>
1076 * H1\n<2015-06-23>
1078 (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
1079 (org-clone-subtree-with-time-shift 1 "+2d")
1080 (replace-regexp-in-string
1081 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1082 nil nil 1))))
1083 ;; Clone repeating once.
1084 (should
1085 (equal "\
1086 * H1\n<2015-06-21>
1087 * H1\n<2015-06-23>
1088 * H1\n<2015-06-25 +1w>
1090 (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
1091 (org-clone-subtree-with-time-shift 1 "+2d")
1092 (replace-regexp-in-string
1093 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1094 nil nil 1))))
1095 ;; Clone non-repeating zero times.
1096 (should
1097 (equal "\
1098 * H1\n<2015-06-21>
1100 (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
1101 (org-clone-subtree-with-time-shift 0 "+2d")
1102 (replace-regexp-in-string
1103 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1104 nil nil 1))))
1105 ;; Clone repeating "zero" times.
1106 (should
1107 (equal "\
1108 * H1\n<2015-06-21>
1109 * H1\n<2015-06-23 +1w>
1111 (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
1112 (org-clone-subtree-with-time-shift 0 "+2d")
1113 (replace-regexp-in-string
1114 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1115 nil nil 1)))))
1118 ;;; Fixed-Width Areas
1120 (ert-deftest test-org/toggle-fixed-width ()
1121 "Test `org-toggle-fixed-width' specifications."
1122 ;; No region: Toggle on fixed-width marker in paragraphs.
1123 (should
1124 (equal ": A"
1125 (org-test-with-temp-text "A"
1126 (org-toggle-fixed-width)
1127 (buffer-string))))
1128 ;; No region: Toggle off fixed-width markers in fixed-width areas.
1129 (should
1130 (equal "A"
1131 (org-test-with-temp-text ": A"
1132 (org-toggle-fixed-width)
1133 (buffer-string))))
1134 ;; No region: Toggle on marker in blank lines after elements or just
1135 ;; after a headline.
1136 (should
1137 (equal "* H\n: "
1138 (org-test-with-temp-text "* H\n"
1139 (forward-line)
1140 (org-toggle-fixed-width)
1141 (buffer-string))))
1142 (should
1143 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
1144 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
1145 (goto-char (point-max))
1146 (org-toggle-fixed-width)
1147 (buffer-string))))
1148 ;; No region: Toggle on marker in front of one line elements (e.g.,
1149 ;; headlines, clocks)
1150 (should
1151 (equal ": * Headline"
1152 (org-test-with-temp-text "* Headline"
1153 (org-toggle-fixed-width)
1154 (buffer-string))))
1155 (should
1156 (equal ": #+KEYWORD: value"
1157 (org-test-with-temp-text "#+KEYWORD: value"
1158 (org-toggle-fixed-width)
1159 (buffer-string))))
1160 ;; No region: error in other situations.
1161 (should-error
1162 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
1163 (forward-line)
1164 (org-toggle-fixed-width)
1165 (buffer-string)))
1166 ;; No region: Indentation is preserved.
1167 (should
1168 (equal "- A\n : B"
1169 (org-test-with-temp-text "- A\n B"
1170 (forward-line)
1171 (org-toggle-fixed-width)
1172 (buffer-string))))
1173 ;; Region: If it contains only fixed-width elements and blank lines,
1174 ;; toggle off fixed-width markup.
1175 (should
1176 (equal "A\n\nB"
1177 (org-test-with-temp-text ": A\n\n: B"
1178 (transient-mark-mode 1)
1179 (push-mark (point) t t)
1180 (goto-char (point-max))
1181 (org-toggle-fixed-width)
1182 (buffer-string))))
1183 ;; Region: If it contains anything else, toggle on fixed-width but
1184 ;; not on fixed-width areas.
1185 (should
1186 (equal ": A\n: \n: B\n: \n: C"
1187 (org-test-with-temp-text "A\n\n: B\n\nC"
1188 (transient-mark-mode 1)
1189 (push-mark (point) t t)
1190 (goto-char (point-max))
1191 (org-toggle-fixed-width)
1192 (buffer-string))))
1193 ;; Region: Ignore blank lines at its end, unless it contains only
1194 ;; such lines.
1195 (should
1196 (equal ": A\n\n"
1197 (org-test-with-temp-text "A\n\n"
1198 (transient-mark-mode 1)
1199 (push-mark (point) t t)
1200 (goto-char (point-max))
1201 (org-toggle-fixed-width)
1202 (buffer-string))))
1203 (should
1204 (equal ": \n: \n"
1205 (org-test-with-temp-text "\n\n"
1206 (transient-mark-mode 1)
1207 (push-mark (point) t t)
1208 (goto-char (point-max))
1209 (org-toggle-fixed-width)
1210 (buffer-string)))))
1214 ;;; Headline
1216 (ert-deftest test-org/in-commented-heading-p ()
1217 "Test `org-in-commented-heading-p' specifications."
1218 ;; Commented headline.
1219 (should
1220 (org-test-with-temp-text "* COMMENT Headline\nBody"
1221 (goto-char (point-max))
1222 (org-in-commented-heading-p)))
1223 ;; Commented ancestor.
1224 (should
1225 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1226 (goto-char (point-max))
1227 (org-in-commented-heading-p)))
1228 ;; Comment keyword is case-sensitive.
1229 (should-not
1230 (org-test-with-temp-text "* Comment Headline\nBody"
1231 (goto-char (point-max))
1232 (org-in-commented-heading-p)))
1233 ;; Keyword is standalone.
1234 (should-not
1235 (org-test-with-temp-text "* COMMENTHeadline\nBody"
1236 (goto-char (point-max))
1237 (org-in-commented-heading-p)))
1238 ;; Optional argument.
1239 (should-not
1240 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1241 (goto-char (point-max))
1242 (org-in-commented-heading-p t))))
1246 ;;; Keywords
1248 (ert-deftest test-org/set-regexps-and-options ()
1249 "Test `org-set-regexps-and-options' specifications."
1250 ;; TAGS keyword.
1251 (should
1252 (equal '(("A" . ?a) ("B") ("C"))
1253 (org-test-with-temp-text "#+TAGS: A(a) B C"
1254 (org-mode-restart)
1255 org-tag-alist)))
1256 (should
1257 (equal '(("A") (:newline) ("B"))
1258 (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
1259 (org-mode-restart)
1260 org-tag-alist)))
1261 (should
1262 (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
1263 (org-test-with-temp-text "#+TAGS: { A B } C"
1264 (org-mode-restart)
1265 org-tag-alist)))
1266 (should
1267 (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
1268 (org-test-with-temp-text "#+TAGS: { A : B C }"
1269 (org-mode-restart)
1270 org-tag-alist)))
1271 (should
1272 (equal '(("A" "B" "C"))
1273 (org-test-with-temp-text "#+TAGS: { A : B C }"
1274 (org-mode-restart)
1275 org-tag-groups-alist)))
1276 (should
1277 (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
1278 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1279 (org-mode-restart)
1280 org-tag-alist)))
1281 (should
1282 (equal '(("A" "B" "C"))
1283 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1284 (org-mode-restart)
1285 org-tag-groups-alist)))
1286 ;; FILETAGS keyword.
1287 (should
1288 (equal '("A" "B" "C")
1289 (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
1290 (org-mode-restart)
1291 org-file-tags)))
1292 ;; PROPERTY keyword. Property names are case-insensitive.
1293 (should
1294 (equal "foo=1"
1295 (org-test-with-temp-text "#+PROPERTY: var foo=1"
1296 (org-mode-restart)
1297 (cdr (assoc "var" org-file-properties)))))
1298 (should
1299 (equal
1300 "foo=1 bar=2"
1301 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
1302 (org-mode-restart)
1303 (cdr (assoc "var" org-file-properties)))))
1304 (should
1305 (equal
1306 "foo=1 bar=2"
1307 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
1308 (org-mode-restart)
1309 (cdr (assoc "var" org-file-properties)))))
1310 ;; ARCHIVE keyword.
1311 (should
1312 (equal "%s_done::"
1313 (org-test-with-temp-text "#+ARCHIVE: %s_done::"
1314 (org-mode-restart)
1315 org-archive-location)))
1316 ;; CATEGORY keyword.
1317 (should
1318 (eq 'test
1319 (org-test-with-temp-text "#+CATEGORY: test"
1320 (org-mode-restart)
1321 org-category)))
1322 (should
1323 (equal "test"
1324 (org-test-with-temp-text "#+CATEGORY: test"
1325 (org-mode-restart)
1326 (cdr (assoc "CATEGORY" org-file-properties)))))
1327 ;; COLUMNS keyword.
1328 (should
1329 (equal "%25ITEM %TAGS %PRIORITY %TODO"
1330 (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
1331 (org-mode-restart)
1332 org-columns-default-format)))
1333 ;; CONSTANTS keyword. Constants names are case sensitive.
1334 (should
1335 (equal '("299792458." "3.14")
1336 (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
1337 (org-mode-restart)
1338 (mapcar
1339 (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
1340 '("c" "pi")))))
1341 (should
1342 (equal "3.14"
1343 (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
1344 (org-mode-restart)
1345 (cdr (assoc "pi" org-table-formula-constants-local)))))
1346 (should
1347 (equal "22/7"
1348 (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
1349 (org-mode-restart)
1350 (cdr (assoc "PI" org-table-formula-constants-local)))))
1351 ;; LINK keyword.
1352 (should
1353 (equal
1354 '("url1" "url2")
1355 (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
1356 (org-mode-restart)
1357 (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
1358 '("a" "b")))))
1359 ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
1360 (should
1361 (equal
1362 '(?X ?Z ?Y)
1363 (org-test-with-temp-text "#+PRIORITIES: X Z Y"
1364 (org-mode-restart)
1365 (list org-highest-priority org-lowest-priority org-default-priority))))
1366 (should
1367 (equal
1368 '(?A ?C ?B)
1369 (org-test-with-temp-text "#+PRIORITIES: X Z"
1370 (org-mode-restart)
1371 (list org-highest-priority org-lowest-priority org-default-priority))))
1372 ;; STARTUP keyword.
1373 (should
1374 (equal '(t t)
1375 (org-test-with-temp-text "#+STARTUP: fold odd"
1376 (org-mode-restart)
1377 (list org-startup-folded org-odd-levels-only))))
1378 ;; TODO keywords.
1379 (should
1380 (equal '(("A" "B") ("C"))
1381 (org-test-with-temp-text "#+TODO: A B | C"
1382 (org-mode-restart)
1383 (list org-not-done-keywords org-done-keywords))))
1384 (should
1385 (equal '(("A" "C") ("B" "D"))
1386 (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
1387 (org-mode-restart)
1388 (list org-not-done-keywords org-done-keywords))))
1389 (should
1390 (equal '(("A" "B") ("C"))
1391 (org-test-with-temp-text "#+TYP_TODO: A B | C"
1392 (org-mode-restart)
1393 (list org-not-done-keywords org-done-keywords))))
1394 (should
1395 (equal '((:startgroup) ("A" . ?a) (:endgroup))
1396 (org-test-with-temp-text "#+TODO: A(a)"
1397 (org-mode-restart)
1398 org-todo-key-alist)))
1399 (should
1400 (equal '(("D" note nil) ("C" time nil) ("B" note time))
1401 (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
1402 (org-mode-restart)
1403 org-todo-log-states)))
1404 ;; Enter SETUPFILE keyword.
1405 (should
1406 (equal "1"
1407 (org-test-with-temp-text
1408 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
1409 (org-mode-restart)
1410 (cdr (assoc "a" org-file-properties))))))
1414 ;;; Links
1416 ;;;; Coderefs
1418 (ert-deftest test-org/coderef ()
1419 "Test coderef links specifications."
1420 (should
1421 (org-test-with-temp-text "
1422 #+BEGIN_SRC emacs-lisp
1423 \(+ 1 1) (ref:sc)
1424 #+END_SRC
1425 \[[(sc)]]"
1426 (goto-char (point-max))
1427 (org-open-at-point)
1428 (looking-at "(ref:sc)"))))
1430 ;;;; Custom ID
1432 (ert-deftest test-org/custom-id ()
1433 "Test custom ID links specifications."
1434 (should
1435 (org-test-with-temp-text
1436 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
1437 (goto-char (point-max))
1438 (org-open-at-point)
1439 (org-looking-at-p "\\* H1")))
1440 ;; Ignore false positives.
1441 (should-not
1442 (org-test-with-temp-text
1443 "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
1444 (goto-char (point-max))
1445 (let (org-link-search-must-match-exact-headline) (org-open-at-point))
1446 (org-looking-at-p "\\* H1"))))
1448 ;;;; Fuzzy Links
1450 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
1451 ;; a named element (#+name: text) and to headlines (* Text).
1453 (ert-deftest test-org/fuzzy-links ()
1454 "Test fuzzy links specifications."
1455 ;; Fuzzy link goes in priority to a matching target.
1456 (should
1457 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
1458 (goto-line 5)
1459 (org-open-at-point)
1460 (looking-at "<<Test>>")))
1461 ;; Then fuzzy link points to an element with a given name.
1462 (should
1463 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
1464 (goto-line 5)
1465 (org-open-at-point)
1466 (looking-at "#\\+NAME: Test")))
1467 ;; A target still lead to a matching headline otherwise.
1468 (should
1469 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
1470 (goto-line 4)
1471 (org-open-at-point)
1472 (looking-at "\\* Head2")))
1473 ;; With a leading star in link, enforce heading match.
1474 (should
1475 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
1476 (goto-line 3)
1477 (org-open-at-point)
1478 (looking-at "\\* Test")))
1479 ;; With a leading star in link, enforce exact heading match, even
1480 ;; with `org-link-search-must-match-exact-headline' set to nil.
1481 (should-error
1482 (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
1483 (let ((org-link-search-must-match-exact-headline nil))
1484 (org-open-at-point))))
1485 ;; Heading match should not care about spaces, cookies, todo
1486 ;; keywords, priorities, and tags.
1487 (should
1488 (let ((first-line
1489 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1490 (org-test-with-temp-text
1491 (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
1492 (let ((org-link-search-must-match-exact-headline nil)
1493 (org-todo-regexp "TODO"))
1494 (org-open-at-point))
1495 (looking-at (regexp-quote first-line)))))
1496 ;; Heading match should still be exact.
1497 (should-error
1498 (let ((first-line
1499 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1500 (org-test-with-temp-text
1501 (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
1502 (let ((org-link-search-must-match-exact-headline nil)
1503 (org-todo-regexp "TODO"))
1504 (org-open-at-point)))))
1505 ;; Heading match ignores COMMENT keyword.
1506 (should
1507 (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
1508 (org-open-at-point)
1509 (looking-at "\\* COMMENT Test")))
1510 ;; Correctly un-hexify fuzzy links.
1511 (should
1512 (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
1513 (goto-char (point-max))
1514 (org-open-at-point)
1515 (bobp))))
1517 ;;;; Link Escaping
1519 (ert-deftest test-org/org-link-escape-ascii-character ()
1520 "Escape an ascii character."
1521 (should
1522 (string=
1523 "%5B"
1524 (org-link-escape "["))))
1526 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
1527 "Escape an ascii control character."
1528 (should
1529 (string=
1530 "%09"
1531 (org-link-escape "\t"))))
1533 (ert-deftest test-org/org-link-escape-multibyte-character ()
1534 "Escape an unicode multibyte character."
1535 (should
1536 (string=
1537 "%E2%82%AC"
1538 (org-link-escape "€"))))
1540 (ert-deftest test-org/org-link-escape-custom-table ()
1541 "Escape string with custom character table."
1542 (should
1543 (string=
1544 "Foo%3A%42ar%0A"
1545 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
1547 (ert-deftest test-org/org-link-escape-custom-table-merge ()
1548 "Escape string with custom table merged with default table."
1549 (should
1550 (string=
1551 "%5BF%6F%6F%3A%42ar%0A%5D"
1552 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
1554 (ert-deftest test-org/org-link-unescape-ascii-character ()
1555 "Unescape an ascii character."
1556 (should
1557 (string=
1559 (org-link-unescape "%5B"))))
1561 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
1562 "Unescpae an ascii control character."
1563 (should
1564 (string=
1565 "\n"
1566 (org-link-unescape "%0A"))))
1568 (ert-deftest test-org/org-link-unescape-multibyte-character ()
1569 "Unescape unicode multibyte character."
1570 (should
1571 (string=
1572 "€"
1573 (org-link-unescape "%E2%82%AC"))))
1575 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
1576 "Unescape old style percent escaped character."
1577 (should
1578 (string=
1579 "àâçèéêîôùû"
1580 (decode-coding-string
1581 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
1583 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
1584 "Escape and unescape a URL that includes an escaped char.
1585 http://article.gmane.org/gmane.emacs.orgmode/21459/"
1586 (should
1587 (string=
1588 "http://some.host.com/form?&id=blah%2Bblah25"
1589 (org-link-unescape
1590 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
1592 (ert-deftest test-org/org-link-escape-chars-browser ()
1593 "Test of the constant `org-link-escape-chars-browser'.
1594 See there why this test is a candidate to be removed once Org
1595 drops support for Emacs 24.1 and 24.2."
1596 (should
1597 (string=
1598 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1599 "%22Release%208.2%22&idxname=emacs-orgmode")
1600 (org-link-escape-browser ; Do not replace with `url-encode-url',
1601 ; see docstring above.
1602 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1603 "\"Release 8.2\"&idxname=emacs-orgmode")))))
1605 ;;;; Open at point
1607 (ert-deftest test-org/open-at-point-in-keyword ()
1608 "Does `org-open-at-point' open link in a keyword line?"
1609 (should
1610 (org-test-with-temp-text
1611 "#+KEYWORD: <point>[[info:emacs#Top]]"
1612 (org-open-at-point) t)))
1614 (ert-deftest test-org/open-at-point-in-property ()
1615 "Does `org-open-at-point' open link in property drawer?"
1616 (should
1617 (org-test-with-temp-text
1618 "* Headline
1619 :PROPERTIES:
1620 :URL: <point>[[info:emacs#Top]]
1621 :END:"
1622 (org-open-at-point) t)))
1624 (ert-deftest test-org/open-at-point-in-comment ()
1625 "Does `org-open-at-point' open link in a commented line?"
1626 (should
1627 (org-test-with-temp-text
1628 "# <point>[[info:emacs#Top]]"
1629 (org-open-at-point) t)))
1631 (ert-deftest test-org/open-at-point/info ()
1632 "Test `org-open-at-point' on info links."
1633 (should
1634 (org-test-with-temp-text
1635 "<point>[[info:emacs#Top]]"
1636 (org-open-at-point)
1637 (and (switch-to-buffer "*info*")
1638 (prog1
1639 (looking-at "\nThe Emacs Editor")
1640 (kill-buffer))))))
1642 (ert-deftest test-org/open-at-point/inline-image ()
1643 "Test `org-open-at-point' on nested links."
1644 (should
1645 (org-test-with-temp-text "[[info:org#Top][info:<point>emacs#Top]]"
1646 (org-open-at-point)
1647 (prog1 (with-current-buffer "*info*" (looking-at "\nOrg Mode Manual"))
1648 (kill-buffer "*info*")))))
1651 ;;; Node Properties
1653 (ert-deftest test-org/accumulated-properties-in-drawers ()
1654 "Ensure properties accumulate in subtree drawers."
1655 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
1656 (org-babel-next-src-block)
1657 (should (equal '(2 1) (org-babel-execute-src-block)))))
1659 (ert-deftest test-org/custom-properties ()
1660 "Test custom properties specifications."
1661 ;; Standard test.
1662 (should
1663 (let ((org-custom-properties '("FOO")))
1664 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1665 (org-toggle-custom-properties-visibility)
1666 (org-invisible-p2))))
1667 ;; Properties are case-insensitive.
1668 (should
1669 (let ((org-custom-properties '("FOO")))
1670 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
1671 (org-toggle-custom-properties-visibility)
1672 (org-invisible-p2))))
1673 (should
1674 (let ((org-custom-properties '("foo")))
1675 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1676 (org-toggle-custom-properties-visibility)
1677 (org-invisible-p2))))
1678 ;; Multiple custom properties in the same drawer.
1679 (should
1680 (let ((org-custom-properties '("FOO" "BAR")))
1681 (org-test-with-temp-text
1682 "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
1683 (org-toggle-custom-properties-visibility)
1684 (and (org-invisible-p2)
1685 (not (progn (forward-line) (org-invisible-p2)))
1686 (progn (forward-line) (org-invisible-p2))))))
1687 ;; Hide custom properties with an empty value.
1688 (should
1689 (let ((org-custom-properties '("FOO")))
1690 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
1691 (org-toggle-custom-properties-visibility)
1692 (org-invisible-p2))))
1693 ;; Do not hide fake properties.
1694 (should-not
1695 (let ((org-custom-properties '("FOO")))
1696 (org-test-with-temp-text ":FOO: val\n"
1697 (org-toggle-custom-properties-visibility)
1698 (org-invisible-p2))))
1699 (should-not
1700 (let ((org-custom-properties '("A")))
1701 (org-test-with-temp-text
1702 "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
1703 (org-toggle-custom-properties-visibility)
1704 (org-invisible-p2)))))
1708 ;;; Mark Region
1710 (ert-deftest test-org/mark-subtree ()
1711 "Test `org-mark-subtree' specifications."
1712 ;; Error when point is before first headline.
1713 (should-error
1714 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
1715 (progn (transient-mark-mode 1)
1716 (org-mark-subtree))))
1717 ;; Without argument, mark current subtree.
1718 (should
1719 (equal
1720 '(12 32)
1721 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1722 (progn (transient-mark-mode 1)
1723 (forward-line 2)
1724 (org-mark-subtree)
1725 (list (region-beginning) (region-end))))))
1726 ;; With an argument, move ARG up.
1727 (should
1728 (equal
1729 '(1 32)
1730 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1731 (progn (transient-mark-mode 1)
1732 (forward-line 2)
1733 (org-mark-subtree 1)
1734 (list (region-beginning) (region-end))))))
1735 ;; Do not get fooled by inlinetasks.
1736 (when (featurep 'org-inlinetask)
1737 (should
1738 (= 1
1739 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
1740 (progn (transient-mark-mode 1)
1741 (forward-line 1)
1742 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
1743 (region-beginning)))))))
1747 ;;; Navigation
1749 (ert-deftest test-org/end-of-meta-data ()
1750 "Test `org-end-of-meta-data' specifications."
1751 ;; Skip planning line.
1752 (should
1753 (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
1754 (org-end-of-meta-data)
1755 (eobp)))
1756 ;; Skip properties drawer.
1757 (should
1758 (org-test-with-temp-text
1759 "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
1760 (org-end-of-meta-data)
1761 (eobp)))
1762 ;; Skip both.
1763 (should
1764 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
1765 (org-end-of-meta-data)
1766 (eobp)))
1767 ;; Nothing to skip, go to first line.
1768 (should
1769 (org-test-with-temp-text "* Headline\nContents"
1770 (org-end-of-meta-data)
1771 (looking-at "Contents")))
1772 ;; With option argument, skip empty lines, regular drawers and
1773 ;; clocking lines.
1774 (should
1775 (org-test-with-temp-text "* Headline\n\nContents"
1776 (org-end-of-meta-data t)
1777 (looking-at "Contents")))
1778 (should
1779 (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
1780 (org-end-of-meta-data t)
1781 (looking-at "Contents")))
1782 (should
1783 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
1784 (org-end-of-meta-data t)
1785 (looking-at "Contents")))
1786 ;; Special case: do not skip incomplete drawers.
1787 (should
1788 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
1789 (org-end-of-meta-data t)
1790 (looking-at ":LOGBOOK:")))
1791 ;; Special case: Be careful about consecutive headlines.
1792 (should-not
1793 (org-test-with-temp-text "* H1\n*H2\nContents"
1794 (org-end-of-meta-data t)
1795 (looking-at "Contents"))))
1797 (ert-deftest test-org/beginning-of-line ()
1798 "Test `org-beginning-of-line' specifications."
1799 ;; Standard test.
1800 (should
1801 (org-test-with-temp-text "Some text\nSome other text"
1802 (progn (org-beginning-of-line) (bolp))))
1803 ;; Standard test with `visual-line-mode'.
1804 (should-not
1805 (org-test-with-temp-text "A long line of text\nSome other text"
1806 (progn (visual-line-mode)
1807 (forward-char 2)
1808 (dotimes (i 1000) (insert "very "))
1809 (org-beginning-of-line)
1810 (bolp))))
1811 ;; At an headline with special movement.
1812 (should
1813 (org-test-with-temp-text "* TODO Headline"
1814 (let ((org-special-ctrl-a/e t))
1815 (org-end-of-line)
1816 (and (progn (org-beginning-of-line) (looking-at "Headline"))
1817 (progn (org-beginning-of-line) (bolp))
1818 (progn (org-beginning-of-line) (looking-at "Headline")))))))
1820 (ert-deftest test-org/end-of-line ()
1821 "Test `org-end-of-line' specifications."
1822 ;; Standard test.
1823 (should
1824 (org-test-with-temp-text "Some text\nSome other text"
1825 (progn (org-end-of-line) (eolp))))
1826 ;; Standard test with `visual-line-mode'.
1827 (should-not
1828 (org-test-with-temp-text "A long line of text\nSome other text"
1829 (progn (visual-line-mode)
1830 (forward-char 2)
1831 (dotimes (i 1000) (insert "very "))
1832 (goto-char (point-min))
1833 (org-end-of-line)
1834 (eolp))))
1835 ;; At an headline with special movement.
1836 (should
1837 (org-test-with-temp-text "* Headline1 :tag:\n"
1838 (let ((org-special-ctrl-a/e t))
1839 (and (progn (org-end-of-line) (looking-at " :tag:"))
1840 (progn (org-end-of-line) (eolp))
1841 (progn (org-end-of-line) (looking-at " :tag:"))))))
1842 ;; At an headline without special movement.
1843 (should
1844 (org-test-with-temp-text "* Headline2 :tag:\n"
1845 (let ((org-special-ctrl-a/e nil))
1846 (and (progn (org-end-of-line) (eolp))
1847 (progn (org-end-of-line) (eolp))))))
1848 ;; At an headline, with reversed movement.
1849 (should
1850 (org-test-with-temp-text "* Headline3 :tag:\n"
1851 (let ((org-special-ctrl-a/e 'reversed)
1852 (this-command last-command))
1853 (and (progn (org-end-of-line) (eolp))
1854 (progn (org-end-of-line) (looking-at " :tag:"))))))
1855 ;; At a block without hidden contents.
1856 (should
1857 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1858 (progn (org-end-of-line) (eolp))))
1859 ;; At a block with hidden contents.
1860 (should-not
1861 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1862 (let ((org-special-ctrl-a/e t))
1863 (org-hide-block-toggle)
1864 (org-end-of-line)
1865 (eobp)))))
1867 (ert-deftest test-org/forward-sentence ()
1868 "Test `org-forward-sentence' specifications."
1869 ;; At the end of a table cell, move to the end of the next one.
1870 (should
1871 (org-test-with-temp-text "| a<point> | b |"
1872 (org-forward-sentence)
1873 (looking-at " |$")))
1874 ;; Elsewhere in a cell, move to its end.
1875 (should
1876 (org-test-with-temp-text "| a<point>c | b |"
1877 (org-forward-sentence)
1878 (looking-at " | b |$")))
1879 ;; Otherwise, simply call `forward-sentence'.
1880 (should
1881 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
1882 (org-forward-sentence)
1883 (looking-at " Sentence 2.")))
1884 (should
1885 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
1886 (org-forward-sentence)
1887 (org-forward-sentence)
1888 (eobp)))
1889 ;; At the end of an element, jump to the next one, without stopping
1890 ;; on blank lines in-between.
1891 (should
1892 (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
1893 (org-forward-sentence)
1894 (eobp))))
1896 (ert-deftest test-org/backward-sentence ()
1897 "Test `org-backward-sentence' specifications."
1898 ;; At the beginning of a table cell, move to the beginning of the
1899 ;; previous one.
1900 (should
1901 (org-test-with-temp-text "| a | <point>b |"
1902 (org-backward-sentence)
1903 (looking-at "a | b |$")))
1904 ;; Elsewhere in a cell, move to its beginning.
1905 (should
1906 (org-test-with-temp-text "| a | b<point>c |"
1907 (org-backward-sentence)
1908 (looking-at "bc |$")))
1909 ;; Otherwise, simply call `backward-sentence'.
1910 (should
1911 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
1912 (org-backward-sentence)
1913 (looking-at "Sentence 2.")))
1914 (should
1915 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
1916 (org-backward-sentence)
1917 (org-backward-sentence)
1918 (bobp)))
1919 ;; Make sure to hit the beginning of a sentence on the same line as
1920 ;; an item.
1921 (should
1922 (org-test-with-temp-text "- Line 1\n line <point>2."
1923 (org-backward-sentence)
1924 (looking-at "Line 1"))))
1926 (ert-deftest test-org/forward-paragraph ()
1927 "Test `org-forward-paragraph' specifications."
1928 ;; At end of buffer, return an error.
1929 (should-error
1930 (org-test-with-temp-text "Paragraph"
1931 (goto-char (point-max))
1932 (org-forward-paragraph)))
1933 ;; Standard test.
1934 (should
1935 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1936 (org-forward-paragraph)
1937 (looking-at "P2")))
1938 ;; Ignore depth.
1939 (should
1940 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
1941 (org-forward-paragraph)
1942 (looking-at "P1")))
1943 ;; Do not enter elements with invisible contents.
1944 (should
1945 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
1946 (org-hide-block-toggle)
1947 (org-forward-paragraph)
1948 (looking-at "P3")))
1949 ;; On an affiliated keyword, jump to the beginning of the element.
1950 (should
1951 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
1952 (org-forward-paragraph)
1953 (looking-at "Para")))
1954 ;; On an item or a footnote definition, move to the second element
1955 ;; inside, if any.
1956 (should
1957 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
1958 (org-forward-paragraph)
1959 (looking-at " Paragraph")))
1960 (should
1961 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
1962 (org-forward-paragraph)
1963 (looking-at "Paragraph")))
1964 ;; On an item, or a footnote definition, when the first line is
1965 ;; empty, move to the first item.
1966 (should
1967 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
1968 (org-forward-paragraph)
1969 (looking-at " Paragraph")))
1970 (should
1971 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
1972 (org-forward-paragraph)
1973 (looking-at "Paragraph")))
1974 ;; On a table (resp. a property drawer) do not move through table
1975 ;; rows (resp. node properties).
1976 (should
1977 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
1978 (org-forward-paragraph)
1979 (looking-at "Paragraph")))
1980 (should
1981 (org-test-with-temp-text
1982 "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
1983 (org-forward-paragraph)
1984 (looking-at "Paragraph")))
1985 ;; On a verse or source block, stop after blank lines.
1986 (should
1987 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
1988 (org-forward-paragraph)
1989 (looking-at "L2")))
1990 (should
1991 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
1992 (org-forward-paragraph)
1993 (looking-at "L2"))))
1995 (ert-deftest test-org/backward-paragraph ()
1996 "Test `org-backward-paragraph' specifications."
1997 ;; Error at beginning of buffer.
1998 (should-error
1999 (org-test-with-temp-text "Paragraph"
2000 (org-backward-paragraph)))
2001 ;; Regular test.
2002 (should
2003 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2004 (goto-char (point-max))
2005 (org-backward-paragraph)
2006 (looking-at "P3")))
2007 (should
2008 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2009 (goto-char (point-max))
2010 (beginning-of-line)
2011 (org-backward-paragraph)
2012 (looking-at "P2")))
2013 ;; Ignore depth.
2014 (should
2015 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
2016 (goto-char (point-max))
2017 (beginning-of-line)
2018 (org-backward-paragraph)
2019 (looking-at "P2")))
2020 ;; Ignore invisible elements.
2021 (should
2022 (org-test-with-temp-text "* H1\n P1\n* H2"
2023 (org-cycle)
2024 (goto-char (point-max))
2025 (beginning-of-line)
2026 (org-backward-paragraph)
2027 (bobp)))
2028 ;; On an affiliated keyword, jump to the first one.
2029 (should
2030 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
2031 (search-forward "c2")
2032 (org-backward-paragraph)
2033 (looking-at "#\\+name")))
2034 ;; On the second element in an item or a footnote definition, jump
2035 ;; to item or the definition.
2036 (should
2037 (org-test-with-temp-text "- line1\n\n line2"
2038 (goto-char (point-max))
2039 (beginning-of-line)
2040 (org-backward-paragraph)
2041 (looking-at "- line1")))
2042 (should
2043 (org-test-with-temp-text "[fn:1] line1\n\n line2"
2044 (goto-char (point-max))
2045 (beginning-of-line)
2046 (org-backward-paragraph)
2047 (looking-at "\\[fn:1\\] line1")))
2048 ;; On a table (resp. a property drawer), ignore table rows
2049 ;; (resp. node properties).
2050 (should
2051 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
2052 (goto-char (point-max))
2053 (beginning-of-line)
2054 (org-backward-paragraph)
2055 (bobp)))
2056 (should
2057 (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
2058 (org-backward-paragraph)
2059 (looking-at ":PROPERTIES:")))
2060 ;; On a source or verse block, stop before blank lines.
2061 (should
2062 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
2063 (search-forward "L3")
2064 (beginning-of-line)
2065 (org-backward-paragraph)
2066 (looking-at "L2")))
2067 (should
2068 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
2069 (search-forward "L3")
2070 (beginning-of-line)
2071 (org-backward-paragraph)
2072 (looking-at "L2"))))
2074 (ert-deftest test-org/forward-element ()
2075 "Test `org-forward-element' specifications."
2076 ;; 1. At EOB: should error.
2077 (org-test-with-temp-text "Some text\n"
2078 (goto-char (point-max))
2079 (should-error (org-forward-element)))
2080 ;; 2. Standard move: expected to ignore blank lines.
2081 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
2082 (org-forward-element)
2083 (should (looking-at (regexp-quote "Second paragraph."))))
2084 ;; 3. Headline tests.
2085 (org-test-with-temp-text "
2086 * Head 1
2087 ** Head 1.1
2088 *** Head 1.1.1
2089 ** Head 1.2"
2090 ;; 3.1. At an headline beginning: move to next headline at the
2091 ;; same level.
2092 (goto-line 3)
2093 (org-forward-element)
2094 (should (looking-at (regexp-quote "** Head 1.2")))
2095 ;; 3.2. At an headline beginning: move to parent headline if no
2096 ;; headline at the same level.
2097 (goto-line 3)
2098 (org-forward-element)
2099 (should (looking-at (regexp-quote "** Head 1.2"))))
2100 ;; 4. Greater element tests.
2101 (org-test-with-temp-text
2102 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
2103 ;; 4.1. At a greater element: expected to skip contents.
2104 (org-forward-element)
2105 (should (looking-at (regexp-quote "Outside.")))
2106 ;; 4.2. At the end of greater element contents: expected to skip
2107 ;; to the end of the greater element.
2108 (goto-line 2)
2109 (org-forward-element)
2110 (should (looking-at (regexp-quote "Outside."))))
2111 ;; 5. List tests.
2112 (org-test-with-temp-text "
2113 - item1
2115 - sub1
2117 - sub2
2119 - sub3
2121 Inner paragraph.
2123 - item2
2125 Outside."
2126 ;; 5.1. At list top point: expected to move to the element after
2127 ;; the list.
2128 (goto-line 2)
2129 (org-forward-element)
2130 (should (looking-at (regexp-quote "Outside.")))
2131 ;; 5.2. Special case: at the first line of a sub-list, but not at
2132 ;; beginning of line, move to next item.
2133 (goto-line 2)
2134 (forward-char)
2135 (org-forward-element)
2136 (should (looking-at "- item2"))
2137 (goto-line 4)
2138 (forward-char)
2139 (org-forward-element)
2140 (should (looking-at " - sub2"))
2141 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
2142 (goto-line 4)
2143 (org-forward-element)
2144 (should (looking-at (regexp-quote " Inner paragraph.")))
2145 ;; 5.4. At sub-list end: expected to move outside the sub-list.
2146 (goto-line 8)
2147 (org-forward-element)
2148 (should (looking-at (regexp-quote " Inner paragraph.")))
2149 ;; 5.5. At an item: expected to move to next item, if any.
2150 (goto-line 6)
2151 (org-forward-element)
2152 (should (looking-at " - sub3"))))
2154 (ert-deftest test-org/backward-element ()
2155 "Test `org-backward-element' specifications."
2156 ;; 1. Should error at BOB.
2157 (org-test-with-temp-text " \nParagraph."
2158 (should-error (org-backward-element)))
2159 ;; 2. Should move at BOB when called on the first element in buffer.
2160 (should
2161 (org-test-with-temp-text "\n#+TITLE: test"
2162 (progn (forward-line)
2163 (org-backward-element)
2164 (bobp))))
2165 ;; 3. Not at the beginning of an element: move at its beginning.
2166 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2167 (goto-line 3)
2168 (end-of-line)
2169 (org-backward-element)
2170 (should (looking-at (regexp-quote "Paragraph2."))))
2171 ;; 4. Headline tests.
2172 (org-test-with-temp-text "
2173 * Head 1
2174 ** Head 1.1
2175 *** Head 1.1.1
2176 ** Head 1.2"
2177 ;; 4.1. At an headline beginning: move to previous headline at the
2178 ;; same level.
2179 (goto-line 5)
2180 (org-backward-element)
2181 (should (looking-at (regexp-quote "** Head 1.1")))
2182 ;; 4.2. At an headline beginning: move to parent headline if no
2183 ;; headline at the same level.
2184 (goto-line 3)
2185 (org-backward-element)
2186 (should (looking-at (regexp-quote "* Head 1")))
2187 ;; 4.3. At the first top-level headline: should error.
2188 (goto-line 2)
2189 (should-error (org-backward-element)))
2190 ;; 5. At beginning of first element inside a greater element:
2191 ;; expected to move to greater element's beginning.
2192 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
2193 (goto-line 3)
2194 (org-backward-element)
2195 (should (looking-at "#\\+BEGIN_CENTER")))
2196 ;; 6. At the beginning of the first element in a section: should
2197 ;; move back to headline, if any.
2198 (should
2199 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
2200 (progn (goto-char (point-max))
2201 (beginning-of-line)
2202 (org-backward-element)
2203 (org-at-heading-p))))
2204 ;; 7. List tests.
2205 (org-test-with-temp-text "
2206 - item1
2208 - sub1
2210 - sub2
2212 - sub3
2214 Inner paragraph.
2216 - item2
2219 Outside."
2220 ;; 7.1. At beginning of sub-list: expected to move to the
2221 ;; paragraph before it.
2222 (goto-line 4)
2223 (org-backward-element)
2224 (should (looking-at "item1"))
2225 ;; 7.2. At an item in a list: expected to move at previous item.
2226 (goto-line 8)
2227 (org-backward-element)
2228 (should (looking-at " - sub2"))
2229 (goto-line 12)
2230 (org-backward-element)
2231 (should (looking-at "- item1"))
2232 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
2233 ;; beginning.
2234 (goto-line 10)
2235 (org-backward-element)
2236 (should (looking-at " - sub1"))
2237 (goto-line 15)
2238 (org-backward-element)
2239 (should (looking-at "- item1"))
2240 ;; 7.4. At blank-lines before list end: expected to move to top
2241 ;; item.
2242 (goto-line 14)
2243 (org-backward-element)
2244 (should (looking-at "- item1"))))
2246 (ert-deftest test-org/up-element ()
2247 "Test `org-up-element' specifications."
2248 ;; 1. At BOB or with no surrounding element: should error.
2249 (org-test-with-temp-text "Paragraph."
2250 (should-error (org-up-element)))
2251 (org-test-with-temp-text "* Head1\n* Head2"
2252 (goto-line 2)
2253 (should-error (org-up-element)))
2254 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2255 (goto-line 3)
2256 (should-error (org-up-element)))
2257 ;; 2. At an headline: move to parent headline.
2258 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
2259 (goto-line 3)
2260 (org-up-element)
2261 (should (looking-at "\\* Head1")))
2262 ;; 3. Inside a greater element: move to greater element beginning.
2263 (org-test-with-temp-text
2264 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
2265 (goto-line 3)
2266 (org-up-element)
2267 (should (looking-at "#\\+BEGIN_CENTER")))
2268 ;; 4. List tests.
2269 (org-test-with-temp-text "* Top
2270 - item1
2272 - sub1
2274 - sub2
2276 Paragraph within sub2.
2278 - item2"
2279 ;; 4.1. Within an item: move to the item beginning.
2280 (goto-line 8)
2281 (org-up-element)
2282 (should (looking-at " - sub2"))
2283 ;; 4.2. At an item in a sub-list: move to parent item.
2284 (goto-line 4)
2285 (org-up-element)
2286 (should (looking-at "- item1"))
2287 ;; 4.3. At an item in top list: move to beginning of whole list.
2288 (goto-line 10)
2289 (org-up-element)
2290 (should (looking-at "- item1"))
2291 ;; 4.4. Special case. At very top point: should move to parent of
2292 ;; list.
2293 (goto-line 2)
2294 (org-up-element)
2295 (should (looking-at "\\* Top"))))
2297 (ert-deftest test-org/down-element ()
2298 "Test `org-down-element' specifications."
2299 ;; Error when the element hasn't got a recursive type.
2300 (org-test-with-temp-text "Paragraph."
2301 (should-error (org-down-element)))
2302 ;; Error when the element has no contents
2303 (org-test-with-temp-text "* Headline"
2304 (should-error (org-down-element)))
2305 ;; When at a plain-list, move to first item.
2306 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
2307 (goto-line 2)
2308 (org-down-element)
2309 (should (looking-at " - Item 1.1")))
2310 (org-test-with-temp-text "#+NAME: list\n- Item 1"
2311 (org-down-element)
2312 (should (looking-at " Item 1")))
2313 ;; When at a table, move to first row
2314 (org-test-with-temp-text "#+NAME: table\n| a | b |"
2315 (org-down-element)
2316 (should (looking-at " a | b |")))
2317 ;; Otherwise, move inside the greater element.
2318 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
2319 (org-down-element)
2320 (should (looking-at "Paragraph"))))
2322 (ert-deftest test-org/drag-element-backward ()
2323 "Test `org-drag-element-backward' specifications."
2324 ;; Standard test.
2325 (should
2326 (equal
2327 "#+key2: val2\n#+key1: val1\n#+key3: val3"
2328 (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
2329 (org-drag-element-backward)
2330 (buffer-string))))
2331 (should
2332 (equal
2333 "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
2334 (org-test-with-temp-text
2335 "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
2336 (org-drag-element-backward)
2337 (buffer-string))))
2338 ;; Preserve blank lines.
2339 (should
2340 (equal "Paragraph 2\n\n\nPara1\n\nPara3"
2341 (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
2342 (org-drag-element-backward)
2343 (buffer-string))))
2344 ;; Preserve column.
2345 (should
2346 (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
2347 (org-drag-element-backward)
2348 (org-looking-at-p "2")))
2349 ;; Error when trying to move first element of buffer.
2350 (should-error
2351 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2352 (org-drag-element-backward)))
2353 ;; Error when trying to swap nested elements.
2354 (should-error
2355 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2356 (forward-line)
2357 (org-drag-element-backward)))
2358 ;; Error when trying to swap an headline element and a non-headline
2359 ;; element.
2360 (should-error
2361 (org-test-with-temp-text "Test.\n* Head 1"
2362 (forward-line)
2363 (org-drag-element-backward)))
2364 ;; Preserve visibility of elements and their contents.
2365 (should
2366 (equal '((63 . 82) (26 . 48))
2367 (org-test-with-temp-text "
2368 #+BEGIN_CENTER
2369 Text.
2370 #+END_CENTER
2371 - item 1
2372 #+BEGIN_QUOTE
2373 Text.
2374 #+END_QUOTE"
2375 (while (search-forward "BEGIN_" nil t) (org-cycle))
2376 (search-backward "- item 1")
2377 (org-drag-element-backward)
2378 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2379 (overlays-in (point-min) (point-max)))))))
2381 (ert-deftest test-org/drag-element-forward ()
2382 "Test `org-drag-element-forward' specifications."
2383 ;; 1. Error when trying to move first element of buffer.
2384 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2385 (goto-line 3)
2386 (should-error (org-drag-element-forward)))
2387 ;; 2. Error when trying to swap nested elements.
2388 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2389 (forward-line)
2390 (should-error (org-drag-element-forward)))
2391 ;; 3. Error when trying to swap a non-headline element and an
2392 ;; headline.
2393 (org-test-with-temp-text "Test.\n* Head 1"
2394 (should-error (org-drag-element-forward)))
2395 ;; 4. Otherwise, swap elements, preserving column and blank lines
2396 ;; between elements.
2397 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
2398 (search-forward "graph")
2399 (org-drag-element-forward)
2400 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
2401 (should (looking-at " 1")))
2402 ;; 5. Preserve visibility of elements and their contents.
2403 (org-test-with-temp-text "
2404 #+BEGIN_CENTER
2405 Text.
2406 #+END_CENTER
2407 - item 1
2408 #+BEGIN_QUOTE
2409 Text.
2410 #+END_QUOTE"
2411 (while (search-forward "BEGIN_" nil t) (org-cycle))
2412 (search-backward "#+BEGIN_CENTER")
2413 (org-drag-element-forward)
2414 (should
2415 (equal
2416 '((63 . 82) (26 . 48))
2417 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2418 (overlays-in (point-min) (point-max)))))))
2420 (ert-deftest test-org/next-block ()
2421 "Test `org-next-block' specifications."
2422 ;; Regular test.
2423 (should
2424 (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
2425 (org-next-block 1)
2426 (looking-at "#\\+BEGIN_CENTER")))
2427 ;; Ignore case.
2428 (should
2429 (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
2430 (let ((case-fold-search nil))
2431 (org-next-block 1)
2432 (looking-at "#\\+begin_center"))))
2433 ;; Ignore current line.
2434 (should
2435 (org-test-with-temp-text
2436 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
2437 (org-next-block 1)
2438 (looking-at "#\\+BEGIN_CENTER")))
2439 ;; Throw an error when no block is found.
2440 (should-error
2441 (org-test-with-temp-text "Paragraph"
2442 (org-next-block 1)))
2443 ;; With an argument, skip many blocks at once.
2444 (should
2445 (org-test-with-temp-text
2446 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
2447 (org-next-block 2)
2448 (looking-at "#\\+BEGIN_QUOTE")))
2449 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
2450 (should
2451 (org-test-with-temp-text
2452 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
2453 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
2454 (looking-at "#\\+BEGIN_QUOTE")))
2455 ;; Optional argument is also case-insensitive.
2456 (should
2457 (org-test-with-temp-text
2458 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
2459 (let ((case-fold-search nil))
2460 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
2461 (looking-at "#\\+begin_quote")))))
2463 (ert-deftest test-org/previous-block ()
2464 "Test `org-previous-block' specifications."
2465 ;; Regular test.
2466 (should
2467 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
2468 (org-previous-block 1)
2469 (looking-at "#\\+BEGIN_CENTER")))
2470 ;; Ignore case.
2471 (should
2472 (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
2473 (let ((case-fold-search nil))
2474 (org-previous-block 1)
2475 (looking-at "#\\+begin_center"))))
2476 ;; Ignore current line.
2477 (should
2478 (org-test-with-temp-text
2479 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
2480 (org-previous-block 1)
2481 (looking-at "#\\+BEGIN_QUOTE")))
2482 ;; Throw an error when no block is found.
2483 (should-error
2484 (org-test-with-temp-text "Paragraph<point>"
2485 (org-previous-block 1)))
2486 ;; With an argument, skip many blocks at once.
2487 (should
2488 (org-test-with-temp-text
2489 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
2490 (org-previous-block 2)
2491 (looking-at "#\\+BEGIN_CENTER")))
2492 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
2493 (should
2494 (org-test-with-temp-text
2495 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
2496 (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
2497 (looking-at "#\\+BEGIN_QUOTE")))
2498 ;; Optional argument is also case-insensitive.
2499 (should
2500 (org-test-with-temp-text
2501 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
2502 (let ((case-fold-search nil))
2503 (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
2504 (looking-at "#\\+begin_quote")))))
2507 ;;; Outline structure
2509 (ert-deftest test-org/demote ()
2510 "Test `org-demote' specifications."
2511 ;; Add correct number of stars according to `org-odd-levels-only'.
2512 (should
2513 (= 2
2514 (org-test-with-temp-text "* H"
2515 (let ((org-odd-levels-only nil)) (org-demote))
2516 (org-current-level))))
2517 (should
2518 (= 3
2519 (org-test-with-temp-text "* H"
2520 (let ((org-odd-levels-only t)) (org-demote))
2521 (org-current-level))))
2522 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2523 (should
2524 (org-test-with-temp-text "* H :tag:"
2525 (let ((org-tags-column 10)
2526 (org-auto-align-tags t)
2527 (org-odd-levels-only nil))
2528 (org-demote))
2529 (org-move-to-column 10)
2530 (org-looking-at-p ":tag:$")))
2531 (should-not
2532 (org-test-with-temp-text "* H :tag:"
2533 (let ((org-tags-column 10)
2534 (org-auto-align-tags nil)
2535 (org-odd-levels-only nil))
2536 (org-demote))
2537 (org-move-to-column 10)
2538 (org-looking-at-p ":tag:$")))
2539 ;; When `org-adapt-indentation' is non-nil, always indent planning
2540 ;; info and property drawers accordingly.
2541 (should
2542 (= 3
2543 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2544 (let ((org-odd-levels-only nil)
2545 (org-adapt-indentation t))
2546 (org-demote))
2547 (forward-line)
2548 (org-get-indentation))))
2549 (should
2550 (= 3
2551 (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2552 (let ((org-odd-levels-only nil)
2553 (org-adapt-indentation t))
2554 (org-demote))
2555 (forward-line)
2556 (org-get-indentation))))
2557 (should-not
2558 (= 3
2559 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2560 (let ((org-odd-levels-only nil)
2561 (org-adapt-indentation nil))
2562 (org-demote))
2563 (forward-line)
2564 (org-get-indentation))))
2565 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2566 ;; section accordingly. Ignore, however, footnote definitions and
2567 ;; inlinetasks boundaries.
2568 (should
2569 (= 3
2570 (org-test-with-temp-text "* H\n Paragraph"
2571 (let ((org-odd-levels-only nil)
2572 (org-adapt-indentation t))
2573 (org-demote))
2574 (forward-line)
2575 (org-get-indentation))))
2576 (should
2577 (= 2
2578 (org-test-with-temp-text "* H\n Paragraph"
2579 (let ((org-odd-levels-only nil)
2580 (org-adapt-indentation nil))
2581 (org-demote))
2582 (forward-line)
2583 (org-get-indentation))))
2584 (should
2585 (zerop
2586 (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
2587 (let ((org-odd-levels-only nil)
2588 (org-adapt-indentation t))
2589 (org-demote))
2590 (goto-char (point-max))
2591 (org-get-indentation))))
2592 (should
2593 (= 3
2594 (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
2595 (let ((org-odd-levels-only nil)
2596 (org-adapt-indentation t))
2597 (org-demote))
2598 (goto-char (point-max))
2599 (org-get-indentation))))
2600 (when (featurep 'org-inlinetask)
2601 (should
2602 (zerop
2603 (let ((org-inlinetask-min-level 5)
2604 (org-adapt-indentation t))
2605 (org-test-with-temp-text "* H\n***** I\n***** END"
2606 (org-demote)
2607 (forward-line)
2608 (org-get-indentation))))))
2609 (when (featurep 'org-inlinetask)
2610 (should
2611 (= 3
2612 (let ((org-inlinetask-min-level 5)
2613 (org-adapt-indentation t))
2614 (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
2615 (org-demote)
2616 (forward-line 2)
2617 (org-get-indentation))))))
2618 ;; Ignore contents of source blocks or example blocks when
2619 ;; indentation should be preserved (through
2620 ;; `org-src-preserve-indentation' or "-i" flag).
2621 (should-not
2622 (zerop
2623 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2624 (let ((org-adapt-indentation t)
2625 (org-src-preserve-indentation nil))
2626 (org-demote))
2627 (forward-line 2)
2628 (org-get-indentation))))
2629 (should
2630 (zerop
2631 (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2632 (let ((org-adapt-indentation t)
2633 (org-src-preserve-indentation t))
2634 (org-demote))
2635 (forward-line 2)
2636 (org-get-indentation))))
2637 (should
2638 (zerop
2639 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2640 (let ((org-adapt-indentation t)
2641 (org-src-preserve-indentation t))
2642 (org-demote))
2643 (forward-line 2)
2644 (org-get-indentation))))
2645 (should
2646 (zerop
2647 (org-test-with-temp-text
2648 "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
2649 (let ((org-adapt-indentation t)
2650 (org-src-preserve-indentation nil))
2651 (org-demote))
2652 (forward-line 2)
2653 (org-get-indentation)))))
2655 (ert-deftest test-org/promote ()
2656 "Test `org-promote' specifications."
2657 ;; Return an error if headline is to be promoted to level 0, unless
2658 ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
2659 ;; headline becomes a comment.
2660 (should-error
2661 (org-test-with-temp-text "* H"
2662 (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
2663 (should
2664 (equal "# H"
2665 (org-test-with-temp-text "* H"
2666 (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
2667 (buffer-string))))
2668 ;; Remove correct number of stars according to
2669 ;; `org-odd-levels-only'.
2670 (should
2671 (= 2
2672 (org-test-with-temp-text "*** H"
2673 (let ((org-odd-levels-only nil)) (org-promote))
2674 (org-current-level))))
2675 (should
2676 (= 1
2677 (org-test-with-temp-text "*** H"
2678 (let ((org-odd-levels-only t)) (org-promote))
2679 (org-current-level))))
2680 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2681 (should
2682 (org-test-with-temp-text "** H :tag:"
2683 (let ((org-tags-column 10)
2684 (org-auto-align-tags t)
2685 (org-odd-levels-only nil))
2686 (org-promote))
2687 (org-move-to-column 10)
2688 (org-looking-at-p ":tag:$")))
2689 (should-not
2690 (org-test-with-temp-text "** H :tag:"
2691 (let ((org-tags-column 10)
2692 (org-auto-align-tags nil)
2693 (org-odd-levels-only nil))
2694 (org-promote))
2695 (org-move-to-column 10)
2696 (org-looking-at-p ":tag:$")))
2697 ;; When `org-adapt-indentation' is non-nil, always indent planning
2698 ;; info and property drawers.
2699 (should
2700 (= 2
2701 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2702 (let ((org-odd-levels-only nil)
2703 (org-adapt-indentation t))
2704 (org-promote))
2705 (forward-line)
2706 (org-get-indentation))))
2707 (should
2708 (= 2
2709 (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2710 (let ((org-odd-levels-only nil)
2711 (org-adapt-indentation t))
2712 (org-promote))
2713 (forward-line)
2714 (org-get-indentation))))
2715 (should-not
2716 (= 2
2717 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2718 (let ((org-odd-levels-only nil)
2719 (org-adapt-indentation nil))
2720 (org-promote))
2721 (forward-line)
2722 (org-get-indentation))))
2723 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2724 ;; section accordingly. Ignore, however, footnote definitions and
2725 ;; inlinetasks boundaries.
2726 (should
2727 (= 2
2728 (org-test-with-temp-text "** H\n Paragraph"
2729 (let ((org-odd-levels-only nil)
2730 (org-adapt-indentation t))
2731 (org-promote))
2732 (forward-line)
2733 (org-get-indentation))))
2734 (should-not
2735 (= 2
2736 (org-test-with-temp-text "** H\n Paragraph"
2737 (let ((org-odd-levels-only nil)
2738 (org-adapt-indentation nil))
2739 (org-promote))
2740 (forward-line)
2741 (org-get-indentation))))
2742 (should
2743 (= 2
2744 (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
2745 (let ((org-odd-levels-only nil)
2746 (org-adapt-indentation t))
2747 (org-promote))
2748 (forward-line)
2749 (org-get-indentation))))
2750 (when (featurep 'org-inlinetask)
2751 (should
2752 (zerop
2753 (let ((org-inlinetask-min-level 5)
2754 (org-adapt-indentation t))
2755 (org-test-with-temp-text "** H\n***** I\n***** END"
2756 (org-promote)
2757 (forward-line)
2758 (org-get-indentation))))))
2759 (when (featurep 'org-inlinetask)
2760 (should
2761 (= 2
2762 (let ((org-inlinetask-min-level 5)
2763 (org-adapt-indentation t))
2764 (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
2765 (org-promote)
2766 (forward-line 2)
2767 (org-get-indentation))))))
2768 ;; Give up shifting if it would break document's structure
2769 ;; otherwise.
2770 (should
2771 (= 3
2772 (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
2773 (let ((org-odd-levels-only nil)
2774 (org-adapt-indentation t))
2775 (org-promote))
2776 (forward-line)
2777 (org-get-indentation))))
2778 (should
2779 (= 3
2780 (org-test-with-temp-text "** H\n Paragraph\n * list."
2781 (let ((org-odd-levels-only nil)
2782 (org-adapt-indentation t))
2783 (org-promote))
2784 (forward-line)
2785 (org-get-indentation))))
2786 ;; Ignore contents of source blocks or example blocks when
2787 ;; indentation should be preserved (through
2788 ;; `org-src-preserve-indentation' or "-i" flag).
2789 (should-not
2790 (zerop
2791 (org-test-with-temp-text
2792 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2793 (let ((org-adapt-indentation t)
2794 (org-src-preserve-indentation nil)
2795 (org-odd-levels-only nil))
2796 (org-promote))
2797 (forward-line)
2798 (org-get-indentation))))
2799 (should
2800 (zerop
2801 (org-test-with-temp-text
2802 "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
2803 (let ((org-adapt-indentation t)
2804 (org-src-preserve-indentation t)
2805 (org-odd-levels-only nil))
2806 (org-promote))
2807 (forward-line)
2808 (org-get-indentation))))
2809 (should
2810 (zerop
2811 (org-test-with-temp-text
2812 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2813 (let ((org-adapt-indentation t)
2814 (org-src-preserve-indentation t)
2815 (org-odd-levels-only nil))
2816 (org-promote))
2817 (forward-line)
2818 (org-get-indentation))))
2819 (should
2820 (zerop
2821 (org-test-with-temp-text
2822 "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
2823 (let ((org-adapt-indentation t)
2824 (org-src-preserve-indentation nil)
2825 (org-odd-levels-only nil))
2826 (org-promote))
2827 (forward-line)
2828 (org-get-indentation)))))
2831 ;;; Planning
2833 (ert-deftest test-org/at-planning-p ()
2834 "Test `org-at-planning-p' specifications."
2835 ;; Regular test.
2836 (should
2837 (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
2838 (org-at-planning-p)))
2839 (should-not
2840 (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
2841 (org-at-planning-p)))
2842 ;; Correctly find planning attached to inlinetasks.
2843 (when (featurep 'org-inlinetask)
2844 (should
2845 (org-test-with-temp-text
2846 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
2847 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2848 (should-not
2849 (org-test-with-temp-text
2850 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
2851 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2852 (should-not
2853 (org-test-with-temp-text
2854 "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
2855 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2856 (should-not
2857 (org-test-with-temp-text
2858 "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
2859 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
2861 (ert-deftest test-org/add-planning-info ()
2862 "Test `org-add-planning-info'."
2863 ;; Create deadline when `org-adapt-indentation' is non-nil.
2864 (should
2865 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
2866 (org-test-with-temp-text "* H\nParagraph<point>"
2867 (let ((org-adapt-indentation t))
2868 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
2869 (replace-regexp-in-string
2870 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
2871 nil nil 1))))
2872 ;; Create deadline when `org-adapt-indentation' is nil.
2873 (should
2874 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
2875 (org-test-with-temp-text "* H\nParagraph<point>"
2876 (let ((org-adapt-indentation nil))
2877 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
2878 (replace-regexp-in-string
2879 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
2880 nil nil 1))))
2881 ;; Update deadline when `org-adapt-indentation' is non-nil.
2882 (should
2883 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
2884 (org-test-with-temp-text "\
2886 DEADLINE: <2015-06-24 Wed>
2887 Paragraph<point>"
2888 (let ((org-adapt-indentation t))
2889 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
2890 (replace-regexp-in-string
2891 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
2892 nil nil 1))))
2893 ;; Update deadline when `org-adapt-indentation' is nil.
2894 (should
2895 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
2896 (org-test-with-temp-text "\
2898 DEADLINE: <2015-06-24 Wed>
2899 Paragraph<point>"
2900 (let ((org-adapt-indentation nil))
2901 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
2902 (replace-regexp-in-string
2903 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
2904 nil nil 1))))
2905 ;; Schedule when `org-adapt-indentation' is non-nil.
2906 (should
2907 (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
2908 (org-test-with-temp-text "* H\nParagraph<point>"
2909 (let ((org-adapt-indentation t))
2910 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
2911 (replace-regexp-in-string
2912 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
2913 nil nil 1))))
2914 ;; Schedule when `org-adapt-indentation' is nil.
2915 (should
2916 (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
2917 (org-test-with-temp-text "* H\nParagraph<point>"
2918 (let ((org-adapt-indentation nil))
2919 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
2920 (replace-regexp-in-string
2921 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
2922 nil nil 1))))
2923 ;; Add deadline when scheduled.
2924 (should
2925 (equal "\
2927 DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
2928 Paragraph"
2929 (org-test-with-temp-text "\
2931 SCHEDULED: <2015-06-24 Wed>
2932 Paragraph<point>"
2933 (let ((org-adapt-indentation t))
2934 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
2935 (replace-regexp-in-string
2936 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
2937 nil nil 1))))
2938 ;; Remove middle entry.
2939 (should
2940 (equal "\
2942 CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
2943 Paragraph"
2944 (org-test-with-temp-text "\
2946 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
2947 Paragraph<point>"
2948 (let ((org-adapt-indentation t))
2949 (org-add-planning-info nil nil 'deadline))
2950 (replace-regexp-in-string
2951 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
2952 nil nil 1))))
2953 ;; Remove last entry and then middle entry (order should not
2954 ;; matter).
2955 (should
2956 (equal "\
2958 CLOSED: [2015-06-24]
2959 Paragraph"
2960 (org-test-with-temp-text "\
2962 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
2963 Paragraph<point>"
2964 (let ((org-adapt-indentation t))
2965 (org-add-planning-info nil nil 'scheduled 'deadline))
2966 (replace-regexp-in-string
2967 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
2968 nil nil 1))))
2969 ;; Remove closed when `org-adapt-indentation' is non-nil.
2970 (should
2971 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
2972 (org-test-with-temp-text "\
2974 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
2975 Paragraph<point>"
2976 (let ((org-adapt-indentation t))
2977 (org-add-planning-info nil nil 'closed))
2978 (replace-regexp-in-string
2979 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
2980 nil nil 1))))
2981 ;; Remove closed when `org-adapt-indentation' is nil.
2982 (should
2983 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
2984 (org-test-with-temp-text "\
2986 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
2987 Paragraph<point>"
2988 (let ((org-adapt-indentation nil))
2989 (org-add-planning-info nil nil 'closed))
2990 (replace-regexp-in-string
2991 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
2992 nil nil 1))))
2993 ;; Remove closed entry and delete empty line.
2994 (should
2995 (equal "\
2997 Paragraph"
2998 (org-test-with-temp-text "\
3000 CLOSED: [2015-06-24 Wed]
3001 Paragraph<point>"
3002 (let ((org-adapt-indentation t))
3003 (org-add-planning-info nil nil 'closed))
3004 (replace-regexp-in-string
3005 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3006 nil nil 1))))
3007 ;; Remove one entry and update another.
3008 (should
3009 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3010 (org-test-with-temp-text "\
3012 SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
3013 Paragraph<point>"
3014 (let ((org-adapt-indentation t))
3015 (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
3016 (replace-regexp-in-string
3017 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3018 nil nil 1)))))
3021 ;;; Property API
3023 (ert-deftest test-org/buffer-property-keys ()
3024 "Test `org-buffer-property-keys' specifications."
3025 ;; Retrieve properties accross siblings.
3026 (should
3027 (equal '("A" "B")
3028 (org-test-with-temp-text "
3029 * H1
3030 :PROPERTIES:
3031 :A: 1
3032 :END:
3033 * H2
3034 :PROPERTIES:
3035 :B: 1
3036 :END:"
3037 (org-buffer-property-keys))))
3038 ;; Retrieve properties accross children.
3039 (should
3040 (equal '("A" "B")
3041 (org-test-with-temp-text "
3042 * H1
3043 :PROPERTIES:
3044 :A: 1
3045 :END:
3046 ** H2
3047 :PROPERTIES:
3048 :B: 1
3049 :END:"
3050 (org-buffer-property-keys))))
3051 ;; Retrieve muliple properties in the same drawer.
3052 (should
3053 (equal '("A" "B")
3054 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3055 (org-buffer-property-keys))))
3056 ;; Ignore extension symbol in property name.
3057 (should
3058 (equal '("A")
3059 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
3060 (org-buffer-property-keys))))
3061 ;; With non-nil COLUMNS, extract property names from columns.
3062 (should
3063 (equal '("A" "B")
3064 (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
3065 (org-buffer-property-keys nil nil t))))
3066 (should
3067 (equal '("A" "B" "COLUMNS")
3068 (org-test-with-temp-text
3069 "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
3070 (org-buffer-property-keys nil nil t)))))
3072 (ert-deftest test-org/property-values ()
3073 "Test `org-property-values' specifications."
3074 ;; Regular test.
3075 (should
3076 (equal '("2" "1")
3077 (org-test-with-temp-text
3078 "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
3079 (org-property-values "A"))))
3080 ;; Ignore empty values.
3081 (should-not
3082 (org-test-with-temp-text
3083 "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
3084 (org-property-values "A")))
3085 ;; Take into consideration extended values.
3086 (should
3087 (equal '("1 2")
3088 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
3089 (org-property-values "A")))))
3091 (ert-deftest test-org/find-property ()
3092 "Test `org-find-property' specifications."
3093 ;; Regular test.
3094 (should
3095 (= 1
3096 (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
3097 (org-find-property "prop"))))
3098 ;; Ignore false positives.
3099 (should
3100 (= 27
3101 (org-test-with-temp-text
3102 "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
3103 (org-find-property "A"))))
3104 ;; Return first entry found in buffer.
3105 (should
3106 (= 1
3107 (org-test-with-temp-text
3108 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
3109 (org-find-property "A"))))
3110 ;; Only search visible part of the buffer.
3111 (should
3112 (= 31
3113 (org-test-with-temp-text
3114 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
3115 (org-narrow-to-subtree)
3116 (org-find-property "A"))))
3117 ;; With optional argument, only find entries with a specific value.
3118 (should-not
3119 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3120 (org-find-property "A" "2")))
3121 (should
3122 (= 31
3123 (org-test-with-temp-text
3124 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
3125 (org-find-property "A" "2"))))
3126 ;; Use "nil" for explicit nil values.
3127 (should
3128 (= 31
3129 (org-test-with-temp-text
3130 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
3131 (org-find-property "A" "nil")))))
3133 (ert-deftest test-org/entry-delete ()
3134 "Test `org-entry-delete' specifications."
3135 ;; Regular test.
3136 (should
3137 (string-match
3138 " *:PROPERTIES:\n *:B: +2\n *:END:"
3139 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3140 (org-entry-delete (point) "A")
3141 (buffer-string))))
3142 ;; Also remove accumulated properties.
3143 (should-not
3144 (string-match
3145 ":A"
3146 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
3147 (org-entry-delete (point) "A")
3148 (buffer-string))))
3149 ;; When last property is removed, remove the property drawer.
3150 (should-not
3151 (string-match
3152 ":PROPERTIES:"
3153 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
3154 (org-entry-delete (point) "A")
3155 (buffer-string))))
3156 ;; Return a non-nil value when some property was removed.
3157 (should
3158 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3159 (org-entry-delete (point) "A")))
3160 (should-not
3161 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3162 (org-entry-delete (point) "C"))))
3164 (ert-deftest test-org/entry-get ()
3165 "Test `org-entry-get' specifications."
3166 ;; Regular test.
3167 (should
3168 (equal "1"
3169 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3170 (org-entry-get (point) "A"))))
3171 ;; Ignore case.
3172 (should
3173 (equal "1"
3174 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3175 (org-entry-get (point) "a"))))
3176 ;; Handle extended values, both before and after base value.
3177 (should
3178 (equal "1 2 3"
3179 (org-test-with-temp-text
3180 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
3181 (org-entry-get (point) "A"))))
3182 ;; Empty values are returned as the empty string.
3183 (should
3184 (equal ""
3185 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
3186 (org-entry-get (point) "A"))))
3187 ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
3188 ;; otherwise, return nil.
3189 (should-not
3190 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
3191 (org-entry-get (point) "A")))
3192 (should
3193 (equal "nil"
3194 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
3195 (org-entry-get (point) "A" nil t))))
3196 ;; Return nil when no property is found, independently on the
3197 ;; LITERAL-NIL argument.
3198 (should-not
3199 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3200 (org-entry-get (point) "B")))
3201 (should-not
3202 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3203 (org-entry-get (point) "B" nil t)))
3204 ;; Handle inheritance, when allowed. Include extended values and
3205 ;; possibly global values.
3206 (should
3207 (equal
3209 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3210 (org-entry-get (point) "A" t))))
3211 (should
3212 (equal
3214 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3215 (let ((org-use-property-inheritance t))
3216 (org-entry-get (point) "A" 'selective)))))
3217 (should-not
3218 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3219 (let ((org-use-property-inheritance nil))
3220 (org-entry-get (point) "A" 'selective))))
3221 (should
3222 (equal
3223 "1 2"
3224 (org-test-with-temp-text
3225 "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
3226 (org-entry-get (point-max) "A" t))))
3227 (should
3228 (equal "1"
3229 (org-test-with-temp-text
3230 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
3231 (org-mode-restart)
3232 (org-entry-get (point-max) "A" t))))
3233 (should
3234 (equal "0 1"
3235 (org-test-with-temp-text
3236 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
3237 (org-mode-restart)
3238 (org-entry-get (point-max) "A" t)))))
3240 (ert-deftest test-org/entry-properties ()
3241 "Test `org-entry-properties' specifications."
3242 ;; Get "ITEM" property.
3243 (should
3244 (equal "* H"
3245 (org-test-with-temp-text "* TODO H"
3246 (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
3247 (should
3248 (equal "* H"
3249 (org-test-with-temp-text "* TODO H"
3250 (cdr (assoc "ITEM" (org-entry-properties))))))
3251 ;; Get "TODO" property.
3252 (should
3253 (equal "TODO"
3254 (org-test-with-temp-text "* TODO H"
3255 (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
3256 (should
3257 (equal "TODO"
3258 (org-test-with-temp-text "* TODO H"
3259 (cdr (assoc "TODO" (org-entry-properties))))))
3260 (should-not
3261 (org-test-with-temp-text "* H"
3262 (assoc "TODO" (org-entry-properties nil "TODO"))))
3263 ;; Get "PRIORITY" property.
3264 (should
3265 (equal "A"
3266 (org-test-with-temp-text "* [#A] H"
3267 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
3268 (should
3269 (equal "A"
3270 (org-test-with-temp-text "* [#A] H"
3271 (cdr (assoc "PRIORITY" (org-entry-properties))))))
3272 (should-not
3273 (org-test-with-temp-text "* H"
3274 (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
3275 ;; Get "FILE" property.
3276 (should
3277 (org-test-with-temp-text-in-file "* H\nParagraph"
3278 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
3279 (buffer-file-name))))
3280 (should
3281 (org-test-with-temp-text-in-file "* H\nParagraph"
3282 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
3283 (buffer-file-name))))
3284 (should-not
3285 (org-test-with-temp-text "* H\nParagraph"
3286 (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
3287 ;; Get "TAGS" property.
3288 (should
3289 (equal ":tag1:tag2:"
3290 (org-test-with-temp-text "* H :tag1:tag2:"
3291 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
3292 (should
3293 (equal ":tag1:tag2:"
3294 (org-test-with-temp-text "* H :tag1:tag2:"
3295 (cdr (assoc "TAGS" (org-entry-properties))))))
3296 (should-not
3297 (org-test-with-temp-text "* H"
3298 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
3299 ;; Get "ALLTAGS" property.
3300 (should
3301 (equal ":tag1:tag2:"
3302 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
3303 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
3304 (should
3305 (equal ":tag1:tag2:"
3306 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
3307 (cdr (assoc "ALLTAGS" (org-entry-properties))))))
3308 (should-not
3309 (org-test-with-temp-text "* H"
3310 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
3311 ;; Get "BLOCKED" property.
3312 (should
3313 (equal "t"
3314 (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
3315 (let ((org-enforce-todo-dependencies t)
3316 (org-blocker-hook
3317 '(org-block-todo-from-children-or-siblings-or-parent)))
3318 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
3319 (should
3320 (equal "t"
3321 (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
3322 (let ((org-enforce-todo-dependencies t)
3323 (org-blocker-hook
3324 '(org-block-todo-from-children-or-siblings-or-parent)))
3325 (cdr (assoc "BLOCKED" (org-entry-properties)))))))
3326 (should
3327 (equal ""
3328 (org-test-with-temp-text "* Blocked\n** DONE one\n** DONE two"
3329 (let ((org-enforce-todo-dependencies t))
3330 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
3331 ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
3332 (should
3333 (equal
3334 "[2012-03-29 thu.]"
3335 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
3336 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
3337 (should
3338 (equal
3339 "[2012-03-29 thu.]"
3340 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
3341 (cdr (assoc "CLOSED" (org-entry-properties))))))
3342 (should-not
3343 (org-test-with-temp-text "* H"
3344 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
3345 (should
3346 (equal
3347 "<2014-03-04 tue.>"
3348 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3349 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
3350 (should
3351 (equal
3352 "<2014-03-04 tue.>"
3353 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3354 (cdr (assoc "DEADLINE" (org-entry-properties))))))
3355 (should-not
3356 (org-test-with-temp-text "* H"
3357 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
3358 (should
3359 (equal
3360 "<2014-03-04 tue.>"
3361 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3362 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
3363 (should
3364 (equal
3365 "<2014-03-04 tue.>"
3366 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3367 (cdr (assoc "SCHEDULED" (org-entry-properties))))))
3368 (should-not
3369 (org-test-with-temp-text "* H"
3370 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
3371 ;; Get "CATEGORY"
3372 (should
3373 (equal "cat"
3374 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
3375 (cdr (assoc "CATEGORY" (org-entry-properties))))))
3376 (should
3377 (equal "cat"
3378 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
3379 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3380 (should
3381 (equal "cat"
3382 (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
3383 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3384 (should
3385 (equal "cat2"
3386 (org-test-with-temp-text
3387 (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
3388 "\n"
3389 "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
3390 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3391 ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
3392 (should
3393 (equal "<2012-03-29 thu.>"
3394 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
3395 (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
3396 (should
3397 (equal "[2012-03-29 thu.]"
3398 (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
3399 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
3400 (should
3401 (equal "<2012-03-29 thu.>"
3402 (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
3403 (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
3404 (should
3405 (equal "[2014-03-04 tue.]"
3406 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
3407 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties nil "TIMESTAMP_IA"))))))
3408 ;; Get standard properties.
3409 (should
3410 (equal "1"
3411 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3412 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3413 ;; Handle extended properties.
3414 (should
3415 (equal "1 2 3"
3416 (org-test-with-temp-text
3417 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
3418 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3419 (should
3420 (equal "1 2 3"
3421 (org-test-with-temp-text
3422 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
3423 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3424 ;; Ignore forbidden (special) properties.
3425 (should-not
3426 (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
3427 (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
3429 (ert-deftest test-org/entry-put ()
3430 "Test `org-entry-put' specifications."
3431 ;; Error when not a string or nil.
3432 (should-error
3433 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
3434 (org-entry-put 1 "test" 2)))
3435 ;; Set "TODO" property.
3436 (should
3437 (string-match (regexp-quote " TODO H")
3438 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
3439 (org-entry-put (point) "TODO" "TODO")
3440 (buffer-string))))
3441 (should
3442 (string-match (regexp-quote "* H")
3443 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
3444 (org-entry-put (point) "TODO" nil)
3445 (buffer-string))))
3446 ;; Set "PRIORITY" property.
3447 (should
3448 (equal "* [#A] H"
3449 (org-test-with-temp-text "* [#B] H"
3450 (org-entry-put (point) "PRIORITY" "A")
3451 (buffer-string))))
3452 (should
3453 (equal "* H"
3454 (org-test-with-temp-text "* [#B] H"
3455 (org-entry-put (point) "PRIORITY" nil)
3456 (buffer-string))))
3457 ;; Set "SCHEDULED" property.
3458 (should
3459 (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
3460 (org-test-with-temp-text "* H"
3461 (org-entry-put (point) "SCHEDULED" "2014-03-04")
3462 (buffer-string))))
3463 (should
3464 (string= "* H\n"
3465 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3466 (org-entry-put (point) "SCHEDULED" nil)
3467 (buffer-string))))
3468 (should
3469 (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
3470 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3471 (org-entry-put (point) "SCHEDULED" "earlier")
3472 (buffer-string))))
3473 (should
3474 (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
3475 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3476 (org-entry-put (point) "SCHEDULED" "later")
3477 (buffer-string))))
3478 ;; Set "DEADLINE" property.
3479 (should
3480 (string-match "^ *DEADLINE: <2014-03-04 .*?>"
3481 (org-test-with-temp-text "* H"
3482 (org-entry-put (point) "DEADLINE" "2014-03-04")
3483 (buffer-string))))
3484 (should
3485 (string= "* H\n"
3486 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3487 (org-entry-put (point) "DEADLINE" nil)
3488 (buffer-string))))
3489 (should
3490 (string-match "^ *DEADLINE: <2014-03-03 .*?>"
3491 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3492 (org-entry-put (point) "DEADLINE" "earlier")
3493 (buffer-string))))
3494 (should
3495 (string-match "^ *DEADLINE: <2014-03-05 .*?>"
3496 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3497 (org-entry-put (point) "DEADLINE" "later")
3498 (buffer-string))))
3499 ;; Set "CATEGORY" property
3500 (should
3501 (string-match "^ *:CATEGORY: cat"
3502 (org-test-with-temp-text "* H"
3503 (org-entry-put (point) "CATEGORY" "cat")
3504 (buffer-string))))
3505 ;; Regular properties, with or without pre-existing drawer.
3506 (should
3507 (string-match "^ *:A: +2$"
3508 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3509 (org-entry-put (point) "A" "2")
3510 (buffer-string))))
3511 (should
3512 (string-match "^ *:A: +1$"
3513 (org-test-with-temp-text "* H"
3514 (org-entry-put (point) "A" "1")
3515 (buffer-string))))
3516 ;; Special case: two consecutive headlines.
3517 (should
3518 (string-match "\\* A\n *:PROPERTIES:"
3519 (org-test-with-temp-text "* A\n** B"
3520 (org-entry-put (point) "A" "1")
3521 (buffer-string)))))
3524 ;;; Radio Targets
3526 (ert-deftest test-org/update-radio-target-regexp ()
3527 "Test `org-update-radio-target-regexp' specifications."
3528 ;; Properly update cache with no previous radio target regexp.
3529 (should
3530 (eq 'link
3531 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3532 (save-excursion (goto-char (point-max)) (org-element-context))
3533 (insert "<<<")
3534 (search-forward "o")
3535 (insert ">>>")
3536 (org-update-radio-target-regexp)
3537 (goto-char (point-max))
3538 (org-element-type (org-element-context)))))
3539 ;; Properly update cache with previous radio target regexp.
3540 (should
3541 (eq 'link
3542 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3543 (save-excursion (goto-char (point-max)) (org-element-context))
3544 (insert "<<<")
3545 (search-forward "o")
3546 (insert ">>>")
3547 (org-update-radio-target-regexp)
3548 (search-backward "r")
3549 (delete-char 5)
3550 (insert "new")
3551 (org-update-radio-target-regexp)
3552 (goto-char (point-max))
3553 (delete-region (line-beginning-position) (point))
3554 (insert "new")
3555 (org-element-type (org-element-context))))))
3558 ;;; Sparse trees
3560 (ert-deftest test-org/match-sparse-tree ()
3561 "Test `org-match-sparse-tree' specifications."
3562 ;; Match tags.
3563 (should-not
3564 (org-test-with-temp-text "* H\n** H1 :tag:"
3565 (org-match-sparse-tree nil "tag")
3566 (search-forward "H1")
3567 (org-invisible-p2)))
3568 (should
3569 (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
3570 (org-match-sparse-tree nil "tag")
3571 (search-forward "H2")
3572 (org-invisible-p2)))
3573 ;; "-" operator for tags.
3574 (should-not
3575 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3576 (org-match-sparse-tree nil "tag1-tag2")
3577 (search-forward "H1")
3578 (org-invisible-p2)))
3579 (should
3580 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3581 (org-match-sparse-tree nil "tag1-tag2")
3582 (search-forward "H2")
3583 (org-invisible-p2)))
3584 ;; "&" operator for tags.
3585 (should
3586 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3587 (org-match-sparse-tree nil "tag1&tag2")
3588 (search-forward "H1")
3589 (org-invisible-p2)))
3590 (should-not
3591 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3592 (org-match-sparse-tree nil "tag1&tag2")
3593 (search-forward "H2")
3594 (org-invisible-p2)))
3595 ;; "|" operator for tags.
3596 (should-not
3597 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3598 (org-match-sparse-tree nil "tag1|tag2")
3599 (search-forward "H1")
3600 (org-invisible-p2)))
3601 (should-not
3602 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3603 (org-match-sparse-tree nil "tag1|tag2")
3604 (search-forward "H2")
3605 (org-invisible-p2)))
3606 ;; Regexp match on tags.
3607 (should-not
3608 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3609 (org-match-sparse-tree nil "{^tag.*}")
3610 (search-forward "H1")
3611 (org-invisible-p2)))
3612 (should
3613 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3614 (org-match-sparse-tree nil "{^tag.*}")
3615 (search-forward "H2")
3616 (org-invisible-p2)))
3617 ;; Match group tags.
3618 (should-not
3619 (org-test-with-temp-text
3620 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3621 (org-match-sparse-tree nil "work")
3622 (search-forward "H1")
3623 (org-invisible-p2)))
3624 (should-not
3625 (org-test-with-temp-text
3626 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3627 (org-match-sparse-tree nil "work")
3628 (search-forward "H2")
3629 (org-invisible-p2)))
3630 ;; Match group tags with hard brackets.
3631 (should-not
3632 (org-test-with-temp-text
3633 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
3634 (org-match-sparse-tree nil "work")
3635 (search-forward "H1")
3636 (org-invisible-p2)))
3637 (should-not
3638 (org-test-with-temp-text
3639 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
3640 (org-match-sparse-tree nil "work")
3641 (search-forward "H2")
3642 (org-invisible-p2)))
3643 ;; Match tags in hierarchies
3644 (should-not
3645 (org-test-with-temp-text
3646 "#+TAGS: [ Lev_1 : Lev_2 ]\n
3647 #+TAGS: [ Lev_2 : Lev_3 ]\n
3648 #+TAGS: { Lev_3 : Lev_4 }\n
3649 * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
3650 (org-match-sparse-tree nil "Lev_1")
3651 (search-forward "H4")
3652 (org-invisible-p2)))
3653 ;; Match regular expressions in tags
3654 (should-not
3655 (org-test-with-temp-text
3656 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
3657 (org-match-sparse-tree nil "Lev")
3658 (search-forward "H1")
3659 (org-invisible-p2)))
3660 (should
3661 (org-test-with-temp-text
3662 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
3663 (org-match-sparse-tree nil "Lev")
3664 (search-forward "H1")
3665 (org-invisible-p2)))
3666 ;; Match properties.
3667 (should
3668 (org-test-with-temp-text
3669 "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
3670 (org-match-sparse-tree nil "A=\"1\"")
3671 (search-forward "H2")
3672 (org-invisible-p2)))
3673 (should-not
3674 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3675 (org-match-sparse-tree nil "A=\"1\"")
3676 (search-forward "H2")
3677 (org-invisible-p2)))
3678 ;; Case is not significant when matching properties.
3679 (should-not
3680 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3681 (org-match-sparse-tree nil "a=\"1\"")
3682 (search-forward "H2")
3683 (org-invisible-p2)))
3684 (should-not
3685 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
3686 (org-match-sparse-tree nil "A=\"1\"")
3687 (search-forward "H2")
3688 (org-invisible-p2)))
3689 ;; Match special LEVEL property.
3690 (should-not
3691 (org-test-with-temp-text "* H\n** H1\n*** H2"
3692 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3693 (search-forward "H1")
3694 (org-invisible-p2)))
3695 (should
3696 (org-test-with-temp-text "* H\n** H1\n*** H2"
3697 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3698 (search-forward "H2")
3699 (org-invisible-p2)))
3700 ;; Comparison operators when matching properties.
3701 (should
3702 (org-test-with-temp-text
3703 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3704 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3705 (search-forward "H1")
3706 (org-invisible-p2)))
3707 (should-not
3708 (org-test-with-temp-text
3709 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3710 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3711 (search-forward "H2")
3712 (org-invisible-p2)))
3713 ;; Regexp match on properties values.
3714 (should-not
3715 (org-test-with-temp-text
3716 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3717 (org-match-sparse-tree nil "A={f.*}")
3718 (search-forward "H1")
3719 (org-invisible-p2)))
3720 (should
3721 (org-test-with-temp-text
3722 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3723 (org-match-sparse-tree nil "A={f.*}")
3724 (search-forward "H2")
3725 (org-invisible-p2)))
3726 ;; With an optional argument, limit match to TODO entries.
3727 (should-not
3728 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3729 (org-match-sparse-tree t "tag")
3730 (search-forward "H1")
3731 (org-invisible-p2)))
3732 (should
3733 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3734 (org-match-sparse-tree t "tag")
3735 (search-forward "H2")
3736 (org-invisible-p2))))
3739 ;;; Timestamps API
3741 (ert-deftest test-org/time-stamp ()
3742 "Test `org-time-stamp' specifications."
3743 ;; Insert chosen time stamp at point.
3744 (should
3745 (string-match
3746 "Te<2014-03-04 .*?>xt"
3747 (org-test-with-temp-text "Te<point>xt"
3748 (flet ((org-read-date
3749 (&rest args)
3750 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3751 (org-time-stamp nil)
3752 (buffer-string)))))
3753 ;; With a prefix argument, also insert time.
3754 (should
3755 (string-match
3756 "Te<2014-03-04 .*? 00:41>xt"
3757 (org-test-with-temp-text "Te<point>xt"
3758 (flet ((org-read-date
3759 (&rest args)
3760 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
3761 (org-time-stamp '(4))
3762 (buffer-string)))))
3763 ;; With two universal prefix arguments, insert an active timestamp
3764 ;; with the current time without prompting the user.
3765 (should
3766 (string-match
3767 "Te<2014-03-04 .*? 00:41>xt"
3768 (org-test-with-temp-text "Te<point>xt"
3769 (flet ((current-time
3771 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
3772 (org-time-stamp '(16))
3773 (buffer-string)))))
3774 ;; When optional argument is non-nil, insert an inactive timestamp.
3775 (should
3776 (string-match
3777 "Te\\[2014-03-04 .*?\\]xt"
3778 (org-test-with-temp-text "Te<point>xt"
3779 (flet ((org-read-date
3780 (&rest args)
3781 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3782 (org-time-stamp nil t)
3783 (buffer-string)))))
3784 ;; When called from a timestamp, replace existing one.
3785 (should
3786 (string-match
3787 "<2014-03-04 .*?>"
3788 (org-test-with-temp-text "<2012-03-29<point> thu.>"
3789 (flet ((org-read-date
3790 (&rest args)
3791 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3792 (org-time-stamp nil)
3793 (buffer-string)))))
3794 (should
3795 (string-match
3796 "<2014-03-04 .*?>--<2014-03-04 .*?>"
3797 (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
3798 (flet ((org-read-date
3799 (&rest args)
3800 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3801 (org-time-stamp nil)
3802 (buffer-string)))))
3803 ;; When replacing a timestamp, preserve repeater, if any.
3804 (should
3805 (string-match
3806 "<2014-03-04 .*? \\+2y>"
3807 (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
3808 (flet ((org-read-date
3809 (&rest args)
3810 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3811 (org-time-stamp nil)
3812 (buffer-string)))))
3813 ;; When called twice in a raw, build a date range.
3814 (should
3815 (string-match
3816 "<2012-03-29 .*?>--<2014-03-04 .*?>"
3817 (org-test-with-temp-text "<2012-03-29 thu.><point>"
3818 (flet ((org-read-date
3819 (&rest args)
3820 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3821 (let ((last-command 'org-time-stamp)
3822 (this-command 'org-time-stamp))
3823 (org-time-stamp nil))
3824 (buffer-string))))))
3826 (ert-deftest test-org/timestamp-has-time-p ()
3827 "Test `org-timestamp-has-time-p' specifications."
3828 ;; With time.
3829 (should
3830 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
3831 (org-timestamp-has-time-p (org-element-context))))
3832 ;; Without time.
3833 (should-not
3834 (org-test-with-temp-text "<2012-03-29 Thu>"
3835 (org-timestamp-has-time-p (org-element-context)))))
3837 (ert-deftest test-org/timestamp-format ()
3838 "Test `org-timestamp-format' specifications."
3839 ;; Regular test.
3840 (should
3841 (equal
3842 "2012-03-29 16:40"
3843 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
3844 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
3845 ;; Range end.
3846 (should
3847 (equal
3848 "2012-03-29"
3849 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
3850 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
3852 (ert-deftest test-org/timestamp-split-range ()
3853 "Test `org-timestamp-split-range' specifications."
3854 ;; Extract range start (active).
3855 (should
3856 (equal '(2012 3 29)
3857 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3858 (let ((ts (org-timestamp-split-range (org-element-context))))
3859 (mapcar (lambda (p) (org-element-property p ts))
3860 '(:year-end :month-end :day-end))))))
3861 ;; Extract range start (inactive)
3862 (should
3863 (equal '(2012 3 29)
3864 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
3865 (let ((ts (org-timestamp-split-range (org-element-context))))
3866 (mapcar (lambda (p) (org-element-property p ts))
3867 '(:year-end :month-end :day-end))))))
3868 ;; Extract range end (active).
3869 (should
3870 (equal '(2012 3 30)
3871 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3872 (let ((ts (org-timestamp-split-range
3873 (org-element-context) t)))
3874 (mapcar (lambda (p) (org-element-property p ts))
3875 '(:year-end :month-end :day-end))))))
3876 ;; Extract range end (inactive)
3877 (should
3878 (equal '(2012 3 30)
3879 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
3880 (let ((ts (org-timestamp-split-range
3881 (org-element-context) t)))
3882 (mapcar (lambda (p) (org-element-property p ts))
3883 '(:year-end :month-end :day-end))))))
3884 ;; Return the timestamp if not a range.
3885 (should
3886 (org-test-with-temp-text "[2012-03-29 Thu]"
3887 (let* ((ts-orig (org-element-context))
3888 (ts-copy (org-timestamp-split-range ts-orig)))
3889 (eq ts-orig ts-copy))))
3890 (should
3891 (org-test-with-temp-text "<%%(org-float t 4 2)>"
3892 (let* ((ts-orig (org-element-context))
3893 (ts-copy (org-timestamp-split-range ts-orig)))
3894 (eq ts-orig ts-copy)))))
3896 (ert-deftest test-org/timestamp-translate ()
3897 "Test `org-timestamp-translate' specifications."
3898 ;; Translate whole date range.
3899 (should
3900 (equal "<29>--<30>"
3901 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3902 (let ((org-display-custom-times t)
3903 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3904 (org-timestamp-translate (org-element-context))))))
3905 ;; Translate date range start.
3906 (should
3907 (equal "<29>"
3908 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3909 (let ((org-display-custom-times t)
3910 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3911 (org-timestamp-translate (org-element-context) 'start)))))
3912 ;; Translate date range end.
3913 (should
3914 (equal "<30>"
3915 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3916 (let ((org-display-custom-times t)
3917 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3918 (org-timestamp-translate (org-element-context) 'end)))))
3919 ;; Translate time range.
3920 (should
3921 (equal "<08>--<16>"
3922 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
3923 (let ((org-display-custom-times t)
3924 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
3925 (org-timestamp-translate (org-element-context))))))
3926 ;; Translate non-range timestamp.
3927 (should
3928 (equal "<29>"
3929 (org-test-with-temp-text "<2012-03-29 Thu>"
3930 (let ((org-display-custom-times t)
3931 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3932 (org-timestamp-translate (org-element-context))))))
3933 ;; Do not change `diary' timestamps.
3934 (should
3935 (equal "<%%(org-float t 4 2)>"
3936 (org-test-with-temp-text "<%%(org-float t 4 2)>"
3937 (let ((org-display-custom-times t)
3938 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3939 (org-timestamp-translate (org-element-context)))))))
3943 ;;; Visibility
3945 (ert-deftest test-org/flag-drawer ()
3946 "Test `org-flag-drawer' specifications."
3947 ;; Hide drawer.
3948 (should
3949 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3950 (org-flag-drawer t)
3951 (get-char-property (line-end-position) 'invisible)))
3952 ;; Show drawer.
3953 (should-not
3954 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3955 (org-flag-drawer t)
3956 (org-flag-drawer nil)
3957 (get-char-property (line-end-position) 'invisible)))
3958 ;; Test optional argument.
3959 (should
3960 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
3961 (let ((drawer (save-excursion (search-forward ":D2")
3962 (org-element-at-point))))
3963 (org-flag-drawer t drawer)
3964 (get-char-property (progn (search-forward ":D2") (line-end-position))
3965 'invisible))))
3966 (should-not
3967 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
3968 (let ((drawer (save-excursion (search-forward ":D2")
3969 (org-element-at-point))))
3970 (org-flag-drawer t drawer)
3971 (get-char-property (line-end-position) 'invisible))))
3972 ;; Do not hide fake drawers.
3973 (should-not
3974 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
3975 (forward-line 1)
3976 (org-flag-drawer t)
3977 (get-char-property (line-end-position) 'invisible)))
3978 ;; Do not hide incomplete drawers.
3979 (should-not
3980 (org-test-with-temp-text ":D:\nparagraph"
3981 (forward-line 1)
3982 (org-flag-drawer t)
3983 (get-char-property (line-end-position) 'invisible)))
3984 ;; Do not hide drawers when called from final blank lines.
3985 (should-not
3986 (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
3987 (goto-char (point-max))
3988 (org-flag-drawer t)
3989 (goto-char (point-min))
3990 (get-char-property (line-end-position) 'invisible)))
3991 ;; Don't leave point in an invisible part of the buffer when hiding
3992 ;; a drawer away.
3993 (should-not
3994 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3995 (goto-char (point-max))
3996 (org-flag-drawer t)
3997 (get-char-property (point) 'invisible))))
3999 (ert-deftest test-org/hide-block-toggle ()
4000 "Test `org-hide-block-toggle' specifications."
4001 ;; Error when not at a block.
4002 (should-error
4003 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
4004 (org-hide-block-toggle 'off)
4005 (get-char-property (line-end-position) 'invisible)))
4006 ;; Hide block.
4007 (should
4008 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
4009 (org-hide-block-toggle)
4010 (get-char-property (line-end-position) 'invisible)))
4011 (should
4012 (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
4013 (org-hide-block-toggle)
4014 (get-char-property (line-end-position) 'invisible)))
4015 ;; Show block unconditionally when optional argument is `off'.
4016 (should-not
4017 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4018 (org-hide-block-toggle)
4019 (org-hide-block-toggle 'off)
4020 (get-char-property (line-end-position) 'invisible)))
4021 (should-not
4022 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4023 (org-hide-block-toggle 'off)
4024 (get-char-property (line-end-position) 'invisible)))
4025 ;; Hide block unconditionally when optional argument is non-nil.
4026 (should
4027 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4028 (org-hide-block-toggle t)
4029 (get-char-property (line-end-position) 'invisible)))
4030 (should
4031 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4032 (org-hide-block-toggle)
4033 (org-hide-block-toggle t)
4034 (get-char-property (line-end-position) 'invisible)))
4035 ;; Do not hide block when called from final blank lines.
4036 (should-not
4037 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
4038 (org-hide-block-toggle)
4039 (goto-char (point-min))
4040 (get-char-property (line-end-position) 'invisible)))
4041 ;; Don't leave point in an invisible part of the buffer when hiding
4042 ;; a block away.
4043 (should-not
4044 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
4045 (org-hide-block-toggle)
4046 (get-char-property (point) 'invisible))))
4048 (ert-deftest test-org/hide-block-toggle-maybe ()
4049 "Test `org-hide-block-toggle-maybe' specifications."
4050 (should
4051 (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
4052 (org-hide-block-toggle-maybe)))
4053 (should-not
4054 (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
4056 (ert-deftest test-org/show-set-visibility ()
4057 "Test `org-show-set-visibility' specifications."
4058 ;; Do not throw an error before first heading.
4059 (should
4060 (org-test-with-temp-text "Preamble\n* Headline"
4061 (org-show-set-visibility 'tree)
4063 ;; Test all visibility spans, both on headline and in entry.
4064 (let ((list-visible-lines
4065 (lambda (state headerp)
4066 (org-test-with-temp-text "* Grandmother (0)
4067 ** Uncle (1)
4068 *** Heir (2)
4069 ** Father (3)
4070 Ancestor text (4)
4071 *** Sister (5)
4072 Sibling text (6)
4073 *** Self (7)
4074 Match (8)
4075 **** First born (9)
4076 Child text (10)
4077 **** The other child (11)
4078 *** Brother (12)
4079 ** Aunt (13)
4081 (org-cycle t)
4082 (search-forward (if headerp "Self" "Match"))
4083 (org-show-set-visibility state)
4084 (goto-char (point-min))
4085 (let (result (line 0))
4086 (while (not (eobp))
4087 (unless (org-invisible-p2) (push line result))
4088 (incf line)
4089 (forward-line))
4090 (nreverse result))))))
4091 (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
4092 (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
4093 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
4094 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
4095 (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
4096 (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
4097 (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
4098 (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
4099 (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
4100 (should (equal '(0 1 3 5 7 8 9 11 12 13)
4101 (funcall list-visible-lines 'tree nil)))
4102 (should (equal '(0 1 3 4 5 7 12 13)
4103 (funcall list-visible-lines 'canonical t)))
4104 (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
4105 (funcall list-visible-lines 'canonical nil)))))
4108 (provide 'test-org)
4110 ;;; test-org.el ends here