blob: 5732b63a6d040d55fb66a742a72f3264e66e0c22 (
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
;;; test-org-drill-statistics-source-buffer.el --- Dashboard source-buffer tests -*- lexical-binding: t; -*-
;;; Commentary:
;; ERT tests for the statistics dashboard's source-buffer plumbing and
;; the org-drill-card link type. The dashboard must scan the deck the
;; command was invoked from, not its own buffer, and its card links must
;; be followable.
;;; Code:
(require 'ert)
(require 'org-drill)
(require 'org)
(defmacro org-drill-statistics-source-test--with-deck (var &rest body)
"Run BODY with VAR bound to a live org deck buffer holding two drill cards."
(declare (indent 1))
`(let ((,var (generate-new-buffer "*source-deck*")))
(unwind-protect
(with-current-buffer ,var
(org-mode)
(insert "* Card one :drill:\nBody one.\n"
"* Card two :drill:\nBody two.\n")
(goto-char (point-min))
,@body)
(when (buffer-live-p ,var) (kill-buffer ,var)))))
;;; Normal cases
(ert-deftest test-org-drill-statistics-render-counts-source-deck-cards ()
"Rendering into the dashboard counts the cards of the source deck.
With the default file scope, the card scans must run in the source
buffer, not the dashboard buffer being rendered into."
(org-drill-statistics-source-test--with-deck deck
(let ((dash (generate-new-buffer "*dash-test*"))
(org-drill-scope 'file)
(org-drill-session-log nil))
(unwind-protect
(progn
(with-current-buffer dash (org-mode))
(org-drill-statistics--render dash 'file "last 90d" nil deck)
(with-current-buffer dash
(goto-char (point-min))
;; Overview table data row: | total | new | mature | lapsed |
(should (search-forward "| 2 | " nil t))))
(kill-buffer dash)))))
(ert-deftest test-org-drill-statistics-refresh-uses-stored-source ()
"Refresh from inside the dashboard re-scans the stored source deck."
(org-drill-statistics-source-test--with-deck deck
(let ((dash (generate-new-buffer "*dash-test*"))
(org-drill-scope 'file)
(org-drill-session-log nil))
(unwind-protect
(progn
(with-current-buffer dash (org-mode))
(org-drill-statistics--render dash 'file "last 90d" nil deck)
(with-current-buffer dash
(org-drill-statistics-refresh)
(goto-char (point-min))
(should (search-forward "| 2 | " nil t))))
(kill-buffer dash)))))
(ert-deftest test-org-drill-statistics-card-link-type-is-registered ()
"The org-drill-card link type has a follow handler registered."
(should (functionp (org-link-get-parameter "org-drill-card" :follow))))
(ert-deftest test-org-drill-statistics-card-link-encodes-file ()
"Card links carry the file identity after the position."
(let ((link (org-drill-statistics--card-link "Heading" 42 "/tmp/deck.org")))
(should (string-prefix-p "[[org-drill-card:42@/tmp/deck.org][" link))))
(ert-deftest test-org-drill-statistics-follow-jumps-to-file-position ()
"Following a card link visits the file and moves point to the position."
(let* ((file (make-temp-file "org-drill-follow-test" nil ".org"
"* Card one :drill:\nBody.\n"))
(buf nil))
(unwind-protect
(progn
(org-drill-statistics--follow-card-link (format "1@%s" file))
(setq buf (current-buffer))
(should (equal (buffer-file-name) file))
(should (= (point) 1)))
(when (and buf (buffer-live-p buf)) (kill-buffer buf))
(delete-file file))))
;;; Boundary cases
(ert-deftest test-org-drill-statistics-render-empty-deck-counts-zero ()
"An empty source deck renders a zero-card overview, not an error."
(let ((deck (generate-new-buffer "*empty-deck*"))
(dash (generate-new-buffer "*dash-test*"))
(org-drill-scope 'file)
(org-drill-session-log nil))
(unwind-protect
(progn
(with-current-buffer deck (org-mode))
(with-current-buffer dash (org-mode))
(org-drill-statistics--render dash 'file "last 90d" nil deck)
(with-current-buffer dash
(goto-char (point-min))
(should (search-forward "| 0 | " nil t))))
(kill-buffer deck)
(kill-buffer dash))))
(ert-deftest test-org-drill-statistics-card-link-empty-file-omits-identity ()
"A card link built with no file still carries the position."
(let ((link (org-drill-statistics--card-link "Heading" 7 nil)))
(should (string-prefix-p "[[org-drill-card:7@][" link))))
(ert-deftest test-org-drill-statistics-export-cards-from-source-deck ()
"CSV export scans the stored source deck, not the dashboard buffer."
(org-drill-statistics-source-test--with-deck deck
(let ((dash (generate-new-buffer "*dash-test*"))
(dir (make-temp-file "org-drill-export-test" t))
(org-drill-scope 'file)
(org-drill-session-log nil))
(unwind-protect
(progn
(with-current-buffer dash (org-mode))
(org-drill-statistics--render dash 'file "last 90d" nil deck)
(with-current-buffer dash
(org-drill-statistics-export-csv dir))
(with-temp-buffer
(insert-file-contents (expand-file-name "cards.csv" dir))
;; Header plus one row per deck card.
(should (= 3 (count-lines (point-min) (point-max))))))
(kill-buffer dash)
(delete-directory dir t)))))
(ert-deftest test-org-drill-statistics-follow-clamps-position-floor ()
"A zero or garbage position clamps to buffer start instead of erroring."
(let* ((file (make-temp-file "org-drill-follow-clamp" nil ".org"
"* Card :drill:\n"))
(buf nil))
(unwind-protect
(progn
(org-drill-statistics--follow-card-link (format "0@%s" file))
(setq buf (current-buffer))
(should (= (point) 1)))
(when (and buf (buffer-live-p buf)) (kill-buffer buf))
(delete-file file))))
;;; Error cases
(ert-deftest test-org-drill-statistics-refresh-dead-source-signals-user-error ()
"Refresh after the source deck is killed signals a clear user-error."
(let ((dash (generate-new-buffer "*dash-test*"))
(deck (generate-new-buffer "*doomed-deck*"))
(org-drill-scope 'file)
(org-drill-session-log nil))
(unwind-protect
(progn
(with-current-buffer deck
(org-mode)
(insert "* Card :drill:\n"))
(with-current-buffer dash (org-mode))
(org-drill-statistics--render dash 'file "last 90d" nil deck)
(kill-buffer deck)
(with-current-buffer dash
(should-error (org-drill-statistics-refresh)
:type 'user-error)))
(when (buffer-live-p deck) (kill-buffer deck))
(kill-buffer dash))))
(ert-deftest test-org-drill-statistics-follow-dead-source-signals-user-error ()
"Following a bufferless link with no live source signals a user-error."
(with-temp-buffer
;; No dashboard source var set in this buffer; empty file identity.
(should-error (org-drill-statistics--follow-card-link "5@")
:type 'user-error)))
(provide 'test-org-drill-statistics-source-buffer)
;;; test-org-drill-statistics-source-buffer.el ends here
|