Replace org-babel-*-graphical-output-file with generic version
[org-mode.git] / testing / lisp / test-org.el
blob069beb989bc728ec60356225d013682298e89963
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")))))
196 (ert-deftest test-org/org-parse-time-string ()
197 "Test `org-parse-time-string'."
198 (should (equal (org-parse-time-string "2012-03-29 16:40")
199 '(0 40 16 29 3 2012 nil nil nil)))
200 (should (equal (org-parse-time-string "[2012-03-29 16:40]")
201 '(0 40 16 29 3 2012 nil nil nil)))
202 (should (equal (org-parse-time-string "<2012-03-29 16:40>")
203 '(0 40 16 29 3 2012 nil nil nil)))
204 (should (equal (org-parse-time-string "<2012-03-29>")
205 '(0 0 0 29 3 2012 nil nil nil)))
206 (should (equal (org-parse-time-string "<2012-03-29>" t)
207 '(0 nil nil 29 3 2012 nil nil nil))))
210 ;;; Filling
212 (ert-deftest test-org/fill-paragraph ()
213 "Test `org-fill-paragraph' specifications."
214 ;; At an Org table, align it.
215 (should
216 (equal "| a |\n"
217 (org-test-with-temp-text "|a|"
218 (org-fill-paragraph)
219 (buffer-string))))
220 (should
221 (equal "#+name: table\n| a |\n"
222 (org-test-with-temp-text "#+name: table\n| a |"
223 (org-fill-paragraph)
224 (buffer-string))))
225 ;; At a paragraph, preserve line breaks.
226 (org-test-with-temp-text "some \\\\\nlong\ntext"
227 (let ((fill-column 20))
228 (org-fill-paragraph)
229 (should (equal (buffer-string) "some \\\\\nlong text"))))
230 ;; Correctly fill a paragraph when point is at its very end.
231 (should
232 (equal "A B"
233 (org-test-with-temp-text "A\nB"
234 (let ((fill-column 20))
235 (goto-char (point-max))
236 (org-fill-paragraph)
237 (buffer-string)))))
238 ;; Correctly fill the last paragraph of a greater element.
239 (should
240 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
241 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
242 (let ((fill-column 8))
243 (forward-line)
244 (end-of-line)
245 (org-fill-paragraph)
246 (buffer-string)))))
247 ;; Correctly fill an element in a narrowed buffer.
248 (should
249 (equal "01234\n6"
250 (org-test-with-temp-text "01234 6789"
251 (let ((fill-column 5))
252 (narrow-to-region 1 8)
253 (org-fill-paragraph)
254 (buffer-string)))))
255 ;; Handle `adaptive-fill-regexp' in paragraphs.
256 (should
257 (equal "> a b"
258 (org-test-with-temp-text "> a\n> b"
259 (let ((fill-column 5)
260 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
261 (org-fill-paragraph)
262 (buffer-string)))))
263 ;; Special case: Fill first paragraph when point is at an item or
264 ;; a plain-list or a footnote reference.
265 (should
266 (equal "- A B"
267 (org-test-with-temp-text "- A\n B"
268 (let ((fill-column 20))
269 (org-fill-paragraph)
270 (buffer-string)))))
271 (should
272 (equal "[fn:1] A B"
273 (org-test-with-temp-text "[fn:1] A\nB"
274 (let ((fill-column 20))
275 (org-fill-paragraph)
276 (buffer-string)))))
277 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
278 (let ((fill-column 20))
279 (org-fill-paragraph)
280 (should (equal (buffer-string)
281 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
282 ;; Fill contents of `comment-block' elements.
283 (should
284 (equal
285 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
286 (let ((fill-column 20))
287 (forward-line)
288 (org-fill-paragraph)
289 (buffer-string)))
290 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
291 ;; Fill `comment' elements.
292 (should
293 (equal " # A B"
294 (org-test-with-temp-text " # A\n # B"
295 (let ((fill-column 20))
296 (org-fill-paragraph)
297 (buffer-string)))))
298 ;; Do not mix consecutive comments when filling one of them.
299 (should
300 (equal "# A B\n\n# C"
301 (org-test-with-temp-text "# A\n# B\n\n# C"
302 (let ((fill-column 20))
303 (org-fill-paragraph)
304 (buffer-string)))))
305 ;; Use commented empty lines as separators when filling comments.
306 (should
307 (equal "# A B\n#\n# C"
308 (org-test-with-temp-text "# A\n# B\n#\n# C"
309 (let ((fill-column 20))
310 (org-fill-paragraph)
311 (buffer-string)))))
312 ;; Handle `adaptive-fill-regexp' in comments.
313 (should
314 (equal "# > a b"
315 (org-test-with-temp-text "# > a\n# > b"
316 (let ((fill-column 20)
317 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
318 (org-fill-paragraph)
319 (buffer-string)))))
320 ;; Do nothing at affiliated keywords.
321 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
322 (let ((fill-column 20))
323 (org-fill-paragraph)
324 (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
325 ;; Do not move point after table when filling a table.
326 (should-not
327 (org-test-with-temp-text "| a | b |\n| c | d |\n"
328 (forward-char)
329 (org-fill-paragraph)
330 (eobp))))
332 (ert-deftest test-org/auto-fill-function ()
333 "Test auto-filling features."
334 ;; Auto fill paragraph.
335 (should
336 (equal "12345\n7890"
337 (org-test-with-temp-text "12345 7890"
338 (let ((fill-column 5))
339 (end-of-line)
340 (org-auto-fill-function)
341 (buffer-string)))))
342 ;; Auto fill first paragraph in an item.
343 (should
344 (equal "- 12345\n 7890"
345 (org-test-with-temp-text "- 12345 7890"
346 (let ((fill-column 7))
347 (end-of-line)
348 (org-auto-fill-function)
349 (buffer-string)))))
350 ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
351 (should
352 (equal "> 12345\n 7890"
353 (org-test-with-temp-text "> 12345 7890"
354 (let ((fill-column 10)
355 (adaptive-fill-regexp "[ \t]*>+[ \t]*")
356 (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
357 (end-of-line)
358 (org-auto-fill-function)
359 (buffer-string)))))
360 (should
361 (equal "> 12345\n> 12345\n> 7890"
362 (org-test-with-temp-text "> 12345\n> 12345 7890"
363 (let ((fill-column 10)
364 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
365 (goto-char (point-max))
366 (org-auto-fill-function)
367 (buffer-string)))))
368 (should-not
369 (equal " 12345\n *12345\n *12345"
370 (org-test-with-temp-text " 12345\n *12345 12345"
371 (let ((fill-column 10)
372 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
373 (goto-char (point-max))
374 (org-auto-fill-function)
375 (buffer-string)))))
376 ;; Auto fill comments.
377 (should
378 (equal " # 12345\n # 7890"
379 (org-test-with-temp-text " # 12345 7890"
380 (let ((fill-column 10))
381 (end-of-line)
382 (org-auto-fill-function)
383 (buffer-string)))))
384 ;; A hash within a line isn't a comment.
385 (should-not
386 (equal "12345 # 7890\n# 1"
387 (org-test-with-temp-text "12345 # 7890 1"
388 (let ((fill-column 12))
389 (end-of-line)
390 (org-auto-fill-function)
391 (buffer-string)))))
392 ;; Correctly interpret empty prefix.
393 (should-not
394 (equal "# a\n# b\nRegular\n# paragraph"
395 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
396 (let ((fill-column 12))
397 (end-of-line 3)
398 (org-auto-fill-function)
399 (buffer-string)))))
400 ;; Comment block: auto fill contents.
401 (should
402 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
403 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
404 (let ((fill-column 5))
405 (forward-line)
406 (end-of-line)
407 (org-auto-fill-function)
408 (buffer-string)))))
409 (should
410 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
411 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
412 (let ((fill-column 5))
413 (forward-line)
414 (end-of-line)
415 (org-auto-fill-function)
416 (buffer-string)))))
417 ;; Do not fill if a new item could be created.
418 (should-not
419 (equal "12345\n- 90"
420 (org-test-with-temp-text "12345 - 90"
421 (let ((fill-column 5))
422 (end-of-line)
423 (org-auto-fill-function)
424 (buffer-string)))))
425 ;; Do not fill if a line break could be introduced.
426 (should-not
427 (equal "123\\\\\n7890"
428 (org-test-with-temp-text "123\\\\ 7890"
429 (let ((fill-column 6))
430 (end-of-line)
431 (org-auto-fill-function)
432 (buffer-string)))))
433 ;; Do not fill affiliated keywords.
434 (should-not
435 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
436 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
437 (let ((fill-column 20))
438 (end-of-line)
439 (org-auto-fill-function)
440 (buffer-string))))))
444 ;;; Editing
446 ;;;; Insert elements
448 (ert-deftest test-org/meta-return ()
449 "Test M-RET (`org-meta-return')."
450 ;; In a table field insert a row above.
451 (should
452 (org-test-with-temp-text "| a |"
453 (forward-char)
454 (org-meta-return)
455 (forward-line -1)
456 (looking-at "| |$")))
457 ;; In a paragraph change current line into a header.
458 (should
459 (org-test-with-temp-text "a"
460 (org-meta-return)
461 (beginning-of-line)
462 (looking-at "\* a$")))
463 ;; In an item insert an item, in this case above.
464 (should
465 (org-test-with-temp-text "- a"
466 (org-meta-return)
467 (beginning-of-line)
468 (looking-at "- $")))
469 ;; In a drawer and paragraph insert an empty line, in this case above.
470 (should
471 (org-test-with-temp-text ":MYDRAWER:\na\n:END:"
472 (forward-line)
473 (org-meta-return)
474 (forward-line -1)
475 (looking-at "$")))
476 ;; In a drawer and item insert an item, in this case above.
477 (should
478 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
479 (forward-line)
480 (org-meta-return)
481 (beginning-of-line)
482 (looking-at "- $"))))
484 (ert-deftest test-org/insert-todo-heading-respect-content ()
485 "Test `org-insert-todo-heading-respect-content' specifications."
486 ;; Create a TODO heading.
487 (should
488 (org-test-with-temp-text "* H1\n Body"
489 (org-insert-todo-heading-respect-content)
490 (nth 2 (org-heading-components))))
491 ;; Add headline at the end of the first subtree
492 (should
493 (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
494 (search-forward "H1Body")
495 (org-insert-todo-heading-respect-content)
496 (and (eobp) (org-at-heading-p))))
497 ;; In a list, do not create a new item.
498 (should
499 (org-test-with-temp-text "* H\n- an item\n- another one"
500 (search-forward "an ")
501 (org-insert-todo-heading-respect-content)
502 (and (eobp) (org-at-heading-p)))))
506 ;;; Fixed-Width Areas
508 (ert-deftest test-org/toggle-fixed-with ()
509 "Test `org-toggle-fixed-width' specifications."
510 ;; No region: Toggle on fixed-width marker in paragraphs.
511 (should
512 (equal ": A"
513 (org-test-with-temp-text "A"
514 (org-toggle-fixed-width)
515 (buffer-string))))
516 ;; No region: Toggle off fixed-width markers in fixed-width areas.
517 (should
518 (equal "A"
519 (org-test-with-temp-text ": A"
520 (org-toggle-fixed-width)
521 (buffer-string))))
522 ;; No region: Toggle on marker in blank lines after elements or just
523 ;; after a headline.
524 (should
525 (equal "* H\n: "
526 (org-test-with-temp-text "* H\n"
527 (forward-line)
528 (org-toggle-fixed-width)
529 (buffer-string))))
530 (should
531 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
532 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
533 (goto-char (point-max))
534 (org-toggle-fixed-width)
535 (buffer-string))))
536 ;; No region: Toggle on marker in front of one line elements (e.g.,
537 ;; headlines, clocks)
538 (should
539 (equal ": * Headline"
540 (org-test-with-temp-text "* Headline"
541 (org-toggle-fixed-width)
542 (buffer-string))))
543 (should
544 (equal ": #+KEYWORD: value"
545 (org-test-with-temp-text "#+KEYWORD: value"
546 (org-toggle-fixed-width)
547 (buffer-string))))
548 ;; No region: error in other situations.
549 (should-error
550 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
551 (forward-line)
552 (org-toggle-fixed-width)
553 (buffer-string)))
554 ;; No region: Indentation is preserved.
555 (should
556 (equal "- A\n : B"
557 (org-test-with-temp-text "- A\n B"
558 (forward-line)
559 (org-toggle-fixed-width)
560 (buffer-string))))
561 ;; Region: If it contains only fixed-width elements and blank lines,
562 ;; toggle off fixed-width markup.
563 (should
564 (equal "A\n\nB"
565 (org-test-with-temp-text ": A\n\n: B"
566 (transient-mark-mode 1)
567 (push-mark (point) t t)
568 (goto-char (point-max))
569 (org-toggle-fixed-width)
570 (buffer-string))))
571 ;; Region: If it contains anything else, toggle on fixed-width but
572 ;; not on fixed-width areas.
573 (should
574 (equal ": A\n: \n: B\n: \n: C"
575 (org-test-with-temp-text "A\n\n: B\n\nC"
576 (transient-mark-mode 1)
577 (push-mark (point) t t)
578 (goto-char (point-max))
579 (org-toggle-fixed-width)
580 (buffer-string))))
581 ;; Region: Ignore blank lines at its end, unless it contains only
582 ;; such lines.
583 (should
584 (equal ": A\n\n"
585 (org-test-with-temp-text "A\n\n"
586 (transient-mark-mode 1)
587 (push-mark (point) t t)
588 (goto-char (point-max))
589 (org-toggle-fixed-width)
590 (buffer-string))))
591 (should
592 (equal ": \n: \n"
593 (org-test-with-temp-text "\n\n"
594 (transient-mark-mode 1)
595 (push-mark (point) t t)
596 (goto-char (point-max))
597 (org-toggle-fixed-width)
598 (buffer-string)))))
602 ;;; Headline
604 (ert-deftest test-org/in-commented-heading-p ()
605 "Test `org-in-commented-heading-p' specifications."
606 ;; Commented headline.
607 (should
608 (org-test-with-temp-text "* COMMENT Headline\nBody"
609 (goto-char (point-max))
610 (org-in-commented-heading-p)))
611 ;; Commented ancestor.
612 (should
613 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
614 (goto-char (point-max))
615 (org-in-commented-heading-p)))
616 ;; Comment keyword is case-sensitive.
617 (should-not
618 (org-test-with-temp-text "* Comment Headline\nBody"
619 (goto-char (point-max))
620 (org-in-commented-heading-p)))
621 ;; Keyword is standalone.
622 (should-not
623 (org-test-with-temp-text "* COMMENTHeadline\nBody"
624 (goto-char (point-max))
625 (org-in-commented-heading-p)))
626 ;; Optional argument.
627 (should-not
628 (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
629 (goto-char (point-max))
630 (org-in-commented-heading-p t))))
634 ;;; Links
636 ;;;; Coderefs
638 (ert-deftest test-org/coderef ()
639 "Test coderef links specifications."
640 (should
641 (org-test-with-temp-text "
642 #+BEGIN_SRC emacs-lisp
643 \(+ 1 1) (ref:sc)
644 #+END_SRC
645 \[[(sc)]]"
646 (goto-char (point-max))
647 (org-open-at-point)
648 (looking-at "(ref:sc)"))))
650 ;;;; Custom ID
652 (ert-deftest test-org/custom-id ()
653 "Test custom ID links specifications."
654 (should
655 (org-test-with-temp-text
656 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
657 (goto-char (point-max))
658 (org-open-at-point)
659 (org-looking-at-p "\\* H1"))))
661 ;;;; Fuzzy Links
663 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
664 ;; a named element (#+name: text) and to headlines (* Text).
666 (ert-deftest test-org/fuzzy-links ()
667 "Test fuzzy links specifications."
668 ;; Fuzzy link goes in priority to a matching target.
669 (should
670 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
671 (goto-line 5)
672 (org-open-at-point)
673 (looking-at "<<Test>>")))
674 ;; Then fuzzy link points to an element with a given name.
675 (should
676 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
677 (goto-line 5)
678 (org-open-at-point)
679 (looking-at "#\\+NAME: Test")))
680 ;; A target still lead to a matching headline otherwise.
681 (should
682 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
683 (goto-line 4)
684 (org-open-at-point)
685 (looking-at "\\* Head2")))
686 ;; With a leading star in link, enforce heading match.
687 (should
688 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
689 (goto-line 3)
690 (org-open-at-point)
691 (looking-at "\\* Test")))
692 ;; Correctly un-hexify fuzzy links.
693 (should
694 (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
695 (goto-char (point-max))
696 (org-open-at-point)
697 (bobp))))
700 ;;;; Link Escaping
702 (ert-deftest test-org/org-link-escape-ascii-character ()
703 "Escape an ascii character."
704 (should
705 (string=
706 "%5B"
707 (org-link-escape "["))))
709 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
710 "Escape an ascii control character."
711 (should
712 (string=
713 "%09"
714 (org-link-escape "\t"))))
716 (ert-deftest test-org/org-link-escape-multibyte-character ()
717 "Escape an unicode multibyte character."
718 (should
719 (string=
720 "%E2%82%AC"
721 (org-link-escape "€"))))
723 (ert-deftest test-org/org-link-escape-custom-table ()
724 "Escape string with custom character table."
725 (should
726 (string=
727 "Foo%3A%42ar%0A"
728 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
730 (ert-deftest test-org/org-link-escape-custom-table-merge ()
731 "Escape string with custom table merged with default table."
732 (should
733 (string=
734 "%5BF%6F%6F%3A%42ar%0A%5D"
735 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
737 (ert-deftest test-org/org-link-unescape-ascii-character ()
738 "Unescape an ascii character."
739 (should
740 (string=
742 (org-link-unescape "%5B"))))
744 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
745 "Unescpae an ascii control character."
746 (should
747 (string=
748 "\n"
749 (org-link-unescape "%0A"))))
751 (ert-deftest test-org/org-link-unescape-multibyte-character ()
752 "Unescape unicode multibyte character."
753 (should
754 (string=
755 "€"
756 (org-link-unescape "%E2%82%AC"))))
758 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
759 "Unescape old style percent escaped character."
760 (should
761 (string=
762 "àâçèéêîôùû"
763 (decode-coding-string
764 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
766 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
767 "Escape and unescape a URL that includes an escaped char.
768 http://article.gmane.org/gmane.emacs.orgmode/21459/"
769 (should
770 (string=
771 "http://some.host.com/form?&id=blah%2Bblah25"
772 (org-link-unescape
773 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
775 (ert-deftest test-org/org-link-escape-chars-browser ()
776 "Test of the constant `org-link-escape-chars-browser'.
777 See there why this test is a candidate to be removed once Org
778 drops support for Emacs 24.1 and 24.2."
779 (should
780 (string=
781 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
782 "%22Release%208.2%22&idxname=emacs-orgmode")
783 (org-link-escape-browser ; Do not replace with `url-encode-url',
784 ; see docstring above.
785 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
786 "\"Release 8.2\"&idxname=emacs-orgmode")))))
790 ;;; Node Properties
792 (ert-deftest test-org/accumulated-properties-in-drawers ()
793 "Ensure properties accumulate in subtree drawers."
794 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
795 (org-babel-next-src-block)
796 (should (equal '(2 1) (org-babel-execute-src-block)))))
800 ;;; Mark Region
802 (ert-deftest test-org/mark-subtree ()
803 "Test `org-mark-subtree' specifications."
804 ;; Error when point is before first headline.
805 (should-error
806 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
807 (progn (transient-mark-mode 1)
808 (org-mark-subtree))))
809 ;; Without argument, mark current subtree.
810 (should
811 (equal
812 '(12 32)
813 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
814 (progn (transient-mark-mode 1)
815 (forward-line 2)
816 (org-mark-subtree)
817 (list (region-beginning) (region-end))))))
818 ;; With an argument, move ARG up.
819 (should
820 (equal
821 '(1 32)
822 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
823 (progn (transient-mark-mode 1)
824 (forward-line 2)
825 (org-mark-subtree 1)
826 (list (region-beginning) (region-end))))))
827 ;; Do not get fooled by inlinetasks.
828 (when (featurep 'org-inlinetask)
829 (should
830 (= 1
831 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
832 (progn (transient-mark-mode 1)
833 (forward-line 1)
834 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
835 (region-beginning)))))))
839 ;;; Navigation
841 (ert-deftest test-org/beginning-of-line ()
842 "Test `org-beginning-of-line' specifications."
843 ;; Standard test.
844 (should
845 (org-test-with-temp-text "Some text\nSome other text"
846 (progn (org-beginning-of-line) (bolp))))
847 ;; Standard test with `visual-line-mode'.
848 (should-not
849 (org-test-with-temp-text "A long line of text\nSome other text"
850 (progn (visual-line-mode)
851 (forward-char 2)
852 (dotimes (i 1000) (insert "very "))
853 (org-beginning-of-line)
854 (bolp))))
855 ;; At an headline with special movement.
856 (should
857 (org-test-with-temp-text "* TODO Headline"
858 (let ((org-special-ctrl-a/e t))
859 (org-end-of-line)
860 (and (progn (org-beginning-of-line) (looking-at "Headline"))
861 (progn (org-beginning-of-line) (bolp))
862 (progn (org-beginning-of-line) (looking-at "Headline")))))))
864 (ert-deftest test-org/end-of-line ()
865 "Test `org-end-of-line' specifications."
866 ;; Standard test.
867 (should
868 (org-test-with-temp-text "Some text\nSome other text"
869 (progn (org-end-of-line) (eolp))))
870 ;; Standard test with `visual-line-mode'.
871 (should-not
872 (org-test-with-temp-text "A long line of text\nSome other text"
873 (progn (visual-line-mode)
874 (forward-char 2)
875 (dotimes (i 1000) (insert "very "))
876 (goto-char (point-min))
877 (org-end-of-line)
878 (eolp))))
879 ;; At an headline with special movement.
880 (should
881 (org-test-with-temp-text "* Headline1 :tag:\n"
882 (let ((org-special-ctrl-a/e t))
883 (and (progn (org-end-of-line) (looking-at " :tag:"))
884 (progn (org-end-of-line) (eolp))
885 (progn (org-end-of-line) (looking-at " :tag:"))))))
886 ;; At an headline without special movement.
887 (should
888 (org-test-with-temp-text "* Headline2 :tag:\n"
889 (let ((org-special-ctrl-a/e nil))
890 (and (progn (org-end-of-line) (eolp))
891 (progn (org-end-of-line) (eolp))))))
892 ;; At an headline, with reversed movement.
893 (should
894 (org-test-with-temp-text "* Headline3 :tag:\n"
895 (let ((org-special-ctrl-a/e 'reversed)
896 (this-command last-command))
897 (and (progn (org-end-of-line) (eolp))
898 (progn (org-end-of-line) (looking-at " :tag:"))))))
899 ;; At a block without hidden contents.
900 (should
901 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
902 (progn (org-end-of-line) (eolp))))
903 ;; At a block with hidden contents.
904 (should-not
905 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
906 (let ((org-special-ctrl-a/e t))
907 (org-hide-block-toggle)
908 (org-end-of-line)
909 (eobp)))))
911 (ert-deftest test-org/forward-paragraph ()
912 "Test `org-forward-paragraph' specifications."
913 ;; At end of buffer, return an error.
914 (should-error
915 (org-test-with-temp-text "Paragraph"
916 (goto-char (point-max))
917 (org-forward-paragraph)))
918 ;; Standard test.
919 (should
920 (org-test-with-temp-text "P1\n\nP2\n\nP3"
921 (org-forward-paragraph)
922 (looking-at "P2")))
923 ;; Ignore depth.
924 (should
925 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
926 (org-forward-paragraph)
927 (looking-at "P1")))
928 ;; Do not enter elements with invisible contents.
929 (should
930 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
931 (org-hide-block-toggle)
932 (org-forward-paragraph)
933 (looking-at "P3")))
934 ;; On an affiliated keyword, jump to the beginning of the element.
935 (should
936 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
937 (org-forward-paragraph)
938 (looking-at "Para")))
939 ;; On an item or a footnote definition, move to the second element
940 ;; inside, if any.
941 (should
942 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
943 (org-forward-paragraph)
944 (looking-at " Paragraph")))
945 (should
946 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
947 (org-forward-paragraph)
948 (looking-at "Paragraph")))
949 ;; On an item, or a footnote definition, when the first line is
950 ;; empty, move to the first item.
951 (should
952 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
953 (org-forward-paragraph)
954 (looking-at " Paragraph")))
955 (should
956 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
957 (org-forward-paragraph)
958 (looking-at "Paragraph")))
959 ;; On a table (resp. a property drawer) do not move through table
960 ;; rows (resp. node properties).
961 (should
962 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
963 (org-forward-paragraph)
964 (looking-at "Paragraph")))
965 (should
966 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
967 (org-forward-paragraph)
968 (looking-at "Paragraph")))
969 ;; On a verse or source block, stop after blank lines.
970 (should
971 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
972 (org-forward-paragraph)
973 (looking-at "L2")))
974 (should
975 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
976 (org-forward-paragraph)
977 (looking-at "L2"))))
979 (ert-deftest test-org/backward-paragraph ()
980 "Test `org-backward-paragraph' specifications."
981 ;; Error at beginning of buffer.
982 (should-error
983 (org-test-with-temp-text "Paragraph"
984 (org-backward-paragraph)))
985 ;; Regular test.
986 (should
987 (org-test-with-temp-text "P1\n\nP2\n\nP3"
988 (goto-char (point-max))
989 (org-backward-paragraph)
990 (looking-at "P3")))
991 (should
992 (org-test-with-temp-text "P1\n\nP2\n\nP3"
993 (goto-char (point-max))
994 (beginning-of-line)
995 (org-backward-paragraph)
996 (looking-at "P2")))
997 ;; Ignore depth.
998 (should
999 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
1000 (goto-char (point-max))
1001 (beginning-of-line)
1002 (org-backward-paragraph)
1003 (looking-at "P2")))
1004 ;; Ignore invisible elements.
1005 (should
1006 (org-test-with-temp-text "* H1\n P1\n* H2"
1007 (org-cycle)
1008 (goto-char (point-max))
1009 (beginning-of-line)
1010 (org-backward-paragraph)
1011 (bobp)))
1012 ;; On an affiliated keyword, jump to the first one.
1013 (should
1014 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
1015 (search-forward "c2")
1016 (org-backward-paragraph)
1017 (looking-at "#\\+name")))
1018 ;; On the second element in an item or a footnote definition, jump
1019 ;; to item or the definition.
1020 (should
1021 (org-test-with-temp-text "- line1\n\n line2"
1022 (goto-char (point-max))
1023 (beginning-of-line)
1024 (org-backward-paragraph)
1025 (looking-at "- line1")))
1026 (should
1027 (org-test-with-temp-text "[fn:1] line1\n\n line2"
1028 (goto-char (point-max))
1029 (beginning-of-line)
1030 (org-backward-paragraph)
1031 (looking-at "\\[fn:1\\] line1")))
1032 ;; On a table (resp. a property drawer), ignore table rows
1033 ;; (resp. node properties).
1034 (should
1035 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
1036 (goto-char (point-max))
1037 (beginning-of-line)
1038 (org-backward-paragraph)
1039 (bobp)))
1040 (should
1041 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
1042 (goto-char (point-max))
1043 (beginning-of-line)
1044 (org-backward-paragraph)
1045 (bobp)))
1046 ;; On a source or verse block, stop before blank lines.
1047 (should
1048 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
1049 (search-forward "L3")
1050 (beginning-of-line)
1051 (org-backward-paragraph)
1052 (looking-at "L2")))
1053 (should
1054 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
1055 (search-forward "L3")
1056 (beginning-of-line)
1057 (org-backward-paragraph)
1058 (looking-at "L2"))))
1060 (ert-deftest test-org/forward-element ()
1061 "Test `org-forward-element' specifications."
1062 ;; 1. At EOB: should error.
1063 (org-test-with-temp-text "Some text\n"
1064 (goto-char (point-max))
1065 (should-error (org-forward-element)))
1066 ;; 2. Standard move: expected to ignore blank lines.
1067 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
1068 (org-forward-element)
1069 (should (looking-at (regexp-quote "Second paragraph."))))
1070 ;; 3. Headline tests.
1071 (org-test-with-temp-text "
1072 * Head 1
1073 ** Head 1.1
1074 *** Head 1.1.1
1075 ** Head 1.2"
1076 ;; 3.1. At an headline beginning: move to next headline at the
1077 ;; same level.
1078 (goto-line 3)
1079 (org-forward-element)
1080 (should (looking-at (regexp-quote "** Head 1.2")))
1081 ;; 3.2. At an headline beginning: move to parent headline if no
1082 ;; headline at the same level.
1083 (goto-line 3)
1084 (org-forward-element)
1085 (should (looking-at (regexp-quote "** Head 1.2"))))
1086 ;; 4. Greater element tests.
1087 (org-test-with-temp-text
1088 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
1089 ;; 4.1. At a greater element: expected to skip contents.
1090 (org-forward-element)
1091 (should (looking-at (regexp-quote "Outside.")))
1092 ;; 4.2. At the end of greater element contents: expected to skip
1093 ;; to the end of the greater element.
1094 (goto-line 2)
1095 (org-forward-element)
1096 (should (looking-at (regexp-quote "Outside."))))
1097 ;; 5. List tests.
1098 (org-test-with-temp-text "
1099 - item1
1101 - sub1
1103 - sub2
1105 - sub3
1107 Inner paragraph.
1109 - item2
1111 Outside."
1112 ;; 5.1. At list top point: expected to move to the element after
1113 ;; the list.
1114 (goto-line 2)
1115 (org-forward-element)
1116 (should (looking-at (regexp-quote "Outside.")))
1117 ;; 5.2. Special case: at the first line of a sub-list, but not at
1118 ;; beginning of line, move to next item.
1119 (goto-line 2)
1120 (forward-char)
1121 (org-forward-element)
1122 (should (looking-at "- item2"))
1123 (goto-line 4)
1124 (forward-char)
1125 (org-forward-element)
1126 (should (looking-at " - sub2"))
1127 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
1128 (goto-line 4)
1129 (org-forward-element)
1130 (should (looking-at (regexp-quote " Inner paragraph.")))
1131 ;; 5.4. At sub-list end: expected to move outside the sub-list.
1132 (goto-line 8)
1133 (org-forward-element)
1134 (should (looking-at (regexp-quote " Inner paragraph.")))
1135 ;; 5.5. At an item: expected to move to next item, if any.
1136 (goto-line 6)
1137 (org-forward-element)
1138 (should (looking-at " - sub3"))))
1140 (ert-deftest test-org/backward-element ()
1141 "Test `org-backward-element' specifications."
1142 ;; 1. Should error at BOB.
1143 (org-test-with-temp-text " \nParagraph."
1144 (should-error (org-backward-element)))
1145 ;; 2. Should move at BOB when called on the first element in buffer.
1146 (should
1147 (org-test-with-temp-text "\n#+TITLE: test"
1148 (progn (forward-line)
1149 (org-backward-element)
1150 (bobp))))
1151 ;; 3. Not at the beginning of an element: move at its beginning.
1152 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
1153 (goto-line 3)
1154 (end-of-line)
1155 (org-backward-element)
1156 (should (looking-at (regexp-quote "Paragraph2."))))
1157 ;; 4. Headline tests.
1158 (org-test-with-temp-text "
1159 * Head 1
1160 ** Head 1.1
1161 *** Head 1.1.1
1162 ** Head 1.2"
1163 ;; 4.1. At an headline beginning: move to previous headline at the
1164 ;; same level.
1165 (goto-line 5)
1166 (org-backward-element)
1167 (should (looking-at (regexp-quote "** Head 1.1")))
1168 ;; 4.2. At an headline beginning: move to parent headline if no
1169 ;; headline at the same level.
1170 (goto-line 3)
1171 (org-backward-element)
1172 (should (looking-at (regexp-quote "* Head 1")))
1173 ;; 4.3. At the first top-level headline: should error.
1174 (goto-line 2)
1175 (should-error (org-backward-element)))
1176 ;; 5. At beginning of first element inside a greater element:
1177 ;; expected to move to greater element's beginning.
1178 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
1179 (goto-line 3)
1180 (org-backward-element)
1181 (should (looking-at "#\\+BEGIN_CENTER")))
1182 ;; 6. At the beginning of the first element in a section: should
1183 ;; move back to headline, if any.
1184 (should
1185 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
1186 (progn (goto-char (point-max))
1187 (beginning-of-line)
1188 (org-backward-element)
1189 (org-at-heading-p))))
1190 ;; 7. List tests.
1191 (org-test-with-temp-text "
1192 - item1
1194 - sub1
1196 - sub2
1198 - sub3
1200 Inner paragraph.
1202 - item2
1205 Outside."
1206 ;; 7.1. At beginning of sub-list: expected to move to the
1207 ;; paragraph before it.
1208 (goto-line 4)
1209 (org-backward-element)
1210 (should (looking-at "item1"))
1211 ;; 7.2. At an item in a list: expected to move at previous item.
1212 (goto-line 8)
1213 (org-backward-element)
1214 (should (looking-at " - sub2"))
1215 (goto-line 12)
1216 (org-backward-element)
1217 (should (looking-at "- item1"))
1218 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
1219 ;; beginning.
1220 (goto-line 10)
1221 (org-backward-element)
1222 (should (looking-at " - sub1"))
1223 (goto-line 15)
1224 (org-backward-element)
1225 (should (looking-at "- item1"))
1226 ;; 7.4. At blank-lines before list end: expected to move to top
1227 ;; item.
1228 (goto-line 14)
1229 (org-backward-element)
1230 (should (looking-at "- item1"))))
1232 (ert-deftest test-org/up-element ()
1233 "Test `org-up-element' specifications."
1234 ;; 1. At BOB or with no surrounding element: should error.
1235 (org-test-with-temp-text "Paragraph."
1236 (should-error (org-up-element)))
1237 (org-test-with-temp-text "* Head1\n* Head2"
1238 (goto-line 2)
1239 (should-error (org-up-element)))
1240 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
1241 (goto-line 3)
1242 (should-error (org-up-element)))
1243 ;; 2. At an headline: move to parent headline.
1244 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
1245 (goto-line 3)
1246 (org-up-element)
1247 (should (looking-at "\\* Head1")))
1248 ;; 3. Inside a greater element: move to greater element beginning.
1249 (org-test-with-temp-text
1250 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
1251 (goto-line 3)
1252 (org-up-element)
1253 (should (looking-at "#\\+BEGIN_CENTER")))
1254 ;; 4. List tests.
1255 (org-test-with-temp-text "* Top
1256 - item1
1258 - sub1
1260 - sub2
1262 Paragraph within sub2.
1264 - item2"
1265 ;; 4.1. Within an item: move to the item beginning.
1266 (goto-line 8)
1267 (org-up-element)
1268 (should (looking-at " - sub2"))
1269 ;; 4.2. At an item in a sub-list: move to parent item.
1270 (goto-line 4)
1271 (org-up-element)
1272 (should (looking-at "- item1"))
1273 ;; 4.3. At an item in top list: move to beginning of whole list.
1274 (goto-line 10)
1275 (org-up-element)
1276 (should (looking-at "- item1"))
1277 ;; 4.4. Special case. At very top point: should move to parent of
1278 ;; list.
1279 (goto-line 2)
1280 (org-up-element)
1281 (should (looking-at "\\* Top"))))
1283 (ert-deftest test-org/down-element ()
1284 "Test `org-down-element' specifications."
1285 ;; Error when the element hasn't got a recursive type.
1286 (org-test-with-temp-text "Paragraph."
1287 (should-error (org-down-element)))
1288 ;; Error when the element has no contents
1289 (org-test-with-temp-text "* Headline"
1290 (should-error (org-down-element)))
1291 ;; When at a plain-list, move to first item.
1292 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
1293 (goto-line 2)
1294 (org-down-element)
1295 (should (looking-at " - Item 1.1")))
1296 (org-test-with-temp-text "#+NAME: list\n- Item 1"
1297 (org-down-element)
1298 (should (looking-at " Item 1")))
1299 ;; When at a table, move to first row
1300 (org-test-with-temp-text "#+NAME: table\n| a | b |"
1301 (org-down-element)
1302 (should (looking-at " a | b |")))
1303 ;; Otherwise, move inside the greater element.
1304 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
1305 (org-down-element)
1306 (should (looking-at "Paragraph"))))
1308 (ert-deftest test-org/drag-element-backward ()
1309 "Test `org-drag-element-backward' specifications."
1310 ;; 1. Error when trying to move first element of buffer.
1311 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
1312 (should-error (org-drag-element-backward)))
1313 ;; 2. Error when trying to swap nested elements.
1314 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
1315 (forward-line)
1316 (should-error (org-drag-element-backward)))
1317 ;; 3. Error when trying to swap an headline element and
1318 ;; a non-headline element.
1319 (org-test-with-temp-text "Test.\n* Head 1"
1320 (forward-line)
1321 (should-error (org-drag-element-backward)))
1322 ;; 4. Otherwise, swap elements, preserving column and blank lines
1323 ;; between elements.
1324 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
1325 (search-forward "graph")
1326 (org-drag-element-backward)
1327 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
1328 (should (looking-at " 2")))
1329 ;; 5. Preserve visibility of elements and their contents.
1330 (org-test-with-temp-text "
1331 #+BEGIN_CENTER
1332 Text.
1333 #+END_CENTER
1334 - item 1
1335 #+BEGIN_QUOTE
1336 Text.
1337 #+END_QUOTE"
1338 (while (search-forward "BEGIN_" nil t) (org-cycle))
1339 (search-backward "- item 1")
1340 (org-drag-element-backward)
1341 (should
1342 (equal
1343 '((63 . 82) (26 . 48))
1344 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
1345 (overlays-in (point-min) (point-max)))))))
1347 (ert-deftest test-org/drag-element-forward ()
1348 "Test `org-drag-element-forward' specifications."
1349 ;; 1. Error when trying to move first element of buffer.
1350 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
1351 (goto-line 3)
1352 (should-error (org-drag-element-forward)))
1353 ;; 2. Error when trying to swap nested elements.
1354 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
1355 (forward-line)
1356 (should-error (org-drag-element-forward)))
1357 ;; 3. Error when trying to swap a non-headline element and an
1358 ;; headline.
1359 (org-test-with-temp-text "Test.\n* Head 1"
1360 (should-error (org-drag-element-forward)))
1361 ;; 4. Otherwise, swap elements, preserving column and blank lines
1362 ;; between elements.
1363 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
1364 (search-forward "graph")
1365 (org-drag-element-forward)
1366 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
1367 (should (looking-at " 1")))
1368 ;; 5. Preserve visibility of elements and their contents.
1369 (org-test-with-temp-text "
1370 #+BEGIN_CENTER
1371 Text.
1372 #+END_CENTER
1373 - item 1
1374 #+BEGIN_QUOTE
1375 Text.
1376 #+END_QUOTE"
1377 (while (search-forward "BEGIN_" nil t) (org-cycle))
1378 (search-backward "#+BEGIN_CENTER")
1379 (org-drag-element-forward)
1380 (should
1381 (equal
1382 '((63 . 82) (26 . 48))
1383 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
1384 (overlays-in (point-min) (point-max)))))))
1388 ;;; Planning
1390 (ert-deftest test-org/timestamp-has-time-p ()
1391 "Test `org-timestamp-has-time-p' specifications."
1392 ;; With time.
1393 (should
1394 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
1395 (org-timestamp-has-time-p (org-element-context))))
1396 ;; Without time.
1397 (should-not
1398 (org-test-with-temp-text "<2012-03-29 Thu>"
1399 (org-timestamp-has-time-p (org-element-context)))))
1401 (ert-deftest test-org/timestamp-format ()
1402 "Test `org-timestamp-format' specifications."
1403 ;; Regular test.
1404 (should
1405 (equal
1406 "2012-03-29 16:40"
1407 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
1408 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
1409 ;; Range end.
1410 (should
1411 (equal
1412 "2012-03-29"
1413 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
1414 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
1416 (ert-deftest test-org/timestamp-split-range ()
1417 "Test `org-timestamp-split-range' specifications."
1418 ;; Extract range start (active).
1419 (should
1420 (equal '(2012 3 29)
1421 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1422 (let ((ts (org-timestamp-split-range (org-element-context))))
1423 (mapcar (lambda (p) (org-element-property p ts))
1424 '(:year-end :month-end :day-end))))))
1425 ;; Extract range start (inactive)
1426 (should
1427 (equal '(2012 3 29)
1428 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1429 (let ((ts (org-timestamp-split-range (org-element-context))))
1430 (mapcar (lambda (p) (org-element-property p ts))
1431 '(:year-end :month-end :day-end))))))
1432 ;; Extract range end (active).
1433 (should
1434 (equal '(2012 3 30)
1435 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1436 (let ((ts (org-timestamp-split-range
1437 (org-element-context) t)))
1438 (mapcar (lambda (p) (org-element-property p ts))
1439 '(:year-end :month-end :day-end))))))
1440 ;; Extract range end (inactive)
1441 (should
1442 (equal '(2012 3 30)
1443 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1444 (let ((ts (org-timestamp-split-range
1445 (org-element-context) t)))
1446 (mapcar (lambda (p) (org-element-property p ts))
1447 '(:year-end :month-end :day-end))))))
1448 ;; Return the timestamp if not a range.
1449 (should
1450 (org-test-with-temp-text "[2012-03-29 Thu]"
1451 (let* ((ts-orig (org-element-context))
1452 (ts-copy (org-timestamp-split-range ts-orig)))
1453 (eq ts-orig ts-copy))))
1454 (should
1455 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1456 (let* ((ts-orig (org-element-context))
1457 (ts-copy (org-timestamp-split-range ts-orig)))
1458 (eq ts-orig ts-copy))))
1459 ;; Check that parent is the same when a range was split.
1460 (should
1461 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1462 (let* ((ts-orig (org-element-context))
1463 (ts-copy (org-timestamp-split-range ts-orig)))
1464 (eq (org-element-property :parent ts-orig)
1465 (org-element-property :parent ts-copy))))))
1467 (ert-deftest test-org/timestamp-translate ()
1468 "Test `org-timestamp-translate' specifications."
1469 ;; Translate whole date range.
1470 (should
1471 (equal "<29>--<30>"
1472 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1473 (let ((org-display-custom-times t)
1474 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1475 (org-timestamp-translate (org-element-context))))))
1476 ;; Translate date range start.
1477 (should
1478 (equal "<29>"
1479 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1480 (let ((org-display-custom-times t)
1481 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1482 (org-timestamp-translate (org-element-context) 'start)))))
1483 ;; Translate date range end.
1484 (should
1485 (equal "<30>"
1486 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1487 (let ((org-display-custom-times t)
1488 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1489 (org-timestamp-translate (org-element-context) 'end)))))
1490 ;; Translate time range.
1491 (should
1492 (equal "<08>--<16>"
1493 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
1494 (let ((org-display-custom-times t)
1495 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
1496 (org-timestamp-translate (org-element-context))))))
1497 ;; Translate non-range timestamp.
1498 (should
1499 (equal "<29>"
1500 (org-test-with-temp-text "<2012-03-29 Thu>"
1501 (let ((org-display-custom-times t)
1502 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1503 (org-timestamp-translate (org-element-context))))))
1504 ;; Do not change `diary' timestamps.
1505 (should
1506 (equal "<%%(org-float t 4 2)>"
1507 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1508 (let ((org-display-custom-times t)
1509 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1510 (org-timestamp-translate (org-element-context)))))))
1514 ;;; Radio Targets
1516 (ert-deftest test-org/update-radio-target-regexp ()
1517 "Test `org-update-radio-target-regexp' specifications."
1518 (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
1519 (save-excursion (goto-char (point-max)) (org-element-context))
1520 (insert "<<<")
1521 (search-forward "o")
1522 (insert ">>>")
1523 (org-update-radio-target-regexp)
1524 (goto-char (point-max))
1525 (org-element-type (org-element-context))))
1529 ;;; Visibility
1531 (ert-deftest test-org/flag-drawer ()
1532 "Test `org-flag-drawer' specifications."
1533 ;; Hide drawer.
1534 (should
1535 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
1536 (org-flag-drawer t)
1537 (get-char-property (line-end-position) 'invisible)))
1538 ;; Show drawer.
1539 (should-not
1540 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
1541 (org-flag-drawer t)
1542 (org-flag-drawer nil)
1543 (get-char-property (line-end-position) 'invisible)))
1544 ;; Test optional argument.
1545 (should
1546 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
1547 (let ((drawer (save-excursion (search-forward ":D2")
1548 (org-element-at-point))))
1549 (org-flag-drawer t drawer)
1550 (get-char-property (progn (search-forward ":D2") (line-end-position))
1551 'invisible))))
1552 (should-not
1553 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
1554 (let ((drawer (save-excursion (search-forward ":D2")
1555 (org-element-at-point))))
1556 (org-flag-drawer t drawer)
1557 (get-char-property (line-end-position) 'invisible))))
1558 ;; Do not hide fake drawers.
1559 (should-not
1560 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
1561 (forward-line 1)
1562 (org-flag-drawer t)
1563 (get-char-property (line-end-position) 'invisible)))
1564 ;; Do not hide incomplete drawers.
1565 (should-not
1566 (org-test-with-temp-text ":D:\nparagraph"
1567 (forward-line 1)
1568 (org-flag-drawer t)
1569 (get-char-property (line-end-position) 'invisible))))
1572 (provide 'test-org)
1574 ;;; test-org.el ends here