-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
typewriter-roll-mode-tests.el
310 lines (262 loc) · 12.2 KB
/
typewriter-roll-mode-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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
;;; typewriter-roll-mode-tests.el -- tests for typewriter-roll-mode
;;; Commentary:
;;; Code:
(require 'ert)
(require 'typewriter-roll-mode)
(defun typewriter-roll--cursor-line ()
"Current line of cursor."
(line-number-at-pos))
(defun typewriter-roll--top-line ()
"Current line on the top of the buffer."
(line-number-at-pos (window-start)))
(defun typewriter-roll--fix-ert-window ()
"Fix \"‘recenter’ing a window that does not display current-buffer.\"."
(switch-to-buffer (current-buffer)))
(ert-deftest trm-backspace ()
(should (not
(let ((this-command "hello"))
(typewriter-roll--is-backspace))))
(should (let ((this-command 'delete-backward-char))
(typewriter-roll--is-backspace))))
(ert-deftest normal-behavior ()
"Write something until it overflows `fill-column', then scroll it away."
(let ((scroll-margin 0)
(typewriter-roll-keep-in-focus 0))
(with-temp-buffer
(typewriter-roll--fix-ert-window)
(let* ((old-win-start (window-start))
(old-win-end (window-end))
(old-text "text text")
(expected-text (format "%s\ntext " old-text)))
;; initial text
(insert old-text)
;; default state
(should (string= (buffer-string) old-text))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
(let ((fill-column 10))
(typewriter-roll-mode)
(execute-kbd-macro (kbd "SPC"))
;; default state, SPC does not trigger the roll
;; keep cursor on line 1, keep top on line 1
(should (string= (buffer-string) (format "%s " old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
;; text is inserted, --scroll-main not called yet
(insert "text")
(should (string= (buffer-string) (format "%s text" old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
;; after --scroll-main call
;; move cursor to line 2, move top to line 2
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 2))
;; another space does not trigger the roll
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) (format "%s " expected-text)))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 2)))))))
(ert-deftest bug-1-prefer-keep-in-focus-zero ()
"https://github.com/KeyWeeUsr/typewriter-roll-mode/issues/1
`typewriter-roll-keep-in-focus' is zero, `scroll-margin' is
ignored and zero. The cursor should move to the next line and
that line should be the only one visible on top."
(let ((scroll-margin 0)
(typewriter-roll-keep-in-focus 0)
(typewriter-roll-prefer-scroll-margin nil))
(with-temp-buffer
(typewriter-roll--fix-ert-window)
(let* ((old-win-start (window-start))
(old-win-end (window-end))
(old-text "text text")
(expected-text (format "%s\ntext " old-text)))
;; initial text
(insert old-text)
;; default state, cursor on line 1, top is line 1
(should (string= (buffer-string) old-text))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
(let ((fill-column 10))
(typewriter-roll-mode)
(execute-kbd-macro (kbd "SPC"))
;; default state, SPC does not trigger the roll
;; keep cursor on line 1, keep top on line 1
(should (string= (buffer-string) (format "%s " old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
;; text is inserted, --scroll-main not called yet
(insert "text")
(should (string= (buffer-string) (format "%s text" old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
;; after --scroll-main call
;; move cursor to line 2, move top to line 2
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 2))
;; another space does not trigger the roll
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) (format "%s " expected-text)))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 2)))))))
(ert-deftest bug-1-prefer-keep-in-focus-non-zero ()
"https://github.com/KeyWeeUsr/typewriter-roll-mode/issues/1
`typewriter-roll-keep-in-focus' is non-zero, `scroll-margin' is
ignored and zero. The cursor should move to the next line, but
keep one more line visible above."
(let ((scroll-margin 0)
(typewriter-roll-keep-in-focus 1)
(typewriter-roll-prefer-scroll-margin nil))
(with-temp-buffer
(typewriter-roll--fix-ert-window)
(let* ((old-win-start (window-start))
(old-win-end (window-end))
(old-text "text text")
(expected-text (format "%s\ntext " old-text)))
;; initial text
(insert old-text)
;; default state, cursor on line 1, top is line 1
(should (string= (buffer-string) old-text))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
(let ((fill-column 10))
(typewriter-roll-mode)
(execute-kbd-macro (kbd "SPC"))
;; default state, SPC does not trigger the roll
;; keep cursor on line 1, keep top on line 1
(should (string= (buffer-string) (format "%s " old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
;; text is inserted, --scroll-main not called yet
(insert "text")
(should (string= (buffer-string) (format "%s text" old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
;; after --scroll-main call
;; move cursor to line 2, keep top on line 1
;; (i.e. +1 previous in focus)
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 1))
;; another space does not trigger the roll
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) (format "%s " expected-text)))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 1)))))))
(ert-deftest bug-1-prefer-scroll-margin-non-zero ()
"https://github.com/KeyWeeUsr/typewriter-roll-mode/issues/1
`typewriter-roll-keep-in-focus' is zero, `scroll-margin' is
preferred and non-zero. The cursor should move to the next line,
but keep one more line visible above."
(let ((scroll-margin 1)
(typewriter-roll-keep-in-focus 0)
(typewriter-roll-prefer-scroll-margin t))
(with-temp-buffer
(typewriter-roll--fix-ert-window)
(let* ((old-win-start (window-start))
(old-win-end (window-end))
(old-text "text text")
(expected-text (format "%s\ntext " old-text)))
;; initial text
(insert old-text)
;; default state, cursor on line 1, top is line 1
(should (string= (buffer-string) old-text))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
(let ((fill-column 10))
(typewriter-roll-mode)
(execute-kbd-macro (kbd "SPC"))
;; default state, SPC does not trigger the roll
;; keep cursor on line 1, keep top on line 1
(should (string= (buffer-string) (format "%s " old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
;; text is inserted, --scroll-main not called yet
(insert "text")
(should (string= (buffer-string) (format "%s text" old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
;; after --scroll-main call
;; move cursor to line 2, keep top on line 1
;; (i.e. +1 previous in focus)
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 1))
;; another space does not trigger the roll
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) (format "%s " expected-text)))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 1)))))))
(ert-deftest bug-1-prefer-scroll-margin-zero ()
"https://github.com/KeyWeeUsr/typewriter-roll-mode/issues/1
`typewriter-roll-keep-in-focus' is zero, `scroll-margin' is
preferred and zero. The cursor should move to the next line and
that line should be the only one visible on top."
(let ((scroll-margin 0)
(typewriter-roll-keep-in-focus 0)
(typewriter-roll-prefer-scroll-margin t))
(with-temp-buffer
(typewriter-roll--fix-ert-window)
(let* ((old-win-start (window-start))
(old-win-end (window-end))
(old-text "text text")
(expected-text (format "%s\ntext " old-text)))
;; initial text
(insert old-text)
;; default state, cursor on line 1, top is line 1
(should (string= (buffer-string) old-text))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
(let ((fill-column 10))
(typewriter-roll-mode)
(execute-kbd-macro (kbd "SPC"))
;; default state, SPC does not trigger the roll
;; keep cursor on line 1, keep top on line 1
(should (string= (buffer-string) (format "%s " old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
;; text is inserted, --scroll-main not called yet
(insert "text")
(should (string= (buffer-string) (format "%s text" old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
;; after --scroll-main call
;; move cursor to line 2, move top to line 2
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 2))
;; another space does not trigger the roll
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) (format "%s " expected-text)))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 2)))))))
(ert-deftest bug-1-prefer-scroll-margin-branch ()
"https://github.com/KeyWeeUsr/typewriter-roll-mode/issues/1
Test scrolling value used based on `typewriter-roll-prefer-scroll-margin'."
(let ((scroll-margin 123)
(typewriter-roll-keep-in-focus 456)
result)
(unwind-protect
(progn
(advice-add 'recenter-top-bottom
:override
(lambda (arg) (push arg result)))
(dolist (item '(nil t))
(let ((typewriter-roll-prefer-scroll-margin item))
(with-temp-buffer
(typewriter-roll--fix-ert-window)
(typewriter-roll-mode)
(insert "1\n2")
(typewriter-roll--scroll-main (current-column)))))
(should (equal (reverse result)
`(,typewriter-roll-keep-in-focus ,scroll-margin))))
(advice-remove 'recenter-top-bottom
(lambda (arg) (push arg result))))))
(provide 'typewriter-roll-mode-tests)
;;; typewriter-roll-mode-tests.el ends here