blob: a5727e4fcb241afaf15b00b671d43487c8ba0693 (
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
|
;;; test-org-drill-undo.el --- Tests for undo/retry of the last rating -*- lexical-binding: t; -*-
;;; Commentary:
;; Undo lets the user take back a misclicked rating. Before a rating is
;; applied, the entry's scheduling state is snapshotted onto the session's
;; undo-stack (capped at org-drill-undo-limit). org-drill-undo-last-rating
;; restores that state, drops the recorded quality, and re-queues the card so
;; it comes around again.
;;; Code:
(require 'ert)
(require 'org-drill)
;;;; defcustoms
(ert-deftest test-org-drill-undo-limit-defaults-to-3 ()
(should (eq 3 (default-value 'org-drill-undo-limit))))
(ert-deftest test-org-drill-undo-key-is-customizable-and-defaults-to-u ()
(should (custom-variable-p 'org-drill--undo-key))
(should (eq ?u (default-value 'org-drill--undo-key))))
;;;; snapshot / restore round-trip
(ert-deftest test-org-drill-snapshot-restore-round-trips-scheduling-data ()
"Restoring a snapshot puts changed properties back to their captured values."
(with-temp-buffer
(insert "* Card :drill:\n")
(org-mode)
(goto-char (point-min))
(org-set-property "DRILL_LAST_QUALITY" "4")
(org-set-property "DRILL_EASE" "2.5")
(let ((snap (org-drill--snapshot-entry-data)))
(org-set-property "DRILL_LAST_QUALITY" "1")
(org-set-property "DRILL_EASE" "1.8")
(org-drill--restore-entry-data snap)
(should (equal "4" (org-entry-get (point) "DRILL_LAST_QUALITY")))
(should (equal "2.5" (org-entry-get (point) "DRILL_EASE"))))))
(ert-deftest test-org-drill-restore-deletes-properties-absent-at-snapshot ()
"A property that did not exist when snapshotted is removed on restore."
(with-temp-buffer
(insert "* Card :drill:\n")
(org-mode)
(goto-char (point-min))
(let ((snap (org-drill--snapshot-entry-data)))
(org-set-property "DRILL_LAST_QUALITY" "3")
(org-drill--restore-entry-data snap)
(should (null (org-entry-get (point) "DRILL_LAST_QUALITY"))))))
;;;; org-drill-undo-last-rating
(ert-deftest test-org-drill-undo-last-rating-restores-pops-and-requeues ()
"Undo restores the entry data, drops the recorded quality, and re-queues
the card onto the again list."
(with-temp-buffer
(insert "* Card :drill:\n")
(org-mode)
(goto-char (point-min))
(org-set-property "DRILL_LAST_QUALITY" "4")
(let ((session (org-drill-session)))
(push (org-drill--snapshot-entry-data) (oref session undo-stack))
(org-set-property "DRILL_LAST_QUALITY" "1")
(push 1 (oref session qualities))
(org-drill-undo-last-rating session)
(should (equal "4" (org-entry-get (point) "DRILL_LAST_QUALITY")))
(should (null (oref session qualities)))
(should (= 1 (length (oref session again-entries)))))))
(ert-deftest test-org-drill-undo-last-rating-empty-stack-is-noop ()
"Undo with nothing recorded does not error and changes no session state."
(let ((session (org-drill-session)))
(org-drill-undo-last-rating session)
(should (null (oref session qualities)))
(should (null (oref session again-entries)))))
;;;; undo-key routing through the rating prompt
(ert-deftest test-org-drill-reschedule-undo-key-takes-back-previous-rating ()
"Pressing the undo key at the prompt restores the previous card and
re-queues it, then the prompt loop continues (here, a quit ends it).
Components integrated:
- org-drill-reschedule (entry point, real)
- org-drill--read-rating-key (MOCKED — returns undo key then quit key)
- org-drill-undo-last-rating + snapshot/restore (real)
Validates the undo key is handled in the prompt loop rather than treated
as a rating, and that the loop keeps prompting afterward."
(with-temp-buffer
(insert "* Prev :drill:\n* Current :drill:\n")
(org-mode)
(let ((session (org-drill-session)))
(goto-char (point-min))
(org-set-property "DRILL_LAST_QUALITY" "5")
(push (org-drill--snapshot-entry-data) (oref session undo-stack))
(org-set-property "DRILL_LAST_QUALITY" "0") ; the misrating to take back
(push 0 (oref session qualities))
(goto-char (point-min))
(re-search-forward "Current")
(let ((keys (list org-drill--undo-key org-drill--quit-key)))
(cl-letf (((symbol-function 'org-drill--read-rating-key)
(lambda (&rest _) (pop keys)))
((symbol-function 'sit-for) #'ignore))
(should (null (org-drill-reschedule session)))))
(goto-char (point-min))
(should (equal "5" (org-entry-get (point) "DRILL_LAST_QUALITY")))
(should (null (oref session qualities)))
(should (= 1 (length (oref session again-entries)))))))
(provide 'test-org-drill-undo)
;;; test-org-drill-undo.el ends here
|