* doc/lispintro/emacs-lisp-intro.texi (zap-to-char): Remove obsolete stuff.
[emacs.git] / test / eshell.el
blob126a47220c4515649d32023f5c350c1aeab97822
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-buffer (eshell t)))
34 (unwind-protect
35 (with-current-buffer eshell-buffer
36 ,@body)
37 (kill-buffer eshell-buffer))))
39 (defun eshell-insert-command (text &optional func)
40 "Insert a command at the end of the buffer."
41 (goto-char eshell-last-output-end)
42 (insert-and-inherit text)
43 (funcall (or func 'eshell-send-input)))
45 (defun eshell-match-result (regexp)
46 "Insert a command at the end of the buffer."
47 (goto-char eshell-last-input-end)
48 (looking-at regexp))
50 (defun eshell-command-result-p (text regexp &optional func)
51 "Insert a command at the end of the buffer."
52 (eshell-insert-command text func)
53 (eshell-match-result regexp))
55 ;;; Tests:
57 (ert-deftest eshell-test/simple-command-result ()
58 "Test `eshell-command-result' with a simple command."
59 (should (equal (eshell-command-result "+ 1 2") 3)))
61 (ert-deftest eshell-test/lisp-command ()
62 "Test `eshell-command-result' with an elisp command."
63 (should (equal (eshell-command-result "(+ 1 2)") 3)))
65 (ert-deftest eshell-test/lisp-command-args ()
66 "Test `eshell-command-result' with elisp and trailing args.
67 Test that trailing arguments outside the S-expression are
68 ignored. e.g. \"(+ 1 2) 3\" => 3"
69 (should (equal (eshell-command-result "(+ 1 2) 3") 3)))
71 (ert-deftest eshell-test/subcommand ()
72 "Test `eshell-command-result' with a simple subcommand."
73 (should (equal (eshell-command-result "{+ 1 2}") 3)))
75 (ert-deftest eshell-test/subcommand-args ()
76 "Test `eshell-command-result' with a subcommand and trailing args.
77 Test that trailing arguments outside the subcommand are ignored.
78 e.g. \"{+ 1 2} 3\" => 3"
79 (should (equal (eshell-command-result "{+ 1 2} 3") 3)))
81 (ert-deftest eshell-test/subcommand-lisp ()
82 "Test `eshell-command-result' with an elisp subcommand and trailing args.
83 Test that trailing arguments outside the subcommand are ignored.
84 e.g. \"{(+ 1 2)} 3\" => 3"
85 (should (equal (eshell-command-result "{(+ 1 2)} 3") 3)))
87 (ert-deftest eshell-test/interp-cmd ()
88 "Interpolate command result"
89 (should (equal (eshell-command-result "+ ${+ 1 2} 3") 6)))
91 (ert-deftest eshell-test/interp-lisp ()
92 "Interpolate Lisp form evaluation"
93 (should (equal (eshell-command-result "+ $(+ 1 2) 3") 6)))
95 (ert-deftest eshell-test/interp-concat ()
96 "Interpolate and concat command"
97 (should (equal (eshell-command-result "+ ${+ 1 2}3 3") 36)))
99 (ert-deftest eshell-test/interp-concat-lisp ()
100 "Interpolate and concat Lisp form"
101 (should (equal (eshell-command-result "+ $(+ 1 2)3 3") 36)))
103 (ert-deftest eshell-test/interp-concat2 ()
104 "Interpolate and concat two commands"
105 (should (equal (eshell-command-result "+ ${+ 1 2}${+ 1 2} 3") 36)))
107 (ert-deftest eshell-test/interp-concat-lisp2 ()
108 "Interpolate and concat two Lisp forms"
109 (should (equal (eshell-command-result "+ $(+ 1 2)$(+ 1 2) 3") 36)))
111 (ert-deftest eshell-test/window-height ()
112 "$LINES should equal (window-height)"
113 (should (eshell-command-result "= $LINES (window-height)")))
115 (ert-deftest eshell-test/window-width ()
116 "$COLUMNS should equal (window-width)"
117 (should (eshell-command-result "= $COLUMNS (window-width)")))
119 (ert-deftest eshell-test/last-result-var ()
120 "Test using the \"last result\" ($$) variable"
121 (with-temp-eshell
122 (should
123 (eshell-command-result-p "+ 1 2; + $$ 2"
124 "3\n5\n"))))
126 (ert-deftest eshell-test/last-result-var2 ()
127 "Test using the \"last result\" ($$) variable twice"
128 (with-temp-eshell
129 (should
130 (eshell-command-result-p "+ 1 2; + $$ $$"
131 "3\n6\n"))))
133 (ert-deftest eshell-test/last-arg-var ()
134 "Test using the \"last arg\" ($_) variable"
135 (with-temp-eshell
136 (should
137 (eshell-command-result-p "+ 1 2; + $_ 4"
138 "3\n6\n"))))
140 (ert-deftest eshell-test/command-running-p ()
141 "Modeline should show no command running"
142 (with-temp-eshell
143 (let ((eshell-status-in-mode-line t))
144 (should (memq 'eshell-command-running-string mode-line-format))
145 (should (equal eshell-command-running-string "--")))))
147 (ert-deftest eshell-test/forward-arg ()
148 "Test moving across command arguments"
149 (with-temp-eshell
150 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore)
151 (let ((here (point)) begin valid)
152 (eshell-bol)
153 (setq begin (point))
154 (eshell-forward-argument 4)
155 (setq valid (= here (point)))
156 (eshell-backward-argument 4)
157 (prog1
158 (and valid (= begin (point)))
159 (eshell-bol)
160 (delete-region (point) (point-max))))))
162 (ert-deftest eshell-test/queue-input ()
163 "Test queuing command input"
164 (with-temp-eshell
165 (eshell-insert-command "sleep 2")
166 (eshell-insert-command "echo alpha" 'eshell-queue-input)
167 (let ((count 10))
168 (while (and eshell-current-command
169 (> count 0))
170 (sit-for 1)
171 (setq count (1- count))))
172 (should (eshell-match-result "alpha\n"))))
174 (ert-deftest eshell-test/flush-output ()
175 "Test flushing of previous output"
176 (with-temp-eshell
177 (eshell-insert-command "echo alpha")
178 (eshell-kill-output)
179 (should (eshell-match-result (regexp-quote "*** output flushed ***\n")))
180 (should (forward-line))
181 (should (= (point) eshell-last-output-start))))
183 (ert-deftest eshell-test/run-old-command ()
184 "Re-run an old command"
185 (with-temp-eshell
186 (eshell-insert-command "echo alpha")
187 (goto-char eshell-last-input-start)
188 (string= (eshell-get-old-input) "echo alpha")))
190 (provide 'esh-test)
192 ;;; tests/eshell.el ends here