1 ;;; ert-async.el --- Async support for ERT -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2014 Johan Andersson
5 ;; Author: Johan Andersson <johan.rejeep@gmail.com>
6 ;; Maintainer: Johan Andersson <johan.rejeep@gmail.com>
9 ;; URL: http://github.com/rejeep/ert-async.el
11 ;; This file is NOT part of GNU Emacs.
15 ;; This program is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 3, or (at your option)
20 ;; This program is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
36 (defvar ert-async-timeout 10
37 "Number of seconds to wait for callbacks before failing.")
39 (defun ert-async-activate-font-lock-keywords ()
40 "Activate font-lock keywords for `ert-deftest-async'."
41 (font-lock-add-keywords
43 '(("(\\(\\<ert-deftest\\(?:-async\\)?\\)\\>\\s *\\(\\(?:\\sw\\|\\s_\\)+\\)?"
44 (1 font-lock-keyword-face nil t)
45 (2 font-lock-function-name-face nil t)))))
47 (defmacro ert-deftest-async (name callbacks &rest body)
48 "Like `ert-deftest' but with support for async.
50 NAME is the name of the test, which is the first argument to
53 CALLBACKS is a list of callback functions that all must be called
54 before `ert-async-timeout'. If all callback functions have not
55 been called before the timeout, the test fails.
57 The callback functions should be called without any argument. If
58 a callback function is called with a string as argument, the test
59 will fail with that error string.
61 BODY is the actual test."
70 `(lambda (&optional error-message)
72 (ert-fail (format "Callback %s invoked with argument: %s" ',callback error-message))
73 (if (member ',callback callbacked)
74 (ert-fail (format "Callback %s called multiple times" ',callback))
75 (push ',callback callbacked))))))
77 `(ert-deftest ,name ()
81 (ert-fail (format "Timeout of %ds exceeded" ert-async-timeout)))
83 (while (not (equal (sort (mapcar 'symbol-name callbacked) 'string<)
84 (sort (mapcar 'symbol-name ',callbacks) 'string<)))
85 (accept-process-output nil 0.05)))))))
89 ;;; ert-async.el ends here