test-org-agenda.el 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ;;; test-org-agenda.el --- Tests for org-agenda.el -*- lexical-binding: t ; -*-
  2. ;; Copyright (C) 2017, 2019 Marco Wahl
  3. ;; Author: Marco Wahl <marcowahlsoft@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 <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;; Unit tests for Org Agenda.
  16. ;;; Code:
  17. (require 'org-test "../testing/org-test")
  18. (require 'org-agenda)
  19. (eval-and-compile (require 'cl-lib))
  20. ;; General auxiliaries
  21. (defun org-test-agenda--agenda-buffers ()
  22. "Return agenda buffers in a list."
  23. (cl-remove-if-not (lambda (x)
  24. (with-current-buffer x
  25. (eq major-mode 'org-agenda-mode)))
  26. (buffer-list)))
  27. (defun org-test-agenda--kill-all-agendas ()
  28. "Kill all agenda buffers."
  29. (mapc #'kill-buffer
  30. (org-test-agenda--agenda-buffers)))
  31. (defmacro org-test-agenda-with-agenda (text &rest body)
  32. (declare (indent 1))
  33. `(org-test-with-temp-text-in-file ,text
  34. (let ((org-agenda-files `(,buffer-file-name)))
  35. ,@body
  36. (org-test-agenda--kill-all-agendas))))
  37. ;; Test the Agenda
  38. (ert-deftest test-org-agenda/empty ()
  39. "Empty agenda."
  40. (cl-assert (not org-agenda-sticky) nil "precondition violation")
  41. (cl-assert (not (org-test-agenda--agenda-buffers))
  42. nil "precondition violation")
  43. (let ((org-agenda-span 'day)
  44. org-agenda-files)
  45. (org-agenda-list)
  46. (set-buffer org-agenda-buffer-name)
  47. (should (= 2 (count-lines (point-min) (point-max)))))
  48. (org-test-agenda--kill-all-agendas))
  49. (ert-deftest test-org-agenda/one-line ()
  50. "One informative line in the agenda."
  51. (cl-assert (not org-agenda-sticky) nil "precondition violation")
  52. (cl-assert (not (org-test-agenda--agenda-buffers))
  53. nil "precondition violation")
  54. (let ((org-agenda-span 'day)
  55. (org-agenda-files `(,(expand-file-name "examples/agenda-file.org"
  56. org-test-dir))))
  57. (org-agenda-list nil "<2017-03-10 Fri>")
  58. (set-buffer org-agenda-buffer-name)
  59. (should (= 3 (count-lines (point-min) (point-max)))))
  60. (org-test-agenda--kill-all-agendas))
  61. (ert-deftest test-org-agenda/scheduled-non-todo ()
  62. "One informative line in the agenda from scheduled non-todo-keyword-item."
  63. (cl-assert (not org-agenda-sticky) nil "precondition violation")
  64. (cl-assert (not (org-test-agenda--agenda-buffers))
  65. nil "precondition violation")
  66. (let ((org-agenda-span 'day)
  67. (org-agenda-files `(,(expand-file-name "examples/agenda-file.org"
  68. org-test-dir))))
  69. (org-agenda-list nil "<2017-07-19 Wed>")
  70. (set-buffer org-agenda-buffer-name)
  71. (should
  72. (progn (goto-line 3)
  73. (looking-at " *agenda-file:Scheduled: *test agenda"))))
  74. (org-test-agenda--kill-all-agendas))
  75. (ert-deftest test-org-agenda/set-priority ()
  76. "One informative line in the agenda. Check that org-agenda-priority updates the agenda."
  77. (cl-assert (not org-agenda-sticky) nil "precondition violation")
  78. (cl-assert (not (org-test-agenda--agenda-buffers))
  79. nil "precondition violation")
  80. (let ((org-agenda-span 'day)
  81. (org-agenda-files `(,(expand-file-name "examples/agenda-file.org"
  82. org-test-dir))))
  83. (org-agenda-list nil "<2017-07-19 Wed>")
  84. (set-buffer org-agenda-buffer-name)
  85. (should
  86. (progn (goto-line 3)
  87. (org-agenda-priority ?B)
  88. (looking-at-p " *agenda-file:Scheduled: *\\[#B\\] test agenda"))))
  89. (org-test-agenda--kill-all-agendas))
  90. (ert-deftest test-org-agenda/sticky-agenda-name ()
  91. "Agenda buffer name after having created one sticky agenda buffer."
  92. (cl-assert (not org-agenda-sticky) nil "precondition violation")
  93. (cl-assert (not (org-test-agenda--agenda-buffers))
  94. nil "precondition violation")
  95. (let ((org-agenda-span 'day)
  96. (buf (get-buffer org-agenda-buffer-name))
  97. org-agenda-files)
  98. (when buf (kill-buffer buf))
  99. (dolist (fn '(org-agenda-list org-todo-list))
  100. (org-test-with-temp-text "<2017-03-17 Fri>"
  101. (org-follow-timestamp-link)) ;creates a sticky agenda
  102. (org-test-agenda--kill-all-agendas)
  103. (funcall fn)
  104. (should (= 1 (length (org-test-agenda--agenda-buffers))))
  105. (should (string= "*Org Agenda*"
  106. (buffer-name (car (org-test-agenda--agenda-buffers)))))))
  107. (org-test-agenda--kill-all-agendas))
  108. (ert-deftest test-org-agenda/sticky-agenda-name-after-reload ()
  109. "Agenda buffer name of sticky agenda after reload."
  110. (cl-assert (not org-agenda-sticky) nil "precondition violation")
  111. (cl-assert (not (org-test-agenda--agenda-buffers))
  112. nil "precondition violation")
  113. (org-toggle-sticky-agenda)
  114. (let (org-agenda-files)
  115. (org-agenda-list)
  116. (let* ((agenda-buffer-name
  117. (progn
  118. (cl-assert (= 1 (length (org-test-agenda--agenda-buffers))))
  119. (buffer-name (car (org-test-agenda--agenda-buffers))))))
  120. (set-buffer agenda-buffer-name)
  121. (org-agenda-redo)
  122. (should (= 1 (length (org-test-agenda--agenda-buffers))))
  123. (should (string= agenda-buffer-name
  124. (buffer-name (car (org-test-agenda--agenda-buffers)))))))
  125. (org-toggle-sticky-agenda)
  126. (org-test-agenda--kill-all-agendas))
  127. (ert-deftest test-org-agenda/goto-date ()
  128. "Test `org-agenda-goto-date'."
  129. (unwind-protect
  130. (should
  131. (equal
  132. (time-to-days (org-time-string-to-time "2019-12-30"))
  133. (let ((org-agenda-files nil))
  134. (org-agenda-list nil nil 'day)
  135. (org-agenda-goto-date "2019-12-30")
  136. (get-text-property (point) 'day))))
  137. (org-test-agenda--kill-all-agendas)))
  138. ;; agenda redo
  139. (require 'face-remap)
  140. (ert-deftest test-org-agenda/rescale ()
  141. "Text scale survives `org-agenda-redo'."
  142. (org-test-agenda--kill-all-agendas)
  143. (unwind-protect
  144. (let ((org-agenda-span 'day)
  145. org-agenda-files)
  146. (org-agenda-list)
  147. (set-buffer org-agenda-buffer-name)
  148. (text-scale-mode)
  149. (text-scale-set 11)
  150. (cl-assert (and (boundp text-scale-mode) text-scale-mode))
  151. (org-agenda-redo)
  152. (should text-scale-mode)
  153. (should (= 11 text-scale-mode-amount)))
  154. (org-test-agenda--kill-all-agendas)))
  155. (ert-deftest test-org-agenda/diary-inclusion ()
  156. "Diary inclusion happens."
  157. (org-test-agenda--kill-all-agendas)
  158. (let ((diary-file (expand-file-name "examples/diary-file" org-test-dir))
  159. (org-agenda-files `(,(expand-file-name "examples/agenda-file.org"
  160. org-test-dir)))
  161. (diary-date-forms '((month "[-/]" day "[^-/0-9]")
  162. (year "[-/]" month "[-/]" day "[^0-9]")
  163. (monthname " *" day "[^-0-9]")
  164. (year " *" monthname " *" day "[^0-9]")
  165. (dayname "\\W")))
  166. (org-agenda-span 'day)
  167. (org-agenda-include-diary t))
  168. (org-agenda-list nil "<2019-01-08>")
  169. (should (search-forward "f0bcf0cd8bad93c1451bb6e1b2aaedef5cce7cbb" nil t))
  170. (org-test-agenda--kill-all-agendas)))
  171. ;; agenda bulk actions
  172. (ert-deftest test-org-agenda/bulk ()
  173. "Bulk actions are applied to marked items."
  174. (org-test-agenda-with-agenda "* TODO a\n* TODO b"
  175. (org-todo-list)
  176. (org-agenda-bulk-mark-all)
  177. (cl-letf (((symbol-function 'read-char-exclusive)
  178. (lambda () ?t))
  179. ((symbol-function 'completing-read)
  180. (lambda (&rest rest) "DONE")))
  181. (org-agenda-bulk-action))
  182. (org-agenda-previous-item 99)
  183. (should (looking-at ".*DONE a"))
  184. (org-agenda-next-item 1)
  185. (should (looking-at ".*DONE b"))))
  186. (ert-deftest test-org-agenda/bulk-custom ()
  187. "Custom bulk actions are applied to all marked items."
  188. (org-test-agenda-with-agenda "* TODO a\n* TODO b"
  189. (org-todo-list)
  190. (org-agenda-bulk-mark-all)
  191. ;; Mock read functions
  192. (let* ((f-call-cnt 0)
  193. (org-agenda-bulk-custom-functions
  194. `((?P ,(lambda () (setq f-call-cnt (1+ f-call-cnt)))))))
  195. (cl-letf* (((symbol-function 'read-char-exclusive)
  196. (lambda () ?P)))
  197. (org-agenda-bulk-action)
  198. (should (= f-call-cnt 2))))))
  199. (ert-deftest test-org-agenda/bulk-custom-arg-func ()
  200. "Argument collection functions can be specified for custom bulk
  201. functions."
  202. (org-test-agenda-with-agenda "* TODO a\n* TODO b"
  203. (org-todo-list)
  204. (org-agenda-bulk-mark-all)
  205. (let* ((f-called-cnt 0)
  206. (arg-f-call-cnt 0)
  207. (f-called-args nil)
  208. (org-agenda-bulk-custom-functions
  209. `((?P
  210. ;; Custom bulk function
  211. ,(lambda (&rest args)
  212. (message "test" args)
  213. (setq f-called-cnt (1+ f-called-cnt)
  214. f-called-args args))
  215. ;; Argument collection function
  216. ,(lambda ()
  217. (setq arg-f-call-cnt (1+ arg-f-call-cnt))
  218. '(1 2 3))))))
  219. (cl-letf (((symbol-function 'read-char-exclusive)
  220. (lambda () ?P)))
  221. (org-agenda-bulk-action))
  222. (should (= f-called-cnt 2))
  223. (should (= arg-f-call-cnt 1))
  224. (should (equal f-called-args '(1 2 3))))))
  225. (provide 'test-org-agenda)
  226. ;;; test-org-agenda.el ends here