Open outer link on nested links
[org-mode.git] / testing / lisp / test-org.el
blobcee67525ef5de652f77d29ccd0d4bcba91a560f1
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")))))
196 (ert-deftest test-org/org-parse-time-string ()
197 "Test `org-parse-time-string'."
198 (should (equal (org-parse-time-string "2012-03-29 16:40")
199 '(0 40 16 29 3 2012 nil nil nil)))
200 (should (equal (org-parse-time-string "[2012-03-29 16:40]")
201 '(0 40 16 29 3 2012 nil nil nil)))
202 (should (equal (org-parse-time-string "<2012-03-29 16:40>")
203 '(0 40 16 29 3 2012 nil nil nil)))
204 (should (equal (org-parse-time-string "<2012-03-29>")
205 '(0 0 0 29 3 2012 nil nil nil)))
206 (should (equal (org-parse-time-string "<2012-03-29>" t)
207 '(0 nil nil 29 3 2012 nil nil nil))))
210 ;;; Drawers
212 (ert-deftest test-org/insert-property-drawer ()
213 "Test `org-insert-property-drawer' specifications."
214 ;; Error before first headline.
215 (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
216 ;; Insert drawer right after headline if there is no planning line,
217 ;; or after it otherwise.
218 (should
219 (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
220 (org-test-with-temp-text "* H\nParagraph<point>"
221 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
222 (buffer-string))))
223 (should
224 (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
225 (org-test-with-temp-text
226 "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
227 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
228 (buffer-string))))
229 ;; Indent inserted drawer.
230 (should
231 (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
232 (org-test-with-temp-text "* H\nParagraph<point>"
233 (let ((org-adapt-indentation t)) (org-insert-property-drawer))
234 (buffer-string))))
235 ;; Handle insertion at eob.
236 (should
237 (equal "* H\n:PROPERTIES:\n:END:\n"
238 (org-test-with-temp-text "* H"
239 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
240 (buffer-string))))
241 ;; Skip inlinetasks before point.
242 (when (featurep 'org-inlinetask)
243 (should
244 (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
245 (org-test-with-temp-text
246 "* H\n*************** I\n*************** END\nP<point>"
247 (let ((org-adapt-indentation nil)
248 (org-inlinetask-min-level 15))
249 (org-insert-property-drawer))
250 (buffer-string)))))
251 ;; Correctly set drawer in an inlinetask.
252 (when (featurep 'org-inlinetask)
253 (should
254 (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
255 (org-test-with-temp-text
256 "* H\n*************** I\nP<point>\n*************** END"
257 (let ((org-adapt-indentation nil)
258 (org-inlinetask-min-level 15))
259 (org-insert-property-drawer))
260 (buffer-string))))))
263 ;;; Filling
265 (ert-deftest test-org/fill-paragraph ()
266 "Test `org-fill-paragraph' specifications."
267 ;; At an Org table, align it.
268 (should
269 (equal "| a |\n"
270 (org-test-with-temp-text "|a|"
271 (org-fill-paragraph)
272 (buffer-string))))
273 (should
274 (equal "#+name: table\n| a |\n"
275 (org-test-with-temp-text "#+name: table\n| a |"
276 (org-fill-paragraph)
277 (buffer-string))))
278 ;; At a paragraph, preserve line breaks.
279 (org-test-with-temp-text "some \\\\\nlong\ntext"
280 (let ((fill-column 20))
281 (org-fill-paragraph)
282 (should (equal (buffer-string) "some \\\\\nlong text"))))
283 ;; Correctly fill a paragraph when point is at its very end.
284 (should
285 (equal "A B"
286 (org-test-with-temp-text "A\nB"
287 (let ((fill-column 20))
288 (goto-char (point-max))
289 (org-fill-paragraph)
290 (buffer-string)))))
291 ;; Correctly fill the last paragraph of a greater element.
292 (should
293 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
294 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
295 (let ((fill-column 8))
296 (forward-line)
297 (end-of-line)
298 (org-fill-paragraph)
299 (buffer-string)))))
300 ;; Correctly fill an element in a narrowed buffer.
301 (should
302 (equal "01234\n6"
303 (org-test-with-temp-text "01234 6789"
304 (let ((fill-column 5))
305 (narrow-to-region 1 8)
306 (org-fill-paragraph)
307 (buffer-string)))))
308 ;; Handle `adaptive-fill-regexp' in paragraphs.
309 (should
310 (equal "> a b"
311 (org-test-with-temp-text "> a\n> b"
312 (let ((fill-column 5)
313 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
314 (org-fill-paragraph)
315 (buffer-string)))))
316 ;; Special case: Fill first paragraph when point is at an item or
317 ;; a plain-list or a footnote reference.
318 (should
319 (equal "- A B"
320 (org-test-with-temp-text "- A\n B"
321 (let ((fill-column 20))
322 (org-fill-paragraph)
323 (buffer-string)))))
324 (should
325 (equal "[fn:1] A B"
326 (org-test-with-temp-text "[fn:1] A\nB"
327 (let ((fill-column 20))
328 (org-fill-paragraph)
329 (buffer-string)))))
330 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
331 (let ((fill-column 20))
332 (org-fill-paragraph)
333 (should (equal (buffer-string)
334 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
335 ;; Fill contents of `comment-block' elements.
336 (should
337 (equal
338 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
339 (let ((fill-column 20))
340 (forward-line)
341 (org-fill-paragraph)
342 (buffer-string)))
343 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
344 ;; Fill `comment' elements.
345 (should
346 (equal " # A B"
347 (org-test-with-temp-text " # A\n # B"
348 (let ((fill-column 20))
349 (org-fill-paragraph)
350 (buffer-string)))))
351 ;; Do not mix consecutive comments when filling one of them.
352 (should
353 (equal "# A B\n\n# C"
354 (org-test-with-temp-text "# A\n# B\n\n# C"
355 (let ((fill-column 20))
356 (org-fill-paragraph)
357 (buffer-string)))))
358 ;; Use commented empty lines as separators when filling comments.
359 (should
360 (equal "# A B\n#\n# C"
361 (org-test-with-temp-text "# A\n# B\n#\n# C"
362 (let ((fill-column 20))
363 (org-fill-paragraph)
364 (buffer-string)))))
365 ;; Handle `adaptive-fill-regexp' in comments.
366 (should
367 (equal "# > a b"
368 (org-test-with-temp-text "# > a\n# > b"
369 (let ((fill-column 20)
370 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
371 (org-fill-paragraph)
372 (buffer-string)))))
373 ;; Do nothing at affiliated keywords.
374 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
375 (let ((fill-column 20))
376 (org-fill-paragraph)
377 (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
378 ;; Do not move point after table when filling a table.
379 (should-not
380 (org-test-with-temp-text "| a | b |\n| c | d |\n"
381 (forward-char)
382 (org-fill-paragraph)
383 (eobp))))
385 (ert-deftest test-org/auto-fill-function ()
386 "Test auto-filling features."
387 ;; Auto fill paragraph.
388 (should
389 (equal "12345\n7890"
390 (org-test-with-temp-text "12345 7890"
391 (let ((fill-column 5))
392 (end-of-line)
393 (org-auto-fill-function)
394 (buffer-string)))))
395 ;; Auto fill first paragraph in an item.
396 (should
397 (equal "- 12345\n 7890"
398 (org-test-with-temp-text "- 12345 7890"
399 (let ((fill-column 7))
400 (end-of-line)
401 (org-auto-fill-function)
402 (buffer-string)))))
403 ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
404 (should
405 (equal "> 12345\n 7890"
406 (org-test-with-temp-text "> 12345 7890"
407 (let ((fill-column 10)
408 (adaptive-fill-regexp "[ \t]*>+[ \t]*")
409 (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
410 (end-of-line)
411 (org-auto-fill-function)
412 (buffer-string)))))
413 (should
414 (equal "> 12345\n> 12345\n> 7890"
415 (org-test-with-temp-text "> 12345\n> 12345 7890"
416 (let ((fill-column 10)
417 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
418 (goto-char (point-max))
419 (org-auto-fill-function)
420 (buffer-string)))))
421 (should-not
422 (equal " 12345\n *12345\n *12345"
423 (org-test-with-temp-text " 12345\n *12345 12345"
424 (let ((fill-column 10)
425 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
426 (goto-char (point-max))
427 (org-auto-fill-function)
428 (buffer-string)))))
429 ;; Auto fill comments.
430 (should
431 (equal " # 12345\n # 7890"
432 (org-test-with-temp-text " # 12345 7890"
433 (let ((fill-column 10))
434 (end-of-line)
435 (org-auto-fill-function)
436 (buffer-string)))))
437 ;; A hash within a line isn't a comment.
438 (should-not
439 (equal "12345 # 7890\n# 1"
440 (org-test-with-temp-text "12345 # 7890 1"
441 (let ((fill-column 12))
442 (end-of-line)
443 (org-auto-fill-function)
444 (buffer-string)))))
445 ;; Correctly interpret empty prefix.
446 (should-not
447 (equal "# a\n# b\nRegular\n# paragraph"
448 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
449 (let ((fill-column 12))
450 (end-of-line 3)
451 (org-auto-fill-function)
452 (buffer-string)))))
453 ;; Comment block: auto fill contents.
454 (should
455 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
456 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
457 (let ((fill-column 5))
458 (forward-line)
459 (end-of-line)
460 (org-auto-fill-function)
461 (buffer-string)))))
462 (should
463 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
464 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
465 (let ((fill-column 5))
466 (forward-line)
467 (end-of-line)
468 (org-auto-fill-function)
469 (buffer-string)))))
470 ;; Do not fill if a new item could be created.
471 (should-not
472 (equal "12345\n- 90"
473 (org-test-with-temp-text "12345 - 90"
474 (let ((fill-column 5))
475 (end-of-line)
476 (org-auto-fill-function)
477 (buffer-string)))))
478 ;; Do not fill if a line break could be introduced.
479 (should-not
480 (equal "123\\\\\n7890"
481 (org-test-with-temp-text "123\\\\ 7890"
482 (let ((fill-column 6))
483 (end-of-line)
484 (org-auto-fill-function)
485 (buffer-string)))))
486 ;; Do not fill affiliated keywords.
487 (should-not
488 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
489 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
490 (let ((fill-column 20))
491 (end-of-line)
492 (org-auto-fill-function)
493 (buffer-string))))))
497 ;;; Indentation
499 (ert-deftest test-org/indent-line ()
500 "Test `org-indent-line' specifications."
501 ;; Do not indent footnote definitions or headlines.
502 (should
503 (zerop
504 (org-test-with-temp-text "* H"
505 (org-indent-line)
506 (org-get-indentation))))
507 (should
508 (zerop
509 (org-test-with-temp-text "[fn:1] fn"
510 (let ((org-adapt-indentation t)) (org-indent-line))
511 (org-get-indentation))))
512 ;; Do not indent before first headline.
513 (should
514 (zerop
515 (org-test-with-temp-text ""
516 (org-indent-line)
517 (org-get-indentation))))
518 ;; Indent according to headline level otherwise, unless
519 ;; `org-adapt-indentation' is nil.
520 (should
521 (= 2
522 (org-test-with-temp-text "* H\nA"
523 (forward-line)
524 (let ((org-adapt-indentation t)) (org-indent-line))
525 (org-get-indentation))))
526 (should
527 (= 2
528 (org-test-with-temp-text "* H\n\nA"
529 (forward-line)
530 (let ((org-adapt-indentation t)) (org-indent-line))
531 (org-get-indentation))))
532 (should
533 (zerop
534 (org-test-with-temp-text "* H\nA"
535 (forward-line)
536 (let ((org-adapt-indentation nil)) (org-indent-line))
537 (org-get-indentation))))
538 ;; Indenting preserves point position.
539 (should
540 (org-test-with-temp-text "* H\nAB"
541 (forward-line)
542 (forward-char)
543 (let ((org-adapt-indentation t)) (org-indent-line))
544 (looking-at "B")))
545 ;; Do not change indentation at an item.
546 (should
547 (= 1
548 (org-test-with-temp-text "* H\n - A"
549 (forward-line)
550 (let ((org-adapt-indentation t)) (org-indent-line))
551 (org-get-indentation))))
552 ;; On blank lines at the end of a list, indent like last element
553 ;; within it if the line is still in the list. Otherwise, indent
554 ;; like the whole list.
555 (should
556 (= 4
557 (org-test-with-temp-text "* H\n- A\n - AA\n"
558 (goto-char (point-max))
559 (let ((org-adapt-indentation t)) (org-indent-line))
560 (org-get-indentation))))
561 (should
562 (zerop
563 (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
564 (goto-char (point-max))
565 (let ((org-adapt-indentation t)) (org-indent-line))
566 (org-get-indentation))))
567 ;; Likewise, on a blank line at the end of a footnote definition,
568 ;; indent at column 0 if line belongs to the definition. Otherwise,
569 ;; indent like the definition itself.
570 (should
571 (zerop
572 (org-test-with-temp-text "* H\n[fn:1] Definition\n"
573 (goto-char (point-max))
574 (let ((org-adapt-indentation t)) (org-indent-line))
575 (org-get-indentation))))
576 (should
577 (zerop
578 (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
579 (goto-char (point-max))
580 (let ((org-adapt-indentation t)) (org-indent-line))
581 (org-get-indentation))))
582 ;; After the end of the contents of a greater element, indent like
583 ;; the beginning of the element.
584 (should
585 (= 1
586 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
587 (forward-line 2)
588 (org-indent-line)
589 (org-get-indentation))))
590 ;; On blank lines after a paragraph, indent like its last non-empty
591 ;; line.
592 (should
593 (= 1
594 (org-test-with-temp-text " Paragraph\n\n<point>"
595 (org-indent-line)
596 (org-get-indentation))))
597 ;; At the first line of an element, indent like previous element's
598 ;; first line, ignoring footnotes definitions and inline tasks, or
599 ;; according to parent.
600 (should
601 (= 2
602 (org-test-with-temp-text "A\n\n B\n\nC"
603 (goto-char (point-max))
604 (org-indent-line)
605 (org-get-indentation))))
606 (should
607 (= 1
608 (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
609 (goto-char (point-max))
610 (org-indent-line)
611 (org-get-indentation))))
612 (should
613 (= 1
614 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
615 (forward-line 1)
616 (org-indent-line)
617 (org-get-indentation))))
618 ;; Within code part of a source block, use language major mode if
619 ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
620 ;; according to line above.
621 (should
622 (= 6
623 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
624 (forward-line 2)
625 (let ((org-src-tab-acts-natively t)
626 (org-edit-src-content-indentation 0))
627 (org-indent-line))
628 (org-get-indentation))))
629 (should
630 (= 1
631 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
632 (forward-line 2)
633 (let ((org-src-tab-acts-natively nil)
634 (org-edit-src-content-indentation 0))
635 (org-indent-line))
636 (org-get-indentation))))
637 ;; Otherwise, indent like the first non-blank line above.
638 (should
639 (zerop
640 (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
641 (forward-line 3)
642 (org-indent-line)
643 (org-get-indentation))))
644 ;; Align node properties according to `org-property-format'. Handle
645 ;; nicely empty values.
646 (should
647 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
648 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
649 (let ((org-property-format "%-10s %s")) (org-indent-line))
650 (buffer-string))))
651 (should
652 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
653 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
654 (let ((org-property-format "%-10s %s")) (org-indent-line))
655 (buffer-string)))))
657 (ert-deftest test-org/indent-region ()
658 "Test `org-indent-region' specifications."
659 ;; Indent paragraph.
660 (should
661 (equal "A\nB\nC"
662 (org-test-with-temp-text " A\nB\n C"
663 (org-indent-region (point-min) (point-max))
664 (buffer-string))))
665 ;; Indent greater elements along with their contents.
666 (should
667 (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
668 (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
669 (org-indent-region (point-min) (point-max))
670 (buffer-string))))
671 ;; Ignore contents of verse blocks and example blocks.
672 (should
673 (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
674 (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
675 (org-indent-region (point-min) (point-max))
676 (buffer-string))))
677 (should
678 (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
679 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
680 (org-indent-region (point-min) (point-max))
681 (buffer-string))))
682 ;; Indent according to mode if `org-src-tab-acts-natively' is
683 ;; non-nil. Otherwise, do not indent code at all.
684 (should
685 (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
686 (org-test-with-temp-text
687 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
688 (let ((org-src-tab-acts-natively t)
689 (org-edit-src-content-indentation 0))
690 (org-indent-region (point-min) (point-max)))
691 (buffer-string))))
692 (should
693 (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
694 (org-test-with-temp-text
695 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
696 (let ((org-src-tab-acts-natively nil)
697 (org-edit-src-content-indentation 0))
698 (org-indent-region (point-min) (point-max)))
699 (buffer-string))))
700 ;; Align node properties according to `org-property-format'. Handle
701 ;; nicely empty values.
702 (should
703 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
704 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
705 (let ((org-property-format "%-10s %s")
706 (org-adapt-indentation nil))
707 (org-indent-region (point) (point-max)))
708 (buffer-string))))
709 (should
710 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
711 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
712 (let ((org-property-format "%-10s %s")
713 (org-adapt-indentation nil))
714 (org-indent-region (point) (point-max)))
715 (buffer-string))))
716 ;; Indent plain lists.
717 (should
718 (equal "- A\n B\n - C\n\n D"
719 (org-test-with-temp-text "- A\n B\n - C\n\n D"
720 (org-indent-region (point-min) (point-max))
721 (buffer-string))))
722 (should
723 (equal "- A\n\n- B"
724 (org-test-with-temp-text " - A\n\n - B"
725 (org-indent-region (point-min) (point-max))
726 (buffer-string))))
727 ;; Indent footnote definitions.
728 (should
729 (equal "[fn:1] Definition\n\nDefinition"
730 (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
731 (org-indent-region (point-min) (point-max))
732 (buffer-string))))
733 ;; Special case: Start indenting on a blank line.
734 (should
735 (equal "\nParagraph"
736 (org-test-with-temp-text "\n Paragraph"
737 (org-indent-region (point-min) (point-max))
738 (buffer-string)))))
742 ;;; Editing
744 (ert-deftest test-org/return ()
745 "Test RET (`org-return') specifications."
746 ;; Regular test.
747 (should
748 (equal "Para\ngraph"
749 (org-test-with-temp-text "Para<point>graph"
750 (org-return)
751 (buffer-string))))
752 ;; With optional argument, indent line.
753 (should
754 (equal " Para\n graph"
755 (org-test-with-temp-text " Para<point>graph"
756 (org-return t)
757 (buffer-string))))
758 ;; On a table, call `org-table-next-row'.
759 (should
760 (org-test-with-temp-text "| <point>a |\n| b |"
761 (org-return)
762 (org-looking-at-p "b")))
763 ;; Open link or timestamp under point when `org-return-follows-link'
764 ;; is non-nil.
765 (should
766 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
767 (let ((org-return-follows-link t)) (org-return))
768 (org-looking-at-p "<<target>>")))
769 (should-not
770 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
771 (let ((org-return-follows-link nil)) (org-return))
772 (org-looking-at-p "<<target>>")))
773 ;; However, do not open link when point is in a table.
774 (should
775 (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
776 (let ((org-return-follows-link t)) (org-return))
777 (org-looking-at-p "between")))
778 ;; Special case: in a list, when indenting, do not break structure.
779 (should
780 (equal "- A\n B"
781 (org-test-with-temp-text "- A <point>B"
782 (org-return t)
783 (buffer-string))))
784 ;; Special case: on tags part of a headline, add a newline below it
785 ;; instead of breaking it.
786 (should
787 (equal "* H :tag:\n"
788 (org-test-with-temp-text "* H :<point>tag:"
789 (org-return)
790 (buffer-string)))))
792 (ert-deftest test-org/meta-return ()
793 "Test M-RET (`org-meta-return') specifications."
794 ;; In a table field insert a row above.
795 (should
796 (org-test-with-temp-text "| a |"
797 (forward-char)
798 (org-meta-return)
799 (forward-line -1)
800 (looking-at "| |$")))
801 ;; In a paragraph change current line into a header.
802 (should
803 (org-test-with-temp-text "a"
804 (org-meta-return)
805 (beginning-of-line)
806 (looking-at "\* a$")))
807 ;; In an item insert an item, in this case above.
808 (should
809 (org-test-with-temp-text "- a"
810 (org-meta-return)
811 (beginning-of-line)
812 (looking-at "- $")))
813 ;; In a drawer and item insert an item, in this case above.
814 (should
815 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
816 (forward-line)
817 (org-meta-return)
818 (beginning-of-line)
819 (looking-at "- $"))))
821 (ert-deftest test-org/insert-heading ()
822 "Test `org-insert-heading' specifications."
823 ;; FIXME: Test coverage is incomplete yet.
825 ;; In an empty buffer, insert a new headline.
826 (should
827 (equal "* "
828 (org-test-with-temp-text ""
829 (org-insert-heading)
830 (buffer-string))))
831 ;; At the beginning of a line, turn it into a headline
832 (should
833 (equal "* P"
834 (org-test-with-temp-text "<point>P"
835 (org-insert-heading)
836 (buffer-string))))
837 ;; In the middle of a line, split the line if allowed, otherwise,
838 ;; insert the headline at its end.
839 (should
840 (equal "Para\n* graph"
841 (org-test-with-temp-text "Para<point>graph"
842 (let ((org-M-RET-may-split-line '((default . t))))
843 (org-insert-heading))
844 (buffer-string))))
845 (should
846 (equal "Paragraph\n* "
847 (org-test-with-temp-text "Para<point>graph"
848 (let ((org-M-RET-may-split-line '((default . nil))))
849 (org-insert-heading))
850 (buffer-string))))
851 ;; When on a list, insert an item instead, unless called with an
852 ;; universal argument or if list is invisible. In this case, create
853 ;; a new headline after contents.
854 (should
855 (equal "* H\n- item\n- "
856 (org-test-with-temp-text "* H\n- item<point>"
857 (let ((org-insert-heading-respect-content nil))
858 (org-insert-heading))
859 (buffer-string))))
860 (should
861 (equal "* H\n- item\n- item 2\n* "
862 (org-test-with-temp-text "* H\n- item<point>\n- item 2"
863 (let ((org-insert-heading-respect-content nil))
864 (org-insert-heading '(4)))
865 (buffer-string))))
866 (should
867 (equal "* H\n- item\n* "
868 (org-test-with-temp-text "* H\n- item"
869 (org-cycle)
870 (goto-char (point-max))
871 (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
872 (buffer-string))))
873 ;; When called with two universal arguments, insert a new headline
874 ;; at the end of the grandparent subtree.
875 (should
876 (equal "* H1\n** H3\n- item\n** H2\n** "
877 (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
878 (let ((org-insert-heading-respect-content nil))
879 (org-insert-heading '(16)))
880 (buffer-string))))
881 ;; Corner case: correctly insert a headline after an empty one.
882 (should
883 (equal "* \n* "
884 (org-test-with-temp-text "* <point>"
885 (org-insert-heading)
886 (buffer-string)))))
888 (ert-deftest test-org/insert-todo-heading-respect-content ()
889 "Test `org-insert-todo-heading-respect-content' specifications."
890 ;; Create a TODO heading.
891 (should
892 (org-test-with-temp-text "* H1\n Body"
893 (org-insert-todo-heading-respect-content)
894 (nth 2 (org-heading-components))))
895 ;; Add headline at the end of the first subtree
896 (should
897 (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
898 (search-forward "H1Body")
899 (org-insert-todo-heading-respect-content)
900 (and (eobp) (org-at-heading-p))))
901 ;; In a list, do not create a new item.
902 (should
903 (org-test-with-temp-text "* H\n- an item\n- another one"
904 (search-forward "an ")
905 (org-insert-todo-heading-respect-content)
906 (and (eobp) (org-at-heading-p)))))
910 ;;; Fixed-Width Areas
912 (ert-deftest test-org/toggle-fixed-width ()
913 "Test `org-toggle-fixed-width' specifications."
914 ;; No region: Toggle on fixed-width marker in paragraphs.
915 (should
916 (equal ": A"
917 (org-test-with-temp-text "A"
918 (org-toggle-fixed-width)
919 (buffer-string))))
920 ;; No region: Toggle off fixed-width markers in fixed-width areas.
921 (should
922 (equal "A"
923 (org-test-with-temp-text ": A"
924 (org-toggle-fixed-width)
925 (buffer-string))))
926 ;; No region: Toggle on marker in blank lines after elements or just
927 ;; after a headline.
928 (should
929 (equal "* H\n: "
930 (org-test-with-temp-text "* H\n"
931 (forward-line)
932 (org-toggle-fixed-width)
933 (buffer-string))))
934 (should
935 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
936 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
937 (goto-char (point-max))
938 (org-toggle-fixed-width)
939 (buffer-string))))
940 ;; No region: Toggle on marker in front of one line elements (e.g.,
941 ;; headlines, clocks)
942 (should
943 (equal ": * Headline"
944 (org-test-with-temp-text "* Headline"
945 (org-toggle-fixed-width)
946 (buffer-string))))
947 (should
948 (equal ": #+KEYWORD: value"
949 (org-test-with-temp-text "#+KEYWORD: value"
950 (org-toggle-fixed-width)
951 (buffer-string))))
952 ;; No region: error in other situations.
953 (should-error
954 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
955 (forward-line)
956 (org-toggle-fixed-width)
957 (buffer-string)))
958 ;; No region: Indentation is preserved.
959 (should
960 (equal "- A\n : B"
961 (org-test-with-temp-text "- A\n B"
962 (forward-line)
963 (org-toggle-fixed-width)
964 (buffer-string))))
965 ;; Region: If it contains only fixed-width elements and blank lines,
966 ;; toggle off fixed-width markup.
967 (should
968 (equal "A\n\nB"
969 (org-test-with-temp-text ": A\n\n: B"
970 (transient-mark-mode 1)
971 (push-mark (point) t t)
972 (goto-char (point-max))
973 (org-toggle-fixed-width)
974 (buffer-string))))
975 ;; Region: If it contains anything else, toggle on fixed-width but
976 ;; not on fixed-width areas.
977 (should
978 (equal ": A\n: \n: B\n: \n: C"
979 (org-test-with-temp-text "A\n\n: B\n\nC"
980 (transient-mark-mode 1)
981 (push-mark (point) t t)
982 (goto-char (point-max))
983 (org-toggle-fixed-width)
984 (buffer-string))))
985 ;; Region: Ignore blank lines at its end, unless it contains only
986 ;; such lines.
987 (should
988 (equal ": A\n\n"
989 (org-test-with-temp-text "A\n\n"
990 (transient-mark-mode 1)
991 (push-mark (point) t t)
992 (goto-char (point-max))
993 (org-toggle-fixed-width)
994 (buffer-string))))
995 (should
996 (equal ": \n: \n"
997 (org-test-with-temp-text "\n\n"
998 (transient-mark-mode 1)
999 (push-mark (point) t t)
1000 (goto-char (point-max))
1001 (org-toggle-fixed-width)
1002 (buffer-string)))))
1006 ;;; Headline
1008 (ert-deftest test-org/in-commented-heading-p ()
1009 "Test `org-in-commented-heading-p' specifications."
1010 ;; Commented headline.
1011 (should
1012 (org-test-with-temp-text "* COMMENT Headline\nBody"
1013 (goto-char (point-max))
1014 (org-in-commented-heading-p)))
1015 ;; Commented ancestor.
1016 (should
1017 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1018 (goto-char (point-max))
1019 (org-in-commented-heading-p)))
1020 ;; Comment keyword is case-sensitive.
1021 (should-not
1022 (org-test-with-temp-text "* Comment Headline\nBody"
1023 (goto-char (point-max))
1024 (org-in-commented-heading-p)))
1025 ;; Keyword is standalone.
1026 (should-not
1027 (org-test-with-temp-text "* COMMENTHeadline\nBody"
1028 (goto-char (point-max))
1029 (org-in-commented-heading-p)))
1030 ;; Optional argument.
1031 (should-not
1032 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1033 (goto-char (point-max))
1034 (org-in-commented-heading-p t))))
1038 ;;; Keywords
1040 (ert-deftest test-org/set-regexps-and-options ()
1041 "Test `org-set-regexps-and-options' specifications."
1042 ;; TAGS keyword.
1043 (should
1044 (equal '(("A" . ?a) ("B") ("C"))
1045 (org-test-with-temp-text "#+TAGS: A(a) B C"
1046 (org-mode-restart)
1047 org-tag-alist)))
1048 (should
1049 (equal '(("A") (:newline) ("B"))
1050 (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
1051 (org-mode-restart)
1052 org-tag-alist)))
1053 (should
1054 (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
1055 (org-test-with-temp-text "#+TAGS: { A B } C"
1056 (org-mode-restart)
1057 org-tag-alist)))
1058 (should
1059 (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
1060 (org-test-with-temp-text "#+TAGS: { A : B C }"
1061 (org-mode-restart)
1062 org-tag-alist)))
1063 (should
1064 (equal '(("A" "B" "C"))
1065 (org-test-with-temp-text "#+TAGS: { A : B C }"
1066 (org-mode-restart)
1067 org-tag-groups-alist)))
1068 ;; FILETAGS keyword.
1069 (should
1070 (equal '("A" "B" "C")
1071 (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
1072 (org-mode-restart)
1073 org-file-tags)))
1074 ;; PROPERTY keyword. Property names are case-insensitive.
1075 (should
1076 (equal "foo=1"
1077 (org-test-with-temp-text "#+PROPERTY: var foo=1"
1078 (org-mode-restart)
1079 (cdr (assoc "var" org-file-properties)))))
1080 (should
1081 (equal
1082 "foo=1 bar=2"
1083 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
1084 (org-mode-restart)
1085 (cdr (assoc "var" org-file-properties)))))
1086 (should
1087 (equal
1088 "foo=1 bar=2"
1089 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
1090 (org-mode-restart)
1091 (cdr (assoc "var" org-file-properties)))))
1092 ;; ARCHIVE keyword.
1093 (should
1094 (equal "%s_done::"
1095 (org-test-with-temp-text "#+ARCHIVE: %s_done::"
1096 (org-mode-restart)
1097 org-archive-location)))
1098 ;; CATEGORY keyword.
1099 (should
1100 (eq 'test
1101 (org-test-with-temp-text "#+CATEGORY: test"
1102 (org-mode-restart)
1103 org-category)))
1104 (should
1105 (equal "test"
1106 (org-test-with-temp-text "#+CATEGORY: test"
1107 (org-mode-restart)
1108 (cdr (assoc "CATEGORY" org-file-properties)))))
1109 ;; COLUMNS keyword.
1110 (should
1111 (equal "%25ITEM %TAGS %PRIORITY %TODO"
1112 (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
1113 (org-mode-restart)
1114 org-columns-default-format)))
1115 ;; CONSTANTS keyword. Constants names are case sensitive.
1116 (should
1117 (equal '("299792458." "3.14")
1118 (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
1119 (org-mode-restart)
1120 (mapcar
1121 (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
1122 '("c" "pi")))))
1123 (should
1124 (equal "3.14"
1125 (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
1126 (org-mode-restart)
1127 (cdr (assoc "pi" org-table-formula-constants-local)))))
1128 (should
1129 (equal "22/7"
1130 (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
1131 (org-mode-restart)
1132 (cdr (assoc "PI" org-table-formula-constants-local)))))
1133 ;; LINK keyword.
1134 (should
1135 (equal
1136 '("url1" "url2")
1137 (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
1138 (org-mode-restart)
1139 (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
1140 '("a" "b")))))
1141 ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
1142 (should
1143 (equal
1144 '(?X ?Z ?Y)
1145 (org-test-with-temp-text "#+PRIORITIES: X Z Y"
1146 (org-mode-restart)
1147 (list org-highest-priority org-lowest-priority org-default-priority))))
1148 (should
1149 (equal
1150 '(?A ?C ?B)
1151 (org-test-with-temp-text "#+PRIORITIES: X Z"
1152 (org-mode-restart)
1153 (list org-highest-priority org-lowest-priority org-default-priority))))
1154 ;; STARTUP keyword.
1155 (should
1156 (equal '(t t)
1157 (org-test-with-temp-text "#+STARTUP: fold odd"
1158 (org-mode-restart)
1159 (list org-startup-folded org-odd-levels-only))))
1160 ;; TODO keywords.
1161 (should
1162 (equal '(("A" "B") ("C"))
1163 (org-test-with-temp-text "#+TODO: A B | C"
1164 (org-mode-restart)
1165 (list org-not-done-keywords org-done-keywords))))
1166 (should
1167 (equal '(("A" "C") ("B" "D"))
1168 (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
1169 (org-mode-restart)
1170 (list org-not-done-keywords org-done-keywords))))
1171 (should
1172 (equal '(("A" "B") ("C"))
1173 (org-test-with-temp-text "#+TYP_TODO: A B | C"
1174 (org-mode-restart)
1175 (list org-not-done-keywords org-done-keywords))))
1176 (should
1177 (equal '((:startgroup) ("A" . ?a) (:endgroup))
1178 (org-test-with-temp-text "#+TODO: A(a)"
1179 (org-mode-restart)
1180 org-todo-key-alist)))
1181 (should
1182 (equal '(("D" note nil) ("C" time nil) ("B" note time))
1183 (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
1184 (org-mode-restart)
1185 org-todo-log-states)))
1186 ;; Enter SETUPFILE keyword.
1187 (should
1188 (equal "1"
1189 (org-test-with-temp-text
1190 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
1191 (org-mode-restart)
1192 (cdr (assoc "a" org-file-properties))))))
1196 ;;; Links
1198 ;;;; Coderefs
1200 (ert-deftest test-org/coderef ()
1201 "Test coderef links specifications."
1202 (should
1203 (org-test-with-temp-text "
1204 #+BEGIN_SRC emacs-lisp
1205 \(+ 1 1) (ref:sc)
1206 #+END_SRC
1207 \[[(sc)]]"
1208 (goto-char (point-max))
1209 (org-open-at-point)
1210 (looking-at "(ref:sc)"))))
1212 ;;;; Custom ID
1214 (ert-deftest test-org/custom-id ()
1215 "Test custom ID links specifications."
1216 (should
1217 (org-test-with-temp-text
1218 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
1219 (goto-char (point-max))
1220 (org-open-at-point)
1221 (org-looking-at-p "\\* H1"))))
1223 ;;;; Fuzzy Links
1225 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
1226 ;; a named element (#+name: text) and to headlines (* Text).
1228 (ert-deftest test-org/fuzzy-links ()
1229 "Test fuzzy links specifications."
1230 ;; Fuzzy link goes in priority to a matching target.
1231 (should
1232 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
1233 (goto-line 5)
1234 (org-open-at-point)
1235 (looking-at "<<Test>>")))
1236 ;; Then fuzzy link points to an element with a given name.
1237 (should
1238 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
1239 (goto-line 5)
1240 (org-open-at-point)
1241 (looking-at "#\\+NAME: Test")))
1242 ;; A target still lead to a matching headline otherwise.
1243 (should
1244 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
1245 (goto-line 4)
1246 (org-open-at-point)
1247 (looking-at "\\* Head2")))
1248 ;; With a leading star in link, enforce heading match.
1249 (should
1250 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
1251 (goto-line 3)
1252 (org-open-at-point)
1253 (looking-at "\\* Test")))
1254 ;; Correctly un-hexify fuzzy links.
1255 (should
1256 (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
1257 (goto-char (point-max))
1258 (org-open-at-point)
1259 (bobp))))
1261 ;;;; Link Escaping
1263 (ert-deftest test-org/org-link-escape-ascii-character ()
1264 "Escape an ascii character."
1265 (should
1266 (string=
1267 "%5B"
1268 (org-link-escape "["))))
1270 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
1271 "Escape an ascii control character."
1272 (should
1273 (string=
1274 "%09"
1275 (org-link-escape "\t"))))
1277 (ert-deftest test-org/org-link-escape-multibyte-character ()
1278 "Escape an unicode multibyte character."
1279 (should
1280 (string=
1281 "%E2%82%AC"
1282 (org-link-escape "€"))))
1284 (ert-deftest test-org/org-link-escape-custom-table ()
1285 "Escape string with custom character table."
1286 (should
1287 (string=
1288 "Foo%3A%42ar%0A"
1289 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
1291 (ert-deftest test-org/org-link-escape-custom-table-merge ()
1292 "Escape string with custom table merged with default table."
1293 (should
1294 (string=
1295 "%5BF%6F%6F%3A%42ar%0A%5D"
1296 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
1298 (ert-deftest test-org/org-link-unescape-ascii-character ()
1299 "Unescape an ascii character."
1300 (should
1301 (string=
1303 (org-link-unescape "%5B"))))
1305 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
1306 "Unescpae an ascii control character."
1307 (should
1308 (string=
1309 "\n"
1310 (org-link-unescape "%0A"))))
1312 (ert-deftest test-org/org-link-unescape-multibyte-character ()
1313 "Unescape unicode multibyte character."
1314 (should
1315 (string=
1316 "€"
1317 (org-link-unescape "%E2%82%AC"))))
1319 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
1320 "Unescape old style percent escaped character."
1321 (should
1322 (string=
1323 "àâçèéêîôùû"
1324 (decode-coding-string
1325 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
1327 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
1328 "Escape and unescape a URL that includes an escaped char.
1329 http://article.gmane.org/gmane.emacs.orgmode/21459/"
1330 (should
1331 (string=
1332 "http://some.host.com/form?&id=blah%2Bblah25"
1333 (org-link-unescape
1334 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
1336 (ert-deftest test-org/org-link-escape-chars-browser ()
1337 "Test of the constant `org-link-escape-chars-browser'.
1338 See there why this test is a candidate to be removed once Org
1339 drops support for Emacs 24.1 and 24.2."
1340 (should
1341 (string=
1342 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1343 "%22Release%208.2%22&idxname=emacs-orgmode")
1344 (org-link-escape-browser ; Do not replace with `url-encode-url',
1345 ; see docstring above.
1346 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1347 "\"Release 8.2\"&idxname=emacs-orgmode")))))
1349 ;;;; Open at point
1351 (ert-deftest test-org/open-at-point-in-property ()
1352 "Does `org-open-at-point' open link in property drawer?"
1353 (should
1354 (org-test-with-temp-text
1355 "* Headline
1356 :PROPERTIES:
1357 :URL: <point>[[info:emacs#Top]]
1358 :END:"
1359 (org-open-at-point) t)))
1361 (ert-deftest test-org/open-at-point-in-comment ()
1362 "Does `org-open-at-point' open link in a commented line?"
1363 (should
1364 (org-test-with-temp-text
1365 "# <point>[[info:emacs#Top]]"
1366 (org-open-at-point) t)))
1368 (ert-deftest test-org/open-at-point/info ()
1369 "Test `org-open-at-point' on info links."
1370 (should
1371 (org-test-with-temp-text
1372 "<point>[[info:emacs#Top]]"
1373 (org-open-at-point)
1374 (and (switch-to-buffer "*info*")
1375 (prog1
1376 (looking-at "\nThe Emacs Editor")
1377 (kill-buffer))))))
1379 (ert-deftest test-org/open-at-point/inline-image ()
1380 "Test `org-open-at-point' on nested links."
1381 (should
1382 (org-test-with-temp-text "[[info:org#Top][info:<point>emacs#Top]]"
1383 (org-open-at-point)
1384 (prog1 (with-current-buffer "*info*" (looking-at "\nOrg Mode Manual"))
1385 (kill-buffer "*info*")))))
1388 ;;; Node Properties
1390 (ert-deftest test-org/accumulated-properties-in-drawers ()
1391 "Ensure properties accumulate in subtree drawers."
1392 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
1393 (org-babel-next-src-block)
1394 (should (equal '(2 1) (org-babel-execute-src-block)))))
1396 (ert-deftest test-org/custom-properties ()
1397 "Test custom properties specifications."
1398 ;; Standard test.
1399 (should
1400 (let ((org-custom-properties '("FOO")))
1401 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1402 (org-toggle-custom-properties-visibility)
1403 (org-invisible-p2))))
1404 ;; Properties are case-insensitive.
1405 (should
1406 (let ((org-custom-properties '("FOO")))
1407 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
1408 (org-toggle-custom-properties-visibility)
1409 (org-invisible-p2))))
1410 (should
1411 (let ((org-custom-properties '("foo")))
1412 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1413 (org-toggle-custom-properties-visibility)
1414 (org-invisible-p2))))
1415 ;; Multiple custom properties in the same drawer.
1416 (should
1417 (let ((org-custom-properties '("FOO" "BAR")))
1418 (org-test-with-temp-text
1419 "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
1420 (org-toggle-custom-properties-visibility)
1421 (and (org-invisible-p2)
1422 (not (progn (forward-line) (org-invisible-p2)))
1423 (progn (forward-line) (org-invisible-p2))))))
1424 ;; Hide custom properties with an empty value.
1425 (should
1426 (let ((org-custom-properties '("FOO")))
1427 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
1428 (org-toggle-custom-properties-visibility)
1429 (org-invisible-p2))))
1430 ;; Do not hide fake properties.
1431 (should-not
1432 (let ((org-custom-properties '("FOO")))
1433 (org-test-with-temp-text ":FOO: val\n"
1434 (org-toggle-custom-properties-visibility)
1435 (org-invisible-p2))))
1436 (should-not
1437 (let ((org-custom-properties '("A")))
1438 (org-test-with-temp-text
1439 "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
1440 (org-toggle-custom-properties-visibility)
1441 (org-invisible-p2)))))
1445 ;;; Mark Region
1447 (ert-deftest test-org/mark-subtree ()
1448 "Test `org-mark-subtree' specifications."
1449 ;; Error when point is before first headline.
1450 (should-error
1451 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
1452 (progn (transient-mark-mode 1)
1453 (org-mark-subtree))))
1454 ;; Without argument, mark current subtree.
1455 (should
1456 (equal
1457 '(12 32)
1458 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1459 (progn (transient-mark-mode 1)
1460 (forward-line 2)
1461 (org-mark-subtree)
1462 (list (region-beginning) (region-end))))))
1463 ;; With an argument, move ARG up.
1464 (should
1465 (equal
1466 '(1 32)
1467 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1468 (progn (transient-mark-mode 1)
1469 (forward-line 2)
1470 (org-mark-subtree 1)
1471 (list (region-beginning) (region-end))))))
1472 ;; Do not get fooled by inlinetasks.
1473 (when (featurep 'org-inlinetask)
1474 (should
1475 (= 1
1476 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
1477 (progn (transient-mark-mode 1)
1478 (forward-line 1)
1479 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
1480 (region-beginning)))))))
1484 ;;; Navigation
1486 (ert-deftest test-org/beginning-of-line ()
1487 "Test `org-beginning-of-line' specifications."
1488 ;; Standard test.
1489 (should
1490 (org-test-with-temp-text "Some text\nSome other text"
1491 (progn (org-beginning-of-line) (bolp))))
1492 ;; Standard test with `visual-line-mode'.
1493 (should-not
1494 (org-test-with-temp-text "A long line of text\nSome other text"
1495 (progn (visual-line-mode)
1496 (forward-char 2)
1497 (dotimes (i 1000) (insert "very "))
1498 (org-beginning-of-line)
1499 (bolp))))
1500 ;; At an headline with special movement.
1501 (should
1502 (org-test-with-temp-text "* TODO Headline"
1503 (let ((org-special-ctrl-a/e t))
1504 (org-end-of-line)
1505 (and (progn (org-beginning-of-line) (looking-at "Headline"))
1506 (progn (org-beginning-of-line) (bolp))
1507 (progn (org-beginning-of-line) (looking-at "Headline")))))))
1509 (ert-deftest test-org/end-of-line ()
1510 "Test `org-end-of-line' specifications."
1511 ;; Standard test.
1512 (should
1513 (org-test-with-temp-text "Some text\nSome other text"
1514 (progn (org-end-of-line) (eolp))))
1515 ;; Standard test with `visual-line-mode'.
1516 (should-not
1517 (org-test-with-temp-text "A long line of text\nSome other text"
1518 (progn (visual-line-mode)
1519 (forward-char 2)
1520 (dotimes (i 1000) (insert "very "))
1521 (goto-char (point-min))
1522 (org-end-of-line)
1523 (eolp))))
1524 ;; At an headline with special movement.
1525 (should
1526 (org-test-with-temp-text "* Headline1 :tag:\n"
1527 (let ((org-special-ctrl-a/e t))
1528 (and (progn (org-end-of-line) (looking-at " :tag:"))
1529 (progn (org-end-of-line) (eolp))
1530 (progn (org-end-of-line) (looking-at " :tag:"))))))
1531 ;; At an headline without special movement.
1532 (should
1533 (org-test-with-temp-text "* Headline2 :tag:\n"
1534 (let ((org-special-ctrl-a/e nil))
1535 (and (progn (org-end-of-line) (eolp))
1536 (progn (org-end-of-line) (eolp))))))
1537 ;; At an headline, with reversed movement.
1538 (should
1539 (org-test-with-temp-text "* Headline3 :tag:\n"
1540 (let ((org-special-ctrl-a/e 'reversed)
1541 (this-command last-command))
1542 (and (progn (org-end-of-line) (eolp))
1543 (progn (org-end-of-line) (looking-at " :tag:"))))))
1544 ;; At a block without hidden contents.
1545 (should
1546 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1547 (progn (org-end-of-line) (eolp))))
1548 ;; At a block with hidden contents.
1549 (should-not
1550 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1551 (let ((org-special-ctrl-a/e t))
1552 (org-hide-block-toggle)
1553 (org-end-of-line)
1554 (eobp)))))
1556 (ert-deftest test-org/forward-paragraph ()
1557 "Test `org-forward-paragraph' specifications."
1558 ;; At end of buffer, return an error.
1559 (should-error
1560 (org-test-with-temp-text "Paragraph"
1561 (goto-char (point-max))
1562 (org-forward-paragraph)))
1563 ;; Standard test.
1564 (should
1565 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1566 (org-forward-paragraph)
1567 (looking-at "P2")))
1568 ;; Ignore depth.
1569 (should
1570 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
1571 (org-forward-paragraph)
1572 (looking-at "P1")))
1573 ;; Do not enter elements with invisible contents.
1574 (should
1575 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
1576 (org-hide-block-toggle)
1577 (org-forward-paragraph)
1578 (looking-at "P3")))
1579 ;; On an affiliated keyword, jump to the beginning of the element.
1580 (should
1581 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
1582 (org-forward-paragraph)
1583 (looking-at "Para")))
1584 ;; On an item or a footnote definition, move to the second element
1585 ;; inside, if any.
1586 (should
1587 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
1588 (org-forward-paragraph)
1589 (looking-at " Paragraph")))
1590 (should
1591 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
1592 (org-forward-paragraph)
1593 (looking-at "Paragraph")))
1594 ;; On an item, or a footnote definition, when the first line is
1595 ;; empty, move to the first item.
1596 (should
1597 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
1598 (org-forward-paragraph)
1599 (looking-at " Paragraph")))
1600 (should
1601 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
1602 (org-forward-paragraph)
1603 (looking-at "Paragraph")))
1604 ;; On a table (resp. a property drawer) do not move through table
1605 ;; rows (resp. node properties).
1606 (should
1607 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
1608 (org-forward-paragraph)
1609 (looking-at "Paragraph")))
1610 (should
1611 (org-test-with-temp-text
1612 "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
1613 (org-forward-paragraph)
1614 (looking-at "Paragraph")))
1615 ;; On a verse or source block, stop after blank lines.
1616 (should
1617 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
1618 (org-forward-paragraph)
1619 (looking-at "L2")))
1620 (should
1621 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
1622 (org-forward-paragraph)
1623 (looking-at "L2"))))
1625 (ert-deftest test-org/backward-paragraph ()
1626 "Test `org-backward-paragraph' specifications."
1627 ;; Error at beginning of buffer.
1628 (should-error
1629 (org-test-with-temp-text "Paragraph"
1630 (org-backward-paragraph)))
1631 ;; Regular test.
1632 (should
1633 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1634 (goto-char (point-max))
1635 (org-backward-paragraph)
1636 (looking-at "P3")))
1637 (should
1638 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1639 (goto-char (point-max))
1640 (beginning-of-line)
1641 (org-backward-paragraph)
1642 (looking-at "P2")))
1643 ;; Ignore depth.
1644 (should
1645 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
1646 (goto-char (point-max))
1647 (beginning-of-line)
1648 (org-backward-paragraph)
1649 (looking-at "P2")))
1650 ;; Ignore invisible elements.
1651 (should
1652 (org-test-with-temp-text "* H1\n P1\n* H2"
1653 (org-cycle)
1654 (goto-char (point-max))
1655 (beginning-of-line)
1656 (org-backward-paragraph)
1657 (bobp)))
1658 ;; On an affiliated keyword, jump to the first one.
1659 (should
1660 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
1661 (search-forward "c2")
1662 (org-backward-paragraph)
1663 (looking-at "#\\+name")))
1664 ;; On the second element in an item or a footnote definition, jump
1665 ;; to item or the definition.
1666 (should
1667 (org-test-with-temp-text "- line1\n\n line2"
1668 (goto-char (point-max))
1669 (beginning-of-line)
1670 (org-backward-paragraph)
1671 (looking-at "- line1")))
1672 (should
1673 (org-test-with-temp-text "[fn:1] line1\n\n line2"
1674 (goto-char (point-max))
1675 (beginning-of-line)
1676 (org-backward-paragraph)
1677 (looking-at "\\[fn:1\\] line1")))
1678 ;; On a table (resp. a property drawer), ignore table rows
1679 ;; (resp. node properties).
1680 (should
1681 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
1682 (goto-char (point-max))
1683 (beginning-of-line)
1684 (org-backward-paragraph)
1685 (bobp)))
1686 (should
1687 (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
1688 (org-backward-paragraph)
1689 (looking-at ":PROPERTIES:")))
1690 ;; On a source or verse block, stop before blank lines.
1691 (should
1692 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
1693 (search-forward "L3")
1694 (beginning-of-line)
1695 (org-backward-paragraph)
1696 (looking-at "L2")))
1697 (should
1698 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
1699 (search-forward "L3")
1700 (beginning-of-line)
1701 (org-backward-paragraph)
1702 (looking-at "L2"))))
1704 (ert-deftest test-org/forward-element ()
1705 "Test `org-forward-element' specifications."
1706 ;; 1. At EOB: should error.
1707 (org-test-with-temp-text "Some text\n"
1708 (goto-char (point-max))
1709 (should-error (org-forward-element)))
1710 ;; 2. Standard move: expected to ignore blank lines.
1711 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
1712 (org-forward-element)
1713 (should (looking-at (regexp-quote "Second paragraph."))))
1714 ;; 3. Headline tests.
1715 (org-test-with-temp-text "
1716 * Head 1
1717 ** Head 1.1
1718 *** Head 1.1.1
1719 ** Head 1.2"
1720 ;; 3.1. At an headline beginning: move to next headline at the
1721 ;; same level.
1722 (goto-line 3)
1723 (org-forward-element)
1724 (should (looking-at (regexp-quote "** Head 1.2")))
1725 ;; 3.2. At an headline beginning: move to parent headline if no
1726 ;; headline at the same level.
1727 (goto-line 3)
1728 (org-forward-element)
1729 (should (looking-at (regexp-quote "** Head 1.2"))))
1730 ;; 4. Greater element tests.
1731 (org-test-with-temp-text
1732 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
1733 ;; 4.1. At a greater element: expected to skip contents.
1734 (org-forward-element)
1735 (should (looking-at (regexp-quote "Outside.")))
1736 ;; 4.2. At the end of greater element contents: expected to skip
1737 ;; to the end of the greater element.
1738 (goto-line 2)
1739 (org-forward-element)
1740 (should (looking-at (regexp-quote "Outside."))))
1741 ;; 5. List tests.
1742 (org-test-with-temp-text "
1743 - item1
1745 - sub1
1747 - sub2
1749 - sub3
1751 Inner paragraph.
1753 - item2
1755 Outside."
1756 ;; 5.1. At list top point: expected to move to the element after
1757 ;; the list.
1758 (goto-line 2)
1759 (org-forward-element)
1760 (should (looking-at (regexp-quote "Outside.")))
1761 ;; 5.2. Special case: at the first line of a sub-list, but not at
1762 ;; beginning of line, move to next item.
1763 (goto-line 2)
1764 (forward-char)
1765 (org-forward-element)
1766 (should (looking-at "- item2"))
1767 (goto-line 4)
1768 (forward-char)
1769 (org-forward-element)
1770 (should (looking-at " - sub2"))
1771 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
1772 (goto-line 4)
1773 (org-forward-element)
1774 (should (looking-at (regexp-quote " Inner paragraph.")))
1775 ;; 5.4. At sub-list end: expected to move outside the sub-list.
1776 (goto-line 8)
1777 (org-forward-element)
1778 (should (looking-at (regexp-quote " Inner paragraph.")))
1779 ;; 5.5. At an item: expected to move to next item, if any.
1780 (goto-line 6)
1781 (org-forward-element)
1782 (should (looking-at " - sub3"))))
1784 (ert-deftest test-org/backward-element ()
1785 "Test `org-backward-element' specifications."
1786 ;; 1. Should error at BOB.
1787 (org-test-with-temp-text " \nParagraph."
1788 (should-error (org-backward-element)))
1789 ;; 2. Should move at BOB when called on the first element in buffer.
1790 (should
1791 (org-test-with-temp-text "\n#+TITLE: test"
1792 (progn (forward-line)
1793 (org-backward-element)
1794 (bobp))))
1795 ;; 3. Not at the beginning of an element: move at its beginning.
1796 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
1797 (goto-line 3)
1798 (end-of-line)
1799 (org-backward-element)
1800 (should (looking-at (regexp-quote "Paragraph2."))))
1801 ;; 4. Headline tests.
1802 (org-test-with-temp-text "
1803 * Head 1
1804 ** Head 1.1
1805 *** Head 1.1.1
1806 ** Head 1.2"
1807 ;; 4.1. At an headline beginning: move to previous headline at the
1808 ;; same level.
1809 (goto-line 5)
1810 (org-backward-element)
1811 (should (looking-at (regexp-quote "** Head 1.1")))
1812 ;; 4.2. At an headline beginning: move to parent headline if no
1813 ;; headline at the same level.
1814 (goto-line 3)
1815 (org-backward-element)
1816 (should (looking-at (regexp-quote "* Head 1")))
1817 ;; 4.3. At the first top-level headline: should error.
1818 (goto-line 2)
1819 (should-error (org-backward-element)))
1820 ;; 5. At beginning of first element inside a greater element:
1821 ;; expected to move to greater element's beginning.
1822 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
1823 (goto-line 3)
1824 (org-backward-element)
1825 (should (looking-at "#\\+BEGIN_CENTER")))
1826 ;; 6. At the beginning of the first element in a section: should
1827 ;; move back to headline, if any.
1828 (should
1829 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
1830 (progn (goto-char (point-max))
1831 (beginning-of-line)
1832 (org-backward-element)
1833 (org-at-heading-p))))
1834 ;; 7. List tests.
1835 (org-test-with-temp-text "
1836 - item1
1838 - sub1
1840 - sub2
1842 - sub3
1844 Inner paragraph.
1846 - item2
1849 Outside."
1850 ;; 7.1. At beginning of sub-list: expected to move to the
1851 ;; paragraph before it.
1852 (goto-line 4)
1853 (org-backward-element)
1854 (should (looking-at "item1"))
1855 ;; 7.2. At an item in a list: expected to move at previous item.
1856 (goto-line 8)
1857 (org-backward-element)
1858 (should (looking-at " - sub2"))
1859 (goto-line 12)
1860 (org-backward-element)
1861 (should (looking-at "- item1"))
1862 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
1863 ;; beginning.
1864 (goto-line 10)
1865 (org-backward-element)
1866 (should (looking-at " - sub1"))
1867 (goto-line 15)
1868 (org-backward-element)
1869 (should (looking-at "- item1"))
1870 ;; 7.4. At blank-lines before list end: expected to move to top
1871 ;; item.
1872 (goto-line 14)
1873 (org-backward-element)
1874 (should (looking-at "- item1"))))
1876 (ert-deftest test-org/up-element ()
1877 "Test `org-up-element' specifications."
1878 ;; 1. At BOB or with no surrounding element: should error.
1879 (org-test-with-temp-text "Paragraph."
1880 (should-error (org-up-element)))
1881 (org-test-with-temp-text "* Head1\n* Head2"
1882 (goto-line 2)
1883 (should-error (org-up-element)))
1884 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
1885 (goto-line 3)
1886 (should-error (org-up-element)))
1887 ;; 2. At an headline: move to parent headline.
1888 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
1889 (goto-line 3)
1890 (org-up-element)
1891 (should (looking-at "\\* Head1")))
1892 ;; 3. Inside a greater element: move to greater element beginning.
1893 (org-test-with-temp-text
1894 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
1895 (goto-line 3)
1896 (org-up-element)
1897 (should (looking-at "#\\+BEGIN_CENTER")))
1898 ;; 4. List tests.
1899 (org-test-with-temp-text "* Top
1900 - item1
1902 - sub1
1904 - sub2
1906 Paragraph within sub2.
1908 - item2"
1909 ;; 4.1. Within an item: move to the item beginning.
1910 (goto-line 8)
1911 (org-up-element)
1912 (should (looking-at " - sub2"))
1913 ;; 4.2. At an item in a sub-list: move to parent item.
1914 (goto-line 4)
1915 (org-up-element)
1916 (should (looking-at "- item1"))
1917 ;; 4.3. At an item in top list: move to beginning of whole list.
1918 (goto-line 10)
1919 (org-up-element)
1920 (should (looking-at "- item1"))
1921 ;; 4.4. Special case. At very top point: should move to parent of
1922 ;; list.
1923 (goto-line 2)
1924 (org-up-element)
1925 (should (looking-at "\\* Top"))))
1927 (ert-deftest test-org/down-element ()
1928 "Test `org-down-element' specifications."
1929 ;; Error when the element hasn't got a recursive type.
1930 (org-test-with-temp-text "Paragraph."
1931 (should-error (org-down-element)))
1932 ;; Error when the element has no contents
1933 (org-test-with-temp-text "* Headline"
1934 (should-error (org-down-element)))
1935 ;; When at a plain-list, move to first item.
1936 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
1937 (goto-line 2)
1938 (org-down-element)
1939 (should (looking-at " - Item 1.1")))
1940 (org-test-with-temp-text "#+NAME: list\n- Item 1"
1941 (org-down-element)
1942 (should (looking-at " Item 1")))
1943 ;; When at a table, move to first row
1944 (org-test-with-temp-text "#+NAME: table\n| a | b |"
1945 (org-down-element)
1946 (should (looking-at " a | b |")))
1947 ;; Otherwise, move inside the greater element.
1948 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
1949 (org-down-element)
1950 (should (looking-at "Paragraph"))))
1952 (ert-deftest test-org/drag-element-backward ()
1953 "Test `org-drag-element-backward' specifications."
1954 ;; Standard test.
1955 (should
1956 (equal
1957 "#+key2: val2\n#+key1: val1\n#+key3: val3"
1958 (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
1959 (org-drag-element-backward)
1960 (buffer-string))))
1961 (should
1962 (equal
1963 "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
1964 (org-test-with-temp-text
1965 "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
1966 (org-drag-element-backward)
1967 (buffer-string))))
1968 ;; Preserve blank lines.
1969 (should
1970 (equal "Paragraph 2\n\n\nPara1\n\nPara3"
1971 (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
1972 (org-drag-element-backward)
1973 (buffer-string))))
1974 ;; Preserve column.
1975 (should
1976 (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
1977 (org-drag-element-backward)
1978 (org-looking-at-p "2")))
1979 ;; Error when trying to move first element of buffer.
1980 (should-error
1981 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
1982 (org-drag-element-backward)))
1983 ;; Error when trying to swap nested elements.
1984 (should-error
1985 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
1986 (forward-line)
1987 (org-drag-element-backward)))
1988 ;; Error when trying to swap an headline element and a non-headline
1989 ;; element.
1990 (should-error
1991 (org-test-with-temp-text "Test.\n* Head 1"
1992 (forward-line)
1993 (org-drag-element-backward)))
1994 ;; Preserve visibility of elements and their contents.
1995 (should
1996 (equal '((63 . 82) (26 . 48))
1997 (org-test-with-temp-text "
1998 #+BEGIN_CENTER
1999 Text.
2000 #+END_CENTER
2001 - item 1
2002 #+BEGIN_QUOTE
2003 Text.
2004 #+END_QUOTE"
2005 (while (search-forward "BEGIN_" nil t) (org-cycle))
2006 (search-backward "- item 1")
2007 (org-drag-element-backward)
2008 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2009 (overlays-in (point-min) (point-max)))))))
2011 (ert-deftest test-org/drag-element-forward ()
2012 "Test `org-drag-element-forward' specifications."
2013 ;; 1. Error when trying to move first element of buffer.
2014 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2015 (goto-line 3)
2016 (should-error (org-drag-element-forward)))
2017 ;; 2. Error when trying to swap nested elements.
2018 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2019 (forward-line)
2020 (should-error (org-drag-element-forward)))
2021 ;; 3. Error when trying to swap a non-headline element and an
2022 ;; headline.
2023 (org-test-with-temp-text "Test.\n* Head 1"
2024 (should-error (org-drag-element-forward)))
2025 ;; 4. Otherwise, swap elements, preserving column and blank lines
2026 ;; between elements.
2027 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
2028 (search-forward "graph")
2029 (org-drag-element-forward)
2030 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
2031 (should (looking-at " 1")))
2032 ;; 5. Preserve visibility of elements and their contents.
2033 (org-test-with-temp-text "
2034 #+BEGIN_CENTER
2035 Text.
2036 #+END_CENTER
2037 - item 1
2038 #+BEGIN_QUOTE
2039 Text.
2040 #+END_QUOTE"
2041 (while (search-forward "BEGIN_" nil t) (org-cycle))
2042 (search-backward "#+BEGIN_CENTER")
2043 (org-drag-element-forward)
2044 (should
2045 (equal
2046 '((63 . 82) (26 . 48))
2047 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2048 (overlays-in (point-min) (point-max)))))))
2052 ;;; Outline structure
2054 (ert-deftest test-org/demote ()
2055 "Test `org-demote' specifications."
2056 ;; Add correct number of stars according to `org-odd-levels-only'.
2057 (should
2058 (= 2
2059 (org-test-with-temp-text "* H"
2060 (let ((org-odd-levels-only nil)) (org-demote))
2061 (org-current-level))))
2062 (should
2063 (= 3
2064 (org-test-with-temp-text "* H"
2065 (let ((org-odd-levels-only t)) (org-demote))
2066 (org-current-level))))
2067 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2068 (should
2069 (org-test-with-temp-text "* H :tag:"
2070 (let ((org-tags-column 10)
2071 (org-auto-align-tags t)
2072 (org-odd-levels-only nil))
2073 (org-demote))
2074 (org-move-to-column 10)
2075 (org-looking-at-p ":tag:$")))
2076 (should-not
2077 (org-test-with-temp-text "* H :tag:"
2078 (let ((org-tags-column 10)
2079 (org-auto-align-tags nil)
2080 (org-odd-levels-only nil))
2081 (org-demote))
2082 (org-move-to-column 10)
2083 (org-looking-at-p ":tag:$")))
2084 ;; When `org-adapt-indentation' is non-nil, always indent planning
2085 ;; info and property drawers accordingly.
2086 (should
2087 (= 3
2088 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2089 (let ((org-odd-levels-only nil)
2090 (org-adapt-indentation t))
2091 (org-demote))
2092 (forward-line)
2093 (org-get-indentation))))
2094 (should
2095 (= 3
2096 (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2097 (let ((org-odd-levels-only nil)
2098 (org-adapt-indentation t))
2099 (org-demote))
2100 (forward-line)
2101 (org-get-indentation))))
2102 (should-not
2103 (= 3
2104 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2105 (let ((org-odd-levels-only nil)
2106 (org-adapt-indentation nil))
2107 (org-demote))
2108 (forward-line)
2109 (org-get-indentation))))
2110 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2111 ;; section accordingly. Ignore, however, footnote definitions and
2112 ;; inlinetasks boundaries.
2113 (should
2114 (= 3
2115 (org-test-with-temp-text "* H\n Paragraph"
2116 (let ((org-odd-levels-only nil)
2117 (org-adapt-indentation t))
2118 (org-demote))
2119 (forward-line)
2120 (org-get-indentation))))
2121 (should
2122 (= 2
2123 (org-test-with-temp-text "* H\n Paragraph"
2124 (let ((org-odd-levels-only nil)
2125 (org-adapt-indentation nil))
2126 (org-demote))
2127 (forward-line)
2128 (org-get-indentation))))
2129 (should
2130 (zerop
2131 (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
2132 (let ((org-odd-levels-only nil)
2133 (org-adapt-indentation t))
2134 (org-demote))
2135 (goto-char (point-max))
2136 (org-get-indentation))))
2137 (should
2138 (= 3
2139 (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
2140 (let ((org-odd-levels-only nil)
2141 (org-adapt-indentation t))
2142 (org-demote))
2143 (goto-char (point-max))
2144 (org-get-indentation))))
2145 (when (featurep 'org-inlinetask)
2146 (should
2147 (zerop
2148 (let ((org-inlinetask-min-level 5)
2149 (org-adapt-indentation t))
2150 (org-test-with-temp-text "* H\n***** I\n***** END"
2151 (org-demote)
2152 (forward-line)
2153 (org-get-indentation))))))
2154 (when (featurep 'org-inlinetask)
2155 (should
2156 (= 3
2157 (let ((org-inlinetask-min-level 5)
2158 (org-adapt-indentation t))
2159 (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
2160 (org-demote)
2161 (forward-line 2)
2162 (org-get-indentation))))))
2163 ;; Ignore contents of source blocks or example blocks when
2164 ;; indentation should be preserved (through
2165 ;; `org-src-preserve-indentation' or "-i" flag).
2166 (should-not
2167 (zerop
2168 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2169 (let ((org-adapt-indentation t)
2170 (org-src-preserve-indentation nil))
2171 (org-demote))
2172 (forward-line 2)
2173 (org-get-indentation))))
2174 (should
2175 (zerop
2176 (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2177 (let ((org-adapt-indentation t)
2178 (org-src-preserve-indentation t))
2179 (org-demote))
2180 (forward-line 2)
2181 (org-get-indentation))))
2182 (should
2183 (zerop
2184 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2185 (let ((org-adapt-indentation t)
2186 (org-src-preserve-indentation t))
2187 (org-demote))
2188 (forward-line 2)
2189 (org-get-indentation))))
2190 (should
2191 (zerop
2192 (org-test-with-temp-text
2193 "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
2194 (let ((org-adapt-indentation t)
2195 (org-src-preserve-indentation nil))
2196 (org-demote))
2197 (forward-line 2)
2198 (org-get-indentation)))))
2200 (ert-deftest test-org/promote ()
2201 "Test `org-promote' specifications."
2202 ;; Return an error if headline is to be promoted to level 0, unless
2203 ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
2204 ;; headline becomes a comment.
2205 (should-error
2206 (org-test-with-temp-text "* H"
2207 (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
2208 (should
2209 (equal "# H"
2210 (org-test-with-temp-text "* H"
2211 (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
2212 (buffer-string))))
2213 ;; Remove correct number of stars according to
2214 ;; `org-odd-levels-only'.
2215 (should
2216 (= 2
2217 (org-test-with-temp-text "*** H"
2218 (let ((org-odd-levels-only nil)) (org-promote))
2219 (org-current-level))))
2220 (should
2221 (= 1
2222 (org-test-with-temp-text "*** H"
2223 (let ((org-odd-levels-only t)) (org-promote))
2224 (org-current-level))))
2225 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2226 (should
2227 (org-test-with-temp-text "** H :tag:"
2228 (let ((org-tags-column 10)
2229 (org-auto-align-tags t)
2230 (org-odd-levels-only nil))
2231 (org-promote))
2232 (org-move-to-column 10)
2233 (org-looking-at-p ":tag:$")))
2234 (should-not
2235 (org-test-with-temp-text "** H :tag:"
2236 (let ((org-tags-column 10)
2237 (org-auto-align-tags nil)
2238 (org-odd-levels-only nil))
2239 (org-promote))
2240 (org-move-to-column 10)
2241 (org-looking-at-p ":tag:$")))
2242 ;; When `org-adapt-indentation' is non-nil, always indent planning
2243 ;; info and property drawers.
2244 (should
2245 (= 2
2246 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2247 (let ((org-odd-levels-only nil)
2248 (org-adapt-indentation t))
2249 (org-promote))
2250 (forward-line)
2251 (org-get-indentation))))
2252 (should
2253 (= 2
2254 (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2255 (let ((org-odd-levels-only nil)
2256 (org-adapt-indentation t))
2257 (org-promote))
2258 (forward-line)
2259 (org-get-indentation))))
2260 (should-not
2261 (= 2
2262 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2263 (let ((org-odd-levels-only nil)
2264 (org-adapt-indentation nil))
2265 (org-promote))
2266 (forward-line)
2267 (org-get-indentation))))
2268 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2269 ;; section accordingly. Ignore, however, footnote definitions and
2270 ;; inlinetasks boundaries.
2271 (should
2272 (= 2
2273 (org-test-with-temp-text "** H\n Paragraph"
2274 (let ((org-odd-levels-only nil)
2275 (org-adapt-indentation t))
2276 (org-promote))
2277 (forward-line)
2278 (org-get-indentation))))
2279 (should-not
2280 (= 2
2281 (org-test-with-temp-text "** H\n Paragraph"
2282 (let ((org-odd-levels-only nil)
2283 (org-adapt-indentation nil))
2284 (org-promote))
2285 (forward-line)
2286 (org-get-indentation))))
2287 (should
2288 (= 2
2289 (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
2290 (let ((org-odd-levels-only nil)
2291 (org-adapt-indentation t))
2292 (org-promote))
2293 (forward-line)
2294 (org-get-indentation))))
2295 (when (featurep 'org-inlinetask)
2296 (should
2297 (zerop
2298 (let ((org-inlinetask-min-level 5)
2299 (org-adapt-indentation t))
2300 (org-test-with-temp-text "** H\n***** I\n***** END"
2301 (org-promote)
2302 (forward-line)
2303 (org-get-indentation))))))
2304 (when (featurep 'org-inlinetask)
2305 (should
2306 (= 2
2307 (let ((org-inlinetask-min-level 5)
2308 (org-adapt-indentation t))
2309 (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
2310 (org-promote)
2311 (forward-line 2)
2312 (org-get-indentation))))))
2313 ;; Give up shifting if it would break document's structure
2314 ;; otherwise.
2315 (should
2316 (= 3
2317 (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
2318 (let ((org-odd-levels-only nil)
2319 (org-adapt-indentation t))
2320 (org-promote))
2321 (forward-line)
2322 (org-get-indentation))))
2323 (should
2324 (= 3
2325 (org-test-with-temp-text "** H\n Paragraph\n * list."
2326 (let ((org-odd-levels-only nil)
2327 (org-adapt-indentation t))
2328 (org-promote))
2329 (forward-line)
2330 (org-get-indentation))))
2331 ;; Ignore contents of source blocks or example blocks when
2332 ;; indentation should be preserved (through
2333 ;; `org-src-preserve-indentation' or "-i" flag).
2334 (should-not
2335 (zerop
2336 (org-test-with-temp-text
2337 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2338 (let ((org-adapt-indentation t)
2339 (org-src-preserve-indentation nil)
2340 (org-odd-levels-only nil))
2341 (org-promote))
2342 (forward-line)
2343 (org-get-indentation))))
2344 (should
2345 (zerop
2346 (org-test-with-temp-text
2347 "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
2348 (let ((org-adapt-indentation t)
2349 (org-src-preserve-indentation t)
2350 (org-odd-levels-only nil))
2351 (org-promote))
2352 (forward-line)
2353 (org-get-indentation))))
2354 (should
2355 (zerop
2356 (org-test-with-temp-text
2357 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2358 (let ((org-adapt-indentation t)
2359 (org-src-preserve-indentation t)
2360 (org-odd-levels-only nil))
2361 (org-promote))
2362 (forward-line)
2363 (org-get-indentation))))
2364 (should
2365 (zerop
2366 (org-test-with-temp-text
2367 "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
2368 (let ((org-adapt-indentation t)
2369 (org-src-preserve-indentation nil)
2370 (org-odd-levels-only nil))
2371 (org-promote))
2372 (forward-line)
2373 (org-get-indentation)))))
2376 ;;; Planning
2378 (ert-deftest test-org/timestamp-has-time-p ()
2379 "Test `org-timestamp-has-time-p' specifications."
2380 ;; With time.
2381 (should
2382 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
2383 (org-timestamp-has-time-p (org-element-context))))
2384 ;; Without time.
2385 (should-not
2386 (org-test-with-temp-text "<2012-03-29 Thu>"
2387 (org-timestamp-has-time-p (org-element-context)))))
2389 (ert-deftest test-org/timestamp-format ()
2390 "Test `org-timestamp-format' specifications."
2391 ;; Regular test.
2392 (should
2393 (equal
2394 "2012-03-29 16:40"
2395 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
2396 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
2397 ;; Range end.
2398 (should
2399 (equal
2400 "2012-03-29"
2401 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
2402 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
2404 (ert-deftest test-org/timestamp-split-range ()
2405 "Test `org-timestamp-split-range' specifications."
2406 ;; Extract range start (active).
2407 (should
2408 (equal '(2012 3 29)
2409 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
2410 (let ((ts (org-timestamp-split-range (org-element-context))))
2411 (mapcar (lambda (p) (org-element-property p ts))
2412 '(:year-end :month-end :day-end))))))
2413 ;; Extract range start (inactive)
2414 (should
2415 (equal '(2012 3 29)
2416 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
2417 (let ((ts (org-timestamp-split-range (org-element-context))))
2418 (mapcar (lambda (p) (org-element-property p ts))
2419 '(:year-end :month-end :day-end))))))
2420 ;; Extract range end (active).
2421 (should
2422 (equal '(2012 3 30)
2423 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
2424 (let ((ts (org-timestamp-split-range
2425 (org-element-context) t)))
2426 (mapcar (lambda (p) (org-element-property p ts))
2427 '(:year-end :month-end :day-end))))))
2428 ;; Extract range end (inactive)
2429 (should
2430 (equal '(2012 3 30)
2431 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
2432 (let ((ts (org-timestamp-split-range
2433 (org-element-context) t)))
2434 (mapcar (lambda (p) (org-element-property p ts))
2435 '(:year-end :month-end :day-end))))))
2436 ;; Return the timestamp if not a range.
2437 (should
2438 (org-test-with-temp-text "[2012-03-29 Thu]"
2439 (let* ((ts-orig (org-element-context))
2440 (ts-copy (org-timestamp-split-range ts-orig)))
2441 (eq ts-orig ts-copy))))
2442 (should
2443 (org-test-with-temp-text "<%%(org-float t 4 2)>"
2444 (let* ((ts-orig (org-element-context))
2445 (ts-copy (org-timestamp-split-range ts-orig)))
2446 (eq ts-orig ts-copy))))
2447 ;; Check that parent is the same when a range was split.
2448 (should
2449 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
2450 (let* ((ts-orig (org-element-context))
2451 (ts-copy (org-timestamp-split-range ts-orig)))
2452 (eq (org-element-property :parent ts-orig)
2453 (org-element-property :parent ts-copy))))))
2455 (ert-deftest test-org/timestamp-translate ()
2456 "Test `org-timestamp-translate' specifications."
2457 ;; Translate whole date range.
2458 (should
2459 (equal "<29>--<30>"
2460 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
2461 (let ((org-display-custom-times t)
2462 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
2463 (org-timestamp-translate (org-element-context))))))
2464 ;; Translate date range start.
2465 (should
2466 (equal "<29>"
2467 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
2468 (let ((org-display-custom-times t)
2469 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
2470 (org-timestamp-translate (org-element-context) 'start)))))
2471 ;; Translate date range end.
2472 (should
2473 (equal "<30>"
2474 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
2475 (let ((org-display-custom-times t)
2476 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
2477 (org-timestamp-translate (org-element-context) 'end)))))
2478 ;; Translate time range.
2479 (should
2480 (equal "<08>--<16>"
2481 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
2482 (let ((org-display-custom-times t)
2483 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
2484 (org-timestamp-translate (org-element-context))))))
2485 ;; Translate non-range timestamp.
2486 (should
2487 (equal "<29>"
2488 (org-test-with-temp-text "<2012-03-29 Thu>"
2489 (let ((org-display-custom-times t)
2490 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
2491 (org-timestamp-translate (org-element-context))))))
2492 ;; Do not change `diary' timestamps.
2493 (should
2494 (equal "<%%(org-float t 4 2)>"
2495 (org-test-with-temp-text "<%%(org-float t 4 2)>"
2496 (let ((org-display-custom-times t)
2497 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
2498 (org-timestamp-translate (org-element-context)))))))
2502 ;;; Property API
2504 (ert-deftest test-org/buffer-property-keys ()
2505 "Test `org-buffer-property-keys' specifications."
2506 ;; Retrieve properties accross siblings.
2507 (should
2508 (equal '("A" "B")
2509 (org-test-with-temp-text "
2510 * H1
2511 :PROPERTIES:
2512 :A: 1
2513 :END:
2514 * H2
2515 :PROPERTIES:
2516 :B: 1
2517 :END:"
2518 (org-buffer-property-keys))))
2519 ;; Retrieve properties accross children.
2520 (should
2521 (equal '("A" "B")
2522 (org-test-with-temp-text "
2523 * H1
2524 :PROPERTIES:
2525 :A: 1
2526 :END:
2527 ** H2
2528 :PROPERTIES:
2529 :B: 1
2530 :END:"
2531 (org-buffer-property-keys))))
2532 ;; Retrieve muliple properties in the same drawer.
2533 (should
2534 (equal '("A" "B")
2535 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2536 (org-buffer-property-keys))))
2537 ;; Ignore extension symbol in property name.
2538 (should
2539 (equal '("A")
2540 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
2541 (org-buffer-property-keys))))
2542 ;; With non-nil COLUMNS, extract property names from columns.
2543 (should
2544 (equal '("A" "B")
2545 (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
2546 (org-buffer-property-keys nil nil t))))
2547 (should
2548 (equal '("A" "B" "COLUMNS")
2549 (org-test-with-temp-text
2550 "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
2551 (org-buffer-property-keys nil nil t)))))
2553 (ert-deftest test-org/property-values ()
2554 "Test `org-property-values' specifications."
2555 ;; Regular test.
2556 (should
2557 (equal '("2" "1")
2558 (org-test-with-temp-text
2559 "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
2560 (org-property-values "A"))))
2561 ;; Ignore empty values.
2562 (should-not
2563 (org-test-with-temp-text
2564 "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
2565 (org-property-values "A")))
2566 ;; Take into consideration extended values.
2567 (should
2568 (equal '("1 2")
2569 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
2570 (org-property-values "A")))))
2572 (ert-deftest test-org/entry-delete ()
2573 "Test `org-entry-delete' specifications."
2574 ;; Regular test.
2575 (should
2576 (string-match
2577 " *:PROPERTIES:\n *:B: +2\n *:END:"
2578 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2579 (org-entry-delete (point) "A")
2580 (buffer-string))))
2581 ;; When last property is removed, remove the property drawer.
2582 (should-not
2583 (string-match
2584 ":PROPERTIES:"
2585 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
2586 (org-entry-delete (point) "A")
2587 (buffer-string)))))
2589 (ert-deftest test-org/entry-get ()
2590 "Test `org-entry-get' specifications."
2591 ;; Regular test.
2592 (should
2593 (equal "1"
2594 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2595 (org-entry-get (point) "A"))))
2596 ;; Ignore case.
2597 (should
2598 (equal "1"
2599 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2600 (org-entry-get (point) "a"))))
2601 ;; Handle extended values, both before and after base value.
2602 (should
2603 (equal "1 2 3"
2604 (org-test-with-temp-text
2605 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
2606 (org-entry-get (point) "A"))))
2607 ;; Empty values are returned as the empty string.
2608 (should
2609 (equal ""
2610 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
2611 (org-entry-get (point) "A"))))
2612 ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
2613 ;; otherwise, return nil.
2614 (should-not
2615 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
2616 (org-entry-get (point) "A")))
2617 (should
2618 (equal "nil"
2619 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
2620 (org-entry-get (point) "A" nil t))))
2621 ;; Return nil when no property is found, independently on the
2622 ;; LITERAL-NIL argument.
2623 (should-not
2624 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2625 (org-entry-get (point) "B")))
2626 (should-not
2627 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2628 (org-entry-get (point) "B" nil t)))
2629 ;; Handle inheritance, when allowed.
2630 (should
2631 (equal
2633 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2634 (org-entry-get (point) "A" t))))
2635 (should
2636 (equal
2638 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2639 (let ((org-use-property-inheritance t))
2640 (org-entry-get (point) "A" 'selective)))))
2641 (should-not
2642 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2643 (let ((org-use-property-inheritance nil))
2644 (org-entry-get (point) "A" 'selective)))))
2646 (ert-deftest test-org/entry-properties ()
2647 "Test `org-entry-properties' specifications."
2648 ;; Get "ITEM" property.
2649 (should
2650 (equal "* H"
2651 (org-test-with-temp-text "* TODO H"
2652 (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
2653 (should
2654 (equal "* H"
2655 (org-test-with-temp-text "* TODO H"
2656 (cdr (assoc "ITEM" (org-entry-properties))))))
2657 ;; Get "TODO" property.
2658 (should
2659 (equal "TODO"
2660 (org-test-with-temp-text "* TODO H"
2661 (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
2662 (should
2663 (equal "TODO"
2664 (org-test-with-temp-text "* TODO H"
2665 (cdr (assoc "TODO" (org-entry-properties))))))
2666 (should-not
2667 (org-test-with-temp-text "* H"
2668 (assoc "TODO" (org-entry-properties nil "TODO"))))
2669 ;; Get "PRIORITY" property.
2670 (should
2671 (equal "A"
2672 (org-test-with-temp-text "* [#A] H"
2673 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
2674 (should
2675 (equal "A"
2676 (org-test-with-temp-text "* [#A] H"
2677 (cdr (assoc "PRIORITY" (org-entry-properties))))))
2678 (should-not
2679 (org-test-with-temp-text "* H"
2680 (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
2681 ;; Get "FILE" property.
2682 (should
2683 (org-test-with-temp-text-in-file "* H\nParagraph"
2684 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
2685 (buffer-file-name))))
2686 (should
2687 (org-test-with-temp-text-in-file "* H\nParagraph"
2688 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
2689 (buffer-file-name))))
2690 (should-not
2691 (org-test-with-temp-text "* H\nParagraph"
2692 (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
2693 ;; Get "TAGS" property.
2694 (should
2695 (equal ":tag1:tag2:"
2696 (org-test-with-temp-text "* H :tag1:tag2:"
2697 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
2698 (should
2699 (equal ":tag1:tag2:"
2700 (org-test-with-temp-text "* H :tag1:tag2:"
2701 (cdr (assoc "TAGS" (org-entry-properties))))))
2702 (should-not
2703 (org-test-with-temp-text "* H"
2704 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
2705 ;; Get "ALLTAGS" property.
2706 (should
2707 (equal ":tag1:tag2:"
2708 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
2709 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
2710 (should
2711 (equal ":tag1:tag2:"
2712 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
2713 (cdr (assoc "ALLTAGS" (org-entry-properties))))))
2714 (should-not
2715 (org-test-with-temp-text "* H"
2716 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
2717 ;; Get "BLOCKED" property.
2718 (should
2719 (equal "t"
2720 (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
2721 (let ((org-enforce-todo-dependencies t)
2722 (org-blocker-hook
2723 '(org-block-todo-from-children-or-siblings-or-parent)))
2724 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
2725 (should
2726 (equal "t"
2727 (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
2728 (let ((org-enforce-todo-dependencies t)
2729 (org-blocker-hook
2730 '(org-block-todo-from-children-or-siblings-or-parent)))
2731 (cdr (assoc "BLOCKED" (org-entry-properties)))))))
2732 (should
2733 (equal ""
2734 (org-test-with-temp-text "* Blocked\n** DONE one\n** DONE two"
2735 (let ((org-enforce-todo-dependencies t))
2736 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
2737 ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
2738 (should
2739 (equal
2740 "[2012-03-29 thu.]"
2741 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
2742 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
2743 (should
2744 (equal
2745 "[2012-03-29 thu.]"
2746 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
2747 (cdr (assoc "CLOSED" (org-entry-properties))))))
2748 (should-not
2749 (org-test-with-temp-text "* H"
2750 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
2751 (should
2752 (equal
2753 "<2014-03-04 tue.>"
2754 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
2755 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
2756 (should
2757 (equal
2758 "<2014-03-04 tue.>"
2759 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
2760 (cdr (assoc "DEADLINE" (org-entry-properties))))))
2761 (should-not
2762 (org-test-with-temp-text "* H"
2763 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
2764 (should
2765 (equal
2766 "<2014-03-04 tue.>"
2767 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2768 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
2769 (should
2770 (equal
2771 "<2014-03-04 tue.>"
2772 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2773 (cdr (assoc "SCHEDULED" (org-entry-properties))))))
2774 (should-not
2775 (org-test-with-temp-text "* H"
2776 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
2777 ;; Get "CATEGORY"
2778 (should
2779 (equal "cat"
2780 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
2781 (cdr (assoc "CATEGORY" (org-entry-properties))))))
2782 (should
2783 (equal "cat"
2784 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
2785 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
2786 (should
2787 (equal "cat"
2788 (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
2789 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
2790 ;; Get standard properties.
2791 (should
2792 (equal "1"
2793 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2794 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
2795 ;; Handle extended properties.
2796 (should
2797 (equal "1 2 3"
2798 (org-test-with-temp-text
2799 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
2800 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
2801 (should
2802 (equal "1 2 3"
2803 (org-test-with-temp-text
2804 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
2805 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
2806 ;; Ignore forbidden (special) properties.
2807 (should-not
2808 (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
2809 (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
2811 (ert-deftest test-org/entry-put ()
2812 "Test `org-entry-put' specifications."
2813 ;; Error when not a string or nil.
2814 (should-error
2815 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
2816 (org-entry-put 1 "test" 2)))
2817 ;; Set "TODO" property.
2818 (should
2819 (string-match (regexp-quote " TODO H")
2820 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
2821 (org-entry-put (point) "TODO" "TODO")
2822 (buffer-string))))
2823 (should
2824 (string-match (regexp-quote "* H")
2825 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
2826 (org-entry-put (point) "TODO" nil)
2827 (buffer-string))))
2828 ;; Set "PRIORITY" property.
2829 (should
2830 (equal "* [#A] H"
2831 (org-test-with-temp-text "* [#B] H"
2832 (org-entry-put (point) "PRIORITY" "A")
2833 (buffer-string))))
2834 (should
2835 (equal "* H"
2836 (org-test-with-temp-text "* [#B] H"
2837 (org-entry-put (point) "PRIORITY" nil)
2838 (buffer-string))))
2839 ;; Set "SCHEDULED" property.
2840 (should
2841 (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
2842 (org-test-with-temp-text "* H"
2843 (org-entry-put (point) "SCHEDULED" "2014-03-04")
2844 (buffer-string))))
2845 (should
2846 (string= "* H\n"
2847 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2848 (org-entry-put (point) "SCHEDULED" nil)
2849 (buffer-string))))
2850 (should
2851 (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
2852 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2853 (org-entry-put (point) "SCHEDULED" "earlier")
2854 (buffer-string))))
2855 (should
2856 (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
2857 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2858 (org-entry-put (point) "SCHEDULED" "later")
2859 (buffer-string))))
2860 ;; Set "DEADLINE" property.
2861 (should
2862 (string-match "^ *DEADLINE: <2014-03-04 .*?>"
2863 (org-test-with-temp-text "* H"
2864 (org-entry-put (point) "DEADLINE" "2014-03-04")
2865 (buffer-string))))
2866 (should
2867 (string= "* H\n"
2868 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
2869 (org-entry-put (point) "DEADLINE" nil)
2870 (buffer-string))))
2871 (should
2872 (string-match "^ *DEADLINE: <2014-03-03 .*?>"
2873 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
2874 (org-entry-put (point) "DEADLINE" "earlier")
2875 (buffer-string))))
2876 (should
2877 (string-match "^ *DEADLINE: <2014-03-05 .*?>"
2878 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
2879 (org-entry-put (point) "DEADLINE" "later")
2880 (buffer-string))))
2881 ;; Regular properties, with or without pre-existing drawer.
2882 (should
2883 (string-match "^ *:A: +2$"
2884 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2885 (org-entry-put (point) "A" "2")
2886 (buffer-string))))
2887 (should
2888 (string-match "^ *:A: +1$"
2889 (org-test-with-temp-text "* H"
2890 (org-entry-put (point) "A" "1")
2891 (buffer-string))))
2892 ;; Special case: two consecutive headlines.
2893 (should
2894 (string-match "\\* A\n *:PROPERTIES:"
2895 (org-test-with-temp-text "* A\n** B"
2896 (org-entry-put (point) "A" "1")
2897 (buffer-string)))))
2900 ;;; Radio Targets
2902 (ert-deftest test-org/update-radio-target-regexp ()
2903 "Test `org-update-radio-target-regexp' specifications."
2904 ;; Properly update cache with no previous radio target regexp.
2905 (should
2906 (eq 'link
2907 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
2908 (save-excursion (goto-char (point-max)) (org-element-context))
2909 (insert "<<<")
2910 (search-forward "o")
2911 (insert ">>>")
2912 (org-update-radio-target-regexp)
2913 (goto-char (point-max))
2914 (org-element-type (org-element-context)))))
2915 ;; Properly update cache with previous radio target regexp.
2916 (should
2917 (eq 'link
2918 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
2919 (save-excursion (goto-char (point-max)) (org-element-context))
2920 (insert "<<<")
2921 (search-forward "o")
2922 (insert ">>>")
2923 (org-update-radio-target-regexp)
2924 (search-backward "r")
2925 (delete-char 5)
2926 (insert "new")
2927 (org-update-radio-target-regexp)
2928 (goto-char (point-max))
2929 (delete-region (line-beginning-position) (point))
2930 (insert "new")
2931 (org-element-type (org-element-context))))))
2935 ;;; Visibility
2937 (ert-deftest test-org/flag-drawer ()
2938 "Test `org-flag-drawer' specifications."
2939 ;; Hide drawer.
2940 (should
2941 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
2942 (org-flag-drawer t)
2943 (get-char-property (line-end-position) 'invisible)))
2944 ;; Show drawer.
2945 (should-not
2946 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
2947 (org-flag-drawer t)
2948 (org-flag-drawer nil)
2949 (get-char-property (line-end-position) 'invisible)))
2950 ;; Test optional argument.
2951 (should
2952 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
2953 (let ((drawer (save-excursion (search-forward ":D2")
2954 (org-element-at-point))))
2955 (org-flag-drawer t drawer)
2956 (get-char-property (progn (search-forward ":D2") (line-end-position))
2957 'invisible))))
2958 (should-not
2959 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
2960 (let ((drawer (save-excursion (search-forward ":D2")
2961 (org-element-at-point))))
2962 (org-flag-drawer t drawer)
2963 (get-char-property (line-end-position) 'invisible))))
2964 ;; Do not hide fake drawers.
2965 (should-not
2966 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
2967 (forward-line 1)
2968 (org-flag-drawer t)
2969 (get-char-property (line-end-position) 'invisible)))
2970 ;; Do not hide incomplete drawers.
2971 (should-not
2972 (org-test-with-temp-text ":D:\nparagraph"
2973 (forward-line 1)
2974 (org-flag-drawer t)
2975 (get-char-property (line-end-position) 'invisible)))
2976 ;; Do not hide drawers when called from final blank lines.
2977 (should-not
2978 (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
2979 (goto-char (point-max))
2980 (org-flag-drawer t)
2981 (goto-char (point-min))
2982 (get-char-property (line-end-position) 'invisible)))
2983 ;; Don't leave point in an invisible part of the buffer when hiding
2984 ;; a drawer away.
2985 (should-not
2986 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
2987 (goto-char (point-max))
2988 (org-flag-drawer t)
2989 (get-char-property (point) 'invisible))))
2991 (ert-deftest test-org/hide-block-toggle ()
2992 "Test `org-hide-block-toggle' specifications."
2993 ;; Error when not at a block.
2994 (should-error
2995 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
2996 (org-hide-block-toggle 'off)
2997 (get-char-property (line-end-position) 'invisible)))
2998 ;; Hide block.
2999 (should
3000 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
3001 (org-hide-block-toggle)
3002 (get-char-property (line-end-position) 'invisible)))
3003 (should
3004 (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
3005 (org-hide-block-toggle)
3006 (get-char-property (line-end-position) 'invisible)))
3007 ;; Show block unconditionally when optional argument is `off'.
3008 (should-not
3009 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3010 (org-hide-block-toggle)
3011 (org-hide-block-toggle 'off)
3012 (get-char-property (line-end-position) 'invisible)))
3013 (should-not
3014 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3015 (org-hide-block-toggle 'off)
3016 (get-char-property (line-end-position) 'invisible)))
3017 ;; Hide block unconditionally when optional argument is non-nil.
3018 (should
3019 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3020 (org-hide-block-toggle t)
3021 (get-char-property (line-end-position) 'invisible)))
3022 (should
3023 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3024 (org-hide-block-toggle)
3025 (org-hide-block-toggle t)
3026 (get-char-property (line-end-position) 'invisible)))
3027 ;; Do not hide block when called from final blank lines.
3028 (should-not
3029 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
3030 (org-hide-block-toggle)
3031 (goto-char (point-min))
3032 (get-char-property (line-end-position) 'invisible)))
3033 ;; Don't leave point in an invisible part of the buffer when hiding
3034 ;; a block away.
3035 (should-not
3036 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
3037 (org-hide-block-toggle)
3038 (get-char-property (point) 'invisible))))
3040 (ert-deftest test-org/hide-block-toggle-maybe ()
3041 "Test `org-hide-block-toggle-maybe' specifications."
3042 (should
3043 (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
3044 (org-hide-block-toggle-maybe)))
3045 (should-not
3046 (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
3049 (provide 'test-org)
3051 ;;; test-org.el ends here