test-framework.lisp 1.9 KB

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