-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathassertions.dylan
596 lines (533 loc) · 22.3 KB
/
assertions.dylan
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
Module: %testworks
Synopsis: Testworks testing harness
Author: Andrew Armstrong, James Kirsch
Copyright: Original Code is Copyright (c) 1995-2004 Functional Objects, Inc.
All rights reserved.
License: See License.txt in this distribution for details.
Warranty: Distributed WITHOUT WARRANTY OF ANY KIND
define constant $invalid-description = "*** invalid description ***";
// This is used to do a non-local exit to the end of a test and skip remaining assertions.
define class <assertion-failure> (<condition>) end;
/// Assertion macros
// The check-* macros are non-terminating, require the caller to provide a
// name, and are DEPRECATED. Use expect-* instead in new code.
//
// The expect-* macros are non-terminating and auto-generate a description if
// none is provided.
//
// The assert-* macros are terminating (they cause the remainder of a test to
// be skipped when they fail) and they auto-generate a description by default.
// Note that these macros wrap up the real macro arguments inside
// methods to delay their evaluation until they are within the scope
// of whatever condition handling is required.
define function eval-check-description
(thunk :: <function>) => (description :: <string>)
let (description, #rest args) = thunk();
if (empty?(args))
format-to-string("%s", description)
else
apply(format-to-string, description, args)
end
end function;
// Deprecated; use expect.
define macro check
{ check(?description:expression, ?fun:expression, ?args:*) }
=> { expect(?fun(?args), ?description) }
end macro;
define macro expect
{ expect(?expr:expression) }
=> { expect(?expr, ?"expr" " is true") }
{ expect(?expr:expression, ?description:*) }
=> { do-check-true(method () values(?description) end,
method () values(?expr, ?"expr") end,
"expect",
terminate?: #f)
}
end macro;
// This is for symmetry with expect-false. Usually expect is preferred.
define macro expect-true
{ expect-true(?expr:expression) }
=> { expect-true(?expr, ?"expr" " is true") }
{ expect-true(?expr:expression, ?description:*) }
=> { do-check-true(method () values(?description) end,
method () values(?expr, ?"expr") end,
"expect",
terminate?: #f)
}
end macro;
// Deprecated; use expect-equal.
define macro check-equal
{ check-equal(?description:expression, ?want:expression, ?got:expression) }
=> { expect-equal(?want, ?got, ?description) }
end macro;
define macro assert-equal
{ assert-equal (?want:expression, ?got:expression)
} => {
assert-equal(?want, ?got, ?"want" " = " ?"got")
}
{ assert-equal (?want:expression, ?got:expression, ?description:*)
} => {
do-check-equal(method () values(?description) end,
method ()
values(?want, ?got, ?"want", ?"got")
end,
"assert-equal",
terminate?: #t)
}
end macro assert-equal;
define macro expect-equal
{ expect-equal(?want:expression, ?got:expression) }
=> { expect-equal(?want, ?got, ?"want" " = " ?"got") }
{ expect-equal(?want:expression, ?got:expression, ?description:*) }
=> { do-check-equal(method () values(?description) end,
method () values(?want, ?got, ?"want", ?"got") end,
"expect-equal",
terminate?: #f) }
end macro;
define function do-check-equal
(description-thunk :: <function>, arguments-thunk :: <function>, caller :: <string>,
#key terminate? :: <boolean>)
=> ()
let phase = format-to-string("evaluating %s description", caller);
let description :: false-or(<string>) = #f;
block ()
description := eval-check-description(description-thunk);
phase := format-to-string("evaluating %s expressions", caller);
let (want, got, want-expr, got-expr) = arguments-thunk();
phase := format-to-string("while comparing %s and %s for equality",
want-expr, got-expr);
if (want = got)
record-check(description, $passed, #f);
else
phase := format-to-string("getting %s failure detail", caller);
let detail = check-equal-failure-detail(want, got);
let detail = if (detail)
format-to-string("\n%s%sdetail: %s",
*indent*, $indent-step, detail)
else
""
end;
record-check(description, $failed,
format-to-string("want: %=\n%s%sgot: %=%s",
want, *indent*, $indent-step, got, detail));
terminate? & signal(make(<assertion-failure>));
end;
exception (err :: <serious-condition>, test: method (cond) ~debug?() end)
record-check(description | $invalid-description,
$crashed,
format-to-string("Error %s: %s", phase, err));
terminate? & signal(make(<assertion-failure>));
end block
end function;
define macro expect-not-equal
{ expect-not-equal(?expr1:expression, ?expr2:expression) }
=> { expect-not-equal(?expr1, ?expr2, ?"expr1" " ~= " ?"expr2") }
{ expect-not-equal(?expr1:expression, ?expr2:expression, ?description:*) }
=> { do-check-not-equal(method () values(?description) end,
method () values(?expr1, ?expr2, ?"expr1", ?"expr2") end,
"expect-not-equal",
terminate?: #f) }
end macro;
define macro assert-not-equal
{ assert-not-equal (?expr1:expression, ?expr2:expression)
} => {
assert-not-equal(?expr1, ?expr2, ?"expr1" " ~= " ?"expr2")
}
{ assert-not-equal (?expr1:expression, ?expr2:expression, ?description:*)
} => {
do-check-not-equal(method () values(?description) end,
method ()
values(?expr1, ?expr2, ?"expr1", ?"expr2")
end,
"assert-not-equal",
terminate?: #t)
}
end macro assert-not-equal;
define function do-check-not-equal
(description-thunk :: <function>, arguments-thunk :: <function>, caller :: <string>,
#key terminate? :: <boolean>)
=> ()
let phase = format-to-string("evaluating %s description", caller);
let description :: false-or(<string>) = #f;
block ()
description := eval-check-description(description-thunk);
phase := format-to-string("evaluating %s expressions", caller);
let (val1, val2, expr1, expr2) = arguments-thunk();
phase := format-to-string("while comparing %s and %s for inequality",
expr1, expr2);
if (val1 ~= val2)
record-check(description, $passed, #f);
else
phase := format-to-string("getting %s failure detail", caller);
record-check(description, $failed,
format-to-string("%= and %= are =.", val1, val2));
terminate? & signal(make(<assertion-failure>));
end;
exception (err :: <serious-condition>, test: method (cond) ~debug?() end)
record-check(description | $invalid-description,
$crashed,
format-to-string("Error %s: %s", phase, err));
terminate? & signal(make(<assertion-failure>));
end block
end function;
// Return a string with details about why two objects are not =.
// Users can override this for their own classes. The output should
// be indented 4 spaces if you want it to display nicely.
define open generic check-equal-failure-detail
(val1 :: <object>, val2 :: <object>) => (detail :: false-or(<string>));
define method check-equal-failure-detail
(val1 :: <object>, val2 :: <object>) => (detail :: false-or(<string>))
#f // We have no details.
end;
define method check-equal-failure-detail
(coll1 :: <collection>, coll2 :: <collection>) => (detail :: false-or(<string>))
if (coll1.size ~= coll2.size)
format-to-string("sizes differ (%d and %d)", coll1.size, coll2.size)
end
end method;
define method check-equal-failure-detail
(seq1 :: <sequence>, seq2 :: <sequence>) => (detail :: false-or(<string>))
let detail1 = next-method();
let detail2 = #f;
for (e1 in seq1, e2 in seq2, i from 0, while: e1 = e2)
finally
if (i < seq1.size & i < seq2.size)
detail2 := format-to-string("element %d is the first mismatch", i);
end;
end for;
join(choose(identity, vector(detail1, detail2)), "; ")
end method;
// TODO: limit the total number of keys/values output
define method check-equal-failure-detail
(t1 :: <table>, t2 :: <table>) => (detail :: false-or(<string>))
let detail1 = next-method();
let t1-missing-keys = make(<stretchy-vector>);
let t2-missing-keys = make(<stretchy-vector>);
for (_ keyed-by k in t2)
if (unfound?(element(t1, k, default: $unfound)))
add!(t1-missing-keys, k);
end;
end;
for (_ keyed-by k in t1)
if (unfound?(element(t2, k, default: $unfound)))
add!(t2-missing-keys, k);
end;
end;
let eformat = curry(format-to-string, "%="); // e for escape
let detail2 = (~empty?(t1-missing-keys)
& concatenate("table1 is missing keys ",
join(t1-missing-keys, ", ", key: eformat)));
let detail3 = (~empty?(t2-missing-keys)
& concatenate("table2 is missing keys ",
join(t2-missing-keys, ", ", key: eformat)));
join(choose(identity, vector(detail1, detail2, detail3)), "; ")
end method;
// Deprecated; use expect-instance?.
define macro check-instance?
{ check-instance?(?description:expression, ?type:expression, ?value:expression) }
=> { expect-instance?(?type, ?value, ?description) }
end macro;
define macro expect-instance?
{ expect-instance?(?type:expression, ?value:expression) }
=> { expect-instance?(?type, ?value, ?"value" " is an instance of " ?"type") }
{ expect-instance?(?type:expression, ?value:expression, ?description:*) }
=> { do-check-instance?(method () values(?description) end,
method () values(?type, ?value, ?"value") end,
"expect-instance?",
terminate?: #f)
}
end macro;
define macro assert-instance?
{ assert-instance? (?type:expression, ?value:expression)
} => {
assert-instance? (?type, ?value, ?"value" " is an instance of " ?"type")
}
{ assert-instance? (?type:expression, ?value:expression, ?description:*)
} => {
do-check-instance?(method () values(?description) end,
method ()
values(?type, ?value, ?"value")
end,
"assert-instance?",
negate?: #f,
terminate?: #t)
}
end macro assert-instance?;
define macro expect-not-instance?
{ expect-not-instance?(?type:expression, ?value:expression) }
=> { expect-not-instance?(?type, ?value, ?"value" " is not an instance of " ?"type") }
{ expect-not-instance?(?type:expression, ?value:expression, ?description:*) }
=> { do-check-instance?(method () values(?description) end,
method () values(?type, ?value, ?"value") end,
"expect-not-instance?",
negate?: #t,
terminate?: #f)
}
end macro;
define macro assert-not-instance?
{ assert-not-instance? (?type:expression, ?value:expression)
} => {
assert-not-instance? (?type, ?value, ?"value" " is not an instance of " ?"type")
}
{ assert-not-instance? (?type:expression, ?value:expression, ?description:*)
} => {
do-check-instance?(method () values(?description) end,
method ()
values(?type, ?value, ?"value")
end,
"assert-not-instance?",
negate?: #t,
terminate?: #t)
}
end macro assert-not-instance?;
define function do-check-instance?
(description-thunk :: <function>, get-arguments :: <function>, caller :: <string>,
#key negate? :: <boolean>,
terminate? :: <boolean>)
=> ()
let phase = format-to-string("evaluating %s description", caller);
let description :: false-or(<string>) = #f;
block ()
description := eval-check-description(description-thunk);
phase := format-to-string("evaluating %s expressions", caller);
let (type :: <type>, value, value-expr :: <string>) = get-arguments();
phase := format-to-string("checking if %= is %=an instance of %s",
value-expr, if (negate?) "not " else "" end, type);
if (instance?(value, type) ~= negate?)
record-check(description, $passed, #f);
else
record-check(description, $failed,
format-to-string("%s (from expression %=) is not an instance of %s.",
value, value-expr, type));
terminate? & signal(make(<assertion-failure>));
end;
exception (err :: <serious-condition>, test: method (cond) ~debug?() end)
record-check(description | $invalid-description,
$crashed,
format-to-string("Error %s: %s", phase, err));
terminate? & signal(make(<assertion-failure>));
end block
end function do-check-instance?;
// Deprecated; use expect.
define macro check-true
{ check-true(?description:expression, ?expr:expression) }
=> { expect(?expr, ?description) }
end macro;
define macro assert-true
{ assert-true (?expr:expression)
} => {
assert-true(?expr, ?"expr" " is true")
}
{ assert-true (?expr:expression, ?description:*)
} => {
do-check-true(method () values(?description) end,
method () values(?expr, ?"expr") end,
"assert-true",
terminate?: #t)
}
end macro assert-true;
define function do-check-true
(description-thunk :: <function>, get-arguments :: <function>, caller :: <string>,
#key terminate? :: <boolean>)
=> ()
let phase = format-to-string("evaluating %s description", caller);
let description :: false-or(<string>) = #f;
block ()
description := eval-check-description(description-thunk);
phase := format-to-string("evaluating %s expression", caller);
let (value, value-expr :: <string>) = get-arguments();
phase := format-to-string("checking if %= evaluates to true", value-expr);
if (value)
record-check(description, $passed, #f);
else
record-check(description, $failed,
format-to-string("expression %= evaluates to #f.", value-expr));
terminate? & signal(make(<assertion-failure>));
end;
exception (err :: <serious-condition>, test: method (cond) ~debug?() end)
record-check(description | $invalid-description,
$crashed,
format-to-string("Error %s: %s", phase, err));
terminate? & signal(make(<assertion-failure>));
end block
end function do-check-true;
// Deprecated; use expect-false.
define macro check-false
{ check-false(?description:expression, ?expr:expression) }
=> { expect-false(?expr, ?description) }
end macro;
define macro expect-false
{ expect-false(?expr:expression) }
=> { expect-false(?expr, ?"expr" " evaluates to #f") }
{ expect-false (?expr:expression, ?description:*) }
=> { do-check-false(method () values(?description) end,
method () values(?expr, ?"expr") end,
"expect-false",
terminate?: #f)
}
end macro;
define macro assert-false
{ assert-false (?expr:expression)
} => {
assert-false(?expr, ?"expr" " evaluates to #f")
}
{ assert-false (?expr:expression, ?description:*)
} => {
do-check-false(method () values(?description) end,
method ()
values(?expr, ?"expr")
end,
"assert-false",
terminate?: #t)
}
end macro assert-false;
define function do-check-false
(description-thunk :: <function>, get-arguments :: <function>, caller :: <string>,
#key terminate? :: <boolean>)
=> ()
let phase = format-to-string("evaluating %s description", caller);
let description :: false-or(<string>) = #f;
block ()
description := eval-check-description(description-thunk);
phase := format-to-string("evaluating %s expression", caller);
let (value, value-expr :: <string>) = get-arguments();
phase := format-to-string("checking if %= evaluates to #f", value-expr);
if (~value)
record-check(description, $passed, #f);
else
record-check(description, $failed,
format-to-string("expression %= evaluates to %=; expected #f.",
value-expr, value));
terminate? & signal(make(<assertion-failure>));
end;
exception (err :: <serious-condition>, test: method (cond) ~debug?() end)
record-check(description | $invalid-description,
$crashed,
format-to-string("Error %s: %s", phase, err));
terminate? & signal(make(<assertion-failure>));
end block
end function do-check-false;
// Deprecated; use expect-condition.
define macro check-condition
{ check-condition(?description:expression, ?condition:expression, ?expr:expression) }
=> { expect-condition(?condition, ?expr, ?description) }
end macro;
define macro expect-condition
{ expect-condition(?condition:expression, ?expr:expression) }
=> { expect-condition(?condition, ?expr, ?"expr" " signals condition " ?"condition") }
{ expect-condition(?condition:expression, ?expr:expression, ?description:*) }
=> { do-check-condition(method () values(?description) end,
method () values(?condition, method () ?expr end, ?"expr") end,
"expect-condition",
terminate?: #f) }
end macro;
// Deprecated; use assert-condition.
define macro assert-signals
{ assert-signals(?condition:expression, ?expr:expression)
} => {
assert-signals(?condition, ?expr, ?"expr" " signals condition " ?"condition")
}
{ assert-signals(?condition:expression, ?expr:expression, ?description:*)
} => {
do-check-condition(method () values(?description) end,
method ()
values(?condition, method () ?expr end, ?"expr")
end,
"assert-signals",
terminate?: #t)
}
end macro assert-signals;
define macro assert-condition
{ assert-condition(?condition:expression, ?expr:expression) }
=> { assert-condition(?condition, ?expr, ?"expr" " signals condition " ?"condition") }
{ assert-condition(?condition:expression, ?expr:expression, ?description:*) }
=> { do-check-condition(method () values(?description) end,
method () values(?condition, method () ?expr end, ?"expr") end,
"assert-condition",
terminate?: #t) }
end macro;
define function do-check-condition
(description-thunk :: <function>, get-arguments :: <function>, caller :: <string>,
#key terminate? :: <boolean>)
=> ()
let phase = format-to-string("evaluating %s description", caller);
let description :: false-or(<string>) = #f;
block ()
description := eval-check-description(description-thunk);
phase := format-to-string("evaluating %s expression", caller);
let (condition-class, thunk :: <function>, expr :: <string>) = get-arguments();
phase := format-to-string("checking if %= signals a condition of class %s",
expr, condition-class);
block ()
thunk();
record-check(description, $failed, "no condition signaled");
terminate? & signal(make(<assertion-failure>));
exception (ex :: condition-class)
record-check(description, $passed, #f);
// Not really sure if this should catch something broader, like
// <condition>, but leaving it this way for compat with old code.
exception (ex :: <serious-condition>)
record-check(description, $failed,
format-to-string("condition of class %s signaled; "
"expected a condition of class %s. "
"The error was: %s",
ex.object-class, condition-class, ex));
terminate? & signal(make(<assertion-failure>));
end;
exception (err :: <serious-condition>, test: method (cond) ~debug?() end)
record-check(description | $invalid-description,
$crashed,
format-to-string("Error %s: %s", phase, err));
terminate? & signal(make(<assertion-failure>));
end block
end function do-check-condition;
// Deprecated; use expect-no-condition.
// Same as check-no-errors, for symmetry with check-condition...
define macro check-no-condition
{ check-no-condition(?description:expression, ?expr:expression) }
=> { expect-no-condition(?expr, ?description) }
end macro;
// Deprecated; use expect-no-condition.
define macro check-no-errors
{ check-no-errors(?description:expression, ?expr:expression) }
=> { expect-no-condition(?expr, ?description) }
end macro;
define macro expect-no-condition
{ expect-no-condition(?expr:expression) }
=> { expect(begin ?expr; #t end) }
{ expect-no-condition(?expr:expression, ?description:*) }
=> { expect(begin ?expr; #t end, ?description) }
end macro;
// Deprecated; use assert-no-condition.
define macro assert-no-errors
{ assert-no-errors(?expr:expression)
} => {
assert-no-errors(?expr, ?"expr" " doesn't signal an error ")
}
{ assert-no-errors(?expr:expression, ?description:*)
} => {
assert-true(begin ?expr; #t end, ?description)
}
end macro assert-no-errors;
define macro assert-no-condition
{ assert-no-condition(?expr:expression) }
=> { assert-no-condition(?expr, ?"expr" " doesn't signal an error ") }
{ assert-no-condition(?expr:expression, ?description:*) }
=> { assert-true(begin ?expr; #t end, ?description) }
end macro;
/// Check recording
define thread variable *check-recording-function* = always(#f);
define method record-check
(name :: <string>, status :: <result-status>, reason :: false-or(<string>))
=> (status :: <result>)
if ((status = $failed | status = $crashed)
& expected-to-fail?(*component*))
// If a test is expected to fail it propagates to the test's assertions,
// otherwise they would turn the result red incorrectly.
status := $expected-failure;
end;
let result = make(<check-result>,
name: name, status: status, reason: reason);
*check-recording-function*(result);
result
end method record-check;