test-org-datetree.el 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. (provide 'test-org-datetree)
  108. ;;; test-org-datetree.el ends here