ClojureDocs

导航

命名空间

gen

  • (gen spec)
  • (gen spec overrides)
Given a spec, returns the generator for it, or throws if none can
be constructed. Optionally an overrides map can be provided which
should map spec names or paths (vectors of keywords) to no-arg
generator-creating fns. These will be used instead of the generators at those
names/paths. Note that parent generator (in the spec or overrides
map) will supersede those of any subtrees. A generator for a regex
op must always return a sequential collection (i.e. a generator for
s/? should return either an empty sequence/vector or a
sequence/vector with one item in it)
2 Examples
;; get a generator for the spec 'int?' and generate a sample from it
(gen/sample (s/gen int?))
; => (-1 0 0 -1 7 2 1 0 59 -6)
;; Generator override example
(s/def :foo/bar
  (s/keys :req [:biz/baz]))

(s/def :biz/baz int?)

(comment
  (gen/sample
   (s/gen :foo/bar {:biz/baz #(s/gen #{1 10 100})}) 5)
  ;; Example Result
  (#:biz{:baz 100}
   #:biz{:baz 100}
   #:biz{:baz 1}
   #:biz{:baz 10}
   #:biz{:baz 10}))

;; Note that "Spec does not trust custom generators and any values they produce
;; will also be checked by their associated spec to guarantee they pass
;; conformance."
;; See: https://clojure.org/guides/spec#_custom_generators
See Also

Takes a spec and a no-arg, generator-returning fn and returns a version of that spec that uses that ...

Added by yuhan0
0 Notes
No notes for gen