1 ;;; test-org-capture.el --- Tests for org-capture.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015, 2017 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;; Unit tests for Org Capture library.
26 (require 'org-capture
)
28 (ert-deftest test-org-capture
/fill-template
()
29 "Test `org-capture-fill-template' specifications."
31 ;; When working on these tests consider to also change
32 ;; `test-org-feed/fill-template'.
34 ;; %(sexp) placeholder.
37 (org-capture-fill-template "%(concat \"success\" \"!\")")))
38 ;; It is possible to include other place holders in %(sexp). In
39 ;; that case properly escape \ and " characters.
41 (equal "Nested string \"\\\"\\\"\"\n"
42 (let ((org-store-link-plist nil
))
43 (org-capture-fill-template "%(concat \"%i\")"
44 "Nested string \"\\\"\\\"\""))))
45 ;; %<...> placeholder.
47 (equal (concat (format-time-string "%Y") "\n")
48 (org-capture-fill-template "%<%Y>")))
49 ;; %t and %T placeholders.
51 (equal (concat (format-time-string (org-time-stamp-format nil nil
)) "\n")
52 (org-capture-fill-template "%t")))
54 (equal (concat (format-time-string (org-time-stamp-format t nil
)) "\n")
55 (org-capture-fill-template "%T")))
56 ;; %u and %U placeholders.
59 (concat (format-time-string (org-time-stamp-format nil t
)) "\n")
60 (org-capture-fill-template "%u")))
63 (concat (format-time-string (org-time-stamp-format t t
)) "\n")
64 (org-capture-fill-template "%U")))
65 ;; %i placeholder. Make sure sexp placeholders are not expanded
66 ;; when they are inserted through this one.
69 (let ((org-store-link-plist nil
))
70 (org-capture-fill-template "%i" "success!"))))
72 (equal "%(concat \"no \" \"evaluation\")\n"
73 (let ((org-store-link-plist nil
))
74 (org-capture-fill-template
75 "%i" "%(concat \"no \" \"evaluation\")"))))
76 ;; When %i contents span over multiple line, repeat initial leading
77 ;; characters over each line.
79 (equal "> line 1\n> line 2\n"
80 (let ((org-store-link-plist nil
))
81 (org-capture-fill-template "> %i" "line 1\nline 2"))))
82 ;; Test %-escaping with \ character.
85 (let ((org-store-link-plist nil
))
86 (org-capture-fill-template "\\%i" "success!"))))
89 (let ((org-store-link-plist nil
))
90 (org-capture-fill-template "\\\\%i" "success!"))))
93 (let ((org-store-link-plist nil
))
94 (org-capture-fill-template "\\\\\\%i" "success!"))))
95 ;; More than one placeholder in the same template.
97 (equal "success! success! success! success!\n"
98 (let ((org-store-link-plist nil
))
99 (org-capture-fill-template "%i %i %i %i" "success!"))))
100 ;; %(sexp) placeholder with an input containing the traps %, " and )
101 ;; all at once which is complicated to parse.
103 (equal "5 % Less (See Item \"3)\" Somewhere)\n"
104 (let ((org-store-link-plist nil
))
105 (org-capture-fill-template
106 "%(capitalize \"%i\")"
107 "5 % less (see item \"3)\" somewhere)")))))
109 (ert-deftest test-org-capture
/refile
()
110 "Test `org-capture-refile' specifications."
111 ;; When refiling, make sure the headline being refiled is the one
112 ;; being captured. In particular, empty lines after the entry may
113 ;; be removed, and we don't want to shift onto the next heading.
117 (org-test-with-temp-text-in-file "* A\n* B\n"
118 (let* ((file (buffer-file-name))
119 (org-capture-templates
120 `(("t" "Todo" entry
(file+headline
,file
"A") "** H1 %?"))))
121 (org-capture nil
"t")
123 (cl-letf (((symbol-function 'org-refile
)
127 (buffer-substring-no-properties
128 (line-beginning-position)
129 (line-end-position))))))
130 (catch :return
(org-capture-refile))))))))
133 (provide 'test-org-capture
)
134 ;;; test-org-capture.el ends here