/
report.clj
272 lines (222 loc) · 8.34 KB
/
report.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
(ns kaocha.report
(:require [kaocha.output :as out]
[kaocha.plugin.capture-output :as capture]
[kaocha.stacktrace :as stack]
[clojure.test :as t]
[slingshot.slingshot :refer [throw+]]
[clojure.string :as str]
[kaocha.history :as history]
[kaocha.testable :as testable]))
(def clojure-test-report t/report)
(defn dispatch-extra-keys
"Call the original clojure.test/report multimethod when dispatching an unknown
key. This is to support libraries like nubank/matcher-combinators that extend
clojure.test/assert-expr, as well as clojure.test/report, to signal special
conditions."
[m]
(when-not (contains? #{:pass
:fail
:error
:begin-test-suite
:end-test-suite
:begin-test-ns
:end-test-ns
:begin-test-var
:end-test-var
:summary}
(:type m))
(clojure-test-report m)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti dots* :type)
(defmethod dots* :default [_])
(defmethod dots* :pass [_]
(t/with-test-out
(print ".")
(flush)))
;; newer versions of matcher-combinators
(defmethod dots* :matcher.combinators/mismatch [_]
(t/with-test-out
(print (out/colored :red "F"))
(flush)))
;; older versions of matcher-combinators
(defmethod dots* :mismatch [_]
(t/with-test-out
(print (out/colored :red "F"))
(flush)))
(defmethod dots* :fail [_]
(t/with-test-out
(print (out/colored :red "F"))
(flush)))
(defmethod dots* :error [_]
(t/with-test-out
(print (out/colored :red "E"))
(flush)))
(defmethod dots* :begin-test-ns [_]
(t/with-test-out
(print "(")
(flush)))
(defmethod dots* :end-test-ns [_]
(t/with-test-out
(print ")")
(flush)))
(defmethod dots* :begin-test-suite [_]
(t/with-test-out
(print "[")
(flush)))
(defmethod dots* :end-test-suite [_]
(t/with-test-out
(print "]")
(flush)))
(defmethod dots* :summary [_]
(t/with-test-out
(println)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti report-counters :type)
(defmethod report-counters :default [_])
(defmethod report-counters :pass [m]
(t/inc-report-counter :pass))
;; As long as we pass :mismatch events on to matcher-combinators they will get
;; reported as failures there
;; (defmethod report-counters :mismatch [m] (t/inc-report-counter :fail))
(defmethod report-counters :fail [m]
(t/inc-report-counter :fail))
(defmethod report-counters :error [m]
(t/inc-report-counter :error))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti result :type)
(defmethod result :default [_])
(defn- testing-vars-str
"Returns a string representation of the current test. Renders names
in :testing-vars as a list, then the source file and line of current
assertion."
[{:keys [file line testing-vars kaocha/testable] :as m}]
(let [file (or (some-> testable ::testable/meta :file) file)
line (or (some-> testable ::testable/meta :line) line)]
(str
;; Uncomment to include namespace in failure report:
;;(ns-name (:ns (meta (first *testing-vars*)))) "/ "
(if (seq testing-vars)
(reverse (map #(:name (meta %)) testing-vars))
(name (:kaocha.testable/id testable)))
" (" file ":" line ")")))
(defn- print-output [m]
(let [buffer (get-in m [:kaocha/testable ::capture/buffer])
out (capture/read-buffer buffer)]
(when (seq out)
(println "------ Test output -------------------------------------")
(println (str/trim-newline out))
(println "--------------------------------------------------------"))))
(defn- summary-fail [{:keys [testing-contexts testing-vars] :as m}]
(println "\nFAIL in" (testing-vars-str m))
(when (seq testing-contexts)
(println (str/join " " testing-contexts)))
(when-let [message (:message m)]
(println message))
(println "expected:" (pr-str (:expected m)))
(println " actual:" (pr-str (:actual m)))
(print-output m))
(defn- summary-error [{:keys [testing-contexts testing-vars] :as m}]
(println "\nERROR in" (testing-vars-str m))
(when (seq testing-contexts)
(println (str/join " " testing-contexts)))
(when-let [message (:message m)]
(println message))
(print-output m)
(print "Exception: ")
(let [actual (:actual m)]
(if (instance? Throwable actual)
(stack/print-cause-trace actual t/*stack-trace-depth*)
(prn actual))))
(defmethod result :summary [m]
(t/with-test-out
(let [failures (filter (comp #{:fail :error} :type) @history/*history*)]
(doseq [{:keys [testing-contexts testing-vars] :as m} failures]
(binding [t/*testing-contexts* testing-contexts
t/*testing-vars* testing-vars]
(case (:type m)
:fail (summary-fail m)
:error (summary-error m)))))
(let [{:keys [test pass fail error] :or {pass 0 fail 0 error 0}} m
passed? (pos-int? (+ fail error))]
(println (out/colored (if passed? :red :green)
(str test " test vars, "
(+ pass fail error) " assertions, "
(when (pos-int? error)
(str error " errors, "))
fail " failures."))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn fail-fast
"Fail fast reporter, add this as a final reporter to interrupt testing as soon
as a failure or error is encountered."
[m]
(when (and (some #{(:type m)} [:error :fail :mismatch])
(not (:kaocha.result/exception m))) ;; prevent handled exceptions from being re-thrown
(throw+ {:kaocha/fail-fast true})))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def doc-printed-contexts (atom nil))
(defn doc-print-contexts [contexts & [suffix]]
(let [printed-contexts @doc-printed-contexts]
(let [contexts (reverse contexts)
printed (reverse printed-contexts)
pairwise (map vector (concat printed (repeat nil)) contexts)
nesting (->> pairwise (take-while (fn [[x y]] (= x y))) count)
new-contexts (->> pairwise (drop-while (fn [[x y]] (= x y))) (map last))]
(when (seq new-contexts)
(doseq [[ctx idx] (map vector new-contexts (range))
:let [nesting (+ nesting idx)]]
(print (str "\n"
" "
(apply str (repeat nesting " "))
ctx))
(flush))))
#_(when (> (count contexts) (count printed-contexts))
(println)
(doseq [[c1 c2] (map vector (concat printed-contexts
(repeat nil)) contexts)]
(print (if (= c1 c2)
" "
(str " " c2)))))
(reset! doc-printed-contexts contexts)))
(defmulti doc :type)
(defmethod doc :default [_])
(defmethod doc :begin-test-suite [m]
(t/with-test-out
(reset! doc-printed-contexts (list))
(print "---" (-> m :kaocha/testable :kaocha.testable/id) "---------------------------")
(flush)))
(defmethod doc :begin-test-ns [m]
(t/with-test-out
(reset! doc-printed-contexts (list))
(print (str "\n" (-> m :kaocha/testable :kaocha.ns/name)))
(flush)))
(defmethod doc :end-test-ns [m]
(t/with-test-out
(println)))
(defmethod doc :begin-test-var [m]
(t/with-test-out
(let [{:keys [name]} (-> m :var meta)]
(print (str "\n " name))
(flush))))
(defmethod doc :pass [m]
(t/with-test-out
(doc-print-contexts t/*testing-contexts*)))
(defmethod doc :error [m]
(t/with-test-out
(doc-print-contexts t/*testing-contexts*)
(print (out/colored :red " ERROR"))))
(defmethod doc :fail [m]
(t/with-test-out
(doc-print-contexts t/*testing-contexts*)
(print (out/colored :red " FAIL"))))
(defmethod doc :summary [m]
(t/with-test-out
(println)))
(defn debug [m]
(prn (cond-> (select-keys m [:type :var :ns])
(:kaocha/testable m) (update :kaocha/testable select-keys [:kaocha.testable/id :kaocha.testable/type]))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def dots
"Reporter that prints progress as a sequence of dots and letters."
[dots* result])
(def documentation
[doc result])