Skip to content

Commit

Permalink
[nop] Housekeeping
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed May 6, 2024
1 parent a865e8f commit 92bfe26
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 71 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

:dependencies
[[org.clojure/test.check "1.1.1"]
[com.taoensso/encore "3.62.0"
[com.taoensso/encore "3.77.0"
:exclusions [com.taoensso/truss]]]}

:graal-tests
Expand Down
14 changes: 7 additions & 7 deletions src/taoensso/truss.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
trapping errors. If any pred test fails, throws a detailed assertion error.
Otherwise returns input val/vals for convenient inline-use/binding.
Respects *assert* value so tests can be elided from production for zero
Respects `*assert*` value so tests can be elided from production for zero
runtime costs.
Provides a small, simple, flexible feature subset to alternative tools like
Expand Down Expand Up @@ -64,8 +64,8 @@

#?(:clj
(defmacro have!
"Like `have` but ignores *assert* value (so can never be elided). Useful
for important conditions in production (e.g. security checks)."
"Like `have` but ignores `*assert*` value (so can never be elided).
Useful for important conditions in production (e.g. security checks)."
{:arglists '([x] [pred (:in) x] [pred (:in) x & more-xs])}
[& args]
(let [[&form args] (clj-865-workaround &form args)
Expand All @@ -79,7 +79,7 @@
when the return vals aren't necessary.
**WARNING**: Do NOT use in :pre/:post conds since those are ALWAYS subject
to *assert*, directly contradicting the intention of the bang (`!`) here."
to `*assert*`, directly contradicting the intention of the bang (`!`) here."
{:arglists '([x] [pred (:in) x] [pred (:in) x & more-xs])}
[& args]
(let [[&form args] (clj-865-workaround &form args)
Expand Down Expand Up @@ -116,16 +116,16 @@

(macroexpand '(have string? 5))
(macroexpand '(have string? 5 :data "foo"))
(macroexpand '(have string? 5 :data (enc/get-env)))
(let [x :x] (have string? 5 :data (enc/get-env)))
(macroexpand '(have string? 5 :data (enc/get-locals)))
(let [x :x] (have string? 5 :data (enc/get-locals)))

(have string? 5)
(have string? 5 :data {:a "a"})
(have string? 5 :data {:a (/ 5 0)})

((fn [x]
(let [a "a" b "b"]
(have string? x :data {:env (enc/get-env)}))) 5)
(have string? x :data {:env (enc/get-locals)}))) 5)

(do
(set! *assert* false)
Expand Down
96 changes: 36 additions & 60 deletions src/taoensso/truss/impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,17 @@
;; - Allows Encore to depend on Truss (esp. nb for back-compatibility wrappers).
;; - Allows Truss to be entirely dependency free.

#?(:clj (defmacro if-cljs [then else] (if (:ns &env) then else)))
#?(:clj
(defmacro catching
"Cross-platform try/catch/finally."
;; Very unfortunate that CLJ-1293 has not yet been addressed
([try-expr ] `(catching ~try-expr ~'_ nil))
([try-expr error-sym catch-expr]
`(if-cljs
(try ~try-expr (catch js/Error ~error-sym ~catch-expr))
(try ~try-expr (catch Throwable ~error-sym ~catch-expr))))
(if (:ns &env)
`(try ~try-expr (catch js/Error ~error-sym ~catch-expr))
`(try ~try-expr (catch Throwable ~error-sym ~catch-expr))))
([try-expr error-sym catch-expr finally-expr]
`(if-cljs
(try ~try-expr (catch js/Error ~error-sym ~catch-expr) (finally ~finally-expr))
(try ~try-expr (catch Throwable ~error-sym ~catch-expr) (finally ~finally-expr))))))
(if (:ns &env)
`(try ~try-expr (catch js/Error ~error-sym ~catch-expr) (finally ~finally-expr))
`(try ~try-expr (catch Throwable ~error-sym ~catch-expr) (finally ~finally-expr))))))

(defn rsome [pred coll] (reduce (fn [acc in] (when-let [p (pred in)] (reduced p))) nil coll))
(defn revery? [pred coll] (reduce (fn [acc in] (if (pred in) true (reduced nil))) true coll))
Expand All @@ -51,9 +48,9 @@
(if-not (:ns env)
*file* ; Compiling clj
(or ; Compiling cljs
(when-let [url (and file (try (io/resource file) (catch Throwable _ nil)))]
(try (.getPath (io/file url)) (catch Throwable _ nil))
(do (str url)))
(when-let [url (and file (catching (io/resource file)))]
(catching (.getPath (io/file url)))
(do (str url)))
file))]

{:ns (str *ns*)
Expand All @@ -67,32 +64,31 @@
(comment (io/resource "taoensso/truss.cljc"))

#?(:clj
(let [resolve-clj clojure.core/resolve
resolve-cljs
(let [resolve-clj clojure.core/resolve ; Returns var
resolve-cljs ; Returns ?{:keys [meta ns name]}
(when-let [ns (find-ns 'cljs.analyzer.api)]
(when-let [v (ns-resolve ns 'resolve)] @v))]

(defn resolve-var
#?(:clj ([sym] (resolve-clj sym)))
([env sym]
(when (symbol? sym)
(if (:ns env)
(when resolve-cljs (resolve-cljs env sym))
(do (resolve-clj env sym))))))))

(comment (resolve-var nil 'string?))

#?(:clj
(defn- var->sym [cljs? v]
(let [m (if cljs? v (meta v))]
(symbol (str (:ns m)) (name (:name m))))))

#?(:clj
(defn resolve-sym
#?(:clj ([sym] (when-let [v (resolve-var sym)] (var->sym false v))))
([env sym] (when-let [v (resolve-var env sym)] (var->sym (:ns env) v)))))

(comment (resolve-sym nil 'string?))
(when-let [v (ns-resolve ns 'resolve)] @v))

resolve-auto
(fn [macro-env sym]
(when-let [m
(if (:ns macro-env)
(and resolve-cljs (resolve-cljs macro-env sym))
(meta (resolve-clj macro-env sym)))]
(symbol (str (:ns m)) (name (:name m)))))]

(defn resolve-sym
[macro-env sym may-require-ns?]
(when (symbol? sym)
(if-not may-require-ns?
(resolve-auto macro-env sym)
(or
(resolve-auto macro-env sym)
(when-let [ns (namespace sym)]
(when (catching (do (require (symbol ns)) true))
(resolve-auto macro-env sym)))))))))

(comment (resolve-sym nil 'string? false))

;;;; Truss

Expand Down Expand Up @@ -123,7 +119,7 @@
(keyword? pred-form)
(map? pred-form)
(set? pred-form)
(when-let [rsym (resolve-sym env pred-form)]
(when-let [rsym (resolve-sym env pred-form false)]
(contains? safe-pred-forms rsym)))))

(comment (safe-pred-form? nil 'nil?))
Expand All @@ -136,7 +132,7 @@
(= pred-form ::some?) (parse-pred-form env `some?)
(not (vector? pred-form))
{:pred-form pred-form
:rsym (resolve-sym env pred-form)
:rsym (resolve-sym env pred-form true)
:safe? (safe-pred-form? env pred-form)}

:else
Expand Down Expand Up @@ -212,16 +208,6 @@
(parse-pred-form nil [:and 'string? 'seq])
(parse-pred-form nil [:and 'integer? [:and 'number? 'pos? 'int?]])])

;; #?(:clj
;; (defn- fast-pr-str
;; "Combination `with-out-str`, `pr`. Ignores *print-dup*."
;; [x]
;; (let [w (java.io.StringWriter.)]
;; (print-method x w)
;; (.toString w))))

;; (comment (enc/qb 1e5 (pr-str {:a :A}) (fast-pr-str {:a :A})))

(defn- error-message
;; Temporary, to support Clojure 1.9
;; Clojure 1.10+ now has `ex-message`
Expand All @@ -242,16 +228,6 @@
arg-val (if undefn-arg? 'truss/undefined-arg arg)
arg-type (if undefn-arg? 'truss/undefined-arg (type arg))

;; arg-str
;; (cond
;; undefn-arg? "<truss/undefined-arg>"
;; (nil? arg) "<truss/nil>"
;; :else
;; (binding [*print-readably* false
;; *print-length* 3]
;; #?(:clj (fast-pr-str arg)
;; :cljs (pr-str arg))))

?err
(cond
(identical? -dummy-error ?err) nil
Expand All @@ -261,7 +237,7 @@

msg_
(delay
(let [;arg-form (if (nil? arg-form) 'nil arg-form)
(let [; arg-form (if (nil? arg-form) 'nil arg-form)
msg
(str "Invariant failed at " ns-sym
(when ?line (str "[" ?line (when ?column (str "," ?column)) "]")) ": "
Expand Down
7 changes: 4 additions & 3 deletions test/taoensso/truss_tests.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@
:cljs 'cljs.core/string?)))])

(testing "Special preds"
[(is (= nil (have [:or nil? string?] nil)))
(is (= "hello" (have [:or nil? string?] "hello")))
(is (= "hello" (have [:or pos? string?] "hello")))
[(is (= nil (have [:or nil? string?] nil)))
(is (= "hello" (have [:or nil? string?] "hello")))
(is (= "hello" (have [:or pos? string?] "hello")))
(is (= ["a" "b"] (have [:or pos? string?] "a" "b")))

(is (throws? :common {:arg {:value -5}}
(have [:or pos? string?] -5)))
Expand Down

0 comments on commit 92bfe26

Please sign in to comment.