| 1 |
;;; |
| 2 |
;;; SRFI-64 |
| 3 |
;;; |
| 4 |
|
| 5 |
;; This file is based on SRFI-64 reference implementation, |
| 6 |
;; but modified to work cooperatively with gauche's test framework. |
| 7 |
|
| 8 |
;; Original copyright follows: |
| 9 |
|
| 10 |
;; Copyright (c) 2005, 2006, 2007, 2012, 2013 Per Bothner |
| 11 |
;; Added "full" support for Chicken, Gauche, Guile and SISC. |
| 12 |
;; Alex Shinn, Copyright (c) 2005. |
| 13 |
;; Modified for Scheme Spheres by Álvaro Castro-Castilla, Copyright (c) 2012. |
| 14 |
;; Support for Guile 2 by Mark H Weaver <[email protected]>, Copyright (c) 2014. |
| 15 |
;; |
| 16 |
;; Permission is hereby granted, free of charge, to any person |
| 17 |
;; obtaining a copy of this software and associated documentation |
| 18 |
;; files (the "Software"), to deal in the Software without |
| 19 |
;; restriction, including without limitation the rights to use, copy, |
| 20 |
;; modify, merge, publish, distribute, sublicense, and/or sell copies |
| 21 |
;; of the Software, and to permit persons to whom the Software is |
| 22 |
;; furnished to do so, subject to the following conditions: |
| 23 |
;; |
| 24 |
;; The above copyright notice and this permission notice shall be |
| 25 |
;; included in all copies or substantial portions of the Software. |
| 26 |
;; |
| 27 |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 28 |
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 29 |
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 30 |
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 31 |
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 32 |
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 33 |
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 34 |
;; SOFTWARE. |
| 35 |
|
| 36 |
;; Gauche tweak: |
| 37 |
;; |
| 38 |
;; We define a default runner, which is used when a test runner is |
| 39 |
;; implicitly created during gauche.test is active. It differs from |
| 40 |
;; simple runner that (1) it updates test success/failure counts of |
| 41 |
;; gauche.test, and (2) it sends log to stdout, to be merged to the |
| 42 |
;; gauche.test logs. |
| 43 |
|
| 44 |
(define-module srfi.64 |
| 45 |
(use gauche.record) |
| 46 |
(use gauche.test :prefix test:) |
| 47 |
(export test-begin |
| 48 |
test-end test-assert test-eqv test-eq test-equal |
| 49 |
test-approximate test-assert test-error test-apply test-with-runner |
| 50 |
test-match-nth test-match-all test-match-any test-match-name |
| 51 |
test-skip test-expect-fail test-read-eval-string |
| 52 |
test-runner-group-path test-group test-group-with-cleanup |
| 53 |
test-result-ref test-result-set! test-result-clear test-result-remove |
| 54 |
test-result-kind test-passed? |
| 55 |
|
| 56 |
;; Misc test-runner functions |
| 57 |
test-runner? test-runner-reset test-runner-null |
| 58 |
test-runner-simple test-runner-current test-runner-factory test-runner-get |
| 59 |
test-runner-create test-runner-test-name |
| 60 |
;; test-runner field setter and getter functions - see %test-record-define: |
| 61 |
test-runner-pass-count test-runner-pass-count! |
| 62 |
test-runner-fail-count test-runner-fail-count! |
| 63 |
test-runner-xpass-count test-runner-xpass-count! |
| 64 |
test-runner-xfail-count test-runner-xfail-count! |
| 65 |
test-runner-skip-count test-runner-skip-count! |
| 66 |
test-runner-group-stack test-runner-group-stack! |
| 67 |
test-runner-on-test-begin test-runner-on-test-begin! |
| 68 |
test-runner-on-test-end test-runner-on-test-end! |
| 69 |
test-runner-on-group-begin test-runner-on-group-begin! |
| 70 |
test-runner-on-group-end test-runner-on-group-end! |
| 71 |
test-runner-on-final test-runner-on-final! |
| 72 |
test-runner-on-bad-count test-runner-on-bad-count! |
| 73 |
test-runner-on-bad-end-name test-runner-on-bad-end-name! |
| 74 |
test-result-alist test-result-alist! |
| 75 |
test-runner-aux-value test-runner-aux-value! |
| 76 |
;; default/simple call-back functions, used in default test-runner, |
| 77 |
;; but can be called to construct more complex ones. |
| 78 |
test-on-group-begin-simple test-on-group-end-simple |
| 79 |
test-on-bad-count-simple test-on-bad-end-name-simple |
| 80 |
test-on-final-simple test-on-test-end-simple |
| 81 |
test-on-final-simple)) |
| 82 |
(select-module srfi.64) |
| 83 |
|
| 84 |
(define-record-type test-runner (%test-runner-alloc) test-runner? |
| 85 |
;; Cumulate count of all tests that have passed and were expected to. |
| 86 |
(pass-count test-runner-pass-count test-runner-pass-count!) |
| 87 |
(fail-count test-runner-fail-count test-runner-fail-count!) |
| 88 |
(xpass-count test-runner-xpass-count test-runner-xpass-count!) |
| 89 |
(xfail-count test-runner-xfail-count test-runner-xfail-count!) |
| 90 |
(skip-count test-runner-skip-count test-runner-skip-count!) |
| 91 |
(skip-list %test-runner-skip-list %test-runner-skip-list!) |
| 92 |
(fail-list %test-runner-fail-list %test-runner-fail-list!) |
| 93 |
;; Normally #t, except when in a test-apply. |
| 94 |
(run-list %test-runner-run-list %test-runner-run-list!) |
| 95 |
(skip-save %test-runner-skip-save %test-runner-skip-save!) |
| 96 |
(fail-save %test-runner-fail-save %test-runner-fail-save!) |
| 97 |
(group-stack test-runner-group-stack test-runner-group-stack!) |
| 98 |
(on-test-begin test-runner-on-test-begin test-runner-on-test-begin!) |
| 99 |
(on-test-end test-runner-on-test-end test-runner-on-test-end!) |
| 100 |
;; Call-back when entering a group. Takes (runner suite-name count). |
| 101 |
(on-group-begin test-runner-on-group-begin test-runner-on-group-begin!) |
| 102 |
;; Call-back when leaving a group. |
| 103 |
(on-group-end test-runner-on-group-end test-runner-on-group-end!) |
| 104 |
;; Call-back when leaving the outermost group. |
| 105 |
(on-final test-runner-on-final test-runner-on-final!) |
| 106 |
;; Call-back when expected number of tests was wrong. |
| 107 |
(on-bad-count test-runner-on-bad-count test-runner-on-bad-count!) |
| 108 |
;; Call-back when name in test=end doesn't match test-begin. |
| 109 |
(on-bad-end-name test-runner-on-bad-end-name test-runner-on-bad-end-name!) |
| 110 |
;; Cumulate count of all tests that have been done. |
| 111 |
(total-count %test-runner-total-count %test-runner-total-count!) |
| 112 |
;; Stack (list) of (count-at-start . expected-count): |
| 113 |
(count-list %test-runner-count-list %test-runner-count-list!) |
| 114 |
(result-alist test-result-alist test-result-alist!) |
| 115 |
;; Field can be used by test-runner for any purpose. |
| 116 |
(aux-value test-runner-aux-value test-runner-aux-value!) |
| 117 |
) |
| 118 |
|
| 119 |
(define (test-runner-reset runner) |
| 120 |
(test-result-alist! runner '()) |
| 121 |
(test-runner-pass-count! runner 0) |
| 122 |
(test-runner-fail-count! runner 0) |
| 123 |
(test-runner-xpass-count! runner 0) |
| 124 |
(test-runner-xfail-count! runner 0) |
| 125 |
(test-runner-skip-count! runner 0) |
| 126 |
(%test-runner-total-count! runner 0) |
| 127 |
(%test-runner-count-list! runner '()) |
| 128 |
(%test-runner-run-list! runner #t) |
| 129 |
(%test-runner-skip-list! runner '()) |
| 130 |
(%test-runner-fail-list! runner '()) |
| 131 |
(%test-runner-skip-save! runner '()) |
| 132 |
(%test-runner-fail-save! runner '()) |
| 133 |
(test-runner-group-stack! runner '()) |
| 134 |
(test-runner-aux-value! runner #f)) |
| 135 |
|
| 136 |
(define (test-runner-group-path runner) |
| 137 |
(reverse (test-runner-group-stack runner))) |
| 138 |
|
| 139 |
|
| 140 |
;;; |
| 141 |
;;; Gauche specific hooks |
| 142 |
;;; |
| 143 |
|
| 144 |
;; Called when enetering a new group. |
| 145 |
(define (gauche-test-group-hook runner suite-name) |
| 146 |
;; [Gauche] SRFI-64's test-begin/test-end is effectively our grouping. |
| 147 |
;; We indent the message according to te group nesting level |
| 148 |
(when (and (test:test-running?) |
| 149 |
(zero? (test-runner-nesting))) |
| 150 |
(let1 nesting (length (test-runner-group-stack runner)) |
| 151 |
(test:test-section |
| 152 |
(format "~a~a" (make-string nesting #\_) suite-name))))) |
| 153 |
|
| 154 |
;; If a test runner is run during other test runner is running, |
| 155 |
;; the 'inner' runner is likely a 'dummy' one, e.g. when you want to |
| 156 |
;; the test runner itself. Success/failure of such inner runners |
| 157 |
;; shouldn't be included in gauche.test's results, so we keep track |
| 158 |
;; of runner nesting level. |
| 159 |
(define test-runner-nesting (make-parameter 0)) |
| 160 |
|
| 161 |
;; pre hook and post hook are called between each test |
| 162 |
;; that handles Gauche-style result logging. |
| 163 |
(define (gauche-test-pre-hook runner expr expect) |
| 164 |
(when (and (test:test-running?) |
| 165 |
(zero? (test-runner-nesting))) |
| 166 |
(let ([msg (or (test-result-ref runner 'test-name) |
| 167 |
expr)]) |
| 168 |
(test-result-set! runner 'test-msg msg) |
| 169 |
(format #t "test ~a, expects ~s ==> " msg expect) |
| 170 |
(flush) |
| 171 |
(test:test-count++)))) |
| 172 |
|
| 173 |
(define (gauche-test-post-hook runner ok? expect actual) |
| 174 |
(when (and (test:test-running?) |
| 175 |
(zero? (test-runner-nesting))) |
| 176 |
(cond [ok? (format #t "ok\n") (test:test-pass++)] |
| 177 |
[(eq? (test-result-ref runner 'result-kind) 'xfail) |
| 178 |
(format #t "ok (expected failure)\n") (test:test-pass++)] |
| 179 |
[else |
| 180 |
(let ([msg (test-result-ref runner 'test-msg)]) |
| 181 |
(begin (format #t "ERROR: GOT ~s\n" actual) |
| 182 |
(test:test-fail++ msg expect actual)))]))) |
| 183 |
|
| 184 |
|
| 185 |
;;; |
| 186 |
;;; Null runner |
| 187 |
;;; |
| 188 |
|
| 189 |
(define (%test-null-callback runner) #f) |
| 190 |
|
| 191 |
(define (test-runner-null) |
| 192 |
(let ((runner (%test-runner-alloc))) |
| 193 |
(test-runner-reset runner) |
| 194 |
(test-runner-on-group-begin! runner (lambda (runner name count) #f)) |
| 195 |
(test-runner-on-group-end! runner %test-null-callback) |
| 196 |
(test-runner-on-final! runner %test-null-callback) |
| 197 |
(test-runner-on-test-begin! runner %test-null-callback) |
| 198 |
(test-runner-on-test-end! runner %test-null-callback) |
| 199 |
(test-runner-on-bad-count! runner (lambda (runner count expected) #f)) |
| 200 |
(test-runner-on-bad-end-name! runner (lambda (runner begin end) #f)) |
| 201 |
runner)) |
| 202 |
|
| 203 |
;;; |
| 204 |
;;; Simple runner |
| 205 |
;;; |
| 206 |
|
| 207 |
(define (test-runner-simple) |
| 208 |
(let ((runner (%test-runner-alloc))) |
| 209 |
(test-runner-reset runner) |
| 210 |
(test-runner-on-group-begin! runner test-on-group-begin-simple) |
| 211 |
(test-runner-on-group-end! runner test-on-group-end-simple) |
| 212 |
(test-runner-on-final! runner test-on-final-simple) |
| 213 |
(test-runner-on-test-begin! runner test-on-test-begin-simple) |
| 214 |
(test-runner-on-test-end! runner test-on-test-end-simple) |
| 215 |
(test-runner-on-bad-count! runner test-on-bad-count-simple) |
| 216 |
(test-runner-on-bad-end-name! runner test-on-bad-end-name-simple) |
| 217 |
runner)) |
| 218 |
|
| 219 |
(define test-runner-current (make-parameter #f)) |
| 220 |
(define test-runner-factory (make-parameter test-runner-simple)) |
| 221 |
|
| 222 |
;; A safer wrapper to test-runner-current. |
| 223 |
(define (test-runner-get) |
| 224 |
(let ((r (test-runner-current))) |
| 225 |
(if (not r) |
| 226 |
(error "test-runner not initialized - test-begin missing?")) |
| 227 |
r)) |
| 228 |
|
| 229 |
(define (%test-specifier-matches spec runner) |
| 230 |
(spec runner)) |
| 231 |
|
| 232 |
(define (test-runner-create) |
| 233 |
((test-runner-factory))) |
| 234 |
|
| 235 |
(define (%test-any-specifier-matches list runner) |
| 236 |
(let ((result #f)) |
| 237 |
(let loop ((l list)) |
| 238 |
(cond ((null? l) result) |
| 239 |
(else |
| 240 |
(if (%test-specifier-matches (car l) runner) |
| 241 |
(set! result #t)) |
| 242 |
(loop (cdr l))))))) |
| 243 |
|
| 244 |
;; Returns #f, #t, or 'xfail. |
| 245 |
(define (%test-should-execute runner) |
| 246 |
(let ((run (%test-runner-run-list runner))) |
| 247 |
(cond ((or |
| 248 |
(not (or (eqv? run #t) |
| 249 |
(%test-any-specifier-matches run runner))) |
| 250 |
(%test-any-specifier-matches |
| 251 |
(%test-runner-skip-list runner) |
| 252 |
runner)) |
| 253 |
(test-result-set! runner 'result-kind 'skip) |
| 254 |
#f) |
| 255 |
((%test-any-specifier-matches |
| 256 |
(%test-runner-fail-list runner) |
| 257 |
runner) |
| 258 |
(test-result-set! runner 'result-kind 'xfail) |
| 259 |
'xfail) |
| 260 |
(else #t)))) |
| 261 |
|
| 262 |
(define (%test-begin suite-name count) |
| 263 |
(if (not (test-runner-current)) |
| 264 |
(test-runner-current (test-runner-create))) |
| 265 |
(let ((runner (test-runner-current))) |
| 266 |
(gauche-test-group-hook runner suite-name) |
| 267 |
((test-runner-on-group-begin runner) runner suite-name count) |
| 268 |
(%test-runner-skip-save! runner |
| 269 |
(cons (%test-runner-skip-list runner) |
| 270 |
(%test-runner-skip-save runner))) |
| 271 |
(%test-runner-fail-save! runner |
| 272 |
(cons (%test-runner-fail-list runner) |
| 273 |
(%test-runner-fail-save runner))) |
| 274 |
(%test-runner-count-list! runner |
| 275 |
(cons (cons (%test-runner-total-count runner) |
| 276 |
count) |
| 277 |
(%test-runner-count-list runner))) |
| 278 |
(test-runner-group-stack! runner (cons suite-name |
| 279 |
(test-runner-group-stack runner))))) |
| 280 |
(define-syntax test-begin |
| 281 |
(syntax-rules () |
| 282 |
((test-begin suite-name) |
| 283 |
(%test-begin suite-name #f)) |
| 284 |
((test-begin suite-name count) |
| 285 |
(%test-begin suite-name count)))) |
| 286 |
|
| 287 |
(define (test-on-group-begin-simple runner suite-name count) |
| 288 |
;; The reference implementation had code to emit logs, but |
| 289 |
;; that's not in the SRFI spec. We can log the test results |
| 290 |
;; if gauche.test is active, so we omit that feature. |
| 291 |
#f) |
| 292 |
|
| 293 |
(define (test-on-group-end-simple runner) |
| 294 |
;; The reference implementation had code to close log file. |
| 295 |
;; Log file is handled by gauche.test, so we omit it. |
| 296 |
#f) |
| 297 |
|
| 298 |
(define (%test-on-bad-count-write runner count expected-count port) |
| 299 |
(display "*** Total number of tests was " port) |
| 300 |
(display count port) |
| 301 |
(display " but should be " port) |
| 302 |
(display expected-count port) |
| 303 |
(display ". ***" port) |
| 304 |
(newline port) |
| 305 |
(display "*** Discrepancy indicates testsuite error or exceptions. ***" port) |
| 306 |
(newline port)) |
| 307 |
|
| 308 |
(define (test-on-bad-count-simple runner count expected-count) |
| 309 |
(%test-on-bad-count-write runner count expected-count (current-output-port)) |
| 310 |
(let ((log (test-runner-aux-value runner))) |
| 311 |
(if (output-port? log) |
| 312 |
(%test-on-bad-count-write runner count expected-count log)))) |
| 313 |
|
| 314 |
(define (test-on-bad-end-name-simple runner begin-name end-name) |
| 315 |
(let ((msg (string-append (%test-format-line runner) "test-end " begin-name |
| 316 |
" does not match test-begin " end-name))) |
| 317 |
(error msg))) |
| 318 |
|
| 319 |
(define (%test-final-report1 value label port) |
| 320 |
(if (> value 0) |
| 321 |
(begin |
| 322 |
(display label port) |
| 323 |
(display value port) |
| 324 |
(newline port)))) |
| 325 |
|
| 326 |
(define (%test-final-report-simple runner port) |
| 327 |
(%test-final-report1 (test-runner-pass-count runner) |
| 328 |
"# of expected passes " port) |
| 329 |
(%test-final-report1 (test-runner-xfail-count runner) |
| 330 |
"# of expected failures " port) |
| 331 |
(%test-final-report1 (test-runner-xpass-count runner) |
| 332 |
"# of unexpected successes " port) |
| 333 |
(%test-final-report1 (test-runner-fail-count runner) |
| 334 |
"# of unexpected failures " port) |
| 335 |
(%test-final-report1 (test-runner-skip-count runner) |
| 336 |
"# of skipped tests " port)) |
| 337 |
|
| 338 |
(define (test-on-final-simple runner) |
| 339 |
(%test-final-report-simple runner (current-output-port)) |
| 340 |
(let ((log (test-runner-aux-value runner))) |
| 341 |
(if (output-port? log) |
| 342 |
(%test-final-report-simple runner log)))) |
| 343 |
|
| 344 |
;;; |
| 345 |
;;; Test API |
| 346 |
;;; |
| 347 |
|
| 348 |
(define (%test-format-line runner) |
| 349 |
(let* ((line-info (test-result-alist runner)) |
| 350 |
(source-file (assq 'source-file line-info)) |
| 351 |
(source-line (assq 'source-line line-info)) |
| 352 |
(file (if source-file (cdr source-file) ""))) |
| 353 |
(if source-line |
| 354 |
(string-append file ":" |
| 355 |
(number->string (cdr source-line)) ": ") |
| 356 |
""))) |
| 357 |
|
| 358 |
(define (%test-end suite-name line-info) |
| 359 |
(let* ((r (test-runner-get)) |
| 360 |
(groups (test-runner-group-stack r)) |
| 361 |
(line (%test-format-line r))) |
| 362 |
(test-result-alist! r line-info) |
| 363 |
(if (null? groups) |
| 364 |
(let ((msg (string-append line "test-end not in a group"))) |
| 365 |
(error msg))) |
| 366 |
(if (and suite-name (not (equal? suite-name (car groups)))) |
| 367 |
((test-runner-on-bad-end-name r) r suite-name (car groups))) |
| 368 |
(let* ((count-list (%test-runner-count-list r)) |
| 369 |
(expected-count (cdar count-list)) |
| 370 |
(saved-count (caar count-list)) |
| 371 |
(group-count (- (%test-runner-total-count r) saved-count))) |
| 372 |
(if (and expected-count |
| 373 |
(not (= expected-count group-count))) |
| 374 |
((test-runner-on-bad-count r) r group-count expected-count)) |
| 375 |
((test-runner-on-group-end r) r) |
| 376 |
(test-runner-group-stack! r (cdr (test-runner-group-stack r))) |
| 377 |
(%test-runner-skip-list! r (car (%test-runner-skip-save r))) |
| 378 |
(%test-runner-skip-save! r (cdr (%test-runner-skip-save r))) |
| 379 |
(%test-runner-fail-list! r (car (%test-runner-fail-save r))) |
| 380 |
(%test-runner-fail-save! r (cdr (%test-runner-fail-save r))) |
| 381 |
(%test-runner-count-list! r (cdr count-list)) |
| 382 |
(if (null? (test-runner-group-stack r)) |
| 383 |
((test-runner-on-final r) r))))) |
| 384 |
|
| 385 |
(define-syntax test-group |
| 386 |
(syntax-rules () |
| 387 |
((test-group suite-name . body) |
| 388 |
(let ((r (test-runner-current))) |
| 389 |
;; Ideally should also set line-number, if available. |
| 390 |
(test-result-alist! r (list (cons 'test-name suite-name))) |
| 391 |
(if (%test-should-execute r) |
| 392 |
(dynamic-wind |
| 393 |
(lambda () (test-begin suite-name)) |
| 394 |
(lambda () . body) |
| 395 |
(lambda () (test-end suite-name)))))))) |
| 396 |
|
| 397 |
(define-syntax test-group-with-cleanup |
| 398 |
(syntax-rules () |
| 399 |
((test-group-with-cleanup suite-name form cleanup-form) |
| 400 |
(test-group suite-name |
| 401 |
(dynamic-wind |
| 402 |
(lambda () #f) |
| 403 |
(lambda () form) |
| 404 |
(lambda () cleanup-form)))) |
| 405 |
((test-group-with-cleanup suite-name cleanup-form) |
| 406 |
(test-group-with-cleanup suite-name #f cleanup-form)) |
| 407 |
((test-group-with-cleanup suite-name form1 form2 form3 . rest) |
| 408 |
(test-group-with-cleanup suite-name (begin form1 form2) form3 . rest)))) |
| 409 |
|
| 410 |
(define (test-on-test-begin-simple runner) |
| 411 |
(let ((log (test-runner-aux-value runner))) |
| 412 |
(if (output-port? log) |
| 413 |
(let* ((results (test-result-alist runner)) |
| 414 |
(source-file (assq 'source-file results)) |
| 415 |
(source-line (assq 'source-line results)) |
| 416 |
(source-form (assq 'source-form results)) |
| 417 |
(test-name (assq 'test-name results))) |
| 418 |
(display "Test begin:" log) |
| 419 |
(newline log) |
| 420 |
(if test-name (%test-write-result1 test-name log)) |
| 421 |
(if source-file (%test-write-result1 source-file log)) |
| 422 |
(if source-line (%test-write-result1 source-line log)) |
| 423 |
(if source-form (%test-write-result1 source-form log)))))) |
| 424 |
|
| 425 |
(define (test-result-ref runner pname :optional (default #f)) |
| 426 |
(let ((p (assq pname (test-result-alist runner)))) |
| 427 |
(if p (cdr p) default))) |
| 428 |
|
| 429 |
(define (test-on-test-end-simple runner) |
| 430 |
(let ((log (test-runner-aux-value runner)) |
| 431 |
(kind (test-result-ref runner 'result-kind))) |
| 432 |
(if (memq kind '(fail xpass)) |
| 433 |
(let* ((results (test-result-alist runner)) |
| 434 |
(source-file (assq 'source-file results)) |
| 435 |
(source-line (assq 'source-line results)) |
| 436 |
(test-name (assq 'test-name results))) |
| 437 |
(if (or source-file source-line) |
| 438 |
(begin |
| 439 |
(if source-file (display (cdr source-file))) |
| 440 |
(display ":") |
| 441 |
(if source-line (display (cdr source-line))) |
| 442 |
(display ": "))) |
| 443 |
(display (if (eq? kind 'xpass) "XPASS" "FAIL")) |
| 444 |
(if test-name |
| 445 |
(begin |
| 446 |
(display " ") |
| 447 |
(display (cdr test-name)))) |
| 448 |
(newline) |
| 449 |
(display " expected:") |
| 450 |
(write (test-result-ref runner 'expected-value)) |
| 451 |
(newline) |
| 452 |
(display " actual:") |
| 453 |
(write (test-result-ref runner 'actual-value)) |
| 454 |
(newline))) |
| 455 |
(if (output-port? log) |
| 456 |
(begin |
| 457 |
(display "Test end:" log) |
| 458 |
(newline log) |
| 459 |
(let loop ((list (test-result-alist runner))) |
| 460 |
(if (pair? list) |
| 461 |
(let ((pair (car list))) |
| 462 |
;; Write out properties not written out by on-test-begin. |
| 463 |
(if (not (memq (car pair) |
| 464 |
'(test-name source-file source-line source-form))) |
| 465 |
(%test-write-result1 pair log)) |
| 466 |
(loop (cdr list))))))))) |
| 467 |
|
| 468 |
(define (%test-write-result1 pair port) |
| 469 |
(display " " port) |
| 470 |
(display (car pair) port) |
| 471 |
(display ": " port) |
| 472 |
(write (cdr pair) port) |
| 473 |
(newline port)) |
| 474 |
|
| 475 |
(define (test-result-set! runner pname value) |
| 476 |
(let* ((alist (test-result-alist runner)) |
| 477 |
(p (assq pname alist))) |
| 478 |
(if p |
| 479 |
(set-cdr! p value) |
| 480 |
(test-result-alist! runner (cons (cons pname value) alist))))) |
| 481 |
|
| 482 |
(define (test-result-clear runner) |
| 483 |
(test-result-alist! runner '())) |
| 484 |
|
| 485 |
(define (test-result-remove runner pname) |
| 486 |
(let* ((alist (test-result-alist runner)) |
| 487 |
(p (assq pname alist))) |
| 488 |
(if p |
| 489 |
(test-result-alist! runner |
| 490 |
(let loop ((r alist)) |
| 491 |
(if (eq? r p) (cdr r) |
| 492 |
(cons (car r) (loop (cdr r))))))))) |
| 493 |
|
| 494 |
(define (test-result-kind . rest) |
| 495 |
(let ((runner (if (pair? rest) (car rest) (test-runner-current)))) |
| 496 |
(test-result-ref runner 'result-kind))) |
| 497 |
|
| 498 |
(define (test-passed? . rest) |
| 499 |
(let ((runner (if (pair? rest) (car rest) (test-runner-get)))) |
| 500 |
(memq (test-result-ref runner 'result-kind) '(pass xpass)))) |
| 501 |
|
| 502 |
(define (%test-report-result) |
| 503 |
(let* ((r (test-runner-get)) |
| 504 |
(result-kind (test-result-kind r))) |
| 505 |
(case result-kind |
| 506 |
((pass) |
| 507 |
(test-runner-pass-count! r (+ 1 (test-runner-pass-count r)))) |
| 508 |
((fail) |
| 509 |
(test-runner-fail-count! r (+ 1 (test-runner-fail-count r)))) |
| 510 |
((xpass) |
| 511 |
(test-runner-xpass-count! r (+ 1 (test-runner-xpass-count r)))) |
| 512 |
((xfail) |
| 513 |
(test-runner-xfail-count! r (+ 1 (test-runner-xfail-count r)))) |
| 514 |
(else |
| 515 |
(test-runner-skip-count! r (+ 1 (test-runner-skip-count r))))) |
| 516 |
(%test-runner-total-count! r (+ 1 (%test-runner-total-count r))) |
| 517 |
((test-runner-on-test-end r) r))) |
| 518 |
|
| 519 |
(define-syntax %test-evaluate-with-catch |
| 520 |
(syntax-rules () |
| 521 |
((%test-evaluate-with-catch test-expression) |
| 522 |
(guard (err (else (print err) #f)) test-expression)))) |
| 523 |
|
| 524 |
(define (%test-source-line2 form) ; can be something - later |
| 525 |
'()) |
| 526 |
|
| 527 |
(define (%test-on-test-begin r) |
| 528 |
(%test-should-execute r) |
| 529 |
((test-runner-on-test-begin r) r) |
| 530 |
(not (eq? 'skip (test-result-ref r 'result-kind)))) |
| 531 |
|
| 532 |
(define (%test-on-test-end r result) |
| 533 |
(test-result-set! r 'result-kind |
| 534 |
(if (eq? (test-result-ref r 'result-kind) 'xfail) |
| 535 |
(if result 'xpass 'xfail) |
| 536 |
(if result 'pass 'fail)))) |
| 537 |
|
| 538 |
(define (test-runner-test-name runner) |
| 539 |
(test-result-ref runner 'test-name "")) |
| 540 |
|
| 541 |
(define-syntax %test-comp2body |
| 542 |
(syntax-rules () |
| 543 |
((%test-comp2body r comp expected expr) |
| 544 |
(let () |
| 545 |
(if (%test-on-test-begin r) |
| 546 |
(let ((exp expected)) |
| 547 |
(test-result-set! r 'expected-value exp) |
| 548 |
(gauche-test-pre-hook r 'expr exp) |
| 549 |
(let* ((res (%test-evaluate-with-catch expr)) |
| 550 |
(compared (comp exp res))) |
| 551 |
(test-result-set! r 'actual-value res) |
| 552 |
(gauche-test-post-hook r compared exp res) |
| 553 |
(%test-on-test-end r compared)))) |
| 554 |
(%test-report-result))))) |
| 555 |
|
| 556 |
(define (%test-approximate= error) |
| 557 |
(lambda (value expected) |
| 558 |
(let ((rval (real-part value)) |
| 559 |
(ival (imag-part value)) |
| 560 |
(rexp (real-part expected)) |
| 561 |
(iexp (imag-part expected))) |
| 562 |
(and (>= rval (- rexp error)) |
| 563 |
(>= ival (- iexp error)) |
| 564 |
(<= rval (+ rexp error)) |
| 565 |
(<= ival (+ iexp error)))))) |
| 566 |
|
| 567 |
(define-syntax %test-comp1body |
| 568 |
(syntax-rules () |
| 569 |
((%test-comp1body r expr) |
| 570 |
(let () |
| 571 |
(if (%test-on-test-begin r) |
| 572 |
(let () |
| 573 |
(test-result-set! r 'expected-value #t) |
| 574 |
(gauche-test-pre-hook r 'expr #t) |
| 575 |
(let ((res (%test-evaluate-with-catch expr))) |
| 576 |
(test-result-set! r 'actual-value res) |
| 577 |
(gauche-test-post-hook r (boolean res) #t (boolean res)) |
| 578 |
(%test-on-test-end r res)))) |
| 579 |
(%test-report-result))))) |
| 580 |
|
| 581 |
(define-syntax test-end |
| 582 |
(syntax-rules () |
| 583 |
((test-end) |
| 584 |
(%test-end #f '())) |
| 585 |
((test-end suite-name) |
| 586 |
(%test-end suite-name '())))) |
| 587 |
(define-syntax test-assert |
| 588 |
(syntax-rules () |
| 589 |
((test-assert tname test-expression) |
| 590 |
(let* ((r (test-runner-get)) |
| 591 |
(name tname)) |
| 592 |
(test-result-alist! r '((test-name . tname))) |
| 593 |
(%test-comp1body r test-expression))) |
| 594 |
((test-assert test-expression) |
| 595 |
(let* ((r (test-runner-get))) |
| 596 |
(test-result-alist! r '()) |
| 597 |
(%test-comp1body r test-expression))))) |
| 598 |
(define-syntax %test-comp2 |
| 599 |
(syntax-rules () |
| 600 |
((%test-comp2 comp tname expected expr) |
| 601 |
(let* ((r (test-runner-get)) |
| 602 |
(name tname)) |
| 603 |
(test-result-alist! r (list (cons 'test-name tname))) |
| 604 |
(%test-comp2body r comp expected expr))) |
| 605 |
((%test-comp2 comp expected expr) |
| 606 |
(let* ((r (test-runner-get))) |
| 607 |
(test-result-alist! r (list (cons 'test-name 'expr))) |
| 608 |
(%test-comp2body r comp expected expr))))) |
| 609 |
(define-syntax test-equal |
| 610 |
(syntax-rules () |
| 611 |
((test-equal . rest) |
| 612 |
(%test-comp2 equal? . rest)))) |
| 613 |
(define-syntax test-eqv |
| 614 |
(syntax-rules () |
| 615 |
((test-eqv . rest) |
| 616 |
(%test-comp2 eqv? . rest)))) |
| 617 |
(define-syntax test-eq |
| 618 |
(syntax-rules () |
| 619 |
((test-eq . rest) |
| 620 |
(%test-comp2 eq? . rest)))) |
| 621 |
(define-syntax test-approximate |
| 622 |
(syntax-rules () |
| 623 |
((test-approximate tname expected expr error) |
| 624 |
(%test-comp2 (%test-approximate= error) tname expected expr)) |
| 625 |
((test-approximate expected expr error) |
| 626 |
(%test-comp2 (%test-approximate= error) expected expr)))) |
| 627 |
|
| 628 |
(define-syntax %test-error |
| 629 |
(syntax-rules () |
| 630 |
((%test-error r etype expr) |
| 631 |
(%test-comp1body r (guard (ex ((condition-type? etype) |
| 632 |
(and (condition? ex) (condition-has-type? ex etype))) |
| 633 |
((procedure? etype) |
| 634 |
(etype ex)) |
| 635 |
((equal? etype #t) |
| 636 |
#t) |
| 637 |
(else #t)) |
| 638 |
expr #f))))) |
| 639 |
|
| 640 |
(define-syntax test-error |
| 641 |
(syntax-rules () |
| 642 |
((test-error name etype expr) |
| 643 |
(let ((r (test-runner-get))) |
| 644 |
(test-result-alist! r `((test-name . ,name))) |
| 645 |
(%test-error r etype expr))) |
| 646 |
((test-error etype expr) |
| 647 |
(let ((r (test-runner-get))) |
| 648 |
(test-result-alist! r '()) |
| 649 |
(%test-error r etype expr))) |
| 650 |
((test-error expr) |
| 651 |
(let ((r (test-runner-get))) |
| 652 |
(test-result-alist! r '()) |
| 653 |
(%test-error r #t expr))))) |
| 654 |
|
| 655 |
(define-syntax test-with-runner |
| 656 |
(syntax-rules () |
| 657 |
((test-with-runner runner form ...) |
| 658 |
(parameterize ([test-runner-current runner] |
| 659 |
[test-runner-nesting (+ (test-runner-nesting) 1)]) |
| 660 |
form ...)))) |
| 661 |
|
| 662 |
(define (test-apply first . rest) |
| 663 |
(if (test-runner? first) |
| 664 |
(test-with-runner first (apply test-apply rest)) |
| 665 |
(let ((r (test-runner-current))) |
| 666 |
(if r |
| 667 |
(let ((run-list (%test-runner-run-list r))) |
| 668 |
(cond ((null? rest) |
| 669 |
(%test-runner-run-list! r (reverse run-list)) |
| 670 |
(first)) ;; actually apply procedure thunk |
| 671 |
(else |
| 672 |
(%test-runner-run-list! |
| 673 |
r |
| 674 |
(if (eq? run-list #t) (list first) (cons first run-list))) |
| 675 |
(apply test-apply rest) |
| 676 |
(%test-runner-run-list! r run-list)))) |
| 677 |
(let ((r (test-runner-create))) |
| 678 |
(test-with-runner r (apply test-apply first rest)) |
| 679 |
((test-runner-on-final r) r)))))) |
| 680 |
|
| 681 |
;;; Predicates |
| 682 |
|
| 683 |
(define (%test-match-nth n count) |
| 684 |
(let ((i 0)) |
| 685 |
(lambda (runner) |
| 686 |
(set! i (+ i 1)) |
| 687 |
(and (>= i n) (< i (+ n count)))))) |
| 688 |
|
| 689 |
(define-syntax test-match-nth |
| 690 |
(syntax-rules () |
| 691 |
((test-match-nth n) |
| 692 |
(test-match-nth n 1)) |
| 693 |
((test-match-nth n count) |
| 694 |
(%test-match-nth n count)))) |
| 695 |
|
| 696 |
(define (%test-match-all . pred-list) |
| 697 |
(lambda (runner) |
| 698 |
(let ((result #t)) |
| 699 |
(let loop ((l pred-list)) |
| 700 |
(if (null? l) |
| 701 |
result |
| 702 |
(begin |
| 703 |
(if (not ((car l) runner)) |
| 704 |
(set! result #f)) |
| 705 |
(loop (cdr l)))))))) |
| 706 |
|
| 707 |
(define-syntax test-match-all |
| 708 |
(syntax-rules () |
| 709 |
((test-match-all pred ...) |
| 710 |
(%test-match-all (%test-as-specifier pred) ...)))) |
| 711 |
|
| 712 |
(define (%test-match-any . pred-list) |
| 713 |
(lambda (runner) |
| 714 |
(let ((result #f)) |
| 715 |
(let loop ((l pred-list)) |
| 716 |
(if (null? l) |
| 717 |
result |
| 718 |
(begin |
| 719 |
(if ((car l) runner) |
| 720 |
(set! result #t)) |
| 721 |
(loop (cdr l)))))))) |
| 722 |
|
| 723 |
(define-syntax test-match-any |
| 724 |
(syntax-rules () |
| 725 |
((test-match-any pred ...) |
| 726 |
(%test-match-any (%test-as-specifier pred) ...)))) |
| 727 |
|
| 728 |
;; Coerce to a predicate function: |
| 729 |
(define (%test-as-specifier specifier) |
| 730 |
(cond ((procedure? specifier) specifier) |
| 731 |
((integer? specifier) (test-match-nth 1 specifier)) |
| 732 |
((string? specifier) (test-match-name specifier)) |
| 733 |
(else |
| 734 |
(error "not a valid test specifier")))) |
| 735 |
|
| 736 |
(define-syntax test-skip |
| 737 |
(syntax-rules () |
| 738 |
((test-skip pred ...) |
| 739 |
(let ((runner (test-runner-get))) |
| 740 |
(%test-runner-skip-list! runner |
| 741 |
(cons (test-match-all (%test-as-specifier pred) ...) |
| 742 |
(%test-runner-skip-list runner))))))) |
| 743 |
|
| 744 |
(define-syntax test-expect-fail |
| 745 |
(syntax-rules () |
| 746 |
((test-expect-fail pred ...) |
| 747 |
(let ((runner (test-runner-get))) |
| 748 |
(%test-runner-fail-list! runner |
| 749 |
(cons (test-match-all (%test-as-specifier pred) ...) |
| 750 |
(%test-runner-fail-list runner))))))) |
| 751 |
|
| 752 |
(define (test-match-name name) |
| 753 |
(lambda (runner) |
| 754 |
(equal? name (test-runner-test-name runner)))) |
| 755 |
|
| 756 |
(define (test-read-eval-string string) |
| 757 |
(let* ((port (open-input-string string)) |
| 758 |
(form (read port))) |
| 759 |
(if (eof-object? (read-char port)) |
| 760 |
(eval form ((with-module gauche.internal vm-current-module))) |
| 761 |
(error "(not at eof)")))) |