chibi-scheme/lib/srfi/16/test.sld
Alex Shinn 4e5cdedc03 Converting tests to modules instead of separate programs.
By convention, a library meant for testing exports "run-tests".
Also by convention, assume the test for (foo bar) is (foo bar-test),
keeping the test in the same directory and avoiding confusion since
(chibi test) is not a test for (chibi).
- Avoids the hack of "load"ing test, with resulting namespace complications.
- Allows keeping tests together with the libraries.
- Allows setting up test hooks before running.
- Allows implicit inference of test locations when using above conventions.
2015-04-05 23:38:51 +09:00

45 lines
1.2 KiB
Scheme

(define-library (srfi 16 test)
(export run-tests)
(import (chibi) (chibi test) (srfi 16))
(begin
(define (run-tests)
(define plus
(case-lambda
(() 0)
((x) x)
((x y) (+ x y))
((x y z) (+ (+ x y) z))
(args (apply + args))))
(test-begin "srfi-16: case-lambda")
(test 0 (plus))
(test 1 (plus 1))
(test 6 (plus 1 2 3))
(test-error ((case-lambda ((a) a) ((a b) (* a b))) 1 2 3))
(define print
(case-lambda
(()
(display ""))
((arg)
(display arg))
((arg . args)
(display arg)
(display " ")
(apply print args))))
(define (print-to-string . args)
(let ((out (open-output-string))
(old-out (current-output-port)))
(dynamic-wind
(lambda () (current-output-port out))
(lambda () (apply print args))
(lambda () (current-output-port old-out)))
(get-output-string out)))
(test "" (print-to-string))
(test "hi" (print-to-string 'hi))
(test "hi there world" (print-to-string 'hi 'there 'world))
(test-end))))