1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-22 07:09:54 +00:00
emacs/test/lisp/battery-tests.el

155 lines
6.9 KiB
EmacsLisp
Raw Normal View History

;;; battery-tests.el --- tests for battery.el -*- lexical-binding: t -*-
2024-01-02 01:47:10 +00:00
;; Copyright (C) 2020-2024 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Code:
(require 'battery)
(ert-deftest battery-linux-proc-apm-regexp ()
Various battery.el improvements (bug#41808) * lisp/battery.el: Mention BSD support in Commentary. Don't load preloaded lisp/emacs-lisp/timer.el. (battery--files): New function. (battery--find-linux-sysfs-batteries): Use it and make fewer syscalls. (battery-status-function): Perform GNU/Linux checks in increasing order of obsolescence: sysfs, ACPI, and then APM. Simplify Darwin check. Add :version tag now that battery-upower is the default. (battery-echo-area-format, battery-mode-line-format): Mention %s. (battery-load-low, battery-load-critical): New faces. (battery-update): Display battery-mode-line-format even if percentage is N/A. Apply faces battery-load-low or battery-load-critical according to the percentage, but append them so they don't override user customizations. Update all mode lines since we are in global-mode-string. (battery-linux-proc-apm-regexp): Mark as obsolete, replacing with... (battery--linux-proc-apm): ...this new rx definition. (battery-linux-proc-apm): Use it. Fix indentation. Simplify. (battery--acpi-rate, battery--acpi-capacity): New rx definitions. (battery-linux-proc-acpi): Use them. Fix pathological whitespace regexps. Simplify. (battery-linux-sysfs): Fix docstring and indentation. Reduce number of file searches. Simplify. (battery-bsd-apm): Fix docstring. Simplify. (battery-pmset): Fix docstring. Simplify ID regexp. * lisp/emacs-lisp/rx.el (rx-define): Indent as a defun. * test/lisp/battery-tests.el (battery-linux-proc-apm-regexp): Test new battery--linux-proc-apm rx definition. (battery-acpi-rate-regexp, battery-acpi-capacity-regexp): New tests.
2020-06-11 12:48:37 +00:00
"Test `rx' definition `battery--linux-proc-apm'."
(let ((str "1.16 1.2 0x07 0x01 0xff 0x80 -1% -1 ?"))
Various battery.el improvements (bug#41808) * lisp/battery.el: Mention BSD support in Commentary. Don't load preloaded lisp/emacs-lisp/timer.el. (battery--files): New function. (battery--find-linux-sysfs-batteries): Use it and make fewer syscalls. (battery-status-function): Perform GNU/Linux checks in increasing order of obsolescence: sysfs, ACPI, and then APM. Simplify Darwin check. Add :version tag now that battery-upower is the default. (battery-echo-area-format, battery-mode-line-format): Mention %s. (battery-load-low, battery-load-critical): New faces. (battery-update): Display battery-mode-line-format even if percentage is N/A. Apply faces battery-load-low or battery-load-critical according to the percentage, but append them so they don't override user customizations. Update all mode lines since we are in global-mode-string. (battery-linux-proc-apm-regexp): Mark as obsolete, replacing with... (battery--linux-proc-apm): ...this new rx definition. (battery-linux-proc-apm): Use it. Fix indentation. Simplify. (battery--acpi-rate, battery--acpi-capacity): New rx definitions. (battery-linux-proc-acpi): Use them. Fix pathological whitespace regexps. Simplify. (battery-linux-sysfs): Fix docstring and indentation. Reduce number of file searches. Simplify. (battery-bsd-apm): Fix docstring. Simplify. (battery-pmset): Fix docstring. Simplify ID regexp. * lisp/emacs-lisp/rx.el (rx-define): Indent as a defun. * test/lisp/battery-tests.el (battery-linux-proc-apm-regexp): Test new battery--linux-proc-apm rx definition. (battery-acpi-rate-regexp, battery-acpi-capacity-regexp): New tests.
2020-06-11 12:48:37 +00:00
(should (string-match (rx battery--linux-proc-apm) str))
(should (equal (match-string 0 str) str))
(should (equal (match-string 1 str) "1.16"))
(should (equal (match-string 2 str) "1.2"))
(should (equal (match-string 3 str) "07"))
(should (equal (match-string 4 str) "01"))
(should (equal (match-string 5 str) "ff"))
(should (equal (match-string 6 str) "80"))
(should (equal (match-string 7 str) "-1"))
(should (equal (match-string 8 str) "-1"))
(should (equal (match-string 9 str) "?")))
(let ((str "1.16 1.2 0x03 0x00 0x00 0x01 99% 1792 min"))
Various battery.el improvements (bug#41808) * lisp/battery.el: Mention BSD support in Commentary. Don't load preloaded lisp/emacs-lisp/timer.el. (battery--files): New function. (battery--find-linux-sysfs-batteries): Use it and make fewer syscalls. (battery-status-function): Perform GNU/Linux checks in increasing order of obsolescence: sysfs, ACPI, and then APM. Simplify Darwin check. Add :version tag now that battery-upower is the default. (battery-echo-area-format, battery-mode-line-format): Mention %s. (battery-load-low, battery-load-critical): New faces. (battery-update): Display battery-mode-line-format even if percentage is N/A. Apply faces battery-load-low or battery-load-critical according to the percentage, but append them so they don't override user customizations. Update all mode lines since we are in global-mode-string. (battery-linux-proc-apm-regexp): Mark as obsolete, replacing with... (battery--linux-proc-apm): ...this new rx definition. (battery-linux-proc-apm): Use it. Fix indentation. Simplify. (battery--acpi-rate, battery--acpi-capacity): New rx definitions. (battery-linux-proc-acpi): Use them. Fix pathological whitespace regexps. Simplify. (battery-linux-sysfs): Fix docstring and indentation. Reduce number of file searches. Simplify. (battery-bsd-apm): Fix docstring. Simplify. (battery-pmset): Fix docstring. Simplify ID regexp. * lisp/emacs-lisp/rx.el (rx-define): Indent as a defun. * test/lisp/battery-tests.el (battery-linux-proc-apm-regexp): Test new battery--linux-proc-apm rx definition. (battery-acpi-rate-regexp, battery-acpi-capacity-regexp): New tests.
2020-06-11 12:48:37 +00:00
(should (string-match (rx battery--linux-proc-apm) str))
(should (equal (match-string 0 str) str))
(should (equal (match-string 1 str) "1.16"))
(should (equal (match-string 2 str) "1.2"))
(should (equal (match-string 3 str) "03"))
(should (equal (match-string 4 str) "00"))
(should (equal (match-string 5 str) "00"))
(should (equal (match-string 6 str) "01"))
(should (equal (match-string 7 str) "99"))
(should (equal (match-string 8 str) "1792"))
(should (equal (match-string 9 str) "min"))))
Various battery.el improvements (bug#41808) * lisp/battery.el: Mention BSD support in Commentary. Don't load preloaded lisp/emacs-lisp/timer.el. (battery--files): New function. (battery--find-linux-sysfs-batteries): Use it and make fewer syscalls. (battery-status-function): Perform GNU/Linux checks in increasing order of obsolescence: sysfs, ACPI, and then APM. Simplify Darwin check. Add :version tag now that battery-upower is the default. (battery-echo-area-format, battery-mode-line-format): Mention %s. (battery-load-low, battery-load-critical): New faces. (battery-update): Display battery-mode-line-format even if percentage is N/A. Apply faces battery-load-low or battery-load-critical according to the percentage, but append them so they don't override user customizations. Update all mode lines since we are in global-mode-string. (battery-linux-proc-apm-regexp): Mark as obsolete, replacing with... (battery--linux-proc-apm): ...this new rx definition. (battery-linux-proc-apm): Use it. Fix indentation. Simplify. (battery--acpi-rate, battery--acpi-capacity): New rx definitions. (battery-linux-proc-acpi): Use them. Fix pathological whitespace regexps. Simplify. (battery-linux-sysfs): Fix docstring and indentation. Reduce number of file searches. Simplify. (battery-bsd-apm): Fix docstring. Simplify. (battery-pmset): Fix docstring. Simplify ID regexp. * lisp/emacs-lisp/rx.el (rx-define): Indent as a defun. * test/lisp/battery-tests.el (battery-linux-proc-apm-regexp): Test new battery--linux-proc-apm rx definition. (battery-acpi-rate-regexp, battery-acpi-capacity-regexp): New tests.
2020-06-11 12:48:37 +00:00
(ert-deftest battery-acpi-rate-regexp ()
"Test `rx' definition `battery--acpi-rate'."
(let ((str "01 mA"))
(should (string-match (rx (battery--acpi-rate)) str))
(should (equal (match-string 0 str) str))
(should (equal (match-string 1 str) "01"))
(should (equal (match-string 2 str) "mA")))
(let ((str "23 mW"))
(should (string-match (rx (battery--acpi-rate)) str))
(should (equal (match-string 0 str) str))
(should (equal (match-string 1 str) "23"))
(should (equal (match-string 2 str) "mW")))
(let ((str "23 mWh"))
(should (string-match (rx (battery--acpi-rate)) str))
(should (equal (match-string 0 str) "23 mW"))
(should (equal (match-string 1 str) "23"))
(should (equal (match-string 2 str) "mW")))
(should-not (string-match (rx (battery--acpi-rate) eos) "45 mWh")))
(ert-deftest battery-acpi-capacity-regexp ()
"Test `rx' definition `battery--acpi-capacity'."
(let ((str "01 mAh"))
(should (string-match (rx battery--acpi-capacity) str))
(should (equal (match-string 0 str) str))
(should (equal (match-string 1 str) "01"))
(should (equal (match-string 2 str) "mAh")))
(let ((str "23 mWh"))
(should (string-match (rx battery--acpi-capacity) str))
(should (equal (match-string 0 str) str))
(should (equal (match-string 1 str) "23"))
(should (equal (match-string 2 str) "mWh")))
(should-not (string-match (rx battery--acpi-capacity eos) "45 mW")))
Improve battery.el UPower support For discussion, see the following threads: https://lists.gnu.org/archive/html/emacs-devel/2020-01/msg00843.html https://lists.gnu.org/archive/html/emacs-devel/2020-02/msg00042.html https://lists.gnu.org/archive/html/emacs-devel/2020-02/msg00282.html * etc/NEWS: Announce that battery-upower is enabled by default. * lisp/battery.el (battery-upower-device): Accept both battery and line power device names, or a list thereof (bug#39491). (battery-upower-line-power-device): Remove user option; superseded by battery-upower-device. (battery-upower-subscribe): New user option. (battery-status-function): Check whether a UPower service is provided without activating it. (display-battery-mode): Subscribe to UPower signals when using battery-upower. (battery-upower): Merge data from multiple power sources. Calculate terse battery status %b based on average battery load percentage rather than coarse and often missing BatteryLevel (bug#39491). Add support for average temperature %d. (battery-upower-dbus-service) (battery-upower-dbus-interface) (battery-upower-dbus-path) (battery-upower-dbus-device-interface) (battery-upower-dbus-device-path) (battery-upower-device-all-properties): Rename to... (battery-upower-service) (battery-upower-interface) (battery-upower-path) (battery-upower-device-interface) (battery-upower-device-path) (battery--upower-device-properties): ...these, respectively. (battery-upower-device-list): Rename to... (battery--upower-devices) ...this. Return a flat list of device names determined by battery-upower-device. (battery-upower-types, battery-upower-states) (battery-upower-device-property, battery-upower-device-autodetect): Remove. (battery--upower-signals): New variable. (battery--upower-signal-handler, battery--upower-props-changed) (battery--upower-unsubscribe, battery--upower-subsribe) (battery--upower-state): New functions. * test/lisp/battery-tests.el (battery-upower-state) (battery-upower-state-unknown): New tests.
2020-06-11 12:49:31 +00:00
(ert-deftest battery-upower-state ()
"Test `battery--upower-state'."
;; Charging.
(dolist (total '(nil charging discharging empty fully-charged
pending-charge pending-discharge))
(should (eq (battery--upower-state '(("State" . 1)) total) 'charging)))
(dolist (state '(nil 0 1 2 3 4 5 6))
(should (eq (battery--upower-state `(("State" . ,state)) 'charging)
'charging)))
;; Discharging.
(dolist (total '(nil discharging empty fully-charged
pending-charge pending-discharge))
(should (eq (battery--upower-state '(("State" . 2)) total) 'discharging)))
(dolist (state '(nil 0 2 3 4 5 6))
(should (eq (battery--upower-state `(("State" . ,state)) 'discharging)
'discharging)))
;; Pending charge.
(dolist (total '(nil empty fully-charged pending-charge pending-discharge))
(should (eq (battery--upower-state '(("State" . 5)) total)
'pending-charge)))
(dolist (state '(nil 0 3 4 5 6))
(should (eq (battery--upower-state `(("State" . ,state)) 'pending-charge)
'pending-charge)))
;; Pending discharge.
(dolist (total '(nil empty fully-charged pending-discharge))
(should (eq (battery--upower-state '(("State" . 6)) total)
'pending-discharge)))
(dolist (state '(nil 0 3 4 6))
(should (eq (battery--upower-state `(("State" . ,state)) 'pending-discharge)
'pending-discharge)))
;; Empty.
(dolist (total '(nil empty))
(should (eq (battery--upower-state '(("State" . 3)) total) 'empty)))
(dolist (state '(nil 0 3))
(should (eq (battery--upower-state `(("State" . ,state)) 'empty) 'empty)))
;; Fully charged.
(dolist (total '(nil fully-charged))
(should (eq (battery--upower-state '(("State" . 4)) total) 'fully-charged)))
(dolist (state '(nil 0 4))
(should (eq (battery--upower-state `(("State" . ,state)) 'fully-charged)
'fully-charged))))
(ert-deftest battery-upower-state-unknown ()
"Test `battery--upower-state' with unknown states."
;; Unknown running total retains new state.
(should-not (battery--upower-state () nil))
(should-not (battery--upower-state '(("State" . state)) nil))
(should-not (battery--upower-state '(("State" . 0)) nil))
(should (eq (battery--upower-state '(("State" . 1)) nil) 'charging))
(should (eq (battery--upower-state '(("State" . 2)) nil) 'discharging))
(should (eq (battery--upower-state '(("State" . 3)) nil) 'empty))
(should (eq (battery--upower-state '(("State" . 4)) nil) 'fully-charged))
(should (eq (battery--upower-state '(("State" . 5)) nil) 'pending-charge))
(should (eq (battery--upower-state '(("State" . 6)) nil) 'pending-discharge))
;; Unknown new state retains running total.
(dolist (props '(() (("State" . state)) (("State" . 0))))
(dolist (total '(nil charging discharging empty fully-charged
pending-charge pending-discharge))
(should (eq (battery--upower-state props total) total))))
;; Conflicting empty and fully-charged.
(should-not (battery--upower-state '(("State" . 3)) 'fully-charged))
(should-not (battery--upower-state '(("State" . 4)) 'empty)))
(ert-deftest battery-format ()
"Test `battery-format'."
(should (equal (battery-format "" ()) ""))
(should (equal (battery-format "" '((?b . "-"))) ""))
Fix and extend format-spec (bug#41758) * lisp/format-spec.el: Use lexical-binding. Remove dependence on subr-x.el. (format-spec-make): Clarify docstring. (format-spec--parse-modifiers): Rename to... (format-spec--parse-flags): ...this and simplify. In particular, don't bother parsing :space-pad which is redundant and unused. (format-spec--pad): Remove, replacing with... (format-spec--do-flags): ...this new helper function which performs more of format-spec's supported text manipulation. (format-spec): Autoload. Allow optional argument to take on special values 'ignore' and 'delete' for more control over what happens when a replacement for a format specification isn't provided. Bring back proper support for a precision modifier similar to that of 'format'. * lisp/battery.el (battery-format): Rewrite in terms of format-spec. (battery-echo-area-format, battery-mode-line-format): Mention support of format-spec syntax in docstrings. * doc/lispref/strings.texi (Custom Format Strings): * etc/NEWS: Document and announce these changes. * lisp/dired-aux.el (dired-do-compress-to): * lisp/erc/erc-match.el (erc-log-matches): * lisp/erc/erc.el (erc-update-mode-line-buffer): * lisp/gnus/gnus-sieve.el (gnus-sieve-update): * lisp/gnus/gssapi.el (open-gssapi-stream): * lisp/gnus/mail-source.el (mail-source-fetch-file) (mail-source-fetch-directory, mail-source-fetch-pop) (mail-source-fetch-imap): * lisp/gnus/message.el (message-insert-formatted-citation-line): * lisp/image-dired.el: * lisp/net/eww.el: * lisp/net/imap.el (imap-kerberos4-open, imap-gssapi-open) (imap-shell-open): * lisp/net/network-stream.el (network-stream-open-shell): * lisp/obsolete/tls.el (open-tls-stream): * lisp/textmodes/tex-mode.el: Remove extraneous loads and autoloads of format-spec now that it is autoloaded and simplify its uses where possible. * test/lisp/battery-tests.el (battery-format): Test new format-spec support. * test/lisp/format-spec-tests.el (test-format-spec): Rename to... (format-spec) ...this, extending test cases. (test-format-unknown): Rename to... (format-spec-unknown): ...this, extending test cases. (test-format-modifiers): Rename to... (format-spec-flags): ...this. (format-spec-make, format-spec-parse-flags, format-spec-do-flags) (format-spec-do-flags-truncate, format-spec-do-flags-pad) (format-spec-do-flags-chop, format-spec-do-flags-case): New tests.
2020-05-29 18:56:14 +00:00
(should (equal (battery-format "%2a%-3b%.1p%%" '((?b . "-") (?p . "99")))
"- 9%")))
;;; battery-tests.el ends here