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)))))
@jmglov
Copy link
Author

jmglov commented Sep 14, 2016

Here's a usage example:

(ns foo-test
  (:require [foo]
            [spec-test-utils :refer [checking fails-spec]]))

(deftest plus
  (checking 'foo/plus)
  (testing "Adding 0"
    (is (= 1 (foo/plus 1 0))))
  (testing "Invalid input"
    (fails-spec foo/plus 1 :forty-two)))

@bahulneel
Copy link

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.

@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