forked from OdonataResearchLLC/lisp-unit
-
Notifications
You must be signed in to change notification settings - Fork 6
/
summarize.lisp
244 lines (226 loc) · 9.05 KB
/
summarize.lisp
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
;;;-*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
(in-package :lisp-unit2)
(cl-interpol:enable-interpol-syntax)
(defmacro pprint-test-block ((&key per-line-prefix) &body body)
`(block nil ;; this prevents outputing an extra newline/prefix
(pprint-logical-block (*test-stream* nil ,@(when per-line-prefix
`(:per-line-prefix ,per-line-prefix)))
(pprint-indent :current 0 *test-stream*)
,@body
(pprint-exit-if-list-exhausted))))
(defun with-assertion-summary-context (body-fn
&aux (*print-pretty* t) rtn)
(pprint-test-block ()
(format *test-stream* "~%")
(handler-bind
((assertion-pass
(lambda (c) (format *test-stream* "~@:_~A:Passed ~A"
(or
(ignore-errors
(short-full-name c))
"<No Test>")
(assertion c))))
(assertion-fail
(lambda (c) (%print-summary (failure c)))))
(setf rtn (multiple-value-list (funcall body-fn))))
(format *test-stream* "~0I~%"))
(apply #'values rtn))
(defun with-test-results-context (body-fn
&key
collection-place-setter
(summarize? nil summarize?-p)
&aux rtn test-result-dbs)
"A context that collects test-result-databases
if collection-place-setter (lambda (new) (setf collection-place new)) exists
anytime we get a new test result we set the place to the new collection
if summarize? at the end of the block we print the results summary for
all tests this defaults to T if no collection-place-setter is provided
and nil otherwise"
(flet ((collect-test-results (c &aux (results-db (results c)))
(when results-db
(%collect! results-db test-result-dbs)
(when collection-place-setter
(funcall collection-place-setter (head test-result-dbs))))))
(handler-bind
((all-tests-start #'collect-test-results)
(collect-test-results #'collect-test-results))
(setf rtn (multiple-value-list (do-contexts body-fn +interop-test-result-contexts+)))))
;; we tested multiple systems / had multiple calls to run-tests
;; inside this block
(when (or summarize? (and (null summarize?-p)
(null collection-place-setter)))
(iter (for res in (head test-result-dbs))
(print-failure-summary res)))
(apply #'values rtn))
(defmacro with-test-results ((&key collection-place (summarize? nil summarize?-p)) &body body)
"see with-test-results-context"
`(with-test-results-context
(lambda () ,@body)
,@(when collection-place
`(:collection-place-setter (lambda (new) (setf ,collection-place new))))
,@(when summarize?-p
`(:summarize? ,summarize?))))
(defun with-summary-context (body-fn
&key name
&aux (*print-pretty* t) rtn )
(flet ((ensure-names (c)
"Syncs the names between this env and our results object"
(when (and name (null (name (results c))))
(setf (name (results c)) name))
(when (and (null name) (name (results c)))
(setf name (name (results c))))))
;; this resets the actual indenting which, for whatever reason gets
;; banged by compiler messages without this
(pprint-test-block () (format *test-stream* "~%"))
(pprint-test-block ()
(handler-bind
((all-tests-start
(lambda (c)
(ensure-names c)
(if name
(format *test-stream* "~0I------- STARTING Testing: ~A ~%" name)
(format *test-stream* "~%~0I"))))
(test-start
(lambda (c)
(format *test-stream* "~@:_Starting: ~A~@:_"
(short-full-name c))))
(all-tests-complete
(lambda (c)
(ensure-names c)
(%print-result-summary (results c))
(if name
(format *test-stream* "~%~0I------- ENDING Testing: ~A ~%" name)
(format *test-stream* "~0I~%"))))
(test-complete (lambda (c) (%print-summary (result c)))))
(setf rtn (multiple-value-list (funcall body-fn)))))
(apply #'values rtn)))
(defmacro with-summary ((&key name) &body body)
`(with-summary-context (lambda () ,@body)
:name ,name))
(defmacro with-assertion-summary (() &body body)
`(with-assertion-summary-context (lambda () ,@body)))
(defun %out (s &rest args
&aux (*print-pretty* t))
(format *test-stream* "~?~@:_" (or s "") args))
(defgeneric %print-result-summary (db)
(:method ((o test-results-db))
(let ((total (len (tests o)))
(passed (len (passed-assertions o)))
(failed (len (failed-assertions o)))
(errors (len (errors o)))
(warnings (len (all-warnings o)))
(empty (len (empty o)))
(missing (len (missing o)))
(name (short-full-name o)))
(format *test-stream* "~@:_Test Summary ")
(if name
(format *test-stream* "for ~A " name))
(format *test-stream* "(~D tests ~,2F sec)~@:_" total (run-time o))
(format *test-stream* " | ~D assertions total~@:_" (+ passed failed))
(format *test-stream* " | ~D passed~@:_" passed)
(format *test-stream* " | ~D failed~@:_" failed)
(format *test-stream* " | ~D execution errors~@:_" errors)
(format *test-stream* " | ~D warnings~@:_" warnings)
(format *test-stream* " | ~D empty~@:_" empty)
(format *test-stream* " | ~D missing tests~@:_" missing))))
(defun print-summary (o &aux (*print-pretty* t))
(pprint-test-block ()
(%print-summary o))
o)
(defun print-failure-summary (o &aux (*print-pretty* t))
(format *test-stream* "~%~0I")
(pprint-test-block ()
(print-status-summary o '(failed errors warnings empty))
(%print-result-summary o))
o)
(defgeneric %print-summary (o)
(:documentation "Print a summary of all results to the stream.")
(:method ((o vector))
(iter (for res in-vector o)
(%print-summary res))
o)
(:method ((o list))
(iter (for res in o)
(%print-summary res))
o)
(:method ((o test-results-db))
(pprint-test-block ()
(%print-summary (results o))
(%print-result-summary o))
o)
(:method ((run test-result))
(let ((not-ok? (or (failed run) (errors run) (warnings run))))
(format *test-stream* "~@_~A - ~A (~,2Fs) : ~S assertions passed~@:_"
(ignore-errors (short-full-name run))
(status run)
(run-time run)
(len (passed run)))
;; prevents print an extra "\n | "
(when not-ok?
(pprint-test-block (:per-line-prefix " | ")
(iter (for s in '(errors failed warnings))
(print-status-summary run s)))))
run)
(:method :around ((result failure-result))
(%out "Failed Form: ~S" (form result))
(call-next-method)
(when (extras result)
(iter (for (f v) on (extras result) by #'cddr)
(if (equalp f v)
(%out "~S" f)
(%out "~S => ~S" f v))))
result)
(:method ((result failure-result))
(%out "Expected ~{~S~^; ~} " (expected result))
(format *test-stream*
"~<~:;but saw ~{~S~^; ~}~>"
(actual result))
(%out ""))
(:method ((result error-result))
(%out "~@[Should have signalled ~{~S~^; ~} but saw~]"
(expected result))
(%out "~{~S~^; ~}" (actual result))
result)
(:method ((result macro-result))
(%out "Should have expanded to ~{~S~^; ~} " (expected result))
(format *test-stream*
"~<~:;but saw ~{~S~^; ~}~>"
(actual result))
(%out "")
result)
(:method ((result output-result))
(format *test-stream* "~@:_| Should have printed ~{~S~^; ~} "
(expected result))
(format *test-stream* "~<~:;but saw ~{~S~^; ~}~>"
(actual result))
result)
(:method ((w warning))
(%out "WARNING: ~A" w)
(%out "~S" w)
w)
(:method ((e error))
(%out "ERROR: ~A" e)
(%out "~S" e)
e)
(:method (it)
(%out "~A" it)))
(defgeneric print-status-summary (object status)
(:method ((db test-results-db) (status list))
(iter (for s in status)
(print-status-summary db s)))
(:method ((db test-results-db) (s symbol) &aux (objs (funcall s db)))
(when objs
(iter (for o in (typecase objs
(list objs)
(list-collector (head objs))))
(format *test-stream* "~@:_~A (~A)~@:_-----------~@:_" (short-full-name o) (status o))
(print-status-summary o s))))
(:method ((o test-result) (s symbol) &aux (objs (funcall s o)))
(when objs
(format *test-stream* "~A (~D)~@:_" s (len objs))
(iter (for o in (typecase objs
(list objs)
(list-collector (head objs))))
(%print-summary o))
(format *test-stream* "~@:_"))
))