| 1 |
;;; |
| 2 |
;;; SRFI-78 - Lightweight testing |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2020-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
;; If SRFI-78 is used within gauche.test framework, we integrate |
| 35 |
;; it into gauche.test---that is, check works as a wrapper of test*. |
| 36 |
;; Otherwise, check bookkeeps its own testing. |
| 37 |
|
| 38 |
(define-module srfi.78 |
| 39 |
(use gauche.test) |
| 40 |
(use srfi.42) |
| 41 |
(use util.match) |
| 42 |
(export check check-ec check-report check-set-mode! check-reset! |
| 43 |
check-passed?)) |
| 44 |
(select-module srfi.78) |
| 45 |
|
| 46 |
(define check-mode (make-parameter 'report)) |
| 47 |
|
| 48 |
;; API |
| 49 |
;; NB: If SRFI-78 runs within gauche.test, it doesn't do any reporting |
| 50 |
;; by itself. |
| 51 |
(define (check-set-mode! mode) |
| 52 |
(ecase mode |
| 53 |
[(off summary report-failed report) (check-mode mode)])) |
| 54 |
|
| 55 |
;; Track the test results |
| 56 |
(define-class <check-results> () |
| 57 |
((total-count :init-value 0) |
| 58 |
(pass-count :init-value 0) |
| 59 |
(first-failure :init-value #f) ; (name expected result aux) |
| 60 |
)) |
| 61 |
|
| 62 |
(define (reset-results! results) |
| 63 |
(set! (~ results'total-count) 0) |
| 64 |
(set! (~ results'pass-count) 0) |
| 65 |
(set! (~ results'first-failure) #f)) |
| 66 |
|
| 67 |
(define *global-results* (make <check-results>)) |
| 68 |
|
| 69 |
(define (register-failure! results name expected result aux) |
| 70 |
(unless (~ results'first-failure) |
| 71 |
(set! (~ results'first-failure) |
| 72 |
(list name expected result aux)))) |
| 73 |
|
| 74 |
;; API |
| 75 |
(define (check-reset!) (reset-results! *global-results*)) |
| 76 |
|
| 77 |
(define ((%check-hook results) verdict name expected result) |
| 78 |
(inc! (~ results'total-count)) |
| 79 |
(case verdict |
| 80 |
[(pass) (inc! (~ results'pass-count))] |
| 81 |
[(fail) (register-failure! results name expected result '())])) |
| 82 |
|
| 83 |
(define (do-check results name expected thunk eqproc aux) |
| 84 |
(if (test-running?) |
| 85 |
;; delegate to gauche.test |
| 86 |
(test name expected thunk eqproc #f (%check-hook results)) |
| 87 |
(guard ([e (else |
| 88 |
(register-failure! results name expected e aux))]) |
| 89 |
(inc! (~ results'total-count)) |
| 90 |
(let1 this-result (thunk) |
| 91 |
(if (eqproc expected this-result) |
| 92 |
;; NB: if results is not *global-results*, the error message is |
| 93 |
;; handled by the caller. See check-ec. |
| 94 |
(begin |
| 95 |
(when (and (eq? results *global-results*) |
| 96 |
(eq? (check-mode) 'report)) |
| 97 |
(format #t "Checking ~s, expecting ~s => ok\n" |
| 98 |
name expected)) |
| 99 |
(inc! (~ results'pass-count))) |
| 100 |
(begin |
| 101 |
(when (and (eq? results *global-results*) |
| 102 |
(memq (check-mode) '(report report-failed))) |
| 103 |
(format #t "Checking ~s, expecting ~s => ERROR: got ~s\n" |
| 104 |
name expected this-result)) |
| 105 |
(register-failure! results name expected this-result aux))))))) |
| 106 |
|
| 107 |
;; API |
| 108 |
(define-syntax check |
| 109 |
(syntax-rules (=>) |
| 110 |
[(_ expr (=> eqproc) expected) |
| 111 |
(unless (eq? (check-mode) 'off) |
| 112 |
(do-check *global-results* 'expr expected (^[] expr) eqproc '()))] |
| 113 |
[(_ expr => expected) |
| 114 |
(check expr (=> equal?) expected)])) |
| 115 |
|
| 116 |
;; API |
| 117 |
(define (check-passed? expected-num-passed) |
| 118 |
(and (not (~ *global-results*'first-failure)) |
| 119 |
(= expected-num-passed (~ *global-results*'pass-count)))) |
| 120 |
|
| 121 |
;; format the argument part of check-ec |
| 122 |
(define (%format-aux aux) |
| 123 |
(if (null? aux) |
| 124 |
"" |
| 125 |
(string-append ", with " |
| 126 |
(string-join (map (^p (format "~s: ~s" (car p) (cdr p))) |
| 127 |
aux) |
| 128 |
", ")))) |
| 129 |
|
| 130 |
;; API |
| 131 |
;; This only reports in standalone-mode |
| 132 |
(define (check-report) |
| 133 |
(unless (or (test-running?) (eq? (check-mode) 'off)) |
| 134 |
(match (~ *global-results*'first-failure) |
| 135 |
[(name expected result aux) |
| 136 |
(format #t "Passed ~d tests out of ~d tests. First failure on ~s, \ |
| 137 |
expected: ~s, result: ~s~a\n" |
| 138 |
(~ *global-results*'pass-count) |
| 139 |
(~ *global-results*'total-count) |
| 140 |
name expected result |
| 141 |
(%format-aux aux))] |
| 142 |
[_ |
| 143 |
(format #t "All ~d tests passed.\n" |
| 144 |
(~ *global-results*'pass-count))]))) |
| 145 |
|
| 146 |
;; API |
| 147 |
(define-syntax check-ec |
| 148 |
(syntax-rules (=>) |
| 149 |
[(_ q ... expr (=> eqproc) expected (arg ...)) |
| 150 |
(let ((results (make <check-results>))) |
| 151 |
(do-ec q ... |
| 152 |
(do-check results 'expr expected (^[] expr) eqproc |
| 153 |
(list (cons 'arg arg) ...))) |
| 154 |
(inc! (~ *global-results*'total-count)) |
| 155 |
(match (~ results'first-failure) |
| 156 |
[(and (name expected result aux) failure) |
| 157 |
(when (and (not (test-running?)) |
| 158 |
(memq (check-mode) '(report report-failed))) |
| 159 |
(format #t "Checking ~s, expecting ~s => ERROR: got ~s~a\n" |
| 160 |
name expected result |
| 161 |
(%format-aux aux))) |
| 162 |
(apply register-failure! *global-results* failure)] |
| 163 |
[_ |
| 164 |
(when (and (not (test-running?)) |
| 165 |
(eq? (check-mode) 'report)) |
| 166 |
(format #t "Checking ~s, expecting ~s => ok\n" |
| 167 |
'expr 'expected)) |
| 168 |
(inc! (~ *global-results*'pass-count))]))] |
| 169 |
[(_ q ... expr => expected (arg ...)) |
| 170 |
(check-ec q ... expr (=> equal?) expected (arg ...))] |
| 171 |
[(_ q ... expr (=> eqproc) expected) |
| 172 |
(check-ec q ... expr (=> exproc) expected ())] |
| 173 |
[(_ q ... expr => expected) |
| 174 |
(check-ec q ... expr (=> equal?) expected ())])) |