org-texi: Fix typo
[org-mode/org-tableheadings.git] / testing / lisp / test-org.el
blob6faabddb16bab28c67a1608fea051213fd4fab5d
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 ;; FIXME: Test coverage is incomplete yet.
905 ;; In an empty buffer, insert a new headline.
906 (should
907 (equal "* "
908 (org-test-with-temp-text ""
909 (org-insert-heading)
910 (buffer-string))))
911 ;; At the beginning of a line, turn it into a headline
912 (should
913 (equal "* P"
914 (org-test-with-temp-text "<point>P"
915 (org-insert-heading)
916 (buffer-string))))
917 ;; In the middle of a line, split the line if allowed, otherwise,
918 ;; insert the headline at its end.
919 (should
920 (equal "Para\n* graph"
921 (org-test-with-temp-text "Para<point>graph"
922 (let ((org-M-RET-may-split-line '((default . t))))
923 (org-insert-heading))
924 (buffer-string))))
925 (should
926 (equal "Paragraph\n* "
927 (org-test-with-temp-text "Para<point>graph"
928 (let ((org-M-RET-may-split-line '((default . nil))))
929 (org-insert-heading))
930 (buffer-string))))
931 ;; When on a list, insert an item instead, unless called with an
932 ;; universal argument or if list is invisible. In this case, create
933 ;; a new headline after contents.
934 (should
935 (equal "* H\n- item\n- "
936 (org-test-with-temp-text "* H\n- item<point>"
937 (let ((org-insert-heading-respect-content nil))
938 (org-insert-heading))
939 (buffer-string))))
940 (should
941 (equal "* H\n- item\n- item 2\n* "
942 (org-test-with-temp-text "* H\n- item<point>\n- item 2"
943 (let ((org-insert-heading-respect-content nil))
944 (org-insert-heading '(4)))
945 (buffer-string))))
946 (should
947 (equal "* H\n- item\n* "
948 (org-test-with-temp-text "* H\n- item"
949 (org-cycle)
950 (goto-char (point-max))
951 (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
952 (buffer-string))))
953 ;; When called with two universal arguments, insert a new headline
954 ;; at the end of the grandparent subtree.
955 (should
956 (equal "* H1\n** H3\n- item\n** H2\n** "
957 (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
958 (let ((org-insert-heading-respect-content nil))
959 (org-insert-heading '(16)))
960 (buffer-string))))
961 ;; Corner case: correctly insert a headline after an empty one.
962 (should
963 (equal "* \n* "
964 (org-test-with-temp-text "* <point>"
965 (org-insert-heading)
966 (buffer-string)))))
968 (ert-deftest test-org/insert-todo-heading-respect-content ()
969 "Test `org-insert-todo-heading-respect-content' specifications."
970 ;; Create a TODO heading.
971 (should
972 (org-test-with-temp-text "* H1\n Body"
973 (org-insert-todo-heading-respect-content)
974 (nth 2 (org-heading-components))))
975 ;; Add headline at the end of the first subtree
976 (should
977 (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
978 (search-forward "H1Body")
979 (org-insert-todo-heading-respect-content)
980 (and (eobp) (org-at-heading-p))))
981 ;; In a list, do not create a new item.
982 (should
983 (org-test-with-temp-text "* H\n- an item\n- another one"
984 (search-forward "an ")
985 (org-insert-todo-heading-respect-content)
986 (and (eobp) (org-at-heading-p)))))
990 ;;; Fixed-Width Areas
992 (ert-deftest test-org/toggle-fixed-width ()
993 "Test `org-toggle-fixed-width' specifications."
994 ;; No region: Toggle on fixed-width marker in paragraphs.
995 (should
996 (equal ": A"
997 (org-test-with-temp-text "A"
998 (org-toggle-fixed-width)
999 (buffer-string))))
1000 ;; No region: Toggle off fixed-width markers in fixed-width areas.
1001 (should
1002 (equal "A"
1003 (org-test-with-temp-text ": A"
1004 (org-toggle-fixed-width)
1005 (buffer-string))))
1006 ;; No region: Toggle on marker in blank lines after elements or just
1007 ;; after a headline.
1008 (should
1009 (equal "* H\n: "
1010 (org-test-with-temp-text "* H\n"
1011 (forward-line)
1012 (org-toggle-fixed-width)
1013 (buffer-string))))
1014 (should
1015 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
1016 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
1017 (goto-char (point-max))
1018 (org-toggle-fixed-width)
1019 (buffer-string))))
1020 ;; No region: Toggle on marker in front of one line elements (e.g.,
1021 ;; headlines, clocks)
1022 (should
1023 (equal ": * Headline"
1024 (org-test-with-temp-text "* Headline"
1025 (org-toggle-fixed-width)
1026 (buffer-string))))
1027 (should
1028 (equal ": #+KEYWORD: value"
1029 (org-test-with-temp-text "#+KEYWORD: value"
1030 (org-toggle-fixed-width)
1031 (buffer-string))))
1032 ;; No region: error in other situations.
1033 (should-error
1034 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
1035 (forward-line)
1036 (org-toggle-fixed-width)
1037 (buffer-string)))
1038 ;; No region: Indentation is preserved.
1039 (should
1040 (equal "- A\n : B"
1041 (org-test-with-temp-text "- A\n B"
1042 (forward-line)
1043 (org-toggle-fixed-width)
1044 (buffer-string))))
1045 ;; Region: If it contains only fixed-width elements and blank lines,
1046 ;; toggle off fixed-width markup.
1047 (should
1048 (equal "A\n\nB"
1049 (org-test-with-temp-text ": A\n\n: B"
1050 (transient-mark-mode 1)
1051 (push-mark (point) t t)
1052 (goto-char (point-max))
1053 (org-toggle-fixed-width)
1054 (buffer-string))))
1055 ;; Region: If it contains anything else, toggle on fixed-width but
1056 ;; not on fixed-width areas.
1057 (should
1058 (equal ": A\n: \n: B\n: \n: C"
1059 (org-test-with-temp-text "A\n\n: B\n\nC"
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: Ignore blank lines at its end, unless it contains only
1066 ;; such lines.
1067 (should
1068 (equal ": A\n\n"
1069 (org-test-with-temp-text "A\n\n"
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 (should
1076 (equal ": \n: \n"
1077 (org-test-with-temp-text "\n\n"
1078 (transient-mark-mode 1)
1079 (push-mark (point) t t)
1080 (goto-char (point-max))
1081 (org-toggle-fixed-width)
1082 (buffer-string)))))
1086 ;;; Headline
1088 (ert-deftest test-org/in-commented-heading-p ()
1089 "Test `org-in-commented-heading-p' specifications."
1090 ;; Commented headline.
1091 (should
1092 (org-test-with-temp-text "* COMMENT Headline\nBody"
1093 (goto-char (point-max))
1094 (org-in-commented-heading-p)))
1095 ;; Commented ancestor.
1096 (should
1097 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1098 (goto-char (point-max))
1099 (org-in-commented-heading-p)))
1100 ;; Comment keyword is case-sensitive.
1101 (should-not
1102 (org-test-with-temp-text "* Comment Headline\nBody"
1103 (goto-char (point-max))
1104 (org-in-commented-heading-p)))
1105 ;; Keyword is standalone.
1106 (should-not
1107 (org-test-with-temp-text "* COMMENTHeadline\nBody"
1108 (goto-char (point-max))
1109 (org-in-commented-heading-p)))
1110 ;; Optional argument.
1111 (should-not
1112 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1113 (goto-char (point-max))
1114 (org-in-commented-heading-p t))))
1118 ;;; Keywords
1120 (ert-deftest test-org/set-regexps-and-options ()
1121 "Test `org-set-regexps-and-options' specifications."
1122 ;; TAGS keyword.
1123 (should
1124 (equal '(("A" . ?a) ("B") ("C"))
1125 (org-test-with-temp-text "#+TAGS: A(a) B C"
1126 (org-mode-restart)
1127 org-tag-alist)))
1128 (should
1129 (equal '(("A") (:newline) ("B"))
1130 (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
1131 (org-mode-restart)
1132 org-tag-alist)))
1133 (should
1134 (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
1135 (org-test-with-temp-text "#+TAGS: { A B } C"
1136 (org-mode-restart)
1137 org-tag-alist)))
1138 (should
1139 (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
1140 (org-test-with-temp-text "#+TAGS: { A : B C }"
1141 (org-mode-restart)
1142 org-tag-alist)))
1143 (should
1144 (equal '(("A" "B" "C"))
1145 (org-test-with-temp-text "#+TAGS: { A : B C }"
1146 (org-mode-restart)
1147 org-tag-groups-alist)))
1148 ;; FILETAGS keyword.
1149 (should
1150 (equal '("A" "B" "C")
1151 (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
1152 (org-mode-restart)
1153 org-file-tags)))
1154 ;; PROPERTY keyword. Property names are case-insensitive.
1155 (should
1156 (equal "foo=1"
1157 (org-test-with-temp-text "#+PROPERTY: var foo=1"
1158 (org-mode-restart)
1159 (cdr (assoc "var" org-file-properties)))))
1160 (should
1161 (equal
1162 "foo=1 bar=2"
1163 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
1164 (org-mode-restart)
1165 (cdr (assoc "var" org-file-properties)))))
1166 (should
1167 (equal
1168 "foo=1 bar=2"
1169 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
1170 (org-mode-restart)
1171 (cdr (assoc "var" org-file-properties)))))
1172 ;; ARCHIVE keyword.
1173 (should
1174 (equal "%s_done::"
1175 (org-test-with-temp-text "#+ARCHIVE: %s_done::"
1176 (org-mode-restart)
1177 org-archive-location)))
1178 ;; CATEGORY keyword.
1179 (should
1180 (eq 'test
1181 (org-test-with-temp-text "#+CATEGORY: test"
1182 (org-mode-restart)
1183 org-category)))
1184 (should
1185 (equal "test"
1186 (org-test-with-temp-text "#+CATEGORY: test"
1187 (org-mode-restart)
1188 (cdr (assoc "CATEGORY" org-file-properties)))))
1189 ;; COLUMNS keyword.
1190 (should
1191 (equal "%25ITEM %TAGS %PRIORITY %TODO"
1192 (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
1193 (org-mode-restart)
1194 org-columns-default-format)))
1195 ;; CONSTANTS keyword. Constants names are case sensitive.
1196 (should
1197 (equal '("299792458." "3.14")
1198 (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
1199 (org-mode-restart)
1200 (mapcar
1201 (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
1202 '("c" "pi")))))
1203 (should
1204 (equal "3.14"
1205 (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
1206 (org-mode-restart)
1207 (cdr (assoc "pi" org-table-formula-constants-local)))))
1208 (should
1209 (equal "22/7"
1210 (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
1211 (org-mode-restart)
1212 (cdr (assoc "PI" org-table-formula-constants-local)))))
1213 ;; LINK keyword.
1214 (should
1215 (equal
1216 '("url1" "url2")
1217 (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
1218 (org-mode-restart)
1219 (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
1220 '("a" "b")))))
1221 ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
1222 (should
1223 (equal
1224 '(?X ?Z ?Y)
1225 (org-test-with-temp-text "#+PRIORITIES: X Z Y"
1226 (org-mode-restart)
1227 (list org-highest-priority org-lowest-priority org-default-priority))))
1228 (should
1229 (equal
1230 '(?A ?C ?B)
1231 (org-test-with-temp-text "#+PRIORITIES: X Z"
1232 (org-mode-restart)
1233 (list org-highest-priority org-lowest-priority org-default-priority))))
1234 ;; STARTUP keyword.
1235 (should
1236 (equal '(t t)
1237 (org-test-with-temp-text "#+STARTUP: fold odd"
1238 (org-mode-restart)
1239 (list org-startup-folded org-odd-levels-only))))
1240 ;; TODO keywords.
1241 (should
1242 (equal '(("A" "B") ("C"))
1243 (org-test-with-temp-text "#+TODO: A B | C"
1244 (org-mode-restart)
1245 (list org-not-done-keywords org-done-keywords))))
1246 (should
1247 (equal '(("A" "C") ("B" "D"))
1248 (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
1249 (org-mode-restart)
1250 (list org-not-done-keywords org-done-keywords))))
1251 (should
1252 (equal '(("A" "B") ("C"))
1253 (org-test-with-temp-text "#+TYP_TODO: A B | C"
1254 (org-mode-restart)
1255 (list org-not-done-keywords org-done-keywords))))
1256 (should
1257 (equal '((:startgroup) ("A" . ?a) (:endgroup))
1258 (org-test-with-temp-text "#+TODO: A(a)"
1259 (org-mode-restart)
1260 org-todo-key-alist)))
1261 (should
1262 (equal '(("D" note nil) ("C" time nil) ("B" note time))
1263 (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
1264 (org-mode-restart)
1265 org-todo-log-states)))
1266 ;; Enter SETUPFILE keyword.
1267 (should
1268 (equal "1"
1269 (org-test-with-temp-text
1270 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
1271 (org-mode-restart)
1272 (cdr (assoc "a" org-file-properties))))))
1276 ;;; Links
1278 ;;;; Coderefs
1280 (ert-deftest test-org/coderef ()
1281 "Test coderef links specifications."
1282 (should
1283 (org-test-with-temp-text "
1284 #+BEGIN_SRC emacs-lisp
1285 \(+ 1 1) (ref:sc)
1286 #+END_SRC
1287 \[[(sc)]]"
1288 (goto-char (point-max))
1289 (org-open-at-point)
1290 (looking-at "(ref:sc)"))))
1292 ;;;; Custom ID
1294 (ert-deftest test-org/custom-id ()
1295 "Test custom ID links specifications."
1296 (should
1297 (org-test-with-temp-text
1298 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
1299 (goto-char (point-max))
1300 (org-open-at-point)
1301 (org-looking-at-p "\\* H1"))))
1303 ;;;; Fuzzy Links
1305 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
1306 ;; a named element (#+name: text) and to headlines (* Text).
1308 (ert-deftest test-org/fuzzy-links ()
1309 "Test fuzzy links specifications."
1310 ;; Fuzzy link goes in priority to a matching target.
1311 (should
1312 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
1313 (goto-line 5)
1314 (org-open-at-point)
1315 (looking-at "<<Test>>")))
1316 ;; Then fuzzy link points to an element with a given name.
1317 (should
1318 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
1319 (goto-line 5)
1320 (org-open-at-point)
1321 (looking-at "#\\+NAME: Test")))
1322 ;; A target still lead to a matching headline otherwise.
1323 (should
1324 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
1325 (goto-line 4)
1326 (org-open-at-point)
1327 (looking-at "\\* Head2")))
1328 ;; With a leading star in link, enforce heading match.
1329 (should
1330 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
1331 (goto-line 3)
1332 (org-open-at-point)
1333 (looking-at "\\* Test")))
1334 ;; Correctly un-hexify fuzzy links.
1335 (should
1336 (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
1337 (goto-char (point-max))
1338 (org-open-at-point)
1339 (bobp))))
1341 ;;;; Link Escaping
1343 (ert-deftest test-org/org-link-escape-ascii-character ()
1344 "Escape an ascii character."
1345 (should
1346 (string=
1347 "%5B"
1348 (org-link-escape "["))))
1350 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
1351 "Escape an ascii control character."
1352 (should
1353 (string=
1354 "%09"
1355 (org-link-escape "\t"))))
1357 (ert-deftest test-org/org-link-escape-multibyte-character ()
1358 "Escape an unicode multibyte character."
1359 (should
1360 (string=
1361 "%E2%82%AC"
1362 (org-link-escape "€"))))
1364 (ert-deftest test-org/org-link-escape-custom-table ()
1365 "Escape string with custom character table."
1366 (should
1367 (string=
1368 "Foo%3A%42ar%0A"
1369 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
1371 (ert-deftest test-org/org-link-escape-custom-table-merge ()
1372 "Escape string with custom table merged with default table."
1373 (should
1374 (string=
1375 "%5BF%6F%6F%3A%42ar%0A%5D"
1376 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
1378 (ert-deftest test-org/org-link-unescape-ascii-character ()
1379 "Unescape an ascii character."
1380 (should
1381 (string=
1383 (org-link-unescape "%5B"))))
1385 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
1386 "Unescpae an ascii control character."
1387 (should
1388 (string=
1389 "\n"
1390 (org-link-unescape "%0A"))))
1392 (ert-deftest test-org/org-link-unescape-multibyte-character ()
1393 "Unescape unicode multibyte character."
1394 (should
1395 (string=
1396 "€"
1397 (org-link-unescape "%E2%82%AC"))))
1399 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
1400 "Unescape old style percent escaped character."
1401 (should
1402 (string=
1403 "àâçèéêîôùû"
1404 (decode-coding-string
1405 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
1407 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
1408 "Escape and unescape a URL that includes an escaped char.
1409 http://article.gmane.org/gmane.emacs.orgmode/21459/"
1410 (should
1411 (string=
1412 "http://some.host.com/form?&id=blah%2Bblah25"
1413 (org-link-unescape
1414 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
1416 (ert-deftest test-org/org-link-escape-chars-browser ()
1417 "Test of the constant `org-link-escape-chars-browser'.
1418 See there why this test is a candidate to be removed once Org
1419 drops support for Emacs 24.1 and 24.2."
1420 (should
1421 (string=
1422 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1423 "%22Release%208.2%22&idxname=emacs-orgmode")
1424 (org-link-escape-browser ; Do not replace with `url-encode-url',
1425 ; see docstring above.
1426 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1427 "\"Release 8.2\"&idxname=emacs-orgmode")))))
1429 ;;;; Open at point
1431 (ert-deftest test-org/open-at-point-in-property ()
1432 "Does `org-open-at-point' open link in property drawer?"
1433 (should
1434 (org-test-with-temp-text
1435 "* Headline
1436 :PROPERTIES:
1437 :URL: <point>[[info:emacs#Top]]
1438 :END:"
1439 (org-open-at-point) t)))
1441 (ert-deftest test-org/open-at-point-in-comment ()
1442 "Does `org-open-at-point' open link in a commented line?"
1443 (should
1444 (org-test-with-temp-text
1445 "# <point>[[info:emacs#Top]]"
1446 (org-open-at-point) t)))
1448 (ert-deftest test-org/open-at-point/info ()
1449 "Test `org-open-at-point' on info links."
1450 (should
1451 (org-test-with-temp-text
1452 "<point>[[info:emacs#Top]]"
1453 (org-open-at-point)
1454 (and (switch-to-buffer "*info*")
1455 (prog1
1456 (looking-at "\nThe Emacs Editor")
1457 (kill-buffer))))))
1459 (ert-deftest test-org/open-at-point/inline-image ()
1460 "Test `org-open-at-point' on nested links."
1461 (should
1462 (org-test-with-temp-text "[[info:org#Top][info:<point>emacs#Top]]"
1463 (org-open-at-point)
1464 (prog1 (with-current-buffer "*info*" (looking-at "\nOrg Mode Manual"))
1465 (kill-buffer "*info*")))))
1468 ;;; Node Properties
1470 (ert-deftest test-org/accumulated-properties-in-drawers ()
1471 "Ensure properties accumulate in subtree drawers."
1472 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
1473 (org-babel-next-src-block)
1474 (should (equal '(2 1) (org-babel-execute-src-block)))))
1476 (ert-deftest test-org/custom-properties ()
1477 "Test custom properties specifications."
1478 ;; Standard test.
1479 (should
1480 (let ((org-custom-properties '("FOO")))
1481 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1482 (org-toggle-custom-properties-visibility)
1483 (org-invisible-p2))))
1484 ;; Properties are case-insensitive.
1485 (should
1486 (let ((org-custom-properties '("FOO")))
1487 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
1488 (org-toggle-custom-properties-visibility)
1489 (org-invisible-p2))))
1490 (should
1491 (let ((org-custom-properties '("foo")))
1492 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1493 (org-toggle-custom-properties-visibility)
1494 (org-invisible-p2))))
1495 ;; Multiple custom properties in the same drawer.
1496 (should
1497 (let ((org-custom-properties '("FOO" "BAR")))
1498 (org-test-with-temp-text
1499 "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
1500 (org-toggle-custom-properties-visibility)
1501 (and (org-invisible-p2)
1502 (not (progn (forward-line) (org-invisible-p2)))
1503 (progn (forward-line) (org-invisible-p2))))))
1504 ;; Hide custom properties with an empty value.
1505 (should
1506 (let ((org-custom-properties '("FOO")))
1507 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
1508 (org-toggle-custom-properties-visibility)
1509 (org-invisible-p2))))
1510 ;; Do not hide fake properties.
1511 (should-not
1512 (let ((org-custom-properties '("FOO")))
1513 (org-test-with-temp-text ":FOO: val\n"
1514 (org-toggle-custom-properties-visibility)
1515 (org-invisible-p2))))
1516 (should-not
1517 (let ((org-custom-properties '("A")))
1518 (org-test-with-temp-text
1519 "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
1520 (org-toggle-custom-properties-visibility)
1521 (org-invisible-p2)))))
1525 ;;; Mark Region
1527 (ert-deftest test-org/mark-subtree ()
1528 "Test `org-mark-subtree' specifications."
1529 ;; Error when point is before first headline.
1530 (should-error
1531 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
1532 (progn (transient-mark-mode 1)
1533 (org-mark-subtree))))
1534 ;; Without argument, mark current subtree.
1535 (should
1536 (equal
1537 '(12 32)
1538 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1539 (progn (transient-mark-mode 1)
1540 (forward-line 2)
1541 (org-mark-subtree)
1542 (list (region-beginning) (region-end))))))
1543 ;; With an argument, move ARG up.
1544 (should
1545 (equal
1546 '(1 32)
1547 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1548 (progn (transient-mark-mode 1)
1549 (forward-line 2)
1550 (org-mark-subtree 1)
1551 (list (region-beginning) (region-end))))))
1552 ;; Do not get fooled by inlinetasks.
1553 (when (featurep 'org-inlinetask)
1554 (should
1555 (= 1
1556 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
1557 (progn (transient-mark-mode 1)
1558 (forward-line 1)
1559 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
1560 (region-beginning)))))))
1564 ;;; Navigation
1566 (ert-deftest test-org/end-of-meta-data ()
1567 "Test `org-end-of-meta-data' specifications."
1568 ;; Skip planning line.
1569 (should
1570 (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
1571 (org-end-of-meta-data)
1572 (eobp)))
1573 ;; Skip properties drawer.
1574 (should
1575 (org-test-with-temp-text
1576 "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
1577 (org-end-of-meta-data)
1578 (eobp)))
1579 ;; Skip both.
1580 (should
1581 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
1582 (org-end-of-meta-data)
1583 (eobp)))
1584 ;; Nothing to skip, go to first line.
1585 (should
1586 (org-test-with-temp-text "* Headline\nContents"
1587 (org-end-of-meta-data)
1588 (looking-at "Contents")))
1589 ;; With option argument, skip empty lines, regular drawers and
1590 ;; clocking lines.
1591 (should
1592 (org-test-with-temp-text "* Headline\n\nContents"
1593 (org-end-of-meta-data t)
1594 (looking-at "Contents")))
1595 (should
1596 (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
1597 (org-end-of-meta-data t)
1598 (looking-at "Contents")))
1599 (should
1600 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
1601 (org-end-of-meta-data t)
1602 (looking-at "Contents")))
1603 ;; Special case: do not skip incomplete drawers.
1604 (should
1605 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
1606 (org-end-of-meta-data t)
1607 (looking-at ":LOGBOOK:")))
1608 ;; Special case: Be careful about consecutive headlines.
1609 (should-not
1610 (org-test-with-temp-text "* H1\n*H2\nContents"
1611 (org-end-of-meta-data t)
1612 (looking-at "Contents"))))
1614 (ert-deftest test-org/beginning-of-line ()
1615 "Test `org-beginning-of-line' specifications."
1616 ;; Standard test.
1617 (should
1618 (org-test-with-temp-text "Some text\nSome other text"
1619 (progn (org-beginning-of-line) (bolp))))
1620 ;; Standard test with `visual-line-mode'.
1621 (should-not
1622 (org-test-with-temp-text "A long line of text\nSome other text"
1623 (progn (visual-line-mode)
1624 (forward-char 2)
1625 (dotimes (i 1000) (insert "very "))
1626 (org-beginning-of-line)
1627 (bolp))))
1628 ;; At an headline with special movement.
1629 (should
1630 (org-test-with-temp-text "* TODO Headline"
1631 (let ((org-special-ctrl-a/e t))
1632 (org-end-of-line)
1633 (and (progn (org-beginning-of-line) (looking-at "Headline"))
1634 (progn (org-beginning-of-line) (bolp))
1635 (progn (org-beginning-of-line) (looking-at "Headline")))))))
1637 (ert-deftest test-org/end-of-line ()
1638 "Test `org-end-of-line' specifications."
1639 ;; Standard test.
1640 (should
1641 (org-test-with-temp-text "Some text\nSome other text"
1642 (progn (org-end-of-line) (eolp))))
1643 ;; Standard test with `visual-line-mode'.
1644 (should-not
1645 (org-test-with-temp-text "A long line of text\nSome other text"
1646 (progn (visual-line-mode)
1647 (forward-char 2)
1648 (dotimes (i 1000) (insert "very "))
1649 (goto-char (point-min))
1650 (org-end-of-line)
1651 (eolp))))
1652 ;; At an headline with special movement.
1653 (should
1654 (org-test-with-temp-text "* Headline1 :tag:\n"
1655 (let ((org-special-ctrl-a/e t))
1656 (and (progn (org-end-of-line) (looking-at " :tag:"))
1657 (progn (org-end-of-line) (eolp))
1658 (progn (org-end-of-line) (looking-at " :tag:"))))))
1659 ;; At an headline without special movement.
1660 (should
1661 (org-test-with-temp-text "* Headline2 :tag:\n"
1662 (let ((org-special-ctrl-a/e nil))
1663 (and (progn (org-end-of-line) (eolp))
1664 (progn (org-end-of-line) (eolp))))))
1665 ;; At an headline, with reversed movement.
1666 (should
1667 (org-test-with-temp-text "* Headline3 :tag:\n"
1668 (let ((org-special-ctrl-a/e 'reversed)
1669 (this-command last-command))
1670 (and (progn (org-end-of-line) (eolp))
1671 (progn (org-end-of-line) (looking-at " :tag:"))))))
1672 ;; At a block without hidden contents.
1673 (should
1674 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1675 (progn (org-end-of-line) (eolp))))
1676 ;; At a block with hidden contents.
1677 (should-not
1678 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1679 (let ((org-special-ctrl-a/e t))
1680 (org-hide-block-toggle)
1681 (org-end-of-line)
1682 (eobp)))))
1684 (ert-deftest test-org/forward-paragraph ()
1685 "Test `org-forward-paragraph' specifications."
1686 ;; At end of buffer, return an error.
1687 (should-error
1688 (org-test-with-temp-text "Paragraph"
1689 (goto-char (point-max))
1690 (org-forward-paragraph)))
1691 ;; Standard test.
1692 (should
1693 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1694 (org-forward-paragraph)
1695 (looking-at "P2")))
1696 ;; Ignore depth.
1697 (should
1698 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
1699 (org-forward-paragraph)
1700 (looking-at "P1")))
1701 ;; Do not enter elements with invisible contents.
1702 (should
1703 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
1704 (org-hide-block-toggle)
1705 (org-forward-paragraph)
1706 (looking-at "P3")))
1707 ;; On an affiliated keyword, jump to the beginning of the element.
1708 (should
1709 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
1710 (org-forward-paragraph)
1711 (looking-at "Para")))
1712 ;; On an item or a footnote definition, move to the second element
1713 ;; inside, if any.
1714 (should
1715 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
1716 (org-forward-paragraph)
1717 (looking-at " Paragraph")))
1718 (should
1719 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
1720 (org-forward-paragraph)
1721 (looking-at "Paragraph")))
1722 ;; On an item, or a footnote definition, when the first line is
1723 ;; empty, move to the first item.
1724 (should
1725 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
1726 (org-forward-paragraph)
1727 (looking-at " Paragraph")))
1728 (should
1729 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
1730 (org-forward-paragraph)
1731 (looking-at "Paragraph")))
1732 ;; On a table (resp. a property drawer) do not move through table
1733 ;; rows (resp. node properties).
1734 (should
1735 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
1736 (org-forward-paragraph)
1737 (looking-at "Paragraph")))
1738 (should
1739 (org-test-with-temp-text
1740 "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
1741 (org-forward-paragraph)
1742 (looking-at "Paragraph")))
1743 ;; On a verse or source block, stop after blank lines.
1744 (should
1745 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
1746 (org-forward-paragraph)
1747 (looking-at "L2")))
1748 (should
1749 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
1750 (org-forward-paragraph)
1751 (looking-at "L2"))))
1753 (ert-deftest test-org/backward-paragraph ()
1754 "Test `org-backward-paragraph' specifications."
1755 ;; Error at beginning of buffer.
1756 (should-error
1757 (org-test-with-temp-text "Paragraph"
1758 (org-backward-paragraph)))
1759 ;; Regular test.
1760 (should
1761 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1762 (goto-char (point-max))
1763 (org-backward-paragraph)
1764 (looking-at "P3")))
1765 (should
1766 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1767 (goto-char (point-max))
1768 (beginning-of-line)
1769 (org-backward-paragraph)
1770 (looking-at "P2")))
1771 ;; Ignore depth.
1772 (should
1773 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
1774 (goto-char (point-max))
1775 (beginning-of-line)
1776 (org-backward-paragraph)
1777 (looking-at "P2")))
1778 ;; Ignore invisible elements.
1779 (should
1780 (org-test-with-temp-text "* H1\n P1\n* H2"
1781 (org-cycle)
1782 (goto-char (point-max))
1783 (beginning-of-line)
1784 (org-backward-paragraph)
1785 (bobp)))
1786 ;; On an affiliated keyword, jump to the first one.
1787 (should
1788 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
1789 (search-forward "c2")
1790 (org-backward-paragraph)
1791 (looking-at "#\\+name")))
1792 ;; On the second element in an item or a footnote definition, jump
1793 ;; to item or the definition.
1794 (should
1795 (org-test-with-temp-text "- line1\n\n line2"
1796 (goto-char (point-max))
1797 (beginning-of-line)
1798 (org-backward-paragraph)
1799 (looking-at "- line1")))
1800 (should
1801 (org-test-with-temp-text "[fn:1] line1\n\n line2"
1802 (goto-char (point-max))
1803 (beginning-of-line)
1804 (org-backward-paragraph)
1805 (looking-at "\\[fn:1\\] line1")))
1806 ;; On a table (resp. a property drawer), ignore table rows
1807 ;; (resp. node properties).
1808 (should
1809 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
1810 (goto-char (point-max))
1811 (beginning-of-line)
1812 (org-backward-paragraph)
1813 (bobp)))
1814 (should
1815 (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
1816 (org-backward-paragraph)
1817 (looking-at ":PROPERTIES:")))
1818 ;; On a source or verse block, stop before blank lines.
1819 (should
1820 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
1821 (search-forward "L3")
1822 (beginning-of-line)
1823 (org-backward-paragraph)
1824 (looking-at "L2")))
1825 (should
1826 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
1827 (search-forward "L3")
1828 (beginning-of-line)
1829 (org-backward-paragraph)
1830 (looking-at "L2"))))
1832 (ert-deftest test-org/forward-element ()
1833 "Test `org-forward-element' specifications."
1834 ;; 1. At EOB: should error.
1835 (org-test-with-temp-text "Some text\n"
1836 (goto-char (point-max))
1837 (should-error (org-forward-element)))
1838 ;; 2. Standard move: expected to ignore blank lines.
1839 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
1840 (org-forward-element)
1841 (should (looking-at (regexp-quote "Second paragraph."))))
1842 ;; 3. Headline tests.
1843 (org-test-with-temp-text "
1844 * Head 1
1845 ** Head 1.1
1846 *** Head 1.1.1
1847 ** Head 1.2"
1848 ;; 3.1. At an headline beginning: move to next headline at the
1849 ;; same level.
1850 (goto-line 3)
1851 (org-forward-element)
1852 (should (looking-at (regexp-quote "** Head 1.2")))
1853 ;; 3.2. At an headline beginning: move to parent headline if no
1854 ;; headline at the same level.
1855 (goto-line 3)
1856 (org-forward-element)
1857 (should (looking-at (regexp-quote "** Head 1.2"))))
1858 ;; 4. Greater element tests.
1859 (org-test-with-temp-text
1860 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
1861 ;; 4.1. At a greater element: expected to skip contents.
1862 (org-forward-element)
1863 (should (looking-at (regexp-quote "Outside.")))
1864 ;; 4.2. At the end of greater element contents: expected to skip
1865 ;; to the end of the greater element.
1866 (goto-line 2)
1867 (org-forward-element)
1868 (should (looking-at (regexp-quote "Outside."))))
1869 ;; 5. List tests.
1870 (org-test-with-temp-text "
1871 - item1
1873 - sub1
1875 - sub2
1877 - sub3
1879 Inner paragraph.
1881 - item2
1883 Outside."
1884 ;; 5.1. At list top point: expected to move to the element after
1885 ;; the list.
1886 (goto-line 2)
1887 (org-forward-element)
1888 (should (looking-at (regexp-quote "Outside.")))
1889 ;; 5.2. Special case: at the first line of a sub-list, but not at
1890 ;; beginning of line, move to next item.
1891 (goto-line 2)
1892 (forward-char)
1893 (org-forward-element)
1894 (should (looking-at "- item2"))
1895 (goto-line 4)
1896 (forward-char)
1897 (org-forward-element)
1898 (should (looking-at " - sub2"))
1899 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
1900 (goto-line 4)
1901 (org-forward-element)
1902 (should (looking-at (regexp-quote " Inner paragraph.")))
1903 ;; 5.4. At sub-list end: expected to move outside the sub-list.
1904 (goto-line 8)
1905 (org-forward-element)
1906 (should (looking-at (regexp-quote " Inner paragraph.")))
1907 ;; 5.5. At an item: expected to move to next item, if any.
1908 (goto-line 6)
1909 (org-forward-element)
1910 (should (looking-at " - sub3"))))
1912 (ert-deftest test-org/backward-element ()
1913 "Test `org-backward-element' specifications."
1914 ;; 1. Should error at BOB.
1915 (org-test-with-temp-text " \nParagraph."
1916 (should-error (org-backward-element)))
1917 ;; 2. Should move at BOB when called on the first element in buffer.
1918 (should
1919 (org-test-with-temp-text "\n#+TITLE: test"
1920 (progn (forward-line)
1921 (org-backward-element)
1922 (bobp))))
1923 ;; 3. Not at the beginning of an element: move at its beginning.
1924 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
1925 (goto-line 3)
1926 (end-of-line)
1927 (org-backward-element)
1928 (should (looking-at (regexp-quote "Paragraph2."))))
1929 ;; 4. Headline tests.
1930 (org-test-with-temp-text "
1931 * Head 1
1932 ** Head 1.1
1933 *** Head 1.1.1
1934 ** Head 1.2"
1935 ;; 4.1. At an headline beginning: move to previous headline at the
1936 ;; same level.
1937 (goto-line 5)
1938 (org-backward-element)
1939 (should (looking-at (regexp-quote "** Head 1.1")))
1940 ;; 4.2. At an headline beginning: move to parent headline if no
1941 ;; headline at the same level.
1942 (goto-line 3)
1943 (org-backward-element)
1944 (should (looking-at (regexp-quote "* Head 1")))
1945 ;; 4.3. At the first top-level headline: should error.
1946 (goto-line 2)
1947 (should-error (org-backward-element)))
1948 ;; 5. At beginning of first element inside a greater element:
1949 ;; expected to move to greater element's beginning.
1950 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
1951 (goto-line 3)
1952 (org-backward-element)
1953 (should (looking-at "#\\+BEGIN_CENTER")))
1954 ;; 6. At the beginning of the first element in a section: should
1955 ;; move back to headline, if any.
1956 (should
1957 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
1958 (progn (goto-char (point-max))
1959 (beginning-of-line)
1960 (org-backward-element)
1961 (org-at-heading-p))))
1962 ;; 7. List tests.
1963 (org-test-with-temp-text "
1964 - item1
1966 - sub1
1968 - sub2
1970 - sub3
1972 Inner paragraph.
1974 - item2
1977 Outside."
1978 ;; 7.1. At beginning of sub-list: expected to move to the
1979 ;; paragraph before it.
1980 (goto-line 4)
1981 (org-backward-element)
1982 (should (looking-at "item1"))
1983 ;; 7.2. At an item in a list: expected to move at previous item.
1984 (goto-line 8)
1985 (org-backward-element)
1986 (should (looking-at " - sub2"))
1987 (goto-line 12)
1988 (org-backward-element)
1989 (should (looking-at "- item1"))
1990 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
1991 ;; beginning.
1992 (goto-line 10)
1993 (org-backward-element)
1994 (should (looking-at " - sub1"))
1995 (goto-line 15)
1996 (org-backward-element)
1997 (should (looking-at "- item1"))
1998 ;; 7.4. At blank-lines before list end: expected to move to top
1999 ;; item.
2000 (goto-line 14)
2001 (org-backward-element)
2002 (should (looking-at "- item1"))))
2004 (ert-deftest test-org/up-element ()
2005 "Test `org-up-element' specifications."
2006 ;; 1. At BOB or with no surrounding element: should error.
2007 (org-test-with-temp-text "Paragraph."
2008 (should-error (org-up-element)))
2009 (org-test-with-temp-text "* Head1\n* Head2"
2010 (goto-line 2)
2011 (should-error (org-up-element)))
2012 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2013 (goto-line 3)
2014 (should-error (org-up-element)))
2015 ;; 2. At an headline: move to parent headline.
2016 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
2017 (goto-line 3)
2018 (org-up-element)
2019 (should (looking-at "\\* Head1")))
2020 ;; 3. Inside a greater element: move to greater element beginning.
2021 (org-test-with-temp-text
2022 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
2023 (goto-line 3)
2024 (org-up-element)
2025 (should (looking-at "#\\+BEGIN_CENTER")))
2026 ;; 4. List tests.
2027 (org-test-with-temp-text "* Top
2028 - item1
2030 - sub1
2032 - sub2
2034 Paragraph within sub2.
2036 - item2"
2037 ;; 4.1. Within an item: move to the item beginning.
2038 (goto-line 8)
2039 (org-up-element)
2040 (should (looking-at " - sub2"))
2041 ;; 4.2. At an item in a sub-list: move to parent item.
2042 (goto-line 4)
2043 (org-up-element)
2044 (should (looking-at "- item1"))
2045 ;; 4.3. At an item in top list: move to beginning of whole list.
2046 (goto-line 10)
2047 (org-up-element)
2048 (should (looking-at "- item1"))
2049 ;; 4.4. Special case. At very top point: should move to parent of
2050 ;; list.
2051 (goto-line 2)
2052 (org-up-element)
2053 (should (looking-at "\\* Top"))))
2055 (ert-deftest test-org/down-element ()
2056 "Test `org-down-element' specifications."
2057 ;; Error when the element hasn't got a recursive type.
2058 (org-test-with-temp-text "Paragraph."
2059 (should-error (org-down-element)))
2060 ;; Error when the element has no contents
2061 (org-test-with-temp-text "* Headline"
2062 (should-error (org-down-element)))
2063 ;; When at a plain-list, move to first item.
2064 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
2065 (goto-line 2)
2066 (org-down-element)
2067 (should (looking-at " - Item 1.1")))
2068 (org-test-with-temp-text "#+NAME: list\n- Item 1"
2069 (org-down-element)
2070 (should (looking-at " Item 1")))
2071 ;; When at a table, move to first row
2072 (org-test-with-temp-text "#+NAME: table\n| a | b |"
2073 (org-down-element)
2074 (should (looking-at " a | b |")))
2075 ;; Otherwise, move inside the greater element.
2076 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
2077 (org-down-element)
2078 (should (looking-at "Paragraph"))))
2080 (ert-deftest test-org/drag-element-backward ()
2081 "Test `org-drag-element-backward' specifications."
2082 ;; Standard test.
2083 (should
2084 (equal
2085 "#+key2: val2\n#+key1: val1\n#+key3: val3"
2086 (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
2087 (org-drag-element-backward)
2088 (buffer-string))))
2089 (should
2090 (equal
2091 "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
2092 (org-test-with-temp-text
2093 "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
2094 (org-drag-element-backward)
2095 (buffer-string))))
2096 ;; Preserve blank lines.
2097 (should
2098 (equal "Paragraph 2\n\n\nPara1\n\nPara3"
2099 (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
2100 (org-drag-element-backward)
2101 (buffer-string))))
2102 ;; Preserve column.
2103 (should
2104 (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
2105 (org-drag-element-backward)
2106 (org-looking-at-p "2")))
2107 ;; Error when trying to move first element of buffer.
2108 (should-error
2109 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2110 (org-drag-element-backward)))
2111 ;; Error when trying to swap nested elements.
2112 (should-error
2113 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2114 (forward-line)
2115 (org-drag-element-backward)))
2116 ;; Error when trying to swap an headline element and a non-headline
2117 ;; element.
2118 (should-error
2119 (org-test-with-temp-text "Test.\n* Head 1"
2120 (forward-line)
2121 (org-drag-element-backward)))
2122 ;; Preserve visibility of elements and their contents.
2123 (should
2124 (equal '((63 . 82) (26 . 48))
2125 (org-test-with-temp-text "
2126 #+BEGIN_CENTER
2127 Text.
2128 #+END_CENTER
2129 - item 1
2130 #+BEGIN_QUOTE
2131 Text.
2132 #+END_QUOTE"
2133 (while (search-forward "BEGIN_" nil t) (org-cycle))
2134 (search-backward "- item 1")
2135 (org-drag-element-backward)
2136 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2137 (overlays-in (point-min) (point-max)))))))
2139 (ert-deftest test-org/drag-element-forward ()
2140 "Test `org-drag-element-forward' specifications."
2141 ;; 1. Error when trying to move first element of buffer.
2142 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2143 (goto-line 3)
2144 (should-error (org-drag-element-forward)))
2145 ;; 2. Error when trying to swap nested elements.
2146 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2147 (forward-line)
2148 (should-error (org-drag-element-forward)))
2149 ;; 3. Error when trying to swap a non-headline element and an
2150 ;; headline.
2151 (org-test-with-temp-text "Test.\n* Head 1"
2152 (should-error (org-drag-element-forward)))
2153 ;; 4. Otherwise, swap elements, preserving column and blank lines
2154 ;; between elements.
2155 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
2156 (search-forward "graph")
2157 (org-drag-element-forward)
2158 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
2159 (should (looking-at " 1")))
2160 ;; 5. Preserve visibility of elements and their contents.
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 "#+BEGIN_CENTER")
2171 (org-drag-element-forward)
2172 (should
2173 (equal
2174 '((63 . 82) (26 . 48))
2175 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2176 (overlays-in (point-min) (point-max)))))))
2179 ;;; Outline structure
2181 (ert-deftest test-org/demote ()
2182 "Test `org-demote' specifications."
2183 ;; Add correct number of stars according to `org-odd-levels-only'.
2184 (should
2185 (= 2
2186 (org-test-with-temp-text "* H"
2187 (let ((org-odd-levels-only nil)) (org-demote))
2188 (org-current-level))))
2189 (should
2190 (= 3
2191 (org-test-with-temp-text "* H"
2192 (let ((org-odd-levels-only t)) (org-demote))
2193 (org-current-level))))
2194 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2195 (should
2196 (org-test-with-temp-text "* H :tag:"
2197 (let ((org-tags-column 10)
2198 (org-auto-align-tags t)
2199 (org-odd-levels-only nil))
2200 (org-demote))
2201 (org-move-to-column 10)
2202 (org-looking-at-p ":tag:$")))
2203 (should-not
2204 (org-test-with-temp-text "* H :tag:"
2205 (let ((org-tags-column 10)
2206 (org-auto-align-tags nil)
2207 (org-odd-levels-only nil))
2208 (org-demote))
2209 (org-move-to-column 10)
2210 (org-looking-at-p ":tag:$")))
2211 ;; When `org-adapt-indentation' is non-nil, always indent planning
2212 ;; info and property drawers accordingly.
2213 (should
2214 (= 3
2215 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2216 (let ((org-odd-levels-only nil)
2217 (org-adapt-indentation t))
2218 (org-demote))
2219 (forward-line)
2220 (org-get-indentation))))
2221 (should
2222 (= 3
2223 (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2224 (let ((org-odd-levels-only nil)
2225 (org-adapt-indentation t))
2226 (org-demote))
2227 (forward-line)
2228 (org-get-indentation))))
2229 (should-not
2230 (= 3
2231 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2232 (let ((org-odd-levels-only nil)
2233 (org-adapt-indentation nil))
2234 (org-demote))
2235 (forward-line)
2236 (org-get-indentation))))
2237 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2238 ;; section accordingly. Ignore, however, footnote definitions and
2239 ;; inlinetasks boundaries.
2240 (should
2241 (= 3
2242 (org-test-with-temp-text "* H\n Paragraph"
2243 (let ((org-odd-levels-only nil)
2244 (org-adapt-indentation t))
2245 (org-demote))
2246 (forward-line)
2247 (org-get-indentation))))
2248 (should
2249 (= 2
2250 (org-test-with-temp-text "* H\n Paragraph"
2251 (let ((org-odd-levels-only nil)
2252 (org-adapt-indentation nil))
2253 (org-demote))
2254 (forward-line)
2255 (org-get-indentation))))
2256 (should
2257 (zerop
2258 (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
2259 (let ((org-odd-levels-only nil)
2260 (org-adapt-indentation t))
2261 (org-demote))
2262 (goto-char (point-max))
2263 (org-get-indentation))))
2264 (should
2265 (= 3
2266 (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
2267 (let ((org-odd-levels-only nil)
2268 (org-adapt-indentation t))
2269 (org-demote))
2270 (goto-char (point-max))
2271 (org-get-indentation))))
2272 (when (featurep 'org-inlinetask)
2273 (should
2274 (zerop
2275 (let ((org-inlinetask-min-level 5)
2276 (org-adapt-indentation t))
2277 (org-test-with-temp-text "* H\n***** I\n***** END"
2278 (org-demote)
2279 (forward-line)
2280 (org-get-indentation))))))
2281 (when (featurep 'org-inlinetask)
2282 (should
2283 (= 3
2284 (let ((org-inlinetask-min-level 5)
2285 (org-adapt-indentation t))
2286 (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
2287 (org-demote)
2288 (forward-line 2)
2289 (org-get-indentation))))))
2290 ;; Ignore contents of source blocks or example blocks when
2291 ;; indentation should be preserved (through
2292 ;; `org-src-preserve-indentation' or "-i" flag).
2293 (should-not
2294 (zerop
2295 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2296 (let ((org-adapt-indentation t)
2297 (org-src-preserve-indentation nil))
2298 (org-demote))
2299 (forward-line 2)
2300 (org-get-indentation))))
2301 (should
2302 (zerop
2303 (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2304 (let ((org-adapt-indentation t)
2305 (org-src-preserve-indentation t))
2306 (org-demote))
2307 (forward-line 2)
2308 (org-get-indentation))))
2309 (should
2310 (zerop
2311 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2312 (let ((org-adapt-indentation t)
2313 (org-src-preserve-indentation t))
2314 (org-demote))
2315 (forward-line 2)
2316 (org-get-indentation))))
2317 (should
2318 (zerop
2319 (org-test-with-temp-text
2320 "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
2321 (let ((org-adapt-indentation t)
2322 (org-src-preserve-indentation nil))
2323 (org-demote))
2324 (forward-line 2)
2325 (org-get-indentation)))))
2327 (ert-deftest test-org/promote ()
2328 "Test `org-promote' specifications."
2329 ;; Return an error if headline is to be promoted to level 0, unless
2330 ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
2331 ;; headline becomes a comment.
2332 (should-error
2333 (org-test-with-temp-text "* H"
2334 (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
2335 (should
2336 (equal "# H"
2337 (org-test-with-temp-text "* H"
2338 (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
2339 (buffer-string))))
2340 ;; Remove correct number of stars according to
2341 ;; `org-odd-levels-only'.
2342 (should
2343 (= 2
2344 (org-test-with-temp-text "*** H"
2345 (let ((org-odd-levels-only nil)) (org-promote))
2346 (org-current-level))))
2347 (should
2348 (= 1
2349 (org-test-with-temp-text "*** H"
2350 (let ((org-odd-levels-only t)) (org-promote))
2351 (org-current-level))))
2352 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2353 (should
2354 (org-test-with-temp-text "** H :tag:"
2355 (let ((org-tags-column 10)
2356 (org-auto-align-tags t)
2357 (org-odd-levels-only nil))
2358 (org-promote))
2359 (org-move-to-column 10)
2360 (org-looking-at-p ":tag:$")))
2361 (should-not
2362 (org-test-with-temp-text "** H :tag:"
2363 (let ((org-tags-column 10)
2364 (org-auto-align-tags nil)
2365 (org-odd-levels-only nil))
2366 (org-promote))
2367 (org-move-to-column 10)
2368 (org-looking-at-p ":tag:$")))
2369 ;; When `org-adapt-indentation' is non-nil, always indent planning
2370 ;; info and property drawers.
2371 (should
2372 (= 2
2373 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2374 (let ((org-odd-levels-only nil)
2375 (org-adapt-indentation t))
2376 (org-promote))
2377 (forward-line)
2378 (org-get-indentation))))
2379 (should
2380 (= 2
2381 (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2382 (let ((org-odd-levels-only nil)
2383 (org-adapt-indentation t))
2384 (org-promote))
2385 (forward-line)
2386 (org-get-indentation))))
2387 (should-not
2388 (= 2
2389 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2390 (let ((org-odd-levels-only nil)
2391 (org-adapt-indentation nil))
2392 (org-promote))
2393 (forward-line)
2394 (org-get-indentation))))
2395 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2396 ;; section accordingly. Ignore, however, footnote definitions and
2397 ;; inlinetasks boundaries.
2398 (should
2399 (= 2
2400 (org-test-with-temp-text "** H\n Paragraph"
2401 (let ((org-odd-levels-only nil)
2402 (org-adapt-indentation t))
2403 (org-promote))
2404 (forward-line)
2405 (org-get-indentation))))
2406 (should-not
2407 (= 2
2408 (org-test-with-temp-text "** H\n Paragraph"
2409 (let ((org-odd-levels-only nil)
2410 (org-adapt-indentation nil))
2411 (org-promote))
2412 (forward-line)
2413 (org-get-indentation))))
2414 (should
2415 (= 2
2416 (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
2417 (let ((org-odd-levels-only nil)
2418 (org-adapt-indentation t))
2419 (org-promote))
2420 (forward-line)
2421 (org-get-indentation))))
2422 (when (featurep 'org-inlinetask)
2423 (should
2424 (zerop
2425 (let ((org-inlinetask-min-level 5)
2426 (org-adapt-indentation t))
2427 (org-test-with-temp-text "** H\n***** I\n***** END"
2428 (org-promote)
2429 (forward-line)
2430 (org-get-indentation))))))
2431 (when (featurep 'org-inlinetask)
2432 (should
2433 (= 2
2434 (let ((org-inlinetask-min-level 5)
2435 (org-adapt-indentation t))
2436 (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
2437 (org-promote)
2438 (forward-line 2)
2439 (org-get-indentation))))))
2440 ;; Give up shifting if it would break document's structure
2441 ;; otherwise.
2442 (should
2443 (= 3
2444 (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
2445 (let ((org-odd-levels-only nil)
2446 (org-adapt-indentation t))
2447 (org-promote))
2448 (forward-line)
2449 (org-get-indentation))))
2450 (should
2451 (= 3
2452 (org-test-with-temp-text "** H\n Paragraph\n * list."
2453 (let ((org-odd-levels-only nil)
2454 (org-adapt-indentation t))
2455 (org-promote))
2456 (forward-line)
2457 (org-get-indentation))))
2458 ;; Ignore contents of source blocks or example blocks when
2459 ;; indentation should be preserved (through
2460 ;; `org-src-preserve-indentation' or "-i" flag).
2461 (should-not
2462 (zerop
2463 (org-test-with-temp-text
2464 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2465 (let ((org-adapt-indentation t)
2466 (org-src-preserve-indentation nil)
2467 (org-odd-levels-only nil))
2468 (org-promote))
2469 (forward-line)
2470 (org-get-indentation))))
2471 (should
2472 (zerop
2473 (org-test-with-temp-text
2474 "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
2475 (let ((org-adapt-indentation t)
2476 (org-src-preserve-indentation t)
2477 (org-odd-levels-only nil))
2478 (org-promote))
2479 (forward-line)
2480 (org-get-indentation))))
2481 (should
2482 (zerop
2483 (org-test-with-temp-text
2484 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2485 (let ((org-adapt-indentation t)
2486 (org-src-preserve-indentation t)
2487 (org-odd-levels-only nil))
2488 (org-promote))
2489 (forward-line)
2490 (org-get-indentation))))
2491 (should
2492 (zerop
2493 (org-test-with-temp-text
2494 "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
2495 (let ((org-adapt-indentation t)
2496 (org-src-preserve-indentation nil)
2497 (org-odd-levels-only nil))
2498 (org-promote))
2499 (forward-line)
2500 (org-get-indentation)))))
2503 ;;; Planning
2505 (ert-deftest test-org/at-planning-p ()
2506 "Test `org-at-planning-p' specifications."
2507 ;; Regular test.
2508 (should
2509 (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
2510 (org-at-planning-p)))
2511 (should-not
2512 (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
2513 (org-at-planning-p)))
2514 ;; Correctly find planning attached to inlinetasks.
2515 (when (featurep 'org-inlinetask)
2516 (should
2517 (org-test-with-temp-text
2518 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
2519 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2520 (should-not
2521 (org-test-with-temp-text
2522 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
2523 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2524 (should-not
2525 (org-test-with-temp-text
2526 "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
2527 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2528 (should-not
2529 (org-test-with-temp-text
2530 "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
2531 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
2534 ;;; Property API
2536 (ert-deftest test-org/buffer-property-keys ()
2537 "Test `org-buffer-property-keys' specifications."
2538 ;; Retrieve properties accross siblings.
2539 (should
2540 (equal '("A" "B")
2541 (org-test-with-temp-text "
2542 * H1
2543 :PROPERTIES:
2544 :A: 1
2545 :END:
2546 * H2
2547 :PROPERTIES:
2548 :B: 1
2549 :END:"
2550 (org-buffer-property-keys))))
2551 ;; Retrieve properties accross children.
2552 (should
2553 (equal '("A" "B")
2554 (org-test-with-temp-text "
2555 * H1
2556 :PROPERTIES:
2557 :A: 1
2558 :END:
2559 ** H2
2560 :PROPERTIES:
2561 :B: 1
2562 :END:"
2563 (org-buffer-property-keys))))
2564 ;; Retrieve muliple properties in the same drawer.
2565 (should
2566 (equal '("A" "B")
2567 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2568 (org-buffer-property-keys))))
2569 ;; Ignore extension symbol in property name.
2570 (should
2571 (equal '("A")
2572 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
2573 (org-buffer-property-keys))))
2574 ;; With non-nil COLUMNS, extract property names from columns.
2575 (should
2576 (equal '("A" "B")
2577 (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
2578 (org-buffer-property-keys nil nil t))))
2579 (should
2580 (equal '("A" "B" "COLUMNS")
2581 (org-test-with-temp-text
2582 "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
2583 (org-buffer-property-keys nil nil t)))))
2585 (ert-deftest test-org/property-values ()
2586 "Test `org-property-values' specifications."
2587 ;; Regular test.
2588 (should
2589 (equal '("2" "1")
2590 (org-test-with-temp-text
2591 "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
2592 (org-property-values "A"))))
2593 ;; Ignore empty values.
2594 (should-not
2595 (org-test-with-temp-text
2596 "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
2597 (org-property-values "A")))
2598 ;; Take into consideration extended values.
2599 (should
2600 (equal '("1 2")
2601 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
2602 (org-property-values "A")))))
2604 (ert-deftest test-org/entry-delete ()
2605 "Test `org-entry-delete' specifications."
2606 ;; Regular test.
2607 (should
2608 (string-match
2609 " *:PROPERTIES:\n *:B: +2\n *:END:"
2610 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2611 (org-entry-delete (point) "A")
2612 (buffer-string))))
2613 ;; When last property is removed, remove the property drawer.
2614 (should-not
2615 (string-match
2616 ":PROPERTIES:"
2617 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
2618 (org-entry-delete (point) "A")
2619 (buffer-string)))))
2621 (ert-deftest test-org/entry-get ()
2622 "Test `org-entry-get' specifications."
2623 ;; Regular test.
2624 (should
2625 (equal "1"
2626 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2627 (org-entry-get (point) "A"))))
2628 ;; Ignore case.
2629 (should
2630 (equal "1"
2631 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2632 (org-entry-get (point) "a"))))
2633 ;; Handle extended values, both before and after base value.
2634 (should
2635 (equal "1 2 3"
2636 (org-test-with-temp-text
2637 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
2638 (org-entry-get (point) "A"))))
2639 ;; Empty values are returned as the empty string.
2640 (should
2641 (equal ""
2642 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
2643 (org-entry-get (point) "A"))))
2644 ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
2645 ;; otherwise, return nil.
2646 (should-not
2647 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
2648 (org-entry-get (point) "A")))
2649 (should
2650 (equal "nil"
2651 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
2652 (org-entry-get (point) "A" nil t))))
2653 ;; Return nil when no property is found, independently on the
2654 ;; LITERAL-NIL argument.
2655 (should-not
2656 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2657 (org-entry-get (point) "B")))
2658 (should-not
2659 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2660 (org-entry-get (point) "B" nil t)))
2661 ;; Handle inheritance, when allowed.
2662 (should
2663 (equal
2665 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2666 (org-entry-get (point) "A" t))))
2667 (should
2668 (equal
2670 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2671 (let ((org-use-property-inheritance t))
2672 (org-entry-get (point) "A" 'selective)))))
2673 (should-not
2674 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2675 (let ((org-use-property-inheritance nil))
2676 (org-entry-get (point) "A" 'selective)))))
2678 (ert-deftest test-org/entry-properties ()
2679 "Test `org-entry-properties' specifications."
2680 ;; Get "ITEM" property.
2681 (should
2682 (equal "* H"
2683 (org-test-with-temp-text "* TODO H"
2684 (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
2685 (should
2686 (equal "* H"
2687 (org-test-with-temp-text "* TODO H"
2688 (cdr (assoc "ITEM" (org-entry-properties))))))
2689 ;; Get "TODO" property.
2690 (should
2691 (equal "TODO"
2692 (org-test-with-temp-text "* TODO H"
2693 (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
2694 (should
2695 (equal "TODO"
2696 (org-test-with-temp-text "* TODO H"
2697 (cdr (assoc "TODO" (org-entry-properties))))))
2698 (should-not
2699 (org-test-with-temp-text "* H"
2700 (assoc "TODO" (org-entry-properties nil "TODO"))))
2701 ;; Get "PRIORITY" property.
2702 (should
2703 (equal "A"
2704 (org-test-with-temp-text "* [#A] H"
2705 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
2706 (should
2707 (equal "A"
2708 (org-test-with-temp-text "* [#A] H"
2709 (cdr (assoc "PRIORITY" (org-entry-properties))))))
2710 (should-not
2711 (org-test-with-temp-text "* H"
2712 (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
2713 ;; Get "FILE" property.
2714 (should
2715 (org-test-with-temp-text-in-file "* H\nParagraph"
2716 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
2717 (buffer-file-name))))
2718 (should
2719 (org-test-with-temp-text-in-file "* H\nParagraph"
2720 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
2721 (buffer-file-name))))
2722 (should-not
2723 (org-test-with-temp-text "* H\nParagraph"
2724 (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
2725 ;; Get "TAGS" property.
2726 (should
2727 (equal ":tag1:tag2:"
2728 (org-test-with-temp-text "* H :tag1:tag2:"
2729 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
2730 (should
2731 (equal ":tag1:tag2:"
2732 (org-test-with-temp-text "* H :tag1:tag2:"
2733 (cdr (assoc "TAGS" (org-entry-properties))))))
2734 (should-not
2735 (org-test-with-temp-text "* H"
2736 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
2737 ;; Get "ALLTAGS" property.
2738 (should
2739 (equal ":tag1:tag2:"
2740 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
2741 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
2742 (should
2743 (equal ":tag1:tag2:"
2744 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
2745 (cdr (assoc "ALLTAGS" (org-entry-properties))))))
2746 (should-not
2747 (org-test-with-temp-text "* H"
2748 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
2749 ;; Get "BLOCKED" property.
2750 (should
2751 (equal "t"
2752 (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
2753 (let ((org-enforce-todo-dependencies t)
2754 (org-blocker-hook
2755 '(org-block-todo-from-children-or-siblings-or-parent)))
2756 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
2757 (should
2758 (equal "t"
2759 (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
2760 (let ((org-enforce-todo-dependencies t)
2761 (org-blocker-hook
2762 '(org-block-todo-from-children-or-siblings-or-parent)))
2763 (cdr (assoc "BLOCKED" (org-entry-properties)))))))
2764 (should
2765 (equal ""
2766 (org-test-with-temp-text "* Blocked\n** DONE one\n** DONE two"
2767 (let ((org-enforce-todo-dependencies t))
2768 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
2769 ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
2770 (should
2771 (equal
2772 "[2012-03-29 thu.]"
2773 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
2774 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
2775 (should
2776 (equal
2777 "[2012-03-29 thu.]"
2778 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
2779 (cdr (assoc "CLOSED" (org-entry-properties))))))
2780 (should-not
2781 (org-test-with-temp-text "* H"
2782 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
2783 (should
2784 (equal
2785 "<2014-03-04 tue.>"
2786 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
2787 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
2788 (should
2789 (equal
2790 "<2014-03-04 tue.>"
2791 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
2792 (cdr (assoc "DEADLINE" (org-entry-properties))))))
2793 (should-not
2794 (org-test-with-temp-text "* H"
2795 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
2796 (should
2797 (equal
2798 "<2014-03-04 tue.>"
2799 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2800 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
2801 (should
2802 (equal
2803 "<2014-03-04 tue.>"
2804 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2805 (cdr (assoc "SCHEDULED" (org-entry-properties))))))
2806 (should-not
2807 (org-test-with-temp-text "* H"
2808 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
2809 ;; Get "CATEGORY"
2810 (should
2811 (equal "cat"
2812 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
2813 (cdr (assoc "CATEGORY" (org-entry-properties))))))
2814 (should
2815 (equal "cat"
2816 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
2817 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
2818 (should
2819 (equal "cat"
2820 (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
2821 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
2822 ;; Get standard properties.
2823 (should
2824 (equal "1"
2825 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2826 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
2827 ;; Handle extended properties.
2828 (should
2829 (equal "1 2 3"
2830 (org-test-with-temp-text
2831 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
2832 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
2833 (should
2834 (equal "1 2 3"
2835 (org-test-with-temp-text
2836 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
2837 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
2838 ;; Ignore forbidden (special) properties.
2839 (should-not
2840 (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
2841 (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
2843 (ert-deftest test-org/entry-put ()
2844 "Test `org-entry-put' specifications."
2845 ;; Error when not a string or nil.
2846 (should-error
2847 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
2848 (org-entry-put 1 "test" 2)))
2849 ;; Set "TODO" property.
2850 (should
2851 (string-match (regexp-quote " TODO H")
2852 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
2853 (org-entry-put (point) "TODO" "TODO")
2854 (buffer-string))))
2855 (should
2856 (string-match (regexp-quote "* H")
2857 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
2858 (org-entry-put (point) "TODO" nil)
2859 (buffer-string))))
2860 ;; Set "PRIORITY" property.
2861 (should
2862 (equal "* [#A] H"
2863 (org-test-with-temp-text "* [#B] H"
2864 (org-entry-put (point) "PRIORITY" "A")
2865 (buffer-string))))
2866 (should
2867 (equal "* H"
2868 (org-test-with-temp-text "* [#B] H"
2869 (org-entry-put (point) "PRIORITY" nil)
2870 (buffer-string))))
2871 ;; Set "SCHEDULED" property.
2872 (should
2873 (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
2874 (org-test-with-temp-text "* H"
2875 (org-entry-put (point) "SCHEDULED" "2014-03-04")
2876 (buffer-string))))
2877 (should
2878 (string= "* H\n"
2879 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2880 (org-entry-put (point) "SCHEDULED" nil)
2881 (buffer-string))))
2882 (should
2883 (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
2884 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2885 (org-entry-put (point) "SCHEDULED" "earlier")
2886 (buffer-string))))
2887 (should
2888 (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
2889 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2890 (org-entry-put (point) "SCHEDULED" "later")
2891 (buffer-string))))
2892 ;; Set "DEADLINE" property.
2893 (should
2894 (string-match "^ *DEADLINE: <2014-03-04 .*?>"
2895 (org-test-with-temp-text "* H"
2896 (org-entry-put (point) "DEADLINE" "2014-03-04")
2897 (buffer-string))))
2898 (should
2899 (string= "* H\n"
2900 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
2901 (org-entry-put (point) "DEADLINE" nil)
2902 (buffer-string))))
2903 (should
2904 (string-match "^ *DEADLINE: <2014-03-03 .*?>"
2905 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
2906 (org-entry-put (point) "DEADLINE" "earlier")
2907 (buffer-string))))
2908 (should
2909 (string-match "^ *DEADLINE: <2014-03-05 .*?>"
2910 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
2911 (org-entry-put (point) "DEADLINE" "later")
2912 (buffer-string))))
2913 ;; Regular properties, with or without pre-existing drawer.
2914 (should
2915 (string-match "^ *:A: +2$"
2916 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2917 (org-entry-put (point) "A" "2")
2918 (buffer-string))))
2919 (should
2920 (string-match "^ *:A: +1$"
2921 (org-test-with-temp-text "* H"
2922 (org-entry-put (point) "A" "1")
2923 (buffer-string))))
2924 ;; Special case: two consecutive headlines.
2925 (should
2926 (string-match "\\* A\n *:PROPERTIES:"
2927 (org-test-with-temp-text "* A\n** B"
2928 (org-entry-put (point) "A" "1")
2929 (buffer-string)))))
2932 ;;; Radio Targets
2934 (ert-deftest test-org/update-radio-target-regexp ()
2935 "Test `org-update-radio-target-regexp' specifications."
2936 ;; Properly update cache with no previous radio target regexp.
2937 (should
2938 (eq 'link
2939 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
2940 (save-excursion (goto-char (point-max)) (org-element-context))
2941 (insert "<<<")
2942 (search-forward "o")
2943 (insert ">>>")
2944 (org-update-radio-target-regexp)
2945 (goto-char (point-max))
2946 (org-element-type (org-element-context)))))
2947 ;; Properly update cache with previous radio target regexp.
2948 (should
2949 (eq 'link
2950 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
2951 (save-excursion (goto-char (point-max)) (org-element-context))
2952 (insert "<<<")
2953 (search-forward "o")
2954 (insert ">>>")
2955 (org-update-radio-target-regexp)
2956 (search-backward "r")
2957 (delete-char 5)
2958 (insert "new")
2959 (org-update-radio-target-regexp)
2960 (goto-char (point-max))
2961 (delete-region (line-beginning-position) (point))
2962 (insert "new")
2963 (org-element-type (org-element-context))))))
2966 ;;; Sparse trees
2968 (ert-deftest test-org/match-sparse-tree ()
2969 "Test `org-match-sparse-tree' specifications."
2970 ;; Match tags.
2971 (should-not
2972 (org-test-with-temp-text "* H\n** H1 :tag:"
2973 (org-match-sparse-tree nil "tag")
2974 (search-forward "H1")
2975 (org-invisible-p2)))
2976 (should
2977 (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
2978 (org-match-sparse-tree nil "tag")
2979 (search-forward "H2")
2980 (org-invisible-p2)))
2981 ;; "-" operator for tags.
2982 (should-not
2983 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
2984 (org-match-sparse-tree nil "tag1-tag2")
2985 (search-forward "H1")
2986 (org-invisible-p2)))
2987 (should
2988 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
2989 (org-match-sparse-tree nil "tag1-tag2")
2990 (search-forward "H2")
2991 (org-invisible-p2)))
2992 ;; "&" operator for tags.
2993 (should
2994 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
2995 (org-match-sparse-tree nil "tag1&tag2")
2996 (search-forward "H1")
2997 (org-invisible-p2)))
2998 (should-not
2999 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3000 (org-match-sparse-tree nil "tag1&tag2")
3001 (search-forward "H2")
3002 (org-invisible-p2)))
3003 ;; "|" operator for tags.
3004 (should-not
3005 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3006 (org-match-sparse-tree nil "tag1|tag2")
3007 (search-forward "H1")
3008 (org-invisible-p2)))
3009 (should-not
3010 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3011 (org-match-sparse-tree nil "tag1|tag2")
3012 (search-forward "H2")
3013 (org-invisible-p2)))
3014 ;; Regexp match on tags.
3015 (should-not
3016 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3017 (org-match-sparse-tree nil "{^tag.*}")
3018 (search-forward "H1")
3019 (org-invisible-p2)))
3020 (should
3021 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3022 (org-match-sparse-tree nil "{^tag.*}")
3023 (search-forward "H2")
3024 (org-invisible-p2)))
3025 ;; Match group tags.
3026 (should-not
3027 (org-test-with-temp-text
3028 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3029 (org-match-sparse-tree nil "work")
3030 (search-forward "H1")
3031 (org-invisible-p2)))
3032 (should-not
3033 (org-test-with-temp-text
3034 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3035 (org-match-sparse-tree nil "work")
3036 (search-forward "H2")
3037 (org-invisible-p2)))
3038 ;; Match properties.
3039 (should
3040 (org-test-with-temp-text
3041 "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
3042 (org-match-sparse-tree nil "A=\"1\"")
3043 (search-forward "H2")
3044 (org-invisible-p2)))
3045 (should-not
3046 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3047 (org-match-sparse-tree nil "A=\"1\"")
3048 (search-forward "H2")
3049 (org-invisible-p2)))
3050 ;; Case is not significant when matching properties.
3051 (should-not
3052 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3053 (org-match-sparse-tree nil "a=\"1\"")
3054 (search-forward "H2")
3055 (org-invisible-p2)))
3056 (should-not
3057 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
3058 (org-match-sparse-tree nil "A=\"1\"")
3059 (search-forward "H2")
3060 (org-invisible-p2)))
3061 ;; Match special LEVEL property.
3062 (should-not
3063 (org-test-with-temp-text "* H\n** H1\n*** H2"
3064 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3065 (search-forward "H1")
3066 (org-invisible-p2)))
3067 (should
3068 (org-test-with-temp-text "* H\n** H1\n*** H2"
3069 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3070 (search-forward "H2")
3071 (org-invisible-p2)))
3072 ;; Comparison operators when matching properties.
3073 (should
3074 (org-test-with-temp-text
3075 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3076 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3077 (search-forward "H1")
3078 (org-invisible-p2)))
3079 (should-not
3080 (org-test-with-temp-text
3081 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3082 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3083 (search-forward "H2")
3084 (org-invisible-p2)))
3085 ;; Regexp match on properties values.
3086 (should-not
3087 (org-test-with-temp-text
3088 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3089 (org-match-sparse-tree nil "A={f.*}")
3090 (search-forward "H1")
3091 (org-invisible-p2)))
3092 (should
3093 (org-test-with-temp-text
3094 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3095 (org-match-sparse-tree nil "A={f.*}")
3096 (search-forward "H2")
3097 (org-invisible-p2)))
3098 ;; With an optional argument, limit match to TODO entries.
3099 (should-not
3100 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3101 (org-match-sparse-tree t "tag")
3102 (search-forward "H1")
3103 (org-invisible-p2)))
3104 (should
3105 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3106 (org-match-sparse-tree t "tag")
3107 (search-forward "H2")
3108 (org-invisible-p2))))
3111 ;;; Timestamps API
3113 (ert-deftest test-org/timestamp-has-time-p ()
3114 "Test `org-timestamp-has-time-p' specifications."
3115 ;; With time.
3116 (should
3117 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
3118 (org-timestamp-has-time-p (org-element-context))))
3119 ;; Without time.
3120 (should-not
3121 (org-test-with-temp-text "<2012-03-29 Thu>"
3122 (org-timestamp-has-time-p (org-element-context)))))
3124 (ert-deftest test-org/timestamp-format ()
3125 "Test `org-timestamp-format' specifications."
3126 ;; Regular test.
3127 (should
3128 (equal
3129 "2012-03-29 16:40"
3130 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
3131 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
3132 ;; Range end.
3133 (should
3134 (equal
3135 "2012-03-29"
3136 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
3137 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
3139 (ert-deftest test-org/timestamp-split-range ()
3140 "Test `org-timestamp-split-range' specifications."
3141 ;; Extract range start (active).
3142 (should
3143 (equal '(2012 3 29)
3144 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3145 (let ((ts (org-timestamp-split-range (org-element-context))))
3146 (mapcar (lambda (p) (org-element-property p ts))
3147 '(:year-end :month-end :day-end))))))
3148 ;; Extract range start (inactive)
3149 (should
3150 (equal '(2012 3 29)
3151 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
3152 (let ((ts (org-timestamp-split-range (org-element-context))))
3153 (mapcar (lambda (p) (org-element-property p ts))
3154 '(:year-end :month-end :day-end))))))
3155 ;; Extract range end (active).
3156 (should
3157 (equal '(2012 3 30)
3158 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3159 (let ((ts (org-timestamp-split-range
3160 (org-element-context) t)))
3161 (mapcar (lambda (p) (org-element-property p ts))
3162 '(:year-end :month-end :day-end))))))
3163 ;; Extract range end (inactive)
3164 (should
3165 (equal '(2012 3 30)
3166 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
3167 (let ((ts (org-timestamp-split-range
3168 (org-element-context) t)))
3169 (mapcar (lambda (p) (org-element-property p ts))
3170 '(:year-end :month-end :day-end))))))
3171 ;; Return the timestamp if not a range.
3172 (should
3173 (org-test-with-temp-text "[2012-03-29 Thu]"
3174 (let* ((ts-orig (org-element-context))
3175 (ts-copy (org-timestamp-split-range ts-orig)))
3176 (eq ts-orig ts-copy))))
3177 (should
3178 (org-test-with-temp-text "<%%(org-float t 4 2)>"
3179 (let* ((ts-orig (org-element-context))
3180 (ts-copy (org-timestamp-split-range ts-orig)))
3181 (eq ts-orig ts-copy))))
3182 ;; Check that parent is the same when a range was split.
3183 (should
3184 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
3185 (let* ((ts-orig (org-element-context))
3186 (ts-copy (org-timestamp-split-range ts-orig)))
3187 (eq (org-element-property :parent ts-orig)
3188 (org-element-property :parent ts-copy))))))
3190 (ert-deftest test-org/timestamp-translate ()
3191 "Test `org-timestamp-translate' specifications."
3192 ;; Translate whole date range.
3193 (should
3194 (equal "<29>--<30>"
3195 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3196 (let ((org-display-custom-times t)
3197 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3198 (org-timestamp-translate (org-element-context))))))
3199 ;; Translate date range start.
3200 (should
3201 (equal "<29>"
3202 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3203 (let ((org-display-custom-times t)
3204 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3205 (org-timestamp-translate (org-element-context) 'start)))))
3206 ;; Translate date range end.
3207 (should
3208 (equal "<30>"
3209 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3210 (let ((org-display-custom-times t)
3211 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3212 (org-timestamp-translate (org-element-context) 'end)))))
3213 ;; Translate time range.
3214 (should
3215 (equal "<08>--<16>"
3216 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
3217 (let ((org-display-custom-times t)
3218 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
3219 (org-timestamp-translate (org-element-context))))))
3220 ;; Translate non-range timestamp.
3221 (should
3222 (equal "<29>"
3223 (org-test-with-temp-text "<2012-03-29 Thu>"
3224 (let ((org-display-custom-times t)
3225 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3226 (org-timestamp-translate (org-element-context))))))
3227 ;; Do not change `diary' timestamps.
3228 (should
3229 (equal "<%%(org-float t 4 2)>"
3230 (org-test-with-temp-text "<%%(org-float t 4 2)>"
3231 (let ((org-display-custom-times t)
3232 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3233 (org-timestamp-translate (org-element-context)))))))
3237 ;;; Visibility
3239 (ert-deftest test-org/flag-drawer ()
3240 "Test `org-flag-drawer' specifications."
3241 ;; Hide drawer.
3242 (should
3243 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3244 (org-flag-drawer t)
3245 (get-char-property (line-end-position) 'invisible)))
3246 ;; Show drawer.
3247 (should-not
3248 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3249 (org-flag-drawer t)
3250 (org-flag-drawer nil)
3251 (get-char-property (line-end-position) 'invisible)))
3252 ;; Test optional argument.
3253 (should
3254 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
3255 (let ((drawer (save-excursion (search-forward ":D2")
3256 (org-element-at-point))))
3257 (org-flag-drawer t drawer)
3258 (get-char-property (progn (search-forward ":D2") (line-end-position))
3259 'invisible))))
3260 (should-not
3261 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
3262 (let ((drawer (save-excursion (search-forward ":D2")
3263 (org-element-at-point))))
3264 (org-flag-drawer t drawer)
3265 (get-char-property (line-end-position) 'invisible))))
3266 ;; Do not hide fake drawers.
3267 (should-not
3268 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
3269 (forward-line 1)
3270 (org-flag-drawer t)
3271 (get-char-property (line-end-position) 'invisible)))
3272 ;; Do not hide incomplete drawers.
3273 (should-not
3274 (org-test-with-temp-text ":D:\nparagraph"
3275 (forward-line 1)
3276 (org-flag-drawer t)
3277 (get-char-property (line-end-position) 'invisible)))
3278 ;; Do not hide drawers when called from final blank lines.
3279 (should-not
3280 (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
3281 (goto-char (point-max))
3282 (org-flag-drawer t)
3283 (goto-char (point-min))
3284 (get-char-property (line-end-position) 'invisible)))
3285 ;; Don't leave point in an invisible part of the buffer when hiding
3286 ;; a drawer away.
3287 (should-not
3288 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3289 (goto-char (point-max))
3290 (org-flag-drawer t)
3291 (get-char-property (point) 'invisible))))
3293 (ert-deftest test-org/hide-block-toggle ()
3294 "Test `org-hide-block-toggle' specifications."
3295 ;; Error when not at a block.
3296 (should-error
3297 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
3298 (org-hide-block-toggle 'off)
3299 (get-char-property (line-end-position) 'invisible)))
3300 ;; Hide block.
3301 (should
3302 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
3303 (org-hide-block-toggle)
3304 (get-char-property (line-end-position) 'invisible)))
3305 (should
3306 (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
3307 (org-hide-block-toggle)
3308 (get-char-property (line-end-position) 'invisible)))
3309 ;; Show block unconditionally when optional argument is `off'.
3310 (should-not
3311 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3312 (org-hide-block-toggle)
3313 (org-hide-block-toggle 'off)
3314 (get-char-property (line-end-position) 'invisible)))
3315 (should-not
3316 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3317 (org-hide-block-toggle 'off)
3318 (get-char-property (line-end-position) 'invisible)))
3319 ;; Hide block unconditionally when optional argument is non-nil.
3320 (should
3321 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3322 (org-hide-block-toggle t)
3323 (get-char-property (line-end-position) 'invisible)))
3324 (should
3325 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3326 (org-hide-block-toggle)
3327 (org-hide-block-toggle t)
3328 (get-char-property (line-end-position) 'invisible)))
3329 ;; Do not hide block when called from final blank lines.
3330 (should-not
3331 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
3332 (org-hide-block-toggle)
3333 (goto-char (point-min))
3334 (get-char-property (line-end-position) 'invisible)))
3335 ;; Don't leave point in an invisible part of the buffer when hiding
3336 ;; a block away.
3337 (should-not
3338 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
3339 (org-hide-block-toggle)
3340 (get-char-property (point) 'invisible))))
3342 (ert-deftest test-org/hide-block-toggle-maybe ()
3343 "Test `org-hide-block-toggle-maybe' specifications."
3344 (should
3345 (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
3346 (org-hide-block-toggle-maybe)))
3347 (should-not
3348 (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
3351 (provide 'test-org)
3353 ;;; test-org.el ends here