/
assert.scm
57 lines (45 loc) · 1.39 KB
/
assert.scm
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
;;;
;;; Unit test framework for Scheme
;;; Copyright (c) 2017, Peter Kofler, http://www.code-cop.org/
;;; BSD licensed.
;;;
;;; Non S5RS used functions
;;; * error from R6RS
;;; * with-exception-catcher from Gambit
;;;
;; SchemeUnit from http://c2.com/cgi/wiki?SchemeUnit
(define (report-error msg)
(error (string-append "AssertionError" ": " msg)))
(define (check msg condition)
(if (not condition)
(report-error msg)
#t))
(define (assert msg condition)
(lambda () (check msg condition)))
;; extensions
(define (assert-generic-equal to-string eq-op expected actual)
(assert
(string-append "expected:<" (to-string expected) "> but was:<" (to-string actual) ">")
(eq-op expected actual)))
(define (assert= expected actual)
(assert-generic-equal number->string = expected actual))
(define (assert-raise expected-exc body)
(lambda ()
(with-exception-catcher
(lambda (exc)
(check (string-append "Should raise " (symbol->string expected-exc))
(eq? expected-exc exc)))
(lambda () (report-error (body))))))
(define (test-case name . assertions)
(display name)
(newline)
(for-each (lambda (a) (a)) assertions)
(display "OK")
(newline))
;; self test
(test-case "assert="
(assert= 1 1))
(test-case "assert-raise"
(assert-raise
'a
(lambda () (raise 'a))))