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