Fix `org-entry-delete' with accumulated properties
[org-mode/org-tableheadings.git] / testing / lisp / test-org.el
blob0053a5d720ab4b64d8e0d1caab1937c60eb39802
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 footnote definitions or headlines.
582 (should
583 (zerop
584 (org-test-with-temp-text "* H"
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 ;; Do not indent before first headline.
593 (should
594 (zerop
595 (org-test-with-temp-text ""
596 (org-indent-line)
597 (org-get-indentation))))
598 ;; Indent according to headline level otherwise, unless
599 ;; `org-adapt-indentation' is nil.
600 (should
601 (= 2
602 (org-test-with-temp-text "* H\nA"
603 (forward-line)
604 (let ((org-adapt-indentation t)) (org-indent-line))
605 (org-get-indentation))))
606 (should
607 (= 2
608 (org-test-with-temp-text "* H\n\nA"
609 (forward-line)
610 (let ((org-adapt-indentation t)) (org-indent-line))
611 (org-get-indentation))))
612 (should
613 (zerop
614 (org-test-with-temp-text "* H\nA"
615 (forward-line)
616 (let ((org-adapt-indentation nil)) (org-indent-line))
617 (org-get-indentation))))
618 ;; Indenting preserves point position.
619 (should
620 (org-test-with-temp-text "* H\nAB"
621 (forward-line)
622 (forward-char)
623 (let ((org-adapt-indentation t)) (org-indent-line))
624 (looking-at "B")))
625 ;; Do not change indentation at an item.
626 (should
627 (= 1
628 (org-test-with-temp-text "* H\n - A"
629 (forward-line)
630 (let ((org-adapt-indentation t)) (org-indent-line))
631 (org-get-indentation))))
632 ;; On blank lines at the end of a list, indent like last element
633 ;; within it if the line is still in the list. Otherwise, indent
634 ;; like the whole list.
635 (should
636 (= 4
637 (org-test-with-temp-text "* H\n- A\n - AA\n"
638 (goto-char (point-max))
639 (let ((org-adapt-indentation t)) (org-indent-line))
640 (org-get-indentation))))
641 (should
642 (zerop
643 (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
644 (goto-char (point-max))
645 (let ((org-adapt-indentation t)) (org-indent-line))
646 (org-get-indentation))))
647 ;; Likewise, on a blank line at the end of a footnote definition,
648 ;; indent at column 0 if line belongs to the definition. Otherwise,
649 ;; indent like the definition itself.
650 (should
651 (zerop
652 (org-test-with-temp-text "* H\n[fn:1] Definition\n"
653 (goto-char (point-max))
654 (let ((org-adapt-indentation t)) (org-indent-line))
655 (org-get-indentation))))
656 (should
657 (zerop
658 (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
659 (goto-char (point-max))
660 (let ((org-adapt-indentation t)) (org-indent-line))
661 (org-get-indentation))))
662 ;; After the end of the contents of a greater element, indent like
663 ;; the beginning of the element.
664 (should
665 (= 1
666 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
667 (forward-line 2)
668 (org-indent-line)
669 (org-get-indentation))))
670 ;; On blank lines after a paragraph, indent like its last non-empty
671 ;; line.
672 (should
673 (= 1
674 (org-test-with-temp-text " Paragraph\n\n<point>"
675 (org-indent-line)
676 (org-get-indentation))))
677 ;; At the first line of an element, indent like previous element's
678 ;; first line, ignoring footnotes definitions and inline tasks, or
679 ;; according to parent.
680 (should
681 (= 2
682 (org-test-with-temp-text "A\n\n B\n\nC"
683 (goto-char (point-max))
684 (org-indent-line)
685 (org-get-indentation))))
686 (should
687 (= 1
688 (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
689 (goto-char (point-max))
690 (org-indent-line)
691 (org-get-indentation))))
692 (should
693 (= 1
694 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
695 (forward-line 1)
696 (org-indent-line)
697 (org-get-indentation))))
698 ;; Within code part of a source block, use language major mode if
699 ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
700 ;; according to line above.
701 (should
702 (= 6
703 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
704 (forward-line 2)
705 (let ((org-src-tab-acts-natively t)
706 (org-edit-src-content-indentation 0))
707 (org-indent-line))
708 (org-get-indentation))))
709 (should
710 (= 1
711 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
712 (forward-line 2)
713 (let ((org-src-tab-acts-natively nil)
714 (org-edit-src-content-indentation 0))
715 (org-indent-line))
716 (org-get-indentation))))
717 ;; Otherwise, indent like the first non-blank line above.
718 (should
719 (zerop
720 (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
721 (forward-line 3)
722 (org-indent-line)
723 (org-get-indentation))))
724 ;; Align node properties according to `org-property-format'. Handle
725 ;; nicely empty values.
726 (should
727 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
728 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
729 (let ((org-property-format "%-10s %s")) (org-indent-line))
730 (buffer-string))))
731 (should
732 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
733 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
734 (let ((org-property-format "%-10s %s")) (org-indent-line))
735 (buffer-string)))))
737 (ert-deftest test-org/indent-region ()
738 "Test `org-indent-region' specifications."
739 ;; Indent paragraph.
740 (should
741 (equal "A\nB\nC"
742 (org-test-with-temp-text " A\nB\n C"
743 (org-indent-region (point-min) (point-max))
744 (buffer-string))))
745 ;; Indent greater elements along with their contents.
746 (should
747 (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
748 (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
749 (org-indent-region (point-min) (point-max))
750 (buffer-string))))
751 ;; Ignore contents of verse blocks and example blocks.
752 (should
753 (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
754 (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
755 (org-indent-region (point-min) (point-max))
756 (buffer-string))))
757 (should
758 (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
759 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
760 (org-indent-region (point-min) (point-max))
761 (buffer-string))))
762 ;; Indent according to mode if `org-src-tab-acts-natively' is
763 ;; non-nil. Otherwise, do not indent code at all.
764 (should
765 (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
766 (org-test-with-temp-text
767 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
768 (let ((org-src-tab-acts-natively t)
769 (org-edit-src-content-indentation 0))
770 (org-indent-region (point-min) (point-max)))
771 (buffer-string))))
772 (should
773 (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
774 (org-test-with-temp-text
775 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
776 (let ((org-src-tab-acts-natively nil)
777 (org-edit-src-content-indentation 0))
778 (org-indent-region (point-min) (point-max)))
779 (buffer-string))))
780 ;; Align node properties according to `org-property-format'. Handle
781 ;; nicely empty values.
782 (should
783 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
784 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
785 (let ((org-property-format "%-10s %s")
786 (org-adapt-indentation nil))
787 (org-indent-region (point) (point-max)))
788 (buffer-string))))
789 (should
790 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
791 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
792 (let ((org-property-format "%-10s %s")
793 (org-adapt-indentation nil))
794 (org-indent-region (point) (point-max)))
795 (buffer-string))))
796 ;; Indent plain lists.
797 (should
798 (equal "- A\n B\n - C\n\n D"
799 (org-test-with-temp-text "- A\n B\n - C\n\n D"
800 (org-indent-region (point-min) (point-max))
801 (buffer-string))))
802 (should
803 (equal "- A\n\n- B"
804 (org-test-with-temp-text " - A\n\n - B"
805 (org-indent-region (point-min) (point-max))
806 (buffer-string))))
807 ;; Indent footnote definitions.
808 (should
809 (equal "[fn:1] Definition\n\nDefinition"
810 (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
811 (org-indent-region (point-min) (point-max))
812 (buffer-string))))
813 ;; Special case: Start indenting on a blank line.
814 (should
815 (equal "\nParagraph"
816 (org-test-with-temp-text "\n Paragraph"
817 (org-indent-region (point-min) (point-max))
818 (buffer-string)))))
822 ;;; Editing
824 (ert-deftest test-org/return ()
825 "Test RET (`org-return') specifications."
826 ;; Regular test.
827 (should
828 (equal "Para\ngraph"
829 (org-test-with-temp-text "Para<point>graph"
830 (org-return)
831 (buffer-string))))
832 ;; With optional argument, indent line.
833 (should
834 (equal " Para\n graph"
835 (org-test-with-temp-text " Para<point>graph"
836 (org-return t)
837 (buffer-string))))
838 ;; On a table, call `org-table-next-row'.
839 (should
840 (org-test-with-temp-text "| <point>a |\n| b |"
841 (org-return)
842 (org-looking-at-p "b")))
843 ;; Open link or timestamp under point when `org-return-follows-link'
844 ;; is non-nil.
845 (should
846 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
847 (let ((org-return-follows-link t)) (org-return))
848 (org-looking-at-p "<<target>>")))
849 (should-not
850 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
851 (let ((org-return-follows-link nil)) (org-return))
852 (org-looking-at-p "<<target>>")))
853 ;; However, do not open link when point is in a table.
854 (should
855 (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
856 (let ((org-return-follows-link t)) (org-return))
857 (org-looking-at-p "between")))
858 ;; Special case: in a list, when indenting, do not break structure.
859 (should
860 (equal "- A\n B"
861 (org-test-with-temp-text "- A <point>B"
862 (org-return t)
863 (buffer-string))))
864 (should
865 (equal "- A\n\n- B"
866 (org-test-with-temp-text "- A\n<point>- B"
867 (org-return t)
868 (buffer-string))))
869 ;; Special case: on tags part of a headline, add a newline below it
870 ;; instead of breaking it.
871 (should
872 (equal "* H :tag:\n"
873 (org-test-with-temp-text "* H :<point>tag:"
874 (org-return)
875 (buffer-string)))))
877 (ert-deftest test-org/meta-return ()
878 "Test M-RET (`org-meta-return') specifications."
879 ;; In a table field insert a row above.
880 (should
881 (org-test-with-temp-text "| a |"
882 (forward-char)
883 (org-meta-return)
884 (forward-line -1)
885 (looking-at "| |$")))
886 ;; In a paragraph change current line into a header.
887 (should
888 (org-test-with-temp-text "a"
889 (org-meta-return)
890 (beginning-of-line)
891 (looking-at "\* a$")))
892 ;; In an item insert an item, in this case above.
893 (should
894 (org-test-with-temp-text "- a"
895 (org-meta-return)
896 (beginning-of-line)
897 (looking-at "- $")))
898 ;; In a drawer and item insert an item, in this case above.
899 (should
900 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
901 (forward-line)
902 (org-meta-return)
903 (beginning-of-line)
904 (looking-at "- $"))))
906 (ert-deftest test-org/insert-heading ()
907 "Test `org-insert-heading' specifications."
908 ;; In an empty buffer, insert a new headline.
909 (should
910 (equal "* "
911 (org-test-with-temp-text ""
912 (org-insert-heading)
913 (buffer-string))))
914 ;; At the beginning of a line, turn it into a headline
915 (should
916 (equal "* P"
917 (org-test-with-temp-text "<point>P"
918 (org-insert-heading)
919 (buffer-string))))
920 ;; In the middle of a line, split the line if allowed, otherwise,
921 ;; insert the headline at its end.
922 (should
923 (equal "Para\n* graph"
924 (org-test-with-temp-text "Para<point>graph"
925 (let ((org-M-RET-may-split-line '((default . t))))
926 (org-insert-heading))
927 (buffer-string))))
928 (should
929 (equal "Paragraph\n* "
930 (org-test-with-temp-text "Para<point>graph"
931 (let ((org-M-RET-may-split-line '((default . nil))))
932 (org-insert-heading))
933 (buffer-string))))
934 ;; When on a list, insert an item instead, unless called with an
935 ;; universal argument or if list is invisible. In this case, create
936 ;; a new headline after contents.
937 (should
938 (equal "* H\n- item\n- "
939 (org-test-with-temp-text "* H\n- item<point>"
940 (let ((org-insert-heading-respect-content nil))
941 (org-insert-heading))
942 (buffer-string))))
943 (should
944 (equal "* H\n- item\n- item 2\n* "
945 (org-test-with-temp-text "* H\n- item<point>\n- item 2"
946 (let ((org-insert-heading-respect-content nil))
947 (org-insert-heading '(4)))
948 (buffer-string))))
949 (should
950 (equal "* H\n- item\n* "
951 (org-test-with-temp-text "* H\n- item"
952 (org-cycle)
953 (goto-char (point-max))
954 (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
955 (buffer-string))))
956 ;; When called with two universal arguments, insert a new headline
957 ;; at the end of the grandparent subtree.
958 (should
959 (equal "* H1\n** H3\n- item\n** H2\n** "
960 (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
961 (let ((org-insert-heading-respect-content nil))
962 (org-insert-heading '(16)))
963 (buffer-string))))
964 ;; When optional TOP-LEVEL argument is non-nil, always insert
965 ;; a level 1 heading.
966 (should
967 (equal "* H1\n** H2\n* "
968 (org-test-with-temp-text "* H1\n** H2<point>"
969 (org-insert-heading nil nil t)
970 (buffer-string))))
971 (should
972 (equal "* H1\n- item\n* "
973 (org-test-with-temp-text "* H1\n- item<point>"
974 (org-insert-heading nil nil t)
975 (buffer-string))))
976 ;; Corner case: correctly insert a headline after an empty one.
977 (should
978 (equal "* \n* "
979 (org-test-with-temp-text "* <point>"
980 (org-insert-heading)
981 (buffer-string)))))
983 (ert-deftest test-org/insert-todo-heading-respect-content ()
984 "Test `org-insert-todo-heading-respect-content' specifications."
985 ;; Create a TODO heading.
986 (should
987 (org-test-with-temp-text "* H1\n Body"
988 (org-insert-todo-heading-respect-content)
989 (nth 2 (org-heading-components))))
990 ;; Add headline at the end of the first subtree
991 (should
992 (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
993 (search-forward "H1Body")
994 (org-insert-todo-heading-respect-content)
995 (and (eobp) (org-at-heading-p))))
996 ;; In a list, do not create a new item.
997 (should
998 (org-test-with-temp-text "* H\n- an item\n- another one"
999 (search-forward "an ")
1000 (org-insert-todo-heading-respect-content)
1001 (and (eobp) (org-at-heading-p)))))
1005 ;;; Fixed-Width Areas
1007 (ert-deftest test-org/toggle-fixed-width ()
1008 "Test `org-toggle-fixed-width' specifications."
1009 ;; No region: Toggle on fixed-width marker in paragraphs.
1010 (should
1011 (equal ": A"
1012 (org-test-with-temp-text "A"
1013 (org-toggle-fixed-width)
1014 (buffer-string))))
1015 ;; No region: Toggle off fixed-width markers in fixed-width areas.
1016 (should
1017 (equal "A"
1018 (org-test-with-temp-text ": A"
1019 (org-toggle-fixed-width)
1020 (buffer-string))))
1021 ;; No region: Toggle on marker in blank lines after elements or just
1022 ;; after a headline.
1023 (should
1024 (equal "* H\n: "
1025 (org-test-with-temp-text "* H\n"
1026 (forward-line)
1027 (org-toggle-fixed-width)
1028 (buffer-string))))
1029 (should
1030 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
1031 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
1032 (goto-char (point-max))
1033 (org-toggle-fixed-width)
1034 (buffer-string))))
1035 ;; No region: Toggle on marker in front of one line elements (e.g.,
1036 ;; headlines, clocks)
1037 (should
1038 (equal ": * Headline"
1039 (org-test-with-temp-text "* Headline"
1040 (org-toggle-fixed-width)
1041 (buffer-string))))
1042 (should
1043 (equal ": #+KEYWORD: value"
1044 (org-test-with-temp-text "#+KEYWORD: value"
1045 (org-toggle-fixed-width)
1046 (buffer-string))))
1047 ;; No region: error in other situations.
1048 (should-error
1049 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
1050 (forward-line)
1051 (org-toggle-fixed-width)
1052 (buffer-string)))
1053 ;; No region: Indentation is preserved.
1054 (should
1055 (equal "- A\n : B"
1056 (org-test-with-temp-text "- A\n B"
1057 (forward-line)
1058 (org-toggle-fixed-width)
1059 (buffer-string))))
1060 ;; Region: If it contains only fixed-width elements and blank lines,
1061 ;; toggle off fixed-width markup.
1062 (should
1063 (equal "A\n\nB"
1064 (org-test-with-temp-text ": A\n\n: B"
1065 (transient-mark-mode 1)
1066 (push-mark (point) t t)
1067 (goto-char (point-max))
1068 (org-toggle-fixed-width)
1069 (buffer-string))))
1070 ;; Region: If it contains anything else, toggle on fixed-width but
1071 ;; not on fixed-width areas.
1072 (should
1073 (equal ": A\n: \n: B\n: \n: C"
1074 (org-test-with-temp-text "A\n\n: B\n\nC"
1075 (transient-mark-mode 1)
1076 (push-mark (point) t t)
1077 (goto-char (point-max))
1078 (org-toggle-fixed-width)
1079 (buffer-string))))
1080 ;; Region: Ignore blank lines at its end, unless it contains only
1081 ;; such lines.
1082 (should
1083 (equal ": A\n\n"
1084 (org-test-with-temp-text "A\n\n"
1085 (transient-mark-mode 1)
1086 (push-mark (point) t t)
1087 (goto-char (point-max))
1088 (org-toggle-fixed-width)
1089 (buffer-string))))
1090 (should
1091 (equal ": \n: \n"
1092 (org-test-with-temp-text "\n\n"
1093 (transient-mark-mode 1)
1094 (push-mark (point) t t)
1095 (goto-char (point-max))
1096 (org-toggle-fixed-width)
1097 (buffer-string)))))
1101 ;;; Headline
1103 (ert-deftest test-org/in-commented-heading-p ()
1104 "Test `org-in-commented-heading-p' specifications."
1105 ;; Commented headline.
1106 (should
1107 (org-test-with-temp-text "* COMMENT Headline\nBody"
1108 (goto-char (point-max))
1109 (org-in-commented-heading-p)))
1110 ;; Commented ancestor.
1111 (should
1112 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1113 (goto-char (point-max))
1114 (org-in-commented-heading-p)))
1115 ;; Comment keyword is case-sensitive.
1116 (should-not
1117 (org-test-with-temp-text "* Comment Headline\nBody"
1118 (goto-char (point-max))
1119 (org-in-commented-heading-p)))
1120 ;; Keyword is standalone.
1121 (should-not
1122 (org-test-with-temp-text "* COMMENTHeadline\nBody"
1123 (goto-char (point-max))
1124 (org-in-commented-heading-p)))
1125 ;; Optional argument.
1126 (should-not
1127 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1128 (goto-char (point-max))
1129 (org-in-commented-heading-p t))))
1133 ;;; Keywords
1135 (ert-deftest test-org/set-regexps-and-options ()
1136 "Test `org-set-regexps-and-options' specifications."
1137 ;; TAGS keyword.
1138 (should
1139 (equal '(("A" . ?a) ("B") ("C"))
1140 (org-test-with-temp-text "#+TAGS: A(a) B C"
1141 (org-mode-restart)
1142 org-tag-alist)))
1143 (should
1144 (equal '(("A") (:newline) ("B"))
1145 (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
1146 (org-mode-restart)
1147 org-tag-alist)))
1148 (should
1149 (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
1150 (org-test-with-temp-text "#+TAGS: { A B } C"
1151 (org-mode-restart)
1152 org-tag-alist)))
1153 (should
1154 (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
1155 (org-test-with-temp-text "#+TAGS: { A : B C }"
1156 (org-mode-restart)
1157 org-tag-alist)))
1158 (should
1159 (equal '(("A" "B" "C"))
1160 (org-test-with-temp-text "#+TAGS: { A : B C }"
1161 (org-mode-restart)
1162 org-tag-groups-alist)))
1163 ;; FILETAGS keyword.
1164 (should
1165 (equal '("A" "B" "C")
1166 (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
1167 (org-mode-restart)
1168 org-file-tags)))
1169 ;; PROPERTY keyword. Property names are case-insensitive.
1170 (should
1171 (equal "foo=1"
1172 (org-test-with-temp-text "#+PROPERTY: var foo=1"
1173 (org-mode-restart)
1174 (cdr (assoc "var" org-file-properties)))))
1175 (should
1176 (equal
1177 "foo=1 bar=2"
1178 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
1179 (org-mode-restart)
1180 (cdr (assoc "var" org-file-properties)))))
1181 (should
1182 (equal
1183 "foo=1 bar=2"
1184 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
1185 (org-mode-restart)
1186 (cdr (assoc "var" org-file-properties)))))
1187 ;; ARCHIVE keyword.
1188 (should
1189 (equal "%s_done::"
1190 (org-test-with-temp-text "#+ARCHIVE: %s_done::"
1191 (org-mode-restart)
1192 org-archive-location)))
1193 ;; CATEGORY keyword.
1194 (should
1195 (eq 'test
1196 (org-test-with-temp-text "#+CATEGORY: test"
1197 (org-mode-restart)
1198 org-category)))
1199 (should
1200 (equal "test"
1201 (org-test-with-temp-text "#+CATEGORY: test"
1202 (org-mode-restart)
1203 (cdr (assoc "CATEGORY" org-file-properties)))))
1204 ;; COLUMNS keyword.
1205 (should
1206 (equal "%25ITEM %TAGS %PRIORITY %TODO"
1207 (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
1208 (org-mode-restart)
1209 org-columns-default-format)))
1210 ;; CONSTANTS keyword. Constants names are case sensitive.
1211 (should
1212 (equal '("299792458." "3.14")
1213 (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
1214 (org-mode-restart)
1215 (mapcar
1216 (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
1217 '("c" "pi")))))
1218 (should
1219 (equal "3.14"
1220 (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
1221 (org-mode-restart)
1222 (cdr (assoc "pi" org-table-formula-constants-local)))))
1223 (should
1224 (equal "22/7"
1225 (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
1226 (org-mode-restart)
1227 (cdr (assoc "PI" org-table-formula-constants-local)))))
1228 ;; LINK keyword.
1229 (should
1230 (equal
1231 '("url1" "url2")
1232 (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
1233 (org-mode-restart)
1234 (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
1235 '("a" "b")))))
1236 ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
1237 (should
1238 (equal
1239 '(?X ?Z ?Y)
1240 (org-test-with-temp-text "#+PRIORITIES: X Z Y"
1241 (org-mode-restart)
1242 (list org-highest-priority org-lowest-priority org-default-priority))))
1243 (should
1244 (equal
1245 '(?A ?C ?B)
1246 (org-test-with-temp-text "#+PRIORITIES: X Z"
1247 (org-mode-restart)
1248 (list org-highest-priority org-lowest-priority org-default-priority))))
1249 ;; STARTUP keyword.
1250 (should
1251 (equal '(t t)
1252 (org-test-with-temp-text "#+STARTUP: fold odd"
1253 (org-mode-restart)
1254 (list org-startup-folded org-odd-levels-only))))
1255 ;; TODO keywords.
1256 (should
1257 (equal '(("A" "B") ("C"))
1258 (org-test-with-temp-text "#+TODO: A B | C"
1259 (org-mode-restart)
1260 (list org-not-done-keywords org-done-keywords))))
1261 (should
1262 (equal '(("A" "C") ("B" "D"))
1263 (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
1264 (org-mode-restart)
1265 (list org-not-done-keywords org-done-keywords))))
1266 (should
1267 (equal '(("A" "B") ("C"))
1268 (org-test-with-temp-text "#+TYP_TODO: A B | C"
1269 (org-mode-restart)
1270 (list org-not-done-keywords org-done-keywords))))
1271 (should
1272 (equal '((:startgroup) ("A" . ?a) (:endgroup))
1273 (org-test-with-temp-text "#+TODO: A(a)"
1274 (org-mode-restart)
1275 org-todo-key-alist)))
1276 (should
1277 (equal '(("D" note nil) ("C" time nil) ("B" note time))
1278 (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
1279 (org-mode-restart)
1280 org-todo-log-states)))
1281 ;; Enter SETUPFILE keyword.
1282 (should
1283 (equal "1"
1284 (org-test-with-temp-text
1285 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
1286 (org-mode-restart)
1287 (cdr (assoc "a" org-file-properties))))))
1291 ;;; Links
1293 ;;;; Coderefs
1295 (ert-deftest test-org/coderef ()
1296 "Test coderef links specifications."
1297 (should
1298 (org-test-with-temp-text "
1299 #+BEGIN_SRC emacs-lisp
1300 \(+ 1 1) (ref:sc)
1301 #+END_SRC
1302 \[[(sc)]]"
1303 (goto-char (point-max))
1304 (org-open-at-point)
1305 (looking-at "(ref:sc)"))))
1307 ;;;; Custom ID
1309 (ert-deftest test-org/custom-id ()
1310 "Test custom ID links specifications."
1311 (should
1312 (org-test-with-temp-text
1313 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
1314 (goto-char (point-max))
1315 (org-open-at-point)
1316 (org-looking-at-p "\\* H1")))
1317 ;; Ignore false positives.
1318 (should-not
1319 (org-test-with-temp-text
1320 "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
1321 (goto-char (point-max))
1322 (let (org-link-search-must-match-exact-headline) (org-open-at-point))
1323 (org-looking-at-p "\\* H1"))))
1325 ;;;; Fuzzy Links
1327 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
1328 ;; a named element (#+name: text) and to headlines (* Text).
1330 (ert-deftest test-org/fuzzy-links ()
1331 "Test fuzzy links specifications."
1332 ;; Fuzzy link goes in priority to a matching target.
1333 (should
1334 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
1335 (goto-line 5)
1336 (org-open-at-point)
1337 (looking-at "<<Test>>")))
1338 ;; Then fuzzy link points to an element with a given name.
1339 (should
1340 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
1341 (goto-line 5)
1342 (org-open-at-point)
1343 (looking-at "#\\+NAME: Test")))
1344 ;; A target still lead to a matching headline otherwise.
1345 (should
1346 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
1347 (goto-line 4)
1348 (org-open-at-point)
1349 (looking-at "\\* Head2")))
1350 ;; With a leading star in link, enforce heading match.
1351 (should
1352 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
1353 (goto-line 3)
1354 (org-open-at-point)
1355 (looking-at "\\* Test")))
1356 ;; With a leading star in link, enforce exact heading match, even
1357 ;; with `org-link-search-must-match-exact-headline' set to nil.
1358 (should-error
1359 (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
1360 (let ((org-link-search-must-match-exact-headline nil))
1361 (org-open-at-point))))
1362 ;; Heading match should not care about spaces, cookies, todo
1363 ;; keywords, priorities, and tags.
1364 (should
1365 (let ((first-line
1366 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1367 (org-test-with-temp-text
1368 (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
1369 (let ((org-link-search-must-match-exact-headline nil)
1370 (org-todo-regexp "TODO"))
1371 (org-open-at-point))
1372 (looking-at (regexp-quote first-line)))))
1373 ;; Heading match should still be exact.
1374 (should-error
1375 (let ((first-line
1376 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1377 (org-test-with-temp-text
1378 (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
1379 (let ((org-link-search-must-match-exact-headline nil)
1380 (org-todo-regexp "TODO"))
1381 (org-open-at-point)))))
1382 ;; Correctly un-hexify fuzzy links.
1383 (should
1384 (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
1385 (goto-char (point-max))
1386 (org-open-at-point)
1387 (bobp))))
1389 ;;;; Link Escaping
1391 (ert-deftest test-org/org-link-escape-ascii-character ()
1392 "Escape an ascii character."
1393 (should
1394 (string=
1395 "%5B"
1396 (org-link-escape "["))))
1398 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
1399 "Escape an ascii control character."
1400 (should
1401 (string=
1402 "%09"
1403 (org-link-escape "\t"))))
1405 (ert-deftest test-org/org-link-escape-multibyte-character ()
1406 "Escape an unicode multibyte character."
1407 (should
1408 (string=
1409 "%E2%82%AC"
1410 (org-link-escape "€"))))
1412 (ert-deftest test-org/org-link-escape-custom-table ()
1413 "Escape string with custom character table."
1414 (should
1415 (string=
1416 "Foo%3A%42ar%0A"
1417 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
1419 (ert-deftest test-org/org-link-escape-custom-table-merge ()
1420 "Escape string with custom table merged with default table."
1421 (should
1422 (string=
1423 "%5BF%6F%6F%3A%42ar%0A%5D"
1424 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
1426 (ert-deftest test-org/org-link-unescape-ascii-character ()
1427 "Unescape an ascii character."
1428 (should
1429 (string=
1431 (org-link-unescape "%5B"))))
1433 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
1434 "Unescpae an ascii control character."
1435 (should
1436 (string=
1437 "\n"
1438 (org-link-unescape "%0A"))))
1440 (ert-deftest test-org/org-link-unescape-multibyte-character ()
1441 "Unescape unicode multibyte character."
1442 (should
1443 (string=
1444 "€"
1445 (org-link-unescape "%E2%82%AC"))))
1447 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
1448 "Unescape old style percent escaped character."
1449 (should
1450 (string=
1451 "àâçèéêîôùû"
1452 (decode-coding-string
1453 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
1455 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
1456 "Escape and unescape a URL that includes an escaped char.
1457 http://article.gmane.org/gmane.emacs.orgmode/21459/"
1458 (should
1459 (string=
1460 "http://some.host.com/form?&id=blah%2Bblah25"
1461 (org-link-unescape
1462 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
1464 (ert-deftest test-org/org-link-escape-chars-browser ()
1465 "Test of the constant `org-link-escape-chars-browser'.
1466 See there why this test is a candidate to be removed once Org
1467 drops support for Emacs 24.1 and 24.2."
1468 (should
1469 (string=
1470 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1471 "%22Release%208.2%22&idxname=emacs-orgmode")
1472 (org-link-escape-browser ; Do not replace with `url-encode-url',
1473 ; see docstring above.
1474 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1475 "\"Release 8.2\"&idxname=emacs-orgmode")))))
1477 ;;;; Open at point
1479 (ert-deftest test-org/open-at-point-in-property ()
1480 "Does `org-open-at-point' open link in property drawer?"
1481 (should
1482 (org-test-with-temp-text
1483 "* Headline
1484 :PROPERTIES:
1485 :URL: <point>[[info:emacs#Top]]
1486 :END:"
1487 (org-open-at-point) t)))
1489 (ert-deftest test-org/open-at-point-in-comment ()
1490 "Does `org-open-at-point' open link in a commented line?"
1491 (should
1492 (org-test-with-temp-text
1493 "# <point>[[info:emacs#Top]]"
1494 (org-open-at-point) t)))
1496 (ert-deftest test-org/open-at-point/info ()
1497 "Test `org-open-at-point' on info links."
1498 (should
1499 (org-test-with-temp-text
1500 "<point>[[info:emacs#Top]]"
1501 (org-open-at-point)
1502 (and (switch-to-buffer "*info*")
1503 (prog1
1504 (looking-at "\nThe Emacs Editor")
1505 (kill-buffer))))))
1507 (ert-deftest test-org/open-at-point/inline-image ()
1508 "Test `org-open-at-point' on nested links."
1509 (should
1510 (org-test-with-temp-text "[[info:org#Top][info:<point>emacs#Top]]"
1511 (org-open-at-point)
1512 (prog1 (with-current-buffer "*info*" (looking-at "\nOrg Mode Manual"))
1513 (kill-buffer "*info*")))))
1516 ;;; Node Properties
1518 (ert-deftest test-org/accumulated-properties-in-drawers ()
1519 "Ensure properties accumulate in subtree drawers."
1520 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
1521 (org-babel-next-src-block)
1522 (should (equal '(2 1) (org-babel-execute-src-block)))))
1524 (ert-deftest test-org/custom-properties ()
1525 "Test custom properties specifications."
1526 ;; Standard test.
1527 (should
1528 (let ((org-custom-properties '("FOO")))
1529 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1530 (org-toggle-custom-properties-visibility)
1531 (org-invisible-p2))))
1532 ;; Properties are case-insensitive.
1533 (should
1534 (let ((org-custom-properties '("FOO")))
1535 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
1536 (org-toggle-custom-properties-visibility)
1537 (org-invisible-p2))))
1538 (should
1539 (let ((org-custom-properties '("foo")))
1540 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1541 (org-toggle-custom-properties-visibility)
1542 (org-invisible-p2))))
1543 ;; Multiple custom properties in the same drawer.
1544 (should
1545 (let ((org-custom-properties '("FOO" "BAR")))
1546 (org-test-with-temp-text
1547 "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
1548 (org-toggle-custom-properties-visibility)
1549 (and (org-invisible-p2)
1550 (not (progn (forward-line) (org-invisible-p2)))
1551 (progn (forward-line) (org-invisible-p2))))))
1552 ;; Hide custom properties with an empty value.
1553 (should
1554 (let ((org-custom-properties '("FOO")))
1555 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
1556 (org-toggle-custom-properties-visibility)
1557 (org-invisible-p2))))
1558 ;; Do not hide fake properties.
1559 (should-not
1560 (let ((org-custom-properties '("FOO")))
1561 (org-test-with-temp-text ":FOO: val\n"
1562 (org-toggle-custom-properties-visibility)
1563 (org-invisible-p2))))
1564 (should-not
1565 (let ((org-custom-properties '("A")))
1566 (org-test-with-temp-text
1567 "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
1568 (org-toggle-custom-properties-visibility)
1569 (org-invisible-p2)))))
1573 ;;; Mark Region
1575 (ert-deftest test-org/mark-subtree ()
1576 "Test `org-mark-subtree' specifications."
1577 ;; Error when point is before first headline.
1578 (should-error
1579 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
1580 (progn (transient-mark-mode 1)
1581 (org-mark-subtree))))
1582 ;; Without argument, mark current subtree.
1583 (should
1584 (equal
1585 '(12 32)
1586 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1587 (progn (transient-mark-mode 1)
1588 (forward-line 2)
1589 (org-mark-subtree)
1590 (list (region-beginning) (region-end))))))
1591 ;; With an argument, move ARG up.
1592 (should
1593 (equal
1594 '(1 32)
1595 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1596 (progn (transient-mark-mode 1)
1597 (forward-line 2)
1598 (org-mark-subtree 1)
1599 (list (region-beginning) (region-end))))))
1600 ;; Do not get fooled by inlinetasks.
1601 (when (featurep 'org-inlinetask)
1602 (should
1603 (= 1
1604 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
1605 (progn (transient-mark-mode 1)
1606 (forward-line 1)
1607 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
1608 (region-beginning)))))))
1612 ;;; Navigation
1614 (ert-deftest test-org/end-of-meta-data ()
1615 "Test `org-end-of-meta-data' specifications."
1616 ;; Skip planning line.
1617 (should
1618 (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
1619 (org-end-of-meta-data)
1620 (eobp)))
1621 ;; Skip properties drawer.
1622 (should
1623 (org-test-with-temp-text
1624 "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
1625 (org-end-of-meta-data)
1626 (eobp)))
1627 ;; Skip both.
1628 (should
1629 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
1630 (org-end-of-meta-data)
1631 (eobp)))
1632 ;; Nothing to skip, go to first line.
1633 (should
1634 (org-test-with-temp-text "* Headline\nContents"
1635 (org-end-of-meta-data)
1636 (looking-at "Contents")))
1637 ;; With option argument, skip empty lines, regular drawers and
1638 ;; clocking lines.
1639 (should
1640 (org-test-with-temp-text "* Headline\n\nContents"
1641 (org-end-of-meta-data t)
1642 (looking-at "Contents")))
1643 (should
1644 (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
1645 (org-end-of-meta-data t)
1646 (looking-at "Contents")))
1647 (should
1648 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
1649 (org-end-of-meta-data t)
1650 (looking-at "Contents")))
1651 ;; Special case: do not skip incomplete drawers.
1652 (should
1653 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
1654 (org-end-of-meta-data t)
1655 (looking-at ":LOGBOOK:")))
1656 ;; Special case: Be careful about consecutive headlines.
1657 (should-not
1658 (org-test-with-temp-text "* H1\n*H2\nContents"
1659 (org-end-of-meta-data t)
1660 (looking-at "Contents"))))
1662 (ert-deftest test-org/beginning-of-line ()
1663 "Test `org-beginning-of-line' specifications."
1664 ;; Standard test.
1665 (should
1666 (org-test-with-temp-text "Some text\nSome other text"
1667 (progn (org-beginning-of-line) (bolp))))
1668 ;; Standard test with `visual-line-mode'.
1669 (should-not
1670 (org-test-with-temp-text "A long line of text\nSome other text"
1671 (progn (visual-line-mode)
1672 (forward-char 2)
1673 (dotimes (i 1000) (insert "very "))
1674 (org-beginning-of-line)
1675 (bolp))))
1676 ;; At an headline with special movement.
1677 (should
1678 (org-test-with-temp-text "* TODO Headline"
1679 (let ((org-special-ctrl-a/e t))
1680 (org-end-of-line)
1681 (and (progn (org-beginning-of-line) (looking-at "Headline"))
1682 (progn (org-beginning-of-line) (bolp))
1683 (progn (org-beginning-of-line) (looking-at "Headline")))))))
1685 (ert-deftest test-org/end-of-line ()
1686 "Test `org-end-of-line' specifications."
1687 ;; Standard test.
1688 (should
1689 (org-test-with-temp-text "Some text\nSome other text"
1690 (progn (org-end-of-line) (eolp))))
1691 ;; Standard test with `visual-line-mode'.
1692 (should-not
1693 (org-test-with-temp-text "A long line of text\nSome other text"
1694 (progn (visual-line-mode)
1695 (forward-char 2)
1696 (dotimes (i 1000) (insert "very "))
1697 (goto-char (point-min))
1698 (org-end-of-line)
1699 (eolp))))
1700 ;; At an headline with special movement.
1701 (should
1702 (org-test-with-temp-text "* Headline1 :tag:\n"
1703 (let ((org-special-ctrl-a/e t))
1704 (and (progn (org-end-of-line) (looking-at " :tag:"))
1705 (progn (org-end-of-line) (eolp))
1706 (progn (org-end-of-line) (looking-at " :tag:"))))))
1707 ;; At an headline without special movement.
1708 (should
1709 (org-test-with-temp-text "* Headline2 :tag:\n"
1710 (let ((org-special-ctrl-a/e nil))
1711 (and (progn (org-end-of-line) (eolp))
1712 (progn (org-end-of-line) (eolp))))))
1713 ;; At an headline, with reversed movement.
1714 (should
1715 (org-test-with-temp-text "* Headline3 :tag:\n"
1716 (let ((org-special-ctrl-a/e 'reversed)
1717 (this-command last-command))
1718 (and (progn (org-end-of-line) (eolp))
1719 (progn (org-end-of-line) (looking-at " :tag:"))))))
1720 ;; At a block without hidden contents.
1721 (should
1722 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1723 (progn (org-end-of-line) (eolp))))
1724 ;; At a block with hidden contents.
1725 (should-not
1726 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1727 (let ((org-special-ctrl-a/e t))
1728 (org-hide-block-toggle)
1729 (org-end-of-line)
1730 (eobp)))))
1732 (ert-deftest test-org/forward-paragraph ()
1733 "Test `org-forward-paragraph' specifications."
1734 ;; At end of buffer, return an error.
1735 (should-error
1736 (org-test-with-temp-text "Paragraph"
1737 (goto-char (point-max))
1738 (org-forward-paragraph)))
1739 ;; Standard test.
1740 (should
1741 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1742 (org-forward-paragraph)
1743 (looking-at "P2")))
1744 ;; Ignore depth.
1745 (should
1746 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
1747 (org-forward-paragraph)
1748 (looking-at "P1")))
1749 ;; Do not enter elements with invisible contents.
1750 (should
1751 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
1752 (org-hide-block-toggle)
1753 (org-forward-paragraph)
1754 (looking-at "P3")))
1755 ;; On an affiliated keyword, jump to the beginning of the element.
1756 (should
1757 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
1758 (org-forward-paragraph)
1759 (looking-at "Para")))
1760 ;; On an item or a footnote definition, move to the second element
1761 ;; inside, if any.
1762 (should
1763 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
1764 (org-forward-paragraph)
1765 (looking-at " Paragraph")))
1766 (should
1767 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
1768 (org-forward-paragraph)
1769 (looking-at "Paragraph")))
1770 ;; On an item, or a footnote definition, when the first line is
1771 ;; empty, move to the first item.
1772 (should
1773 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
1774 (org-forward-paragraph)
1775 (looking-at " Paragraph")))
1776 (should
1777 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
1778 (org-forward-paragraph)
1779 (looking-at "Paragraph")))
1780 ;; On a table (resp. a property drawer) do not move through table
1781 ;; rows (resp. node properties).
1782 (should
1783 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
1784 (org-forward-paragraph)
1785 (looking-at "Paragraph")))
1786 (should
1787 (org-test-with-temp-text
1788 "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
1789 (org-forward-paragraph)
1790 (looking-at "Paragraph")))
1791 ;; On a verse or source block, stop after blank lines.
1792 (should
1793 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
1794 (org-forward-paragraph)
1795 (looking-at "L2")))
1796 (should
1797 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
1798 (org-forward-paragraph)
1799 (looking-at "L2"))))
1801 (ert-deftest test-org/backward-paragraph ()
1802 "Test `org-backward-paragraph' specifications."
1803 ;; Error at beginning of buffer.
1804 (should-error
1805 (org-test-with-temp-text "Paragraph"
1806 (org-backward-paragraph)))
1807 ;; Regular test.
1808 (should
1809 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1810 (goto-char (point-max))
1811 (org-backward-paragraph)
1812 (looking-at "P3")))
1813 (should
1814 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1815 (goto-char (point-max))
1816 (beginning-of-line)
1817 (org-backward-paragraph)
1818 (looking-at "P2")))
1819 ;; Ignore depth.
1820 (should
1821 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
1822 (goto-char (point-max))
1823 (beginning-of-line)
1824 (org-backward-paragraph)
1825 (looking-at "P2")))
1826 ;; Ignore invisible elements.
1827 (should
1828 (org-test-with-temp-text "* H1\n P1\n* H2"
1829 (org-cycle)
1830 (goto-char (point-max))
1831 (beginning-of-line)
1832 (org-backward-paragraph)
1833 (bobp)))
1834 ;; On an affiliated keyword, jump to the first one.
1835 (should
1836 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
1837 (search-forward "c2")
1838 (org-backward-paragraph)
1839 (looking-at "#\\+name")))
1840 ;; On the second element in an item or a footnote definition, jump
1841 ;; to item or the definition.
1842 (should
1843 (org-test-with-temp-text "- line1\n\n line2"
1844 (goto-char (point-max))
1845 (beginning-of-line)
1846 (org-backward-paragraph)
1847 (looking-at "- line1")))
1848 (should
1849 (org-test-with-temp-text "[fn:1] line1\n\n line2"
1850 (goto-char (point-max))
1851 (beginning-of-line)
1852 (org-backward-paragraph)
1853 (looking-at "\\[fn:1\\] line1")))
1854 ;; On a table (resp. a property drawer), ignore table rows
1855 ;; (resp. node properties).
1856 (should
1857 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
1858 (goto-char (point-max))
1859 (beginning-of-line)
1860 (org-backward-paragraph)
1861 (bobp)))
1862 (should
1863 (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
1864 (org-backward-paragraph)
1865 (looking-at ":PROPERTIES:")))
1866 ;; On a source or verse block, stop before blank lines.
1867 (should
1868 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
1869 (search-forward "L3")
1870 (beginning-of-line)
1871 (org-backward-paragraph)
1872 (looking-at "L2")))
1873 (should
1874 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
1875 (search-forward "L3")
1876 (beginning-of-line)
1877 (org-backward-paragraph)
1878 (looking-at "L2"))))
1880 (ert-deftest test-org/forward-element ()
1881 "Test `org-forward-element' specifications."
1882 ;; 1. At EOB: should error.
1883 (org-test-with-temp-text "Some text\n"
1884 (goto-char (point-max))
1885 (should-error (org-forward-element)))
1886 ;; 2. Standard move: expected to ignore blank lines.
1887 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
1888 (org-forward-element)
1889 (should (looking-at (regexp-quote "Second paragraph."))))
1890 ;; 3. Headline tests.
1891 (org-test-with-temp-text "
1892 * Head 1
1893 ** Head 1.1
1894 *** Head 1.1.1
1895 ** Head 1.2"
1896 ;; 3.1. At an headline beginning: move to next headline at the
1897 ;; same level.
1898 (goto-line 3)
1899 (org-forward-element)
1900 (should (looking-at (regexp-quote "** Head 1.2")))
1901 ;; 3.2. At an headline beginning: move to parent headline if no
1902 ;; headline at the same level.
1903 (goto-line 3)
1904 (org-forward-element)
1905 (should (looking-at (regexp-quote "** Head 1.2"))))
1906 ;; 4. Greater element tests.
1907 (org-test-with-temp-text
1908 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
1909 ;; 4.1. At a greater element: expected to skip contents.
1910 (org-forward-element)
1911 (should (looking-at (regexp-quote "Outside.")))
1912 ;; 4.2. At the end of greater element contents: expected to skip
1913 ;; to the end of the greater element.
1914 (goto-line 2)
1915 (org-forward-element)
1916 (should (looking-at (regexp-quote "Outside."))))
1917 ;; 5. List tests.
1918 (org-test-with-temp-text "
1919 - item1
1921 - sub1
1923 - sub2
1925 - sub3
1927 Inner paragraph.
1929 - item2
1931 Outside."
1932 ;; 5.1. At list top point: expected to move to the element after
1933 ;; the list.
1934 (goto-line 2)
1935 (org-forward-element)
1936 (should (looking-at (regexp-quote "Outside.")))
1937 ;; 5.2. Special case: at the first line of a sub-list, but not at
1938 ;; beginning of line, move to next item.
1939 (goto-line 2)
1940 (forward-char)
1941 (org-forward-element)
1942 (should (looking-at "- item2"))
1943 (goto-line 4)
1944 (forward-char)
1945 (org-forward-element)
1946 (should (looking-at " - sub2"))
1947 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
1948 (goto-line 4)
1949 (org-forward-element)
1950 (should (looking-at (regexp-quote " Inner paragraph.")))
1951 ;; 5.4. At sub-list end: expected to move outside the sub-list.
1952 (goto-line 8)
1953 (org-forward-element)
1954 (should (looking-at (regexp-quote " Inner paragraph.")))
1955 ;; 5.5. At an item: expected to move to next item, if any.
1956 (goto-line 6)
1957 (org-forward-element)
1958 (should (looking-at " - sub3"))))
1960 (ert-deftest test-org/backward-element ()
1961 "Test `org-backward-element' specifications."
1962 ;; 1. Should error at BOB.
1963 (org-test-with-temp-text " \nParagraph."
1964 (should-error (org-backward-element)))
1965 ;; 2. Should move at BOB when called on the first element in buffer.
1966 (should
1967 (org-test-with-temp-text "\n#+TITLE: test"
1968 (progn (forward-line)
1969 (org-backward-element)
1970 (bobp))))
1971 ;; 3. Not at the beginning of an element: move at its beginning.
1972 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
1973 (goto-line 3)
1974 (end-of-line)
1975 (org-backward-element)
1976 (should (looking-at (regexp-quote "Paragraph2."))))
1977 ;; 4. Headline tests.
1978 (org-test-with-temp-text "
1979 * Head 1
1980 ** Head 1.1
1981 *** Head 1.1.1
1982 ** Head 1.2"
1983 ;; 4.1. At an headline beginning: move to previous headline at the
1984 ;; same level.
1985 (goto-line 5)
1986 (org-backward-element)
1987 (should (looking-at (regexp-quote "** Head 1.1")))
1988 ;; 4.2. At an headline beginning: move to parent headline if no
1989 ;; headline at the same level.
1990 (goto-line 3)
1991 (org-backward-element)
1992 (should (looking-at (regexp-quote "* Head 1")))
1993 ;; 4.3. At the first top-level headline: should error.
1994 (goto-line 2)
1995 (should-error (org-backward-element)))
1996 ;; 5. At beginning of first element inside a greater element:
1997 ;; expected to move to greater element's beginning.
1998 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
1999 (goto-line 3)
2000 (org-backward-element)
2001 (should (looking-at "#\\+BEGIN_CENTER")))
2002 ;; 6. At the beginning of the first element in a section: should
2003 ;; move back to headline, if any.
2004 (should
2005 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
2006 (progn (goto-char (point-max))
2007 (beginning-of-line)
2008 (org-backward-element)
2009 (org-at-heading-p))))
2010 ;; 7. List tests.
2011 (org-test-with-temp-text "
2012 - item1
2014 - sub1
2016 - sub2
2018 - sub3
2020 Inner paragraph.
2022 - item2
2025 Outside."
2026 ;; 7.1. At beginning of sub-list: expected to move to the
2027 ;; paragraph before it.
2028 (goto-line 4)
2029 (org-backward-element)
2030 (should (looking-at "item1"))
2031 ;; 7.2. At an item in a list: expected to move at previous item.
2032 (goto-line 8)
2033 (org-backward-element)
2034 (should (looking-at " - sub2"))
2035 (goto-line 12)
2036 (org-backward-element)
2037 (should (looking-at "- item1"))
2038 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
2039 ;; beginning.
2040 (goto-line 10)
2041 (org-backward-element)
2042 (should (looking-at " - sub1"))
2043 (goto-line 15)
2044 (org-backward-element)
2045 (should (looking-at "- item1"))
2046 ;; 7.4. At blank-lines before list end: expected to move to top
2047 ;; item.
2048 (goto-line 14)
2049 (org-backward-element)
2050 (should (looking-at "- item1"))))
2052 (ert-deftest test-org/up-element ()
2053 "Test `org-up-element' specifications."
2054 ;; 1. At BOB or with no surrounding element: should error.
2055 (org-test-with-temp-text "Paragraph."
2056 (should-error (org-up-element)))
2057 (org-test-with-temp-text "* Head1\n* Head2"
2058 (goto-line 2)
2059 (should-error (org-up-element)))
2060 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2061 (goto-line 3)
2062 (should-error (org-up-element)))
2063 ;; 2. At an headline: move to parent headline.
2064 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
2065 (goto-line 3)
2066 (org-up-element)
2067 (should (looking-at "\\* Head1")))
2068 ;; 3. Inside a greater element: move to greater element beginning.
2069 (org-test-with-temp-text
2070 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
2071 (goto-line 3)
2072 (org-up-element)
2073 (should (looking-at "#\\+BEGIN_CENTER")))
2074 ;; 4. List tests.
2075 (org-test-with-temp-text "* Top
2076 - item1
2078 - sub1
2080 - sub2
2082 Paragraph within sub2.
2084 - item2"
2085 ;; 4.1. Within an item: move to the item beginning.
2086 (goto-line 8)
2087 (org-up-element)
2088 (should (looking-at " - sub2"))
2089 ;; 4.2. At an item in a sub-list: move to parent item.
2090 (goto-line 4)
2091 (org-up-element)
2092 (should (looking-at "- item1"))
2093 ;; 4.3. At an item in top list: move to beginning of whole list.
2094 (goto-line 10)
2095 (org-up-element)
2096 (should (looking-at "- item1"))
2097 ;; 4.4. Special case. At very top point: should move to parent of
2098 ;; list.
2099 (goto-line 2)
2100 (org-up-element)
2101 (should (looking-at "\\* Top"))))
2103 (ert-deftest test-org/down-element ()
2104 "Test `org-down-element' specifications."
2105 ;; Error when the element hasn't got a recursive type.
2106 (org-test-with-temp-text "Paragraph."
2107 (should-error (org-down-element)))
2108 ;; Error when the element has no contents
2109 (org-test-with-temp-text "* Headline"
2110 (should-error (org-down-element)))
2111 ;; When at a plain-list, move to first item.
2112 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
2113 (goto-line 2)
2114 (org-down-element)
2115 (should (looking-at " - Item 1.1")))
2116 (org-test-with-temp-text "#+NAME: list\n- Item 1"
2117 (org-down-element)
2118 (should (looking-at " Item 1")))
2119 ;; When at a table, move to first row
2120 (org-test-with-temp-text "#+NAME: table\n| a | b |"
2121 (org-down-element)
2122 (should (looking-at " a | b |")))
2123 ;; Otherwise, move inside the greater element.
2124 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
2125 (org-down-element)
2126 (should (looking-at "Paragraph"))))
2128 (ert-deftest test-org/drag-element-backward ()
2129 "Test `org-drag-element-backward' specifications."
2130 ;; Standard test.
2131 (should
2132 (equal
2133 "#+key2: val2\n#+key1: val1\n#+key3: val3"
2134 (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
2135 (org-drag-element-backward)
2136 (buffer-string))))
2137 (should
2138 (equal
2139 "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
2140 (org-test-with-temp-text
2141 "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
2142 (org-drag-element-backward)
2143 (buffer-string))))
2144 ;; Preserve blank lines.
2145 (should
2146 (equal "Paragraph 2\n\n\nPara1\n\nPara3"
2147 (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
2148 (org-drag-element-backward)
2149 (buffer-string))))
2150 ;; Preserve column.
2151 (should
2152 (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
2153 (org-drag-element-backward)
2154 (org-looking-at-p "2")))
2155 ;; Error when trying to move first element of buffer.
2156 (should-error
2157 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2158 (org-drag-element-backward)))
2159 ;; Error when trying to swap nested elements.
2160 (should-error
2161 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2162 (forward-line)
2163 (org-drag-element-backward)))
2164 ;; Error when trying to swap an headline element and a non-headline
2165 ;; element.
2166 (should-error
2167 (org-test-with-temp-text "Test.\n* Head 1"
2168 (forward-line)
2169 (org-drag-element-backward)))
2170 ;; Preserve visibility of elements and their contents.
2171 (should
2172 (equal '((63 . 82) (26 . 48))
2173 (org-test-with-temp-text "
2174 #+BEGIN_CENTER
2175 Text.
2176 #+END_CENTER
2177 - item 1
2178 #+BEGIN_QUOTE
2179 Text.
2180 #+END_QUOTE"
2181 (while (search-forward "BEGIN_" nil t) (org-cycle))
2182 (search-backward "- item 1")
2183 (org-drag-element-backward)
2184 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2185 (overlays-in (point-min) (point-max)))))))
2187 (ert-deftest test-org/drag-element-forward ()
2188 "Test `org-drag-element-forward' specifications."
2189 ;; 1. Error when trying to move first element of buffer.
2190 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2191 (goto-line 3)
2192 (should-error (org-drag-element-forward)))
2193 ;; 2. Error when trying to swap nested elements.
2194 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2195 (forward-line)
2196 (should-error (org-drag-element-forward)))
2197 ;; 3. Error when trying to swap a non-headline element and an
2198 ;; headline.
2199 (org-test-with-temp-text "Test.\n* Head 1"
2200 (should-error (org-drag-element-forward)))
2201 ;; 4. Otherwise, swap elements, preserving column and blank lines
2202 ;; between elements.
2203 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
2204 (search-forward "graph")
2205 (org-drag-element-forward)
2206 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
2207 (should (looking-at " 1")))
2208 ;; 5. Preserve visibility of elements and their contents.
2209 (org-test-with-temp-text "
2210 #+BEGIN_CENTER
2211 Text.
2212 #+END_CENTER
2213 - item 1
2214 #+BEGIN_QUOTE
2215 Text.
2216 #+END_QUOTE"
2217 (while (search-forward "BEGIN_" nil t) (org-cycle))
2218 (search-backward "#+BEGIN_CENTER")
2219 (org-drag-element-forward)
2220 (should
2221 (equal
2222 '((63 . 82) (26 . 48))
2223 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2224 (overlays-in (point-min) (point-max)))))))
2227 ;;; Outline structure
2229 (ert-deftest test-org/demote ()
2230 "Test `org-demote' specifications."
2231 ;; Add correct number of stars according to `org-odd-levels-only'.
2232 (should
2233 (= 2
2234 (org-test-with-temp-text "* H"
2235 (let ((org-odd-levels-only nil)) (org-demote))
2236 (org-current-level))))
2237 (should
2238 (= 3
2239 (org-test-with-temp-text "* H"
2240 (let ((org-odd-levels-only t)) (org-demote))
2241 (org-current-level))))
2242 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2243 (should
2244 (org-test-with-temp-text "* H :tag:"
2245 (let ((org-tags-column 10)
2246 (org-auto-align-tags t)
2247 (org-odd-levels-only nil))
2248 (org-demote))
2249 (org-move-to-column 10)
2250 (org-looking-at-p ":tag:$")))
2251 (should-not
2252 (org-test-with-temp-text "* H :tag:"
2253 (let ((org-tags-column 10)
2254 (org-auto-align-tags nil)
2255 (org-odd-levels-only nil))
2256 (org-demote))
2257 (org-move-to-column 10)
2258 (org-looking-at-p ":tag:$")))
2259 ;; When `org-adapt-indentation' is non-nil, always indent planning
2260 ;; info and property drawers accordingly.
2261 (should
2262 (= 3
2263 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2264 (let ((org-odd-levels-only nil)
2265 (org-adapt-indentation t))
2266 (org-demote))
2267 (forward-line)
2268 (org-get-indentation))))
2269 (should
2270 (= 3
2271 (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2272 (let ((org-odd-levels-only nil)
2273 (org-adapt-indentation t))
2274 (org-demote))
2275 (forward-line)
2276 (org-get-indentation))))
2277 (should-not
2278 (= 3
2279 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2280 (let ((org-odd-levels-only nil)
2281 (org-adapt-indentation nil))
2282 (org-demote))
2283 (forward-line)
2284 (org-get-indentation))))
2285 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2286 ;; section accordingly. Ignore, however, footnote definitions and
2287 ;; inlinetasks boundaries.
2288 (should
2289 (= 3
2290 (org-test-with-temp-text "* H\n Paragraph"
2291 (let ((org-odd-levels-only nil)
2292 (org-adapt-indentation t))
2293 (org-demote))
2294 (forward-line)
2295 (org-get-indentation))))
2296 (should
2297 (= 2
2298 (org-test-with-temp-text "* H\n Paragraph"
2299 (let ((org-odd-levels-only nil)
2300 (org-adapt-indentation nil))
2301 (org-demote))
2302 (forward-line)
2303 (org-get-indentation))))
2304 (should
2305 (zerop
2306 (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
2307 (let ((org-odd-levels-only nil)
2308 (org-adapt-indentation t))
2309 (org-demote))
2310 (goto-char (point-max))
2311 (org-get-indentation))))
2312 (should
2313 (= 3
2314 (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
2315 (let ((org-odd-levels-only nil)
2316 (org-adapt-indentation t))
2317 (org-demote))
2318 (goto-char (point-max))
2319 (org-get-indentation))))
2320 (when (featurep 'org-inlinetask)
2321 (should
2322 (zerop
2323 (let ((org-inlinetask-min-level 5)
2324 (org-adapt-indentation t))
2325 (org-test-with-temp-text "* H\n***** I\n***** END"
2326 (org-demote)
2327 (forward-line)
2328 (org-get-indentation))))))
2329 (when (featurep 'org-inlinetask)
2330 (should
2331 (= 3
2332 (let ((org-inlinetask-min-level 5)
2333 (org-adapt-indentation t))
2334 (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
2335 (org-demote)
2336 (forward-line 2)
2337 (org-get-indentation))))))
2338 ;; Ignore contents of source blocks or example blocks when
2339 ;; indentation should be preserved (through
2340 ;; `org-src-preserve-indentation' or "-i" flag).
2341 (should-not
2342 (zerop
2343 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2344 (let ((org-adapt-indentation t)
2345 (org-src-preserve-indentation nil))
2346 (org-demote))
2347 (forward-line 2)
2348 (org-get-indentation))))
2349 (should
2350 (zerop
2351 (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2352 (let ((org-adapt-indentation t)
2353 (org-src-preserve-indentation t))
2354 (org-demote))
2355 (forward-line 2)
2356 (org-get-indentation))))
2357 (should
2358 (zerop
2359 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2360 (let ((org-adapt-indentation t)
2361 (org-src-preserve-indentation t))
2362 (org-demote))
2363 (forward-line 2)
2364 (org-get-indentation))))
2365 (should
2366 (zerop
2367 (org-test-with-temp-text
2368 "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
2369 (let ((org-adapt-indentation t)
2370 (org-src-preserve-indentation nil))
2371 (org-demote))
2372 (forward-line 2)
2373 (org-get-indentation)))))
2375 (ert-deftest test-org/promote ()
2376 "Test `org-promote' specifications."
2377 ;; Return an error if headline is to be promoted to level 0, unless
2378 ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
2379 ;; headline becomes a comment.
2380 (should-error
2381 (org-test-with-temp-text "* H"
2382 (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
2383 (should
2384 (equal "# H"
2385 (org-test-with-temp-text "* H"
2386 (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
2387 (buffer-string))))
2388 ;; Remove correct number of stars according to
2389 ;; `org-odd-levels-only'.
2390 (should
2391 (= 2
2392 (org-test-with-temp-text "*** H"
2393 (let ((org-odd-levels-only nil)) (org-promote))
2394 (org-current-level))))
2395 (should
2396 (= 1
2397 (org-test-with-temp-text "*** H"
2398 (let ((org-odd-levels-only t)) (org-promote))
2399 (org-current-level))))
2400 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2401 (should
2402 (org-test-with-temp-text "** H :tag:"
2403 (let ((org-tags-column 10)
2404 (org-auto-align-tags t)
2405 (org-odd-levels-only nil))
2406 (org-promote))
2407 (org-move-to-column 10)
2408 (org-looking-at-p ":tag:$")))
2409 (should-not
2410 (org-test-with-temp-text "** H :tag:"
2411 (let ((org-tags-column 10)
2412 (org-auto-align-tags nil)
2413 (org-odd-levels-only nil))
2414 (org-promote))
2415 (org-move-to-column 10)
2416 (org-looking-at-p ":tag:$")))
2417 ;; When `org-adapt-indentation' is non-nil, always indent planning
2418 ;; info and property drawers.
2419 (should
2420 (= 2
2421 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2422 (let ((org-odd-levels-only nil)
2423 (org-adapt-indentation t))
2424 (org-promote))
2425 (forward-line)
2426 (org-get-indentation))))
2427 (should
2428 (= 2
2429 (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2430 (let ((org-odd-levels-only nil)
2431 (org-adapt-indentation t))
2432 (org-promote))
2433 (forward-line)
2434 (org-get-indentation))))
2435 (should-not
2436 (= 2
2437 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2438 (let ((org-odd-levels-only nil)
2439 (org-adapt-indentation nil))
2440 (org-promote))
2441 (forward-line)
2442 (org-get-indentation))))
2443 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2444 ;; section accordingly. Ignore, however, footnote definitions and
2445 ;; inlinetasks boundaries.
2446 (should
2447 (= 2
2448 (org-test-with-temp-text "** H\n Paragraph"
2449 (let ((org-odd-levels-only nil)
2450 (org-adapt-indentation t))
2451 (org-promote))
2452 (forward-line)
2453 (org-get-indentation))))
2454 (should-not
2455 (= 2
2456 (org-test-with-temp-text "** H\n Paragraph"
2457 (let ((org-odd-levels-only nil)
2458 (org-adapt-indentation nil))
2459 (org-promote))
2460 (forward-line)
2461 (org-get-indentation))))
2462 (should
2463 (= 2
2464 (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
2465 (let ((org-odd-levels-only nil)
2466 (org-adapt-indentation t))
2467 (org-promote))
2468 (forward-line)
2469 (org-get-indentation))))
2470 (when (featurep 'org-inlinetask)
2471 (should
2472 (zerop
2473 (let ((org-inlinetask-min-level 5)
2474 (org-adapt-indentation t))
2475 (org-test-with-temp-text "** H\n***** I\n***** END"
2476 (org-promote)
2477 (forward-line)
2478 (org-get-indentation))))))
2479 (when (featurep 'org-inlinetask)
2480 (should
2481 (= 2
2482 (let ((org-inlinetask-min-level 5)
2483 (org-adapt-indentation t))
2484 (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
2485 (org-promote)
2486 (forward-line 2)
2487 (org-get-indentation))))))
2488 ;; Give up shifting if it would break document's structure
2489 ;; otherwise.
2490 (should
2491 (= 3
2492 (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
2493 (let ((org-odd-levels-only nil)
2494 (org-adapt-indentation t))
2495 (org-promote))
2496 (forward-line)
2497 (org-get-indentation))))
2498 (should
2499 (= 3
2500 (org-test-with-temp-text "** H\n Paragraph\n * list."
2501 (let ((org-odd-levels-only nil)
2502 (org-adapt-indentation t))
2503 (org-promote))
2504 (forward-line)
2505 (org-get-indentation))))
2506 ;; Ignore contents of source blocks or example blocks when
2507 ;; indentation should be preserved (through
2508 ;; `org-src-preserve-indentation' or "-i" flag).
2509 (should-not
2510 (zerop
2511 (org-test-with-temp-text
2512 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2513 (let ((org-adapt-indentation t)
2514 (org-src-preserve-indentation nil)
2515 (org-odd-levels-only nil))
2516 (org-promote))
2517 (forward-line)
2518 (org-get-indentation))))
2519 (should
2520 (zerop
2521 (org-test-with-temp-text
2522 "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
2523 (let ((org-adapt-indentation t)
2524 (org-src-preserve-indentation t)
2525 (org-odd-levels-only nil))
2526 (org-promote))
2527 (forward-line)
2528 (org-get-indentation))))
2529 (should
2530 (zerop
2531 (org-test-with-temp-text
2532 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2533 (let ((org-adapt-indentation t)
2534 (org-src-preserve-indentation t)
2535 (org-odd-levels-only nil))
2536 (org-promote))
2537 (forward-line)
2538 (org-get-indentation))))
2539 (should
2540 (zerop
2541 (org-test-with-temp-text
2542 "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
2543 (let ((org-adapt-indentation t)
2544 (org-src-preserve-indentation nil)
2545 (org-odd-levels-only nil))
2546 (org-promote))
2547 (forward-line)
2548 (org-get-indentation)))))
2551 ;;; Planning
2553 (ert-deftest test-org/at-planning-p ()
2554 "Test `org-at-planning-p' specifications."
2555 ;; Regular test.
2556 (should
2557 (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
2558 (org-at-planning-p)))
2559 (should-not
2560 (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
2561 (org-at-planning-p)))
2562 ;; Correctly find planning attached to inlinetasks.
2563 (when (featurep 'org-inlinetask)
2564 (should
2565 (org-test-with-temp-text
2566 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
2567 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2568 (should-not
2569 (org-test-with-temp-text
2570 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
2571 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2572 (should-not
2573 (org-test-with-temp-text
2574 "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
2575 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2576 (should-not
2577 (org-test-with-temp-text
2578 "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
2579 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
2582 ;;; Property API
2584 (ert-deftest test-org/buffer-property-keys ()
2585 "Test `org-buffer-property-keys' specifications."
2586 ;; Retrieve properties accross siblings.
2587 (should
2588 (equal '("A" "B")
2589 (org-test-with-temp-text "
2590 * H1
2591 :PROPERTIES:
2592 :A: 1
2593 :END:
2594 * H2
2595 :PROPERTIES:
2596 :B: 1
2597 :END:"
2598 (org-buffer-property-keys))))
2599 ;; Retrieve properties accross children.
2600 (should
2601 (equal '("A" "B")
2602 (org-test-with-temp-text "
2603 * H1
2604 :PROPERTIES:
2605 :A: 1
2606 :END:
2607 ** H2
2608 :PROPERTIES:
2609 :B: 1
2610 :END:"
2611 (org-buffer-property-keys))))
2612 ;; Retrieve muliple properties in the same drawer.
2613 (should
2614 (equal '("A" "B")
2615 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2616 (org-buffer-property-keys))))
2617 ;; Ignore extension symbol in property name.
2618 (should
2619 (equal '("A")
2620 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
2621 (org-buffer-property-keys))))
2622 ;; With non-nil COLUMNS, extract property names from columns.
2623 (should
2624 (equal '("A" "B")
2625 (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
2626 (org-buffer-property-keys nil nil t))))
2627 (should
2628 (equal '("A" "B" "COLUMNS")
2629 (org-test-with-temp-text
2630 "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
2631 (org-buffer-property-keys nil nil t)))))
2633 (ert-deftest test-org/property-values ()
2634 "Test `org-property-values' specifications."
2635 ;; Regular test.
2636 (should
2637 (equal '("2" "1")
2638 (org-test-with-temp-text
2639 "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
2640 (org-property-values "A"))))
2641 ;; Ignore empty values.
2642 (should-not
2643 (org-test-with-temp-text
2644 "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
2645 (org-property-values "A")))
2646 ;; Take into consideration extended values.
2647 (should
2648 (equal '("1 2")
2649 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
2650 (org-property-values "A")))))
2652 (ert-deftest test-org/find-property ()
2653 "Test `org-find-property' specifications."
2654 ;; Regular test.
2655 (should
2656 (= 1
2657 (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
2658 (org-find-property "prop"))))
2659 ;; Ignore false positives.
2660 (should
2661 (= 27
2662 (org-test-with-temp-text
2663 "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
2664 (org-find-property "A"))))
2665 ;; Return first entry found in buffer.
2666 (should
2667 (= 1
2668 (org-test-with-temp-text
2669 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
2670 (org-find-property "A"))))
2671 ;; Only search visible part of the buffer.
2672 (should
2673 (= 31
2674 (org-test-with-temp-text
2675 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
2676 (org-narrow-to-subtree)
2677 (org-find-property "A"))))
2678 ;; With optional argument, only find entries with a specific value.
2679 (should-not
2680 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2681 (org-find-property "A" "2")))
2682 (should
2683 (= 31
2684 (org-test-with-temp-text
2685 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
2686 (org-find-property "A" "2"))))
2687 ;; Use "nil" for explicit nil values.
2688 (should
2689 (= 31
2690 (org-test-with-temp-text
2691 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
2692 (org-find-property "A" "nil"))))
2693 ;; Optional argument is matched against real value, including PROP+
2694 ;; syntax.
2695 (should
2696 (= 1
2697 (org-test-with-temp-text "* H1\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
2698 (org-find-property "A" "1 2")))))
2700 (ert-deftest test-org/entry-delete ()
2701 "Test `org-entry-delete' specifications."
2702 ;; Regular test.
2703 (should
2704 (string-match
2705 " *:PROPERTIES:\n *:B: +2\n *:END:"
2706 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2707 (org-entry-delete (point) "A")
2708 (buffer-string))))
2709 ;; Also remove accumulated properties.
2710 (should-not
2711 (string-match
2712 ":A"
2713 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
2714 (org-entry-delete (point) "A")
2715 (buffer-string))))
2716 ;; When last property is removed, remove the property drawer.
2717 (should-not
2718 (string-match
2719 ":PROPERTIES:"
2720 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
2721 (org-entry-delete (point) "A")
2722 (buffer-string))))
2723 ;; Return a non-nil value when some property was removed.
2724 (should
2725 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2726 (org-entry-delete (point) "A")))
2727 (should-not
2728 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2729 (org-entry-delete (point) "C"))))
2731 (ert-deftest test-org/entry-get ()
2732 "Test `org-entry-get' specifications."
2733 ;; Regular test.
2734 (should
2735 (equal "1"
2736 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2737 (org-entry-get (point) "A"))))
2738 ;; Ignore case.
2739 (should
2740 (equal "1"
2741 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2742 (org-entry-get (point) "a"))))
2743 ;; Handle extended values, both before and after base value.
2744 (should
2745 (equal "1 2 3"
2746 (org-test-with-temp-text
2747 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
2748 (org-entry-get (point) "A"))))
2749 ;; Empty values are returned as the empty string.
2750 (should
2751 (equal ""
2752 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
2753 (org-entry-get (point) "A"))))
2754 ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
2755 ;; otherwise, return nil.
2756 (should-not
2757 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
2758 (org-entry-get (point) "A")))
2759 (should
2760 (equal "nil"
2761 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
2762 (org-entry-get (point) "A" nil t))))
2763 ;; Return nil when no property is found, independently on the
2764 ;; LITERAL-NIL argument.
2765 (should-not
2766 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2767 (org-entry-get (point) "B")))
2768 (should-not
2769 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2770 (org-entry-get (point) "B" nil t)))
2771 ;; Handle inheritance, when allowed.
2772 (should
2773 (equal
2775 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2776 (org-entry-get (point) "A" t))))
2777 (should
2778 (equal
2780 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2781 (let ((org-use-property-inheritance t))
2782 (org-entry-get (point) "A" 'selective)))))
2783 (should-not
2784 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2785 (let ((org-use-property-inheritance nil))
2786 (org-entry-get (point) "A" 'selective)))))
2788 (ert-deftest test-org/entry-properties ()
2789 "Test `org-entry-properties' specifications."
2790 ;; Get "ITEM" property.
2791 (should
2792 (equal "* H"
2793 (org-test-with-temp-text "* TODO H"
2794 (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
2795 (should
2796 (equal "* H"
2797 (org-test-with-temp-text "* TODO H"
2798 (cdr (assoc "ITEM" (org-entry-properties))))))
2799 ;; Get "TODO" property.
2800 (should
2801 (equal "TODO"
2802 (org-test-with-temp-text "* TODO H"
2803 (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
2804 (should
2805 (equal "TODO"
2806 (org-test-with-temp-text "* TODO H"
2807 (cdr (assoc "TODO" (org-entry-properties))))))
2808 (should-not
2809 (org-test-with-temp-text "* H"
2810 (assoc "TODO" (org-entry-properties nil "TODO"))))
2811 ;; Get "PRIORITY" property.
2812 (should
2813 (equal "A"
2814 (org-test-with-temp-text "* [#A] H"
2815 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
2816 (should
2817 (equal "A"
2818 (org-test-with-temp-text "* [#A] H"
2819 (cdr (assoc "PRIORITY" (org-entry-properties))))))
2820 (should-not
2821 (org-test-with-temp-text "* H"
2822 (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
2823 ;; Get "FILE" property.
2824 (should
2825 (org-test-with-temp-text-in-file "* H\nParagraph"
2826 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
2827 (buffer-file-name))))
2828 (should
2829 (org-test-with-temp-text-in-file "* H\nParagraph"
2830 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
2831 (buffer-file-name))))
2832 (should-not
2833 (org-test-with-temp-text "* H\nParagraph"
2834 (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
2835 ;; Get "TAGS" property.
2836 (should
2837 (equal ":tag1:tag2:"
2838 (org-test-with-temp-text "* H :tag1:tag2:"
2839 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
2840 (should
2841 (equal ":tag1:tag2:"
2842 (org-test-with-temp-text "* H :tag1:tag2:"
2843 (cdr (assoc "TAGS" (org-entry-properties))))))
2844 (should-not
2845 (org-test-with-temp-text "* H"
2846 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
2847 ;; Get "ALLTAGS" property.
2848 (should
2849 (equal ":tag1:tag2:"
2850 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
2851 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
2852 (should
2853 (equal ":tag1:tag2:"
2854 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
2855 (cdr (assoc "ALLTAGS" (org-entry-properties))))))
2856 (should-not
2857 (org-test-with-temp-text "* H"
2858 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
2859 ;; Get "BLOCKED" property.
2860 (should
2861 (equal "t"
2862 (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
2863 (let ((org-enforce-todo-dependencies t)
2864 (org-blocker-hook
2865 '(org-block-todo-from-children-or-siblings-or-parent)))
2866 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
2867 (should
2868 (equal "t"
2869 (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
2870 (let ((org-enforce-todo-dependencies t)
2871 (org-blocker-hook
2872 '(org-block-todo-from-children-or-siblings-or-parent)))
2873 (cdr (assoc "BLOCKED" (org-entry-properties)))))))
2874 (should
2875 (equal ""
2876 (org-test-with-temp-text "* Blocked\n** DONE one\n** DONE two"
2877 (let ((org-enforce-todo-dependencies t))
2878 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
2879 ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
2880 (should
2881 (equal
2882 "[2012-03-29 thu.]"
2883 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
2884 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
2885 (should
2886 (equal
2887 "[2012-03-29 thu.]"
2888 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
2889 (cdr (assoc "CLOSED" (org-entry-properties))))))
2890 (should-not
2891 (org-test-with-temp-text "* H"
2892 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
2893 (should
2894 (equal
2895 "<2014-03-04 tue.>"
2896 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
2897 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
2898 (should
2899 (equal
2900 "<2014-03-04 tue.>"
2901 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
2902 (cdr (assoc "DEADLINE" (org-entry-properties))))))
2903 (should-not
2904 (org-test-with-temp-text "* H"
2905 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
2906 (should
2907 (equal
2908 "<2014-03-04 tue.>"
2909 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2910 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
2911 (should
2912 (equal
2913 "<2014-03-04 tue.>"
2914 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2915 (cdr (assoc "SCHEDULED" (org-entry-properties))))))
2916 (should-not
2917 (org-test-with-temp-text "* H"
2918 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
2919 ;; Get "CATEGORY"
2920 (should
2921 (equal "cat"
2922 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
2923 (cdr (assoc "CATEGORY" (org-entry-properties))))))
2924 (should
2925 (equal "cat"
2926 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
2927 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
2928 (should
2929 (equal "cat"
2930 (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
2931 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
2932 ;; Get standard properties.
2933 (should
2934 (equal "1"
2935 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2936 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
2937 ;; Handle extended properties.
2938 (should
2939 (equal "1 2 3"
2940 (org-test-with-temp-text
2941 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
2942 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
2943 (should
2944 (equal "1 2 3"
2945 (org-test-with-temp-text
2946 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
2947 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
2948 ;; Ignore forbidden (special) properties.
2949 (should-not
2950 (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
2951 (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
2953 (ert-deftest test-org/entry-put ()
2954 "Test `org-entry-put' specifications."
2955 ;; Error when not a string or nil.
2956 (should-error
2957 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
2958 (org-entry-put 1 "test" 2)))
2959 ;; Set "TODO" property.
2960 (should
2961 (string-match (regexp-quote " TODO H")
2962 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
2963 (org-entry-put (point) "TODO" "TODO")
2964 (buffer-string))))
2965 (should
2966 (string-match (regexp-quote "* H")
2967 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
2968 (org-entry-put (point) "TODO" nil)
2969 (buffer-string))))
2970 ;; Set "PRIORITY" property.
2971 (should
2972 (equal "* [#A] H"
2973 (org-test-with-temp-text "* [#B] H"
2974 (org-entry-put (point) "PRIORITY" "A")
2975 (buffer-string))))
2976 (should
2977 (equal "* H"
2978 (org-test-with-temp-text "* [#B] H"
2979 (org-entry-put (point) "PRIORITY" nil)
2980 (buffer-string))))
2981 ;; Set "SCHEDULED" property.
2982 (should
2983 (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
2984 (org-test-with-temp-text "* H"
2985 (org-entry-put (point) "SCHEDULED" "2014-03-04")
2986 (buffer-string))))
2987 (should
2988 (string= "* H\n"
2989 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2990 (org-entry-put (point) "SCHEDULED" nil)
2991 (buffer-string))))
2992 (should
2993 (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
2994 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2995 (org-entry-put (point) "SCHEDULED" "earlier")
2996 (buffer-string))))
2997 (should
2998 (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
2999 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3000 (org-entry-put (point) "SCHEDULED" "later")
3001 (buffer-string))))
3002 ;; Set "DEADLINE" property.
3003 (should
3004 (string-match "^ *DEADLINE: <2014-03-04 .*?>"
3005 (org-test-with-temp-text "* H"
3006 (org-entry-put (point) "DEADLINE" "2014-03-04")
3007 (buffer-string))))
3008 (should
3009 (string= "* H\n"
3010 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3011 (org-entry-put (point) "DEADLINE" nil)
3012 (buffer-string))))
3013 (should
3014 (string-match "^ *DEADLINE: <2014-03-03 .*?>"
3015 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3016 (org-entry-put (point) "DEADLINE" "earlier")
3017 (buffer-string))))
3018 (should
3019 (string-match "^ *DEADLINE: <2014-03-05 .*?>"
3020 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3021 (org-entry-put (point) "DEADLINE" "later")
3022 (buffer-string))))
3023 ;; Set "CATEGORY" property
3024 (should
3025 (string-match "^ *:CATEGORY: cat"
3026 (org-test-with-temp-text "* H"
3027 (org-entry-put (point) "CATEGORY" "cat")
3028 (buffer-string))))
3029 ;; Regular properties, with or without pre-existing drawer.
3030 (should
3031 (string-match "^ *:A: +2$"
3032 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3033 (org-entry-put (point) "A" "2")
3034 (buffer-string))))
3035 (should
3036 (string-match "^ *:A: +1$"
3037 (org-test-with-temp-text "* H"
3038 (org-entry-put (point) "A" "1")
3039 (buffer-string))))
3040 ;; Special case: two consecutive headlines.
3041 (should
3042 (string-match "\\* A\n *:PROPERTIES:"
3043 (org-test-with-temp-text "* A\n** B"
3044 (org-entry-put (point) "A" "1")
3045 (buffer-string)))))
3048 ;;; Radio Targets
3050 (ert-deftest test-org/update-radio-target-regexp ()
3051 "Test `org-update-radio-target-regexp' specifications."
3052 ;; Properly update cache with no previous radio target regexp.
3053 (should
3054 (eq 'link
3055 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3056 (save-excursion (goto-char (point-max)) (org-element-context))
3057 (insert "<<<")
3058 (search-forward "o")
3059 (insert ">>>")
3060 (org-update-radio-target-regexp)
3061 (goto-char (point-max))
3062 (org-element-type (org-element-context)))))
3063 ;; Properly update cache with previous radio target regexp.
3064 (should
3065 (eq 'link
3066 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3067 (save-excursion (goto-char (point-max)) (org-element-context))
3068 (insert "<<<")
3069 (search-forward "o")
3070 (insert ">>>")
3071 (org-update-radio-target-regexp)
3072 (search-backward "r")
3073 (delete-char 5)
3074 (insert "new")
3075 (org-update-radio-target-regexp)
3076 (goto-char (point-max))
3077 (delete-region (line-beginning-position) (point))
3078 (insert "new")
3079 (org-element-type (org-element-context))))))
3082 ;;; Sparse trees
3084 (ert-deftest test-org/match-sparse-tree ()
3085 "Test `org-match-sparse-tree' specifications."
3086 ;; Match tags.
3087 (should-not
3088 (org-test-with-temp-text "* H\n** H1 :tag:"
3089 (org-match-sparse-tree nil "tag")
3090 (search-forward "H1")
3091 (org-invisible-p2)))
3092 (should
3093 (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
3094 (org-match-sparse-tree nil "tag")
3095 (search-forward "H2")
3096 (org-invisible-p2)))
3097 ;; "-" operator for tags.
3098 (should-not
3099 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3100 (org-match-sparse-tree nil "tag1-tag2")
3101 (search-forward "H1")
3102 (org-invisible-p2)))
3103 (should
3104 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3105 (org-match-sparse-tree nil "tag1-tag2")
3106 (search-forward "H2")
3107 (org-invisible-p2)))
3108 ;; "&" operator for tags.
3109 (should
3110 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3111 (org-match-sparse-tree nil "tag1&tag2")
3112 (search-forward "H1")
3113 (org-invisible-p2)))
3114 (should-not
3115 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3116 (org-match-sparse-tree nil "tag1&tag2")
3117 (search-forward "H2")
3118 (org-invisible-p2)))
3119 ;; "|" operator for tags.
3120 (should-not
3121 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3122 (org-match-sparse-tree nil "tag1|tag2")
3123 (search-forward "H1")
3124 (org-invisible-p2)))
3125 (should-not
3126 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3127 (org-match-sparse-tree nil "tag1|tag2")
3128 (search-forward "H2")
3129 (org-invisible-p2)))
3130 ;; Regexp match on tags.
3131 (should-not
3132 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3133 (org-match-sparse-tree nil "{^tag.*}")
3134 (search-forward "H1")
3135 (org-invisible-p2)))
3136 (should
3137 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3138 (org-match-sparse-tree nil "{^tag.*}")
3139 (search-forward "H2")
3140 (org-invisible-p2)))
3141 ;; Match group tags.
3142 (should-not
3143 (org-test-with-temp-text
3144 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3145 (org-match-sparse-tree nil "work")
3146 (search-forward "H1")
3147 (org-invisible-p2)))
3148 (should-not
3149 (org-test-with-temp-text
3150 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3151 (org-match-sparse-tree nil "work")
3152 (search-forward "H2")
3153 (org-invisible-p2)))
3154 ;; Match properties.
3155 (should
3156 (org-test-with-temp-text
3157 "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
3158 (org-match-sparse-tree nil "A=\"1\"")
3159 (search-forward "H2")
3160 (org-invisible-p2)))
3161 (should-not
3162 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3163 (org-match-sparse-tree nil "A=\"1\"")
3164 (search-forward "H2")
3165 (org-invisible-p2)))
3166 ;; Case is not significant when matching properties.
3167 (should-not
3168 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3169 (org-match-sparse-tree nil "a=\"1\"")
3170 (search-forward "H2")
3171 (org-invisible-p2)))
3172 (should-not
3173 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
3174 (org-match-sparse-tree nil "A=\"1\"")
3175 (search-forward "H2")
3176 (org-invisible-p2)))
3177 ;; Match special LEVEL property.
3178 (should-not
3179 (org-test-with-temp-text "* H\n** H1\n*** H2"
3180 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3181 (search-forward "H1")
3182 (org-invisible-p2)))
3183 (should
3184 (org-test-with-temp-text "* H\n** H1\n*** H2"
3185 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3186 (search-forward "H2")
3187 (org-invisible-p2)))
3188 ;; Comparison operators when matching properties.
3189 (should
3190 (org-test-with-temp-text
3191 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3192 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3193 (search-forward "H1")
3194 (org-invisible-p2)))
3195 (should-not
3196 (org-test-with-temp-text
3197 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3198 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3199 (search-forward "H2")
3200 (org-invisible-p2)))
3201 ;; Regexp match on properties values.
3202 (should-not
3203 (org-test-with-temp-text
3204 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3205 (org-match-sparse-tree nil "A={f.*}")
3206 (search-forward "H1")
3207 (org-invisible-p2)))
3208 (should
3209 (org-test-with-temp-text
3210 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3211 (org-match-sparse-tree nil "A={f.*}")
3212 (search-forward "H2")
3213 (org-invisible-p2)))
3214 ;; With an optional argument, limit match to TODO entries.
3215 (should-not
3216 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3217 (org-match-sparse-tree t "tag")
3218 (search-forward "H1")
3219 (org-invisible-p2)))
3220 (should
3221 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3222 (org-match-sparse-tree t "tag")
3223 (search-forward "H2")
3224 (org-invisible-p2))))
3227 ;;; Timestamps API
3229 (ert-deftest test-org/time-stamp ()
3230 "Test `org-time-stamp' specifications."
3231 ;; Insert chosen time stamp at point.
3232 (should
3233 (string-match
3234 "Te<2014-03-04 .*?>xt"
3235 (org-test-with-temp-text "Te<point>xt"
3236 (flet ((org-read-date
3237 (&rest args)
3238 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3239 (org-time-stamp nil)
3240 (buffer-string)))))
3241 ;; With a prefix argument, also insert time.
3242 (should
3243 (string-match
3244 "Te<2014-03-04 .*? 00:41>xt"
3245 (org-test-with-temp-text "Te<point>xt"
3246 (flet ((org-read-date
3247 (&rest args)
3248 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
3249 (org-time-stamp '(4))
3250 (buffer-string)))))
3251 ;; With two universal prefix arguments, insert an active timestamp
3252 ;; with the current time without prompting the user.
3253 (should
3254 (string-match
3255 "Te<2014-03-04 .*? 00:41>xt"
3256 (org-test-with-temp-text "Te<point>xt"
3257 (flet ((current-time
3259 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
3260 (org-time-stamp '(16))
3261 (buffer-string)))))
3262 ;; When optional argument is non-nil, insert an inactive timestamp.
3263 (should
3264 (string-match
3265 "Te\\[2014-03-04 .*?\\]xt"
3266 (org-test-with-temp-text "Te<point>xt"
3267 (flet ((org-read-date
3268 (&rest args)
3269 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3270 (org-time-stamp nil t)
3271 (buffer-string)))))
3272 ;; When called from a timestamp, replace existing one.
3273 (should
3274 (string-match
3275 "<2014-03-04 .*?>"
3276 (org-test-with-temp-text "<2012-03-29<point> thu.>"
3277 (flet ((org-read-date
3278 (&rest args)
3279 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3280 (org-time-stamp nil)
3281 (buffer-string)))))
3282 (should
3283 (string-match
3284 "<2014-03-04 .*?>--<2014-03-04 .*?>"
3285 (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
3286 (flet ((org-read-date
3287 (&rest args)
3288 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3289 (org-time-stamp nil)
3290 (buffer-string)))))
3291 ;; When replacing a timestamp, preserve repeater, if any.
3292 (should
3293 (string-match
3294 "<2014-03-04 .*? \\+2y>"
3295 (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
3296 (flet ((org-read-date
3297 (&rest args)
3298 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3299 (org-time-stamp nil)
3300 (buffer-string)))))
3301 ;; When called twice in a raw, build a date range.
3302 (should
3303 (string-match
3304 "<2012-03-29 .*?>--<2014-03-04 .*?>"
3305 (org-test-with-temp-text "<2012-03-29 thu.><point>"
3306 (flet ((org-read-date
3307 (&rest args)
3308 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3309 (let ((last-command 'org-time-stamp)
3310 (this-command 'org-time-stamp))
3311 (org-time-stamp nil))
3312 (buffer-string))))))
3314 (ert-deftest test-org/timestamp-has-time-p ()
3315 "Test `org-timestamp-has-time-p' specifications."
3316 ;; With time.
3317 (should
3318 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
3319 (org-timestamp-has-time-p (org-element-context))))
3320 ;; Without time.
3321 (should-not
3322 (org-test-with-temp-text "<2012-03-29 Thu>"
3323 (org-timestamp-has-time-p (org-element-context)))))
3325 (ert-deftest test-org/timestamp-format ()
3326 "Test `org-timestamp-format' specifications."
3327 ;; Regular test.
3328 (should
3329 (equal
3330 "2012-03-29 16:40"
3331 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
3332 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
3333 ;; Range end.
3334 (should
3335 (equal
3336 "2012-03-29"
3337 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
3338 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
3340 (ert-deftest test-org/timestamp-split-range ()
3341 "Test `org-timestamp-split-range' specifications."
3342 ;; Extract range start (active).
3343 (should
3344 (equal '(2012 3 29)
3345 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3346 (let ((ts (org-timestamp-split-range (org-element-context))))
3347 (mapcar (lambda (p) (org-element-property p ts))
3348 '(:year-end :month-end :day-end))))))
3349 ;; Extract range start (inactive)
3350 (should
3351 (equal '(2012 3 29)
3352 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
3353 (let ((ts (org-timestamp-split-range (org-element-context))))
3354 (mapcar (lambda (p) (org-element-property p ts))
3355 '(:year-end :month-end :day-end))))))
3356 ;; Extract range end (active).
3357 (should
3358 (equal '(2012 3 30)
3359 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3360 (let ((ts (org-timestamp-split-range
3361 (org-element-context) t)))
3362 (mapcar (lambda (p) (org-element-property p ts))
3363 '(:year-end :month-end :day-end))))))
3364 ;; Extract range end (inactive)
3365 (should
3366 (equal '(2012 3 30)
3367 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
3368 (let ((ts (org-timestamp-split-range
3369 (org-element-context) t)))
3370 (mapcar (lambda (p) (org-element-property p ts))
3371 '(:year-end :month-end :day-end))))))
3372 ;; Return the timestamp if not a range.
3373 (should
3374 (org-test-with-temp-text "[2012-03-29 Thu]"
3375 (let* ((ts-orig (org-element-context))
3376 (ts-copy (org-timestamp-split-range ts-orig)))
3377 (eq ts-orig ts-copy))))
3378 (should
3379 (org-test-with-temp-text "<%%(org-float t 4 2)>"
3380 (let* ((ts-orig (org-element-context))
3381 (ts-copy (org-timestamp-split-range ts-orig)))
3382 (eq ts-orig ts-copy)))))
3384 (ert-deftest test-org/timestamp-translate ()
3385 "Test `org-timestamp-translate' specifications."
3386 ;; Translate whole date range.
3387 (should
3388 (equal "<29>--<30>"
3389 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3390 (let ((org-display-custom-times t)
3391 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3392 (org-timestamp-translate (org-element-context))))))
3393 ;; Translate date range start.
3394 (should
3395 (equal "<29>"
3396 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3397 (let ((org-display-custom-times t)
3398 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3399 (org-timestamp-translate (org-element-context) 'start)))))
3400 ;; Translate date range end.
3401 (should
3402 (equal "<30>"
3403 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3404 (let ((org-display-custom-times t)
3405 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3406 (org-timestamp-translate (org-element-context) 'end)))))
3407 ;; Translate time range.
3408 (should
3409 (equal "<08>--<16>"
3410 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
3411 (let ((org-display-custom-times t)
3412 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
3413 (org-timestamp-translate (org-element-context))))))
3414 ;; Translate non-range timestamp.
3415 (should
3416 (equal "<29>"
3417 (org-test-with-temp-text "<2012-03-29 Thu>"
3418 (let ((org-display-custom-times t)
3419 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3420 (org-timestamp-translate (org-element-context))))))
3421 ;; Do not change `diary' timestamps.
3422 (should
3423 (equal "<%%(org-float t 4 2)>"
3424 (org-test-with-temp-text "<%%(org-float t 4 2)>"
3425 (let ((org-display-custom-times t)
3426 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3427 (org-timestamp-translate (org-element-context)))))))
3431 ;;; Visibility
3433 (ert-deftest test-org/flag-drawer ()
3434 "Test `org-flag-drawer' specifications."
3435 ;; Hide drawer.
3436 (should
3437 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3438 (org-flag-drawer t)
3439 (get-char-property (line-end-position) 'invisible)))
3440 ;; Show drawer.
3441 (should-not
3442 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3443 (org-flag-drawer t)
3444 (org-flag-drawer nil)
3445 (get-char-property (line-end-position) 'invisible)))
3446 ;; Test optional argument.
3447 (should
3448 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
3449 (let ((drawer (save-excursion (search-forward ":D2")
3450 (org-element-at-point))))
3451 (org-flag-drawer t drawer)
3452 (get-char-property (progn (search-forward ":D2") (line-end-position))
3453 'invisible))))
3454 (should-not
3455 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
3456 (let ((drawer (save-excursion (search-forward ":D2")
3457 (org-element-at-point))))
3458 (org-flag-drawer t drawer)
3459 (get-char-property (line-end-position) 'invisible))))
3460 ;; Do not hide fake drawers.
3461 (should-not
3462 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
3463 (forward-line 1)
3464 (org-flag-drawer t)
3465 (get-char-property (line-end-position) 'invisible)))
3466 ;; Do not hide incomplete drawers.
3467 (should-not
3468 (org-test-with-temp-text ":D:\nparagraph"
3469 (forward-line 1)
3470 (org-flag-drawer t)
3471 (get-char-property (line-end-position) 'invisible)))
3472 ;; Do not hide drawers when called from final blank lines.
3473 (should-not
3474 (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
3475 (goto-char (point-max))
3476 (org-flag-drawer t)
3477 (goto-char (point-min))
3478 (get-char-property (line-end-position) 'invisible)))
3479 ;; Don't leave point in an invisible part of the buffer when hiding
3480 ;; a drawer away.
3481 (should-not
3482 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3483 (goto-char (point-max))
3484 (org-flag-drawer t)
3485 (get-char-property (point) 'invisible))))
3487 (ert-deftest test-org/hide-block-toggle ()
3488 "Test `org-hide-block-toggle' specifications."
3489 ;; Error when not at a block.
3490 (should-error
3491 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
3492 (org-hide-block-toggle 'off)
3493 (get-char-property (line-end-position) 'invisible)))
3494 ;; Hide block.
3495 (should
3496 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
3497 (org-hide-block-toggle)
3498 (get-char-property (line-end-position) 'invisible)))
3499 (should
3500 (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
3501 (org-hide-block-toggle)
3502 (get-char-property (line-end-position) 'invisible)))
3503 ;; Show block unconditionally when optional argument is `off'.
3504 (should-not
3505 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3506 (org-hide-block-toggle)
3507 (org-hide-block-toggle 'off)
3508 (get-char-property (line-end-position) 'invisible)))
3509 (should-not
3510 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3511 (org-hide-block-toggle 'off)
3512 (get-char-property (line-end-position) 'invisible)))
3513 ;; Hide block unconditionally when optional argument is non-nil.
3514 (should
3515 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3516 (org-hide-block-toggle t)
3517 (get-char-property (line-end-position) 'invisible)))
3518 (should
3519 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3520 (org-hide-block-toggle)
3521 (org-hide-block-toggle t)
3522 (get-char-property (line-end-position) 'invisible)))
3523 ;; Do not hide block when called from final blank lines.
3524 (should-not
3525 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
3526 (org-hide-block-toggle)
3527 (goto-char (point-min))
3528 (get-char-property (line-end-position) 'invisible)))
3529 ;; Don't leave point in an invisible part of the buffer when hiding
3530 ;; a block away.
3531 (should-not
3532 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
3533 (org-hide-block-toggle)
3534 (get-char-property (point) 'invisible))))
3536 (ert-deftest test-org/hide-block-toggle-maybe ()
3537 "Test `org-hide-block-toggle-maybe' specifications."
3538 (should
3539 (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
3540 (org-hide-block-toggle-maybe)))
3541 (should-not
3542 (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
3544 (ert-deftest test-org/show-set-visibility ()
3545 "Test `org-show-set-visibility' specifications."
3546 ;; Do not throw an error before first heading.
3547 (should
3548 (org-test-with-temp-text "Preamble\n* Headline"
3549 (org-show-set-visibility 'tree)
3551 ;; Test all visibility spans, both on headline and in entry.
3552 (let ((list-visible-lines
3553 (lambda (state headerp)
3554 (org-test-with-temp-text "* Grandmother (0)
3555 ** Uncle (1)
3556 *** Heir (2)
3557 ** Father (3)
3558 Ancestor text (4)
3559 *** Sister (5)
3560 Sibling text (6)
3561 *** Self (7)
3562 Match (8)
3563 **** First born (9)
3564 Child text (10)
3565 **** The other child (11)
3566 *** Brother (12)
3567 ** Aunt (13)
3569 (org-cycle t)
3570 (search-forward (if headerp "Self" "Match"))
3571 (org-show-set-visibility state)
3572 (goto-char (point-min))
3573 (let (result (line 0))
3574 (while (not (eobp))
3575 (unless (org-invisible-p2) (push line result))
3576 (incf line)
3577 (forward-line))
3578 (nreverse result))))))
3579 (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
3580 (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
3581 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
3582 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
3583 (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
3584 (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
3585 (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
3586 (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
3587 (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
3588 (should (equal '(0 1 3 5 7 8 9 11 12 13)
3589 (funcall list-visible-lines 'tree nil)))
3590 (should (equal '(0 1 3 4 5 7 12 13)
3591 (funcall list-visible-lines 'canonical t)))
3592 (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
3593 (funcall list-visible-lines 'canonical nil)))))
3596 (provide 'test-org)
3598 ;;; test-org.el ends here