2020-05-01 13:02:38 +02:00
|
|
|
;;; dabbrev-tests.el --- Test suite for dabbrev. -*- lexical-binding:t -*-
|
2016-03-02 17:21:24 +00:00
|
|
|
|
2024-01-02 09:47:10 +08:00
|
|
|
;; Copyright (C) 2016-2024 Free Software Foundation, Inc.
|
2016-03-02 17:21:24 +00:00
|
|
|
|
|
|
|
;; Author: Alan Third <alan@idiocy.org>
|
|
|
|
;; Keywords: dabbrev
|
|
|
|
|
|
|
|
;; 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
|
2017-09-13 15:52:52 -07:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2016-03-02 17:21:24 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'ert)
|
Fix bugs in dabbrev-expand (bug#74090)
* lisp/dabbrev.el (dabbrev-expand): Use the buffer where the
expansion was found when setting the internal variables used to
determine the next expansion or a replacement expansion.
* test/lisp/dabbrev-tests.el (ert-x): Require for
'ert-with-temp-directory', 'ert-resource-directory' and
'ert-resource-file'.
(with-dabbrev-test): New macro.
(dabbrev-expand-test-same-buffer-{1,2,3,4})
(dabbrev-expand-test-other-buffer-{1,2,3,4})
(dabbrev-expand-test-minibuffer-{1,2,3,4}): New tests.
* test/lisp/dabbrev-resources/dabbrev-expand.el:
* test/lisp/dabbrev-resources/INSTALL_BEGIN: New test resources.
(cherry picked from commit f6c359cb66a0e9b851e3467b1ba9cab7efa8f744)
2024-10-31 10:46:27 +01:00
|
|
|
(require 'ert-x)
|
2016-03-02 17:21:24 +00:00
|
|
|
(require 'dabbrev)
|
|
|
|
|
|
|
|
(ert-deftest dabbrev-expand-test ()
|
|
|
|
"Test for bug#1948.
|
2021-10-21 20:22:15 +03:00
|
|
|
When `dabbrev-eliminate-newlines' is non-nil (the default),
|
|
|
|
repeated calls to `dabbrev-expand' can result in the source of
|
2016-03-02 17:21:24 +00:00
|
|
|
first expansion being replaced rather than the destination."
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert "ab x\na\nab y")
|
|
|
|
(goto-char 8)
|
|
|
|
(save-window-excursion
|
|
|
|
(set-window-buffer nil (current-buffer))
|
2021-10-21 20:22:15 +03:00
|
|
|
(execute-kbd-macro (kbd "M-/ SPC M-/ M-/")))
|
2016-03-02 17:21:24 +00:00
|
|
|
(should (string= (buffer-string) "ab x\nab y\nab y"))))
|
2019-12-05 13:14:00 +00:00
|
|
|
|
|
|
|
(ert-deftest dabbrev-completion-test ()
|
|
|
|
"Test for bug#17899.
|
|
|
|
dabbrev-completion should not look for expansions in other
|
|
|
|
buffers unless a prefix argument is used."
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert "axy")
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert "abc\na")
|
|
|
|
(goto-char 6)
|
|
|
|
(save-window-excursion
|
|
|
|
(set-window-buffer nil (current-buffer))
|
2021-10-21 20:22:15 +03:00
|
|
|
(execute-kbd-macro (kbd "C-M-/")))
|
2019-12-05 13:14:00 +00:00
|
|
|
(should (string= (buffer-string) "abc\nabc")))))
|
|
|
|
|
|
|
|
(ert-deftest dabbrev-completion-test-with-argument ()
|
|
|
|
"Test for bug#17899.
|
|
|
|
dabbrev-completion should not complete because it has found
|
|
|
|
multiple expansions."
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert "axy")
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert "abc\na")
|
|
|
|
(goto-char 6)
|
|
|
|
(save-window-excursion
|
|
|
|
(set-window-buffer nil (current-buffer))
|
2021-10-21 20:22:15 +03:00
|
|
|
(execute-kbd-macro (kbd "C-u C-u C-M-/")))
|
2019-12-05 13:14:00 +00:00
|
|
|
(should (string= (buffer-string) "abc\na")))))
|
2021-09-26 01:53:56 +02:00
|
|
|
|
Fix bugs in dabbrev-expand (bug#74090)
* lisp/dabbrev.el (dabbrev-expand): Use the buffer where the
expansion was found when setting the internal variables used to
determine the next expansion or a replacement expansion.
* test/lisp/dabbrev-tests.el (ert-x): Require for
'ert-with-temp-directory', 'ert-resource-directory' and
'ert-resource-file'.
(with-dabbrev-test): New macro.
(dabbrev-expand-test-same-buffer-{1,2,3,4})
(dabbrev-expand-test-other-buffer-{1,2,3,4})
(dabbrev-expand-test-minibuffer-{1,2,3,4}): New tests.
* test/lisp/dabbrev-resources/dabbrev-expand.el:
* test/lisp/dabbrev-resources/INSTALL_BEGIN: New test resources.
(cherry picked from commit f6c359cb66a0e9b851e3467b1ba9cab7efa8f744)
2024-10-31 10:46:27 +01:00
|
|
|
(defmacro with-dabbrev-test (&rest body)
|
|
|
|
"Set up an isolated `dabbrev' test environment."
|
|
|
|
(declare (debug (body)))
|
|
|
|
`(ert-with-temp-directory dabbrev-test-home
|
|
|
|
(let* (;; Since we change HOME, clear this to avoid a conflict
|
|
|
|
;; e.g. if Emacs runs within the user's home directory.
|
|
|
|
(abbreviated-home-dir nil)
|
|
|
|
(process-environment (cons (format "HOME=%s" dabbrev-test-home)
|
|
|
|
process-environment))
|
|
|
|
(dabbrev-directory (ert-resource-directory)))
|
|
|
|
(unwind-protect
|
|
|
|
(progn ,@body)
|
|
|
|
;; Restore pre-test-run state of test files.
|
|
|
|
(dolist (f (directory-files dabbrev-directory))
|
|
|
|
(let ((buf (get-file-buffer f)))
|
|
|
|
(when buf
|
|
|
|
(with-current-buffer buf
|
|
|
|
(restore-buffer-modified-p nil)
|
|
|
|
(kill-buffer)))))
|
|
|
|
(dabbrev--reset-global-variables)))))
|
|
|
|
|
|
|
|
(ert-deftest dabbrev-expand-test-same-buffer-1 ()
|
|
|
|
"Test expanding a string twice within a single buffer.
|
|
|
|
The first expansion should expand the input (a prefix-string) to a
|
|
|
|
string in the buffer containing no whitespace character, the second
|
|
|
|
expansion, after adding a space to the first expansion, should extend
|
|
|
|
the string with the following string in the buffer up to the next
|
|
|
|
whitespace character."
|
|
|
|
(with-dabbrev-test
|
|
|
|
(find-file (ert-resource-file "INSTALL_BEGIN"))
|
|
|
|
(goto-char (point-max))
|
|
|
|
(terpri)
|
|
|
|
(execute-kbd-macro (kbd "Ind M-/"))
|
|
|
|
(should (string= (buffer-substring (pos-bol) (pos-eol)) "Indic"))
|
|
|
|
(execute-kbd-macro (kbd "SPC M-/"))
|
|
|
|
(should (string= (buffer-substring (pos-bol) (pos-eol)) "Indic and"))))
|
|
|
|
|
|
|
|
(ert-deftest dabbrev-expand-test-same-buffer-2 ()
|
|
|
|
"Test expanding a string plus space twice within a single buffer.
|
|
|
|
Each expansion should extend the string with the following string in the
|
|
|
|
buffer up to the next whitespace character."
|
|
|
|
(with-dabbrev-test
|
|
|
|
(find-file (ert-resource-file "INSTALL_BEGIN"))
|
|
|
|
(goto-char (point-max))
|
|
|
|
(terpri)
|
|
|
|
(execute-kbd-macro (kbd "Indic SPC M-/"))
|
|
|
|
(should (string= (buffer-substring (pos-bol) (pos-eol)) "Indic and"))
|
|
|
|
(execute-kbd-macro (kbd "SPC M-/"))
|
|
|
|
(should (string= (buffer-substring (pos-bol) (pos-eol)) "Indic and Khmer"))))
|
|
|
|
|
|
|
|
(ert-deftest dabbrev-expand-test-same-buffer-3 ()
|
|
|
|
"Test replacing an expansion within a single buffer."
|
|
|
|
(with-dabbrev-test
|
|
|
|
(find-file (ert-resource-file "INSTALL_BEGIN"))
|
|
|
|
(goto-char (point-max))
|
|
|
|
(terpri)
|
|
|
|
(insert-file-contents (ert-resource-file "dabbrev-expand.el"))
|
|
|
|
(goto-char (point-max))
|
|
|
|
(terpri)
|
|
|
|
(execute-kbd-macro (kbd "Ind M-/"))
|
|
|
|
(should (string= (buffer-substring (pos-bol) (pos-eol)) "Indicate"))
|
|
|
|
(kill-whole-line)
|
|
|
|
(execute-kbd-macro (kbd "Ind M-/ M-/"))
|
|
|
|
(should (string= (buffer-substring (pos-bol) (pos-eol)) "Indic"))
|
|
|
|
(execute-kbd-macro (kbd "SPC M-/"))
|
|
|
|
(should (string= (buffer-substring (pos-bol) (pos-eol)) "Indic and"))))
|
|
|
|
|
|
|
|
(ert-deftest dabbrev-expand-test-same-buffer-4 ()
|
|
|
|
"Test expanding a string in a narrowed-region."
|
|
|
|
(with-dabbrev-test
|
|
|
|
(let (disabled-command-function) ; Enable narrow-to-region.
|
|
|
|
(find-file (ert-resource-file "INSTALL_BEGIN"))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(execute-kbd-macro (kbd "C-s Ind M-a C-SPC M-} C-x n n"))
|
|
|
|
(goto-char (point-max))
|
|
|
|
(terpri)
|
|
|
|
(execute-kbd-macro (kbd "Ind M-/"))
|
|
|
|
(should (string= (buffer-substring (pos-bol) (pos-eol)) "Indic"))
|
|
|
|
(execute-kbd-macro (kbd "SPC M-/"))
|
|
|
|
(should (string= (buffer-substring (pos-bol) (pos-eol)) "Indic and")))))
|
|
|
|
|
|
|
|
(ert-deftest dabbrev-expand-test-other-buffer-1 ()
|
|
|
|
"Test expanding a prefix string to a string from another buffer."
|
|
|
|
(with-dabbrev-test
|
|
|
|
(find-file (ert-resource-file "INSTALL_BEGIN"))
|
|
|
|
(switch-to-buffer (get-buffer-create "a" t))
|
|
|
|
(execute-kbd-macro (kbd "Ind M-/"))
|
|
|
|
(should (string= (buffer-string) "Indic"))
|
|
|
|
(execute-kbd-macro (kbd "SPC M-/"))
|
|
|
|
(should (string= (buffer-string) "Indic and"))
|
|
|
|
(kill-buffer "a")))
|
|
|
|
|
|
|
|
(ert-deftest dabbrev-expand-test-other-buffer-2 ()
|
|
|
|
"Test expanding a string + space to a string from another buffer."
|
|
|
|
(with-dabbrev-test
|
|
|
|
(find-file (ert-resource-file "INSTALL_BEGIN"))
|
|
|
|
(switch-to-buffer (get-buffer-create "a" t))
|
|
|
|
(execute-kbd-macro (kbd "Indic SPC M-/"))
|
|
|
|
(should (string= (buffer-string) "Indic and"))
|
|
|
|
(execute-kbd-macro (kbd "SPC M-/"))
|
|
|
|
(should (string= (buffer-string) "Indic and Khmer"))
|
|
|
|
(kill-buffer "a")))
|
|
|
|
|
|
|
|
(ert-deftest dabbrev-expand-test-other-buffer-3 ()
|
|
|
|
"Test replacing an expansion with three different buffers.
|
|
|
|
A prefix string in a buffer should find the first expansion in a
|
|
|
|
different buffer and then find a replacement expansion is yet another
|
|
|
|
buffer."
|
|
|
|
(with-dabbrev-test
|
|
|
|
(find-file (ert-resource-file "INSTALL_BEGIN"))
|
|
|
|
(find-file (ert-resource-file "dabbrev-expand.el"))
|
|
|
|
(switch-to-buffer (get-buffer-create "a" t))
|
|
|
|
(emacs-lisp-mode)
|
|
|
|
(execute-kbd-macro (kbd "Ind M-/"))
|
|
|
|
(should (string= (buffer-string) "Indicate"))
|
|
|
|
(erase-buffer)
|
|
|
|
(execute-kbd-macro (kbd "Ind M-/ M-/"))
|
|
|
|
(should (string= (buffer-string) "Indic"))
|
|
|
|
(execute-kbd-macro (kbd "SPC M-/"))
|
|
|
|
(should (string= (buffer-string) "Indic and"))
|
|
|
|
(kill-buffer "a")))
|
|
|
|
|
|
|
|
(ert-deftest dabbrev-expand-test-other-buffer-4 ()
|
|
|
|
"Test expanding a string using another narrowed buffer."
|
|
|
|
(with-dabbrev-test
|
|
|
|
(let (disabled-command-function) ; Enable narrow-to-region.
|
|
|
|
(find-file (ert-resource-file "INSTALL_BEGIN"))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(execute-kbd-macro (kbd "C-s Ind M-a C-SPC M-} C-x n n"))
|
|
|
|
(switch-to-buffer (get-buffer-create "a" t))
|
|
|
|
(execute-kbd-macro (kbd "Ind M-/"))
|
|
|
|
(should (string= (buffer-string) "Indic"))
|
|
|
|
(execute-kbd-macro (kbd "SPC M-/"))
|
|
|
|
(should (string= (buffer-string) "Indic and"))
|
|
|
|
(kill-buffer "a"))))
|
|
|
|
|
|
|
|
(ert-deftest dabbrev-expand-test-minibuffer-1 ()
|
|
|
|
"Test expanding a prefix string twice in the minibuffer.
|
|
|
|
Both expansions should come from the buffer from which the minibuffer
|
|
|
|
was entered."
|
|
|
|
(with-dabbrev-test
|
|
|
|
(find-file (ert-resource-file "INSTALL_BEGIN"))
|
|
|
|
(with-selected-window (minibuffer-window)
|
|
|
|
(insert "Ind")
|
|
|
|
(dabbrev-expand nil)
|
|
|
|
(should (string= (minibuffer-contents) "Indic"))
|
|
|
|
(insert " ")
|
|
|
|
(dabbrev-expand nil)
|
|
|
|
(should (string= (minibuffer-contents) "Indic and"))
|
|
|
|
(delete-minibuffer-contents))))
|
|
|
|
|
|
|
|
(ert-deftest dabbrev-expand-test-minibuffer-2 ()
|
|
|
|
"Test expanding a string + space in the minibuffer.
|
|
|
|
The expansions should come from the buffer from which the minibuffer was
|
|
|
|
entered."
|
|
|
|
(with-dabbrev-test
|
|
|
|
(find-file (ert-resource-file "INSTALL_BEGIN"))
|
|
|
|
(with-selected-window (minibuffer-window)
|
|
|
|
(insert "Indic ")
|
|
|
|
(dabbrev-expand nil)
|
|
|
|
(should (string= (minibuffer-contents) "Indic and"))
|
|
|
|
(insert " ")
|
|
|
|
(dabbrev-expand nil)
|
|
|
|
(should (string= (buffer-string) "Indic and Khmer"))
|
|
|
|
(delete-minibuffer-contents))))
|
|
|
|
|
|
|
|
;; FIXME: Why is dabbrev--reset-global-variables needed here?
|
|
|
|
(ert-deftest dabbrev-expand-test-minibuffer-3 ()
|
|
|
|
"Test replacing an expansion in the minibuffer using two buffers.
|
2024-11-30 23:28:06 +01:00
|
|
|
The first expansion should be found in the buffer from which the
|
Fix bugs in dabbrev-expand (bug#74090)
* lisp/dabbrev.el (dabbrev-expand): Use the buffer where the
expansion was found when setting the internal variables used to
determine the next expansion or a replacement expansion.
* test/lisp/dabbrev-tests.el (ert-x): Require for
'ert-with-temp-directory', 'ert-resource-directory' and
'ert-resource-file'.
(with-dabbrev-test): New macro.
(dabbrev-expand-test-same-buffer-{1,2,3,4})
(dabbrev-expand-test-other-buffer-{1,2,3,4})
(dabbrev-expand-test-minibuffer-{1,2,3,4}): New tests.
* test/lisp/dabbrev-resources/dabbrev-expand.el:
* test/lisp/dabbrev-resources/INSTALL_BEGIN: New test resources.
(cherry picked from commit f6c359cb66a0e9b851e3467b1ba9cab7efa8f744)
2024-10-31 10:46:27 +01:00
|
|
|
minibuffer was entered, the replacement should found in another buffer."
|
|
|
|
(with-dabbrev-test
|
|
|
|
(find-file (ert-resource-file "INSTALL_BEGIN"))
|
|
|
|
(find-file (ert-resource-file "dabbrev-expand.el"))
|
|
|
|
(with-selected-window (minibuffer-window)
|
|
|
|
(insert "Ind")
|
|
|
|
(dabbrev-expand nil)
|
|
|
|
(should (string= (minibuffer-contents) "Indicate"))
|
|
|
|
(kill-whole-line)
|
|
|
|
(dabbrev--reset-global-variables)
|
|
|
|
(insert "Ind")
|
|
|
|
(dabbrev-expand nil)
|
|
|
|
(dabbrev-expand nil)
|
|
|
|
(should (string= (minibuffer-contents) "Indic"))
|
|
|
|
(dabbrev--reset-global-variables)
|
|
|
|
(insert " ")
|
|
|
|
(dabbrev-expand nil)
|
|
|
|
(should (string= (minibuffer-contents) "Indic and"))
|
|
|
|
(delete-minibuffer-contents))))
|
|
|
|
|
|
|
|
(ert-deftest dabbrev-expand-test-minibuffer-4 ()
|
|
|
|
"Test expansion in the minibuffer using another narrowed buffer."
|
|
|
|
(with-dabbrev-test
|
|
|
|
(let (disabled-command-function) ; Enable narrow-to-region.
|
|
|
|
(find-file (ert-resource-file "INSTALL_BEGIN"))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(execute-kbd-macro (kbd "C-s Ind M-a C-SPC M-} C-x n n")))
|
|
|
|
(with-selected-window (minibuffer-window)
|
|
|
|
(insert "Ind")
|
|
|
|
(dabbrev-expand nil)
|
|
|
|
(should (string= (minibuffer-contents) "Indic"))
|
|
|
|
(insert " ")
|
|
|
|
(dabbrev-expand nil)
|
|
|
|
(should (string= (minibuffer-contents) "Indic and"))
|
|
|
|
(delete-minibuffer-contents))))
|
|
|
|
|
2024-11-30 23:28:06 +01:00
|
|
|
(ert-deftest dabbrev-expand-after-killing-buffer ()
|
|
|
|
"Test expansion after killing buffer containing first expansion.
|
|
|
|
Finding successive expansions in another live buffer should succeed, but
|
2024-12-02 13:13:39 +01:00
|
|
|
after killing the buffer, expansion should fail with a user-error,
|
|
|
|
leaving the unexpanded string in the buffer." ; See bug#74090.
|
2024-11-30 23:28:06 +01:00
|
|
|
(with-dabbrev-test
|
|
|
|
(with-current-buffer (get-buffer-create "foo")
|
|
|
|
(insert "abc abd"))
|
|
|
|
(switch-to-buffer "*scratch*")
|
|
|
|
(erase-buffer)
|
|
|
|
(execute-kbd-macro (kbd "ab M-/"))
|
|
|
|
(should (string= (buffer-string) "abc"))
|
|
|
|
(execute-kbd-macro (kbd "SPC ab M-/"))
|
|
|
|
(should (string= (buffer-string) "abc abc"))
|
|
|
|
(erase-buffer)
|
|
|
|
(execute-kbd-macro (kbd "abc SPC ab M-/ M-/"))
|
|
|
|
(should (string= (buffer-string) "abc abd"))
|
|
|
|
(kill-buffer "foo")
|
|
|
|
(erase-buffer)
|
2024-12-03 10:38:26 +01:00
|
|
|
;; In batch runs of this file, the user-error message contains curved
|
|
|
|
;; quotes, but grave quotes when running `make check' (so here was
|
|
|
|
;; evidently not passed to `substitute-command-keys'), so use grave
|
|
|
|
;; quotes so the test succeeds in both modes of execution.
|
|
|
|
(let* ((text-quoting-style 'grave)
|
|
|
|
(msg (cadr (should-error (execute-kbd-macro (kbd "abc SPC ab M-/ M-/"))
|
2024-12-02 13:13:39 +01:00
|
|
|
:type 'user-error))))
|
|
|
|
(should (string= (buffer-string) "abc ab"))
|
2024-12-03 10:38:26 +01:00
|
|
|
(should (string= msg "No further dynamic expansion for `ab' found")))))
|
2024-11-30 23:28:06 +01:00
|
|
|
|
2021-09-26 01:53:56 +02:00
|
|
|
;;; dabbrev-tests.el ends here
|