Created
September 14, 2016 12:53
-
-
Save jmglov/30571ede32d34208d77bebe51bb64f29 to your computer and use it in GitHub Desktop.
Utility functions for using `clojure.spec` generative tests in standard `clojure.test` unit tests.
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
(ns spec-test-utils | |
(:require [clojure.spec.test :as stest] | |
[clojure.test :refer [is testing]])) | |
(defn- check [function num-tests] | |
(if num-tests | |
(stest/check function {:clojure.spec.test.check/opts {:num-tests num-tests}}) | |
(stest/check function))) | |
(defn checking | |
([function] | |
(checking function nil)) | |
([function num-tests] | |
(testing "Schema" | |
(let [result (-> (check function num-tests) | |
first | |
:clojure.spec.test.check/ret | |
:result)] | |
(cond | |
(true? result) (is result) | |
(nil? (ex-data result)) (is (= {} result)) | |
:else (is (= {} (ex-data result)))))))) | |
(defn fails-spec [f & args] | |
(try | |
(let [res (apply f args)] | |
(is (instance? Exception res))) | |
(catch Exception e | |
(is (contains? (ex-data e) :clojure.spec/problems))))) |
is fails-spec
only intended to test non-conformance to a function's arg-spec? would it therefore be better to just lookup the arg-spec and try it directly? As-is I feel like there's ambiguity where the exception could have been thrown from farther down the stack potentially.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A useful way to include this in your project is to:
Then add the path to
test-paths
inproject.clj
You may need to remember to have:
In your build scripts somewhere.