1 ;;; test.el --- tests for easy-kill -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
5 ;; Author: Leo Liu <sdl.web@gmail.com>
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
26 ;; (defmacro with-named-temp-buffer (name &rest body)
27 ;; (declare (indent with-current-buffer))
29 ;; (with-current-buffer (get-buffer-create ,name)
31 ;; (and (get-buffer ,name) (kill-buffer ,name))))
33 ;; (ert-deftest test-compilation ()
34 ;; (should (zerop (call-process "emacs" nil nil nil
35 ;; "-batch" "-Q" "-f" "batch-byte-compile"
38 (ert-deftest test-easy-kill-trim ()
39 (should (string= "" (easy-kill-trim " \f\t\n\n\n")))
40 (should (string= "abc" (easy-kill-trim " \t\fabc")))
41 (should (string= "abc" (easy-kill-trim "abc")))
42 (should (string= " \t\fabc" (easy-kill-trim " \t\fabc" 'right)))
43 (should (string= "abc" (easy-kill-trim " \t\fabc" 'left))))
45 (ert-deftest test-easy-kill-get ()
49 (setf (easy-kill-get bounds) '(1 . 4))
50 (should (string= (easy-kill-candidate) "two"))
51 (should-error (setf (easy-kill-get bounds) nil))
52 (setf (easy-kill-get end) (point-max))
53 (should (= (easy-kill-get end) (point-max)))))
55 (ert-deftest test-easy-kill-candidate ()
59 (should-error (easy-kill-candidate))
61 (easy-kill-thing 'word)
62 (should (string= w (easy-kill-candidate)))
63 (easy-kill-thing 'buffer-file-name)
64 (should (string= (directory-file-name default-directory)
65 (easy-kill-candidate))))))
67 (ert-deftest test-easy-kill-describe-candidate ()
69 (insert "(list 1 2 3)")
72 (easy-kill-thing 'sexp)
73 (easy-kill-thing 'list)
74 (should (string-match-p "^\\s-*thing:\\s-*list"
75 (easy-kill-describe-candidate)))))
77 (ert-deftest test-easy-kill-append ()
81 (easy-kill-thing 'word)
82 (call-interactively #'easy-kill-append)
83 (should (string= (car kill-ring) "abc"))))
85 ;;; Make sure the old format of easy-kill-alist is still supported.
86 (ert-deftest test-old-easy-kill-alist ()
87 (let ((easy-kill-alist '((?w . word)
93 (?b . buffer-file-name)))
94 (text "(first line\nsecond line\nthird line)"))
97 (goto-char (point-min))
99 (let ((last-command-event ?d))
100 (call-interactively #'easy-kill-thing))
101 (should (string= text (easy-kill-candidate))))))
103 (ert-deftest test-easy-kill-help ()
104 (let ((easy-kill-alist '((?w . word)
110 (?b . buffer-file-name)
111 (?x buffer-file-name-buffer-file-name "\t"))))
113 (with-current-buffer (help-buffer)
114 (goto-char (point-min))
115 (should (save-excursion
116 (re-search-forward "^w\\s-*word$" nil t)))
117 (should (save-excursion
118 (re-search-forward "^d\\s-*defun\\s-*\"\\\\n\\\\n\"$" nil t)))
119 (should (save-excursion
120 (re-search-forward "^f\\s-*filename$" nil t)))
121 (should (save-excursion
122 (re-search-forward "^b\\s-*buffer-file-name$" nil t))))))
124 (ert-deftest test-easy-kill-thing-handler ()
125 (should (eq (easy-kill-thing-handler "easy-kill-on-list" 'nxml-mode)
126 'easy-kill-on-list:nxml))
127 (should (eq (easy-kill-thing-handler "easy-kill-on-list" 'js2-mode)
128 'easy-kill-on-list:js2))
129 (should (eq (easy-kill-thing-handler "easy-kill-on-sexp" 'nxml-mode)
132 (fset 'js2:easy-kill-on-list #'ignore)
133 (eq (easy-kill-thing-handler "easy-kill-on-sexp" 'js2-mode)
134 'js2:easy-kill-on-list))
136 (ert-deftest test-easy-kill-bounds-of-list-at-point ()
137 (let ((text "\"abc (1 2 3) xyz\" ; dummy comment")
140 (should (string= \"display editor.\\nsome of the ways to customize it;\"
141 (easy-kill-candidate))))"))
142 (cl-labels ((getb (bounds)
144 (buffer-substring (car bounds) (cdr bounds))
149 (search-backward "2")
150 (should (string= (getb (easy-kill-bounds-of-list-at-point)) "(1 2 3)"))
153 (should (string= (getb (easy-kill-bounds-of-list-at-point))
154 "\"abc (1 2 3) xyz\""))
155 (search-forward "dummy")
157 (should (string= (getb (easy-kill-bounds-of-list-at-point))
162 (goto-char (point-min))
163 (re-search-forward "customize")
164 (call-interactively 'easy-kill)
165 (easy-kill-thing 'list)
166 (should (string= "\"display editor.\\nsome of the ways to customize it;\""
167 (easy-kill-candidate)))))))
169 (ert-deftest test-easy-kill-on-list ()
170 (let ((text "(defun first-error (&optional n)
171 \"This operates on the output from the \\\\[compile] command, for instance.\"
173 (next-error n t)) ;some dummy comment here"))
177 (goto-char (point-min))
178 (search-forward "[compile")
179 (call-interactively 'easy-kill)
180 (easy-kill-thing 'list)
181 (should (string= "[compile]" (easy-kill-candidate)))
183 (call-interactively 'easy-kill)
185 (cl-letf (((symbol-function 'easy-kill-interprogram-cut)
186 (lambda (text) (setq clipboard text))))
187 (easy-kill-thing 'list))
188 (should (string= (easy-kill-candidate) clipboard)))
189 (should (string= "\"This operates on the output from the \\\\[compile] command, for instance.\""
190 (easy-kill-candidate)))
191 (easy-kill-thing 'list)
192 (should (string-match-p "(interactive \"p\")" (easy-kill-candidate)))
193 (should (string-prefix-p "\"This operates on" (easy-kill-candidate)))
194 (forward-sexp 1) ; where bounds of list is nil
195 (call-interactively 'easy-kill)
196 (easy-kill-thing 'list)
197 (should (string= "\"This operates on the output from the \\\\[compile] command, for instance.\""
198 (easy-kill-candidate)))
199 (search-forward "dummy")
201 (call-interactively 'easy-kill)
202 (easy-kill-thing 'list)
203 (should (string= "dummy" (easy-kill-candidate))))))
205 (ert-deftest test-js2-mode ()
206 :expected-result :failed
207 (let ((js "function View(name, options) {
208 options = options || {};
210 this.root = options.root;
211 var engines = options.engines;
212 this.defaultEngine = options.defaultEngine;
213 var ext = this.ext = extname(name);
214 if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no extension was provided.');
215 if (!ext) name += (ext = this.ext = ('.' != this.defaultEngine[0] ? '.' : '') + this.defaultEngine);
216 this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express);
217 this.path = this.lookup(name);
219 (buff (get-buffer-create "*js2*")))
220 (eval-and-compile (require 'js2-mode nil t))
221 (setq js2-idle-timer-delay 0)
222 (global-font-lock-mode 1)
224 (with-current-buffer buff
227 (goto-char (point-min))
230 (while js2-mode-buffer-dirty-p
232 (search-forward "this.defaultEngine =")
235 (easy-kill-thing 'char)
236 (easy-kill-thing 'list)
237 (should (string= "this.defaultEngine = options.defaultEngine"
238 (easy-kill-candidate)))
239 ;; XXX: should also test (easy-kill-digit-argument 0)
241 (kill-buffer buff))))
243 (ert-deftest test-nxml-mode ()
244 (let ((xml "<?xml version=\"1.0\"?>
247 <author>Gambardella, Matthew</author>
248 <title>XML Developer's Guide</title>
249 <genre>Computer</genre>
251 <publish_date>2000-10-01</publish_date>
252 <description>An in-depth look at creating applications
253 with XML.</description>
259 (goto-char (point-min))
260 (search-forward "Gambardella")
262 (easy-kill-thing 'sexp)
266 (should (eq (easy-kill-get thing) 'sexp))
267 (should (string= "<author>Gambardella, Matthew</author>"
268 (easy-kill-candidate)))
269 (call-interactively 'easy-kill)
270 (easy-kill-thing 'list)
271 (should (string= "<author>Gambardella, Matthew</author>"
272 (easy-kill-candidate)))
273 (easy-kill-thing nil 1)
274 (easy-kill-digit-argument 0)
275 (should (string= "<author>Gambardella, Matthew</author>"
276 (easy-kill-candidate))))))
278 (ert-deftest test-org-mode ()
279 (let ((org "#+title: This is a title
282 This is an example of org document.
299 (goto-char (point-min))
300 (search-forward "This is")
301 (call-interactively 'easy-kill)
302 (easy-kill-thing 'sexp)
304 (should (string= "#+title: This is a title\n" (easy-kill-candidate)))
305 (search-forward "Fruits")
306 (call-interactively 'easy-kill)
307 (easy-kill-thing 'sexp)
309 (should (string-prefix-p "*** Fruits" (easy-kill-candidate)))
310 (search-forward "Ferrari")
311 (call-interactively 'easy-kill)
312 (easy-kill-thing 'list)
313 (should (string= "Ferrari\n" (easy-kill-candidate)))
315 (should (string= " + Ferrari\n" (easy-kill-candidate)))
317 (search-backward "Lamborghini")
318 (call-interactively 'easy-kill)
319 (easy-kill-thing 'list)
320 ;; You get the whole plainlist here; see `org-element-at-point'.
322 (should (string= " + Lamborghini\n + Ferrari\n + Porsche\n"
323 (easy-kill-candidate)))
325 (should (string= "* Sports cars\n + Lamborghini\n + Ferrari\n + Porsche\n"
326 (easy-kill-candidate))))))
328 (ert-deftest test-elisp-mode ()
329 (let ((el "(defun set-hard-newline-properties (from to)
330 (let ((sticky (get-text-property from 'rear-nonsticky)))
331 ;; XXX: (put-text-property from to 'hard 't)
332 ;; If rear-nonsticky is not \"t\", add 'hard to rear-nonsticky list
333 (if (and (listp sticky) (not (memq 'hard sticky)))
334 (put-text-property from (point) 'rear-nonsticky
335 (cons 'hard sticky)))))"))
338 (goto-char (point-min))
339 (search-forward "put-text-property")
341 (easy-kill-thing 'sexp)
343 (should (eq (easy-kill-get thing) 'sexp))
344 (should (string= "(put-text-property from to 'hard 't)"
345 (easy-kill-candidate)))
348 (should (string= el (easy-kill-candidate)))
351 (should (string= "(put-text-property from to 'hard 't)"
352 (easy-kill-candidate)))
353 (easy-kill-digit-argument 0)
354 (should (string= (thing-at-point 'sexp) (easy-kill-candidate))))))
356 (ert-deftest test-easy-kill-thing-forward ()
357 (let ((txt "Emacs is the extensible
359 some of the ways to customize it;
365 (easy-kill-thing 'line)
366 (should (string= "some of the ways to customize it;\n24.3."
367 (easy-kill-candidate)))
369 (should (string= "some of the ways to customize it;\n"
370 (easy-kill-candidate)))
372 (should (string= " display editor.\nsome of the ways to customize it;\n"
373 (easy-kill-candidate)))
374 (easy-kill-thing 'word)
376 (should (string= "editor.\nsome" (easy-kill-candidate)))
378 (should (string= "display editor.\nsome" (easy-kill-candidate)))
380 (should (string= " editor.\nsome" (easy-kill-candidate)))
381 (easy-kill-destroy-candidate)
382 (goto-char (point-min))
384 (call-interactively 'easy-mark)
385 (should (string= " display" (easy-kill-candidate)))
386 (goto-char (easy-kill-get origin))
388 ;; Test the case where there is no thing at point
389 (call-interactively 'easy-kill)
390 (setf (easy-kill-get thing) 'word)
391 (setf (easy-kill-get bounds) (cons (point) (point)))
393 (should (string= " display" (easy-kill-candidate)))
397 (should (string= "the extensible\n" (easy-kill-candidate))))))
399 (ert-deftest test-workaround-defun-bug ()
400 ;; http://debbugs.gnu.org/17247
401 (let ((txt "(tan 2)\n\n(sin 2)\n(cos 2)\n"))
405 (easy-kill-thing 'defun)
407 (should (string= "(sin 2)\n(cos 2)\n" (easy-kill-candidate)))
409 (should (string= txt (easy-kill-candidate))))))
411 (ert-deftest test-easy-kill-exchange-point-and-mark ()
412 (let ((txt "(delete-region (point)
413 (if (re-search-forward \"[^ \\t\\n]\" nil t)
414 (progn (beginning-of-line) (point))
419 (goto-char (point-min))
420 (search-forward "re-search-forward")
421 (call-interactively 'easy-mark)
422 (should (string= "re-search-forward" (easy-kill-candidate)))
423 (should (= (point) (easy-kill-get end)))
424 (call-interactively 'easy-kill-exchange-point-and-mark)
426 (should (= (point) (easy-kill-get start)))
427 (should (= (mark t) (easy-kill-get end)))
428 (call-interactively 'easy-kill-exchange-point-and-mark)
430 (should (= (mark t) (easy-kill-get start)))
431 (should (= (point) (easy-kill-get end))))))
433 ;;; test.el ends here