Fix indentation in empty items
[org-mode/org-tableheadings.git] / testing / lisp / test-org.el
blob7d09288f625b2beb51be514e7d076d2fbb555519
1 ;;; test-org.el --- tests for org.el
3 ;; Copyright (c) David Maus
4 ;; Authors: David Maus
6 ;; This file is not part of GNU Emacs.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Comments:
23 ;; Template test file for Org-mode tests
25 ;;; Code:
28 ;;; Comments
30 (ert-deftest test-org/toggle-comment ()
31 "Test `org-toggle-comment' specifications."
32 ;; Simple headline.
33 (should
34 (equal "* Test"
35 (org-test-with-temp-text "* COMMENT Test"
36 (org-toggle-comment)
37 (buffer-string))))
38 (should
39 (equal "* COMMENT Test"
40 (org-test-with-temp-text "* Test"
41 (org-toggle-comment)
42 (buffer-string))))
43 ;; Headline with a regular keyword.
44 (should
45 (equal "* TODO Test"
46 (org-test-with-temp-text "* TODO COMMENT Test"
47 (org-toggle-comment)
48 (buffer-string))))
49 (should
50 (equal "* TODO COMMENT Test"
51 (org-test-with-temp-text "* TODO Test"
52 (org-toggle-comment)
53 (buffer-string))))
54 ;; Empty headline.
55 (should
56 (equal "* "
57 (org-test-with-temp-text "* COMMENT"
58 (org-toggle-comment)
59 (buffer-string))))
60 (should
61 (equal "* COMMENT"
62 (org-test-with-temp-text "* "
63 (org-toggle-comment)
64 (buffer-string))))
65 ;; Headline with a single keyword.
66 (should
67 (equal "* TODO "
68 (org-test-with-temp-text "* TODO COMMENT"
69 (org-toggle-comment)
70 (buffer-string))))
71 (should
72 (equal "* TODO COMMENT"
73 (org-test-with-temp-text "* TODO"
74 (org-toggle-comment)
75 (buffer-string))))
76 ;; Headline with a keyword, a priority cookie and contents.
77 (should
78 (equal "* TODO [#A] Headline"
79 (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
80 (org-toggle-comment)
81 (buffer-string))))
82 (should
83 (equal "* TODO [#A] COMMENT Headline"
84 (org-test-with-temp-text "* TODO [#A] Headline"
85 (org-toggle-comment)
86 (buffer-string)))))
88 (ert-deftest test-org/comment-dwim ()
89 "Test `comment-dwim' behaviour in an Org buffer."
90 ;; No region selected, no comment on current line and line not
91 ;; empty: insert comment on line above.
92 (should
93 (equal "# \nComment"
94 (org-test-with-temp-text "Comment"
95 (progn (call-interactively 'comment-dwim)
96 (buffer-string)))))
97 ;; No region selected, no comment on current line and line empty:
98 ;; insert comment on this line.
99 (should
100 (equal "# \nParagraph"
101 (org-test-with-temp-text "\nParagraph"
102 (progn (call-interactively 'comment-dwim)
103 (buffer-string)))))
104 ;; No region selected, and a comment on this line: indent it.
105 (should
106 (equal "* Headline\n # Comment"
107 (org-test-with-temp-text "* Headline\n# Comment"
108 (progn (forward-line)
109 (let ((org-adapt-indentation t))
110 (call-interactively 'comment-dwim))
111 (buffer-string)))))
112 ;; Also recognize single # at column 0 as comments.
113 (should
114 (equal "# Comment"
115 (org-test-with-temp-text "# Comment"
116 (progn (forward-line)
117 (call-interactively 'comment-dwim)
118 (buffer-string)))))
119 ;; Region selected and only comments and blank lines within it:
120 ;; un-comment all commented lines.
121 (should
122 (equal "Comment 1\n\nComment 2"
123 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
124 (progn
125 (transient-mark-mode 1)
126 (push-mark (point) t t)
127 (goto-char (point-max))
128 (call-interactively 'comment-dwim)
129 (buffer-string)))))
130 ;; Region selected without comments: comment all lines if
131 ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
132 (should
133 (equal "# Comment 1\n\n# Comment 2"
134 (org-test-with-temp-text "Comment 1\n\nComment 2"
135 (progn
136 (transient-mark-mode 1)
137 (push-mark (point) t t)
138 (goto-char (point-max))
139 (let ((comment-empty-lines nil))
140 (call-interactively 'comment-dwim))
141 (buffer-string)))))
142 (should
143 (equal "# Comment 1\n# \n# Comment 2"
144 (org-test-with-temp-text "Comment 1\n\nComment 2"
145 (progn
146 (transient-mark-mode 1)
147 (push-mark (point) t t)
148 (goto-char (point-max))
149 (let ((comment-empty-lines t))
150 (call-interactively 'comment-dwim))
151 (buffer-string)))))
152 ;; In front of a keyword without region, insert a new comment.
153 (should
154 (equal "# \n#+KEYWORD: value"
155 (org-test-with-temp-text "#+KEYWORD: value"
156 (progn (call-interactively 'comment-dwim)
157 (buffer-string)))))
158 ;; In a source block, use appropriate syntax.
159 (should
160 (equal " ;; "
161 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n\n#+END_SRC"
162 (forward-line)
163 (let ((org-edit-src-content-indentation 2))
164 (call-interactively 'comment-dwim))
165 (buffer-substring-no-properties (line-beginning-position) (point)))))
166 (should
167 (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
168 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\na\nb\n#+END_SRC"
169 (forward-line)
170 (transient-mark-mode 1)
171 (push-mark (point) t t)
172 (forward-line 2)
173 (let ((org-edit-src-content-indentation 2))
174 (call-interactively 'comment-dwim))
175 (buffer-string)))))
179 ;;; Date and time analysis
181 (ert-deftest test-org/org-read-date ()
182 "Test `org-read-date' specifications."
183 ;; Parse ISO date with abbreviated year and month.
184 (should (equal "2012-03-29 16:40"
185 (let ((org-time-was-given t))
186 (org-read-date t nil "12-3-29 16:40"))))
187 ;; Parse Europeans dates.
188 (should (equal "2012-03-29 16:40"
189 (let ((org-time-was-given t))
190 (org-read-date t nil "29.03.2012 16:40"))))
191 ;; Parse Europeans dates without year.
192 (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
193 (let ((org-time-was-given t))
194 (org-read-date t nil "29.03. 16:40"))))
195 ;; Relative date applied to current time if there is single
196 ;; plus/minus, or to default date when there are two of them.
197 (should
198 (equal
199 "2015-03-04"
200 (flet ((current-time () (apply #'encode-time
201 (org-parse-time-string "2014-03-04"))))
202 (org-read-date
203 t nil "+1y" nil
204 (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
205 (should
206 (equal
207 "2013-03-29"
208 (flet ((current-time () (apply #'encode-time
209 (org-parse-time-string "2014-03-04"))))
210 (org-read-date
211 t nil "++1y" nil
212 (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
213 ;; When `org-read-date-prefer-future' is non-nil, prefer future
214 ;; dates (relatively to now) when incomplete. Otherwise, use
215 ;; default date.
216 (should
217 (equal
218 "2014-04-01"
219 (flet ((current-time () (apply #'encode-time
220 (org-parse-time-string "2014-03-04"))))
221 (let ((org-read-date-prefer-future t))
222 (org-read-date t nil "1")))))
223 (should
224 (equal
225 "2013-03-04"
226 (flet ((current-time () (apply #'encode-time
227 (org-parse-time-string "2012-03-29"))))
228 (let ((org-read-date-prefer-future t))
229 (org-read-date t nil "3-4")))))
230 (should
231 (equal
232 "2012-03-04"
233 (flet ((current-time () (apply #'encode-time
234 (org-parse-time-string "2012-03-29"))))
235 (let ((org-read-date-prefer-future nil))
236 (org-read-date t nil "3-4")))))
237 ;; When set to `org-read-date-prefer-future' is set to `time', read
238 ;; day is moved to tomorrow if specified hour is before current
239 ;; time. However, it only happens in no other part of the date is
240 ;; specified.
241 (should
242 (equal
243 "2012-03-30"
244 (flet ((current-time () (apply #'encode-time
245 (org-parse-time-string "2012-03-29 16:40"))))
246 (let ((org-read-date-prefer-future 'time))
247 (org-read-date t nil "00:40" nil)))))
248 (should-not
249 (equal
250 "2012-03-30"
251 (flet ((current-time () (apply #'encode-time
252 (org-parse-time-string "2012-03-29 16:40"))))
253 (let ((org-read-date-prefer-future 'time))
254 (org-read-date t nil "29 00:40" nil)))))
255 ;; Caveat: `org-read-date-prefer-future' always refers to current
256 ;; time, not default time, when they differ.
257 (should
258 (equal
259 "2014-04-01"
260 (flet ((current-time
261 () (apply #'encode-time (org-parse-time-string "2014-03-04"))))
262 (let ((org-read-date-prefer-future t))
263 (org-read-date
264 t nil "1" nil
265 (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
266 (should
267 (equal
268 "2014-03-25"
269 (flet ((current-time
270 () (apply #'encode-time (org-parse-time-string "2014-03-04"))))
271 (let ((org-read-date-prefer-future t))
272 (org-read-date
273 t nil "25" nil
274 (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
276 (ert-deftest test-org/org-parse-time-string ()
277 "Test `org-parse-time-string'."
278 (should (equal (org-parse-time-string "2012-03-29 16:40")
279 '(0 40 16 29 3 2012 nil nil nil)))
280 (should (equal (org-parse-time-string "[2012-03-29 16:40]")
281 '(0 40 16 29 3 2012 nil nil nil)))
282 (should (equal (org-parse-time-string "<2012-03-29 16:40>")
283 '(0 40 16 29 3 2012 nil nil nil)))
284 (should (equal (org-parse-time-string "<2012-03-29>")
285 '(0 0 0 29 3 2012 nil nil nil)))
286 (should (equal (org-parse-time-string "<2012-03-29>" t)
287 '(0 nil nil 29 3 2012 nil nil nil))))
290 ;;; Drawers
292 (ert-deftest test-org/insert-property-drawer ()
293 "Test `org-insert-property-drawer' specifications."
294 ;; Error before first headline.
295 (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
296 ;; Insert drawer right after headline if there is no planning line,
297 ;; or after it otherwise.
298 (should
299 (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
300 (org-test-with-temp-text "* H\nParagraph<point>"
301 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
302 (buffer-string))))
303 (should
304 (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
305 (org-test-with-temp-text
306 "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
307 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
308 (buffer-string))))
309 ;; Indent inserted drawer.
310 (should
311 (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
312 (org-test-with-temp-text "* H\nParagraph<point>"
313 (let ((org-adapt-indentation t)) (org-insert-property-drawer))
314 (buffer-string))))
315 ;; Handle insertion at eob.
316 (should
317 (equal "* H\n:PROPERTIES:\n:END:\n"
318 (org-test-with-temp-text "* H"
319 (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
320 (buffer-string))))
321 ;; Skip inlinetasks before point.
322 (when (featurep 'org-inlinetask)
323 (should
324 (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
325 (org-test-with-temp-text
326 "* H\n*************** I\n*************** END\nP<point>"
327 (let ((org-adapt-indentation nil)
328 (org-inlinetask-min-level 15))
329 (org-insert-property-drawer))
330 (buffer-string)))))
331 ;; Correctly set drawer in an inlinetask.
332 (when (featurep 'org-inlinetask)
333 (should
334 (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
335 (org-test-with-temp-text
336 "* H\n*************** I\nP<point>\n*************** END"
337 (let ((org-adapt-indentation nil)
338 (org-inlinetask-min-level 15))
339 (org-insert-property-drawer))
340 (buffer-string))))))
343 ;;; Filling
345 (ert-deftest test-org/fill-paragraph ()
346 "Test `org-fill-paragraph' specifications."
347 ;; At an Org table, align it.
348 (should
349 (equal "| a |\n"
350 (org-test-with-temp-text "|a|"
351 (org-fill-paragraph)
352 (buffer-string))))
353 (should
354 (equal "#+name: table\n| a |\n"
355 (org-test-with-temp-text "#+name: table\n| a |"
356 (org-fill-paragraph)
357 (buffer-string))))
358 ;; At a paragraph, preserve line breaks.
359 (org-test-with-temp-text "some \\\\\nlong\ntext"
360 (let ((fill-column 20))
361 (org-fill-paragraph)
362 (should (equal (buffer-string) "some \\\\\nlong text"))))
363 ;; Correctly fill a paragraph when point is at its very end.
364 (should
365 (equal "A B"
366 (org-test-with-temp-text "A\nB"
367 (let ((fill-column 20))
368 (goto-char (point-max))
369 (org-fill-paragraph)
370 (buffer-string)))))
371 ;; Correctly fill the last paragraph of a greater element.
372 (should
373 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
374 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
375 (let ((fill-column 8))
376 (forward-line)
377 (end-of-line)
378 (org-fill-paragraph)
379 (buffer-string)))))
380 ;; Correctly fill an element in a narrowed buffer.
381 (should
382 (equal "01234\n6"
383 (org-test-with-temp-text "01234 6789"
384 (let ((fill-column 5))
385 (narrow-to-region 1 8)
386 (org-fill-paragraph)
387 (buffer-string)))))
388 ;; Handle `adaptive-fill-regexp' in paragraphs.
389 (should
390 (equal "> a b"
391 (org-test-with-temp-text "> a\n> b"
392 (let ((fill-column 5)
393 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
394 (org-fill-paragraph)
395 (buffer-string)))))
396 ;; Special case: Fill first paragraph when point is at an item or
397 ;; a plain-list or a footnote reference.
398 (should
399 (equal "- A B"
400 (org-test-with-temp-text "- A\n B"
401 (let ((fill-column 20))
402 (org-fill-paragraph)
403 (buffer-string)))))
404 (should
405 (equal "[fn:1] A B"
406 (org-test-with-temp-text "[fn:1] A\nB"
407 (let ((fill-column 20))
408 (org-fill-paragraph)
409 (buffer-string)))))
410 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
411 (let ((fill-column 20))
412 (org-fill-paragraph)
413 (should (equal (buffer-string)
414 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
415 ;; Fill contents of `comment-block' elements.
416 (should
417 (equal
418 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
419 (let ((fill-column 20))
420 (forward-line)
421 (org-fill-paragraph)
422 (buffer-string)))
423 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
424 ;; Fill `comment' elements.
425 (should
426 (equal " # A B"
427 (org-test-with-temp-text " # A\n # B"
428 (let ((fill-column 20))
429 (org-fill-paragraph)
430 (buffer-string)))))
431 ;; Do not mix consecutive comments when filling one of them.
432 (should
433 (equal "# A B\n\n# C"
434 (org-test-with-temp-text "# A\n# B\n\n# C"
435 (let ((fill-column 20))
436 (org-fill-paragraph)
437 (buffer-string)))))
438 ;; Use commented empty lines as separators when filling comments.
439 (should
440 (equal "# A B\n#\n# C"
441 (org-test-with-temp-text "# A\n# B\n#\n# C"
442 (let ((fill-column 20))
443 (org-fill-paragraph)
444 (buffer-string)))))
445 ;; Handle `adaptive-fill-regexp' in comments.
446 (should
447 (equal "# > a b"
448 (org-test-with-temp-text "# > a\n# > b"
449 (let ((fill-column 20)
450 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
451 (org-fill-paragraph)
452 (buffer-string)))))
453 ;; Do nothing at affiliated keywords.
454 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
455 (let ((fill-column 20))
456 (org-fill-paragraph)
457 (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
458 ;; Do not move point after table when filling a table.
459 (should-not
460 (org-test-with-temp-text "| a | b |\n| c | d |\n"
461 (forward-char)
462 (org-fill-paragraph)
463 (eobp))))
465 (ert-deftest test-org/auto-fill-function ()
466 "Test auto-filling features."
467 ;; Auto fill paragraph.
468 (should
469 (equal "12345\n7890"
470 (org-test-with-temp-text "12345 7890"
471 (let ((fill-column 5))
472 (end-of-line)
473 (org-auto-fill-function)
474 (buffer-string)))))
475 ;; Auto fill first paragraph in an item.
476 (should
477 (equal "- 12345\n 7890"
478 (org-test-with-temp-text "- 12345 7890"
479 (let ((fill-column 7))
480 (end-of-line)
481 (org-auto-fill-function)
482 (buffer-string)))))
483 ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
484 (should
485 (equal "> 12345\n 7890"
486 (org-test-with-temp-text "> 12345 7890"
487 (let ((fill-column 10)
488 (adaptive-fill-regexp "[ \t]*>+[ \t]*")
489 (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
490 (end-of-line)
491 (org-auto-fill-function)
492 (buffer-string)))))
493 (should
494 (equal "> 12345\n> 12345\n> 7890"
495 (org-test-with-temp-text "> 12345\n> 12345 7890"
496 (let ((fill-column 10)
497 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
498 (goto-char (point-max))
499 (org-auto-fill-function)
500 (buffer-string)))))
501 (should-not
502 (equal " 12345\n *12345\n *12345"
503 (org-test-with-temp-text " 12345\n *12345 12345"
504 (let ((fill-column 10)
505 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
506 (goto-char (point-max))
507 (org-auto-fill-function)
508 (buffer-string)))))
509 ;; Auto fill comments.
510 (should
511 (equal " # 12345\n # 7890"
512 (org-test-with-temp-text " # 12345 7890"
513 (let ((fill-column 10))
514 (end-of-line)
515 (org-auto-fill-function)
516 (buffer-string)))))
517 ;; A hash within a line isn't a comment.
518 (should-not
519 (equal "12345 # 7890\n# 1"
520 (org-test-with-temp-text "12345 # 7890 1"
521 (let ((fill-column 12))
522 (end-of-line)
523 (org-auto-fill-function)
524 (buffer-string)))))
525 ;; Correctly interpret empty prefix.
526 (should-not
527 (equal "# a\n# b\nRegular\n# paragraph"
528 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
529 (let ((fill-column 12))
530 (end-of-line 3)
531 (org-auto-fill-function)
532 (buffer-string)))))
533 ;; Comment block: auto fill contents.
534 (should
535 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
536 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
537 (let ((fill-column 5))
538 (forward-line)
539 (end-of-line)
540 (org-auto-fill-function)
541 (buffer-string)))))
542 (should
543 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
544 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
545 (let ((fill-column 5))
546 (forward-line)
547 (end-of-line)
548 (org-auto-fill-function)
549 (buffer-string)))))
550 ;; Do not fill if a new item could be created.
551 (should-not
552 (equal "12345\n- 90"
553 (org-test-with-temp-text "12345 - 90"
554 (let ((fill-column 5))
555 (end-of-line)
556 (org-auto-fill-function)
557 (buffer-string)))))
558 ;; Do not fill if a line break could be introduced.
559 (should-not
560 (equal "123\\\\\n7890"
561 (org-test-with-temp-text "123\\\\ 7890"
562 (let ((fill-column 6))
563 (end-of-line)
564 (org-auto-fill-function)
565 (buffer-string)))))
566 ;; Do not fill affiliated keywords.
567 (should-not
568 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
569 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
570 (let ((fill-column 20))
571 (end-of-line)
572 (org-auto-fill-function)
573 (buffer-string))))))
577 ;;; Indentation
579 (ert-deftest test-org/indent-line ()
580 "Test `org-indent-line' specifications."
581 ;; Do not indent diary sexps, footnote definitions or headlines.
582 (should
583 (zerop
584 (org-test-with-temp-text "%%(org-calendar-holiday)"
585 (org-indent-line)
586 (org-get-indentation))))
587 (should
588 (zerop
589 (org-test-with-temp-text "[fn:1] fn"
590 (let ((org-adapt-indentation t)) (org-indent-line))
591 (org-get-indentation))))
592 (should
593 (zerop
594 (org-test-with-temp-text "* H"
595 (org-indent-line)
596 (org-get-indentation))))
597 ;; Do not indent before first headline.
598 (should
599 (zerop
600 (org-test-with-temp-text ""
601 (org-indent-line)
602 (org-get-indentation))))
603 ;; Indent according to headline level otherwise, unless
604 ;; `org-adapt-indentation' is nil.
605 (should
606 (= 2
607 (org-test-with-temp-text "* H\nA"
608 (forward-line)
609 (let ((org-adapt-indentation t)) (org-indent-line))
610 (org-get-indentation))))
611 (should
612 (= 2
613 (org-test-with-temp-text "* H\n\nA"
614 (forward-line)
615 (let ((org-adapt-indentation t)) (org-indent-line))
616 (org-get-indentation))))
617 (should
618 (zerop
619 (org-test-with-temp-text "* H\nA"
620 (forward-line)
621 (let ((org-adapt-indentation nil)) (org-indent-line))
622 (org-get-indentation))))
623 ;; Indenting preserves point position.
624 (should
625 (org-test-with-temp-text "* H\nAB"
626 (forward-line)
627 (forward-char)
628 (let ((org-adapt-indentation t)) (org-indent-line))
629 (looking-at "B")))
630 ;; Do not change indentation at an item.
631 (should
632 (= 1
633 (org-test-with-temp-text "* H\n - A"
634 (forward-line)
635 (let ((org-adapt-indentation t)) (org-indent-line))
636 (org-get-indentation))))
637 ;; On blank lines at the end of a list, indent like last element
638 ;; within it if the line is still in the list. If the last element
639 ;; is an item, indent like its contents. Otherwise, indent like the
640 ;; whole list.
641 (should
642 (= 4
643 (org-test-with-temp-text "* H\n- A\n - AA\n"
644 (goto-char (point-max))
645 (let ((org-adapt-indentation t)) (org-indent-line))
646 (org-get-indentation))))
647 (should
648 (zerop
649 (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
650 (goto-char (point-max))
651 (let ((org-adapt-indentation t)) (org-indent-line))
652 (org-get-indentation))))
653 (should
654 (= 4
655 (org-test-with-temp-text "* H\n- A\n - \n"
656 (goto-char (point-max))
657 (let ((org-adapt-indentation t)) (org-indent-line))
658 (org-get-indentation))))
659 ;; Likewise, on a blank line at the end of a footnote definition,
660 ;; indent at column 0 if line belongs to the definition. Otherwise,
661 ;; indent like the definition itself.
662 (should
663 (zerop
664 (org-test-with-temp-text "* H\n[fn:1] Definition\n"
665 (goto-char (point-max))
666 (let ((org-adapt-indentation t)) (org-indent-line))
667 (org-get-indentation))))
668 (should
669 (zerop
670 (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
671 (goto-char (point-max))
672 (let ((org-adapt-indentation t)) (org-indent-line))
673 (org-get-indentation))))
674 ;; After the end of the contents of a greater element, indent like
675 ;; the beginning of the element.
676 (should
677 (= 1
678 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
679 (forward-line 2)
680 (org-indent-line)
681 (org-get-indentation))))
682 ;; On blank lines after a paragraph, indent like its last non-empty
683 ;; line.
684 (should
685 (= 1
686 (org-test-with-temp-text " Paragraph\n\n<point>"
687 (org-indent-line)
688 (org-get-indentation))))
689 ;; At the first line of an element, indent like previous element's
690 ;; first line, ignoring footnotes definitions and inline tasks, or
691 ;; according to parent.
692 (should
693 (= 2
694 (org-test-with-temp-text "A\n\n B\n\nC"
695 (goto-char (point-max))
696 (org-indent-line)
697 (org-get-indentation))))
698 (should
699 (= 1
700 (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
701 (goto-char (point-max))
702 (org-indent-line)
703 (org-get-indentation))))
704 (should
705 (= 1
706 (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
707 (forward-line 1)
708 (org-indent-line)
709 (org-get-indentation))))
710 ;; Within code part of a source block, use language major mode if
711 ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
712 ;; according to line above.
713 (should
714 (= 6
715 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
716 (forward-line 2)
717 (let ((org-src-tab-acts-natively t)
718 (org-edit-src-content-indentation 0))
719 (org-indent-line))
720 (org-get-indentation))))
721 (should
722 (= 1
723 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
724 (forward-line 2)
725 (let ((org-src-tab-acts-natively nil)
726 (org-edit-src-content-indentation 0))
727 (org-indent-line))
728 (org-get-indentation))))
729 ;; Otherwise, indent like the first non-blank line above.
730 (should
731 (zerop
732 (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
733 (forward-line 3)
734 (org-indent-line)
735 (org-get-indentation))))
736 ;; Align node properties according to `org-property-format'. Handle
737 ;; nicely empty values.
738 (should
739 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
740 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
741 (let ((org-property-format "%-10s %s")) (org-indent-line))
742 (buffer-string))))
743 (should
744 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
745 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
746 (let ((org-property-format "%-10s %s")) (org-indent-line))
747 (buffer-string)))))
749 (ert-deftest test-org/indent-region ()
750 "Test `org-indent-region' specifications."
751 ;; Indent paragraph.
752 (should
753 (equal "A\nB\nC"
754 (org-test-with-temp-text " A\nB\n C"
755 (org-indent-region (point-min) (point-max))
756 (buffer-string))))
757 ;; Indent greater elements along with their contents.
758 (should
759 (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
760 (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
761 (org-indent-region (point-min) (point-max))
762 (buffer-string))))
763 ;; Ignore contents of verse blocks and example blocks.
764 (should
765 (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
766 (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
767 (org-indent-region (point-min) (point-max))
768 (buffer-string))))
769 (should
770 (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
771 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
772 (org-indent-region (point-min) (point-max))
773 (buffer-string))))
774 ;; Indent according to mode if `org-src-tab-acts-natively' is
775 ;; non-nil. Otherwise, do not indent code at all.
776 (should
777 (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
778 (org-test-with-temp-text
779 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
780 (let ((org-src-tab-acts-natively t)
781 (org-edit-src-content-indentation 0))
782 (org-indent-region (point-min) (point-max)))
783 (buffer-string))))
784 (should
785 (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
786 (org-test-with-temp-text
787 "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
788 (let ((org-src-tab-acts-natively nil)
789 (org-edit-src-content-indentation 0))
790 (org-indent-region (point-min) (point-max)))
791 (buffer-string))))
792 ;; Align node properties according to `org-property-format'. Handle
793 ;; nicely empty values.
794 (should
795 (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
796 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
797 (let ((org-property-format "%-10s %s")
798 (org-adapt-indentation nil))
799 (org-indent-region (point) (point-max)))
800 (buffer-string))))
801 (should
802 (equal "* H\n:PROPERTIES:\n:key:\n:END:"
803 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
804 (let ((org-property-format "%-10s %s")
805 (org-adapt-indentation nil))
806 (org-indent-region (point) (point-max)))
807 (buffer-string))))
808 ;; Indent plain lists.
809 (should
810 (equal "- A\n B\n - C\n\n D"
811 (org-test-with-temp-text "- A\n B\n - C\n\n D"
812 (org-indent-region (point-min) (point-max))
813 (buffer-string))))
814 (should
815 (equal "- A\n\n- B"
816 (org-test-with-temp-text " - A\n\n - B"
817 (org-indent-region (point-min) (point-max))
818 (buffer-string))))
819 ;; Indent footnote definitions.
820 (should
821 (equal "[fn:1] Definition\n\nDefinition"
822 (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
823 (org-indent-region (point-min) (point-max))
824 (buffer-string))))
825 ;; Special case: Start indenting on a blank line.
826 (should
827 (equal "\nParagraph"
828 (org-test-with-temp-text "\n Paragraph"
829 (org-indent-region (point-min) (point-max))
830 (buffer-string)))))
834 ;;; Editing
836 (ert-deftest test-org/return ()
837 "Test RET (`org-return') specifications."
838 ;; Regular test.
839 (should
840 (equal "Para\ngraph"
841 (org-test-with-temp-text "Para<point>graph"
842 (org-return)
843 (buffer-string))))
844 ;; With optional argument, indent line.
845 (should
846 (equal " Para\n graph"
847 (org-test-with-temp-text " Para<point>graph"
848 (org-return t)
849 (buffer-string))))
850 ;; On a table, call `org-table-next-row'.
851 (should
852 (org-test-with-temp-text "| <point>a |\n| b |"
853 (org-return)
854 (org-looking-at-p "b")))
855 ;; Open link or timestamp under point when `org-return-follows-link'
856 ;; is non-nil.
857 (should
858 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
859 (let ((org-return-follows-link t)) (org-return))
860 (org-looking-at-p "<<target>>")))
861 (should-not
862 (org-test-with-temp-text "Link [[target<point>]] <<target>>"
863 (let ((org-return-follows-link nil)) (org-return))
864 (org-looking-at-p "<<target>>")))
865 ;; However, do not open link when point is in a table.
866 (should
867 (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
868 (let ((org-return-follows-link t)) (org-return))
869 (org-looking-at-p "between")))
870 ;; Special case: in a list, when indenting, do not break structure.
871 (should
872 (equal "- A\n B"
873 (org-test-with-temp-text "- A <point>B"
874 (org-return t)
875 (buffer-string))))
876 (should
877 (equal "- A\n\n- B"
878 (org-test-with-temp-text "- A\n<point>- B"
879 (org-return t)
880 (buffer-string))))
881 ;; Special case: on tags part of a headline, add a newline below it
882 ;; instead of breaking it.
883 (should
884 (equal "* H :tag:\n"
885 (org-test-with-temp-text "* H :<point>tag:"
886 (org-return)
887 (buffer-string)))))
889 (ert-deftest test-org/meta-return ()
890 "Test M-RET (`org-meta-return') specifications."
891 ;; In a table field insert a row above.
892 (should
893 (org-test-with-temp-text "| a |"
894 (forward-char)
895 (org-meta-return)
896 (forward-line -1)
897 (looking-at "| |$")))
898 ;; In a paragraph change current line into a header.
899 (should
900 (org-test-with-temp-text "a"
901 (org-meta-return)
902 (beginning-of-line)
903 (looking-at "\* a$")))
904 ;; In an item insert an item, in this case above.
905 (should
906 (org-test-with-temp-text "- a"
907 (org-meta-return)
908 (beginning-of-line)
909 (looking-at "- $")))
910 ;; In a drawer and item insert an item, in this case above.
911 (should
912 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
913 (forward-line)
914 (org-meta-return)
915 (beginning-of-line)
916 (looking-at "- $"))))
918 (ert-deftest test-org/insert-heading ()
919 "Test `org-insert-heading' specifications."
920 ;; In an empty buffer, insert a new headline.
921 (should
922 (equal "* "
923 (org-test-with-temp-text ""
924 (org-insert-heading)
925 (buffer-string))))
926 ;; At the beginning of a line, turn it into a headline
927 (should
928 (equal "* P"
929 (org-test-with-temp-text "<point>P"
930 (org-insert-heading)
931 (buffer-string))))
932 ;; In the middle of a line, split the line if allowed, otherwise,
933 ;; insert the headline at its end.
934 (should
935 (equal "Para\n* graph"
936 (org-test-with-temp-text "Para<point>graph"
937 (let ((org-M-RET-may-split-line '((default . t))))
938 (org-insert-heading))
939 (buffer-string))))
940 (should
941 (equal "Paragraph\n* "
942 (org-test-with-temp-text "Para<point>graph"
943 (let ((org-M-RET-may-split-line '((default . nil))))
944 (org-insert-heading))
945 (buffer-string))))
946 ;; When on a list, insert an item instead, unless called with an
947 ;; universal argument or if list is invisible. In this case, create
948 ;; a new headline after contents.
949 (should
950 (equal "* H\n- item\n- "
951 (org-test-with-temp-text "* H\n- item<point>"
952 (let ((org-insert-heading-respect-content nil))
953 (org-insert-heading))
954 (buffer-string))))
955 (should
956 (equal "* H\n- item\n- item 2\n* "
957 (org-test-with-temp-text "* H\n- item<point>\n- item 2"
958 (let ((org-insert-heading-respect-content nil))
959 (org-insert-heading '(4)))
960 (buffer-string))))
961 (should
962 (equal "* H\n- item\n* "
963 (org-test-with-temp-text "* H\n- item"
964 (org-cycle)
965 (goto-char (point-max))
966 (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
967 (buffer-string))))
968 ;; When called with two universal arguments, insert a new headline
969 ;; at the end of the grandparent subtree.
970 (should
971 (equal "* H1\n** H3\n- item\n** H2\n** "
972 (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
973 (let ((org-insert-heading-respect-content nil))
974 (org-insert-heading '(16)))
975 (buffer-string))))
976 ;; When optional TOP-LEVEL argument is non-nil, always insert
977 ;; a level 1 heading.
978 (should
979 (equal "* H1\n** H2\n* "
980 (org-test-with-temp-text "* H1\n** H2<point>"
981 (org-insert-heading nil nil t)
982 (buffer-string))))
983 (should
984 (equal "* H1\n- item\n* "
985 (org-test-with-temp-text "* H1\n- item<point>"
986 (org-insert-heading nil nil t)
987 (buffer-string))))
988 ;; Corner case: correctly insert a headline after an empty one.
989 (should
990 (equal "* \n* "
991 (org-test-with-temp-text "* <point>"
992 (org-insert-heading)
993 (buffer-string)))))
995 (ert-deftest test-org/insert-todo-heading-respect-content ()
996 "Test `org-insert-todo-heading-respect-content' specifications."
997 ;; Create a TODO heading.
998 (should
999 (org-test-with-temp-text "* H1\n Body"
1000 (org-insert-todo-heading-respect-content)
1001 (nth 2 (org-heading-components))))
1002 ;; Add headline at the end of the first subtree
1003 (should
1004 (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
1005 (search-forward "H1Body")
1006 (org-insert-todo-heading-respect-content)
1007 (and (eobp) (org-at-heading-p))))
1008 ;; In a list, do not create a new item.
1009 (should
1010 (org-test-with-temp-text "* H\n- an item\n- another one"
1011 (search-forward "an ")
1012 (org-insert-todo-heading-respect-content)
1013 (and (eobp) (org-at-heading-p)))))
1017 ;;; Fixed-Width Areas
1019 (ert-deftest test-org/toggle-fixed-width ()
1020 "Test `org-toggle-fixed-width' specifications."
1021 ;; No region: Toggle on fixed-width marker in paragraphs.
1022 (should
1023 (equal ": A"
1024 (org-test-with-temp-text "A"
1025 (org-toggle-fixed-width)
1026 (buffer-string))))
1027 ;; No region: Toggle off fixed-width markers in fixed-width areas.
1028 (should
1029 (equal "A"
1030 (org-test-with-temp-text ": A"
1031 (org-toggle-fixed-width)
1032 (buffer-string))))
1033 ;; No region: Toggle on marker in blank lines after elements or just
1034 ;; after a headline.
1035 (should
1036 (equal "* H\n: "
1037 (org-test-with-temp-text "* H\n"
1038 (forward-line)
1039 (org-toggle-fixed-width)
1040 (buffer-string))))
1041 (should
1042 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
1043 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
1044 (goto-char (point-max))
1045 (org-toggle-fixed-width)
1046 (buffer-string))))
1047 ;; No region: Toggle on marker in front of one line elements (e.g.,
1048 ;; headlines, clocks)
1049 (should
1050 (equal ": * Headline"
1051 (org-test-with-temp-text "* Headline"
1052 (org-toggle-fixed-width)
1053 (buffer-string))))
1054 (should
1055 (equal ": #+KEYWORD: value"
1056 (org-test-with-temp-text "#+KEYWORD: value"
1057 (org-toggle-fixed-width)
1058 (buffer-string))))
1059 ;; No region: error in other situations.
1060 (should-error
1061 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
1062 (forward-line)
1063 (org-toggle-fixed-width)
1064 (buffer-string)))
1065 ;; No region: Indentation is preserved.
1066 (should
1067 (equal "- A\n : B"
1068 (org-test-with-temp-text "- A\n B"
1069 (forward-line)
1070 (org-toggle-fixed-width)
1071 (buffer-string))))
1072 ;; Region: If it contains only fixed-width elements and blank lines,
1073 ;; toggle off fixed-width markup.
1074 (should
1075 (equal "A\n\nB"
1076 (org-test-with-temp-text ": A\n\n: B"
1077 (transient-mark-mode 1)
1078 (push-mark (point) t t)
1079 (goto-char (point-max))
1080 (org-toggle-fixed-width)
1081 (buffer-string))))
1082 ;; Region: If it contains anything else, toggle on fixed-width but
1083 ;; not on fixed-width areas.
1084 (should
1085 (equal ": A\n: \n: B\n: \n: C"
1086 (org-test-with-temp-text "A\n\n: B\n\nC"
1087 (transient-mark-mode 1)
1088 (push-mark (point) t t)
1089 (goto-char (point-max))
1090 (org-toggle-fixed-width)
1091 (buffer-string))))
1092 ;; Region: Ignore blank lines at its end, unless it contains only
1093 ;; such lines.
1094 (should
1095 (equal ": A\n\n"
1096 (org-test-with-temp-text "A\n\n"
1097 (transient-mark-mode 1)
1098 (push-mark (point) t t)
1099 (goto-char (point-max))
1100 (org-toggle-fixed-width)
1101 (buffer-string))))
1102 (should
1103 (equal ": \n: \n"
1104 (org-test-with-temp-text "\n\n"
1105 (transient-mark-mode 1)
1106 (push-mark (point) t t)
1107 (goto-char (point-max))
1108 (org-toggle-fixed-width)
1109 (buffer-string)))))
1113 ;;; Headline
1115 (ert-deftest test-org/in-commented-heading-p ()
1116 "Test `org-in-commented-heading-p' specifications."
1117 ;; Commented headline.
1118 (should
1119 (org-test-with-temp-text "* COMMENT Headline\nBody"
1120 (goto-char (point-max))
1121 (org-in-commented-heading-p)))
1122 ;; Commented ancestor.
1123 (should
1124 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1125 (goto-char (point-max))
1126 (org-in-commented-heading-p)))
1127 ;; Comment keyword is case-sensitive.
1128 (should-not
1129 (org-test-with-temp-text "* Comment Headline\nBody"
1130 (goto-char (point-max))
1131 (org-in-commented-heading-p)))
1132 ;; Keyword is standalone.
1133 (should-not
1134 (org-test-with-temp-text "* COMMENTHeadline\nBody"
1135 (goto-char (point-max))
1136 (org-in-commented-heading-p)))
1137 ;; Optional argument.
1138 (should-not
1139 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
1140 (goto-char (point-max))
1141 (org-in-commented-heading-p t))))
1145 ;;; Keywords
1147 (ert-deftest test-org/set-regexps-and-options ()
1148 "Test `org-set-regexps-and-options' specifications."
1149 ;; TAGS keyword.
1150 (should
1151 (equal '(("A" . ?a) ("B") ("C"))
1152 (org-test-with-temp-text "#+TAGS: A(a) B C"
1153 (org-mode-restart)
1154 org-tag-alist)))
1155 (should
1156 (equal '(("A") (:newline) ("B"))
1157 (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
1158 (org-mode-restart)
1159 org-tag-alist)))
1160 (should
1161 (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
1162 (org-test-with-temp-text "#+TAGS: { A B } C"
1163 (org-mode-restart)
1164 org-tag-alist)))
1165 (should
1166 (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
1167 (org-test-with-temp-text "#+TAGS: { A : B C }"
1168 (org-mode-restart)
1169 org-tag-alist)))
1170 (should
1171 (equal '(("A" "B" "C"))
1172 (org-test-with-temp-text "#+TAGS: { A : B C }"
1173 (org-mode-restart)
1174 org-tag-groups-alist)))
1175 (should
1176 (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
1177 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1178 (org-mode-restart)
1179 org-tag-alist)))
1180 (should
1181 (equal '(("A" "B" "C"))
1182 (org-test-with-temp-text "#+TAGS: [ A : B C ]"
1183 (org-mode-restart)
1184 org-tag-groups-alist)))
1185 ;; FILETAGS keyword.
1186 (should
1187 (equal '("A" "B" "C")
1188 (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
1189 (org-mode-restart)
1190 org-file-tags)))
1191 ;; PROPERTY keyword. Property names are case-insensitive.
1192 (should
1193 (equal "foo=1"
1194 (org-test-with-temp-text "#+PROPERTY: var foo=1"
1195 (org-mode-restart)
1196 (cdr (assoc "var" org-file-properties)))))
1197 (should
1198 (equal
1199 "foo=1 bar=2"
1200 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
1201 (org-mode-restart)
1202 (cdr (assoc "var" org-file-properties)))))
1203 (should
1204 (equal
1205 "foo=1 bar=2"
1206 (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
1207 (org-mode-restart)
1208 (cdr (assoc "var" org-file-properties)))))
1209 ;; ARCHIVE keyword.
1210 (should
1211 (equal "%s_done::"
1212 (org-test-with-temp-text "#+ARCHIVE: %s_done::"
1213 (org-mode-restart)
1214 org-archive-location)))
1215 ;; CATEGORY keyword.
1216 (should
1217 (eq 'test
1218 (org-test-with-temp-text "#+CATEGORY: test"
1219 (org-mode-restart)
1220 org-category)))
1221 (should
1222 (equal "test"
1223 (org-test-with-temp-text "#+CATEGORY: test"
1224 (org-mode-restart)
1225 (cdr (assoc "CATEGORY" org-file-properties)))))
1226 ;; COLUMNS keyword.
1227 (should
1228 (equal "%25ITEM %TAGS %PRIORITY %TODO"
1229 (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
1230 (org-mode-restart)
1231 org-columns-default-format)))
1232 ;; CONSTANTS keyword. Constants names are case sensitive.
1233 (should
1234 (equal '("299792458." "3.14")
1235 (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
1236 (org-mode-restart)
1237 (mapcar
1238 (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
1239 '("c" "pi")))))
1240 (should
1241 (equal "3.14"
1242 (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
1243 (org-mode-restart)
1244 (cdr (assoc "pi" org-table-formula-constants-local)))))
1245 (should
1246 (equal "22/7"
1247 (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
1248 (org-mode-restart)
1249 (cdr (assoc "PI" org-table-formula-constants-local)))))
1250 ;; LINK keyword.
1251 (should
1252 (equal
1253 '("url1" "url2")
1254 (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
1255 (org-mode-restart)
1256 (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
1257 '("a" "b")))))
1258 ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
1259 (should
1260 (equal
1261 '(?X ?Z ?Y)
1262 (org-test-with-temp-text "#+PRIORITIES: X Z Y"
1263 (org-mode-restart)
1264 (list org-highest-priority org-lowest-priority org-default-priority))))
1265 (should
1266 (equal
1267 '(?A ?C ?B)
1268 (org-test-with-temp-text "#+PRIORITIES: X Z"
1269 (org-mode-restart)
1270 (list org-highest-priority org-lowest-priority org-default-priority))))
1271 ;; STARTUP keyword.
1272 (should
1273 (equal '(t t)
1274 (org-test-with-temp-text "#+STARTUP: fold odd"
1275 (org-mode-restart)
1276 (list org-startup-folded org-odd-levels-only))))
1277 ;; TODO keywords.
1278 (should
1279 (equal '(("A" "B") ("C"))
1280 (org-test-with-temp-text "#+TODO: A B | C"
1281 (org-mode-restart)
1282 (list org-not-done-keywords org-done-keywords))))
1283 (should
1284 (equal '(("A" "C") ("B" "D"))
1285 (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
1286 (org-mode-restart)
1287 (list org-not-done-keywords org-done-keywords))))
1288 (should
1289 (equal '(("A" "B") ("C"))
1290 (org-test-with-temp-text "#+TYP_TODO: A B | C"
1291 (org-mode-restart)
1292 (list org-not-done-keywords org-done-keywords))))
1293 (should
1294 (equal '((:startgroup) ("A" . ?a) (:endgroup))
1295 (org-test-with-temp-text "#+TODO: A(a)"
1296 (org-mode-restart)
1297 org-todo-key-alist)))
1298 (should
1299 (equal '(("D" note nil) ("C" time nil) ("B" note time))
1300 (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
1301 (org-mode-restart)
1302 org-todo-log-states)))
1303 ;; Enter SETUPFILE keyword.
1304 (should
1305 (equal "1"
1306 (org-test-with-temp-text
1307 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
1308 (org-mode-restart)
1309 (cdr (assoc "a" org-file-properties))))))
1313 ;;; Links
1315 ;;;; Coderefs
1317 (ert-deftest test-org/coderef ()
1318 "Test coderef links specifications."
1319 (should
1320 (org-test-with-temp-text "
1321 #+BEGIN_SRC emacs-lisp
1322 \(+ 1 1) (ref:sc)
1323 #+END_SRC
1324 \[[(sc)]]"
1325 (goto-char (point-max))
1326 (org-open-at-point)
1327 (looking-at "(ref:sc)"))))
1329 ;;;; Custom ID
1331 (ert-deftest test-org/custom-id ()
1332 "Test custom ID links specifications."
1333 (should
1334 (org-test-with-temp-text
1335 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
1336 (goto-char (point-max))
1337 (org-open-at-point)
1338 (org-looking-at-p "\\* H1")))
1339 ;; Ignore false positives.
1340 (should-not
1341 (org-test-with-temp-text
1342 "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
1343 (goto-char (point-max))
1344 (let (org-link-search-must-match-exact-headline) (org-open-at-point))
1345 (org-looking-at-p "\\* H1"))))
1347 ;;;; Fuzzy Links
1349 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
1350 ;; a named element (#+name: text) and to headlines (* Text).
1352 (ert-deftest test-org/fuzzy-links ()
1353 "Test fuzzy links specifications."
1354 ;; Fuzzy link goes in priority to a matching target.
1355 (should
1356 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
1357 (goto-line 5)
1358 (org-open-at-point)
1359 (looking-at "<<Test>>")))
1360 ;; Then fuzzy link points to an element with a given name.
1361 (should
1362 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
1363 (goto-line 5)
1364 (org-open-at-point)
1365 (looking-at "#\\+NAME: Test")))
1366 ;; A target still lead to a matching headline otherwise.
1367 (should
1368 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
1369 (goto-line 4)
1370 (org-open-at-point)
1371 (looking-at "\\* Head2")))
1372 ;; With a leading star in link, enforce heading match.
1373 (should
1374 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
1375 (goto-line 3)
1376 (org-open-at-point)
1377 (looking-at "\\* Test")))
1378 ;; With a leading star in link, enforce exact heading match, even
1379 ;; with `org-link-search-must-match-exact-headline' set to nil.
1380 (should-error
1381 (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
1382 (let ((org-link-search-must-match-exact-headline nil))
1383 (org-open-at-point))))
1384 ;; Heading match should not care about spaces, cookies, todo
1385 ;; keywords, priorities, and tags.
1386 (should
1387 (let ((first-line
1388 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1389 (org-test-with-temp-text
1390 (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
1391 (let ((org-link-search-must-match-exact-headline nil)
1392 (org-todo-regexp "TODO"))
1393 (org-open-at-point))
1394 (looking-at (regexp-quote first-line)))))
1395 ;; Heading match should still be exact.
1396 (should-error
1397 (let ((first-line
1398 "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
1399 (org-test-with-temp-text
1400 (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
1401 (let ((org-link-search-must-match-exact-headline nil)
1402 (org-todo-regexp "TODO"))
1403 (org-open-at-point)))))
1404 ;; Heading match ignores COMMENT keyword.
1405 (should
1406 (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
1407 (org-open-at-point)
1408 (looking-at "\\* COMMENT Test")))
1409 ;; Correctly un-hexify fuzzy links.
1410 (should
1411 (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
1412 (goto-char (point-max))
1413 (org-open-at-point)
1414 (bobp))))
1416 ;;;; Link Escaping
1418 (ert-deftest test-org/org-link-escape-ascii-character ()
1419 "Escape an ascii character."
1420 (should
1421 (string=
1422 "%5B"
1423 (org-link-escape "["))))
1425 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
1426 "Escape an ascii control character."
1427 (should
1428 (string=
1429 "%09"
1430 (org-link-escape "\t"))))
1432 (ert-deftest test-org/org-link-escape-multibyte-character ()
1433 "Escape an unicode multibyte character."
1434 (should
1435 (string=
1436 "%E2%82%AC"
1437 (org-link-escape "€"))))
1439 (ert-deftest test-org/org-link-escape-custom-table ()
1440 "Escape string with custom character table."
1441 (should
1442 (string=
1443 "Foo%3A%42ar%0A"
1444 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
1446 (ert-deftest test-org/org-link-escape-custom-table-merge ()
1447 "Escape string with custom table merged with default table."
1448 (should
1449 (string=
1450 "%5BF%6F%6F%3A%42ar%0A%5D"
1451 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
1453 (ert-deftest test-org/org-link-unescape-ascii-character ()
1454 "Unescape an ascii character."
1455 (should
1456 (string=
1458 (org-link-unescape "%5B"))))
1460 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
1461 "Unescpae an ascii control character."
1462 (should
1463 (string=
1464 "\n"
1465 (org-link-unescape "%0A"))))
1467 (ert-deftest test-org/org-link-unescape-multibyte-character ()
1468 "Unescape unicode multibyte character."
1469 (should
1470 (string=
1471 "€"
1472 (org-link-unescape "%E2%82%AC"))))
1474 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
1475 "Unescape old style percent escaped character."
1476 (should
1477 (string=
1478 "àâçèéêîôùû"
1479 (decode-coding-string
1480 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
1482 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
1483 "Escape and unescape a URL that includes an escaped char.
1484 http://article.gmane.org/gmane.emacs.orgmode/21459/"
1485 (should
1486 (string=
1487 "http://some.host.com/form?&id=blah%2Bblah25"
1488 (org-link-unescape
1489 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
1491 (ert-deftest test-org/org-link-escape-chars-browser ()
1492 "Test of the constant `org-link-escape-chars-browser'.
1493 See there why this test is a candidate to be removed once Org
1494 drops support for Emacs 24.1 and 24.2."
1495 (should
1496 (string=
1497 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1498 "%22Release%208.2%22&idxname=emacs-orgmode")
1499 (org-link-escape-browser ; Do not replace with `url-encode-url',
1500 ; see docstring above.
1501 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
1502 "\"Release 8.2\"&idxname=emacs-orgmode")))))
1504 ;;;; Open at point
1506 (ert-deftest test-org/open-at-point-in-keyword ()
1507 "Does `org-open-at-point' open link in a keyword line?"
1508 (should
1509 (org-test-with-temp-text
1510 "#+KEYWORD: <point>[[info:emacs#Top]]"
1511 (org-open-at-point) t)))
1513 (ert-deftest test-org/open-at-point-in-property ()
1514 "Does `org-open-at-point' open link in property drawer?"
1515 (should
1516 (org-test-with-temp-text
1517 "* Headline
1518 :PROPERTIES:
1519 :URL: <point>[[info:emacs#Top]]
1520 :END:"
1521 (org-open-at-point) t)))
1523 (ert-deftest test-org/open-at-point-in-comment ()
1524 "Does `org-open-at-point' open link in a commented line?"
1525 (should
1526 (org-test-with-temp-text
1527 "# <point>[[info:emacs#Top]]"
1528 (org-open-at-point) t)))
1530 (ert-deftest test-org/open-at-point/info ()
1531 "Test `org-open-at-point' on info links."
1532 (should
1533 (org-test-with-temp-text
1534 "<point>[[info:emacs#Top]]"
1535 (org-open-at-point)
1536 (and (switch-to-buffer "*info*")
1537 (prog1
1538 (looking-at "\nThe Emacs Editor")
1539 (kill-buffer))))))
1541 (ert-deftest test-org/open-at-point/inline-image ()
1542 "Test `org-open-at-point' on nested links."
1543 (should
1544 (org-test-with-temp-text "[[info:org#Top][info:<point>emacs#Top]]"
1545 (org-open-at-point)
1546 (prog1 (with-current-buffer "*info*" (looking-at "\nOrg Mode Manual"))
1547 (kill-buffer "*info*")))))
1550 ;;; Node Properties
1552 (ert-deftest test-org/accumulated-properties-in-drawers ()
1553 "Ensure properties accumulate in subtree drawers."
1554 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
1555 (org-babel-next-src-block)
1556 (should (equal '(2 1) (org-babel-execute-src-block)))))
1558 (ert-deftest test-org/custom-properties ()
1559 "Test custom properties specifications."
1560 ;; Standard test.
1561 (should
1562 (let ((org-custom-properties '("FOO")))
1563 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1564 (org-toggle-custom-properties-visibility)
1565 (org-invisible-p2))))
1566 ;; Properties are case-insensitive.
1567 (should
1568 (let ((org-custom-properties '("FOO")))
1569 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
1570 (org-toggle-custom-properties-visibility)
1571 (org-invisible-p2))))
1572 (should
1573 (let ((org-custom-properties '("foo")))
1574 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
1575 (org-toggle-custom-properties-visibility)
1576 (org-invisible-p2))))
1577 ;; Multiple custom properties in the same drawer.
1578 (should
1579 (let ((org-custom-properties '("FOO" "BAR")))
1580 (org-test-with-temp-text
1581 "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
1582 (org-toggle-custom-properties-visibility)
1583 (and (org-invisible-p2)
1584 (not (progn (forward-line) (org-invisible-p2)))
1585 (progn (forward-line) (org-invisible-p2))))))
1586 ;; Hide custom properties with an empty value.
1587 (should
1588 (let ((org-custom-properties '("FOO")))
1589 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
1590 (org-toggle-custom-properties-visibility)
1591 (org-invisible-p2))))
1592 ;; Do not hide fake properties.
1593 (should-not
1594 (let ((org-custom-properties '("FOO")))
1595 (org-test-with-temp-text ":FOO: val\n"
1596 (org-toggle-custom-properties-visibility)
1597 (org-invisible-p2))))
1598 (should-not
1599 (let ((org-custom-properties '("A")))
1600 (org-test-with-temp-text
1601 "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
1602 (org-toggle-custom-properties-visibility)
1603 (org-invisible-p2)))))
1607 ;;; Mark Region
1609 (ert-deftest test-org/mark-subtree ()
1610 "Test `org-mark-subtree' specifications."
1611 ;; Error when point is before first headline.
1612 (should-error
1613 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
1614 (progn (transient-mark-mode 1)
1615 (org-mark-subtree))))
1616 ;; Without argument, mark current subtree.
1617 (should
1618 (equal
1619 '(12 32)
1620 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1621 (progn (transient-mark-mode 1)
1622 (forward-line 2)
1623 (org-mark-subtree)
1624 (list (region-beginning) (region-end))))))
1625 ;; With an argument, move ARG up.
1626 (should
1627 (equal
1628 '(1 32)
1629 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
1630 (progn (transient-mark-mode 1)
1631 (forward-line 2)
1632 (org-mark-subtree 1)
1633 (list (region-beginning) (region-end))))))
1634 ;; Do not get fooled by inlinetasks.
1635 (when (featurep 'org-inlinetask)
1636 (should
1637 (= 1
1638 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
1639 (progn (transient-mark-mode 1)
1640 (forward-line 1)
1641 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
1642 (region-beginning)))))))
1646 ;;; Navigation
1648 (ert-deftest test-org/end-of-meta-data ()
1649 "Test `org-end-of-meta-data' specifications."
1650 ;; Skip planning line.
1651 (should
1652 (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
1653 (org-end-of-meta-data)
1654 (eobp)))
1655 ;; Skip properties drawer.
1656 (should
1657 (org-test-with-temp-text
1658 "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
1659 (org-end-of-meta-data)
1660 (eobp)))
1661 ;; Skip both.
1662 (should
1663 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
1664 (org-end-of-meta-data)
1665 (eobp)))
1666 ;; Nothing to skip, go to first line.
1667 (should
1668 (org-test-with-temp-text "* Headline\nContents"
1669 (org-end-of-meta-data)
1670 (looking-at "Contents")))
1671 ;; With option argument, skip empty lines, regular drawers and
1672 ;; clocking lines.
1673 (should
1674 (org-test-with-temp-text "* Headline\n\nContents"
1675 (org-end-of-meta-data t)
1676 (looking-at "Contents")))
1677 (should
1678 (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
1679 (org-end-of-meta-data t)
1680 (looking-at "Contents")))
1681 (should
1682 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
1683 (org-end-of-meta-data t)
1684 (looking-at "Contents")))
1685 ;; Special case: do not skip incomplete drawers.
1686 (should
1687 (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
1688 (org-end-of-meta-data t)
1689 (looking-at ":LOGBOOK:")))
1690 ;; Special case: Be careful about consecutive headlines.
1691 (should-not
1692 (org-test-with-temp-text "* H1\n*H2\nContents"
1693 (org-end-of-meta-data t)
1694 (looking-at "Contents"))))
1696 (ert-deftest test-org/beginning-of-line ()
1697 "Test `org-beginning-of-line' specifications."
1698 ;; Standard test.
1699 (should
1700 (org-test-with-temp-text "Some text\nSome other text"
1701 (progn (org-beginning-of-line) (bolp))))
1702 ;; Standard test with `visual-line-mode'.
1703 (should-not
1704 (org-test-with-temp-text "A long line of text\nSome other text"
1705 (progn (visual-line-mode)
1706 (forward-char 2)
1707 (dotimes (i 1000) (insert "very "))
1708 (org-beginning-of-line)
1709 (bolp))))
1710 ;; At an headline with special movement.
1711 (should
1712 (org-test-with-temp-text "* TODO Headline"
1713 (let ((org-special-ctrl-a/e t))
1714 (org-end-of-line)
1715 (and (progn (org-beginning-of-line) (looking-at "Headline"))
1716 (progn (org-beginning-of-line) (bolp))
1717 (progn (org-beginning-of-line) (looking-at "Headline")))))))
1719 (ert-deftest test-org/end-of-line ()
1720 "Test `org-end-of-line' specifications."
1721 ;; Standard test.
1722 (should
1723 (org-test-with-temp-text "Some text\nSome other text"
1724 (progn (org-end-of-line) (eolp))))
1725 ;; Standard test with `visual-line-mode'.
1726 (should-not
1727 (org-test-with-temp-text "A long line of text\nSome other text"
1728 (progn (visual-line-mode)
1729 (forward-char 2)
1730 (dotimes (i 1000) (insert "very "))
1731 (goto-char (point-min))
1732 (org-end-of-line)
1733 (eolp))))
1734 ;; At an headline with special movement.
1735 (should
1736 (org-test-with-temp-text "* Headline1 :tag:\n"
1737 (let ((org-special-ctrl-a/e t))
1738 (and (progn (org-end-of-line) (looking-at " :tag:"))
1739 (progn (org-end-of-line) (eolp))
1740 (progn (org-end-of-line) (looking-at " :tag:"))))))
1741 ;; At an headline without special movement.
1742 (should
1743 (org-test-with-temp-text "* Headline2 :tag:\n"
1744 (let ((org-special-ctrl-a/e nil))
1745 (and (progn (org-end-of-line) (eolp))
1746 (progn (org-end-of-line) (eolp))))))
1747 ;; At an headline, with reversed movement.
1748 (should
1749 (org-test-with-temp-text "* Headline3 :tag:\n"
1750 (let ((org-special-ctrl-a/e 'reversed)
1751 (this-command last-command))
1752 (and (progn (org-end-of-line) (eolp))
1753 (progn (org-end-of-line) (looking-at " :tag:"))))))
1754 ;; At a block without hidden contents.
1755 (should
1756 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1757 (progn (org-end-of-line) (eolp))))
1758 ;; At a block with hidden contents.
1759 (should-not
1760 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
1761 (let ((org-special-ctrl-a/e t))
1762 (org-hide-block-toggle)
1763 (org-end-of-line)
1764 (eobp)))))
1766 (ert-deftest test-org/forward-sentence ()
1767 "Test `org-forward-sentence' specifications."
1768 ;; At the end of a table cell, move to the end of the next one.
1769 (should
1770 (org-test-with-temp-text "| a<point> | b |"
1771 (org-forward-sentence)
1772 (looking-at " |$")))
1773 ;; Elsewhere in a cell, move to its end.
1774 (should
1775 (org-test-with-temp-text "| a<point>c | b |"
1776 (org-forward-sentence)
1777 (looking-at " | b |$")))
1778 ;; Otherwise, simply call `forward-sentence'.
1779 (should
1780 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
1781 (org-forward-sentence)
1782 (looking-at " Sentence 2.")))
1783 (should
1784 (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
1785 (org-forward-sentence)
1786 (org-forward-sentence)
1787 (eobp)))
1788 ;; At the end of an element, jump to the next one, without stopping
1789 ;; on blank lines in-between.
1790 (should
1791 (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
1792 (org-forward-sentence)
1793 (eobp))))
1795 (ert-deftest test-org/backward-sentence ()
1796 "Test `org-backward-sentence' specifications."
1797 ;; At the beginning of a table cell, move to the beginning of the
1798 ;; previous one.
1799 (should
1800 (org-test-with-temp-text "| a | <point>b |"
1801 (org-backward-sentence)
1802 (looking-at "a | b |$")))
1803 ;; Elsewhere in a cell, move to its beginning.
1804 (should
1805 (org-test-with-temp-text "| a | b<point>c |"
1806 (org-backward-sentence)
1807 (looking-at "bc |$")))
1808 ;; Otherwise, simply call `backward-sentence'.
1809 (should
1810 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
1811 (org-backward-sentence)
1812 (looking-at "Sentence 2.")))
1813 (should
1814 (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
1815 (org-backward-sentence)
1816 (org-backward-sentence)
1817 (bobp)))
1818 ;; Make sure to hit the beginning of a sentence on the same line as
1819 ;; an item.
1820 (should
1821 (org-test-with-temp-text "- Line 1\n line <point>2."
1822 (org-backward-sentence)
1823 (looking-at "Line 1"))))
1825 (ert-deftest test-org/forward-paragraph ()
1826 "Test `org-forward-paragraph' specifications."
1827 ;; At end of buffer, return an error.
1828 (should-error
1829 (org-test-with-temp-text "Paragraph"
1830 (goto-char (point-max))
1831 (org-forward-paragraph)))
1832 ;; Standard test.
1833 (should
1834 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1835 (org-forward-paragraph)
1836 (looking-at "P2")))
1837 ;; Ignore depth.
1838 (should
1839 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
1840 (org-forward-paragraph)
1841 (looking-at "P1")))
1842 ;; Do not enter elements with invisible contents.
1843 (should
1844 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
1845 (org-hide-block-toggle)
1846 (org-forward-paragraph)
1847 (looking-at "P3")))
1848 ;; On an affiliated keyword, jump to the beginning of the element.
1849 (should
1850 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
1851 (org-forward-paragraph)
1852 (looking-at "Para")))
1853 ;; On an item or a footnote definition, move to the second element
1854 ;; inside, if any.
1855 (should
1856 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
1857 (org-forward-paragraph)
1858 (looking-at " Paragraph")))
1859 (should
1860 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
1861 (org-forward-paragraph)
1862 (looking-at "Paragraph")))
1863 ;; On an item, or a footnote definition, when the first line is
1864 ;; empty, move to the first item.
1865 (should
1866 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
1867 (org-forward-paragraph)
1868 (looking-at " Paragraph")))
1869 (should
1870 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
1871 (org-forward-paragraph)
1872 (looking-at "Paragraph")))
1873 ;; On a table (resp. a property drawer) do not move through table
1874 ;; rows (resp. node properties).
1875 (should
1876 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
1877 (org-forward-paragraph)
1878 (looking-at "Paragraph")))
1879 (should
1880 (org-test-with-temp-text
1881 "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
1882 (org-forward-paragraph)
1883 (looking-at "Paragraph")))
1884 ;; On a verse or source block, stop after blank lines.
1885 (should
1886 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
1887 (org-forward-paragraph)
1888 (looking-at "L2")))
1889 (should
1890 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
1891 (org-forward-paragraph)
1892 (looking-at "L2"))))
1894 (ert-deftest test-org/backward-paragraph ()
1895 "Test `org-backward-paragraph' specifications."
1896 ;; Error at beginning of buffer.
1897 (should-error
1898 (org-test-with-temp-text "Paragraph"
1899 (org-backward-paragraph)))
1900 ;; Regular test.
1901 (should
1902 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1903 (goto-char (point-max))
1904 (org-backward-paragraph)
1905 (looking-at "P3")))
1906 (should
1907 (org-test-with-temp-text "P1\n\nP2\n\nP3"
1908 (goto-char (point-max))
1909 (beginning-of-line)
1910 (org-backward-paragraph)
1911 (looking-at "P2")))
1912 ;; Ignore depth.
1913 (should
1914 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
1915 (goto-char (point-max))
1916 (beginning-of-line)
1917 (org-backward-paragraph)
1918 (looking-at "P2")))
1919 ;; Ignore invisible elements.
1920 (should
1921 (org-test-with-temp-text "* H1\n P1\n* H2"
1922 (org-cycle)
1923 (goto-char (point-max))
1924 (beginning-of-line)
1925 (org-backward-paragraph)
1926 (bobp)))
1927 ;; On an affiliated keyword, jump to the first one.
1928 (should
1929 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
1930 (search-forward "c2")
1931 (org-backward-paragraph)
1932 (looking-at "#\\+name")))
1933 ;; On the second element in an item or a footnote definition, jump
1934 ;; to item or the definition.
1935 (should
1936 (org-test-with-temp-text "- line1\n\n line2"
1937 (goto-char (point-max))
1938 (beginning-of-line)
1939 (org-backward-paragraph)
1940 (looking-at "- line1")))
1941 (should
1942 (org-test-with-temp-text "[fn:1] line1\n\n line2"
1943 (goto-char (point-max))
1944 (beginning-of-line)
1945 (org-backward-paragraph)
1946 (looking-at "\\[fn:1\\] line1")))
1947 ;; On a table (resp. a property drawer), ignore table rows
1948 ;; (resp. node properties).
1949 (should
1950 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
1951 (goto-char (point-max))
1952 (beginning-of-line)
1953 (org-backward-paragraph)
1954 (bobp)))
1955 (should
1956 (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
1957 (org-backward-paragraph)
1958 (looking-at ":PROPERTIES:")))
1959 ;; On a source or verse block, stop before blank lines.
1960 (should
1961 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
1962 (search-forward "L3")
1963 (beginning-of-line)
1964 (org-backward-paragraph)
1965 (looking-at "L2")))
1966 (should
1967 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
1968 (search-forward "L3")
1969 (beginning-of-line)
1970 (org-backward-paragraph)
1971 (looking-at "L2"))))
1973 (ert-deftest test-org/forward-element ()
1974 "Test `org-forward-element' specifications."
1975 ;; 1. At EOB: should error.
1976 (org-test-with-temp-text "Some text\n"
1977 (goto-char (point-max))
1978 (should-error (org-forward-element)))
1979 ;; 2. Standard move: expected to ignore blank lines.
1980 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
1981 (org-forward-element)
1982 (should (looking-at (regexp-quote "Second paragraph."))))
1983 ;; 3. Headline tests.
1984 (org-test-with-temp-text "
1985 * Head 1
1986 ** Head 1.1
1987 *** Head 1.1.1
1988 ** Head 1.2"
1989 ;; 3.1. At an headline beginning: move to next headline at the
1990 ;; same level.
1991 (goto-line 3)
1992 (org-forward-element)
1993 (should (looking-at (regexp-quote "** Head 1.2")))
1994 ;; 3.2. At an headline beginning: move to parent headline if no
1995 ;; headline at the same level.
1996 (goto-line 3)
1997 (org-forward-element)
1998 (should (looking-at (regexp-quote "** Head 1.2"))))
1999 ;; 4. Greater element tests.
2000 (org-test-with-temp-text
2001 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
2002 ;; 4.1. At a greater element: expected to skip contents.
2003 (org-forward-element)
2004 (should (looking-at (regexp-quote "Outside.")))
2005 ;; 4.2. At the end of greater element contents: expected to skip
2006 ;; to the end of the greater element.
2007 (goto-line 2)
2008 (org-forward-element)
2009 (should (looking-at (regexp-quote "Outside."))))
2010 ;; 5. List tests.
2011 (org-test-with-temp-text "
2012 - item1
2014 - sub1
2016 - sub2
2018 - sub3
2020 Inner paragraph.
2022 - item2
2024 Outside."
2025 ;; 5.1. At list top point: expected to move to the element after
2026 ;; the list.
2027 (goto-line 2)
2028 (org-forward-element)
2029 (should (looking-at (regexp-quote "Outside.")))
2030 ;; 5.2. Special case: at the first line of a sub-list, but not at
2031 ;; beginning of line, move to next item.
2032 (goto-line 2)
2033 (forward-char)
2034 (org-forward-element)
2035 (should (looking-at "- item2"))
2036 (goto-line 4)
2037 (forward-char)
2038 (org-forward-element)
2039 (should (looking-at " - sub2"))
2040 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
2041 (goto-line 4)
2042 (org-forward-element)
2043 (should (looking-at (regexp-quote " Inner paragraph.")))
2044 ;; 5.4. At sub-list end: expected to move outside the sub-list.
2045 (goto-line 8)
2046 (org-forward-element)
2047 (should (looking-at (regexp-quote " Inner paragraph.")))
2048 ;; 5.5. At an item: expected to move to next item, if any.
2049 (goto-line 6)
2050 (org-forward-element)
2051 (should (looking-at " - sub3"))))
2053 (ert-deftest test-org/backward-element ()
2054 "Test `org-backward-element' specifications."
2055 ;; 1. Should error at BOB.
2056 (org-test-with-temp-text " \nParagraph."
2057 (should-error (org-backward-element)))
2058 ;; 2. Should move at BOB when called on the first element in buffer.
2059 (should
2060 (org-test-with-temp-text "\n#+TITLE: test"
2061 (progn (forward-line)
2062 (org-backward-element)
2063 (bobp))))
2064 ;; 3. Not at the beginning of an element: move at its beginning.
2065 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2066 (goto-line 3)
2067 (end-of-line)
2068 (org-backward-element)
2069 (should (looking-at (regexp-quote "Paragraph2."))))
2070 ;; 4. Headline tests.
2071 (org-test-with-temp-text "
2072 * Head 1
2073 ** Head 1.1
2074 *** Head 1.1.1
2075 ** Head 1.2"
2076 ;; 4.1. At an headline beginning: move to previous headline at the
2077 ;; same level.
2078 (goto-line 5)
2079 (org-backward-element)
2080 (should (looking-at (regexp-quote "** Head 1.1")))
2081 ;; 4.2. At an headline beginning: move to parent headline if no
2082 ;; headline at the same level.
2083 (goto-line 3)
2084 (org-backward-element)
2085 (should (looking-at (regexp-quote "* Head 1")))
2086 ;; 4.3. At the first top-level headline: should error.
2087 (goto-line 2)
2088 (should-error (org-backward-element)))
2089 ;; 5. At beginning of first element inside a greater element:
2090 ;; expected to move to greater element's beginning.
2091 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
2092 (goto-line 3)
2093 (org-backward-element)
2094 (should (looking-at "#\\+BEGIN_CENTER")))
2095 ;; 6. At the beginning of the first element in a section: should
2096 ;; move back to headline, if any.
2097 (should
2098 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
2099 (progn (goto-char (point-max))
2100 (beginning-of-line)
2101 (org-backward-element)
2102 (org-at-heading-p))))
2103 ;; 7. List tests.
2104 (org-test-with-temp-text "
2105 - item1
2107 - sub1
2109 - sub2
2111 - sub3
2113 Inner paragraph.
2115 - item2
2118 Outside."
2119 ;; 7.1. At beginning of sub-list: expected to move to the
2120 ;; paragraph before it.
2121 (goto-line 4)
2122 (org-backward-element)
2123 (should (looking-at "item1"))
2124 ;; 7.2. At an item in a list: expected to move at previous item.
2125 (goto-line 8)
2126 (org-backward-element)
2127 (should (looking-at " - sub2"))
2128 (goto-line 12)
2129 (org-backward-element)
2130 (should (looking-at "- item1"))
2131 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
2132 ;; beginning.
2133 (goto-line 10)
2134 (org-backward-element)
2135 (should (looking-at " - sub1"))
2136 (goto-line 15)
2137 (org-backward-element)
2138 (should (looking-at "- item1"))
2139 ;; 7.4. At blank-lines before list end: expected to move to top
2140 ;; item.
2141 (goto-line 14)
2142 (org-backward-element)
2143 (should (looking-at "- item1"))))
2145 (ert-deftest test-org/up-element ()
2146 "Test `org-up-element' specifications."
2147 ;; 1. At BOB or with no surrounding element: should error.
2148 (org-test-with-temp-text "Paragraph."
2149 (should-error (org-up-element)))
2150 (org-test-with-temp-text "* Head1\n* Head2"
2151 (goto-line 2)
2152 (should-error (org-up-element)))
2153 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
2154 (goto-line 3)
2155 (should-error (org-up-element)))
2156 ;; 2. At an headline: move to parent headline.
2157 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
2158 (goto-line 3)
2159 (org-up-element)
2160 (should (looking-at "\\* Head1")))
2161 ;; 3. Inside a greater element: move to greater element beginning.
2162 (org-test-with-temp-text
2163 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
2164 (goto-line 3)
2165 (org-up-element)
2166 (should (looking-at "#\\+BEGIN_CENTER")))
2167 ;; 4. List tests.
2168 (org-test-with-temp-text "* Top
2169 - item1
2171 - sub1
2173 - sub2
2175 Paragraph within sub2.
2177 - item2"
2178 ;; 4.1. Within an item: move to the item beginning.
2179 (goto-line 8)
2180 (org-up-element)
2181 (should (looking-at " - sub2"))
2182 ;; 4.2. At an item in a sub-list: move to parent item.
2183 (goto-line 4)
2184 (org-up-element)
2185 (should (looking-at "- item1"))
2186 ;; 4.3. At an item in top list: move to beginning of whole list.
2187 (goto-line 10)
2188 (org-up-element)
2189 (should (looking-at "- item1"))
2190 ;; 4.4. Special case. At very top point: should move to parent of
2191 ;; list.
2192 (goto-line 2)
2193 (org-up-element)
2194 (should (looking-at "\\* Top"))))
2196 (ert-deftest test-org/down-element ()
2197 "Test `org-down-element' specifications."
2198 ;; Error when the element hasn't got a recursive type.
2199 (org-test-with-temp-text "Paragraph."
2200 (should-error (org-down-element)))
2201 ;; Error when the element has no contents
2202 (org-test-with-temp-text "* Headline"
2203 (should-error (org-down-element)))
2204 ;; When at a plain-list, move to first item.
2205 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
2206 (goto-line 2)
2207 (org-down-element)
2208 (should (looking-at " - Item 1.1")))
2209 (org-test-with-temp-text "#+NAME: list\n- Item 1"
2210 (org-down-element)
2211 (should (looking-at " Item 1")))
2212 ;; When at a table, move to first row
2213 (org-test-with-temp-text "#+NAME: table\n| a | b |"
2214 (org-down-element)
2215 (should (looking-at " a | b |")))
2216 ;; Otherwise, move inside the greater element.
2217 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
2218 (org-down-element)
2219 (should (looking-at "Paragraph"))))
2221 (ert-deftest test-org/drag-element-backward ()
2222 "Test `org-drag-element-backward' specifications."
2223 ;; Standard test.
2224 (should
2225 (equal
2226 "#+key2: val2\n#+key1: val1\n#+key3: val3"
2227 (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
2228 (org-drag-element-backward)
2229 (buffer-string))))
2230 (should
2231 (equal
2232 "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
2233 (org-test-with-temp-text
2234 "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
2235 (org-drag-element-backward)
2236 (buffer-string))))
2237 ;; Preserve blank lines.
2238 (should
2239 (equal "Paragraph 2\n\n\nPara1\n\nPara3"
2240 (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
2241 (org-drag-element-backward)
2242 (buffer-string))))
2243 ;; Preserve column.
2244 (should
2245 (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
2246 (org-drag-element-backward)
2247 (org-looking-at-p "2")))
2248 ;; Error when trying to move first element of buffer.
2249 (should-error
2250 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2251 (org-drag-element-backward)))
2252 ;; Error when trying to swap nested elements.
2253 (should-error
2254 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2255 (forward-line)
2256 (org-drag-element-backward)))
2257 ;; Error when trying to swap an headline element and a non-headline
2258 ;; element.
2259 (should-error
2260 (org-test-with-temp-text "Test.\n* Head 1"
2261 (forward-line)
2262 (org-drag-element-backward)))
2263 ;; Preserve visibility of elements and their contents.
2264 (should
2265 (equal '((63 . 82) (26 . 48))
2266 (org-test-with-temp-text "
2267 #+BEGIN_CENTER
2268 Text.
2269 #+END_CENTER
2270 - item 1
2271 #+BEGIN_QUOTE
2272 Text.
2273 #+END_QUOTE"
2274 (while (search-forward "BEGIN_" nil t) (org-cycle))
2275 (search-backward "- item 1")
2276 (org-drag-element-backward)
2277 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2278 (overlays-in (point-min) (point-max)))))))
2280 (ert-deftest test-org/drag-element-forward ()
2281 "Test `org-drag-element-forward' specifications."
2282 ;; 1. Error when trying to move first element of buffer.
2283 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
2284 (goto-line 3)
2285 (should-error (org-drag-element-forward)))
2286 ;; 2. Error when trying to swap nested elements.
2287 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
2288 (forward-line)
2289 (should-error (org-drag-element-forward)))
2290 ;; 3. Error when trying to swap a non-headline element and an
2291 ;; headline.
2292 (org-test-with-temp-text "Test.\n* Head 1"
2293 (should-error (org-drag-element-forward)))
2294 ;; 4. Otherwise, swap elements, preserving column and blank lines
2295 ;; between elements.
2296 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
2297 (search-forward "graph")
2298 (org-drag-element-forward)
2299 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
2300 (should (looking-at " 1")))
2301 ;; 5. Preserve visibility of elements and their contents.
2302 (org-test-with-temp-text "
2303 #+BEGIN_CENTER
2304 Text.
2305 #+END_CENTER
2306 - item 1
2307 #+BEGIN_QUOTE
2308 Text.
2309 #+END_QUOTE"
2310 (while (search-forward "BEGIN_" nil t) (org-cycle))
2311 (search-backward "#+BEGIN_CENTER")
2312 (org-drag-element-forward)
2313 (should
2314 (equal
2315 '((63 . 82) (26 . 48))
2316 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
2317 (overlays-in (point-min) (point-max)))))))
2319 (ert-deftest test-org/next-block ()
2320 "Test `org-next-block' specifications."
2321 ;; Regular test.
2322 (should
2323 (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
2324 (org-next-block 1)
2325 (looking-at "#\\+BEGIN_CENTER")))
2326 ;; Ignore case.
2327 (should
2328 (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
2329 (let ((case-fold-search nil))
2330 (org-next-block 1)
2331 (looking-at "#\\+begin_center"))))
2332 ;; Ignore current line.
2333 (should
2334 (org-test-with-temp-text
2335 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
2336 (org-next-block 1)
2337 (looking-at "#\\+BEGIN_CENTER")))
2338 ;; Throw an error when no block is found.
2339 (should-error
2340 (org-test-with-temp-text "Paragraph"
2341 (org-next-block 1)))
2342 ;; With an argument, skip many blocks at once.
2343 (should
2344 (org-test-with-temp-text
2345 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
2346 (org-next-block 2)
2347 (looking-at "#\\+BEGIN_QUOTE")))
2348 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
2349 (should
2350 (org-test-with-temp-text
2351 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
2352 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
2353 (looking-at "#\\+BEGIN_QUOTE")))
2354 ;; Optional argument is also case-insensitive.
2355 (should
2356 (org-test-with-temp-text
2357 "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
2358 (let ((case-fold-search nil))
2359 (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
2360 (looking-at "#\\+begin_quote")))))
2362 (ert-deftest test-org/previous-block ()
2363 "Test `org-previous-block' specifications."
2364 ;; Regular test.
2365 (should
2366 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
2367 (org-previous-block 1)
2368 (looking-at "#\\+BEGIN_CENTER")))
2369 ;; Ignore case.
2370 (should
2371 (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
2372 (let ((case-fold-search nil))
2373 (org-previous-block 1)
2374 (looking-at "#\\+begin_center"))))
2375 ;; Ignore current line.
2376 (should
2377 (org-test-with-temp-text
2378 "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
2379 (org-previous-block 1)
2380 (looking-at "#\\+BEGIN_QUOTE")))
2381 ;; Throw an error when no block is found.
2382 (should-error
2383 (org-test-with-temp-text "Paragraph<point>"
2384 (org-previous-block 1)))
2385 ;; With an argument, skip many blocks at once.
2386 (should
2387 (org-test-with-temp-text
2388 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
2389 (org-previous-block 2)
2390 (looking-at "#\\+BEGIN_CENTER")))
2391 ;; With optional argument BLOCK-REGEXP, filter matched blocks.
2392 (should
2393 (org-test-with-temp-text
2394 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
2395 (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
2396 (looking-at "#\\+BEGIN_QUOTE")))
2397 ;; Optional argument is also case-insensitive.
2398 (should
2399 (org-test-with-temp-text
2400 "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
2401 (let ((case-fold-search nil))
2402 (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
2403 (looking-at "#\\+begin_quote")))))
2406 ;;; Outline structure
2408 (ert-deftest test-org/demote ()
2409 "Test `org-demote' specifications."
2410 ;; Add correct number of stars according to `org-odd-levels-only'.
2411 (should
2412 (= 2
2413 (org-test-with-temp-text "* H"
2414 (let ((org-odd-levels-only nil)) (org-demote))
2415 (org-current-level))))
2416 (should
2417 (= 3
2418 (org-test-with-temp-text "* H"
2419 (let ((org-odd-levels-only t)) (org-demote))
2420 (org-current-level))))
2421 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2422 (should
2423 (org-test-with-temp-text "* H :tag:"
2424 (let ((org-tags-column 10)
2425 (org-auto-align-tags t)
2426 (org-odd-levels-only nil))
2427 (org-demote))
2428 (org-move-to-column 10)
2429 (org-looking-at-p ":tag:$")))
2430 (should-not
2431 (org-test-with-temp-text "* H :tag:"
2432 (let ((org-tags-column 10)
2433 (org-auto-align-tags nil)
2434 (org-odd-levels-only nil))
2435 (org-demote))
2436 (org-move-to-column 10)
2437 (org-looking-at-p ":tag:$")))
2438 ;; When `org-adapt-indentation' is non-nil, always indent planning
2439 ;; info and property drawers accordingly.
2440 (should
2441 (= 3
2442 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2443 (let ((org-odd-levels-only nil)
2444 (org-adapt-indentation t))
2445 (org-demote))
2446 (forward-line)
2447 (org-get-indentation))))
2448 (should
2449 (= 3
2450 (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2451 (let ((org-odd-levels-only nil)
2452 (org-adapt-indentation t))
2453 (org-demote))
2454 (forward-line)
2455 (org-get-indentation))))
2456 (should-not
2457 (= 3
2458 (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
2459 (let ((org-odd-levels-only nil)
2460 (org-adapt-indentation nil))
2461 (org-demote))
2462 (forward-line)
2463 (org-get-indentation))))
2464 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2465 ;; section accordingly. Ignore, however, footnote definitions and
2466 ;; inlinetasks boundaries.
2467 (should
2468 (= 3
2469 (org-test-with-temp-text "* H\n Paragraph"
2470 (let ((org-odd-levels-only nil)
2471 (org-adapt-indentation t))
2472 (org-demote))
2473 (forward-line)
2474 (org-get-indentation))))
2475 (should
2476 (= 2
2477 (org-test-with-temp-text "* H\n Paragraph"
2478 (let ((org-odd-levels-only nil)
2479 (org-adapt-indentation nil))
2480 (org-demote))
2481 (forward-line)
2482 (org-get-indentation))))
2483 (should
2484 (zerop
2485 (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
2486 (let ((org-odd-levels-only nil)
2487 (org-adapt-indentation t))
2488 (org-demote))
2489 (goto-char (point-max))
2490 (org-get-indentation))))
2491 (should
2492 (= 3
2493 (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
2494 (let ((org-odd-levels-only nil)
2495 (org-adapt-indentation t))
2496 (org-demote))
2497 (goto-char (point-max))
2498 (org-get-indentation))))
2499 (when (featurep 'org-inlinetask)
2500 (should
2501 (zerop
2502 (let ((org-inlinetask-min-level 5)
2503 (org-adapt-indentation t))
2504 (org-test-with-temp-text "* H\n***** I\n***** END"
2505 (org-demote)
2506 (forward-line)
2507 (org-get-indentation))))))
2508 (when (featurep 'org-inlinetask)
2509 (should
2510 (= 3
2511 (let ((org-inlinetask-min-level 5)
2512 (org-adapt-indentation t))
2513 (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
2514 (org-demote)
2515 (forward-line 2)
2516 (org-get-indentation))))))
2517 ;; Ignore contents of source blocks or example blocks when
2518 ;; indentation should be preserved (through
2519 ;; `org-src-preserve-indentation' or "-i" flag).
2520 (should-not
2521 (zerop
2522 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2523 (let ((org-adapt-indentation t)
2524 (org-src-preserve-indentation nil))
2525 (org-demote))
2526 (forward-line 2)
2527 (org-get-indentation))))
2528 (should
2529 (zerop
2530 (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2531 (let ((org-adapt-indentation t)
2532 (org-src-preserve-indentation t))
2533 (org-demote))
2534 (forward-line 2)
2535 (org-get-indentation))))
2536 (should
2537 (zerop
2538 (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2539 (let ((org-adapt-indentation t)
2540 (org-src-preserve-indentation t))
2541 (org-demote))
2542 (forward-line 2)
2543 (org-get-indentation))))
2544 (should
2545 (zerop
2546 (org-test-with-temp-text
2547 "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
2548 (let ((org-adapt-indentation t)
2549 (org-src-preserve-indentation nil))
2550 (org-demote))
2551 (forward-line 2)
2552 (org-get-indentation)))))
2554 (ert-deftest test-org/promote ()
2555 "Test `org-promote' specifications."
2556 ;; Return an error if headline is to be promoted to level 0, unless
2557 ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
2558 ;; headline becomes a comment.
2559 (should-error
2560 (org-test-with-temp-text "* H"
2561 (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
2562 (should
2563 (equal "# H"
2564 (org-test-with-temp-text "* H"
2565 (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
2566 (buffer-string))))
2567 ;; Remove correct number of stars according to
2568 ;; `org-odd-levels-only'.
2569 (should
2570 (= 2
2571 (org-test-with-temp-text "*** H"
2572 (let ((org-odd-levels-only nil)) (org-promote))
2573 (org-current-level))))
2574 (should
2575 (= 1
2576 (org-test-with-temp-text "*** H"
2577 (let ((org-odd-levels-only t)) (org-promote))
2578 (org-current-level))))
2579 ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
2580 (should
2581 (org-test-with-temp-text "** H :tag:"
2582 (let ((org-tags-column 10)
2583 (org-auto-align-tags t)
2584 (org-odd-levels-only nil))
2585 (org-promote))
2586 (org-move-to-column 10)
2587 (org-looking-at-p ":tag:$")))
2588 (should-not
2589 (org-test-with-temp-text "** H :tag:"
2590 (let ((org-tags-column 10)
2591 (org-auto-align-tags nil)
2592 (org-odd-levels-only nil))
2593 (org-promote))
2594 (org-move-to-column 10)
2595 (org-looking-at-p ":tag:$")))
2596 ;; When `org-adapt-indentation' is non-nil, always indent planning
2597 ;; info and property drawers.
2598 (should
2599 (= 2
2600 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2601 (let ((org-odd-levels-only nil)
2602 (org-adapt-indentation t))
2603 (org-promote))
2604 (forward-line)
2605 (org-get-indentation))))
2606 (should
2607 (= 2
2608 (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
2609 (let ((org-odd-levels-only nil)
2610 (org-adapt-indentation t))
2611 (org-promote))
2612 (forward-line)
2613 (org-get-indentation))))
2614 (should-not
2615 (= 2
2616 (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
2617 (let ((org-odd-levels-only nil)
2618 (org-adapt-indentation nil))
2619 (org-promote))
2620 (forward-line)
2621 (org-get-indentation))))
2622 ;; When `org-adapt-indentation' is non-nil, shift all lines in
2623 ;; section accordingly. Ignore, however, footnote definitions and
2624 ;; inlinetasks boundaries.
2625 (should
2626 (= 2
2627 (org-test-with-temp-text "** H\n Paragraph"
2628 (let ((org-odd-levels-only nil)
2629 (org-adapt-indentation t))
2630 (org-promote))
2631 (forward-line)
2632 (org-get-indentation))))
2633 (should-not
2634 (= 2
2635 (org-test-with-temp-text "** H\n Paragraph"
2636 (let ((org-odd-levels-only nil)
2637 (org-adapt-indentation nil))
2638 (org-promote))
2639 (forward-line)
2640 (org-get-indentation))))
2641 (should
2642 (= 2
2643 (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
2644 (let ((org-odd-levels-only nil)
2645 (org-adapt-indentation t))
2646 (org-promote))
2647 (forward-line)
2648 (org-get-indentation))))
2649 (when (featurep 'org-inlinetask)
2650 (should
2651 (zerop
2652 (let ((org-inlinetask-min-level 5)
2653 (org-adapt-indentation t))
2654 (org-test-with-temp-text "** H\n***** I\n***** END"
2655 (org-promote)
2656 (forward-line)
2657 (org-get-indentation))))))
2658 (when (featurep 'org-inlinetask)
2659 (should
2660 (= 2
2661 (let ((org-inlinetask-min-level 5)
2662 (org-adapt-indentation t))
2663 (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
2664 (org-promote)
2665 (forward-line 2)
2666 (org-get-indentation))))))
2667 ;; Give up shifting if it would break document's structure
2668 ;; otherwise.
2669 (should
2670 (= 3
2671 (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
2672 (let ((org-odd-levels-only nil)
2673 (org-adapt-indentation t))
2674 (org-promote))
2675 (forward-line)
2676 (org-get-indentation))))
2677 (should
2678 (= 3
2679 (org-test-with-temp-text "** H\n Paragraph\n * list."
2680 (let ((org-odd-levels-only nil)
2681 (org-adapt-indentation t))
2682 (org-promote))
2683 (forward-line)
2684 (org-get-indentation))))
2685 ;; Ignore contents of source blocks or example blocks when
2686 ;; indentation should be preserved (through
2687 ;; `org-src-preserve-indentation' or "-i" flag).
2688 (should-not
2689 (zerop
2690 (org-test-with-temp-text
2691 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2692 (let ((org-adapt-indentation t)
2693 (org-src-preserve-indentation nil)
2694 (org-odd-levels-only nil))
2695 (org-promote))
2696 (forward-line)
2697 (org-get-indentation))))
2698 (should
2699 (zerop
2700 (org-test-with-temp-text
2701 "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
2702 (let ((org-adapt-indentation t)
2703 (org-src-preserve-indentation t)
2704 (org-odd-levels-only nil))
2705 (org-promote))
2706 (forward-line)
2707 (org-get-indentation))))
2708 (should
2709 (zerop
2710 (org-test-with-temp-text
2711 "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
2712 (let ((org-adapt-indentation t)
2713 (org-src-preserve-indentation t)
2714 (org-odd-levels-only nil))
2715 (org-promote))
2716 (forward-line)
2717 (org-get-indentation))))
2718 (should
2719 (zerop
2720 (org-test-with-temp-text
2721 "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
2722 (let ((org-adapt-indentation t)
2723 (org-src-preserve-indentation nil)
2724 (org-odd-levels-only nil))
2725 (org-promote))
2726 (forward-line)
2727 (org-get-indentation)))))
2730 ;;; Planning
2732 (ert-deftest test-org/at-planning-p ()
2733 "Test `org-at-planning-p' specifications."
2734 ;; Regular test.
2735 (should
2736 (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
2737 (org-at-planning-p)))
2738 (should-not
2739 (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
2740 (org-at-planning-p)))
2741 ;; Correctly find planning attached to inlinetasks.
2742 (when (featurep 'org-inlinetask)
2743 (should
2744 (org-test-with-temp-text
2745 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
2746 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2747 (should-not
2748 (org-test-with-temp-text
2749 "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
2750 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2751 (should-not
2752 (org-test-with-temp-text
2753 "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
2754 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
2755 (should-not
2756 (org-test-with-temp-text
2757 "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
2758 (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
2761 ;;; Property API
2763 (ert-deftest test-org/buffer-property-keys ()
2764 "Test `org-buffer-property-keys' specifications."
2765 ;; Retrieve properties accross siblings.
2766 (should
2767 (equal '("A" "B")
2768 (org-test-with-temp-text "
2769 * H1
2770 :PROPERTIES:
2771 :A: 1
2772 :END:
2773 * H2
2774 :PROPERTIES:
2775 :B: 1
2776 :END:"
2777 (org-buffer-property-keys))))
2778 ;; Retrieve properties accross children.
2779 (should
2780 (equal '("A" "B")
2781 (org-test-with-temp-text "
2782 * H1
2783 :PROPERTIES:
2784 :A: 1
2785 :END:
2786 ** H2
2787 :PROPERTIES:
2788 :B: 1
2789 :END:"
2790 (org-buffer-property-keys))))
2791 ;; Retrieve muliple properties in the same drawer.
2792 (should
2793 (equal '("A" "B")
2794 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2795 (org-buffer-property-keys))))
2796 ;; Ignore extension symbol in property name.
2797 (should
2798 (equal '("A")
2799 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
2800 (org-buffer-property-keys))))
2801 ;; With non-nil COLUMNS, extract property names from columns.
2802 (should
2803 (equal '("A" "B")
2804 (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
2805 (org-buffer-property-keys nil nil t))))
2806 (should
2807 (equal '("A" "B" "COLUMNS")
2808 (org-test-with-temp-text
2809 "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
2810 (org-buffer-property-keys nil nil t)))))
2812 (ert-deftest test-org/property-values ()
2813 "Test `org-property-values' specifications."
2814 ;; Regular test.
2815 (should
2816 (equal '("2" "1")
2817 (org-test-with-temp-text
2818 "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
2819 (org-property-values "A"))))
2820 ;; Ignore empty values.
2821 (should-not
2822 (org-test-with-temp-text
2823 "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
2824 (org-property-values "A")))
2825 ;; Take into consideration extended values.
2826 (should
2827 (equal '("1 2")
2828 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
2829 (org-property-values "A")))))
2831 (ert-deftest test-org/find-property ()
2832 "Test `org-find-property' specifications."
2833 ;; Regular test.
2834 (should
2835 (= 1
2836 (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
2837 (org-find-property "prop"))))
2838 ;; Ignore false positives.
2839 (should
2840 (= 27
2841 (org-test-with-temp-text
2842 "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
2843 (org-find-property "A"))))
2844 ;; Return first entry found in buffer.
2845 (should
2846 (= 1
2847 (org-test-with-temp-text
2848 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
2849 (org-find-property "A"))))
2850 ;; Only search visible part of the buffer.
2851 (should
2852 (= 31
2853 (org-test-with-temp-text
2854 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
2855 (org-narrow-to-subtree)
2856 (org-find-property "A"))))
2857 ;; With optional argument, only find entries with a specific value.
2858 (should-not
2859 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2860 (org-find-property "A" "2")))
2861 (should
2862 (= 31
2863 (org-test-with-temp-text
2864 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
2865 (org-find-property "A" "2"))))
2866 ;; Use "nil" for explicit nil values.
2867 (should
2868 (= 31
2869 (org-test-with-temp-text
2870 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
2871 (org-find-property "A" "nil")))))
2873 (ert-deftest test-org/entry-delete ()
2874 "Test `org-entry-delete' specifications."
2875 ;; Regular test.
2876 (should
2877 (string-match
2878 " *:PROPERTIES:\n *:B: +2\n *:END:"
2879 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2880 (org-entry-delete (point) "A")
2881 (buffer-string))))
2882 ;; Also remove accumulated properties.
2883 (should-not
2884 (string-match
2885 ":A"
2886 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
2887 (org-entry-delete (point) "A")
2888 (buffer-string))))
2889 ;; When last property is removed, remove the property drawer.
2890 (should-not
2891 (string-match
2892 ":PROPERTIES:"
2893 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
2894 (org-entry-delete (point) "A")
2895 (buffer-string))))
2896 ;; Return a non-nil value when some property was removed.
2897 (should
2898 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2899 (org-entry-delete (point) "A")))
2900 (should-not
2901 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
2902 (org-entry-delete (point) "C"))))
2904 (ert-deftest test-org/entry-get ()
2905 "Test `org-entry-get' specifications."
2906 ;; Regular test.
2907 (should
2908 (equal "1"
2909 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2910 (org-entry-get (point) "A"))))
2911 ;; Ignore case.
2912 (should
2913 (equal "1"
2914 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2915 (org-entry-get (point) "a"))))
2916 ;; Handle extended values, both before and after base value.
2917 (should
2918 (equal "1 2 3"
2919 (org-test-with-temp-text
2920 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
2921 (org-entry-get (point) "A"))))
2922 ;; Empty values are returned as the empty string.
2923 (should
2924 (equal ""
2925 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
2926 (org-entry-get (point) "A"))))
2927 ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
2928 ;; otherwise, return nil.
2929 (should-not
2930 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
2931 (org-entry-get (point) "A")))
2932 (should
2933 (equal "nil"
2934 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
2935 (org-entry-get (point) "A" nil t))))
2936 ;; Return nil when no property is found, independently on the
2937 ;; LITERAL-NIL argument.
2938 (should-not
2939 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2940 (org-entry-get (point) "B")))
2941 (should-not
2942 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
2943 (org-entry-get (point) "B" nil t)))
2944 ;; Handle inheritance, when allowed.
2945 (should
2946 (equal
2948 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2949 (org-entry-get (point) "A" t))))
2950 (should
2951 (equal
2953 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2954 (let ((org-use-property-inheritance t))
2955 (org-entry-get (point) "A" 'selective)))))
2956 (should-not
2957 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
2958 (let ((org-use-property-inheritance nil))
2959 (org-entry-get (point) "A" 'selective)))))
2961 (ert-deftest test-org/entry-properties ()
2962 "Test `org-entry-properties' specifications."
2963 ;; Get "ITEM" property.
2964 (should
2965 (equal "* H"
2966 (org-test-with-temp-text "* TODO H"
2967 (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
2968 (should
2969 (equal "* H"
2970 (org-test-with-temp-text "* TODO H"
2971 (cdr (assoc "ITEM" (org-entry-properties))))))
2972 ;; Get "TODO" property.
2973 (should
2974 (equal "TODO"
2975 (org-test-with-temp-text "* TODO H"
2976 (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
2977 (should
2978 (equal "TODO"
2979 (org-test-with-temp-text "* TODO H"
2980 (cdr (assoc "TODO" (org-entry-properties))))))
2981 (should-not
2982 (org-test-with-temp-text "* H"
2983 (assoc "TODO" (org-entry-properties nil "TODO"))))
2984 ;; Get "PRIORITY" property.
2985 (should
2986 (equal "A"
2987 (org-test-with-temp-text "* [#A] H"
2988 (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
2989 (should
2990 (equal "A"
2991 (org-test-with-temp-text "* [#A] H"
2992 (cdr (assoc "PRIORITY" (org-entry-properties))))))
2993 (should-not
2994 (org-test-with-temp-text "* H"
2995 (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
2996 ;; Get "FILE" property.
2997 (should
2998 (org-test-with-temp-text-in-file "* H\nParagraph"
2999 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
3000 (buffer-file-name))))
3001 (should
3002 (org-test-with-temp-text-in-file "* H\nParagraph"
3003 (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
3004 (buffer-file-name))))
3005 (should-not
3006 (org-test-with-temp-text "* H\nParagraph"
3007 (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
3008 ;; Get "TAGS" property.
3009 (should
3010 (equal ":tag1:tag2:"
3011 (org-test-with-temp-text "* H :tag1:tag2:"
3012 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
3013 (should
3014 (equal ":tag1:tag2:"
3015 (org-test-with-temp-text "* H :tag1:tag2:"
3016 (cdr (assoc "TAGS" (org-entry-properties))))))
3017 (should-not
3018 (org-test-with-temp-text "* H"
3019 (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
3020 ;; Get "ALLTAGS" property.
3021 (should
3022 (equal ":tag1:tag2:"
3023 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
3024 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
3025 (should
3026 (equal ":tag1:tag2:"
3027 (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
3028 (cdr (assoc "ALLTAGS" (org-entry-properties))))))
3029 (should-not
3030 (org-test-with-temp-text "* H"
3031 (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
3032 ;; Get "BLOCKED" property.
3033 (should
3034 (equal "t"
3035 (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
3036 (let ((org-enforce-todo-dependencies t)
3037 (org-blocker-hook
3038 '(org-block-todo-from-children-or-siblings-or-parent)))
3039 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
3040 (should
3041 (equal "t"
3042 (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
3043 (let ((org-enforce-todo-dependencies t)
3044 (org-blocker-hook
3045 '(org-block-todo-from-children-or-siblings-or-parent)))
3046 (cdr (assoc "BLOCKED" (org-entry-properties)))))))
3047 (should
3048 (equal ""
3049 (org-test-with-temp-text "* Blocked\n** DONE one\n** DONE two"
3050 (let ((org-enforce-todo-dependencies t))
3051 (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
3052 ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
3053 (should
3054 (equal
3055 "[2012-03-29 thu.]"
3056 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
3057 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
3058 (should
3059 (equal
3060 "[2012-03-29 thu.]"
3061 (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
3062 (cdr (assoc "CLOSED" (org-entry-properties))))))
3063 (should-not
3064 (org-test-with-temp-text "* H"
3065 (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
3066 (should
3067 (equal
3068 "<2014-03-04 tue.>"
3069 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3070 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
3071 (should
3072 (equal
3073 "<2014-03-04 tue.>"
3074 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3075 (cdr (assoc "DEADLINE" (org-entry-properties))))))
3076 (should-not
3077 (org-test-with-temp-text "* H"
3078 (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
3079 (should
3080 (equal
3081 "<2014-03-04 tue.>"
3082 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3083 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
3084 (should
3085 (equal
3086 "<2014-03-04 tue.>"
3087 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3088 (cdr (assoc "SCHEDULED" (org-entry-properties))))))
3089 (should-not
3090 (org-test-with-temp-text "* H"
3091 (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
3092 ;; Get "CATEGORY"
3093 (should
3094 (equal "cat"
3095 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
3096 (cdr (assoc "CATEGORY" (org-entry-properties))))))
3097 (should
3098 (equal "cat"
3099 (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
3100 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3101 (should
3102 (equal "cat"
3103 (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
3104 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3105 (should
3106 (equal "cat2"
3107 (org-test-with-temp-text
3108 (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
3109 "\n"
3110 "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
3111 (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
3112 ;; Get standard properties.
3113 (should
3114 (equal "1"
3115 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3116 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3117 ;; Handle extended properties.
3118 (should
3119 (equal "1 2 3"
3120 (org-test-with-temp-text
3121 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
3122 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3123 (should
3124 (equal "1 2 3"
3125 (org-test-with-temp-text
3126 "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
3127 (cdr (assoc "A" (org-entry-properties nil 'standard))))))
3128 ;; Ignore forbidden (special) properties.
3129 (should-not
3130 (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
3131 (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
3133 (ert-deftest test-org/entry-put ()
3134 "Test `org-entry-put' specifications."
3135 ;; Error when not a string or nil.
3136 (should-error
3137 (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
3138 (org-entry-put 1 "test" 2)))
3139 ;; Set "TODO" property.
3140 (should
3141 (string-match (regexp-quote " TODO H")
3142 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
3143 (org-entry-put (point) "TODO" "TODO")
3144 (buffer-string))))
3145 (should
3146 (string-match (regexp-quote "* H")
3147 (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
3148 (org-entry-put (point) "TODO" nil)
3149 (buffer-string))))
3150 ;; Set "PRIORITY" property.
3151 (should
3152 (equal "* [#A] H"
3153 (org-test-with-temp-text "* [#B] H"
3154 (org-entry-put (point) "PRIORITY" "A")
3155 (buffer-string))))
3156 (should
3157 (equal "* H"
3158 (org-test-with-temp-text "* [#B] H"
3159 (org-entry-put (point) "PRIORITY" nil)
3160 (buffer-string))))
3161 ;; Set "SCHEDULED" property.
3162 (should
3163 (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
3164 (org-test-with-temp-text "* H"
3165 (org-entry-put (point) "SCHEDULED" "2014-03-04")
3166 (buffer-string))))
3167 (should
3168 (string= "* H\n"
3169 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3170 (org-entry-put (point) "SCHEDULED" nil)
3171 (buffer-string))))
3172 (should
3173 (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
3174 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3175 (org-entry-put (point) "SCHEDULED" "earlier")
3176 (buffer-string))))
3177 (should
3178 (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
3179 (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
3180 (org-entry-put (point) "SCHEDULED" "later")
3181 (buffer-string))))
3182 ;; Set "DEADLINE" property.
3183 (should
3184 (string-match "^ *DEADLINE: <2014-03-04 .*?>"
3185 (org-test-with-temp-text "* H"
3186 (org-entry-put (point) "DEADLINE" "2014-03-04")
3187 (buffer-string))))
3188 (should
3189 (string= "* H\n"
3190 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3191 (org-entry-put (point) "DEADLINE" nil)
3192 (buffer-string))))
3193 (should
3194 (string-match "^ *DEADLINE: <2014-03-03 .*?>"
3195 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3196 (org-entry-put (point) "DEADLINE" "earlier")
3197 (buffer-string))))
3198 (should
3199 (string-match "^ *DEADLINE: <2014-03-05 .*?>"
3200 (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
3201 (org-entry-put (point) "DEADLINE" "later")
3202 (buffer-string))))
3203 ;; Set "CATEGORY" property
3204 (should
3205 (string-match "^ *:CATEGORY: cat"
3206 (org-test-with-temp-text "* H"
3207 (org-entry-put (point) "CATEGORY" "cat")
3208 (buffer-string))))
3209 ;; Regular properties, with or without pre-existing drawer.
3210 (should
3211 (string-match "^ *:A: +2$"
3212 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
3213 (org-entry-put (point) "A" "2")
3214 (buffer-string))))
3215 (should
3216 (string-match "^ *:A: +1$"
3217 (org-test-with-temp-text "* H"
3218 (org-entry-put (point) "A" "1")
3219 (buffer-string))))
3220 ;; Special case: two consecutive headlines.
3221 (should
3222 (string-match "\\* A\n *:PROPERTIES:"
3223 (org-test-with-temp-text "* A\n** B"
3224 (org-entry-put (point) "A" "1")
3225 (buffer-string)))))
3228 ;;; Radio Targets
3230 (ert-deftest test-org/update-radio-target-regexp ()
3231 "Test `org-update-radio-target-regexp' specifications."
3232 ;; Properly update cache with no previous radio target regexp.
3233 (should
3234 (eq 'link
3235 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3236 (save-excursion (goto-char (point-max)) (org-element-context))
3237 (insert "<<<")
3238 (search-forward "o")
3239 (insert ">>>")
3240 (org-update-radio-target-regexp)
3241 (goto-char (point-max))
3242 (org-element-type (org-element-context)))))
3243 ;; Properly update cache with previous radio target regexp.
3244 (should
3245 (eq 'link
3246 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
3247 (save-excursion (goto-char (point-max)) (org-element-context))
3248 (insert "<<<")
3249 (search-forward "o")
3250 (insert ">>>")
3251 (org-update-radio-target-regexp)
3252 (search-backward "r")
3253 (delete-char 5)
3254 (insert "new")
3255 (org-update-radio-target-regexp)
3256 (goto-char (point-max))
3257 (delete-region (line-beginning-position) (point))
3258 (insert "new")
3259 (org-element-type (org-element-context))))))
3262 ;;; Sparse trees
3264 (ert-deftest test-org/match-sparse-tree ()
3265 "Test `org-match-sparse-tree' specifications."
3266 ;; Match tags.
3267 (should-not
3268 (org-test-with-temp-text "* H\n** H1 :tag:"
3269 (org-match-sparse-tree nil "tag")
3270 (search-forward "H1")
3271 (org-invisible-p2)))
3272 (should
3273 (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
3274 (org-match-sparse-tree nil "tag")
3275 (search-forward "H2")
3276 (org-invisible-p2)))
3277 ;; "-" operator for tags.
3278 (should-not
3279 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3280 (org-match-sparse-tree nil "tag1-tag2")
3281 (search-forward "H1")
3282 (org-invisible-p2)))
3283 (should
3284 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3285 (org-match-sparse-tree nil "tag1-tag2")
3286 (search-forward "H2")
3287 (org-invisible-p2)))
3288 ;; "&" operator for tags.
3289 (should
3290 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3291 (org-match-sparse-tree nil "tag1&tag2")
3292 (search-forward "H1")
3293 (org-invisible-p2)))
3294 (should-not
3295 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3296 (org-match-sparse-tree nil "tag1&tag2")
3297 (search-forward "H2")
3298 (org-invisible-p2)))
3299 ;; "|" operator for tags.
3300 (should-not
3301 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3302 (org-match-sparse-tree nil "tag1|tag2")
3303 (search-forward "H1")
3304 (org-invisible-p2)))
3305 (should-not
3306 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
3307 (org-match-sparse-tree nil "tag1|tag2")
3308 (search-forward "H2")
3309 (org-invisible-p2)))
3310 ;; Regexp match on tags.
3311 (should-not
3312 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3313 (org-match-sparse-tree nil "{^tag.*}")
3314 (search-forward "H1")
3315 (org-invisible-p2)))
3316 (should
3317 (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
3318 (org-match-sparse-tree nil "{^tag.*}")
3319 (search-forward "H2")
3320 (org-invisible-p2)))
3321 ;; Match group tags.
3322 (should-not
3323 (org-test-with-temp-text
3324 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3325 (org-match-sparse-tree nil "work")
3326 (search-forward "H1")
3327 (org-invisible-p2)))
3328 (should-not
3329 (org-test-with-temp-text
3330 "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
3331 (org-match-sparse-tree nil "work")
3332 (search-forward "H2")
3333 (org-invisible-p2)))
3334 ;; Match group tags with hard brackets.
3335 (should-not
3336 (org-test-with-temp-text
3337 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
3338 (org-match-sparse-tree nil "work")
3339 (search-forward "H1")
3340 (org-invisible-p2)))
3341 (should-not
3342 (org-test-with-temp-text
3343 "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
3344 (org-match-sparse-tree nil "work")
3345 (search-forward "H2")
3346 (org-invisible-p2)))
3347 ;; Match tags in hierarchies
3348 (should-not
3349 (org-test-with-temp-text
3350 "#+TAGS: [ Lev_1 : Lev_2 ]\n
3351 #+TAGS: [ Lev_2 : Lev_3 ]\n
3352 #+TAGS: { Lev_3 : Lev_4 }\n
3353 * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
3354 (org-match-sparse-tree nil "Lev_1")
3355 (search-forward "H4")
3356 (org-invisible-p2)))
3357 ;; Match regular expressions in tags
3358 (should-not
3359 (org-test-with-temp-text
3360 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
3361 (org-match-sparse-tree nil "Lev")
3362 (search-forward "H1")
3363 (org-invisible-p2)))
3364 (should
3365 (org-test-with-temp-text
3366 "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
3367 (org-match-sparse-tree nil "Lev")
3368 (search-forward "H1")
3369 (org-invisible-p2)))
3370 ;; Match properties.
3371 (should
3372 (org-test-with-temp-text
3373 "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
3374 (org-match-sparse-tree nil "A=\"1\"")
3375 (search-forward "H2")
3376 (org-invisible-p2)))
3377 (should-not
3378 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3379 (org-match-sparse-tree nil "A=\"1\"")
3380 (search-forward "H2")
3381 (org-invisible-p2)))
3382 ;; Case is not significant when matching properties.
3383 (should-not
3384 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
3385 (org-match-sparse-tree nil "a=\"1\"")
3386 (search-forward "H2")
3387 (org-invisible-p2)))
3388 (should-not
3389 (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
3390 (org-match-sparse-tree nil "A=\"1\"")
3391 (search-forward "H2")
3392 (org-invisible-p2)))
3393 ;; Match special LEVEL property.
3394 (should-not
3395 (org-test-with-temp-text "* H\n** H1\n*** H2"
3396 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3397 (search-forward "H1")
3398 (org-invisible-p2)))
3399 (should
3400 (org-test-with-temp-text "* H\n** H1\n*** H2"
3401 (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
3402 (search-forward "H2")
3403 (org-invisible-p2)))
3404 ;; Comparison operators when matching properties.
3405 (should
3406 (org-test-with-temp-text
3407 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3408 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3409 (search-forward "H1")
3410 (org-invisible-p2)))
3411 (should-not
3412 (org-test-with-temp-text
3413 "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
3414 (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
3415 (search-forward "H2")
3416 (org-invisible-p2)))
3417 ;; Regexp match on properties values.
3418 (should-not
3419 (org-test-with-temp-text
3420 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3421 (org-match-sparse-tree nil "A={f.*}")
3422 (search-forward "H1")
3423 (org-invisible-p2)))
3424 (should
3425 (org-test-with-temp-text
3426 "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
3427 (org-match-sparse-tree nil "A={f.*}")
3428 (search-forward "H2")
3429 (org-invisible-p2)))
3430 ;; With an optional argument, limit match to TODO entries.
3431 (should-not
3432 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3433 (org-match-sparse-tree t "tag")
3434 (search-forward "H1")
3435 (org-invisible-p2)))
3436 (should
3437 (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
3438 (org-match-sparse-tree t "tag")
3439 (search-forward "H2")
3440 (org-invisible-p2))))
3443 ;;; Timestamps API
3445 (ert-deftest test-org/time-stamp ()
3446 "Test `org-time-stamp' specifications."
3447 ;; Insert chosen time stamp at point.
3448 (should
3449 (string-match
3450 "Te<2014-03-04 .*?>xt"
3451 (org-test-with-temp-text "Te<point>xt"
3452 (flet ((org-read-date
3453 (&rest args)
3454 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3455 (org-time-stamp nil)
3456 (buffer-string)))))
3457 ;; With a prefix argument, also insert time.
3458 (should
3459 (string-match
3460 "Te<2014-03-04 .*? 00:41>xt"
3461 (org-test-with-temp-text "Te<point>xt"
3462 (flet ((org-read-date
3463 (&rest args)
3464 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
3465 (org-time-stamp '(4))
3466 (buffer-string)))))
3467 ;; With two universal prefix arguments, insert an active timestamp
3468 ;; with the current time without prompting the user.
3469 (should
3470 (string-match
3471 "Te<2014-03-04 .*? 00:41>xt"
3472 (org-test-with-temp-text "Te<point>xt"
3473 (flet ((current-time
3475 (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
3476 (org-time-stamp '(16))
3477 (buffer-string)))))
3478 ;; When optional argument is non-nil, insert an inactive timestamp.
3479 (should
3480 (string-match
3481 "Te\\[2014-03-04 .*?\\]xt"
3482 (org-test-with-temp-text "Te<point>xt"
3483 (flet ((org-read-date
3484 (&rest args)
3485 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3486 (org-time-stamp nil t)
3487 (buffer-string)))))
3488 ;; When called from a timestamp, replace existing one.
3489 (should
3490 (string-match
3491 "<2014-03-04 .*?>"
3492 (org-test-with-temp-text "<2012-03-29<point> thu.>"
3493 (flet ((org-read-date
3494 (&rest args)
3495 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3496 (org-time-stamp nil)
3497 (buffer-string)))))
3498 (should
3499 (string-match
3500 "<2014-03-04 .*?>--<2014-03-04 .*?>"
3501 (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
3502 (flet ((org-read-date
3503 (&rest args)
3504 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3505 (org-time-stamp nil)
3506 (buffer-string)))))
3507 ;; When replacing a timestamp, preserve repeater, if any.
3508 (should
3509 (string-match
3510 "<2014-03-04 .*? \\+2y>"
3511 (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
3512 (flet ((org-read-date
3513 (&rest args)
3514 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3515 (org-time-stamp nil)
3516 (buffer-string)))))
3517 ;; When called twice in a raw, build a date range.
3518 (should
3519 (string-match
3520 "<2012-03-29 .*?>--<2014-03-04 .*?>"
3521 (org-test-with-temp-text "<2012-03-29 thu.><point>"
3522 (flet ((org-read-date
3523 (&rest args)
3524 (apply #'encode-time (org-parse-time-string "2014-03-04"))))
3525 (let ((last-command 'org-time-stamp)
3526 (this-command 'org-time-stamp))
3527 (org-time-stamp nil))
3528 (buffer-string))))))
3530 (ert-deftest test-org/timestamp-has-time-p ()
3531 "Test `org-timestamp-has-time-p' specifications."
3532 ;; With time.
3533 (should
3534 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
3535 (org-timestamp-has-time-p (org-element-context))))
3536 ;; Without time.
3537 (should-not
3538 (org-test-with-temp-text "<2012-03-29 Thu>"
3539 (org-timestamp-has-time-p (org-element-context)))))
3541 (ert-deftest test-org/timestamp-format ()
3542 "Test `org-timestamp-format' specifications."
3543 ;; Regular test.
3544 (should
3545 (equal
3546 "2012-03-29 16:40"
3547 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
3548 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
3549 ;; Range end.
3550 (should
3551 (equal
3552 "2012-03-29"
3553 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
3554 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
3556 (ert-deftest test-org/timestamp-split-range ()
3557 "Test `org-timestamp-split-range' specifications."
3558 ;; Extract range start (active).
3559 (should
3560 (equal '(2012 3 29)
3561 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3562 (let ((ts (org-timestamp-split-range (org-element-context))))
3563 (mapcar (lambda (p) (org-element-property p ts))
3564 '(:year-end :month-end :day-end))))))
3565 ;; Extract range start (inactive)
3566 (should
3567 (equal '(2012 3 29)
3568 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
3569 (let ((ts (org-timestamp-split-range (org-element-context))))
3570 (mapcar (lambda (p) (org-element-property p ts))
3571 '(:year-end :month-end :day-end))))))
3572 ;; Extract range end (active).
3573 (should
3574 (equal '(2012 3 30)
3575 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3576 (let ((ts (org-timestamp-split-range
3577 (org-element-context) t)))
3578 (mapcar (lambda (p) (org-element-property p ts))
3579 '(:year-end :month-end :day-end))))))
3580 ;; Extract range end (inactive)
3581 (should
3582 (equal '(2012 3 30)
3583 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
3584 (let ((ts (org-timestamp-split-range
3585 (org-element-context) t)))
3586 (mapcar (lambda (p) (org-element-property p ts))
3587 '(:year-end :month-end :day-end))))))
3588 ;; Return the timestamp if not a range.
3589 (should
3590 (org-test-with-temp-text "[2012-03-29 Thu]"
3591 (let* ((ts-orig (org-element-context))
3592 (ts-copy (org-timestamp-split-range ts-orig)))
3593 (eq ts-orig ts-copy))))
3594 (should
3595 (org-test-with-temp-text "<%%(org-float t 4 2)>"
3596 (let* ((ts-orig (org-element-context))
3597 (ts-copy (org-timestamp-split-range ts-orig)))
3598 (eq ts-orig ts-copy)))))
3600 (ert-deftest test-org/timestamp-translate ()
3601 "Test `org-timestamp-translate' specifications."
3602 ;; Translate whole date range.
3603 (should
3604 (equal "<29>--<30>"
3605 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3606 (let ((org-display-custom-times t)
3607 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3608 (org-timestamp-translate (org-element-context))))))
3609 ;; Translate date range start.
3610 (should
3611 (equal "<29>"
3612 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3613 (let ((org-display-custom-times t)
3614 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3615 (org-timestamp-translate (org-element-context) 'start)))))
3616 ;; Translate date range end.
3617 (should
3618 (equal "<30>"
3619 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
3620 (let ((org-display-custom-times t)
3621 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3622 (org-timestamp-translate (org-element-context) 'end)))))
3623 ;; Translate time range.
3624 (should
3625 (equal "<08>--<16>"
3626 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
3627 (let ((org-display-custom-times t)
3628 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
3629 (org-timestamp-translate (org-element-context))))))
3630 ;; Translate non-range timestamp.
3631 (should
3632 (equal "<29>"
3633 (org-test-with-temp-text "<2012-03-29 Thu>"
3634 (let ((org-display-custom-times t)
3635 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3636 (org-timestamp-translate (org-element-context))))))
3637 ;; Do not change `diary' timestamps.
3638 (should
3639 (equal "<%%(org-float t 4 2)>"
3640 (org-test-with-temp-text "<%%(org-float t 4 2)>"
3641 (let ((org-display-custom-times t)
3642 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
3643 (org-timestamp-translate (org-element-context)))))))
3647 ;;; Visibility
3649 (ert-deftest test-org/flag-drawer ()
3650 "Test `org-flag-drawer' specifications."
3651 ;; Hide drawer.
3652 (should
3653 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3654 (org-flag-drawer t)
3655 (get-char-property (line-end-position) 'invisible)))
3656 ;; Show drawer.
3657 (should-not
3658 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3659 (org-flag-drawer t)
3660 (org-flag-drawer nil)
3661 (get-char-property (line-end-position) 'invisible)))
3662 ;; Test optional argument.
3663 (should
3664 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
3665 (let ((drawer (save-excursion (search-forward ":D2")
3666 (org-element-at-point))))
3667 (org-flag-drawer t drawer)
3668 (get-char-property (progn (search-forward ":D2") (line-end-position))
3669 'invisible))))
3670 (should-not
3671 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
3672 (let ((drawer (save-excursion (search-forward ":D2")
3673 (org-element-at-point))))
3674 (org-flag-drawer t drawer)
3675 (get-char-property (line-end-position) 'invisible))))
3676 ;; Do not hide fake drawers.
3677 (should-not
3678 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
3679 (forward-line 1)
3680 (org-flag-drawer t)
3681 (get-char-property (line-end-position) 'invisible)))
3682 ;; Do not hide incomplete drawers.
3683 (should-not
3684 (org-test-with-temp-text ":D:\nparagraph"
3685 (forward-line 1)
3686 (org-flag-drawer t)
3687 (get-char-property (line-end-position) 'invisible)))
3688 ;; Do not hide drawers when called from final blank lines.
3689 (should-not
3690 (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
3691 (goto-char (point-max))
3692 (org-flag-drawer t)
3693 (goto-char (point-min))
3694 (get-char-property (line-end-position) 'invisible)))
3695 ;; Don't leave point in an invisible part of the buffer when hiding
3696 ;; a drawer away.
3697 (should-not
3698 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
3699 (goto-char (point-max))
3700 (org-flag-drawer t)
3701 (get-char-property (point) 'invisible))))
3703 (ert-deftest test-org/hide-block-toggle ()
3704 "Test `org-hide-block-toggle' specifications."
3705 ;; Error when not at a block.
3706 (should-error
3707 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
3708 (org-hide-block-toggle 'off)
3709 (get-char-property (line-end-position) 'invisible)))
3710 ;; Hide block.
3711 (should
3712 (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
3713 (org-hide-block-toggle)
3714 (get-char-property (line-end-position) 'invisible)))
3715 (should
3716 (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
3717 (org-hide-block-toggle)
3718 (get-char-property (line-end-position) 'invisible)))
3719 ;; Show block unconditionally when optional argument is `off'.
3720 (should-not
3721 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3722 (org-hide-block-toggle)
3723 (org-hide-block-toggle 'off)
3724 (get-char-property (line-end-position) 'invisible)))
3725 (should-not
3726 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3727 (org-hide-block-toggle 'off)
3728 (get-char-property (line-end-position) 'invisible)))
3729 ;; Hide block unconditionally when optional argument is non-nil.
3730 (should
3731 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3732 (org-hide-block-toggle t)
3733 (get-char-property (line-end-position) 'invisible)))
3734 (should
3735 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
3736 (org-hide-block-toggle)
3737 (org-hide-block-toggle t)
3738 (get-char-property (line-end-position) 'invisible)))
3739 ;; Do not hide block when called from final blank lines.
3740 (should-not
3741 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
3742 (org-hide-block-toggle)
3743 (goto-char (point-min))
3744 (get-char-property (line-end-position) 'invisible)))
3745 ;; Don't leave point in an invisible part of the buffer when hiding
3746 ;; a block away.
3747 (should-not
3748 (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
3749 (org-hide-block-toggle)
3750 (get-char-property (point) 'invisible))))
3752 (ert-deftest test-org/hide-block-toggle-maybe ()
3753 "Test `org-hide-block-toggle-maybe' specifications."
3754 (should
3755 (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
3756 (org-hide-block-toggle-maybe)))
3757 (should-not
3758 (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
3760 (ert-deftest test-org/show-set-visibility ()
3761 "Test `org-show-set-visibility' specifications."
3762 ;; Do not throw an error before first heading.
3763 (should
3764 (org-test-with-temp-text "Preamble\n* Headline"
3765 (org-show-set-visibility 'tree)
3767 ;; Test all visibility spans, both on headline and in entry.
3768 (let ((list-visible-lines
3769 (lambda (state headerp)
3770 (org-test-with-temp-text "* Grandmother (0)
3771 ** Uncle (1)
3772 *** Heir (2)
3773 ** Father (3)
3774 Ancestor text (4)
3775 *** Sister (5)
3776 Sibling text (6)
3777 *** Self (7)
3778 Match (8)
3779 **** First born (9)
3780 Child text (10)
3781 **** The other child (11)
3782 *** Brother (12)
3783 ** Aunt (13)
3785 (org-cycle t)
3786 (search-forward (if headerp "Self" "Match"))
3787 (org-show-set-visibility state)
3788 (goto-char (point-min))
3789 (let (result (line 0))
3790 (while (not (eobp))
3791 (unless (org-invisible-p2) (push line result))
3792 (incf line)
3793 (forward-line))
3794 (nreverse result))))))
3795 (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
3796 (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
3797 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
3798 (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
3799 (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
3800 (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
3801 (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
3802 (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
3803 (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
3804 (should (equal '(0 1 3 5 7 8 9 11 12 13)
3805 (funcall list-visible-lines 'tree nil)))
3806 (should (equal '(0 1 3 4 5 7 12 13)
3807 (funcall list-visible-lines 'canonical t)))
3808 (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
3809 (funcall list-visible-lines 'canonical nil)))))
3812 (provide 'test-org)
3814 ;;; test-org.el ends here