edward

An extensible POSIX-compatible implementation of the ed(1) text editor

git clone https://git.8pit.net/edward.git

  1(import (edward buffer))
  2
  3(define (test-buffer name expected proc)
  4  (test name
  5        expected
  6        (let ((b (make-buffer)))
  7          (proc b)
  8          (buffer->list b))))
  9
 10;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 11
 12(test-group "append"
 13  (test-buffer "append start"
 14               '("foo" "bar")
 15               (lambda (b)
 16                 (buffer-append! b 0 '("foo" "bar"))))
 17
 18  (test-buffer "append between"
 19               '("foo" "baz" "bar")
 20               (lambda (b)
 21                 (buffer-append! b 0 '("foo" "bar"))
 22                 (buffer-append! b 1 '("baz"))))
 23
 24  (test-buffer "append at end"
 25               '("foo" "123")
 26               (lambda (b)
 27                 (buffer-append! b 0 '("foo"))
 28                 (buffer-append! b 1 '("123")))))
 29
 30(test-group "buffer->list"
 31  (test "same start/end index"
 32        '()
 33        (let ((b (make-buffer)))
 34          (buffer-append! b 0 '("1" "2" "3"))
 35          (buffer->list b 1 1)))
 36
 37  (test "sublist in between"
 38        '("2" "3")
 39        (let ((b (make-buffer)))
 40          (buffer-append! b 0 '("1" "2" "3" "4" "5"))
 41          (buffer->list b 1 3)))
 42
 43  (test "sublist including end"
 44        '("4" "5")
 45        (let ((b (make-buffer)))
 46          (buffer-append! b 0 '("1" "2" "3" "4" "5"))
 47          (buffer->list b 3 5))))
 48
 49(test-group "remove"
 50  (test-buffer "remove start full"
 51               '()
 52               (lambda (b)
 53                 (buffer-append! b 0 '("foo" "bar"))
 54                 (buffer-remove! b 0 2)))
 55
 56  (test-buffer "remove start partial"
 57               '("bar")
 58               (lambda (b)
 59                 (buffer-append! b 0 '("foo" "bar"))
 60                 (buffer-remove! b 0 1)))
 61
 62  (test-buffer "remove between"
 63               '("foo" "bar")
 64               (lambda (b)
 65                 (buffer-append! b 0 '("foo" "baz" "bar"))
 66                 (buffer-remove! b 2 2)))
 67
 68  (test-buffer "remove last"
 69               '("foo" "baz")
 70               (lambda (b)
 71                 (buffer-append! b 0 '("foo" "baz" "bar"))
 72                 (buffer-remove! b 3 3))))
 73
 74(test-group "replace command"
 75  (test-buffer "replace single line"
 76               '("foo" "345" "bar")
 77               (lambda (b)
 78                 (buffer-append! b 0 '("foo" "123" "bar"))
 79                 (buffer-replace! b 2 2 '("345"))))
 80
 81  (test-buffer "replace multiple lines"
 82               '("foo" "bar")
 83               (lambda (b)
 84                 (buffer-append! b 0 '("1" "2"))
 85                 (buffer-replace! b 1 2 '("foo" "bar"))))
 86
 87  (test-buffer "replace add lines"
 88               '("foo" "bar" "baz")
 89               (lambda (b)
 90                 (buffer-append! b 0 '("foo" "test test"))
 91                 (buffer-replace! b 2 2 '("bar" "baz"))))
 92
 93  (test-buffer "buffer replace everything"
 94               '()
 95               (lambda (b)
 96                 (buffer-append! b 0 '("foo" "bar" "baz"))
 97                 (buffer-replace! b 1 3 '()))))
 98
 99(test-group "join command"
100  (test-buffer "join entire buffer"
101               '("foobar")
102               (lambda (b)
103                 (buffer-append! b 0 '("foo" "bar"))
104                 (buffer-join! b 0 2)))
105
106  (test-buffer "join keep last"
107               '("foobar" "baz")
108               (lambda (b)
109                 (buffer-append! b 0 '("foo" "bar" "baz"))
110                 (buffer-join! b 1 2)))
111
112  (test-buffer "join keep first"
113               '("foo" "barbaz")
114               (lambda (b)
115                 (buffer-append! b 0 '("foo" "bar" "baz"))
116                 (buffer-join! b 2 3)))
117
118  (test-buffer "join betwen"
119               '("foo" "123" "bar")
120               (lambda (b)
121                  (buffer-append! b 0 '("foo" "1" "2" "3" "bar"))
122                  (buffer-join! b 2 4))))
123
124(test-group "move command"
125  (test-buffer "move to end"
126               '("bar" "baz" "foo")
127               (lambda (b)
128                 (buffer-append! b 0 '("foo" "bar" "baz"))
129                 (buffer-move! b 2 3 0)))
130
131  (test-buffer "move to start"
132               '("baz" "foo" "bar")
133               (lambda (b)
134                 (buffer-append! b 0 '("foo" "bar" "baz"))
135                 (buffer-move! b 1 2 3)))
136
137  (test-buffer "move single to middle"
138               '("foo" "123" "bar")
139               (lambda (b)
140                 (buffer-append! b 0 '("foo" "bar" "123"))
141                 (buffer-move! b 3 3 1)))
142
143  (test-buffer "move multiple after destination"
144               '("foo" "bar" "---" "t1" "t2" "end")
145               (lambda (b)
146                 (buffer-append! b 0 '("foo" "t1" "t2" "bar" "---" "end"))
147                 (buffer-move! b 2 3 5)))
148
149  (test-buffer "move multiple before destination"
150               '("foo" "t1" "t2" "bar" "---" "end")
151               (lambda (b)
152                 (buffer-append! b 0 '("foo" "bar" "---" "t1" "t2" "end"))
153                 (buffer-move! b 4 5 1)))
154
155  (test-buffer "move everything"
156               '("foo" "bar" "baz")
157               (lambda (b)
158                 (buffer-append! b 0 '("foo" "bar" "baz"))
159                 (buffer-move! b 1 3 0))))
160
161
162(test-group "undo command"
163  (test-buffer "undo append"
164               '()
165               (lambda (b)
166                 (buffer-with-undo b
167                   (lambda ()
168                     (buffer-append! b 0 '("foo" "bar"))))
169                 (buffer-undo! b)))
170
171  (test-buffer "undo remove"
172               '("foo" "bar" "baz")
173               (lambda (b)
174                 (buffer-append! b 0 '("foo" "bar" "baz"))
175                 (buffer-with-undo b
176                   (lambda ()
177                    (buffer-remove! b 2 3)))
178                 (buffer-undo! b)))
179
180  (test-buffer "undo undo"
181               '("foo" "bar")
182               (lambda (b)
183                 (buffer-append! b 0 '("foo" "bar"))
184                 (buffer-undo! b)   ;; undo append
185                 (buffer-undo! b))) ;; undo undo
186
187  (test-buffer "undo replace"
188               '("foo" "bar")
189               (lambda (b)
190                 (buffer-append! b 0 '("foo" "bar"))
191                 (buffer-with-undo b
192                   (lambda ()
193                     (buffer-replace! b 1 2 '("first" "second"))))
194                 (buffer-undo! b)))
195
196  (test-buffer "undo append last"
197               '("foo" "bar")
198               (lambda (b)
199                 (buffer-append! b 0 '("foo" "bar"))
200                 (buffer-with-undo b
201                   (lambda ()
202                     (buffer-append! b 2 '("second line"))))
203                 (buffer-undo! b)))
204
205  (test-buffer "undo replace last"
206               '("foo" "bar")
207               (lambda (b)
208                 (buffer-append! b 0 '("foo" "bar"))
209                 (buffer-with-undo b
210                   (lambda ()
211                     (buffer-replace! b 2 2 '("second line"))))
212                 (buffer-undo! b)))
213
214  (test-buffer "undo replace undo"
215               '("test" "bar")
216               (lambda (b)
217                 (buffer-append! b 0 '("foo" "bar"))
218                 (buffer-with-undo b
219                   (lambda ()
220                     (buffer-replace! b 1 1 '("test"))
221                     (buffer-undo! b) ))  ;; undo replace
222                 (buffer-undo! b))) ;; undo undo
223
224  (test-buffer "undo replace nothing"
225               '("foo" "bar")
226               (lambda (b)
227                 (buffer-append! b 0 '("foo" "bar"))
228                 (buffer-with-undo b
229                   (lambda ()
230                     (buffer-replace! b 0 0 '())))
231                 (buffer-undo! b)))
232
233  (test-buffer "undo join"
234               '("foo" "bar")
235               (lambda (b)
236                 (buffer-append! b 0 '("foo" "bar"))
237                 (buffer-with-undo b
238                   (lambda ()
239                     (buffer-join! b 0 2)))
240                 (buffer-undo! b)))
241
242  (test-buffer "undo multiple"
243               '("foo" "bar")
244               (lambda (b)
245                 (buffer-append! b 0 '("foo" "bar"))
246                 (buffer-with-undo b
247                   (lambda ()
248                     (buffer-append! b 0 '("1"))
249                     (buffer-append! b 0 '("2"))
250                     (buffer-replace! b 0 0 '("0"))))
251                 (buffer-undo! b)))
252
253 (test-buffer "undo move"
254               '("foo" "bar" "baz")
255               (lambda (b)
256                 (buffer-append! b 0 '("foo" "bar" "baz"))
257                 (buffer-with-undo b
258                   (lambda ()
259                     (buffer-move! b 2 3 0)))
260                 (buffer-undo! b)))
261
262 (test "empty undo buffer"
263       #f
264       (let ((b (make-buffer)))
265         (buffer-has-undo? b)))
266
267 (test "operation without buffer-undo"
268       #f
269       (let ((b (make-buffer)))
270         (buffer-append! b 0 '("foo" "bar"))
271         (buffer-has-undo? b)))
272
273 (test "non-empty undo buffer"
274       #t
275       (let ((b (make-buffer)))
276         (buffer-with-undo b
277           (lambda ()
278             (buffer-append! b 0 '("foo" "bar" "baz"))))
279         (buffer-has-undo? b))))