blob: 982eed4411af23d42246604f81f93714f932ce65 (
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
|
;;; test-mousetrap-mode--lighter.el --- Tests for lighter functionality -*- lexical-binding: t; -*-
;;; Commentary:
;; Unit tests for mouse-trap-mode lighter functionality.
;; Tests the dynamic lighter display and interactive clicking behavior.
;;; Code:
(require 'ert)
(require 'mousetrap-mode)
;;; Normal Cases
(ert-deftest test-mousetrap-mode--lighter-normal-shows-mousetrap-when-enabled ()
"Test lighter shows 🪤 when mode is enabled."
(with-temp-buffer
(emacs-lisp-mode)
(mouse-trap-mode 1)
(let ((lighter (mouse-trap--lighter-string)))
(should (string-match-p "🪤" lighter)))))
(ert-deftest test-mousetrap-mode--lighter-normal-shows-mouse-when-disabled ()
"Test lighter shows 🐭 when mode is disabled."
(with-temp-buffer
(emacs-lisp-mode)
(mouse-trap-mode -1)
(let ((lighter (mouse-trap--lighter-string)))
(should (string-match-p "🐭" lighter)))))
(ert-deftest test-mousetrap-mode--lighter-normal-has-help-echo ()
"Test lighter has help-echo tooltip."
(with-temp-buffer
(emacs-lisp-mode)
(mouse-trap-mode 1)
(let ((lighter (mouse-trap--lighter-string)))
(should (get-text-property 0 'help-echo lighter))
(should (string-match-p "Toggle" (get-text-property 0 'help-echo lighter))))))
(ert-deftest test-mousetrap-mode--lighter-normal-has-mouse-face ()
"Test lighter has mouse-face for hover highlighting."
(with-temp-buffer
(emacs-lisp-mode)
(mouse-trap-mode 1)
(let ((lighter (mouse-trap--lighter-string)))
(should (eq (get-text-property 0 'mouse-face lighter) 'mode-line-highlight)))))
(ert-deftest test-mousetrap-mode--lighter-normal-has-local-map ()
"Test lighter has local-map for click handling."
(with-temp-buffer
(emacs-lisp-mode)
(mouse-trap-mode 1)
(let ((lighter (mouse-trap--lighter-string)))
(should (get-text-property 0 'local-map lighter))
(should (keymapp (get-text-property 0 'local-map lighter))))))
(ert-deftest test-mousetrap-mode--lighter-normal-keymap-has-mouse-1-binding ()
"Test lighter keymap has mouse-1 binding."
(should (keymapp mouse-trap--lighter-keymap))
(let ((binding (lookup-key mouse-trap--lighter-keymap [mode-line mouse-1])))
(should binding)
(should (functionp binding))))
(ert-deftest test-mousetrap-mode--lighter-normal-added-to-mode-line-misc-info ()
"Test lighter is added to mode-line-misc-info when mode enabled."
(with-temp-buffer
(emacs-lisp-mode)
(let ((mode-line-misc-info nil)) ; Start fresh
(mouse-trap-mode 1)
(should (member '(:eval (mouse-trap--lighter-string)) mode-line-misc-info)))))
(ert-deftest test-mousetrap-mode--lighter-normal-persists-when-mode-disabled ()
"Test lighter stays in mode-line-misc-info when mode disabled.
This allows it to show the 🐭 indicator when mode is off."
(with-temp-buffer
(emacs-lisp-mode)
(let ((mode-line-misc-info nil)) ; Start fresh
(mouse-trap-mode 1)
(should (member '(:eval (mouse-trap--lighter-string)) mode-line-misc-info))
(mouse-trap-mode -1)
;; Lighter should still be present (to show 🐭)
(should (member '(:eval (mouse-trap--lighter-string)) mode-line-misc-info)))))
;;; Boundary Cases
(ert-deftest test-mousetrap-mode--lighter-boundary-toggle-changes-display ()
"Test toggling mode changes lighter display between 🪤 and 🐭."
(with-temp-buffer
(emacs-lisp-mode)
(mouse-trap-mode 1)
(let ((lighter-on (mouse-trap--lighter-string)))
(should (string-match-p "🪤" lighter-on)))
(mouse-trap-mode -1)
(let ((lighter-off (mouse-trap--lighter-string)))
(should (string-match-p "🐭" lighter-off)))
(mouse-trap-mode 1)
(let ((lighter-on-again (mouse-trap--lighter-string)))
(should (string-match-p "🪤" lighter-on-again)))))
(ert-deftest test-mousetrap-mode--lighter-boundary-multiple-enables-no-duplicates ()
"Test enabling mode multiple times doesn't create duplicate lighters."
(with-temp-buffer
(emacs-lisp-mode)
(let ((mode-line-misc-info nil)) ; Start fresh
(mouse-trap-mode 1)
(mouse-trap-mode -1)
(mouse-trap-mode 1)
;; Should only have one entry
(let ((count 0))
(dolist (item mode-line-misc-info)
(when (equal item '(:eval (mouse-trap--lighter-string)))
(setq count (1+ count))))
(should (= count 1))))))
(ert-deftest test-mousetrap-mode--lighter-boundary-different-buffers-independent ()
"Test lighter state is independent in different buffers."
(let ((buf1 (generate-new-buffer "test1"))
(buf2 (generate-new-buffer "test2")))
(unwind-protect
(progn
(with-current-buffer buf1
(emacs-lisp-mode)
(mouse-trap-mode 1)
(should (string-match-p "🪤" (mouse-trap--lighter-string))))
(with-current-buffer buf2
(emacs-lisp-mode)
(mouse-trap-mode -1)
(should (string-match-p "🐭" (mouse-trap--lighter-string))))
;; Verify buf1 still shows 🪤
(with-current-buffer buf1
(should (string-match-p "🪤" (mouse-trap--lighter-string)))))
(kill-buffer buf1)
(kill-buffer buf2))))
;;; Edge Cases
(ert-deftest test-mousetrap-mode--lighter-edge-always-evaluates-regardless-of-mode-state ()
"Test that lighter entry always evaluates, even when mode is disabled.
This is critical - the entry structure is (:eval ...) not (mouse-trap-mode (:eval ...))
so it displays regardless of the mode variable's value."
(with-temp-buffer
(emacs-lisp-mode)
(let ((mode-line-misc-info nil))
;; Enable mode - adds lighter
(mouse-trap-mode 1)
(let ((entry (car (member '(:eval (mouse-trap--lighter-string)) mode-line-misc-info))))
(should entry)
;; Entry should be (:eval ...) not (mouse-trap-mode (:eval ...))
(should (eq (car entry) :eval))
;; Verify it's not conditional on mouse-trap-mode being the car
(should-not (eq (car entry) 'mouse-trap-mode)))
;; Disable mode - lighter stays and still evaluates
(mouse-trap-mode -1)
(let ((entry (car (member '(:eval (mouse-trap--lighter-string)) mode-line-misc-info))))
(should entry)
(should (eq (car entry) :eval))))))
(ert-deftest test-mousetrap-mode--lighter-edge-string-always-has-space-prefix ()
"Test lighter string always starts with space for proper modeline spacing."
(with-temp-buffer
(emacs-lisp-mode)
(mouse-trap-mode 1)
(let ((lighter-on (mouse-trap--lighter-string)))
(should (string-prefix-p " " lighter-on)))
(mouse-trap-mode -1)
(let ((lighter-off (mouse-trap--lighter-string)))
(should (string-prefix-p " " lighter-off)))))
(ert-deftest test-mousetrap-mode--lighter-edge-properties-cover-entire-string ()
"Test text properties are applied to entire lighter string."
(with-temp-buffer
(emacs-lisp-mode)
(mouse-trap-mode 1)
(let ((lighter (mouse-trap--lighter-string)))
;; Check properties at each position
(dotimes (i (length lighter))
(should (get-text-property i 'local-map lighter))
(should (get-text-property i 'mouse-face lighter))
(should (get-text-property i 'help-echo lighter))))))
(ert-deftest test-mousetrap-mode--lighter-edge-same-keymap-instance ()
"Test all lighters use the same keymap instance for efficiency."
(with-temp-buffer
(emacs-lisp-mode)
(mouse-trap-mode 1)
(let ((lighter1 (mouse-trap--lighter-string))
(lighter2 (mouse-trap--lighter-string)))
(should (eq (get-text-property 0 'local-map lighter1)
(get-text-property 0 'local-map lighter2)))
(should (eq (get-text-property 0 'local-map lighter1)
mouse-trap--lighter-keymap)))))
(provide 'test-mousetrap-mode--lighter)
;;; test-mousetrap-mode--lighter.el ends here
|