]> code.delx.au - gnu-emacs/blob - lisp/eshell/esh-test.el
*** empty log message ***
[gnu-emacs] / lisp / eshell / esh-test.el
1 ;;; esh-test --- Eshell test suite
2
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
21
22 (provide 'esh-test)
23
24 (eval-when-compile (require 'esh-maint))
25
26 (defgroup eshell-test nil
27 "This module is meant to ensure that Eshell is working correctly."
28 :tag "Eshell test suite"
29 :group 'eshell)
30
31 ;;; Commentary:
32
33 ;; The purpose of this module is to verify that Eshell works as
34 ;; expected. To run it on your system, use the command
35 ;; \\[eshell-test].
36
37 ;;; Code:
38
39 (require 'esh-mode)
40
41 ;;; User Variables:
42
43 (defface eshell-test-ok-face
44 '((((class color) (background light)) (:foreground "Green" :bold t))
45 (((class color) (background dark)) (:foreground "Green" :bold t)))
46 "*The face used to highlight OK result strings."
47 :group 'eshell-test)
48
49 (defface eshell-test-failed-face
50 '((((class color) (background light)) (:foreground "OrangeRed" :bold t))
51 (((class color) (background dark)) (:foreground "OrangeRed" :bold t))
52 (t (:bold t)))
53 "*The face used to highlight FAILED result strings."
54 :group 'eshell-test)
55
56 (defcustom eshell-show-usage-metrics nil
57 "*If non-nil, display different usage metrics for each Eshell command."
58 :set (lambda (symbol value)
59 (if value
60 (add-hook 'eshell-mode-hook 'eshell-show-usage-metrics)
61 (remove-hook 'eshell-mode-hook 'eshell-show-usage-metrics))
62 (set symbol value))
63 :type '(choice (const :tag "No metrics" nil)
64 (const :tag "Cons cells consumed" t)
65 (const :tag "Time elapsed" 0))
66 :group 'eshell-test)
67
68 ;;; Code:
69
70 (eval-when-compile
71 (defvar test-buffer))
72
73 (defun eshell-insert-command (text &optional func)
74 "Insert a command at the end of the buffer."
75 (goto-char eshell-last-output-end)
76 (insert-and-inherit text)
77 (funcall (or func 'eshell-send-input)))
78
79 (defun eshell-match-result (regexp)
80 "Insert a command at the end of the buffer."
81 (goto-char eshell-last-input-end)
82 (looking-at regexp))
83
84 (defun eshell-command-result-p (text regexp &optional func)
85 "Insert a command at the end of the buffer."
86 (eshell-insert-command text func)
87 (eshell-match-result regexp))
88
89 (defvar eshell-test-failures nil)
90
91 (defun eshell-run-test (module funcsym label command)
92 "Test whether FORM evaluates to a non-nil value."
93 (when (let ((sym (intern-soft (concat "eshell-" (symbol-name module)))))
94 (or (memq sym (eshell-subgroups 'eshell))
95 (eshell-using-module sym)))
96 (with-current-buffer test-buffer
97 (insert-before-markers
98 (format "%-70s " (substring label 0 (min 70 (length label)))))
99 (insert-before-markers " ....")
100 (eshell-redisplay))
101 (let ((truth (eval command)))
102 (with-current-buffer test-buffer
103 (delete-backward-char 6)
104 (insert-before-markers
105 "[" (let (str)
106 (if truth
107 (progn
108 (setq str " OK ")
109 (put-text-property 0 6 'face
110 'eshell-test-ok-face str))
111 (setq str "FAILED")
112 (setq eshell-test-failures (1+ eshell-test-failures))
113 (put-text-property 0 6 'face
114 'eshell-test-failed-face str))
115 str) "]")
116 (add-text-properties (line-beginning-position) (point)
117 (list 'test-func funcsym))
118 (eshell-redisplay)))))
119
120 (defun eshell-test-goto-func ()
121 "Jump to the function that defines a particular test."
122 (interactive)
123 (let ((fsym (get-text-property (point) 'test-func)))
124 (when fsym
125 (let* ((def (symbol-function fsym))
126 (library (locate-library (symbol-file fsym)))
127 (name (substring (symbol-name fsym)
128 (length "eshell-test--")))
129 (inhibit-redisplay t))
130 (find-file library)
131 (goto-char (point-min))
132 (re-search-forward (concat "^(eshell-deftest\\s-+\\w+\\s-+"
133 name))
134 (beginning-of-line)))))
135
136 (defun eshell-run-one-test (&optional arg)
137 "Jump to the function that defines a particular test."
138 (interactive "P")
139 (let ((fsym (get-text-property (point) 'test-func)))
140 (when fsym
141 (beginning-of-line)
142 (delete-region (point) (line-end-position))
143 (let ((test-buffer (current-buffer)))
144 (set-buffer (let ((inhibit-redisplay t))
145 (save-window-excursion (eshell t))))
146 (funcall fsym)
147 (unless arg
148 (kill-buffer (current-buffer)))))))
149
150 ;;;###autoload
151 (defun eshell-test (&optional arg)
152 "Test Eshell to verify that it works as expected."
153 (interactive "P")
154 (let* ((begin (eshell-time-to-seconds (current-time)))
155 (test-buffer (get-buffer-create "*eshell test*")))
156 (set-buffer (let ((inhibit-redisplay t))
157 (save-window-excursion (eshell t))))
158 (with-current-buffer test-buffer
159 (erase-buffer)
160 (setq major-mode 'eshell-test-mode)
161 (setq mode-name "EShell Test")
162 (set (make-local-variable 'eshell-test-failures) 0)
163 (local-set-key [(control ?c) (control ?c)] 'eshell-test-goto-func)
164 (local-set-key [(control ?c) (control ?r)] 'eshell-run-one-test)
165 (local-set-key [(control ?m)] 'eshell-test-goto-func)
166 (local-set-key [return] 'eshell-test-goto-func)
167
168 (insert "Testing Eshell under "
169 (format "GNU Emacs %s (%s%s)"
170 emacs-version
171 system-configuration
172 (cond ((featurep 'motif) ", Motif")
173 ((featurep 'x-toolkit) ", X toolkit")
174 (t ""))) "\n")
175 (switch-to-buffer test-buffer)
176 (delete-other-windows))
177 (eshell-for funcname
178 (sort (all-completions "eshell-test--" obarray 'functionp)
179 'string-lessp)
180 (with-current-buffer test-buffer
181 (insert "\n"))
182 (funcall (intern-soft funcname)))
183 (with-current-buffer test-buffer
184 (insert (format "\n\n--- %s --- (completed in %d seconds)\n"
185 (current-time-string)
186 (- (eshell-time-to-seconds (current-time))
187 begin)))
188 (message "Eshell test suite completed: %s failure%s"
189 (if (> eshell-test-failures 0)
190 (number-to-string eshell-test-failures)
191 "No")
192 (if (= eshell-test-failures 1) "" "s"))))
193 (goto-char eshell-last-output-end)
194 (unless arg
195 (kill-buffer (current-buffer))))
196
197
198 (defvar eshell-metric-before-command 0)
199 (defvar eshell-metric-after-command 0)
200
201 (defun eshell-show-usage-metrics ()
202 "If run at Eshell mode startup, metrics are shown after each command."
203 (set (make-local-variable 'eshell-metric-before-command)
204 (if (eq eshell-show-usage-metrics t)
205 0
206 (current-time)))
207 (set (make-local-variable 'eshell-metric-after-command)
208 (if (eq eshell-show-usage-metrics t)
209 0
210 (current-time)))
211
212 (make-local-hook 'eshell-pre-command-hook)
213 (add-hook 'eshell-pre-command-hook
214 (function
215 (lambda ()
216 (setq eshell-metric-before-command
217 (if (eq eshell-show-usage-metrics t)
218 (car (memory-use-counts))
219 (current-time))))) nil t)
220
221 (make-local-hook 'eshell-post-command-hook)
222 (add-hook 'eshell-post-command-hook
223 (function
224 (lambda ()
225 (setq eshell-metric-after-command
226 (if (eq eshell-show-usage-metrics t)
227 (car (memory-use-counts))
228 (current-time)))
229 (eshell-interactive-print
230 (concat
231 (int-to-string
232 (if (eq eshell-show-usage-metrics t)
233 (- eshell-metric-after-command
234 eshell-metric-before-command 7)
235 (- (eshell-time-to-seconds
236 eshell-metric-after-command)
237 (eshell-time-to-seconds
238 eshell-metric-before-command))))
239 "\n"))))
240 nil t))
241
242 ;;; esh-test.el ends here