test-org-datetree.el 8.8 KB

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