From 130bbc076b17f8a5a275bea00e5aa2a354baad49 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sat, 4 Apr 2026 12:42:46 -0500 Subject: refactor: extract mode-line icon and string helpers Add wttrin--make-emoji-icon and wttrin--set-mode-line-string to eliminate three near-identical propertize blocks across update-placeholder-error, set-placeholder, and update-display. Reduces wttrin--mode-line-update-display from 40 lines to 25. --- tests/test-wttrin--mode-line-helpers.el | 128 ++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 tests/test-wttrin--mode-line-helpers.el (limited to 'tests/test-wttrin--mode-line-helpers.el') diff --git a/tests/test-wttrin--mode-line-helpers.el b/tests/test-wttrin--mode-line-helpers.el new file mode 100644 index 0000000..b4c6b4e --- /dev/null +++ b/tests/test-wttrin--mode-line-helpers.el @@ -0,0 +1,128 @@ +;;; test-wttrin--mode-line-helpers.el --- Tests for mode-line helper functions -*- lexical-binding: t; -*- + +;; Copyright (C) 2025 Craig Jennings + +;;; Commentary: + +;; Unit tests for wttrin--make-emoji-icon and wttrin--set-mode-line-string. +;; These helpers extract the common mode-line icon creation and string +;; assignment pattern used by placeholder, error, and display functions. + +;;; Code: + +(require 'ert) +(require 'wttrin) +(require 'testutil-wttrin) + +;;; Setup and Teardown + +(defun test-wttrin--mode-line-helpers-setup () + "Setup for mode-line helper tests." + (testutil-wttrin-setup) + (setq wttrin-mode-line-string nil)) + +(defun test-wttrin--mode-line-helpers-teardown () + "Teardown for mode-line helper tests." + (testutil-wttrin-teardown) + (setq wttrin-mode-line-string nil)) + +;;; -------------------------------------------------------------------------- +;;; wttrin--make-emoji-icon +;;; -------------------------------------------------------------------------- + +;;; Normal Cases + +(ert-deftest test-wttrin--make-emoji-icon-normal-without-font () + "Without emoji font configured, should return the plain emoji string." + (let ((wttrin-mode-line-emoji-font nil)) + (should (equal (wttrin--make-emoji-icon "☀") "☀")))) + +(ert-deftest test-wttrin--make-emoji-icon-normal-with-font () + "With emoji font configured, should apply font family face property." + (let ((wttrin-mode-line-emoji-font "Noto Color Emoji")) + (let ((result (wttrin--make-emoji-icon "☀"))) + (should (stringp result)) + (let ((face (get-text-property 0 'face result))) + (should (equal (plist-get face :family) "Noto Color Emoji")) + (should (equal (plist-get face :height) 1.0)))))) + +(ert-deftest test-wttrin--make-emoji-icon-normal-with-foreground () + "Foreground color should be applied when specified." + (let ((wttrin-mode-line-emoji-font nil)) + (let ((result (wttrin--make-emoji-icon "☀" "gray60"))) + (let ((face (get-text-property 0 'face result))) + (should (equal (plist-get face :foreground) "gray60")))))) + +(ert-deftest test-wttrin--make-emoji-icon-normal-with-font-and-foreground () + "Both font and foreground should be applied together." + (let ((wttrin-mode-line-emoji-font "Noto Color Emoji")) + (let ((result (wttrin--make-emoji-icon "⏳" "gray60"))) + (let ((face (get-text-property 0 'face result))) + (should (equal (plist-get face :family) "Noto Color Emoji")) + (should (equal (plist-get face :foreground) "gray60")))))) + +;;; Boundary Cases + +(ert-deftest test-wttrin--make-emoji-icon-boundary-nil-foreground-no-color () + "Nil foreground should not add any :foreground property when no font." + (let ((wttrin-mode-line-emoji-font nil)) + (let ((result (wttrin--make-emoji-icon "☀" nil))) + ;; Without font or foreground, should be plain string + (should (equal result "☀"))))) + +;;; -------------------------------------------------------------------------- +;;; wttrin--set-mode-line-string +;;; -------------------------------------------------------------------------- + +;;; Normal Cases + +(ert-deftest test-wttrin--set-mode-line-string-normal-sets-string () + "Should set wttrin-mode-line-string to a non-nil propertized value." + (test-wttrin--mode-line-helpers-setup) + (unwind-protect + (progn + (wttrin--set-mode-line-string "X" "tooltip text") + (should wttrin-mode-line-string)) + (test-wttrin--mode-line-helpers-teardown))) + +(ert-deftest test-wttrin--set-mode-line-string-normal-includes-icon () + "The icon text should appear in the mode-line string." + (test-wttrin--mode-line-helpers-setup) + (unwind-protect + (progn + (wttrin--set-mode-line-string "⏳" "tip") + (should (string-match-p "⏳" (substring-no-properties wttrin-mode-line-string)))) + (test-wttrin--mode-line-helpers-teardown))) + +(ert-deftest test-wttrin--set-mode-line-string-normal-has-tooltip () + "The help-echo property should contain the tooltip text." + (test-wttrin--mode-line-helpers-setup) + (unwind-protect + (progn + (wttrin--set-mode-line-string "X" "Weather is sunny") + (should (equal (get-text-property 0 'help-echo wttrin-mode-line-string) + "Weather is sunny"))) + (test-wttrin--mode-line-helpers-teardown))) + +(ert-deftest test-wttrin--set-mode-line-string-normal-has-keymap () + "The local-map property should be the mode-line keymap." + (test-wttrin--mode-line-helpers-setup) + (unwind-protect + (progn + (wttrin--set-mode-line-string "X" "tip") + (should (eq (get-text-property 0 'local-map wttrin-mode-line-string) + wttrin--mode-line-map))) + (test-wttrin--mode-line-helpers-teardown))) + +(ert-deftest test-wttrin--set-mode-line-string-normal-has-mouse-face () + "The mouse-face property should highlight on hover." + (test-wttrin--mode-line-helpers-setup) + (unwind-protect + (progn + (wttrin--set-mode-line-string "X" "tip") + (should (equal (get-text-property 0 'mouse-face wttrin-mode-line-string) + 'mode-line-highlight))) + (test-wttrin--mode-line-helpers-teardown))) + +(provide 'test-wttrin--mode-line-helpers) +;;; test-wttrin--mode-line-helpers.el ends here -- cgit v1.2.3