This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-09-20
Channels
- # bangalore-clj (3)
- # beginners (30)
- # boot (117)
- # braid-chat (6)
- # carry (9)
- # cider (6)
- # clara (11)
- # cljs-dev (28)
- # cljsrn (12)
- # clojars (2)
- # clojure (114)
- # clojure-austin (2)
- # clojure-dev (1)
- # clojure-dusseldorf (1)
- # clojure-greece (47)
- # clojure-italy (5)
- # clojure-russia (79)
- # clojure-spec (121)
- # clojure-uk (133)
- # clojurescript (92)
- # community-development (67)
- # copenhagen-clojurians (1)
- # core-async (25)
- # cursive (67)
- # datascript (1)
- # datomic (34)
- # devcards (24)
- # emacs (8)
- # funcool (71)
- # juxt (1)
- # keechma (2)
- # lein-figwheel (6)
- # luminus (8)
- # mount (17)
- # om (135)
- # om-next (13)
- # onyx (147)
- # pedestal (11)
- # planck (7)
- # re-frame (42)
- # reagent (86)
- # rum (11)
- # specter (6)
- # testing (6)
- # untangled (1)
- # vim (6)
- # yada (24)
This is fun. Using a pull-query to generate specs means I can generate test data for UI views based on the pull query they use. https://gist.github.com/olivergeorge/da6487fc60c67b79ba082bc807072e43 This still requires table and field specs based on something like this: https://gist.github.com/olivergeorge/468464ce82b8da486736fe725a4b6ff8
Is there a JIRA issue open for spec coercions (in the same way that schema does them)? I’m aware of conform, but that isn’t quite the same
there is no plan to add that
Rich sees them as separate concerns
I'm presuming I'm doing something wrong and that there is a function in core that will do this, but this bit of code is working for me to integrate spec tests into my normal clojure.test flow
(defn passes? [sym]
(-> sym
stest/check
first
:clojure.spec.test.check/ret
:result))
(t/deftest conformer-testing
(t/testing "Checking the conformer"
(t/is (true? (passes? `sut/my-specced-func)))))
is there something that does the same as passes?
and is true?
really the right way of putting this into t/is ? Success and failure both return truthy values which is why I'm using it atm
@otfrom Here's what @bahulneel and I came up with: https://gist.github.com/jmglov/30571ede32d34208d77bebe51bb64f29
When I try to gen/sample
some deeply nested spec, I get a "Couldn't satisfy such-that predicate after 100 tries." exception with no mention of the spec or predicate in question. Is there a good way to figure out which spec caused this?
So far, my only hope is a binary search by running gen/sample
on the sub-specs until I find the offender. 😞
@jmglov I had this problem also. I had to eyeball my specs and try exercising individual ones. In the end, it's usually ones that use and
and limit the valid input to some "shape". To fix these I use with gen, an example I stole is here: https://github.com/nwjsmith/datomic-spec/blob/master/src/datomic_spec.clj#L6
@bahulneel Yes, that was exactly it. My offending one turned out to be (s/and map? empty?)
. I changed it to #{{}}
. 🙂
@jmglov it's worth noting that, in that case (s/and empty? map?)
will also work fine
the only issue is that the explanation would fail empty? before map? so the message may not be as useful
user> (clojure.spec/explain (clojure.spec/and empty? map?) [1])
val: [1] fails predicate: empty?
nil
user> (clojure.spec/explain (clojure.spec/and empty? map?) [])
val: [] fails predicate: map?
nil
user> (clojure.spec/explain (clojure.spec/and empty? map?) {1 2})
val: {1 2} fails predicate: empty?
nil
user>
But it's better than the alternative:
user> (clojure.spec/explain #{{}} {1 2})
val: {1 2} fails predicate: :clojure.spec/unknown
nil
user> (clojure.spec/explain #{{}} [])
val: [] fails predicate: :clojure.spec/unknown
nil
user>
This works better:
user> (clojure.spec/def ::empty-map #{{}})
:user/empty-map
user> (clojure.spec/explain ::empty-map {1 2})
val: {1 2} fails spec: :user/empty-map predicate: #{{}}
nil
user>
@mpenet would we easy with records, https://groups.google.com/forum/m/#!topic/clojure-dev/9EjTvxPPaGQ
@bahulneel Nice!
ok, not there yet. It would be easy to cook up a working prototype of the conversion from existing spikes, but still gaps to make it robust and extendable. Good thing that, there are smarter people here :)
Hmm... there was one version with s/form & core.match, can't recall what was the problem with it. Maybe just not finished.
actually, it could be extended too, with multimethod on the spec symbol. Ping @miikka.
I have something along these lines already, even tho the multimethod is kinda useless ultimately, it can be closed
@jmglov: regarding protocol implementations - I read (on the mailing list, I think), that directly spec’ing protocol fns wasn’t supported, and the recommendation was to make the protocol fns “private”, provide wrapper fns, and spec those. This worked fine for me, especially because I had some common logic that fit nicely in the wrapper fns.
@bhagany I'm not trying the spec the protocol functions themselves, but rather an argument to a function that should implement a certain protocol. i.e.
(defprotocol Database
(read-thing [this id])
(store-thing [this thing]))
...
(defn do-stuff [db updates]
(let [thing (database/read-thing (:id updates))]
(-> thing
(merge updates)
database/store-thing)))
I want to spec do-stuff
something like this:
(s/fdef do-stuff
:args (s/cat :db #(implements? Database %)
:updates (s/keys :req [::thing/id]
:opt [::thing/name]))
:ret any?)
Of course implements?
is not a real function, but this illustrates what I'm trying to do, I hope.I’m trying to run “free” generative tests in cljs, using doo, but it seems as though the registry is not being populated in my test build. I’m importing a namespace that defines some named specs, and I can run the functions in that namespace, but clojure.spec.test/check
and checkable-syms
are both empty. However, in my figwheel repl, the registry is populated as I would expect. Any ideas what I might be missing?
I should also add that clojure.spec.test/enumerate-namespace
does see the fns that are spec’d in my test build
and also that I can (s/valid? ::some/data-spec data)
in my test build. It seems like only fn specs are missing.
Possibly stupid question: why is s/fdef
not called s/defn
?
because that’s not what Rich named it
good enough for me 🙂 all hail Master Rich 🙂 (I was asking in case there was some subtlety I’m missing)
having a hard time wrapping my head around writing generators, how would i write a generator for a set of distinct strings?
you may need to do some voodoo connect that to spec, I haven't looked too much at how test.check and spec are tied together
if you run more generative test iterations, the size of the strings will get larger, but it would likely be easier to create a special purpose larger string generator
there's sooooo much stuff in the clojure.test.check.generators namespace, trying to internalise all that is the one thing I'm not looking forward to when we start using spec
in many cases it’s easier to just gen from a spec
(s/gen (s/coll-of string? :kind set?))
for some specs i get {:message "Couldn't satisfy such-that predicate after 100 tries.", :data {}}
if you are and’ing specs with fairly restrictive filters, you’re likely to run into this
this is discussed (plus making custom generators) in http://clojure.org/guides/spec if you haven’t seen that yet
yeah i’ve read that over, seems the solution is to write your own generators and then use with-gen
which works great
i’m already re-using a good amount of generators for things like certain numbers and strings with various properties
yes, I think that’s worth improving
I don’t think anyone has actually filed a ticket about it, but I would be in favor of improving that
it is, but I suspect it could be wrapped somehow in spec
haven’t looked at it
I'm having a problem with generators.
(def col-gen
(gen/fmap #(->> % (apply str) str/upper-case keyword)
(gen/vector gen/char-alpha)))
(s/def :cell/col (s/and simple-keyword? (comp (set util/alphabet) name)))
This throws an error `java.lang.AssertionError
Assert failed: Arg to vector must be a generator (generator?
generator)`
How can I generate a vector of characters from the alphabet?the things in clojure.spec.gen aren't actually generators, they are 0 argument functions that you invoke to get a generator
or of course just (s/gen #{:a :b :c})
people do not leverage gens from specs enough imo
that should be your first choice
then stuff in spec.gen, then stuff in test.check
or test.chuck etc
just (s/gen number?)
@alexmiller about that case, is it normal for
(clojure.test.check.generators/sample (cljs.spec/gen #{true false}))
to always be
(true true true true true true true true true true)
?well any set with falsey elements is going to be a bad time
that’s kind of a special case - it will return falsey elements which is the signal that the value doesn’t match the spec
in this case (s/gen boolean?)
will be better
Oh, I get it !
and if you want to support nil,true,false then (s/gen (s/nilable boolean?))
Thank you !
I had some bad time with (s/gen boolean?)
in ClojureScript, throwing an error. I figured it out with with-gen and (gen/elements #{true false})
. I don't know if it's a known bug or not btw…
sounds like a bug
you might ask in #clojurescript to see what @dnolen thinks
@ggaillard it should work, make sure you are on the latest 1.9.229, if it still doesn’t work, file an issue in JIRA
You are quick! I didn't had time to post in #clojurescript 🙂 Thank you, I'll check that.
I was using 1.9.216, 1.9.229 fixed it. Sorry for that 😕