Fork me on GitHub
#clojure-spec
<
2018-01-10
>
mpenet08:01:07

Would it be considered ok to promote s/assert* to good for "public" use (atm the docstring says : "Do not call this directly, use 'assert'.")

mpenet08:01:27

in cases where you want the check to always happen no matter the value of check-assert/compile-asserts

mpenet08:01:55

or is there a better way? ping @alexmiller

mpenet08:01:36

I guess if it would be used that way it could warrant a rename maybe

Andreas Liljeqvist10:01:52

I seem to remember that I could generate with a provided example. Something like (gen/fill-in ::spec {::id "all generated will have this id, other values are generated"})

Andreas Liljeqvist10:01:12

Can't remember what it was called though... anyone?

misha11:01:07

@andreas862

(s/def ::id string?)
(s/def ::foo (s/keys :req [::id]))
(s/exercise ::foo 2 {::id #(s/gen #{"a" "b"})})
;=>
;([{::id "a"} {::id "a"}]
; [{::id "b"} {::id "b"}])
?

misha16:01:12

(defn gen
  "... Optionally an overrides map can be provided which
  should map spec names or paths (vectors of keywords) to no-arg
  generator-creating fns. These will be used instead of the generators at those
  names/paths. Note that parent generator (in the spec or overrides
  map) will supersede those of any subtrees. 
parent generator will supersede those of any subtrees part just bit me in the ass harold

misha16:01:51

but it forces me to keep generators and specs separate, which ends up to be cleaner

pablore21:01:39

I have some function

(defn foo
  [a]
  {:pre [(s/valid? ::bar a)]}
...)
That asserts if the input conforms to a spec. It works well, but I’m having trouble to debug when it fails, I want to know what input I’m passing that doesnt conform and why.

pablore21:01:27

I just get an AssertionError

misha22:01:27

@pablore I use s/explain-data instead of s/valid. it returns nil if everything is ok, and a datastructure, if there are any errors.

misha22:01:23

So you can use it in if or when.

misha22:01:23

read through this too, may be it'll give you some ideas https://clojure.org/guides/spec#_using_spec_for_validation afaik, specs are replacement of :pre/`:post`-conditions

pablore22:01:29

s/explain works better than I expected! I know of s/fdef but I’m writing a really complex reducing function with lot of if-cases.

misha22:01:01

(s/check-asserts true)
(s/assert string? :a)
;; clojure.lang.ExceptionInfo: Spec assertion failed
;;    val: :a fails predicate: :clojure.spec.alpha/unknown
note string? and predicate: :clojure.spec.alpha/unknown