Merge branch 'maint'
[org-mode.git] / testing / lisp / test-org-datetree.el
blob721ec2dc4968af5766e5241d1e76ee2aeddf92e2
1 ;;; test-org-datetree.el --- Tests for Org Datetree -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
7 ;; This file is not part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Code:
24 (ert-deftest test-org-datetree/find-date-create ()
25 "Test `org-datetree-find-date-create' specifications."
26 ;; When date is missing, create it.
27 (should
28 (string-match
29 "\\`\\* 2012\n\\*\\* 2012-03 .*\n\\*\\*\\* 2012-03-29 .*\\'"
30 (org-test-with-temp-text ""
31 (let ((org-datetree-add-timestamp nil))
32 (org-datetree-find-date-create '(3 29 2012)))
33 (org-trim (buffer-string)))))
34 ;; Do not create new year node when one exists.
35 (should
36 (string-match
37 "\\`\\* 2012\n\\*\\* 2012-03 .*\n\\*\\*\\* 2012-03-29 .*\\'"
38 (org-test-with-temp-text "* 2012\n"
39 (let ((org-datetree-add-timestamp nil))
40 (org-datetree-find-date-create '(3 29 2012)))
41 (org-trim (buffer-string)))))
42 ;; Do not create new month node when one exists.
43 (should
44 (string-match
45 "\\`\\* 2012\n\\*\\* 2012-03 .*\n\\*\\*\\* 2012-03-29 .*\\'"
46 (org-test-with-temp-text "* 2012\n** 2012-03 month"
47 (let ((org-datetree-add-timestamp nil))
48 (org-datetree-find-date-create '(3 29 2012)))
49 (org-trim (buffer-string)))))
50 ;; Do not create new day node when one exists.
51 (should
52 (string-match
53 "\\`\\* 2012\n\\*\\* 2012-03 .*\n\\*\\*\\* 2012-03-29 .*\\'"
54 (org-test-with-temp-text "* 2012\n** 2012-03 month\n*** 2012-03-29 day"
55 (let ((org-datetree-add-timestamp nil))
56 (org-datetree-find-date-create '(3 29 2012)))
57 (org-trim (buffer-string)))))
58 ;; Sort new entry in right place.
59 (should
60 (string-match
61 "\\`\\* 2012\n\\*\\* 2012-02 .*\n\\*\\*\\* 2012-02-01 .*\n\n\\*\\* 2012-03 .*\n\\*\\*\\* 2012-03-29 .*\\'"
62 (org-test-with-temp-text "* 2012\n** 2012-03 month\n*** 2012-03-29 day"
63 (let ((org-datetree-add-timestamp nil))
64 (org-datetree-find-date-create '(3 29 2012))
65 (org-datetree-find-date-create '(2 1 2012)))
66 (org-trim (buffer-string)))))
67 ;; When `org-datetree-add-timestamp' is non-nil, insert a timestamp
68 ;; in entry. When set to `inactive', insert an inactive one.
69 (should
70 (string-match
71 "\\`\\* 2012\n\\*\\* 2012-03 .*\n\\*\\*\\* \\(2012-03-29\\) .*\n[ \t]*<\\1.*?>\\'"
72 (org-test-with-temp-text "* 2012\n"
73 (let ((org-datetree-add-timestamp t))
74 (org-datetree-find-date-create '(3 29 2012)))
75 (org-trim (buffer-string)))))
76 (should
77 (string-match
78 "\\`\\* 2012\n\\*\\* 2012-03 .*\n\\*\\*\\* \\(2012-03-29\\) .*\n[ \t]*\\[\\1.*?\\]\\'"
79 (org-test-with-temp-text "* 2012\n"
80 (let ((org-datetree-add-timestamp 'inactive))
81 (org-datetree-find-date-create '(3 29 2012)))
82 (org-trim (buffer-string)))))
83 ;; Insert at top level, unless some node has DATE_TREE property. In
84 ;; this case, date tree becomes one of its sub-trees.
85 (should
86 (string-match
87 "\\* 2012"
88 (org-test-with-temp-text "* Top"
89 (let ((org-datetree-add-timestamp nil))
90 (org-datetree-find-date-create '(3 29 2012)))
91 (org-trim (buffer-string)))))
92 (should
93 (string-match
94 "\\*\\* H1.1\n:PROPERTIES:\n:DATE_TREE: t\n:END:\n\\*\\*\\* 2012"
95 (org-test-with-temp-text
96 "* H1\n** H1.1\n:PROPERTIES:\n:DATE_TREE: t\n:END:\n* H2"
97 (let ((org-datetree-add-timestamp nil))
98 (org-datetree-find-date-create '(3 29 2012)))
99 (org-trim (buffer-string)))))
100 ;; Always leave point at beginning of day entry.
101 (should
102 (string-match
103 "\\*\\*\\* 2012-03-29"
104 (org-test-with-temp-text "* 2012\n** 2012-03 month\n*** 2012-03-29 day"
105 (let ((org-datetree-add-timestamp nil))
106 (org-datetree-find-date-create '(3 29 2012)))
107 (buffer-substring (point) (line-end-position)))))
108 (should
109 (string-match
110 "\\*\\*\\* 2012-03-29"
111 (org-test-with-temp-text "* 2012\n** 2012-03 month\n*** 2012-03-29 day"
112 (let ((org-datetree-add-timestamp t))
113 (org-datetree-find-date-create '(3 29 2012)))
114 (buffer-substring (point) (line-end-position))))))
116 (ert-deftest test-org-datetree/find-iso-week-create ()
117 "Test `org-datetree-find-iso-date-create' specificaiton."
118 ;; When date is missing, create it.
119 (should
120 (string-match
121 "\\`\\* 2015\n\\*\\* 2015-W01\n\\*\\*\\* 2014-12-31 .*\\'"
122 (org-test-with-temp-text ""
123 (let ((org-datetree-add-timestamp nil))
124 (org-datetree-find-iso-week-create '(12 31 2014)))
125 (org-trim (buffer-string)))))
126 ;; Do not create new year node when one exists.
127 (should
128 (string-match
129 "\\`\\* 2015\n\\*\\* 2015-W01\n\\*\\*\\* 2014-12-31 .*\\'"
130 (org-test-with-temp-text "* 2015\n"
131 (let ((org-datetree-add-timestamp nil))
132 (org-datetree-find-iso-week-create '(12 31 2014)))
133 (org-trim (buffer-string)))))
134 ;; Do not create new month node when one exists.
135 (should
136 (string-match
137 "\\`\\* 2015\n\\*\\* 2015-W01\n\\*\\*\\* 2014-12-31 .*\\'"
138 (org-test-with-temp-text "* 2015\n** 2015-W01"
139 (let ((org-datetree-add-timestamp nil))
140 (org-datetree-find-iso-week-create '(12 31 2014)))
141 (org-trim (buffer-string)))))
142 ;; Do not create new day node when one exists.
143 (should
144 (string-match
145 "\\`\\* 2015\n\\*\\* 2015-W01\n\\*\\*\\* 2014-12-31 .*\\'"
146 (org-test-with-temp-text "* 2015\n** 2015-W01\n*** 2014-12-31 day"
147 (let ((org-datetree-add-timestamp nil))
148 (org-datetree-find-iso-week-create '(12 31 2014)))
149 (org-trim (buffer-string)))))
150 ;; Sort new entry in right place.
151 (should
152 (string-match
153 "\\`\\* 2015\n\\*\\* 2015-W01\n\\*\\*\\* 2014-12-31 .*\n\n\\*\\* 2015-W36\n\\*\\*\\* 2015-09-01 .*\\'"
154 (org-test-with-temp-text "* 2015"
155 (let ((org-datetree-add-timestamp nil))
156 (org-datetree-find-iso-week-create '(9 1 2015))
157 (org-datetree-find-iso-week-create '(12 31 2014)))
158 (org-trim (buffer-string)))))
159 ;; When `org-datetree-add-timestamp' is non-nil, insert a timestamp
160 ;; in entry. When set to `inactive', insert an inactive one.
161 (should
162 (string-match
163 "\\`\\* 2015\n\\*\\* 2015-W01\n\\*\\*\\* \\(2014-12-31\\) .*\n[ \t]*<\\1.*?>\\'"
164 (org-test-with-temp-text "* 2015\n"
165 (let ((org-datetree-add-timestamp t))
166 (org-datetree-find-iso-week-create '(12 31 2014)))
167 (org-trim (buffer-string)))))
168 (should
169 (string-match
170 "\\`\\* 2015\n\\*\\* 2015-W01\n\\*\\*\\* \\(2014-12-31\\) .*\n[ \t]*\\[\\1.*?\\]\\'"
171 (org-test-with-temp-text "* 2015\n"
172 (let ((org-datetree-add-timestamp 'inactive))
173 (org-datetree-find-iso-week-create '(12 31 2014)))
174 (org-trim (buffer-string)))))
175 ;; Insert at top level, unless some node has WEEK_TREE property. In
176 ;; this case, date tree becomes one of its sub-trees.
177 (should
178 (string-match
179 "\\* 2015"
180 (org-test-with-temp-text "* Top"
181 (let ((org-datetree-add-timestamp nil))
182 (org-datetree-find-iso-week-create '(12 31 2014)))
183 (org-trim (buffer-string)))))
184 (should
185 (string-match
186 "\\*\\* H1.1\n:PROPERTIES:\n:WEEK_TREE: t\n:END:\n\\*\\*\\* 2015"
187 (org-test-with-temp-text
188 "* H1\n** H1.1\n:PROPERTIES:\n:WEEK_TREE: t\n:END:\n* H2"
189 (let ((org-datetree-add-timestamp nil))
190 (org-datetree-find-iso-week-create '(12 31 2014)))
191 (org-trim (buffer-string)))))
192 ;; Always leave point at beginning of day entry.
193 (should
194 (string-match
195 "\\*\\*\\* 2014-12-31"
196 (org-test-with-temp-text "* 2015\n** 2015-W01\n*** 2014-12-31 day"
197 (let ((org-datetree-add-timestamp nil))
198 (org-datetree-find-iso-week-create '(12 31 2014)))
199 (buffer-substring (point) (line-end-position)))))
200 (should
201 (string-match
202 "\\*\\*\\* 2014-12-31"
203 (org-test-with-temp-text "* 2015\n** 2015-W01\n*** 2014-12-31 day"
204 (let ((org-datetree-add-timestamp t))
205 (org-datetree-find-iso-week-create '(12 31 2014)))
206 (buffer-substring (point) (line-end-position))))))
208 (provide 'test-org-datetree)
209 ;;; test-org-datetree.el ends here