blob: 6a54d9e67ad0acfd00d92348bcb1689e75c32617 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
;;; test-org-agenda-config-category.el --- Tests for project-name category derivation -*- lexical-binding: t; -*-
;;; Commentary:
;; Tests for the agenda-display category helpers in org-agenda-config.el:
;; - cj/--org-todo-category-from-file (pure)
;; - cj/--org-set-todo-category (org-mode-hook side effect)
;;
;; Goal: when a buffer visits a project-local todo.org, its `org-category`
;; should be the parent directory name (the project slug) rather than the
;; default "todo" -- so the agenda's %c column shows "emacs.d:" instead of
;; "todo:" for every project's tasks.
;;; Code:
(require 'ert)
(require 'org)
(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(require 'org-agenda-config)
;;; ---------- cj/--org-todo-category-from-file (pure helper) ----------
;;; Normal Cases
(ert-deftest test-org-agenda-config-category-normal-emacs-d-todo ()
"Normal: todo.org under .emacs.d returns \"emacs.d\"."
(should (equal "emacs.d"
(cj/--org-todo-category-from-file
"/home/cjennings/.emacs.d/todo.org"))))
(ert-deftest test-org-agenda-config-category-normal-project-todo ()
"Normal: todo.org under a project dir returns the project basename."
(should (equal "dotemacs"
(cj/--org-todo-category-from-file
"/home/cjennings/code/dotemacs/todo.org"))))
(ert-deftest test-org-agenda-config-category-normal-deep-project ()
"Normal: deeply nested todo.org returns only the immediate parent."
(should (equal "frontend"
(cj/--org-todo-category-from-file
"/home/cjennings/projects/work/myapp/frontend/todo.org"))))
;;; Boundary Cases
(ert-deftest test-org-agenda-config-category-boundary-non-todo-file ()
"Boundary: non-todo.org file returns nil so default category stays."
(should (null (cj/--org-todo-category-from-file
"/home/cjennings/sync/org/roam/inbox.org"))))
(ert-deftest test-org-agenda-config-category-boundary-schedule-org ()
"Boundary: schedule.org returns nil; not a project todo file."
(should (null (cj/--org-todo-category-from-file
"/home/cjennings/sync/org/schedule.org"))))
(ert-deftest test-org-agenda-config-category-boundary-todo-at-fs-root ()
"Boundary: /todo.org with no real parent directory returns nil."
(should (null (cj/--org-todo-category-from-file "/todo.org"))))
(ert-deftest test-org-agenda-config-category-boundary-relative-path ()
"Boundary: bare relative \"todo.org\" with no directory returns nil."
(should (null (cj/--org-todo-category-from-file "todo.org"))))
(ert-deftest test-org-agenda-config-category-boundary-todo-with-trailing-dir ()
"Boundary: tolerate a path that is already directory-form."
(should (equal "emacs.d"
(cj/--org-todo-category-from-file
"/home/cjennings/.emacs.d/todo.org"))))
;;; Error Cases
(ert-deftest test-org-agenda-config-category-error-nil-path ()
"Error: nil PATH returns nil, no signal."
(should (null (cj/--org-todo-category-from-file nil))))
(ert-deftest test-org-agenda-config-category-error-empty-path ()
"Error: empty-string PATH returns nil."
(should (null (cj/--org-todo-category-from-file ""))))
;;; ---------- cj/--org-set-todo-category (hook function) ----------
(defmacro test-org-agenda-config-category--with-file (path body-form)
"Visit PATH in a temp buffer with org-mode active, evaluate BODY-FORM.
Sets `buffer-file-name' so the hook's lookup sees the desired path.
Suppresses other org-mode hooks to keep the test isolated."
(declare (indent 1))
`(with-temp-buffer
(let ((org-mode-hook nil)
(text-mode-hook nil))
(org-mode))
(setq buffer-file-name ,path)
;; mimic org's default category (filename-sans-extension) so the
;; hook's "only override the default" guard is exercised.
(setq-local org-category
(and ,path
(file-name-sans-extension
(file-name-nondirectory ,path))))
,body-form))
;;; Normal Cases
(ert-deftest test-org-agenda-config-category-hook-normal-overrides-todo ()
"Normal: hook overrides default \"todo\" with the parent dir name."
(test-org-agenda-config-category--with-file "/home/cjennings/.emacs.d/todo.org"
(progn
(cj/--org-set-todo-category)
(should (equal "emacs.d" org-category)))))
(ert-deftest test-org-agenda-config-category-hook-normal-leaves-inbox-alone ()
"Normal: hook leaves inbox.org's category at its filename default."
(test-org-agenda-config-category--with-file "/home/cjennings/sync/org/roam/inbox.org"
(progn
(cj/--org-set-todo-category)
(should (equal "inbox" org-category)))))
;;; Boundary Cases
(ert-deftest test-org-agenda-config-category-hook-boundary-respects-explicit ()
"Boundary: explicit category (not the filename default) is preserved."
(test-org-agenda-config-category--with-file "/home/cjennings/.emacs.d/todo.org"
(progn
(setq-local org-category "Personal")
(cj/--org-set-todo-category)
(should (equal "Personal" org-category)))))
(ert-deftest test-org-agenda-config-category-hook-boundary-nil-buffer-file-name ()
"Boundary: hook is safe in buffers with no `buffer-file-name'."
(with-temp-buffer
(let ((org-mode-hook nil)
(text-mode-hook nil))
(org-mode))
(setq buffer-file-name nil)
(cj/--org-set-todo-category)
;; no error and no spurious mutation
(should t)))
(provide 'test-org-agenda-config-category)
;;; test-org-agenda-config-category.el ends here
|