This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-01-24
Channels
- # beginners (19)
- # boot (118)
- # capetown (4)
- # cider (37)
- # cljs-dev (69)
- # cljsjs (23)
- # clojure (212)
- # clojure-austin (10)
- # clojure-india (3)
- # clojure-italy (2)
- # clojure-mke (1)
- # clojure-nl (1)
- # clojure-russia (5)
- # clojure-spec (52)
- # clojure-uk (86)
- # clojurescript (31)
- # core-async (9)
- # cursive (123)
- # datomic (91)
- # emacs (22)
- # events (3)
- # hoplon (68)
- # klipse (4)
- # lambdaisland (10)
- # leiningen (2)
- # off-topic (14)
- # om (14)
- # onyx (44)
- # perun (14)
- # proton (20)
- # re-frame (15)
- # reagent (10)
- # ring-swagger (9)
- # specter (18)
- # untangled (3)
- # vim (26)
- # yada (4)
Hi, I’m pretty new to Clojure and I am playing around with Spec. I’ve got following error message
[{:type java.io.FileNotFoundException,
:message "Could not locate clojure/test/check/generators__init.class or clojure/test/check/generators.clj on classpath.",
while evaluating
(ns foo-test
(:require [clojure.spec :as s]
[clojure.spec.test :as stest]
[clojure.spec.gen]
[clojure.pprint :as pprint]
[clojure.test :as t]))
;; Sample function and a function spec
(defn fooo [i] (- i 20))
(s/fdef fooo
:args (s/cat :i integer?)
:ret integer?
:fn #(> (:ret %) (-> % :args :i)))
(stest/check `fooo)
What’s going on here? ^^generators use the test.check library. this is optional so it’s not a required compile-time dependency
but if you want to use it at test time, you’ll need to include test.check as a test dependency
[org.clojure/test.check “0.9.0”]
you’d want that in the :dev profile for it to be test-scoped only (not a dependency that downstream consumers would get)
okay I c, sweet now it works, thank you for explaining it to me so clearly @alexmiller - really appreciated 🙂
np - this is in the spec guide too - https://clojure.org/guides/spec
if you haven’t seen that yet
Hi, I am having trouble writing a spec that has the following constraints:
{:id 123
:foo [{:id 456 :parent-id 123}
{:id 789 :parent-id 123}]}
See that :id
and :parent-id
are required to have the same value.Aha, a simple predicate s/and
ed with s/keys
would do nicely 🦆
(s/and (s/keys :req-un [::id ::foo])
(fn [{:keys [id foo]}]
(every? #{id} (map :parent-id foo))))
I'm specced a function which performs a deep-merge via addition on some maps with longs at the leafs, and I wanted to create a generative test for it
however my generative test actually fails because I run into integer overflow, which is fair enough, that's good to know in theory
but in practice none of the values I use are ever going to get that large as to cause an overflow
so in this case, should I be handling the bounding of these values via instrument
by using :gen
overrides?
or should I handle it at the site of the spec definition by adding some arbitrary bound as a predicate to go with pos-int?
I tried working around this issue by using stest/instrument
and overriding the gen that is causing the overflow via :gen
, and then running stest/check
on the function again but this did not seem to have any effect
Maybe you want something like a ::smallish-long
that describes a domain consistent with your expected values in production?
@donaldball yeah I tried that approach as well, but it has the following problem: the function I'm testing is essentially taking a collection of values of ::map-with-smallish-int
and summing all of the ::smallish-int
together, so I specced the function to use the same return value
and this of course fails because the largest values of what ::smallish-int
generates summed together are going to be larger than the bound I'm using
so of course I could use use a new spec for the return value of the function under test, which uses a higher bound
Annoying, albeit correct 😛
You… could merge with +’
yeah that's another option but then I'm changing the definition of my function to use a slower function just to appease my tests
but I'm guessing that I don't understand how instrument
with :gen
overrides is supposed to be used
it would be nice if during tests I could just override the generators of my specs to match to the domain values I'm expecting during normal use, and then I write the test to perform inside this context
using +'
doesn't really help either as once the values get high enough to coerce to BigInt they will fail nat-int?
@luxbock the approach of using instrument with a :gen override is what I would recommend and should work
if you can a repro, would be interested
Is there any chance that you guys could make the clojure.spec guide (PDF) printable? 🙂 https://clojure.org/guides/spec
@alexmiller this is what I'm doing, is this supposed to work? https://gist.github.com/luxbock/ecb263750f61cc6fab7a736e5bd96008
Sounds just great @qqq, because printable pages would make http://clojure.org so much more useful (for users that want to read the guides etc. on ereaders etc.)
I don't know how to fix this, sorry -- it seems like the CSS I delete to make it printable also deletes the nice CSS formatting that makes it pretty and has nice code layout.
https://github.com/clojure/clojure-site/blob/master/content/guides/spec.adoc <-- is printable for me, think this is probably the best for now
there is an issue for adding print support at https://github.com/clojure/clojure-site/issues/99
Making that work is probably not something I’m going to work on, but if someone else wanted to help, that would be cool