aboutsummaryrefslogtreecommitdiff
path: root/tests/test-org-drill-card-presenters.el
blob: 189f9a1e845802a124726079e7a1cabb9e8965a7 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
;;; test-org-drill-card-presenters.el --- Tests for card presentation functions  -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests for the functions that show a card to the user:
;;
;; - `org-drill-present-simple-card': default presenter — hides cloze
;;   text, hides comments, hides drawers, hides subheadings, then
;;   prompts.
;; - `org-drill-present-default-answer': reveals the answer.  Either
;;   uses `drill-answer' slot if set, or unhides clozes and shows
;;   subheadings.
;; - `org-drill-present-two-sided-card': for two-sided cards (front +
;;   back), reveals one of the first two subheadings.
;; - `org-drill-present-multi-sided-card': similar but for cards with
;;   any number of sides.
;; - `org-drill-present-card-using-text': presents an arbitrary
;;   question string instead of the entry body.
;;
;; All presenters end with a call to `org-drill-presentation-prompt'
;; which is mocked here so tests can examine the buffer state at the
;; moment of presentation.

;;; Code:

(require 'ert)
(require 'cl-lib)
(require 'org)
(require 'org-drill)

;;;; Helpers

(defmacro with-card-buffer (content &rest body)
  (declare (indent 1))
  `(with-temp-buffer
     (let ((org-startup-folded nil))
       (insert ,content)
       (org-mode)
       (goto-char (point-min))
       (setq-local org-drill-cloze-regexp (org-drill--compute-cloze-regexp))
       ,@body)))

(defmacro with-mocked-presentation (return-val &rest body)
  "Run BODY with prompt + LaTeX + inline-images stubbed for batch."
  (declare (indent 1))
  `(cl-letf (((symbol-function 'org-drill-presentation-prompt)
              (lambda (&rest _) ,return-val))
             ((symbol-function 'org-drill-presentation-prompt-for-string)
              (lambda (&rest _) ,return-val))
             ((symbol-function 'org-drill--show-latex-fragments) #'ignore)
             ((symbol-function 'org-display-inline-images) #'ignore)
             ((symbol-function 'org-clear-latex-preview) #'ignore)
             ((symbol-function 'org--latex-preview-region) #'ignore))
     ,@body))

(defun count-overlays-of-category (cat)
  (let ((n 0))
    (dolist (ovl (overlays-in (point-min) (point-max)))
      (when (eql cat (overlay-get ovl 'category))
        (cl-incf n)))
    n))

(defvar overlays-snapshot nil
  "Captured cloze overlay count at the moment the prompt fires.")

(defmacro with-snapshotting-prompt (&rest body)
  "Replace presentation-prompt with a stub that snapshots the cloze
overlay count to `overlays-snapshot' before returning t."
  `(cl-letf (((symbol-function 'org-drill-presentation-prompt)
              (lambda (&rest _)
                (setq overlays-snapshot
                      (count-overlays-of-category 'org-drill-cloze-overlay-defaults))
                t))
             ((symbol-function 'org-drill--show-latex-fragments) #'ignore)
             ((symbol-function 'org-display-inline-images) #'ignore))
     (setq overlays-snapshot nil)
     ,@body))

;;;; org-drill-present-simple-card

(ert-deftest test-present-simple-card-hides-cloze-text-during-prompt ()
  "On a card with a cloze body, presentation-prompt sees the cloze hidden."
  (with-card-buffer "* Question :drill:
What is the capital? [Paris]
"
    (with-snapshotting-prompt
     (org-drill-present-simple-card (org-drill-session)))
    (should (= 1 overlays-snapshot))))

(ert-deftest test-present-simple-card-returns-prompt-result ()
  (with-card-buffer "* Question :drill:
plain body
"
    (with-mocked-presentation 'edit
      (let ((result (org-drill-present-simple-card (org-drill-session))))
        (should (eq 'edit result))))))

(ert-deftest test-present-simple-card-restores-cloze-overlays-after-prompt ()
  "After the prompt returns, the cloze overlays are gone — `unhide-clozed-text'
runs as part of the unwind-protect macro `with-hidden-cloze-text'."
  (with-card-buffer "* Question :drill:
The answer is [hidden].
"
    (with-mocked-presentation t
      (org-drill-present-simple-card (org-drill-session)))
    (should (= 0 (count-overlays-of-category 'org-drill-cloze-overlay-defaults)))))

;;;; org-drill-present-default-answer

(ert-deftest test-present-default-answer-with-drill-answer-slot ()
  "When session->drill-answer is set, that text is overlay-displayed instead
of the entry's body."
  (with-card-buffer "* Question :drill:
hidden body
"
    (let ((session (org-drill-session))
          (reschedule-called nil))
      (oset session drill-answer "Forty-two")
      (with-mocked-presentation t
        (cl-letf (((symbol-function 'org-mark-subtree) #'ignore)
                  ((symbol-function 'deactivate-mark) #'ignore))
          (org-drill-present-default-answer session
            (lambda (_) (setq reschedule-called t)))))
      (should reschedule-called))))

(ert-deftest test-present-default-answer-without-drill-answer-slot ()
  "Without a drill-answer slot, the function takes the unhide path
and calls reschedule-fn.

`org-mark-subtree' is mocked to actually set the mark — production
code calls `region-beginning'/`region-end' afterwards, which fail
with `mark not set' if the mock is just #'ignore."
  (with-card-buffer "* Question :drill:
body content
"
    (let ((session (org-drill-session))
          (reschedule-called nil))
      (oset session drill-answer nil)
      (with-mocked-presentation t
        (cl-letf (((symbol-function 'org-mark-subtree)
                   (lambda (&rest _)
                     (push-mark (point-min) t t)
                     (goto-char (point-max))))
                  ((symbol-function 'deactivate-mark) #'ignore))
          (org-drill-present-default-answer session
            (lambda (_) (setq reschedule-called t)))))
      (should reschedule-called))))

;;;; org-drill-present-card-using-text

(ert-deftest test-present-card-using-text-replaces-entry-body ()
  "The provided question text is shown via a replaced-text overlay."
  (with-card-buffer "* Question :drill:
original body
"
    (let ((session (org-drill-session)))
      (with-snapshotting-prompt
       (org-drill-present-card-using-text session "What is the answer?"))
      ;; replaced-text overlay should have been present at prompt time.
      ;; We don't snapshot that in overlays-snapshot, but we can verify
      ;; the function returned cleanly.
      (should t))))

(ert-deftest test-present-card-using-text-sets-drill-answer-when-given ()
  "If an ANSWER arg is supplied, it goes into the session's drill-answer slot."
  (with-card-buffer "* Question :drill:
original body
"
    (let ((session (org-drill-session)))
      (with-mocked-presentation t
        (org-drill-present-card-using-text session "Q" "A"))
      (should (equal "A" (oref session drill-answer))))))

;;;; org-drill-present-two-sided-card

(ert-deftest test-present-two-sided-card-runs-without-error ()
  "On a two-sided card with two subheadings, runs cleanly."
  (with-card-buffer "* Card :drill:
:PROPERTIES:
:DRILL_CARD_TYPE: twosided
:END:

** English
hello
** Spanish
hola
"
    (with-mocked-presentation t
      (let ((result (org-drill-present-two-sided-card (org-drill-session))))
        (should (eq t result))))))

(ert-deftest test-present-multi-sided-card-runs-without-error ()
  (with-card-buffer "* Card :drill:
:PROPERTIES:
:DRILL_CARD_TYPE: multisided
:END:

** Side A
content A
** Side B
content B
** Side C
content C
"
    (with-mocked-presentation t
      (let ((result (org-drill-present-multi-sided-card (org-drill-session))))
        (should (eq t result))))))

(provide 'test-org-drill-card-presenters)

;;; test-org-drill-card-presenters.el ends here