Fork me on GitHub
#clojure-spec
<
2016-08-31
>
ag00:08:32

errr… so I have spec like this:

#?(:clj (defn uuid-str-gen [] (gen/return (str (java.util.UUID/randomUUID))))
   :cljs (defn uuid-str-gen [] (gen/return (str (random-uuid)))))

(s/def ::uuid (s/with-gen (s/and
                            string?
                            #(re-matches uuid-regex %)
                            uuid-str?)
                uuid-str-gen))
for uuid vals. A bit bulky, I couldn’t find anything better. my problem now, if another spec includes it it always generates same uuids. How do I fix that?

ag00:08:25

(gen/sample (s/gen ::uuid))
("06cd3a07-98e0-4ee2-bce8-fa0b9e96d9e2" "06cd3a07-98e0-4ee2-bce8-fa0b9e96d9e2" "06cd3a07-98e0-4ee2-bce8-fa0b9e96d9e2" "06cd3a07-98e0-4ee2-bce8-fa0b9e96d9e2" "06cd3a07-98e0-4ee2-bce8-fa0b9e96d9e2" "06cd3a07-98e0-4ee2-bce8-fa0b9e96d9e2" "06cd3a07-98e0-4ee2-bce8-fa0b9e96d9e2" "06cd3a07-98e0-4ee2-bce8-fa0b9e96d9e2" "06cd3a07-98e0-4ee2-bce8-fa0b9e96d9e2" "06cd3a07-98e0-4ee2-bce8-fa0b9e96d9e2”)
`

ag00:08:32

oopsie… there’s gen/uuid I guess I should be using that

ag00:08:53

I need only the sting though

ag00:08:21

so why this is not working:

(s/exercise (s/with-gen string? (gen/fmap str (gen/uuid))))

ag00:08:00

nvmd… got it

(s/exercise (s/with-gen string? #(gen/fmap str (gen/uuid))))

borkdude12:08:50

Was there s/instrument-all, which got removed later on?

minimal12:08:54

@borkdude just call instrument with no args

minimal12:08:56

it got changed at one point

sveri12:08:56

Anybody experimented with using spec for form / input validation?

sveri12:08:14

@martinklepsch thanks, so basically its: 1. validate input with spec 2. try to parse result and display some useful text

martinklepsch12:08:31

right. Do all of that on a form level and do some basic dirty? tracking

sveri12:08:00

Ok, thank you very much, I will try some stuff 🙂

Alex Miller (Clojure team)12:08:55

Rich checked in a round of perf improvements to master yesterday which fixed some hot spots

mschmele14:08:52

When I try to run check on a function that I’ve spec’d with fdef, it fails with the cause “Unable to construct gen at: [] for : clojure.spec$…"

mschmele14:08:57

am I missing something obvious?

mschmele14:08:39

Code is up there for anybody who wants to read through it

minimal14:08:42

@mschmele you should (generally) use the predicates on their own instead of wrapping in an anonymous function. Otherwise you won.t get generators for the functions that have them.

minimal14:08:14

e.g. instead of (s/def ::id #(string? %)) use (s/def ::id string?)

mschmele14:08:31

Okay, I just saw an example that was formatted that way and it seemed to work. Changing that now

minimal14:08:48

also you should just use the specs inside fdef, no need for assert or valid which will probably give strange results

mschmele14:08:34

so something like

minimal14:08:46

and you need to use s/cat for arg lists

mschmele14:08:49

:args [::accounts ::id]

minimal14:08:53

more like :args (s/cat :acc ::accounts :id ::id)

mschmele14:08:08

ah, thank you

ag21:08:26

is there a concise way to get a keyword? s/def but the one that always generates short keywords? because this looks awful;

(s/def ::keyword (s/with-gen keyword? (fn [] (gen/fmap #(keyword %)
                                               (gen/such-that #(and (> 10 (count %))
                                                                 (< 0 (count %)))
                                                 (gen/string-alphanumeric) 1000)))))

sattvik22:08:14

Could you do as part of the spec itself?

sattvik22:08:51

Maybe (s/and keyword? #(< (count (name %)) 10)?

bfabry22:08:34

@ag

(s/def ::short-keyword (s/with-gen keyword? (fn [] (gen/fmap #(keyword %)
                                               (gen/such-that #(and (> 10 (count %))
                                                                 (< 0 (count %)))
                                                 (gen/string-alphanumeric) 1000)))))

(s/def ::keyword ::short-keyword)
(s/def ::keyword2 ::short-keyword)

bfabry22:08:37

@sattvik doing it that way makes the spec validate that the keyword is short, which you may not want, and also makes for slow generators

sattvik22:08:04

Well, if the idea is to accept any keyword but only generate short ones during testing, then supplying the generator at check time is probably what you want. test.check has things like c.t.c.generators/resize that can fix the generator to a manageable size, e.g. 10.

bfabry22:08:32

this is true

sattvik22:08:35

Unfortunately, that’s not one of the functions clojure.spec mirrors.