-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypewriter-roll-mode-tests.el
226 lines (190 loc) · 8.94 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
;;; typewriter-roll-mode-tests.el -- tests for typewriter-roll-mode
;;; Code:
(require 'ert)
(require 'typewriter-roll-mode)
(defun typewriter-roll--cursor-line ()
(line-number-at-pos))
(defun typewriter-roll--top-line ()
(line-number-at-pos (window-start)))
(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
(switch-to-buffer (current-buffer))
(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)
(typewriter-roll--scroll-main (current-column))
;; default state, not enough chars to fill `fill-column'
(should (string= (buffer-string) old-text))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))
(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))
;; only after
(typewriter-roll--scroll-main (current-column))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 2))
(insert " ") ; will be removed by filling
(typewriter-roll--scroll-main (current-column))
(should (string= (buffer-string) 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
(switch-to-buffer (current-buffer))
(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)
(typewriter-roll--scroll-main (current-column))
;; default state, not enough chars to fill `fill-column'
;; keep cursor on line 1, keep top on line 1
(should (string= (buffer-string) 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
(typewriter-roll--scroll-main (current-column))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 2))
;; space will be removed by filling, keep previous state
(insert " ")
(typewriter-roll--scroll-main (current-column))
(should (string= (buffer-string) 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
(switch-to-buffer (current-buffer))
(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)
(typewriter-roll--scroll-main (current-column))
;; default state, not enough chars to fill `fill-column'
;; keep cursor on line 1, keep top on line 1
(should (string= (buffer-string) 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)
(typewriter-roll--scroll-main (current-column))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 1))
;; space will be removed by filling, keep previous state
(insert " ")
(typewriter-roll--scroll-main (current-column))
(should (string= (buffer-string) 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
(switch-to-buffer (current-buffer))
(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)
(typewriter-roll--scroll-main (current-column))
;; default state, not enough chars to fill `fill-column'
;; keep cursor on line 1, keep top on line 1
(should (string= (buffer-string) 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)
(typewriter-roll--scroll-main (current-column))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 1))
;; space will be removed by filling, keep previous state
(insert " ")
(typewriter-roll--scroll-main (current-column))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 1)))))))
(provide 'typewriter-roll-mode-tests)
;;; typewriter-roll-mode-tests.el ends here