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