aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test-system-lib--completion-file-annotator.el54
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/test-system-lib--completion-file-annotator.el b/tests/test-system-lib--completion-file-annotator.el
new file mode 100644
index 000000000..9e1f4aa4a
--- /dev/null
+++ b/tests/test-system-lib--completion-file-annotator.el
@@ -0,0 +1,54 @@
+;;; test-system-lib--completion-file-annotator.el --- Tests for cj/completion-file-annotator -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; Unit tests for `cj/completion-file-annotator', the annotation-function
+;; factory used to annotate file-basename completion pickers with size and
+;; modification date.
+
+;;; Code:
+
+(require 'ert)
+(require 'system-lib)
+
+(ert-deftest test-system-lib-completion-file-annotator-normal-file-shows-size-and-date ()
+ "Normal: a regular file is annotated with a size and an ISO date."
+ (let ((file (make-temp-file "cfa-test-" nil ".txt" "hello world")))
+ (unwind-protect
+ (let* ((annotate (cj/completion-file-annotator
+ (lambda (_cand) file)))
+ (result (funcall annotate "anything")))
+ (should (stringp result))
+ ;; file-size-human-readable of 11 bytes is "11"
+ (should (string-match-p "11" result))
+ ;; ISO date for the file's mtime
+ (should (string-match-p
+ (format-time-string "%Y-%m-%d"
+ (file-attribute-modification-time
+ (file-attributes file)))
+ result)))
+ (delete-file file))))
+
+(ert-deftest test-system-lib-completion-file-annotator-boundary-directory-marked-dir ()
+ "Boundary: a directory candidate is annotated with the `dir' marker."
+ (let ((dir (make-temp-file "cfa-dir-" t)))
+ (unwind-protect
+ (let* ((annotate (cj/completion-file-annotator (lambda (_c) dir)))
+ (result (funcall annotate "d")))
+ (should (stringp result))
+ (should (string-match-p "dir" result)))
+ (delete-directory dir t))))
+
+(ert-deftest test-system-lib-completion-file-annotator-error-nil-path-returns-nil ()
+ "Error: a candidate whose path-resolver returns nil yields no annotation."
+ (let ((annotate (cj/completion-file-annotator (lambda (_c) nil))))
+ (should (null (funcall annotate "missing")))))
+
+(ert-deftest test-system-lib-completion-file-annotator-error-missing-file-returns-nil ()
+ "Error: a path that does not exist yields no annotation."
+ (let* ((path (expand-file-name "definitely-not-here-12345.txt"
+ temporary-file-directory))
+ (annotate (cj/completion-file-annotator (lambda (_c) path))))
+ (should (null (funcall annotate "gone")))))
+
+(provide 'test-system-lib--completion-file-annotator)
+;;; test-system-lib--completion-file-annotator.el ends here