Can I use clojure.spec.gen.alpha
's together with Ghostwheel? In my particular case I cannot figure out how to create a spec that would generate tests for only two possible strings, for example something like this:
(>defn my-fun
[either-one]
[(gen/elements ["good" "bad"])
=> string?]
(str "this is " either-one "code"))
Expected generated input would either string "good" or "bad" for every case. How to do this? Please, help 🙏I tried many approaches, including | (s/or #(= % "good") #(= % "bad"))
, (s/alt :g "good" :b "bad")
, nothing worked, generator cannot generate tests with such specs
Though this works with clojure.spec.gen
, at least in repl
@armikhalev This doesn't work, you're passing a generator where you need a spec. You want to define the generator with the spec:
(s/def ::some-spec (s/with-gen <spec> <generator-fn>)
(>defn my-fun
[either-one]
[::some-spec => string?]
...)
Although in this particular case it would be easier to simply let the standard generators handle it:
(>defn my-fun
[either-one]
[#{"good" "bad"} => string?]
...)
I recommend reading through the whole Spec guide to clear this stuff up:
https://clojure.org/guides/spec
It's not too long and will answer all of these questions.P.S. If you want to see what Ghostwheel does exactly in a particular situation and how it desugars to standard spec, you can just do (macroexpand-1 '(>defn ....))
in the REPL.
Thank you, #{"good" "bad"} was exactly what I needed! Didn't realize that I can use this as a spec, I'm still too new to Clojure and Spec.