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