Skip to content

Instantly share code, notes, and snippets.

@tavisrudd
Forked from cemerick/generative.clj
Created July 20, 2012 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tavisrudd/3152423 to your computer and use it in GitHub Desktop.
Save tavisrudd/3152423 to your computer and use it in GitHub Desktop.
"Integrating" clojure.test and test.generative
;; 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