branch: externals/hyperbole
commit 402317d48c920e98192e22bce8986cfdde9511e9
Author: Mats Lidell <mats.lid...@lidells.se>
Commit: GitHub <nore...@github.com>

    Introduce DSL for testing Hywiki words (#690)
    
    
    
    * More review comments
---
 ChangeLog            |  10 +++++
 test/hywiki-tests.el | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 126 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index a4d513920a..99015dd4a5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2025-03-23  Mats Lidell  <ma...@gnu.org>
+
+* test/hywiki-tests.el (hywiki-tests--run-test-case): Run DSL for testing
+    Hywiki words.
+    (hywiki-tests--lorem-ipsum): Surrounding test text.
+    (hywiki-tests--verify-hywiki-word): DSL verification helper.
+    (hywiki-tests--wikiword-step-check-verification)
+    (hywiki-tests--wikiword-step-check-verification-with-surrounding-text):
+    DSL based tests of WikiWords.
+
 2025-03-20  Mats Lidell  <ma...@gnu.org>
 
 * Makefile (DOCKER_VERSIONS): Include 30.1.
diff --git a/test/hywiki-tests.el b/test/hywiki-tests.el
index 4b6ecdae84..58373f17fa 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:     19-Mar-25 at 17:00:58 by Mats Lidell
+;; Last-Mod:     19-Mar-25 at 19:59:12 by Mats Lidell
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -1363,6 +1363,121 @@ See gh#rswgnu/hyperbole/669."
         (hy-delete-file-and-buffer wiki-page)
         (hy-delete-dir-and-buffer hywiki-directory)))))
 
+(defun hywiki-tests--verify-hywiki-word (expected)
+  "Verify that `hywiki-word-at' returns t if a wikiword is EXPECTED.
+If EXPECTED is a string also verify that the wikiword matches the
+string."
+  (if (not expected)
+      (should-not (hywiki-word-at))
+    (if (stringp expected)
+        (should (string= expected (hywiki-word-at)))
+      (should (hywiki-word-at)))))
+
+(defun hywiki-tests--run-test-case (test-case)
+  "Run the TEST-CASE from point.
+Each test case consists of cons cells with an operation and the expected
+state of the WikiWord being constructed.  Operations are either a string
+to be inserted, a number of chars to be deleted or a symbol p<number>
+for where to move point.  The expected state is either nil for not a
+wikiword or non-nil for a wikiword.  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)))
+    (should (listp test-case))           ; For traceability
+    (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 (+ origin (1- pos))))
+                 (when (or (> 0 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
+  '(
+    (("H") ("i" . "Hi"))
+    (("H") ("iHo" . t) ("#") ("s " . "HiHo#s"))
+    (("H") ("iHo" . t) ("#") ("s" . t) (-1) (-1 . "HiHo"))
+    (("H") ("iHo" . t) ("#") ("s" . t) (-1) (-3 . t) (-1) ("i" . "Hi"))
+    (("H") ("iHo" . t) ("#") ("s " . t) ("n"))
+    (("H") ("iHo" . t) ("#") ("s " . t) (" n"))
+    ;; With delimiters
+    (("(H") ("iHo" . t) ("#") ("s" . "HiHo#s") (" " . "HiHo#s"))
+    (("(H") ("iHo" . t) ("#") ("s" . "HiHo#s") (")" . "HiHo#s)")) ; Delimiter 
part of WikiWord. See below too.
+    (("(H") ("iHo" . t) ("#") ("s" . "HiHo#s") ("-" . "HiHo#s-") ("n" . 
"HiHo#s-n") (")" . "HiHo#s-n)"))
+    ;; Insert and delete between WikiWords
+    (("H") ("iHo" . t) (p3 . t) (" " . "Hi") (p4 . "Ho") (-1 . "HiHo"))
+    (("H") ("iho" . t) (p3 . t) (" " . "Hi") (p4) (-1 . "Hiho"))
+    )
+  "List of test cases for WikiWords.")
+
+(ert-deftest hywiki-tests--wikiword-step-check-verification ()
+  "Run the step check to verify WikiWord is identified under change.
+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-directory (make-temp-file "hywiki" t)))
+    (unwind-protect
+        (progn
+          (hywiki-mode 1)
+          (dolist (testcase hywiki-tests--wikiword-step-check)
+            (with-temp-buffer
+              (hywiki-tests--run-test-case testcase))))
+      (hy-delete-dir-and-buffer hywiki-directory)))))
+
+(defconst hywiki-tests--lorem-ipsum "\
+Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse
+aliquet diam euismod turpis ultricies, et porta sem blandit. Sed vitae."
+  "Bulk text for in the middle of text tests.")
+
+(ert-deftest 
hywiki-tests--wikiword-step-check-verification-with-surrounding-text ()
+  "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-directory (make-temp-file "hywiki" t)))
+     (unwind-protect
+         (progn
+           (hywiki-mode 1)
+           (with-temp-buffer
+             (insert hywiki-tests--lorem-ipsum)
+             (goto-char (/ (point-max) 2))
+             (let ((pos (point)))
+               (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))))
+           (with-temp-buffer
+             (insert hywiki-tests--lorem-ipsum)
+             (goto-char (/ (point-max) 2))
+             (let ((pos (point)))
+               (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-dir-and-buffer hywiki-directory)))))
+
 (provide 'hywiki-tests)
 
 ;; This file can't be byte-compiled without the `el-mock' package

Reply via email to