-
Notifications
You must be signed in to change notification settings - Fork 2
/
vimvars-regtest.el
454 lines (355 loc) · 13.9 KB
/
vimvars-regtest.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
;;; vimvars-regtest.el --- Regression tests for vimvars.el
;; Copyright (C) 2010 Free Software Foundation, Inc.
;; Author: James Youngman <[email protected]>
;; Maintainer: James Youngman <[email protected]>
;; vimvars is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; vimvars is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with vimvars. If not, see <http://www.gnu.org/licenses/>.
(require 'cl)
(require 'cc-mode)
(defun kill-buffers-visiting (filename)
(while
(let (existing (get-file-buffer filename))
(when existing (kill-buffer existing)))))
(defmacro run-checks-for-file-body (suffix content &rest checks)
"Write a temporary file whose name has suffix SUFFIX, having content CONTENT.
Visit it with find-file, and evaluate CHECKS.
Returns the result of the final item in CHECKS."
(let ((fname (make-symbol "filename")))
`(let ((,fname (make-temp-file "foo" nil ,suffix)))
(unwind-protect
(progn
(with-temp-file ,fname (insert ,content))
(kill-buffers-visiting ,fname)
(find-file ,fname)
,@checks)
(progn
;; Kill the buffer and delete the temporary file.
(kill-buffer)
(delete-file ,fname))))))
(defmacro run-checks-for-text-file (content &rest checks)
"Calls run-checks-for-file-body with SUFFIX set to .txt."
`(run-checks-for-file-body ".txt" ,content ,@checks))
(defmacro check (name description &rest body)
"Run a test case, verbosely.
Evaluate BODY, printing progress messages. Returns nil if an error occurred."
(declare (debug t) (indent 1))
(let ((error-var (make-symbol "errordetail")))
`(condition-case ,error-var
(progn
(princ (format "Running %s" ,name ,description))
(let ((test-name ,name))
,@body)
(princ ": passed.\n")
t)
(cl-assertion-failed
(princ (message "FAIL: Test %s (%s) FAILED: %s\n"
,name ,description error-var))
nil))))
(defun assert-equal (varname expected-value)
(let* ((value (symbol-value varname))
(failmsg (format "Expected %s to be %s, but it is %s"
varname expected-value value)))
(assert (equal value expected-value) t failmsg)))
(defun assert-true (varname)
(let* ((value (symbol-value varname))
(failmsg (format "Expected %s to be true, but it is nil" varname)))
(assert (not (not value)) t failmsg)))
(defun assert-false (varname)
(let* ((value (symbol-value varname))
(failmsg (format "Expected %s to be nil, but it is %s"
varname value)))
(assert (not value) t failmsg)))
(defmacro with-temp-default (varname temp-value &rest body)
"Execute BODY with the default value of VAR set to VALUE.
The value of the final expression in BODY is returned."
(let ((oldval-var (make-symbol "previous-default")))
`(let ((,oldval-var ,varname))
(setq-default ,varname ,temp-value)
(unwind-protect
(progn ,@body)
(setq-default ,varname ,oldval-var)))))
(defmacro run-tests (description &rest tests)
"Run the regression tests"
`(let
((old-tab-width (default-value 'tab-width))
(old-fill-column (default-value 'fill-column))
(old-find-file-hook find-file-hook)
(old-vimvars-check-lines vimvars-check-lines))
;; Set up a standard testing environment.
(setq-default tab-width 14
fill-column 40)
(setq find-file-hook '(vimvars-obey-vim-modeline)
vimvars-check-lines 5)
;; Run the tests and restore the old environment.
(princ (format "*** Test Case \"%s\"\n" ,description))
(unwind-protect
(prog1
,@tests
(princ (format "Test case \"%s\" is complete.\n\n" ,description))
(setq-default tab-wdith old-tab-width
fill-column old-fill-column)
(setq find-file-hook old-find-file-hook
vimvars-check-lines old-vimvars-check-lines)))))
(defmacro define-test-suite (name docstring &rest body)
`(defun ,name () ,docstring
(run-tests
,docstring
,@body)))
(define-test-suite unit-test-accept-tag
"Unit test for vimvars-accept-tag"
(check "test-vim-bol"
(assert (vimvars-accept-tag "" "vim")))
(check "test-vim-not-bol"
(assert (vimvars-accept-tag " " "vim")))
(check "test-vi-bol"
(assert (vimvars-accept-tag "" "vi")))
(check "test-vi-not-bol"
(assert (vimvars-accept-tag " " "vi")))
(check "test-ex-bol"
(assert (not (vimvars-accept-tag "" "ex"))))
(check "test-ex-not-bol"
(assert (vimvars-accept-tag " " "ex")))
(check "test-foo-bol"
(assert (not (vimvars-accept-tag "" "foo"))))
(check "test-foo-not-bol"
(assert (not (vimvars-accept-tag " " "foo")))))
(define-test-suite unit-test-vimvars-should-obey-modeline
"Unit test for vimvars-should-obey-modeline"
(check "test-vimvars-enabled-nil"
(let ((vimvars-enabled nil)
(file-local-variables-alist nil)
(vimvars-ignore-mode-line-if-local-variables-exist nil))
(assert (not (vimvars-should-obey-modeline))
t "case 1")))
(check "test-no-local-variables"
(let ((vimvars-enabled t)
(file-local-variables-alist nil)
(vimvars-ignore-mode-line-if-local-variables-exist nil))
(assert (vimvars-should-obey-modeline)
t "case 2")))
(check "test-with-local-variables"
(let ((vimvars-enabled t)
(file-local-variables-alist '((tab-width . 8)))
(vimvars-ignore-mode-line-if-local-variables-exist t))
(assert (not (vimvars-should-obey-modeline))
t "case 3")))
(check "test-with-both"
(let ((vimvars-enabled t)
(file-local-variables-alist '((tab-width . 8)))
(vimvars-ignore-mode-line-if-local-variables-exist nil))
(assert (vimvars-should-obey-modeline)
t "case 4"))))
(define-test-suite unit-test-vimvars-expand-option-name
"Unit test for vimvars-expand-option-name"
(check "test-expansions"
(assert (equal (vimvars-expand-option-name "ro") "readonly"))
(assert (equal (vimvars-expand-option-name "sts") "softtabstop"))
(assert (equal (vimvars-expand-option-name "sw") "shiftwidth"))
(assert (equal (vimvars-expand-option-name "ts") "tabstop"))
(assert (equal (vimvars-expand-option-name "tw") "textwidth")))
(check "test-nonexpansions"
(assert (equal (vimvars-expand-option-name "blehbleh") "blehbleh"))))
(define-test-suite test-basics
"Basic tests"
(check "test-disable"
"Check that setting vimvars-enabled to nil turns vimvars-obey-vim-modeline off."
(let ((vimvars-enabled nil))
(run-checks-for-text-file
"This is a text file.\n# vim: set ts=18 :\n"
(assert-equal 'tab-width 14))))
(check "test-basic-success-case"
"Check that even one item actually works."
(run-checks-for-text-file
"# vi: set ts=18 :\n"
(assert-equal 'tab-width 18))))
(define-test-suite test-modeline-format
"modeline format"
(defun check-recognise-modeline (description modeline)
(check
description
(concat "Verify that we recognise the modeline " modeline)
(let ((actual-modeline (concat modeline "\n")))
(run-checks-for-text-file
actual-modeline
(assert-equal 'tab-width 18)))))
(check-recognise-modeline "test-accept-vim-set"
"# vim: set ts=18 :\n")
(check-recognise-modeline "test-accept-vi-set"
"# vi: set ts=18 :\n")
(check-recognise-modeline "test-accept-ex-set"
"# ex: set ts=18 :\n")
(check-recognise-modeline "test-accept-vim-se"
"# vim: se ts=18 :\n")
(check-recognise-modeline "test-accept-vi-se"
"# vi: se ts=18 :\n")
(check-recognise-modeline "test-accept-ex-se"
"# ex: se ts=18 :\n")
(check-recognise-modeline "test-accept-vim-setlocal"
"# vim: setlocal ts=18 :\n")
(check-recognise-modeline "test-accept-vi-setlocal"
"# vi: setlocal ts=18 :\n")
(check-recognise-modeline "test-accept-ex-setlocal"
"# ex: setlocal ts=18 :\n")
(check "test-require-trailing-colon"
"Check that we ignore modelines with no trailing colon."
(run-checks-for-text-file
"This is a text file.\n# vim: set ts=18 \n"
(assert-equal 'tab-width 14))) ; i.e. unchanged from default.
;; Check modelines work even without a space before the final colon.
(check-recognise-modeline "test-accept-no-space-before-final-colon"
"# vi: set ts=18:\n")
;; Check extra spaces before vi: are accepted
(check-recognise-modeline "test-accept-extra-spaces"
"# vi: set ts=18 :\n")
;; Check tabs before vi: are accepted.
(check-recognise-modeline "test-accept-tab"
"#\tvi: set ts=18 :\n")
;; Check vi: at the start of the line is accepted
(check-recognise-modeline "test-accept-vi-at-start" "vi: set ts=18 :\n")
(check "test-reject-ex-at-start"
"Check ex: at the start of the line is rejected."
(run-checks-for-text-file
"ex: set ts=18 :\n"
(assert-equal 'tab-width 14)))
(check "test-modeline-too-far"
"Check we ignore mode lines more than `vimvars-check-lines' into the file."
(run-checks-for-text-file
"Mode lines at line 6 should be ignored\n\n\n\n\n# vim: set ts=6 :\n"
(assert-equal 'tab-width 14))
(run-checks-for-text-file
"Mode lines at line 5 should be accepted.\n\n\n\n# vim: set ts=10 :\n"
(assert-equal 'tab-width 10))))
(define-test-suite test-misc
"test makeprg, ignorecase, noignorecase, wrap, nowrap, textwidth"
(check "makeprg"
"Check makeprg=blah works."
(run-checks-for-text-file
"This is a text file.\n# vim: set makeprg=gmake :\n"
(assert-equal 'compile-command "gmake")))
(with-temp-default
case-fold-search t
(check "noignorecase"
(run-checks-for-text-file
"This is a text file.\n# vim: set noignorecase :\n"
(assert-false 'case-fold-search))))
(with-temp-default
case-fold-search nil
(check "ignorecase"
(run-checks-for-text-file
"This is a text file.\n# vim: set ignorecase :\n"
(assert-true 'case-fold-search))))
(with-temp-default
truncate-lines t
(check "wrap"
(run-checks-for-text-file
"This is a text file.\n# vim: set wrap :\n"
(assert-false 'truncate-lines))))
(with-temp-default
truncate-lines nil
(check "nowrap"
(run-checks-for-text-file
"This is a text file.\n# vim: set nowrap :\n"
(assert-true 'truncate-lines))))
(check "test-textwidth-87"
(run-checks-for-text-file
"This is a text file.\n# vim: set tw=87 :\n"
(assert-equal 'tab-width 14)
(assert-equal 'fill-column 87))))
(define-test-suite test-readonly
"test readonnly, noreadonly, write, nowrite"
(check "readonly"
(run-checks-for-text-file
"This is a text file.\n# vim: set readonly :\n"
(assert-true 'buffer-read-only)))
;; This test doesn't really do anything useful, since
;; not read-only is the default anyway.
(check "noreadonly"
(run-checks-for-text-file
"This is a text file.\n# vim: set noreadonly :\n"
(assert-false 'buffer-read-only)))
(check "write"
(run-checks-for-text-file
"This is a text file.\n# vim: set write :\n"
(assert-false 'buffer-read-only)))
(check "nowrite"
(run-checks-for-text-file
"This is a text file.\n# vim: set nowrite :\n"
(assert-true 'buffer-read-only))))
(define-test-suite test-shiftwidth
"Check sw=N works."
(check "sw-2"
(run-checks-for-file-body
".c"
"/* This is a C source file.\n vim: set sw=2 :\n*/\n"
(assert-equal 'c-basic-offset 2)))
;; Do a different check with a distinct c-basic-offset so that
;; we can tell for sure we're actually changing it.
(check "sw-4"
(run-checks-for-file-body
".c"
"/* This is a C source file.\n vim: set sw=4 :\n*/\n"
(assert-equal 'c-basic-offset 4))))
(define-test-suite test-tabstop
"tab stop and tab expansion"
(check "test-ts-18"
"Check ts=X works."
(run-checks-for-text-file
"This is a text file.\n# vim: set ts=18 :\n"
(assert-equal 'tab-width 18)
(assert-equal 'fill-column 40)))
(check "test-expandtab"
"Check expandtabs works."
(with-temp-default
indent-tabs-mode t
(run-checks-for-text-file
"# vim: set expandtab :\n"
(assert-false 'indent-tabs-mode))))
(check "test-noexpandtab"
"Check noexpandtabs works."
(with-temp-default
indent-tabs-mode nil
(run-checks-for-text-file
"# vim: set noexpandtab :\n"
(assert-true 'indent-tabs-mode)))))
(define-test-suite test-local-vars-interaction
"checks for interactions with Emacs local variables"
(let ((filebody "# vim: set ts=18 :
Some random text in the middle of the file.
Local Variables:
tab-width: 11
fill-column: 59
End:
"))
(check "test-ignore-mode-line"
"Check that we ignore modelines when `vimvars-ignore-mode-line-if-local-variables-exist'."
(let ((vimvars-ignore-mode-line-if-local-variables-exist t))
(run-checks-for-text-file
filebody
(assert-equal 'tab-width 11)
(assert-equal 'fill-column 59)))
(let ((vimvars-ignore-mode-line-if-local-variables-exist nil))
(run-checks-for-text-file
filebody
(assert-equal 'tab-width 18)
(assert-equal 'fill-column 59))))))
(let
((debug-on-error t))
(with-output-to-temp-buffer "*Unit Test Results*"
(unit-test-accept-tag)
(unit-test-vimvars-should-obey-modeline)
(unit-test-vimvars-expand-option-name)
(test-basics)
(test-modeline-format)
(test-tabstop)
(test-misc)
(test-readonly)
(test-shiftwidth)
(test-local-vars-interaction)))