-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
test.cljs
114 lines (100 loc) · 3.57 KB
/
test.cljs
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
(ns shadow.test
"cljs.test just without all those damn macros
requires the shadow.build.cljs-hacks deftest mod which calls shadow.test/register-test"
(:require [cljs.test :as ct]
[shadow.test.env :as env]))
(declare test-ns-block)
(defn run-tests-block
"Like test-vars, but returns a block for further composition and
later execution."
[env namespaces]
(let [summary
(volatile!
{:test 0 :pass 0 :fail 0 :error 0
:type :summary})
merge-counters
(fn []
(vswap!
summary
(partial merge-with +)
(:report-counters (ct/get-current-env))))]
(-> [(fn [] (ct/set-env! env))]
(into (->> namespaces
(mapcat (fn [ns]
(-> (test-ns-block env ns)
(conj merge-counters))))))
(conj (fn []
(ct/report @summary)
(ct/report (assoc @summary :type :end-run-tests))
(ct/clear-env!))))))
(defn run-tests
([]
(run-tests (ct/empty-env)))
([env]
(run-tests env (env/get-test-namespaces)))
([env namespaces]
(ct/run-block (run-tests-block env namespaces))))
(defn run-all-tests
"Runs all tests in all namespaces; prints results.
Optional argument is a regular expression; only namespaces with
names matching the regular expression (with re-matches) will be
tested."
([] (run-all-tests (ct/empty-env) nil))
([env] (run-all-tests env nil))
([env re]
(run-tests env
(->> (env/get-test-namespaces)
(filter #(or (nil? re)
(re-matches re (str %))))
(into [])))))
(defn test-all-vars-block [ns]
(let [env (ct/get-current-env)
{:keys [fixtures each-fixtures vars] :as test-ns}
(env/get-test-ns-info ns)]
(-> [(fn []
(when (nil? env)
(ct/set-env! (ct/empty-env)))
(when-let [fix (:once fixtures)]
(ct/update-current-env! [:once-fixtures] assoc ns fix))
(when-let [fix (:each fixtures)]
(ct/update-current-env! [:each-fixtures] assoc ns fix)))]
(into (ct/test-vars-block
(->> vars ;; vars is {test-name test-var}
(vals)
(sort-by #(-> % meta :line)))))
#_(conj (fn []
(when (nil? env)
(ct/clear-env!)))))))
(defn test-all-vars
"Calls test-vars on every var with :test metadata interned in the
namespace, with fixtures."
[ns]
(ct/run-block
(-> (test-all-vars-block ns)
(conj (fn []
(ct/report {:type :end-test-all-vars :ns ns}))))))
(defn test-ns-block
"Like test-ns, but returns a block for further composition and
later execution. Does not clear the current env."
([env ns]
{:pre [(symbol? ns)]}
[(fn []
(ct/set-env! env)
(ct/do-report {:type :begin-test-ns, :ns ns})
;; If the namespace has a test-ns-hook function, call that:
;; FIXME: must turn test-ns-hook into macro so it registers itself instead of just calling a defn
(ct/block (test-all-vars-block ns)))
(fn []
(ct/do-report {:type :end-test-ns, :ns ns}))]))
(defn test-ns
"If the namespace defines a function named test-ns-hook, calls that.
Otherwise, calls test-all-vars on the namespace. 'ns' is a
namespace object or a symbol.
Internally binds *report-counters* to a ref initialized to
*initial-report-counters*. "
([ns] (test-ns (ct/empty-env) ns))
([env ns]
(ct/run-block
(concat (test-ns-block env ns)
[(fn []
(ct/clear-env!))]))))