test-org-agenda.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <https://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. ;; NOTE: Be aware that `org-agenda-list' may or may not display
  58. ;; past scheduled items depending whether the date is today
  59. ;; `org-today' or not.
  60. (org-agenda-list nil "<2017-03-10 Fri>")
  61. (set-buffer org-agenda-buffer-name)
  62. (should (= 3 (count-lines (point-min) (point-max)))))
  63. (org-test-agenda--kill-all-agendas))
  64. (ert-deftest test-org-agenda/scheduled-non-todo ()
  65. "One informative line in the agenda from scheduled non-todo-keyword-item."
  66. (cl-assert (not org-agenda-sticky) nil "precondition violation")
  67. (cl-assert (not (org-test-agenda--agenda-buffers))
  68. nil "precondition violation")
  69. (let ((org-agenda-span 'day)
  70. (org-agenda-files `(,(expand-file-name "examples/agenda-file.org"
  71. org-test-dir))))
  72. ;; NOTE: Be aware that `org-agenda-list' may or may not display
  73. ;; past scheduled items depending whether the date is today
  74. ;; `org-today' or not.
  75. (org-agenda-list nil "<2017-07-19 Wed>")
  76. (set-buffer org-agenda-buffer-name)
  77. (should
  78. (progn (goto-line 3)
  79. (looking-at " *agenda-file:Scheduled: *test agenda"))))
  80. (org-test-agenda--kill-all-agendas))
  81. (ert-deftest test-org-agenda/non-scheduled-re-matces ()
  82. "Make sure that scheduled-looking elements do not appear in agenda.
  83. See https://list.orgmode.org/20220101200103.GB29829@itccanarias.org/T/#t."
  84. (cl-assert (not org-agenda-sticky) nil "precondition violation")
  85. (cl-assert (not (org-test-agenda--agenda-buffers))
  86. nil "precondition violation")
  87. (let ((org-agenda-span 'day)
  88. (org-agenda-files `(,(expand-file-name "examples/agenda-file.org"
  89. org-test-dir))))
  90. ;; NOTE: Be aware that `org-agenda-list' may or may not display
  91. ;; past scheduled items depending whether the date is today
  92. ;; `org-today' or not.
  93. (org-agenda-list nil "<2022-01-03 Mon>")
  94. (set-buffer org-agenda-buffer-name)
  95. (should (= 2 (count-lines (point-min) (point-max)))))
  96. (org-test-agenda--kill-all-agendas))
  97. (ert-deftest test-org-agenda/property-timestamp ()
  98. "Match timestamps inside property drawer.
  99. See https://list.orgmode.org/06d301d83d9e$f8b44340$ea1cc9c0$@tomdavey.com"
  100. (cl-assert (not org-agenda-sticky) nil "precondition violation")
  101. (cl-assert (not (org-test-agenda--agenda-buffers))
  102. nil "precondition violation")
  103. (let ((org-agenda-span 'day)
  104. (org-agenda-files `(,(expand-file-name "examples/agenda-file.org"
  105. org-test-dir))))
  106. ;; NOTE: Be aware that `org-agenda-list' may or may not display
  107. ;; past scheduled items depending whether the date is today
  108. ;; `org-today' or not.
  109. (org-agenda-list nil "<2022-03-22 Tue>")
  110. (set-buffer org-agenda-buffer-name)
  111. (should (= 3 (count-lines (point-min) (point-max))))
  112. (org-test-agenda--kill-all-agendas))
  113. (ert-deftest test-org-agenda/set-priority ()
  114. "One informative line in the agenda. Check that org-agenda-priority updates the agenda."
  115. (cl-assert (not org-agenda-sticky) nil "precondition violation")
  116. (cl-assert (not (org-test-agenda--agenda-buffers))
  117. nil "precondition violation")
  118. (let ((org-agenda-span 'day)
  119. (org-agenda-files `(,(expand-file-name "examples/agenda-file.org"
  120. org-test-dir))))
  121. ;; NOTE: Be aware that `org-agenda-list' may or may not display
  122. ;; past scheduled items depending whether the date is today
  123. ;; `org-today' or not.
  124. (org-agenda-list nil "<2017-07-19 Wed>")
  125. (set-buffer org-agenda-buffer-name)
  126. (should
  127. (progn (goto-line 3)
  128. (org-agenda-priority ?B)
  129. (looking-at-p " *agenda-file:Scheduled: *\\[#B\\] test agenda"))))
  130. (org-test-agenda--kill-all-agendas))
  131. (ert-deftest test-org-agenda/sticky-agenda-name ()
  132. "Agenda buffer name after having created one sticky agenda buffer."
  133. (cl-assert (not org-agenda-sticky) nil "precondition violation")
  134. (cl-assert (not (org-test-agenda--agenda-buffers))
  135. nil "precondition violation")
  136. (let ((org-agenda-span 'day)
  137. (buf (get-buffer org-agenda-buffer-name))
  138. org-agenda-files)
  139. (when buf (kill-buffer buf))
  140. (dolist (fn '(org-agenda-list org-todo-list))
  141. (org-test-with-temp-text "<2017-03-17 Fri>"
  142. (org-follow-timestamp-link)) ;creates a sticky agenda
  143. (org-test-agenda--kill-all-agendas)
  144. (funcall fn)
  145. (should (= 1 (length (org-test-agenda--agenda-buffers))))
  146. (should (string= "*Org Agenda*"
  147. (buffer-name (car (org-test-agenda--agenda-buffers)))))))
  148. (org-test-agenda--kill-all-agendas))
  149. (ert-deftest test-org-agenda/sticky-agenda-name-after-reload ()
  150. "Agenda buffer name of sticky agenda after reload."
  151. (cl-assert (not org-agenda-sticky) nil "precondition violation")
  152. (cl-assert (not (org-test-agenda--agenda-buffers))
  153. nil "precondition violation")
  154. (org-toggle-sticky-agenda)
  155. (let (org-agenda-files)
  156. (org-agenda-list)
  157. (let* ((agenda-buffer-name
  158. (progn
  159. (cl-assert (= 1 (length (org-test-agenda--agenda-buffers))))
  160. (buffer-name (car (org-test-agenda--agenda-buffers))))))
  161. (set-buffer agenda-buffer-name)
  162. (org-agenda-redo)
  163. (should (= 1 (length (org-test-agenda--agenda-buffers))))
  164. (should (string= agenda-buffer-name
  165. (buffer-name (car (org-test-agenda--agenda-buffers)))))))
  166. (org-toggle-sticky-agenda)
  167. (org-test-agenda--kill-all-agendas))
  168. (ert-deftest test-org-agenda/goto-date ()
  169. "Test `org-agenda-goto-date'."
  170. (unwind-protect
  171. (should
  172. (equal
  173. (time-to-days (org-time-string-to-time "2019-12-30"))
  174. (let ((org-agenda-files nil))
  175. (org-agenda-list nil nil 'day)
  176. (org-agenda-goto-date "2019-12-30")
  177. (get-text-property (point) 'day))))
  178. (org-test-agenda--kill-all-agendas)))
  179. ;; agenda redo
  180. (require 'face-remap)
  181. (ert-deftest test-org-agenda/rescale ()
  182. "Text scale survives `org-agenda-redo'."
  183. (org-test-agenda--kill-all-agendas)
  184. (unwind-protect
  185. (let ((org-agenda-span 'day)
  186. org-agenda-files)
  187. (org-agenda-list)
  188. (set-buffer org-agenda-buffer-name)
  189. (text-scale-mode)
  190. (text-scale-set 11)
  191. (cl-assert (and (boundp text-scale-mode) text-scale-mode))
  192. (org-agenda-redo)
  193. (should text-scale-mode)
  194. (should (= 11 text-scale-mode-amount)))
  195. (org-test-agenda--kill-all-agendas)))
  196. (ert-deftest test-org-agenda/diary-inclusion ()
  197. "Diary inclusion happens."
  198. (org-test-agenda--kill-all-agendas)
  199. (let ((diary-file (expand-file-name "examples/diary-file" org-test-dir))
  200. (org-agenda-files `(,(expand-file-name "examples/agenda-file.org"
  201. org-test-dir)))
  202. (diary-date-forms '((month "[-/]" day "[^-/0-9]")
  203. (year "[-/]" month "[-/]" day "[^0-9]")
  204. (monthname " *" day "[^-0-9]")
  205. (year " *" monthname " *" day "[^0-9]")
  206. (dayname "\\W")))
  207. (org-agenda-span 'day)
  208. (org-agenda-include-diary t))
  209. ;; NOTE: Be aware that `org-agenda-list' may or may not display
  210. ;; past scheduled items depending whether the date is today
  211. ;; `org-today' or not.
  212. (org-agenda-list nil "<2019-01-08>")
  213. (should (search-forward "f0bcf0cd8bad93c1451bb6e1b2aaedef5cce7cbb" nil t))
  214. (org-test-agenda--kill-all-agendas)))
  215. ;; agenda bulk actions
  216. (ert-deftest test-org-agenda/bulk ()
  217. "Bulk actions are applied to marked items."
  218. (org-test-agenda-with-agenda "* TODO a\n* TODO b"
  219. (org-todo-list)
  220. (org-agenda-bulk-mark-all)
  221. (cl-letf (((symbol-function 'read-char-exclusive)
  222. (lambda () ?t))
  223. ((symbol-function 'completing-read)
  224. (lambda (&rest rest) "DONE")))
  225. (org-agenda-bulk-action))
  226. (org-agenda-previous-item 99)
  227. (should (looking-at ".*DONE a"))
  228. (org-agenda-next-item 1)
  229. (should (looking-at ".*DONE b"))))
  230. (ert-deftest test-org-agenda/bulk-custom ()
  231. "Custom bulk actions are applied to all marked items."
  232. (org-test-agenda-with-agenda "* TODO a\n* TODO b"
  233. (org-todo-list)
  234. (org-agenda-bulk-mark-all)
  235. ;; Mock read functions
  236. (let* ((f-call-cnt 0)
  237. (org-agenda-bulk-custom-functions
  238. `((?P ,(lambda () (setq f-call-cnt (1+ f-call-cnt)))))))
  239. (cl-letf* (((symbol-function 'read-char-exclusive)
  240. (lambda () ?P)))
  241. (org-agenda-bulk-action)
  242. (should (= f-call-cnt 2))))))
  243. (ert-deftest test-org-agenda/bulk-custom-arg-func ()
  244. "Argument collection functions can be specified for custom bulk
  245. functions."
  246. (org-test-agenda-with-agenda "* TODO a\n* TODO b"
  247. (org-todo-list)
  248. (org-agenda-bulk-mark-all)
  249. (let* ((f-called-cnt 0)
  250. (arg-f-call-cnt 0)
  251. (f-called-args nil)
  252. (org-agenda-bulk-custom-functions
  253. `((?P
  254. ;; Custom bulk function
  255. ,(lambda (&rest args)
  256. (message "test" args)
  257. (setq f-called-cnt (1+ f-called-cnt)
  258. f-called-args args))
  259. ;; Argument collection function
  260. ,(lambda ()
  261. (setq arg-f-call-cnt (1+ arg-f-call-cnt))
  262. '(1 2 3))))))
  263. (cl-letf (((symbol-function 'read-char-exclusive)
  264. (lambda () ?P)))
  265. (org-agenda-bulk-action))
  266. (should (= f-called-cnt 2))
  267. (should (= arg-f-call-cnt 1))
  268. (should (equal f-called-args '(1 2 3))))))
  269. (provide 'test-org-agenda)
  270. ;;; test-org-agenda.el ends here