forked from KeyWeeUsr/emacs-syncthing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncthing-keyboard-tests.el
233 lines (220 loc) · 10.9 KB
/
syncthing-keyboard-tests.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
;;; syncthing-keyboard-tests.el -- tests -*- lexical-binding: t; -*-
;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Commentary:
;;; Code:
(require 'ert)
(require 'widget)
(require 'syncthing-constants)
(require 'syncthing-keyboard)
(require 'syncthing-state)
(defun syncthing-ert-cleanup ()
(should (eq nil syncthing--servers))
(mapc
(lambda (buff)
(unless
(or (string-match-p (regexp-quote "*Messages*") (buffer-name buff))
(string-match-p (regexp-quote " *code-conversion-work*")
(buffer-name buff))
(string-match-p (regexp-quote " *code-converting-work*")
(buffer-name buff))
(string-match-p "syncthing.*\\.el" (buffer-name buff)))
(kill-buffer buff)))
(buffer-list)))
(ert-deftest syncthing-keyboard-newline-apply ()
"Call widget's `:action' when `point' is on top of it."
(syncthing-ert-cleanup)
(with-temp-buffer
(let (called)
(widget-create 'push-button :action (lambda (&rest _) (setq called t)))
(widget-setup)
(should (string= "[]" (buffer-string)))
(syncthing--newline 1) ; Move into the button area first
(should called))))
(ert-deftest syncthing-keyboard-newline-apply-non-widget ()
"Show a message when interacting with a non-widget."
(syncthing-ert-cleanup)
(with-temp-buffer
(let (called args)
(advice-add 'message
:override
(lambda (&rest largs) (setq called t) (setq args largs)))
(syncthing--newline 1) ; Move into the button area first
(advice-remove 'message
(lambda (&rest largs) (setq called t) (setq args largs)))
(should called)
(should (string= (caddr args) syncthing-msg-cant-edit-buffer))
(should called))))
(ert-deftest syncthing-keyboard-tab-fold-all ()
"Fold all foldable widgets."
(syncthing-ert-cleanup)
(with-temp-buffer
(let* ((skip-folders '(a b c))
(skip-devices '(x y z))
(syncthing-server
(syncthing--server
:data (list (cons 'folders (list (list (cons 'id "a"))
(list (cons 'id "b"))
(list (cons 'id "c"))))
(cons 'devices (list (list (cons 'deviceID "x"))
(list (cons 'deviceID "y"))
(list (cons 'deviceID "z")))))))
(syncthing-buffer
(syncthing--buffer :fold-folders nil
:fold-devices nil
:skip-fold-folders skip-folders
:skip-fold-devices skip-devices))
redrawn)
(should (null (syncthing-buffer-fold-folders syncthing-buffer)))
(should (null (syncthing-buffer-fold-devices syncthing-buffer)))
(should (string= (format "%s" (syncthing-buffer-skip-fold-folders
syncthing-buffer))
(format "%s" skip-folders)))
(should (string= (format "%s" (syncthing-buffer-skip-fold-devices
syncthing-buffer))
(format "%s" skip-devices)))
(advice-add 'syncthing--draw
:override
(lambda (&rest _) (setq redrawn t)))
(syncthing--tab)
(should (string= (format "%s" (syncthing-buffer-fold-folders
syncthing-buffer))
(format "%s" (reverse skip-folders))))
(should (string= (format "%s" (syncthing-buffer-fold-devices
syncthing-buffer))
(format "%s" (reverse skip-devices))))
(should (null (syncthing-buffer-skip-fold-folders syncthing-buffer)))
(should (null (syncthing-buffer-skip-fold-devices syncthing-buffer)))
(advice-remove 'syncthing--draw (lambda (&rest _) (setq redrawn t)))
(should redrawn))))
(ert-deftest syncthing-keyboard-tab-unfold-all ()
"Unfold all foldable widgets."
(syncthing-ert-cleanup)
(with-temp-buffer
(let* ((skip-folders '(a b c))
(skip-devices '(x y z))
(syncthing-server
(syncthing--server
:data (list (cons 'folders (list (list (cons 'id "a"))
(list (cons 'id "b"))
(list (cons 'id "c"))))
(cons 'devices (list (list (cons 'deviceID "x"))
(list (cons 'deviceID "y"))
(list (cons 'deviceID "z")))))))
(syncthing-buffer
(syncthing--buffer :fold-folders skip-folders
:fold-devices skip-devices
:skip-fold-folders '(a)
:skip-fold-devices '(x)))
redrawn)
(should-not (null (syncthing-buffer-fold-folders syncthing-buffer)))
(should-not (null (syncthing-buffer-fold-devices syncthing-buffer)))
(should (string= (format "%s" (syncthing-buffer-fold-folders
syncthing-buffer))
(format "%s" skip-folders)))
(should (string= (format "%s" (syncthing-buffer-fold-devices
syncthing-buffer))
(format "%s" skip-devices)))
(advice-add 'syncthing--draw
:override
(lambda (&rest _) (setq redrawn t)))
(syncthing--tab)
(should (string= (format "%s" (syncthing-buffer-skip-fold-folders
syncthing-buffer))
(format "%s" (reverse skip-folders))))
(should (string= (format "%s" (syncthing-buffer-skip-fold-devices
syncthing-buffer))
(format "%s" (reverse skip-devices))))
(should (null (syncthing-buffer-fold-folders syncthing-buffer)))
(should (null (syncthing-buffer-fold-devices syncthing-buffer)))
(advice-remove 'syncthing--draw (lambda (&rest _) (setq redrawn t)))
(should redrawn))))
(ert-deftest syncthing-keyboard-tab-fold-folders-unfold-devices ()
"Fold all folders if some are open, unfold all devices when all are fold."
(syncthing-ert-cleanup)
(with-temp-buffer
(let* ((skip-folders '(a b c))
(skip-devices '(x y z))
(syncthing-server
(syncthing--server
:data (list (cons 'folders (list (list (cons 'id "a"))
(list (cons 'id "b"))
(list (cons 'id "c"))))
(cons 'devices (list (list (cons 'deviceID "x"))
(list (cons 'deviceID "y"))
(list (cons 'deviceID "z")))))))
(syncthing-buffer
(syncthing--buffer :fold-folders nil
:fold-devices skip-devices
:skip-fold-folders skip-folders
:skip-fold-devices '(x)))
redrawn)
(should (null (syncthing-buffer-fold-folders syncthing-buffer)))
(should (string= (format "%s" (syncthing-buffer-fold-devices
syncthing-buffer))
(format "%s" skip-devices)))
(should (string= (format "%s" (syncthing-buffer-skip-fold-folders
syncthing-buffer))
(format "%s" skip-folders)))
(should (string= (format "%s" (syncthing-buffer-skip-fold-devices
syncthing-buffer))
(format "%s" '(x))))
(advice-add 'syncthing--draw
:override
(lambda (&rest _) (setq redrawn t)))
(syncthing--tab)
(should (string= (format "%s" (syncthing-buffer-fold-folders
syncthing-buffer))
(format "%s" (reverse skip-folders))))
(should (null (syncthing-buffer-fold-devices syncthing-buffer)))
(should (null (syncthing-buffer-skip-fold-folders syncthing-buffer)))
(should (string= (format "%s" (syncthing-buffer-skip-fold-devices
syncthing-buffer))
(format "%s" (reverse skip-devices))))
(advice-remove 'syncthing--draw (lambda (&rest _) (setq redrawn t)))
(should redrawn))))
(ert-deftest syncthing-keyboard-tab-unfold-folders-fold-devices ()
"Unfold all folders if all are fold, fold all devices when some are open."
(syncthing-ert-cleanup)
(with-temp-buffer
(let* ((skip-folders '(a b c))
(skip-devices '(x y z))
(syncthing-server
(syncthing--server
:data (list (cons 'folders (list (list (cons 'id "a"))
(list (cons 'id "b"))
(list (cons 'id "c"))))
(cons 'devices (list (list (cons 'deviceID "x"))
(list (cons 'deviceID "y"))
(list (cons 'deviceID "z")))))))
(syncthing-buffer
(syncthing--buffer :fold-folders skip-folders
:fold-devices nil
:skip-fold-folders '(a)
:skip-fold-devices skip-devices))
redrawn)
(should (string= (format "%s" (syncthing-buffer-fold-folders
syncthing-buffer))
(format "%s" skip-folders)))
(should (null (syncthing-buffer-fold-devices syncthing-buffer)))
(should (string= (format "%s" (syncthing-buffer-skip-fold-folders
syncthing-buffer))
(format "%s" '(a))))
(should (string= (format "%s" (syncthing-buffer-skip-fold-devices
syncthing-buffer))
(format "%s" skip-devices)))
(advice-add 'syncthing--draw
:override
(lambda (&rest _) (setq redrawn t)))
(syncthing--tab)
(should (null (syncthing-buffer-fold-folders syncthing-buffer)))
(should (string= (format "%s" (syncthing-buffer-fold-devices
syncthing-buffer))
(format "%s" (reverse skip-devices))))
(should (string= (format "%s" (syncthing-buffer-skip-fold-folders
syncthing-buffer))
(format "%s" (reverse skip-folders))))
(should (null (syncthing-buffer-skip-fold-devices syncthing-buffer)))
(advice-remove 'syncthing--draw (lambda (&rest _) (setq redrawn t)))
(should redrawn))))
(provide 'syncthing-keyboard-tests)
;;; syncthing-keyboard-tests.el ends here