assert.scm 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. ;
  2. ; syntax: assert ?expr ?expr ... [report: ?r-exp ?r-exp ...]
  3. ;
  4. ; If (and ?expr ?expr ...) evaluates to anything but #f, the result
  5. ; is the value of that expression.
  6. ; If (and ?expr ?expr ...) evaluates to #f, an error is reported.
  7. ; The error message will show the failed expressions, as well
  8. ; as the values of selected variables (or expressions, in general).
  9. ; The user may explicitly specify the expressions whose
  10. ; values are to be printed upon assertion failure -- as ?r-exp that
  11. ; follow the identifier 'report:'
  12. ; Typically, ?r-exp is either a variable or a string constant.
  13. ; If the user specified no ?r-exp, the values of variables that are
  14. ; referenced in ?expr will be printed upon the assertion failure.
  15. (define-syntax assert
  16. (syntax-rules (report:)
  17. ((assert "doit" (expr ...) (r-exp ...))
  18. (cond
  19. ((and expr ...) => (lambda (x) x))
  20. (else
  21. (error "assertion failure: ~a" (list '(and expr ...) r-exp ...)))))
  22. ((assert "collect" (expr ...))
  23. (assert "doit" (expr ...) ()))
  24. ((assert "collect" (expr ...) report: r-exp ...)
  25. (assert "doit" (expr ...) (r-exp ...)))
  26. ((assert "collect" (expr ...) expr1 stuff ...)
  27. (assert "collect" (expr ... expr1) stuff ...))
  28. ((assert stuff ...)
  29. (assert "collect" () stuff ...))))
  30. (define-syntax assure
  31. (syntax-rules ()
  32. ((assure exp error-msg)
  33. (assert exp report: error-msg))))