diff --git a/test/zen/schema_test.clj b/test/zen/schema_test.clj new file mode 100644 index 0000000..b736e8d --- /dev/null +++ b/test/zen/schema_test.clj @@ -0,0 +1,73 @@ +(ns zen.schema-test + (:require [zen.schema :as sut] + [clojure.test :as t] + [clojure.string :as str] + [zen.core])) + + +(t/deftest custom-interpreter-test + (t/testing "typescript type generation" + (def ztx (zen.core/new-context {})) + + (def my-structs-ns + '{:ns my-sturcts + + User + {:zen/tags #{zen/schema} + :type zen/map + :keys {:id {:type zen/string} + :email {:type zen/string + #_#_:regex "@"}}}}) + + (zen.core/load-ns ztx my-structs-ns) + + (def ts-typedef-assert + (str "type User = {" + "id: string;" + "email: string;" + "}")) + + (sut/register-compile-key-interpreter! + [:keys ::ts] + (fn [_ ztx ks] + (fn [vtx data opts] + (if-let [s (or (when-let [nm (:zen/name data)] + (str "type " (name nm) " = {")) + (when-let [tp (:type data)] + (str (name (last (:path vtx))) ": " + (get {'zen/string "string"} + tp) + ";")))] + (update vtx ::ts conj s) + vtx)))) + + (sut/register-compile-key-interpreter! + [:type ::ts] + (fn [_ ztx ks] + (fn [vtx data opts] + (-> vtx + #_(update ::ts conj [:type (:schema vtx) (:path vtx) data]))))) + + (sut/register-schema-pre-process-hook! + ::ts + (fn [ztx schema] + (fn [vtx data opts] + (-> vtx + #_(update ::ts conj [:pre (:schema vtx) (:path vtx) data]))))) + + (sut/register-schema-post-process-hook! + ::ts + (fn [ztx schema] + (fn [vtx data opts] + (if-let [nm (:zen/name data)] + (update vtx ::ts conj "}") + vtx)))) + + (def r + (sut/apply-schema ztx + {::ts []} + (zen.core/get-symbol ztx 'zen/schema) + (zen.core/get-symbol ztx 'my-sturcts/User) + {:interpreters [#_:zen.v2-validation/validate ::ts]})) + + (t/is (= ts-typedef-assert (str/join "" (distinct (::ts r)))))))