Do you all unit test specs?
Never unit tested specs. But I'm using it as part of the test. I'd test it if I was going to release it as a library.
for complex specs that i use to validate data, would like to know if the spec is right
That’s… interesting! Because I think I already think of specs as, more or less, akin to tests themselves.
I do of course sometimes find that a spec I’ve written turns out to not quite capture what I had in mind, or a requirement, etc… but I’ve done the same with “traditional unit tests” (i.e. example tests) …
can I express "this key is optional, but if is in map - that key has to be there too, " as s/keys out of the box? with some combination of :opt :req and or
like
(s/keys :req [(or :a/foo :a/bar)])
or something?
Or should I s/and
it?:
(s/and
(s/keys :opt [:a/foo :a/bar])
(fn [m]
(let [present (partial contains? m)
missing (complement present)
ks [:a/foo :a/bar]]
(or
(every? present ks)
(every? missing ks)))))
s/and'ing a predicate is prob best
is it s/and
in spec2 for the same use case?
thank you
yeah, I would do the same in spec 2
or rather I would probably make 2 different s/selects for the same s/schema
s/keys is probably going away in spec 2
I use spec to help me to parse some data but I'm testing the function that does that. The fact I'm using a spec is just a implementation detail. At least, that was most of my use cases.
Ah, I see, yeah. That makes sense.
In that case, yes, I would probably write some example tests.
@alexmiller Over what time-frame are you anticipating spec 2 to come out?
I am assuming it will be spec.alpha2 right?
@dev.4openid I'm not Alex but my understanding is that Spec 2 will eventually become just clojure.spec
when it is ready to come out of alpha -- and there's no timeframe for it yet, based on what I've seen/heard, since a lot of things are still being designed/revised.
The repo is here https://github.com/clojure/spec-alpha2
For a while, I kept a branch of our codebase current against that repo but it involved quite a few changes (and it had to keep changing as Spec 2 changed). I stopped tracking it back in ... September/October I think? Our approach going forward -- once Spec comes out of alpha -- will be to use the new Spec for new code we write and slowly, over time, migrate our Spec 1 code to "Spec 2" as needed. Spec 1 will stay available as-is "forever" so folks can stay on the old version if they don't want to migrate.
My approach to developing complex Specs is to have a (comment ,,,)
form under the Specs with expressions in it that I use to test validation, conformance, and generation.
I also make sure to "test" each individual part of a Spec (so I can catch generation problems early -- debugging generation problems in a large Spec is no fun!).
Thx
I’m deep in something else right now, but hoping to pop the stack back to that soon. Not sure, prob mostly gated in rework of function specs which rich has been hammocking on
I’m hoping it will just be clojure.spec
how can I create a generator from an arbitrary function?
here’s a silly example but It would be something like (s/def ::name (s/spec string? :gen (constantly "Margaret")))
not sure if possible but id like to combine it with a lib like https://github.com/paraseba/faker to generate test data
s/with-gen
whats the difference between those two?
(s/def ::name (s/with-gen string? #(gen/return "Margaret")))
(s/def ::name (s/spec string? :gen #(gen/return "Margaret")))
effectively, not much
any one is preferred?
not really. I usually do the first one.
thanks for the info 🙂