Fork me on GitHub
#clojure-spec
<
2018-09-10
>
snowell16:09:08

I have a function that takes 2 arguments, the second of which needs to conform to a spec:

(s/def ::MySpec (s/cat ::foo string? ::bar keyword? ::baz vector?))

(defn myFunc [one two])
(s/fdef myFunc :args (s/cat :one ::One :two ::MySpec))

snowell16:09:33

When running stest/check on this function, I get an ArityException because the (s/cat a (s/cat b c d)) is becoming 4 arguments

snowell16:09:51

How can I tell it that the second cat needs to be as one arg?

favila17:09:45

surround with s/spec

snowell17:09:49

OK cool...that at least gets me a different failure

borkdude18:09:17

I listened to the REPL podcast today and the interviewee (Higginbotham) said there were benefits with putting specs in their own namespace. How exactly? For fdefs I find it useful to have them close to the function as documentation?

seancorfield18:09:45

@borkdude It means your code can still be used without spec since the specs -- and those namespaces -- are optional.

seancorfield18:09:00

clojure.java.jdbc does that so it can still support back to Clojure 1.7.

borkdude18:09:09

for libraries that’s nice yes

seancorfield18:09:47

Also, most of our specs at work are data specs, not function specs -- and we find it easier to have a namespace for data specs. One place to go read them all (for a given data concern).

misha18:09:14

+1 to data specs mostly

borkdude18:09:53

do you use instrumentations with these specs at all?

borkdude19:09:00

e.g. in jdbc

seancorfield19:09:38

I instrument the java.jdbc lib for the tests within that lib -- which slows things down dramatically.

seancorfield19:09:06

At work, we tend to instrument specific functions (that have fdef specs) only within their specific tests.

seancorfield19:09:20

We use the data specs in production code (to validate/conform data).

borkdude19:09:37

I find it annoying to get the wrong keys. Whenever I encounter that I tend to put an fdef now and instrument it (only for dev)

seancorfield19:09:51

We also use the data specs in tests -- for generative testing, or just for random example-based test data.

Jon Walch23:09:18

Is there anyway to force a specific path with a call to s/gen? Here’s a trivial example:

(s/def ::value (s/or :a map? :b int?)
(s/def ::message :req-un [::value])

(gen/generate (s/gen ::message))

I want the final line to always return with the ::value codepath of :a for the purpose of generating data for a test. Is this possible?

taylor23:09:54

take a look at the 2-arity version of s/gen https://clojuredocs.org/clojure.spec.alpha/gen, it takes an overrides map

taylor23:09:34

you might be able to override ::value there by providing a different generator

Jon Walch23:09:00

perfect thanks