Fork me on GitHub
#malli
<
2021-02-15
>
aaron5103:02:37

Is there an easy way to use Malli in clojure.test/is? I’d like to assert that m/validate returns true, and show m/explain output when it fails.

aaron5104:02:34

(is (validate schema data) (humanize (explain schema data))) — anything like this built in? Or just validate! that throws on failure?

borkdude07:02:47

Write a function?

ikitommi08:02:16

there are no test helpers atm, but should be easy to do in user space as borkdude suggests, Are there good helpers / utilities for this (testing) in spec? in schema?

aaron5122:02:45

Yes, it is straightforward enough to write it in user space. But I thought it would be a common case… Found a few similar things: • schema: https://github.com/plumatic/schema/blob/master/test/clj/schema/test_macros.clj • schema: https://github.com/plumatic/schema/blob/master/test/cljx/schema/core_test.cljx • clojure-expectations has “spec expectations” https://github.com/clojure-expectations/clojure-test

borkdude22:02:42

I also have this one: https://github.com/borkdude/respeced This was written to test fdef specs.

ikitommi08:02:06

wip: function generators generate correctly function with different arities

(def f
  (mg/generate
    [:function
     [:=> :cat int?]
     [:=> [:cat int?] nat-int?]]))

(f)
;=> 132816
;=> -7823115
;=> -36
;=> -97
;=> 13412759
;=> 1444

(f 1)
;=> 1038018
;=> 11009747
;=> 8
;=> 59186626
;=> 10
;=> 5373734

(f "1")
; =throws=> :malli.generator/invalid-input {:schema [:cat int?], :args ["1"]}

(f 1 2)
; =throws=> :malli.generator/invalid-arity {:arity 2, :arities #{0 1}, :args (1 2), :schema [:function [:=> :cat int?] [:=> [:cat int?] nat-int?]]}

borkdude08:02:26

is this in a branch somewhere?

ikitommi08:02:52

not yet, needs a new -min-count protocol method to regex-schma to resolve the arities from :=> schemas. few hours away from a branch.

borkdude08:02:46

cool. how are you generating the fn per arity?

ikitommi08:02:34

but that code doesn’t infer the arity, needs to set manually:

(def f
  (mg/generate
    [:function
     [0 [:=> :cat int?]]
     [1 [:=> [:cat int?] nat-int?]]]))