Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. ;; Normally the exception thrown by such-that does not point to the failed spec, which makes it useless
  2. ;; Eval this to override it and include `path` to the spec (and `form`, not sure which better)
  3. (in-ns 'clojure.spec.alpha)
  4. (defn- gensub
  5. [spec overrides path rmap form]
  6. ;;(prn {:spec spec :over overrides :path path :form form})
  7. (let [spec (specize spec)]
  8. (if-let [g (c/or (when-let [gfn (c/or (get overrides (c/or (spec-name spec) spec))
  9. (get overrides path))]
  10. (gfn))
  11. (gen* spec overrides path rmap))]
  12. (gen/such-that #(valid? spec %) g {:max-tries 100
  13. :ex-fn (fn [{:keys [max-tries]}]
  14. (ex-info (str "Couldn't satisfy " (spec-name spec) " after " max-tries " tries.")
  15. {:max max-tries
  16. :path path
  17. :sample-explain (->> (first (gen/sample g 1))
  18. (explain-data spec)
  19. :clojure.spec.alpha/problems)}))}
  20. (let [abbr (abbrev form)]
  21. (throw (ex-info (str "Unable to construct gen at: " path " for: " abbr)
  22. {::path path ::form form ::failure :no-gen}))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement