Fork me on GitHub
#clojure-spec
<
2016-12-24
>
sophiago00:12:56

i still can't figure out how to specify a list of possible s/defs to pass to :args and :ret πŸ˜• i keep getting a "No value supplied for key" error

seancorfield02:12:27

@hlship see http://dev.clojure.org/jira/browse/CLJ-1941 -- known places where instrument doesn't work (includes primitive returning fn and protocols)

sophiago02:12:50

ah, that says only primitives are affected tho?

seancorfield02:12:02

Protocols are mentioned in the comments.

seancorfield02:12:30

Can you share the problematic code re: :args :ret?

sophiago02:12:19

i'm just trying to pass a list of specs to each:

sophiago02:12:51

can't imagine this is the correct way to do it?

seancorfield02:12:55

remove the [a b] s-exp

seancorfield02:12:01

Then it should work.

seancorfield02:12:40

As for ::sym -- it can't be all those things at once -- shouldn't that be s/or?

seancorfield02:12:58

and int? to make it a predicate

sophiago02:12:26

oh yeah. i was playing around with several expressions trying to get it to work

sophiago02:12:03

wow. i can't believe i even had the guide open and misread speccing functions as taking an argument list

sophiago02:12:26

that means it's time to go to sleep soon πŸ˜›

seancorfield02:12:34

Heh, I've read that thing over and over and I still write nonsense for specs at times πŸ™‚

sophiago02:12:03

so i should just be able to call (s/exercise add)`?

sophiago02:12:22

oh that doesn't place nicely with slack

sophiago02:12:51

(s/exercise `add)

seancorfield02:12:14

If your specs are generatable... which yours won't be since you can't generate from instance? I believe...

sophiago02:12:45

s/exercise-fn right?

seancorfield02:12:46

Hmm, actually I'd have to try s/exercise to check whether it only uses the :args spec

sophiago02:12:02

for functions defined in protocols, but dispatched on defrecords not primitives so hopefully it should work

seancorfield02:12:16

Ah, yes, it must generate data to even pass stuff into :args...

sophiago02:12:46

ah ok. so would i use s/gen for the list of types?

seancorfield02:12:59

You'll need to write generators for your rational and complex types I suspect...

seancorfield02:12:14

What happens if you try (s/exercise ::rational)?

sophiago02:12:32

i get the same error: "Unable to construct gen..."

seancorfield02:12:10

Right... you need something like this:

(defrecord Foo [a])
(require '[clojure.spec :as s] '[clojure.spec.gen :as gen])
(s/def ::foo (s/with-gen #(instance? Foo %)
               (fn [] (gen/fmap ->Foo (s/gen int?)))))
(s/exercise ::foo)

seancorfield02:12:18

Basically you need to specify a generator that maps your record constructor over a generator for the values it accepts

sophiago02:12:58

i truly did make this much more difficult by trying to go that granular

sophiago03:12:22

it wasn't something obvious in the guide i missed going over this all day

sophiago03:12:50

ok, thanks. going to give it a try

seancorfield03:12:42

Making specs generative can be quite the process... we try to do that with all of ours but sometimes it takes... ingenuity...

sophiago03:12:09

well i was trying to get it to somehow enumerate tests on the correct output types automatically...

sophiago03:12:38

this is actually much simpler than i imagined

sophiago03:12:45

but still not covered in the guide

seancorfield03:12:13

Gary Fredericks has created a good companion library for spec that helps generate all sorts of things BTW (we use it, specifically, for regex generation)

sophiago03:12:30

oh i think i saw that

sophiago03:12:41

i just glanced over the functions and don't think it would help in this case though

sophiago03:12:04

tbh this is not so complicated. protocols are generally really easy to debug

sophiago03:12:43

i actually thought since it's so systematic it would be good to learn how to spec a whole project so i could apply it to code where i really can't figure out what's going on

seancorfield03:12:42

FYI, s/exercise-fn doesn't check :ret -- only :args

seancorfield03:12:20

(defn foo [a b] (+ a b))
(s/fdef foo :args (s/cat :a int? :b int?) :ret string?)
(s/exercise-fn `foo)
doesn't complain that foo is supposed to return string? values

sophiago03:12:37

well i passed it the whole list anyway πŸ™‚

sophiago03:12:45

i'll have to check it manually

sophiago03:12:27

but it will tell me if any subtyping combinations are throwing errors, which i believe was the case before

sophiago03:12:07

huh. i'm getting the same error...

sveri10:12:43

I get this error: RuntimeException Var clojure.test.check.generators/keyword-ns is not on the classpath clojure.spec.gen/dynaload (gen.clj:21)when running this code: (gen/generate (s/gen number?))in the REPL. Any ideas what I am missing here?

gfredericks11:12:17

@sveri do you have a test.check dependency declared?

sveri11:12:34

@gfredericks No, I dont think so

gfredericks11:12:40

you need one to use generators

sveri11:12:43

So this is needed whenever I want to use generators

sveri11:12:44

ok πŸ™‚

gfredericks11:12:46

[org.clojure/test.check "0.9.0"]

sveri11:12:47

Thank you very much

sophiago18:12:00

what is clojure.spec/Specize? i'm getting the following error: IllegalArgumentException No implementation of method: :specize* of protocol: #'clojure.spec/Specize found for class: nil

donaldball18:12:36

IIRC it’s the protocol by which values are coerced into specs, notably by which keywords resolve themselves in the spec registry

sophiago18:12:32

ah, so it does have to do with trying to exercise protocol functions? because i think i may be experiencing this issue, but am having trouble isolating it to be sure: http://dev.clojure.org/jira/browse/CLJ-1941

sophiago18:12:07

to be fair, that's the not the error reported in that ticket

sophiago18:12:41

my case is complicated because the protocol functions i'd like to test all dispatch on records so theoretically should be unaffected. however, i went so granular as to define arguments to their type constructors using s/with-gen and gen/fmap. so i can't tell if some of the errors are actually what i'm looking for, known subtyping bugs, because i'm not sure if the generators that don't do that are affected and am currently getting three different error codes dependent on whether the arguments to the type constuctors are primitives or other records and also (the really odd part) the function itself, which shouldn't make a difference as far as i can tell