-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.cljc
66 lines (56 loc) · 2.32 KB
/
api.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(ns nedap.utils.test.api
(:require
#?(:clj [clojure.test] :cljs [cljs.test])
[clojure.spec.alpha :as spec]
[clojure.walk :as walk]
[nedap.utils.test.impl :as impl]))
(defn ^:deprecated simple=
"Check whether all `vals` have similar structure disregarding possible order
NOTE: this function will disregard duplicates"
[& vals]
{:post [(boolean? %)]}
(apply = (map impl/simplify vals)))
(defn meta=
"Do all `xs` (and their metadata, and their members' metadata, and also any metametadata) equal?"
[& xs]
{:post [(boolean? %)]}
(impl/meta= xs))
(defn macroexpansion=
"Do all `xs` equal, when deeming any contained gensyms unconditionally equal?
Adequate for when the presence of gensyms would hinder a comparison."
[& xs]
{:post [(boolean? %)]}
(letfn [(r [tree]
(walk/postwalk impl/replace-gensyms tree))]
(->> xs (map r) (apply =))))
(defmacro run-tests
"Runs all tests for the given namespaces (defaulting to the current namespace if none given),
printing the results.
When invoked under a cljs context, the Unix exit code will be set to 0 or 1, depending on success."
[& namespaces]
{:pre [(spec/valid? (spec/coll-of impl/quoted-namespace?)
namespaces)]}
(if-let [clj? (-> &env :ns nil?)]
`(clojure.test/run-tests ~@namespaces)
`(cljs.test/run-tests (cljs.test/empty-env ::impl/exit-code-reporter) ~@namespaces)))
(defmacro expect
"Asserts (via `#'clojure.test/is`) that the expression denoted by `to-change` changes from `from`, to `to`.
The equality comparator can be overridden with `with`.`.
`(expect (swap! a inc) :to-change @a :from 1 :to 2)`"
[& forms]
(let [options (->> (reverse forms)
(partition 2)
(take-while (fn [[_v k]]
(keyword? k)))
(reduce (fn [memo [v k]]
(assoc memo k v)) {}))
bodies (drop-last (* 2 (count options)) forms)
defaults {:with 'meta=}
clj? (-> &env :ns nil?)]
(impl/expect bodies (merge defaults options) clj?)))
(defmethod impl/expect-matcher 'meta= [_]
{:pred-sym-failure "`to-change` does not equal `from`: (not (meta= %s %s))"
:pred-failure "`from` is not allowed to equal `to`: %s"
:assert-expr-sym `meta=
:pred-sym `meta=
:pred meta=})