test-framework.lisp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. (defpackage trivial-gray-streams-test
  2. (:use :cl :trivial-gray-streams)
  3. (:shadow :method))
  4. (in-package :trivial-gray-streams-test)
  5. ;;; test framework
  6. (defclass test-result ()
  7. ((name :type symbol
  8. :initarg :name
  9. :initform (error ":name is requierd")
  10. :accessor name)
  11. (status :type (or (eql :ok) (eql :fail))
  12. :initform :ok
  13. :initarg :status
  14. :accessor status)
  15. (cause :type (or null condition)
  16. :initform nil
  17. :initarg :cause
  18. :accessor cause)))
  19. (defun failed-p (test-result)
  20. (eq (status test-result) :ok))
  21. (defmethod print-object ((r test-result) stream)
  22. (print-unreadable-object (r stream :type t)
  23. (format stream "~S ~S~@[ ~A~]" (name r) (status r) (cause r))))
  24. (defparameter *allow-debugger* nil)
  25. (defun test-impl (name body-fn)
  26. (flet ((make-result (status &optional cause)
  27. (make-instance 'test-result :name name :status status :cause cause)))
  28. (handler-bind ((serious-condition
  29. (lambda (c)
  30. (unless *allow-debugger*
  31. (format t "FAIL: ~A~%" c)
  32. (return-from test-impl
  33. (make-result :fail c))))))
  34. (format t "Running test ~S... " name)
  35. (funcall body-fn)
  36. (format t "OK~%")
  37. (make-result :ok))))
  38. (defmacro test ((name) &body body)
  39. "If the BODY signals a SERIOUS-CONDITION
  40. this macro returns a failed TEST-RESULT; otherwise
  41. returns a successfull TEST-RESULT."
  42. `(test-impl (quote ,name) (lambda () ,@body)))
  43. #|
  44. Used like this:
  45. (list (test (a) (assert (= 1 2)))
  46. (test (b) (assert (= 2 2)))
  47. (test (c) (assert (= 2 3))))
  48. => ;; list of test results, 2 failed 1 passed
  49. (#<TEST-RESULT A :FAIL Failed assertion: (= 1 2)> #<TEST-RESULT B :OK> #<TEST-RESULT C :FAIL Failed assertion: (= 2 3)>)
  50. |#