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