Welcome! Please see the About page for a little more info on how this works.

0 votes
in Spec by
Given a generator using such-that that fails to find a value, the error does not give enough information to determine which spec or predicate was at fault:


(require '[clojure.spec :as s])
(s/exercise (s/and string? #{"hi"}))
ExceptionInfo Couldn't satisfy such-that predicate after 100 tries.  clojure.core/ex-info (core.clj:4725)


Another special case of this is when providing a custom generator that produces a valid that doesn't satisfy the spec (Clojure adds this filter internally):


(require '[clojure.spec :as s])
(s/exercise (s/with-gen int? #(s/gen #{:a})))


*Proposal:* Indicate in the error which spec failed to generate and possibly the path in the overall spec if feasible.

(Note: original description moved to comment)

6 Answers

0 votes
by
_Comment made by: alexmiller_

[Original description from ticket:]

I created a generator that did not conform to the spec (doh!). The generator contained the such-that predicate. When I tried creating a sample from the generator I got this error:

ExceptionInfo Couldn't satisfy such-that predicate after 100 tries.  clojure.core/ex-info (core.clj:4725)

I assumed that it referred to my custom generator but that was a red herring because in fact spec must be using such-that to ensure that the generated value conforms to the spec, and it was this such-that that generated the failure, not the one in my custom generator.

Code (with the problem corrected but showing the such-that in my generator:


(defn mod11-checkdigit
  "Calculate the checkdigit see http://freagra.com/imthealth/mitNNC.html"
  [n]
  (let [x (->> (map #(Integer/parseInt (str %)) (take 9 n))
               (map * (range 10 1 -1))
               (reduce +))
        y (mod x 11)
        c (- 11 y)]
    (cond (== 10 c) nil
          (== 11 c) 0
          :else c)))

(def nhs-number-gen
  "Generates a valid NHS number"
  (gen/fmap #(str (+ (* 10 %) (mod11-checkdigit (str %))))
            (gen/such-that #(mod11-checkdigit (str %))
                           (gen/choose 100000000 999999999))))

(defn nhs-number?
  "Returns true if passed a valid nhs number else returns false"
  [n]
  (and (string? n) (= 10 (count n)) (= (str (mod11-checkdigit n)) (str (last n)))))

(s/def ::nhs-number (s/with-gen nhs-number?
                                (fn [] nhs-number-gen)))




It would be nicer if the error thrown due to the generated value being non-conformant with the spec stated this.
0 votes
by

Comment made by: alexmiller

I'm not sure that this is possible right now based on what we give to and get back from test.check.

0 votes
by

Comment made by: glittershark

It looks like test.check is being updated to support error customization: https://github.com/clojure/test.check/commit/5aea0e275257680b672309b1e940be6dae92c17d . I've got a patch which updates clojure.spec to use it, though obviously it doesn't work since the linked commit hasn't made it into a released version of test.check yet.

0 votes
by

Comment made by: glittershark

See also http://dev.clojure.org/jira/browse/TCHECK-107 I suppose

0 votes
by

Comment made by: glittershark

Attached the patch for future reference (better-such-that-info.patch).

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-2025 (reported by alex+import)
...