branch: externals/hotfuzz
commit 1c651942008a51f1283a405c4b3975d742ca1f25
Author: Axel Forsman <[email protected]>
Commit: Axel Forsman <[email protected]>

    Add a few tests
    
    Uses undercover.el to generate test coverage reports. A GitHub Actions
    workflow runs the tests on every commit.
---
 .github/workflows/test.yml | 24 ++++++++++++++++++++++++
 .gitignore                 |  2 ++
 Eldev                      |  3 +++
 README.md                  |  5 ++++-
 hotfuzz.el                 |  2 +-
 test/tests.el              | 39 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 73 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
new file mode 100644
index 0000000000..2b9d3707ea
--- /dev/null
+++ b/.github/workflows/test.yml
@@ -0,0 +1,24 @@
+name: test
+on: [push, pull_request]
+
+jobs:
+  test:
+    runs-on: ubuntu-latest
+    continue-on-error: ${{ matrix.emacs_version == 'snapshot' }}
+    strategy:
+      matrix:
+        emacs_version: ['27.1', 'snapshot']
+    steps:
+    - name: Set up Emacs
+      uses: purcell/setup-emacs@master
+      with:
+        version: ${{ matrix.emacs_version }}
+    - name: Install Eldev
+      run: curl -fsSL 
https://raw.github.com/doublep/eldev/master/webinstall/github-eldev | sh
+    - uses: actions/checkout@v2
+    - name: Run tests
+      run: |
+        eldev --trace --color=always compile --warnings-as-errors
+        eldev --loading=built-source --trace --color=always test --undercover 
codecov
+    - name: Upload coverage
+      uses: codecov/codecov-action@v1
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000000..53f9fceadb
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/.eldev
+/Eldev-local
diff --git a/Eldev b/Eldev
new file mode 100644
index 0000000000..e9b56cd8fa
--- /dev/null
+++ b/Eldev
@@ -0,0 +1,3 @@
+; -*- mode: emacs-lisp; lexical-binding: t -*-
+
+(eldev-use-plugin 'undercover)
diff --git a/README.md b/README.md
index cb922d775e..c970007a0a 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,8 @@
 # hotfuzz
 
+[![test](https://github.com/axelf4/hotfuzz/actions/workflows/test.yml/badge.svg)](https://github.com/axelf4/hotfuzz/actions/workflows/test.yml)
+[![codecov](https://codecov.io/gh/axelf4/hotfuzz/branch/master/graph/badge.svg?token=OV1BqTB7QL)](https://codecov.io/gh/axelf4/hotfuzz)
+
 Approximate string matching completion style with a scoring algorithm
 that factors in substring matches and word/path component/camelCase
 boundaries.
@@ -53,7 +56,7 @@ It should be said that this limitation combined with
 the bountiful caching that flx does,
 means that it can be faster at scoring long candidates than hotfuzz.
 
-## orderless
+### orderless
 
 The [orderless] completion style allows
 every component of a space-delimited (by default) pattern
diff --git a/hotfuzz.el b/hotfuzz.el
index f7bbd0df42..2ce731483d 100644
--- a/hotfuzz.el
+++ b/hotfuzz.el
@@ -6,7 +6,7 @@
 ;; Version: 0.1
 ;; Package-Requires: ((emacs "27.1") cl-lib)
 ;; Keywords: matching
-;; Homepage: https://github.com/axelf4/hotfuzz.el
+;; Homepage: https://github.com/axelf4/hotfuzz
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 
 ;;; Commentary:
diff --git a/test/tests.el b/test/tests.el
new file mode 100644
index 0000000000..b861427cca
--- /dev/null
+++ b/test/tests.el
@@ -0,0 +1,39 @@
+;;; tests.el --- The hotfuzz test suite  -*- lexical-binding: t; -*-
+
+(require 'ert)
+(require 'hotfuzz)
+
+;;; Validation of costs of preferable traits, all else being equal
+
+(ert-deftest 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 ()
+  "Matches at the beginning are preferred."
+  (should (< (hotfuzz--cost "a" "ab") (hotfuzz--cost "a" "ba"))))
+
+(ert-deftest 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 ()
+  (should (< (hotfuzz--cost "ac" "AbCd") (hotfuzz--cost "ac" "abcd"))))
+
+(ert-deftest 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"))))
+
+;;; Filtering tests
+
+(ert-deftest case-sensitivity-test ()
+  (let ((xs '("aa" "aA" "Aa" "AA")))
+    (let ((completion-ignore-case nil))
+      (should (equal (hotfuzz-filter "a" xs) '("aa" "aA" "Aa")))
+      (should (equal (hotfuzz-filter "A" xs) '("Aa" "AA" "aA"))))
+    (let ((completion-ignore-case t))
+      (should (equal (hotfuzz-filter "a" xs) xs))
+      (should (equal (hotfuzz-filter "A" xs) xs)))))

Reply via email to