Streamline testing framework.

This commit is contained in:
Dominik Pantůček 2023-03-13 19:21:56 +01:00
parent 4d51b4cc14
commit 83c9a089ee

View file

@ -22,9 +22,10 @@
;; ;;
(module (module
testing testing
(test-eq? test-equal? test-exn) (test-eq? test-equal? test-exn run-tests)
(import scheme) (import scheme
(chicken condition))
;; Evaluates body ... expressions with exception handler installed. ;; Evaluates body ... expressions with exception handler installed.
(define-syntax with-handler (define-syntax with-handler
@ -40,21 +41,23 @@
;; is eq? to expected-result. ;; is eq? to expected-result.
(define-syntax test-eq? (define-syntax test-eq?
(syntax-rules () (syntax-rules ()
((_ name the-test expected-result) ((_ name expression expected-result)
(if (with-handler (lambda (x) #f) (let ((result expression))
(eq? the-test expected-result)) (if (equal? result expected-result)
(display ".") (display ".")
(error 'unit-test name))))) (error 'test-eq? (sprintf "~A expression=~S expected-result=~S result=~S"
'name 'expression expected-result result)))))))
;; Test passes if the-test does not raise an exception and the result ;; Test passes if the-test does not raise an exception and the result
;; is equal? to expected-result. ;; is equal? to expected-result.
(define-syntax test-equal? (define-syntax test-equal?
(syntax-rules () (syntax-rules ()
((_ name the-test expected-result) ((_ name expression expected-result)
(if (with-handler (lambda (x) #f) (let ((result expression))
(eq? the-test expected-result)) (if (equal? result expected-result)
(display ".") (display ".")
(error 'unit-test name))))) (error 'test-equal? (sprintf "~A expression=~S expected-result=~S result=~S"
'name 'expression expected-result result)))))))
;; Passes if the-test raises an exception ;; Passes if the-test raises an exception
(define-syntax test-exn (define-syntax test-exn
@ -65,4 +68,14 @@
(display ".") (display ".")
(error 'unit-test name))))) (error 'unit-test name)))))
;; Displays test specification, evaluates all body ... expressions
;; and prints "ok." at the and of the line if all succeed.
(define-syntax run-tests
(syntax-rules ()
((_ name body ...)
(let ()
(display (sprintf "[test] ~A " 'name))
body ...
(print " ok.")))))
) )