A Clojure library designed to help you with assertions.
Add the following dependency to your project.clj
file:
[com.github.gustavo-flor/easy-assert "*.*.*"]
Other available options on Clojars repo.
First, require the necessary namespaces in your Clojure file:
(ns your-namespace
(:require [easy-assert.api :refer :all]))
The default API provides a set of helper functions for common assertions.
assert-that
The assert-that
function is used to assert various conditions:
(assert-that 5 :is-equal-to 5)
(assert-that 5 :is-not-equal-to 10)
(assert-that "hello" :starts-with "he")
; You can attach multiple conditions to be validated
(assert-that [1 2 3]
:has-size 3
:includes [1 2]
:does-not-include [4 5])
fail
The fail
function is used to force a failure with a given message:
(fail "This should always fail")
; Always fails with the message "This should always fail"
assert-that-thrown-by
The assert-that-thrown-by
function is used to assert that a function throws an exception:
(def exception (Exception. "error"))
(assert-that-thrown-by #(throw exception)
:exception-has-message "error"
:exception-caused-by nil) ; Passes
You can customize the assertion functions by generating your own helpers.
First, require the necessary namespaces:
(ns your-namespace
(:require [easy-assert.generator :refer [generate-helpers]]))
Then, generate the helper functions using your assertion function and matchers:
(def my-assert (fn [result message]
(when (not result)
(throw (AssertionError. message)))))
(defn has-status?
[actual expected]
(if (= (:status actual) expected)
[true]
[false "My custom failure message"]))
(def my-matchers {:has-status has-status?})
(def helpers (generate-helpers my-assert (merge default-matchers my-matchers)))
(def assert-that (:assert-that helpers))
Now you can use the generated helper functions in the same way as the default API:
(def http-response {:status 200 ,,,})
(assert-that http-response :has-status 200)
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close