Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite indentation testing code #26

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/clojure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
env:
cache-name: cache-m2
with:
path: ~/.m2
path: ~/.m2/repository
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/project.clj') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
Expand Down
3 changes: 1 addition & 2 deletions clj/.clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
{:lint-as {vim.test/with-tempfile clojure.core/fn
vim.test/defpredicates clojure.core/def
{:lint-as {vim.test/defpredicates clojure.core/def
vim.test/defsyntaxtest clojure.core/def}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.")

;; vim:ft=clojure:
5 changes: 5 additions & 0 deletions clj/resources/indent-test-cases/basic-sexp/out.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns test-basic-sexp-indent
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.")
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@ bar))

(#_(foo bar)
a)

;; vim:ft=clojure:
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@

(#_(foo bar)
a)

;; vim:ft=clojure:
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{:indent? false
:extra-cmds ["normal! gg"
"exec \"normal! /α\\<CR>s\\<C-O>Oa\\<Esc>/β\\<CR>s\\<CR>\\<CR>\\<C-H>\\<C-H>\\<C-H>\\<C-H>\\<C-H>\\<C-H>\\<C-H>b\\<CR>c\\<CR>\\<CR>d\\<Esc>\""]}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,3 @@
(six-times [y]
(* (twice y) 3))]
(foo #{:foo :bar :biz} :foo))

;; vim:ft=clojure:
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,3 @@
(six-times [y]
(* (twice y) 3))]
(foo #{:foo :bar :biz} :foo))

;; vim:ft=clojure:
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
(let [Δt (if foo
bar
baz)])

;; vim:ft=clojure:
3 changes: 3 additions & 0 deletions clj/resources/indent-test-cases/multibyte-indentation/out.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(let [Δt (if foo
bar
baz)])
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@

#?@(:clj [5 6 7 8]
:cljs [1 2 3 4])))

;; vim:ft=clojure:
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@

#?@(:clj [5 6 7 8]
:cljs [1 2 3 4])))

;; vim:ft=clojure:
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{:indent? false
:extra-cmds ["normal! gg"
"exec \"normal! /α\\<CR>:call GetClojureIndent()\\<CR>rxj:call GetClojureIndent()\\<CR>ry\""]}
21 changes: 21 additions & 0 deletions clj/test/vim/helpers.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(ns vim.helpers
(:require [clojure.edn :as edn]
[clojure.java.shell :as shell])
(:import [java.io File FileReader PushbackReader]))

(defn read-edn-file [^File file]
(when (.exists file)
(with-open [rdr (FileReader. file)]
(edn/read (PushbackReader. rdr)))))

(def ^:dynamic *vim* "vim")

(defn vim!
"Run commands on a file in Vim."
[^File file cmds & {:keys [vimrc], :or {vimrc "NONE"}}]
(let [cmds (mapcat (fn [cmd] ["-c" cmd]) cmds)
args (concat ["--clean" "-N" "-u" (str vimrc)] cmds ["-c" "quitall!" "--" (str file)])
ret (apply shell/sh *vim* args)]
(when (pos? (:exit ret))
(throw (ex-info "Failed to run Vim command"
(assoc ret :vim *vim*, :args args))))))
75 changes: 39 additions & 36 deletions clj/test/vim/indent_test.clj
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
(ns vim.indent-test
(:require [clojure.test :refer [deftest]]
[vim.test :refer [test-indent]]))
(:require [clojure.test :refer [deftest testing is]]
[clojure.string :as str]
[clojure.java.io :as io]
[vim.helpers :as h])
(:import [java.io File]))

(deftest test-basic-sexp-indent
(test-indent "works as expected with basic S-expressions"
:in "test-basic-sexp-indent.txt"
:out "test-basic-sexp-indent.txt"))
(defn get-test-cases [^File test-case-dir]
(into []
(comp
(filter #(.isDirectory ^File %))
(map #(.getName ^File %)))
(.listFiles test-case-dir)))

(deftest test-multibyte-indent
(test-indent "with multibyte characters"
:in "test-multibyte-indent.txt"
:out "test-multibyte-indent.txt"))
(defn run-test-case [test-case-dir test-case]
(testing (str "Preparation for " test-case)
(let [input (io/file test-case-dir test-case "in.clj")
expected (io/file test-case-dir test-case "out.clj")
actual (File/createTempFile test-case ".clj")
config (let [f (io/file test-case-dir test-case "config.edn")]
(or (h/read-edn-file f) {}))
cmds (concat (:extra-cmds config)
(when (:indent? config true) ["normal! gg=G"])
["write"])]
(io/make-parents actual)
(io/copy input actual)
(h/vim! actual cmds :vimrc (io/file "vim/test-runtime.vim"))
{:test-case test-case
:expected (slurp expected)
:expected-file expected
:actual (slurp actual)
:actual-file actual})))

(deftest test-inherit-indent
(test-indent "is inherited from previous element"
:in "test-inherit-indent.in"
:out "test-inherit-indent.out"
:keys "/α\\<CR>s\\<C-O>Oa\\<Esc>/β\\<CR>s\\<CR>\\<CR>\\<C-H>\\<C-H>\\<C-H>\\<C-H>\\<C-H>\\<C-H>\\<C-H>b\\<CR>c\\<CR>\\<CR>d\\<Esc>"))

(deftest test-side-effects-in-indentexpr
(test-indent "GetClojureIndent does not move cursor"
:in "test-side-effects-in-indentexpr.in"
:out "test-side-effects-in-indentexpr.out"
:keys "/α\\<CR>:call GetClojureIndent()\\<CR>rxj:call GetClojureIndent()\\<CR>ry"))

(deftest test-reader-conditional-indent
(test-indent "reader conditionals are indented like maps"
:in "test-reader-conditional-indent.in"
:out "test-reader-conditional-indent.out"))

(deftest test-dispatch-macro-indent
(test-indent "dispatch macro indentation is handled correctly"
:in "test-dispatch-macro-indent.in"
:out "test-dispatch-macro-indent.out"))

(deftest test-special-case-indent
(test-indent "special case indentation is handled correctly"
:in "test-special-case-indent.in"
:out "test-special-case-indent.out"))
;; TODO: do this parallisation more intelligently with agents.
(deftest test-indent
"Runs all indentation tests in parallel"
(let [test-case-dir (io/file (io/resource "indent-test-cases"))
test-cases (get-test-cases test-case-dir)]
(doseq [{:keys [test-case expected expected-file actual actual-file]}
(pmap (partial run-test-case test-case-dir) test-cases)]
(testing test-case
(is (= expected actual)
(format "(not= \"%s\"\n \"%s\")" expected-file actual-file))))))
77 changes: 16 additions & 61 deletions clj/test/vim/test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,25 @@
(:import [java.io File]
[java.util List]))

(defmacro with-tempfile
{:requires [File]}
[[tmp-sym] & body]
`(let [~tmp-sym (File/createTempFile "vim-clojure-static" ".tmp")]
(try
~@body
(finally
(.delete ~tmp-sym)))))

(defn vim-exec
"Spit buf into file, then execute vim-expr after Vim loads the file. The
value of vim-expr is evaluated as EDN and returned."
value of vim-expr is evaluated as EDN and returned."
[file buf vim-expr & opts]
(let [{:keys [pre]} (apply hash-map opts)]
(with-tempfile [tmp]
(io/make-parents file)
(spit file buf)
(spit tmp (str "let @x = " vim-expr))
(let [{:keys [exit err]}
(shell/sh "vim" "-N" "-u" "vim/test-runtime.vim"
"-c" (or pre "execute")
"-c" (str "source " tmp)
"-c" (str "call writefile([@x], " (pr-str (str tmp)) ")")
"-c" "quitall!"
file)]
(when-not (zero? exit)
(throw (RuntimeException. ^String err))))
(edn/read-string (slurp tmp)))))
(let [{:keys [pre]} (apply hash-map opts)
tmp (File/createTempFile "vim-clojure-static" ".tmp")]
(io/make-parents file)
(spit file buf)
(spit tmp (str "let @x = " vim-expr))
(let [{:keys [exit err]}
(shell/sh "vim" "-N" "-u" "vim/test-runtime.vim"
"-c" (or pre "execute")
"-c" (str "source " tmp)
"-c" (str "call writefile([@x], " (pr-str (str tmp)) ")")
"-c" "quitall!"
file)]
(when-not (zero? exit)
(throw (RuntimeException. ^String err))))
(edn/read-string (slurp tmp))))

(defn syn-id-names
"Map lines of clojure text to vim synID names at each column as keywords:
Expand Down Expand Up @@ -115,42 +106,6 @@
[coll#]
(boolean (some (partial not= ~kw) coll#)))))

(defmacro with-transform-test
"Copy contents of `in` to a tempfile, execute body with tempfile bound to
tmp-sym, then finally compare the transformed contents of the tempfile with
the contents of `out`.

`in` and `out` are urls that will be passed to clojure.java.io/resource."
{:requires [#'test/testing #'with-tempfile]}
[string {:keys [in out]} [tmp-sym :as tmp-binding] & body]
`(test/testing ~string
(with-tempfile ~tmp-binding
(try
(spit ~tmp-sym (slurp (~io/resource (str "indent-test-cases/" ~in))))
~@body
(catch Throwable e#
(spit ~tmp-sym e#))
(finally
(test/is (= (slurp ~tmp-sym)
(slurp (~io/resource (str "indent-test-cases/" ~out))))))))))

(defmacro test-indent
{:requires [#'with-transform-test]}
[string & opts]
(let [{:keys [in out pre keys]} (apply hash-map opts)
test-file (str "tmp/test-indent-" (string/replace string #"[^\w-]" "-") ".clj")
vim-expr (if keys
(format "TypeKeys(%s)" (string/replace (pr-str keys) "\\\\" "\\"))
"IndentFile()")]
`(with-transform-test ~string
{:in ~in :out ~out}
[tmp#]
;; FIXME: Too much file IO
(~io/make-parents ~test-file)
(spit ~test-file "")
(~vim-exec ~test-file (slurp tmp#) ~vim-expr ~@(when pre [:pre pre]))
(spit tmp# (slurp ~test-file)))))

(defn benchmark [n file buf & exprs]
(vim-exec file buf (format "Benchmark(%d, %s)"
n
Expand Down
15 changes: 2 additions & 13 deletions clj/vim/test-runtime.vim
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
" Authors: Sung Pae <self@sungpae.com>

execute 'set rtp=' . expand('%:p:h:h:h') . ',$VIMRUNTIME'
let &rtp = getcwd() . '/..,' . &rtp
filetype plugin indent on
syntax on
syntax enable
set synmaxcol=0 backspace=2
setfiletype clojure

function! EDN(value)
" Changing the quotes may make this valid EDN
Expand All @@ -20,16 +19,6 @@ function! ClojureSynIDNames()
return EDN(names)
endfunction

function! TypeKeys(keys)
execute 'normal! ' . a:keys
write
endfunction

function! IndentFile()
normal! gg=G
write
endfunction

function! Time(n, expr)
let start = reltime()
let i = 0
Expand Down