Fork me on GitHub
#clojure-spec
<
2017-10-10
>
erichmond01:10:24

Is there a good doc/faq/something on using spec's ability to generate mock data? Generating a string like "001212314" in particular? (meaning, n length, chars consisting of just 0-9?

Alex Miller (Clojure team)02:10:38

spec itself does not really have something like that built in

Alex Miller (Clojure team)02:10:59

you can use fmap to build a set of characters, then apply str to them

Alex Miller (Clojure team)02:10:10

or use the regex gen support in the test.chuck library

erichmond03:10:48

I had been using test.chuck, but was curious if there was a more spec-native solution. thanks, I'll keep going with what I have!

jwkoelewijn13:10:54

question, when i run (clojure.spec.test/check 'merge-users {:clojure.spec.test.check/opts {:num-tests 15 :max-size 2}}) it seems the num-tests option seems to be ignored. Am I passing the options for quick-check wrong? or is this to be expected and should I address my own expectations? 🙂

jwkoelewijn13:10:18

where I use the backtick instead of the quote (to help in markdown slack formatting)

taylor14:10:49

(stest/check `foo {:clojure.spec.test.check/opts {:num-tests 1}})
this works for me

borkdude15:10:26

In Clojure spec, how could I generate symbols or strings of max length 2 that contain only lowercased alphabetic characters? This works, but maybe it can be simplified?

(s/def ::varname
  (s/with-gen symbol?
    #(gen/fmap (fn [[i j l]]
                 (symbol
                  (str/lower-case (.substring
                                   (str i j)
                                   0 l))))
               (gen/tuple (gen/char-alpha)
                          (gen/char-alpha)
                          (gen/choose 1 2)))))

gfredericks15:10:06

min length 1?

gfredericks15:10:55

I don't think that's terrible

nwjsmith16:10:52

(s/def ::varname
  (s/with-gen symbol?
              #(gen/fmap (fn [characters]
                           (symbol
                             (string/lower-case
                               (apply str characters))))
                         (gen/vector (gen/char-alphanumeric)
                                     1
                                     2))))

jonas17:10:00

With spec/keys (or spec in general), what’s the best way to express: One and only one of the keys ::foo or ::bar is required?

jonas17:10:25

Or alternatively “Either ::foo or ::bar or both is required”

bfabry17:10:26

(s/keys :req [(or ::foo ::bar)]) and then and'd with a spec to enforce only one if you want that

jonas17:10:09

Thanks, let me try that

bfabry17:10:12

The :req key vector supports 'and' and 'or' for key groups:
(s/keys :req [::x ::y (or ::secret (and ::user ::pwd))] :opt [::z])

jonas17:10:47

I think this is exactly what I was looking for 👍

nwjsmith20:10:44

Crazy, I was just reading your blog post. Great stuff!

nwjsmith23:10:53

How are you folks debugging Couldn't satisfy such-that predicate errors?