From 9815dae79d4780535180b8c8e7c9e6edcc8e0559 Mon Sep 17 00:00:00 2001 From: XeroOl Date: Fri, 29 Jul 2022 00:08:14 -0500 Subject: [PATCH] Test rework, and more file tests --- Makefile | 1 - fls/file.fnl | 11 +++++++++-- main.fnl | 6 +++--- test/file-test.fnl | 43 +++++++++++++++++++++++++++++++++++------- test/lsp-test.fnl | 21 +++++++++------------ test/macros.fnl | 24 ++++++++++++++++++++--- test/protocol-test.fnl | 20 +++++++++----------- 7 files changed, 87 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index 91d9a19..58f3df8 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,5 @@ fennel-ls: $(SOURCES) fennel --compile-binary main.fnl fennel-ls $(STATIC_LUA_LIB) $(LUA_INCLUDE_PATH) test: - @echo testing fennel test.fnl diff --git a/fls/file.fnl b/fls/file.fnl index ba60699..c50d4fa 100644 --- a/fls/file.fnl +++ b/fls/file.fnl @@ -17,6 +17,13 @@ (icollect [line (file:lines)] line)))) -{: make-file - : make-file-from-disk} +(λ sub [self start-line start-char end-line end-char replacement] + (local index (+ 1 start-line)) + (tset self.lines index + (.. (string.sub (. self.lines index) 1 start-char) + replacement + (string.sub (. self.lines index) (+ 1 end-char))))) +{: make-file + : make-file-from-disk + : sub} diff --git a/main.fnl b/main.fnl index 2100a1d..abec441 100644 --- a/main.fnl +++ b/main.fnl @@ -2,7 +2,8 @@ (local fls (require :fls)) (local {: run} (require :fennel-ls)) -(λ main-loop [in out state] +(λ main-loop [in out] + (var state {}) (while (let [msg (fls.protocol.read in)] (fls.log.log msg) @@ -14,7 +15,6 @@ (λ main [] (main-loop (io.input) - (io.output) - (fls.state.new-state))) + (io.output))) (main) diff --git a/test/file-test.fnl b/test/file-test.fnl index e4c9d7a..6b54bf3 100644 --- a/test/file-test.fnl +++ b/test/file-test.fnl @@ -1,11 +1,10 @@ -(import-macros {: it! : describe!} :test.macros) +(import-macros {: assert-matches : describe : it} :test.macros) (local assert (require :luassert)) (local fennel (require :fennel)) (local fls (require :fls)) (local stringx (require :pl.stringx)) - (local ROOT-URI (.. "file://" (-> (io.popen "pwd") @@ -13,19 +12,49 @@ (stringx.strip)) "/")) -(describe! "File Loading" - (it! "can open files from disk" +(local FILE-URI (.. ROOT-URI "test_file")) + +(describe "File Loading" + (it "can open files from disk" (local uri (.. ROOT-URI "test.fnl")) (local test-fnl-file (fls.file.make-file-from-disk uri)) (assert.equal (. test-fnl-file.lines 1) "((require :busted.runner))")) - (it! "can have files with fixed contents" - (local uri (.. ROOT-URI "test.fnl")) + (it "can open files from fixed contents" + (local uri (.. ROOT-URI "test_file")) (local my-file (fls.file.make-file uri ["line 1" "line 2" "line 3"])) (assert (match my-file {:lines ["line 1" "line 2" "line 3"]} true - otherwise (values false (fennel.view otherwise)))))) + otherwise (values false (fennel.view otherwise))))) + + (it "can update the start of a line" + (local my-file (fls.file.make-file FILE-URI ["replace beginning"])) + (fls.file.sub my-file 0 0 0 7 "the") + (assert-matches my-file {:lines ["the beginning"]})) + + (it "can update the end of a line" + (local my-file (fls.file.make-file FILE-URI ["replace end"])) + (fls.file.sub my-file 0 7 0 11 "ment") + (assert-matches my-file {:lines ["replacement"]})) + + (it "can replace a line" + (local my-file (fls.file.make-file FILE-URI ["replace all"])) + (fls.file.sub my-file 0 0 0 11 "new string") + (assert-matches my-file {:lines ["new string"]}))) + + ;; next steps: + ;; test for errors on out of bounds + ;; test for multiline edits + ;; test for unicode utf8 utf16 nightmare + + ;; (it "can handle unicode" + ;; (local uri (.. ROOT-URI "test_file")) + ;; (local my-file (fls.file.make-file uri [""])) + ;; (fls.file.sub my-file 0 0 0 0 "どれみふぁそらてぃど") + ;; (fls.file.sub my-file 0 1 0 3 "😀") + ;; (fls.file.sub my-file 0 11 0 11 "end") + ;; (assert-matches my-file {:lines ["ど😀ふぁそらてぃどend"]}))) diff --git a/test/lsp-test.fnl b/test/lsp-test.fnl index 397b5f1..497a5c1 100644 --- a/test/lsp-test.fnl +++ b/test/lsp-test.fnl @@ -1,12 +1,12 @@ -(import-macros {: it! : describe!} :test.macros) +(import-macros {: assert-matches : describe : it} :test.macros) (local assert (require :luassert)) (local {: run} (require :fennel-ls)) (local fennel (require :fennel)) -(describe! "initialization" +(describe "initialization" ;; TODO get rid of hardcoded paths here - (it! "responds to initialize" + (it "responds to initialize" (local initialize {:id 1 :jsonrpc "2.0" @@ -21,12 +21,9 @@ :trace "off" :workspaceFolders [{:name "/home/xerool/Documents/projects/fennel-ls" :uri "file:///home/xerool/Documents/projects/fennel-ls"}]}}) - (assert - (match (run [] initialize) - {:id 1 - :jsonrpc "2.0" - :result {:capabilities {} - :serverInfo {:name "fennel-ls" : version}}} - true - otherwise (values false (fennel.view otherwise)))))) - + (assert-matches + (run [] initialize) + {:id 1 + :jsonrpc "2.0" + :result {:capabilities {} + :serverInfo {:name "fennel-ls" : version}}}))) diff --git a/test/macros.fnl b/test/macros.fnl index c9f6877..495f71c 100644 --- a/test/macros.fnl +++ b/test/macros.fnl @@ -1,4 +1,22 @@ -(fn it! [title ...] `((. (require :busted) :it) ,title (fn [] ,...))) -(fn describe! [title ...] `((. (require :busted) :describe) ,title (fn [] ,...))) +(fn it [title ...] + `((. (require :busted) :it) + ,title (fn [] ,...))) -{: it! : describe!} +(fn describe [title ...] + `((. (require :busted) :describe) + ,title (fn [] ,...))) + +(fn assert-matches [item pattern] + `(match ,item + ,pattern nil + ?otherwise# + (error + (.. "Pattern did not match:\n" + (let [fennel# (require :fennel)] + (fennel#.view ?otherwise#)) + "\ndid not match pattern:\n" + ,(view pattern))))) + +{: it + : describe + : assert-matches} diff --git a/test/protocol-test.fnl b/test/protocol-test.fnl index 60b4e84..7559df6 100644 --- a/test/protocol-test.fnl +++ b/test/protocol-test.fnl @@ -1,4 +1,4 @@ -(import-macros {: it! : describe!} :test.macros) +(import-macros {: assert-matches : describe : it} :test.macros) (local assert (require :luassert)) (local fennel (require :fennel)) @@ -6,19 +6,19 @@ (local fls (require :fls)) (local {: run} (require :fennel-ls)) -(describe! "fls.protocol" - (it! "parses incoming messages" +(describe "fls.protocol" + (it "parses incoming messages" (let [out (stringio.open "Content-Length: 29\r\n\r\n{\"my json content\":\"is cool\"}")] (assert.same {"my json content" "is cool"} (fls.protocol.read out)))) - (it! "serializes outgoing messages" + (it "serializes outgoing messages" (let [in (stringio.create)] (fls.protocol.write in {"my json content" "is cool"}) (assert.same "Content-Length: 29\r\n\r\n{\"my json content\":\"is cool\"}" (in:value)))) - (it! "can read multiple incoming messages" + (it "can read multiple incoming messages" (let [out (stringio.open "Content-Length: 29\r\n\r\n{\"my json content\":\"is cool\"}Content-Length: 29\r\n\r\n{\"my json content\":\"is cool\"}")] (assert.same {"my json content" "is cool"} (fls.protocol.read out)) @@ -27,10 +27,8 @@ (assert.same nil (fls.protocol.read out)))) - (it! "can report the ParseError code" + (it "can report the ParseError code" (let [out (stringio.open "Content-Length: 9\r\n\r\n{{{{{}}}}")] - (assert - (match (run [] (fls.protocol.read out)) - {:error {:code -32700} :jsonrpc "2.0"} - true - otherwise (values false (fennel.view otherwise))))))) + (assert-matches + (run [] (fls.protocol.read out)) + {:error {:code -32700} :jsonrpc "2.0"}))))