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

+1 vote
in Spec by
Generator overrides for spec aliases are not respected.


(require '[clojure.spec :as s])
(require '[clojure.spec.gen :as gen])
(s/def ::original number?)
(s/def ::alias ::original)

(every? double? (gen/sample (s/gen ::alias {::alias gen/double})))
;; => false


Providing a generator override for the original spec works as expected:


(every? double? (gen/sample (s/gen ::alias {::original gen/double})))
;; => true

5 Answers

0 votes
by

Comment made by: alexmiller

Probably a missing delay in the alias case - there's another ticket that has the same cause.

0 votes
by

Comment made by: nwjsmith

Looks like it might be because {{gensub}} looks for matching overrides by calling {{spec-name}}, which returns the wrong value for spec aliases.

(require '[clojure.spec :as s]) (s/def ::original number?) (s/def ::alias ::original) (@#'clojure.spec/spec-name (s/get-spec ::alias)) ;; => :user/original

0 votes
by
_Comment made by: charleshd_

I've a somewhat similar issue. I think it is related.
I'm trying to do something like :
{code:none}
(require '[clojure.spec.alpha :as s])
(require '[clojure.spec.gen.alpha :as gen])
(s/def ::bar any?)
(s/def ::foo (s/with-gen any? (fn [] (s/gen ::bar))))
(gen/generate (s/gen ::foo {::bar (fn [] (s/gen int?))}))

I'm somewhat expecting it generates me an integer like it would have with a direct aliasing to ::bar in ::foo definition. But it doesn't and keep the with-gen binded generator.
Is that the same issue or is that an expected behaviour or should i fill a new issue ?
0 votes
by

Comment made by: jimrthy

I think I'm probably running into this, except that there seems to be some non-determinism involved.

It seems to behave differently, depending on whether I run it from within a deftest or from the REPL. (Running it from the REPL seems to fail every time. Running it inside deftest seems much more reliable).

My really long-winded description is at https://groups.google.com/forum/#!topic/clojure/zPWPmQGm94w

The sample where I tried to document exactly what I'm seeing is at https://gist.github.com/jimrthy/21851c52a8cd6b04a31ed08b1d0a7f04 (when I wrote that, running inside deftest seemed to work every time. That is not the case).

If nothing else, it would be nice to have better error messages that include the name of the spec it's failing to generate. Actually, that would be generally helpful and possibly worth its own ticket. Please let me know if you'd like me to create one.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-2079 (reported by nwjsmith)
...