blob: 31e0e6cc87c01743157cc7b91c734b6613818dbe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
;;; 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-under-persist ()
"The default theme file should live under `persist/' inside `user-emacs-directory'."
(should (equal theme-file
(expand-file-name "persist/emacs-theme" user-emacs-directory))))
(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
|