Fix block hiding
[org-mode.git] / testing / lisp / test-org.el
blobecbdadf26d6e862d459c84c2ded9c1f00fa04761
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 ;;; Filling
212 (ert-deftest test-org/fill-paragraph ()
213 "Test `org-fill-paragraph' specifications."
214 ;; At an Org table, align it.
215 (should
216 (equal "| a |\n"
217 (org-test-with-temp-text "|a|"
218 (org-fill-paragraph)
219 (buffer-string))))
220 (should
221 (equal "#+name: table\n| a |\n"
222 (org-test-with-temp-text "#+name: table\n| a |"
223 (org-fill-paragraph)
224 (buffer-string))))
225 ;; At a paragraph, preserve line breaks.
226 (org-test-with-temp-text "some \\\\\nlong\ntext"
227 (let ((fill-column 20))
228 (org-fill-paragraph)
229 (should (equal (buffer-string) "some \\\\\nlong text"))))
230 ;; Correctly fill a paragraph when point is at its very end.
231 (should
232 (equal "A B"
233 (org-test-with-temp-text "A\nB"
234 (let ((fill-column 20))
235 (goto-char (point-max))
236 (org-fill-paragraph)
237 (buffer-string)))))
238 ;; Correctly fill the last paragraph of a greater element.
239 (should
240 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
241 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
242 (let ((fill-column 8))
243 (forward-line)
244 (end-of-line)
245 (org-fill-paragraph)
246 (buffer-string)))))
247 ;; Correctly fill an element in a narrowed buffer.
248 (should
249 (equal "01234\n6"
250 (org-test-with-temp-text "01234 6789"
251 (let ((fill-column 5))
252 (narrow-to-region 1 8)
253 (org-fill-paragraph)
254 (buffer-string)))))
255 ;; Handle `adaptive-fill-regexp' in paragraphs.
256 (should
257 (equal "> a b"
258 (org-test-with-temp-text "> a\n> b"
259 (let ((fill-column 5)
260 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
261 (org-fill-paragraph)
262 (buffer-string)))))
263 ;; Special case: Fill first paragraph when point is at an item or
264 ;; a plain-list or a footnote reference.
265 (should
266 (equal "- A B"
267 (org-test-with-temp-text "- A\n B"
268 (let ((fill-column 20))
269 (org-fill-paragraph)
270 (buffer-string)))))
271 (should
272 (equal "[fn:1] A B"
273 (org-test-with-temp-text "[fn:1] A\nB"
274 (let ((fill-column 20))
275 (org-fill-paragraph)
276 (buffer-string)))))
277 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
278 (let ((fill-column 20))
279 (org-fill-paragraph)
280 (should (equal (buffer-string)
281 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
282 ;; Fill contents of `comment-block' elements.
283 (should
284 (equal
285 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
286 (let ((fill-column 20))
287 (forward-line)
288 (org-fill-paragraph)
289 (buffer-string)))
290 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
291 ;; Fill `comment' elements.
292 (should
293 (equal " # A B"
294 (org-test-with-temp-text " # A\n # B"
295 (let ((fill-column 20))
296 (org-fill-paragraph)
297 (buffer-string)))))
298 ;; Do not mix consecutive comments when filling one of them.
299 (should
300 (equal "# A B\n\n# C"
301 (org-test-with-temp-text "# A\n# B\n\n# C"
302 (let ((fill-column 20))
303 (org-fill-paragraph)
304 (buffer-string)))))
305 ;; Use commented empty lines as separators when filling comments.
306 (should
307 (equal "# A B\n#\n# C"
308 (org-test-with-temp-text "# A\n# B\n#\n# C"
309 (let ((fill-column 20))
310 (org-fill-paragraph)
311 (buffer-string)))))
312 ;; Handle `adaptive-fill-regexp' in comments.
313 (should
314 (equal "# > a b"
315 (org-test-with-temp-text "# > a\n# > b"
316 (let ((fill-column 20)
317 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
318 (org-fill-paragraph)
319 (buffer-string)))))
320 ;; Do nothing at affiliated keywords.
321 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
322 (let ((fill-column 20))
323 (org-fill-paragraph)
324 (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
325 ;; Do not move point after table when filling a table.
326 (should-not
327 (org-test-with-temp-text "| a | b |\n| c | d |\n"
328 (forward-char)
329 (org-fill-paragraph)
330 (eobp))))
332 (ert-deftest test-org/auto-fill-function ()
333 "Test auto-filling features."
334 ;; Auto fill paragraph.
335 (should
336 (equal "12345\n7890"
337 (org-test-with-temp-text "12345 7890"
338 (let ((fill-column 5))
339 (end-of-line)
340 (org-auto-fill-function)
341 (buffer-string)))))
342 ;; Auto fill first paragraph in an item.
343 (should
344 (equal "- 12345\n 7890"
345 (org-test-with-temp-text "- 12345 7890"
346 (let ((fill-column 7))
347 (end-of-line)
348 (org-auto-fill-function)
349 (buffer-string)))))
350 ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
351 (should
352 (equal "> 12345\n 7890"
353 (org-test-with-temp-text "> 12345 7890"
354 (let ((fill-column 10)
355 (adaptive-fill-regexp "[ \t]*>+[ \t]*")
356 (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
357 (end-of-line)
358 (org-auto-fill-function)
359 (buffer-string)))))
360 (should
361 (equal "> 12345\n> 12345\n> 7890"
362 (org-test-with-temp-text "> 12345\n> 12345 7890"
363 (let ((fill-column 10)
364 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
365 (goto-char (point-max))
366 (org-auto-fill-function)
367 (buffer-string)))))
368 (should-not
369 (equal " 12345\n *12345\n *12345"
370 (org-test-with-temp-text " 12345\n *12345 12345"
371 (let ((fill-column 10)
372 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
373 (goto-char (point-max))
374 (org-auto-fill-function)
375 (buffer-string)))))
376 ;; Auto fill comments.
377 (should
378 (equal " # 12345\n # 7890"
379 (org-test-with-temp-text " # 12345 7890"
380 (let ((fill-column 10))
381 (end-of-line)
382 (org-auto-fill-function)
383 (buffer-string)))))
384 ;; A hash within a line isn't a comment.
385 (should-not
386 (equal "12345 # 7890\n# 1"
387 (org-test-with-temp-text "12345 # 7890 1"
388 (let ((fill-column 12))
389 (end-of-line)
390 (org-auto-fill-function)
391 (buffer-string)))))
392 ;; Correctly interpret empty prefix.
393 (should-not
394 (equal "# a\n# b\nRegular\n# paragraph"
395 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
396 (let ((fill-column 12))
397 (end-of-line 3)
398 (org-auto-fill-function)
399 (buffer-string)))))
400 ;; Comment block: auto fill contents.
401 (should
402 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
403 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
404 (let ((fill-column 5))
405 (forward-line)
406 (end-of-line)
407 (org-auto-fill-function)
408 (buffer-string)))))
409 (should
410 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
411 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
412 (let ((fill-column 5))
413 (forward-line)
414 (end-of-line)
415 (org-auto-fill-function)
416 (buffer-string)))))
417 ;; Do not fill if a new item could be created.
418 (should-not
419 (equal "12345\n- 90"
420 (org-test-with-temp-text "12345 - 90"
421 (let ((fill-column 5))
422 (end-of-line)
423 (org-auto-fill-function)
424 (buffer-string)))))
425 ;; Do not fill if a line break could be introduced.
426 (should-not
427 (equal "123\\\\\n7890"
428 (org-test-with-temp-text "123\\\\ 7890"
429 (let ((fill-column 6))
430 (end-of-line)
431 (org-auto-fill-function)
432 (buffer-string)))))
433 ;; Do not fill affiliated keywords.
434 (should-not
435 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
436 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
437 (let ((fill-column 20))
438 (end-of-line)
439 (org-auto-fill-function)
440 (buffer-string))))))
444 ;;; Indentation
446 (ert-deftest test-org/indent-line ()
447 "Test `org-indent-line' specifications."
448 ;; Do not indent footnote definitions or headlines.
449 (should
450 (zerop
451 (org-test-with-temp-text "* H"
452 (org-indent-line)
453 (org-get-indentation))))
454 (should
455 (zerop
456 (org-test-with-temp-text "[fn:1] fn"
457 (let ((org-adapt-indentation t)) (org-indent-line))
458 (org-get-indentation))))
459 ;; Do not indent before first headline.
460 (should
461 (zerop
462 (org-test-with-temp-text ""
463 (org-indent-line)
464 (org-get-indentation))))
465 ;; Indent according to headline level otherwise, unless
466 ;; `org-adapt-indentation' is nil.
467 (should
468 (= 2
469 (org-test-with-temp-text "* H\nA"
470 (forward-line)
471 (let ((org-adapt-indentation t)) (org-indent-line))
472 (org-get-indentation))))
473 (should
474 (= 2
475 (org-test-with-temp-text "* H\n\nA"
476 (forward-line)
477 (let ((org-adapt-indentation t)) (org-indent-line))
478 (org-get-indentation))))
479 (should
480 (zerop
481 (org-test-with-temp-text "* H\nA"
482 (forward-line)
483 (let ((org-adapt-indentation nil)) (org-indent-line))
484 (org-get-indentation))))
485 ;; Indenting preserves point position.
486 (should
487 (org-test-with-temp-text "* H\nAB"
488 (forward-line)
489 (forward-char)
490 (let ((org-adapt-indentation t)) (org-indent-line))
491 (looking-at "B")))
492 ;; Do not change indentation at an item.
493 (should
494 (= 1
495 (org-test-with-temp-text "* H\n - A"
496 (forward-line)
497 (let ((org-adapt-indentation t)) (org-indent-line))
498 (org-get-indentation))))
499 ;; On blank lines at the end of a list, indent like last element
500 ;; within it if the line is still in the list. Otherwise, indent
501 ;; like the whole list.
502 (should
503 (= 4
504 (org-test-with-temp-text "* H\n- A\n - AA\n"
505 (goto-char (point-max))
506 (let ((org-adapt-indentation t)) (org-indent-line))
507 (org-get-indentation))))
508 (should
509 (zerop
510 (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
511 (goto-char (point-max))
512 (let ((org-adapt-indentation t)) (org-indent-line))
513 (org-get-indentation))))
514 ;; Likewise, on a blank line at the end of a footnote definition,
515 ;; indent at column 0 if line belongs to the definition. Otherwise,
516 ;; indent like the definition itself.
517 (should
518 (zerop
519 (org-test-with-temp-text "* H\n[fn:1] Definition\n"
520 (goto-char (point-max))
521 (let ((org-adapt-indentation t)) (org-indent-line))
522 (org-get-indentation))))
523 (should
524 (zerop
525 (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
526 (goto-char (point-max))
527 (let ((org-adapt-indentation t)) (org-indent-line))
528 (org-get-indentation))))
529 ;; After the end of the contents of a greater element, indent like
530 ;; the beginning of the element.
531 (should
532 (= 1
533 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
534 (forward-line 2)
535 (org-indent-line)
536 (org-get-indentation))))
537 ;; At the first line of an element, indent like previous element's
538 ;; first line, ignoring footnotes definitions and inline tasks, or
539 ;; according to parent.
540 (should
541 (= 2
542 (org-test-with-temp-text "A\n\n B\n\nC"
543 (goto-char (point-max))
544 (org-indent-line)
545 (org-get-indentation))))
546 (should
547 (= 1
548 (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
549 (goto-char (point-max))
550 (org-indent-line)
551 (org-get-indentation))))
552 (should
553 (= 1
554 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
555 (forward-line 1)
556 (org-indent-line)
557 (org-get-indentation))))
558 ;; Within code part of a source block, use language major mode if
559 ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
560 ;; according to line above.
561 (should
562 (= 6
563 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
564 (forward-line 2)
565 (let ((org-src-tab-acts-natively t)
566 (org-edit-src-content-indentation 0))
567 (org-indent-line))
568 (org-get-indentation))))
569 (should
570 (= 1
571 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
572 (forward-line 2)
573 (let ((org-src-tab-acts-natively nil)
574 (org-edit-src-content-indentation 0))
575 (org-indent-line))
576 (org-get-indentation))))
577 ;; Otherwise, indent like the first non-blank line above.
578 (should
579 (zerop
580 (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
581 (forward-line 3)
582 (org-indent-line)
583 (org-get-indentation))))
584 ;; Align node properties according to `org-property-format'. Handle
585 ;; nicely empty values.
586 (should
587 (equal ":PROPERTIES:\n:key: value\n:END:"
588 (org-test-with-temp-text ":PROPERTIES:\n:key: value\n:END:"
589 (forward-line)
590 (let ((org-property-format "%-10s %s"))
591 (org-indent-line)
592 (buffer-string)))))
593 (should
594 (equal ":PROPERTIES:\n:key:\n:END:"
595 (org-test-with-temp-text ":PROPERTIES:\n:key:\n:END:"
596 (forward-line)
597 (let ((org-property-format "%-10s %s"))
598 (org-indent-line)
599 (buffer-string))))))
601 (ert-deftest test-org/indent-region ()
602 "Test `org-indent-region' specifications."
603 ;; Indent paragraph.
604 (should
605 (equal "A\nB\nC"
606 (org-test-with-temp-text " A\nB\n C"
607 (org-indent-region (point-min) (point-max))
608 (buffer-string))))
609 ;; Indent greater elements along with their contents.
610 (should
611 (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
612 (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
613 (org-indent-region (point-min) (point-max))
614 (buffer-string))))
615 ;; Ignore contents of verse blocks and example blocks.
616 (should
617 (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
618 (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
619 (org-indent-region (point-min) (point-max))
620 (buffer-string))))
621 (should
622 (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
623 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
624 (org-indent-region (point-min) (point-max))
625 (buffer-string))))
626 ;; Indent according to mode if `org-src-tab-acts-natively' is
627 ;; non-nil. Otherwise, do not indent code at all.
628 (should
629 (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
630 (org-test-with-temp-text
631 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
632 (let ((org-src-tab-acts-natively t)
633 (org-edit-src-content-indentation 0))
634 (org-indent-region (point-min) (point-max)))
635 (buffer-string))))
636 (should
637 (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
638 (org-test-with-temp-text
639 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
640 (let ((org-src-tab-acts-natively nil)
641 (org-edit-src-content-indentation 0))
642 (org-indent-region (point-min) (point-max)))
643 (buffer-string))))
644 ;; Align node properties according to `org-property-format'. Handle
645 ;; nicely empty values.
646 (should
647 (equal ":PROPERTIES:\n:key: value\n:END:"
648 (org-test-with-temp-text ":PROPERTIES:\n:key: value\n:END:"
649 (let ((org-property-format "%-10s %s"))
650 (org-indent-region (point-min) (point-max)))
651 (buffer-string))))
652 (should
653 (equal ":PROPERTIES:\n:key:\n:END:"
654 (org-test-with-temp-text ":PROPERTIES:\n:key:\n:END:"
655 (let ((org-property-format "%-10s %s"))
656 (org-indent-region (point-min) (point-max)))
657 (buffer-string))))
658 ;; Special case: plain lists and footnote definitions.
659 (should
660 (equal "- A\n B\n - C\n\n D"
661 (org-test-with-temp-text "- A\n B\n - C\n\n D"
662 (org-indent-region (point-min) (point-max))
663 (buffer-string))))
664 (should
665 (equal "[fn:1] Definition\n\nDefinition"
666 (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
667 (org-indent-region (point-min) (point-max))
668 (buffer-string)))))
672 ;;; Editing
674 ;;;; Insert elements
676 (ert-deftest test-org/meta-return ()
677 "Test M-RET (`org-meta-return')."
678 ;; In a table field insert a row above.
679 (should
680 (org-test-with-temp-text "| a |"
681 (forward-char)
682 (org-meta-return)
683 (forward-line -1)
684 (looking-at "| |$")))
685 ;; In a paragraph change current line into a header.
686 (should
687 (org-test-with-temp-text "a"
688 (org-meta-return)
689 (beginning-of-line)
690 (looking-at "\* a$")))
691 ;; In an item insert an item, in this case above.
692 (should
693 (org-test-with-temp-text "- a"
694 (org-meta-return)
695 (beginning-of-line)
696 (looking-at "- $")))
697 ;; In a drawer and item insert an item, in this case above.
698 (should
699 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
700 (forward-line)
701 (org-meta-return)
702 (beginning-of-line)
703 (looking-at "- $"))))
705 (ert-deftest test-org/insert-heading ()
706 "Test `org-insert-heading' specifications."
707 ;; FIXME: Test coverage is incomplete yet.
709 ;; In an empty buffer, insert a new headline.
710 (should
711 (equal "* "
712 (org-test-with-temp-text ""
713 (org-insert-heading)
714 (buffer-string))))
715 ;; At the beginning of a line, turn it into a headline
716 (should
717 (equal "* P"
718 (org-test-with-temp-text "<point>P"
719 (org-insert-heading)
720 (buffer-string))))
721 ;; In the middle of a line, split the line if allowed, otherwise,
722 ;; insert the headline at its end.
723 (should
724 (equal "Para\n* graph"
725 (org-test-with-temp-text "Para<point>graph"
726 (let ((org-M-RET-may-split-line '((default . t))))
727 (org-insert-heading))
728 (buffer-string))))
729 (should
730 (equal "Paragraph\n* "
731 (org-test-with-temp-text "Para<point>graph"
732 (let ((org-M-RET-may-split-line '((default . nil))))
733 (org-insert-heading))
734 (buffer-string)))))
736 (ert-deftest test-org/insert-todo-heading-respect-content ()
737 "Test `org-insert-todo-heading-respect-content' specifications."
738 ;; Create a TODO heading.
739 (should
740 (org-test-with-temp-text "* H1\n Body"
741 (org-insert-todo-heading-respect-content)
742 (nth 2 (org-heading-components))))
743 ;; Add headline at the end of the first subtree
744 (should
745 (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
746 (search-forward "H1Body")
747 (org-insert-todo-heading-respect-content)
748 (and (eobp) (org-at-heading-p))))
749 ;; In a list, do not create a new item.
750 (should
751 (org-test-with-temp-text "* H\n- an item\n- another one"
752 (search-forward "an ")
753 (org-insert-todo-heading-respect-content)
754 (and (eobp) (org-at-heading-p)))))
758 ;;; Fixed-Width Areas
760 (ert-deftest test-org/toggle-fixed-width ()
761 "Test `org-toggle-fixed-width' specifications."
762 ;; No region: Toggle on fixed-width marker in paragraphs.
763 (should
764 (equal ": A"
765 (org-test-with-temp-text "A"
766 (org-toggle-fixed-width)
767 (buffer-string))))
768 ;; No region: Toggle off fixed-width markers in fixed-width areas.
769 (should
770 (equal "A"
771 (org-test-with-temp-text ": A"
772 (org-toggle-fixed-width)
773 (buffer-string))))
774 ;; No region: Toggle on marker in blank lines after elements or just
775 ;; after a headline.
776 (should
777 (equal "* H\n: "
778 (org-test-with-temp-text "* H\n"
779 (forward-line)
780 (org-toggle-fixed-width)
781 (buffer-string))))
782 (should
783 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
784 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
785 (goto-char (point-max))
786 (org-toggle-fixed-width)
787 (buffer-string))))
788 ;; No region: Toggle on marker in front of one line elements (e.g.,
789 ;; headlines, clocks)
790 (should
791 (equal ": * Headline"
792 (org-test-with-temp-text "* Headline"
793 (org-toggle-fixed-width)
794 (buffer-string))))
795 (should
796 (equal ": #+KEYWORD: value"
797 (org-test-with-temp-text "#+KEYWORD: value"
798 (org-toggle-fixed-width)
799 (buffer-string))))
800 ;; No region: error in other situations.
801 (should-error
802 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
803 (forward-line)
804 (org-toggle-fixed-width)
805 (buffer-string)))
806 ;; No region: Indentation is preserved.
807 (should
808 (equal "- A\n : B"
809 (org-test-with-temp-text "- A\n B"
810 (forward-line)
811 (org-toggle-fixed-width)
812 (buffer-string))))
813 ;; Region: If it contains only fixed-width elements and blank lines,
814 ;; toggle off fixed-width markup.
815 (should
816 (equal "A\n\nB"
817 (org-test-with-temp-text ": A\n\n: B"
818 (transient-mark-mode 1)
819 (push-mark (point) t t)
820 (goto-char (point-max))
821 (org-toggle-fixed-width)
822 (buffer-string))))
823 ;; Region: If it contains anything else, toggle on fixed-width but
824 ;; not on fixed-width areas.
825 (should
826 (equal ": A\n: \n: B\n: \n: C"
827 (org-test-with-temp-text "A\n\n: B\n\nC"
828 (transient-mark-mode 1)
829 (push-mark (point) t t)
830 (goto-char (point-max))
831 (org-toggle-fixed-width)
832 (buffer-string))))
833 ;; Region: Ignore blank lines at its end, unless it contains only
834 ;; such lines.
835 (should
836 (equal ": A\n\n"
837 (org-test-with-temp-text "A\n\n"
838 (transient-mark-mode 1)
839 (push-mark (point) t t)
840 (goto-char (point-max))
841 (org-toggle-fixed-width)
842 (buffer-string))))
843 (should
844 (equal ": \n: \n"
845 (org-test-with-temp-text "\n\n"
846 (transient-mark-mode 1)
847 (push-mark (point) t t)
848 (goto-char (point-max))
849 (org-toggle-fixed-width)
850 (buffer-string)))))
854 ;;; Headline
856 (ert-deftest test-org/in-commented-heading-p ()
857 "Test `org-in-commented-heading-p' specifications."
858 ;; Commented headline.
859 (should
860 (org-test-with-temp-text "* COMMENT Headline\nBody"
861 (goto-char (point-max))
862 (org-in-commented-heading-p)))
863 ;; Commented ancestor.
864 (should
865 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
866 (goto-char (point-max))
867 (org-in-commented-heading-p)))
868 ;; Comment keyword is case-sensitive.
869 (should-not
870 (org-test-with-temp-text "* Comment Headline\nBody"
871 (goto-char (point-max))
872 (org-in-commented-heading-p)))
873 ;; Keyword is standalone.
874 (should-not
875 (org-test-with-temp-text "* COMMENTHeadline\nBody"
876 (goto-char (point-max))
877 (org-in-commented-heading-p)))
878 ;; Optional argument.
879 (should-not
880 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
881 (goto-char (point-max))
882 (org-in-commented-heading-p t))))
886 ;;; Links
888 ;;;; Coderefs
890 (ert-deftest test-org/coderef ()
891 "Test coderef links specifications."
892 (should
893 (org-test-with-temp-text "
894 #+BEGIN_SRC emacs-lisp
895 \(+ 1 1) (ref:sc)
896 #+END_SRC
897 \[[(sc)]]"
898 (goto-char (point-max))
899 (org-open-at-point)
900 (looking-at "(ref:sc)"))))
902 ;;;; Custom ID
904 (ert-deftest test-org/custom-id ()
905 "Test custom ID links specifications."
906 (should
907 (org-test-with-temp-text
908 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
909 (goto-char (point-max))
910 (org-open-at-point)
911 (org-looking-at-p "\\* H1"))))
913 ;;;; Fuzzy Links
915 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
916 ;; a named element (#+name: text) and to headlines (* Text).
918 (ert-deftest test-org/fuzzy-links ()
919 "Test fuzzy links specifications."
920 ;; Fuzzy link goes in priority to a matching target.
921 (should
922 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
923 (goto-line 5)
924 (org-open-at-point)
925 (looking-at "<<Test>>")))
926 ;; Then fuzzy link points to an element with a given name.
927 (should
928 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
929 (goto-line 5)
930 (org-open-at-point)
931 (looking-at "#\\+NAME: Test")))
932 ;; A target still lead to a matching headline otherwise.
933 (should
934 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
935 (goto-line 4)
936 (org-open-at-point)
937 (looking-at "\\* Head2")))
938 ;; With a leading star in link, enforce heading match.
939 (should
940 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
941 (goto-line 3)
942 (org-open-at-point)
943 (looking-at "\\* Test")))
944 ;; Correctly un-hexify fuzzy links.
945 (should
946 (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
947 (goto-char (point-max))
948 (org-open-at-point)
949 (bobp))))
951 ;;;; Link Escaping
953 (ert-deftest test-org/org-link-escape-ascii-character ()
954 "Escape an ascii character."
955 (should
956 (string=
957 "%5B"
958 (org-link-escape "["))))
960 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
961 "Escape an ascii control character."
962 (should
963 (string=
964 "%09"
965 (org-link-escape "\t"))))
967 (ert-deftest test-org/org-link-escape-multibyte-character ()
968 "Escape an unicode multibyte character."
969 (should
970 (string=
971 "%E2%82%AC"
972 (org-link-escape "€"))))
974 (ert-deftest test-org/org-link-escape-custom-table ()
975 "Escape string with custom character table."
976 (should
977 (string=
978 "Foo%3A%42ar%0A"
979 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
981 (ert-deftest test-org/org-link-escape-custom-table-merge ()
982 "Escape string with custom table merged with default table."
983 (should
984 (string=
985 "%5BF%6F%6F%3A%42ar%0A%5D"
986 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
988 (ert-deftest test-org/org-link-unescape-ascii-character ()
989 "Unescape an ascii character."
990 (should
991 (string=
993 (org-link-unescape "%5B"))))
995 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
996 "Unescpae an ascii control character."
997 (should
998 (string=
999 "\n"
1000 (org-link-unescape "%0A"))))
1002 (ert-deftest test-org/org-link-unescape-multibyte-character ()
1003 "Unescape unicode multibyte character."
1004 (should
1005 (string=
1006 "€"
1007 (org-link-unescape "%E2%82%AC"))))
1009 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
1010 "Unescape old style percent escaped character."
1011 (should
1012 (string=
1013 "àâçèéêîôùû"
1014 (decode-coding-string
1015 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
1017 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
1018 "Escape and unescape a URL that includes an escaped char.
1019 http://article.gmane.org/gmane.emacs.orgmode/21459/"
1020 (should
1021 (string=
1022 "http://some.host.com/form?&id=blah%2Bblah25"
1023 (org-link-unescape
1024 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
1026 (ert-deftest test-org/org-link-escape-chars-browser ()
1027 "Test of the constant `org-link-escape-chars-browser'.
1028 See there why this test is a candidate to be removed once Org
1029 drops support for Emacs 24.1 and 24.2."
1030 (should
1031 (string=
1032 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1033 "%22Release%208.2%22&idxname=emacs-orgmode")
1034 (org-link-escape-browser ; Do not replace with `url-encode-url',
1035 ; see docstring above.
1036 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1037 "\"Release 8.2\"&idxname=emacs-orgmode")))))
1039 ;;;; Open at point
1041 (ert-deftest test-org/open-at-point-in-property ()
1042 "Does `org-open-at-point' open link in property drawer?"
1043 (should
1044 (org-test-with-temp-text
1045 "* Headline
1046 :PROPERTIES:
1047 :URL: <point>[[info:emacs#Top]]
1048 :END:"
1049 (org-open-at-point) t)))
1051 (ert-deftest test-org/open-at-point-in-comment ()
1052 "Does `org-open-at-point' open link in a commented line?"
1053 (should
1054 (org-test-with-temp-text
1055 "# <point>[[info:emacs#Top]]"
1056 (org-open-at-point) t)))
1058 (ert-deftest test-org/open-at-point/info ()
1059 "Test `org-open-at-point' on info links."
1060 (should
1061 (org-test-with-temp-text
1062 "<point>[[info:emacs#Top]]"
1063 (org-open-at-point)
1064 (and (switch-to-buffer "*info*")
1065 (prog1
1066 (looking-at "\nThe Emacs Editor")
1067 (kill-buffer))))))
1070 ;;; Node Properties
1072 (ert-deftest test-org/accumulated-properties-in-drawers ()
1073 "Ensure properties accumulate in subtree drawers."
1074 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
1075 (org-babel-next-src-block)
1076 (should (equal '(2 1) (org-babel-execute-src-block)))))
1080 ;;; Mark Region
1082 (ert-deftest test-org/mark-subtree ()
1083 "Test `org-mark-subtree' specifications."
1084 ;; Error when point is before first headline.
1085 (should-error
1086 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
1087 (progn (transient-mark-mode 1)
1088 (org-mark-subtree))))
1089 ;; Without argument, mark current subtree.
1090 (should
1091 (equal
1092 '(12 32)
1093 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1094 (progn (transient-mark-mode 1)
1095 (forward-line 2)
1096 (org-mark-subtree)
1097 (list (region-beginning) (region-end))))))
1098 ;; With an argument, move ARG up.
1099 (should
1100 (equal
1101 '(1 32)
1102 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1103 (progn (transient-mark-mode 1)
1104 (forward-line 2)
1105 (org-mark-subtree 1)
1106 (list (region-beginning) (region-end))))))
1107 ;; Do not get fooled by inlinetasks.
1108 (when (featurep 'org-inlinetask)
1109 (should
1110 (= 1
1111 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
1112 (progn (transient-mark-mode 1)
1113 (forward-line 1)
1114 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
1115 (region-beginning)))))))
1119 ;;; Navigation
1121 (ert-deftest test-org/beginning-of-line ()
1122 "Test `org-beginning-of-line' specifications."
1123 ;; Standard test.
1124 (should
1125 (org-test-with-temp-text "Some text\nSome other text"
1126 (progn (org-beginning-of-line) (bolp))))
1127 ;; Standard test with `visual-line-mode'.
1128 (should-not
1129 (org-test-with-temp-text "A long line of text\nSome other text"
1130 (progn (visual-line-mode)
1131 (forward-char 2)
1132 (dotimes (i 1000) (insert "very "))
1133 (org-beginning-of-line)
1134 (bolp))))
1135 ;; At an headline with special movement.
1136 (should
1137 (org-test-with-temp-text "* TODO Headline"
1138 (let ((org-special-ctrl-a/e t))
1139 (org-end-of-line)
1140 (and (progn (org-beginning-of-line) (looking-at "Headline"))
1141 (progn (org-beginning-of-line) (bolp))
1142 (progn (org-beginning-of-line) (looking-at "Headline")))))))
1144 (ert-deftest test-org/end-of-line ()
1145 "Test `org-end-of-line' specifications."
1146 ;; Standard test.
1147 (should
1148 (org-test-with-temp-text "Some text\nSome other text"
1149 (progn (org-end-of-line) (eolp))))
1150 ;; Standard test with `visual-line-mode'.
1151 (should-not
1152 (org-test-with-temp-text "A long line of text\nSome other text"
1153 (progn (visual-line-mode)
1154 (forward-char 2)
1155 (dotimes (i 1000) (insert "very "))
1156 (goto-char (point-min))
1157 (org-end-of-line)
1158 (eolp))))
1159 ;; At an headline with special movement.
1160 (should
1161 (org-test-with-temp-text "* Headline1 :tag:\n"
1162 (let ((org-special-ctrl-a/e t))
1163 (and (progn (org-end-of-line) (looking-at " :tag:"))
1164 (progn (org-end-of-line) (eolp))
1165 (progn (org-end-of-line) (looking-at " :tag:"))))))
1166 ;; At an headline without special movement.
1167 (should
1168 (org-test-with-temp-text "* Headline2 :tag:\n"
1169 (let ((org-special-ctrl-a/e nil))
1170 (and (progn (org-end-of-line) (eolp))
1171 (progn (org-end-of-line) (eolp))))))
1172 ;; At an headline, with reversed movement.
1173 (should
1174 (org-test-with-temp-text "* Headline3 :tag:\n"
1175 (let ((org-special-ctrl-a/e 'reversed)
1176 (this-command last-command))
1177 (and (progn (org-end-of-line) (eolp))
1178 (progn (org-end-of-line) (looking-at " :tag:"))))))
1179 ;; At a block without hidden contents.
1180 (should
1181 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1182 (progn (org-end-of-line) (eolp))))
1183 ;; At a block with hidden contents.
1184 (should-not
1185 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1186 (let ((org-special-ctrl-a/e t))
1187 (org-hide-block-toggle)
1188 (org-end-of-line)
1189 (eobp)))))
1191 (ert-deftest test-org/forward-paragraph ()
1192 "Test `org-forward-paragraph' specifications."
1193 ;; At end of buffer, return an error.
1194 (should-error
1195 (org-test-with-temp-text "Paragraph"
1196 (goto-char (point-max))
1197 (org-forward-paragraph)))
1198 ;; Standard test.
1199 (should
1200 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1201 (org-forward-paragraph)
1202 (looking-at "P2")))
1203 ;; Ignore depth.
1204 (should
1205 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
1206 (org-forward-paragraph)
1207 (looking-at "P1")))
1208 ;; Do not enter elements with invisible contents.
1209 (should
1210 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
1211 (org-hide-block-toggle)
1212 (org-forward-paragraph)
1213 (looking-at "P3")))
1214 ;; On an affiliated keyword, jump to the beginning of the element.
1215 (should
1216 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
1217 (org-forward-paragraph)
1218 (looking-at "Para")))
1219 ;; On an item or a footnote definition, move to the second element
1220 ;; inside, if any.
1221 (should
1222 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
1223 (org-forward-paragraph)
1224 (looking-at " Paragraph")))
1225 (should
1226 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
1227 (org-forward-paragraph)
1228 (looking-at "Paragraph")))
1229 ;; On an item, or a footnote definition, when the first line is
1230 ;; empty, move to the first item.
1231 (should
1232 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
1233 (org-forward-paragraph)
1234 (looking-at " Paragraph")))
1235 (should
1236 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
1237 (org-forward-paragraph)
1238 (looking-at "Paragraph")))
1239 ;; On a table (resp. a property drawer) do not move through table
1240 ;; rows (resp. node properties).
1241 (should
1242 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
1243 (org-forward-paragraph)
1244 (looking-at "Paragraph")))
1245 (should
1246 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
1247 (org-forward-paragraph)
1248 (looking-at "Paragraph")))
1249 ;; On a verse or source block, stop after blank lines.
1250 (should
1251 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
1252 (org-forward-paragraph)
1253 (looking-at "L2")))
1254 (should
1255 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
1256 (org-forward-paragraph)
1257 (looking-at "L2"))))
1259 (ert-deftest test-org/backward-paragraph ()
1260 "Test `org-backward-paragraph' specifications."
1261 ;; Error at beginning of buffer.
1262 (should-error
1263 (org-test-with-temp-text "Paragraph"
1264 (org-backward-paragraph)))
1265 ;; Regular test.
1266 (should
1267 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1268 (goto-char (point-max))
1269 (org-backward-paragraph)
1270 (looking-at "P3")))
1271 (should
1272 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1273 (goto-char (point-max))
1274 (beginning-of-line)
1275 (org-backward-paragraph)
1276 (looking-at "P2")))
1277 ;; Ignore depth.
1278 (should
1279 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
1280 (goto-char (point-max))
1281 (beginning-of-line)
1282 (org-backward-paragraph)
1283 (looking-at "P2")))
1284 ;; Ignore invisible elements.
1285 (should
1286 (org-test-with-temp-text "* H1\n P1\n* H2"
1287 (org-cycle)
1288 (goto-char (point-max))
1289 (beginning-of-line)
1290 (org-backward-paragraph)
1291 (bobp)))
1292 ;; On an affiliated keyword, jump to the first one.
1293 (should
1294 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
1295 (search-forward "c2")
1296 (org-backward-paragraph)
1297 (looking-at "#\\+name")))
1298 ;; On the second element in an item or a footnote definition, jump
1299 ;; to item or the definition.
1300 (should
1301 (org-test-with-temp-text "- line1\n\n line2"
1302 (goto-char (point-max))
1303 (beginning-of-line)
1304 (org-backward-paragraph)
1305 (looking-at "- line1")))
1306 (should
1307 (org-test-with-temp-text "[fn:1] line1\n\n line2"
1308 (goto-char (point-max))
1309 (beginning-of-line)
1310 (org-backward-paragraph)
1311 (looking-at "\\[fn:1\\] line1")))
1312 ;; On a table (resp. a property drawer), ignore table rows
1313 ;; (resp. node properties).
1314 (should
1315 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
1316 (goto-char (point-max))
1317 (beginning-of-line)
1318 (org-backward-paragraph)
1319 (bobp)))
1320 (should
1321 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
1322 (goto-char (point-max))
1323 (beginning-of-line)
1324 (org-backward-paragraph)
1325 (bobp)))
1326 ;; On a source or verse block, stop before blank lines.
1327 (should
1328 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
1329 (search-forward "L3")
1330 (beginning-of-line)
1331 (org-backward-paragraph)
1332 (looking-at "L2")))
1333 (should
1334 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
1335 (search-forward "L3")
1336 (beginning-of-line)
1337 (org-backward-paragraph)
1338 (looking-at "L2"))))
1340 (ert-deftest test-org/forward-element ()
1341 "Test `org-forward-element' specifications."
1342 ;; 1. At EOB: should error.
1343 (org-test-with-temp-text "Some text\n"
1344 (goto-char (point-max))
1345 (should-error (org-forward-element)))
1346 ;; 2. Standard move: expected to ignore blank lines.
1347 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
1348 (org-forward-element)
1349 (should (looking-at (regexp-quote "Second paragraph."))))
1350 ;; 3. Headline tests.
1351 (org-test-with-temp-text "
1352 * Head 1
1353 ** Head 1.1
1354 *** Head 1.1.1
1355 ** Head 1.2"
1356 ;; 3.1. At an headline beginning: move to next headline at the
1357 ;; same level.
1358 (goto-line 3)
1359 (org-forward-element)
1360 (should (looking-at (regexp-quote "** Head 1.2")))
1361 ;; 3.2. At an headline beginning: move to parent headline if no
1362 ;; headline at the same level.
1363 (goto-line 3)
1364 (org-forward-element)
1365 (should (looking-at (regexp-quote "** Head 1.2"))))
1366 ;; 4. Greater element tests.
1367 (org-test-with-temp-text
1368 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
1369 ;; 4.1. At a greater element: expected to skip contents.
1370 (org-forward-element)
1371 (should (looking-at (regexp-quote "Outside.")))
1372 ;; 4.2. At the end of greater element contents: expected to skip
1373 ;; to the end of the greater element.
1374 (goto-line 2)
1375 (org-forward-element)
1376 (should (looking-at (regexp-quote "Outside."))))
1377 ;; 5. List tests.
1378 (org-test-with-temp-text "
1379 - item1
1381 - sub1
1383 - sub2
1385 - sub3
1387 Inner paragraph.
1389 - item2
1391 Outside."
1392 ;; 5.1. At list top point: expected to move to the element after
1393 ;; the list.
1394 (goto-line 2)
1395 (org-forward-element)
1396 (should (looking-at (regexp-quote "Outside.")))
1397 ;; 5.2. Special case: at the first line of a sub-list, but not at
1398 ;; beginning of line, move to next item.
1399 (goto-line 2)
1400 (forward-char)
1401 (org-forward-element)
1402 (should (looking-at "- item2"))
1403 (goto-line 4)
1404 (forward-char)
1405 (org-forward-element)
1406 (should (looking-at " - sub2"))
1407 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
1408 (goto-line 4)
1409 (org-forward-element)
1410 (should (looking-at (regexp-quote " Inner paragraph.")))
1411 ;; 5.4. At sub-list end: expected to move outside the sub-list.
1412 (goto-line 8)
1413 (org-forward-element)
1414 (should (looking-at (regexp-quote " Inner paragraph.")))
1415 ;; 5.5. At an item: expected to move to next item, if any.
1416 (goto-line 6)
1417 (org-forward-element)
1418 (should (looking-at " - sub3"))))
1420 (ert-deftest test-org/backward-element ()
1421 "Test `org-backward-element' specifications."
1422 ;; 1. Should error at BOB.
1423 (org-test-with-temp-text " \nParagraph."
1424 (should-error (org-backward-element)))
1425 ;; 2. Should move at BOB when called on the first element in buffer.
1426 (should
1427 (org-test-with-temp-text "\n#+TITLE: test"
1428 (progn (forward-line)
1429 (org-backward-element)
1430 (bobp))))
1431 ;; 3. Not at the beginning of an element: move at its beginning.
1432 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
1433 (goto-line 3)
1434 (end-of-line)
1435 (org-backward-element)
1436 (should (looking-at (regexp-quote "Paragraph2."))))
1437 ;; 4. Headline tests.
1438 (org-test-with-temp-text "
1439 * Head 1
1440 ** Head 1.1
1441 *** Head 1.1.1
1442 ** Head 1.2"
1443 ;; 4.1. At an headline beginning: move to previous headline at the
1444 ;; same level.
1445 (goto-line 5)
1446 (org-backward-element)
1447 (should (looking-at (regexp-quote "** Head 1.1")))
1448 ;; 4.2. At an headline beginning: move to parent headline if no
1449 ;; headline at the same level.
1450 (goto-line 3)
1451 (org-backward-element)
1452 (should (looking-at (regexp-quote "* Head 1")))
1453 ;; 4.3. At the first top-level headline: should error.
1454 (goto-line 2)
1455 (should-error (org-backward-element)))
1456 ;; 5. At beginning of first element inside a greater element:
1457 ;; expected to move to greater element's beginning.
1458 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
1459 (goto-line 3)
1460 (org-backward-element)
1461 (should (looking-at "#\\+BEGIN_CENTER")))
1462 ;; 6. At the beginning of the first element in a section: should
1463 ;; move back to headline, if any.
1464 (should
1465 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
1466 (progn (goto-char (point-max))
1467 (beginning-of-line)
1468 (org-backward-element)
1469 (org-at-heading-p))))
1470 ;; 7. List tests.
1471 (org-test-with-temp-text "
1472 - item1
1474 - sub1
1476 - sub2
1478 - sub3
1480 Inner paragraph.
1482 - item2
1485 Outside."
1486 ;; 7.1. At beginning of sub-list: expected to move to the
1487 ;; paragraph before it.
1488 (goto-line 4)
1489 (org-backward-element)
1490 (should (looking-at "item1"))
1491 ;; 7.2. At an item in a list: expected to move at previous item.
1492 (goto-line 8)
1493 (org-backward-element)
1494 (should (looking-at " - sub2"))
1495 (goto-line 12)
1496 (org-backward-element)
1497 (should (looking-at "- item1"))
1498 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
1499 ;; beginning.
1500 (goto-line 10)
1501 (org-backward-element)
1502 (should (looking-at " - sub1"))
1503 (goto-line 15)
1504 (org-backward-element)
1505 (should (looking-at "- item1"))
1506 ;; 7.4. At blank-lines before list end: expected to move to top
1507 ;; item.
1508 (goto-line 14)
1509 (org-backward-element)
1510 (should (looking-at "- item1"))))
1512 (ert-deftest test-org/up-element ()
1513 "Test `org-up-element' specifications."
1514 ;; 1. At BOB or with no surrounding element: should error.
1515 (org-test-with-temp-text "Paragraph."
1516 (should-error (org-up-element)))
1517 (org-test-with-temp-text "* Head1\n* Head2"
1518 (goto-line 2)
1519 (should-error (org-up-element)))
1520 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
1521 (goto-line 3)
1522 (should-error (org-up-element)))
1523 ;; 2. At an headline: move to parent headline.
1524 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
1525 (goto-line 3)
1526 (org-up-element)
1527 (should (looking-at "\\* Head1")))
1528 ;; 3. Inside a greater element: move to greater element beginning.
1529 (org-test-with-temp-text
1530 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
1531 (goto-line 3)
1532 (org-up-element)
1533 (should (looking-at "#\\+BEGIN_CENTER")))
1534 ;; 4. List tests.
1535 (org-test-with-temp-text "* Top
1536 - item1
1538 - sub1
1540 - sub2
1542 Paragraph within sub2.
1544 - item2"
1545 ;; 4.1. Within an item: move to the item beginning.
1546 (goto-line 8)
1547 (org-up-element)
1548 (should (looking-at " - sub2"))
1549 ;; 4.2. At an item in a sub-list: move to parent item.
1550 (goto-line 4)
1551 (org-up-element)
1552 (should (looking-at "- item1"))
1553 ;; 4.3. At an item in top list: move to beginning of whole list.
1554 (goto-line 10)
1555 (org-up-element)
1556 (should (looking-at "- item1"))
1557 ;; 4.4. Special case. At very top point: should move to parent of
1558 ;; list.
1559 (goto-line 2)
1560 (org-up-element)
1561 (should (looking-at "\\* Top"))))
1563 (ert-deftest test-org/down-element ()
1564 "Test `org-down-element' specifications."
1565 ;; Error when the element hasn't got a recursive type.
1566 (org-test-with-temp-text "Paragraph."
1567 (should-error (org-down-element)))
1568 ;; Error when the element has no contents
1569 (org-test-with-temp-text "* Headline"
1570 (should-error (org-down-element)))
1571 ;; When at a plain-list, move to first item.
1572 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
1573 (goto-line 2)
1574 (org-down-element)
1575 (should (looking-at " - Item 1.1")))
1576 (org-test-with-temp-text "#+NAME: list\n- Item 1"
1577 (org-down-element)
1578 (should (looking-at " Item 1")))
1579 ;; When at a table, move to first row
1580 (org-test-with-temp-text "#+NAME: table\n| a | b |"
1581 (org-down-element)
1582 (should (looking-at " a | b |")))
1583 ;; Otherwise, move inside the greater element.
1584 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
1585 (org-down-element)
1586 (should (looking-at "Paragraph"))))
1588 (ert-deftest test-org/drag-element-backward ()
1589 "Test `org-drag-element-backward' specifications."
1590 ;; Standard test.
1591 (should
1592 (equal
1593 "#+key2: val2\n#+key1: val1\n#+key3: val3"
1594 (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
1595 (org-drag-element-backward)
1596 (buffer-string))))
1597 (should
1598 (equal
1599 "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
1600 (org-test-with-temp-text
1601 "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
1602 (org-drag-element-backward)
1603 (buffer-string))))
1604 ;; Preserve blank lines.
1605 (should
1606 (equal "Paragraph 2\n\n\nPara1\n\nPara3"
1607 (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
1608 (org-drag-element-backward)
1609 (buffer-string))))
1610 ;; Preserve column.
1611 (should
1612 (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
1613 (org-drag-element-backward)
1614 (org-looking-at-p "2")))
1615 ;; Error when trying to move first element of buffer.
1616 (should-error
1617 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
1618 (org-drag-element-backward)))
1619 ;; Error when trying to swap nested elements.
1620 (should-error
1621 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
1622 (forward-line)
1623 (org-drag-element-backward)))
1624 ;; Error when trying to swap an headline element and a non-headline
1625 ;; element.
1626 (should-error
1627 (org-test-with-temp-text "Test.\n* Head 1"
1628 (forward-line)
1629 (org-drag-element-backward)))
1630 ;; Preserve visibility of elements and their contents.
1631 (should
1632 (equal '((63 . 82) (26 . 48))
1633 (org-test-with-temp-text "
1634 #+BEGIN_CENTER
1635 Text.
1636 #+END_CENTER
1637 - item 1
1638 #+BEGIN_QUOTE
1639 Text.
1640 #+END_QUOTE"
1641 (while (search-forward "BEGIN_" nil t) (org-cycle))
1642 (search-backward "- item 1")
1643 (org-drag-element-backward)
1644 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
1645 (overlays-in (point-min) (point-max)))))))
1647 (ert-deftest test-org/drag-element-forward ()
1648 "Test `org-drag-element-forward' specifications."
1649 ;; 1. Error when trying to move first element of buffer.
1650 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
1651 (goto-line 3)
1652 (should-error (org-drag-element-forward)))
1653 ;; 2. Error when trying to swap nested elements.
1654 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
1655 (forward-line)
1656 (should-error (org-drag-element-forward)))
1657 ;; 3. Error when trying to swap a non-headline element and an
1658 ;; headline.
1659 (org-test-with-temp-text "Test.\n* Head 1"
1660 (should-error (org-drag-element-forward)))
1661 ;; 4. Otherwise, swap elements, preserving column and blank lines
1662 ;; between elements.
1663 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
1664 (search-forward "graph")
1665 (org-drag-element-forward)
1666 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
1667 (should (looking-at " 1")))
1668 ;; 5. Preserve visibility of elements and their contents.
1669 (org-test-with-temp-text "
1670 #+BEGIN_CENTER
1671 Text.
1672 #+END_CENTER
1673 - item 1
1674 #+BEGIN_QUOTE
1675 Text.
1676 #+END_QUOTE"
1677 (while (search-forward "BEGIN_" nil t) (org-cycle))
1678 (search-backward "#+BEGIN_CENTER")
1679 (org-drag-element-forward)
1680 (should
1681 (equal
1682 '((63 . 82) (26 . 48))
1683 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
1684 (overlays-in (point-min) (point-max)))))))
1688 ;;; Planning
1690 (ert-deftest test-org/timestamp-has-time-p ()
1691 "Test `org-timestamp-has-time-p' specifications."
1692 ;; With time.
1693 (should
1694 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
1695 (org-timestamp-has-time-p (org-element-context))))
1696 ;; Without time.
1697 (should-not
1698 (org-test-with-temp-text "<2012-03-29 Thu>"
1699 (org-timestamp-has-time-p (org-element-context)))))
1701 (ert-deftest test-org/timestamp-format ()
1702 "Test `org-timestamp-format' specifications."
1703 ;; Regular test.
1704 (should
1705 (equal
1706 "2012-03-29 16:40"
1707 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
1708 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
1709 ;; Range end.
1710 (should
1711 (equal
1712 "2012-03-29"
1713 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
1714 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
1716 (ert-deftest test-org/timestamp-split-range ()
1717 "Test `org-timestamp-split-range' specifications."
1718 ;; Extract range start (active).
1719 (should
1720 (equal '(2012 3 29)
1721 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1722 (let ((ts (org-timestamp-split-range (org-element-context))))
1723 (mapcar (lambda (p) (org-element-property p ts))
1724 '(:year-end :month-end :day-end))))))
1725 ;; Extract range start (inactive)
1726 (should
1727 (equal '(2012 3 29)
1728 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1729 (let ((ts (org-timestamp-split-range (org-element-context))))
1730 (mapcar (lambda (p) (org-element-property p ts))
1731 '(:year-end :month-end :day-end))))))
1732 ;; Extract range end (active).
1733 (should
1734 (equal '(2012 3 30)
1735 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1736 (let ((ts (org-timestamp-split-range
1737 (org-element-context) t)))
1738 (mapcar (lambda (p) (org-element-property p ts))
1739 '(:year-end :month-end :day-end))))))
1740 ;; Extract range end (inactive)
1741 (should
1742 (equal '(2012 3 30)
1743 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1744 (let ((ts (org-timestamp-split-range
1745 (org-element-context) t)))
1746 (mapcar (lambda (p) (org-element-property p ts))
1747 '(:year-end :month-end :day-end))))))
1748 ;; Return the timestamp if not a range.
1749 (should
1750 (org-test-with-temp-text "[2012-03-29 Thu]"
1751 (let* ((ts-orig (org-element-context))
1752 (ts-copy (org-timestamp-split-range ts-orig)))
1753 (eq ts-orig ts-copy))))
1754 (should
1755 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1756 (let* ((ts-orig (org-element-context))
1757 (ts-copy (org-timestamp-split-range ts-orig)))
1758 (eq ts-orig ts-copy))))
1759 ;; Check that parent is the same when a range was split.
1760 (should
1761 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1762 (let* ((ts-orig (org-element-context))
1763 (ts-copy (org-timestamp-split-range ts-orig)))
1764 (eq (org-element-property :parent ts-orig)
1765 (org-element-property :parent ts-copy))))))
1767 (ert-deftest test-org/timestamp-translate ()
1768 "Test `org-timestamp-translate' specifications."
1769 ;; Translate whole date range.
1770 (should
1771 (equal "<29>--<30>"
1772 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1773 (let ((org-display-custom-times t)
1774 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1775 (org-timestamp-translate (org-element-context))))))
1776 ;; Translate date range start.
1777 (should
1778 (equal "<29>"
1779 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1780 (let ((org-display-custom-times t)
1781 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1782 (org-timestamp-translate (org-element-context) 'start)))))
1783 ;; Translate date range end.
1784 (should
1785 (equal "<30>"
1786 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1787 (let ((org-display-custom-times t)
1788 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1789 (org-timestamp-translate (org-element-context) 'end)))))
1790 ;; Translate time range.
1791 (should
1792 (equal "<08>--<16>"
1793 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
1794 (let ((org-display-custom-times t)
1795 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
1796 (org-timestamp-translate (org-element-context))))))
1797 ;; Translate non-range timestamp.
1798 (should
1799 (equal "<29>"
1800 (org-test-with-temp-text "<2012-03-29 Thu>"
1801 (let ((org-display-custom-times t)
1802 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1803 (org-timestamp-translate (org-element-context))))))
1804 ;; Do not change `diary' timestamps.
1805 (should
1806 (equal "<%%(org-float t 4 2)>"
1807 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1808 (let ((org-display-custom-times t)
1809 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1810 (org-timestamp-translate (org-element-context)))))))
1814 ;;; Radio Targets
1816 (ert-deftest test-org/update-radio-target-regexp ()
1817 "Test `org-update-radio-target-regexp' specifications."
1818 ;; Properly update cache with no previous radio target regexp.
1819 (should
1820 (eq 'link
1821 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
1822 (save-excursion (goto-char (point-max)) (org-element-context))
1823 (insert "<<<")
1824 (search-forward "o")
1825 (insert ">>>")
1826 (org-update-radio-target-regexp)
1827 (goto-char (point-max))
1828 (org-element-type (org-element-context)))))
1829 ;; Properly update cache with previous radio target regexp.
1830 (should
1831 (eq 'link
1832 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
1833 (save-excursion (goto-char (point-max)) (org-element-context))
1834 (insert "<<<")
1835 (search-forward "o")
1836 (insert ">>>")
1837 (org-update-radio-target-regexp)
1838 (search-backward "r")
1839 (delete-char 5)
1840 (insert "new")
1841 (org-update-radio-target-regexp)
1842 (goto-char (point-max))
1843 (delete-region (line-beginning-position) (point))
1844 (insert "new")
1845 (org-element-type (org-element-context))))))
1849 ;;; Visibility
1851 (ert-deftest test-org/flag-drawer ()
1852 "Test `org-flag-drawer' specifications."
1853 ;; Hide drawer.
1854 (should
1855 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
1856 (org-flag-drawer t)
1857 (get-char-property (line-end-position) 'invisible)))
1858 ;; Show drawer.
1859 (should-not
1860 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
1861 (org-flag-drawer t)
1862 (org-flag-drawer nil)
1863 (get-char-property (line-end-position) 'invisible)))
1864 ;; Test optional argument.
1865 (should
1866 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
1867 (let ((drawer (save-excursion (search-forward ":D2")
1868 (org-element-at-point))))
1869 (org-flag-drawer t drawer)
1870 (get-char-property (progn (search-forward ":D2") (line-end-position))
1871 'invisible))))
1872 (should-not
1873 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
1874 (let ((drawer (save-excursion (search-forward ":D2")
1875 (org-element-at-point))))
1876 (org-flag-drawer t drawer)
1877 (get-char-property (line-end-position) 'invisible))))
1878 ;; Do not hide fake drawers.
1879 (should-not
1880 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
1881 (forward-line 1)
1882 (org-flag-drawer t)
1883 (get-char-property (line-end-position) 'invisible)))
1884 ;; Do not hide incomplete drawers.
1885 (should-not
1886 (org-test-with-temp-text ":D:\nparagraph"
1887 (forward-line 1)
1888 (org-flag-drawer t)
1889 (get-char-property (line-end-position) 'invisible)))
1890 ;; Do not hide drawers when called from final blank lines.
1891 (should-not
1892 (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
1893 (goto-char (point-max))
1894 (org-flag-drawer t)
1895 (goto-char (point-min))
1896 (get-char-property (line-end-position) 'invisible)))
1897 ;; Don't leave point in an invisible part of the buffer when hiding
1898 ;; a drawer away.
1899 (should-not
1900 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
1901 (goto-char (point-max))
1902 (org-flag-drawer t)
1903 (get-char-property (point) 'invisible))))
1905 (ert-deftest test-org/hide-block-toggle ()
1906 "Test `org-hide-block-toggle' specifications."
1907 ;; Error when not at a block.
1908 (should-error
1909 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
1910 (org-hide-block-toggle 'off)
1911 (get-char-property (line-end-position) 'invisible)))
1912 ;; Hide block.
1913 (should
1914 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
1915 (org-hide-block-toggle)
1916 (get-char-property (line-end-position) 'invisible)))
1917 (should
1918 (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
1919 (org-hide-block-toggle)
1920 (get-char-property (line-end-position) 'invisible)))
1921 ;; Show block unconditionally when optional argument is `off'.
1922 (should-not
1923 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
1924 (org-hide-block-toggle)
1925 (org-hide-block-toggle 'off)
1926 (get-char-property (line-end-position) 'invisible)))
1927 (should-not
1928 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
1929 (org-hide-block-toggle 'off)
1930 (get-char-property (line-end-position) 'invisible)))
1931 ;; Hide block unconditionally when optional argument is non-nil.
1932 (should
1933 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
1934 (org-hide-block-toggle t)
1935 (get-char-property (line-end-position) 'invisible)))
1936 (should
1937 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
1938 (org-hide-block-toggle)
1939 (org-hide-block-toggle t)
1940 (get-char-property (line-end-position) 'invisible)))
1941 ;; Do not hide block when called from final blank lines.
1942 (should-not
1943 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
1944 (org-hide-block-toggle)
1945 (goto-char (point-min))
1946 (get-char-property (line-end-position) 'invisible)))
1947 ;; Don't leave point in an invisible part of the buffer when hiding
1948 ;; a block away.
1949 (should-not
1950 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
1951 (org-hide-block-toggle)
1952 (get-char-property (point) 'invisible))))
1955 (provide 'test-org)
1957 ;;; test-org.el ends here