Merge branch 'maint'
[org-mode.git] / testing / lisp / test-org-datetree.el
blobd500130b2c44072d8abf86e32f2758ec41ad2f54
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 ;; When `org-datetree-add-timestamp' is non-nil, insert a timestamp
59 ;; in entry. When set to `inactive', insert an inactive one.
60 (should
61 (string-match
62 "\\`\\* 2012\n\\*\\* 2012-03 .*\n\\*\\*\\* \\(2012-03-29\\) .*\n[ \t]*<\\1.*?>\\'"
63 (org-test-with-temp-text "* 2012\n"
64 (let ((org-datetree-add-timestamp t))
65 (org-datetree-find-date-create '(3 29 2012)))
66 (org-trim (buffer-string)))))
67 (should
68 (string-match
69 "\\`\\* 2012\n\\*\\* 2012-03 .*\n\\*\\*\\* \\(2012-03-29\\) .*\n[ \t]*\\[\\1.*?\\]\\'"
70 (org-test-with-temp-text "* 2012\n"
71 (let ((org-datetree-add-timestamp 'inactive))
72 (org-datetree-find-date-create '(3 29 2012)))
73 (org-trim (buffer-string)))))
74 ;; Insert at top level, unless some node has DATE_TREE property. In
75 ;; this case, date tree becomes one of its sub-trees.
76 (should
77 (string-match
78 "\\* 2012"
79 (org-test-with-temp-text "* Top"
80 (let ((org-datetree-add-timestamp nil))
81 (org-datetree-find-date-create '(3 29 2012)))
82 (org-trim (buffer-string)))))
83 (should
84 (string-match
85 "\\*\\* H1.1\n:PROPERTIES:\n:DATE_TREE: t\n:END:\n\\*\\*\\* 2012"
86 (org-test-with-temp-text
87 "* H1\n** H1.1\n:PROPERTIES:\n:DATE_TREE: t\n:END:\n* H2"
88 (let ((org-datetree-add-timestamp nil))
89 (org-datetree-find-date-create '(3 29 2012)))
90 (org-trim (buffer-string)))))
91 ;; Always leave point at beginning of day entry.
92 (should
93 (string-match
94 "\\*\\*\\* 2012-03-29"
95 (org-test-with-temp-text "* 2012\n** 2012-03 month\n*** 2012-03-29 day"
96 (let ((org-datetree-add-timestamp nil))
97 (org-datetree-find-date-create '(3 29 2012)))
98 (buffer-substring (point) (line-end-position)))))
99 (should
100 (string-match
101 "\\*\\*\\* 2012-03-29"
102 (org-test-with-temp-text "* 2012\n** 2012-03 month\n*** 2012-03-29 day"
103 (let ((org-datetree-add-timestamp t))
104 (org-datetree-find-date-create '(3 29 2012)))
105 (buffer-substring (point) (line-end-position))))))
107 (provide 'test-org-datetree)
108 ;;; test-org-datetree.el ends here