Skip to content

Commit

Permalink
Namespace tests
Browse files Browse the repository at this point in the history
  • Loading branch information
axelf4 committed Sep 7, 2024
1 parent aa6bf36 commit e41827e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
9 changes: 4 additions & 5 deletions hotfuzz.el
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; hotfuzz.el --- Fuzzy completion style -*- lexical-binding: t -*-

;; Copyright (C) 2021 Axel Forsman
;; Copyright (C) Axel Forsman

;; Author: Axel Forsman <[email protected]>
;; Version: 0.1
Expand All @@ -27,7 +27,7 @@
(require 'hotfuzz-module nil t)
(declare-function hotfuzz--filter-c "hotfuzz-module")

(defgroup hotfuzz nil "Fuzzy completion style." :group 'minibuffer)
(defgroup hotfuzz () "Fuzzy completion style." :group 'minibuffer)

(defcustom hotfuzz-max-highlighted-completions 25
"The number of top-ranking completions that should be highlighted.
Expand Down Expand Up @@ -145,8 +145,7 @@ will lead to inaccuracies."
(cons (concat "\\`" re) completion-regexp-list))))
(all (if (and (string= prefix "") (or (stringp (car-safe table)) (null table))
(not (or pred completion-regexp-list (string= needle ""))))
table
(all-completions prefix table pred))))
table (all-completions prefix table pred))))
;; `completion-pcm--all-completions' tests completion-regexp-list
;; again with functional tables even though they should handle it.
(cond
Expand Down Expand Up @@ -176,7 +175,7 @@ will lead to inaccuracies."
(add-to-list 'completion-styles-alist
'(hotfuzz completion-flex-try-completion hotfuzz-all-completions
"Fuzzy completion."))
;; Why is the Emacs completions API so cursed?
;; Why is the Emacs completion API so cursed?
(put 'hotfuzz 'completion--adjust-metadata #'hotfuzz--adjust-metadata))

(provide 'hotfuzz)
Expand Down
43 changes: 21 additions & 22 deletions test/tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,43 @@

;;; Validation of costs of preferable traits, all else being equal

(ert-deftest shorter-match-cost-test ()
(ert-deftest hotfuzz-shorter-match-cost-test ()
"Shorter matches should be given lower costs than longer ones."
(should (< (hotfuzz--cost "b" "abc") (hotfuzz--cost "b" "abcd"))))

(ert-deftest bos-match-cost-test ()
(ert-deftest hotfuzz-bos-match-cost-test ()
"Matches at the beginning are preferred."
(should (< (hotfuzz--cost "a" "ab") (hotfuzz--cost "a" "ba"))))

(ert-deftest substring-match-cost-test ()
(ert-deftest hotfuzz-substring-match-cost-test ()
"A substring match means fewer gaps and lower cost."
(should (< (hotfuzz--cost "ab" "abcd") (hotfuzz--cost "ab" "acbd"))))

(ert-deftest camelcase-match-cost-test ()
(ert-deftest hotfuzz-camelcase-match-cost-test ()
(should (< (hotfuzz--cost "ac" "AbCd") (hotfuzz--cost "ac" "abcd"))))

(ert-deftest special-match-cost-test ()
(ert-deftest hotfuzz-special-match-cost-test ()
(should (<= (hotfuzz--cost "x" "/x")
(hotfuzz--cost "x" "-x")
(hotfuzz--cost "x" " x")
(hotfuzz--cost "x" ".x")
(hotfuzz--cost "x" "yx"))))

(ert-deftest tighter-match-cost-test ()
(ert-deftest hotfuzz-tighter-match-cost-test ()
"Test that matches spanning fewer characters are better."
(should (< (hotfuzz--cost "ab" "xaxbxx") (hotfuzz--cost "ab" "xaxxbx"))))

;;; Highlighting tests

(ert-deftest highlight-optimal-test ()
(ert-deftest hotfuzz-highlight-optimal-test ()
"Test that the algorithm is non-greedy."
(should (ert-equal-including-properties
(hotfuzz-highlight "ab" "xaxbxabxaxbx")
#("xaxbxabxaxbx" 5 7 (face completions-common-part)))))

;;; Filtering tests

(ert-deftest case-sensitivity-test ()
(ert-deftest hotfuzz-case-sensitivity-test ()
(let ((xs '("aa" "aA " "Aa " "AA ")))
(let ((completion-ignore-case nil))
(should (equal (hotfuzz-all-completions "a" xs) '("aa" "aA " "Aa ")))
Expand All @@ -50,40 +50,39 @@
(should (equal (hotfuzz-all-completions "a" xs) xs))
(should (equal (hotfuzz-all-completions "A" xs) xs)))))

(ert-deftest long-candidates-test ()
(ert-deftest hotfuzz-long-candidates-test ()
(let ((a (make-string 4096 ?x))
(b (concat (make-string 2047 ?y) "x" (make-string 2048 ?y))))
;; Too long candidates should still be filtered with matches
;; lumped together at the end in their original order.
(should (equal (hotfuzz-all-completions "x" (list (make-string 4096 ?y) b a "x"))
(list "x" b a)))))

(ert-deftest filter-long-needle-test ()
(ert-deftest hotfuzz-filter-long-needle-test ()
(let* ((needle (make-string (1+ hotfuzz--max-needle-len) ?x))
(a (concat needle "y")))
;; With a too long search string candidates should only be
;; filtered but not sorted.
(should (equal (hotfuzz-all-completions needle (list a "y" needle))
(list a needle)))))

(ert-deftest all-completions-test ()
(ert-deftest hotfuzz-all-completions-test ()
(let* ((completion-styles '(hotfuzz))
(s "fb")
(table '("foobar" "fxxx" "foo-baz" "" "fb"))
(meta (completion-metadata s table nil))
(candidates (completion-all-completions s table nil (length s) meta))
(sort-fn (alist-get 'display-sort-function meta))
(last (last candidates)))
(when (numberp (cdr last)) (setcdr last nil))
(when sort-fn (setq candidates (funcall sort-fn candidates)))
(md (completion-metadata s table nil))
(all (completion-all-completions s table nil (length s) md))
(sort-fn (completion-metadata-get md 'display-sort-function)))
(setcdr (last all) nil)
(when sort-fn (setq all (funcall sort-fn all)))
;; Completions should be eagerly fontified by default
(should (equal-including-properties
candidates
all
'(#("fb" 0 2 (face completions-common-part))
#("foo-baz" 0 1 (face completions-common-part) 4 5 (face completions-common-part))
#("foobar" 0 1 (face completions-common-part) 3 4 (face completions-common-part)))))))

(ert-deftest display-sort-function-test ()
(ert-deftest hotfuzz-display-sort-function-test ()
"Test that empty strings apply the completion function `display-sort-function'."
(cl-flet ((sorted-completions (string)
(let* ((completion-styles '(hotfuzz))
Expand All @@ -92,11 +91,11 @@
. ,(lambda (xs) (sort xs #'string<)))))
(all (completion-all-completions
string table nil (length string) md)))
(funcall (alist-get 'display-sort-function md) all))))
(funcall (completion-metadata-get md 'display-sort-function) all))))
(should (equal (sorted-completions "") '("xax" "xbbx" "xx"))) ; Lexicographically sorted
(should (equal (sorted-completions "xx") '("xx" "xax" "xbbx")))))

(ert-deftest boundaries-test ()
(ert-deftest hotfuzz-boundaries-test ()
"Test completion on a single field of a filename."
(let ((completion-styles '(hotfuzz)))
(should
Expand All @@ -115,7 +114,7 @@

(defvar completion-lazy-hilit)
(defvar completion-lazy-hilit-fn)
(ert-deftest lazy-hilit-test ()
(ert-deftest hotfuzz-lazy-hilit-test ()
"Test lazy fontification."
(let ((completion-lazy-hilit t) completion-lazy-hilit-fn)
(should (equal-including-properties (hotfuzz-all-completions "x" '("x")) '("x")))
Expand Down

0 comments on commit e41827e

Please sign in to comment.