Do we have a repo with a collection of arbitrary specs for all sorts different cases? I often need to create a spec for something so trivial but end up spending more time than intended, only to remember later that I did it already in some other Clojure project. Sometimes I just need to perform Github search and find some gems. Would be nice to have a community driven repo of good examples.
Hmm, I see Specs as being tied to domains rather than being that sort of reusable -- what sort of things do you have in mind @ag?
things like various datetime, timestamps, comma separated lists of currencies or/and US states and US-state abbrevs, etc.
You right though, maybe not a collection, but perhaps something like a cookbook
You're talking about strings? Parsing strings? That's not a good use for Spec tho'...
Specs for datetime/timestamps -- inst?
, s/inst-in
.
Locales, country codes, currency codes -- all easily available from Java classes but I guess ready made Specs for sets of those things would save you writing a single line of Clojure?
US states -- that's domain-specific: do you just want the 50 states, plus DC? Plus the various US territories/islands? But stuff like that belongs in databases and you can construct a set spec from that.
hi, I am wondering if it is possible to instrument the return value of a function?
I have not used it before, but the orchestra library aims to do this: https://github.com/jeaye/orchestra. It is not included in spec because it is instead recommended to only do checking of return values during testing, e.g. property-based testing using randomly generated arguments.
I like to use post-assertions combined with spec. They look like this.
(defn foo
[x y]
{:post [(s/valid? ::my-spec %)]}
(do-something x y))
You can also validate your arguments similarly using a pre-assertion.
(defn foo
[x y]
{:pre [(s/valid? ::x-spec x)
(s/valid? ::y-spec y)]
:post [(s/valid? ::my-spec %)]}
(do-something x y))