-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.lisp
280 lines (229 loc) · 7.45 KB
/
core.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
(uiop:define-package :fiveam-matchers/core
(:use #:cl
#:alexandria)
(:export
#:equal-to
#:is-not
#:matcher
#:assert-that
#:describe-self
#:describe-mismatch
#:single-value-matcher
#:self-describing-list
#:has-all
#:has-any
#:has-typep
#:ensure-matcher
#:matchesp
#:is-equal-to
#:signals-error-matching
#:error-with-string-matching))
(in-package :fiveam-matchers/core)
(defclass matcher ()
())
(defun render-description (description output)
(loop for x in description
do
(etypecase x
(string
(format output "~a" description))
(list
(render-description x output)))))
(defgeneric matchesp (matcher value))
(defgeneric describe-mismatch (matcher value))
(defmethod describe-self ((matcher matcher))
`(" matches a matcher of type " ,(type-of matcher)))
;; core matchers
(defclass single-value-matcher (matcher)
((value :initarg :value
:reader value)))
(defclass equal-to (single-value-matcher)
())
(defun equal-to (val)
(make-instance 'equal-to :value val))
(defun is-equal-to (val)
"Synonym for equal-to"
(equal-to val))
(defun ensure-matcher (val)
(typecase val
(matcher val)
(t (equal-to val))))
(defmethod matchesp ((matcher equal-to) value)
(equal (value matcher) value))
(defmethod describe-mismatch ((matcher equal-to) value)
`("was " ,(esc value)))
(defmethod describe-mismatch (matcher value)
`("No description of mismatch available, see expression "))
(defmethod describe-self ((matcher equal-to))
(esc (value matcher)))
(defclass is-not (matcher)
((value :initarg :value
:reader value)))
(defun is-not (value)
(check-type value matcher)
(make-instance 'is-not :value value))
(defun does-not (value)
;; synonym
(is-not value))
(defmethod matchesp ((matcher matcher) value)
;; This allows for specializing of matchesp on the value method
nil)
(defmethod matchesp ((is-not is-not) value)
(not (matchesp (value is-not) value)))
(defmethod describe-self ((is-not is-not))
`("not " ,(describe-self (value is-not))))
(defmethod describe-mismatch ((is-not is-not) value)
`("not " ,(describe-mismatch (value is-not) value)))
;; finally, an assert-that function
(defclass escaped ()
((value :initarg :value
:reader value)))
(defun esc (x)
(make-instance 'escaped :value x))
(defclass self-describing-list ()
((value :initarg :value
:reader value)
(start :initarg :start
:initform "["
:reader start)
(end :initarg :end
:initform "]"
:reader end)
(sep :initarg :sep
:reader sep
:initform ", ")))
(defun self-describing-list (list &rest args)
(apply 'make-instance 'self-describing-list
:value list
args))
(defun format-description (description stream)
(etypecase description
(list
(loop for x in description do
(format-description x stream)))
(escaped
(format stream "~S" (value description)))
(self-describing-list
(format stream "~a" (start description))
(loop for (x . next) on (value description)
do
(progn
(format-description (describe-self x)
stream)
(when next
(format stream "~a" (sep description)))))
(format stream "~a" (end description)))
(t
(format stream "~a" description))))
(defclass has-all (matcher)
((matchers :initarg :matchers
:reader matchers)))
(defclass has-any (matcher)
((matchers :initarg :matchers
:reader matchers)))
(defmethod matchesp ((has-all has-all) value)
(not
(loop for matcher in (matchers has-all)
unless (matchesp matcher value)
return t)))
(defmethod describe-mismatch ((has-all has-all) value)
(loop for matcher in (matchers has-all)
unless (matchesp matcher value)
return (describe-mismatch matcher value)
finally
(return "no mismatch")))
(defmethod matchesp ((has-any has-any) value)
(loop for matcher in (matchers has-any)
if (matchesp matcher value)
return t))
(defun check-matcher-list (matchers)
(dolist (x matchers)
(check-type x matcher)))
(defun has-all (&rest matchers)
(check-matcher-list matchers)
(make-instance 'has-all :matchers matchers))
(defun has-any (&rest matchers)
(check-matcher-list matchers)
(make-instance 'has-any :matchers matchers))
(defclass has-typep (single-value-matcher)
())
(defun has-typep (type)
(make-instance 'has-typep :value type))
(defmethod matchesp ((matcher has-typep) value)
(typep value (value matcher)))
(defmethod describe-self ((matcher has-typep))
`("has type " ,(value matcher)))
(Defmethod describe-mismatch ((matcher has-typep) value)
`("has type " ,(type-of value)))
(defmethod describe-mismatch-to-string (matcher value)
(format
nil
"Expected: ~a
but: ~a"
(with-output-to-string (s)
(format-description (describe-self matcher) s))
(with-output-to-string (s)
(format-description (describe-mismatch matcher value) s))))
(defun call-assert-that (value matcher expression match-expression)
(declare (optimize (debug 3) (speed 0))) ;; Keep everything in the stacktrace!
(cond
((matchesp matcher value)
(fiveam::add-result 'fiveam::test-passed
:test-expr `(assert-that
,expression
,match-expression)))
(t
(fiveam::process-failure
`(assert-that
,expression
,match-expression)
"~a"
(describe-mismatch-to-string
matcher value)))))
(defmacro assert-that (value &rest matchers)
(alexandria:with-gensyms (value-sym)
`(let ((,value-sym ,value))
,@(loop for matcher in matchers collect
`(call-assert-that
,value-sym
,matcher
',value
',matcher)))))
(defclass error-with-string-matching (matcher)
((delegate :initarg :delegate
:reader delegate)))
(defmethod matchesp ((matcher error-with-string-matching) value)
(and
(typep value 'error)
(matchesp
(delegate matcher)
(princ-to-string value))))
(defmethod describe-mismatch-to-string ((matcher error-with-string-matching) value)
(cond
((not (typep value 'error))
(format nil "Wasn't provided an error type"))
(t
(format nil "Expected error with message to match: ~a"
(describe-mismatch-to-string (delegate matcher) (princ-to-string value))))))
(defmethod describe-self ((matcher error-with-string-matching))
(format nil "An error with message: ~a"
(describe-self (delegate matcher))))
(defmethod error-with-string-matching (matcher)
(make-instance 'error-with-string-matching :delegate matcher))
(defmethod error-with-string-matching ((matcher string))
(error-with-string-matching (equal-to matcher)))
(defmacro signals-error-matching ((&optional (error-class 'simple-error)) expr &rest matchers)
"Checks if a the expr signaled an error whose string representation
matches the given matchers. Useful for asserting SIMPLE-ERRORs."
`(progn
(let ((no-error nil))
(handler-case
(progn
,expr
(setf no-error t))
(,error-class (e)
(assert-that
e
,@matchers)))
(if no-error
(fiveam:fail "Expected to see an exception when running ~a, but didn't see any" ',expr)))))