Created
October 28, 2017 19:32
-
-
Save mike706574/26749bd4992ad8a3536a9456e35d3b02 to your computer and use it in GitHub Desktop.
generators-question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[clojure.spec.alpha :as s]) | |
(require '[clojure.spec.gen.alpha :as gen]) | |
;; I was trying to write a spec for an 5-10 character alpha string with a | |
;; custom generator that uses clojure.spec.gen.alpha/char-alpha | |
(s/def ::test | |
(s/spec (s/and string? #(<= 5 (count %) 10)) | |
:gen (fn [] (gen/fmap #(apply str %) | |
(gen/vector gen/char-alpha 5 10))))) | |
;; sampling fails because clojure.spec.gen.alpha/char-alpha is not a generator | |
(gen/sample (s/gen ::test)) | |
;; => Unhandled java.lang.AssertionError | |
;; Assert failed: First arg to sample must be a generator (generator? generator) | |
;; clojure.spec.gen.alpha/char-alpha doesn't seem to pass | |
;; clojure.test.check.generators/generator? | |
(clojure.test.check.generators/generator? clojure.spec.gen.alpha/int) ;; => false | |
(gens/generator? gens/int) ;; => true | |
;; using clojure.test.check.generators/char-alpha instead seems to work | |
(require '[clojure.test.check.generators :as gens]) | |
(s/def ::test (s/spec (s/and string? #(<= 5 (count %) 10)) | |
:gen (fn [] (gen/fmap #(apply str %) | |
(gen/vector gens/char-alpha 5 10))))) | |
(gen/sample (s/gen ::test)) | |
;; => ("ylfWBh" | |
;; "kuEIOqw" | |
;; "GSdwuOOy" | |
;; "sMUQFUAUyU" | |
;; "nJAwjJbB" | |
;; "fmNjbG" | |
;; "GKTFADDaDc" | |
;; "GGxblg" | |
;; "KQveJyi" | |
;; "EeBAy") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment