ox: Remove `org-export-get-headline-id'
[org-mode/org-tableheadings.git] / testing / lisp / test-org.el
blob3c4d34da0aa43841cad37ea612aceb0b9d25d2c7
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 diary sexps, footnote definitions or headlines.
582 (should
583 (zerop
584 (org-test-with-temp-text "%%(org-calendar-holiday)"
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 (should
593 (zerop
594 (org-test-with-temp-text "* H"
595 (org-indent-line)
596 (org-get-indentation))))
597 ;; Do not indent before first headline.
598 (should
599 (zerop
600 (org-test-with-temp-text ""
601 (org-indent-line)
602 (org-get-indentation))))
603 ;; Indent according to headline level otherwise, unless
604 ;; `org-adapt-indentation' is nil.
605 (should
606 (= 2
607 (org-test-with-temp-text "* H\nA"
608 (forward-line)
609 (let ((org-adapt-indentation t)) (org-indent-line))
610 (org-get-indentation))))
611 (should
612 (= 2
613 (org-test-with-temp-text "* H\n\nA"
614 (forward-line)
615 (let ((org-adapt-indentation t)) (org-indent-line))
616 (org-get-indentation))))
617 (should
618 (zerop
619 (org-test-with-temp-text "* H\nA"
620 (forward-line)
621 (let ((org-adapt-indentation nil)) (org-indent-line))
622 (org-get-indentation))))
623 ;; Indenting preserves point position.
624 (should
625 (org-test-with-temp-text "* H\nAB"
626 (forward-line)
627 (forward-char)
628 (let ((org-adapt-indentation t)) (org-indent-line))
629 (looking-at "B")))
630 ;; Do not change indentation at an item.
631 (should
632 (= 1
633 (org-test-with-temp-text "* H\n - A"
634 (forward-line)
635 (let ((org-adapt-indentation t)) (org-indent-line))
636 (org-get-indentation))))
637 ;; On blank lines at the end of a list, indent like last element
638 ;; within it if the line is still in the list. Otherwise, indent
639 ;; like the whole list.
640 (should
641 (= 4
642 (org-test-with-temp-text "* H\n- A\n - AA\n"
643 (goto-char (point-max))
644 (let ((org-adapt-indentation t)) (org-indent-line))
645 (org-get-indentation))))
646 (should
647 (zerop
648 (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
649 (goto-char (point-max))
650 (let ((org-adapt-indentation t)) (org-indent-line))
651 (org-get-indentation))))
652 ;; Likewise, on a blank line at the end of a footnote definition,
653 ;; indent at column 0 if line belongs to the definition. Otherwise,
654 ;; indent like the definition itself.
655 (should
656 (zerop
657 (org-test-with-temp-text "* H\n[fn:1] Definition\n"
658 (goto-char (point-max))
659 (let ((org-adapt-indentation t)) (org-indent-line))
660 (org-get-indentation))))
661 (should
662 (zerop
663 (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
664 (goto-char (point-max))
665 (let ((org-adapt-indentation t)) (org-indent-line))
666 (org-get-indentation))))
667 ;; After the end of the contents of a greater element, indent like
668 ;; the beginning of the element.
669 (should
670 (= 1
671 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
672 (forward-line 2)
673 (org-indent-line)
674 (org-get-indentation))))
675 ;; On blank lines after a paragraph, indent like its last non-empty
676 ;; line.
677 (should
678 (= 1
679 (org-test-with-temp-text " Paragraph\n\n<point>"
680 (org-indent-line)
681 (org-get-indentation))))
682 ;; At the first line of an element, indent like previous element's
683 ;; first line, ignoring footnotes definitions and inline tasks, or
684 ;; according to parent.
685 (should
686 (= 2
687 (org-test-with-temp-text "A\n\n B\n\nC"
688 (goto-char (point-max))
689 (org-indent-line)
690 (org-get-indentation))))
691 (should
692 (= 1
693 (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
694 (goto-char (point-max))
695 (org-indent-line)
696 (org-get-indentation))))
697 (should
698 (= 1
699 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
700 (forward-line 1)
701 (org-indent-line)
702 (org-get-indentation))))
703 ;; Within code part of a source block, use language major mode if
704 ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
705 ;; according to line above.
706 (should
707 (= 6
708 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
709 (forward-line 2)
710 (let ((org-src-tab-acts-natively t)
711 (org-edit-src-content-indentation 0))
712 (org-indent-line))
713 (org-get-indentation))))
714 (should
715 (= 1
716 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
717 (forward-line 2)
718 (let ((org-src-tab-acts-natively nil)
719 (org-edit-src-content-indentation 0))
720 (org-indent-line))
721 (org-get-indentation))))
722 ;; Otherwise, indent like the first non-blank line above.
723 (should
724 (zerop
725 (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
726 (forward-line 3)
727 (org-indent-line)
728 (org-get-indentation))))
729 ;; Align node properties according to `org-property-format'. Handle
730 ;; nicely empty values.
731 (should
732 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
733 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
734 (let ((org-property-format "%-10s %s")) (org-indent-line))
735 (buffer-string))))
736 (should
737 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
738 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
739 (let ((org-property-format "%-10s %s")) (org-indent-line))
740 (buffer-string)))))
742 (ert-deftest test-org/indent-region ()
743 "Test `org-indent-region' specifications."
744 ;; Indent paragraph.
745 (should
746 (equal "A\nB\nC"
747 (org-test-with-temp-text " A\nB\n C"
748 (org-indent-region (point-min) (point-max))
749 (buffer-string))))
750 ;; Indent greater elements along with their contents.
751 (should
752 (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
753 (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
754 (org-indent-region (point-min) (point-max))
755 (buffer-string))))
756 ;; Ignore contents of verse blocks and example blocks.
757 (should
758 (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
759 (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
760 (org-indent-region (point-min) (point-max))
761 (buffer-string))))
762 (should
763 (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
764 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
765 (org-indent-region (point-min) (point-max))
766 (buffer-string))))
767 ;; Indent according to mode if `org-src-tab-acts-natively' is
768 ;; non-nil. Otherwise, do not indent code at all.
769 (should
770 (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
771 (org-test-with-temp-text
772 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
773 (let ((org-src-tab-acts-natively t)
774 (org-edit-src-content-indentation 0))
775 (org-indent-region (point-min) (point-max)))
776 (buffer-string))))
777 (should
778 (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
779 (org-test-with-temp-text
780 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
781 (let ((org-src-tab-acts-natively nil)
782 (org-edit-src-content-indentation 0))
783 (org-indent-region (point-min) (point-max)))
784 (buffer-string))))
785 ;; Align node properties according to `org-property-format'. Handle
786 ;; nicely empty values.
787 (should
788 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
789 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
790 (let ((org-property-format "%-10s %s")
791 (org-adapt-indentation nil))
792 (org-indent-region (point) (point-max)))
793 (buffer-string))))
794 (should
795 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
796 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
797 (let ((org-property-format "%-10s %s")
798 (org-adapt-indentation nil))
799 (org-indent-region (point) (point-max)))
800 (buffer-string))))
801 ;; Indent plain lists.
802 (should
803 (equal "- A\n B\n - C\n\n D"
804 (org-test-with-temp-text "- A\n B\n - C\n\n D"
805 (org-indent-region (point-min) (point-max))
806 (buffer-string))))
807 (should
808 (equal "- A\n\n- B"
809 (org-test-with-temp-text " - A\n\n - B"
810 (org-indent-region (point-min) (point-max))
811 (buffer-string))))
812 ;; Indent footnote definitions.
813 (should
814 (equal "[fn:1] Definition\n\nDefinition"
815 (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
816 (org-indent-region (point-min) (point-max))
817 (buffer-string))))
818 ;; Special case: Start indenting on a blank line.
819 (should
820 (equal "\nParagraph"
821 (org-test-with-temp-text "\n Paragraph"
822 (org-indent-region (point-min) (point-max))
823 (buffer-string)))))
827 ;;; Editing
829 (ert-deftest test-org/return ()
830 "Test RET (`org-return') specifications."
831 ;; Regular test.
832 (should
833 (equal "Para\ngraph"
834 (org-test-with-temp-text "Para<point>graph"
835 (org-return)
836 (buffer-string))))
837 ;; With optional argument, indent line.
838 (should
839 (equal " Para\n graph"
840 (org-test-with-temp-text " Para<point>graph"
841 (org-return t)
842 (buffer-string))))
843 ;; On a table, call `org-table-next-row'.
844 (should
845 (org-test-with-temp-text "| <point>a |\n| b |"
846 (org-return)
847 (org-looking-at-p "b")))
848 ;; Open link or timestamp under point when `org-return-follows-link'
849 ;; is non-nil.
850 (should
851 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
852 (let ((org-return-follows-link t)) (org-return))
853 (org-looking-at-p "<<target>>")))
854 (should-not
855 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
856 (let ((org-return-follows-link nil)) (org-return))
857 (org-looking-at-p "<<target>>")))
858 ;; However, do not open link when point is in a table.
859 (should
860 (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
861 (let ((org-return-follows-link t)) (org-return))
862 (org-looking-at-p "between")))
863 ;; Special case: in a list, when indenting, do not break structure.
864 (should
865 (equal "- A\n B"
866 (org-test-with-temp-text "- A <point>B"
867 (org-return t)
868 (buffer-string))))
869 (should
870 (equal "- A\n\n- B"
871 (org-test-with-temp-text "- A\n<point>- B"
872 (org-return t)
873 (buffer-string))))
874 ;; Special case: on tags part of a headline, add a newline below it
875 ;; instead of breaking it.
876 (should
877 (equal "* H :tag:\n"
878 (org-test-with-temp-text "* H :<point>tag:"
879 (org-return)
880 (buffer-string)))))
882 (ert-deftest test-org/meta-return ()
883 "Test M-RET (`org-meta-return') specifications."
884 ;; In a table field insert a row above.
885 (should
886 (org-test-with-temp-text "| a |"
887 (forward-char)
888 (org-meta-return)
889 (forward-line -1)
890 (looking-at "| |$")))
891 ;; In a paragraph change current line into a header.
892 (should
893 (org-test-with-temp-text "a"
894 (org-meta-return)
895 (beginning-of-line)
896 (looking-at "\* a$")))
897 ;; In an item insert an item, in this case above.
898 (should
899 (org-test-with-temp-text "- a"
900 (org-meta-return)
901 (beginning-of-line)
902 (looking-at "- $")))
903 ;; In a drawer and item insert an item, in this case above.
904 (should
905 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
906 (forward-line)
907 (org-meta-return)
908 (beginning-of-line)
909 (looking-at "- $"))))
911 (ert-deftest test-org/insert-heading ()
912 "Test `org-insert-heading' specifications."
913 ;; In an empty buffer, insert a new headline.
914 (should
915 (equal "* "
916 (org-test-with-temp-text ""
917 (org-insert-heading)
918 (buffer-string))))
919 ;; At the beginning of a line, turn it into a headline
920 (should
921 (equal "* P"
922 (org-test-with-temp-text "<point>P"
923 (org-insert-heading)
924 (buffer-string))))
925 ;; In the middle of a line, split the line if allowed, otherwise,
926 ;; insert the headline at its end.
927 (should
928 (equal "Para\n* graph"
929 (org-test-with-temp-text "Para<point>graph"
930 (let ((org-M-RET-may-split-line '((default . t))))
931 (org-insert-heading))
932 (buffer-string))))
933 (should
934 (equal "Paragraph\n* "
935 (org-test-with-temp-text "Para<point>graph"
936 (let ((org-M-RET-may-split-line '((default . nil))))
937 (org-insert-heading))
938 (buffer-string))))
939 ;; When on a list, insert an item instead, unless called with an
940 ;; universal argument or if list is invisible. In this case, create
941 ;; a new headline after contents.
942 (should
943 (equal "* H\n- item\n- "
944 (org-test-with-temp-text "* H\n- item<point>"
945 (let ((org-insert-heading-respect-content nil))
946 (org-insert-heading))
947 (buffer-string))))
948 (should
949 (equal "* H\n- item\n- item 2\n* "
950 (org-test-with-temp-text "* H\n- item<point>\n- item 2"
951 (let ((org-insert-heading-respect-content nil))
952 (org-insert-heading '(4)))
953 (buffer-string))))
954 (should
955 (equal "* H\n- item\n* "
956 (org-test-with-temp-text "* H\n- item"
957 (org-cycle)
958 (goto-char (point-max))
959 (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
960 (buffer-string))))
961 ;; When called with two universal arguments, insert a new headline
962 ;; at the end of the grandparent subtree.
963 (should
964 (equal "* H1\n** H3\n- item\n** H2\n** "
965 (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
966 (let ((org-insert-heading-respect-content nil))
967 (org-insert-heading '(16)))
968 (buffer-string))))
969 ;; When optional TOP-LEVEL argument is non-nil, always insert
970 ;; a level 1 heading.
971 (should
972 (equal "* H1\n** H2\n* "
973 (org-test-with-temp-text "* H1\n** H2<point>"
974 (org-insert-heading nil nil t)
975 (buffer-string))))
976 (should
977 (equal "* H1\n- item\n* "
978 (org-test-with-temp-text "* H1\n- item<point>"
979 (org-insert-heading nil nil t)
980 (buffer-string))))
981 ;; Corner case: correctly insert a headline after an empty one.
982 (should
983 (equal "* \n* "
984 (org-test-with-temp-text "* <point>"
985 (org-insert-heading)
986 (buffer-string)))))
988 (ert-deftest test-org/insert-todo-heading-respect-content ()
989 "Test `org-insert-todo-heading-respect-content' specifications."
990 ;; Create a TODO heading.
991 (should
992 (org-test-with-temp-text "* H1\n Body"
993 (org-insert-todo-heading-respect-content)
994 (nth 2 (org-heading-components))))
995 ;; Add headline at the end of the first subtree
996 (should
997 (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
998 (search-forward "H1Body")
999 (org-insert-todo-heading-respect-content)
1000 (and (eobp) (org-at-heading-p))))
1001 ;; In a list, do not create a new item.
1002 (should
1003 (org-test-with-temp-text "* H\n- an item\n- another one"
1004 (search-forward "an ")
1005 (org-insert-todo-heading-respect-content)
1006 (and (eobp) (org-at-heading-p)))))
1010 ;;; Fixed-Width Areas
1012 (ert-deftest test-org/toggle-fixed-width ()
1013 "Test `org-toggle-fixed-width' specifications."
1014 ;; No region: Toggle on fixed-width marker in paragraphs.
1015 (should
1016 (equal ": A"
1017 (org-test-with-temp-text "A"
1018 (org-toggle-fixed-width)
1019 (buffer-string))))
1020 ;; No region: Toggle off fixed-width markers in fixed-width areas.
1021 (should
1022 (equal "A"
1023 (org-test-with-temp-text ": A"
1024 (org-toggle-fixed-width)
1025 (buffer-string))))
1026 ;; No region: Toggle on marker in blank lines after elements or just
1027 ;; after a headline.
1028 (should
1029 (equal "* H\n: "
1030 (org-test-with-temp-text "* H\n"
1031 (forward-line)
1032 (org-toggle-fixed-width)
1033 (buffer-string))))
1034 (should
1035 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
1036 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
1037 (goto-char (point-max))
1038 (org-toggle-fixed-width)
1039 (buffer-string))))
1040 ;; No region: Toggle on marker in front of one line elements (e.g.,
1041 ;; headlines, clocks)
1042 (should
1043 (equal ": * Headline"
1044 (org-test-with-temp-text "* Headline"
1045 (org-toggle-fixed-width)
1046 (buffer-string))))
1047 (should
1048 (equal ": #+KEYWORD: value"
1049 (org-test-with-temp-text "#+KEYWORD: value"
1050 (org-toggle-fixed-width)
1051 (buffer-string))))
1052 ;; No region: error in other situations.
1053 (should-error
1054 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
1055 (forward-line)
1056 (org-toggle-fixed-width)
1057 (buffer-string)))
1058 ;; No region: Indentation is preserved.
1059 (should
1060 (equal "- A\n : B"
1061 (org-test-with-temp-text "- A\n B"
1062 (forward-line)
1063 (org-toggle-fixed-width)
1064 (buffer-string))))
1065 ;; Region: If it contains only fixed-width elements and blank lines,
1066 ;; toggle off fixed-width markup.
1067 (should
1068 (equal "A\n\nB"
1069 (org-test-with-temp-text ": A\n\n: B"
1070 (transient-mark-mode 1)
1071 (push-mark (point) t t)
1072 (goto-char (point-max))
1073 (org-toggle-fixed-width)
1074 (buffer-string))))
1075 ;; Region: If it contains anything else, toggle on fixed-width but
1076 ;; not on fixed-width areas.
1077 (should
1078 (equal ": A\n: \n: B\n: \n: C"
1079 (org-test-with-temp-text "A\n\n: B\n\nC"
1080 (transient-mark-mode 1)
1081 (push-mark (point) t t)
1082 (goto-char (point-max))
1083 (org-toggle-fixed-width)
1084 (buffer-string))))
1085 ;; Region: Ignore blank lines at its end, unless it contains only
1086 ;; such lines.
1087 (should
1088 (equal ": A\n\n"
1089 (org-test-with-temp-text "A\n\n"
1090 (transient-mark-mode 1)
1091 (push-mark (point) t t)
1092 (goto-char (point-max))
1093 (org-toggle-fixed-width)
1094 (buffer-string))))
1095 (should
1096 (equal ": \n: \n"
1097 (org-test-with-temp-text "\n\n"
1098 (transient-mark-mode 1)
1099 (push-mark (point) t t)
1100 (goto-char (point-max))
1101 (org-toggle-fixed-width)
1102 (buffer-string)))))
1106 ;;; Headline
1108 (ert-deftest test-org/in-commented-heading-p ()
1109 "Test `org-in-commented-heading-p' specifications."
1110 ;; Commented headline.
1111 (should
1112 (org-test-with-temp-text "* COMMENT Headline\nBody"
1113 (goto-char (point-max))
1114 (org-in-commented-heading-p)))
1115 ;; Commented ancestor.
1116 (should
1117 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1118 (goto-char (point-max))
1119 (org-in-commented-heading-p)))
1120 ;; Comment keyword is case-sensitive.
1121 (should-not
1122 (org-test-with-temp-text "* Comment Headline\nBody"
1123 (goto-char (point-max))
1124 (org-in-commented-heading-p)))
1125 ;; Keyword is standalone.
1126 (should-not
1127 (org-test-with-temp-text "* COMMENTHeadline\nBody"
1128 (goto-char (point-max))
1129 (org-in-commented-heading-p)))
1130 ;; Optional argument.
1131 (should-not
1132 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1133 (goto-char (point-max))
1134 (org-in-commented-heading-p t))))
1138 ;;; Keywords
1140 (ert-deftest test-org/set-regexps-and-options ()
1141 "Test `org-set-regexps-and-options' specifications."
1142 ;; TAGS keyword.
1143 (should
1144 (equal '(("A" . ?a) ("B") ("C"))
1145 (org-test-with-temp-text "#+TAGS: A(a) B C"
1146 (org-mode-restart)
1147 org-tag-alist)))
1148 (should
1149 (equal '(("A") (:newline) ("B"))
1150 (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
1151 (org-mode-restart)
1152 org-tag-alist)))
1153 (should
1154 (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
1155 (org-test-with-temp-text "#+TAGS: { A B } C"
1156 (org-mode-restart)
1157 org-tag-alist)))
1158 (should
1159 (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
1160 (org-test-with-temp-text "#+TAGS: { A : B C }"
1161 (org-mode-restart)
1162 org-tag-alist)))
1163 (should
1164 (equal '(("A" "B" "C"))
1165 (org-test-with-temp-text "#+TAGS: { A : B C }"
1166 (org-mode-restart)
1167 org-tag-groups-alist)))
1168 (should
1169 (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
1170 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1171 (org-mode-restart)
1172 org-tag-alist)))
1173 (should
1174 (equal '(("A" "B" "C"))
1175 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1176 (org-mode-restart)
1177 org-tag-groups-alist)))
1178 ;; FILETAGS keyword.
1179 (should
1180 (equal '("A" "B" "C")
1181 (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
1182 (org-mode-restart)
1183 org-file-tags)))
1184 ;; PROPERTY keyword. Property names are case-insensitive.
1185 (should
1186 (equal "foo=1"
1187 (org-test-with-temp-text "#+PROPERTY: var foo=1"
1188 (org-mode-restart)
1189 (cdr (assoc "var" org-file-properties)))))
1190 (should
1191 (equal
1192 "foo=1 bar=2"
1193 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
1194 (org-mode-restart)
1195 (cdr (assoc "var" org-file-properties)))))
1196 (should
1197 (equal
1198 "foo=1 bar=2"
1199 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
1200 (org-mode-restart)
1201 (cdr (assoc "var" org-file-properties)))))
1202 ;; ARCHIVE keyword.
1203 (should
1204 (equal "%s_done::"
1205 (org-test-with-temp-text "#+ARCHIVE: %s_done::"
1206 (org-mode-restart)
1207 org-archive-location)))
1208 ;; CATEGORY keyword.
1209 (should
1210 (eq 'test
1211 (org-test-with-temp-text "#+CATEGORY: test"
1212 (org-mode-restart)
1213 org-category)))
1214 (should
1215 (equal "test"
1216 (org-test-with-temp-text "#+CATEGORY: test"
1217 (org-mode-restart)
1218 (cdr (assoc "CATEGORY" org-file-properties)))))
1219 ;; COLUMNS keyword.
1220 (should
1221 (equal "%25ITEM %TAGS %PRIORITY %TODO"
1222 (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
1223 (org-mode-restart)
1224 org-columns-default-format)))
1225 ;; CONSTANTS keyword. Constants names are case sensitive.
1226 (should
1227 (equal '("299792458." "3.14")
1228 (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
1229 (org-mode-restart)
1230 (mapcar
1231 (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
1232 '("c" "pi")))))
1233 (should
1234 (equal "3.14"
1235 (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
1236 (org-mode-restart)
1237 (cdr (assoc "pi" org-table-formula-constants-local)))))
1238 (should
1239 (equal "22/7"
1240 (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
1241 (org-mode-restart)
1242 (cdr (assoc "PI" org-table-formula-constants-local)))))
1243 ;; LINK keyword.
1244 (should
1245 (equal
1246 '("url1" "url2")
1247 (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
1248 (org-mode-restart)
1249 (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
1250 '("a" "b")))))
1251 ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
1252 (should
1253 (equal
1254 '(?X ?Z ?Y)
1255 (org-test-with-temp-text "#+PRIORITIES: X Z Y"
1256 (org-mode-restart)
1257 (list org-highest-priority org-lowest-priority org-default-priority))))
1258 (should
1259 (equal
1260 '(?A ?C ?B)
1261 (org-test-with-temp-text "#+PRIORITIES: X Z"
1262 (org-mode-restart)
1263 (list org-highest-priority org-lowest-priority org-default-priority))))
1264 ;; STARTUP keyword.
1265 (should
1266 (equal '(t t)
1267 (org-test-with-temp-text "#+STARTUP: fold odd"
1268 (org-mode-restart)
1269 (list org-startup-folded org-odd-levels-only))))
1270 ;; TODO keywords.
1271 (should
1272 (equal '(("A" "B") ("C"))
1273 (org-test-with-temp-text "#+TODO: A B | C"
1274 (org-mode-restart)
1275 (list org-not-done-keywords org-done-keywords))))
1276 (should
1277 (equal '(("A" "C") ("B" "D"))
1278 (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
1279 (org-mode-restart)
1280 (list org-not-done-keywords org-done-keywords))))
1281 (should
1282 (equal '(("A" "B") ("C"))
1283 (org-test-with-temp-text "#+TYP_TODO: A B | C"
1284 (org-mode-restart)
1285 (list org-not-done-keywords org-done-keywords))))
1286 (should
1287 (equal '((:startgroup) ("A" . ?a) (:endgroup))
1288 (org-test-with-temp-text "#+TODO: A(a)"
1289 (org-mode-restart)
1290 org-todo-key-alist)))
1291 (should
1292 (equal '(("D" note nil) ("C" time nil) ("B" note time))
1293 (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
1294 (org-mode-restart)
1295 org-todo-log-states)))
1296 ;; Enter SETUPFILE keyword.
1297 (should
1298 (equal "1"
1299 (org-test-with-temp-text
1300 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
1301 (org-mode-restart)
1302 (cdr (assoc "a" org-file-properties))))))
1306 ;;; Links
1308 ;;;; Coderefs
1310 (ert-deftest test-org/coderef ()
1311 "Test coderef links specifications."
1312 (should
1313 (org-test-with-temp-text "
1314 #+BEGIN_SRC emacs-lisp
1315 \(+ 1 1) (ref:sc)
1316 #+END_SRC
1317 \[[(sc)]]"
1318 (goto-char (point-max))
1319 (org-open-at-point)
1320 (looking-at "(ref:sc)"))))
1322 ;;;; Custom ID
1324 (ert-deftest test-org/custom-id ()
1325 "Test custom ID links specifications."
1326 (should
1327 (org-test-with-temp-text
1328 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
1329 (goto-char (point-max))
1330 (org-open-at-point)
1331 (org-looking-at-p "\\* H1")))
1332 ;; Ignore false positives.
1333 (should-not
1334 (org-test-with-temp-text
1335 "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
1336 (goto-char (point-max))
1337 (let (org-link-search-must-match-exact-headline) (org-open-at-point))
1338 (org-looking-at-p "\\* H1"))))
1340 ;;;; Fuzzy Links
1342 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
1343 ;; a named element (#+name: text) and to headlines (* Text).
1345 (ert-deftest test-org/fuzzy-links ()
1346 "Test fuzzy links specifications."
1347 ;; Fuzzy link goes in priority to a matching target.
1348 (should
1349 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
1350 (goto-line 5)
1351 (org-open-at-point)
1352 (looking-at "<<Test>>")))
1353 ;; Then fuzzy link points to an element with a given name.
1354 (should
1355 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
1356 (goto-line 5)
1357 (org-open-at-point)
1358 (looking-at "#\\+NAME: Test")))
1359 ;; A target still lead to a matching headline otherwise.
1360 (should
1361 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
1362 (goto-line 4)
1363 (org-open-at-point)
1364 (looking-at "\\* Head2")))
1365 ;; With a leading star in link, enforce heading match.
1366 (should
1367 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
1368 (goto-line 3)
1369 (org-open-at-point)
1370 (looking-at "\\* Test")))
1371 ;; With a leading star in link, enforce exact heading match, even
1372 ;; with `org-link-search-must-match-exact-headline' set to nil.
1373 (should-error
1374 (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
1375 (let ((org-link-search-must-match-exact-headline nil))
1376 (org-open-at-point))))
1377 ;; Heading match should not care about spaces, cookies, todo
1378 ;; keywords, priorities, and tags.
1379 (should
1380 (let ((first-line
1381 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1382 (org-test-with-temp-text
1383 (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
1384 (let ((org-link-search-must-match-exact-headline nil)
1385 (org-todo-regexp "TODO"))
1386 (org-open-at-point))
1387 (looking-at (regexp-quote first-line)))))
1388 ;; Heading match should still be exact.
1389 (should-error
1390 (let ((first-line
1391 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1392 (org-test-with-temp-text
1393 (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
1394 (let ((org-link-search-must-match-exact-headline nil)
1395 (org-todo-regexp "TODO"))
1396 (org-open-at-point)))))
1397 ;; Heading match ignores COMMENT keyword.
1398 (should
1399 (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
1400 (org-open-at-point)
1401 (looking-at "\\* COMMENT Test")))
1402 ;; Correctly un-hexify fuzzy links.
1403 (should
1404 (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
1405 (goto-char (point-max))
1406 (org-open-at-point)
1407 (bobp))))
1409 ;;;; Link Escaping
1411 (ert-deftest test-org/org-link-escape-ascii-character ()
1412 "Escape an ascii character."
1413 (should
1414 (string=
1415 "%5B"
1416 (org-link-escape "["))))
1418 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
1419 "Escape an ascii control character."
1420 (should
1421 (string=
1422 "%09"
1423 (org-link-escape "\t"))))
1425 (ert-deftest test-org/org-link-escape-multibyte-character ()
1426 "Escape an unicode multibyte character."
1427 (should
1428 (string=
1429 "%E2%82%AC"
1430 (org-link-escape "€"))))
1432 (ert-deftest test-org/org-link-escape-custom-table ()
1433 "Escape string with custom character table."
1434 (should
1435 (string=
1436 "Foo%3A%42ar%0A"
1437 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
1439 (ert-deftest test-org/org-link-escape-custom-table-merge ()
1440 "Escape string with custom table merged with default table."
1441 (should
1442 (string=
1443 "%5BF%6F%6F%3A%42ar%0A%5D"
1444 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
1446 (ert-deftest test-org/org-link-unescape-ascii-character ()
1447 "Unescape an ascii character."
1448 (should
1449 (string=
1451 (org-link-unescape "%5B"))))
1453 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
1454 "Unescpae an ascii control character."
1455 (should
1456 (string=
1457 "\n"
1458 (org-link-unescape "%0A"))))
1460 (ert-deftest test-org/org-link-unescape-multibyte-character ()
1461 "Unescape unicode multibyte character."
1462 (should
1463 (string=
1464 "€"
1465 (org-link-unescape "%E2%82%AC"))))
1467 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
1468 "Unescape old style percent escaped character."
1469 (should
1470 (string=
1471 "àâçèéêîôùû"
1472 (decode-coding-string
1473 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
1475 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
1476 "Escape and unescape a URL that includes an escaped char.
1477 http://article.gmane.org/gmane.emacs.orgmode/21459/"
1478 (should
1479 (string=
1480 "http://some.host.com/form?&id=blah%2Bblah25"
1481 (org-link-unescape
1482 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
1484 (ert-deftest test-org/org-link-escape-chars-browser ()
1485 "Test of the constant `org-link-escape-chars-browser'.
1486 See there why this test is a candidate to be removed once Org
1487 drops support for Emacs 24.1 and 24.2."
1488 (should
1489 (string=
1490 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1491 "%22Release%208.2%22&idxname=emacs-orgmode")
1492 (org-link-escape-browser ; Do not replace with `url-encode-url',
1493 ; see docstring above.
1494 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1495 "\"Release 8.2\"&idxname=emacs-orgmode")))))
1497 ;;;; Open at point
1499 (ert-deftest test-org/open-at-point-in-keyword ()
1500 "Does `org-open-at-point' open link in a keyword line?"
1501 (should
1502 (org-test-with-temp-text
1503 "#+KEYWORD: <point>[[info:emacs#Top]]"
1504 (org-open-at-point) t)))
1506 (ert-deftest test-org/open-at-point-in-property ()
1507 "Does `org-open-at-point' open link in property drawer?"
1508 (should
1509 (org-test-with-temp-text
1510 "* Headline
1511 :PROPERTIES:
1512 :URL: <point>[[info:emacs#Top]]
1513 :END:"
1514 (org-open-at-point) t)))
1516 (ert-deftest test-org/open-at-point-in-comment ()
1517 "Does `org-open-at-point' open link in a commented line?"
1518 (should
1519 (org-test-with-temp-text
1520 "# <point>[[info:emacs#Top]]"
1521 (org-open-at-point) t)))
1523 (ert-deftest test-org/open-at-point/info ()
1524 "Test `org-open-at-point' on info links."
1525 (should
1526 (org-test-with-temp-text
1527 "<point>[[info:emacs#Top]]"
1528 (org-open-at-point)
1529 (and (switch-to-buffer "*info*")
1530 (prog1
1531 (looking-at "\nThe Emacs Editor")
1532 (kill-buffer))))))
1534 (ert-deftest test-org/open-at-point/inline-image ()
1535 "Test `org-open-at-point' on nested links."
1536 (should
1537 (org-test-with-temp-text "[[info:org#Top][info:<point>emacs#Top]]"
1538 (org-open-at-point)
1539 (prog1 (with-current-buffer "*info*" (looking-at "\nOrg Mode Manual"))
1540 (kill-buffer "*info*")))))
1543 ;;; Node Properties
1545 (ert-deftest test-org/accumulated-properties-in-drawers ()
1546 "Ensure properties accumulate in subtree drawers."
1547 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
1548 (org-babel-next-src-block)
1549 (should (equal '(2 1) (org-babel-execute-src-block)))))
1551 (ert-deftest test-org/custom-properties ()
1552 "Test custom properties specifications."
1553 ;; Standard test.
1554 (should
1555 (let ((org-custom-properties '("FOO")))
1556 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1557 (org-toggle-custom-properties-visibility)
1558 (org-invisible-p2))))
1559 ;; Properties are case-insensitive.
1560 (should
1561 (let ((org-custom-properties '("FOO")))
1562 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
1563 (org-toggle-custom-properties-visibility)
1564 (org-invisible-p2))))
1565 (should
1566 (let ((org-custom-properties '("foo")))
1567 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1568 (org-toggle-custom-properties-visibility)
1569 (org-invisible-p2))))
1570 ;; Multiple custom properties in the same drawer.
1571 (should
1572 (let ((org-custom-properties '("FOO" "BAR")))
1573 (org-test-with-temp-text
1574 "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
1575 (org-toggle-custom-properties-visibility)
1576 (and (org-invisible-p2)
1577 (not (progn (forward-line) (org-invisible-p2)))
1578 (progn (forward-line) (org-invisible-p2))))))
1579 ;; Hide custom properties with an empty value.
1580 (should
1581 (let ((org-custom-properties '("FOO")))
1582 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
1583 (org-toggle-custom-properties-visibility)
1584 (org-invisible-p2))))
1585 ;; Do not hide fake properties.
1586 (should-not
1587 (let ((org-custom-properties '("FOO")))
1588 (org-test-with-temp-text ":FOO: val\n"
1589 (org-toggle-custom-properties-visibility)
1590 (org-invisible-p2))))
1591 (should-not
1592 (let ((org-custom-properties '("A")))
1593 (org-test-with-temp-text
1594 "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
1595 (org-toggle-custom-properties-visibility)
1596 (org-invisible-p2)))))
1600 ;;; Mark Region
1602 (ert-deftest test-org/mark-subtree ()
1603 "Test `org-mark-subtree' specifications."
1604 ;; Error when point is before first headline.
1605 (should-error
1606 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
1607 (progn (transient-mark-mode 1)
1608 (org-mark-subtree))))
1609 ;; Without argument, mark current subtree.
1610 (should
1611 (equal
1612 '(12 32)
1613 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1614 (progn (transient-mark-mode 1)
1615 (forward-line 2)
1616 (org-mark-subtree)
1617 (list (region-beginning) (region-end))))))
1618 ;; With an argument, move ARG up.
1619 (should
1620 (equal
1621 '(1 32)
1622 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1623 (progn (transient-mark-mode 1)
1624 (forward-line 2)
1625 (org-mark-subtree 1)
1626 (list (region-beginning) (region-end))))))
1627 ;; Do not get fooled by inlinetasks.
1628 (when (featurep 'org-inlinetask)
1629 (should
1630 (= 1
1631 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
1632 (progn (transient-mark-mode 1)
1633 (forward-line 1)
1634 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
1635 (region-beginning)))))))
1639 ;;; Navigation
1641 (ert-deftest test-org/end-of-meta-data ()
1642 "Test `org-end-of-meta-data' specifications."
1643 ;; Skip planning line.
1644 (should
1645 (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
1646 (org-end-of-meta-data)
1647 (eobp)))
1648 ;; Skip properties drawer.
1649 (should
1650 (org-test-with-temp-text
1651 "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
1652 (org-end-of-meta-data)
1653 (eobp)))
1654 ;; Skip both.
1655 (should
1656 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
1657 (org-end-of-meta-data)
1658 (eobp)))
1659 ;; Nothing to skip, go to first line.
1660 (should
1661 (org-test-with-temp-text "* Headline\nContents"
1662 (org-end-of-meta-data)
1663 (looking-at "Contents")))
1664 ;; With option argument, skip empty lines, regular drawers and
1665 ;; clocking lines.
1666 (should
1667 (org-test-with-temp-text "* Headline\n\nContents"
1668 (org-end-of-meta-data t)
1669 (looking-at "Contents")))
1670 (should
1671 (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
1672 (org-end-of-meta-data t)
1673 (looking-at "Contents")))
1674 (should
1675 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
1676 (org-end-of-meta-data t)
1677 (looking-at "Contents")))
1678 ;; Special case: do not skip incomplete drawers.
1679 (should
1680 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
1681 (org-end-of-meta-data t)
1682 (looking-at ":LOGBOOK:")))
1683 ;; Special case: Be careful about consecutive headlines.
1684 (should-not
1685 (org-test-with-temp-text "* H1\n*H2\nContents"
1686 (org-end-of-meta-data t)
1687 (looking-at "Contents"))))
1689 (ert-deftest test-org/beginning-of-line ()
1690 "Test `org-beginning-of-line' specifications."
1691 ;; Standard test.
1692 (should
1693 (org-test-with-temp-text "Some text\nSome other text"
1694 (progn (org-beginning-of-line) (bolp))))
1695 ;; Standard test with `visual-line-mode'.
1696 (should-not
1697 (org-test-with-temp-text "A long line of text\nSome other text"
1698 (progn (visual-line-mode)
1699 (forward-char 2)
1700 (dotimes (i 1000) (insert "very "))
1701 (org-beginning-of-line)
1702 (bolp))))
1703 ;; At an headline with special movement.
1704 (should
1705 (org-test-with-temp-text "* TODO Headline"
1706 (let ((org-special-ctrl-a/e t))
1707 (org-end-of-line)
1708 (and (progn (org-beginning-of-line) (looking-at "Headline"))
1709 (progn (org-beginning-of-line) (bolp))
1710 (progn (org-beginning-of-line) (looking-at "Headline")))))))
1712 (ert-deftest test-org/end-of-line ()
1713 "Test `org-end-of-line' specifications."
1714 ;; Standard test.
1715 (should
1716 (org-test-with-temp-text "Some text\nSome other text"
1717 (progn (org-end-of-line) (eolp))))
1718 ;; Standard test with `visual-line-mode'.
1719 (should-not
1720 (org-test-with-temp-text "A long line of text\nSome other text"
1721 (progn (visual-line-mode)
1722 (forward-char 2)
1723 (dotimes (i 1000) (insert "very "))
1724 (goto-char (point-min))
1725 (org-end-of-line)
1726 (eolp))))
1727 ;; At an headline with special movement.
1728 (should
1729 (org-test-with-temp-text "* Headline1 :tag:\n"
1730 (let ((org-special-ctrl-a/e t))
1731 (and (progn (org-end-of-line) (looking-at " :tag:"))
1732 (progn (org-end-of-line) (eolp))
1733 (progn (org-end-of-line) (looking-at " :tag:"))))))
1734 ;; At an headline without special movement.
1735 (should
1736 (org-test-with-temp-text "* Headline2 :tag:\n"
1737 (let ((org-special-ctrl-a/e nil))
1738 (and (progn (org-end-of-line) (eolp))
1739 (progn (org-end-of-line) (eolp))))))
1740 ;; At an headline, with reversed movement.
1741 (should
1742 (org-test-with-temp-text "* Headline3 :tag:\n"
1743 (let ((org-special-ctrl-a/e 'reversed)
1744 (this-command last-command))
1745 (and (progn (org-end-of-line) (eolp))
1746 (progn (org-end-of-line) (looking-at " :tag:"))))))
1747 ;; At a block without hidden contents.
1748 (should
1749 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1750 (progn (org-end-of-line) (eolp))))
1751 ;; At a block with hidden contents.
1752 (should-not
1753 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1754 (let ((org-special-ctrl-a/e t))
1755 (org-hide-block-toggle)
1756 (org-end-of-line)
1757 (eobp)))))
1759 (ert-deftest test-org/forward-sentence ()
1760 "Test `org-forward-sentence' specifications."
1761 ;; At the end of a table cell, move to the end of the next one.
1762 (should
1763 (org-test-with-temp-text "| a<point> | b |"
1764 (org-forward-sentence)
1765 (looking-at " |$")))
1766 ;; Elsewhere in a cell, move to its end.
1767 (should
1768 (org-test-with-temp-text "| a<point>c | b |"
1769 (org-forward-sentence)
1770 (looking-at " | b |$")))
1771 ;; Otherwise, simply call `forward-sentence'.
1772 (should
1773 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
1774 (org-forward-sentence)
1775 (looking-at " Sentence 2.")))
1776 (should
1777 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
1778 (org-forward-sentence)
1779 (org-forward-sentence)
1780 (eobp)))
1781 ;; At the end of an element, jump to the next one, without stopping
1782 ;; on blank lines in-between.
1783 (should
1784 (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
1785 (org-forward-sentence)
1786 (eobp))))
1788 (ert-deftest test-org/backward-sentence ()
1789 "Test `org-backward-sentence' specifications."
1790 ;; At the beginning of a table cell, move to the beginning of the
1791 ;; previous one.
1792 (should
1793 (org-test-with-temp-text "| a | <point>b |"
1794 (org-backward-sentence)
1795 (looking-at "a | b |$")))
1796 ;; Elsewhere in a cell, move to its beginning.
1797 (should
1798 (org-test-with-temp-text "| a | b<point>c |"
1799 (org-backward-sentence)
1800 (looking-at "bc |$")))
1801 ;; Otherwise, simply call `backward-sentence'.
1802 (should
1803 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
1804 (org-backward-sentence)
1805 (looking-at "Sentence 2.")))
1806 (should
1807 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
1808 (org-backward-sentence)
1809 (org-backward-sentence)
1810 (bobp)))
1811 ;; Make sure to hit the beginning of a sentence on the same line as
1812 ;; an item.
1813 (should
1814 (org-test-with-temp-text "- Line 1\n line <point>2."
1815 (org-backward-sentence)
1816 (looking-at "Line 1"))))
1818 (ert-deftest test-org/forward-paragraph ()
1819 "Test `org-forward-paragraph' specifications."
1820 ;; At end of buffer, return an error.
1821 (should-error
1822 (org-test-with-temp-text "Paragraph"
1823 (goto-char (point-max))
1824 (org-forward-paragraph)))
1825 ;; Standard test.
1826 (should
1827 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1828 (org-forward-paragraph)
1829 (looking-at "P2")))
1830 ;; Ignore depth.
1831 (should
1832 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
1833 (org-forward-paragraph)
1834 (looking-at "P1")))
1835 ;; Do not enter elements with invisible contents.
1836 (should
1837 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
1838 (org-hide-block-toggle)
1839 (org-forward-paragraph)
1840 (looking-at "P3")))
1841 ;; On an affiliated keyword, jump to the beginning of the element.
1842 (should
1843 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
1844 (org-forward-paragraph)
1845 (looking-at "Para")))
1846 ;; On an item or a footnote definition, move to the second element
1847 ;; inside, if any.
1848 (should
1849 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
1850 (org-forward-paragraph)
1851 (looking-at " Paragraph")))
1852 (should
1853 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
1854 (org-forward-paragraph)
1855 (looking-at "Paragraph")))
1856 ;; On an item, or a footnote definition, when the first line is
1857 ;; empty, move to the first item.
1858 (should
1859 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
1860 (org-forward-paragraph)
1861 (looking-at " Paragraph")))
1862 (should
1863 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
1864 (org-forward-paragraph)
1865 (looking-at "Paragraph")))
1866 ;; On a table (resp. a property drawer) do not move through table
1867 ;; rows (resp. node properties).
1868 (should
1869 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
1870 (org-forward-paragraph)
1871 (looking-at "Paragraph")))
1872 (should
1873 (org-test-with-temp-text
1874 "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
1875 (org-forward-paragraph)
1876 (looking-at "Paragraph")))
1877 ;; On a verse or source block, stop after blank lines.
1878 (should
1879 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
1880 (org-forward-paragraph)
1881 (looking-at "L2")))
1882 (should
1883 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
1884 (org-forward-paragraph)
1885 (looking-at "L2"))))
1887 (ert-deftest test-org/backward-paragraph ()
1888 "Test `org-backward-paragraph' specifications."
1889 ;; Error at beginning of buffer.
1890 (should-error
1891 (org-test-with-temp-text "Paragraph"
1892 (org-backward-paragraph)))
1893 ;; Regular test.
1894 (should
1895 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1896 (goto-char (point-max))
1897 (org-backward-paragraph)
1898 (looking-at "P3")))
1899 (should
1900 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1901 (goto-char (point-max))
1902 (beginning-of-line)
1903 (org-backward-paragraph)
1904 (looking-at "P2")))
1905 ;; Ignore depth.
1906 (should
1907 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
1908 (goto-char (point-max))
1909 (beginning-of-line)
1910 (org-backward-paragraph)
1911 (looking-at "P2")))
1912 ;; Ignore invisible elements.
1913 (should
1914 (org-test-with-temp-text "* H1\n P1\n* H2"
1915 (org-cycle)
1916 (goto-char (point-max))
1917 (beginning-of-line)
1918 (org-backward-paragraph)
1919 (bobp)))
1920 ;; On an affiliated keyword, jump to the first one.
1921 (should
1922 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
1923 (search-forward "c2")
1924 (org-backward-paragraph)
1925 (looking-at "#\\+name")))
1926 ;; On the second element in an item or a footnote definition, jump
1927 ;; to item or the definition.
1928 (should
1929 (org-test-with-temp-text "- line1\n\n line2"
1930 (goto-char (point-max))
1931 (beginning-of-line)
1932 (org-backward-paragraph)
1933 (looking-at "- line1")))
1934 (should
1935 (org-test-with-temp-text "[fn:1] line1\n\n line2"
1936 (goto-char (point-max))
1937 (beginning-of-line)
1938 (org-backward-paragraph)
1939 (looking-at "\\[fn:1\\] line1")))
1940 ;; On a table (resp. a property drawer), ignore table rows
1941 ;; (resp. node properties).
1942 (should
1943 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
1944 (goto-char (point-max))
1945 (beginning-of-line)
1946 (org-backward-paragraph)
1947 (bobp)))
1948 (should
1949 (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
1950 (org-backward-paragraph)
1951 (looking-at ":PROPERTIES:")))
1952 ;; On a source or verse block, stop before blank lines.
1953 (should
1954 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
1955 (search-forward "L3")
1956 (beginning-of-line)
1957 (org-backward-paragraph)
1958 (looking-at "L2")))
1959 (should
1960 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
1961 (search-forward "L3")
1962 (beginning-of-line)
1963 (org-backward-paragraph)
1964 (looking-at "L2"))))
1966 (ert-deftest test-org/forward-element ()
1967 "Test `org-forward-element' specifications."
1968 ;; 1. At EOB: should error.
1969 (org-test-with-temp-text "Some text\n"
1970 (goto-char (point-max))
1971 (should-error (org-forward-element)))
1972 ;; 2. Standard move: expected to ignore blank lines.
1973 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
1974 (org-forward-element)
1975 (should (looking-at (regexp-quote "Second paragraph."))))
1976 ;; 3. Headline tests.
1977 (org-test-with-temp-text "
1978 * Head 1
1979 ** Head 1.1
1980 *** Head 1.1.1
1981 ** Head 1.2"
1982 ;; 3.1. At an headline beginning: move to next headline at the
1983 ;; same level.
1984 (goto-line 3)
1985 (org-forward-element)
1986 (should (looking-at (regexp-quote "** Head 1.2")))
1987 ;; 3.2. At an headline beginning: move to parent headline if no
1988 ;; headline at the same level.
1989 (goto-line 3)
1990 (org-forward-element)
1991 (should (looking-at (regexp-quote "** Head 1.2"))))
1992 ;; 4. Greater element tests.
1993 (org-test-with-temp-text
1994 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
1995 ;; 4.1. At a greater element: expected to skip contents.
1996 (org-forward-element)
1997 (should (looking-at (regexp-quote "Outside.")))
1998 ;; 4.2. At the end of greater element contents: expected to skip
1999 ;; to the end of the greater element.
2000 (goto-line 2)
2001 (org-forward-element)
2002 (should (looking-at (regexp-quote "Outside."))))
2003 ;; 5. List tests.
2004 (org-test-with-temp-text "
2005 - item1
2007 - sub1
2009 - sub2
2011 - sub3
2013 Inner paragraph.
2015 - item2
2017 Outside."
2018 ;; 5.1. At list top point: expected to move to the element after
2019 ;; the list.
2020 (goto-line 2)
2021 (org-forward-element)
2022 (should (looking-at (regexp-quote "Outside.")))
2023 ;; 5.2. Special case: at the first line of a sub-list, but not at
2024 ;; beginning of line, move to next item.
2025 (goto-line 2)
2026 (forward-char)
2027 (org-forward-element)
2028 (should (looking-at "- item2"))
2029 (goto-line 4)
2030 (forward-char)
2031 (org-forward-element)
2032 (should (looking-at " - sub2"))
2033 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
2034 (goto-line 4)
2035 (org-forward-element)
2036 (should (looking-at (regexp-quote " Inner paragraph.")))
2037 ;; 5.4. At sub-list end: expected to move outside the sub-list.
2038 (goto-line 8)
2039 (org-forward-element)
2040 (should (looking-at (regexp-quote " Inner paragraph.")))
2041 ;; 5.5. At an item: expected to move to next item, if any.
2042 (goto-line 6)
2043 (org-forward-element)
2044 (should (looking-at " - sub3"))))
2046 (ert-deftest test-org/backward-element ()
2047 "Test `org-backward-element' specifications."
2048 ;; 1. Should error at BOB.
2049 (org-test-with-temp-text " \nParagraph."
2050 (should-error (org-backward-element)))
2051 ;; 2. Should move at BOB when called on the first element in buffer.
2052 (should
2053 (org-test-with-temp-text "\n#+TITLE: test"
2054 (progn (forward-line)
2055 (org-backward-element)
2056 (bobp))))
2057 ;; 3. Not at the beginning of an element: move at its beginning.
2058 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2059 (goto-line 3)
2060 (end-of-line)
2061 (org-backward-element)
2062 (should (looking-at (regexp-quote "Paragraph2."))))
2063 ;; 4. Headline tests.
2064 (org-test-with-temp-text "
2065 * Head 1
2066 ** Head 1.1
2067 *** Head 1.1.1
2068 ** Head 1.2"
2069 ;; 4.1. At an headline beginning: move to previous headline at the
2070 ;; same level.
2071 (goto-line 5)
2072 (org-backward-element)
2073 (should (looking-at (regexp-quote "** Head 1.1")))
2074 ;; 4.2. At an headline beginning: move to parent headline if no
2075 ;; headline at the same level.
2076 (goto-line 3)
2077 (org-backward-element)
2078 (should (looking-at (regexp-quote "* Head 1")))
2079 ;; 4.3. At the first top-level headline: should error.
2080 (goto-line 2)
2081 (should-error (org-backward-element)))
2082 ;; 5. At beginning of first element inside a greater element:
2083 ;; expected to move to greater element's beginning.
2084 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
2085 (goto-line 3)
2086 (org-backward-element)
2087 (should (looking-at "#\\+BEGIN_CENTER")))
2088 ;; 6. At the beginning of the first element in a section: should
2089 ;; move back to headline, if any.
2090 (should
2091 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
2092 (progn (goto-char (point-max))
2093 (beginning-of-line)
2094 (org-backward-element)
2095 (org-at-heading-p))))
2096 ;; 7. List tests.
2097 (org-test-with-temp-text "
2098 - item1
2100 - sub1
2102 - sub2
2104 - sub3
2106 Inner paragraph.
2108 - item2
2111 Outside."
2112 ;; 7.1. At beginning of sub-list: expected to move to the
2113 ;; paragraph before it.
2114 (goto-line 4)
2115 (org-backward-element)
2116 (should (looking-at "item1"))
2117 ;; 7.2. At an item in a list: expected to move at previous item.
2118 (goto-line 8)
2119 (org-backward-element)
2120 (should (looking-at " - sub2"))
2121 (goto-line 12)
2122 (org-backward-element)
2123 (should (looking-at "- item1"))
2124 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
2125 ;; beginning.
2126 (goto-line 10)
2127 (org-backward-element)
2128 (should (looking-at " - sub1"))
2129 (goto-line 15)
2130 (org-backward-element)
2131 (should (looking-at "- item1"))
2132 ;; 7.4. At blank-lines before list end: expected to move to top
2133 ;; item.
2134 (goto-line 14)
2135 (org-backward-element)
2136 (should (looking-at "- item1"))))
2138 (ert-deftest test-org/up-element ()
2139 "Test `org-up-element' specifications."
2140 ;; 1. At BOB or with no surrounding element: should error.
2141 (org-test-with-temp-text "Paragraph."
2142 (should-error (org-up-element)))
2143 (org-test-with-temp-text "* Head1\n* Head2"
2144 (goto-line 2)
2145 (should-error (org-up-element)))
2146 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2147 (goto-line 3)
2148 (should-error (org-up-element)))
2149 ;; 2. At an headline: move to parent headline.
2150 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
2151 (goto-line 3)
2152 (org-up-element)
2153 (should (looking-at "\\* Head1")))
2154 ;; 3. Inside a greater element: move to greater element beginning.
2155 (org-test-with-temp-text
2156 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
2157 (goto-line 3)
2158 (org-up-element)
2159 (should (looking-at "#\\+BEGIN_CENTER")))
2160 ;; 4. List tests.
2161 (org-test-with-temp-text "* Top
2162 - item1
2164 - sub1
2166 - sub2
2168 Paragraph within sub2.
2170 - item2"
2171 ;; 4.1. Within an item: move to the item beginning.
2172 (goto-line 8)
2173 (org-up-element)
2174 (should (looking-at " - sub2"))
2175 ;; 4.2. At an item in a sub-list: move to parent item.
2176 (goto-line 4)
2177 (org-up-element)
2178 (should (looking-at "- item1"))
2179 ;; 4.3. At an item in top list: move to beginning of whole list.
2180 (goto-line 10)
2181 (org-up-element)
2182 (should (looking-at "- item1"))
2183 ;; 4.4. Special case. At very top point: should move to parent of
2184 ;; list.
2185 (goto-line 2)
2186 (org-up-element)
2187 (should (looking-at "\\* Top"))))
2189 (ert-deftest test-org/down-element ()
2190 "Test `org-down-element' specifications."
2191 ;; Error when the element hasn't got a recursive type.
2192 (org-test-with-temp-text "Paragraph."
2193 (should-error (org-down-element)))
2194 ;; Error when the element has no contents
2195 (org-test-with-temp-text "* Headline"
2196 (should-error (org-down-element)))
2197 ;; When at a plain-list, move to first item.
2198 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
2199 (goto-line 2)
2200 (org-down-element)
2201 (should (looking-at " - Item 1.1")))
2202 (org-test-with-temp-text "#+NAME: list\n- Item 1"
2203 (org-down-element)
2204 (should (looking-at " Item 1")))
2205 ;; When at a table, move to first row
2206 (org-test-with-temp-text "#+NAME: table\n| a | b |"
2207 (org-down-element)
2208 (should (looking-at " a | b |")))
2209 ;; Otherwise, move inside the greater element.
2210 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
2211 (org-down-element)
2212 (should (looking-at "Paragraph"))))
2214 (ert-deftest test-org/drag-element-backward ()
2215 "Test `org-drag-element-backward' specifications."
2216 ;; Standard test.
2217 (should
2218 (equal
2219 "#+key2: val2\n#+key1: val1\n#+key3: val3"
2220 (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
2221 (org-drag-element-backward)
2222 (buffer-string))))
2223 (should
2224 (equal
2225 "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
2226 (org-test-with-temp-text
2227 "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
2228 (org-drag-element-backward)
2229 (buffer-string))))
2230 ;; Preserve blank lines.
2231 (should
2232 (equal "Paragraph 2\n\n\nPara1\n\nPara3"
2233 (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
2234 (org-drag-element-backward)
2235 (buffer-string))))
2236 ;; Preserve column.
2237 (should
2238 (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
2239 (org-drag-element-backward)
2240 (org-looking-at-p "2")))
2241 ;; Error when trying to move first element of buffer.
2242 (should-error
2243 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2244 (org-drag-element-backward)))
2245 ;; Error when trying to swap nested elements.
2246 (should-error
2247 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2248 (forward-line)
2249 (org-drag-element-backward)))
2250 ;; Error when trying to swap an headline element and a non-headline
2251 ;; element.
2252 (should-error
2253 (org-test-with-temp-text "Test.\n* Head 1"
2254 (forward-line)
2255 (org-drag-element-backward)))
2256 ;; Preserve visibility of elements and their contents.
2257 (should
2258 (equal '((63 . 82) (26 . 48))
2259 (org-test-with-temp-text "
2260 #+BEGIN_CENTER
2261 Text.
2262 #+END_CENTER
2263 - item 1
2264 #+BEGIN_QUOTE
2265 Text.
2266 #+END_QUOTE"
2267 (while (search-forward "BEGIN_" nil t) (org-cycle))
2268 (search-backward "- item 1")
2269 (org-drag-element-backward)
2270 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2271 (overlays-in (point-min) (point-max)))))))
2273 (ert-deftest test-org/drag-element-forward ()
2274 "Test `org-drag-element-forward' specifications."
2275 ;; 1. Error when trying to move first element of buffer.
2276 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2277 (goto-line 3)
2278 (should-error (org-drag-element-forward)))
2279 ;; 2. Error when trying to swap nested elements.
2280 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2281 (forward-line)
2282 (should-error (org-drag-element-forward)))
2283 ;; 3. Error when trying to swap a non-headline element and an
2284 ;; headline.
2285 (org-test-with-temp-text "Test.\n* Head 1"
2286 (should-error (org-drag-element-forward)))
2287 ;; 4. Otherwise, swap elements, preserving column and blank lines
2288 ;; between elements.
2289 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
2290 (search-forward "graph")
2291 (org-drag-element-forward)
2292 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
2293 (should (looking-at " 1")))
2294 ;; 5. Preserve visibility of elements and their contents.
2295 (org-test-with-temp-text "
2296 #+BEGIN_CENTER
2297 Text.
2298 #+END_CENTER
2299 - item 1
2300 #+BEGIN_QUOTE
2301 Text.
2302 #+END_QUOTE"
2303 (while (search-forward "BEGIN_" nil t) (org-cycle))
2304 (search-backward "#+BEGIN_CENTER")
2305 (org-drag-element-forward)
2306 (should
2307 (equal
2308 '((63 . 82) (26 . 48))
2309 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2310 (overlays-in (point-min) (point-max)))))))
2313 ;;; Outline structure
2315 (ert-deftest test-org/demote ()
2316 "Test `org-demote' specifications."
2317 ;; Add correct number of stars according to `org-odd-levels-only'.
2318 (should
2319 (= 2
2320 (org-test-with-temp-text "* H"
2321 (let ((org-odd-levels-only nil)) (org-demote))
2322 (org-current-level))))
2323 (should
2324 (= 3
2325 (org-test-with-temp-text "* H"
2326 (let ((org-odd-levels-only t)) (org-demote))
2327 (org-current-level))))
2328 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2329 (should
2330 (org-test-with-temp-text "* H :tag:"
2331 (let ((org-tags-column 10)
2332 (org-auto-align-tags t)
2333 (org-odd-levels-only nil))
2334 (org-demote))
2335 (org-move-to-column 10)
2336 (org-looking-at-p ":tag:$")))
2337 (should-not
2338 (org-test-with-temp-text "* H :tag:"
2339 (let ((org-tags-column 10)
2340 (org-auto-align-tags nil)
2341 (org-odd-levels-only nil))
2342 (org-demote))
2343 (org-move-to-column 10)
2344 (org-looking-at-p ":tag:$")))
2345 ;; When `org-adapt-indentation' is non-nil, always indent planning
2346 ;; info and property drawers accordingly.
2347 (should
2348 (= 3
2349 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2350 (let ((org-odd-levels-only nil)
2351 (org-adapt-indentation t))
2352 (org-demote))
2353 (forward-line)
2354 (org-get-indentation))))
2355 (should
2356 (= 3
2357 (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2358 (let ((org-odd-levels-only nil)
2359 (org-adapt-indentation t))
2360 (org-demote))
2361 (forward-line)
2362 (org-get-indentation))))
2363 (should-not
2364 (= 3
2365 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2366 (let ((org-odd-levels-only nil)
2367 (org-adapt-indentation nil))
2368 (org-demote))
2369 (forward-line)
2370 (org-get-indentation))))
2371 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2372 ;; section accordingly. Ignore, however, footnote definitions and
2373 ;; inlinetasks boundaries.
2374 (should
2375 (= 3
2376 (org-test-with-temp-text "* H\n Paragraph"
2377 (let ((org-odd-levels-only nil)
2378 (org-adapt-indentation t))
2379 (org-demote))
2380 (forward-line)
2381 (org-get-indentation))))
2382 (should
2383 (= 2
2384 (org-test-with-temp-text "* H\n Paragraph"
2385 (let ((org-odd-levels-only nil)
2386 (org-adapt-indentation nil))
2387 (org-demote))
2388 (forward-line)
2389 (org-get-indentation))))
2390 (should
2391 (zerop
2392 (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
2393 (let ((org-odd-levels-only nil)
2394 (org-adapt-indentation t))
2395 (org-demote))
2396 (goto-char (point-max))
2397 (org-get-indentation))))
2398 (should
2399 (= 3
2400 (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
2401 (let ((org-odd-levels-only nil)
2402 (org-adapt-indentation t))
2403 (org-demote))
2404 (goto-char (point-max))
2405 (org-get-indentation))))
2406 (when (featurep 'org-inlinetask)
2407 (should
2408 (zerop
2409 (let ((org-inlinetask-min-level 5)
2410 (org-adapt-indentation t))
2411 (org-test-with-temp-text "* H\n***** I\n***** END"
2412 (org-demote)
2413 (forward-line)
2414 (org-get-indentation))))))
2415 (when (featurep 'org-inlinetask)
2416 (should
2417 (= 3
2418 (let ((org-inlinetask-min-level 5)
2419 (org-adapt-indentation t))
2420 (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
2421 (org-demote)
2422 (forward-line 2)
2423 (org-get-indentation))))))
2424 ;; Ignore contents of source blocks or example blocks when
2425 ;; indentation should be preserved (through
2426 ;; `org-src-preserve-indentation' or "-i" flag).
2427 (should-not
2428 (zerop
2429 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2430 (let ((org-adapt-indentation t)
2431 (org-src-preserve-indentation nil))
2432 (org-demote))
2433 (forward-line 2)
2434 (org-get-indentation))))
2435 (should
2436 (zerop
2437 (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2438 (let ((org-adapt-indentation t)
2439 (org-src-preserve-indentation t))
2440 (org-demote))
2441 (forward-line 2)
2442 (org-get-indentation))))
2443 (should
2444 (zerop
2445 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2446 (let ((org-adapt-indentation t)
2447 (org-src-preserve-indentation t))
2448 (org-demote))
2449 (forward-line 2)
2450 (org-get-indentation))))
2451 (should
2452 (zerop
2453 (org-test-with-temp-text
2454 "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
2455 (let ((org-adapt-indentation t)
2456 (org-src-preserve-indentation nil))
2457 (org-demote))
2458 (forward-line 2)
2459 (org-get-indentation)))))
2461 (ert-deftest test-org/promote ()
2462 "Test `org-promote' specifications."
2463 ;; Return an error if headline is to be promoted to level 0, unless
2464 ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
2465 ;; headline becomes a comment.
2466 (should-error
2467 (org-test-with-temp-text "* H"
2468 (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
2469 (should
2470 (equal "# H"
2471 (org-test-with-temp-text "* H"
2472 (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
2473 (buffer-string))))
2474 ;; Remove correct number of stars according to
2475 ;; `org-odd-levels-only'.
2476 (should
2477 (= 2
2478 (org-test-with-temp-text "*** H"
2479 (let ((org-odd-levels-only nil)) (org-promote))
2480 (org-current-level))))
2481 (should
2482 (= 1
2483 (org-test-with-temp-text "*** H"
2484 (let ((org-odd-levels-only t)) (org-promote))
2485 (org-current-level))))
2486 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2487 (should
2488 (org-test-with-temp-text "** H :tag:"
2489 (let ((org-tags-column 10)
2490 (org-auto-align-tags t)
2491 (org-odd-levels-only nil))
2492 (org-promote))
2493 (org-move-to-column 10)
2494 (org-looking-at-p ":tag:$")))
2495 (should-not
2496 (org-test-with-temp-text "** H :tag:"
2497 (let ((org-tags-column 10)
2498 (org-auto-align-tags nil)
2499 (org-odd-levels-only nil))
2500 (org-promote))
2501 (org-move-to-column 10)
2502 (org-looking-at-p ":tag:$")))
2503 ;; When `org-adapt-indentation' is non-nil, always indent planning
2504 ;; info and property drawers.
2505 (should
2506 (= 2
2507 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2508 (let ((org-odd-levels-only nil)
2509 (org-adapt-indentation t))
2510 (org-promote))
2511 (forward-line)
2512 (org-get-indentation))))
2513 (should
2514 (= 2
2515 (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2516 (let ((org-odd-levels-only nil)
2517 (org-adapt-indentation t))
2518 (org-promote))
2519 (forward-line)
2520 (org-get-indentation))))
2521 (should-not
2522 (= 2
2523 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2524 (let ((org-odd-levels-only nil)
2525 (org-adapt-indentation nil))
2526 (org-promote))
2527 (forward-line)
2528 (org-get-indentation))))
2529 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2530 ;; section accordingly. Ignore, however, footnote definitions and
2531 ;; inlinetasks boundaries.
2532 (should
2533 (= 2
2534 (org-test-with-temp-text "** H\n Paragraph"
2535 (let ((org-odd-levels-only nil)
2536 (org-adapt-indentation t))
2537 (org-promote))
2538 (forward-line)
2539 (org-get-indentation))))
2540 (should-not
2541 (= 2
2542 (org-test-with-temp-text "** H\n Paragraph"
2543 (let ((org-odd-levels-only nil)
2544 (org-adapt-indentation nil))
2545 (org-promote))
2546 (forward-line)
2547 (org-get-indentation))))
2548 (should
2549 (= 2
2550 (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
2551 (let ((org-odd-levels-only nil)
2552 (org-adapt-indentation t))
2553 (org-promote))
2554 (forward-line)
2555 (org-get-indentation))))
2556 (when (featurep 'org-inlinetask)
2557 (should
2558 (zerop
2559 (let ((org-inlinetask-min-level 5)
2560 (org-adapt-indentation t))
2561 (org-test-with-temp-text "** H\n***** I\n***** END"
2562 (org-promote)
2563 (forward-line)
2564 (org-get-indentation))))))
2565 (when (featurep 'org-inlinetask)
2566 (should
2567 (= 2
2568 (let ((org-inlinetask-min-level 5)
2569 (org-adapt-indentation t))
2570 (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
2571 (org-promote)
2572 (forward-line 2)
2573 (org-get-indentation))))))
2574 ;; Give up shifting if it would break document's structure
2575 ;; otherwise.
2576 (should
2577 (= 3
2578 (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
2579 (let ((org-odd-levels-only nil)
2580 (org-adapt-indentation t))
2581 (org-promote))
2582 (forward-line)
2583 (org-get-indentation))))
2584 (should
2585 (= 3
2586 (org-test-with-temp-text "** H\n Paragraph\n * list."
2587 (let ((org-odd-levels-only nil)
2588 (org-adapt-indentation t))
2589 (org-promote))
2590 (forward-line)
2591 (org-get-indentation))))
2592 ;; Ignore contents of source blocks or example blocks when
2593 ;; indentation should be preserved (through
2594 ;; `org-src-preserve-indentation' or "-i" flag).
2595 (should-not
2596 (zerop
2597 (org-test-with-temp-text
2598 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2599 (let ((org-adapt-indentation t)
2600 (org-src-preserve-indentation nil)
2601 (org-odd-levels-only nil))
2602 (org-promote))
2603 (forward-line)
2604 (org-get-indentation))))
2605 (should
2606 (zerop
2607 (org-test-with-temp-text
2608 "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
2609 (let ((org-adapt-indentation t)
2610 (org-src-preserve-indentation t)
2611 (org-odd-levels-only nil))
2612 (org-promote))
2613 (forward-line)
2614 (org-get-indentation))))
2615 (should
2616 (zerop
2617 (org-test-with-temp-text
2618 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2619 (let ((org-adapt-indentation t)
2620 (org-src-preserve-indentation t)
2621 (org-odd-levels-only nil))
2622 (org-promote))
2623 (forward-line)
2624 (org-get-indentation))))
2625 (should
2626 (zerop
2627 (org-test-with-temp-text
2628 "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
2629 (let ((org-adapt-indentation t)
2630 (org-src-preserve-indentation nil)
2631 (org-odd-levels-only nil))
2632 (org-promote))
2633 (forward-line)
2634 (org-get-indentation)))))
2637 ;;; Planning
2639 (ert-deftest test-org/at-planning-p ()
2640 "Test `org-at-planning-p' specifications."
2641 ;; Regular test.
2642 (should
2643 (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
2644 (org-at-planning-p)))
2645 (should-not
2646 (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
2647 (org-at-planning-p)))
2648 ;; Correctly find planning attached to inlinetasks.
2649 (when (featurep 'org-inlinetask)
2650 (should
2651 (org-test-with-temp-text
2652 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
2653 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2654 (should-not
2655 (org-test-with-temp-text
2656 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
2657 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2658 (should-not
2659 (org-test-with-temp-text
2660 "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
2661 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2662 (should-not
2663 (org-test-with-temp-text
2664 "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
2665 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
2668 ;;; Property API
2670 (ert-deftest test-org/buffer-property-keys ()
2671 "Test `org-buffer-property-keys' specifications."
2672 ;; Retrieve properties accross siblings.
2673 (should
2674 (equal '("A" "B")
2675 (org-test-with-temp-text "
2676 * H1
2677 :PROPERTIES:
2678 :A: 1
2679 :END:
2680 * H2
2681 :PROPERTIES:
2682 :B: 1
2683 :END:"
2684 (org-buffer-property-keys))))
2685 ;; Retrieve properties accross children.
2686 (should
2687 (equal '("A" "B")
2688 (org-test-with-temp-text "
2689 * H1
2690 :PROPERTIES:
2691 :A: 1
2692 :END:
2693 ** H2
2694 :PROPERTIES:
2695 :B: 1
2696 :END:"
2697 (org-buffer-property-keys))))
2698 ;; Retrieve muliple properties in the same drawer.
2699 (should
2700 (equal '("A" "B")
2701 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2702 (org-buffer-property-keys))))
2703 ;; Ignore extension symbol in property name.
2704 (should
2705 (equal '("A")
2706 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
2707 (org-buffer-property-keys))))
2708 ;; With non-nil COLUMNS, extract property names from columns.
2709 (should
2710 (equal '("A" "B")
2711 (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
2712 (org-buffer-property-keys nil nil t))))
2713 (should
2714 (equal '("A" "B" "COLUMNS")
2715 (org-test-with-temp-text
2716 "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
2717 (org-buffer-property-keys nil nil t)))))
2719 (ert-deftest test-org/property-values ()
2720 "Test `org-property-values' specifications."
2721 ;; Regular test.
2722 (should
2723 (equal '("2" "1")
2724 (org-test-with-temp-text
2725 "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
2726 (org-property-values "A"))))
2727 ;; Ignore empty values.
2728 (should-not
2729 (org-test-with-temp-text
2730 "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
2731 (org-property-values "A")))
2732 ;; Take into consideration extended values.
2733 (should
2734 (equal '("1 2")
2735 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
2736 (org-property-values "A")))))
2738 (ert-deftest test-org/find-property ()
2739 "Test `org-find-property' specifications."
2740 ;; Regular test.
2741 (should
2742 (= 1
2743 (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
2744 (org-find-property "prop"))))
2745 ;; Ignore false positives.
2746 (should
2747 (= 27
2748 (org-test-with-temp-text
2749 "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
2750 (org-find-property "A"))))
2751 ;; Return first entry found in buffer.
2752 (should
2753 (= 1
2754 (org-test-with-temp-text
2755 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
2756 (org-find-property "A"))))
2757 ;; Only search visible part of the buffer.
2758 (should
2759 (= 31
2760 (org-test-with-temp-text
2761 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
2762 (org-narrow-to-subtree)
2763 (org-find-property "A"))))
2764 ;; With optional argument, only find entries with a specific value.
2765 (should-not
2766 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2767 (org-find-property "A" "2")))
2768 (should
2769 (= 31
2770 (org-test-with-temp-text
2771 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
2772 (org-find-property "A" "2"))))
2773 ;; Use "nil" for explicit nil values.
2774 (should
2775 (= 31
2776 (org-test-with-temp-text
2777 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
2778 (org-find-property "A" "nil")))))
2780 (ert-deftest test-org/entry-delete ()
2781 "Test `org-entry-delete' specifications."
2782 ;; Regular test.
2783 (should
2784 (string-match
2785 " *:PROPERTIES:\n *:B: +2\n *:END:"
2786 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2787 (org-entry-delete (point) "A")
2788 (buffer-string))))
2789 ;; Also remove accumulated properties.
2790 (should-not
2791 (string-match
2792 ":A"
2793 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
2794 (org-entry-delete (point) "A")
2795 (buffer-string))))
2796 ;; When last property is removed, remove the property drawer.
2797 (should-not
2798 (string-match
2799 ":PROPERTIES:"
2800 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
2801 (org-entry-delete (point) "A")
2802 (buffer-string))))
2803 ;; Return a non-nil value when some property was removed.
2804 (should
2805 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2806 (org-entry-delete (point) "A")))
2807 (should-not
2808 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2809 (org-entry-delete (point) "C"))))
2811 (ert-deftest test-org/entry-get ()
2812 "Test `org-entry-get' specifications."
2813 ;; Regular test.
2814 (should
2815 (equal "1"
2816 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2817 (org-entry-get (point) "A"))))
2818 ;; Ignore case.
2819 (should
2820 (equal "1"
2821 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2822 (org-entry-get (point) "a"))))
2823 ;; Handle extended values, both before and after base value.
2824 (should
2825 (equal "1 2 3"
2826 (org-test-with-temp-text
2827 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
2828 (org-entry-get (point) "A"))))
2829 ;; Empty values are returned as the empty string.
2830 (should
2831 (equal ""
2832 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
2833 (org-entry-get (point) "A"))))
2834 ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
2835 ;; otherwise, return nil.
2836 (should-not
2837 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
2838 (org-entry-get (point) "A")))
2839 (should
2840 (equal "nil"
2841 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
2842 (org-entry-get (point) "A" nil t))))
2843 ;; Return nil when no property is found, independently on the
2844 ;; LITERAL-NIL argument.
2845 (should-not
2846 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2847 (org-entry-get (point) "B")))
2848 (should-not
2849 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2850 (org-entry-get (point) "B" nil t)))
2851 ;; Handle inheritance, when allowed.
2852 (should
2853 (equal
2855 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2856 (org-entry-get (point) "A" t))))
2857 (should
2858 (equal
2860 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2861 (let ((org-use-property-inheritance t))
2862 (org-entry-get (point) "A" 'selective)))))
2863 (should-not
2864 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2865 (let ((org-use-property-inheritance nil))
2866 (org-entry-get (point) "A" 'selective)))))
2868 (ert-deftest test-org/entry-properties ()
2869 "Test `org-entry-properties' specifications."
2870 ;; Get "ITEM" property.
2871 (should
2872 (equal "* H"
2873 (org-test-with-temp-text "* TODO H"
2874 (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
2875 (should
2876 (equal "* H"
2877 (org-test-with-temp-text "* TODO H"
2878 (cdr (assoc "ITEM" (org-entry-properties))))))
2879 ;; Get "TODO" property.
2880 (should
2881 (equal "TODO"
2882 (org-test-with-temp-text "* TODO H"
2883 (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
2884 (should
2885 (equal "TODO"
2886 (org-test-with-temp-text "* TODO H"
2887 (cdr (assoc "TODO" (org-entry-properties))))))
2888 (should-not
2889 (org-test-with-temp-text "* H"
2890 (assoc "TODO" (org-entry-properties nil "TODO"))))
2891 ;; Get "PRIORITY" property.
2892 (should
2893 (equal "A"
2894 (org-test-with-temp-text "* [#A] H"
2895 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
2896 (should
2897 (equal "A"
2898 (org-test-with-temp-text "* [#A] H"
2899 (cdr (assoc "PRIORITY" (org-entry-properties))))))
2900 (should-not
2901 (org-test-with-temp-text "* H"
2902 (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
2903 ;; Get "FILE" property.
2904 (should
2905 (org-test-with-temp-text-in-file "* H\nParagraph"
2906 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
2907 (buffer-file-name))))
2908 (should
2909 (org-test-with-temp-text-in-file "* H\nParagraph"
2910 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
2911 (buffer-file-name))))
2912 (should-not
2913 (org-test-with-temp-text "* H\nParagraph"
2914 (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
2915 ;; Get "TAGS" property.
2916 (should
2917 (equal ":tag1:tag2:"
2918 (org-test-with-temp-text "* H :tag1:tag2:"
2919 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
2920 (should
2921 (equal ":tag1:tag2:"
2922 (org-test-with-temp-text "* H :tag1:tag2:"
2923 (cdr (assoc "TAGS" (org-entry-properties))))))
2924 (should-not
2925 (org-test-with-temp-text "* H"
2926 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
2927 ;; Get "ALLTAGS" property.
2928 (should
2929 (equal ":tag1:tag2:"
2930 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
2931 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
2932 (should
2933 (equal ":tag1:tag2:"
2934 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
2935 (cdr (assoc "ALLTAGS" (org-entry-properties))))))
2936 (should-not
2937 (org-test-with-temp-text "* H"
2938 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
2939 ;; Get "BLOCKED" property.
2940 (should
2941 (equal "t"
2942 (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
2943 (let ((org-enforce-todo-dependencies t)
2944 (org-blocker-hook
2945 '(org-block-todo-from-children-or-siblings-or-parent)))
2946 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
2947 (should
2948 (equal "t"
2949 (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
2950 (let ((org-enforce-todo-dependencies t)
2951 (org-blocker-hook
2952 '(org-block-todo-from-children-or-siblings-or-parent)))
2953 (cdr (assoc "BLOCKED" (org-entry-properties)))))))
2954 (should
2955 (equal ""
2956 (org-test-with-temp-text "* Blocked\n** DONE one\n** DONE two"
2957 (let ((org-enforce-todo-dependencies t))
2958 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
2959 ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
2960 (should
2961 (equal
2962 "[2012-03-29 thu.]"
2963 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
2964 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
2965 (should
2966 (equal
2967 "[2012-03-29 thu.]"
2968 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
2969 (cdr (assoc "CLOSED" (org-entry-properties))))))
2970 (should-not
2971 (org-test-with-temp-text "* H"
2972 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
2973 (should
2974 (equal
2975 "<2014-03-04 tue.>"
2976 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
2977 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
2978 (should
2979 (equal
2980 "<2014-03-04 tue.>"
2981 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
2982 (cdr (assoc "DEADLINE" (org-entry-properties))))))
2983 (should-not
2984 (org-test-with-temp-text "* H"
2985 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
2986 (should
2987 (equal
2988 "<2014-03-04 tue.>"
2989 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2990 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
2991 (should
2992 (equal
2993 "<2014-03-04 tue.>"
2994 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
2995 (cdr (assoc "SCHEDULED" (org-entry-properties))))))
2996 (should-not
2997 (org-test-with-temp-text "* H"
2998 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
2999 ;; Get "CATEGORY"
3000 (should
3001 (equal "cat"
3002 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
3003 (cdr (assoc "CATEGORY" (org-entry-properties))))))
3004 (should
3005 (equal "cat"
3006 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
3007 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3008 (should
3009 (equal "cat"
3010 (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
3011 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3012 ;; Get standard properties.
3013 (should
3014 (equal "1"
3015 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3016 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3017 ;; Handle extended properties.
3018 (should
3019 (equal "1 2 3"
3020 (org-test-with-temp-text
3021 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
3022 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3023 (should
3024 (equal "1 2 3"
3025 (org-test-with-temp-text
3026 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
3027 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3028 ;; Ignore forbidden (special) properties.
3029 (should-not
3030 (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
3031 (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
3033 (ert-deftest test-org/entry-put ()
3034 "Test `org-entry-put' specifications."
3035 ;; Error when not a string or nil.
3036 (should-error
3037 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
3038 (org-entry-put 1 "test" 2)))
3039 ;; Set "TODO" property.
3040 (should
3041 (string-match (regexp-quote " TODO H")
3042 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
3043 (org-entry-put (point) "TODO" "TODO")
3044 (buffer-string))))
3045 (should
3046 (string-match (regexp-quote "* H")
3047 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
3048 (org-entry-put (point) "TODO" nil)
3049 (buffer-string))))
3050 ;; Set "PRIORITY" property.
3051 (should
3052 (equal "* [#A] H"
3053 (org-test-with-temp-text "* [#B] H"
3054 (org-entry-put (point) "PRIORITY" "A")
3055 (buffer-string))))
3056 (should
3057 (equal "* H"
3058 (org-test-with-temp-text "* [#B] H"
3059 (org-entry-put (point) "PRIORITY" nil)
3060 (buffer-string))))
3061 ;; Set "SCHEDULED" property.
3062 (should
3063 (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
3064 (org-test-with-temp-text "* H"
3065 (org-entry-put (point) "SCHEDULED" "2014-03-04")
3066 (buffer-string))))
3067 (should
3068 (string= "* H\n"
3069 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3070 (org-entry-put (point) "SCHEDULED" nil)
3071 (buffer-string))))
3072 (should
3073 (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
3074 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3075 (org-entry-put (point) "SCHEDULED" "earlier")
3076 (buffer-string))))
3077 (should
3078 (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
3079 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3080 (org-entry-put (point) "SCHEDULED" "later")
3081 (buffer-string))))
3082 ;; Set "DEADLINE" property.
3083 (should
3084 (string-match "^ *DEADLINE: <2014-03-04 .*?>"
3085 (org-test-with-temp-text "* H"
3086 (org-entry-put (point) "DEADLINE" "2014-03-04")
3087 (buffer-string))))
3088 (should
3089 (string= "* H\n"
3090 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3091 (org-entry-put (point) "DEADLINE" nil)
3092 (buffer-string))))
3093 (should
3094 (string-match "^ *DEADLINE: <2014-03-03 .*?>"
3095 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3096 (org-entry-put (point) "DEADLINE" "earlier")
3097 (buffer-string))))
3098 (should
3099 (string-match "^ *DEADLINE: <2014-03-05 .*?>"
3100 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3101 (org-entry-put (point) "DEADLINE" "later")
3102 (buffer-string))))
3103 ;; Set "CATEGORY" property
3104 (should
3105 (string-match "^ *:CATEGORY: cat"
3106 (org-test-with-temp-text "* H"
3107 (org-entry-put (point) "CATEGORY" "cat")
3108 (buffer-string))))
3109 ;; Regular properties, with or without pre-existing drawer.
3110 (should
3111 (string-match "^ *:A: +2$"
3112 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3113 (org-entry-put (point) "A" "2")
3114 (buffer-string))))
3115 (should
3116 (string-match "^ *:A: +1$"
3117 (org-test-with-temp-text "* H"
3118 (org-entry-put (point) "A" "1")
3119 (buffer-string))))
3120 ;; Special case: two consecutive headlines.
3121 (should
3122 (string-match "\\* A\n *:PROPERTIES:"
3123 (org-test-with-temp-text "* A\n** B"
3124 (org-entry-put (point) "A" "1")
3125 (buffer-string)))))
3128 ;;; Radio Targets
3130 (ert-deftest test-org/update-radio-target-regexp ()
3131 "Test `org-update-radio-target-regexp' specifications."
3132 ;; Properly update cache with no previous radio target regexp.
3133 (should
3134 (eq 'link
3135 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3136 (save-excursion (goto-char (point-max)) (org-element-context))
3137 (insert "<<<")
3138 (search-forward "o")
3139 (insert ">>>")
3140 (org-update-radio-target-regexp)
3141 (goto-char (point-max))
3142 (org-element-type (org-element-context)))))
3143 ;; Properly update cache with previous radio target regexp.
3144 (should
3145 (eq 'link
3146 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3147 (save-excursion (goto-char (point-max)) (org-element-context))
3148 (insert "<<<")
3149 (search-forward "o")
3150 (insert ">>>")
3151 (org-update-radio-target-regexp)
3152 (search-backward "r")
3153 (delete-char 5)
3154 (insert "new")
3155 (org-update-radio-target-regexp)
3156 (goto-char (point-max))
3157 (delete-region (line-beginning-position) (point))
3158 (insert "new")
3159 (org-element-type (org-element-context))))))
3162 ;;; Sparse trees
3164 (ert-deftest test-org/match-sparse-tree ()
3165 "Test `org-match-sparse-tree' specifications."
3166 ;; Match tags.
3167 (should-not
3168 (org-test-with-temp-text "* H\n** H1 :tag:"
3169 (org-match-sparse-tree nil "tag")
3170 (search-forward "H1")
3171 (org-invisible-p2)))
3172 (should
3173 (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
3174 (org-match-sparse-tree nil "tag")
3175 (search-forward "H2")
3176 (org-invisible-p2)))
3177 ;; "-" operator for tags.
3178 (should-not
3179 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3180 (org-match-sparse-tree nil "tag1-tag2")
3181 (search-forward "H1")
3182 (org-invisible-p2)))
3183 (should
3184 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3185 (org-match-sparse-tree nil "tag1-tag2")
3186 (search-forward "H2")
3187 (org-invisible-p2)))
3188 ;; "&" operator for tags.
3189 (should
3190 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3191 (org-match-sparse-tree nil "tag1&tag2")
3192 (search-forward "H1")
3193 (org-invisible-p2)))
3194 (should-not
3195 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3196 (org-match-sparse-tree nil "tag1&tag2")
3197 (search-forward "H2")
3198 (org-invisible-p2)))
3199 ;; "|" operator for tags.
3200 (should-not
3201 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3202 (org-match-sparse-tree nil "tag1|tag2")
3203 (search-forward "H1")
3204 (org-invisible-p2)))
3205 (should-not
3206 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3207 (org-match-sparse-tree nil "tag1|tag2")
3208 (search-forward "H2")
3209 (org-invisible-p2)))
3210 ;; Regexp match on tags.
3211 (should-not
3212 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3213 (org-match-sparse-tree nil "{^tag.*}")
3214 (search-forward "H1")
3215 (org-invisible-p2)))
3216 (should
3217 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3218 (org-match-sparse-tree nil "{^tag.*}")
3219 (search-forward "H2")
3220 (org-invisible-p2)))
3221 ;; Match group tags.
3222 (should-not
3223 (org-test-with-temp-text
3224 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3225 (org-match-sparse-tree nil "work")
3226 (search-forward "H1")
3227 (org-invisible-p2)))
3228 (should-not
3229 (org-test-with-temp-text
3230 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3231 (org-match-sparse-tree nil "work")
3232 (search-forward "H2")
3233 (org-invisible-p2)))
3234 ;; Match group tags with hard brackets.
3235 (should-not
3236 (org-test-with-temp-text
3237 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
3238 (org-match-sparse-tree nil "work")
3239 (search-forward "H1")
3240 (org-invisible-p2)))
3241 (should-not
3242 (org-test-with-temp-text
3243 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
3244 (org-match-sparse-tree nil "work")
3245 (search-forward "H2")
3246 (org-invisible-p2)))
3247 ;; Match tags in hierarchies
3248 (should-not
3249 (org-test-with-temp-text
3250 "#+TAGS: [ Lev_1 : Lev_2 ]\n
3251 #+TAGS: [ Lev_2 : Lev_3 ]\n
3252 #+TAGS: { Lev_3 : Lev_4 }\n
3253 * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
3254 (org-match-sparse-tree nil "Lev_1")
3255 (search-forward "H4")
3256 (org-invisible-p2)))
3257 ;; Match regular expressions in tags
3258 (should-not
3259 (org-test-with-temp-text
3260 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
3261 (org-match-sparse-tree nil "Lev")
3262 (search-forward "H1")
3263 (org-invisible-p2)))
3264 (should
3265 (org-test-with-temp-text
3266 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
3267 (org-match-sparse-tree nil "Lev")
3268 (search-forward "H1")
3269 (org-invisible-p2)))
3270 ;; Match properties.
3271 (should
3272 (org-test-with-temp-text
3273 "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
3274 (org-match-sparse-tree nil "A=\"1\"")
3275 (search-forward "H2")
3276 (org-invisible-p2)))
3277 (should-not
3278 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3279 (org-match-sparse-tree nil "A=\"1\"")
3280 (search-forward "H2")
3281 (org-invisible-p2)))
3282 ;; Case is not significant when matching properties.
3283 (should-not
3284 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3285 (org-match-sparse-tree nil "a=\"1\"")
3286 (search-forward "H2")
3287 (org-invisible-p2)))
3288 (should-not
3289 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
3290 (org-match-sparse-tree nil "A=\"1\"")
3291 (search-forward "H2")
3292 (org-invisible-p2)))
3293 ;; Match special LEVEL property.
3294 (should-not
3295 (org-test-with-temp-text "* H\n** H1\n*** H2"
3296 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3297 (search-forward "H1")
3298 (org-invisible-p2)))
3299 (should
3300 (org-test-with-temp-text "* H\n** H1\n*** H2"
3301 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3302 (search-forward "H2")
3303 (org-invisible-p2)))
3304 ;; Comparison operators when matching properties.
3305 (should
3306 (org-test-with-temp-text
3307 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3308 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3309 (search-forward "H1")
3310 (org-invisible-p2)))
3311 (should-not
3312 (org-test-with-temp-text
3313 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3314 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3315 (search-forward "H2")
3316 (org-invisible-p2)))
3317 ;; Regexp match on properties values.
3318 (should-not
3319 (org-test-with-temp-text
3320 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3321 (org-match-sparse-tree nil "A={f.*}")
3322 (search-forward "H1")
3323 (org-invisible-p2)))
3324 (should
3325 (org-test-with-temp-text
3326 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3327 (org-match-sparse-tree nil "A={f.*}")
3328 (search-forward "H2")
3329 (org-invisible-p2)))
3330 ;; With an optional argument, limit match to TODO entries.
3331 (should-not
3332 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3333 (org-match-sparse-tree t "tag")
3334 (search-forward "H1")
3335 (org-invisible-p2)))
3336 (should
3337 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3338 (org-match-sparse-tree t "tag")
3339 (search-forward "H2")
3340 (org-invisible-p2))))
3343 ;;; Timestamps API
3345 (ert-deftest test-org/time-stamp ()
3346 "Test `org-time-stamp' specifications."
3347 ;; Insert chosen time stamp at point.
3348 (should
3349 (string-match
3350 "Te<2014-03-04 .*?>xt"
3351 (org-test-with-temp-text "Te<point>xt"
3352 (flet ((org-read-date
3353 (&rest args)
3354 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3355 (org-time-stamp nil)
3356 (buffer-string)))))
3357 ;; With a prefix argument, also insert time.
3358 (should
3359 (string-match
3360 "Te<2014-03-04 .*? 00:41>xt"
3361 (org-test-with-temp-text "Te<point>xt"
3362 (flet ((org-read-date
3363 (&rest args)
3364 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
3365 (org-time-stamp '(4))
3366 (buffer-string)))))
3367 ;; With two universal prefix arguments, insert an active timestamp
3368 ;; with the current time without prompting the user.
3369 (should
3370 (string-match
3371 "Te<2014-03-04 .*? 00:41>xt"
3372 (org-test-with-temp-text "Te<point>xt"
3373 (flet ((current-time
3375 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
3376 (org-time-stamp '(16))
3377 (buffer-string)))))
3378 ;; When optional argument is non-nil, insert an inactive timestamp.
3379 (should
3380 (string-match
3381 "Te\\[2014-03-04 .*?\\]xt"
3382 (org-test-with-temp-text "Te<point>xt"
3383 (flet ((org-read-date
3384 (&rest args)
3385 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3386 (org-time-stamp nil t)
3387 (buffer-string)))))
3388 ;; When called from a timestamp, replace existing one.
3389 (should
3390 (string-match
3391 "<2014-03-04 .*?>"
3392 (org-test-with-temp-text "<2012-03-29<point> thu.>"
3393 (flet ((org-read-date
3394 (&rest args)
3395 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3396 (org-time-stamp nil)
3397 (buffer-string)))))
3398 (should
3399 (string-match
3400 "<2014-03-04 .*?>--<2014-03-04 .*?>"
3401 (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
3402 (flet ((org-read-date
3403 (&rest args)
3404 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3405 (org-time-stamp nil)
3406 (buffer-string)))))
3407 ;; When replacing a timestamp, preserve repeater, if any.
3408 (should
3409 (string-match
3410 "<2014-03-04 .*? \\+2y>"
3411 (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
3412 (flet ((org-read-date
3413 (&rest args)
3414 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3415 (org-time-stamp nil)
3416 (buffer-string)))))
3417 ;; When called twice in a raw, build a date range.
3418 (should
3419 (string-match
3420 "<2012-03-29 .*?>--<2014-03-04 .*?>"
3421 (org-test-with-temp-text "<2012-03-29 thu.><point>"
3422 (flet ((org-read-date
3423 (&rest args)
3424 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3425 (let ((last-command 'org-time-stamp)
3426 (this-command 'org-time-stamp))
3427 (org-time-stamp nil))
3428 (buffer-string))))))
3430 (ert-deftest test-org/timestamp-has-time-p ()
3431 "Test `org-timestamp-has-time-p' specifications."
3432 ;; With time.
3433 (should
3434 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
3435 (org-timestamp-has-time-p (org-element-context))))
3436 ;; Without time.
3437 (should-not
3438 (org-test-with-temp-text "<2012-03-29 Thu>"
3439 (org-timestamp-has-time-p (org-element-context)))))
3441 (ert-deftest test-org/timestamp-format ()
3442 "Test `org-timestamp-format' specifications."
3443 ;; Regular test.
3444 (should
3445 (equal
3446 "2012-03-29 16:40"
3447 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
3448 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
3449 ;; Range end.
3450 (should
3451 (equal
3452 "2012-03-29"
3453 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
3454 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
3456 (ert-deftest test-org/timestamp-split-range ()
3457 "Test `org-timestamp-split-range' specifications."
3458 ;; Extract range start (active).
3459 (should
3460 (equal '(2012 3 29)
3461 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3462 (let ((ts (org-timestamp-split-range (org-element-context))))
3463 (mapcar (lambda (p) (org-element-property p ts))
3464 '(:year-end :month-end :day-end))))))
3465 ;; Extract range start (inactive)
3466 (should
3467 (equal '(2012 3 29)
3468 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
3469 (let ((ts (org-timestamp-split-range (org-element-context))))
3470 (mapcar (lambda (p) (org-element-property p ts))
3471 '(:year-end :month-end :day-end))))))
3472 ;; Extract range end (active).
3473 (should
3474 (equal '(2012 3 30)
3475 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3476 (let ((ts (org-timestamp-split-range
3477 (org-element-context) t)))
3478 (mapcar (lambda (p) (org-element-property p ts))
3479 '(:year-end :month-end :day-end))))))
3480 ;; Extract range end (inactive)
3481 (should
3482 (equal '(2012 3 30)
3483 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
3484 (let ((ts (org-timestamp-split-range
3485 (org-element-context) t)))
3486 (mapcar (lambda (p) (org-element-property p ts))
3487 '(:year-end :month-end :day-end))))))
3488 ;; Return the timestamp if not a range.
3489 (should
3490 (org-test-with-temp-text "[2012-03-29 Thu]"
3491 (let* ((ts-orig (org-element-context))
3492 (ts-copy (org-timestamp-split-range ts-orig)))
3493 (eq ts-orig ts-copy))))
3494 (should
3495 (org-test-with-temp-text "<%%(org-float t 4 2)>"
3496 (let* ((ts-orig (org-element-context))
3497 (ts-copy (org-timestamp-split-range ts-orig)))
3498 (eq ts-orig ts-copy)))))
3500 (ert-deftest test-org/timestamp-translate ()
3501 "Test `org-timestamp-translate' specifications."
3502 ;; Translate whole date range.
3503 (should
3504 (equal "<29>--<30>"
3505 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3506 (let ((org-display-custom-times t)
3507 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3508 (org-timestamp-translate (org-element-context))))))
3509 ;; Translate date range start.
3510 (should
3511 (equal "<29>"
3512 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3513 (let ((org-display-custom-times t)
3514 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3515 (org-timestamp-translate (org-element-context) 'start)))))
3516 ;; Translate date range end.
3517 (should
3518 (equal "<30>"
3519 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3520 (let ((org-display-custom-times t)
3521 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3522 (org-timestamp-translate (org-element-context) 'end)))))
3523 ;; Translate time range.
3524 (should
3525 (equal "<08>--<16>"
3526 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
3527 (let ((org-display-custom-times t)
3528 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
3529 (org-timestamp-translate (org-element-context))))))
3530 ;; Translate non-range timestamp.
3531 (should
3532 (equal "<29>"
3533 (org-test-with-temp-text "<2012-03-29 Thu>"
3534 (let ((org-display-custom-times t)
3535 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3536 (org-timestamp-translate (org-element-context))))))
3537 ;; Do not change `diary' timestamps.
3538 (should
3539 (equal "<%%(org-float t 4 2)>"
3540 (org-test-with-temp-text "<%%(org-float t 4 2)>"
3541 (let ((org-display-custom-times t)
3542 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3543 (org-timestamp-translate (org-element-context)))))))
3547 ;;; Visibility
3549 (ert-deftest test-org/flag-drawer ()
3550 "Test `org-flag-drawer' specifications."
3551 ;; Hide drawer.
3552 (should
3553 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3554 (org-flag-drawer t)
3555 (get-char-property (line-end-position) 'invisible)))
3556 ;; Show drawer.
3557 (should-not
3558 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3559 (org-flag-drawer t)
3560 (org-flag-drawer nil)
3561 (get-char-property (line-end-position) 'invisible)))
3562 ;; Test optional argument.
3563 (should
3564 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
3565 (let ((drawer (save-excursion (search-forward ":D2")
3566 (org-element-at-point))))
3567 (org-flag-drawer t drawer)
3568 (get-char-property (progn (search-forward ":D2") (line-end-position))
3569 'invisible))))
3570 (should-not
3571 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
3572 (let ((drawer (save-excursion (search-forward ":D2")
3573 (org-element-at-point))))
3574 (org-flag-drawer t drawer)
3575 (get-char-property (line-end-position) 'invisible))))
3576 ;; Do not hide fake drawers.
3577 (should-not
3578 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
3579 (forward-line 1)
3580 (org-flag-drawer t)
3581 (get-char-property (line-end-position) 'invisible)))
3582 ;; Do not hide incomplete drawers.
3583 (should-not
3584 (org-test-with-temp-text ":D:\nparagraph"
3585 (forward-line 1)
3586 (org-flag-drawer t)
3587 (get-char-property (line-end-position) 'invisible)))
3588 ;; Do not hide drawers when called from final blank lines.
3589 (should-not
3590 (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
3591 (goto-char (point-max))
3592 (org-flag-drawer t)
3593 (goto-char (point-min))
3594 (get-char-property (line-end-position) 'invisible)))
3595 ;; Don't leave point in an invisible part of the buffer when hiding
3596 ;; a drawer away.
3597 (should-not
3598 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3599 (goto-char (point-max))
3600 (org-flag-drawer t)
3601 (get-char-property (point) 'invisible))))
3603 (ert-deftest test-org/hide-block-toggle ()
3604 "Test `org-hide-block-toggle' specifications."
3605 ;; Error when not at a block.
3606 (should-error
3607 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
3608 (org-hide-block-toggle 'off)
3609 (get-char-property (line-end-position) 'invisible)))
3610 ;; Hide block.
3611 (should
3612 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
3613 (org-hide-block-toggle)
3614 (get-char-property (line-end-position) 'invisible)))
3615 (should
3616 (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
3617 (org-hide-block-toggle)
3618 (get-char-property (line-end-position) 'invisible)))
3619 ;; Show block unconditionally when optional argument is `off'.
3620 (should-not
3621 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3622 (org-hide-block-toggle)
3623 (org-hide-block-toggle 'off)
3624 (get-char-property (line-end-position) 'invisible)))
3625 (should-not
3626 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3627 (org-hide-block-toggle 'off)
3628 (get-char-property (line-end-position) 'invisible)))
3629 ;; Hide block unconditionally when optional argument is non-nil.
3630 (should
3631 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3632 (org-hide-block-toggle t)
3633 (get-char-property (line-end-position) 'invisible)))
3634 (should
3635 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3636 (org-hide-block-toggle)
3637 (org-hide-block-toggle t)
3638 (get-char-property (line-end-position) 'invisible)))
3639 ;; Do not hide block when called from final blank lines.
3640 (should-not
3641 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
3642 (org-hide-block-toggle)
3643 (goto-char (point-min))
3644 (get-char-property (line-end-position) 'invisible)))
3645 ;; Don't leave point in an invisible part of the buffer when hiding
3646 ;; a block away.
3647 (should-not
3648 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
3649 (org-hide-block-toggle)
3650 (get-char-property (point) 'invisible))))
3652 (ert-deftest test-org/hide-block-toggle-maybe ()
3653 "Test `org-hide-block-toggle-maybe' specifications."
3654 (should
3655 (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
3656 (org-hide-block-toggle-maybe)))
3657 (should-not
3658 (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
3660 (ert-deftest test-org/show-set-visibility ()
3661 "Test `org-show-set-visibility' specifications."
3662 ;; Do not throw an error before first heading.
3663 (should
3664 (org-test-with-temp-text "Preamble\n* Headline"
3665 (org-show-set-visibility 'tree)
3667 ;; Test all visibility spans, both on headline and in entry.
3668 (let ((list-visible-lines
3669 (lambda (state headerp)
3670 (org-test-with-temp-text "* Grandmother (0)
3671 ** Uncle (1)
3672 *** Heir (2)
3673 ** Father (3)
3674 Ancestor text (4)
3675 *** Sister (5)
3676 Sibling text (6)
3677 *** Self (7)
3678 Match (8)
3679 **** First born (9)
3680 Child text (10)
3681 **** The other child (11)
3682 *** Brother (12)
3683 ** Aunt (13)
3685 (org-cycle t)
3686 (search-forward (if headerp "Self" "Match"))
3687 (org-show-set-visibility state)
3688 (goto-char (point-min))
3689 (let (result (line 0))
3690 (while (not (eobp))
3691 (unless (org-invisible-p2) (push line result))
3692 (incf line)
3693 (forward-line))
3694 (nreverse result))))))
3695 (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
3696 (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
3697 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
3698 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
3699 (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
3700 (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
3701 (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
3702 (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
3703 (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
3704 (should (equal '(0 1 3 5 7 8 9 11 12 13)
3705 (funcall list-visible-lines 'tree nil)))
3706 (should (equal '(0 1 3 4 5 7 12 13)
3707 (funcall list-visible-lines 'canonical t)))
3708 (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
3709 (funcall list-visible-lines 'canonical nil)))))
3712 (provide 'test-org)
3714 ;;; test-org.el ends here