Function: should-error

should-error is a macro defined in ert.el.gz.

Signature

(should-error FORM &rest KEYWORD-ARGS)

Documentation

Evaluate FORM and check that it signals an error.

If no error was signaled, abort the test as failed and return (ERROR-SYMBOL . DATA) from the error.

You can also match specific errors using the KEYWORD-ARGS arguments, which is specified as keyword/argument pairs. The following arguments are defined:

:type TYPE -- If TYPE is non-nil, the error signaled needs to match
TYPE. TYPE should be a list of condition names. It can also be a symbol, which is equivalent to a one-element list containing that symbol.

:exclude-subtypes EXCLUDED -- If EXCLUDED is non-nil, the error matches
TYPE only if it is an element of TYPE. If nil (the default), the error matches TYPE if one of its condition names is an element of TYPE.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/ert.el.gz
;; FIXME: The expansion will evaluate the keyword args (if any) in
;; nonstandard order.
(cl-defmacro should-error (form &rest keys &key type exclude-subtypes)
  "Evaluate FORM and check that it signals an error.

If no error was signaled, abort the test as failed and
return (ERROR-SYMBOL . DATA) from the error.

You can also match specific errors using the KEYWORD-ARGS arguments,
which is specified as keyword/argument pairs.  The following arguments
are defined:

:type TYPE -- If TYPE is non-nil, the error signaled needs to match
TYPE.  TYPE should be a list of condition names.  It can also be a
symbol, which is equivalent to a one-element list containing that
symbol.

:exclude-subtypes EXCLUDED -- If EXCLUDED is non-nil, the error matches
TYPE only if it is an element of TYPE.  If nil (the default), the error
matches TYPE if one of its condition names is an element of TYPE.

\(fn FORM &rest KEYWORD-ARGS)"
  (declare (debug t))
  (unless type (setq type ''error))
  (ert--expand-should
   `(should-error ,form ,@keys)
   form
   (lambda (inner-form form-description-form value-var)
     (let ((errorp (gensym "errorp"))
           (form-description-fn (gensym "form-description-fn-")))
       `(let ((,errorp nil)
              (,form-description-fn (lambda () ,form-description-form)))
          (condition-case -condition-
              ,inner-form
            ;; We can't use ,type here because we want to evaluate it.
            (error
             (setq ,errorp t)
             (ert--should-error-handle-error ,form-description-fn
                                             -condition-
                                             ,type ,exclude-subtypes)
             (setq ,value-var -condition-)))
          (unless ,errorp
            (ert-fail (append
                       (funcall ,form-description-fn)
                       (list
                        :fail-reason "did not signal an error")))))))))