Fix syms_of_(mac|ms)font calls.
[emacs.git] / test / automated / eshell.el
blob6f7a35371a64ac85605e7ef053e9d58ae32a8a60
1 ;;; tests/eshell.el --- Eshell test suite
3 ;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; Eshell test suite.
26 ;;; Code:
28 (require 'ert)
29 (require 'eshell)
31 (defmacro with-temp-eshell (&rest body)
32 "Evaluate BODY in a temporary Eshell buffer."
33 `(let* ((eshell-directory-name (make-temp-file "eshell" t))
34 (eshell-history-file-name nil)
35 (eshell-buffer (eshell t)))
36 (unwind-protect
37 (with-current-buffer eshell-buffer
38 ,@body)
39 (let (kill-buffer-query-functions)
40 (kill-buffer eshell-buffer)
41 (delete-directory eshell-directory-name t)))))
43 (defun eshell-insert-command (text &optional func)
44 "Insert a command at the end of the buffer."
45 (goto-char eshell-last-output-end)
46 (insert-and-inherit text)
47 (funcall (or func 'eshell-send-input)))
49 (defun eshell-match-result (regexp)
50 "Insert a command at the end of the buffer."
51 (goto-char eshell-last-input-end)
52 (looking-at regexp))
54 (defun eshell-command-result-p (text regexp &optional func)
55 "Insert a command at the end of the buffer."
56 (eshell-insert-command text func)
57 (eshell-match-result regexp))
59 (defun eshell-test-command-result (command)
60 "Like `eshell-command-result', but not using HOME."
61 (let ((eshell-directory-name (make-temp-file "eshell" t))
62 (eshell-history-file-name nil))
63 (unwind-protect
64 (eshell-command-result command)
65 (delete-directory eshell-directory-name t))))
67 ;;; Tests:
69 (ert-deftest eshell-test/simple-command-result ()
70 "Test `eshell-command-result' with a simple command."
71 (should (equal (eshell-test-command-result "+ 1 2") 3)))
73 (ert-deftest eshell-test/lisp-command ()
74 "Test `eshell-command-result' with an elisp command."
75 (should (equal (eshell-test-command-result "(+ 1 2)") 3)))
77 (ert-deftest eshell-test/for-loop ()
78 "Test `eshell-command-result' with a for loop.."
79 (let ((process-environment (cons "foo" process-environment)))
80 (should (equal (eshell-test-command-result
81 "for foo in 5 { echo $foo }") 5))))
83 (ert-deftest eshell-test/for-name-loop () ;Bug#15231
84 "Test `eshell-command-result' with a for loop using `name'."
85 (let ((process-environment (cons "name" process-environment)))
86 (should (equal (eshell-test-command-result
87 "for name in 3 { echo $name }") 3))))
89 (ert-deftest eshell-test/for-name-shadow-loop () ; bug#15372
90 "Test `eshell-command-result' with a for loop using an env-var."
91 (let ((process-environment (cons "name=env-value" process-environment)))
92 (should (equal (eshell-test-command-result
93 "for name in 3 { echo $name }") 3))))
95 (ert-deftest eshell-test/lisp-command-args ()
96 "Test `eshell-command-result' with elisp and trailing args.
97 Test that trailing arguments outside the S-expression are
98 ignored. e.g. \"(+ 1 2) 3\" => 3"
99 (should (equal (eshell-test-command-result "(+ 1 2) 3") 3)))
101 (ert-deftest eshell-test/subcommand ()
102 "Test `eshell-command-result' with a simple subcommand."
103 (should (equal (eshell-test-command-result "{+ 1 2}") 3)))
105 (ert-deftest eshell-test/subcommand-args ()
106 "Test `eshell-command-result' with a subcommand and trailing args.
107 Test that trailing arguments outside the subcommand are ignored.
108 e.g. \"{+ 1 2} 3\" => 3"
109 (should (equal (eshell-test-command-result "{+ 1 2} 3") 3)))
111 (ert-deftest eshell-test/subcommand-lisp ()
112 "Test `eshell-command-result' with an elisp subcommand and trailing args.
113 Test that trailing arguments outside the subcommand are ignored.
114 e.g. \"{(+ 1 2)} 3\" => 3"
115 (should (equal (eshell-test-command-result "{(+ 1 2)} 3") 3)))
117 (ert-deftest eshell-test/interp-cmd ()
118 "Interpolate command result"
119 (should (equal (eshell-test-command-result "+ ${+ 1 2} 3") 6)))
121 (ert-deftest eshell-test/interp-lisp ()
122 "Interpolate Lisp form evaluation"
123 (should (equal (eshell-test-command-result "+ $(+ 1 2) 3") 6)))
125 (ert-deftest eshell-test/interp-concat ()
126 "Interpolate and concat command"
127 (should (equal (eshell-test-command-result "+ ${+ 1 2}3 3") 36)))
129 (ert-deftest eshell-test/interp-concat-lisp ()
130 "Interpolate and concat Lisp form"
131 (should (equal (eshell-test-command-result "+ $(+ 1 2)3 3") 36)))
133 (ert-deftest eshell-test/interp-concat2 ()
134 "Interpolate and concat two commands"
135 (should (equal (eshell-test-command-result "+ ${+ 1 2}${+ 1 2} 3") 36)))
137 (ert-deftest eshell-test/interp-concat-lisp2 ()
138 "Interpolate and concat two Lisp forms"
139 (should (equal (eshell-test-command-result "+ $(+ 1 2)$(+ 1 2) 3") 36)))
141 (ert-deftest eshell-test/window-height ()
142 "$LINES should equal (window-height)"
143 (should (eshell-test-command-result "= $LINES (window-height)")))
145 (ert-deftest eshell-test/window-width ()
146 "$COLUMNS should equal (window-width)"
147 (should (eshell-test-command-result "= $COLUMNS (window-width)")))
149 (ert-deftest eshell-test/last-result-var ()
150 "Test using the \"last result\" ($$) variable"
151 (with-temp-eshell
152 (should
153 (eshell-command-result-p "+ 1 2; + $$ 2"
154 "3\n5\n"))))
156 (ert-deftest eshell-test/last-result-var2 ()
157 "Test using the \"last result\" ($$) variable twice"
158 (with-temp-eshell
159 (should
160 (eshell-command-result-p "+ 1 2; + $$ $$"
161 "3\n6\n"))))
163 (ert-deftest eshell-test/last-arg-var ()
164 "Test using the \"last arg\" ($_) variable"
165 (with-temp-eshell
166 (should
167 (eshell-command-result-p "+ 1 2; + $_ 4"
168 "3\n6\n"))))
170 (ert-deftest eshell-test/command-running-p ()
171 "Modeline should show no command running"
172 (with-temp-eshell
173 (let ((eshell-status-in-mode-line t))
174 (should (memq 'eshell-command-running-string mode-line-format))
175 (should (equal eshell-command-running-string "--")))))
177 (ert-deftest eshell-test/forward-arg ()
178 "Test moving across command arguments"
179 (with-temp-eshell
180 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore)
181 (let ((here (point)) begin valid)
182 (eshell-bol)
183 (setq begin (point))
184 (eshell-forward-argument 4)
185 (setq valid (= here (point)))
186 (eshell-backward-argument 4)
187 (prog1
188 (and valid (= begin (point)))
189 (eshell-bol)
190 (delete-region (point) (point-max))))))
192 (ert-deftest eshell-test/queue-input ()
193 "Test queuing command input"
194 (with-temp-eshell
195 (eshell-insert-command "sleep 2")
196 (eshell-insert-command "echo alpha" 'eshell-queue-input)
197 (let ((count 10))
198 (while (and eshell-current-command
199 (> count 0))
200 (sit-for 1)
201 (setq count (1- count))))
202 (should (eshell-match-result "alpha\n"))))
204 (ert-deftest eshell-test/flush-output ()
205 "Test flushing of previous output"
206 (with-temp-eshell
207 (eshell-insert-command "echo alpha")
208 (eshell-kill-output)
209 (should (eshell-match-result (regexp-quote "*** output flushed ***\n")))
210 (should (forward-line))
211 (should (= (point) eshell-last-output-start))))
213 (ert-deftest eshell-test/run-old-command ()
214 "Re-run an old command"
215 (with-temp-eshell
216 (eshell-insert-command "echo alpha")
217 (goto-char eshell-last-input-start)
218 (string= (eshell-get-old-input) "echo alpha")))
220 (provide 'esh-test)
222 ;;; tests/eshell.el ends here