Skip to content

Instantly share code, notes, and snippets.

@jmglov
Created September 14, 2016 12:53
Show Gist options
  • Save jmglov/30571ede32d34208d77bebe51bb64f29 to your computer and use it in GitHub Desktop.
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.
(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)))))
@gfredericks
Copy link

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