-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.clj
executable file
·285 lines (251 loc) · 11.4 KB
/
test.clj
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
;; copyright (c) 2018-2019 sean corfield, all rights reserved
(ns expectations.clojure.test
"This namespace provides compatibility with clojure.test and related tooling.
This namespace should be used standalone, without requiring the 'expectations'
namespace -- this provides a translation layer from Expectations syntax down
to clojure.test functionality.
We do not support ClojureScript in clojure.test mode, sorry."
(:require [clojure.data :as data]
[clojure.string :as str]
[clojure.test :as t]))
(def humane-test-output?
"If Humane Test Output is available, activate it, and enable compatibility
of our =? with it."
(try
(require 'pjstadig.humane-test-output)
((resolve 'pjstadig.humane-test-output/activate!))
true
(catch Throwable _)))
;; stub functions for :refer compatibility:
(defn- bad-usage [s]
`(throw (IllegalArgumentException.
(str ~s " should only be used inside expect"))))
(defmacro ^:no-doc in [& _] (bad-usage "in"))
(defmacro ^:no-doc from-each [& _] (bad-usage "from-each"))
(defmacro ^:no-doc more-of [& _] (bad-usage "more-of"))
(defmacro ^:no-doc more-> [& _] (bad-usage "more->"))
(defmacro ^:no-doc more [& _] (bad-usage "more"))
(defn ^:no-doc spec? [e]
(and (keyword? e)
(try
(require 'clojure.spec.alpha)
(when-let [get-spec (resolve 'clojure.spec.alpha/get-spec)]
(boolean (get-spec e)))
(catch Throwable _))))
;; smart equality extension to clojure.test assertion -- if the expected form
;; is a predicate (function) then the assertion is equivalent to (is (e a))
;; rather than (is (= e a)) and we need the type check done at runtime, not
;; as part of the macro translation layer
(defmethod t/assert-expr '=? [msg form]
;; (is (=? val-or-pred expr))
(let [[_ e a] form
conform? (spec? e)]
`(let [e# ~e
a# ~a
valid?# (when ~conform? (resolve 'clojure.spec.alpha/valid?))
explain-str?# (when ~conform? (resolve 'clojure.spec.alpha/explain-str))
r# (cond ~conform?
(valid?# e# a#)
(fn? e#)
(e# a#)
:else
(= e# a#))
humane?# (and humane-test-output? (not (fn? e#)) (not ~conform?))]
(if r#
(t/do-report {:type :pass, :message ~msg,
:expected '~form, :actual (if (fn? e#)
(list '~e a#)
a#)})
(t/do-report {:type :fail, :message (if ~conform?
(explain-str?# e# a#)
~msg)
:diffs (if humane?#
[[a# (take 2 (data/diff e# a#))]]
[])
:expected (if humane?# e# '~form)
:actual (cond (fn? e#)
(list '~'not (list '~e a#))
humane?#
[a#]
:else
(list '~'not (list '~'=? e# a#)))}))
r#)))
(defmacro ^:no-doc ?
"Wrapper for forms that might throw an exception so exception class names
can be used as predicates. This is only needed for more-> so that you can
thread exceptions into code that can parse information out of them, to be
used with various expect predicates."
[form]
`(try ~form (catch Throwable t# t#)))
(defn ^:no-doc all-report
"Given an atom in which to accumulate results, return a function that
can be used in place of clojure.test/do-report, which simply remembers
all the reported results.
This is used to support the semantics of expect/in."
[store]
(fn [m]
(swap! store update (:type m) (fnil conj []) m)))
(defmacro expect
"Translate Expectations DSL to clojure.test language.
These are approximate translations for the most basic forms:
`(expect actual)` => `(is actual)`
`(expect expected actual)` => `(is (= expected actual))`
`(expect predicate actual)` => `(is (predicate actual))`
`(expect regex actual)` => `(is (re-find regex actual))`
`(expect ClassName actual)` => `(is (instance? ClassName actual))`
`(expect ExceptionType actual)` => `(is (thrown? ExceptionType actual))`
`(expect spec actual)` => `(is (s/valid? spec actual))`
In addition, `actual` can be `(from-each [x coll] (computation-of x))`
or `(in set-of-results)` or `(in larger-hash-map)`.
Also, `expect` can be one of `(more predicate1 .. predicateN)`,
`(more-> exp1 expr1 .. expN exprN)` where `actual` is threaded through
each expression `exprX` and checked with the expected value `expX`,
or `(more-of binding exp1 val1 .. expN valN)` where `actual` is
destructured using the `binding` and then each expected value `expX`
is used to check each `valX` -- expressions based on symbols in the
`binding`."
([a] `(t/is ~a))
([e a] `(expect ~e ~a true ~e))
([e a ex?] `(expect ~e ~a ~ex? ~e))
([e a ex? e']
(let [msg (when-not (= e e')
(str " within: "
(pr-str (if (and (sequential? e') (= 'expect (first e')))
e'
(list 'expect e' a)))))]
(cond
(and (sequential? a) (= 'from-each (first a)))
(let [[_ bindings & body] a]
(if (= 1 (count body))
`(doseq ~bindings
(expect ~e ~(first body) ~ex?))
`(doseq ~bindings
(expect ~e (do ~@body) ~ex?))))
(and (sequential? a) (= 'in (first a)))
(let [form `(~'expect ~e ~a)]
`(let [a# ~(second a)]
(cond (or (sequential? a#) (set? a#))
(let [all-reports# (atom nil)]
(with-redefs [t/do-report (all-report all-reports#)]
(doseq [~'x a#]
;; TODO: really want x evaluated here!
(expect ~'x ~e ~ex? ~form)))
(if (contains? @all-reports# :pass)
;; report all the passes (and no failures or errors)
(doseq [r# (:pass @all-reports#)] (t/do-report r#))
(do
;; report all the errors and all the failures
(doseq [r# (:error @all-reports#)] (t/do-report r#))
(doseq [r# (:fail @all-reports#)] (t/do-report r#)))))
(map? a#)
(let [e# ~e]
(expect e# (select-keys a# (keys e#)) ~ex? ~form))
:else
(throw (IllegalArgumentException. "'in' requires map or sequence")))))
(and (sequential? e) (= 'more (first e)))
(let [es (mapv (fn [e] `(expect ~e ~a ~ex? ~e')) (rest e))]
`(do ~@es))
(and (sequential? e) (= 'more-> (first e)))
(let [es (mapv (fn [[e a->]]
(if (and (sequential? a->)
(symbol? (first a->))
(let [s (name (first a->))]
(or (str/ends-with? s "->")
(str/ends-with? s "->>"))))
`(expect ~e (~(first a->) (? ~a) ~@(rest a->)) false ~e')
`(expect ~e (-> (? ~a) ~a->) false ~e')))
(partition 2 (rest e)))]
`(do ~@es))
(and (sequential? e) (= 'more-of (first e)))
(let [es (mapv (fn [[e a]] `(expect ~e ~a ~ex? ~e'))
(partition 2 (rest (rest e))))]
`(let [~(second e) ~a] ~@es))
(and ex? (symbol? e) (resolve e) (class? (resolve e)))
(if msg
(if (isa? (resolve e) Throwable)
`(t/is (~'thrown? ~e ~a) ~msg)
`(t/is (~'instance? ~e ~a) ~msg))
(if (isa? (resolve e) Throwable)
`(t/is (~'thrown? ~e ~a))
`(t/is (~'instance? ~e ~a))))
(isa? (type e) java.util.regex.Pattern)
(if msg
`(t/is (re-find ~e ~a) ~msg)
`(t/is (re-find ~e ~a)))
:else
(if msg
`(t/is (~'=? ~e ~a) ~msg)
`(t/is (~'=? ~e ~a)))))))
(comment
(macroexpand '(expect (more-> 1 :a 2 :b 3 (-> :c :d)) {:a 1 :b 2 :c {:d 4}}))
(macroexpand '(expect (more-of a 2 a) 4))
(macroexpand '(expect (more-of {:keys [a b c]} 1 a 2 b 3 c) {:a 1 :b 2 :c 3})))
(defn- contains-expect?
"Given a form, return true if it contains any calls to the 'expect' macro."
[e]
(when (and (coll? e) (not (vector? e)))
(or (= 'expect (first e))
(some contains-expect? e))))
(defmacro defexpect
"Given a name (a symbol that may include metadata) and a test body,
produce a standard 'clojure.test' test var (using 'deftest').
`(defexpect name expected actual)` is a special case shorthand for
`(defexpect name (expect expected actual))` provided as an easy way to migrate
legacy Expectation tests to the 'clojure.test' compatibility version."
[n & body]
(if (and (>= 2 (count body))
(not (some contains-expect? body)))
`(t/deftest ~n (expect ~@body))
`(t/deftest ~n ~@body)))
(defmacro expecting
"The Expectations version of `clojure.test/testing`."
[string & body]
`(t/testing ~string ~@body))
;; DSL functions copied from Expectations:
(defmacro side-effects
"Given a vector of functions to track calls to, execute the body.
Returns a vector of each set of arguments used in calls to those
functions. The specified functions will not actually be called:
only their arguments will be tracked, and the tracking versions of
those functions will not return useful values (so they should be
purely side-effecting functions, whose results are not used!)."
[fn-vec & forms]
(when-not (vector? fn-vec)
(throw (IllegalArgumentException.
"side-effects requires a vector as its first argument")))
(let [side-effects-sym (gensym "conf-fn")]
`(let [~side-effects-sym (atom [])]
(with-redefs ~(vec (interleave fn-vec (repeat `(fn [& args#] (swap! ~side-effects-sym conj args#)))))
~@forms)
@~side-effects-sym)))
(defn approximately
"Given a value and an optional delta (default 0.001), return a predicate
that expects its argument to be within that delta of the given value."
([^double v] (approximately v 0.001))
([^double v ^double d]
(fn [x] (<= (- v (Math/abs d)) x (+ v (Math/abs d))))))
(defn between
"Given a pair of (numeric) values, return a predicate that expects its
argument to be be those values or between them -- inclusively."
[a b]
(fn [x] (<= a x b)))
(defn between'
"Given a pair of (numeric) values, return a predicate that expects its
argument to be (strictly) between those values -- exclusively."
[a b]
(fn [x] (< a x b)))
(defn functionally
"Given a pair of functions, return a custom predicate that checks that they
return the same result when applied to a value. May optionally accept a
'difference' function that should accept the result of each function and
return a string explaininhg how they actually differ.
For explaining strings, you could use expectations/strings-difference.
(only when I port it across!)
Right now this produces pretty awful failure messages. FIXME!"
([expected-fn actual-fn]
(functionally expected-fn actual-fn (constantly "not functionally equivalent")))
([expected-fn actual-fn difference-fn]
(fn [x]
(let [e-val (expected-fn x)
a-val (actual-fn x)]
(t/is (= e-val a-val) (difference-fn e-val a-val))))))