Fork me on GitHub
#clojure-spec
<
2017-01-24
>
basti04:01:27

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

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)

basti04:01:37

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

basti04:01:39

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

lsnape09:01:15

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.

lsnape09:01:01

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))))

luxbock13:01:19

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

luxbock13:01:02

however my generative test actually fails because I run into integer overflow, which is fair enough, that's good to know in theory

luxbock13:01:21

but in practice none of the values I use are ever going to get that large as to cause an overflow

luxbock13:01:19

so in this case, should I be handling the bounding of these values via instrument by using :gen overrides?

luxbock13:01:49

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?

luxbock14:01:44

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

donaldball14:01:21

Maybe you want something like a ::smallish-long that describes a domain consistent with your expected values in production?

luxbock14:01:49

@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

luxbock14:01:27

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

luxbock14:01:46

so of course I could use use a new spec for the return value of the function under test, which uses a higher bound

luxbock14:01:54

but that feels less than ideal

donaldball14:01:04

Annoying, albeit correct 😛

donaldball14:01:31

You… could merge with +’

luxbock14:01:02

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

luxbock14:01:32

but I'm guessing that I don't understand how instrument with :gen overrides is supposed to be used

luxbock14:01:19

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

luxbock15:01:58

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

luposlip16:01:21

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

qqq16:01:47

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

luposlip17:01:45

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

qqq17:01:15

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

qqq17:01:31

time to edit some HTML

qqq17:01:42

hmm, I get something printable

qqq17:01:46

but it loses all the formatting

luposlip17:01:33

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.)

qqq17:01:58

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.

luposlip17:01:00

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

qqq17:01:23

is the main clojure site on github ?

qqq17:01:28

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

luposlip17:01:40

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

qqq17:01:03

can yo print that page?

qqq17:01:28

https://github.com/clojure/clojure-site/blob/master/content/guides/spec.adoc <-- is printable for me, think this is probably the best for now

luposlip17:01:43

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

mac22:01:09

@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