annotate lisp/emacs-lisp/ert.el @ 112429:6e50030da731

Some copyright.el updates. * lisp/emacs-lisp/copyright.el (copyright-find-copyright): New function, split out from copyright-update-year. (copyright-update): Don't mess with the GPL version if we don't own the copyright. Update license regexp, and remove no longer needed Esperanto stuff.
author Glenn Morris <rgm@gnu.org>
date Sat, 22 Jan 2011 14:09:09 -0800
parents c5917804ecad
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
112201
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1 ;;; ert.el --- Emacs Lisp Regression Testing
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 2007, 2008, 2010, 2011 Free Software Foundation, Inc.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
4
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
5 ;; Author: Christian Ohler <ohler@gnu.org>
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
6 ;; Keywords: lisp, tools
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
7
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
9
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
10 ;; This program is free software: you can redistribute it and/or
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
11 ;; modify it under the terms of the GNU General Public License as
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
12 ;; published by the Free Software Foundation, either version 3 of the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
13 ;; License, or (at your option) any later version.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
14 ;;
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
15 ;; This program is distributed in the hope that it will be useful, but
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
18 ;; General Public License for more details.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
19 ;;
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
21 ;; along with this program. If not, see `http://www.gnu.org/licenses/'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
22
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
23 ;;; Commentary:
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
24
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
25 ;; ERT is a tool for automated testing in Emacs Lisp. Its main
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
26 ;; features are facilities for defining and running test cases and
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
27 ;; reporting the results as well as for debugging test failures
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
28 ;; interactively.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
29 ;;
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
30 ;; The main entry points are `ert-deftest', which is similar to
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
31 ;; `defun' but defines a test, and `ert-run-tests-interactively',
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
32 ;; which runs tests and offers an interactive interface for inspecting
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
33 ;; results and debugging. There is also
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
34 ;; `ert-run-tests-batch-and-exit' for non-interactive use.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
35 ;;
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
36 ;; The body of `ert-deftest' forms resembles a function body, but the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
37 ;; additional operators `should', `should-not' and `should-error' are
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
38 ;; available. `should' is similar to cl's `assert', but signals a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
39 ;; different error when its condition is violated that is caught and
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
40 ;; processed by ERT. In addition, it analyzes its argument form and
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
41 ;; records information that helps debugging (`assert' tries to do
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
42 ;; something similar when its second argument SHOW-ARGS is true, but
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
43 ;; `should' is more sophisticated). For information on `should-not'
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
44 ;; and `should-error', see their docstrings.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
45 ;;
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
46 ;; See ERT's info manual as well as the docstrings for more details.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
47 ;; To compile the manual, run `makeinfo ert.texinfo' in the ERT
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
48 ;; directory, then C-u M-x info ert.info in Emacs to view it.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
49 ;;
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
50 ;; To see some examples of tests written in ERT, see its self-tests in
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
51 ;; ert-tests.el. Some of these are tricky due to the bootstrapping
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
52 ;; problem of writing tests for a testing tool, others test simple
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
53 ;; functions and are straightforward.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
54
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
55 ;;; Code:
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
56
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
57 (eval-when-compile
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
58 (require 'cl))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
59 (require 'button)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
60 (require 'debug)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
61 (require 'easymenu)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
62 (require 'ewoc)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
63 (require 'find-func)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
64 (require 'help)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
65
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
66
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
67 ;;; UI customization options.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
68
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
69 (defgroup ert ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
70 "ERT, the Emacs Lisp regression testing tool."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
71 :prefix "ert-"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
72 :group 'lisp)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
73
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
74 (defface ert-test-result-expected '((((class color) (background light))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
75 :background "green1")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
76 (((class color) (background dark))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
77 :background "green3"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
78 "Face used for expected results in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
79 :group 'ert)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
80
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
81 (defface ert-test-result-unexpected '((((class color) (background light))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
82 :background "red1")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
83 (((class color) (background dark))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
84 :background "red3"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
85 "Face used for unexpected results in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
86 :group 'ert)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
87
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
88
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
89 ;;; Copies/reimplementations of cl functions.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
90
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
91 (defun ert--cl-do-remf (plist tag)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
92 "Copy of `cl-do-remf'. Modify PLIST by removing TAG."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
93 (let ((p (cdr plist)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
94 (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
95 (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
96
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
97 (defun ert--remprop (sym tag)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
98 "Copy of `cl-remprop'. Modify SYM's plist by removing TAG."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
99 (let ((plist (symbol-plist sym)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
100 (if (and plist (eq tag (car plist)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
101 (progn (setplist sym (cdr (cdr plist))) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
102 (ert--cl-do-remf plist tag))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
103
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
104 (defun ert--remove-if-not (ert-pred ert-list)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
105 "A reimplementation of `remove-if-not'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
106
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
107 ERT-PRED is a predicate, ERT-LIST is the input list."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
108 (loop for ert-x in ert-list
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
109 if (funcall ert-pred ert-x)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
110 collect ert-x))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
111
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
112 (defun ert--intersection (a b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
113 "A reimplementation of `intersection'. Intersect the sets A and B.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
114
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
115 Elements are compared using `eql'."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
116 (loop for x in a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
117 if (memql x b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
118 collect x))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
119
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
120 (defun ert--set-difference (a b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
121 "A reimplementation of `set-difference'. Subtract the set B from the set A.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
122
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
123 Elements are compared using `eql'."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
124 (loop for x in a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
125 unless (memql x b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
126 collect x))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
127
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
128 (defun ert--set-difference-eq (a b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
129 "A reimplementation of `set-difference'. Subtract the set B from the set A.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
130
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
131 Elements are compared using `eq'."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
132 (loop for x in a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
133 unless (memq x b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
134 collect x))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
135
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
136 (defun ert--union (a b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
137 "A reimplementation of `union'. Compute the union of the sets A and B.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
138
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
139 Elements are compared using `eql'."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
140 (append a (ert--set-difference b a)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
141
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
142 (eval-and-compile
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
143 (defvar ert--gensym-counter 0))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
144
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
145 (eval-and-compile
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
146 (defun ert--gensym (&optional prefix)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
147 "Only allows string PREFIX, not compatible with CL."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
148 (unless prefix (setq prefix "G"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
149 (make-symbol (format "%s%s"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
150 prefix
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
151 (prog1 ert--gensym-counter
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
152 (incf ert--gensym-counter))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
153
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
154 (defun ert--coerce-to-vector (x)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
155 "Coerce X to a vector."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
156 (when (char-table-p x) (error "Not supported"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
157 (if (vectorp x)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
158 x
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
159 (vconcat x)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
160
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
161 (defun* ert--remove* (x list &key key test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
162 "Does not support all the keywords of remove*."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
163 (unless key (setq key #'identity))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
164 (unless test (setq test #'eql))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
165 (loop for y in list
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
166 unless (funcall test x (funcall key y))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
167 collect y))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
168
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
169 (defun ert--string-position (c s)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
170 "Return the position of the first occurrence of C in S, or nil if none."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
171 (loop for i from 0
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
172 for x across s
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
173 when (eql x c) return i))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
174
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
175 (defun ert--mismatch (a b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
176 "Return index of first element that differs between A and B.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
177
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
178 Like `mismatch'. Uses `equal' for comparison."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
179 (cond ((or (listp a) (listp b))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
180 (ert--mismatch (ert--coerce-to-vector a)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
181 (ert--coerce-to-vector b)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
182 ((> (length a) (length b))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
183 (ert--mismatch b a))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
184 (t
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
185 (let ((la (length a))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
186 (lb (length b)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
187 (assert (arrayp a) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
188 (assert (arrayp b) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
189 (assert (<= la lb) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
190 (loop for i below la
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
191 when (not (equal (aref a i) (aref b i))) return i
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
192 finally (return (if (/= la lb)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
193 la
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
194 (assert (equal a b) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
195 nil)))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
196
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
197 (defun ert--subseq (seq start &optional end)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
198 "Return a subsequence of SEQ from START to END."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
199 (when (char-table-p seq) (error "Not supported"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
200 (let ((vector (substring (ert--coerce-to-vector seq) start end)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
201 (etypecase seq
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
202 (vector vector)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
203 (string (concat vector))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
204 (list (append vector nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
205 (bool-vector (loop with result = (make-bool-vector (length vector) nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
206 for i below (length vector) do
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
207 (setf (aref result i) (aref vector i))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
208 finally (return result)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
209 (char-table (assert nil)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
210
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
211 (defun ert-equal-including-properties (a b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
212 "Return t if A and B have similar structure and contents.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
213
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
214 This is like `equal-including-properties' except that it compares
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
215 the property values of text properties structurally (by
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
216 recursing) rather than with `eq'. Perhaps this is what
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
217 `equal-including-properties' should do in the first place; see
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
218 Emacs bug 6581 at URL `http://debbugs.gnu.org/cgi/bugreport.cgi?bug=6581'."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
219 ;; This implementation is inefficient. Rather than making it
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
220 ;; efficient, let's hope bug 6581 gets fixed so that we can delete
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
221 ;; it altogether.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
222 (not (ert--explain-not-equal-including-properties a b)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
223
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
224
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
225 ;;; Defining and locating tests.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
226
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
227 ;; The data structure that represents a test case.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
228 (defstruct ert-test
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
229 (name nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
230 (documentation nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
231 (body (assert nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
232 (most-recent-result nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
233 (expected-result-type ':passed)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
234 (tags '()))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
235
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
236 (defun ert-test-boundp (symbol)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
237 "Return non-nil if SYMBOL names a test."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
238 (and (get symbol 'ert--test) t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
239
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
240 (defun ert-get-test (symbol)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
241 "If SYMBOL names a test, return that. Signal an error otherwise."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
242 (unless (ert-test-boundp symbol) (error "No test named `%S'" symbol))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
243 (get symbol 'ert--test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
244
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
245 (defun ert-set-test (symbol definition)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
246 "Make SYMBOL name the test DEFINITION, and return DEFINITION."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
247 (when (eq symbol 'nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
248 ;; We disallow nil since `ert-test-at-point' and related functions
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
249 ;; want to return a test name, but also need an out-of-band value
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
250 ;; on failure. Nil is the most natural out-of-band value; using 0
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
251 ;; or "" or signalling an error would be too awkward.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
252 ;;
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
253 ;; Note that nil is still a valid value for the `name' slot in
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
254 ;; ert-test objects. It designates an anonymous test.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
255 (error "Attempt to define a test named nil"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
256 (put symbol 'ert--test definition)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
257 definition)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
258
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
259 (defun ert-make-test-unbound (symbol)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
260 "Make SYMBOL name no test. Return SYMBOL."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
261 (ert--remprop symbol 'ert--test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
262 symbol)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
263
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
264 (defun ert--parse-keys-and-body (keys-and-body)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
265 "Split KEYS-AND-BODY into keyword-and-value pairs and the remaining body.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
266
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
267 KEYS-AND-BODY should have the form of a property list, with the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
268 exception that only keywords are permitted as keys and that the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
269 tail -- the body -- is a list of forms that does not start with a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
270 keyword.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
271
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
272 Returns a two-element list containing the keys-and-values plist
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
273 and the body."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
274 (let ((extracted-key-accu '())
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
275 (remaining keys-and-body))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
276 (while (and (consp remaining) (keywordp (first remaining)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
277 (let ((keyword (pop remaining)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
278 (unless (consp remaining)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
279 (error "Value expected after keyword %S in %S"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
280 keyword keys-and-body))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
281 (when (assoc keyword extracted-key-accu)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
282 (warn "Keyword %S appears more than once in %S" keyword
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
283 keys-and-body))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
284 (push (cons keyword (pop remaining)) extracted-key-accu)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
285 (setq extracted-key-accu (nreverse extracted-key-accu))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
286 (list (loop for (key . value) in extracted-key-accu
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
287 collect key
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
288 collect value)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
289 remaining)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
290
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
291 ;;;###autoload
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
292 (defmacro* ert-deftest (name () &body docstring-keys-and-body)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
293 "Define NAME (a symbol) as a test.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
294
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
295 BODY is evaluated as a `progn' when the test is run. It should
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
296 signal a condition on failure or just return if the test passes.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
297
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
298 `should', `should-not' and `should-error' are useful for
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
299 assertions in BODY.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
300
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
301 Use `ert' to run tests interactively.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
302
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
303 Tests that are expected to fail can be marked as such
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
304 using :expected-result. See `ert-test-result-type-p' for a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
305 description of valid values for RESULT-TYPE.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
306
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
307 \(fn NAME () [DOCSTRING] [:expected-result RESULT-TYPE] \
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
308 \[:tags '(TAG...)] BODY...)"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
309 (declare (debug (&define :name test
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
310 name sexp [&optional stringp]
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
311 [&rest keywordp sexp] def-body))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
312 (doc-string 3)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
313 (indent 2))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
314 (let ((documentation nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
315 (documentation-supplied-p nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
316 (when (stringp (first docstring-keys-and-body))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
317 (setq documentation (pop docstring-keys-and-body)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
318 documentation-supplied-p t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
319 (destructuring-bind ((&key (expected-result nil expected-result-supplied-p)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
320 (tags nil tags-supplied-p))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
321 body)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
322 (ert--parse-keys-and-body docstring-keys-and-body)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
323 `(progn
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
324 (ert-set-test ',name
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
325 (make-ert-test
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
326 :name ',name
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
327 ,@(when documentation-supplied-p
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
328 `(:documentation ,documentation))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
329 ,@(when expected-result-supplied-p
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
330 `(:expected-result-type ,expected-result))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
331 ,@(when tags-supplied-p
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
332 `(:tags ,tags))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
333 :body (lambda () ,@body)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
334 ;; This hack allows `symbol-file' to associate `ert-deftest'
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
335 ;; forms with files, and therefore enables `find-function' to
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
336 ;; work with tests. However, it leads to warnings in
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
337 ;; `unload-feature', which doesn't know how to undefine tests
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
338 ;; and has no mechanism for extension.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
339 (push '(ert-deftest . ,name) current-load-list)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
340 ',name))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
341
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
342 ;; We use these `put' forms in addition to the (declare (indent)) in
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
343 ;; the defmacro form since the `declare' alone does not lead to
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
344 ;; correct indentation before the .el/.elc file is loaded.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
345 ;; Autoloading these `put' forms solves this.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
346 ;;;###autoload
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
347 (progn
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
348 ;; TODO(ohler): Figure out what these mean and make sure they are correct.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
349 (put 'ert-deftest 'lisp-indent-function 2)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
350 (put 'ert-info 'lisp-indent-function 1))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
351
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
352 (defvar ert--find-test-regexp
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
353 (concat "^\\s-*(ert-deftest"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
354 find-function-space-re
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
355 "%s\\(\\s-\\|$\\)")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
356 "The regexp the `find-function' mechanisms use for finding test definitions.")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
357
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
358
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
359 (put 'ert-test-failed 'error-conditions '(error ert-test-failed))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
360 (put 'ert-test-failed 'error-message "Test failed")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
361
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
362 (defun ert-pass ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
363 "Terminate the current test and mark it passed. Does not return."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
364 (throw 'ert--pass nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
365
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
366 (defun ert-fail (data)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
367 "Terminate the current test and mark it failed. Does not return.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
368 DATA is displayed to the user and should state the reason of the failure."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
369 (signal 'ert-test-failed (list data)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
370
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
371
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
372 ;;; The `should' macros.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
373
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
374 (defvar ert--should-execution-observer nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
375
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
376 (defun ert--signal-should-execution (form-description)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
377 "Tell the current `should' form observer (if any) about FORM-DESCRIPTION."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
378 (when ert--should-execution-observer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
379 (funcall ert--should-execution-observer form-description)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
380
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
381 (defun ert--special-operator-p (thing)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
382 "Return non-nil if THING is a symbol naming a special operator."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
383 (and (symbolp thing)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
384 (let ((definition (indirect-function thing t)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
385 (and (subrp definition)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
386 (eql (cdr (subr-arity definition)) 'unevalled)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
387
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
388 (defun ert--expand-should-1 (whole form inner-expander)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
389 "Helper function for the `should' macro and its variants."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
390 (let ((form
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
391 ;; If `cl-macroexpand' isn't bound, the code that we're
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
392 ;; compiling doesn't depend on cl and thus doesn't need an
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
393 ;; environment arg for `macroexpand'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
394 (if (fboundp 'cl-macroexpand)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
395 ;; Suppress warning about run-time call to cl funtion: we
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
396 ;; only call it if it's fboundp.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
397 (with-no-warnings
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
398 (cl-macroexpand form (and (boundp 'cl-macro-environment)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
399 cl-macro-environment)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
400 (macroexpand form))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
401 (cond
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
402 ((or (atom form) (ert--special-operator-p (car form)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
403 (let ((value (ert--gensym "value-")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
404 `(let ((,value (ert--gensym "ert-form-evaluation-aborted-")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
405 ,(funcall inner-expander
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
406 `(setq ,value ,form)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
407 `(list ',whole :form ',form :value ,value)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
408 value)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
409 ,value)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
410 (t
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
411 (let ((fn-name (car form))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
412 (arg-forms (cdr form)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
413 (assert (or (symbolp fn-name)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
414 (and (consp fn-name)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
415 (eql (car fn-name) 'lambda)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
416 (listp (cdr fn-name)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
417 (let ((fn (ert--gensym "fn-"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
418 (args (ert--gensym "args-"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
419 (value (ert--gensym "value-"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
420 (default-value (ert--gensym "ert-form-evaluation-aborted-")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
421 `(let ((,fn (function ,fn-name))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
422 (,args (list ,@arg-forms)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
423 (let ((,value ',default-value))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
424 ,(funcall inner-expander
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
425 `(setq ,value (apply ,fn ,args))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
426 `(nconc (list ',whole)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
427 (list :form `(,,fn ,@,args))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
428 (unless (eql ,value ',default-value)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
429 (list :value ,value))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
430 (let ((-explainer-
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
431 (and (symbolp ',fn-name)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
432 (get ',fn-name 'ert-explainer))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
433 (when -explainer-
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
434 (list :explanation
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
435 (apply -explainer- ,args)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
436 value)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
437 ,value))))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
438
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
439 (defun ert--expand-should (whole form inner-expander)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
440 "Helper function for the `should' macro and its variants.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
441
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
442 Analyzes FORM and returns an expression that has the same
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
443 semantics under evaluation but records additional debugging
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
444 information.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
445
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
446 INNER-EXPANDER should be a function and is called with two
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
447 arguments: INNER-FORM and FORM-DESCRIPTION-FORM, where INNER-FORM
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
448 is an expression equivalent to FORM, and FORM-DESCRIPTION-FORM is
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
449 an expression that returns a description of FORM. INNER-EXPANDER
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
450 should return code that calls INNER-FORM and performs the checks
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
451 and error signalling specific to the particular variant of
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
452 `should'. The code that INNER-EXPANDER returns must not call
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
453 FORM-DESCRIPTION-FORM before it has called INNER-FORM."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
454 (lexical-let ((inner-expander inner-expander))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
455 (ert--expand-should-1
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
456 whole form
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
457 (lambda (inner-form form-description-form value-var)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
458 (let ((form-description (ert--gensym "form-description-")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
459 `(let (,form-description)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
460 ,(funcall inner-expander
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
461 `(unwind-protect
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
462 ,inner-form
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
463 (setq ,form-description ,form-description-form)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
464 (ert--signal-should-execution ,form-description))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
465 `,form-description
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
466 value-var)))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
467
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
468 (defmacro* should (form)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
469 "Evaluate FORM. If it returns nil, abort the current test as failed.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
470
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
471 Returns the value of FORM."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
472 (ert--expand-should `(should ,form) form
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
473 (lambda (inner-form form-description-form value-var)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
474 `(unless ,inner-form
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
475 (ert-fail ,form-description-form)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
476
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
477 (defmacro* should-not (form)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
478 "Evaluate FORM. If it returns non-nil, abort the current test as failed.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
479
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
480 Returns nil."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
481 (ert--expand-should `(should-not ,form) form
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
482 (lambda (inner-form form-description-form value-var)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
483 `(unless (not ,inner-form)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
484 (ert-fail ,form-description-form)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
485
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
486 (defun ert--should-error-handle-error (form-description-fn
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
487 condition type exclude-subtypes)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
488 "Helper function for `should-error'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
489
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
490 Determines whether CONDITION matches TYPE and EXCLUDE-SUBTYPES,
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
491 and aborts the current test as failed if it doesn't."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
492 (let ((signalled-conditions (get (car condition) 'error-conditions))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
493 (handled-conditions (etypecase type
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
494 (list type)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
495 (symbol (list type)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
496 (assert signalled-conditions)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
497 (unless (ert--intersection signalled-conditions handled-conditions)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
498 (ert-fail (append
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
499 (funcall form-description-fn)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
500 (list
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
501 :condition condition
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
502 :fail-reason (concat "the error signalled did not"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
503 " have the expected type")))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
504 (when exclude-subtypes
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
505 (unless (member (car condition) handled-conditions)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
506 (ert-fail (append
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
507 (funcall form-description-fn)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
508 (list
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
509 :condition condition
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
510 :fail-reason (concat "the error signalled was a subtype"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
511 " of the expected type"))))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
512
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
513 ;; FIXME: The expansion will evaluate the keyword args (if any) in
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
514 ;; nonstandard order.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
515 (defmacro* should-error (form &rest keys &key type exclude-subtypes)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
516 "Evaluate FORM and check that it signals an error.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
517
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
518 The error signalled needs to match TYPE. TYPE should be a list
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
519 of condition names. (It can also be a non-nil symbol, which is
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
520 equivalent to a singleton list containing that symbol.) If
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
521 EXCLUDE-SUBTYPES is nil, the error matches TYPE if one of its
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
522 condition names is an element of TYPE. If EXCLUDE-SUBTYPES is
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
523 non-nil, the error matches TYPE if it is an element of TYPE.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
524
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
525 If the error matches, returns (ERROR-SYMBOL . DATA) from the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
526 error. If not, or if no error was signalled, abort the test as
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
527 failed."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
528 (unless type (setq type ''error))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
529 (ert--expand-should
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
530 `(should-error ,form ,@keys)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
531 form
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
532 (lambda (inner-form form-description-form value-var)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
533 (let ((errorp (ert--gensym "errorp"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
534 (form-description-fn (ert--gensym "form-description-fn-")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
535 `(let ((,errorp nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
536 (,form-description-fn (lambda () ,form-description-form)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
537 (condition-case -condition-
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
538 ,inner-form
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
539 ;; We can't use ,type here because we want to evaluate it.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
540 (error
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
541 (setq ,errorp t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
542 (ert--should-error-handle-error ,form-description-fn
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
543 -condition-
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
544 ,type ,exclude-subtypes)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
545 (setq ,value-var -condition-)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
546 (unless ,errorp
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
547 (ert-fail (append
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
548 (funcall ,form-description-fn)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
549 (list
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
550 :fail-reason "did not signal an error")))))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
551
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
552
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
553 ;;; Explanation of `should' failures.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
554
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
555 ;; TODO(ohler): Rework explanations so that they are displayed in a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
556 ;; similar way to `ert-info' messages; in particular, allow text
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
557 ;; buttons in explanations that give more detail or open an ediff
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
558 ;; buffer. Perhaps explanations should be reported through `ert-info'
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
559 ;; rather than as part of the condition.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
560
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
561 (defun ert--proper-list-p (x)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
562 "Return non-nil if X is a proper list, nil otherwise."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
563 (loop
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
564 for firstp = t then nil
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
565 for fast = x then (cddr fast)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
566 for slow = x then (cdr slow) do
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
567 (when (null fast) (return t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
568 (when (not (consp fast)) (return nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
569 (when (null (cdr fast)) (return t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
570 (when (not (consp (cdr fast))) (return nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
571 (when (and (not firstp) (eq fast slow)) (return nil))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
572
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
573 (defun ert--explain-format-atom (x)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
574 "Format the atom X for `ert--explain-not-equal'."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
575 (typecase x
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
576 (fixnum (list x (format "#x%x" x) (format "?%c" x)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
577 (t x)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
578
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
579 (defun ert--explain-not-equal (a b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
580 "Explainer function for `equal'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
581
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
582 Returns a programmer-readable explanation of why A and B are not
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
583 `equal', or nil if they are."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
584 (if (not (equal (type-of a) (type-of b)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
585 `(different-types ,a ,b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
586 (etypecase a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
587 (cons
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
588 (let ((a-proper-p (ert--proper-list-p a))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
589 (b-proper-p (ert--proper-list-p b)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
590 (if (not (eql (not a-proper-p) (not b-proper-p)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
591 `(one-list-proper-one-improper ,a ,b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
592 (if a-proper-p
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
593 (if (not (equal (length a) (length b)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
594 `(proper-lists-of-different-length ,(length a) ,(length b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
595 ,a ,b
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
596 first-mismatch-at
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
597 ,(ert--mismatch a b))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
598 (loop for i from 0
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
599 for ai in a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
600 for bi in b
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
601 for xi = (ert--explain-not-equal ai bi)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
602 do (when xi (return `(list-elt ,i ,xi)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
603 finally (assert (equal a b) t)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
604 (let ((car-x (ert--explain-not-equal (car a) (car b))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
605 (if car-x
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
606 `(car ,car-x)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
607 (let ((cdr-x (ert--explain-not-equal (cdr a) (cdr b))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
608 (if cdr-x
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
609 `(cdr ,cdr-x)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
610 (assert (equal a b) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
611 nil))))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
612 (array (if (not (equal (length a) (length b)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
613 `(arrays-of-different-length ,(length a) ,(length b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
614 ,a ,b
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
615 ,@(unless (char-table-p a)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
616 `(first-mismatch-at
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
617 ,(ert--mismatch a b))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
618 (loop for i from 0
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
619 for ai across a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
620 for bi across b
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
621 for xi = (ert--explain-not-equal ai bi)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
622 do (when xi (return `(array-elt ,i ,xi)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
623 finally (assert (equal a b) t))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
624 (atom (if (not (equal a b))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
625 (if (and (symbolp a) (symbolp b) (string= a b))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
626 `(different-symbols-with-the-same-name ,a ,b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
627 `(different-atoms ,(ert--explain-format-atom a)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
628 ,(ert--explain-format-atom b)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
629 nil)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
630 (put 'equal 'ert-explainer 'ert--explain-not-equal)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
631
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
632 (defun ert--significant-plist-keys (plist)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
633 "Return the keys of PLIST that have non-null values, in order."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
634 (assert (zerop (mod (length plist) 2)) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
635 (loop for (key value . rest) on plist by #'cddr
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
636 unless (or (null value) (memq key accu)) collect key into accu
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
637 finally (return accu)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
638
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
639 (defun ert--plist-difference-explanation (a b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
640 "Return a programmer-readable explanation of why A and B are different plists.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
641
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
642 Returns nil if they are equivalent, i.e., have the same value for
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
643 each key, where absent values are treated as nil. The order of
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
644 key/value pairs in each list does not matter."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
645 (assert (zerop (mod (length a) 2)) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
646 (assert (zerop (mod (length b) 2)) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
647 ;; Normalizing the plists would be another way to do this but it
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
648 ;; requires a total ordering on all lisp objects (since any object
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
649 ;; is valid as a text property key). Perhaps defining such an
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
650 ;; ordering is useful in other contexts, too, but it's a lot of
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
651 ;; work, so let's punt on it for now.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
652 (let* ((keys-a (ert--significant-plist-keys a))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
653 (keys-b (ert--significant-plist-keys b))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
654 (keys-in-a-not-in-b (ert--set-difference-eq keys-a keys-b))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
655 (keys-in-b-not-in-a (ert--set-difference-eq keys-b keys-a)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
656 (flet ((explain-with-key (key)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
657 (let ((value-a (plist-get a key))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
658 (value-b (plist-get b key)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
659 (assert (not (equal value-a value-b)) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
660 `(different-properties-for-key
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
661 ,key ,(ert--explain-not-equal-including-properties value-a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
662 value-b)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
663 (cond (keys-in-a-not-in-b
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
664 (explain-with-key (first keys-in-a-not-in-b)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
665 (keys-in-b-not-in-a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
666 (explain-with-key (first keys-in-b-not-in-a)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
667 (t
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
668 (loop for key in keys-a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
669 when (not (equal (plist-get a key) (plist-get b key)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
670 return (explain-with-key key)))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
671
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
672 (defun ert--abbreviate-string (s len suffixp)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
673 "Shorten string S to at most LEN chars.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
674
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
675 If SUFFIXP is non-nil, returns a suffix of S, otherwise a prefix."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
676 (let ((n (length s)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
677 (cond ((< n len)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
678 s)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
679 (suffixp
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
680 (substring s (- n len)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
681 (t
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
682 (substring s 0 len)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
683
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
684 (defun ert--explain-not-equal-including-properties (a b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
685 "Explainer function for `ert-equal-including-properties'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
686
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
687 Returns a programmer-readable explanation of why A and B are not
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
688 `ert-equal-including-properties', or nil if they are."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
689 (if (not (equal a b))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
690 (ert--explain-not-equal a b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
691 (assert (stringp a) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
692 (assert (stringp b) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
693 (assert (eql (length a) (length b)) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
694 (loop for i from 0 to (length a)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
695 for props-a = (text-properties-at i a)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
696 for props-b = (text-properties-at i b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
697 for difference = (ert--plist-difference-explanation props-a props-b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
698 do (when difference
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
699 (return `(char ,i ,(substring-no-properties a i (1+ i))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
700 ,difference
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
701 context-before
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
702 ,(ert--abbreviate-string
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
703 (substring-no-properties a 0 i)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
704 10 t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
705 context-after
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
706 ,(ert--abbreviate-string
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
707 (substring-no-properties a (1+ i))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
708 10 nil))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
709 ;; TODO(ohler): Get `equal-including-properties' fixed in
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
710 ;; Emacs, delete `ert-equal-including-properties', and
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
711 ;; re-enable this assertion.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
712 ;;finally (assert (equal-including-properties a b) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
713 )))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
714 (put 'ert-equal-including-properties
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
715 'ert-explainer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
716 'ert--explain-not-equal-including-properties)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
717
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
718
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
719 ;;; Implementation of `ert-info'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
720
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
721 ;; TODO(ohler): The name `info' clashes with
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
722 ;; `ert--test-execution-info'. One or both should be renamed.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
723 (defvar ert--infos '()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
724 "The stack of `ert-info' infos that currently apply.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
725
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
726 Bound dynamically. This is a list of (PREFIX . MESSAGE) pairs.")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
727
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
728 (defmacro* ert-info ((message-form &key ((:prefix prefix-form) "Info: "))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
729 &body body)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
730 "Evaluate MESSAGE-FORM and BODY, and report the message if BODY fails.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
731
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
732 To be used within ERT tests. MESSAGE-FORM should evaluate to a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
733 string that will be displayed together with the test result if
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
734 the test fails. PREFIX-FORM should evaluate to a string as well
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
735 and is displayed in front of the value of MESSAGE-FORM."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
736 (declare (debug ((form &rest [sexp form]) body))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
737 (indent 1))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
738 `(let ((ert--infos (cons (cons ,prefix-form ,message-form) ert--infos)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
739 ,@body))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
740
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
741
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
742
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
743 ;;; Facilities for running a single test.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
744
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
745 (defvar ert-debug-on-error nil
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
746 "Non-nil means enter debugger when a test fails or terminates with an error.")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
747
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
748 ;; The data structures that represent the result of running a test.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
749 (defstruct ert-test-result
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
750 (messages nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
751 (should-forms nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
752 )
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
753 (defstruct (ert-test-passed (:include ert-test-result)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
754 (defstruct (ert-test-result-with-condition (:include ert-test-result))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
755 (condition (assert nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
756 (backtrace (assert nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
757 (infos (assert nil)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
758 (defstruct (ert-test-quit (:include ert-test-result-with-condition)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
759 (defstruct (ert-test-failed (:include ert-test-result-with-condition)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
760 (defstruct (ert-test-aborted-with-non-local-exit (:include ert-test-result)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
761
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
762
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
763 (defun ert--record-backtrace ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
764 "Record the current backtrace (as a list) and return it."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
765 ;; Since the backtrace is stored in the result object, result
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
766 ;; objects must only be printed with appropriate limits
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
767 ;; (`print-level' and `print-length') in place. For interactive
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
768 ;; use, the cost of ensuring this possibly outweighs the advantage
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
769 ;; of storing the backtrace for
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
770 ;; `ert-results-pop-to-backtrace-for-test-at-point' given that we
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
771 ;; already have `ert-results-rerun-test-debugging-errors-at-point'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
772 ;; For batch use, however, printing the backtrace may be useful.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
773 (loop
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
774 ;; 6 is the number of frames our own debugger adds (when
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
775 ;; compiled; more when interpreted). FIXME: Need to describe a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
776 ;; procedure for determining this constant.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
777 for i from 6
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
778 for frame = (backtrace-frame i)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
779 while frame
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
780 collect frame))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
781
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
782 (defun ert--print-backtrace (backtrace)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
783 "Format the backtrace BACKTRACE to the current buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
784 ;; This is essentially a reimplementation of Fbacktrace
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
785 ;; (src/eval.c), but for a saved backtrace, not the current one.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
786 (let ((print-escape-newlines t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
787 (print-level 8)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
788 (print-length 50))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
789 (dolist (frame backtrace)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
790 (ecase (first frame)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
791 ((nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
792 ;; Special operator.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
793 (destructuring-bind (special-operator &rest arg-forms)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
794 (cdr frame)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
795 (insert
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
796 (format " %S\n" (list* special-operator arg-forms)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
797 ((t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
798 ;; Function call.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
799 (destructuring-bind (fn &rest args) (cdr frame)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
800 (insert (format " %S(" fn))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
801 (loop for firstp = t then nil
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
802 for arg in args do
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
803 (unless firstp
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
804 (insert " "))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
805 (insert (format "%S" arg)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
806 (insert ")\n")))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
807
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
808 ;; A container for the state of the execution of a single test and
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
809 ;; environment data needed during its execution.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
810 (defstruct ert--test-execution-info
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
811 (test (assert nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
812 (result (assert nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
813 ;; A thunk that may be called when RESULT has been set to its final
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
814 ;; value and test execution should be terminated. Should not
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
815 ;; return.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
816 (exit-continuation (assert nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
817 ;; The binding of `debugger' outside of the execution of the test.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
818 next-debugger
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
819 ;; The binding of `ert-debug-on-error' that is in effect for the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
820 ;; execution of the current test. We store it to avoid being
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
821 ;; affected by any new bindings the test itself may establish. (I
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
822 ;; don't remember whether this feature is important.)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
823 ert-debug-on-error)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
824
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
825 (defun ert--run-test-debugger (info debugger-args)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
826 "During a test run, `debugger' is bound to a closure that calls this function.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
827
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
828 This function records failures and errors and either terminates
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
829 the test silently or calls the interactive debugger, as
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
830 appropriate.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
831
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
832 INFO is the ert--test-execution-info corresponding to this test
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
833 run. DEBUGGER-ARGS are the arguments to `debugger'."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
834 (destructuring-bind (first-debugger-arg &rest more-debugger-args)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
835 debugger-args
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
836 (ecase first-debugger-arg
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
837 ((lambda debug t exit nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
838 (apply (ert--test-execution-info-next-debugger info) debugger-args))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
839 (error
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
840 (let* ((condition (first more-debugger-args))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
841 (type (case (car condition)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
842 ((quit) 'quit)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
843 (otherwise 'failed)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
844 (backtrace (ert--record-backtrace))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
845 (infos (reverse ert--infos)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
846 (setf (ert--test-execution-info-result info)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
847 (ecase type
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
848 (quit
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
849 (make-ert-test-quit :condition condition
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
850 :backtrace backtrace
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
851 :infos infos))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
852 (failed
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
853 (make-ert-test-failed :condition condition
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
854 :backtrace backtrace
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
855 :infos infos))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
856 ;; Work around Emacs' heuristic (in eval.c) for detecting
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
857 ;; errors in the debugger.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
858 (incf num-nonmacro-input-events)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
859 ;; FIXME: We should probably implement more fine-grained
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
860 ;; control a la non-t `debug-on-error' here.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
861 (cond
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
862 ((ert--test-execution-info-ert-debug-on-error info)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
863 (apply (ert--test-execution-info-next-debugger info) debugger-args))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
864 (t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
865 (funcall (ert--test-execution-info-exit-continuation info)))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
866
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
867 (defun ert--run-test-internal (ert-test-execution-info)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
868 "Low-level function to run a test according to ERT-TEST-EXECUTION-INFO.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
869
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
870 This mainly sets up debugger-related bindings."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
871 (lexical-let ((info ert-test-execution-info))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
872 (setf (ert--test-execution-info-next-debugger info) debugger
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
873 (ert--test-execution-info-ert-debug-on-error info) ert-debug-on-error)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
874 (catch 'ert--pass
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
875 ;; For now, each test gets its own temp buffer and its own
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
876 ;; window excursion, just to be safe. If this turns out to be
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
877 ;; too expensive, we can remove it.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
878 (with-temp-buffer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
879 (save-window-excursion
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
880 (let ((debugger (lambda (&rest debugger-args)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
881 (ert--run-test-debugger info debugger-args)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
882 (debug-on-error t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
883 (debug-on-quit t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
884 ;; FIXME: Do we need to store the old binding of this
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
885 ;; and consider it in `ert--run-test-debugger'?
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
886 (debug-ignored-errors nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
887 (ert--infos '()))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
888 (funcall (ert-test-body (ert--test-execution-info-test info))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
889 (ert-pass))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
890 (setf (ert--test-execution-info-result info) (make-ert-test-passed)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
891 nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
892
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
893 (defun ert--force-message-log-buffer-truncation ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
894 "Immediately truncate *Messages* buffer according to `message-log-max'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
895
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
896 This can be useful after reducing the value of `message-log-max'."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
897 (with-current-buffer (get-buffer-create "*Messages*")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
898 ;; This is a reimplementation of this part of message_dolog() in xdisp.c:
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
899 ;; if (NATNUMP (Vmessage_log_max))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
900 ;; {
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
901 ;; scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
902 ;; -XFASTINT (Vmessage_log_max) - 1, 0);
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
903 ;; del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
904 ;; }
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
905 (when (and (integerp message-log-max) (>= message-log-max 0))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
906 (let ((begin (point-min))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
907 (end (save-excursion
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
908 (goto-char (point-max))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
909 (forward-line (- message-log-max))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
910 (point))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
911 (delete-region begin end)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
912
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
913 (defvar ert--running-tests nil
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
914 "List of tests that are currently in execution.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
915
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
916 This list is empty while no test is running, has one element
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
917 while a test is running, two elements while a test run from
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
918 inside a test is running, etc. The list is in order of nesting,
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
919 innermost test first.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
920
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
921 The elements are of type `ert-test'.")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
922
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
923 (defun ert-run-test (ert-test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
924 "Run ERT-TEST.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
925
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
926 Returns the result and stores it in ERT-TEST's `most-recent-result' slot."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
927 (setf (ert-test-most-recent-result ert-test) nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
928 (block error
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
929 (lexical-let ((begin-marker
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
930 (with-current-buffer (get-buffer-create "*Messages*")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
931 (set-marker (make-marker) (point-max)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
932 (unwind-protect
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
933 (lexical-let ((info (make-ert--test-execution-info
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
934 :test ert-test
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
935 :result
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
936 (make-ert-test-aborted-with-non-local-exit)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
937 :exit-continuation (lambda ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
938 (return-from error nil))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
939 (should-form-accu (list)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
940 (unwind-protect
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
941 (let ((ert--should-execution-observer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
942 (lambda (form-description)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
943 (push form-description should-form-accu)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
944 (message-log-max t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
945 (ert--running-tests (cons ert-test ert--running-tests)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
946 (ert--run-test-internal info))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
947 (let ((result (ert--test-execution-info-result info)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
948 (setf (ert-test-result-messages result)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
949 (with-current-buffer (get-buffer-create "*Messages*")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
950 (buffer-substring begin-marker (point-max))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
951 (ert--force-message-log-buffer-truncation)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
952 (setq should-form-accu (nreverse should-form-accu))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
953 (setf (ert-test-result-should-forms result)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
954 should-form-accu)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
955 (setf (ert-test-most-recent-result ert-test) result))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
956 (set-marker begin-marker nil))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
957 (ert-test-most-recent-result ert-test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
958
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
959 (defun ert-running-test ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
960 "Return the top-level test currently executing."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
961 (car (last ert--running-tests)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
962
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
963
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
964 ;;; Test selectors.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
965
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
966 (defun ert-test-result-type-p (result result-type)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
967 "Return non-nil if RESULT matches type RESULT-TYPE.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
968
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
969 Valid result types:
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
970
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
971 nil -- Never matches.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
972 t -- Always matches.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
973 :failed, :passed -- Matches corresponding results.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
974 \(and TYPES...\) -- Matches if all TYPES match.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
975 \(or TYPES...\) -- Matches if some TYPES match.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
976 \(not TYPE\) -- Matches if TYPE does not match.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
977 \(satisfies PREDICATE\) -- Matches if PREDICATE returns true when called with
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
978 RESULT."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
979 ;; It would be easy to add `member' and `eql' types etc., but I
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
980 ;; haven't bothered yet.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
981 (etypecase result-type
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
982 ((member nil) nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
983 ((member t) t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
984 ((member :failed) (ert-test-failed-p result))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
985 ((member :passed) (ert-test-passed-p result))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
986 (cons
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
987 (destructuring-bind (operator &rest operands) result-type
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
988 (ecase operator
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
989 (and
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
990 (case (length operands)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
991 (0 t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
992 (t
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
993 (and (ert-test-result-type-p result (first operands))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
994 (ert-test-result-type-p result `(and ,@(rest operands)))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
995 (or
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
996 (case (length operands)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
997 (0 nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
998 (t
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
999 (or (ert-test-result-type-p result (first operands))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1000 (ert-test-result-type-p result `(or ,@(rest operands)))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1001 (not
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1002 (assert (eql (length operands) 1))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1003 (not (ert-test-result-type-p result (first operands))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1004 (satisfies
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1005 (assert (eql (length operands) 1))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1006 (funcall (first operands) result)))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1007
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1008 (defun ert-test-result-expected-p (test result)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1009 "Return non-nil if TEST's expected result type matches RESULT."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1010 (ert-test-result-type-p result (ert-test-expected-result-type test)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1011
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1012 (defun ert-select-tests (selector universe)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1013 "Return the tests that match SELECTOR.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1014
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1015 UNIVERSE specifies the set of tests to select from; it should be
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1016 a list of tests, or t, which refers to all tests named by symbols
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1017 in `obarray'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1018
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1019 Returns the set of tests as a list.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1020
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1021 Valid selectors:
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1022
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1023 nil -- Selects the empty set.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1024 t -- Selects UNIVERSE.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1025 :new -- Selects all tests that have not been run yet.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1026 :failed, :passed -- Select tests according to their most recent result.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1027 :expected, :unexpected -- Select tests according to their most recent result.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1028 a string -- Selects all tests that have a name that matches the string,
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1029 a regexp.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1030 a test -- Selects that test.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1031 a symbol -- Selects the test that the symbol names, errors if none.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1032 \(member TESTS...\) -- Selects TESTS, a list of tests or symbols naming tests.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1033 \(eql TEST\) -- Selects TEST, a test or a symbol naming a test.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1034 \(and SELECTORS...\) -- Selects the tests that match all SELECTORS.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1035 \(or SELECTORS...\) -- Selects the tests that match any SELECTOR.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1036 \(not SELECTOR\) -- Selects all tests that do not match SELECTOR.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1037 \(tag TAG) -- Selects all tests that have TAG on their tags list.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1038 \(satisfies PREDICATE\) -- Selects all tests that satisfy PREDICATE.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1039
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1040 Only selectors that require a superset of tests, such
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1041 as (satisfies ...), strings, :new, etc. make use of UNIVERSE.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1042 Selectors that do not, such as \(member ...\), just return the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1043 set implied by them without checking whether it is really
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1044 contained in UNIVERSE."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1045 ;; This code needs to match the etypecase in
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1046 ;; `ert-insert-human-readable-selector'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1047 (etypecase selector
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1048 ((member nil) nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1049 ((member t) (etypecase universe
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1050 (list universe)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1051 ((member t) (ert-select-tests "" universe))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1052 ((member :new) (ert-select-tests
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1053 `(satisfies ,(lambda (test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1054 (null (ert-test-most-recent-result test))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1055 universe))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1056 ((member :failed) (ert-select-tests
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1057 `(satisfies ,(lambda (test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1058 (ert-test-result-type-p
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1059 (ert-test-most-recent-result test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1060 ':failed)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1061 universe))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1062 ((member :passed) (ert-select-tests
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1063 `(satisfies ,(lambda (test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1064 (ert-test-result-type-p
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1065 (ert-test-most-recent-result test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1066 ':passed)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1067 universe))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1068 ((member :expected) (ert-select-tests
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1069 `(satisfies
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1070 ,(lambda (test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1071 (ert-test-result-expected-p
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1072 test
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1073 (ert-test-most-recent-result test))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1074 universe))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1075 ((member :unexpected) (ert-select-tests `(not :expected) universe))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1076 (string
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1077 (etypecase universe
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1078 ((member t) (mapcar #'ert-get-test
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1079 (apropos-internal selector #'ert-test-boundp)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1080 (list (ert--remove-if-not (lambda (test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1081 (and (ert-test-name test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1082 (string-match selector
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1083 (ert-test-name test))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1084 universe))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1085 (ert-test (list selector))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1086 (symbol
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1087 (assert (ert-test-boundp selector))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1088 (list (ert-get-test selector)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1089 (cons
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1090 (destructuring-bind (operator &rest operands) selector
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1091 (ecase operator
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1092 (member
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1093 (mapcar (lambda (purported-test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1094 (etypecase purported-test
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1095 (symbol (assert (ert-test-boundp purported-test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1096 (ert-get-test purported-test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1097 (ert-test purported-test)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1098 operands))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1099 (eql
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1100 (assert (eql (length operands) 1))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1101 (ert-select-tests `(member ,@operands) universe))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1102 (and
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1103 ;; Do these definitions of AND, NOT and OR satisfy de
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1104 ;; Morgan's laws? Should they?
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1105 (case (length operands)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1106 (0 (ert-select-tests 't universe))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1107 (t (ert-select-tests `(and ,@(rest operands))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1108 (ert-select-tests (first operands)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1109 universe)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1110 (not
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1111 (assert (eql (length operands) 1))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1112 (let ((all-tests (ert-select-tests 't universe)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1113 (ert--set-difference all-tests
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1114 (ert-select-tests (first operands)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1115 all-tests))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1116 (or
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1117 (case (length operands)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1118 (0 (ert-select-tests 'nil universe))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1119 (t (ert--union (ert-select-tests (first operands) universe)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1120 (ert-select-tests `(or ,@(rest operands))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1121 universe)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1122 (tag
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1123 (assert (eql (length operands) 1))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1124 (let ((tag (first operands)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1125 (ert-select-tests `(satisfies
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1126 ,(lambda (test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1127 (member tag (ert-test-tags test))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1128 universe)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1129 (satisfies
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1130 (assert (eql (length operands) 1))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1131 (ert--remove-if-not (first operands)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1132 (ert-select-tests 't universe))))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1133
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1134 (defun ert--insert-human-readable-selector (selector)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1135 "Insert a human-readable presentation of SELECTOR into the current buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1136 ;; This is needed to avoid printing the (huge) contents of the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1137 ;; `backtrace' slot of the result objects in the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1138 ;; `most-recent-result' slots of test case objects in (eql ...) or
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1139 ;; (member ...) selectors.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1140 (labels ((rec (selector)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1141 ;; This code needs to match the etypecase in `ert-select-tests'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1142 (etypecase selector
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1143 ((or (member nil t
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1144 :new :failed :passed
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1145 :expected :unexpected)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1146 string
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1147 symbol)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1148 selector)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1149 (ert-test
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1150 (if (ert-test-name selector)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1151 (make-symbol (format "<%S>" (ert-test-name selector)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1152 (make-symbol "<unnamed test>")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1153 (cons
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1154 (destructuring-bind (operator &rest operands) selector
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1155 (ecase operator
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1156 ((member eql and not or)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1157 `(,operator ,@(mapcar #'rec operands)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1158 ((member tag satisfies)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1159 selector)))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1160 (insert (format "%S" (rec selector)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1161
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1162
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1163 ;;; Facilities for running a whole set of tests.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1164
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1165 ;; The data structure that contains the set of tests being executed
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1166 ;; during one particular test run, their results, the state of the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1167 ;; execution, and some statistics.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1168 ;;
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1169 ;; The data about results and expected results of tests may seem
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1170 ;; redundant here, since the test objects also carry such information.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1171 ;; However, the information in the test objects may be more recent, it
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1172 ;; may correspond to a different test run. We need the information
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1173 ;; that corresponds to this run in order to be able to update the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1174 ;; statistics correctly when a test is re-run interactively and has a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1175 ;; different result than before.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1176 (defstruct ert--stats
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1177 (selector (assert nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1178 ;; The tests, in order.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1179 (tests (assert nil) :type vector)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1180 ;; A map of test names (or the test objects themselves for unnamed
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1181 ;; tests) to indices into the `tests' vector.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1182 (test-map (assert nil) :type hash-table)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1183 ;; The results of the tests during this run, in order.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1184 (test-results (assert nil) :type vector)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1185 ;; The start times of the tests, in order, as reported by
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1186 ;; `current-time'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1187 (test-start-times (assert nil) :type vector)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1188 ;; The end times of the tests, in order, as reported by
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1189 ;; `current-time'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1190 (test-end-times (assert nil) :type vector)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1191 (passed-expected 0)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1192 (passed-unexpected 0)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1193 (failed-expected 0)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1194 (failed-unexpected 0)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1195 (start-time nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1196 (end-time nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1197 (aborted-p nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1198 (current-test nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1199 ;; The time at or after which the next redisplay should occur, as a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1200 ;; float.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1201 (next-redisplay 0.0))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1202
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1203 (defun ert-stats-completed-expected (stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1204 "Return the number of tests in STATS that had expected results."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1205 (+ (ert--stats-passed-expected stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1206 (ert--stats-failed-expected stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1207
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1208 (defun ert-stats-completed-unexpected (stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1209 "Return the number of tests in STATS that had unexpected results."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1210 (+ (ert--stats-passed-unexpected stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1211 (ert--stats-failed-unexpected stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1212
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1213 (defun ert-stats-completed (stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1214 "Number of tests in STATS that have run so far."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1215 (+ (ert-stats-completed-expected stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1216 (ert-stats-completed-unexpected stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1217
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1218 (defun ert-stats-total (stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1219 "Number of tests in STATS, regardless of whether they have run yet."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1220 (length (ert--stats-tests stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1221
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1222 ;; The stats object of the current run, dynamically bound. This is
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1223 ;; used for the mode line progress indicator.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1224 (defvar ert--current-run-stats nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1225
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1226 (defun ert--stats-test-key (test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1227 "Return the key used for TEST in the test map of ert--stats objects.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1228
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1229 Returns the name of TEST if it has one, or TEST itself otherwise."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1230 (or (ert-test-name test) test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1231
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1232 (defun ert--stats-set-test-and-result (stats pos test result)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1233 "Change STATS by replacing the test at position POS with TEST and RESULT.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1234
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1235 Also changes the counters in STATS to match."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1236 (let* ((tests (ert--stats-tests stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1237 (results (ert--stats-test-results stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1238 (old-test (aref tests pos))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1239 (map (ert--stats-test-map stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1240 (flet ((update (d)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1241 (if (ert-test-result-expected-p (aref tests pos)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1242 (aref results pos))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1243 (etypecase (aref results pos)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1244 (ert-test-passed (incf (ert--stats-passed-expected stats) d))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1245 (ert-test-failed (incf (ert--stats-failed-expected stats) d))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1246 (null)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1247 (ert-test-aborted-with-non-local-exit))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1248 (etypecase (aref results pos)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1249 (ert-test-passed (incf (ert--stats-passed-unexpected stats) d))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1250 (ert-test-failed (incf (ert--stats-failed-unexpected stats) d))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1251 (null)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1252 (ert-test-aborted-with-non-local-exit)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1253 ;; Adjust counters to remove the result that is currently in stats.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1254 (update -1)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1255 ;; Put new test and result into stats.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1256 (setf (aref tests pos) test
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1257 (aref results pos) result)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1258 (remhash (ert--stats-test-key old-test) map)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1259 (setf (gethash (ert--stats-test-key test) map) pos)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1260 ;; Adjust counters to match new result.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1261 (update +1)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1262 nil)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1263
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1264 (defun ert--make-stats (tests selector)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1265 "Create a new `ert--stats' object for running TESTS.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1266
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1267 SELECTOR is the selector that was used to select TESTS."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1268 (setq tests (ert--coerce-to-vector tests))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1269 (let ((map (make-hash-table :size (length tests))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1270 (loop for i from 0
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1271 for test across tests
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1272 for key = (ert--stats-test-key test) do
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1273 (assert (not (gethash key map)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1274 (setf (gethash key map) i))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1275 (make-ert--stats :selector selector
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1276 :tests tests
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1277 :test-map map
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1278 :test-results (make-vector (length tests) nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1279 :test-start-times (make-vector (length tests) nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1280 :test-end-times (make-vector (length tests) nil))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1281
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1282 (defun ert-run-or-rerun-test (stats test listener)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1283 ;; checkdoc-order: nil
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1284 "Run the single test TEST and record the result using STATS and LISTENER."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1285 (let ((ert--current-run-stats stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1286 (pos (ert--stats-test-pos stats test)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1287 (ert--stats-set-test-and-result stats pos test nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1288 ;; Call listener after setting/before resetting
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1289 ;; (ert--stats-current-test stats); the listener might refresh the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1290 ;; mode line display, and if the value is not set yet/any more
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1291 ;; during this refresh, the mode line will flicker unnecessarily.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1292 (setf (ert--stats-current-test stats) test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1293 (funcall listener 'test-started stats test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1294 (setf (ert-test-most-recent-result test) nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1295 (setf (aref (ert--stats-test-start-times stats) pos) (current-time))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1296 (unwind-protect
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1297 (ert-run-test test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1298 (setf (aref (ert--stats-test-end-times stats) pos) (current-time))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1299 (let ((result (ert-test-most-recent-result test)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1300 (ert--stats-set-test-and-result stats pos test result)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1301 (funcall listener 'test-ended stats test result))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1302 (setf (ert--stats-current-test stats) nil))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1303
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1304 (defun ert-run-tests (selector listener)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1305 "Run the tests specified by SELECTOR, sending progress updates to LISTENER."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1306 (let* ((tests (ert-select-tests selector t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1307 (stats (ert--make-stats tests selector)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1308 (setf (ert--stats-start-time stats) (current-time))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1309 (funcall listener 'run-started stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1310 (let ((abortedp t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1311 (unwind-protect
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1312 (let ((ert--current-run-stats stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1313 (force-mode-line-update)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1314 (unwind-protect
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1315 (progn
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1316 (loop for test in tests do
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1317 (ert-run-or-rerun-test stats test listener))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1318 (setq abortedp nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1319 (setf (ert--stats-aborted-p stats) abortedp)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1320 (setf (ert--stats-end-time stats) (current-time))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1321 (funcall listener 'run-ended stats abortedp)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1322 (force-mode-line-update))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1323 stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1324
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1325 (defun ert--stats-test-pos (stats test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1326 ;; checkdoc-order: nil
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1327 "Return the position (index) of TEST in the run represented by STATS."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1328 (gethash (ert--stats-test-key test) (ert--stats-test-map stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1329
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1330
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1331 ;;; Formatting functions shared across UIs.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1332
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1333 (defun ert--format-time-iso8601 (time)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1334 "Format TIME in the variant of ISO 8601 used for timestamps in ERT."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1335 (format-time-string "%Y-%m-%d %T%z" time))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1336
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1337 (defun ert-char-for-test-result (result expectedp)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1338 "Return a character that represents the test result RESULT.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1339
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1340 EXPECTEDP specifies whether the result was expected."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1341 (let ((s (etypecase result
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1342 (ert-test-passed ".P")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1343 (ert-test-failed "fF")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1344 (null "--")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1345 (ert-test-aborted-with-non-local-exit "aA"))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1346 (elt s (if expectedp 0 1))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1347
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1348 (defun ert-string-for-test-result (result expectedp)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1349 "Return a string that represents the test result RESULT.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1350
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1351 EXPECTEDP specifies whether the result was expected."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1352 (let ((s (etypecase result
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1353 (ert-test-passed '("passed" "PASSED"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1354 (ert-test-failed '("failed" "FAILED"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1355 (null '("unknown" "UNKNOWN"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1356 (ert-test-aborted-with-non-local-exit '("aborted" "ABORTED")))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1357 (elt s (if expectedp 0 1))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1358
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1359 (defun ert--pp-with-indentation-and-newline (object)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1360 "Pretty-print OBJECT, indenting it to the current column of point.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1361 Ensures a final newline is inserted."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1362 (let ((begin (point)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1363 (pp object (current-buffer))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1364 (unless (bolp) (insert "\n"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1365 (save-excursion
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1366 (goto-char begin)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1367 (indent-sexp))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1368
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1369 (defun ert--insert-infos (result)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1370 "Insert `ert-info' infos from RESULT into current buffer.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1371
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1372 RESULT must be an `ert-test-result-with-condition'."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1373 (check-type result ert-test-result-with-condition)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1374 (dolist (info (ert-test-result-with-condition-infos result))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1375 (destructuring-bind (prefix . message) info
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1376 (let ((begin (point))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1377 (indentation (make-string (+ (length prefix) 4) ?\s))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1378 (end nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1379 (unwind-protect
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1380 (progn
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1381 (insert message "\n")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1382 (setq end (copy-marker (point)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1383 (goto-char begin)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1384 (insert " " prefix)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1385 (forward-line 1)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1386 (while (< (point) end)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1387 (insert indentation)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1388 (forward-line 1)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1389 (when end (set-marker end nil)))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1390
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1391
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1392 ;;; Running tests in batch mode.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1393
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1394 (defvar ert-batch-backtrace-right-margin 70
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1395 "*The maximum line length for printing backtraces in `ert-run-tests-batch'.")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1396
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1397 ;;;###autoload
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1398 (defun ert-run-tests-batch (&optional selector)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1399 "Run the tests specified by SELECTOR, printing results to the terminal.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1400
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1401 SELECTOR works as described in `ert-select-tests', except if
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1402 SELECTOR is nil, in which case all tests rather than none will be
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1403 run; this makes the command line \"emacs -batch -l my-tests.el -f
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1404 ert-run-tests-batch-and-exit\" useful.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1405
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1406 Returns the stats object."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1407 (unless selector (setq selector 't))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1408 (ert-run-tests
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1409 selector
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1410 (lambda (event-type &rest event-args)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1411 (ecase event-type
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1412 (run-started
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1413 (destructuring-bind (stats) event-args
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1414 (message "Running %s tests (%s)"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1415 (length (ert--stats-tests stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1416 (ert--format-time-iso8601 (ert--stats-start-time stats)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1417 (run-ended
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1418 (destructuring-bind (stats abortedp) event-args
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1419 (let ((unexpected (ert-stats-completed-unexpected stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1420 (expected-failures (ert--stats-failed-expected stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1421 (message "\n%sRan %s tests, %s results as expected%s (%s)%s\n"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1422 (if (not abortedp)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1423 ""
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1424 "Aborted: ")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1425 (ert-stats-total stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1426 (ert-stats-completed-expected stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1427 (if (zerop unexpected)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1428 ""
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1429 (format ", %s unexpected" unexpected))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1430 (ert--format-time-iso8601 (ert--stats-end-time stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1431 (if (zerop expected-failures)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1432 ""
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1433 (format "\n%s expected failures" expected-failures)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1434 (unless (zerop unexpected)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1435 (message "%s unexpected results:" unexpected)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1436 (loop for test across (ert--stats-tests stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1437 for result = (ert-test-most-recent-result test) do
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1438 (when (not (ert-test-result-expected-p test result))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1439 (message "%9s %S"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1440 (ert-string-for-test-result result nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1441 (ert-test-name test))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1442 (message "%s" "")))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1443 (test-started
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1444 )
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1445 (test-ended
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1446 (destructuring-bind (stats test result) event-args
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1447 (unless (ert-test-result-expected-p test result)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1448 (etypecase result
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1449 (ert-test-passed
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1450 (message "Test %S passed unexpectedly" (ert-test-name test)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1451 (ert-test-result-with-condition
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1452 (message "Test %S backtrace:" (ert-test-name test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1453 (with-temp-buffer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1454 (ert--print-backtrace (ert-test-result-with-condition-backtrace
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1455 result))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1456 (goto-char (point-min))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1457 (while (not (eobp))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1458 (let ((start (point))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1459 (end (progn (end-of-line) (point))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1460 (setq end (min end
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1461 (+ start ert-batch-backtrace-right-margin)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1462 (message "%s" (buffer-substring-no-properties
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1463 start end)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1464 (forward-line 1)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1465 (with-temp-buffer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1466 (ert--insert-infos result)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1467 (insert " ")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1468 (let ((print-escape-newlines t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1469 (print-level 5)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1470 (print-length 10))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1471 (let ((begin (point)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1472 (ert--pp-with-indentation-and-newline
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1473 (ert-test-result-with-condition-condition result))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1474 (goto-char (1- (point-max)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1475 (assert (looking-at "\n"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1476 (delete-char 1)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1477 (message "Test %S condition:" (ert-test-name test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1478 (message "%s" (buffer-string))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1479 (ert-test-aborted-with-non-local-exit
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1480 (message "Test %S aborted with non-local exit"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1481 (ert-test-name test)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1482 (let* ((max (prin1-to-string (length (ert--stats-tests stats))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1483 (format-string (concat "%9s %"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1484 (prin1-to-string (length max))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1485 "s/" max " %S")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1486 (message format-string
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1487 (ert-string-for-test-result result
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1488 (ert-test-result-expected-p
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1489 test result))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1490 (1+ (ert--stats-test-pos stats test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1491 (ert-test-name test)))))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1492
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1493 ;;;###autoload
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1494 (defun ert-run-tests-batch-and-exit (&optional selector)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1495 "Like `ert-run-tests-batch', but exits Emacs when done.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1496
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1497 The exit status will be 0 if all test results were as expected, 1
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1498 on unexpected results, or 2 if the tool detected an error outside
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1499 of the tests (e.g. invalid SELECTOR or bug in the code that runs
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1500 the tests)."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1501 (unwind-protect
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1502 (let ((stats (ert-run-tests-batch selector)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1503 (kill-emacs (if (zerop (ert-stats-completed-unexpected stats)) 0 1)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1504 (unwind-protect
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1505 (progn
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1506 (message "Error running tests")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1507 (backtrace))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1508 (kill-emacs 2))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1509
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1510
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1511 ;;; Utility functions for load/unload actions.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1512
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1513 (defun ert--activate-font-lock-keywords ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1514 "Activate font-lock keywords for some of ERT's symbols."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1515 (font-lock-add-keywords
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1516 nil
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1517 '(("(\\(\\<ert-deftest\\)\\>\\s *\\(\\sw+\\)?"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1518 (1 font-lock-keyword-face nil t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1519 (2 font-lock-function-name-face nil t)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1520
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1521 (defun* ert--remove-from-list (list-var element &key key test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1522 "Remove ELEMENT from the value of LIST-VAR if present.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1523
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1524 This can be used as an inverse of `add-to-list'."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1525 (unless key (setq key #'identity))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1526 (unless test (setq test #'equal))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1527 (setf (symbol-value list-var)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1528 (ert--remove* element
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1529 (symbol-value list-var)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1530 :key key
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1531 :test test)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1532
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1533
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1534 ;;; Some basic interactive functions.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1535
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1536 (defun ert-read-test-name (prompt &optional default history
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1537 add-default-to-prompt)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1538 "Read the name of a test and return it as a symbol.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1539
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1540 Prompt with PROMPT. If DEFAULT is a valid test name, use it as a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1541 default. HISTORY is the history to use; see `completing-read'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1542 If ADD-DEFAULT-TO-PROMPT is non-nil, PROMPT will be modified to
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1543 include the default, if any.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1544
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1545 Signals an error if no test name was read."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1546 (etypecase default
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1547 (string (let ((symbol (intern-soft default)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1548 (unless (and symbol (ert-test-boundp symbol))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1549 (setq default nil))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1550 (symbol (setq default
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1551 (if (ert-test-boundp default)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1552 (symbol-name default)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1553 nil)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1554 (ert-test (setq default (ert-test-name default))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1555 (when add-default-to-prompt
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1556 (setq prompt (if (null default)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1557 (format "%s: " prompt)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1558 (format "%s (default %s): " prompt default))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1559 (let ((input (completing-read prompt obarray #'ert-test-boundp
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1560 t nil history default nil)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1561 ;; completing-read returns an empty string if default was nil and
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1562 ;; the user just hit enter.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1563 (let ((sym (intern-soft input)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1564 (if (ert-test-boundp sym)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1565 sym
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1566 (error "Input does not name a test")))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1567
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1568 (defun ert-read-test-name-at-point (prompt)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1569 "Read the name of a test and return it as a symbol.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1570 As a default, use the symbol at point, or the test at point if in
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1571 the ERT results buffer. Prompt with PROMPT, augmented with the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1572 default (if any)."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1573 (ert-read-test-name prompt (ert-test-at-point) nil t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1574
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1575 (defun ert-find-test-other-window (test-name)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1576 "Find, in another window, the definition of TEST-NAME."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1577 (interactive (list (ert-read-test-name-at-point "Find test definition: ")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1578 (find-function-do-it test-name 'ert-deftest 'switch-to-buffer-other-window))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1579
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1580 (defun ert-delete-test (test-name)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1581 "Make the test TEST-NAME unbound.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1582
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1583 Nothing more than an interactive interface to `ert-make-test-unbound'."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1584 (interactive (list (ert-read-test-name-at-point "Delete test")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1585 (ert-make-test-unbound test-name))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1586
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1587 (defun ert-delete-all-tests ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1588 "Make all symbols in `obarray' name no test."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1589 (interactive)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1590 (when (interactive-p)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1591 (unless (y-or-n-p "Delete all tests? ")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1592 (error "Aborted")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1593 ;; We can't use `ert-select-tests' here since that gives us only
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1594 ;; test objects, and going from them back to the test name symbols
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1595 ;; can fail if the `ert-test' defstruct has been redefined.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1596 (mapc #'ert-make-test-unbound (apropos-internal "" #'ert-test-boundp))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1597 t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1598
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1599
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1600 ;;; Display of test progress and results.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1601
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1602 ;; An entry in the results buffer ewoc. There is one entry per test.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1603 (defstruct ert--ewoc-entry
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1604 (test (assert nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1605 ;; If the result of this test was expected, its ewoc entry is hidden
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1606 ;; initially.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1607 (hidden-p (assert nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1608 ;; An ewoc entry may be collapsed to hide details such as the error
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1609 ;; condition.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1610 ;;
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1611 ;; I'm not sure the ability to expand and collapse entries is still
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1612 ;; a useful feature.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1613 (expanded-p t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1614 ;; By default, the ewoc entry presents the error condition with
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1615 ;; certain limits on how much to print (`print-level',
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1616 ;; `print-length'). The user can interactively switch to a set of
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1617 ;; higher limits.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1618 (extended-printer-limits-p nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1619
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1620 ;; Variables local to the results buffer.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1621
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1622 ;; The ewoc.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1623 (defvar ert--results-ewoc)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1624 ;; The stats object.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1625 (defvar ert--results-stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1626 ;; A string with one character per test. Each character represents
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1627 ;; the result of the corresponding test. The string is displayed near
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1628 ;; the top of the buffer and serves as a progress bar.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1629 (defvar ert--results-progress-bar-string)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1630 ;; The position where the progress bar button begins.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1631 (defvar ert--results-progress-bar-button-begin)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1632 ;; The test result listener that updates the buffer when tests are run.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1633 (defvar ert--results-listener)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1634
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1635 (defun ert-insert-test-name-button (test-name)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1636 "Insert a button that links to TEST-NAME."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1637 (insert-text-button (format "%S" test-name)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1638 :type 'ert--test-name-button
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1639 'ert-test-name test-name))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1640
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1641 (defun ert--results-format-expected-unexpected (expected unexpected)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1642 "Return a string indicating EXPECTED expected results, UNEXPECTED unexpected."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1643 (if (zerop unexpected)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1644 (format "%s" expected)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1645 (format "%s (%s unexpected)" (+ expected unexpected) unexpected)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1646
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1647 (defun ert--results-update-ewoc-hf (ewoc stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1648 "Update the header and footer of EWOC to show certain information from STATS.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1649
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1650 Also sets `ert--results-progress-bar-button-begin'."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1651 (let ((run-count (ert-stats-completed stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1652 (results-buffer (current-buffer))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1653 ;; Need to save buffer-local value.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1654 (font-lock font-lock-mode))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1655 (ewoc-set-hf
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1656 ewoc
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1657 ;; header
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1658 (with-temp-buffer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1659 (insert "Selector: ")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1660 (ert--insert-human-readable-selector (ert--stats-selector stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1661 (insert "\n")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1662 (insert
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1663 (format (concat "Passed: %s\n"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1664 "Failed: %s\n"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1665 "Total: %s/%s\n\n")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1666 (ert--results-format-expected-unexpected
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1667 (ert--stats-passed-expected stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1668 (ert--stats-passed-unexpected stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1669 (ert--results-format-expected-unexpected
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1670 (ert--stats-failed-expected stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1671 (ert--stats-failed-unexpected stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1672 run-count
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1673 (ert-stats-total stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1674 (insert
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1675 (format "Started at: %s\n"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1676 (ert--format-time-iso8601 (ert--stats-start-time stats))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1677 ;; FIXME: This is ugly. Need to properly define invariants of
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1678 ;; the `stats' data structure.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1679 (let ((state (cond ((ert--stats-aborted-p stats) 'aborted)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1680 ((ert--stats-current-test stats) 'running)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1681 ((ert--stats-end-time stats) 'finished)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1682 (t 'preparing))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1683 (ecase state
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1684 (preparing
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1685 (insert ""))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1686 (aborted
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1687 (cond ((ert--stats-current-test stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1688 (insert "Aborted during test: ")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1689 (ert-insert-test-name-button
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1690 (ert-test-name (ert--stats-current-test stats))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1691 (t
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1692 (insert "Aborted."))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1693 (running
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1694 (assert (ert--stats-current-test stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1695 (insert "Running test: ")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1696 (ert-insert-test-name-button (ert-test-name
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1697 (ert--stats-current-test stats))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1698 (finished
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1699 (assert (not (ert--stats-current-test stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1700 (insert "Finished.")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1701 (insert "\n")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1702 (if (ert--stats-end-time stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1703 (insert
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1704 (format "%s%s\n"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1705 (if (ert--stats-aborted-p stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1706 "Aborted at: "
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1707 "Finished at: ")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1708 (ert--format-time-iso8601 (ert--stats-end-time stats))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1709 (insert "\n"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1710 (insert "\n"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1711 (let ((progress-bar-string (with-current-buffer results-buffer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1712 ert--results-progress-bar-string)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1713 (let ((progress-bar-button-begin
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1714 (insert-text-button progress-bar-string
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1715 :type 'ert--results-progress-bar-button
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1716 'face (or (and font-lock
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1717 (ert-face-for-stats stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1718 'button))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1719 ;; The header gets copied verbatim to the results buffer,
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1720 ;; and all positions remain the same, so
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1721 ;; `progress-bar-button-begin' will be the right position
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1722 ;; even in the results buffer.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1723 (with-current-buffer results-buffer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1724 (set (make-local-variable 'ert--results-progress-bar-button-begin)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1725 progress-bar-button-begin))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1726 (insert "\n\n")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1727 (buffer-string))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1728 ;; footer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1729 ;;
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1730 ;; We actually want an empty footer, but that would trigger a bug
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1731 ;; in ewoc, sometimes clearing the entire buffer. (It's possible
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1732 ;; that this bug has been fixed since this has been tested; we
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1733 ;; should test it again.)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1734 "\n")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1735
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1736
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1737 (defvar ert-test-run-redisplay-interval-secs .1
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1738 "How many seconds ERT should wait between redisplays while running tests.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1739
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1740 While running tests, ERT shows the current progress, and this variable
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1741 determines how frequently the progress display is updated.")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1742
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1743 (defun ert--results-update-stats-display (ewoc stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1744 "Update EWOC and the mode line to show data from STATS."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1745 ;; TODO(ohler): investigate using `make-progress-reporter'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1746 (ert--results-update-ewoc-hf ewoc stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1747 (force-mode-line-update)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1748 (redisplay t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1749 (setf (ert--stats-next-redisplay stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1750 (+ (float-time) ert-test-run-redisplay-interval-secs)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1751
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1752 (defun ert--results-update-stats-display-maybe (ewoc stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1753 "Call `ert--results-update-stats-display' if not called recently.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1754
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1755 EWOC and STATS are arguments for `ert--results-update-stats-display'."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1756 (when (>= (float-time) (ert--stats-next-redisplay stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1757 (ert--results-update-stats-display ewoc stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1758
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1759 (defun ert--tests-running-mode-line-indicator ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1760 "Return a string for the mode line that shows the test run progress."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1761 (let* ((stats ert--current-run-stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1762 (tests-total (ert-stats-total stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1763 (tests-completed (ert-stats-completed stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1764 (if (>= tests-completed tests-total)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1765 (format " ERT(%s/%s,finished)" tests-completed tests-total)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1766 (format " ERT(%s/%s):%s"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1767 (1+ tests-completed)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1768 tests-total
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1769 (if (null (ert--stats-current-test stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1770 "?"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1771 (format "%S"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1772 (ert-test-name (ert--stats-current-test stats))))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1773
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1774 (defun ert--make-xrefs-region (begin end)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1775 "Attach cross-references to function names between BEGIN and END.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1776
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1777 BEGIN and END specify a region in the current buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1778 (save-excursion
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1779 (save-restriction
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1780 (narrow-to-region begin (point))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1781 ;; Inhibit optimization in `debugger-make-xrefs' that would
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1782 ;; sometimes insert unrelated backtrace info into our buffer.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1783 (let ((debugger-previous-backtrace nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1784 (debugger-make-xrefs)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1785
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1786 (defun ert--string-first-line (s)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1787 "Return the first line of S, or S if it contains no newlines.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1788
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1789 The return value does not include the line terminator."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1790 (substring s 0 (ert--string-position ?\n s)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1791
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1792 (defun ert-face-for-test-result (expectedp)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1793 "Return a face that shows whether a test result was expected or unexpected.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1794
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1795 If EXPECTEDP is nil, returns the face for unexpected results; if
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1796 non-nil, returns the face for expected results.."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1797 (if expectedp 'ert-test-result-expected 'ert-test-result-unexpected))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1798
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1799 (defun ert-face-for-stats (stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1800 "Return a face that represents STATS."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1801 (cond ((ert--stats-aborted-p stats) 'nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1802 ((plusp (ert-stats-completed-unexpected stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1803 (ert-face-for-test-result nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1804 ((eql (ert-stats-completed-expected stats) (ert-stats-total stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1805 (ert-face-for-test-result t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1806 (t 'nil)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1807
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1808 (defun ert--print-test-for-ewoc (entry)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1809 "The ewoc print function for ewoc test entries. ENTRY is the entry to print."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1810 (let* ((test (ert--ewoc-entry-test entry))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1811 (stats ert--results-stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1812 (result (let ((pos (ert--stats-test-pos stats test)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1813 (assert pos)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1814 (aref (ert--stats-test-results stats) pos)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1815 (hiddenp (ert--ewoc-entry-hidden-p entry))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1816 (expandedp (ert--ewoc-entry-expanded-p entry))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1817 (extended-printer-limits-p (ert--ewoc-entry-extended-printer-limits-p
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1818 entry)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1819 (cond (hiddenp)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1820 (t
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1821 (let ((expectedp (ert-test-result-expected-p test result)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1822 (insert-text-button (format "%c" (ert-char-for-test-result
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1823 result expectedp))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1824 :type 'ert--results-expand-collapse-button
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1825 'face (or (and font-lock-mode
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1826 (ert-face-for-test-result
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1827 expectedp))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1828 'button)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1829 (insert " ")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1830 (ert-insert-test-name-button (ert-test-name test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1831 (insert "\n")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1832 (when (and expandedp (not (eql result 'nil)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1833 (when (ert-test-documentation test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1834 (insert " "
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1835 (propertize
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1836 (ert--string-first-line (ert-test-documentation test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1837 'font-lock-face 'font-lock-doc-face)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1838 "\n"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1839 (etypecase result
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1840 (ert-test-passed
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1841 (if (ert-test-result-expected-p test result)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1842 (insert " passed\n")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1843 (insert " passed unexpectedly\n"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1844 (insert ""))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1845 (ert-test-result-with-condition
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1846 (ert--insert-infos result)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1847 (let ((print-escape-newlines t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1848 (print-level (if extended-printer-limits-p 12 6))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1849 (print-length (if extended-printer-limits-p 100 10)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1850 (insert " ")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1851 (let ((begin (point)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1852 (ert--pp-with-indentation-and-newline
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1853 (ert-test-result-with-condition-condition result))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1854 (ert--make-xrefs-region begin (point)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1855 (ert-test-aborted-with-non-local-exit
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1856 (insert " aborted\n")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1857 (insert "\n")))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1858 nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1859
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1860 (defun ert--results-font-lock-function (enabledp)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1861 "Redraw the ERT results buffer after font-lock-mode was switched on or off.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1862
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1863 ENABLEDP is true if font-lock-mode is switched on, false
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1864 otherwise."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1865 (ert--results-update-ewoc-hf ert--results-ewoc ert--results-stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1866 (ewoc-refresh ert--results-ewoc)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1867 (font-lock-default-function enabledp))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1868
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1869 (defun ert--setup-results-buffer (stats listener buffer-name)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1870 "Set up a test results buffer.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1871
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1872 STATS is the stats object; LISTENER is the results listener;
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1873 BUFFER-NAME, if non-nil, is the buffer name to use."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1874 (unless buffer-name (setq buffer-name "*ert*"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1875 (let ((buffer (get-buffer-create buffer-name)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1876 (with-current-buffer buffer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1877 (setq buffer-read-only t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1878 (let ((inhibit-read-only t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1879 (buffer-disable-undo)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1880 (erase-buffer)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1881 (ert-results-mode)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1882 ;; Erase buffer again in case switching out of the previous
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1883 ;; mode inserted anything. (This happens e.g. when switching
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1884 ;; from ert-results-mode to ert-results-mode when
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1885 ;; font-lock-mode turns itself off in change-major-mode-hook.)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1886 (erase-buffer)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1887 (set (make-local-variable 'font-lock-function)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1888 'ert--results-font-lock-function)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1889 (let ((ewoc (ewoc-create 'ert--print-test-for-ewoc nil nil t)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1890 (set (make-local-variable 'ert--results-ewoc) ewoc)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1891 (set (make-local-variable 'ert--results-stats) stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1892 (set (make-local-variable 'ert--results-progress-bar-string)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1893 (make-string (ert-stats-total stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1894 (ert-char-for-test-result nil t)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1895 (set (make-local-variable 'ert--results-listener) listener)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1896 (loop for test across (ert--stats-tests stats) do
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1897 (ewoc-enter-last ewoc
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1898 (make-ert--ewoc-entry :test test :hidden-p t)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1899 (ert--results-update-ewoc-hf ert--results-ewoc ert--results-stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1900 (goto-char (1- (point-max)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1901 buffer)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1902
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1903
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1904 (defvar ert--selector-history nil
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1905 "List of recent test selectors read from terminal.")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1906
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1907 ;; Should OUTPUT-BUFFER-NAME and MESSAGE-FN really be arguments here?
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1908 ;; They are needed only for our automated self-tests at the moment.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1909 ;; Or should there be some other mechanism?
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1910 ;;;###autoload
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1911 (defun ert-run-tests-interactively (selector
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1912 &optional output-buffer-name message-fn)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1913 "Run the tests specified by SELECTOR and display the results in a buffer.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1914
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1915 SELECTOR works as described in `ert-select-tests'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1916 OUTPUT-BUFFER-NAME and MESSAGE-FN should normally be nil; they
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1917 are used for automated self-tests and specify which buffer to use
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1918 and how to display message."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1919 (interactive
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1920 (list (let ((default (if ert--selector-history
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1921 ;; Can't use `first' here as this form is
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1922 ;; not compiled, and `first' is not
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1923 ;; defined without cl.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1924 (car ert--selector-history)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1925 "t")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1926 (read-from-minibuffer (if (null default)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1927 "Run tests: "
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1928 (format "Run tests (default %s): " default))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1929 nil nil t 'ert--selector-history
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1930 default nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1931 nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1932 (unless message-fn (setq message-fn 'message))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1933 (lexical-let ((output-buffer-name output-buffer-name)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1934 buffer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1935 listener
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1936 (message-fn message-fn))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1937 (setq listener
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1938 (lambda (event-type &rest event-args)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1939 (ecase event-type
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1940 (run-started
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1941 (destructuring-bind (stats) event-args
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1942 (setq buffer (ert--setup-results-buffer stats
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1943 listener
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1944 output-buffer-name))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1945 (pop-to-buffer buffer)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1946 (run-ended
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1947 (destructuring-bind (stats abortedp) event-args
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1948 (funcall message-fn
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1949 "%sRan %s tests, %s results were as expected%s"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1950 (if (not abortedp)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1951 ""
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1952 "Aborted: ")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1953 (ert-stats-total stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1954 (ert-stats-completed-expected stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1955 (let ((unexpected
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1956 (ert-stats-completed-unexpected stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1957 (if (zerop unexpected)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1958 ""
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1959 (format ", %s unexpected" unexpected))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1960 (ert--results-update-stats-display (with-current-buffer buffer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1961 ert--results-ewoc)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1962 stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1963 (test-started
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1964 (destructuring-bind (stats test) event-args
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1965 (with-current-buffer buffer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1966 (let* ((ewoc ert--results-ewoc)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1967 (pos (ert--stats-test-pos stats test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1968 (node (ewoc-nth ewoc pos)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1969 (assert node)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1970 (setf (ert--ewoc-entry-test (ewoc-data node)) test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1971 (aset ert--results-progress-bar-string pos
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1972 (ert-char-for-test-result nil t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1973 (ert--results-update-stats-display-maybe ewoc stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1974 (ewoc-invalidate ewoc node)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1975 (test-ended
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1976 (destructuring-bind (stats test result) event-args
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1977 (with-current-buffer buffer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1978 (let* ((ewoc ert--results-ewoc)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1979 (pos (ert--stats-test-pos stats test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1980 (node (ewoc-nth ewoc pos)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1981 (when (ert--ewoc-entry-hidden-p (ewoc-data node))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1982 (setf (ert--ewoc-entry-hidden-p (ewoc-data node))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1983 (ert-test-result-expected-p test result)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1984 (aset ert--results-progress-bar-string pos
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1985 (ert-char-for-test-result result
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1986 (ert-test-result-expected-p
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1987 test result)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1988 (ert--results-update-stats-display-maybe ewoc stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1989 (ewoc-invalidate ewoc node))))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1990 (ert-run-tests
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1991 selector
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1992 listener)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1993 ;;;###autoload
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1994 (defalias 'ert 'ert-run-tests-interactively)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1995
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1996
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1997 ;;; Simple view mode for auxiliary information like stack traces or
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1998 ;;; messages. Mainly binds "q" for quit.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
1999
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2000 (define-derived-mode ert-simple-view-mode fundamental-mode "ERT-View"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2001 "Major mode for viewing auxiliary information in ERT.")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2002
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2003 (loop for (key binding) in
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2004 '(("q" quit-window)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2005 )
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2006 do
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2007 (define-key ert-simple-view-mode-map key binding))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2008
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2009
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2010 ;;; Commands and button actions for the results buffer.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2011
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2012 (define-derived-mode ert-results-mode fundamental-mode "ERT-Results"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2013 "Major mode for viewing results of ERT test runs.")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2014
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2015 (loop for (key binding) in
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2016 '(;; Stuff that's not in the menu.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2017 ("\t" forward-button)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2018 ([backtab] backward-button)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2019 ("j" ert-results-jump-between-summary-and-result)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2020 ("q" quit-window)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2021 ("L" ert-results-toggle-printer-limits-for-test-at-point)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2022 ("n" ert-results-next-test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2023 ("p" ert-results-previous-test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2024 ;; Stuff that is in the menu.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2025 ("R" ert-results-rerun-all-tests)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2026 ("r" ert-results-rerun-test-at-point)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2027 ("d" ert-results-rerun-test-at-point-debugging-errors)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2028 ("." ert-results-find-test-at-point-other-window)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2029 ("b" ert-results-pop-to-backtrace-for-test-at-point)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2030 ("m" ert-results-pop-to-messages-for-test-at-point)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2031 ("l" ert-results-pop-to-should-forms-for-test-at-point)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2032 ("h" ert-results-describe-test-at-point)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2033 ("D" ert-delete-test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2034 ("T" ert-results-pop-to-timings)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2035 )
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2036 do
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2037 (define-key ert-results-mode-map key binding))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2038
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2039 (easy-menu-define ert-results-mode-menu ert-results-mode-map
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2040 "Menu for `ert-results-mode'."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2041 '("ERT Results"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2042 ["Re-run all tests" ert-results-rerun-all-tests]
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2043 "--"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2044 ["Re-run test" ert-results-rerun-test-at-point]
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2045 ["Debug test" ert-results-rerun-test-at-point-debugging-errors]
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2046 ["Show test definition" ert-results-find-test-at-point-other-window]
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2047 "--"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2048 ["Show backtrace" ert-results-pop-to-backtrace-for-test-at-point]
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2049 ["Show messages" ert-results-pop-to-messages-for-test-at-point]
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2050 ["Show `should' forms" ert-results-pop-to-should-forms-for-test-at-point]
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2051 ["Describe test" ert-results-describe-test-at-point]
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2052 "--"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2053 ["Delete test" ert-delete-test]
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2054 "--"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2055 ["Show execution time of each test" ert-results-pop-to-timings]
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2056 ))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2057
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2058 (define-button-type 'ert--results-progress-bar-button
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2059 'action #'ert--results-progress-bar-button-action
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2060 'help-echo "mouse-2, RET: Reveal test result")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2061
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2062 (define-button-type 'ert--test-name-button
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2063 'action #'ert--test-name-button-action
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2064 'help-echo "mouse-2, RET: Find test definition")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2065
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2066 (define-button-type 'ert--results-expand-collapse-button
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2067 'action #'ert--results-expand-collapse-button-action
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2068 'help-echo "mouse-2, RET: Expand/collapse test result")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2069
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2070 (defun ert--results-test-node-or-null-at-point ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2071 "If point is on a valid ewoc node, return it; return nil otherwise.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2072
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2073 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2074 (let* ((ewoc ert--results-ewoc)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2075 (node (ewoc-locate ewoc)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2076 ;; `ewoc-locate' will return an arbitrary node when point is on
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2077 ;; header or footer, or when all nodes are invisible. So we need
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2078 ;; to validate its return value here.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2079 ;;
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2080 ;; Update: I'm seeing nil being returned in some cases now,
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2081 ;; perhaps this has been changed?
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2082 (if (and node
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2083 (>= (point) (ewoc-location node))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2084 (not (ert--ewoc-entry-hidden-p (ewoc-data node))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2085 node
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2086 nil)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2087
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2088 (defun ert--results-test-node-at-point ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2089 "If point is on a valid ewoc node, return it; signal an error otherwise.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2090
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2091 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2092 (or (ert--results-test-node-or-null-at-point)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2093 (error "No test at point")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2094
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2095 (defun ert-results-next-test ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2096 "Move point to the next test.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2097
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2098 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2099 (interactive)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2100 (ert--results-move (ewoc-locate ert--results-ewoc) 'ewoc-next
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2101 "No tests below"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2102
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2103 (defun ert-results-previous-test ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2104 "Move point to the previous test.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2105
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2106 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2107 (interactive)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2108 (ert--results-move (ewoc-locate ert--results-ewoc) 'ewoc-prev
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2109 "No tests above"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2110
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2111 (defun ert--results-move (node ewoc-fn error-message)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2112 "Move point from NODE to the previous or next node.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2113
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2114 EWOC-FN specifies the direction and should be either `ewoc-prev'
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2115 or `ewoc-next'. If there are no more nodes in that direction, an
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2116 error is signalled with the message ERROR-MESSAGE."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2117 (loop
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2118 (setq node (funcall ewoc-fn ert--results-ewoc node))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2119 (when (null node)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2120 (error "%s" error-message))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2121 (unless (ert--ewoc-entry-hidden-p (ewoc-data node))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2122 (goto-char (ewoc-location node))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2123 (return))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2124
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2125 (defun ert--results-expand-collapse-button-action (button)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2126 "Expand or collapse the test node BUTTON belongs to."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2127 (let* ((ewoc ert--results-ewoc)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2128 (node (save-excursion
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2129 (goto-char (ert--button-action-position))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2130 (ert--results-test-node-at-point)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2131 (entry (ewoc-data node)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2132 (setf (ert--ewoc-entry-expanded-p entry)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2133 (not (ert--ewoc-entry-expanded-p entry)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2134 (ewoc-invalidate ewoc node)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2135
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2136 (defun ert-results-find-test-at-point-other-window ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2137 "Find the definition of the test at point in another window.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2138
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2139 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2140 (interactive)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2141 (let ((name (ert-test-at-point)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2142 (unless name
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2143 (error "No test at point"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2144 (ert-find-test-other-window name)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2145
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2146 (defun ert--test-name-button-action (button)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2147 "Find the definition of the test BUTTON belongs to, in another window."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2148 (let ((name (button-get button 'ert-test-name)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2149 (ert-find-test-other-window name)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2150
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2151 (defun ert--ewoc-position (ewoc node)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2152 ;; checkdoc-order: nil
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2153 "Return the position of NODE in EWOC, or nil if NODE is not in EWOC."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2154 (loop for i from 0
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2155 for node-here = (ewoc-nth ewoc 0) then (ewoc-next ewoc node-here)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2156 do (when (eql node node-here)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2157 (return i))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2158 finally (return nil)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2159
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2160 (defun ert-results-jump-between-summary-and-result ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2161 "Jump back and forth between the test run summary and individual test results.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2162
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2163 From an ewoc node, jumps to the character that represents the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2164 same test in the progress bar, and vice versa.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2165
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2166 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2167 ;; Maybe this command isn't actually needed much, but if it is, it
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2168 ;; seems like an indication that the UI design is not optimal. If
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2169 ;; jumping back and forth between a summary at the top of the buffer
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2170 ;; and the error log in the remainder of the buffer is useful, then
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2171 ;; the summary apparently needs to be easily accessible from the
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2172 ;; error log, and perhaps it would be better to have it in a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2173 ;; separate buffer to keep it visible.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2174 (interactive)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2175 (let ((ewoc ert--results-ewoc)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2176 (progress-bar-begin ert--results-progress-bar-button-begin))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2177 (cond ((ert--results-test-node-or-null-at-point)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2178 (let* ((node (ert--results-test-node-at-point))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2179 (pos (ert--ewoc-position ewoc node)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2180 (goto-char (+ progress-bar-begin pos))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2181 ((and (<= progress-bar-begin (point))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2182 (< (point) (button-end (button-at progress-bar-begin))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2183 (let* ((node (ewoc-nth ewoc (- (point) progress-bar-begin)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2184 (entry (ewoc-data node)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2185 (when (ert--ewoc-entry-hidden-p entry)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2186 (setf (ert--ewoc-entry-hidden-p entry) nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2187 (ewoc-invalidate ewoc node))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2188 (ewoc-goto-node ewoc node)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2189 (t
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2190 (goto-char progress-bar-begin)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2191
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2192 (defun ert-test-at-point ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2193 "Return the name of the test at point as a symbol, or nil if none."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2194 (or (and (eql major-mode 'ert-results-mode)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2195 (let ((test (ert--results-test-at-point-no-redefinition)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2196 (and test (ert-test-name test))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2197 (let* ((thing (thing-at-point 'symbol))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2198 (sym (intern-soft thing)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2199 (and (ert-test-boundp sym)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2200 sym))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2201
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2202 (defun ert--results-test-at-point-no-redefinition ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2203 "Return the test at point, or nil.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2204
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2205 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2206 (assert (eql major-mode 'ert-results-mode))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2207 (if (ert--results-test-node-or-null-at-point)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2208 (let* ((node (ert--results-test-node-at-point))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2209 (test (ert--ewoc-entry-test (ewoc-data node))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2210 test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2211 (let ((progress-bar-begin ert--results-progress-bar-button-begin))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2212 (when (and (<= progress-bar-begin (point))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2213 (< (point) (button-end (button-at progress-bar-begin))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2214 (let* ((test-index (- (point) progress-bar-begin))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2215 (test (aref (ert--stats-tests ert--results-stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2216 test-index)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2217 test)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2218
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2219 (defun ert--results-test-at-point-allow-redefinition ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2220 "Look up the test at point, and check whether it has been redefined.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2221
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2222 To be used in the ERT results buffer.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2223
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2224 Returns a list of two elements: the test (or nil) and a symbol
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2225 specifying whether the test has been redefined.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2226
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2227 If a new test has been defined with the same name as the test at
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2228 point, replaces the test at point with the new test, and returns
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2229 the new test and the symbol `redefined'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2230
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2231 If the test has been deleted, returns the old test and the symbol
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2232 `deleted'.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2233
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2234 If the test is still current, returns the test and the symbol nil.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2235
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2236 If there is no test at point, returns a list with two nils."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2237 (let ((test (ert--results-test-at-point-no-redefinition)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2238 (cond ((null test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2239 `(nil nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2240 ((null (ert-test-name test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2241 `(,test nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2242 (t
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2243 (let* ((name (ert-test-name test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2244 (new-test (and (ert-test-boundp name)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2245 (ert-get-test name))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2246 (cond ((eql test new-test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2247 `(,test nil))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2248 ((null new-test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2249 `(,test deleted))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2250 (t
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2251 (ert--results-update-after-test-redefinition
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2252 (ert--stats-test-pos ert--results-stats test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2253 new-test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2254 `(,new-test redefined))))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2255
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2256 (defun ert--results-update-after-test-redefinition (pos new-test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2257 "Update results buffer after the test at pos POS has been redefined.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2258
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2259 Also updates the stats object. NEW-TEST is the new test
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2260 definition."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2261 (let* ((stats ert--results-stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2262 (ewoc ert--results-ewoc)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2263 (node (ewoc-nth ewoc pos))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2264 (entry (ewoc-data node)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2265 (ert--stats-set-test-and-result stats pos new-test nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2266 (setf (ert--ewoc-entry-test entry) new-test
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2267 (aref ert--results-progress-bar-string pos) (ert-char-for-test-result
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2268 nil t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2269 (ewoc-invalidate ewoc node))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2270 nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2271
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2272 (defun ert--button-action-position ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2273 "The buffer position where the last button action was triggered."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2274 (cond ((integerp last-command-event)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2275 (point))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2276 ((eventp last-command-event)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2277 (posn-point (event-start last-command-event)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2278 (t (assert nil))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2279
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2280 (defun ert--results-progress-bar-button-action (button)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2281 "Jump to details for the test represented by the character clicked in BUTTON."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2282 (goto-char (ert--button-action-position))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2283 (ert-results-jump-between-summary-and-result))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2284
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2285 (defun ert-results-rerun-all-tests ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2286 "Re-run all tests, using the same selector.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2287
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2288 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2289 (interactive)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2290 (assert (eql major-mode 'ert-results-mode))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2291 (let ((selector (ert--stats-selector ert--results-stats)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2292 (ert-run-tests-interactively selector (buffer-name))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2293
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2294 (defun ert-results-rerun-test-at-point ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2295 "Re-run the test at point.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2296
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2297 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2298 (interactive)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2299 (destructuring-bind (test redefinition-state)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2300 (ert--results-test-at-point-allow-redefinition)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2301 (when (null test)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2302 (error "No test at point"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2303 (let* ((stats ert--results-stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2304 (progress-message (format "Running %stest %S"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2305 (ecase redefinition-state
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2306 ((nil) "")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2307 (redefined "new definition of ")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2308 (deleted "deleted "))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2309 (ert-test-name test))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2310 ;; Need to save and restore point manually here: When point is on
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2311 ;; the first visible ewoc entry while the header is updated, point
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2312 ;; moves to the top of the buffer. This is undesirable, and a
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2313 ;; simple `save-excursion' doesn't prevent it.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2314 (let ((point (point)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2315 (unwind-protect
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2316 (unwind-protect
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2317 (progn
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2318 (message "%s..." progress-message)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2319 (ert-run-or-rerun-test stats test
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2320 ert--results-listener))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2321 (ert--results-update-stats-display ert--results-ewoc stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2322 (message "%s...%s"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2323 progress-message
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2324 (let ((result (ert-test-most-recent-result test)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2325 (ert-string-for-test-result
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2326 result (ert-test-result-expected-p test result)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2327 (goto-char point))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2328
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2329 (defun ert-results-rerun-test-at-point-debugging-errors ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2330 "Re-run the test at point with `ert-debug-on-error' bound to t.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2331
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2332 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2333 (interactive)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2334 (let ((ert-debug-on-error t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2335 (ert-results-rerun-test-at-point)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2336
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2337 (defun ert-results-pop-to-backtrace-for-test-at-point ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2338 "Display the backtrace for the test at point.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2339
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2340 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2341 (interactive)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2342 (let* ((test (ert--results-test-at-point-no-redefinition))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2343 (stats ert--results-stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2344 (pos (ert--stats-test-pos stats test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2345 (result (aref (ert--stats-test-results stats) pos)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2346 (etypecase result
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2347 (ert-test-passed (error "Test passed, no backtrace available"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2348 (ert-test-result-with-condition
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2349 (let ((backtrace (ert-test-result-with-condition-backtrace result))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2350 (buffer (get-buffer-create "*ERT Backtrace*")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2351 (pop-to-buffer buffer)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2352 (setq buffer-read-only t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2353 (let ((inhibit-read-only t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2354 (buffer-disable-undo)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2355 (erase-buffer)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2356 (ert-simple-view-mode)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2357 ;; Use unibyte because `debugger-setup-buffer' also does so.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2358 (set-buffer-multibyte nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2359 (setq truncate-lines t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2360 (ert--print-backtrace backtrace)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2361 (debugger-make-xrefs)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2362 (goto-char (point-min))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2363 (insert "Backtrace for test `")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2364 (ert-insert-test-name-button (ert-test-name test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2365 (insert "':\n")))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2366
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2367 (defun ert-results-pop-to-messages-for-test-at-point ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2368 "Display the part of the *Messages* buffer generated during the test at point.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2369
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2370 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2371 (interactive)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2372 (let* ((test (ert--results-test-at-point-no-redefinition))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2373 (stats ert--results-stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2374 (pos (ert--stats-test-pos stats test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2375 (result (aref (ert--stats-test-results stats) pos)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2376 (let ((buffer (get-buffer-create "*ERT Messages*")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2377 (pop-to-buffer buffer)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2378 (setq buffer-read-only t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2379 (let ((inhibit-read-only t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2380 (buffer-disable-undo)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2381 (erase-buffer)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2382 (ert-simple-view-mode)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2383 (insert (ert-test-result-messages result))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2384 (goto-char (point-min))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2385 (insert "Messages for test `")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2386 (ert-insert-test-name-button (ert-test-name test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2387 (insert "':\n")))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2388
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2389 (defun ert-results-pop-to-should-forms-for-test-at-point ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2390 "Display the list of `should' forms executed during the test at point.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2391
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2392 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2393 (interactive)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2394 (let* ((test (ert--results-test-at-point-no-redefinition))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2395 (stats ert--results-stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2396 (pos (ert--stats-test-pos stats test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2397 (result (aref (ert--stats-test-results stats) pos)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2398 (let ((buffer (get-buffer-create "*ERT list of should forms*")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2399 (pop-to-buffer buffer)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2400 (setq buffer-read-only t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2401 (let ((inhibit-read-only t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2402 (buffer-disable-undo)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2403 (erase-buffer)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2404 (ert-simple-view-mode)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2405 (if (null (ert-test-result-should-forms result))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2406 (insert "\n(No should forms during this test.)\n")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2407 (loop for form-description in (ert-test-result-should-forms result)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2408 for i from 1 do
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2409 (insert "\n")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2410 (insert (format "%s: " i))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2411 (let ((begin (point)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2412 (ert--pp-with-indentation-and-newline form-description)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2413 (ert--make-xrefs-region begin (point)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2414 (goto-char (point-min))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2415 (insert "`should' forms executed during test `")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2416 (ert-insert-test-name-button (ert-test-name test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2417 (insert "':\n")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2418 (insert "\n")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2419 (insert (concat "(Values are shallow copies and may have "
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2420 "looked different during the test if they\n"
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2421 "have been modified destructively.)\n"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2422 (forward-line 1)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2423
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2424 (defun ert-results-toggle-printer-limits-for-test-at-point ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2425 "Toggle how much of the condition to print for the test at point.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2426
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2427 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2428 (interactive)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2429 (let* ((ewoc ert--results-ewoc)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2430 (node (ert--results-test-node-at-point))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2431 (entry (ewoc-data node)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2432 (setf (ert--ewoc-entry-extended-printer-limits-p entry)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2433 (not (ert--ewoc-entry-extended-printer-limits-p entry)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2434 (ewoc-invalidate ewoc node)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2435
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2436 (defun ert-results-pop-to-timings ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2437 "Display test timings for the last run.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2438
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2439 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2440 (interactive)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2441 (let* ((stats ert--results-stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2442 (start-times (ert--stats-test-start-times stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2443 (end-times (ert--stats-test-end-times stats))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2444 (buffer (get-buffer-create "*ERT timings*"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2445 (data (loop for test across (ert--stats-tests stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2446 for start-time across (ert--stats-test-start-times stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2447 for end-time across (ert--stats-test-end-times stats)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2448 collect (list test
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2449 (float-time (subtract-time end-time
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2450 start-time))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2451 (setq data (sort data (lambda (a b)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2452 (> (second a) (second b)))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2453 (pop-to-buffer buffer)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2454 (setq buffer-read-only t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2455 (let ((inhibit-read-only t))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2456 (buffer-disable-undo)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2457 (erase-buffer)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2458 (ert-simple-view-mode)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2459 (if (null data)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2460 (insert "(No data)\n")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2461 (insert (format "%-3s %8s %8s\n" "" "time" "cumul"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2462 (loop for (test time) in data
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2463 for cumul-time = time then (+ cumul-time time)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2464 for i from 1 do
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2465 (let ((begin (point)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2466 (insert (format "%3s: %8.3f %8.3f " i time cumul-time))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2467 (ert-insert-test-name-button (ert-test-name test))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2468 (insert "\n"))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2469 (goto-char (point-min))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2470 (insert "Tests by run time (seconds):\n\n")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2471 (forward-line 1))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2472
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2473 ;;;###autoload
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2474 (defun ert-describe-test (test-or-test-name)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2475 "Display the documentation for TEST-OR-TEST-NAME (a symbol or ert-test)."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2476 (interactive (list (ert-read-test-name-at-point "Describe test")))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2477 (when (< emacs-major-version 24)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2478 (error "Requires Emacs 24"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2479 (let (test-name
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2480 test-definition)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2481 (etypecase test-or-test-name
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2482 (symbol (setq test-name test-or-test-name
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2483 test-definition (ert-get-test test-or-test-name)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2484 (ert-test (setq test-name (ert-test-name test-or-test-name)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2485 test-definition test-or-test-name)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2486 (help-setup-xref (list #'ert-describe-test test-or-test-name)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2487 (called-interactively-p 'interactive))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2488 (save-excursion
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2489 (with-help-window (help-buffer)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2490 (with-current-buffer (help-buffer)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2491 (insert (if test-name (format "%S" test-name) "<anonymous test>"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2492 (insert " is a test")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2493 (let ((file-name (and test-name
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2494 (symbol-file test-name 'ert-deftest))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2495 (when file-name
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2496 (insert " defined in `" (file-name-nondirectory file-name) "'")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2497 (save-excursion
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2498 (re-search-backward "`\\([^`']+\\)'" nil t)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2499 (help-xref-button 1 'help-function-def test-name file-name)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2500 (insert ".")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2501 (fill-region-as-paragraph (point-min) (point))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2502 (insert "\n\n")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2503 (unless (and (ert-test-boundp test-name)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2504 (eql (ert-get-test test-name) test-definition))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2505 (let ((begin (point)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2506 (insert "Note: This test has been redefined or deleted, "
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2507 "this documentation refers to an old definition.")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2508 (fill-region-as-paragraph begin (point)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2509 (insert "\n\n"))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2510 (insert (or (ert-test-documentation test-definition)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2511 "It is not documented.")
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2512 "\n")))))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2513
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2514 (defun ert-results-describe-test-at-point ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2515 "Display the documentation of the test at point.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2516
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2517 To be used in the ERT results buffer."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2518 (interactive)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2519 (ert-describe-test (ert--results-test-at-point-no-redefinition)))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2520
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2521
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2522 ;;; Actions on load/unload.
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2523
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2524 (add-to-list 'find-function-regexp-alist '(ert-deftest . ert--find-test-regexp))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2525 (add-to-list 'minor-mode-alist '(ert--current-run-stats
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2526 (:eval
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2527 (ert--tests-running-mode-line-indicator))))
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2528 (add-to-list 'emacs-lisp-mode-hook 'ert--activate-font-lock-keywords)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2529
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2530 (defun ert--unload-function ()
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2531 "Unload function to undo the side-effects of loading ert.el."
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2532 (ert--remove-from-list 'find-function-regexp-alist 'ert-deftest :key #'car)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2533 (ert--remove-from-list 'minor-mode-alist 'ert--current-run-stats :key #'car)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2534 (ert--remove-from-list 'emacs-lisp-mode-hook
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2535 'ert--activate-font-lock-keywords)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2536 nil)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2537
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2538 (defvar ert-unload-hook '())
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2539 (add-hook 'ert-unload-hook 'ert--unload-function)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2540
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2541
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2542 (provide 'ert)
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2543
c5917804ecad Add ERT, a tool for automated testing in Emacs Lisp.
Christian Ohler <ohler@gnu.org>
parents:
diff changeset
2544 ;;; ert.el ends here