diff options
| author | Craig Jennings <c@cjennings.net> | 2026-04-30 08:25:45 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-04-30 09:13:43 -0500 |
| commit | 21d5a737f0278baeafd4d72e59cc9505043d91a9 (patch) | |
| tree | 7d7ce883c2ec9594d4033ef535b7fad4020c4daa /tests/test-host-environment--env-laptop-p.el | |
| parent | 17e2366180838b7e170230ff881cd3406be84a70 (diff) | |
| download | dotemacs-21d5a737f0278baeafd4d72e59cc9505043d91a9.tar.gz dotemacs-21d5a737f0278baeafd4d72e59cc9505043d91a9.zip | |
test(host-environment): cover laptop/desktop, platform, display, timezone predicates
Four new test files extending the existing test-host-environment.el
(which already covered the two battery helpers).
- platform-predicates: env-linux-p, env-bsd-p, env-macos-p, env-windows-p
walked across every supported system-type value. 8 tests.
- display-predicates: env-x-p, env-x11-p, env-wayland-p, env-terminal-p,
env-gui-p exercised under every relevant combination of window-system,
WAYLAND_DISPLAY, and display-graphic-p. 13 tests.
- env-laptop-p: composition over the helpers, with Linux dispatch
isolated from non-Linux dispatch via system-type binding. 8 tests
including env-desktop-p as the inverse. battery-status-function is
forward-declared in this test file (initialized to nil) so cl-letf's
symbol-value place can read the prior value without hitting
void-variable.
- detect-system-timezone: the four-method priority chain. Mocks
cj/match-localtime-to-zoneinfo and getenv at the boundary; uses
cl-letf on file-exists-p / insert-file-contents to exercise the
/etc/timezone fall-through without touching real system files.
5 tests.
34 new tests for host-environment, all passing. Full suite green.
Diffstat (limited to 'tests/test-host-environment--env-laptop-p.el')
| -rw-r--r-- | tests/test-host-environment--env-laptop-p.el | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/tests/test-host-environment--env-laptop-p.el b/tests/test-host-environment--env-laptop-p.el new file mode 100644 index 00000000..da00954c --- /dev/null +++ b/tests/test-host-environment--env-laptop-p.el @@ -0,0 +1,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 |
