mirror of
git://git.sv.gnu.org/emacs.git
synced 2025-12-30 09:00:31 -08:00
`string-replace` is easier to understand, less error-prone, much faster, and results in shorter Lisp and byte code. Use it where applicable and obviously safe (erring on the conservative side). * admin/authors.el (authors-scan-change-log): * lisp/autoinsert.el (auto-insert-alist): * lisp/calc/calc-prog.el (calc-edit-macro-combine-alg-ent) (calc-edit-macro-combine-ext-command) (calc-edit-macro-combine-var-name): * lisp/calc/calc-units.el (math-make-unit-string): * lisp/calendar/cal-html.el (cal-html-comment): * lisp/calendar/cal-tex.el (cal-tex-comment): * lisp/calendar/icalendar.el (icalendar--convert-string-for-export) (icalendar--convert-string-for-import): * lisp/calendar/iso8601.el (iso8601--concat-regexps) (iso8601--full-time-match, iso8601--combined-match): * lisp/calendar/time-date.el (format-seconds): * lisp/calendar/todo-mode.el (todo-filter-items-filename): * lisp/cedet/cedet-files.el (cedet-directory-name-to-file-name) (cedet-file-name-to-directory-name): * lisp/comint.el (comint-watch-for-password-prompt): * lisp/dired-aux.el (dired-do-chmod): * lisp/dired-x.el (dired-man): * lisp/dired.el (dired-insert-directory, dired-goto-file-1): * lisp/emacs-lisp/comp.el (comp-c-func-name): * lisp/emacs-lisp/re-builder.el (reb-copy): * lisp/erc/erc-dcc.el (erc-dcc-unquote-filename): * lisp/erc/erc.el (erc-quit-reason-zippy, erc-part-reason-zippy) (erc-update-mode-line-buffer, erc-message-english-PART): * lisp/files.el (make-backup-file-name-1, files--transform-file-name) (read-file-modes): * lisp/fringe.el (fringe-mode): * lisp/gnus/gnus-art.el (gnus-button-handle-info-url): * lisp/gnus/gnus-group.el (gnus-group-completing-read): * lisp/gnus/gnus-icalendar.el (gnus-icalendar-event-from-ical): * lisp/gnus/gnus-mlspl.el (gnus-group-split-fancy): * lisp/gnus/gnus-search.el (gnus-search-query-parse-date) (gnus-search-transform-expression, gnus-search-run-search): * lisp/gnus/gnus-start.el (gnus-dribble-enter): * lisp/gnus/gnus-sum.el (gnus-summary-refer-article): * lisp/gnus/gnus-util.el (gnus-mode-string-quote): * lisp/gnus/message.el (message-put-addresses-in-ecomplete) (message-parse-mailto-url, message-mailto-1): * lisp/gnus/mml-sec.el (mml-secure-epg-sign): * lisp/gnus/mml-smime.el (mml-smime-epg-verify): * lisp/gnus/mml2015.el (mml2015-epg-verify): * lisp/gnus/nnmaildir.el (nnmaildir--system-name) (nnmaildir-request-list, nnmaildir-retrieve-groups) (nnmaildir-request-group, nnmaildir-retrieve-headers): * lisp/gnus/nnrss.el (nnrss-node-text): * lisp/gnus/spam-report.el (spam-report-gmane-internal) (spam-report-user-mail-address): * lisp/ibuffer.el (name): * lisp/image-dired.el (image-dired-pngnq-thumb) (image-dired-pngcrush-thumb, image-dired-optipng-thumb) (image-dired-create-thumb-1): * lisp/info.el (Info-set-mode-line): * lisp/international/mule-cmds.el (describe-language-environment): * lisp/mail/rfc2231.el (rfc2231-parse-string): * lisp/mail/rfc2368.el (rfc2368-parse-mailto-url): * lisp/mail/rmail.el (rmail-insert-inbox-text) (rmail-simplified-subject-regexp): * lisp/mail/rmailout.el (rmail-output-body-to-file): * lisp/mail/undigest.el (rmail-digest-rfc1153): * lisp/man.el (Man-default-man-entry): * lisp/mouse.el (minor-mode-menu-from-indicator): * lisp/mpc.el (mpc--debug): * lisp/net/browse-url.el (browse-url-mail): * lisp/net/eww.el (eww-update-header-line-format): * lisp/net/newst-backend.el (newsticker-save-item): * lisp/net/rcirc.el (rcirc-sentinel): * lisp/net/soap-client.el (soap-decode-date-time): * lisp/nxml/rng-cmpct.el (rng-c-literal-2-re): * lisp/nxml/xmltok.el (let*): * lisp/obsolete/nnir.el (nnir-run-swish-e, nnir-run-hyrex) (nnir-run-find-grep): * lisp/play/dunnet.el (dun-doassign): * lisp/play/handwrite.el (handwrite): * lisp/proced.el (proced-format-args): * lisp/profiler.el (profiler-report-header-line-format): * lisp/progmodes/gdb-mi.el (gdb-mi-quote): * lisp/progmodes/make-mode.el (makefile-bsdmake-rule-action-regex) (makefile-make-font-lock-keywords): * lisp/progmodes/prolog.el (prolog-guess-fill-prefix): * lisp/progmodes/ruby-mode.el (ruby-toggle-string-quotes): * lisp/progmodes/sql.el (sql-remove-tabs-filter, sql-str-literal): * lisp/progmodes/which-func.el (which-func-current): * lisp/replace.el (query-replace-read-from) (occur-engine, replace-quote): * lisp/select.el (xselect--encode-string): * lisp/ses.el (ses-export-tab): * lisp/subr.el (shell-quote-argument): * lisp/term/pc-win.el (msdos-show-help): * lisp/term/w32-win.el (w32--set-selection): * lisp/term/xterm.el (gui-backend-set-selection): * lisp/textmodes/picture.el (picture-tab-search): * lisp/thumbs.el (thumbs-call-setroot-command): * lisp/tooltip.el (tooltip-show-help-non-mode): * lisp/transient.el (transient-format-key): * lisp/url/url-mailto.el (url-mailto): * lisp/vc/log-edit.el (log-edit-changelog-ours-p): * lisp/vc/vc-bzr.el (vc-bzr-status): * lisp/vc/vc-hg.el (vc-hg--glob-to-pcre): * lisp/vc/vc-svn.el (vc-svn-after-dir-status): * lisp/xdg.el (xdg-desktop-strings): * test/lisp/electric-tests.el (defun): * test/lisp/term-tests.el (term-simple-lines): * test/lisp/time-stamp-tests.el (formatz-mod-del-colons): * test/lisp/wdired-tests.el (wdired-test-bug32173-01) (wdired-test-unfinished-edit-01): * test/src/json-tests.el (json-parse-with-custom-null-and-false-objects): Use `string-replace` instead of `replace-regexp-in-string`.
329 lines
11 KiB
EmacsLisp
329 lines
11 KiB
EmacsLisp
;;; term-tests.el --- tests for term.el -*- lexical-binding: t -*-
|
||
|
||
;; Copyright (C) 2017, 2019-2021 Free Software Foundation, Inc.
|
||
|
||
;; This file is part of GNU Emacs.
|
||
|
||
;; GNU Emacs is free software: you can redistribute it and/or modify
|
||
;; it under the terms of the GNU General Public License as published by
|
||
;; the Free Software Foundation, either version 3 of the License, or
|
||
;; (at your option) any later version.
|
||
|
||
;; GNU Emacs is distributed in the hope that it will be useful,
|
||
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
;; GNU General Public License for more details.
|
||
|
||
;; You should have received a copy of the GNU General Public License
|
||
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
||
|
||
;;; Commentary:
|
||
|
||
|
||
;;; Code:
|
||
(require 'ert)
|
||
(require 'term)
|
||
(eval-when-compile (require 'cl-lib))
|
||
|
||
(defvar term-height) ; Number of lines in window.
|
||
(defvar term-width) ; Number of columns in window.
|
||
|
||
(defun term-test-screen-from-input (width height input &optional return-var)
|
||
(with-temp-buffer
|
||
(term-mode)
|
||
;; Keep dimensions independent from window size.
|
||
(remove-function (local 'window-adjust-process-window-size-function)
|
||
'term-maybe-reset-size)
|
||
(term-exec (current-buffer) "test" "cat" nil nil)
|
||
(term-char-mode)
|
||
(setq term-width width)
|
||
(setq term-height height)
|
||
;; Pass input directly to `term-emulate-terminal', it's easier to
|
||
;; control chunking, and we don't have to worry about wrestling
|
||
;; with stty settings.
|
||
(let ((proc (get-buffer-process (current-buffer))))
|
||
;; Don't get stuck when we close the buffer.
|
||
(set-process-query-on-exit-flag proc nil)
|
||
(if (consp input)
|
||
(mapc (lambda (input) (term-emulate-terminal proc input)) input)
|
||
(term-emulate-terminal proc input))
|
||
(if return-var (buffer-local-value return-var (current-buffer))
|
||
(buffer-substring-no-properties (point-min) (point-max))))))
|
||
|
||
(ert-deftest term-simple-lines ()
|
||
(skip-unless (not (memq system-type '(windows-nt ms-dos))))
|
||
(let ((str "\
|
||
first line\r
|
||
next line\r\n"))
|
||
(should (equal (term-test-screen-from-input 40 12 str)
|
||
(string-replace "\r" "" str)))))
|
||
|
||
(ert-deftest term-carriage-return ()
|
||
(skip-unless (not (memq system-type '(windows-nt ms-dos))))
|
||
(let ((str "\
|
||
first line\r_next line\r\n"))
|
||
(should (equal (term-test-screen-from-input 40 12 str)
|
||
"_next line\n"))))
|
||
|
||
(ert-deftest term-line-wrap ()
|
||
(skip-unless (not (memq system-type '(windows-nt ms-dos))))
|
||
(should (string-match-p
|
||
;; Don't be strict about trailing whitespace.
|
||
"\\`a\\{40\\}\na\\{20\\} *\\'"
|
||
(term-test-screen-from-input 40 12 (make-string 60 ?a))))
|
||
;; Again, but split input into chunks.
|
||
(should (string-match-p
|
||
"\\`a\\{40\\}\na\\{20\\} *\\'"
|
||
(term-test-screen-from-input 40 12 (let ((str (make-string 30 ?a)))
|
||
(list str str))))))
|
||
|
||
(ert-deftest term-cursor-movement ()
|
||
(skip-unless (not (memq system-type '(windows-nt ms-dos))))
|
||
;; Absolute positioning.
|
||
(should (equal "ab\ncd"
|
||
(term-test-screen-from-input
|
||
40 12 (concat "\e[2;2Hd"
|
||
"\e[2;1Hc"
|
||
"\e[1;2Hb"
|
||
"\e[1;1Ha"))))
|
||
;; Send one byte at a time.
|
||
(should (equal "ab\ncd"
|
||
(term-test-screen-from-input
|
||
40 12 (split-string (concat "\e[2;2Hd"
|
||
"\e[2;1Hc"
|
||
"\e[1;2Hb"
|
||
"\e[1;1Ha") "" t))))
|
||
(should (equal "abcde j"
|
||
(term-test-screen-from-input
|
||
10 12 '("abcdefghij"
|
||
"\e[H" ;move back to point-min
|
||
"abcde"
|
||
" j"))))
|
||
|
||
;; Relative positioning.
|
||
(should (equal "ab\ncd"
|
||
(term-test-screen-from-input
|
||
40 12 (concat "\e[B\e[Cd"
|
||
"\e[D\e[Dc"
|
||
"\e[Ab"
|
||
"\e[D\e[Da")))))
|
||
|
||
(ert-deftest term-scrolling-region ()
|
||
(skip-unless (not (memq system-type '(windows-nt ms-dos))))
|
||
(should (equal "\
|
||
line3
|
||
line4
|
||
line5
|
||
line6
|
||
"
|
||
(term-test-screen-from-input
|
||
40 12 "\e[1;5r\
|
||
line1\r
|
||
line2\r
|
||
line3\r
|
||
line4\r
|
||
line5\r
|
||
line6\r
|
||
")))
|
||
|
||
;; test reverse scrolling
|
||
(should (equal "line1
|
||
line7
|
||
line6
|
||
line2
|
||
line5"
|
||
(term-test-screen-from-input 40 5
|
||
'("\e[0;0H"
|
||
"\e[J"
|
||
"line1\r
|
||
line2\r
|
||
line3\r
|
||
line4\r
|
||
line5"
|
||
"\e[2;4r"
|
||
"\e[2;0H"
|
||
"\e[2;0H"
|
||
"\eMline6"
|
||
"\e[2;0H"
|
||
"\eMline7"))))
|
||
|
||
;; test scrolling down
|
||
(should (equal "line1
|
||
line3
|
||
line4
|
||
line7
|
||
line5"
|
||
(term-test-screen-from-input 40 5
|
||
'("\e[0;0H"
|
||
"\e[J"
|
||
"line1\r
|
||
line2\r
|
||
line3\r
|
||
line4\r
|
||
line5"
|
||
"\e[2;4r"
|
||
"\e[2;0H"
|
||
"\e[4;5H"
|
||
"\n\rline7"))))
|
||
|
||
;; setting the scroll region end beyond the max height should not
|
||
;; turn on term-scroll-with-delete
|
||
(should (equal "line1
|
||
line2
|
||
line3
|
||
line4
|
||
line5
|
||
line6
|
||
line7"
|
||
(term-test-screen-from-input 40 5
|
||
'("\e[1;10r"
|
||
"line1\r
|
||
line2\r
|
||
line3\r
|
||
line4\r
|
||
line5\r
|
||
line6\r
|
||
line7"))))
|
||
|
||
|
||
;; resetting the terminal should set the scroll region end to (1- term-height).
|
||
(should (equal "
|
||
line1
|
||
line2
|
||
line3
|
||
line4
|
||
"
|
||
(term-test-screen-from-input 40 5
|
||
'("\e[1;10r"
|
||
"\ec" ;reset
|
||
"line1\r
|
||
line2\r
|
||
line3\r
|
||
line4\r
|
||
line5"
|
||
"\e[1;1H"
|
||
"\e[L"))))
|
||
|
||
;; scroll region should be limited to the (1- term-height). Note,
|
||
;; this fixes an off by one error when comparing the scroll region
|
||
;; end with term-height.
|
||
(should (equal "
|
||
line1
|
||
line2
|
||
line3
|
||
line4
|
||
"
|
||
(term-test-screen-from-input 40 5
|
||
'("\e[1;6r"
|
||
"line1\r
|
||
line2\r
|
||
line3\r
|
||
line4\r
|
||
line5"
|
||
"\e[1;1H" ;go back to home
|
||
"\e[L" ;insert a new line at the top
|
||
))))
|
||
|
||
;; setting the scroll region to the entire height should not turn on
|
||
;; term-scroll-with-delete
|
||
(should (equal "line1
|
||
line2
|
||
line3
|
||
line4
|
||
line5
|
||
line6"
|
||
(term-test-screen-from-input 40 5
|
||
'("\e[1;5r"
|
||
"line1\r
|
||
line2\r
|
||
line3\r
|
||
line4\r
|
||
line5\r
|
||
line6"))))
|
||
|
||
;; reset should reset term-scroll-with-delete
|
||
(should (equal "line1
|
||
line2
|
||
line3
|
||
line4
|
||
line5
|
||
line6
|
||
line7"
|
||
(term-test-screen-from-input 40 5
|
||
'("\e[2;5r" ;set the region
|
||
"\ec" ;reset
|
||
"line1\r
|
||
line2\r
|
||
line3\r
|
||
line4\r
|
||
line5\r
|
||
line6\r
|
||
line7")))))
|
||
|
||
(ert-deftest term-set-directory ()
|
||
(skip-unless (not (memq system-type '(windows-nt ms-dos))))
|
||
(let ((term-ansi-at-user (user-real-login-name)))
|
||
(should (equal (term-test-screen-from-input
|
||
40 12 "\eAnSiTc /foo/\n" 'default-directory)
|
||
"/foo/"))
|
||
;; Split input (Bug#17231).
|
||
(should (equal (term-test-screen-from-input
|
||
40 12 (list "\eAnSiTc /f" "oo/\n") 'default-directory)
|
||
"/foo/"))))
|
||
|
||
(ert-deftest term-line-wrapping-then-motion ()
|
||
"Make sure we reset the line-wrapping state after moving cursor.
|
||
A real-life example is the default zsh prompt which writes spaces
|
||
to the end of line (triggering line-wrapping state), and then
|
||
sends a carriage return followed by another space to overwrite
|
||
the first character of the line."
|
||
(skip-unless (not (memq system-type '(windows-nt ms-dos))))
|
||
(let* ((width 10)
|
||
(strs (list "x" (make-string (1- width) ?_)
|
||
"\r_")))
|
||
(should (equal (term-test-screen-from-input width 12 strs)
|
||
(make-string width ?_)))))
|
||
|
||
(ert-deftest term-to-margin ()
|
||
"Test cursor movement at the scroll margin.
|
||
This is a reduced example from GNU nano's initial screen."
|
||
(skip-unless (not (memq system-type '(windows-nt ms-dos))))
|
||
(let* ((width 10)
|
||
(x (make-string width ?x))
|
||
(y (make-string width ?y)))
|
||
(should (equal (term-test-screen-from-input
|
||
width 3
|
||
`("\e[1;3r" ; Setup 3 line scrolling region.
|
||
"\e[2;1H" ; Move to 2nd last line.
|
||
,x ; Fill with 'x'.
|
||
"\r\e[1B" ; Next line.
|
||
,y)) ; Fill with 'y'.
|
||
(concat "\n" x "\n" y)))
|
||
;; Same idea, but moving upwards.
|
||
(should (equal (term-test-screen-from-input
|
||
width 3
|
||
`("\e[1;3r" "\e[2;1H" ,x "\r\e[1A" ,y))
|
||
(concat y "\n" x)))))
|
||
|
||
(ert-deftest term-decode-partial () ;; Bug#25288.
|
||
"Test multibyte characters sent into multiple chunks."
|
||
;; Set `locale-coding-system' so test will be deterministic.
|
||
(let* ((locale-coding-system 'utf-8-unix)
|
||
(string (make-string 7 ?ш))
|
||
(bytes (encode-coding-string string locale-coding-system)))
|
||
(should (equal string
|
||
(term-test-screen-from-input
|
||
40 1 `(,(substring bytes 0 (/ (length bytes) 2))
|
||
,(substring bytes (/ (length bytes) 2))))))))
|
||
(ert-deftest term-undecodable-input () ;; Bug#29918.
|
||
"Undecodable bytes should be passed through without error."
|
||
(let* ((locale-coding-system 'utf-8-unix) ; As above.
|
||
(bytes "\376\340\360\370")
|
||
(string (decode-coding-string bytes locale-coding-system)))
|
||
(should (equal string
|
||
(term-test-screen-from-input
|
||
40 1 bytes)))))
|
||
|
||
(provide 'term-tests)
|
||
|
||
;;; term-tests.el ends here
|