branch: externals/hyperbole
commit 8baa3d39242442e69d1a3b25528b36d4dca9cc0b
Author: bw <[email protected]>
Commit: bw <[email protected]>

    hywiki-tests--word-at - Return HyWikiWord only if it has a referent
    
    From Mats:
    (hywiki-tests--wikiword-step-check-verification)
       (hywiki-tests--wikiword-step-check-verification-with-surrounding-text):
       Create wikipages, required by hywkii-tests--word-at.
    
    (hywiki-tests--wikiword-step-check-verification-with-faces)
       (hywiki-tests--face-property-for-wikiword-with-wikipage)
       (hywiki-tests--no-face-property-for-no-wikipage)
       (hywiki-tests--verify-face-property-when-editing-wikiword)
       (hywiki-tests--verify-face-property-when-editing-wikiword-first-char)
       (hywiki-tests--references-to-org-link, hywiki-tests--word-face-at-p):
       Drop interactive requirement.
---
 ChangeLog            |  22 ++++++++
 hywiki.el            |  41 +++++++++++----
 test/hywiki-tests.el | 140 +++++++++++++++++++++++++++------------------------
 3 files changed, 125 insertions(+), 78 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index f2bc1385db..6164bea1c6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,10 +1,18 @@
 2026-02-01  Bob Weiner  <[email protected]>
 
+* test/hywiki-tests.el (hywiki-tests--word-at): Return HyWikiWord reference
+    at point only if it has a referent.
+
 * hywiki.el (hywiki-word-store-around-point): Dehighlight only if Emacs
     is idle and within a hywikiword in a valid context.
             (hywiki-highlighted-word-at): Rewrite so matches 'hywiki-word-at'
     behavior based on point but requires that the HyWikiWord be highlighted
     or returns nil/nil range.
+            (hywiki-word-get-range): Add and use in
+    'hywiki-highlight-word-get-range'.
+            (hywiki-referent-exists-p)
+             hywiki--maybe-dehighlight-at-point): Get the range but don't
+    highlight the reference.
 
 * test/hywiki-tests.el (hywiki-tests--run-test-case): Remove initial
     'erase-buffer' since callers set up the buffer prior to this call.
@@ -41,6 +49,20 @@
       leaves point after closing string delimiter and then tests if in
       string.  Move point to within string (grouping 1) before testing.
 
+2026-02-01  Mats Lidell  <[email protected]>
+
+  (hywiki-tests--wikiword-step-check-verification)
+    (hywiki-tests--wikiword-step-check-verification-with-surrounding-text):
+    Create wikipages, required by hywkii-tests--word-at.
+
+  (hywiki-tests--wikiword-step-check-verification-with-faces)
+    (hywiki-tests--face-property-for-wikiword-with-wikipage)
+    (hywiki-tests--no-face-property-for-no-wikipage)
+    (hywiki-tests--verify-face-property-when-editing-wikiword)
+    (hywiki-tests--verify-face-property-when-editing-wikiword-first-char)
+    (hywiki-tests--references-to-org-link, hywiki-tests--word-face-at-p):
+    Drop interactive requirement.
+
 2026-01-31  Bob Weiner  <[email protected]>
 
 * hproperty.el (hproperty:char-property-face-p): Rename to
diff --git a/hywiki.el b/hywiki.el
index 9f49422731..c071db996f 100644
--- a/hywiki.el
+++ b/hywiki.el
@@ -3,7 +3,7 @@
 ;; Author:       Bob Weiner
 ;;
 ;; Orig-Date:    21-Apr-24 at 22:41:13
-;; Last-Mod:      1-Feb-26 at 15:33:59 by Bob Weiner
+;; Last-Mod:      1-Feb-26 at 19:16:29 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -3135,7 +3135,7 @@ at point must return non-nil or this function will return 
nil."
     (when (stringp word)
       (setq word (hywiki-strip-org-link word)))
     (if (or (stringp word)
-           (setq word (hywiki-highlight-word-get-range)))
+           (setq word (hywiki-word-get-range)))
        (unless (hywiki-get-referent (if (stringp word) word (nth 0 word)))
          (setq word nil))
       (setq word nil))
@@ -3242,7 +3242,9 @@ end-position); the positions include the entire
 HyWikiWord#section:Lnum:Cnum string but exclude any delimiters.
 
 This does not test whether a referent exists for the HyWikiWord; call
-`hywiki-referent-exists-p' without an argument for that.
+`hywiki-referent-exists-p' without an argument for that.  Nor does it
+test whether the HyWikiWord reference is within a valid context; call
+`hywiki-non-hook-context-p' for that.
 
 A call to `hywiki-active-in-current-buffer-p' at point must return
 non-nil or this will return nil."
@@ -3452,9 +3454,10 @@ Hyperbole button names."
   (unless (hproperty:but-face-p start hywiki-ignore-face-list)
     (hproperty:but-add start end hywiki-word-face)))
 
-(defun hywiki-highlight-word-get-range ()
+(defun hywiki-word-get-range ()
   "Return list of (HyWikiWord#section:Lnum:Cnum start end) around point.
-Also highlight HyWikiWord as necessary.
+Calls to `hywiki-active-in-current-buffer-p' and `hywiki-non-hook-context-p'
+must return non-nil or this will return \\='(nil nil nil).
 
 If the HyWikiWord reference is delimited, point must be within the
 delimiters.  The delimiters are excluded from start and end.  If not
@@ -3465,14 +3468,30 @@ or not.  Call `hywiki-highlighted-word-at' to test for 
a highlighted
 HyWikiWord at point.
 
 This does not test whether a referent exists for the HyWikiWord; call
-`hywiki-referent-exists-p' without an argument for that.
+`hywiki-referent-exists-p' without an argument for that."
+  (if (hywiki-non-hook-context-p)
+      '(nil nil nil)
+    (hywiki-word-at :range)))
+
+(defun hywiki-highlight-word-get-range ()
+  "Return list of (HyWikiWord#section:Lnum:Cnum start end) around point.
+Also highlight HyWikiWord as necessary.
 
 A call to `hywiki-active-in-current-buffer-p' at point must return
-non-nil or this will return \\='(nil nil nil)."
+non-nil or this will return \\='(nil nil nil).
+
+If the HyWikiWord reference is delimited, point must be within the
+delimiters.  The delimiters are excluded from start and end.  If not
+at a HyWikiWord, return \\='(nil nil nil).
+
+This works regardless of whether the HyWikiWord has been highlighted
+or not.  Call `hywiki-highlighted-word-at' to test for a highlighted
+HyWikiWord at point.
+
+This does not test whether a referent exists for the HyWikiWord; call
+`hywiki-referent-exists-p' without an argument for that."
   (cl-destructuring-bind (wikiword start end)
-      (if (hywiki-non-hook-context-p)
-         '(nil nil nil)
-       (hywiki-word-at :range))
+      (hywiki-word-get-range)
     ;; Ensure wikiword in buffer is highlighted before
     ;; returning its non-highlighted string version.
     (when (and wikiword start end
@@ -3937,7 +3956,7 @@ the HyWikiWord reference."
                                    (buffer-substring hywiki--buttonize-start
                                                      hywiki--buttonize-end))
                                  (when (and (setq hywiki--range
-                                                  
(hywiki-highlight-word-get-range))
+                                                  (hywiki-word-get-range))
                                             (nth 1 hywiki--range))
                                    (prog1 (nth 1 hywiki--range)
                                      (setq hywiki--range nil)))
diff --git a/test/hywiki-tests.el b/test/hywiki-tests.el
index 1c40262ebc..45a27ea891 100644
--- a/test/hywiki-tests.el
+++ b/test/hywiki-tests.el
@@ -3,7 +3,7 @@
 ;; Author:       Mats Lidell
 ;;
 ;; Orig-Date:    18-May-24 at 23:59:48
-;; Last-Mod:      1-Feb-26 at 15:52:57 by Bob Weiner
+;; Last-Mod:      1-Feb-26 at 19:39:45 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -772,7 +772,6 @@ Both mod-time and checksum must be changed for a test to 
return true."
 
 (ert-deftest hywiki-tests--face-property-for-wikiword-with-wikipage ()
   "Verify WikiWord for a wiki page gets face property hywiki-word-face."
-  (skip-unless (not noninteractive))
   (hywiki-tests--preserve-hywiki-mode
     (let* ((hsys-org-enable-smart-keys t)
           str)
@@ -791,7 +790,6 @@ Both mod-time and checksum must be changed for a test to 
return true."
 
 (ert-deftest hywiki-tests--no-face-property-for-no-wikipage ()
   "Verify WikiWord for no wiki page does not get face property 
hywiki-word-face."
-  (skip-unless (not noninteractive))
   (hywiki-tests--preserve-hywiki-mode
     (let* ((hsys-org-enable-smart-keys t)
            (hywiki-directory (make-temp-file "hywiki" t)))
@@ -807,7 +805,6 @@ Both mod-time and checksum must be changed for a test to 
return true."
 
 (ert-deftest hywiki-tests--verify-face-property-when-editing-wikiword ()
   "Verify face property changes when WikiWord is edited."
-  (skip-unless (not noninteractive))
   (hywiki-tests--preserve-hywiki-mode
     (hywiki-tests--insert "Wikiord ")
     (goto-char 5)
@@ -825,7 +822,6 @@ Both mod-time and checksum must be changed for a test to 
return true."
 
 (ert-deftest 
hywiki-tests--verify-face-property-when-editing-wikiword-first-char ()
   "Verify face property changes when WikiWord is edited in the first char 
position."
-  (skip-unless (not noninteractive))
   (hywiki-tests--preserve-hywiki-mode
     (hywiki-tests--insert "WikiWord")
     (hywiki-tests--command-execute #'self-insert-command 1 ? )
@@ -844,7 +840,6 @@ Both mod-time and checksum must be changed for a test to 
return true."
 
 (ert-deftest hywiki-tests--references-to-org-link ()
   "Verify `hywiki-references-to-org-links' converts WikiWords to org links."
-  (skip-unless (not noninteractive))
   (hywiki-tests--preserve-hywiki-mode
     (let* ((hywiki-directory (make-temp-file "hywiki" t))
            (wikipage (cdr (hywiki-add-page "WikiWord"))))
@@ -1644,10 +1639,12 @@ comparison with expected overlays stable."
   "Non-nil to perform face validation of WikiWord.")
 
 (defun hywiki-tests--word-at ()
-  "Choose what test to perform based on value of 
`hywiki-tests--with-face-test'."
-  (if hywiki-tests--with-face-test
-      (hywiki-highlighted-word-at)
-    (hywiki-word-at)))
+  "Test if there is a HyWikiWord reference at point with a referent.
+Choose what test to perform based on value of `hywiki-tests--with-face-test'."
+  (when (hywiki-referent-exists-p)
+    (if hywiki-tests--with-face-test
+       (hywiki-highlighted-word-at)
+      (hywiki-word-at))))
 
 (defun hywiki-tests--verify-hywiki-word (expected)
   "Verify that `hywiki-word-at' returns t if a wikiword is EXPECTED.
@@ -1687,36 +1684,32 @@ of the string are inserted.  If equal to a string it is 
checked
 for match with the wikiword.  Movement of point is relative to
 point when the function is called."
   (let ((origin (point)))
-
-    ;; For traceability when looking through the list of should
-    ;; clauses in a failing test.
-    (should (listp test-case))
-
-    (dolist (steps test-case)
-      (let ((step (car steps))
-            (vfy (cdr steps)))
-        (cond ((stringp step)
-               (dolist (ch (string-to-list step))
-                 (hywiki-tests--command-execute #'self-insert-command 1 ch))
-               (save-excursion
-                 (goto-char (1- (point)))
-                 (hywiki-tests--verify-hywiki-word vfy)))
-              ((integerp step)
-               (let ((forward (> step 0)))
-                 (dotimes (_ (abs step))
-                   (if forward
-                       (hywiki-tests--command-execute #'delete-forward-char 1)
-                     (hywiki-tests--command-execute #'backward-delete-char 1)))
-                 (hywiki-tests--verify-hywiki-word vfy)))
-              ((and (symbolp step) (string-prefix-p "p" (symbol-name step)))
-               (let* ((pos (string-to-number (substring (symbol-name step) 1)))
-                      (newpos (max (min (+ origin (1- pos)) (point-max))
-                                  (point-min))))
-                 (when (or (> (point-min) newpos) (< (point-max) newpos))
-                   (ert-fail (format "New point: '%s' is outside of buffer" 
newpos)))
-                 (goto-char newpos))
-               (hywiki-tests--verify-hywiki-word vfy))
-              (t (ert-fail (format "Unknown step: '%s' in WikiWord 
verification" step))))))))
+    (ert-info ((format "Test case => '%s'" test-case))
+      (dolist (steps test-case)
+       (let ((step (car steps))
+              (vfy (cdr steps)))
+          (cond ((stringp step)
+                (dolist (ch (string-to-list step))
+                   (hywiki-tests--command-execute #'self-insert-command 1 ch))
+                (save-excursion
+                   (goto-char (1- (point)))
+                   (hywiki-tests--verify-hywiki-word vfy)))
+               ((integerp step)
+                (let ((forward (> step 0)))
+                   (dotimes (_ (abs step))
+                     (if forward
+                        (hywiki-tests--command-execute #'delete-forward-char 1)
+                       (hywiki-tests--command-execute #'backward-delete-char 
1)))
+                   (hywiki-tests--verify-hywiki-word vfy)))
+               ((and (symbolp step) (string-prefix-p "p" (symbol-name step)))
+                (let* ((pos (string-to-number (substring (symbol-name step) 
1)))
+                       (newpos (max (min (+ origin (1- pos)) (point-max))
+                                    (point-min))))
+                   (when (or (> (point-min) newpos) (< (point-max) newpos))
+                     (ert-fail (format "New point: '%s' is outside of buffer" 
newpos)))
+                   (goto-char newpos))
+                (hywiki-tests--verify-hywiki-word vfy))
+               (t (ert-fail (format "Unknown step: '%s' in WikiWord 
verification" step)))))))))
 
 (defconst hywiki-tests--wikiword-step-check
   '(
@@ -1749,16 +1742,22 @@ point when the function is called."
 Performs each operation from the step check and verifies if the
 resulting state at point is a WikiWord or not."
   (hywiki-tests--preserve-hywiki-mode
-    (let ((hywiki-tests--with-face-test nil))
-      (dolist (testcase hywiki-tests--wikiword-step-check)
-       (erase-buffer)
-        (hywiki-tests--run-test-case testcase)))))
+    (let* ((wikiHiHo (cdr (hywiki-add-page "HiHo")))
+           (wikiHiho (cdr (hywiki-add-page "Hiho")))
+           (wikiHi (cdr (hywiki-add-page "Hi")))
+           (wikiHo (cdr (hywiki-add-page "Ho")))
+           (wiki-page-list (list wikiHiHo wikiHiho wikiHi wikiHo))
+           (hywiki-tests--with-face-test nil))
+      (unwind-protect
+          (dolist (testcase hywiki-tests--wikiword-step-check)
+           (erase-buffer)
+            (hywiki-tests--run-test-case testcase))
+        (hy-delete-files-and-buffers wiki-page-list)))))
 
 (ert-deftest hywiki-tests--wikiword-step-check-verification-with-faces ()
   "Run the step check to verify WikiWord is identified under change.
 Perform each operation from the step check and verify whether there
 is a WikiWord at point or not."
-  (skip-unless (not noninteractive))
   (hywiki-tests--preserve-hywiki-mode
     (let* ((wikiHiHo (cdr (hywiki-add-page "HiHo")))
            (wikiHiho (cdr (hywiki-add-page "Hiho")))
@@ -1780,29 +1779,36 @@ aliquet diam euismod turpis ultricies, et porta sem 
blandit. Sed vitae."
   "Run the step check to verify WikiWord is identified under change.
 Insert test in the middle of other text."
   (hywiki-tests--preserve-hywiki-mode
-    (let ((hywiki-tests--with-face-test nil))
-      (hywiki-tests--insert hywiki-tests--lorem-ipsum)
-      (goto-char (/ (point-max) 2))
-      (let ((pos (point)))
-        (hywiki-tests--insert " HiHo ")
-        (goto-char (1+ pos))
-        (should (looking-at-p "HiHo ")))
-      (hywiki-tests--run-test-case
-       '((p3 . t)
-         (" " . "Hi")
-         (p1 . t) (p4 . t) (-1 . t)))
+    (let* ((wikiHiHo (cdr (hywiki-add-page "HiHo")))
+           (wikiHiho (cdr (hywiki-add-page "Hiho")))
+           (wikiHi (cdr (hywiki-add-page "Hi")))
+           (wikiHo (cdr (hywiki-add-page "Ho")))
+           (wiki-page-list (list wikiHiHo wikiHiho wikiHi wikiHo)))
+      (unwind-protect
+          (progn
+            (insert hywiki-tests--lorem-ipsum)
+            (goto-char (/ (point-max) 2))
+            (let ((pos (point)))
+              (hywiki-tests--insert " HiHo ")
+              (goto-char (1+ pos))
+              (should (looking-at-p "HiHo ")))
+            (hywiki-tests--run-test-case
+             '((p3 . t)
+               (" " . "Hi")
+               (p1 . t) (p4 . t) (-1 . t)))
 
-      (erase-buffer)
-      (hywiki-tests--insert hywiki-tests--lorem-ipsum)
-      (goto-char (/ (point-max) 2))
-      (let ((pos (point)))
-        (hywiki-tests--insert " Hiho ")
-        (goto-char (1+ pos))
-        (should (looking-at-p "Hiho ")))
-      (hywiki-tests--run-test-case
-       '((p3 . t)
-         (" " . "Hi")
-         (p1 . t) (p4) (-1 . "Hiho"))))))
+            (erase-buffer)
+            (insert hywiki-tests--lorem-ipsum)
+            (goto-char (/ (point-max) 2))
+            (let ((pos (point)))
+              (hywiki-tests--insert " Hiho ")
+              (goto-char (1+ pos))
+              (should (looking-at-p "Hiho ")))
+            (hywiki-tests--run-test-case
+             '((p3 . t)
+               (" " . "Hi")
+               (p1 . t) (p4) (-1 . "Hiho"))))
+        (hy-delete-files-and-buffers wiki-page-list)))))
 
 (ert-deftest 
hywiki-tests--wikiword-step-check-edit-wikiword-in-emacs-lisp-mode ()
   "Run the step check to verify WikiWord is identified under change in a 
docstring.

Reply via email to