blob: da00954c46a5d360ef7c3f52361834026cd307b5 (
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
|
;;; test-host-environment--env-laptop-p.el --- Tests for env-laptop-p / env-desktop-p -*- lexical-binding: t; -*-
;;; Commentary:
;; Tests for `env-laptop-p' and the inverse `env-desktop-p'. On Linux,
;; the function delegates to `env--power-supply-has-battery-p' against
;; /sys/class/power_supply. On other platforms, it reads the battery
;; status char. Tests mock `system-type' and the helpers at their
;; boundary so the predicate's dispatch logic is exercised without
;; touching real system files.
;;; Code:
(require 'ert)
(require 'cl-lib)
(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(require 'host-environment)
;; Forward-declare and initialize to nil so let-binding inside tests
;; sees this as special under lexical-binding, and so cl-letf's
;; symbol-value place can read the old value without hitting void-variable.
(defvar battery-status-function nil)
;;; env-laptop-p — Linux dispatch
(ert-deftest test-host-environment-laptop-p-linux-with-battery ()
"Normal: on Linux, env-laptop-p delegates to power-supply helper."
(let ((system-type 'gnu/linux))
(cl-letf (((symbol-function 'env--power-supply-has-battery-p)
(lambda (_dir) t)))
(should (env-laptop-p)))))
(ert-deftest test-host-environment-laptop-p-linux-without-battery ()
"Boundary: on Linux, env-laptop-p returns nil when no BAT* dir is found."
(let ((system-type 'gnu/linux))
(cl-letf (((symbol-function 'env--power-supply-has-battery-p)
(lambda (_dir) nil)))
(should-not (env-laptop-p)))))
(ert-deftest test-host-environment-laptop-p-linux-passes-correct-dir ()
"Boundary: on Linux, the power-supply helper receives /sys/class/power_supply."
(let ((system-type 'gnu/linux)
captured)
(cl-letf (((symbol-function 'env--power-supply-has-battery-p)
(lambda (dir) (setq captured dir) t)))
(env-laptop-p)
(should (equal captured "/sys/class/power_supply")))))
;;; env-laptop-p — non-Linux dispatch via battery-format
(ert-deftest test-host-environment-laptop-p-non-linux-with-battery-char ()
"Normal: on non-Linux, env-laptop-p reads the battery status char."
(let ((system-type 'darwin))
(cl-letf (((symbol-function 'require)
(lambda (feat &rest _) (eq feat 'battery)))
((symbol-function 'battery-format)
(lambda (_format _data) "+"))
((symbol-value 'battery-status-function) (lambda () 'fake-data)))
(should (env-laptop-p)))))
(ert-deftest test-host-environment-laptop-p-non-linux-no-battery-char ()
"Boundary: on non-Linux, an N/A char means no battery."
(let ((system-type 'darwin))
(cl-letf (((symbol-function 'require)
(lambda (feat &rest _) (eq feat 'battery)))
((symbol-function 'battery-format)
(lambda (_format _data) "N/A"))
((symbol-value 'battery-status-function) (lambda () 'fake-data)))
(should-not (env-laptop-p)))))
(ert-deftest test-host-environment-laptop-p-non-linux-no-battery-feature ()
"Error: on non-Linux without the battery feature, env-laptop-p returns nil."
(let ((system-type 'darwin))
(cl-letf (((symbol-function 'require)
(lambda (_feat &rest _) nil))
((symbol-value 'battery-status-function) nil))
(should-not (env-laptop-p)))))
;;; env-desktop-p — inverse of env-laptop-p
(ert-deftest test-host-environment-desktop-p-true-when-not-laptop ()
"Normal: env-desktop-p is t when env-laptop-p is nil."
(cl-letf (((symbol-function 'env-laptop-p) (lambda () nil)))
(should (env-desktop-p))))
(ert-deftest test-host-environment-desktop-p-nil-when-laptop ()
"Boundary: env-desktop-p is nil when env-laptop-p is non-nil."
(cl-letf (((symbol-function 'env-laptop-p) (lambda () t)))
(should-not (env-desktop-p))))
(provide 'test-host-environment--env-laptop-p)
;;; test-host-environment--env-laptop-p.el ends here
|