Chapter 5

Extended Example

This example test suite is included in the SchemeUnit distribution as example.ss

;; The tests below are intended as examples of how to use the test
;; package.  They test PLT Scheme's arithmetic operations and some
;; simple file reading

(require  (lib "assert.ss"  "schemeunit"))
(require  (lib "test.ss"    "schemeunit"))
(require  (lib "text-ui.ss" "schemeunit"))

(test/text-ui
 (make-test-suite
  "Example tests"
  (make-test-suite
   "Arithmetic tests"
   (make-test-case "Multiply by zero" 
		   (assert = (* 1234 0) 0))
   (make-test-case "Add zero" 
		   (assert = (+ 1234 0) 1234))
   (make-test-case "Multiply by one"
		   (assert = (* 123.0 1) 123))
   (make-test-case "Add one"
		   (assert = (+ 123.0 1) 124))
   (make-test-case "Expt 0 0"
		   (assert = 1 (expt 0 0)))
   (make-test-case "Expt 0 1"
		   (assert = 0 (expt 0 1)))
   (make-test-case "Expt 0.0 0.0"
		   (assert = 1.0 (expt 0.0 0.0)))
   (make-test-case "Expt 0.0 1.0"
		   (assert = 0.0 (expt 0.0 1.0))))
  (make-test-suite
   "File tests"
   ;; An example with a teardown action
   (let* ((port (open-input-string "this is a test string"))
          (test (lambda () (assert-equal? "this is a test string"
                                          (read-line port))))
          (teardown (lambda () (close-input-port port))))
     (make-test-case "String port read" (test) (teardown)))
   ;; An example with a setup and a teardown action
   (let* ((setup (lambda () 
                   (with-output-to-file "test-file.dat"
                     (lambda ()
                       (display "this is a test string")))))
          (test (lambda () 
                  (assert-equal? "this is a test string"
                                 (with-input-from-file "test-file.dat"
                                   (lambda () (read-line))))))
          (teardown (lambda () (delete-file "test-file.dat"))))
     (make-test-case "File port read" (test) (setup) (teardown))))
  ))