1 ;; Minimal Scheme unit-test framework
2 ;; Copyright (C) 2010 Peter Brett <peter@peter-b.co.uk>
4 ;; This program is free software; you can redistribute it and/or modify
5 ;; it under the terms of the GNU General Public License as published by
6 ;; the Free Software Foundation; either version 2 of the License, or
7 ;; (at your option) any later version.
9 ;; This program is distributed in the hope that it will be useful,
10 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;; GNU General Public License for more details.
14 ;; You should have received a copy of the GNU General Public License
15 ;; along with this program; if not, write to the Free Software
16 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ;; The following program:
23 ;; (use-modules (unit-test))
24 ;; (begin-test 'SuccessfulTest
26 ;; (assert-equal 1 1))
27 ;; (assert-thrown 'misc-error (error "Blah ~A" "Blah"))
28 ;; (begin-test 'FailTest
29 ;; (assert-equal #t "string"))
32 ;; Produces the output:
34 ;; PASS: SuccessfulTest
36 ;; assert-equal: expected: #t got: "string"
43 (define-module (unit-test)
44 #:use-module (ice-9 pretty-print)
51 #:export-syntax (skip-test
55 (or (defined? 'define-syntax)
56 (use-modules (ice-9 syncase)))
58 (define *failed-tests* '())
59 (define *passed-tests* '())
60 (define *skipped-tests* '())
62 (define (assert-true result)
65 (throw 'test-failed-exception
66 (simple-format #f " assert-true: got: ~S" result))))
68 (define (assert-equal expected result)
69 (if (equal? expected result)
71 (throw 'test-failed-exception
72 (simple-format #f " assert-equal: expected: ~S got: ~S"
75 (define (%assert-thrown key thunk)
79 (throw 'test-failed-exception
80 (simple-format #f " assert-thrown: expected exception: ~S"
82 (lambda (key . args) #t)))
84 (define (%begin-test name test-thunk)
86 (let ((test-success #t)
91 (set! test-success #f)
93 (if (eqv? key 'test-failed-exception)
95 (format #f " unexpected exception: ~S" (cons key args))))))
99 (format #t "PASS: ~A\n" name)
100 (set! *passed-tests* (cons name *passed-tests*)))
102 (format #t "FAIL: ~A\n" name)
104 (format #t "~A\n" test-fail-msg))
105 (set! *failed-tests* (cons name *failed-tests*))))))
107 (define-syntax begin-test
109 ((_ name . test-forms)
110 (%begin-test name (lambda () . test-forms)))))
112 (define-syntax skip-test
114 ((_ name . test-forms)
116 (format #t "SKIP: ~A\n" name)
117 (set! *skipped-tests* (cons name *skipped-tests*))))))
119 (define-syntax assert-thrown
121 ((_ key . test-forms)
122 (%assert-thrown key (lambda () . test-forms)))))
124 (define (tests-passed?) (null? *failed-tests*))
126 (define (report-tests)
127 (display "Test summary")(newline)
128 (display "Passed: ") (display (length *passed-tests*)) (newline)
129 (display "Failed: ") (display (length *failed-tests*)) (newline)
130 (display "Skipped: ") (display (length *skipped-tests*)) (newline))