Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns spec-ex.core
- (:require
- [clojure.spec.alpha :as s]
- [clojure.spec.gen.alpha :as gen]
- [clojure.spec.test.alpha :as stest]))
- ;; [org.clojure/spec.alpha "0.1.123"]
- ;; The goal is to stub functions which require some kind external
- ;; dependency, such as a service or other I/O.
- (defprotocol Y
- (-do-y [r]))
- (def y? (partial satisfies? Y))
- (s/def ::y y?)
- ;; Protocol methods can't be spec'd, so wrap them in a function.
- (defn do-y [r]
- (-do-y r))
- (s/fdef do-y :args (s/cat :y-er ::y))
- ;; Example of the protocol implementation that we're going to stub.
- (defrecord BadYer []
- Y
- (-do-y [_] (throw (Exception. "can't make me!"))))
- ;; Confirm BadYer instances are valid with respect to the protol spec.
- (s/valid? ::y (->BadYer))
- ;; => true
- ;; And confirm BadYer instances will throw when called.
- (try
- (do-y (->BadYer))
- (catch Exception e
- (.getMessage e)))
- ;; => "can't make me!"
- (def y-gen (gen/return (->BadYer)))
- ;; Confirm generator works as expected:
- (gen/sample y-gen 1)
- ;; => (#spec_ex.core.BadYer{})
- ;; We want to stub `do-y`, providing y-gen as a generator for `::y`
- (try
- (stest/instrument `do-y {:stub #{`do-y}
- :gen {::y (constantly y-gen)}})
- (catch Exception e
- (ex-data e)))
- ;; => #:clojure.spec.alpha{:path [:y-er], :form :spec-ex.core/y, :failure :no-gen}
- ;; However, we *can* stub `do-y` if we replace its spec.
- (stest/instrument `do-y
- {:stub #{`do-y}
- :spec {`do-y (s/fspec
- :args (s/cat :y-er (s/with-gen ::y
- (constantly y-gen))))}})
- ;; => [spec-ex.core/do-y]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement