;;AUTOLOAD: T
;;AUTODOC: T "perform regression tests, check for result"
;;(T expression result)
;;
;;Evaluates the (quoted) expression and check that it returns something
;;equal to (evaluated) result
;;A line is printed to indicate success, a more detailed message in case of 
;;error
;;Returns t on success, () on failures
;;T:failures is set to the count of failures
;;
;;e.g.:
;;    (T (+ 2 2) 4)
;;    Test of (+ 2 2): OK!
;;     = t
;;    (T (+ 2 2) 5)
;;    Test of (+ 2 2): FAILED!
;;        Evaluating: (+ 2 2)
;;        Returned:   4
;;        Expecting:  5
;;     = ()
;;
;;SEE_ALSO: E

(defunq T (T:expr T:res)
  (if (loadinfo)
    (print-format "%0:%1: %r2: " (0 (loadinfo)) (1 (loadinfo)) T:expr)
    (print-format "Test of %r0: " T:expr)
  )
  (flush ())
  (setq T:Errres ())
  (with (*error-handlers* T:no-error)
    (setq T:eval 'T:ERROR!)
    (setq T:eval (catch 'ERROR (eval T:expr)))
  )
  (if (= T:eval (setq T:result (eval T:res)))
    (progn (? "OK!\n")
      t
    )
    (progn
      (print-format "\x07FAILED!
    Evaluating: %r0
    Returned:   %1%r2
    Expecting:  %r3\n"
	T:expr 
	(if T:Errres "an error: " "")
	(if T:Errres T:Errres T:eval)
	T:result
      )
      (if (not (typep T:failures Number)) (setq T:failures 0))
      ()
)))

(defun T:no-error (errorcode &rest args)
  (setq T:Errres errorcode)
  :true
)

(defvar T:failures 0)

;;AUTOLOAD: E
;;AUTODOC: E "perform regression tests, check for error"
;;(E expression result)
;;
;;evaluates the (quoted) expression and check that it returns something
;;equal to (evaluated) result
;;a line is printed to indicate success, a more detailed message in case of 
;;error
;;Returns t on success, () on failures
;;T:failures is set to the count of failures
;;e.g.:
;;    (E (/ 2 0) Errors:DivideByZero)
;;    Test of (/ 2 0): OK!
;;     = t
;;    (E (/ 2 1) Errors:DivideByZero)
;;    Test of (/ 2 1): FAILED!
;;        Evaluating: (/ 2 1)
;;        Returned without error!:   2
;;        Expecting the error:  Errors:DivideByZero
;;     = ()
;;
;;SEE_ALSO: T

(defunq E (T:expr T:res &aux T:result)
  (if (loadinfo)
    (print-format "%0:%1: %r2: " (0 (loadinfo)) (1 (loadinfo)) T:expr)
    (print-format "Test of %r0: " T:expr)
  )
  (flush ())
  (setq T:Errres ())
  (with (*error-handlers* T:no-error)
    (setq T:result (catch 'ERROR (eval T:expr)))
  )
  (if (= T:Errres T:res)
    (progn (? "OK!\n")
      t
    )
    (progn
      (print-format "\x07FAILED!
    Evaluating: %r0
    Returned %3:   %r1
    Expecting the error:  %r2\n"
	T:expr (if T:Errres T:Errres T:result) T:res
	(if T:Errres "the error" "without error!")
      )
      (if (not (typep T:failures Number)) (setq T:failures 0))
      ()
)))
