From c0ab72b14d59ec047a0ebfcadeb3d140985cd822 Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Thu, 28 Jul 2022 11:57:17 -0400 Subject: [PATCH] Close #391: improve defalias error msg --- src/cljc/schema/core.cljc | 9 ++++++--- test/clj/schema/test_macros.clj | 8 ++++++++ test/cljc/schema/core_test.cljc | 13 +++++++++---- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/cljc/schema/core.cljc b/src/cljc/schema/core.cljc index afb13415..e8c51f95 100644 --- a/src/cljc/schema/core.cljc +++ b/src/cljc/schema/core.cljc @@ -685,9 +685,9 @@ "Support for (mutually) recursive schemas by passing a var that points to a schema, e.g (recursive #'ExampleRecursiveSchema)." [schema] - (when-not #?(:clj (instance? clojure.lang.IDeref schema) - :cljs (satisfies? IDeref schema)) - (macros/error! (utils/format* "Not an IDeref: %s" schema))) + (macros/assert! #?(:clj (instance? clojure.lang.IDeref schema) + :cljs (satisfies? IDeref schema)) + "Not an IDeref: %s" schema) (Recursive. schema)) @@ -1134,6 +1134,9 @@ (clojure.core/defn schema-with-name "Records name in schema's metadata." [schema name] + (macros/assert! #?(:clj (instance? clojure.lang.IObj schema) + :cljs (satisfies? IWithMeta schema)) + "Named schema (such as the right-most `s/defalias` arg) must support metadata: %s" (utils/type-of schema)) (vary-meta schema assoc :name name)) (clojure.core/defn schema-name diff --git a/test/clj/schema/test_macros.clj b/test/clj/schema/test_macros.clj index 65ad3883..ba7ee5f6 100644 --- a/test/clj/schema/test_macros.clj +++ b/test/clj/schema/test_macros.clj @@ -24,3 +24,11 @@ [f & args] (when (sm/compile-fn-validation? &env f) `(~'is (~'thrown? ~'Throwable (~f ~@args))))) + +(defmacro is-assert! + "Assert that an assert! is thrown with msg" + [form expected-msg] + `(~'is (~'thrown-with-msg? + ~(if (sm/cljs-env? &env) 'js/Error 'java.lang.RuntimeException) + ~expected-msg + ~form))) diff --git a/test/cljc/schema/core_test.cljc b/test/cljc/schema/core_test.cljc index 28dab292..6dc2a743 100644 --- a/test/cljc/schema/core_test.cljc +++ b/test/cljc/schema/core_test.cljc @@ -7,10 +7,10 @@ - The optional last argument also checks the printed Clojure representation of the error. - (invalid-call! s x) asserts that calling the function throws an error." (:refer-clojure :exclude [parse-long]) - #?(:clj (:use clojure.test [schema.test-macros :only [valid! invalid! invalid-call!]])) + #?(:clj (:use clojure.test [schema.test-macros :only [valid! invalid! invalid-call! is-assert!]])) #?(:cljs (:use-macros [cljs.test :only [is deftest testing are]] - [schema.test-macros :only [valid! invalid! invalid-call!]])) + [schema.test-macros :only [valid! invalid! invalid-call! is-assert!]])) #?(:cljs (:require-macros [schema.macros :as macros])) (:require [clojure.string :as str] @@ -421,7 +421,8 @@ #?(:cljs (is (= '(recursive ...) (val explanation)))))) (is (= '{:black {(optional-key :red) (recursive (var schema.core-test/TestBlackNode))}} - (s/explain TestBlackNode)))) + (s/explain TestBlackNode))) + (is-assert! (s/recursive nil) #"Not an IDeref: null")) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -1517,7 +1518,11 @@ (valid! schema {:baz 123}) (invalid! schema {:baz "abc"}) (is (= 'Baz (s/schema-name schema))) - (is (= nil (s/schema-ns schema))))) + (is (= nil (s/schema-ns schema)))) + #?(:clj (is-assert! (s/schema-with-name s/Str 'Baz) + #"Named schema \(such as the right-most `s/defalias` arg\) must support metadata: class java.lang.Class") + :cljs (is-assert! (s/schema-with-name nil 'Baz) + #"Named schema \(such as the right-most `s/defalias` arg\) must support metadata: object"))) (deftest schema-name-test (is (= 'TestFoo (s/schema-name TestFoo))))