This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-01-29
Channels
- # architecture (14)
- # beginners (184)
- # boot (25)
- # cider (23)
- # clara (9)
- # cljsjs (13)
- # cljsrn (5)
- # clojure (140)
- # clojure-austria (1)
- # clojure-dev (24)
- # clojure-greece (15)
- # clojure-italy (7)
- # clojure-nl (1)
- # clojure-norway (1)
- # clojure-sanfrancisco (10)
- # clojure-spec (39)
- # clojure-uk (28)
- # clojured (1)
- # clojurescript (26)
- # core-async (3)
- # cursive (13)
- # datomic (44)
- # docs (3)
- # emacs (31)
- # events (2)
- # figwheel (4)
- # fulcro (6)
- # graphql (2)
- # hoplon (5)
- # jobs (11)
- # juxt (4)
- # keechma (19)
- # leiningen (1)
- # off-topic (8)
- # om (8)
- # onyx (18)
- # parinfer (2)
- # re-frame (18)
- # reagent (24)
- # ring (4)
- # rum (2)
- # shadow-cljs (26)
- # sql (15)
- # timbre (6)
- # vim (2)
👋 Given a naive example of a function spec, how to specify a custom generator such that it satisfies :fn
spec?
(s/fdef sbtrct
:args (s/cat :a int? :b int?)
:ret int?
:fn #(= (:ret %) (- (-> % :args :a) (-> % :args :b))))
are you trying to generate a 2-value vector containing ints that can be used with apply
as the args for sbtrct
?
generators generate values, so by providing a spec for each of your input args and for your return value, you've already provided generators (because spec provides a built-in generator for the int?
predicate)
@U053032QC I want to generate a sequence of args/ret values such that they satisfy relation defined in :fn
spec
that'll generate a bunch of input values for a
and b
(using the generators provided by their specs, i.e. int?
), put them into your function, and return you a sequence of those input pairs with the calculated output
if you want to do a larger number of tests of your function, you can do generative testing using clojure.spec.test.alpha/check
that'll generate 1000 different inputs and check all the outputs for conformance with your :ret
spec and :fn
predicate
> if you want to do a larger number of tests of your function..
that’s exactly what I need, exercise-fn
doesn’t seem to be able to generate huge sequences 🙂
yeah I’m getting integer overflow
oooooh, damn
hm, what can be done about this?
no, just a lot of them
if not, you could try
(s/def ::small-int (s/int-in 0 1000000))
(s/fdef sb
:args (s/cat :a ::small-int :b ::small-int)
:ret int?
:fn #(= (:ret %) (- (-> % :args :a) (-> % :args :b))))
that's specifying a range that the ints can be in, so you won't get huge numbers generated
Should I use with-gen
if I don’t want to modify the original spec?
with-gen
is for when you need to create your own generator because the existing ones can't capture your set of values
that approach above works for me though, with s/int-in
as the spec for the inputs to your function
works for me as well, thanks!
I ended up with (s/int-in Integer/MIN_VALUE Integer/MAX_VALUE)
Does spec.test
in any way supersede test.check
? I couldn't find docs explaining the difference. There seems to be some overlap between the two.
Not that I know of. spec.test
leverages test.check
extensively and seems designed to complement test.check
by enabling you to derive many generators automatically rather than specify them by hand.
correct. test.check is useful for writing arbitrary property-based tests and creating different kinds of custom generators