aboutsummaryrefslogtreecommitdiff
path: root/tests/test-org-drill-language-presenters.el
blob: da0a3a83b52e8d19f2c364cf9d1e9285efbe0f81 (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
;;; test-org-drill-language-presenters.el --- Tests for verb/noun/Spanish presenters  -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests for the language-card presenters that wrap
;; `org-drill-present-card-using-text' and `with-replaced-entry-heading'
;; for verb conjugation and noun declension cards.

;;; 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))
       ,@body)))

(defmacro with-mocked-presentation (return-val &rest body)
  (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))
     ,@body))

;;;; org-drill-present-verb-conjugation

(ert-deftest test-present-verb-conjugation-runs-cleanly ()
  "On a verb card with all required properties, runs through to the prompt."
  (with-card-buffer "* Verb :drill:
:PROPERTIES:
:DRILL_CARD_TYPE: conjugate
:VERB_INFINITIVE: \"hablar\"
:VERB_TRANSLATION: \"to speak\"
:VERB_TENSE: \"present\"
:END:
"
    (with-mocked-presentation t
      (let ((result (org-drill-present-verb-conjugation (org-drill-session))))
        (should (eq t result))))))

(ert-deftest test-present-verb-conjugation-tense-and-mood ()
  "Cards with both tense and mood produce a `tense, mood' string."
  (with-card-buffer "* Verb :drill:
:PROPERTIES:
:VERB_INFINITIVE: \"hablar\"
:VERB_TRANSLATION: \"to speak\"
:VERB_TENSE: \"future\"
:VERB_MOOD: \"subjunctive\"
:END:
"
    (let ((shown-question nil))
      (cl-letf (((symbol-function 'org-drill-present-card-using-text)
                 (lambda (_session question &rest _)
                   (setq shown-question question) t))
                ((symbol-function 'cl-random) (lambda (_) 0)))
        (org-drill-present-verb-conjugation (org-drill-session))
        (should (string-match-p "future tense, subjunctive mood" shown-question))))))

;;;; org-drill-show-answer-verb-conjugation

(ert-deftest test-show-answer-verb-conjugation-runs-cleanly ()
  "Answer presenter for verbs runs cleanly."
  (with-card-buffer "* Verb :drill:
:PROPERTIES:
:VERB_INFINITIVE: \"hablar\"
:VERB_TRANSLATION: \"to speak\"
:VERB_TENSE: \"present\"
:END:
"
    (let ((reschedule-called nil))
      (with-mocked-presentation t
        (org-drill-show-answer-verb-conjugation
         (org-drill-session)
         (lambda (_) (setq reschedule-called t))))
      (should reschedule-called))))

;;;; org-drill-present-noun-declension

(ert-deftest test-present-noun-declension-runs-cleanly ()
  (with-card-buffer "* Noun :drill:
:PROPERTIES:
:DRILL_CARD_TYPE: decline_noun
:NOUN: \"casa\"
:NOUN_GENDER: \"feminine\"
:NOUN_TRANSLATION: \"house\"
:END:
"
    (with-mocked-presentation t
      (let ((result (org-drill-present-noun-declension (org-drill-session))))
        (should (eq t result))))))

(ert-deftest test-present-noun-declension-with-definite-property ()
  "DECLINE_DEFINITE property adds `definite/indefinite' to the question text."
  (with-card-buffer "* Noun :drill:
:PROPERTIES:
:NOUN: \"casa\"
:NOUN_GENDER: \"feminine\"
:NOUN_TRANSLATION: \"house\"
:DECLINE_DEFINITE: t
:END:
"
    (let ((shown-question nil))
      (cl-letf (((symbol-function 'org-drill-present-card-using-text)
                 (lambda (_session question &rest _)
                   (setq shown-question question) t))
                ((symbol-function 'cl-random) (lambda (_) 0)))
        (org-drill-present-noun-declension (org-drill-session))
        (should (string-match-p "definite" shown-question))))))

(ert-deftest test-present-noun-declension-without-extras-skips-decline-suffix ()
  "Without DECLINE_DEFINITE / DECLINE_PLURAL, no suffix is appended."
  (with-card-buffer "* Noun :drill:
:PROPERTIES:
:NOUN: \"casa\"
:NOUN_GENDER: \"feminine\"
:NOUN_TRANSLATION: \"house\"
:END:
"
    (let ((shown-question nil))
      (cl-letf (((symbol-function 'org-drill-present-card-using-text)
                 (lambda (_session question &rest _)
                   (setq shown-question question) t))
                ((symbol-function 'cl-random) (lambda (_) 0)))
        (org-drill-present-noun-declension (org-drill-session))
        (should-not (string-match-p "definite" shown-question))
        (should-not (string-match-p "plural" shown-question))))))

;;;; org-drill-show-answer-noun-declension

(ert-deftest test-show-answer-noun-declension-runs-cleanly ()
  (with-card-buffer "* Noun :drill:
:PROPERTIES:
:NOUN: \"casa\"
:NOUN_GENDER: \"feminine\"
:NOUN_TRANSLATION: \"house\"
:END:
"
    (let ((reschedule-called nil))
      (with-mocked-presentation t
        (org-drill-show-answer-noun-declension
         (org-drill-session)
         (lambda (_) (setq reschedule-called t))))
      (should reschedule-called))))

;;;; org-drill--format-tense-mood

(ert-deftest test-org-drill-format-tense-mood-both ()
  "With both tense and mood, the label names both."
  (should (equal "past tense, subjunctive mood"
                 (org-drill--format-tense-mood "past" "subjunctive"))))

(ert-deftest test-org-drill-format-tense-mood-tense-only ()
  "With only a tense, the label names the tense."
  (should (equal "present tense" (org-drill--format-tense-mood "present" nil))))

(ert-deftest test-org-drill-format-tense-mood-mood-only ()
  "With only a mood, the label names the mood."
  (should (equal "imperative mood" (org-drill--format-tense-mood nil "imperative"))))

(ert-deftest test-org-drill-format-tense-mood-neither-is-nil ()
  "With neither tense nor mood, there is no label."
  (should (null (org-drill--format-tense-mood nil nil))))

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

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