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