From 1f0bf3377e32ce9de33d30d31b3f36447f7264ee Mon Sep 17 00:00:00 2001 From: Jessie Hildebrandt Date: Sat, 18 Nov 2023 20:29:21 -0500 Subject: [PATCH] Add tests --- ert-test.sh | 5 ++ test/mood-line-segment-vc-test.el | 37 +++++++++ test/mood-line-test.el | 121 ++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100755 ert-test.sh create mode 100644 test/mood-line-segment-vc-test.el create mode 100644 test/mood-line-test.el diff --git a/ert-test.sh b/ert-test.sh new file mode 100755 index 0000000..0c9e2e9 --- /dev/null +++ b/ert-test.sh @@ -0,0 +1,5 @@ +#!/bin/bash +emacs --quick --batch --load=ert \ + --load=test/mood-line-test.el \ + --load=test/mood-line-segment-vc-test.el \ + --funcall=ert-run-tests-batch-and-exit diff --git a/test/mood-line-segment-vc-test.el b/test/mood-line-segment-vc-test.el new file mode 100644 index 0000000..15fba75 --- /dev/null +++ b/test/mood-line-segment-vc-test.el @@ -0,0 +1,37 @@ +;;; mood-line-segment-vc-test.el --- Test specifications for mood-line-segment-vc.el -*- lexical-binding: t; -*- + +(add-to-list 'load-path ".") + +(require 'ert) +(require 'mood-line) +(require 'mood-line-segment-vc) + +;;; Code: + +;; -------------------------------------------------------------------------- ;; +;; +;; Helper functions +;; +;; -------------------------------------------------------------------------- ;; + +;; ---------------------------------- ;; +;; mood-line-segment-vc--rev +;; ---------------------------------- ;; + +(ert-deftest --rev/git () + "Current rev should be cleanly extracted from git `vc-mode' status string." + (should (string= (mood-line-segment-vc--rev " Git:main" 'Git) + "main"))) + +(ert-deftest --rev/hg () + "Current rev should be cleanly extracted from hg `vc-mode' status string." + (should (string= (mood-line-segment-vc--rev " Hg:main" 'Hg) + "main"))) + +(ert-deftest --rev/unknown () + "Current rev should be reported as \"???\" when backend is unsupported." + (let ((buffer-file-name "")) + (should (string= (mood-line-segment-vc--rev "" 'SVN) + "???")))) + +;;; mood-line-segment-vc-test.el ends here diff --git a/test/mood-line-test.el b/test/mood-line-test.el new file mode 100644 index 0000000..528daf7 --- /dev/null +++ b/test/mood-line-test.el @@ -0,0 +1,121 @@ +;;; mood-line-test.el --- Test specifications for mood-line.el -*- lexical-binding: t; -*- + +(add-to-list 'load-path ".") + +(require 'ert) +(require 'mood-line) + +;;; Code: + +;; -------------------------------------------------------------------------- ;; +;; +;; Helper functions +;; +;; -------------------------------------------------------------------------- ;; + +;; ---------------------------------- ;; +;; mood-line--get-glyph +;; ---------------------------------- ;; + +(ert-deftest --get-glyph/unicode () + "Glyphs should be fethed from `mood-line-glyph-alist'." + (let ((mood-line-glyph-alist '((:checker-info . ?🛈)))) + (should (string= (mood-line--get-glyph :checker-info) "🛈")))) + +(ert-deftest --get-glyph/fallback-ascii () + "Glyphs should be fetched from `mood-line-glyphs-ascii' as a fallback." + (let ((mood-line-glyph-alist nil)) + (should (string= (mood-line--get-glyph :checker-info) "i")))) + +;; ---------------------------------- ;; +;; mood-line--process-segments +;; ---------------------------------- ;; + +(ert-deftest --process-segments/default () + "`mood-line-format-default' should be processed without error." + (let* ((left (car mood-line-format-default)) + (right (cadr mood-line-format-default)) + (left-str (mood-line--process-segments left)) + (right-str (mood-line--process-segments right))) + (should (> (length left-str) 0)) + (should (> (length right-str) 0)))) + +(ert-deftest --process-segments/strings () + "Literal strings should be concatenated." + (let* ((segments '("ABC" " " "123" " " "XYZ")) + (segments-str (mood-line--process-segments segments))) + (should (string= segments-str "ABC 123 XYZ")))) + +(ert-deftest --process-segments/nil-neighbor-exclusion () + "A nil value should mean skipping evaluation of the following segment." + (let* ((segments '("ABC" nil " " "123" nil " " "XYZ" nil)) + (segments-str (mood-line--process-segments segments))) + (should (string= segments-str "ABC123XYZ")))) + +(ert-deftest --process-segments/functions () + "Functions should be evaluated, and their return values concatenated." + (let* ((segments '((string ?A ?B ?C) + (numberp nil) " " + (number-to-string (+ 100 20 3)) + (identity nil) " " + (concat "X" "Y" "Z"))) + (segments-str (mood-line--process-segments segments))) + (should (string= segments-str "ABC123XYZ")))) + +;; ---------------------------------- ;; +;; mood-line---process-format +;; ---------------------------------- ;; + +(ert-deftest --process-format/default () + "`mood-line-format-default' should be processed without error." + (let* ((format-str (mood-line--process-format mood-line-format-default))) + (should (> (length format-str) 0)))) + +(ert-deftest --process-format/nil-okay () + "Either segment list should process without error if the other list is nil." + (let* ((format-l-nil '(nil ("ABC" nil " " "123" nil " " "XYZ"))) + (format-r-nil '(("ABC" nil " " "123" nil " " "XYZ") nil)) + (format-l-nil-str (mood-line--process-format format-l-nil)) + (format-r-nil-str (mood-line--process-format format-r-nil))) + (should (string-suffix-p "ABC123XYZ" format-l-nil-str)) + (should (string-prefix-p "ABC123XYZ" format-r-nil-str)))) + +;; -------------------------------------------------------------------------- ;; +;; +;; mood-line-mode +;; +;; -------------------------------------------------------------------------- ;; + +;; ---------------------------------- ;; +;; mood-line--activate +;; ---------------------------------- ;; + +(ert-deftest --activate/mode-line-format () + "`mode-line-format' should be set by `mood-line--activate'." + (let* ((setting-val (alist-get 'mode-line-format + mood-line--settings-alist)) + (mood-line--settings-alist `((mode-line-format . ,setting-val))) + (mood-line-format mood-line-format-default) + (mode-line-format '("ABC 123 XYZ")) + (format-str (mood-line--process-format mood-line-format))) + (mood-line--activate) + (should (string= (format-mode-line mode-line-format) + (format-mode-line format-str))))) + +;; ---------------------------------- ;; +;; mood-line--deactivate +;; ---------------------------------- ;; + +(ert-deftest --deactivate/mode-line-format () + "`mode-line-format' should be restored by `mood-line--deactivate'." + (let* ((setting-val (alist-get 'mode-line-format + mood-line--settings-alist)) + (mood-line--settings-alist `((mode-line-format . ,setting-val))) + (mood-line--settings-backup-alist nil) + (mood-line-format mood-line-format-default) + (mode-line-format "ABC 123 XYZ")) + (mood-line--activate) + (mood-line--deactivate) + (should (string= mode-line-format "ABC 123 XYZ")))) + +;;; mood-line-test.el ends here