Created
June 27, 2012 15:03
-
-
Save cemerick/3004649 to your computer and use it in GitHub Desktop.
"Integrating" clojure.test and test.generative
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
;; My good-enough glomming together of clojure.test and test.generative | |
(ns cemerick.generative | |
(:require [clojure.test.generative.generators :as gens] | |
[clojure.test.generative :as gen]) | |
(:use clojure.test)) | |
;; Too bad last-report isn't sent an action upon success as well. | |
;; Perhaps this should just be replaced with a try/catch/rethrow | |
;; around the body in defspectest | |
(defn capture-spec-results | |
[var] | |
(let [watch-key (gens/keyword)] | |
(add-watch gen/last-report watch-key | |
(fn [_ _ _ result] | |
(when (:error result) (is false)))) | |
(try | |
(let [fs (doall (map deref (gen/test-vars var)))] | |
(is (every? (comp #(not (instance? Throwable %))) fs))) | |
(finally | |
(remove-watch gen/last-report watch-key))))) | |
(defmacro defspectest | |
"Defines a clojure.test test var named `name`, and a test.generative | |
test spec named ~(str name \"*\"). When invoked, the `name` test | |
will run the test.generative test spec, reporting each | |
iteration as a passed or failed clojure.test assertion, and | |
potentially failing the clojure.test test run if any spec failures | |
occurred or exceptions were thrown." | |
[name & body] | |
`(do | |
(gen/defspec ~(symbol (str name "*")) | |
~@body | |
(is true)) | |
(deftest ~name | |
(capture-spec-results (var ~(symbol (str name "*"))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment