test-org-datetree.el 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. ;;; test-org-datetree.el --- Tests for Org Datetree -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015 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 <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (ert-deftest test-org-datetree/find-date-create ()
  17. "Test `org-datetree-find-date-create' specifications."
  18. ;; When date is missing, create it.
  19. (should
  20. (string-match
  21. "\\`\\* 2012\n\\*\\* 2012-03 .*\n\\*\\*\\* 2012-03-29 .*\\'"
  22. (org-test-with-temp-text ""
  23. (let ((org-datetree-add-timestamp nil))
  24. (org-datetree-find-date-create '(3 29 2012)))
  25. (org-trim (buffer-string)))))
  26. ;; Do not create new year node when one exists.
  27. (should
  28. (string-match
  29. "\\`\\* 2012\n\\*\\* 2012-03 .*\n\\*\\*\\* 2012-03-29 .*\\'"
  30. (org-test-with-temp-text "* 2012\n"
  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 month 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** 2012-03 month"
  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 day 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\n*** 2012-03-29 day"
  47. (let ((org-datetree-add-timestamp nil))
  48. (org-datetree-find-date-create '(3 29 2012)))
  49. (org-trim (buffer-string)))))
  50. ;; Sort new entry in right place.
  51. (should
  52. (string-match
  53. "\\`\\* 2012\n\\*\\* 2012-02 .*\n\\*\\*\\* 2012-02-01 .*\n\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-datetree-find-date-create '(2 1 2012)))
  58. (org-trim (buffer-string)))))
  59. ;; When `org-datetree-add-timestamp' is non-nil, insert a timestamp
  60. ;; in entry. When set to `inactive', insert an inactive one.
  61. (should
  62. (string-match
  63. "\\`\\* 2012\n\\*\\* 2012-03 .*\n\\*\\*\\* \\(2012-03-29\\) .*\n[ \t]*<\\1.*?>\\'"
  64. (org-test-with-temp-text "* 2012\n"
  65. (let ((org-datetree-add-timestamp t))
  66. (org-datetree-find-date-create '(3 29 2012)))
  67. (org-trim (buffer-string)))))
  68. (should
  69. (string-match
  70. "\\`\\* 2012\n\\*\\* 2012-03 .*\n\\*\\*\\* \\(2012-03-29\\) .*\n[ \t]*\\[\\1.*?\\]\\'"
  71. (org-test-with-temp-text "* 2012\n"
  72. (let ((org-datetree-add-timestamp 'inactive))
  73. (org-datetree-find-date-create '(3 29 2012)))
  74. (org-trim (buffer-string)))))
  75. ;; Insert at top level, unless some node has DATE_TREE property. In
  76. ;; this case, date tree becomes one of its sub-trees.
  77. (should
  78. (string-match
  79. "\\* 2012"
  80. (org-test-with-temp-text "* Top"
  81. (let ((org-datetree-add-timestamp nil))
  82. (org-datetree-find-date-create '(3 29 2012)))
  83. (org-trim (buffer-string)))))
  84. (should
  85. (string-match
  86. "\\*\\* H1.1\n:PROPERTIES:\n:DATE_TREE: t\n:END:\n\\*\\*\\* 2012"
  87. (org-test-with-temp-text
  88. "* H1\n** H1.1\n:PROPERTIES:\n:DATE_TREE: t\n:END:\n* H2"
  89. (let ((org-datetree-add-timestamp nil))
  90. (org-datetree-find-date-create '(3 29 2012)))
  91. (org-trim (buffer-string)))))
  92. ;; Always leave point at beginning of day entry.
  93. (should
  94. (string-match
  95. "\\*\\*\\* 2012-03-29"
  96. (org-test-with-temp-text "* 2012\n** 2012-03 month\n*** 2012-03-29 day"
  97. (let ((org-datetree-add-timestamp nil))
  98. (org-datetree-find-date-create '(3 29 2012)))
  99. (buffer-substring (point) (line-end-position)))))
  100. (should
  101. (string-match
  102. "\\*\\*\\* 2012-03-29"
  103. (org-test-with-temp-text "* 2012\n** 2012-03 month\n*** 2012-03-29 day"
  104. (let ((org-datetree-add-timestamp t))
  105. (org-datetree-find-date-create '(3 29 2012)))
  106. (buffer-substring (point) (line-end-position))))))
  107. (ert-deftest test-org-datetree/find-iso-week-create ()
  108. "Test `org-datetree-find-iso-date-create' specificaiton."
  109. ;; When date is missing, create it.
  110. (should
  111. (string-match
  112. "\\`\\* 2015\n\\*\\* 2015-W01\n\\*\\*\\* 2014-12-31 .*\\'"
  113. (org-test-with-temp-text ""
  114. (let ((org-datetree-add-timestamp nil))
  115. (org-datetree-find-iso-week-create '(12 31 2014)))
  116. (org-trim (buffer-string)))))
  117. ;; Do not create new year node when one exists.
  118. (should
  119. (string-match
  120. "\\`\\* 2015\n\\*\\* 2015-W01\n\\*\\*\\* 2014-12-31 .*\\'"
  121. (org-test-with-temp-text "* 2015\n"
  122. (let ((org-datetree-add-timestamp nil))
  123. (org-datetree-find-iso-week-create '(12 31 2014)))
  124. (org-trim (buffer-string)))))
  125. ;; Do not create new month node when one exists.
  126. (should
  127. (string-match
  128. "\\`\\* 2015\n\\*\\* 2015-W01\n\\*\\*\\* 2014-12-31 .*\\'"
  129. (org-test-with-temp-text "* 2015\n** 2015-W01"
  130. (let ((org-datetree-add-timestamp nil))
  131. (org-datetree-find-iso-week-create '(12 31 2014)))
  132. (org-trim (buffer-string)))))
  133. ;; Do not create new day node when one exists.
  134. (should
  135. (string-match
  136. "\\`\\* 2015\n\\*\\* 2015-W01\n\\*\\*\\* 2014-12-31 .*\\'"
  137. (org-test-with-temp-text "* 2015\n** 2015-W01\n*** 2014-12-31 day"
  138. (let ((org-datetree-add-timestamp nil))
  139. (org-datetree-find-iso-week-create '(12 31 2014)))
  140. (org-trim (buffer-string)))))
  141. ;; Sort new entry in right place.
  142. (should
  143. (string-match
  144. "\\`\\* 2015\n\\*\\* 2015-W01\n\\*\\*\\* 2014-12-31 .*\n\n\\*\\* 2015-W36\n\\*\\*\\* 2015-09-01 .*\\'"
  145. (org-test-with-temp-text "* 2015"
  146. (let ((org-datetree-add-timestamp nil))
  147. (org-datetree-find-iso-week-create '(9 1 2015))
  148. (org-datetree-find-iso-week-create '(12 31 2014)))
  149. (org-trim (buffer-string)))))
  150. ;; When `org-datetree-add-timestamp' is non-nil, insert a timestamp
  151. ;; in entry. When set to `inactive', insert an inactive one.
  152. (should
  153. (string-match
  154. "\\`\\* 2015\n\\*\\* 2015-W01\n\\*\\*\\* \\(2014-12-31\\) .*\n[ \t]*<\\1.*?>\\'"
  155. (org-test-with-temp-text "* 2015\n"
  156. (let ((org-datetree-add-timestamp t))
  157. (org-datetree-find-iso-week-create '(12 31 2014)))
  158. (org-trim (buffer-string)))))
  159. (should
  160. (string-match
  161. "\\`\\* 2015\n\\*\\* 2015-W01\n\\*\\*\\* \\(2014-12-31\\) .*\n[ \t]*\\[\\1.*?\\]\\'"
  162. (org-test-with-temp-text "* 2015\n"
  163. (let ((org-datetree-add-timestamp 'inactive))
  164. (org-datetree-find-iso-week-create '(12 31 2014)))
  165. (org-trim (buffer-string)))))
  166. ;; Insert at top level, unless some node has WEEK_TREE property. In
  167. ;; this case, date tree becomes one of its sub-trees.
  168. (should
  169. (string-match
  170. "\\* 2015"
  171. (org-test-with-temp-text "* Top"
  172. (let ((org-datetree-add-timestamp nil))
  173. (org-datetree-find-iso-week-create '(12 31 2014)))
  174. (org-trim (buffer-string)))))
  175. (should
  176. (string-match
  177. "\\*\\* H1.1\n:PROPERTIES:\n:WEEK_TREE: t\n:END:\n\\*\\*\\* 2015"
  178. (org-test-with-temp-text
  179. "* H1\n** H1.1\n:PROPERTIES:\n:WEEK_TREE: t\n:END:\n* H2"
  180. (let ((org-datetree-add-timestamp nil))
  181. (org-datetree-find-iso-week-create '(12 31 2014)))
  182. (org-trim (buffer-string)))))
  183. ;; Always leave point at beginning of day entry.
  184. (should
  185. (string-match
  186. "\\*\\*\\* 2014-12-31"
  187. (org-test-with-temp-text "* 2015\n** 2015-W01\n*** 2014-12-31 day"
  188. (let ((org-datetree-add-timestamp nil))
  189. (org-datetree-find-iso-week-create '(12 31 2014)))
  190. (buffer-substring (point) (line-end-position)))))
  191. (should
  192. (string-match
  193. "\\*\\*\\* 2014-12-31"
  194. (org-test-with-temp-text "* 2015\n** 2015-W01\n*** 2014-12-31 day"
  195. (let ((org-datetree-add-timestamp t))
  196. (org-datetree-find-iso-week-create '(12 31 2014)))
  197. (buffer-substring (point) (line-end-position))))))
  198. (provide 'test-org-datetree)
  199. ;;; test-org-datetree.el ends here