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
|
;;; FILE "unit-testing.oak"
;;; IMPLEMENTS Unit Testing for Oaklisp
;;; AUTHOR Ken Dickey
;;; COPYRIGHT (c) 2004 by Kenneth A Dickey; All rights reserved.
;;; This is free software. Permission to use, copy, modify and
;;; distribute this software for non-profit purpose is granted without
;;; fee. It is provided "as is" without express or implied warranty.
;;; The author disclaims all warranties with regard to this software.
;;; In no event shall the author be liable for any damages.
;;;USAGE SYNOPSIS
;; Tests are first created and added to a global UNIT-TESTS "database".
;; Tests are arranged by UNIT-NAME (just a symbol naming a set of tests).
;;
;; SPECIAL FORMS:
;;
;; (CREATE-TEST-SUITE unit-tests test-suite-name setup-thunk teardown-thunk)
;; Creates a <unit-test-suite> and associates it with test-suite-name.
;; The SETUP-THUNK is executed before the unit tests are run and
;; TEARDOWN-THUNK is executed afterward.
;;
;; (ADD-TEST unit-name expected-result form =? . message)
;;
;; (ADD-EQ-TEST unit-name expected-result form . message)
;; => (add-test unit-name expected-result form EQ? . message)
;;
;; (ADD-EQUAL-TEST unit-name expected-result form . message)
;; => (add-test unit-name expected-result form EQUAL? . message)
;;
;; (ENSURE-EXCEPTION-RAISED unit-name exception-type form . message)
;; -> test that the form signals a (subtype of) exception-type
;;
;; All forms are "thunkified" by being wrapped in zero argument lambdas.
;; Internal usage is: (=? expected (thunk))
;;
;;
;; TESTING OPERATIONS:
;;
;; (RUN-ALL-TESTS unit-tests) => Run all suites of tests.
;;
;; (RUN-TESTS-FOR unit-tests 'whatever) => Run tests for unit named WHATEVER.
;;
;; (REMOVE-TESTS-FOR unit-tests 'whatever) => Remove tests for unit named WHATEVER.
;; ..handy before rereading a test defining file.
;;
;; If (VERBOSE? unit-tests) is false, only failures and exceptions are
;; reported, else successful tests are reported as well.
;; The default value is #f. Settable.
;;
;; If (BREAK-ON-ERROR? unit-tests) is true, errors and exceptions break
;; into the debugger, otherwise failures are just reported.
;; The default value is #f. Settable.
;;
;; Tests are typically written as separate files containing set-up & tear-down code.
;; @@QUESTION: Add optional set-up and tear-down forms to unit test suites?
;;
;; Note Also:
;; (RUN-TEST <unit-test> (make <test-counter>) verbose-p break-on-error-p)
;; Run a single <unit-test> -- typically only used for debugging tests.
;; If no error, don't report unless VERBOSE-P
;; If error or exception, break into debugger if BREAK-ON-ERROR-P, else continue
;;
;; (make <unit-test> unit-name expected thunk =? . message)
;; Nota Bene: Currently all output goes to (current-output-port).
;; Rebind this port to redirect output elsewhere,
;;;======================================================================
;;; @@FIXME: TABLE-WALK belongs in hash-table.oak
;;; Should define WALK for SEQUENCE as well...
(define-instance table-walk operation)
;; proc takes 2 args: (lambda (key val) ...)
(add-method (table-walk (generic-hash-table table count size) self proc)
(dotimes (index size)
(let ( (chain (nth table index)) )
(for-each
(lambda (bucket)
(proc (car bucket) (cdr bucket)))
chain))))
(add-method (table-walk (eq-hash-table table count size) self proc)
(dotimes (index size)
(let ( (chain (nth table index)) )
(for-each
(lambda (bucket)
(proc (car bucket) (cdr bucket)))
chain))))
;;;======================================================================
;;;A <TEST-COUNTER> keeps track of number passed,
;;; failed (actual != expected), excepted (signalled exception)
;;; and reports on these statistics.
(define-instance <test-counter>
type
'(num-passed num-failed num-excepted)
(list object))
(define-instance zero-counters operation)
(define-instance increment-failed operation)
(define-instance increment-passed operation)
(define-instance increment-excepted operation)
(define-instance number-failed operation)
(define-instance number-passed operation)
(define-instance number-excepted operation)
(define-instance display-results operation)
(add-method (zero-counters (<test-counter> num-passed num-failed num-excepted) self)
(set! num-passed 0)
(set! num-failed 0)
(set! num-excepted 0)
self)
(add-method (initialize (<test-counter> num-passed num-failed num-excepted)
self)
(zero-counters self))
(add-method (increment-failed (<test-counter> num-failed) self)
(set! num-failed (+ 1 num-failed)))
(add-method (increment-passed (<test-counter> num-passed) self)
(set! num-passed (+ 1 num-passed)))
(add-method (increment-excepted (<test-counter> num-excepted) self)
(set! num-excepted (+ 1 num-excepted)))
(add-method (number-failed (<test-counter> num-failed) self)
num-failed)
(add-method (number-passed (<test-counter> num-passed) self)
num-passed)
(add-method (number-excepted (<test-counter> num-excepted) self)
num-excepted)
(add-method (display-results (<test-counter> num-passed num-failed num-excepted)
self port)
(format port "~%TOTAL PASSED: ~D" num-passed)
(format port "~%TOTAL FAILED: ~D" num-failed)
(format port "~%TOTAL EXCEPTIONS: ~D~%~%" num-excepted)
)
;;;======================================================================
;;;A <UNIT-TEST-SUITE> is an UNnamed container of unit tests, setup and
;;; teardown code. Tests are a reversed list of <unit-test>
;;; instances (see below). A <unit-tests> instance maintains the (name X
;;; unit-test-suite) bindings.
(define-instance run-test operation)
(define-instance run-all-tests operation)
(define-instance run-tests-for operation)
(define-instance <unit-test-suite>
type
'(test-list setup-thunk teardown-thunk)
(list object))
(add-method (initialize (<unit-test-suite> test-list setup-thunk teardown-thunk)
self setup teardown)
(set! test-list '())
(set! setup-thunk setup)
(set! teardown-thunk teardown)
self)
(define-instance add-unit-test operation)
(add-method (add-unit-test (<unit-test-suite> test-list) self unit-test)
(unless (is-a? unit-test <unit-test>)
(error "(ADD-UNIT-TEST <unit-test-suite> <unit-test>) not a <unit-test>: ~a" unit-test))
(set! test-list (cons unit-test test-list)))
(define-instance remove-tests-for operation)
(add-method (remove-tests-for (<unit-test-suite> test-list) self)
(set! test-list '()))
(add-method (run-all-tests (<unit-test-suite> test-list setup-thunk teardown-thunk)
self test-name result-counter verbose-p break-on-error-p)
(if (null? test-list)
((if break-on-error-p error warn)
"~&HUH? No tests found for ~A" test-name)
(block
(setup-thunk)
(format #t "~%===> Starting Tests for ~a" test-name)
(for-each
(lambda (test) (run-test test result-counter verbose-p break-on-error-p))
(reverse test-list))
(format #t "~&===> Completed Tests for ~a~%" test-name)
(teardown-thunk))
) )
;;;======================================================================
;;;A <UNIT-TESTS> contains and runs named test suites
(define-instance <unit-tests>
type
'(verbose-p break-on-error-p)
(list eq-hash-table)) ; really a symbol-table
(add-method (initialize (<unit-tests> verbose-p break-on-error-p)
self)
(set! verbose-p #f)
(set! break-on-error-p #f)
(^super eq-hash-table initialize self))
;; @@FIXME: This should not be required. Why not simply inherited?
(add-method ((setter table-entry) (<unit-tests>) self key value)
(^super eq-hash-table (setter table-entry) self key value))
(define-instance verbose? settable-operation)
(define-instance break-on-error? settable-operation)
(add-method (verbose? (<unit-tests> verbose-p) self)
verbose-p)
(add-method ((setter verbose?) (<unit-tests> verbose-p) self bool)
(set! verbose-p bool))
(add-method (break-on-error? (<unit-tests> break-on-error-p) self)
break-on-error-p)
(add-method ((setter break-on-error?) (<unit-tests> break-on-error-p) self bool)
(set! break-on-error-p bool))
(define-instance create-test-suite operation)
(define default-setup-thunk (lambda () #f))
(define default-teardown-thunk default-setup-thunk)
(add-method (create-test-suite (<unit-tests>)
self test-suite-name setup teardown)
(let ( (test-suite
(make <unit-test-suite> setup teardown))
)
(set! (table-entry self test-suite-name) test-suite)
test-suite
) )
(add-method (add-unit-test (<unit-tests>)
self unit-name test-case)
(unless (is-a? test-case <unit-test>)
(error "(ADD-UNIT-TEST <unit-tests> name <unit-test>) requires a <unit-test>, got: ~a"
test-case))
(cond ((present? self unit-name)
=> (lambda (probe) (add-unit-test (cdr probe) test-case)))
(else
(error "Unit ~a not found. Try (CREATE-TEST-SUITE UNIT-TESTS ~a setup-thunk teardown-thunk)"
unit-name unit-name)))
test-case
)
(define-instance remove-tests-for operation)
;; @@FIXME: no way to remove table entries ?!?
(add-method (remove-tests-for (<unit-tests>) self unit-name)
(cond
((present? self unit-name)
=> (lambda (probe) (remove-tests-for (cdr probe))))
(else
(error "Unit ~a not found. Try (CREATE-TEST-SUITE UNIT-TESTS ~a setup-thunk teardown-thunk)"
unit-name unit-name)))
)
remove
;;; UNIT-TESTS global
(define-constant unit-tests (make <unit-tests>))
;;;======================================================================
;;; A <UNIT-TEST> is a single test
(define-instance <unit-test>
type '(expected thunk compare? message) (list object))
(add-method (initialize (<unit-test> expected thunk compare? message)
self expected-result the-thunk comparison-op? msg)
(set! expected expected-result)
(set! thunk the-thunk)
(set! compare? comparison-op?)
(set! message msg)
self)
(define-syntax (add-test unit-name expect form equivalent? . message)
(let ( (msg (if (pair? message) (car message) "")) )
`(add-unit-test unit-tests
,unit-name
(make <unit-test>
,expect
(lambda () ,form)
,equivalent?
,msg)
) ) )
(define-syntax (add-eq-test unit-name expect form . message)
`(add-test ,unit-name ,expect ,form eq? . ,message))
(define-syntax (add-equal-test unit-name expect form . message)
`(add-test ,unit-name ,expect ,form equal? . ,message))
(define-syntax (ensure-exception-raised unit-name exception-type form . message)
(let ( (msg (if (pair? message) (car message) "")) )
`(add-unit-test unit-tests
,unit-name
(make <exception-test>
,exception-type
(lambda () ,form)
,msg)
) ) )
(define-instance <exception-test> type '() (list <unit-test>))
(add-method (initialize (<exception-test>)
self exception-type thunk message)
(unless (subtype? exception-type condition)
(error "An <xception-test> requires an exception type: ~a" exception-type))
(^super <unit-test> initialize self exception-type thunk subtype? message)
self)
(define (warn format-string . format-args)
;; like WARNING, but without the extra "Warning"" line
(format standard-error format-string . format-args))
;;;======================================================================
;;; RUNNING TESTS
;; Run a <unit-test>
;; If no error, don't report unless VERBOSE-P
;; If error or exception, break into debugger if BREAK-ON-ERROR-P, else continue
;; Result-counter is-a <test-counter>
(add-method (run-test (<unit-test> expected thunk compare? message)
self result-counter verbose-p break-on-error-p)
(let* ( (caught-exception #f)
(actual
(bind-error-handler
(general-error ;; catch every type of error
(lambda (err-obj)
(set! caught-exception err-obj)
(format #t "~&*** EXCEPTION: ~a -- ~a" err-obj message)
(when verbose-p
(describe err-obj))
#f))
(thunk)))
)
(cond
(caught-exception
=> (lambda (err-obj)
(increment-excepted result-counter)
(if break-on-error-p
(invoke-debugger err-obj)
err-obj)) ;; return err-obj if not breaking
)
((compare? actual expected)
(increment-passed result-counter)
(if verbose-p
(format #t "~&PASSED: Expected: ~a Got: ~a -- ~a" expected actual message)
#t) ;; compare => #t
)
(else
(increment-failed result-counter)
((if break-on-error-p error warn)
"~&*** FAILED: Expected ~a Got ~a -- ~a" expected actual message)))
) )
(add-method (run-tests-for (<unit-tests> verbose-p break-on-error-p)
self unit-name)
(let ( (probe (present? self unit-name))
(result-counter (make <test-counter>))
)
(if probe
(block
(run-all-tests (cdr probe) unit-name result-counter verbose-p break-on-error-p)
(display-results result-counter #t))
((if break-on-error-p error warn)
"~&HUH? No tests found for ~A" unit-name)))
)
(add-method (run-all-tests (<unit-tests> verbose-p break-on-error-p) self)
(let ( (result-counter (make <test-counter>)) )
(table-walk self
(lambda (unit-name test-suite)
(if test-suite
(run-all-tests test-suite unit-name result-counter verbose-p break-on-error-p)
((if break-on-error-p error warn) "~&HUH? No tests found for ~A" unit-name))))
(display-results result-counter #t)
) )
(define-instance %run-exception-test operation)
;; NB: Runs in (fluid current-locale) which is probably USER-LOCALE
(add-method (run-test (<exception-test>)
self result-counter verbose-p break-on-error-p)
;; helper required for access to <unit-test> internals
(%run-exception-test self result-counter verbose-p break-on-error-p))
(add-method (%run-exception-test (<unit-test> expected thunk compare? message)
self result-counter verbose-p break-on-error-p)
(let* ( (caught-exception #f)
(actual
(bind-error-handler
(general-error ;; catch every type of error
(lambda (err-obj)
(set! caught-exception err-obj)
err-obj))
(thunk)))
)
(cond
((compare? (get-type actual) expected)
(increment-passed result-counter)
(if verbose-p
(format #t "~&PASSED: Expected: ~a Got: ~a of type ~a -- ~a"
expected actual (get-type actual) message)
#t) ;; compare => #t
)
(caught-exception
=> (lambda (err-obj)
(increment-excepted result-counter)
(format #t "~&*** UNEXPECTED EXCEPTION: got ~a of type ~a expected ~a -- ~a"
err-obj (get-type err-obj) expected message)
(when verbose-p
(describe err-obj))
(if break-on-error-p
(invoke-debugger err-obj)
err-obj)) ;; return err-obj if not breaking
)
(else
(increment-failed result-counter)
((if break-on-error-p error warn)
"~&*** FAILED: Expected exception of type ~a Got value: ~a -- ~a" expected actual message)))
) )
;;; unit-testing.oak ends here
|