Fork me on GitHub

Hi, I’m pretty new to Clojure and I am playing around with Spec. I’ve got following error message

                                                      :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.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? ^^

Alex Miller (Clojure team)04:01:38

generators use the test.check library. this is optional so it’s not a required compile-time dependency

Alex Miller (Clojure team)04:01:01

but if you want to use it at test time, you’ll need to include test.check as a test dependency

Alex Miller (Clojure team)04:01:50

[org.clojure/test.check “0.9.0”]

Alex Miller (Clojure team)04:01:22

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 🙂

Alex Miller (Clojure team)04:01:37

if you haven’t seen that yet


no I haven’t, thanks for pointing me to it!


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/anded 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


but that feels less than ideal


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?

Alex Miller (Clojure team)15:01:19

@luxbock the approach of using instrument with a :gen override is what I would recommend and should work

Alex Miller (Clojure team)15:01:57

if you can a repro, would be interested


Is there any chance that you guys could make the clojure.spec guide (PDF) printable? 🙂


Does Chrome -> Print -> Save as PDF work? Or do you need something else?


@qqq The result is just a lot of empty pages (I'm using Chromium BTW)


@luposlip: my bad, I also get only empty pages in chrome / safari


time to edit some HTML


hmm, I get something printable


but it loses all the formatting


Sounds just great @qqq, because printable pages would make 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.


Would be really nice with a epub export button for the long pages 🙂


is the main clojure site on github ?


I wonder if you can just print the github rendering of the page


I don't think so [UPDATE: I stand corrected]


can yo print that page?

qqq17:01:28 <-- is printable for me, think this is probably the best for now


great idea, didn't know it was on github - thanks for the input @qqq!!


@luposlip Also Firefox does a decent job with the page if you print to pdf.

Alex Miller (Clojure team)22:01:07

Making that work is probably not something I’m going to work on, but if someone else wanted to help, that would be cool