Skip to content

Commit 1cfbf2f

Browse files
committed
Add indentation test setup
1 parent 9e5d08f commit 1cfbf2f

File tree

2 files changed

+81
-30
lines changed

2 files changed

+81
-30
lines changed

clj/src/vim_clojure_static/test.clj

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@
2121
(defn vim-exec
2222
"Spit buf into file, then execute vim-expr after Vim loads the file. The
2323
value of vim-expr is evaluated as EDN and returned."
24-
[file buf vim-expr]
25-
(with-tempfile tmp
26-
(io/make-parents file)
27-
(spit file buf)
28-
(spit tmp (str "let @x = " vim-expr))
29-
(shell/sh "vim" "-N" "-u" "vim/test-runtime.vim"
30-
"-c" (str "source " tmp " | call writefile([@x], " (pr-str (str tmp)) ") | quitall!")
31-
file)
32-
(edn/read-string (slurp tmp))))
24+
[file buf vim-expr & opts]
25+
(let [{:keys [pre]} (apply hash-map opts)]
26+
(with-tempfile tmp
27+
(io/make-parents file)
28+
(spit file buf)
29+
(spit tmp (str "let @x = " vim-expr))
30+
(let [{:keys [exit err]}
31+
(shell/sh "vim" "-N" "-u" "vim/test-runtime.vim"
32+
"-c" (or pre "execute")
33+
"-c" (str "source " tmp)
34+
"-c" (str "call writefile([@x], " (pr-str (str tmp)) ")")
35+
"-c" "quitall!"
36+
file)]
37+
(when-not (zero? exit)
38+
(throw (RuntimeException. ^String err))))
39+
(edn/read-string (slurp tmp)))))
3340

3441
(defn syn-id-names
3542
"Map lines of clojure text to vim synID names at each column as keywords:
@@ -107,6 +114,42 @@
107114
[coll#]
108115
(boolean (some (partial not= ~kw) coll#)))))
109116

117+
(defmacro with-transform-test
118+
"Copy contents of `in` to a tempfile, execute body with tempfile bound to
119+
tmp-sym, then finally compare the transformed contents of the tempfile with
120+
the contents of `out`.
121+
122+
`in` and `out` are urls that will be passed to clojure.java.io/resource."
123+
{:requires [#'test/testing #'with-tempfile]}
124+
[string {:keys [in out]} [tmp-sym] & body]
125+
`(test/testing ~string
126+
(with-tempfile ~tmp-sym
127+
(try
128+
(spit ~tmp-sym (slurp (~io/resource ~in)))
129+
~@body
130+
(catch Throwable e#
131+
(spit ~tmp-sym e#))
132+
(finally
133+
(test/is (= (slurp ~tmp-sym)
134+
(slurp (~io/resource ~out)))))))))
135+
136+
(defmacro test-indent
137+
{:requires [#'with-transform-test]}
138+
[string & opts]
139+
(let [{:keys [in out pre keys]} (apply hash-map opts)
140+
test-file (str "tmp/test-indent-" (string/replace string #"[^\w-]" "-") ".clj")
141+
vim-expr (format "IndentFile(%s)"
142+
(if keys
143+
(string/replace (pr-str keys) "\\\\" "\\")
144+
""))]
145+
`(with-transform-test ~string
146+
{:in ~in :out ~out}
147+
[tmp#]
148+
;; FIXME: Too much file IO
149+
(spit ~test-file "")
150+
(~vim-exec ~test-file (slurp tmp#) ~vim-expr :pre ~pre)
151+
(spit tmp# (slurp ~test-file)))))
152+
110153
(defn benchmark [n file buf & exprs]
111154
(vim-exec file buf (format "Benchmark(%d, %s)"
112155
n

clj/vim/test-runtime.vim

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,47 @@
11
" Authors: Sung Pae <[email protected]>
22

33
execute 'set rtp=' . expand('%:p:h:h:h') . ',$VIMRUNTIME'
4-
filetype plugin on
4+
filetype plugin indent on
55
syntax on
66
set synmaxcol=0
77
setfiletype clojure
88

99
function! EDN(value)
10-
" Changing the quotes may make this valid EDN
11-
return tr(string(a:value), "'", '"')
10+
" Changing the quotes may make this valid EDN
11+
return tr(string(a:value), "'", '"')
1212
endfunction
1313

1414
function! ClojureSynIDNames()
15-
let names = []
16-
for lnum in range(1, line('$'))
17-
let f = 'synIDattr(synID(' . lnum . ', v:val, 0), "name")'
18-
call add(names, map(range(1, virtcol([lnum, '$']) - 1), f))
19-
endfor
20-
return EDN(names)
15+
let names = []
16+
for lnum in range(1, line('$'))
17+
let f = 'synIDattr(synID(' . lnum . ', v:val, 0), "name")'
18+
call add(names, map(range(1, virtcol([lnum, '$']) - 1), f))
19+
endfor
20+
return EDN(names)
21+
endfunction
22+
23+
function! IndentFile(...)
24+
if a:0 | execute 'normal! ' . a:1 | endif
25+
normal! gg=G
26+
write
2127
endfunction
2228

2329
function! Time(n, expr)
24-
let start = reltime()
25-
let i = 0
26-
while i < a:n
27-
execute a:expr
28-
let i += 1
29-
endwhile
30-
return eval(reltimestr(reltime(start)))
30+
let start = reltime()
31+
let i = 0
32+
while i < a:n
33+
execute a:expr
34+
let i += 1
35+
endwhile
36+
return eval(reltimestr(reltime(start)))
3137
endfunction
3238

3339
function! Benchmark(n, ...)
34-
let times = []
35-
for expr in a:000
36-
call add(times, Time(a:n, expr))
37-
endfor
38-
return EDN(times)
40+
let times = []
41+
for expr in a:000
42+
call add(times, Time(a:n, expr))
43+
endfor
44+
return EDN(times)
3945
endfunction
46+
47+
" vim:sts=8:sw=8:ts=8:noet

0 commit comments

Comments
 (0)