Skip to content

Commit

Permalink
test: Add test for elin.function.sexp/get-namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidz committed Jan 22, 2024
1 parent eff06cd commit e830e83
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/elin/function/sexp.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
[writer lnum col]
(e.u.function/call-function writer "elin#compat#sexp#get_expr" [lnum col]))

(m/=> get-namespace [:=> [:cat e.s.server/?Writer] [:maybe string?]])
(m/=> get-namespace [:=> [:cat e.s.server/?Writer] [:maybe symbol?]])
(defn get-namespace
[writer]
(try
Expand Down
27 changes: 27 additions & 0 deletions test/elin/function/sexp_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(ns elin.function.sexp-test
(:require
[clojure.test :as t]
[elin.function.sexp :as sut]
[elin.test-helper :as h]))

(defn- get-namespace-writer
[ns-form]
(let [handler #(if (h/call-function? % "elin#internal#clojure#get_ns_form")
ns-form
"")]
(h/test-writer {:handler handler})))

(t/deftest get-namespace-test
(t/testing "no metadata"
(t/is (= 'foo.bar
(sut/get-namespace (get-namespace-writer "(ns foo.bar)")))))

(t/testing "with metadata"
(t/is (= 'foo.bar
(sut/get-namespace (get-namespace-writer "(ns ^:meta foo.bar)"))))
(t/is (= 'foo.bar
(sut/get-namespace (get-namespace-writer "(ns ^{:meta true} foo.bar)")))))

(t/testing "in-ns"
(t/is (= 'foo.bar
(sut/get-namespace (get-namespace-writer "(in-ns 'foo.bar)"))))))
98 changes: 98 additions & 0 deletions test/elin/test_helper.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,105 @@
(ns elin.test-helper
(:require
[clojure.core.async :as async]
[elin.protocol.rpc :as e.p.rpc]
[elin.schema.server :as e.s.server]
[elin.util.id :as e.u.id]
[malli.core :as m]
[malli.dev.pretty :as m.d.pretty]
[malli.instrument :as m.inst]))

(def ?TestMessageOption
[:map
[:handler [:=> [:cat [:sequential any?]] any?]]])

(m.inst/instrument!
{:report (m.d.pretty/reporter)})

(defprotocol ITestWriter
(get-outputs [this]))

(defrecord TestMessage
[host message]
e.p.rpc/IMessage
(request? [_]
(= 0 (first message)))

(response? [_]
(= 1 (first message)))

(parse-message [_]
(condp = (first message)
;; request
0 (let [[_ id method [params]] message]
{:id id
:method (keyword method)
:params params})
;; response
1 (let [[_ id error result] message]
{:id id
:error error
:result result})
;; notify
2 (let [[_ method [params callback]] message]
{:method (keyword method)
:params params
:callback callback})
{})))

(defrecord TestWriter
[output-stream outputs option]
e.p.rpc/IWriter
(request! [_ content]
(let [id (e.u.id/next-id)
{:keys [handler]} option
[result error] (try
[(handler (concat [0 id] content))]
(catch Exception ex
[nil (ex-message ex)]))
ch (async/chan)]
(async/go
(async/>! ch {:result result :error error}))
ch))

(notify! [_ content]
(let [{:keys [handler]} option]
(handler (concat [2] content))
nil))

(response! [_this _error _result]
nil)

e.p.rpc/IFunction
(call-function [this method params]
(e.p.rpc/request! this ["test_call_function" [method params]]))

(echo-text [_ text]
(swap! outputs conj text))
(echo-message [_ text]
(swap! outputs conj text))
(echo-message [_ text _highlight]
(swap! outputs conj text))

ITestWriter
(get-outputs [_] @outputs))

(m/=> test-message [:=> :cat e.s.server/?Message])
(defn test-message
[]
(map->TestMessage {:host "test"
:message []}))

(m/=> test-writer [:=> [:cat ?TestMessageOption] e.s.server/?Writer])
(defn test-writer
[option]
(map->TestWriter {:output-stream (java.io.ByteArrayOutputStream.)
:outputs (atom [])
:option option}))

;;(0 7 test_call_function [elin#internal#clojure#get_ns_form []])
(defn call-function? [msg fn-name]
(and
(= "test_call_function" (nth msg 2))
(= fn-name (first (nth msg 3)))))

;;(0 7 test_call_function [elin#internal#clojure#get_ns_form []])

0 comments on commit e830e83

Please sign in to comment.