diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-03 23:30:28 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-03 23:41:48 -0500 |
| commit | 9c7654e0e0f4777176ad5a9ea30075431e931c02 (patch) | |
| tree | 4442c7e65a646bb83efc746825c5e9cd3c830086 /tests/test-ui-theme-persistence.el | |
| parent | b2d44d70374db0ab801d18f8c04d1b021396ea46 (diff) | |
| download | dotemacs-9c7654e0e0f4777176ad5a9ea30075431e931c02.tar.gz dotemacs-9c7654e0e0f4777176ad5a9ea30075431e931c02.zip | |
refactor: move and test theme persistence behavior
Diffstat (limited to 'tests/test-ui-theme-persistence.el')
| -rw-r--r-- | tests/test-ui-theme-persistence.el | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/tests/test-ui-theme-persistence.el b/tests/test-ui-theme-persistence.el new file mode 100644 index 00000000..86ed9de9 --- /dev/null +++ b/tests/test-ui-theme-persistence.el @@ -0,0 +1,138 @@ +;;; test-ui-theme-persistence.el --- Tests for UI theme persistence -*- lexical-binding: t; -*- + +;;; Commentary: +;; Smoke and unit coverage for ui-theme.el persistence behavior. + +;;; Code: + +(require 'ert) +(require 'cl-lib) + +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) + +(require 'ui-theme) + +(ert-deftest test-ui-theme-default-theme-file-is-emacs-dotfile () + "The default theme file should live under `user-emacs-directory'." + (should (equal theme-file + (expand-file-name ".emacs-theme" user-emacs-directory))) + (should-not (string-match-p "emacs-theme\\.persist\\'" theme-file))) + +(ert-deftest test-ui-theme-read-missing-file-returns-nil () + "Reading a missing theme file should return nil." + (let ((theme-file (expand-file-name "missing-theme" temporary-file-directory))) + (should-not (cj/theme-read-file-contents theme-file)))) + +(ert-deftest test-ui-theme-write-file-contents-writes-content () + "Writing theme content should persist exactly that content." + (let ((file (make-temp-file "ui-theme-write-"))) + (unwind-protect + (progn + (should (cj/theme-write-file-contents "modus-vivendi" file)) + (should (equal (cj/theme-read-file-contents file) + "modus-vivendi"))) + (delete-file file)))) + +(ert-deftest test-ui-theme-write-file-contents-uses-write-region () + "Theme persistence should write directly instead of visiting the file." + (let ((file (make-temp-file "ui-theme-write-region-")) + write-region-args + write-file-called) + (unwind-protect + (cl-letf (((symbol-function 'write-region) + (lambda (&rest args) + (setq write-region-args args) + nil)) + ((symbol-function 'write-file) + (lambda (&rest _args) + (setq write-file-called t) + (error "write-file should not be used")))) + (should (cj/theme-write-file-contents "dupre" file))) + (delete-file file)) + (should (equal (list (car write-region-args) + (cadr write-region-args) + (nth 2 write-region-args)) + (list "dupre" nil file))) + (should-not write-file-called))) + +(ert-deftest test-ui-theme-load-valid-persisted-theme () + "A valid persisted theme should disable current themes and load that theme." + (let ((file (make-temp-file "ui-theme-valid-")) + (custom-enabled-themes '(old-theme)) + disabled + loaded) + (unwind-protect + (progn + (cj/theme-write-file-contents "modus-vivendi" file) + (let ((theme-file file)) + (cl-letf (((symbol-function 'disable-theme) + (lambda (theme) (push theme disabled))) + ((symbol-function 'load-theme) + (lambda (theme &optional _no-confirm _no-enable) + (push theme loaded)))) + (cj/load-theme-from-file))) + (should (equal disabled '(old-theme))) + (should (equal loaded '(modus-vivendi)))) + (delete-file file)))) + +(ert-deftest test-ui-theme-load-invalid-theme-falls-back () + "An invalid persisted theme should disable current themes and load fallback." + (let ((file (make-temp-file "ui-theme-invalid-")) + (fallback-theme-name "modus-vivendi") + (custom-enabled-themes '(old-theme)) + disabled + loaded) + (unwind-protect + (progn + (cj/theme-write-file-contents "missing-theme" file) + (let ((theme-file file)) + (cl-letf (((symbol-function 'disable-theme) + (lambda (theme) (push theme disabled))) + ((symbol-function 'load-theme) + (lambda (theme &optional _no-confirm _no-enable) + (push theme loaded) + (when (eq theme 'missing-theme) + (error "missing theme"))))) + (cj/load-theme-from-file))) + (should (equal disabled '(old-theme))) + (should (equal loaded '(modus-vivendi missing-theme)))) + (delete-file file)))) + +(ert-deftest test-ui-theme-load-missing-file-loads-fallback () + "A missing theme file should disable current themes and load fallback." + (let ((theme-file (expand-file-name "missing-theme" temporary-file-directory)) + (fallback-theme-name "modus-vivendi") + (custom-enabled-themes '(old-theme)) + disabled + loaded) + (cl-letf (((symbol-function 'disable-theme) + (lambda (theme) (push theme disabled))) + ((symbol-function 'load-theme) + (lambda (theme &optional _no-confirm _no-enable) + (push theme loaded)))) + (cj/load-theme-from-file)) + (should (equal disabled '(old-theme))) + (should (equal loaded '(modus-vivendi))))) + +(ert-deftest test-ui-theme-load-literal-nil-disables-themes () + "A persisted literal nil should disable current themes and load nothing." + (let ((file (make-temp-file "ui-theme-nil-")) + (custom-enabled-themes '(old-theme newer-theme)) + disabled + loaded) + (unwind-protect + (progn + (cj/theme-write-file-contents "nil" file) + (let ((theme-file file)) + (cl-letf (((symbol-function 'disable-theme) + (lambda (theme) (push theme disabled))) + ((symbol-function 'load-theme) + (lambda (theme &optional _no-confirm _no-enable) + (push theme loaded)))) + (cj/load-theme-from-file))) + (should (equal disabled '(newer-theme old-theme))) + (should-not loaded)) + (delete-file file)))) + +(provide 'test-ui-theme-persistence) +;;; test-ui-theme-persistence.el ends here |
