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))))) |
A useful way to include this in your project is to:
git submodule add https://gist.github.com/30571ede32d34208d77bebe51bb64f29.git some/path
Then add the path to test-paths
in project.clj
You may need to remember to have:
git submodule sync
git submodule update --init
In your build scripts somewhere.
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
Here's a usage example: