Newer
Older
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
;;; -*- Lisp -*-
;;; By Dan Weinreb, Oct 1, 2006. Public domain, entirely free, etc.
#|
Daniel Weinreb
dlw@alum.mit.edu
http://danweinreb.org/blog/
http://ilc2009.scheming.org/
|#
(defparameter *all-tests* nil)
(defclass test ()
((system-name :initarg :system-name :reader test-system-name)
(operation-name :initarg :operation-name :reader test-operation-name)
(already-compiled :initarg :already-compiled :reader test-already-compiled)
(expected :initarg :expected :reader test-expected)))
(defmacro define-test (test-name system-name
&key operation-name already-compiled expected)
`(progn
(push ',test-name *all-tests*)
(setf (get ',test-name 'test)
(make-instance 'test
:system-name ',system-name
:operation-name ',operation-name
:already-compiled ',already-compiled
:expected ',expected))))
(defclass test-file (asdf:component) ())
(defvar *test* nil)
(defvar *steps* nil)
(defmethod asdf:operation-done-p ((o asdf:compile-op) (c test-file))
(declare (ignorable o))
(member (asdf:component-name c) (test-already-compiled *test*)
:test #'string=))
(defmethod asdf:operation-done-p ((o asdf:operation) (c test-file))
(declare (ignorable o c))
nil)
(defmethod asdf:perform ((o asdf:compile-op) (c test-file))
(declare (ignorable o))
(push (cons :compiled (asdf:component-name c)) *steps*))
(defmethod asdf:perform ((o asdf:load-op) (c test-file))
(declare (ignorable o))
(push (cons :loaded (asdf:component-name c)) *steps*))
(defun run-unit-test (test-name)
(let ((*test* (get test-name 'test))
(*steps* nil)
(succeeded t))
(flet ((fail (format-string &rest format-args)
(setq succeeded nil)
(format t "Error in test ~S: " test-name)
(apply #'format t format-string format-args)
(terpri)))
(let ((system-name (test-system-name *test*))
(operation-name (test-operation-name *test*)))
(check-type system-name symbol)
(check-type operation-name symbol)
(asdf:operate operation-name system-name))
(setq *steps* (nreverse *steps*))
(loop for steps on *steps* do
(when (member (first steps) (rest steps) :test #'equal)
(fail "The step ~S happened more than once: ~S"
(first steps) *steps*)))
;(format t "~2%STEPS: ~S~3%" *steps*)
(dolist (expectation (test-expected *test*))
(destructuring-bind (op file &rest at)
expectation
;(format t "~2%Expectation: ~S~3%" expectation)
(check-type file string)
(ecase op
((:compiled :loaded)
(let ((pos (position (cons op file) *steps* :test #'equal)))
(if (null pos)
(fail "~S was not ~A" file op)
(loop for (relationship file2) on at by #'cddr do
(check-type file2 string)
(let* ((op2 (ecase relationship
(:after-loading :loaded)
(:after-compiling :compiled)))
(pos2 (position (cons op2 file2) *steps*
:test #'equal)))
(cond ((null pos2)
(fail "~S was not ~A at all, after ~A was ~A"
file2 op2 file op))
((< pos pos2)
(fail "Wrong order between ~A of ~S and ~A of ~S"
op file op2 file2))))))))
(:did-not-compile
(when (member (cons :compiled file) *steps*)
(fail "~A compiled but should not have" file)))
(:did-not-load
(when (member (cons :loaded file) *steps*)
(fail "~A loaded but should not have" file)))))))
succeeded))
(defun run-all-unit-tests ()
(let ((n-tests 0)
(n-succeeded 0))
(dolist (test-name *all-tests*)
(incf n-tests)
(when (run-unit-test test-name)
(incf n-succeeded)))
(format t "Summary: ~D of ~D tests succeeded." n-succeeded n-tests)))
(asdf:defsystem system-1
:components ((:test-file "a")
(:test-file "b")
(:test-file "c" :depends-on ("b"))
(:test-file "d")))
(define-test test-1 system-1
:operation-name asdf:load-op
:already-compiled ("b")
:expected ((:compiled "a")
(:loaded "a" :after-compiling "a")
(:did-not-compile "b")
(:loaded "b")
(:compiled "c" :after-loading "b")
(:loaded "c" :after-loading "b" :after-compiling "c")
(:compiled "d")
(:loaded "d" :after-compiling "d")))
(asdf:defsystem system-2
:components ((:test-file "a" :depends-on ("g" "k"))
(:test-file "b")
(:test-file "c" :depends-on ("b"))
(:test-file "d" :depends-on ("e"))
(:test-file "e")
(:test-file "f" :depends-on ("c"))
(:test-file "g" :depends-on ("h"))
(:test-file "h")
(:test-file "i" :depends-on ("f" "b"))
(:test-file "j" :depends-on ("f" "c"))
(:test-file "k")
(:test-file "l" :depends-on ("d"))))
(define-test test-2 system-2
:operation-name asdf:load-op
:already-compiled ()
:expected ((:compiled "a" :after-compiling "g" :after-compiling "k"
:after-compiling "h" :after-loading "g"
:after-loading "k" :after-loading "h")
(:loaded "a" :after-compiling "a")
(:compiled "b")
(:loaded "b" :after-compiling "b")
(:compiled "c" :after-compiling "b" :after-loading "b")
(:loaded "c" :after-compiling "b" :after-loading "b"
:after-compiling "c")
(:compiled "d" :after-compiling "e")
(:compiled "e")
(:compiled "f" :after-compiling "c" :after-compiling "b")
(:compiled "g" :after-compiling "h")
(:compiled "h")
(:compiled "i" :after-compiling "f" :after-compiling "b")
(:compiled "j" :after-compiling "f" :after-compiling "b"
:after-compiling "c")
(:compiled "i" :after-compiling "f" :after-compiling "b"
:after-compiling "c")
(:compiled "j" :after-compiling "f" :after-compiling "b"
:after-compiling "c")
(:compiled "k")
(:compiled "l" :after-compiling "d" :after-compiling "e")))
(asdf:defsystem system-3
:components ((:test-file "a")
(:test-file "b" :depends-on ("a"))))
(define-test test-3 system-3
:operation-name asdf:compile-op
:already-compiled ("b")
:expected ((:compiled "a")
(:loaded "a" :after-compiling "a")
(:compiled "b" :after-compiling "a" :after-loading "a")
(:did-not-load "b")))
(define-test test-3-a system-3
:operation-name asdf:compile-op
:already-compiled ("a" "b")
:expected ((:did-not-compile "a")
(:did-not-load "a")
(:did-not-compile "b")
(:did-not-load "b")))
(define-test test-3-b system-3
:operation-name asdf:compile-op
:already-compiled ()
:expected ((:compiled "a")
(:loaded "a" :after-compiling "a")
(:compiled "b" :after-compiling "a" :after-loading "a")
(:did-not-load "b")))
(asdf:defsystem system-4
:components ((:test-file "a")
(:test-file "b" :depends-on ("a"))))
(define-test test-4 system-4
:operation-name asdf:compile-op
:already-compiled ("a" "b")
:expected ((:did-not-compile "a")
(:did-not-load "a")
(:did-not-compile "b")
(:did-not-load "b")))
(asdf:defsystem system-5
:components ((:test-file "a")
(:test-file "b" :depends-on ("a")
:in-order-to ((asdf:compile-op
(asdf:load-op "a"))))))
(define-test test-5 system-5
:operation-name asdf:compile-op
:already-compiled ("a" "b")
:expected ((:did-not-compile "a")
(:loaded "a")
(:did-not-compile "b")
(:did-not-load "b")))
(asdf:defsystem system-6
:components ((:test-file "a")
(:test-file "b"
:in-order-to ((asdf:compile-op (asdf:compile-op "a"))
(asdf:load-op (asdf:load-op "a")))
:do-first ((asdf:compile-op (asdf:load-op "a"))))))
(define-test test-6 system-6
:operation-name asdf:compile-op
:already-compiled ("a" "b")
:expected ((:did-not-compile "a")
(:did-not-load "a")
(:did-not-compile "b")
(:did-not-load "b")))
;; Should be just like test-3-b, but a does not get loaded. Why not?
;; Because you can't really specify :do-first because it clobbers
;; the :do-first that you provide!
(define-test test-6-a system-6
:operation-name asdf:compile-op
:already-compiled ()
:expected ((:compiled "a")
(:loaded "a" :after-compiling "a")
(:compiled "b" :after-compiling "a" :after-loading "a")
(:did-not-load "b")))
(asdf:defsystem system-7
:components ((:test-file "a")
(:test-file "b"
:in-order-to ((asdf:compile-op (asdf:compile-op "a"))
(asdf:load-op (asdf:load-op "a"))
(asdf:compile-op (asdf:load-op "a"))))))
(define-test test-7 system-7
:operation-name asdf:compile-op
:already-compiled ("a" "b")
:expected ((:did-not-compile "a")
(:loaded "a")
(:did-not-compile "b")
(:did-not-load "b")))