-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.el
191 lines (156 loc) · 6.13 KB
/
test.el
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
;;; circadian.el-test.el --- Tests for circadian.el
;;; Commentary:
;;; Code:
(require 'cl-lib)
(require 'el-mock)
(require 'benchmark)
(load (expand-file-name "circadian.el" default-directory))
(ert-deftest test-circadian-filter-and-activate-themes ()
"Test filtering of `circadian-themes' list`."
(setq circadian-themes '(("5:01" . wombat)
("14:47" . tango)
("23:59" . adwaita)))
(setq circadian-themes-parsed (circadian-themes-parse))
;; Before 5:01
(let ((time-now '(4 10)))
(should (equal 0 (length (circadian-filter-inactivate-themes
circadian-themes-parsed
time-now)))))
(with-mock
(stub circadian-now-time => '(5 0 0))
(circadian-setup)
(should (equal (list 'adwaita) custom-enabled-themes)))
;; After 5:01, before 14:47
(let ((time-now '(5 2)))
(should (equal 1 (length (circadian-filter-inactivate-themes
circadian-themes-parsed
time-now)))))
(with-mock
(stub circadian-now-time => '(5 2 0))
(circadian-setup)
(should (equal (list 'wombat) custom-enabled-themes)))
;; After 14:47, before 23:59
(let ((time-now '(14 47)))
(should (equal 2 (length (circadian-filter-inactivate-themes
circadian-themes-parsed
time-now)))))
(with-mock
(stub circadian-now-time => '(14 47 1))
(circadian-setup)
(should (equal (list 'tango) custom-enabled-themes)))
;; After 23:59
(let ((time-now '(23 59)))
(should (equal 3 (length (circadian-filter-inactivate-themes
circadian-themes-parsed
time-now)))))
(with-mock
(stub circadian-now-time => '(23 59 15))
(circadian-setup)
(should (equal (list 'adwaita) custom-enabled-themes)))
;; Surpassing midnight
(let ((time-now '(0 2)))
(should (equal 0 (length (circadian-filter-inactivate-themes
circadian-themes-parsed
time-now)))))
(with-mock
(stub circadian-now-time => '(0 2 10))
(circadian-setup)
(should (equal (list 'adwaita) custom-enabled-themes))))
(ert-deftest test-circadian-setup ()
"Test `circadian-setup'."
(setq circadian-themes '(("7:00" . wombat)
("16:00" . tango)))
(with-mock
(stub circadian-now-time => '(7 21 0))
(circadian-setup)
(should (equal (list 'wombat) custom-enabled-themes)))
(with-mock
(stub circadian-now-time => '(17 0 0))
(circadian-setup)
(should (equal (list 'tango) custom-enabled-themes))))
(ert-deftest test-circadian-sunrise-sunset ()
"Test :sunrise and :sunset keywords for theme switching.
@TODO currently failing, needs a fix"
(with-mock
(setq calendar-latitude 49.329896)
(setq calendar-longitude 8.570925)
(setq circadian-themes '((:sunrise . adwaita)
(:sunset . wombat)))
(stub circadian-now-time => '(14 21 0))
(circadian-setup)
(should (equal 'adwaita (cl-first custom-enabled-themes)))
(stub circadian-now-time => '(22 30 0))
(circadian-setup)
(should (equal 'wombat (cl-first custom-enabled-themes)))))
(ert-deftest test-circadian-sunrise-sunset-timezones ()
"Test :sunrise and :sunset for different time zones (TODO)."
;; (print "→ TEST: sunrise/sunset (New York Time Zone)")
(setq calendar-latitude 40.712775)
(setq calendar-longitude -74.005973)
(setq calendar-time-zone -360)
(setq circadian-themes '((:sunrise . wombat)
(:sunset . adwaita))))
(ert-deftest test-circadian-time-comparisons ()
"Test comparison of time strings.
Time A: 17:59
Time B: 17:58
B should be earlier than A
=> `circadian-a-earlier-b-p' should return t."
;; (
(should (equal t (circadian-a-earlier-b-p '(7 50) '(7 51))))
(should (equal nil (circadian-a-earlier-b-p '(19 20) '(19 19))))
(should (equal t (circadian-a-earlier-b-p '(20 20) '(20 20))))
(with-mock
;; tomorrow
(stub decode-time => '(0 30 16 28 4 2024 nil -1 nil))
(let* ((next-time (decode-time (circadian-encode-time 0 0)))
(next-day (nth 3 next-time))
(next-hour (nth 2 next-time)))
;; today
(stub decode-time => '(0 30 14 28 4 2024 nil -1 nil))
(let* ((now (decode-time))
(day (nth 3 now))
(hour (nth 2 now)))
(should (> next-hour hour))
(should (equal day next-day)))))
(with-mock
;; tomorrow
(stub decode-time => '(0 30 7 29 4 2024 nil -1 nil))
(let* ((next-time (decode-time (circadian-encode-time 0 0)))
(next-day (nth 3 next-time))
(next-hour (nth 2 next-time)))
;; today
(stub decode-time => '(0 30 14 28 4 2024 nil -1 nil))
(let* ((now (decode-time))
(day (nth 3 now))
(hour (nth 2 now)))
(should (< next-hour hour))
(should (equal (+ 1 day) next-day))))))
(ert-deftest test-circadian-setup-benchmark ()
"Benchmark (circadian-setup)."
(setq calendar-latitude 49)
(setq calendar-longitude 5)
(setq circadian-themes '((:sunrise . wombat)
(:sunset . tango)))
(let ((elapsed (benchmark-elapse (circadian-setup))))
(should (equal t (< elapsed 0.05)))
(print (concat "(circadian-setup) took " (format "%.10f seconds" elapsed)))))
(ert-deftest test-circadian-invalid-solar-sunrise-sunset ()
:expected-result :failed
"Test to address issue #27.
https://github.com/guidoschmidt/circadian.el/issues/27"
(setq calendar-latitude 79.482623)
(setq calendar-longitude 5.318703)
(setq circadian-themes '((:sunrise . adwaita)
(:sunset . tango)))
(circadian-setup))
(defvar test-order '(member
test-circadian-filter-and-activate-themes
test-circadian-setup
test-circadian-sunrise-sunset
test-circadian-time-comparisons
test-circadian-setup-benchmark
test-circadian-invalid-solar-sunrise-sunset
test-circadian-sunrise-sunset-timezones))
(provide 'circadian.el-test)
;;; circadian.el-test.el ends here