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