test-org-archive.el 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. (ert-deftest test-org-archive/update-status-cookie ()
  16. "Test archiving properly updating status cookies."
  17. ;; Test org-archive-subtree with two children.
  18. (should
  19. (equal
  20. "Top [0%]"
  21. (org-test-with-temp-text-in-file
  22. "* Top [%]\n** DONE One\n** TODO Two"
  23. (forward-line)
  24. (org-archive-subtree)
  25. (forward-line -1)
  26. (org-element-property :title (org-element-at-point)))))
  27. ;; Test org-archive-subtree with one child.
  28. (should
  29. (equal
  30. "Top [100%]"
  31. (org-test-with-temp-text-in-file "* Top [%]\n** TODO Two"
  32. (forward-line)
  33. (org-archive-subtree)
  34. (forward-line -1)
  35. (org-element-property :title (org-element-at-point)))))
  36. ;; Test org-archive-to-archive-sibling with two children.
  37. (should
  38. (equal
  39. "Top [100%]"
  40. (org-test-with-temp-text "* Top [%]\n<point>** TODO One\n** DONE Two"
  41. (org-archive-to-archive-sibling)
  42. (forward-line -1)
  43. (org-element-property :title (org-element-at-point)))))
  44. ;; Test org-archive-to-archive-sibling with two children.
  45. (should
  46. (equal
  47. "Top [0%]"
  48. (org-test-with-temp-text "* Top [%]\n<point>** DONE Two"
  49. (org-archive-to-archive-sibling)
  50. (forward-line -1)
  51. (org-element-property :title (org-element-at-point))))))
  52. (ert-deftest test-org-archive/datetree ()
  53. "Test `org-archive-subtree' with a datetree target."
  54. (org-test-at-time "<2020-07-05 Sun>"
  55. ;; Test in buffer target with no additional subheadings...
  56. (should
  57. (string-match-p
  58. (regexp-quote (format-time-string "*** 2020-07-05 %A\n**** a"))
  59. (org-test-with-temp-text-in-file "* a\n"
  60. (let ((org-archive-location "::datetree/"))
  61. (org-archive-subtree)
  62. (buffer-string)))))
  63. ;; ... and with `org-odd-levels-only' non-nil.
  64. (should
  65. (string-match-p
  66. (regexp-quote (format-time-string "***** 2020-07-05 %A\n******* a"))
  67. (org-test-with-temp-text-in-file "* a\n"
  68. (let ((org-archive-location "::datetree/")
  69. (org-odd-levels-only t))
  70. (org-archive-subtree)
  71. (buffer-string)))))
  72. ;; Test in buffer target with an additional subheading...
  73. (should
  74. (string-match-p
  75. (regexp-quote (format-time-string "*** 2020-07-05 %A\n**** a\n***** b"))
  76. (org-test-with-temp-text-in-file "* b\n"
  77. (let ((org-archive-location "::datetree/* a"))
  78. (org-archive-subtree)
  79. (buffer-string)))))
  80. ;; ... and with `org-odd-levels-only' non-nil.
  81. (should
  82. (string-match-p
  83. (regexp-quote (format-time-string "***** 2020-07-05 %A\n******* a\n********* b"))
  84. (org-test-with-temp-text-in-file "* b\n"
  85. (let ((org-archive-location "::datetree/* a")
  86. (org-odd-levels-only t))
  87. (org-archive-subtree)
  88. (buffer-string)))))))
  89. (ert-deftest test-org-archive/to-archive-sibling ()
  90. "Test `org-archive-to-archive-sibling' specifications."
  91. ;; Archive sibling before or after archive heading.
  92. (should
  93. (equal "* Archive :ARCHIVE:\n** H\n"
  94. (org-test-with-temp-text "* H\n* Archive :ARCHIVE:\n"
  95. (let ((org-archive-sibling-heading "Archive")
  96. (org-archive-tag "ARCHIVE"))
  97. (org-archive-to-archive-sibling)
  98. (goto-char (point-min))
  99. (buffer-substring-no-properties
  100. (point) (line-beginning-position 3))))))
  101. (should
  102. (equal "* Archive :ARCHIVE:\n** H\n"
  103. (org-test-with-temp-text "* Archive :ARCHIVE:\n<point>* H\n"
  104. (let ((org-archive-sibling-heading "Archive")
  105. (org-archive-tag "ARCHIVE"))
  106. (org-archive-to-archive-sibling)
  107. (goto-char (point-min))
  108. (buffer-substring-no-properties
  109. (point) (line-beginning-position 3))))))
  110. ;; When there is no sibling archive heading, create it.
  111. (should
  112. (equal "* Archive :ARCHIVE:\n** H\n"
  113. (org-test-with-temp-text "* H\n"
  114. (let ((org-archive-sibling-heading "Archive")
  115. (org-archive-tag "ARCHIVE")
  116. (org-tags-column 1))
  117. (org-archive-to-archive-sibling)
  118. (goto-char (point-min))
  119. (buffer-substring-no-properties
  120. (point) (line-beginning-position 3))))))
  121. ;; Ignore non-sibling archive headings.
  122. (should
  123. (equal "* Archive :ARCHIVE:\n* Top\n** Archive :ARCHIVE:\n*** H\n"
  124. (org-test-with-temp-text "* Archive :ARCHIVE:\n* Top\n<point>** H\n"
  125. (let ((org-archive-sibling-heading "Archive")
  126. (org-archive-tag "ARCHIVE")
  127. (org-tags-column 0))
  128. (org-archive-to-archive-sibling)
  129. (goto-char (point-min))
  130. (buffer-substring-no-properties
  131. (point) (line-beginning-position 5))))))
  132. ;; When archiving a heading, leave point on next heading.
  133. (should
  134. (equal "* H2"
  135. (org-test-with-temp-text "* H1\n* H2\n* Archive :ARCHIVE:\n"
  136. (let ((org-archive-sibling-heading "Archive")
  137. (org-archive-tag "ARCHIVE"))
  138. (org-archive-to-archive-sibling)
  139. (buffer-substring-no-properties (point) (line-end-position))))))
  140. (should
  141. (equal "* H2"
  142. (org-test-with-temp-text "* Archive :ARCHIVE:\n<point>* H1\n* H2\n"
  143. (let ((org-archive-sibling-heading "Archive")
  144. (org-archive-tag "ARCHIVE"))
  145. (org-archive-to-archive-sibling)
  146. (buffer-substring-no-properties (point) (line-end-position))))))
  147. ;; If `org-archive-reversed-order' is nil, archive as the last
  148. ;; child. Otherwise, archive as the first one.
  149. (should
  150. (equal "* Archive :ARCHIVE:\n** A\n"
  151. (org-test-with-temp-text "* H\n* Archive :ARCHIVE:\n** A\n"
  152. (let ((org-archive-sibling-heading "Archive")
  153. (org-archive-tag "ARCHIVE")
  154. (org-archive-reversed-order nil))
  155. (org-archive-to-archive-sibling)
  156. (goto-char (point-min))
  157. (buffer-substring-no-properties
  158. (point) (line-beginning-position 3))))))
  159. (should
  160. (equal "* Archive :ARCHIVE:\n** H\n"
  161. (org-test-with-temp-text "* H\n* Archive :ARCHIVE:\n** A\n"
  162. (let ((org-archive-sibling-heading "Archive")
  163. (org-archive-tag "ARCHIVE")
  164. (org-archive-reversed-order t))
  165. (org-archive-to-archive-sibling)
  166. (goto-char (point-min))
  167. (buffer-substring-no-properties
  168. (point) (line-beginning-position 3)))))))
  169. (provide 'test-org-archive)
  170. ;;; test-org-archive.el ends here