aboutsummaryrefslogtreecommitdiff
path: root/tests/test-cj-window-geometry.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-05-09 14:48:01 -0500
committerCraig Jennings <c@cjennings.net>2026-05-09 14:48:01 -0500
commit7ad613f96380319c037f367a1b6b1beda03846ca (patch)
treeb442ddc79de803e042505df84ff1d8acb8c812cb /tests/test-cj-window-geometry.el
parenta29ac8d9f31443279ba5897b13cf5cda49519975 (diff)
downloaddotemacs-7ad613f96380319c037f367a1b6b1beda03846ca.tar.gz
dotemacs-7ad613f96380319c037f367a1b6b1beda03846ca.zip
refactor: extract window-geometry helpers shared by F9 and F12
`ai-vterm.el` (F9) and `eshell-vterm-config.el` (F12) both grew the same geometry-preservation pattern: classify a window's position, capture its body size, map cardinal direction to its frame-edge variant. The shared helpers were sitting as near-duplicates in both modules. With two real consumers established, the abstraction has the right shape. I pulled them into `cj-window-geometry.el`. The new module exposes three pure helpers: - `cj/window-direction` returns right/below/left/above based on edges relative to `frame-root-window`. Takes an optional DEFAULT for the single-window-frame fallback so each consumer picks its own (ai-vterm wants 'right, vterm-toggle wants 'below). - `cj/window-body-size` returns body-cols (right/left) or body-lines (below/above). Same body-vs-total reasoning as before: divider-independent, matches what the user sees. - `cj/cardinal-to-edge-direction` maps right/left/below/above to rightmost/leftmost/bottom/top, used by each consumer's `display-saved` action. `ai-vterm.el` and `eshell-vterm-config.el` now `(require 'cj-window-geometry)` and call the shared helpers directly. The consumer-specific `capture-state` and `display-saved` bodies stay in each module because they bind to consumer-specific state vars. Extracting those would either need parameter-passing-via-symbol or a macro, both heavier than the duplication they would remove. Tests: 15 in `test-cj-window-geometry.el` covering all four directions, body-size on both axes, cardinal-to-edge mapping, default-arg fallback, and the unknown-direction nil case. Deleted `test-ai-vterm--window-geometry.el` (now redundant) and trimmed four duplicate window-direction/size tests from `test-vterm-toggle--display.el`. Net LOC: each consumer ~40-50 lines lighter, with the new module + tests paying roughly half that back. Full make test green. make validate-modules green.
Diffstat (limited to 'tests/test-cj-window-geometry.el')
-rw-r--r--tests/test-cj-window-geometry.el103
1 files changed, 103 insertions, 0 deletions
diff --git a/tests/test-cj-window-geometry.el b/tests/test-cj-window-geometry.el
new file mode 100644
index 000000000..8b0b97321
--- /dev/null
+++ b/tests/test-cj-window-geometry.el
@@ -0,0 +1,103 @@
+;;; test-cj-window-geometry.el --- Tests for the shared window-geometry helpers -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; Tests the three pure helpers in `cj-window-geometry.el':
+;; `cj/window-direction', `cj/window-body-size', and
+;; `cj/cardinal-to-edge-direction'.
+
+;;; Code:
+
+(require 'ert)
+
+(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
+(require 'cj-window-geometry)
+
+(ert-deftest test-cj-window-geometry--direction-right-split ()
+ "Normal: 2-window vertical split, right-side window -> right."
+ (save-window-excursion
+ (delete-other-windows)
+ (let ((right (split-window (selected-window) nil 'right)))
+ (should (eq (cj/window-direction right) 'right)))))
+
+(ert-deftest test-cj-window-geometry--direction-left-split ()
+ "Normal: 2-window vertical split, left-side window -> left."
+ (save-window-excursion
+ (delete-other-windows)
+ (split-window (selected-window) nil 'right)
+ (should (eq (cj/window-direction (selected-window)) 'left))))
+
+(ert-deftest test-cj-window-geometry--direction-below-split ()
+ "Normal: 2-window horizontal split, bottom window -> below."
+ (save-window-excursion
+ (delete-other-windows)
+ (let ((below (split-window (selected-window) nil 'below)))
+ (should (eq (cj/window-direction below) 'below)))))
+
+(ert-deftest test-cj-window-geometry--direction-above-split ()
+ "Normal: 2-window horizontal split, top window -> above."
+ (save-window-excursion
+ (delete-other-windows)
+ (split-window (selected-window) nil 'below)
+ (should (eq (cj/window-direction (selected-window)) 'above))))
+
+(ert-deftest test-cj-window-geometry--direction-single-window-default-right ()
+ "Boundary: single-window frame, no DEFAULT arg -> 'right."
+ (save-window-excursion
+ (delete-other-windows)
+ (should (eq (cj/window-direction (selected-window)) 'right))))
+
+(ert-deftest test-cj-window-geometry--direction-single-window-with-default ()
+ "Boundary: single-window frame with DEFAULT='below -> 'below."
+ (save-window-excursion
+ (delete-other-windows)
+ (should (eq (cj/window-direction (selected-window) 'below) 'below))))
+
+(ert-deftest test-cj-window-geometry--body-size-right-returns-body-cols ()
+ "Normal: right window with direction='right -> body-width in cols."
+ (save-window-excursion
+ (delete-other-windows)
+ (let ((right (split-window (selected-window) nil 'right)))
+ (should (= (cj/window-body-size right 'right)
+ (window-body-width right))))))
+
+(ert-deftest test-cj-window-geometry--body-size-below-returns-body-lines ()
+ "Normal: below window with direction='below -> body-height in lines."
+ (save-window-excursion
+ (delete-other-windows)
+ (let ((below (split-window (selected-window) nil 'below)))
+ (should (= (cj/window-body-size below 'below)
+ (window-body-height below))))))
+
+(ert-deftest test-cj-window-geometry--body-size-narrow-window ()
+ "Normal: deliberately narrow right window -> matching body cols."
+ (save-window-excursion
+ (delete-other-windows)
+ (let* ((frame-w (frame-width))
+ (target-cols (/ frame-w 4))
+ (right (split-window (selected-window) (- target-cols) 'right)))
+ (should (= (cj/window-body-size right 'right)
+ (window-body-width right))))))
+
+(ert-deftest test-cj-window-geometry--cardinal-to-edge-right ()
+ "Normal: 'right -> 'rightmost."
+ (should (eq (cj/cardinal-to-edge-direction 'right) 'rightmost)))
+
+(ert-deftest test-cj-window-geometry--cardinal-to-edge-left ()
+ "Normal: 'left -> 'leftmost."
+ (should (eq (cj/cardinal-to-edge-direction 'left) 'leftmost)))
+
+(ert-deftest test-cj-window-geometry--cardinal-to-edge-below ()
+ "Normal: 'below -> 'bottom."
+ (should (eq (cj/cardinal-to-edge-direction 'below) 'bottom)))
+
+(ert-deftest test-cj-window-geometry--cardinal-to-edge-above ()
+ "Normal: 'above -> 'top."
+ (should (eq (cj/cardinal-to-edge-direction 'above) 'top)))
+
+(ert-deftest test-cj-window-geometry--cardinal-to-edge-unknown-returns-nil ()
+ "Boundary: an unknown direction symbol -> nil."
+ (should (null (cj/cardinal-to-edge-direction 'sideways)))
+ (should (null (cj/cardinal-to-edge-direction nil))))
+
+(provide 'test-cj-window-geometry)
+;;; test-cj-window-geometry.el ends here