Add tests for tag width calculation
[org-mode/org-tableheadings.git] / testing / lisp / test-org.el
blob4873fc2f4256d65f44657cf71aaa484f9d078c62
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 |\n"
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 (= 4
649 (org-test-with-temp-text "* H\n- A\n -\n\n<point>"
650 (let ((org-adapt-indentation t)) (org-indent-line))
651 (org-get-indentation))))
652 (should
653 (zerop
654 (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
655 (goto-char (point-max))
656 (let ((org-adapt-indentation t)) (org-indent-line))
657 (org-get-indentation))))
658 (should
659 (= 4
660 (org-test-with-temp-text "* H\n- A\n - \n"
661 (goto-char (point-max))
662 (let ((org-adapt-indentation t)) (org-indent-line))
663 (org-get-indentation))))
664 (should
665 (= 2
666 (org-test-with-temp-text "- A\n B\n\n<point>"
667 (let ((org-adapt-indentation nil)) (org-indent-line))
668 (org-get-indentation))))
669 ;; Likewise, on a blank line at the end of a footnote definition,
670 ;; indent at column 0 if line belongs to the definition. Otherwise,
671 ;; indent like the definition itself.
672 (should
673 (zerop
674 (org-test-with-temp-text "* H\n[fn:1] Definition\n"
675 (goto-char (point-max))
676 (let ((org-adapt-indentation t)) (org-indent-line))
677 (org-get-indentation))))
678 (should
679 (zerop
680 (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
681 (goto-char (point-max))
682 (let ((org-adapt-indentation t)) (org-indent-line))
683 (org-get-indentation))))
684 ;; After the end of the contents of a greater element, indent like
685 ;; the beginning of the element.
686 (should
687 (= 1
688 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
689 (forward-line 2)
690 (org-indent-line)
691 (org-get-indentation))))
692 ;; On blank lines after a paragraph, indent like its last non-empty
693 ;; line.
694 (should
695 (= 1
696 (org-test-with-temp-text " Paragraph\n\n<point>"
697 (org-indent-line)
698 (org-get-indentation))))
699 ;; At the first line of an element, indent like previous element's
700 ;; first line, ignoring footnotes definitions and inline tasks, or
701 ;; according to parent.
702 (should
703 (= 2
704 (org-test-with-temp-text "A\n\n B\n\nC"
705 (goto-char (point-max))
706 (org-indent-line)
707 (org-get-indentation))))
708 (should
709 (= 1
710 (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
711 (goto-char (point-max))
712 (org-indent-line)
713 (org-get-indentation))))
714 (should
715 (= 1
716 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
717 (forward-line 1)
718 (org-indent-line)
719 (org-get-indentation))))
720 ;; Within code part of a source block, use language major mode if
721 ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
722 ;; according to line above.
723 (should
724 (= 6
725 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
726 (forward-line 2)
727 (let ((org-src-tab-acts-natively t)
728 (org-edit-src-content-indentation 0))
729 (org-indent-line))
730 (org-get-indentation))))
731 (should
732 (= 1
733 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
734 (forward-line 2)
735 (let ((org-src-tab-acts-natively nil)
736 (org-edit-src-content-indentation 0))
737 (org-indent-line))
738 (org-get-indentation))))
739 ;; Otherwise, indent like the first non-blank line above.
740 (should
741 (zerop
742 (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
743 (forward-line 3)
744 (org-indent-line)
745 (org-get-indentation))))
746 ;; Align node properties according to `org-property-format'. Handle
747 ;; nicely empty values.
748 (should
749 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
750 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
751 (let ((org-property-format "%-10s %s")) (org-indent-line))
752 (buffer-string))))
753 (should
754 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
755 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
756 (let ((org-property-format "%-10s %s")) (org-indent-line))
757 (buffer-string)))))
759 (ert-deftest test-org/indent-region ()
760 "Test `org-indent-region' specifications."
761 ;; Indent paragraph.
762 (should
763 (equal "A\nB\nC"
764 (org-test-with-temp-text " A\nB\n C"
765 (org-indent-region (point-min) (point-max))
766 (buffer-string))))
767 ;; Indent greater elements along with their contents.
768 (should
769 (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
770 (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
771 (org-indent-region (point-min) (point-max))
772 (buffer-string))))
773 ;; Ignore contents of verse blocks and example blocks.
774 (should
775 (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
776 (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
777 (org-indent-region (point-min) (point-max))
778 (buffer-string))))
779 (should
780 (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
781 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
782 (org-indent-region (point-min) (point-max))
783 (buffer-string))))
784 ;; Indent according to mode if `org-src-tab-acts-natively' is
785 ;; non-nil. Otherwise, do not indent code at all.
786 (should
787 (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
788 (org-test-with-temp-text
789 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
790 (let ((org-src-tab-acts-natively t)
791 (org-edit-src-content-indentation 0))
792 (org-indent-region (point-min) (point-max)))
793 (buffer-string))))
794 (should
795 (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
796 (org-test-with-temp-text
797 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
798 (let ((org-src-tab-acts-natively nil)
799 (org-edit-src-content-indentation 0))
800 (org-indent-region (point-min) (point-max)))
801 (buffer-string))))
802 ;; Align node properties according to `org-property-format'. Handle
803 ;; nicely empty values.
804 (should
805 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
806 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
807 (let ((org-property-format "%-10s %s")
808 (org-adapt-indentation nil))
809 (org-indent-region (point) (point-max)))
810 (buffer-string))))
811 (should
812 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
813 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
814 (let ((org-property-format "%-10s %s")
815 (org-adapt-indentation nil))
816 (org-indent-region (point) (point-max)))
817 (buffer-string))))
818 ;; Indent plain lists.
819 (should
820 (equal "- A\n B\n - C\n\n D"
821 (org-test-with-temp-text "- A\n B\n - C\n\n D"
822 (org-indent-region (point-min) (point-max))
823 (buffer-string))))
824 (should
825 (equal "- A\n\n- B"
826 (org-test-with-temp-text " - A\n\n - B"
827 (org-indent-region (point-min) (point-max))
828 (buffer-string))))
829 ;; Indent footnote definitions.
830 (should
831 (equal "[fn:1] Definition\n\nDefinition"
832 (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
833 (org-indent-region (point-min) (point-max))
834 (buffer-string))))
835 ;; Special case: Start indenting on a blank line.
836 (should
837 (equal "\nParagraph"
838 (org-test-with-temp-text "\n Paragraph"
839 (org-indent-region (point-min) (point-max))
840 (buffer-string)))))
844 ;;; Editing
846 (ert-deftest test-org/delete-indentation ()
847 "Test `org-delete-indentation' specifications."
848 ;; Regular test.
849 (should (equal "foo bar"
850 (org-test-with-temp-text
851 "foo \n bar<point>"
852 (org-delete-indentation)
853 (buffer-string))))
854 ;; With optional argument.
855 (should (equal "foo bar"
856 (org-test-with-temp-text
857 "foo<point> \n bar"
858 (org-delete-indentation t)
859 (buffer-string))))
860 ;; At headline text should be appended to the headline text.
861 (should
862 (equal"* foo bar :tag:"
863 (let (org-auto-align-tags)
864 (org-test-with-temp-text
865 "* foo :tag:\n bar<point>"
866 (org-delete-indentation)
867 (buffer-string)))))
868 (should
869 (equal "* foo bar :tag:"
870 (let (org-auto-align-tags)
871 (org-test-with-temp-text
872 "* foo <point>:tag:\n bar"
873 (org-delete-indentation t)
874 (buffer-string))))))
876 (ert-deftest test-org/return ()
877 "Test `org-return' specifications."
878 ;; Regular test.
879 (should
880 (equal "Para\ngraph"
881 (org-test-with-temp-text "Para<point>graph"
882 (org-return)
883 (buffer-string))))
884 ;; With optional argument, indent line.
885 (should
886 (equal " Para\n graph"
887 (org-test-with-temp-text " Para<point>graph"
888 (org-return t)
889 (buffer-string))))
890 ;; On a table, call `org-table-next-row'.
891 (should
892 (org-test-with-temp-text "| <point>a |\n| b |"
893 (org-return)
894 (org-looking-at-p "b")))
895 ;; Open link or timestamp under point when `org-return-follows-link'
896 ;; is non-nil.
897 (should
898 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
899 (let ((org-return-follows-link t)
900 (org-link-search-must-match-exact-headline nil))
901 (org-return))
902 (org-looking-at-p "<<target>>")))
903 (should-not
904 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
905 (let ((org-return-follows-link nil)) (org-return))
906 (org-looking-at-p "<<target>>")))
907 (should
908 (org-test-with-temp-text "* [[b][a<point>]]\n* b"
909 (let ((org-return-follows-link t)) (org-return))
910 (org-looking-at-p "* b")))
911 (should
912 (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
913 (let ((org-return-follows-link t)
914 (org-link-search-must-match-exact-headline nil))
915 (org-return))
916 (org-looking-at-p "<<target>>")))
917 (should-not
918 (org-test-with-temp-text "Link [[target]]<point> <<target>>"
919 (let ((org-return-follows-link t)
920 (org-link-search-must-match-exact-headline nil))
921 (org-return))
922 (org-looking-at-p "<<target>>")))
923 ;; When `org-return-follows-link' is non-nil, tolerate links and
924 ;; timestamps in comments, node properties, etc.
925 (should
926 (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
927 (let ((org-return-follows-link t)
928 (org-link-search-must-match-exact-headline nil))
929 (org-return))
930 (org-looking-at-p "<<target>>")))
931 (should-not
932 (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
933 (let ((org-return-follows-link nil)) (org-return))
934 (org-looking-at-p "<<target>>")))
935 (should-not
936 (org-test-with-temp-text "# Comment [[target]]<point>\n <<target>>"
937 (let ((org-return-follows-link t)
938 (org-link-search-must-match-exact-headline nil))
939 (org-return))
940 (org-looking-at-p "<<target>>")))
941 ;; However, do not open link when point is in a table.
942 (should
943 (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
944 (let ((org-return-follows-link t)) (org-return))
945 (org-looking-at-p "between")))
946 ;; Special case: in a list, when indenting, do not break structure.
947 (should
948 (equal "- A\n B"
949 (org-test-with-temp-text "- A <point>B"
950 (org-return t)
951 (buffer-string))))
952 (should
953 (equal "- A\n\n- B"
954 (org-test-with-temp-text "- A\n<point>- B"
955 (org-return t)
956 (buffer-string))))
957 ;; On tags part of a headline, add a newline below it instead of
958 ;; breaking it.
959 (should
960 (equal "* H :tag:\n"
961 (org-test-with-temp-text "* H :<point>tag:"
962 (org-return)
963 (buffer-string))))
964 ;; Before headline text, add a newline below it instead of breaking
965 ;; it.
966 (should
967 (equal "* TODO H :tag:\n"
968 (org-test-with-temp-text "* <point>TODO H :tag:"
969 (org-return)
970 (buffer-string))))
971 (should
972 (equal "* TODO [#B] H :tag:\n"
973 (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
974 (org-return)
975 (buffer-string))))
976 ;; At headline text, break headline text but preserve tags.
977 (should
978 (equal "* TODO [#B] foo :tag:\nbar"
979 (let (org-auto-align-tags)
980 (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
981 (org-return)
982 (buffer-string)))))
983 ;; At bol of headline insert newline.
984 (should
985 (equal "\n* h"
986 (org-test-with-temp-text "<point>* h"
987 (org-return)
988 (buffer-string))))
989 ;; Refuse to leave invalid headline in buffer.
990 (should
991 (equal "* h\n"
992 (org-test-with-temp-text "*<point> h"
993 (org-return)
994 (buffer-string)))))
996 (ert-deftest test-org/meta-return ()
997 "Test M-RET (`org-meta-return') specifications."
998 ;; In a table field insert a row above.
999 (should
1000 (org-test-with-temp-text "| a |"
1001 (forward-char)
1002 (org-meta-return)
1003 (forward-line -1)
1004 (looking-at "| |$")))
1005 ;; In a paragraph change current line into a header.
1006 (should
1007 (org-test-with-temp-text "a"
1008 (org-meta-return)
1009 (beginning-of-line)
1010 (looking-at "\* a$")))
1011 ;; In an item insert an item, in this case above.
1012 (should
1013 (org-test-with-temp-text "- a"
1014 (org-meta-return)
1015 (beginning-of-line)
1016 (looking-at "- $")))
1017 ;; In a drawer and item insert an item, in this case above.
1018 (should
1019 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
1020 (forward-line)
1021 (org-meta-return)
1022 (beginning-of-line)
1023 (looking-at "- $"))))
1025 (ert-deftest test-org/insert-heading ()
1026 "Test `org-insert-heading' specifications."
1027 ;; In an empty buffer, insert a new headline.
1028 (should
1029 (equal "* "
1030 (org-test-with-temp-text ""
1031 (org-insert-heading)
1032 (buffer-string))))
1033 ;; At the beginning of a line, turn it into a headline
1034 (should
1035 (equal "* P"
1036 (org-test-with-temp-text "<point>P"
1037 (org-insert-heading)
1038 (buffer-string))))
1039 ;; In the middle of a line, split the line if allowed, otherwise,
1040 ;; insert the headline at its end.
1041 (should
1042 (equal "Para\n* graph"
1043 (org-test-with-temp-text "Para<point>graph"
1044 (let ((org-M-RET-may-split-line '((default . t))))
1045 (org-insert-heading))
1046 (buffer-string))))
1047 (should
1048 (equal "Paragraph\n* "
1049 (org-test-with-temp-text "Para<point>graph"
1050 (let ((org-M-RET-may-split-line '((default . nil))))
1051 (org-insert-heading))
1052 (buffer-string))))
1053 ;; When on a list, insert an item instead, unless called with an
1054 ;; universal argument or if list is invisible. In this case, create
1055 ;; a new headline after contents.
1056 (should
1057 (equal "* H\n- item\n- "
1058 (org-test-with-temp-text "* H\n- item<point>"
1059 (let ((org-insert-heading-respect-content nil))
1060 (org-insert-heading))
1061 (buffer-string))))
1062 (should
1063 (equal "* H\n- item\n- item 2\n* "
1064 (org-test-with-temp-text "* H\n- item<point>\n- item 2"
1065 (let ((org-insert-heading-respect-content nil))
1066 (org-insert-heading '(4)))
1067 (buffer-string))))
1068 (should
1069 (equal "* H\n- item\n* "
1070 (org-test-with-temp-text "* H\n- item"
1071 (org-cycle)
1072 (goto-char (point-max))
1073 (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
1074 (buffer-string))))
1075 ;; When called with two universal arguments, insert a new headline
1076 ;; at the end of the grandparent subtree.
1077 (should
1078 (equal "* H1\n** H3\n- item\n** H2\n** "
1079 (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
1080 (let ((org-insert-heading-respect-content nil))
1081 (org-insert-heading '(16)))
1082 (buffer-string))))
1083 ;; When optional TOP-LEVEL argument is non-nil, always insert
1084 ;; a level 1 heading.
1085 (should
1086 (equal "* H1\n** H2\n* "
1087 (org-test-with-temp-text "* H1\n** H2<point>"
1088 (org-insert-heading nil nil t)
1089 (buffer-string))))
1090 (should
1091 (equal "* H1\n- item\n* "
1092 (org-test-with-temp-text "* H1\n- item<point>"
1093 (org-insert-heading nil nil t)
1094 (buffer-string))))
1095 ;; Corner case: correctly insert a headline after an empty one.
1096 (should
1097 (equal "* \n* "
1098 (org-test-with-temp-text "* <point>"
1099 (org-insert-heading)
1100 (buffer-string)))))
1102 (ert-deftest test-org/insert-todo-heading-respect-content ()
1103 "Test `org-insert-todo-heading-respect-content' specifications."
1104 ;; Create a TODO heading.
1105 (should
1106 (org-test-with-temp-text "* H1\n Body"
1107 (org-insert-todo-heading-respect-content)
1108 (nth 2 (org-heading-components))))
1109 ;; Add headline at the end of the first subtree
1110 (should
1111 (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
1112 (search-forward "H1Body")
1113 (org-insert-todo-heading-respect-content)
1114 (and (eobp) (org-at-heading-p))))
1115 ;; In a list, do not create a new item.
1116 (should
1117 (org-test-with-temp-text "* H\n- an item\n- another one"
1118 (search-forward "an ")
1119 (org-insert-todo-heading-respect-content)
1120 (and (eobp) (org-at-heading-p)))))
1122 (ert-deftest test-org/clone-with-time-shift ()
1123 "Test `org-clone-subtree-with-time-shift'."
1124 ;; Clone non-repeating once.
1125 (should
1126 (equal "\
1127 * H1\n<2015-06-21>
1128 * H1\n<2015-06-23>
1130 (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
1131 (org-clone-subtree-with-time-shift 1 "+2d")
1132 (replace-regexp-in-string
1133 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1134 nil nil 1))))
1135 ;; Clone repeating once.
1136 (should
1137 (equal "\
1138 * H1\n<2015-06-21>
1139 * H1\n<2015-06-23>
1140 * H1\n<2015-06-25 +1w>
1142 (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
1143 (org-clone-subtree-with-time-shift 1 "+2d")
1144 (replace-regexp-in-string
1145 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1146 nil nil 1))))
1147 ;; Clone non-repeating zero times.
1148 (should
1149 (equal "\
1150 * H1\n<2015-06-21>
1152 (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
1153 (org-clone-subtree-with-time-shift 0 "+2d")
1154 (replace-regexp-in-string
1155 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1156 nil nil 1))))
1157 ;; Clone repeating "zero" times.
1158 (should
1159 (equal "\
1160 * H1\n<2015-06-21>
1161 * H1\n<2015-06-23 +1w>
1163 (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
1164 (org-clone-subtree-with-time-shift 0 "+2d")
1165 (replace-regexp-in-string
1166 "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
1167 nil nil 1)))))
1170 ;;; Fixed-Width Areas
1172 (ert-deftest test-org/toggle-fixed-width ()
1173 "Test `org-toggle-fixed-width' specifications."
1174 ;; No region: Toggle on fixed-width marker in paragraphs.
1175 (should
1176 (equal ": A"
1177 (org-test-with-temp-text "A"
1178 (org-toggle-fixed-width)
1179 (buffer-string))))
1180 ;; No region: Toggle off fixed-width markers in fixed-width areas.
1181 (should
1182 (equal "A"
1183 (org-test-with-temp-text ": A"
1184 (org-toggle-fixed-width)
1185 (buffer-string))))
1186 ;; No region: Toggle on marker in blank lines after elements or just
1187 ;; after a headline.
1188 (should
1189 (equal "* H\n: "
1190 (org-test-with-temp-text "* H\n"
1191 (forward-line)
1192 (org-toggle-fixed-width)
1193 (buffer-string))))
1194 (should
1195 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
1196 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
1197 (goto-char (point-max))
1198 (org-toggle-fixed-width)
1199 (buffer-string))))
1200 ;; No region: Toggle on marker in front of one line elements (e.g.,
1201 ;; headlines, clocks)
1202 (should
1203 (equal ": * Headline"
1204 (org-test-with-temp-text "* Headline"
1205 (org-toggle-fixed-width)
1206 (buffer-string))))
1207 (should
1208 (equal ": #+KEYWORD: value"
1209 (org-test-with-temp-text "#+KEYWORD: value"
1210 (org-toggle-fixed-width)
1211 (buffer-string))))
1212 ;; No region: error in other situations.
1213 (should-error
1214 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
1215 (forward-line)
1216 (org-toggle-fixed-width)
1217 (buffer-string)))
1218 ;; No region: Indentation is preserved.
1219 (should
1220 (equal "- A\n : B"
1221 (org-test-with-temp-text "- A\n B"
1222 (forward-line)
1223 (org-toggle-fixed-width)
1224 (buffer-string))))
1225 ;; Region: If it contains only fixed-width elements and blank lines,
1226 ;; toggle off fixed-width markup.
1227 (should
1228 (equal "A\n\nB"
1229 (org-test-with-temp-text ": A\n\n: B"
1230 (transient-mark-mode 1)
1231 (push-mark (point) t t)
1232 (goto-char (point-max))
1233 (org-toggle-fixed-width)
1234 (buffer-string))))
1235 ;; Region: If it contains anything else, toggle on fixed-width but
1236 ;; not on fixed-width areas.
1237 (should
1238 (equal ": A\n: \n: B\n: \n: C"
1239 (org-test-with-temp-text "A\n\n: B\n\nC"
1240 (transient-mark-mode 1)
1241 (push-mark (point) t t)
1242 (goto-char (point-max))
1243 (org-toggle-fixed-width)
1244 (buffer-string))))
1245 ;; Region: Ignore blank lines at its end, unless it contains only
1246 ;; such lines.
1247 (should
1248 (equal ": A\n\n"
1249 (org-test-with-temp-text "A\n\n"
1250 (transient-mark-mode 1)
1251 (push-mark (point) t t)
1252 (goto-char (point-max))
1253 (org-toggle-fixed-width)
1254 (buffer-string))))
1255 (should
1256 (equal ": \n: \n"
1257 (org-test-with-temp-text "\n\n"
1258 (transient-mark-mode 1)
1259 (push-mark (point) t t)
1260 (goto-char (point-max))
1261 (org-toggle-fixed-width)
1262 (buffer-string)))))
1266 ;;; Headline
1268 (ert-deftest test-org/get-heading ()
1269 "Test `org-get-heading' specifications."
1270 ;; Return current heading, even if point is not on it.
1271 (should
1272 (equal "H"
1273 (org-test-with-temp-text "* H"
1274 (org-get-heading))))
1275 (should
1276 (equal "H"
1277 (org-test-with-temp-text "* H\nText<point>"
1278 (org-get-heading))))
1279 ;; Without any optional argument, return TODO keywords and tags.
1280 (should
1281 (equal "TODO H"
1282 (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
1283 (org-get-heading))))
1284 (should
1285 (equal "H :tag:"
1286 (org-test-with-temp-text "* H :tag:"
1287 (org-get-heading))))
1288 ;; With NO-TAGS argument, ignore tags.
1289 (should
1290 (equal "TODO H"
1291 (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
1292 (org-get-heading t))))
1293 (should
1294 (equal "H"
1295 (org-test-with-temp-text "* H :tag:"
1296 (org-get-heading t))))
1297 ;; With NO-TODO, ignore TODO keyword.
1298 (should
1299 (equal "H"
1300 (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
1301 (org-get-heading nil t))))
1302 (should
1303 (equal "H :tag:"
1304 (org-test-with-temp-text "* H :tag:"
1305 (org-get-heading nil t))))
1306 ;; TODO keywords are case-sensitive.
1307 (should
1308 (equal "Todo H"
1309 (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
1310 (org-get-heading nil t)))))
1312 (ert-deftest test-org/in-commented-heading-p ()
1313 "Test `org-in-commented-heading-p' specifications."
1314 ;; Commented headline.
1315 (should
1316 (org-test-with-temp-text "* COMMENT Headline\nBody"
1317 (goto-char (point-max))
1318 (org-in-commented-heading-p)))
1319 ;; Commented ancestor.
1320 (should
1321 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1322 (goto-char (point-max))
1323 (org-in-commented-heading-p)))
1324 ;; Comment keyword is case-sensitive.
1325 (should-not
1326 (org-test-with-temp-text "* Comment Headline\nBody"
1327 (goto-char (point-max))
1328 (org-in-commented-heading-p)))
1329 ;; Keyword is standalone.
1330 (should-not
1331 (org-test-with-temp-text "* COMMENTHeadline\nBody"
1332 (goto-char (point-max))
1333 (org-in-commented-heading-p)))
1334 ;; Optional argument.
1335 (should-not
1336 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1337 (goto-char (point-max))
1338 (org-in-commented-heading-p t))))
1340 (ert-deftest test-org/entry-blocked-p ()
1341 ;; Check other dependencies.
1342 (should
1343 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
1344 (let ((org-enforce-todo-dependencies t)
1345 (org-blocker-hook
1346 '(org-block-todo-from-children-or-siblings-or-parent)))
1347 (org-entry-blocked-p))))
1348 (should-not
1349 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
1350 (let ((org-enforce-todo-dependencies t)
1351 (org-blocker-hook
1352 '(org-block-todo-from-children-or-siblings-or-parent)))
1353 (org-entry-blocked-p))))
1354 ;; Entry without a TODO keyword or with a DONE keyword cannot be
1355 ;; blocked.
1356 (should-not
1357 (org-test-with-temp-text "* Blocked\n** TODO one"
1358 (let ((org-enforce-todo-dependencies t)
1359 (org-blocker-hook
1360 '(org-block-todo-from-children-or-siblings-or-parent)))
1361 (org-entry-blocked-p))))
1362 (should-not
1363 (org-test-with-temp-text "* DONE Blocked\n** TODO one"
1364 (let ((org-enforce-todo-dependencies t)
1365 (org-blocker-hook
1366 '(org-block-todo-from-children-or-siblings-or-parent)))
1367 (org-entry-blocked-p))))
1368 ;; Follow :ORDERED: specifications.
1369 (should
1370 (org-test-with-temp-text
1371 "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
1372 (let ((org-enforce-todo-dependencies t)
1373 (org-blocker-hook
1374 '(org-block-todo-from-children-or-siblings-or-parent)))
1375 (org-entry-blocked-p))))
1376 (should-not
1377 (org-test-with-temp-text
1378 "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
1379 (let ((org-enforce-todo-dependencies t)
1380 (org-blocker-hook
1381 '(org-block-todo-from-children-or-siblings-or-parent)))
1382 (org-entry-blocked-p)))))
1384 (ert-deftest test-org/get-outline-path ()
1385 "Test `org-get-outline-path' specifications."
1386 ;; Top-level headlines have no outline path.
1387 (should-not
1388 (org-test-with-temp-text "* H"
1389 (org-get-outline-path)))
1390 ;; Otherwise, outline path is the path leading to the headline.
1391 (should
1392 (equal '("H")
1393 (org-test-with-temp-text "* H\n** S<point>"
1394 (org-get-outline-path))))
1395 ;; Find path even when point is not on a headline.
1396 (should
1397 (equal '("H")
1398 (org-test-with-temp-text "* H\n** S\nText<point>"
1399 (org-get-outline-path))))
1400 ;; TODO keywords, tags and statistics cookies are ignored.
1401 (should
1402 (equal '("H")
1403 (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
1404 (org-get-outline-path))))
1405 ;; Links are replaced with their description or their path.
1406 (should
1407 (equal '("Org")
1408 (org-test-with-temp-text
1409 "* [[http://orgmode.org][Org]]\n** S<point>"
1410 (org-get-outline-path))))
1411 (should
1412 (equal '("http://orgmode.org")
1413 (org-test-with-temp-text
1414 "* [[http://orgmode.org]]\n** S<point>"
1415 (org-get-outline-path))))
1416 ;; When WITH-SELF is non-nil, include current heading.
1417 (should
1418 (equal '("H")
1419 (org-test-with-temp-text "* H"
1420 (org-get-outline-path t))))
1421 (should
1422 (equal '("H" "S")
1423 (org-test-with-temp-text "* H\n** S\nText<point>"
1424 (org-get-outline-path t))))
1425 ;; Using cache is transparent to the user.
1426 (should
1427 (equal '("H")
1428 (org-test-with-temp-text "* H\n** S<point>"
1429 (setq org-outline-path-cache nil)
1430 (org-get-outline-path nil t))))
1431 ;; Do not corrupt cache when finding outline path in distant part of
1432 ;; the buffer.
1433 (should
1434 (equal '("H2")
1435 (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
1436 (setq org-outline-path-cache nil)
1437 (org-get-outline-path nil t)
1438 (search-forward "S2")
1439 (org-get-outline-path nil t))))
1440 ;; Do not choke on empty headlines.
1441 (should
1442 (org-test-with-temp-text "* H\n** <point>"
1443 (org-get-outline-path)))
1444 (should
1445 (org-test-with-temp-text "* \n** H<point>"
1446 (org-get-outline-path))))
1448 (ert-deftest test-org/format-outline-path ()
1449 "Test `org-format-outline-path' specifications."
1450 (should
1451 (string= (org-format-outline-path (list "one" "two" "three"))
1452 "one/two/three"))
1453 ;; Empty path.
1454 (should
1455 (string= (org-format-outline-path '())
1456 ""))
1457 (should
1458 (string= (org-format-outline-path '(nil))
1459 ""))
1460 ;; Empty path and prefix.
1461 (should
1462 (string= (org-format-outline-path '() nil ">>")
1463 ">>"))
1464 ;; Trailing whitespace in headings.
1465 (should
1466 (string= (org-format-outline-path (list "one\t" "tw o " "three "))
1467 "one/tw o/three"))
1468 ;; Non-default prefix and separators.
1469 (should
1470 (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
1471 ">>|one|two|three"))
1472 ;; Truncate.
1473 (should
1474 (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
1475 "one/two/.."))
1476 ;; Give a very narrow width.
1477 (should
1478 (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
1479 "on"))
1480 ;; Give a prefix that extends beyond the width.
1481 (should
1482 (string= (org-format-outline-path (list "one" "two" "three" "four") 10
1483 ">>>>>>>>>>")
1484 ">>>>>>>>..")))
1487 ;;; Keywords
1489 (ert-deftest test-org/set-regexps-and-options ()
1490 "Test `org-set-regexps-and-options' specifications."
1491 ;; TAGS keyword.
1492 (should
1493 (equal '(("A" . ?a) ("B") ("C"))
1494 (org-test-with-temp-text "#+TAGS: A(a) B C"
1495 (org-mode-restart)
1496 org-tag-alist)))
1497 (should
1498 (equal '(("A") (:newline) ("B"))
1499 (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
1500 (org-mode-restart)
1501 org-tag-alist)))
1502 (should
1503 (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
1504 (org-test-with-temp-text "#+TAGS: { A B } C"
1505 (org-mode-restart)
1506 org-tag-alist)))
1507 (should
1508 (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
1509 (org-test-with-temp-text "#+TAGS: { A : B C }"
1510 (org-mode-restart)
1511 org-tag-alist)))
1512 (should
1513 (equal '(("A" "B" "C"))
1514 (org-test-with-temp-text "#+TAGS: { A : B C }"
1515 (org-mode-restart)
1516 org-tag-groups-alist)))
1517 (should
1518 (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
1519 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1520 (org-mode-restart)
1521 org-tag-alist)))
1522 (should
1523 (equal '(("A" "B" "C"))
1524 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1525 (org-mode-restart)
1526 org-tag-groups-alist)))
1527 ;; FILETAGS keyword.
1528 (should
1529 (equal '("A" "B" "C")
1530 (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
1531 (org-mode-restart)
1532 org-file-tags)))
1533 ;; PROPERTY keyword. Property names are case-insensitive.
1534 (should
1535 (equal "foo=1"
1536 (org-test-with-temp-text "#+PROPERTY: var foo=1"
1537 (org-mode-restart)
1538 (cdr (assoc "var" org-file-properties)))))
1539 (should
1540 (equal
1541 "foo=1 bar=2"
1542 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
1543 (org-mode-restart)
1544 (cdr (assoc "var" org-file-properties)))))
1545 (should
1546 (equal
1547 "foo=1 bar=2"
1548 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
1549 (org-mode-restart)
1550 (cdr (assoc "var" org-file-properties)))))
1551 ;; ARCHIVE keyword.
1552 (should
1553 (equal "%s_done::"
1554 (org-test-with-temp-text "#+ARCHIVE: %s_done::"
1555 (org-mode-restart)
1556 org-archive-location)))
1557 ;; CATEGORY keyword.
1558 (should
1559 (eq 'test
1560 (org-test-with-temp-text "#+CATEGORY: test"
1561 (org-mode-restart)
1562 org-category)))
1563 (should
1564 (equal "test"
1565 (org-test-with-temp-text "#+CATEGORY: test"
1566 (org-mode-restart)
1567 (cdr (assoc "CATEGORY" org-file-properties)))))
1568 ;; COLUMNS keyword.
1569 (should
1570 (equal "%25ITEM %TAGS %PRIORITY %TODO"
1571 (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
1572 (org-mode-restart)
1573 org-columns-default-format)))
1574 ;; CONSTANTS keyword. Constants names are case sensitive.
1575 (should
1576 (equal '("299792458." "3.14")
1577 (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
1578 (org-mode-restart)
1579 (mapcar
1580 (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
1581 '("c" "pi")))))
1582 (should
1583 (equal "3.14"
1584 (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
1585 (org-mode-restart)
1586 (cdr (assoc "pi" org-table-formula-constants-local)))))
1587 (should
1588 (equal "22/7"
1589 (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
1590 (org-mode-restart)
1591 (cdr (assoc "PI" org-table-formula-constants-local)))))
1592 ;; LINK keyword.
1593 (should
1594 (equal
1595 '("url1" "url2")
1596 (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
1597 (org-mode-restart)
1598 (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
1599 '("a" "b")))))
1600 ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
1601 (should
1602 (equal
1603 '(?X ?Z ?Y)
1604 (org-test-with-temp-text "#+PRIORITIES: X Z Y"
1605 (org-mode-restart)
1606 (list org-highest-priority org-lowest-priority org-default-priority))))
1607 (should
1608 (equal
1609 '(?A ?C ?B)
1610 (org-test-with-temp-text "#+PRIORITIES: X Z"
1611 (org-mode-restart)
1612 (list org-highest-priority org-lowest-priority org-default-priority))))
1613 ;; STARTUP keyword.
1614 (should
1615 (equal '(t t)
1616 (org-test-with-temp-text "#+STARTUP: fold odd"
1617 (org-mode-restart)
1618 (list org-startup-folded org-odd-levels-only))))
1619 ;; TODO keywords.
1620 (should
1621 (equal '(("A" "B") ("C"))
1622 (org-test-with-temp-text "#+TODO: A B | C"
1623 (org-mode-restart)
1624 (list org-not-done-keywords org-done-keywords))))
1625 (should
1626 (equal '(("A" "C") ("B" "D"))
1627 (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
1628 (org-mode-restart)
1629 (list org-not-done-keywords org-done-keywords))))
1630 (should
1631 (equal '(("A" "B") ("C"))
1632 (org-test-with-temp-text "#+TYP_TODO: A B | C"
1633 (org-mode-restart)
1634 (list org-not-done-keywords org-done-keywords))))
1635 (should
1636 (equal '((:startgroup) ("A" . ?a) (:endgroup))
1637 (org-test-with-temp-text "#+TODO: A(a)"
1638 (org-mode-restart)
1639 org-todo-key-alist)))
1640 (should
1641 (equal '(("D" note nil) ("C" time nil) ("B" note time))
1642 (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
1643 (org-mode-restart)
1644 org-todo-log-states)))
1645 ;; Enter SETUPFILE keyword.
1646 (should
1647 (equal "1"
1648 (org-test-with-temp-text
1649 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
1650 (org-mode-restart)
1651 (cdr (assoc "a" org-file-properties))))))
1655 ;;; Links
1657 ;;;; Coderefs
1659 (ert-deftest test-org/coderef ()
1660 "Test coderef links specifications."
1661 (should
1662 (org-test-with-temp-text "
1663 #+BEGIN_SRC emacs-lisp
1664 \(+ 1 1) (ref:sc)
1665 #+END_SRC
1666 \[[(sc)<point>]]"
1667 (org-open-at-point)
1668 (looking-at "(ref:sc)")))
1669 ;; Find coderef even with alternate label format.
1670 (should
1671 (org-test-with-temp-text "
1672 #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
1673 \(+ 1 1) {ref:sc}
1674 #+END_SRC
1675 \[[(sc)<point>]]"
1676 (org-open-at-point)
1677 (looking-at "{ref:sc}"))))
1679 ;;;; Custom ID
1681 (ert-deftest test-org/custom-id ()
1682 "Test custom ID links specifications."
1683 (should
1684 (org-test-with-temp-text
1685 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
1686 (org-open-at-point)
1687 (org-looking-at-p "\\* H1")))
1688 ;; Throw an error on false positives.
1689 (should-error
1690 (org-test-with-temp-text
1691 "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
1692 (org-open-at-point)
1693 (org-looking-at-p "\\* H1"))))
1695 ;;;; Fuzzy Links
1697 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
1698 ;; a named element (#+name: text) and to headlines (* Text).
1700 (ert-deftest test-org/fuzzy-links ()
1701 "Test fuzzy links specifications."
1702 ;; Fuzzy link goes in priority to a matching target.
1703 (should
1704 (org-test-with-temp-text
1705 "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
1706 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1707 (looking-at "<<Test>>")))
1708 ;; Then fuzzy link points to an element with a given name.
1709 (should
1710 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
1711 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1712 (looking-at "#\\+NAME: Test")))
1713 ;; A target still lead to a matching headline otherwise.
1714 (should
1715 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
1716 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1717 (looking-at "\\* Head2")))
1718 ;; With a leading star in link, enforce heading match.
1719 (should
1720 (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
1721 (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
1722 (looking-at "\\* Test")))
1723 ;; With a leading star in link, enforce exact heading match, even
1724 ;; with `org-link-search-must-match-exact-headline' set to nil.
1725 (should-error
1726 (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
1727 (let ((org-link-search-must-match-exact-headline nil))
1728 (org-open-at-point))))
1729 ;; Handle non-nil `org-link-search-must-match-exact-headline'.
1730 (should
1731 (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
1732 (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
1733 (looking-at "\\* Test")))
1734 (should
1735 (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
1736 (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
1737 (looking-at "\\* Test")))
1738 ;; Heading match should not care about spaces, cookies, TODO
1739 ;; keywords, priorities, and tags.
1740 (should
1741 (let ((first-line
1742 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1743 (org-test-with-temp-text
1744 (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
1745 (let ((org-link-search-must-match-exact-headline nil)
1746 (org-todo-regexp "TODO"))
1747 (org-open-at-point))
1748 (looking-at (regexp-quote first-line)))))
1749 ;; Heading match should still be exact.
1750 (should-error
1751 (let ((first-line
1752 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1753 (org-test-with-temp-text
1754 (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
1755 (let ((org-link-search-must-match-exact-headline nil)
1756 (org-todo-regexp "TODO"))
1757 (org-open-at-point)))))
1758 ;; Heading match ignores COMMENT keyword.
1759 (should
1760 (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
1761 (org-open-at-point)
1762 (looking-at "\\* COMMENT Test")))
1763 ;; Correctly un-hexify fuzzy links.
1764 (should
1765 (org-test-with-temp-text "* With space\n[[*With%20space][With space<point>]]"
1766 (org-open-at-point)
1767 (bobp))))
1769 ;;;; Link Escaping
1771 (ert-deftest test-org/org-link-escape-ascii-character ()
1772 "Escape an ascii character."
1773 (should
1774 (string=
1775 "%5B"
1776 (org-link-escape "["))))
1778 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
1779 "Escape an ascii control character."
1780 (should
1781 (string=
1782 "%09"
1783 (org-link-escape "\t"))))
1785 (ert-deftest test-org/org-link-escape-multibyte-character ()
1786 "Escape an unicode multibyte character."
1787 (should
1788 (string=
1789 "%E2%82%AC"
1790 (org-link-escape "€"))))
1792 (ert-deftest test-org/org-link-escape-custom-table ()
1793 "Escape string with custom character table."
1794 (should
1795 (string=
1796 "Foo%3A%42ar%0A"
1797 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
1799 (ert-deftest test-org/org-link-escape-custom-table-merge ()
1800 "Escape string with custom table merged with default table."
1801 (should
1802 (string=
1803 "%5BF%6F%6F%3A%42ar%0A%5D"
1804 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
1806 (ert-deftest test-org/org-link-unescape-ascii-character ()
1807 "Unescape an ascii character."
1808 (should
1809 (string=
1811 (org-link-unescape "%5B"))))
1813 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
1814 "Unescpae an ascii control character."
1815 (should
1816 (string=
1817 "\n"
1818 (org-link-unescape "%0A"))))
1820 (ert-deftest test-org/org-link-unescape-multibyte-character ()
1821 "Unescape unicode multibyte character."
1822 (should
1823 (string=
1824 "€"
1825 (org-link-unescape "%E2%82%AC"))))
1827 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
1828 "Unescape old style percent escaped character."
1829 (should
1830 (string=
1831 "àâçèéêîôùû"
1832 (decode-coding-string
1833 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
1835 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
1836 "Escape and unescape a URL that includes an escaped char.
1837 http://article.gmane.org/gmane.emacs.orgmode/21459/"
1838 (should
1839 (string=
1840 "http://some.host.com/form?&id=blah%2Bblah25"
1841 (org-link-unescape
1842 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
1844 (ert-deftest test-org/org-link-escape-chars-browser ()
1845 "Test of the constant `org-link-escape-chars-browser'.
1846 See there why this test is a candidate to be removed once Org
1847 drops support for Emacs 24.1 and 24.2."
1848 (should
1849 (string=
1850 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1851 "%22Release%208.2%22&idxname=emacs-orgmode")
1852 (org-link-escape-browser ; Do not replace with `url-encode-url',
1853 ; see docstring above.
1854 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1855 "\"Release 8.2\"&idxname=emacs-orgmode")))))
1857 ;;;; Open at point
1859 (ert-deftest test-org/open-at-point-in-keyword ()
1860 "Does `org-open-at-point' open link in a keyword line?"
1861 (should
1862 (org-test-with-temp-text
1863 "#+KEYWORD: <point>[[info:emacs#Top]]"
1864 (org-open-at-point) t)))
1866 (ert-deftest test-org/open-at-point-in-property ()
1867 "Does `org-open-at-point' open link in property drawer?"
1868 (should
1869 (org-test-with-temp-text
1870 "* Headline
1871 :PROPERTIES:
1872 :URL: <point>[[info:emacs#Top]]
1873 :END:"
1874 (org-open-at-point) t)))
1876 (ert-deftest test-org/open-at-point-in-comment ()
1877 "Does `org-open-at-point' open link in a commented line?"
1878 (should
1879 (org-test-with-temp-text
1880 "# <point>[[info:emacs#Top]]"
1881 (org-open-at-point) t)))
1883 (ert-deftest test-org/open-at-point/info ()
1884 "Test `org-open-at-point' on info links."
1885 (should
1886 (org-test-with-temp-text
1887 "<point>[[info:emacs#Top]]"
1888 (org-open-at-point)
1889 (and (switch-to-buffer "*info*")
1890 (prog1
1891 (looking-at "\nThe Emacs Editor")
1892 (kill-buffer))))))
1894 (ert-deftest test-org/open-at-point/inline-image ()
1895 "Test `org-open-at-point' on nested links."
1896 (should
1897 (org-test-with-temp-text "[[info:org#Top][info:<point>emacs#Top]]"
1898 (org-open-at-point)
1899 (prog1 (with-current-buffer "*info*" (looking-at "\nOrg Mode Manual"))
1900 (kill-buffer "*info*")))))
1902 (ert-deftest test-org/open-at-point/radio-target ()
1903 "Test `org-open-at-point' on radio targets."
1904 (should
1905 (org-test-with-temp-text "<<<target>>> <point>target"
1906 (org-update-radio-target-regexp)
1907 (org-open-at-point)
1908 (eq (org-element-type (org-element-context)) 'radio-target))))
1910 ;;;; Stored links
1912 (ert-deftest test-org/store-link ()
1913 "Test `org-store-link' specifications."
1914 ;; On a headline, link to that headline. Use heading as the
1915 ;; description of the link.
1916 (should
1917 (let (org-store-link-props org-stored-links)
1918 (org-test-with-temp-text-in-file "* H1"
1919 (let ((file (buffer-file-name)))
1920 (equal (format "[[file:%s::*H1][H1]]" file)
1921 (org-store-link nil))))))
1922 ;; On a headline, remove any link from description.
1923 (should
1924 (let (org-store-link-props org-stored-links)
1925 (org-test-with-temp-text-in-file "* [[#l][d]]"
1926 (let ((file (buffer-file-name)))
1927 (equal (format "[[file:%s::*%s][d]]"
1928 file
1929 (org-link-escape "[[#l][d]]"))
1930 (org-store-link nil))))))
1931 (should
1932 (let (org-store-link-props org-stored-links)
1933 (org-test-with-temp-text-in-file "* [[l]]"
1934 (let ((file (buffer-file-name)))
1935 (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
1936 (org-store-link nil))))))
1937 (should
1938 (let (org-store-link-props org-stored-links)
1939 (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
1940 (let ((file (buffer-file-name)))
1941 (equal (format "[[file:%s::*%s][d1 d2]]"
1942 file
1943 (org-link-escape "[[l1][d1]] [[l2][d2]]"))
1944 (org-store-link nil))))))
1945 ;; On a named element, link to that element.
1946 (should
1947 (let (org-store-link-props org-stored-links)
1948 (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
1949 (let ((file (buffer-file-name)))
1950 (equal (format "[[file:%s::foo][foo]]" file)
1951 (org-store-link nil)))))))
1954 ;;; Node Properties
1956 (ert-deftest test-org/accumulated-properties-in-drawers ()
1957 "Ensure properties accumulate in subtree drawers."
1958 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
1959 (org-babel-next-src-block)
1960 (should (equal '(2 1) (org-babel-execute-src-block)))))
1962 (ert-deftest test-org/custom-properties ()
1963 "Test custom properties specifications."
1964 ;; Standard test.
1965 (should
1966 (let ((org-custom-properties '("FOO")))
1967 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1968 (org-toggle-custom-properties-visibility)
1969 (org-invisible-p2))))
1970 ;; Properties are case-insensitive.
1971 (should
1972 (let ((org-custom-properties '("FOO")))
1973 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
1974 (org-toggle-custom-properties-visibility)
1975 (org-invisible-p2))))
1976 (should
1977 (let ((org-custom-properties '("foo")))
1978 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1979 (org-toggle-custom-properties-visibility)
1980 (org-invisible-p2))))
1981 ;; Multiple custom properties in the same drawer.
1982 (should
1983 (let ((org-custom-properties '("FOO" "BAR")))
1984 (org-test-with-temp-text
1985 "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
1986 (org-toggle-custom-properties-visibility)
1987 (and (org-invisible-p2)
1988 (not (progn (forward-line) (org-invisible-p2)))
1989 (progn (forward-line) (org-invisible-p2))))))
1990 ;; Hide custom properties with an empty value.
1991 (should
1992 (let ((org-custom-properties '("FOO")))
1993 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
1994 (org-toggle-custom-properties-visibility)
1995 (org-invisible-p2))))
1996 ;; Do not hide fake properties.
1997 (should-not
1998 (let ((org-custom-properties '("FOO")))
1999 (org-test-with-temp-text ":FOO: val\n"
2000 (org-toggle-custom-properties-visibility)
2001 (org-invisible-p2))))
2002 (should-not
2003 (let ((org-custom-properties '("A")))
2004 (org-test-with-temp-text
2005 "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
2006 (org-toggle-custom-properties-visibility)
2007 (org-invisible-p2)))))
2011 ;;; Mark Region
2013 (ert-deftest test-org/mark-subtree ()
2014 "Test `org-mark-subtree' specifications."
2015 ;; Error when point is before first headline.
2016 (should-error
2017 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
2018 (progn (transient-mark-mode 1)
2019 (org-mark-subtree))))
2020 ;; Without argument, mark current subtree.
2021 (should
2022 (equal
2023 '(12 32)
2024 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
2025 (progn (transient-mark-mode 1)
2026 (forward-line 2)
2027 (org-mark-subtree)
2028 (list (region-beginning) (region-end))))))
2029 ;; With an argument, move ARG up.
2030 (should
2031 (equal
2032 '(1 32)
2033 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
2034 (progn (transient-mark-mode 1)
2035 (forward-line 2)
2036 (org-mark-subtree 1)
2037 (list (region-beginning) (region-end))))))
2038 ;; Do not get fooled by inlinetasks.
2039 (when (featurep 'org-inlinetask)
2040 (should
2041 (= 1
2042 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
2043 (progn (transient-mark-mode 1)
2044 (forward-line 1)
2045 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
2046 (region-beginning)))))))
2050 ;;; Miscellaneous
2052 (ert-deftest test-org/in-regexp ()
2053 "Test `org-in-regexp' specifications."
2054 ;; Standard tests.
2055 (should
2056 (org-test-with-temp-text "xx ab<point>c xx"
2057 (org-in-regexp "abc")))
2058 (should-not
2059 (org-test-with-temp-text "xx abc <point>xx"
2060 (org-in-regexp "abc")))
2061 ;; Return non-nil even with multiple matching regexps in the same
2062 ;; line.
2063 (should
2064 (org-test-with-temp-text "abc xx ab<point>c xx"
2065 (org-in-regexp "abc")))
2066 ;; With optional argument NLINES, check extra lines around point.
2067 (should-not
2068 (org-test-with-temp-text "A\nB<point>\nC"
2069 (org-in-regexp "A\nB\nC")))
2070 (should
2071 (org-test-with-temp-text "A\nB<point>\nC"
2072 (org-in-regexp "A\nB\nC" 1)))
2073 (should-not
2074 (org-test-with-temp-text "A\nB\nC<point>"
2075 (org-in-regexp "A\nB\nC" 1)))
2076 ;; When optional argument VISUALLY is non-nil, return nil if at
2077 ;; regexp boundaries.
2078 (should
2079 (org-test-with-temp-text "xx abc<point> xx"
2080 (org-in-regexp "abc")))
2081 (should-not
2082 (org-test-with-temp-text "xx abc<point> xx"
2083 (org-in-regexp "abc" nil t))))
2086 ;;; Navigation
2088 (ert-deftest test-org/end-of-meta-data ()
2089 "Test `org-end-of-meta-data' specifications."
2090 ;; Skip planning line.
2091 (should
2092 (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
2093 (org-end-of-meta-data)
2094 (eobp)))
2095 ;; Skip properties drawer.
2096 (should
2097 (org-test-with-temp-text
2098 "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
2099 (org-end-of-meta-data)
2100 (eobp)))
2101 ;; Skip both.
2102 (should
2103 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
2104 (org-end-of-meta-data)
2105 (eobp)))
2106 ;; Nothing to skip, go to first line.
2107 (should
2108 (org-test-with-temp-text "* Headline\nContents"
2109 (org-end-of-meta-data)
2110 (looking-at "Contents")))
2111 ;; With option argument, skip empty lines, regular drawers and
2112 ;; clocking lines.
2113 (should
2114 (org-test-with-temp-text "* Headline\n\nContents"
2115 (org-end-of-meta-data t)
2116 (looking-at "Contents")))
2117 (should
2118 (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
2119 (org-end-of-meta-data t)
2120 (looking-at "Contents")))
2121 (should
2122 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
2123 (org-end-of-meta-data t)
2124 (looking-at "Contents")))
2125 ;; Special case: do not skip incomplete drawers.
2126 (should
2127 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
2128 (org-end-of-meta-data t)
2129 (looking-at ":LOGBOOK:")))
2130 ;; Special case: Be careful about consecutive headlines.
2131 (should-not
2132 (org-test-with-temp-text "* H1\n*H2\nContents"
2133 (org-end-of-meta-data t)
2134 (looking-at "Contents"))))
2136 (ert-deftest test-org/beginning-of-line ()
2137 "Test `org-beginning-of-line' specifications."
2138 ;; Standard test.
2139 (should
2140 (org-test-with-temp-text "Some text\nSome other text"
2141 (progn (org-beginning-of-line) (bolp))))
2142 ;; Standard test with `visual-line-mode'.
2143 (should-not
2144 (org-test-with-temp-text "A long line of text\nSome other text"
2145 (progn (visual-line-mode)
2146 (forward-char 2)
2147 (dotimes (i 1000) (insert "very "))
2148 (org-beginning-of-line)
2149 (bolp))))
2150 ;; At an headline with special movement.
2151 (should
2152 (org-test-with-temp-text "* TODO Headline"
2153 (let ((org-special-ctrl-a/e t))
2154 (org-end-of-line)
2155 (and (progn (org-beginning-of-line) (looking-at "Headline"))
2156 (progn (org-beginning-of-line) (bolp))
2157 (progn (org-beginning-of-line) (looking-at "Headline"))))))
2158 ;; Special case: Do not error when the buffer contains only a single
2159 ;; asterisk.
2160 (should
2161 (org-test-with-temp-text "*<point>"
2162 (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))))
2163 (should
2164 (org-test-with-temp-text "*<point>"
2165 (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line)))))
2167 (ert-deftest test-org/end-of-line ()
2168 "Test `org-end-of-line' specifications."
2169 ;; Standard test.
2170 (should
2171 (org-test-with-temp-text "Some text\nSome other text"
2172 (progn (org-end-of-line) (eolp))))
2173 ;; Standard test with `visual-line-mode'.
2174 (should-not
2175 (org-test-with-temp-text "A long line of text\nSome other text"
2176 (progn (visual-line-mode)
2177 (forward-char 2)
2178 (dotimes (i 1000) (insert "very "))
2179 (goto-char (point-min))
2180 (org-end-of-line)
2181 (eolp))))
2182 ;; At an headline with special movement.
2183 (should
2184 (org-test-with-temp-text "* Headline1 :tag:\n"
2185 (let ((org-special-ctrl-a/e t))
2186 (and (progn (org-end-of-line) (looking-at " :tag:"))
2187 (progn (org-end-of-line) (eolp))
2188 (progn (org-end-of-line) (looking-at " :tag:"))))))
2189 ;; At an headline without special movement.
2190 (should
2191 (org-test-with-temp-text "* Headline2 :tag:\n"
2192 (let ((org-special-ctrl-a/e nil))
2193 (and (progn (org-end-of-line) (eolp))
2194 (progn (org-end-of-line) (eolp))))))
2195 ;; At an headline, with reversed movement.
2196 (should
2197 (org-test-with-temp-text "* Headline3 :tag:\n"
2198 (let ((org-special-ctrl-a/e 'reversed)
2199 (this-command last-command))
2200 (and (progn (org-end-of-line) (eolp))
2201 (progn (org-end-of-line) (looking-at " :tag:"))))))
2202 ;; At a block without hidden contents.
2203 (should
2204 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
2205 (progn (org-end-of-line) (eolp))))
2206 ;; At a block with hidden contents.
2207 (should-not
2208 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
2209 (let ((org-special-ctrl-a/e t))
2210 (org-hide-block-toggle)
2211 (org-end-of-line)
2212 (eobp)))))
2214 (ert-deftest test-org/forward-sentence ()
2215 "Test `org-forward-sentence' specifications."
2216 ;; At the end of a table cell, move to the end of the next one.
2217 (should
2218 (org-test-with-temp-text "| a<point> | b |"
2219 (org-forward-sentence)
2220 (looking-at " |$")))
2221 ;; Elsewhere in a cell, move to its end.
2222 (should
2223 (org-test-with-temp-text "| a<point>c | b |"
2224 (org-forward-sentence)
2225 (looking-at " | b |$")))
2226 ;; Otherwise, simply call `forward-sentence'.
2227 (should
2228 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
2229 (org-forward-sentence)
2230 (looking-at " Sentence 2.")))
2231 (should
2232 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
2233 (org-forward-sentence)
2234 (org-forward-sentence)
2235 (eobp)))
2236 ;; At the end of an element, jump to the next one, without stopping
2237 ;; on blank lines in-between.
2238 (should
2239 (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
2240 (org-forward-sentence)
2241 (eobp))))
2243 (ert-deftest test-org/backward-sentence ()
2244 "Test `org-backward-sentence' specifications."
2245 ;; At the beginning of a table cell, move to the beginning of the
2246 ;; previous one.
2247 (should
2248 (org-test-with-temp-text "| a | <point>b |"
2249 (org-backward-sentence)
2250 (looking-at "a | b |$")))
2251 ;; Elsewhere in a cell, move to its beginning.
2252 (should
2253 (org-test-with-temp-text "| a | b<point>c |"
2254 (org-backward-sentence)
2255 (looking-at "bc |$")))
2256 ;; Otherwise, simply call `backward-sentence'.
2257 (should
2258 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
2259 (org-backward-sentence)
2260 (looking-at "Sentence 2.")))
2261 (should
2262 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
2263 (org-backward-sentence)
2264 (org-backward-sentence)
2265 (bobp)))
2266 ;; Make sure to hit the beginning of a sentence on the same line as
2267 ;; an item.
2268 (should
2269 (org-test-with-temp-text "- Line 1\n line <point>2."
2270 (org-backward-sentence)
2271 (looking-at "Line 1"))))
2273 (ert-deftest test-org/forward-paragraph ()
2274 "Test `org-forward-paragraph' specifications."
2275 ;; At end of buffer, return an error.
2276 (should-error
2277 (org-test-with-temp-text "Paragraph"
2278 (goto-char (point-max))
2279 (org-forward-paragraph)))
2280 ;; Standard test.
2281 (should
2282 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2283 (org-forward-paragraph)
2284 (looking-at "P2")))
2285 ;; Ignore depth.
2286 (should
2287 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
2288 (org-forward-paragraph)
2289 (looking-at "P1")))
2290 ;; Do not enter elements with invisible contents.
2291 (should
2292 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
2293 (org-hide-block-toggle)
2294 (org-forward-paragraph)
2295 (looking-at "P3")))
2296 ;; On an affiliated keyword, jump to the beginning of the element.
2297 (should
2298 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
2299 (org-forward-paragraph)
2300 (looking-at "Para")))
2301 ;; On an item or a footnote definition, move to the second element
2302 ;; inside, if any.
2303 (should
2304 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
2305 (org-forward-paragraph)
2306 (looking-at " Paragraph")))
2307 (should
2308 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
2309 (org-forward-paragraph)
2310 (looking-at "Paragraph")))
2311 ;; On an item, or a footnote definition, when the first line is
2312 ;; empty, move to the first item.
2313 (should
2314 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
2315 (org-forward-paragraph)
2316 (looking-at " Paragraph")))
2317 (should
2318 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
2319 (org-forward-paragraph)
2320 (looking-at "Paragraph")))
2321 ;; On a table (resp. a property drawer) do not move through table
2322 ;; rows (resp. node properties).
2323 (should
2324 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
2325 (org-forward-paragraph)
2326 (looking-at "Paragraph")))
2327 (should
2328 (org-test-with-temp-text
2329 "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
2330 (org-forward-paragraph)
2331 (looking-at "Paragraph")))
2332 ;; On a verse or source block, stop after blank lines.
2333 (should
2334 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
2335 (org-forward-paragraph)
2336 (looking-at "L2")))
2337 (should
2338 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
2339 (org-forward-paragraph)
2340 (looking-at "L2"))))
2342 (ert-deftest test-org/backward-paragraph ()
2343 "Test `org-backward-paragraph' specifications."
2344 ;; Error at beginning of buffer.
2345 (should-error
2346 (org-test-with-temp-text "Paragraph"
2347 (org-backward-paragraph)))
2348 ;; Regular test.
2349 (should
2350 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2351 (goto-char (point-max))
2352 (org-backward-paragraph)
2353 (looking-at "P3")))
2354 (should
2355 (org-test-with-temp-text "P1\n\nP2\n\nP3"
2356 (goto-char (point-max))
2357 (beginning-of-line)
2358 (org-backward-paragraph)
2359 (looking-at "P2")))
2360 ;; Ignore depth.
2361 (should
2362 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
2363 (goto-char (point-max))
2364 (beginning-of-line)
2365 (org-backward-paragraph)
2366 (looking-at "P2")))
2367 ;; Ignore invisible elements.
2368 (should
2369 (org-test-with-temp-text "* H1\n P1\n* H2"
2370 (org-cycle)
2371 (goto-char (point-max))
2372 (beginning-of-line)
2373 (org-backward-paragraph)
2374 (bobp)))
2375 ;; On an affiliated keyword, jump to the first one.
2376 (should
2377 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
2378 (search-forward "c2")
2379 (org-backward-paragraph)
2380 (looking-at "#\\+name")))
2381 ;; On the second element in an item or a footnote definition, jump
2382 ;; to item or the definition.
2383 (should
2384 (org-test-with-temp-text "- line1\n\n line2"
2385 (goto-char (point-max))
2386 (beginning-of-line)
2387 (org-backward-paragraph)
2388 (looking-at "- line1")))
2389 (should
2390 (org-test-with-temp-text "[fn:1] line1\n\n line2"
2391 (goto-char (point-max))
2392 (beginning-of-line)
2393 (org-backward-paragraph)
2394 (looking-at "\\[fn:1\\] line1")))
2395 ;; On a table (resp. a property drawer), ignore table rows
2396 ;; (resp. node properties).
2397 (should
2398 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
2399 (goto-char (point-max))
2400 (beginning-of-line)
2401 (org-backward-paragraph)
2402 (bobp)))
2403 (should
2404 (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
2405 (org-backward-paragraph)
2406 (looking-at ":PROPERTIES:")))
2407 ;; On a source or verse block, stop before blank lines.
2408 (should
2409 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
2410 (search-forward "L3")
2411 (beginning-of-line)
2412 (org-backward-paragraph)
2413 (looking-at "L2")))
2414 (should
2415 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
2416 (search-forward "L3")
2417 (beginning-of-line)
2418 (org-backward-paragraph)
2419 (looking-at "L2"))))
2421 (ert-deftest test-org/forward-element ()
2422 "Test `org-forward-element' specifications."
2423 ;; 1. At EOB: should error.
2424 (org-test-with-temp-text "Some text\n"
2425 (goto-char (point-max))
2426 (should-error (org-forward-element)))
2427 ;; 2. Standard move: expected to ignore blank lines.
2428 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
2429 (org-forward-element)
2430 (should (looking-at (regexp-quote "Second paragraph."))))
2431 ;; 3. Headline tests.
2432 (org-test-with-temp-text "
2433 * Head 1
2434 ** Head 1.1
2435 *** Head 1.1.1
2436 ** Head 1.2"
2437 ;; 3.1. At an headline beginning: move to next headline at the
2438 ;; same level.
2439 (goto-line 3)
2440 (org-forward-element)
2441 (should (looking-at (regexp-quote "** Head 1.2")))
2442 ;; 3.2. At an headline beginning: move to parent headline if no
2443 ;; headline at the same level.
2444 (goto-line 3)
2445 (org-forward-element)
2446 (should (looking-at (regexp-quote "** Head 1.2"))))
2447 ;; 4. Greater element tests.
2448 (org-test-with-temp-text
2449 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
2450 ;; 4.1. At a greater element: expected to skip contents.
2451 (org-forward-element)
2452 (should (looking-at (regexp-quote "Outside.")))
2453 ;; 4.2. At the end of greater element contents: expected to skip
2454 ;; to the end of the greater element.
2455 (goto-line 2)
2456 (org-forward-element)
2457 (should (looking-at (regexp-quote "Outside."))))
2458 ;; 5. List tests.
2459 (org-test-with-temp-text "
2460 - item1
2462 - sub1
2464 - sub2
2466 - sub3
2468 Inner paragraph.
2470 - item2
2472 Outside."
2473 ;; 5.1. At list top point: expected to move to the element after
2474 ;; the list.
2475 (goto-line 2)
2476 (org-forward-element)
2477 (should (looking-at (regexp-quote "Outside.")))
2478 ;; 5.2. Special case: at the first line of a sub-list, but not at
2479 ;; beginning of line, move to next item.
2480 (goto-line 2)
2481 (forward-char)
2482 (org-forward-element)
2483 (should (looking-at "- item2"))
2484 (goto-line 4)
2485 (forward-char)
2486 (org-forward-element)
2487 (should (looking-at " - sub2"))
2488 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
2489 (goto-line 4)
2490 (org-forward-element)
2491 (should (looking-at (regexp-quote " Inner paragraph.")))
2492 ;; 5.4. At sub-list end: expected to move outside the sub-list.
2493 (goto-line 8)
2494 (org-forward-element)
2495 (should (looking-at (regexp-quote " Inner paragraph.")))
2496 ;; 5.5. At an item: expected to move to next item, if any.
2497 (goto-line 6)
2498 (org-forward-element)
2499 (should (looking-at " - sub3"))))
2501 (ert-deftest test-org/backward-element ()
2502 "Test `org-backward-element' specifications."
2503 ;; 1. Should error at BOB.
2504 (org-test-with-temp-text " \nParagraph."
2505 (should-error (org-backward-element)))
2506 ;; 2. Should move at BOB when called on the first element in buffer.
2507 (should
2508 (org-test-with-temp-text "\n#+TITLE: test"
2509 (progn (forward-line)
2510 (org-backward-element)
2511 (bobp))))
2512 ;; 3. Not at the beginning of an element: move at its beginning.
2513 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2514 (goto-line 3)
2515 (end-of-line)
2516 (org-backward-element)
2517 (should (looking-at (regexp-quote "Paragraph2."))))
2518 ;; 4. Headline tests.
2519 (org-test-with-temp-text "
2520 * Head 1
2521 ** Head 1.1
2522 *** Head 1.1.1
2523 ** Head 1.2"
2524 ;; 4.1. At an headline beginning: move to previous headline at the
2525 ;; same level.
2526 (goto-line 5)
2527 (org-backward-element)
2528 (should (looking-at (regexp-quote "** Head 1.1")))
2529 ;; 4.2. At an headline beginning: move to parent headline if no
2530 ;; headline at the same level.
2531 (goto-line 3)
2532 (org-backward-element)
2533 (should (looking-at (regexp-quote "* Head 1")))
2534 ;; 4.3. At the first top-level headline: should error.
2535 (goto-line 2)
2536 (should-error (org-backward-element)))
2537 ;; 5. At beginning of first element inside a greater element:
2538 ;; expected to move to greater element's beginning.
2539 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
2540 (goto-line 3)
2541 (org-backward-element)
2542 (should (looking-at "#\\+BEGIN_CENTER")))
2543 ;; 6. At the beginning of the first element in a section: should
2544 ;; move back to headline, if any.
2545 (should
2546 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
2547 (progn (goto-char (point-max))
2548 (beginning-of-line)
2549 (org-backward-element)
2550 (org-at-heading-p))))
2551 ;; 7. List tests.
2552 (org-test-with-temp-text "
2553 - item1
2555 - sub1
2557 - sub2
2559 - sub3
2561 Inner paragraph.
2563 - item2
2566 Outside."
2567 ;; 7.1. At beginning of sub-list: expected to move to the
2568 ;; paragraph before it.
2569 (goto-line 4)
2570 (org-backward-element)
2571 (should (looking-at "item1"))
2572 ;; 7.2. At an item in a list: expected to move at previous item.
2573 (goto-line 8)
2574 (org-backward-element)
2575 (should (looking-at " - sub2"))
2576 (goto-line 12)
2577 (org-backward-element)
2578 (should (looking-at "- item1"))
2579 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
2580 ;; beginning.
2581 (goto-line 10)
2582 (org-backward-element)
2583 (should (looking-at " - sub1"))
2584 (goto-line 15)
2585 (org-backward-element)
2586 (should (looking-at "- item1"))
2587 ;; 7.4. At blank-lines before list end: expected to move to top
2588 ;; item.
2589 (goto-line 14)
2590 (org-backward-element)
2591 (should (looking-at "- item1"))))
2593 (ert-deftest test-org/up-element ()
2594 "Test `org-up-element' specifications."
2595 ;; 1. At BOB or with no surrounding element: should error.
2596 (org-test-with-temp-text "Paragraph."
2597 (should-error (org-up-element)))
2598 (org-test-with-temp-text "* Head1\n* Head2"
2599 (goto-line 2)
2600 (should-error (org-up-element)))
2601 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2602 (goto-line 3)
2603 (should-error (org-up-element)))
2604 ;; 2. At an headline: move to parent headline.
2605 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
2606 (goto-line 3)
2607 (org-up-element)
2608 (should (looking-at "\\* Head1")))
2609 ;; 3. Inside a greater element: move to greater element beginning.
2610 (org-test-with-temp-text
2611 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
2612 (goto-line 3)
2613 (org-up-element)
2614 (should (looking-at "#\\+BEGIN_CENTER")))
2615 ;; 4. List tests.
2616 (org-test-with-temp-text "* Top
2617 - item1
2619 - sub1
2621 - sub2
2623 Paragraph within sub2.
2625 - item2"
2626 ;; 4.1. Within an item: move to the item beginning.
2627 (goto-line 8)
2628 (org-up-element)
2629 (should (looking-at " - sub2"))
2630 ;; 4.2. At an item in a sub-list: move to parent item.
2631 (goto-line 4)
2632 (org-up-element)
2633 (should (looking-at "- item1"))
2634 ;; 4.3. At an item in top list: move to beginning of whole list.
2635 (goto-line 10)
2636 (org-up-element)
2637 (should (looking-at "- item1"))
2638 ;; 4.4. Special case. At very top point: should move to parent of
2639 ;; list.
2640 (goto-line 2)
2641 (org-up-element)
2642 (should (looking-at "\\* Top"))))
2644 (ert-deftest test-org/down-element ()
2645 "Test `org-down-element' specifications."
2646 ;; Error when the element hasn't got a recursive type.
2647 (org-test-with-temp-text "Paragraph."
2648 (should-error (org-down-element)))
2649 ;; Error when the element has no contents
2650 (org-test-with-temp-text "* Headline"
2651 (should-error (org-down-element)))
2652 ;; When at a plain-list, move to first item.
2653 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
2654 (goto-line 2)
2655 (org-down-element)
2656 (should (looking-at " - Item 1.1")))
2657 (org-test-with-temp-text "#+NAME: list\n- Item 1"
2658 (org-down-element)
2659 (should (looking-at " Item 1")))
2660 ;; When at a table, move to first row
2661 (org-test-with-temp-text "#+NAME: table\n| a | b |"
2662 (org-down-element)
2663 (should (looking-at " a | b |")))
2664 ;; Otherwise, move inside the greater element.
2665 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
2666 (org-down-element)
2667 (should (looking-at "Paragraph"))))
2669 (ert-deftest test-org/drag-element-backward ()
2670 "Test `org-drag-element-backward' specifications."
2671 ;; Standard test.
2672 (should
2673 (equal
2674 "#+key2: val2\n#+key1: val1\n#+key3: val3"
2675 (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
2676 (org-drag-element-backward)
2677 (buffer-string))))
2678 (should
2679 (equal
2680 "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
2681 (org-test-with-temp-text
2682 "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
2683 (org-drag-element-backward)
2684 (buffer-string))))
2685 ;; Preserve blank lines.
2686 (should
2687 (equal "Paragraph 2\n\n\nPara1\n\nPara3"
2688 (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
2689 (org-drag-element-backward)
2690 (buffer-string))))
2691 ;; Preserve column.
2692 (should
2693 (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
2694 (org-drag-element-backward)
2695 (org-looking-at-p "2")))
2696 ;; Error when trying to move first element of buffer.
2697 (should-error
2698 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2699 (org-drag-element-backward)))
2700 ;; Error when trying to swap nested elements.
2701 (should-error
2702 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2703 (forward-line)
2704 (org-drag-element-backward)))
2705 ;; Error when trying to swap an headline element and a non-headline
2706 ;; element.
2707 (should-error
2708 (org-test-with-temp-text "Test.\n* Head 1"
2709 (forward-line)
2710 (org-drag-element-backward)))
2711 ;; Preserve visibility of elements and their contents.
2712 (should
2713 (equal '((63 . 82) (26 . 48))
2714 (org-test-with-temp-text "
2715 #+BEGIN_CENTER
2716 Text.
2717 #+END_CENTER
2718 - item 1
2719 #+BEGIN_QUOTE
2720 Text.
2721 #+END_QUOTE"
2722 (while (search-forward "BEGIN_" nil t) (org-cycle))
2723 (search-backward "- item 1")
2724 (org-drag-element-backward)
2725 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2726 (overlays-in (point-min) (point-max)))))))
2728 (ert-deftest test-org/drag-element-forward ()
2729 "Test `org-drag-element-forward' specifications."
2730 ;; 1. Error when trying to move first element of buffer.
2731 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2732 (goto-line 3)
2733 (should-error (org-drag-element-forward)))
2734 ;; 2. Error when trying to swap nested elements.
2735 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2736 (forward-line)
2737 (should-error (org-drag-element-forward)))
2738 ;; 3. Error when trying to swap a non-headline element and an
2739 ;; headline.
2740 (org-test-with-temp-text "Test.\n* Head 1"
2741 (should-error (org-drag-element-forward)))
2742 ;; 4. Otherwise, swap elements, preserving column and blank lines
2743 ;; between elements.
2744 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
2745 (search-forward "graph")
2746 (org-drag-element-forward)
2747 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
2748 (should (looking-at " 1")))
2749 ;; 5. Preserve visibility of elements and their contents.
2750 (org-test-with-temp-text "
2751 #+BEGIN_CENTER
2752 Text.
2753 #+END_CENTER
2754 - item 1
2755 #+BEGIN_QUOTE
2756 Text.
2757 #+END_QUOTE"
2758 (while (search-forward "BEGIN_" nil t) (org-cycle))
2759 (search-backward "#+BEGIN_CENTER")
2760 (org-drag-element-forward)
2761 (should
2762 (equal
2763 '((63 . 82) (26 . 48))
2764 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2765 (overlays-in (point-min) (point-max)))))))
2767 (ert-deftest test-org/next-block ()
2768 "Test `org-next-block' specifications."
2769 ;; Regular test.
2770 (should
2771 (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
2772 (org-next-block 1)
2773 (looking-at "#\\+BEGIN_CENTER")))
2774 ;; Ignore case.
2775 (should
2776 (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
2777 (let ((case-fold-search nil))
2778 (org-next-block 1)
2779 (looking-at "#\\+begin_center"))))
2780 ;; Ignore current line.
2781 (should
2782 (org-test-with-temp-text
2783 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
2784 (org-next-block 1)
2785 (looking-at "#\\+BEGIN_CENTER")))
2786 ;; Throw an error when no block is found.
2787 (should-error
2788 (org-test-with-temp-text "Paragraph"
2789 (org-next-block 1)))
2790 ;; With an argument, skip many blocks at once.
2791 (should
2792 (org-test-with-temp-text
2793 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
2794 (org-next-block 2)
2795 (looking-at "#\\+BEGIN_QUOTE")))
2796 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
2797 (should
2798 (org-test-with-temp-text
2799 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
2800 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
2801 (looking-at "#\\+BEGIN_QUOTE")))
2802 ;; Optional argument is also case-insensitive.
2803 (should
2804 (org-test-with-temp-text
2805 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
2806 (let ((case-fold-search nil))
2807 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
2808 (looking-at "#\\+begin_quote")))))
2810 (ert-deftest test-org/previous-block ()
2811 "Test `org-previous-block' specifications."
2812 ;; Regular test.
2813 (should
2814 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
2815 (org-previous-block 1)
2816 (looking-at "#\\+BEGIN_CENTER")))
2817 ;; Ignore case.
2818 (should
2819 (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
2820 (let ((case-fold-search nil))
2821 (org-previous-block 1)
2822 (looking-at "#\\+begin_center"))))
2823 ;; Ignore current line.
2824 (should
2825 (org-test-with-temp-text
2826 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
2827 (org-previous-block 1)
2828 (looking-at "#\\+BEGIN_QUOTE")))
2829 ;; Throw an error when no block is found.
2830 (should-error
2831 (org-test-with-temp-text "Paragraph<point>"
2832 (org-previous-block 1)))
2833 ;; With an argument, skip many blocks at once.
2834 (should
2835 (org-test-with-temp-text
2836 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
2837 (org-previous-block 2)
2838 (looking-at "#\\+BEGIN_CENTER")))
2839 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
2840 (should
2841 (org-test-with-temp-text
2842 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
2843 (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
2844 (looking-at "#\\+BEGIN_QUOTE")))
2845 ;; Optional argument is also case-insensitive.
2846 (should
2847 (org-test-with-temp-text
2848 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
2849 (let ((case-fold-search nil))
2850 (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
2851 (looking-at "#\\+begin_quote")))))
2854 ;;; Outline structure
2856 (ert-deftest test-org/demote ()
2857 "Test `org-demote' specifications."
2858 ;; Add correct number of stars according to `org-odd-levels-only'.
2859 (should
2860 (= 2
2861 (org-test-with-temp-text "* H"
2862 (let ((org-odd-levels-only nil)) (org-demote))
2863 (org-current-level))))
2864 (should
2865 (= 3
2866 (org-test-with-temp-text "* H"
2867 (let ((org-odd-levels-only t)) (org-demote))
2868 (org-current-level))))
2869 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2870 (should
2871 (org-test-with-temp-text "* H :tag:"
2872 (let ((org-tags-column 10)
2873 (org-auto-align-tags t)
2874 (org-odd-levels-only nil))
2875 (org-demote))
2876 (org-move-to-column 10)
2877 (org-looking-at-p ":tag:$")))
2878 (should-not
2879 (org-test-with-temp-text "* H :tag:"
2880 (let ((org-tags-column 10)
2881 (org-auto-align-tags nil)
2882 (org-odd-levels-only nil))
2883 (org-demote))
2884 (org-move-to-column 10)
2885 (org-looking-at-p ":tag:$")))
2886 ;; When `org-adapt-indentation' is non-nil, always indent planning
2887 ;; info and property drawers accordingly.
2888 (should
2889 (= 3
2890 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2891 (let ((org-odd-levels-only nil)
2892 (org-adapt-indentation t))
2893 (org-demote))
2894 (forward-line)
2895 (org-get-indentation))))
2896 (should
2897 (= 3
2898 (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2899 (let ((org-odd-levels-only nil)
2900 (org-adapt-indentation t))
2901 (org-demote))
2902 (forward-line)
2903 (org-get-indentation))))
2904 (should-not
2905 (= 3
2906 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2907 (let ((org-odd-levels-only nil)
2908 (org-adapt-indentation nil))
2909 (org-demote))
2910 (forward-line)
2911 (org-get-indentation))))
2912 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2913 ;; section accordingly. Ignore, however, footnote definitions and
2914 ;; inlinetasks boundaries.
2915 (should
2916 (= 3
2917 (org-test-with-temp-text "* H\n Paragraph"
2918 (let ((org-odd-levels-only nil)
2919 (org-adapt-indentation t))
2920 (org-demote))
2921 (forward-line)
2922 (org-get-indentation))))
2923 (should
2924 (= 2
2925 (org-test-with-temp-text "* H\n Paragraph"
2926 (let ((org-odd-levels-only nil)
2927 (org-adapt-indentation nil))
2928 (org-demote))
2929 (forward-line)
2930 (org-get-indentation))))
2931 (should
2932 (zerop
2933 (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
2934 (let ((org-odd-levels-only nil)
2935 (org-adapt-indentation t))
2936 (org-demote))
2937 (goto-char (point-max))
2938 (org-get-indentation))))
2939 (should
2940 (= 3
2941 (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
2942 (let ((org-odd-levels-only nil)
2943 (org-adapt-indentation t))
2944 (org-demote))
2945 (goto-char (point-max))
2946 (org-get-indentation))))
2947 (when (featurep 'org-inlinetask)
2948 (should
2949 (zerop
2950 (let ((org-inlinetask-min-level 5)
2951 (org-adapt-indentation t))
2952 (org-test-with-temp-text "* H\n***** I\n***** END"
2953 (org-demote)
2954 (forward-line)
2955 (org-get-indentation))))))
2956 (when (featurep 'org-inlinetask)
2957 (should
2958 (= 3
2959 (let ((org-inlinetask-min-level 5)
2960 (org-adapt-indentation t))
2961 (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
2962 (org-demote)
2963 (forward-line 2)
2964 (org-get-indentation))))))
2965 ;; Ignore contents of source blocks or example blocks when
2966 ;; indentation should be preserved (through
2967 ;; `org-src-preserve-indentation' or "-i" flag).
2968 (should-not
2969 (zerop
2970 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2971 (let ((org-adapt-indentation t)
2972 (org-src-preserve-indentation nil))
2973 (org-demote))
2974 (forward-line 2)
2975 (org-get-indentation))))
2976 (should
2977 (zerop
2978 (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2979 (let ((org-adapt-indentation t)
2980 (org-src-preserve-indentation t))
2981 (org-demote))
2982 (forward-line 2)
2983 (org-get-indentation))))
2984 (should
2985 (zerop
2986 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2987 (let ((org-adapt-indentation t)
2988 (org-src-preserve-indentation t))
2989 (org-demote))
2990 (forward-line 2)
2991 (org-get-indentation))))
2992 (should
2993 (zerop
2994 (org-test-with-temp-text
2995 "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
2996 (let ((org-adapt-indentation t)
2997 (org-src-preserve-indentation nil))
2998 (org-demote))
2999 (forward-line 2)
3000 (org-get-indentation)))))
3002 (ert-deftest test-org/promote ()
3003 "Test `org-promote' specifications."
3004 ;; Return an error if headline is to be promoted to level 0, unless
3005 ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
3006 ;; headline becomes a comment.
3007 (should-error
3008 (org-test-with-temp-text "* H"
3009 (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
3010 (should
3011 (equal "# H"
3012 (org-test-with-temp-text "* H"
3013 (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
3014 (buffer-string))))
3015 ;; Remove correct number of stars according to
3016 ;; `org-odd-levels-only'.
3017 (should
3018 (= 2
3019 (org-test-with-temp-text "*** H"
3020 (let ((org-odd-levels-only nil)) (org-promote))
3021 (org-current-level))))
3022 (should
3023 (= 1
3024 (org-test-with-temp-text "*** H"
3025 (let ((org-odd-levels-only t)) (org-promote))
3026 (org-current-level))))
3027 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
3028 (should
3029 (org-test-with-temp-text "** H :tag:"
3030 (let ((org-tags-column 10)
3031 (org-auto-align-tags t)
3032 (org-odd-levels-only nil))
3033 (org-promote))
3034 (org-move-to-column 10)
3035 (org-looking-at-p ":tag:$")))
3036 (should-not
3037 (org-test-with-temp-text "** H :tag:"
3038 (let ((org-tags-column 10)
3039 (org-auto-align-tags nil)
3040 (org-odd-levels-only nil))
3041 (org-promote))
3042 (org-move-to-column 10)
3043 (org-looking-at-p ":tag:$")))
3044 ;; When `org-adapt-indentation' is non-nil, always indent planning
3045 ;; info and property drawers.
3046 (should
3047 (= 2
3048 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
3049 (let ((org-odd-levels-only nil)
3050 (org-adapt-indentation t))
3051 (org-promote))
3052 (forward-line)
3053 (org-get-indentation))))
3054 (should
3055 (= 2
3056 (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
3057 (let ((org-odd-levels-only nil)
3058 (org-adapt-indentation t))
3059 (org-promote))
3060 (forward-line)
3061 (org-get-indentation))))
3062 (should-not
3063 (= 2
3064 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
3065 (let ((org-odd-levels-only nil)
3066 (org-adapt-indentation nil))
3067 (org-promote))
3068 (forward-line)
3069 (org-get-indentation))))
3070 ;; When `org-adapt-indentation' is non-nil, shift all lines in
3071 ;; section accordingly. Ignore, however, footnote definitions and
3072 ;; inlinetasks boundaries.
3073 (should
3074 (= 2
3075 (org-test-with-temp-text "** H\n Paragraph"
3076 (let ((org-odd-levels-only nil)
3077 (org-adapt-indentation t))
3078 (org-promote))
3079 (forward-line)
3080 (org-get-indentation))))
3081 (should-not
3082 (= 2
3083 (org-test-with-temp-text "** H\n Paragraph"
3084 (let ((org-odd-levels-only nil)
3085 (org-adapt-indentation nil))
3086 (org-promote))
3087 (forward-line)
3088 (org-get-indentation))))
3089 (should
3090 (= 2
3091 (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
3092 (let ((org-odd-levels-only nil)
3093 (org-adapt-indentation t))
3094 (org-promote))
3095 (forward-line)
3096 (org-get-indentation))))
3097 (when (featurep 'org-inlinetask)
3098 (should
3099 (zerop
3100 (let ((org-inlinetask-min-level 5)
3101 (org-adapt-indentation t))
3102 (org-test-with-temp-text "** H\n***** I\n***** END"
3103 (org-promote)
3104 (forward-line)
3105 (org-get-indentation))))))
3106 (when (featurep 'org-inlinetask)
3107 (should
3108 (= 2
3109 (let ((org-inlinetask-min-level 5)
3110 (org-adapt-indentation t))
3111 (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
3112 (org-promote)
3113 (forward-line 2)
3114 (org-get-indentation))))))
3115 ;; Give up shifting if it would break document's structure
3116 ;; otherwise.
3117 (should
3118 (= 3
3119 (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
3120 (let ((org-odd-levels-only nil)
3121 (org-adapt-indentation t))
3122 (org-promote))
3123 (forward-line)
3124 (org-get-indentation))))
3125 (should
3126 (= 3
3127 (org-test-with-temp-text "** H\n Paragraph\n * list."
3128 (let ((org-odd-levels-only nil)
3129 (org-adapt-indentation t))
3130 (org-promote))
3131 (forward-line)
3132 (org-get-indentation))))
3133 ;; Ignore contents of source blocks or example blocks when
3134 ;; indentation should be preserved (through
3135 ;; `org-src-preserve-indentation' or "-i" flag).
3136 (should-not
3137 (zerop
3138 (org-test-with-temp-text
3139 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
3140 (let ((org-adapt-indentation t)
3141 (org-src-preserve-indentation nil)
3142 (org-odd-levels-only nil))
3143 (org-promote))
3144 (forward-line)
3145 (org-get-indentation))))
3146 (should
3147 (zerop
3148 (org-test-with-temp-text
3149 "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
3150 (let ((org-adapt-indentation t)
3151 (org-src-preserve-indentation t)
3152 (org-odd-levels-only nil))
3153 (org-promote))
3154 (forward-line)
3155 (org-get-indentation))))
3156 (should
3157 (zerop
3158 (org-test-with-temp-text
3159 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
3160 (let ((org-adapt-indentation t)
3161 (org-src-preserve-indentation t)
3162 (org-odd-levels-only nil))
3163 (org-promote))
3164 (forward-line)
3165 (org-get-indentation))))
3166 (should
3167 (zerop
3168 (org-test-with-temp-text
3169 "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
3170 (let ((org-adapt-indentation t)
3171 (org-src-preserve-indentation nil)
3172 (org-odd-levels-only nil))
3173 (org-promote))
3174 (forward-line)
3175 (org-get-indentation)))))
3178 ;;; Planning
3180 (ert-deftest test-org/at-planning-p ()
3181 "Test `org-at-planning-p' specifications."
3182 ;; Regular test.
3183 (should
3184 (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
3185 (org-at-planning-p)))
3186 (should-not
3187 (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
3188 (org-at-planning-p)))
3189 ;; Correctly find planning attached to inlinetasks.
3190 (when (featurep 'org-inlinetask)
3191 (should
3192 (org-test-with-temp-text
3193 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
3194 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
3195 (should-not
3196 (org-test-with-temp-text
3197 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
3198 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
3199 (should-not
3200 (org-test-with-temp-text
3201 "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
3202 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
3203 (should-not
3204 (org-test-with-temp-text
3205 "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
3206 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
3208 (ert-deftest test-org/add-planning-info ()
3209 "Test `org-add-planning-info'."
3210 ;; Create deadline when `org-adapt-indentation' is non-nil.
3211 (should
3212 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3213 (org-test-with-temp-text "* H\nParagraph<point>"
3214 (let ((org-adapt-indentation t))
3215 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3216 (replace-regexp-in-string
3217 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3218 nil nil 1))))
3219 ;; Create deadline when `org-adapt-indentation' is nil.
3220 (should
3221 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3222 (org-test-with-temp-text "* H\nParagraph<point>"
3223 (let ((org-adapt-indentation nil))
3224 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3225 (replace-regexp-in-string
3226 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3227 nil nil 1))))
3228 ;; Update deadline when `org-adapt-indentation' is non-nil.
3229 (should
3230 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3231 (org-test-with-temp-text "\
3233 DEADLINE: <2015-06-24 Wed>
3234 Paragraph<point>"
3235 (let ((org-adapt-indentation t))
3236 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3237 (replace-regexp-in-string
3238 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3239 nil nil 1))))
3240 ;; Update deadline when `org-adapt-indentation' is nil.
3241 (should
3242 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3243 (org-test-with-temp-text "\
3245 DEADLINE: <2015-06-24 Wed>
3246 Paragraph<point>"
3247 (let ((org-adapt-indentation nil))
3248 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3249 (replace-regexp-in-string
3250 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3251 nil nil 1))))
3252 ;; Schedule when `org-adapt-indentation' is non-nil.
3253 (should
3254 (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
3255 (org-test-with-temp-text "* H\nParagraph<point>"
3256 (let ((org-adapt-indentation t))
3257 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
3258 (replace-regexp-in-string
3259 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3260 nil nil 1))))
3261 ;; Schedule when `org-adapt-indentation' is nil.
3262 (should
3263 (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
3264 (org-test-with-temp-text "* H\nParagraph<point>"
3265 (let ((org-adapt-indentation nil))
3266 (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
3267 (replace-regexp-in-string
3268 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3269 nil nil 1))))
3270 ;; Add deadline when scheduled.
3271 (should
3272 (equal "\
3274 DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
3275 Paragraph"
3276 (org-test-with-temp-text "\
3278 SCHEDULED: <2015-06-24 Wed>
3279 Paragraph<point>"
3280 (let ((org-adapt-indentation t))
3281 (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
3282 (replace-regexp-in-string
3283 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3284 nil nil 1))))
3285 ;; Remove middle entry.
3286 (should
3287 (equal "\
3289 CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
3290 Paragraph"
3291 (org-test-with-temp-text "\
3293 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
3294 Paragraph<point>"
3295 (let ((org-adapt-indentation t))
3296 (org-add-planning-info nil nil 'deadline))
3297 (replace-regexp-in-string
3298 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
3299 nil nil 1))))
3300 ;; Remove last entry and then middle entry (order should not
3301 ;; matter).
3302 (should
3303 (equal "\
3305 CLOSED: [2015-06-24]
3306 Paragraph"
3307 (org-test-with-temp-text "\
3309 CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
3310 Paragraph<point>"
3311 (let ((org-adapt-indentation t))
3312 (org-add-planning-info nil nil 'scheduled 'deadline))
3313 (replace-regexp-in-string
3314 "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
3315 nil nil 1))))
3316 ;; Remove closed when `org-adapt-indentation' is non-nil.
3317 (should
3318 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3319 (org-test-with-temp-text "\
3321 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
3322 Paragraph<point>"
3323 (let ((org-adapt-indentation t))
3324 (org-add-planning-info nil nil 'closed))
3325 (replace-regexp-in-string
3326 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3327 nil nil 1))))
3328 (should
3329 (equal "* H\n Paragraph"
3330 (org-test-with-temp-text "\
3332 CLOSED: [2015-06-25 Thu]
3333 Paragraph<point>"
3334 (let ((org-adapt-indentation t))
3335 (org-add-planning-info nil nil 'closed))
3336 (replace-regexp-in-string
3337 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3338 nil nil 1))))
3339 ;; Remove closed when `org-adapt-indentation' is nil.
3340 (should
3341 (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
3342 (org-test-with-temp-text "\
3344 CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
3345 Paragraph<point>"
3346 (let ((org-adapt-indentation nil))
3347 (org-add-planning-info nil nil 'closed))
3348 (replace-regexp-in-string
3349 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3350 nil nil 1))))
3351 (should
3352 (equal "* H\nParagraph"
3353 (org-test-with-temp-text "\
3355 CLOSED: [2015-06-25 Thu]
3356 Paragraph<point>"
3357 (let ((org-adapt-indentation nil))
3358 (org-add-planning-info nil nil 'closed))
3359 (replace-regexp-in-string
3360 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3361 nil nil 1))))
3362 ;; Remove closed entry and delete empty line.
3363 (should
3364 (equal "\
3366 Paragraph"
3367 (org-test-with-temp-text "\
3369 CLOSED: [2015-06-24 Wed]
3370 Paragraph<point>"
3371 (let ((org-adapt-indentation t))
3372 (org-add-planning-info nil nil 'closed))
3373 (replace-regexp-in-string
3374 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3375 nil nil 1))))
3376 ;; Remove one entry and update another.
3377 (should
3378 (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
3379 (org-test-with-temp-text "\
3381 SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
3382 Paragraph<point>"
3383 (let ((org-adapt-indentation t))
3384 (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
3385 (replace-regexp-in-string
3386 "\\( [.A-Za-z]+\\)>" "" (buffer-string)
3387 nil nil 1)))))
3390 ;;; Property API
3392 (ert-deftest test-org/buffer-property-keys ()
3393 "Test `org-buffer-property-keys' specifications."
3394 ;; Retrieve properties accross siblings.
3395 (should
3396 (equal '("A" "B")
3397 (org-test-with-temp-text "
3398 * H1
3399 :PROPERTIES:
3400 :A: 1
3401 :END:
3402 * H2
3403 :PROPERTIES:
3404 :B: 1
3405 :END:"
3406 (org-buffer-property-keys))))
3407 ;; Retrieve properties accross children.
3408 (should
3409 (equal '("A" "B")
3410 (org-test-with-temp-text "
3411 * H1
3412 :PROPERTIES:
3413 :A: 1
3414 :END:
3415 ** H2
3416 :PROPERTIES:
3417 :B: 1
3418 :END:"
3419 (org-buffer-property-keys))))
3420 ;; Retrieve muliple properties in the same drawer.
3421 (should
3422 (equal '("A" "B")
3423 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3424 (org-buffer-property-keys))))
3425 ;; Ignore extension symbol in property name.
3426 (should
3427 (equal '("A")
3428 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
3429 (org-buffer-property-keys))))
3430 ;; With non-nil COLUMNS, extract property names from columns.
3431 (should
3432 (equal '("A" "B")
3433 (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
3434 (org-buffer-property-keys nil nil t))))
3435 (should
3436 (equal '("A" "B" "COLUMNS")
3437 (org-test-with-temp-text
3438 "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
3439 (org-buffer-property-keys nil nil t)))))
3441 (ert-deftest test-org/property-values ()
3442 "Test `org-property-values' specifications."
3443 ;; Regular test.
3444 (should
3445 (equal '("2" "1")
3446 (org-test-with-temp-text
3447 "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
3448 (org-property-values "A"))))
3449 ;; Ignore empty values.
3450 (should-not
3451 (org-test-with-temp-text
3452 "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
3453 (org-property-values "A")))
3454 ;; Take into consideration extended values.
3455 (should
3456 (equal '("1 2")
3457 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
3458 (org-property-values "A")))))
3460 (ert-deftest test-org/find-property ()
3461 "Test `org-find-property' specifications."
3462 ;; Regular test.
3463 (should
3464 (= 1
3465 (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
3466 (org-find-property "prop"))))
3467 ;; Ignore false positives.
3468 (should
3469 (= 27
3470 (org-test-with-temp-text
3471 "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
3472 (org-find-property "A"))))
3473 ;; Return first entry found in buffer.
3474 (should
3475 (= 1
3476 (org-test-with-temp-text
3477 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
3478 (org-find-property "A"))))
3479 ;; Only search visible part of the buffer.
3480 (should
3481 (= 31
3482 (org-test-with-temp-text
3483 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
3484 (org-narrow-to-subtree)
3485 (org-find-property "A"))))
3486 ;; With optional argument, only find entries with a specific value.
3487 (should-not
3488 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3489 (org-find-property "A" "2")))
3490 (should
3491 (= 31
3492 (org-test-with-temp-text
3493 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
3494 (org-find-property "A" "2"))))
3495 ;; Use "nil" for explicit nil values.
3496 (should
3497 (= 31
3498 (org-test-with-temp-text
3499 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
3500 (org-find-property "A" "nil")))))
3502 (ert-deftest test-org/entry-delete ()
3503 "Test `org-entry-delete' specifications."
3504 ;; Regular test.
3505 (should
3506 (string-match
3507 " *:PROPERTIES:\n *:B: +2\n *:END:"
3508 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3509 (org-entry-delete (point) "A")
3510 (buffer-string))))
3511 ;; Also remove accumulated properties.
3512 (should-not
3513 (string-match
3514 ":A"
3515 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
3516 (org-entry-delete (point) "A")
3517 (buffer-string))))
3518 ;; When last property is removed, remove the property drawer.
3519 (should-not
3520 (string-match
3521 ":PROPERTIES:"
3522 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
3523 (org-entry-delete (point) "A")
3524 (buffer-string))))
3525 ;; Return a non-nil value when some property was removed.
3526 (should
3527 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3528 (org-entry-delete (point) "A")))
3529 (should-not
3530 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
3531 (org-entry-delete (point) "C"))))
3533 (ert-deftest test-org/entry-get ()
3534 "Test `org-entry-get' specifications."
3535 ;; Regular test.
3536 (should
3537 (equal "1"
3538 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3539 (org-entry-get (point) "A"))))
3540 ;; Ignore case.
3541 (should
3542 (equal "1"
3543 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3544 (org-entry-get (point) "a"))))
3545 ;; Handle extended values, both before and after base value.
3546 (should
3547 (equal "1 2 3"
3548 (org-test-with-temp-text
3549 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
3550 (org-entry-get (point) "A"))))
3551 ;; Empty values are returned as the empty string.
3552 (should
3553 (equal ""
3554 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
3555 (org-entry-get (point) "A"))))
3556 ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
3557 ;; otherwise, return nil.
3558 (should-not
3559 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
3560 (org-entry-get (point) "A")))
3561 (should
3562 (equal "nil"
3563 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
3564 (org-entry-get (point) "A" nil t))))
3565 ;; Return nil when no property is found, independently on the
3566 ;; LITERAL-NIL argument.
3567 (should-not
3568 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3569 (org-entry-get (point) "B")))
3570 (should-not
3571 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3572 (org-entry-get (point) "B" nil t)))
3573 ;; Handle inheritance, when allowed. Include extended values and
3574 ;; possibly global values.
3575 (should
3576 (equal
3578 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3579 (org-entry-get (point) "A" t))))
3580 (should
3581 (equal
3583 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3584 (let ((org-use-property-inheritance t))
3585 (org-entry-get (point) "A" 'selective)))))
3586 (should-not
3587 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
3588 (let ((org-use-property-inheritance nil))
3589 (org-entry-get (point) "A" 'selective))))
3590 (should
3591 (equal
3592 "1 2"
3593 (org-test-with-temp-text
3594 "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
3595 (org-entry-get (point-max) "A" t))))
3596 (should
3597 (equal "1"
3598 (org-test-with-temp-text
3599 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
3600 (org-mode-restart)
3601 (org-entry-get (point-max) "A" t))))
3602 (should
3603 (equal "0 1"
3604 (org-test-with-temp-text
3605 "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
3606 (org-mode-restart)
3607 (org-entry-get (point-max) "A" t)))))
3609 (ert-deftest test-org/entry-properties ()
3610 "Test `org-entry-properties' specifications."
3611 ;; Get "ITEM" property.
3612 (should
3613 (equal "* H"
3614 (org-test-with-temp-text "* TODO H"
3615 (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
3616 (should
3617 (equal "* H"
3618 (org-test-with-temp-text "* TODO H"
3619 (cdr (assoc "ITEM" (org-entry-properties))))))
3620 ;; Get "TODO" property. TODO keywords are case sensitive.
3621 (should
3622 (equal "TODO"
3623 (org-test-with-temp-text "* TODO H"
3624 (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
3625 (should
3626 (equal "TODO"
3627 (org-test-with-temp-text "* TODO H"
3628 (cdr (assoc "TODO" (org-entry-properties))))))
3629 (should-not
3630 (org-test-with-temp-text "* H"
3631 (assoc "TODO" (org-entry-properties nil "TODO"))))
3632 (should-not
3633 (org-test-with-temp-text "* todo H"
3634 (assoc "TODO" (org-entry-properties nil "TODO"))))
3635 ;; Get "PRIORITY" property.
3636 (should
3637 (equal "A"
3638 (org-test-with-temp-text "* [#A] H"
3639 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
3640 (should
3641 (equal "A"
3642 (org-test-with-temp-text "* [#A] H"
3643 (cdr (assoc "PRIORITY" (org-entry-properties))))))
3644 (should
3645 (equal (char-to-string org-default-priority)
3646 (org-test-with-temp-text "* H"
3647 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
3648 ;; Get "FILE" property.
3649 (should
3650 (org-test-with-temp-text-in-file "* H\nParagraph"
3651 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
3652 (buffer-file-name))))
3653 (should
3654 (org-test-with-temp-text-in-file "* H\nParagraph"
3655 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
3656 (buffer-file-name))))
3657 (should-not
3658 (org-test-with-temp-text "* H\nParagraph"
3659 (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
3660 ;; Get "TAGS" property.
3661 (should
3662 (equal ":tag1:tag2:"
3663 (org-test-with-temp-text "* H :tag1:tag2:"
3664 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
3665 (should
3666 (equal ":tag1:tag2:"
3667 (org-test-with-temp-text "* H :tag1:tag2:"
3668 (cdr (assoc "TAGS" (org-entry-properties))))))
3669 (should-not
3670 (org-test-with-temp-text "* H"
3671 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
3672 ;; Get "ALLTAGS" property.
3673 (should
3674 (equal ":tag1:tag2:"
3675 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
3676 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
3677 (should
3678 (equal ":tag1:tag2:"
3679 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
3680 (cdr (assoc "ALLTAGS" (org-entry-properties))))))
3681 (should-not
3682 (org-test-with-temp-text "* H"
3683 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
3684 ;; Get "BLOCKED" property.
3685 (should
3686 (equal "t"
3687 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
3688 (let ((org-enforce-todo-dependencies t)
3689 (org-blocker-hook
3690 '(org-block-todo-from-children-or-siblings-or-parent)))
3691 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
3692 (should
3693 (equal ""
3694 (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
3695 (let ((org-enforce-todo-dependencies t)
3696 (org-blocker-hook
3697 '(org-block-todo-from-children-or-siblings-or-parent)))
3698 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
3699 ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
3700 (should
3701 (equal
3702 "[2012-03-29 thu.]"
3703 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
3704 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
3705 (should
3706 (equal
3707 "[2012-03-29 thu.]"
3708 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
3709 (cdr (assoc "CLOSED" (org-entry-properties))))))
3710 (should-not
3711 (org-test-with-temp-text "* H"
3712 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
3713 (should
3714 (equal
3715 "<2014-03-04 tue.>"
3716 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3717 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
3718 (should
3719 (equal
3720 "<2014-03-04 tue.>"
3721 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3722 (cdr (assoc "DEADLINE" (org-entry-properties))))))
3723 (should-not
3724 (org-test-with-temp-text "* H"
3725 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
3726 (should
3727 (equal
3728 "<2014-03-04 tue.>"
3729 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3730 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
3731 (should
3732 (equal
3733 "<2014-03-04 tue.>"
3734 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3735 (cdr (assoc "SCHEDULED" (org-entry-properties))))))
3736 (should-not
3737 (org-test-with-temp-text "* H"
3738 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
3739 ;; Get "CATEGORY"
3740 (should
3741 (equal "cat"
3742 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
3743 (cdr (assoc "CATEGORY" (org-entry-properties))))))
3744 (should
3745 (equal "cat"
3746 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
3747 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3748 (should
3749 (equal "cat"
3750 (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
3751 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3752 (should
3753 (equal "cat2"
3754 (org-test-with-temp-text
3755 (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
3756 "\n"
3757 "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
3758 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3759 ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
3760 (should
3761 (equal "<2012-03-29 thu.>"
3762 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
3763 (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
3764 (should
3765 (equal "[2012-03-29 thu.]"
3766 (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
3767 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
3768 (should
3769 (equal "<2012-03-29 thu.>"
3770 (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
3771 (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
3772 (should
3773 (equal "[2014-03-04 tue.]"
3774 (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
3775 (cdr (assoc "TIMESTAMP_IA" (org-entry-properties nil "TIMESTAMP_IA"))))))
3776 ;; Get standard properties.
3777 (should
3778 (equal "1"
3779 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3780 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3781 ;; Handle extended properties.
3782 (should
3783 (equal "1 2 3"
3784 (org-test-with-temp-text
3785 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
3786 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3787 (should
3788 (equal "1 2 3"
3789 (org-test-with-temp-text
3790 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
3791 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3792 ;; Ignore forbidden (special) properties.
3793 (should-not
3794 (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
3795 (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
3797 (ert-deftest test-org/entry-put ()
3798 "Test `org-entry-put' specifications."
3799 ;; Error when not a string or nil.
3800 (should-error
3801 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
3802 (org-entry-put 1 "test" 2)))
3803 ;; Error when property name is invalid.
3804 (should-error
3805 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
3806 (org-entry-put 1 "no space" "value")))
3807 (should-error
3808 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
3809 (org-entry-put 1 "" "value")))
3810 ;; Set "TODO" property.
3811 (should
3812 (string-match (regexp-quote " TODO H")
3813 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
3814 (org-entry-put (point) "TODO" "TODO")
3815 (buffer-string))))
3816 (should
3817 (string-match (regexp-quote "* H")
3818 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
3819 (org-entry-put (point) "TODO" nil)
3820 (buffer-string))))
3821 ;; Set "PRIORITY" property.
3822 (should
3823 (equal "* [#A] H"
3824 (org-test-with-temp-text "* [#B] H"
3825 (org-entry-put (point) "PRIORITY" "A")
3826 (buffer-string))))
3827 (should
3828 (equal "* H"
3829 (org-test-with-temp-text "* [#B] H"
3830 (org-entry-put (point) "PRIORITY" nil)
3831 (buffer-string))))
3832 ;; Set "SCHEDULED" property.
3833 (should
3834 (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
3835 (org-test-with-temp-text "* H"
3836 (org-entry-put (point) "SCHEDULED" "2014-03-04")
3837 (buffer-string))))
3838 (should
3839 (string= "* H\n"
3840 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3841 (org-entry-put (point) "SCHEDULED" nil)
3842 (buffer-string))))
3843 (should
3844 (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
3845 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3846 (org-entry-put (point) "SCHEDULED" "earlier")
3847 (buffer-string))))
3848 (should
3849 (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
3850 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3851 (org-entry-put (point) "SCHEDULED" "later")
3852 (buffer-string))))
3853 ;; Set "DEADLINE" property.
3854 (should
3855 (string-match "^ *DEADLINE: <2014-03-04 .*?>"
3856 (org-test-with-temp-text "* H"
3857 (org-entry-put (point) "DEADLINE" "2014-03-04")
3858 (buffer-string))))
3859 (should
3860 (string= "* H\n"
3861 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3862 (org-entry-put (point) "DEADLINE" nil)
3863 (buffer-string))))
3864 (should
3865 (string-match "^ *DEADLINE: <2014-03-03 .*?>"
3866 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3867 (org-entry-put (point) "DEADLINE" "earlier")
3868 (buffer-string))))
3869 (should
3870 (string-match "^ *DEADLINE: <2014-03-05 .*?>"
3871 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3872 (org-entry-put (point) "DEADLINE" "later")
3873 (buffer-string))))
3874 ;; Set "CATEGORY" property
3875 (should
3876 (string-match "^ *:CATEGORY: cat"
3877 (org-test-with-temp-text "* H"
3878 (org-entry-put (point) "CATEGORY" "cat")
3879 (buffer-string))))
3880 ;; Regular properties, with or without pre-existing drawer.
3881 (should
3882 (string-match "^ *:A: +2$"
3883 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3884 (org-entry-put (point) "A" "2")
3885 (buffer-string))))
3886 (should
3887 (string-match "^ *:A: +1$"
3888 (org-test-with-temp-text "* H"
3889 (org-entry-put (point) "A" "1")
3890 (buffer-string))))
3891 ;; Special case: two consecutive headlines.
3892 (should
3893 (string-match "\\* A\n *:PROPERTIES:"
3894 (org-test-with-temp-text "* A\n** B"
3895 (org-entry-put (point) "A" "1")
3896 (buffer-string)))))
3899 ;;; Radio Targets
3901 (ert-deftest test-org/update-radio-target-regexp ()
3902 "Test `org-update-radio-target-regexp' specifications."
3903 ;; Properly update cache with no previous radio target regexp.
3904 (should
3905 (eq 'link
3906 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3907 (save-excursion (goto-char (point-max)) (org-element-context))
3908 (insert "<<<")
3909 (search-forward "o")
3910 (insert ">>>")
3911 (org-update-radio-target-regexp)
3912 (goto-char (point-max))
3913 (org-element-type (org-element-context)))))
3914 ;; Properly update cache with previous radio target regexp.
3915 (should
3916 (eq 'link
3917 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3918 (save-excursion (goto-char (point-max)) (org-element-context))
3919 (insert "<<<")
3920 (search-forward "o")
3921 (insert ">>>")
3922 (org-update-radio-target-regexp)
3923 (search-backward "r")
3924 (delete-char 5)
3925 (insert "new")
3926 (org-update-radio-target-regexp)
3927 (goto-char (point-max))
3928 (delete-region (line-beginning-position) (point))
3929 (insert "new")
3930 (org-element-type (org-element-context))))))
3933 ;;; Sparse trees
3935 (ert-deftest test-org/match-sparse-tree ()
3936 "Test `org-match-sparse-tree' specifications."
3937 ;; Match tags.
3938 (should-not
3939 (org-test-with-temp-text "* H\n** H1 :tag:"
3940 (org-match-sparse-tree nil "tag")
3941 (search-forward "H1")
3942 (org-invisible-p2)))
3943 (should
3944 (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
3945 (org-match-sparse-tree nil "tag")
3946 (search-forward "H2")
3947 (org-invisible-p2)))
3948 ;; "-" operator for tags.
3949 (should-not
3950 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3951 (org-match-sparse-tree nil "tag1-tag2")
3952 (search-forward "H1")
3953 (org-invisible-p2)))
3954 (should
3955 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3956 (org-match-sparse-tree nil "tag1-tag2")
3957 (search-forward "H2")
3958 (org-invisible-p2)))
3959 ;; "&" operator for tags.
3960 (should
3961 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3962 (org-match-sparse-tree nil "tag1&tag2")
3963 (search-forward "H1")
3964 (org-invisible-p2)))
3965 (should-not
3966 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3967 (org-match-sparse-tree nil "tag1&tag2")
3968 (search-forward "H2")
3969 (org-invisible-p2)))
3970 ;; "|" operator for tags.
3971 (should-not
3972 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3973 (org-match-sparse-tree nil "tag1|tag2")
3974 (search-forward "H1")
3975 (org-invisible-p2)))
3976 (should-not
3977 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3978 (org-match-sparse-tree nil "tag1|tag2")
3979 (search-forward "H2")
3980 (org-invisible-p2)))
3981 ;; Regexp match on tags.
3982 (should-not
3983 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3984 (org-match-sparse-tree nil "{^tag.*}")
3985 (search-forward "H1")
3986 (org-invisible-p2)))
3987 (should
3988 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3989 (org-match-sparse-tree nil "{^tag.*}")
3990 (search-forward "H2")
3991 (org-invisible-p2)))
3992 ;; Match group tags.
3993 (should-not
3994 (org-test-with-temp-text
3995 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3996 (org-match-sparse-tree nil "work")
3997 (search-forward "H1")
3998 (org-invisible-p2)))
3999 (should-not
4000 (org-test-with-temp-text
4001 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
4002 (org-match-sparse-tree nil "work")
4003 (search-forward "H2")
4004 (org-invisible-p2)))
4005 ;; Match group tags with hard brackets.
4006 (should-not
4007 (org-test-with-temp-text
4008 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
4009 (org-match-sparse-tree nil "work")
4010 (search-forward "H1")
4011 (org-invisible-p2)))
4012 (should-not
4013 (org-test-with-temp-text
4014 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
4015 (org-match-sparse-tree nil "work")
4016 (search-forward "H2")
4017 (org-invisible-p2)))
4018 ;; Match tags in hierarchies
4019 (should-not
4020 (org-test-with-temp-text
4021 "#+TAGS: [ Lev_1 : Lev_2 ]\n
4022 #+TAGS: [ Lev_2 : Lev_3 ]\n
4023 #+TAGS: { Lev_3 : Lev_4 }\n
4024 * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
4025 (org-match-sparse-tree nil "Lev_1")
4026 (search-forward "H4")
4027 (org-invisible-p2)))
4028 ;; Match regular expressions in tags
4029 (should-not
4030 (org-test-with-temp-text
4031 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
4032 (org-match-sparse-tree nil "Lev")
4033 (search-forward "H1")
4034 (org-invisible-p2)))
4035 (should
4036 (org-test-with-temp-text
4037 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
4038 (org-match-sparse-tree nil "Lev")
4039 (search-forward "H1")
4040 (org-invisible-p2)))
4041 ;; Match properties.
4042 (should
4043 (org-test-with-temp-text
4044 "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
4045 (org-match-sparse-tree nil "A=\"1\"")
4046 (search-forward "H2")
4047 (org-invisible-p2)))
4048 (should-not
4049 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
4050 (org-match-sparse-tree nil "A=\"1\"")
4051 (search-forward "H2")
4052 (org-invisible-p2)))
4053 ;; Case is not significant when matching properties.
4054 (should-not
4055 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
4056 (org-match-sparse-tree nil "a=\"1\"")
4057 (search-forward "H2")
4058 (org-invisible-p2)))
4059 (should-not
4060 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
4061 (org-match-sparse-tree nil "A=\"1\"")
4062 (search-forward "H2")
4063 (org-invisible-p2)))
4064 ;; Match special LEVEL property.
4065 (should-not
4066 (org-test-with-temp-text "* H\n** H1\n*** H2"
4067 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
4068 (search-forward "H1")
4069 (org-invisible-p2)))
4070 (should
4071 (org-test-with-temp-text "* H\n** H1\n*** H2"
4072 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
4073 (search-forward "H2")
4074 (org-invisible-p2)))
4075 ;; Comparison operators when matching properties.
4076 (should
4077 (org-test-with-temp-text
4078 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
4079 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
4080 (search-forward "H1")
4081 (org-invisible-p2)))
4082 (should-not
4083 (org-test-with-temp-text
4084 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
4085 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
4086 (search-forward "H2")
4087 (org-invisible-p2)))
4088 ;; Regexp match on properties values.
4089 (should-not
4090 (org-test-with-temp-text
4091 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
4092 (org-match-sparse-tree nil "A={f.*}")
4093 (search-forward "H1")
4094 (org-invisible-p2)))
4095 (should
4096 (org-test-with-temp-text
4097 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
4098 (org-match-sparse-tree nil "A={f.*}")
4099 (search-forward "H2")
4100 (org-invisible-p2)))
4101 ;; With an optional argument, limit match to TODO entries.
4102 (should-not
4103 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
4104 (org-match-sparse-tree t "tag")
4105 (search-forward "H1")
4106 (org-invisible-p2)))
4107 (should
4108 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
4109 (org-match-sparse-tree t "tag")
4110 (search-forward "H2")
4111 (org-invisible-p2))))
4113 (ert-deftest test-org/occur ()
4114 "Test `org-occur' specifications."
4115 ;; Count number of matches.
4116 (should
4117 (= 1
4118 (org-test-with-temp-text "* H\nA\n* H2"
4119 (org-occur "A"))))
4120 (should
4121 (= 2
4122 (org-test-with-temp-text "* H\nA\n* H2\nA"
4123 (org-occur "A"))))
4124 ;; Test CALLBACK optional argument.
4125 (should
4126 (= 0
4127 (org-test-with-temp-text "* H\nA\n* H2"
4128 (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
4129 (should
4130 (= 1
4131 (org-test-with-temp-text "* H\nA\n* H2\nA"
4132 (org-occur "A" nil (lambda () (equal (org-get-heading) "H2")))))))
4135 ;;; Tags
4137 (ert-deftest test-org/tag-align ()
4138 "Test `org-align-tags-here' with different display width."
4139 (should
4140 ;; 12345678901234567890
4141 (equal "* Test :abc:"
4142 (org-test-with-temp-text "* Test :abc:"
4143 (let ((org-tags-column -20)
4144 (indent-tabs-mode nil))
4145 (org-fix-tags-on-the-fly))
4146 (buffer-string))))
4147 (should
4148 ;; 12345678901234567890
4149 (equal "* Test :日本語:"
4150 (org-test-with-temp-text "* Test :日本語:"
4151 (let ((org-tags-column -20)
4152 (indent-tabs-mode nil))
4153 (org-fix-tags-on-the-fly))
4154 (buffer-string)))))
4157 ;;; Timestamps API
4159 (ert-deftest test-org/time-stamp ()
4160 "Test `org-time-stamp' specifications."
4161 ;; Insert chosen time stamp at point.
4162 (should
4163 (string-match
4164 "Te<2014-03-04 .*?>xt"
4165 (org-test-with-temp-text "Te<point>xt"
4166 (flet ((org-read-date
4167 (&rest args)
4168 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
4169 (org-time-stamp nil)
4170 (buffer-string)))))
4171 ;; With a prefix argument, also insert time.
4172 (should
4173 (string-match
4174 "Te<2014-03-04 .*? 00:41>xt"
4175 (org-test-with-temp-text "Te<point>xt"
4176 (flet ((org-read-date
4177 (&rest args)
4178 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
4179 (org-time-stamp '(4))
4180 (buffer-string)))))
4181 ;; With two universal prefix arguments, insert an active timestamp
4182 ;; with the current time without prompting the user.
4183 (should
4184 (string-match
4185 "Te<2014-03-04 .*? 00:41>xt"
4186 (org-test-with-temp-text "Te<point>xt"
4187 (flet ((current-time
4189 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
4190 (org-time-stamp '(16))
4191 (buffer-string)))))
4192 ;; When optional argument is non-nil, insert an inactive timestamp.
4193 (should
4194 (string-match
4195 "Te\\[2014-03-04 .*?\\]xt"
4196 (org-test-with-temp-text "Te<point>xt"
4197 (flet ((org-read-date
4198 (&rest args)
4199 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
4200 (org-time-stamp nil t)
4201 (buffer-string)))))
4202 ;; When called from a timestamp, replace existing one.
4203 (should
4204 (string-match
4205 "<2014-03-04 .*?>"
4206 (org-test-with-temp-text "<2012-03-29<point> thu.>"
4207 (flet ((org-read-date
4208 (&rest args)
4209 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
4210 (org-time-stamp nil)
4211 (buffer-string)))))
4212 (should
4213 (string-match
4214 "<2014-03-04 .*?>--<2014-03-04 .*?>"
4215 (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
4216 (flet ((org-read-date
4217 (&rest args)
4218 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
4219 (org-time-stamp nil)
4220 (buffer-string)))))
4221 ;; When replacing a timestamp, preserve repeater, if any.
4222 (should
4223 (string-match
4224 "<2014-03-04 .*? \\+2y>"
4225 (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
4226 (flet ((org-read-date
4227 (&rest args)
4228 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
4229 (org-time-stamp nil)
4230 (buffer-string)))))
4231 ;; When called twice in a raw, build a date range.
4232 (should
4233 (string-match
4234 "<2012-03-29 .*?>--<2014-03-04 .*?>"
4235 (org-test-with-temp-text "<2012-03-29 thu.><point>"
4236 (flet ((org-read-date
4237 (&rest args)
4238 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
4239 (let ((last-command 'org-time-stamp)
4240 (this-command 'org-time-stamp))
4241 (org-time-stamp nil))
4242 (buffer-string))))))
4244 (ert-deftest test-org/timestamp-has-time-p ()
4245 "Test `org-timestamp-has-time-p' specifications."
4246 ;; With time.
4247 (should
4248 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
4249 (org-timestamp-has-time-p (org-element-context))))
4250 ;; Without time.
4251 (should-not
4252 (org-test-with-temp-text "<2012-03-29 Thu>"
4253 (org-timestamp-has-time-p (org-element-context)))))
4255 (ert-deftest test-org/timestamp-format ()
4256 "Test `org-timestamp-format' specifications."
4257 ;; Regular test.
4258 (should
4259 (equal
4260 "2012-03-29 16:40"
4261 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
4262 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
4263 ;; Range end.
4264 (should
4265 (equal
4266 "2012-03-29"
4267 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
4268 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
4270 (ert-deftest test-org/timestamp-split-range ()
4271 "Test `org-timestamp-split-range' specifications."
4272 ;; Extract range start (active).
4273 (should
4274 (equal '(2012 3 29)
4275 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4276 (let ((ts (org-timestamp-split-range (org-element-context))))
4277 (mapcar (lambda (p) (org-element-property p ts))
4278 '(:year-end :month-end :day-end))))))
4279 ;; Extract range start (inactive)
4280 (should
4281 (equal '(2012 3 29)
4282 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
4283 (let ((ts (org-timestamp-split-range (org-element-context))))
4284 (mapcar (lambda (p) (org-element-property p ts))
4285 '(:year-end :month-end :day-end))))))
4286 ;; Extract range end (active).
4287 (should
4288 (equal '(2012 3 30)
4289 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4290 (let ((ts (org-timestamp-split-range
4291 (org-element-context) t)))
4292 (mapcar (lambda (p) (org-element-property p ts))
4293 '(:year-end :month-end :day-end))))))
4294 ;; Extract range end (inactive)
4295 (should
4296 (equal '(2012 3 30)
4297 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
4298 (let ((ts (org-timestamp-split-range
4299 (org-element-context) t)))
4300 (mapcar (lambda (p) (org-element-property p ts))
4301 '(:year-end :month-end :day-end))))))
4302 ;; Return the timestamp if not a range.
4303 (should
4304 (org-test-with-temp-text "[2012-03-29 Thu]"
4305 (let* ((ts-orig (org-element-context))
4306 (ts-copy (org-timestamp-split-range ts-orig)))
4307 (eq ts-orig ts-copy))))
4308 (should
4309 (org-test-with-temp-text "<%%(org-float t 4 2)>"
4310 (let* ((ts-orig (org-element-context))
4311 (ts-copy (org-timestamp-split-range ts-orig)))
4312 (eq ts-orig ts-copy)))))
4314 (ert-deftest test-org/timestamp-translate ()
4315 "Test `org-timestamp-translate' specifications."
4316 ;; Translate whole date range.
4317 (should
4318 (equal "<29>--<30>"
4319 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4320 (let ((org-display-custom-times t)
4321 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4322 (org-timestamp-translate (org-element-context))))))
4323 ;; Translate date range start.
4324 (should
4325 (equal "<29>"
4326 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4327 (let ((org-display-custom-times t)
4328 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4329 (org-timestamp-translate (org-element-context) 'start)))))
4330 ;; Translate date range end.
4331 (should
4332 (equal "<30>"
4333 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
4334 (let ((org-display-custom-times t)
4335 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4336 (org-timestamp-translate (org-element-context) 'end)))))
4337 ;; Translate time range.
4338 (should
4339 (equal "<08>--<16>"
4340 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
4341 (let ((org-display-custom-times t)
4342 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
4343 (org-timestamp-translate (org-element-context))))))
4344 ;; Translate non-range timestamp.
4345 (should
4346 (equal "<29>"
4347 (org-test-with-temp-text "<2012-03-29 Thu>"
4348 (let ((org-display-custom-times t)
4349 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4350 (org-timestamp-translate (org-element-context))))))
4351 ;; Do not change `diary' timestamps.
4352 (should
4353 (equal "<%%(org-float t 4 2)>"
4354 (org-test-with-temp-text "<%%(org-float t 4 2)>"
4355 (let ((org-display-custom-times t)
4356 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
4357 (org-timestamp-translate (org-element-context)))))))
4361 ;;; Visibility
4363 (ert-deftest test-org/flag-drawer ()
4364 "Test `org-flag-drawer' specifications."
4365 ;; Hide drawer.
4366 (should
4367 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4368 (org-flag-drawer t)
4369 (get-char-property (line-end-position) 'invisible)))
4370 ;; Show drawer.
4371 (should-not
4372 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4373 (org-flag-drawer t)
4374 (org-flag-drawer nil)
4375 (get-char-property (line-end-position) 'invisible)))
4376 ;; Test optional argument.
4377 (should
4378 (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
4379 (let ((drawer (save-excursion (search-forward ":D2")
4380 (org-element-at-point))))
4381 (org-flag-drawer t drawer)
4382 (get-char-property (progn (search-forward ":D2") (line-end-position))
4383 'invisible))))
4384 (should-not
4385 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
4386 (let ((drawer (save-excursion (search-forward ":D2")
4387 (org-element-at-point))))
4388 (org-flag-drawer t drawer)
4389 (get-char-property (line-end-position) 'invisible))))
4390 ;; Do not hide fake drawers.
4391 (should-not
4392 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
4393 (forward-line 1)
4394 (org-flag-drawer t)
4395 (get-char-property (line-end-position) 'invisible)))
4396 ;; Do not hide incomplete drawers.
4397 (should-not
4398 (org-test-with-temp-text ":D:\nparagraph"
4399 (forward-line 1)
4400 (org-flag-drawer t)
4401 (get-char-property (line-end-position) 'invisible)))
4402 ;; Do not hide drawers when called from final blank lines.
4403 (should-not
4404 (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
4405 (goto-char (point-max))
4406 (org-flag-drawer t)
4407 (goto-char (point-min))
4408 (get-char-property (line-end-position) 'invisible)))
4409 ;; Don't leave point in an invisible part of the buffer when hiding
4410 ;; a drawer away.
4411 (should-not
4412 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
4413 (goto-char (point-max))
4414 (org-flag-drawer t)
4415 (get-char-property (point) 'invisible))))
4417 (ert-deftest test-org/hide-block-toggle ()
4418 "Test `org-hide-block-toggle' specifications."
4419 ;; Error when not at a block.
4420 (should-error
4421 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
4422 (org-hide-block-toggle 'off)
4423 (get-char-property (line-end-position) 'invisible)))
4424 ;; Hide block.
4425 (should
4426 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
4427 (org-hide-block-toggle)
4428 (get-char-property (line-end-position) 'invisible)))
4429 (should
4430 (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
4431 (org-hide-block-toggle)
4432 (get-char-property (line-end-position) 'invisible)))
4433 ;; Show block unconditionally when optional argument is `off'.
4434 (should-not
4435 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4436 (org-hide-block-toggle)
4437 (org-hide-block-toggle 'off)
4438 (get-char-property (line-end-position) 'invisible)))
4439 (should-not
4440 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4441 (org-hide-block-toggle 'off)
4442 (get-char-property (line-end-position) 'invisible)))
4443 ;; Hide block unconditionally when optional argument is non-nil.
4444 (should
4445 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4446 (org-hide-block-toggle t)
4447 (get-char-property (line-end-position) 'invisible)))
4448 (should
4449 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
4450 (org-hide-block-toggle)
4451 (org-hide-block-toggle t)
4452 (get-char-property (line-end-position) 'invisible)))
4453 ;; Do not hide block when called from final blank lines.
4454 (should-not
4455 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
4456 (org-hide-block-toggle)
4457 (goto-char (point-min))
4458 (get-char-property (line-end-position) 'invisible)))
4459 ;; Don't leave point in an invisible part of the buffer when hiding
4460 ;; a block away.
4461 (should-not
4462 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
4463 (org-hide-block-toggle)
4464 (get-char-property (point) 'invisible))))
4466 (ert-deftest test-org/hide-block-toggle-maybe ()
4467 "Test `org-hide-block-toggle-maybe' specifications."
4468 (should
4469 (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
4470 (org-hide-block-toggle-maybe)))
4471 (should-not
4472 (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
4474 (ert-deftest test-org/show-set-visibility ()
4475 "Test `org-show-set-visibility' specifications."
4476 ;; Do not throw an error before first heading.
4477 (should
4478 (org-test-with-temp-text "Preamble\n* Headline"
4479 (org-show-set-visibility 'tree)
4481 ;; Test all visibility spans, both on headline and in entry.
4482 (let ((list-visible-lines
4483 (lambda (state headerp)
4484 (org-test-with-temp-text "* Grandmother (0)
4485 ** Uncle (1)
4486 *** Heir (2)
4487 ** Father (3)
4488 Ancestor text (4)
4489 *** Sister (5)
4490 Sibling text (6)
4491 *** Self (7)
4492 Match (8)
4493 **** First born (9)
4494 Child text (10)
4495 **** The other child (11)
4496 *** Brother (12)
4497 ** Aunt (13)
4499 (org-cycle t)
4500 (search-forward (if headerp "Self" "Match"))
4501 (org-show-set-visibility state)
4502 (goto-char (point-min))
4503 (let (result (line 0))
4504 (while (not (eobp))
4505 (unless (org-invisible-p2) (push line result))
4506 (incf line)
4507 (forward-line))
4508 (nreverse result))))))
4509 (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
4510 (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
4511 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
4512 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
4513 (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
4514 (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
4515 (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
4516 (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
4517 (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
4518 (should (equal '(0 1 3 5 7 8 9 11 12 13)
4519 (funcall list-visible-lines 'tree nil)))
4520 (should (equal '(0 1 3 4 5 7 12 13)
4521 (funcall list-visible-lines 'canonical t)))
4522 (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
4523 (funcall list-visible-lines 'canonical nil)))))
4526 (provide 'test-org)
4528 ;;; test-org.el ends here