chibi-scheme/lib/chibi/generic-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

37 lines
1.1 KiB
Scheme

(define-library (chibi filesystem-test)
(export run-tests)
(import (chibi) (chibi generic) (chibi test))
(begin
(define (run-tests)
(test-begin "generics")
(let ()
(define-generic add)
(define-method (add (x number?) (y number?))
(+ x y))
(define-method (add (x string?) (y string?))
(string-append x y))
(define-method (add x (y list?))
(append x y))
(test 4 (add 2 2))
(test "22" (add "2" "2"))
(test '(2 2) (add '() '(2 2)))
(test '(2 2) (add '(2) '(2)))
(test '(2 2) (add '(2 2) '()))
(test '(2) (add #f '(2)))
(test-error (add #(2) #(2))))
(let ()
(define-generic mul)
(define-method (mul (x number?) (y number?))
(* x y))
(define-method (mul (x inexact?) (y inexact?))
(+ (* x y) 0.1))
(define-method (mul (x exact?) (y exact?))
(inexact->exact (call-next-method)))
(test 21 (mul 3 7))
(test 21.0 (mul 3.0 7))
(test 21.0 (mul 3 7.0))
(test 21.1 (mul 3.0 7.0)))
(test-end))))