-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassertions.scm
123 lines (110 loc) · 4.45 KB
/
assertions.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
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
;;; ----------------------------------------------------------------------
;;; Copyright 2007-2009 Alexey Radul.
;;; ----------------------------------------------------------------------
;;; This file is part of Test Manager.
;;;
;;; Test Manager is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Test Manager is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Test Manager. If not, see <http://www.gnu.org/licenses/>.
;;; ----------------------------------------------------------------------
; When compiling for MIT Scheme
; (declare (usual-integrations force promise?))
; to interoperate with sensible implementations of iterative forcing.
(define (ensure-forced object)
(if (promise? object)
(force object)
object))
(define (instantiate-template template arguments)
(if (not (= (length arguments) (- (length template) 1)))
(error "Template and argument lists are length-mismatched: "
template arguments))
(let loop ((result (car template))
(template (cdr template))
(arguments arguments))
(if (null? template)
result
(loop (string-append result (car arguments) (car template))
(cdr template)
(cdr arguments)))))
(define (messagify object)
(with-output-to-string (lambda () (display object))))
(define (build-message header template . arguments)
(delay
(let ((body (instantiate-template template (map messagify arguments))))
(if header
(string-append (messagify (ensure-forced header)) "\n" body)
(string-append "\n" body)))))
(define (assert-proc message proc)
(if (proc)
'ok
(test-fail (messagify (ensure-forced message)))))
(define (assert-equivalent predicate . opt-pred-name)
(define (full-message message expected actual)
(if (null? opt-pred-name)
(build-message message
'("<" "> expected but was\n<" ">.")
expected actual)
(build-message message
'("<" "> expected to be " " to\n<"
">.")
expected (car opt-pred-name) actual)))
(lambda (expected actual . opt-message)
(let-optional
opt-message ((message #f))
(assert-proc (full-message message expected actual)
(lambda () (predicate expected actual))))))
(define assert-eq (assert-equivalent eq? "eq?"))
(define assert-eqv (assert-equivalent eqv? "eqv?"))
(define assert-equal (assert-equivalent equal? "equal?"))
(define assert-= (assert-equivalent = "="))
(define assert-equals assert-equal)
(define assert= assert-=)
(define assert-< (assert-equivalent < "<"))
(define assert-> (assert-equivalent > ">"))
(define assert-<= (assert-equivalent <= "<="))
(define assert->= (assert-equivalent >= ">="))
(define (assert-in-delta expected actual delta . opt-message)
(let-optional opt-message ((message #f))
(let ((full-message
(build-message message '("<" "> and\n<" "> expected to be within\n<"
"> of each other.")
expected actual delta)))
(assert-proc full-message (lambda () (<= (abs (- expected actual)) delta))))))
(define (assert-matches regexp string . opt-message)
(let-optional opt-message ((message #f))
(let ((full-message
(build-message message '("<" "> expected to match <" ">")
string regexp)))
(assert-proc full-message
(lambda ()
(generic-match regexp string))))))
;; TODO how repetitive!
(define (assert-no-match regexp string . opt-message)
(let-optional opt-message ((message #f))
(let ((full-message
(build-message message '("<" "> expected not to match <" ">")
string regexp)))
(assert-proc full-message
(lambda ()
(not (generic-match regexp string)))))))
(define (assert-true thing . opt-message)
(let-optional opt-message ((message #f))
(let ((full-message
(build-message message '("<" "> expected to be a true value.")
thing)))
(assert-proc full-message (lambda () thing)))))
(define (assert-false thing . opt-message)
(let-optional opt-message ((message #f))
(let ((full-message
(build-message message '("<" "> expected to be a false value.")
thing)))
(assert-proc full-message (lambda () (not thing))))))