forked from OdonataResearchLLC/lisp-unit
-
Notifications
You must be signed in to change notification settings - Fork 6
/
asserts.lisp
319 lines (267 loc) · 12.3 KB
/
asserts.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
(in-package :lisp-unit2)
(cl-interpol:enable-interpol-syntax)
(define-condition assertion-pass (condition)
((unit-test :accessor unit-test :initarg :unit-test :initform *unit-test*)
(assertion :accessor assertion :initarg :assertion :initform nil)))
(define-condition assertion-fail (condition)
((unit-test :accessor unit-test :initarg :unit-test :initform *unit-test*)
(assertion :accessor assertion :initarg :assertion :initform nil)
(failure :accessor failure :initarg :failure :initform nil))
(:report (lambda (c s)
(let ((*test-stream* s))
(format *test-stream* "Failed Assertion in ~A~%"
(or (short-full-name c) "<UNIT-TEST>"))
(print-summary (failure c))))))
(defun set-equal (list1 list2 &rest initargs &key key (test #'equal))
"Return true if every element of list1 is an element of list2 and
vice versa."
(setf list1 (alexandria:ensure-list list1)
list2 (alexandria:ensure-list list2))
(and (apply #'subsetp list1 list2 initargs :test test :key key)
(apply #'subsetp list2 list1 initargs :test test :key key)))
(defun logically-equal (x y)
(or (and x y)
(and (null x) (null y))))
;;; Assert macros
(defmacro assert-eq (&whole whole expected form &rest extras)
"Assert whether expected and form are EQ."
`(expand-assert 'equal-result ,form ,form ,expected ,extras
:test #'eq
:full-form ',whole))
(defmacro assert-eql (&whole whole expected form &rest extras)
"Assert whether expected and form are EQL."
`(expand-assert 'equal-result ,form ,form ,expected ,extras :test #'eql
:full-form ',whole))
(defmacro assert-equal (&whole whole expected form &rest extras)
"Assert whether expected and form are EQUAL."
`(expand-assert 'equal-result ,form ,form ,expected ,extras :test #'equal
:full-form ',whole))
(defmacro assert-equalp (&whole whole expected form &rest extras)
"Assert whether expected and form are EQUALP."
`(expand-assert 'equal-result ,form ,form ,expected ,extras :test #'equalp
:full-form ',whole))
(defmacro assert-string= (&whole whole expected form &rest extras)
"Assert whether expected and form are STRING=."
`(expand-assert 'equal-result ,form ,form ,expected ,extras :test #'string=
:full-form ',whole))
(defmacro assert-string-equal (&whole whole expected form &rest extras)
"Assert whether expected and form are STRING-EQUAL."
`(expand-assert 'equal-result ,form ,form ,expected ,extras :test #'string-equal
:full-form ',whole))
(defmacro assert-string/= (&whole whole expected form &rest extras)
"Assert whether expected and form are STRING/=."
`(expand-assert 'equal-result ,form ,form ,expected ,extras :test #'string/=
:full-form ',whole))
(defmacro assert-string-not-equal (&whole whole expected form &rest extras)
"Assert whether expected and form are STRING-NOT-EQUAL."
`(expand-assert 'equal-result ,form ,form ,expected ,extras :test #'string-not-equal
:full-form ',whole))
(defmacro assert-char= (&whole whole expected form &rest extras)
"Assert whether expected and form are CHAR=."
`(expand-assert 'equal-result ,form ,form ,expected ,extras :test #'char=
:full-form ',whole))
(defmacro assert-char-equal (&whole whole expected form &rest extras)
"Assert whether expected and form are CHAR-EQUAL."
`(expand-assert 'equal-result ,form ,form ,expected ,extras :test #'char-equal
:full-form ',whole))
(defmacro assert-char/= (&whole whole expected form &rest extras)
"Assert whether expected and form are CHAR/=."
`(expand-assert 'equal-result ,form ,form ,expected ,extras :test #'char/=
:full-form ',whole))
(defmacro assert-char-not-equal (&whole whole expected form &rest extras)
"Assert whether expected and form are CHAR-NOT-EQUAL."
`(expand-assert 'equal-result ,form ,form ,expected ,extras :test #'char-not-equal
:full-form ',whole))
(defmacro assert= (&whole whole expected form &rest extras)
"Assert whether expected and form are =."
`(expand-assert 'equal-result ,form ,form ,expected ,extras :test #'=
:full-form ',whole))
(defmacro assert/= (&whole whole expected form &rest extras)
"Assert whether expected and form are /=."
`(expand-assert 'equal-result ,form ,form ,expected ,extras :test #'/=
:full-form ',whole))
(defmacro assert-typep (&whole whole expected-type form &rest extras)
"Assert whether expected and form are TYPEP."
`(expand-assert
'equal-result ,form ,form
,expected-type ,extras
:test #'typep
:full-form ',whole))
(defun expand-signaled-handler (whole condition form extras should-be-signaled?)
(alexandria:with-unique-names (signaled rtn)
`(let (,signaled ,rtn)
(block ,signaled
(handler-bind
((condition #'(lambda (c)
(when (typep c ,condition)
(setf ,signaled c)
(when (typep c 'warning)
(muffle-warning c))
(when (typep c 'error)
(return-from ,signaled))
))))
(setf ,rtn (multiple-value-list ,form))))
(expand-assert 'signal-result
,form ,signaled ,should-be-signaled? ,extras
:full-form ',whole)
(apply #'values ,rtn))))
(defmacro assert-signal (&whole whole condition form &rest extras)
(expand-signaled-handler whole condition form extras t))
(defmacro assert-no-signal (&whole whole condition form &rest extras)
(expand-signaled-handler whole condition form extras nil))
(defmacro assert-warning (condition form &rest extras)
`(assert-signal ,condition ,form ,@extras))
(defmacro assert-no-warning (condition form &rest extras)
`(assert-no-signal ,condition ,form ,@extras))
(defmacro assert-error (condition form &rest extras)
"Assert whether form signals condition."
`(assert-signal ,condition ,form ,@extras))
(defmacro assert-no-error (condition form &rest extras)
"Assert whether form signals condition."
`(assert-no-signal ,condition ,form ,@extras))
(defmacro assert-expands (expansion form &rest extras)
"Assert whether form expands to expansion."
`(expand-assert 'macro-result ,form (expand-macro-form ,form) ',expansion ,extras))
(defmacro assert-equality (&whole whole test expected form &rest extras)
"Assert whether expected and form are equal according to test."
`(expand-assert 'equal-result ,form ,form ,expected ,extras :test ,test
:full-form ',whole))
(defmacro assert-prints (&whole whole output form &rest extras)
"Assert whether printing the form generates the output."
`(expand-assert 'output-result ,form (expand-output-form ,form)
,output ,extras
:full-form ',whole))
(defmacro assert-true (&whole whole form &rest extras)
"Assert whether the form is true."
`(expand-assert 'equal-result ,form ,form t ,extras
:test #'(lambda (x y) (and x y))
:full-form ',whole))
(defmacro assert-false (&whole whole form &rest extras)
"Assert whether the form is false."
`(expand-assert 'equal-result ,form ,form nil ,extras
:test #'(lambda (x y) (and (not x) (not y)))
:full-form ',whole))
(defmacro expand-assert (type form body expected extras
&key (test '#'eql)
full-form)
"Expand the assertion to the internal format."
`(internal-assert ,type ',form
(lambda () ,body)
(lambda () ,expected)
(expand-extras ,extras)
,test
:full-form (or ,full-form
'(,type ,expected ,form))))
(defmacro expand-output-form (form)
"Capture the output of the form in a string."
(let ((out (gensym)))
`(let* ((,out (make-string-output-stream))
(*standard-output*
(make-broadcast-stream *standard-output* ,out)))
,form
(get-output-stream-string ,out))))
(defmacro expand-macro-form (form &optional env)
"Expand the macro form once."
`(let ((*gensym-counter* 1)) (macroexpand-1 ',form ,env)))
(defmacro expand-extras (extras)
"Expand extra forms."
`(lambda ()
(list ,@(mapcan (lambda (form) (list `',form form)) extras))))
(defgeneric record-failure (type form actual expected extras test)
(:documentation
"Record the details of the failure.")
(:method (type form actual expected extras test)
(make-instance
type
:form form :actual actual :expected expected
:extras extras :test test)))
(defclass failure-result ()
((unit-test :accessor unit-test :initarg :unit-test :initform *unit-test*)
(form :accessor form :initarg :form :initform nil)
(actual :accessor actual :initarg :actual :initform nil)
(expected :accessor expected :initarg :expected :initform nil)
(extras :accessor extras :initarg :extras :initform nil)
(test :accessor test :initarg :test :initform nil))
(:documentation
"Failure details of the assertion."))
(defclass equal-result (failure-result) ()
(:documentation "Result of a failed equal assertion."))
(defclass error-result (failure-result) ()
(:documentation "Result of a failed error assertion."))
(defclass signal-result (failure-result) ()
(:documentation "Result of a failed warning assertion."))
(defclass macro-result (failure-result) ()
(:documentation "Result of a failed macro expansion assertion."))
(defclass output-result (failure-result) ()
(:documentation "Result of a failed output assertion."))
(defun %form-equal (form1 form2 &aux (invalid `(/= ,form1 ,form2)))
"Descend into the forms checking for equality.
The first unequal part is the second value"
(typecase form1
;; symbols should be name equal (gensyms)
(symbol
(or (eql form1 form2)
;; two gensyms, one is as good as the other?
(and (null (symbol-package form1))
(null (symbol-package form2))
(string= (symbol-name form1) (symbol-name form2)))
(values nil `(/= ,form1 ,form2))))
;; lists need to match by recursion
(list
(multiple-value-bind (res inv)
(ignore-errors (%form-equal (first form1) (first form2)))
(if res
(if (eql (length (rest form1)) (length (rest form2)))
(%form-equal (rest form1) (rest form2))
(values nil inv invalid))
(values nil inv invalid))))
(t ;; everything else should be equal
(or (equal form1 form2)
(values nil invalid)))))
(defgeneric assert-passes? (type test expected actual)
(:documentation "Return the result of the assertion.")
(:method (type test expected actual)
(ecase type
((equal-result failure-result)
(and
(<= (length expected) (length actual))
;; by putting expected in the second position we open up the ability
;; to use many more functions as tests (eg: typep)
(every test actual expected)))
(signal-result
;; These are lists of booleans
(logically-equal (first expected) (first actual)))
(error-result
(or
;; todo: whats with eql?
(eql (car actual) (car expected))
(typep (car actual) (car expected))))
(macro-result
(%form-equal (first expected) (first actual)))
(output-result
(string=
(string-trim '(#\newline #\return #\space) (car actual))
(string-trim '(#\newline #\return #\space) (car expected)))))))
(defun internal-assert
(type form code-thunk expected-thunk extras test &key full-form)
"Perform the assertion and record the results."
(let* ((actual (multiple-value-list (funcall code-thunk)))
(expected (multiple-value-list (funcall expected-thunk)))
(result (assert-passes? type test expected actual)))
(with-simple-restart (abort "Cancel this assertion")
(if result
(signal 'assertion-pass :assertion (or full-form form))
(signal 'assertion-fail
:assertion (or full-form form)
:failure (record-failure
type full-form actual expected
(when extras (funcall extras)) test))))
;; Return the actual-values
(apply #'values actual)))
(defun with-failure-debugging-context (body-fn)
"A context that invokes the debugger on failed assertions"
(handler-bind ((assertion-fail #'invoke-debugger))
(funcall body-fn)))
(defmacro with-failure-debugging (() &body body)
"A context macro that invokes the debugger on failed assertions"
`(with-failure-debugging-context (lambda () ,@body)))