Fork me on GitHub
#clojure-spec
<
2017-03-17
>
hkjels14:03:27

I’d like to simplify this API, but I’m not sure how to create both a “variable” and a function-reference from the same parameter https://gist.github.com/hkjels/1656508f7666ce6fa39699522743505f

hkjels14:03:38

Any ideas?

hkjels14:03:00

I might have to use a macro I guess

Alex Miller (Clojure team)14:03:26

if you start from the symbol, it’s easy enough to get the meta from there

bronsa14:03:59

might not be that easy from cljs

Alex Miller (Clojure team)14:03:07

ah, true, didn’t notice that

hkjels14:03:26

yeah, unfortunately that doesn’t work

waffletower18:03:15

Just starting with custom generators, and I have only looked at the underlying code in clojure.test.check.generators a bit, and didn’t notice if there was a more straight-forward way to use custom generators without input arguments. clojure.test.check.generator’s own uuid is an interesting example, but it relies on an internally passed random number (while ignoring size) and defines itself with namespace internal functions.

(s/def ::saddle (s/with-gen
                (s/and string? (partial re-matches saddle.regex))
                #(gen/fmap (fn [_] (zero-arity-gen)) (gen/return nil))))

gfredericks19:03:39

@waffletower this is generally discouraged since it sidesteps most of the test.check orchestration -- it should be possible for you to write any generator you need using the built in combinators

gfredericks19:03:05

Is your specific problem about generating a string that matches a regex?

waffletower20:03:09

the generator doesn’t need shrinking as with gen/uuid, and it currently uses RNG internally. I could use RNG from the generator namespace instead — which seems to suggest I could continue using gen/fmap

gfredericks20:03:10

you can opt-out of shrinking by wrapping with gen/no-shrink

gfredericks20:03:30

using the randomness from test.check means that at least your generator (and any composition including it) is deterministic

gfredericks20:03:51

@waffletower if you'd like to share the details of the generator you already have I could suggest how to rewrite it