blob: dd2e4bbed9b03713957176fdc4ab1b6e2b4cbbe8 (
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
138
139
140
141
142
143
144
145
146
|
;;; test-wttrin--cleanup-cache-constants.el --- Tests for cache cleanup constants -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Craig Jennings
;;; Commentary:
;; Unit tests verifying cache cleanup behavior with named constants.
;; These tests verify the behavior before and after refactoring magic numbers.
;;; Code:
(require 'ert)
(require 'wttrin)
;;; Normal Cases - Cache Cleanup Behavior
(ert-deftest test-wttrin--cleanup-cache-removes-oldest-entries ()
"Test that cleanup removes oldest entries when cache exceeds max."
(let ((wttrin--cache (make-hash-table :test 'equal))
(wttrin-cache-max-entries 5)
(now (float-time)))
;; Add 10 entries with sequential timestamps
(dotimes (i 10)
(puthash (format "location-%d" i)
(cons (+ now i) (format "data-%d" i))
wttrin--cache))
;; Cache should have 10 entries
(should (= 10 (hash-table-count wttrin--cache)))
;; Trigger cleanup
(wttrin--cleanup-cache-if-needed)
;; After cleanup, cache should have fewer entries
;; With 10 entries and max of 5, should remove 20% = 2 entries
;; Leaving 8 entries
(should (= 8 (hash-table-count wttrin--cache)))
;; Oldest entries (location-0, location-1) should be removed
(should-not (gethash "location-0" wttrin--cache))
(should-not (gethash "location-1" wttrin--cache))
;; Newer entries should remain
(should (gethash "location-9" wttrin--cache))
(should (gethash "location-8" wttrin--cache))))
(ert-deftest test-wttrin--cleanup-cache-removes-approximately-20-percent ()
"Test that cleanup removes approximately 20% of entries."
(let ((wttrin--cache (make-hash-table :test 'equal))
(wttrin-cache-max-entries 10)
(now (float-time)))
;; Add 20 entries (twice the max)
(dotimes (i 20)
(puthash (format "loc-%d" i)
(cons (+ now i) (format "data-%d" i))
wttrin--cache))
(should (= 20 (hash-table-count wttrin--cache)))
;; Trigger cleanup - should remove 20% of 20 = 4 entries
(wttrin--cleanup-cache-if-needed)
;; Should have 16 entries remaining (20 - 4)
(should (= 16 (hash-table-count wttrin--cache)))))
(ert-deftest test-wttrin--cleanup-cache-no-action-when-under-max ()
"Test that cleanup does nothing when cache is under max."
(let ((wttrin--cache (make-hash-table :test 'equal))
(wttrin-cache-max-entries 10)
(now (float-time)))
;; Add 5 entries (under max of 10)
(dotimes (i 5)
(puthash (format "location-%d" i)
(cons (+ now i) (format "data-%d" i))
wttrin--cache))
(should (= 5 (hash-table-count wttrin--cache)))
;; Trigger cleanup
(wttrin--cleanup-cache-if-needed)
;; Should still have 5 entries (no cleanup needed)
(should (= 5 (hash-table-count wttrin--cache)))))
;;; Boundary Cases
(ert-deftest test-wttrin--cleanup-cache-exactly-at-max ()
"Test that cleanup does nothing when cache is exactly at max."
(let ((wttrin--cache (make-hash-table :test 'equal))
(wttrin-cache-max-entries 5)
(now (float-time)))
;; Add exactly 5 entries (at max)
(dotimes (i 5)
(puthash (format "location-%d" i)
(cons (+ now i) (format "data-%d" i))
wttrin--cache))
(should (= 5 (hash-table-count wttrin--cache)))
;; Trigger cleanup - should do nothing since not > max
(wttrin--cleanup-cache-if-needed)
;; Should still have 5 entries
(should (= 5 (hash-table-count wttrin--cache)))))
(ert-deftest test-wttrin--cleanup-cache-one-over-max ()
"Test cleanup when cache has just one entry over max."
(let ((wttrin--cache (make-hash-table :test 'equal))
(wttrin-cache-max-entries 5)
(now (float-time)))
;; Add 6 entries (1 over max)
(dotimes (i 6)
(puthash (format "location-%d" i)
(cons (+ now i) (format "data-%d" i))
wttrin--cache))
(should (= 6 (hash-table-count wttrin--cache)))
;; Trigger cleanup - should remove 20% of 6 = 1.2 = 1 entry (floor)
(wttrin--cleanup-cache-if-needed)
;; Should have 5 entries remaining
(should (= 5 (hash-table-count wttrin--cache)))
;; Oldest entry (location-0) should be removed
(should-not (gethash "location-0" wttrin--cache))))
;;; Tests for Mode-Line Startup Delay (after refactoring)
(ert-deftest test-wttrin-mode-line-startup-delay-exists ()
"Test that wttrin-mode-line-startup-delay defcustom exists after refactoring."
;; This test will fail initially, then pass after refactoring
(should (boundp 'wttrin-mode-line-startup-delay)))
(ert-deftest test-wttrin-mode-line-startup-delay-is-number ()
"Test that startup delay is a number."
(should (numberp wttrin-mode-line-startup-delay)))
(ert-deftest test-wttrin-mode-line-startup-delay-reasonable-range ()
"Test that startup delay default value is in reasonable range (1-10 seconds)."
;; Check the defcustom's standard value, not current runtime value
;; (other tests may set it to 0 for faster testing)
(let ((default-value (eval (car (get 'wttrin-mode-line-startup-delay 'standard-value)))))
(should (>= default-value 1))
(should (<= default-value 10))))
(provide 'test-wttrin--cleanup-cache-constants)
;;; test-wttrin--cleanup-cache-constants.el ends here
|