Skip to content

Commit

Permalink
Close #391: improve defalias error msg
Browse files Browse the repository at this point in the history
  • Loading branch information
frenchy64 committed Jul 28, 2022
1 parent 936331d commit c0ab72b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/cljc/schema/core.cljc
Expand Up @@ -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))


Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/clj/schema/test_macros.clj
Expand Up @@ -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)))
13 changes: 9 additions & 4 deletions test/cljc/schema/core_test.cljc
Expand Up @@ -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]
Expand Down Expand Up @@ -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"))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -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))))
Expand Down

0 comments on commit c0ab72b

Please sign in to comment.