diff options
| author | Craig Jennings <c@cjennings.net> | 2026-06-28 03:20:12 -0400 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-06-28 03:20:12 -0400 |
| commit | e8ea03ecc8c1a2499046b34f785bca4f871531df (patch) | |
| tree | 33232649069265c4541545fc114a6410de369f3c /tests | |
| parent | ee8fdeb692d666c12ce068a2b1ee90e9451ac892 (diff) | |
| download | emacs-wttrin-e8ea03ecc8c1a2499046b34f785bca4f871531df.tar.gz emacs-wttrin-e8ea03ecc8c1a2499046b34f785bca4f871531df.zip | |
feat: fit and center the weather buffer in its window
Centering and auto-fit share one resize hook (wttrin--update-layout on
window-configuration-change-hook).
Centering is always on. The block centers via the window's left margin,
measured in pixels from the buffer font so it stays exact under a font-height
remap. It centers against the available width including the current margin, so
the margin shrinking the body can't make the value oscillate.
Auto-fit is opt-in (wttrin-auto-fit-font, off by default). When on, the font is
sized so the whole buffer fits top to bottom (down to the row past the footer's
last menu item) without the widest line truncating, clamped to
wttrin-font-height-min and -max.
The pixel-exact measurement needs window-font-width and window-font-height, so
the minimum is now Emacs 25.1.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test-wttrin--center.el | 109 | ||||
| -rw-r--r-- | tests/test-wttrin--fit-font-height.el | 64 |
2 files changed, 173 insertions, 0 deletions
diff --git a/tests/test-wttrin--center.el b/tests/test-wttrin--center.el new file mode 100644 index 0000000..2da639c --- /dev/null +++ b/tests/test-wttrin--center.el @@ -0,0 +1,109 @@ +;;; test-wttrin--center.el --- Tests for weather-buffer centering -*- lexical-binding: t; -*- + +;; Copyright (C) 2024-2026 Craig Jennings + +;;; Commentary: +;; Unit tests for the block-centering helpers: wttrin--block-width, +;; wttrin--center-margin, and the wttrin--center-buffer applier. + +;;; Code: + +(require 'ert) +(require 'wttrin) +(require 'testutil-wttrin) + +;;; wttrin--block-width — Normal + +(ert-deftest test-wttrin--block-width-normal-returns-widest-line () + "Normal: returns the display width of the widest line." + (should (= 4 (wttrin--block-width "ab\nabcd\nabc")))) + +(ert-deftest test-wttrin--block-width-normal-counts-wide-chars () + "Normal: wide (double-width) characters count as two columns." + ;; "你好" is two double-width chars = 4 columns, wider than the "a" line. + (should (= 4 (wttrin--block-width "a\n你好")))) + +(ert-deftest test-wttrin--block-width-normal-ignores-text-properties () + "Normal: text properties do not affect the measured width." + (should (= 4 (wttrin--block-width (propertize "abcd" 'face 'bold))))) + +;;; wttrin--block-width — Boundary + +(ert-deftest test-wttrin--block-width-boundary-single-line () + "Boundary: a single line returns its own width." + (should (= 5 (wttrin--block-width "hello")))) + +(ert-deftest test-wttrin--block-width-boundary-empty-string-is-zero () + "Boundary: the empty string has width 0." + (should (= 0 (wttrin--block-width "")))) + +(ert-deftest test-wttrin--block-width-boundary-trailing-newline () + "Boundary: a trailing newline's empty final line does not inflate the width." + (should (= 3 (wttrin--block-width "abc\n")))) + +;;; wttrin--block-width — Error + +(ert-deftest test-wttrin--block-width-error-nil-is-zero () + "Error: nil input returns 0 rather than signaling." + (should (= 0 (wttrin--block-width nil)))) + +;;; wttrin--center-margin — Normal + +(ert-deftest test-wttrin--center-margin-normal-centers-block () + "Normal: a block narrower than the window gets half the slack as margin." + (should (= 30 (wttrin--center-margin 20 80)))) + +;;; wttrin--center-margin — Boundary + +(ert-deftest test-wttrin--center-margin-boundary-equal-widths-zero () + "Boundary: a block exactly as wide as the window gets no margin." + (should (= 0 (wttrin--center-margin 40 40)))) + +(ert-deftest test-wttrin--center-margin-boundary-block-wider-than-window-zero () + "Boundary: a block wider than the window gets no margin (never negative)." + (should (= 0 (wttrin--center-margin 50 40)))) + +(ert-deftest test-wttrin--center-margin-boundary-odd-slack-floors () + "Boundary: an odd amount of slack floors to a whole column." + (should (= 30 (wttrin--center-margin 20 81)))) + +(ert-deftest test-wttrin--center-margin-boundary-one-column-slack-zero () + "Boundary: one column of slack floors to 0." + (should (= 0 (wttrin--center-margin 20 21)))) + +;;; wttrin--center-margin — Error + +(ert-deftest test-wttrin--center-margin-error-non-integer-is-zero () + "Error: non-integer inputs return 0 rather than signaling." + (should (= 0 (wttrin--center-margin nil 80))) + (should (= 0 (wttrin--center-margin 20 nil)))) + +;;; wttrin--center-buffer — applier + +(ert-deftest test-wttrin--center-buffer-normal-is-idempotent () + "Normal: centering a displayed buffer is stable — a second call does not move +the margin (the margin-shrinks-the-body feedback is handled)." + (let ((buf (get-buffer-create "*wttrin-center-test*"))) + (unwind-protect + (save-window-excursion + (with-current-buffer buf + (let ((inhibit-read-only t)) + (erase-buffer) + (insert "hi\nthere"))) + (set-window-buffer (selected-window) buf) + (with-current-buffer buf + (wttrin--center-buffer) + (let* ((win (get-buffer-window buf)) + (first (or (car (window-margins win)) 0))) + (wttrin--center-buffer) + (should (= first (or (car (window-margins win)) 0)))))) + (kill-buffer buf)))) + +(ert-deftest test-wttrin--center-buffer-boundary-no-window-is-noop () + "Boundary: with no window for the buffer, centering is a no-op, no error." + (with-temp-buffer + (insert "content") + (should-not (wttrin--center-buffer)))) + +(provide 'test-wttrin--center) +;;; test-wttrin--center.el ends here diff --git a/tests/test-wttrin--fit-font-height.el b/tests/test-wttrin--fit-font-height.el new file mode 100644 index 0000000..2952fc6 --- /dev/null +++ b/tests/test-wttrin--fit-font-height.el @@ -0,0 +1,64 @@ +;;; test-wttrin--fit-font-height.el --- Tests for font auto-fit math -*- lexical-binding: t; -*- + +;; Copyright (C) 2024-2026 Craig Jennings + +;;; Commentary: +;; Unit tests for wttrin--fit-font-height, the pure helper that scales the +;; weather font so the fixed-width block fills the window, clamped to a floor +;; and cap. + +;;; Code: + +(require 'ert) +(require 'wttrin) +(require 'testutil-wttrin) + +;;; Normal + +(ert-deftest test-wttrin--fit-font-height-normal-exact-fit-keeps-height () + "Normal: a block that already fills the width keeps the current height. +50 cols x 12 px = 600 px = avail, ratio 1.0, so 130 stays 130." + (should (= 130 (wttrin--fit-font-height 50 600 12 130 100 200)))) + +(ert-deftest test-wttrin--fit-font-height-normal-wider-window-grows-height () + "Normal: a wider window grows the height proportionally. +avail 900 / block-px 600 = 1.5, so 130 -> 195, within [100,200]." + (should (= 195 (wttrin--fit-font-height 50 900 12 130 100 200)))) + +(ert-deftest test-wttrin--fit-font-height-normal-narrower-window-shrinks-height () + "Normal: a narrower window shrinks the height proportionally. +avail 480 / block-px 600 = 0.8, so 130 -> 104, within [100,200]." + (should (= 104 (wttrin--fit-font-height 50 480 12 130 100 200)))) + +;;; Boundary + +(ert-deftest test-wttrin--fit-font-height-boundary-clamps-to-cap () + "Boundary: a very wide window clamps the height to the cap." + ;; avail 1800 / 600 = 3 -> 390 -> capped at 200. + (should (= 200 (wttrin--fit-font-height 50 1800 12 130 100 200)))) + +(ert-deftest test-wttrin--fit-font-height-boundary-clamps-to-floor () + "Boundary: a very narrow window clamps the height to the floor." + ;; avail 120 / 600 = 0.2 -> 26 -> floored at 100. + (should (= 100 (wttrin--fit-font-height 50 120 12 130 100 200)))) + +(ert-deftest test-wttrin--fit-font-height-boundary-floor-equals-cap () + "Boundary: when floor equals cap, that value is forced." + (should (= 150 (wttrin--fit-font-height 50 900 12 130 150 150)))) + +;;; Error + +(ert-deftest test-wttrin--fit-font-height-error-zero-block-returns-clamped-current () + "Error: a zero block width returns the current height, clamped." + (should (= 130 (wttrin--fit-font-height 0 900 12 130 100 200)))) + +(ert-deftest test-wttrin--fit-font-height-error-zero-char-px-returns-clamped-current () + "Error: a zero per-character width returns the current height, clamped." + (should (= 130 (wttrin--fit-font-height 50 900 0 130 100 200)))) + +(ert-deftest test-wttrin--fit-font-height-error-nil-avail-returns-clamped-current () + "Error: a nil available width returns the current height, clamped." + (should (= 130 (wttrin--fit-font-height 50 nil 12 130 100 200)))) + +(provide 'test-wttrin--fit-font-height) +;;; test-wttrin--fit-font-height.el ends here |
