; Avoid process kill query during term tests
[emacs.git] / test / lisp / term-tests.el
blob234dfa1f0d519c20d38f1f98df0baa24999dd3fe
1 ;;; term-tests.el --- tests for term.el -*- lexical-binding: t -*-
3 ;; Copyright (C) 2017 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
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 3 of the License, or
10 ;; (at your option) any later version.
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.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
20 ;;; Commentary:
23 ;;; Code:
24 (require 'ert)
25 (require 'term)
26 (eval-when-compile (require 'cl-lib))
28 (defvar term-height) ; Number of lines in window.
29 (defvar term-width) ; Number of columns in window.
31 (defun term-test-screen-from-input (width height input &optional return-var)
32 (with-temp-buffer
33 (term-mode)
34 ;; Keep dimensions independent from window size.
35 (remove-function (local 'window-adjust-process-window-size-function)
36 'term-maybe-reset-size)
37 (term-exec (current-buffer) "test" "cat" nil nil)
38 (term-char-mode)
39 (setq term-width width)
40 (setq term-height height)
41 ;; Pass input directly to `term-emulate-terminal', it's easier to
42 ;; control chunking, and we don't have to worry about wrestling
43 ;; with stty settings.
44 (let ((proc (get-buffer-process (current-buffer))))
45 ;; Don't get stuck when we close the buffer.
46 (set-process-query-on-exit-flag proc nil)
47 (if (consp input)
48 (mapc (lambda (input) (term-emulate-terminal proc input)) input)
49 (term-emulate-terminal proc input))
50 (if return-var (buffer-local-value return-var (current-buffer))
51 (buffer-substring-no-properties (point-min) (point-max))))))
53 (ert-deftest term-simple-lines ()
54 (let ((str "\
55 first line\r
56 next line\r\n"))
57 (should (equal (term-test-screen-from-input 40 12 str)
58 (replace-regexp-in-string "\r" "" str)))))
60 (ert-deftest term-carriage-return ()
61 (let ((str "\
62 first line\r_next line\r\n"))
63 (should (equal (term-test-screen-from-input 40 12 str)
64 "_next line\n"))))
66 (ert-deftest term-line-wrap ()
67 (should (string-match-p
68 ;; Don't be strict about trailing whitespace.
69 "\\`a\\{40\\}\na\\{20\\} *\\'"
70 (term-test-screen-from-input 40 12 (make-string 60 ?a))))
71 ;; Again, but split input into chunks.
72 (should (string-match-p
73 "\\`a\\{40\\}\na\\{20\\} *\\'"
74 (term-test-screen-from-input 40 12 (let ((str (make-string 30 ?a)))
75 (list str str))))))
77 (ert-deftest term-cursor-movement ()
78 ;; Absolute positioning.
79 (should (equal "ab\ncd"
80 (term-test-screen-from-input
81 40 12 (concat "\e[2;2Hd"
82 "\e[2;1Hc"
83 "\e[1;2Hb"
84 "\e[1;1Ha"))))
85 ;; Send one byte at a time.
86 (should (equal "ab\ncd"
87 (term-test-screen-from-input
88 40 12 (split-string (concat "\e[2;2Hd"
89 "\e[2;1Hc"
90 "\e[1;2Hb"
91 "\e[1;1Ha") "" t))))
92 ;; Relative positioning.
93 (should (equal "ab\ncd"
94 (term-test-screen-from-input
95 40 12 (concat "\e[B\e[Cd"
96 "\e[D\e[Dc"
97 "\e[Ab"
98 "\e[D\e[Da")))))
100 (ert-deftest term-scrolling-region ()
101 (should (equal "\
102 line3
103 line4
104 line5
105 line6
107 (term-test-screen-from-input
108 40 12 "\e[1;5r\
109 line1\r
110 line2\r
111 line3\r
112 line4\r
113 line5\r
114 line6\r
115 "))))
117 (ert-deftest term-set-directory ()
118 (let ((term-ansi-at-user (user-real-login-name)))
119 (should (equal (term-test-screen-from-input
120 40 12 "\eAnSiTc /foo/\n" 'default-directory)
121 "/foo/"))
122 ;; Split input (Bug#17231).
123 (should (equal (term-test-screen-from-input
124 40 12 (list "\eAnSiTc /f" "oo/\n") 'default-directory)
125 "/foo/"))))
127 (provide 'term-tests)
129 ;;; term-tests.el ends here