From 9a85e506b7a1e528ac43576bf393b06c3802b58f Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Mon, 24 Jun 2024 16:58:49 +0200 Subject: [PATCH] ; completion-preview-tests.el: Deduplicate some test code * test/lisp/completion-preview-tests.el (completion-preview-tests--insert-and-preview): New function. (completion-preview) (completion-preview-multiple-matches) (completion-preview-exact-match-only) (completion-preview-function-capfs) (completion-preview-non-exclusive-capfs) (completion-preview-face-updates) (completion-preview-capf-errors) (completion-preview-complete) (completion-preview-insert-calls-exit-function): Use it. --- test/lisp/completion-preview-tests.el | 62 +++++++++------------------ 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/test/lisp/completion-preview-tests.el b/test/lisp/completion-preview-tests.el index 5d1c09e9550..b57d57ef79a 100644 --- a/test/lisp/completion-preview-tests.el +++ b/test/lisp/completion-preview-tests.el @@ -48,24 +48,26 @@ instead." (or end-face 'completion-preview)))))) +(defun completion-preview-tests--insert-and-preview (string) + "Insert STRING and update the completion preview." + (insert string) + (let ((this-command 'self-insert-command)) + (completion-preview--post-command))) + (ert-deftest completion-preview () "Test Completion Preview mode." (with-temp-buffer (setq-local completion-at-point-functions (list (completion-preview-tests--capf '("foobarbaz")))) - (insert "foo") - (let ((this-command 'self-insert-command)) - (completion-preview--post-command)) + (completion-preview-tests--insert-and-preview "foo") ;; Exact match (completion-preview-tests--check-preview "barbaz" 'completion-preview-exact 'completion-preview-exact) - (insert "v") - (let ((this-command 'self-insert-command)) - (completion-preview--post-command)) + (completion-preview-tests--insert-and-preview "v") ;; No match, no preview (completion-preview-tests--check-preview nil) @@ -85,9 +87,7 @@ instead." (setq-local completion-at-point-functions (list (completion-preview-tests--capf '("foobar" "foobaz")))) - (insert "foo") - (let ((this-command 'self-insert-command)) - (completion-preview--post-command)) + (completion-preview-tests--insert-and-preview "foo") ;; Multiple matches, the preview shows the first one (completion-preview-tests--check-preview "bar" 'completion-preview-common) @@ -104,17 +104,13 @@ instead." (list (completion-preview-tests--capf '("spam" "foobar" "foobaz"))) completion-preview-exact-match-only t) - (insert "foo") - (let ((this-command 'self-insert-command)) - (completion-preview--post-command)) + (completion-preview-tests--insert-and-preview "foo") ;; Multiple matches, so no preview (completion-preview-tests--check-preview nil) (delete-region (point-min) (point-max)) - (insert "spa") - (let ((this-command 'self-insert-command)) - (completion-preview--post-command)) + (completion-preview-tests--insert-and-preview "spa") ;; Exact match (completion-preview-tests--check-preview "m" @@ -129,9 +125,7 @@ instead." (lambda () #'ignore) (completion-preview-tests--capf '("foobar" "foobaz")))) - (insert "foo") - (let ((this-command 'self-insert-command)) - (completion-preview--post-command)) + (completion-preview-tests--insert-and-preview "foo") (completion-preview-tests--check-preview "bar" 'completion-preview-common))) (ert-deftest completion-preview-non-exclusive-capfs () @@ -145,9 +139,7 @@ instead." '("foobar" "foobaz") :exclusive 'no) (completion-preview-tests--capf '("foobarbaz")))) - (insert "foo") - (let ((this-command 'self-insert-command)) - (completion-preview--post-command)) + (completion-preview-tests--insert-and-preview "foo") (completion-preview-tests--check-preview "bar" 'completion-preview-common) (setq-local completion-preview-exact-match-only t) (let ((this-command 'self-insert-command)) @@ -163,13 +155,9 @@ instead." (list (completion-preview-tests--capf '("foobarbaz" "food")))) - (insert "foo") - (let ((this-command 'self-insert-command)) - (completion-preview--post-command)) + (completion-preview-tests--insert-and-preview "foo") (completion-preview-tests--check-preview "d") - (insert "b") - (let ((this-command 'self-insert-command)) - (completion-preview--post-command)) + (completion-preview-tests--insert-and-preview "b") (completion-preview-tests--check-preview "arbaz" 'completion-preview-exact 'completion-preview-exact) @@ -188,9 +176,7 @@ instead." (lambda () (user-error "Bad")) (completion-preview-tests--capf '("foobarbaz")))) - (insert "foo") - (let ((this-command 'self-insert-command)) - (completion-preview--post-command)) + (completion-preview-tests--insert-and-preview "foo") (completion-preview-tests--check-preview "barbaz" 'completion-preview-exact 'completion-preview-exact))) @@ -225,9 +211,7 @@ instead." (lambda (&rest args) (setq exit-fn-called t exit-fn-args args))))) - (insert "foo") - (let ((this-command 'self-insert-command)) - (completion-preview--post-command)) + (completion-preview-tests--insert-and-preview "foo") (completion-preview-tests--check-preview "bar" 'completion-preview-common) @@ -252,9 +236,7 @@ instead." ;; (completion-preview-active-mode -1) ;; Narrow further. - (insert "s") - (let ((this-command 'self-insert-command)) - (completion-preview--post-command)) + (completion-preview-tests--insert-and-preview "s") ;; The preview should indicate an exact match. (completion-preview-tests--check-preview "h" @@ -296,9 +278,7 @@ instead." (lambda (&rest args) (setq exit-fn-called t exit-fn-args args))))) - (insert "foo") - (let ((this-command 'self-insert-command)) - (completion-preview--post-command)) + (completion-preview-tests--insert-and-preview "foo") (completion-preview-tests--check-preview "bar" 'completion-preview-common) (completion-preview-insert) (should (string= (buffer-string) "foobar")) @@ -313,9 +293,7 @@ instead." (list (completion-preview-tests--capf '("foobar" "foobaz")))) - (insert "foo") - (let ((this-command 'self-insert-command)) - (completion-preview--post-command)) + (completion-preview-tests--insert-and-preview "foo") (should (eq completion-preview-cursor-type (window-cursor-type nil))) (should (completion-preview--cursor-modified-p)) -- 2.39.2