Fork me on GitHub
#clojure-spec
<
2017-09-21
>
richiardiandrea01:09:40

Hello! I have probably asked this already at some point in the past, but is there a way to create a Generator that basically uses a function of mine? I am trying with (tcgen/generate (tcgen/->Generator #(faker/name.findName))) but no luck, it always returns nil

gfredericks01:09:42

@richiardiandrea what sort of thing are you trying to generate?

richiardiandrea01:09:51

fake data generated from a function...it looks like this works:

(def fake-generators
  {:order/customer #(tcgen/return (faker/name.findName))})

richiardiandrea01:09:09

(gen/generate (s/gen :order/customer fake-generators))

richiardiandrea01:09:06

I was basically thinking of it in terms of repeatedly

gfredericks01:09:20

if you're doing property-based testing at all, it's generally better to build things from the combinators

gfredericks01:09:27

what does faker/name.findName do?

richiardiandrea01:09:57

well, I am not using it for property testing with this specific case

richiardiandrea01:09:32

I am bending it a bit, so that I can generate intelligible data structures

gfredericks01:09:49

test.check can generate intelligible data structures 🙂

richiardiandrea01:09:52

so now I get a very nice:

{:name "saladman",
 :key "ia",
 :timestamp #inst "1970-01-01T00:00:00.942-00:00",
 :aggregate "order",
 :data {:customer "Velma Doyle"}}

gfredericks01:09:36

is this because you want to use the generators for friendly examples?

gfredericks01:09:50

I almost feel like that should be a different piece of functionality all together

gfredericks01:09:35

maybe not; I guess it's just the strings that were bothering you?

richiardiandrea01:09:10

yes I completely agree with you, it could be a separate piece functionality, but generators by definition can be a good fit for that as well

richiardiandrea01:09:20

if I have a restricted set of things I can use tcgen/elements and that's nice, but for names...something like the above works well

gfredericks11:09:28

Now I'm thinking this belongs in spec, if anywhere. A function called s/example which will use custom code if registered and fall back on the generator otherwise

bbrinck18:09:21

If often find myself redefining fdefs a few times to get them right and AFAICT, you need to call instrument after each redefinition. Has anyone else run into this? I suppose I could define a new macro named fdef! that did both steps and then just remove the ! before I commited the code?

bbrinck18:09:10

some sort of auto-instrumentation mode that worked as I redefined things would be handy. Has anyone written something like this?

danielcompton20:09:47

@bbrinck I have instrument called in my user namespace that is reloaded after all of the other dependencies are loaded

danielcompton20:09:22

So whenever I reload the code, (with clojure.tools.namespace) there is an instrument call at the end

danielcompton20:09:39

I still haven't figured out what's the best way to do this for testing though

bbrinck20:09:53

Ah, you’re right clojure.tools.namespace would be useful here. Thanks!

danielcompton21:09:54

I've got a lazy way of doing it, which is just to put the instrument call directly in the user ns, but a fancier way to do it would be to attach it to the c.t.n.r/refresh :after handler