all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 12a156fbb3802722a5dc82773f6e1c56c74fcd01 7948 bytes (raw)
name: test/lisp/eshell/em-cmpl-tests.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
 
;;; em-cmpl-tests.el --- em-cmpl test suite  -*- lexical-binding:t -*-

;; Copyright (C) 2023 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:

;; Tests for Eshell's interactive completion.

;;; Code:

(require 'ert)
(require 'eshell)
(require 'em-cmpl)
(require 'em-dirs)
(require 'em-hist)
(require 'em-tramp)
(require 'em-unix)

(require 'eshell-tests-helpers
         (expand-file-name "eshell-tests-helpers"
                           (file-name-directory (or load-file-name
                                                    default-directory))))

(defvar eshell-test-value nil)

(defun eshell-insert-and-complete (input)
  "Insert INPUT and invoke completion, returning the result."
  (insert input)
  (completion-at-point)
  (eshell-get-old-input))

(defun eshell-arguments-equal (actual expected)
  "Return t if ACTUAL and EXPECTED are equal, including properties of strings.
ACTUAL and EXPECTED should both be lists of strings."
  (when (length= actual (length expected))
    (catch 'not-equal
      (cl-mapc (lambda (i j)
                 (unless (equal-including-properties i j)
                   (throw 'not-equal nil)))
               actual expected)
      t)))

(defun eshell-arguments-equal--equal-explainer (actual expected)
  "Explain the result of `eshell-arguments-equal'."
  `(nonequal-result
    (actual ,actual)
    (expected ,expected)))

(put 'eshell-arguments-equal 'ert-explainer
     #'eshell-arguments-equal--equal-explainer)

;;; Tests:

(ert-deftest em-cmpl-test/parse-arguments/pipeline ()
  "Test that parsing arguments for completion discards earlier commands."
  (with-temp-eshell
   (let ((eshell-test-value '("foo" "bar")))
     (insert "echo hi | cat")
     (should (eshell-arguments-equal
              (car (eshell-complete-parse-arguments))
              '("cat"))))))

(ert-deftest em-cmpl-test/parse-arguments/multiple-dots ()
  "Test parsing arguments with multiple dots like \".../\"."
  (with-temp-eshell
   (insert "echo .../file.txt")
   (should (eshell-arguments-equal
            (car (eshell-complete-parse-arguments))
            `("echo" ,(propertize "../../file.txt"
                                  'pcomplete-arg-value
                                  ".../file.txt"))))))

(ert-deftest em-cmpl-test/parse-arguments/variable/numeric ()
  "Test parsing arguments with a numeric variable interpolation."
  (with-temp-eshell
   (let ((eshell-test-value 42))
     (insert "echo $eshell-test-value")
     (should (eshell-arguments-equal
              (car (eshell-complete-parse-arguments))
              `("echo" ,(propertize "42" 'pcomplete-arg-value 42)))))))

(ert-deftest em-cmpl-test/parse-arguments/variable/nil ()
  "Test parsing arguments with a nil variable interpolation."
  (with-temp-eshell
   (let ((eshell-test-value nil))
     (insert "echo $eshell-test-value")
     (should (eshell-arguments-equal
              (car (eshell-complete-parse-arguments))
              `("echo" ,(propertize "" 'pcomplete-arg-value nil)))))))

(ert-deftest em-cmpl-test/parse-arguments/variable/list ()
  "Test parsing arguments with a list variable interpolation."
  (with-temp-eshell
   (let ((eshell-test-value '("foo" "bar")))
     (insert "echo $eshell-test-value")
     (should (eshell-arguments-equal
              (car (eshell-complete-parse-arguments))
              `("echo" ,(propertize "(\"foo\" \"bar\")"
                                    'pcomplete-arg-value
                                    eshell-test-value)))))))

(ert-deftest em-cmpl-test/parse-arguments/variable/splice ()
  "Test parsing arguments with a spliced variable interpolation."
  (with-temp-eshell
   (let ((eshell-test-value '("foo" "bar")))
     (insert "echo $@eshell-test-value")
     (should (eshell-arguments-equal
              (car (eshell-complete-parse-arguments))
              '("echo" "foo" "bar"))))))

(ert-deftest em-cmpl-test/file-completion/unique ()
  "Test completion of file names when there's a unique result."
  (with-temp-eshell
   (ert-with-temp-directory default-directory
     (write-region nil nil (expand-file-name "file.txt"))
     (should (equal (eshell-insert-and-complete "echo fi")
                    "echo file.txt ")))))

(ert-deftest em-cmpl-test/file-completion/non-unique ()
  "Test completion of file names when there are multiple results."
  (with-temp-eshell
   (ert-with-temp-directory default-directory
     (write-region nil nil (expand-file-name "file.txt"))
     (write-region nil nil (expand-file-name "file.el"))
     (should (equal (eshell-insert-and-complete "echo fi")
                    "echo file."))
     ;; Now try completing again.
     (let ((minibuffer-message-timeout 0)
           (inhibit-message t))
       (completion-at-point))
     ;; FIXME: We can't use `current-message' here.
     (with-current-buffer (messages-buffer)
       (save-excursion
         (goto-char (point-max))
         (forward-line -1)
         (should (looking-at "Complete, but not unique")))))))

(ert-deftest em-cmpl-test/file-completion/after-list ()
  "Test completion of file names after previous list arguments.
See bug#59956."
  (with-temp-eshell
   (ert-with-temp-directory default-directory
     (write-region nil nil (expand-file-name "file.txt"))
     (should (equal (eshell-insert-and-complete "echo (list 1 2) fi")
                    "echo (list 1 2) file.txt ")))))

(ert-deftest em-cmpl-test/lisp-symbol-completion ()
  "Test completion of Lisp forms like \"#'symbol\" and \"`symbol\".
See <lisp/eshell/esh-cmd.el>."
  (with-temp-eshell
   (should (equal (eshell-insert-and-complete "echo #'system-nam")
                  "echo #'system-name ")))
  (with-temp-eshell
   (should (equal (eshell-insert-and-complete "echo `system-nam")
                  "echo `system-name "))))

(ert-deftest em-cmpl-test/lisp-function-completion ()
  "Test completion of Lisp forms like \"(func)\".
See <lisp/eshell/esh-cmd.el>."
  (with-temp-eshell
   (should (equal (eshell-insert-and-complete "echo (eshell/ech")
                  "echo (eshell/echo"))))

(ert-deftest em-cmpl-test/variable-ref-completion ()
  "Test completion of variable references like \"$var\".
See <lisp/eshell/esh-var.el>."
  (with-temp-eshell
   (should (equal (eshell-insert-and-complete "echo $system-nam")
                  "echo $system-name "))))

(ert-deftest em-cmpl-test/variable-assign-completion ()
  "Test completion of variable assignments like \"var=value\".
See <lisp/eshell/esh-var.el>."
  (with-temp-eshell
   (ert-with-temp-directory default-directory
     (write-region nil nil (expand-file-name "file.txt"))
     (should (equal (eshell-insert-and-complete "VAR=f")
                    "VAR=file.txt ")))))

(ert-deftest em-cmpl-test/user-ref-completion ()
  "Test completeion of user references like \"~user\".
See <lisp/eshell/em-dirs.el>."
  (unwind-protect
      (with-temp-eshell
       (cl-letf (((symbol-function 'eshell-read-user-names)
                  (lambda () (setq eshell-user-names '((1234 . "user"))))))
         ;; FIXME: Should this really add a space at the end?
         (should (equal (eshell-insert-and-complete "echo ~us")
                        "echo ~user/ "))))
    ;; Clear the cached user names we set above.
    (setq eshell-user-names nil)))

;;; em-cmpl-tests.el ends here

debug log:

solving 12a156fbb38 ...
found 12a156fbb38 in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.