Skip to content

Commit

Permalink
[#38] Add naive ts type generator test
Browse files Browse the repository at this point in the history
  • Loading branch information
KGOH committed Jan 20, 2023
1 parent c480aa6 commit 47f579b
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions test/zen/schema_test.clj
Original file line number Diff line number Diff line change
@@ -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)))))))

0 comments on commit 47f579b

Please sign in to comment.