From e830e839393b28d69188898400df4d01396dbb78 Mon Sep 17 00:00:00 2001 From: liquidz Date: Mon, 22 Jan 2024 22:38:24 +0900 Subject: [PATCH] test: Add test for elin.function.sexp/get-namespace --- src/elin/function/sexp.clj | 2 +- test/elin/function/sexp_test.clj | 27 +++++++++ test/elin/test_helper.clj | 98 ++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 test/elin/function/sexp_test.clj diff --git a/src/elin/function/sexp.clj b/src/elin/function/sexp.clj index 32c98046..a025b0a0 100644 --- a/src/elin/function/sexp.clj +++ b/src/elin/function/sexp.clj @@ -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 diff --git a/test/elin/function/sexp_test.clj b/test/elin/function/sexp_test.clj new file mode 100644 index 00000000..c4f129fc --- /dev/null +++ b/test/elin/function/sexp_test.clj @@ -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)")))))) diff --git a/test/elin/test_helper.clj b/test/elin/test_helper.clj index 55c80c7d..a53644bd 100644 --- a/test/elin/test_helper.clj +++ b/test/elin/test_helper.clj @@ -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 []])