test-org-archive.el 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ;;; test-org-archive.el --- Test for Org Archive -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017, 2019 Jay Kamat
  3. ;; Author: Jay Kamat <jaygkamat@gmail.com>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (require 'org-archive)
  16. (ert-deftest test-org-archive/update-status-cookie ()
  17. "Test archiving properly updating status cookies."
  18. ;; Test org-archive-subtree with two children.
  19. (should
  20. (equal
  21. "Top [0%]"
  22. (org-test-with-temp-text-in-file
  23. "* Top [%]\n** DONE One\n** TODO Two"
  24. (forward-line)
  25. (org-archive-subtree)
  26. (forward-line -1)
  27. (org-element-property :raw-value (org-element-at-point)))))
  28. ;; Test org-archive-subtree with one child.
  29. (should
  30. (equal
  31. "Top [100%]"
  32. (org-test-with-temp-text-in-file "* Top [%]\n** TODO Two"
  33. (forward-line)
  34. (org-archive-subtree)
  35. (forward-line -1)
  36. (org-element-property :raw-value (org-element-at-point)))))
  37. ;; Test org-archive-to-archive-sibling with two children.
  38. (should
  39. (equal
  40. "Top [100%]"
  41. (org-test-with-temp-text "* Top [%]\n<point>** TODO One\n** DONE Two"
  42. (org-archive-to-archive-sibling)
  43. (forward-line -1)
  44. (org-element-property :raw-value (org-element-at-point)))))
  45. ;; Test org-archive-to-archive-sibling with two children.
  46. (should
  47. (equal
  48. "Top [0%]"
  49. (org-test-with-temp-text "* Top [%]\n<point>** DONE Two"
  50. (org-archive-to-archive-sibling)
  51. (forward-line -1)
  52. (org-element-property :raw-value (org-element-at-point))))))
  53. (ert-deftest test-org-archive/datetree ()
  54. "Test `org-archive-subtree' with a datetree target."
  55. (org-test-at-time "<2020-07-05 Sun>"
  56. ;; Test in buffer target with no additional subheadings...
  57. (should
  58. (string-match-p
  59. (regexp-quote (format-time-string "*** 2020-07-05 %A\n**** a"))
  60. (org-test-with-temp-text-in-file "* a\n"
  61. (let ((org-archive-location "::datetree/"))
  62. (org-archive-subtree)
  63. (buffer-string)))))
  64. ;; ... and with `org-odd-levels-only' non-nil.
  65. (should
  66. (string-match-p
  67. (regexp-quote (format-time-string "***** 2020-07-05 %A\n******* a"))
  68. (org-test-with-temp-text-in-file "* a\n"
  69. (let ((org-archive-location "::datetree/")
  70. (org-odd-levels-only t))
  71. (org-archive-subtree)
  72. (buffer-string)))))
  73. ;; Test in buffer target with an additional subheading...
  74. (should
  75. (string-match-p
  76. (regexp-quote (format-time-string "*** 2020-07-05 %A\n**** a\n***** b"))
  77. (org-test-with-temp-text-in-file "* b\n"
  78. (let ((org-archive-location "::datetree/* a"))
  79. (org-archive-subtree)
  80. (buffer-string)))))
  81. ;; ... and with `org-odd-levels-only' non-nil.
  82. (should
  83. (string-match-p
  84. (regexp-quote (format-time-string "***** 2020-07-05 %A\n******* a\n********* b"))
  85. (org-test-with-temp-text-in-file "* b\n"
  86. (let ((org-archive-location "::datetree/* a")
  87. (org-odd-levels-only t))
  88. (org-archive-subtree)
  89. (buffer-string)))))))
  90. (ert-deftest test-org-archive/to-archive-sibling ()
  91. "Test `org-archive-to-archive-sibling' specifications."
  92. ;; Archive sibling before or after archive heading.
  93. (should
  94. (equal "* Archive :ARCHIVE:\n** H\n"
  95. (org-test-with-temp-text "* H\n* Archive :ARCHIVE:\n"
  96. (let ((org-archive-sibling-heading "Archive")
  97. (org-archive-tag "ARCHIVE"))
  98. (org-archive-to-archive-sibling)
  99. (goto-char (point-min))
  100. (buffer-substring-no-properties
  101. (point) (line-beginning-position 3))))))
  102. (should
  103. (equal "* Archive :ARCHIVE:\n** H\n"
  104. (org-test-with-temp-text "* Archive :ARCHIVE:\n<point>* H\n"
  105. (let ((org-archive-sibling-heading "Archive")
  106. (org-archive-tag "ARCHIVE"))
  107. (org-archive-to-archive-sibling)
  108. (goto-char (point-min))
  109. (buffer-substring-no-properties
  110. (point) (line-beginning-position 3))))))
  111. ;; When there is no sibling archive heading, create it.
  112. (should
  113. (equal "* Archive :ARCHIVE:\n** H\n"
  114. (org-test-with-temp-text "* H\n"
  115. (let ((org-archive-sibling-heading "Archive")
  116. (org-archive-tag "ARCHIVE")
  117. (org-tags-column 1))
  118. (org-archive-to-archive-sibling)
  119. (goto-char (point-min))
  120. (buffer-substring-no-properties
  121. (point) (line-beginning-position 3))))))
  122. ;; Ignore non-sibling archive headings.
  123. (should
  124. (equal "* Archive :ARCHIVE:\n* Top\n** Archive :ARCHIVE:\n*** H\n"
  125. (org-test-with-temp-text "* Archive :ARCHIVE:\n* Top\n<point>** H\n"
  126. (let ((org-archive-sibling-heading "Archive")
  127. (org-archive-tag "ARCHIVE")
  128. (org-tags-column 0))
  129. (org-archive-to-archive-sibling)
  130. (goto-char (point-min))
  131. (buffer-substring-no-properties
  132. (point) (line-beginning-position 5))))))
  133. ;; When archiving a heading, leave point on next heading.
  134. (should
  135. (equal "* H2"
  136. (org-test-with-temp-text "* H1\n* H2\n* Archive :ARCHIVE:\n"
  137. (let ((org-archive-sibling-heading "Archive")
  138. (org-archive-tag "ARCHIVE"))
  139. (org-archive-to-archive-sibling)
  140. (buffer-substring-no-properties (point) (line-end-position))))))
  141. (should
  142. (equal "* H2"
  143. (org-test-with-temp-text "* Archive :ARCHIVE:\n<point>* H1\n* H2\n"
  144. (let ((org-archive-sibling-heading "Archive")
  145. (org-archive-tag "ARCHIVE"))
  146. (org-archive-to-archive-sibling)
  147. (buffer-substring-no-properties (point) (line-end-position))))))
  148. ;; If `org-archive-reversed-order' is nil, archive as the last
  149. ;; child. Otherwise, archive as the first one.
  150. (should
  151. (equal "* Archive :ARCHIVE:\n** A\n"
  152. (org-test-with-temp-text "* H\n* Archive :ARCHIVE:\n** A\n"
  153. (let ((org-archive-sibling-heading "Archive")
  154. (org-archive-tag "ARCHIVE")
  155. (org-archive-reversed-order nil))
  156. (org-archive-to-archive-sibling)
  157. (goto-char (point-min))
  158. (buffer-substring-no-properties
  159. (point) (line-beginning-position 3))))))
  160. (should
  161. (equal "* Archive :ARCHIVE:\n** H\n"
  162. (org-test-with-temp-text "* H\n* Archive :ARCHIVE:\n** A\n"
  163. (let ((org-archive-sibling-heading "Archive")
  164. (org-archive-tag "ARCHIVE")
  165. (org-archive-reversed-order t))
  166. (org-archive-to-archive-sibling)
  167. (goto-char (point-min))
  168. (buffer-substring-no-properties
  169. (point) (line-beginning-position 3)))))))
  170. (provide 'test-org-archive)
  171. ;;; test-org-archive.el ends here