Don't unnecessarily use non-ASCII characters in C sources
[emacs.git] / test / lisp / eshell / eshell-tests.el
blobb6dbd09a3d6326d157587258b9eefc1ec4af8fc1
1 ;;; tests/eshell-tests.el --- Eshell test suite
3 ;; Copyright (C) 1999-2018 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 <https://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 "Check that text after `eshell-last-input-end' matches REGEXP."
51 (goto-char eshell-last-input-end)
52 (should (string-match-p regexp (buffer-substring-no-properties
53 (point) (point-max)))))
55 (defun eshell-command-result-p (text regexp &optional func)
56 "Insert a command at the end of the buffer."
57 (eshell-insert-command text func)
58 (eshell-match-result regexp))
60 (defun eshell-test-command-result (command)
61 "Like `eshell-command-result', but not using HOME."
62 (let ((eshell-directory-name (make-temp-file "eshell" t))
63 (eshell-history-file-name nil))
64 (unwind-protect
65 (eshell-command-result command)
66 (delete-directory eshell-directory-name t))))
68 ;;; Tests:
70 (ert-deftest eshell-test/simple-command-result ()
71 "Test `eshell-command-result' with a simple command."
72 (should (equal (eshell-test-command-result "+ 1 2") 3)))
74 (ert-deftest eshell-test/lisp-command ()
75 "Test `eshell-command-result' with an elisp command."
76 (should (equal (eshell-test-command-result "(+ 1 2)") 3)))
78 (ert-deftest eshell-test/for-loop ()
79 "Test `eshell-command-result' with a for loop.."
80 (let ((process-environment (cons "foo" process-environment)))
81 (should (equal (eshell-test-command-result
82 "for foo in 5 { echo $foo }") 5))))
84 (ert-deftest eshell-test/for-name-loop () ;Bug#15231
85 "Test `eshell-command-result' with a for loop using `name'."
86 (let ((process-environment (cons "name" process-environment)))
87 (should (equal (eshell-test-command-result
88 "for name in 3 { echo $name }") 3))))
90 (ert-deftest eshell-test/for-name-shadow-loop () ; bug#15372
91 "Test `eshell-command-result' with a for loop using an env-var."
92 (let ((process-environment (cons "name=env-value" process-environment)))
93 (with-temp-eshell
94 (eshell-command-result-p "echo $name; for name in 3 { echo $name }; echo $name"
95 "env-value\n3\nenv-value\n"))))
97 (ert-deftest eshell-test/lisp-command-args ()
98 "Test `eshell-command-result' with elisp and trailing args.
99 Test that trailing arguments outside the S-expression are
100 ignored. e.g. \"(+ 1 2) 3\" => 3"
101 (should (equal (eshell-test-command-result "(+ 1 2) 3") 3)))
103 (ert-deftest eshell-test/subcommand ()
104 "Test `eshell-command-result' with a simple subcommand."
105 (should (equal (eshell-test-command-result "{+ 1 2}") 3)))
107 (ert-deftest eshell-test/subcommand-args ()
108 "Test `eshell-command-result' with a subcommand and trailing args.
109 Test that trailing arguments outside the subcommand are ignored.
110 e.g. \"{+ 1 2} 3\" => 3"
111 (should (equal (eshell-test-command-result "{+ 1 2} 3") 3)))
113 (ert-deftest eshell-test/subcommand-lisp ()
114 "Test `eshell-command-result' with an elisp subcommand and trailing args.
115 Test that trailing arguments outside the subcommand are ignored.
116 e.g. \"{(+ 1 2)} 3\" => 3"
117 (should (equal (eshell-test-command-result "{(+ 1 2)} 3") 3)))
119 (ert-deftest eshell-test/interp-cmd ()
120 "Interpolate command result"
121 (should (equal (eshell-test-command-result "+ ${+ 1 2} 3") 6)))
123 (ert-deftest eshell-test/interp-lisp ()
124 "Interpolate Lisp form evaluation"
125 (should (equal (eshell-test-command-result "+ $(+ 1 2) 3") 6)))
127 (ert-deftest eshell-test/interp-concat ()
128 "Interpolate and concat command"
129 (should (equal (eshell-test-command-result "+ ${+ 1 2}3 3") 36)))
131 (ert-deftest eshell-test/interp-concat-lisp ()
132 "Interpolate and concat Lisp form"
133 (should (equal (eshell-test-command-result "+ $(+ 1 2)3 3") 36)))
135 (ert-deftest eshell-test/interp-concat2 ()
136 "Interpolate and concat two commands"
137 (should (equal (eshell-test-command-result "+ ${+ 1 2}${+ 1 2} 3") 36)))
139 (ert-deftest eshell-test/interp-concat-lisp2 ()
140 "Interpolate and concat two Lisp forms"
141 (should (equal (eshell-test-command-result "+ $(+ 1 2)$(+ 1 2) 3") 36)))
143 (ert-deftest eshell-test/window-height ()
144 "$LINES should equal (window-height)"
145 (should (eshell-test-command-result "= $LINES (window-height)")))
147 (ert-deftest eshell-test/window-width ()
148 "$COLUMNS should equal (window-width)"
149 (should (eshell-test-command-result "= $COLUMNS (window-width)")))
151 (ert-deftest eshell-test/last-result-var ()
152 "Test using the \"last result\" ($$) variable"
153 (with-temp-eshell
154 (eshell-command-result-p "+ 1 2; + $$ 2"
155 "3\n5\n")))
157 (ert-deftest eshell-test/last-result-var2 ()
158 "Test using the \"last result\" ($$) variable twice"
159 (with-temp-eshell
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 (eshell-command-result-p "+ 1 2; + $_ 4"
167 "3\n6\n")))
169 (ert-deftest eshell-test/escape-nonspecial ()
170 "Test that \"\\c\" and \"c\" are equivalent when \"c\" is not a
171 special character."
172 (with-temp-eshell
173 (eshell-command-result-p "echo he\\llo"
174 "hello\n")))
176 (ert-deftest eshell-test/escape-nonspecial-unicode ()
177 "Test that \"\\c\" and \"c\" are equivalent when \"c\" is a
178 unicode character (unicode characters are nonspecial by
179 definition)."
180 (with-temp-eshell
181 (eshell-command-result-p "echo Vid\\éos"
182 "Vidéos\n")))
184 (ert-deftest eshell-test/escape-nonspecial-quoted ()
185 "Test that the backslash is preserved for escaped nonspecial
186 chars"
187 (with-temp-eshell
188 (eshell-command-result-p "echo \"h\\i\""
189 ;; Backslashes are doubled for regexp.
190 "h\\\\i\n")))
192 (ert-deftest eshell-test/escape-special-quoted ()
193 "Test that the backslash is not preserved for escaped special
194 chars"
195 (with-temp-eshell
196 (eshell-command-result-p "echo \"h\\\\i\""
197 ;; Backslashes are doubled for regexp.
198 "h\\\\i\n")))
200 (ert-deftest eshell-test/command-running-p ()
201 "Modeline should show no command running"
202 (with-temp-eshell
203 (let ((eshell-status-in-mode-line t))
204 (should (memq 'eshell-command-running-string mode-line-format))
205 (should (equal eshell-command-running-string "--")))))
207 (ert-deftest eshell-test/forward-arg ()
208 "Test moving across command arguments"
209 (with-temp-eshell
210 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore)
211 (let ((here (point)) begin valid)
212 (eshell-bol)
213 (setq begin (point))
214 (eshell-forward-argument 4)
215 (setq valid (= here (point)))
216 (eshell-backward-argument 4)
217 (prog1
218 (and valid (= begin (point)))
219 (eshell-bol)
220 (delete-region (point) (point-max))))))
222 (ert-deftest eshell-test/queue-input ()
223 "Test queuing command input"
224 (with-temp-eshell
225 (eshell-insert-command "sleep 2")
226 (eshell-insert-command "echo alpha" 'eshell-queue-input)
227 (let ((count 10))
228 (while (and eshell-current-command
229 (> count 0))
230 (sit-for 1)
231 (setq count (1- count))))
232 (eshell-match-result "alpha\n")))
234 (ert-deftest eshell-test/flush-output ()
235 "Test flushing of previous output"
236 (with-temp-eshell
237 (eshell-insert-command "echo alpha")
238 (eshell-kill-output)
239 (eshell-match-result (regexp-quote "*** output flushed ***\n"))
240 (should (forward-line))
241 (should (= (point) eshell-last-output-start))))
243 (ert-deftest eshell-test/run-old-command ()
244 "Re-run an old command"
245 (with-temp-eshell
246 (eshell-insert-command "echo alpha")
247 (goto-char eshell-last-input-start)
248 (string= (eshell-get-old-input) "echo alpha")))
250 (provide 'eshell-tests)
252 ;;; tests/eshell-tests.el ends here