diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test-system-utils-eval-buffer.el | 62 | ||||
| -rw-r--r-- | tests/test-system-utils-identify-external-open-command.el | 59 |
2 files changed, 121 insertions, 0 deletions
diff --git a/tests/test-system-utils-eval-buffer.el b/tests/test-system-utils-eval-buffer.el new file mode 100644 index 00000000..49aacc73 --- /dev/null +++ b/tests/test-system-utils-eval-buffer.el @@ -0,0 +1,62 @@ +;;; test-system-utils-eval-buffer.el --- Tests for cj/eval-buffer-with-confirmation-or-error-message -*- lexical-binding: t; -*- + +;;; Commentary: +;; Unit tests for `cj/eval-buffer-with-confirmation-or-error-message' +;; in system-utils.el. The function evaluates the current buffer and +;; reports success or any signaled error via `message', so errors +;; don't propagate to the caller. + +;;; Code: + +(require 'ert) +(require 'cl-lib) + +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'system-utils) + +(defmacro test-sueb--capture-message (var &rest body) + "Run BODY with `message' stubbed to set VAR to the formatted string." + (declare (indent 1)) + `(cl-letf (((symbol-function 'message) + (lambda (fmt &rest args) + (setq ,var (apply #'format fmt args))))) + ,@body)) + +;;; Normal cases + +(ert-deftest test-sueb-valid-elisp-reports-success () + "Normal: evaluating valid elisp reports \"Buffer evaluated.\" via message." + (let (captured) + (test-sueb--capture-message captured + (with-temp-buffer + (emacs-lisp-mode) + (insert "(+ 1 2)") + (cj/eval-buffer-with-confirmation-or-error-message))) + (should (string-match-p "Buffer evaluated" captured)))) + +;;; Boundary cases + +(ert-deftest test-sueb-empty-buffer-reports-success () + "Boundary: an empty buffer evaluates to nil cleanly and reports success." + (let (captured) + (test-sueb--capture-message captured + (with-temp-buffer + (emacs-lisp-mode) + (cj/eval-buffer-with-confirmation-or-error-message))) + (should (string-match-p "Buffer evaluated" captured)))) + +;;; Error cases + +(ert-deftest test-sueb-error-caught-not-propagated () + "Error: evaluation of broken code reports the error via message without signaling. +Proves the condition-case wrapper catches errors rather than propagating them." + (let (captured) + (test-sueb--capture-message captured + (with-temp-buffer + (emacs-lisp-mode) + (insert "(this-function-does-not-exist-for-testing)") + (cj/eval-buffer-with-confirmation-or-error-message))) + (should (string-match-p "Error occurred during evaluation" captured)))) + +(provide 'test-system-utils-eval-buffer) +;;; test-system-utils-eval-buffer.el ends here diff --git a/tests/test-system-utils-identify-external-open-command.el b/tests/test-system-utils-identify-external-open-command.el new file mode 100644 index 00000000..dc272570 --- /dev/null +++ b/tests/test-system-utils-identify-external-open-command.el @@ -0,0 +1,59 @@ +;;; test-system-utils-identify-external-open-command.el --- Tests for cj/identify-external-open-command -*- lexical-binding: t; -*- + +;;; Commentary: +;; Unit tests for `cj/identify-external-open-command' in system-utils.el. +;; The function dispatches on host-environment predicates to return the +;; appropriate "open" command: xdg-open on Linux, open on macOS, +;; start on Windows. Anything else is a fatal error. + +;;; Code: + +(require 'ert) +(require 'cl-lib) + +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'system-utils) + +(defmacro test-siuec--with-host (linux macos windows &rest body) + "Run BODY with env-*-p predicates stubbed to LINUX, MACOS, WINDOWS." + (declare (indent 3)) + `(cl-letf (((symbol-function 'env-linux-p) (lambda () ,linux)) + ((symbol-function 'env-macos-p) (lambda () ,macos)) + ((symbol-function 'env-windows-p) (lambda () ,windows))) + ,@body)) + +;;; Normal cases + +(ert-deftest test-siuec-linux-returns-xdg-open () + "Normal: Linux host returns \"xdg-open\"." + (test-siuec--with-host t nil nil + (should (string= "xdg-open" (cj/identify-external-open-command))))) + +(ert-deftest test-siuec-macos-returns-open () + "Normal: macOS host returns \"open\"." + (test-siuec--with-host nil t nil + (should (string= "open" (cj/identify-external-open-command))))) + +(ert-deftest test-siuec-windows-returns-start () + "Normal: Windows host returns \"start\"." + (test-siuec--with-host nil nil t + (should (string= "start" (cj/identify-external-open-command))))) + +;;; Boundary cases + +(ert-deftest test-siuec-dispatch-order-linux-wins () + "Boundary: Linux check runs first; wins when predicates disagree. +Documents the dispatch order. A real host only returns t from one +of these anyway — but if something goes wrong, Linux takes priority." + (test-siuec--with-host t t t + (should (string= "xdg-open" (cj/identify-external-open-command))))) + +;;; Error cases + +(ert-deftest test-siuec-unsupported-host-signals-error () + "Error: when no platform predicate returns non-nil, signals an error." + (test-siuec--with-host nil nil nil + (should-error (cj/identify-external-open-command)))) + +(provide 'test-system-utils-identify-external-open-command) +;;; test-system-utils-identify-external-open-command.el ends here |
