-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal-test.el
153 lines (122 loc) · 5.31 KB
/
modal-test.el
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
(require 'ert)
(defvar modal-test-testing-modes nil)
(defvar modal-test-testing-mode-counter 0)
(defun modal-test-define-testing-mode (&optional action &rest args)
(incf modal-test-testing-mode-counter)
(let ((default-opt (member :default args))
(mode-name (intern (format "testing%d" modal-test-testing-mode-counter)))
(default (plist-get args :default))
(emacs-instance (plist-get args :emacs-instance))
the-mode)
(setq the-mode
(eval `(modal-define-mode
,mode-name
:doc "a testing mode"
:map (( "a" (eval '(modal-carry
(action)
(lambda () (interactive)
(funcall action)))
t)))
:default ,default
:emacs-instance ,emacs-instance)))
(add-to-list 'modal-test-testing-modes mode-name)
the-mode))
(defun modal-remove-testing-modes-empty ()
)
(defmacro modal-mock-fresh-environment (&rest body)
`(let ((emulation-mode-map-alists nil)
(modal-global-emacs-instance-value nil)
(modal--keymap-alist nil)
)
,@body))
(defvar modal-test-run-on-eval nil "should evaling a deftest run it immediately")
(setq modal-test-run-on-eval nil)
(defun modal-test--toggle-run-on-eval ()
(interactive)
(setq modal-test-run-on-eval (not modal-test-run-on-eval)))
(defmacro modal-test (docstring &rest body)
(declare (indent defun))
(let* ((docstring (eval docstring))
(test-name (modal--testing-symbol-from-string docstring)))
`(let (the-test
modal-after-test-hook)
(setq the-test
(ert-deftest ,test-name ()
,docstring
:tags '(modal)
(unwind-protect
(modal-mock-fresh-environment
,@body)
(run-hooks 'modal-after-test-hook))))
(when modal-test-run-on-eval
(ert-run-tests-interactively the-test))
the-test)))
(defun modal--testing-symbol-from-string (str)
(intern (replace-regexp-in-string "[^a-z0-9]" "-" str)))
(defmacro modal-test-with-buffer (&rest body)
`(with-current-buffer (get-buffer-create "modal-test-buffer")
(modal-emacs-mode)
,@body))
(defun modal-run-modal-tests ()
(interactive)
(ert-run-tests-interactively '(tag modal)))
(defun me--mock (&optional call-through)
(eval '(let ((calls nil))
(lambda (&rest args)
(if (equal args '(call-history))
calls
(push args calls))))
t))
(modal-test "test support: modal mock has a recallable history"
(let ((mock (me--mock)))
(funcall mock 10)
(should (equal '((10)) (funcall mock 'call-history)))))
;; defining a mode / interface tests
(modal-test "defines a method which will activate the mode"
(modal-test-define-testing-mode)
(should (functionp 'modal-testing-mode)))
;; enabling the modal system
(modal-test "enabling modal mode adds the correct value to minor-mode-alist"
(modal-test-with-buffer
(let ((alist-entry (assoc 'modal-emacs-mode minor-mode-alist)))
(should (string-match "Modal\\[.*\\]" (prin1-to-string alist-entry))))))
(modal-test "enabling modal mode adds the modal--minor-mode alist variable
to the emulation-mode-map-alists.
emulation-mode-map-alists is how we specify which keymap is active."
(modal-test-with-buffer
(should (member 'modal--keymap-alist emulation-mode-map-alists))))
(modal-test "enabling modal mode enables the default mode not dependent upon order"
(let ((first-test-mode (modal-test-define-testing-mode))
(second-test-mode (modal-test-define-testing-mode nil :default t))
(third-test-mode (modal-test-define-testing-mode nil :default nil)))
(modal-test-with-buffer
(should (me--enabled-p second-test-mode)))))
;; feature-level stuff
(modal-test "feature: enabling modal as minor mode correctly initializes the mode. "
(let ((mock (me--mock)))
(modal-test-define-testing-mode mock)
(modal-test-with-buffer
(call-interactively (key-binding (kbd "a")))
(should (eql (length (funcall mock 'call-history))
1)))))
;;(defclass modal-mock-emacs-instance ())
;;(defmethod )
;; unit tests
(modal-test (concat "modal-emacs-instance me--default-mode chooses its default mode based on "
"modes responding to is-default")
(let* ((mock (me--mock))
(inst (modal-emacs-instance "test-instance"))
(m1 (modal-test-define-testing-mode nil :emacs-instance inst))
(m2 (modal-test-define-testing-mode nil :emacs-instance inst :default t))
(m3 (modal-test-define-testing-mode nil :emacs-instance inst)))
(should (equal (me--default-mode inst)
m2))))
(modal-test (concat "modal-emacs-instance me--default-mode chooses the first instance if "
"there are no selected defaults")
(let* ((mock (me--mock))
(inst (modal-emacs-instance "test-instance"))
(m1 (modal-test-define-testing-mode nil :emacs-instance inst))
(m2 (modal-test-define-testing-mode nil :emacs-instance inst))
(m3 (modal-test-define-testing-mode nil :emacs-instance inst)))
(should (equal (me--default-mode inst)
m1))))