|
21 | 21 | (defn vim-exec
|
22 | 22 | "Spit buf into file, then execute vim-expr after Vim loads the file. The
|
23 | 23 | 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))))) |
33 | 40 |
|
34 | 41 | (defn syn-id-names
|
35 | 42 | "Map lines of clojure text to vim synID names at each column as keywords:
|
|
107 | 114 | [coll#]
|
108 | 115 | (boolean (some (partial not= ~kw) coll#)))))
|
109 | 116 |
|
| 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 | + |
110 | 153 | (defn benchmark [n file buf & exprs]
|
111 | 154 | (vim-exec file buf (format "Benchmark(%d, %s)"
|
112 | 155 | n
|
|
0 commit comments