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