From 2304e34a7e648b611667c74290622ca6fef5df84 Mon Sep 17 00:00:00 2001 From: Mats Lidell Date: Mon, 7 Jul 2025 09:00:29 +0200 Subject: [PATCH 1/6] Prepare framework for insert and yank hywiki tests --- test/hywiki-tests.el | 171 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 169 insertions(+), 2 deletions(-) diff --git a/test/hywiki-tests.el b/test/hywiki-tests.el index ef8299be..eaa09b26 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: 6-Jul-25 at 15:39:40 by Bob Weiner +;; Last-Mod: 7-Jul-25 at 00:12:06 by Mats Lidell ;; ;; SPDX-License-Identifier: GPL-3.0-or-later ;; @@ -1119,7 +1119,7 @@ up the test." ;; Command (defun hywiki-tests--command (wikiword) - "Test command." + "Verify WIKIWORD is WikiReferent." (interactive) (should (string= "WikiReferent" wikiword))) @@ -1691,6 +1691,173 @@ Insert test in the middle of other text." (hy-delete-files-and-buffers (list wikiHi wikiHo)) (hy-delete-dir-and-buffer hywiki-directory))))) +;; Based on functions in simple-test.el add support for also marking +;; highlighted words in buffers using the same idea with point-tag and +;; mark-tag also for start and end of highlighted regions. Note that +;; for setting the highlighted areas hywiki-mode is used. The tag +;; notation is not used for that. It is only used for the +;; verification. +(defconst hywiki-test--point-tag "^") +(defconst hywiki-test--start-tag "<") +(defconst hywiki-test--end-tag ">") + +;; Main function - reads from current buffer and inserts tags +(defun hywiki-test--insert-tags (positions) + "Insert tags at positions and mark point location in current buffer. +POSITIONS is a list of cons cells (START . END) with 1-based positions." + (let* ((buffer-string (buffer-substring-no-properties (point-min) (point-max))) + (current-point (point)) + ;; Convert 1-based positions to 0-based for string operations + ;; Note: Emacs ranges are (start . end) where end is exclusive + (zero-based-positions (mapcar (lambda (pos) + (cons (1- (car pos)) (1- (cdr pos)))) + (or positions '()))) + ;; Sort ranges in reverse order for right-to-left processing + (sorted-positions (sort zero-based-positions + (lambda (a b) (> (car a) (car b))))) + (result buffer-string)) + + ;; First, process all ranges from right to left + (dolist (pos sorted-positions) + (let* ((start (car pos)) + (end (cdr pos)) ; end is exclusive in our 0-based system + (before (substring result 0 start)) + (middle (substring result start end)) ; substring is exclusive too + (after (substring result end))) + + (setq result (concat before hywiki-test--start-tag middle hywiki-test--end-tag after)))) + + ;; Then insert point tag (now we need to adjust for inserted tags) + (let* ((point-pos (1- current-point)) + ;; Calculate how many characters we've added before point + (tags-before-point 0) + ;; Check if point is within any range + (point-in-range nil)) + + ;; Count tag characters and check if point is within ranges + (dolist (pos zero-based-positions) + (let ((range-start (car pos)) + (range-end (cdr pos))) + (cond + ;; Point is within this range (not at boundaries) + ((and (> point-pos range-start) (< point-pos range-end)) + (setq point-in-range t) + ;; Add start-tag length (we're inside the range) + (setq tags-before-point (+ tags-before-point (length hywiki-test--start-tag)))) + ;; Range is completely before point + ((<= range-end point-pos) + (setq tags-before-point (+ tags-before-point + (length hywiki-test--start-tag) + (length hywiki-test--end-tag))))))) + + ;; Insert point tag at adjusted position + (let* ((adjusted-point (+ point-pos tags-before-point)) + (before (substring result 0 adjusted-point)) + (after (substring result adjusted-point))) + (setq result (concat before hywiki-test--point-tag after)))) + + result)) + +(ert-deftest hywiki-test--insert-tags-test () + "Verify `hywiki-test--insert-tags'." + (with-temp-buffer + (insert "Hello World\n") + (goto-char 6) + (should (string= (hywiki-test--insert-tags nil) + "Hello^ World\n")) + (should (string= (hywiki-test--insert-tags '((1 . 6) (7 . 12))) + "^ \n")) + (goto-char 5) + (should (string= (hywiki-test--insert-tags '((1 . 6) (7 . 12))) + " \n")))) + +(defun hywiki-test--set-buffer-text-point-and-highlight (description) + "Set the current buffer's text, point and mark according to DESCRIPTION. + +Erase current buffer and insert DESCRIPTION. Set point to the first +occurrence of `hywiki-test--point-tag' in the buffer, removing it. If +there is no `hywiki-test--point-tag', set point to the beginning of the +buffer. + +End the insertion of text by turning on hywiki-mode and perform a dummy +command to get the pre- and post-hooks executed. This creates the +highlighting overlays we want to test." + (erase-buffer) + (hywiki-mode 0) ; Deactivate hywiki-mode, disable highlighting + (insert description) + (goto-char (point-min)) + (when (search-forward hywiki-test--point-tag nil t) + (delete-char (- (length hywiki-test--point-tag)))) + (hywiki-mode 1) ; Activate hywiki-mode activates highlighting + (save-excursion ; Force pre- and post-hooks. + (end-of-buffer) + (hywiki-tests--command-execute #'self-insert-command 1 ? ) + (hywiki-tests--command-execute #'delete-char -1))) + +(defun hywiki-test--get-buffer-text-point-and-highlight-as-tags () + "An inverse of `hywiki-test--set-buffer-text-point-and-highlight'. +Inserts tags for highlighted areas as well as point." + (hywiki-test--insert-tags (hywiki-get-reference-positions))) + +(defun hywiki-test--insert (string) + "Command to insert a STRING at point." + (interactive "s: ") + (dolist (c (string-to-list string)) + (hywiki-tests--command-execute #'self-insert-command 1 c))) + +;;; Tests to be created ... + +;; text region inserted with no double-quoted string in it; +;; text region inserted with an entire double-quoted string in it; +;; text region inserted with the first part of a double-quoted string there is a string end (double quote); +;; text region inserted with the last part of a double-quoted string and there is a string beginning (double quote); +;; text region inserted with the first part of a double-quoted string but there is no string end (double quote); +;; text region inserted with the last part of a double-quoted string but there is no string beginning (double quote); + +;; All of the same scenarios as the inserts above but with the text yanked rather than inserted. + +;; * Kill / Deletion +;; text region deleted that contains the first part or the last part of a wikiword reference; +;; text region deleted with the first part of a double-quoted string where there is a string end (double quote); +;; text region deleted with the last part of a double-quoted string and there is a string beginning (double quote); +;; text region deleted with the first part of a double-quoted string but there is no string end (double quote); +;; text region deleted with the last part of a double-quoted string but there is no string beginning (double quote). + +(ert-deftest hywiki--verify-get-buffer-text-point-mark-highlight () + (skip-unless (not noninteractive)) ; Only works in interactive mode for now + (hywiki-tests--preserve-hywiki-mode + (let* ((hywiki-directory (make-temp-file "hywiki" t)) + (wikiHi (cdr (hywiki-add-page "Hi"))) + (wikiHo (cdr (hywiki-add-page "Ho"))) + (hywiki-tests--with-face-test t)) + (unwind-protect + (with-temp-buffer + (ert-info ("1" :prefix "Verify point, no highlighting: ") + (hywiki-test--set-buffer-text-point-and-highlight "hej^hopp") + (should (string= "hej^hopp" + (hywiki-test--get-buffer-text-point-and-highlight-as-tags)))) + (ert-info ("2" :prefix "Verify point, no highlighting: ") + (hywiki-test--set-buffer-text-point-and-highlight "hej^hopp") + (forward-char 1) + (should (string= "hejh^opp" + (hywiki-test--get-buffer-text-point-and-highlight-as-tags)))) + (ert-info ("3" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-point-and-highlight "^Hi") + (should (string= "^" + (hywiki-test--get-buffer-text-point-and-highlight-as-tags)))) + (ert-info ("4" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-point-and-highlight "Hi^Ho") + (hywiki-test--insert "\"text\"") + (should (string= "Hi\"text\"^" + (hywiki-test--get-buffer-text-point-and-highlight-as-tags)))) + (ert-info ("5" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-point-and-highlight "Hi^Ho") + (hywiki-test--insert " \"text\"") + (should (string= " \"text\"^" + (hywiki-test--get-buffer-text-point-and-highlight-as-tags))))) + (hy-delete-files-and-buffers (list wikiHi wikiHo)) + (hy-delete-dir-and-buffer hywiki-directory))))) + (provide 'hywiki-tests) ;; This file can't be byte-compiled without the `el-mock' package From abe91465c80f7f58cd623aa7165a9ba8e4824661 Mon Sep 17 00:00:00 2001 From: Mats Lidell Date: Sun, 13 Jul 2025 19:42:20 +0200 Subject: [PATCH 2/6] Refactoring and adding documentation --- test/hywiki-tests.el | 124 +++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 63 deletions(-) diff --git a/test/hywiki-tests.el b/test/hywiki-tests.el index eaa09b26..2c95308c 100644 --- a/test/hywiki-tests.el +++ b/test/hywiki-tests.el @@ -1697,14 +1697,17 @@ Insert test in the middle of other text." ;; for setting the highlighted areas hywiki-mode is used. The tag ;; notation is not used for that. It is only used for the ;; verification. -(defconst hywiki-test--point-tag "^") -(defconst hywiki-test--start-tag "<") -(defconst hywiki-test--end-tag ">") - -;; Main function - reads from current buffer and inserts tags -(defun hywiki-test--insert-tags (positions) - "Insert tags at positions and mark point location in current buffer. -POSITIONS is a list of cons cells (START . END) with 1-based positions." +(defconst hywiki-test--point-char "^") +(defconst hywiki-test--highlight-start-char "<") +(defconst hywiki-test--highlight-end-char ">") + +(defun hywiki-test--insert-chars (positions) + "Insert in string representation of current buffer start, end and point chars. +POSITIONS is a list of cons cells (START . END) with beginning of string +at index 1. For every highlighted word in the buffer a +`hywiki-test--highlight-start-char' and +`hywiki-test--highlight-end-char' surrounding the highlighted word is +inserted. Finally a `hywiki-test--point-char' is inserted where point is." (let* ((buffer-string (buffer-substring-no-properties (point-min) (point-max))) (current-point (point)) ;; Convert 1-based positions to 0-based for string operations @@ -1720,84 +1723,79 @@ POSITIONS is a list of cons cells (START . END) with 1-based positions." ;; First, process all ranges from right to left (dolist (pos sorted-positions) (let* ((start (car pos)) - (end (cdr pos)) ; end is exclusive in our 0-based system + (end (cdr pos)) (before (substring result 0 start)) - (middle (substring result start end)) ; substring is exclusive too + (middle (substring result start end)) (after (substring result end))) - (setq result (concat before hywiki-test--start-tag middle hywiki-test--end-tag after)))) + (setq result (concat before hywiki-test--highlight-start-char middle hywiki-test--highlight-end-char after)))) - ;; Then insert point tag (now we need to adjust for inserted tags) + ;; Then insert point char adjusting for inserted chars. (let* ((point-pos (1- current-point)) - ;; Calculate how many characters we've added before point (tags-before-point 0) - ;; Check if point is within any range (point-in-range nil)) - - ;; Count tag characters and check if point is within ranges + + ;; Count characters and check if point is within ranges (dolist (pos zero-based-positions) (let ((range-start (car pos)) (range-end (cdr pos))) (cond - ;; Point is within this range (not at boundaries) ((and (> point-pos range-start) (< point-pos range-end)) (setq point-in-range t) - ;; Add start-tag length (we're inside the range) - (setq tags-before-point (+ tags-before-point (length hywiki-test--start-tag)))) - ;; Range is completely before point + (setq tags-before-point (+ tags-before-point (length hywiki-test--highlight-start-char)))) ((<= range-end point-pos) (setq tags-before-point (+ tags-before-point - (length hywiki-test--start-tag) - (length hywiki-test--end-tag))))))) + (length hywiki-test--highlight-start-char) + (length hywiki-test--highlight-end-char))))))) - ;; Insert point tag at adjusted position + ;; Insert point char at adjusted position (let* ((adjusted-point (+ point-pos tags-before-point)) (before (substring result 0 adjusted-point)) (after (substring result adjusted-point))) - (setq result (concat before hywiki-test--point-tag after)))) + (setq result (concat before hywiki-test--point-char after)))) result)) -(ert-deftest hywiki-test--insert-tags-test () - "Verify `hywiki-test--insert-tags'." +(ert-deftest hywiki-test--insert--test () + "Verify `hywiki-test--insert-chars'." (with-temp-buffer (insert "Hello World\n") (goto-char 6) - (should (string= (hywiki-test--insert-tags nil) + (should (string= (hywiki-test--insert-chars nil) "Hello^ World\n")) - (should (string= (hywiki-test--insert-tags '((1 . 6) (7 . 12))) + (should (string= (hywiki-test--insert-chars '((1 . 6) (7 . 12))) "^ \n")) (goto-char 5) - (should (string= (hywiki-test--insert-tags '((1 . 6) (7 . 12))) + (should (string= (hywiki-test--insert-chars '((1 . 6) (7 . 12))) " \n")))) -(defun hywiki-test--set-buffer-text-point-and-highlight (description) +(defun hywiki-test--set-buffer-text-with-point-and-highlight (description) "Set the current buffer's text, point and mark according to DESCRIPTION. Erase current buffer and insert DESCRIPTION. Set point to the first -occurrence of `hywiki-test--point-tag' in the buffer, removing it. If -there is no `hywiki-test--point-tag', set point to the beginning of the +occurrence of `hywiki-test--point-char' in the buffer, removing it. If +there is no `hywiki-test--point-char', set point to the beginning of the buffer. End the insertion of text by turning on hywiki-mode and perform a dummy -command to get the pre- and post-hooks executed. This creates the +command to get the pre- and post-hooks executed. This creates the highlighting overlays we want to test." (erase-buffer) (hywiki-mode 0) ; Deactivate hywiki-mode, disable highlighting (insert description) (goto-char (point-min)) - (when (search-forward hywiki-test--point-tag nil t) - (delete-char (- (length hywiki-test--point-tag)))) + (when (search-forward hywiki-test--point-char nil t) + (delete-char (- (length hywiki-test--point-char)))) (hywiki-mode 1) ; Activate hywiki-mode activates highlighting (save-excursion ; Force pre- and post-hooks. (end-of-buffer) (hywiki-tests--command-execute #'self-insert-command 1 ? ) (hywiki-tests--command-execute #'delete-char -1))) -(defun hywiki-test--get-buffer-text-point-and-highlight-as-tags () - "An inverse of `hywiki-test--set-buffer-text-point-and-highlight'. +(defun hywiki-test--get-buffer-text-with-point-and-highlight () + "An inverse of `hywiki-test--set-buffer-text-with-point-and-highlight'. Inserts tags for highlighted areas as well as point." - (hywiki-test--insert-tags (hywiki-get-reference-positions))) + (hywiki-test--insert-chars (hywiki-get-reference-positions))) (defun hywiki-test--insert (string) "Command to insert a STRING at point." @@ -1805,25 +1803,25 @@ Inserts tags for highlighted areas as well as point." (dolist (c (string-to-list string)) (hywiki-tests--command-execute #'self-insert-command 1 c))) -;;; Tests to be created ... +(ert-deftest hywiki--verify-get-buffer-text-with-point-and-highlight () + "Verify proper highlighting after different actions of insertion, killing and deletion. -;; text region inserted with no double-quoted string in it; -;; text region inserted with an entire double-quoted string in it; -;; text region inserted with the first part of a double-quoted string there is a string end (double quote); -;; text region inserted with the last part of a double-quoted string and there is a string beginning (double quote); -;; text region inserted with the first part of a double-quoted string but there is no string end (double quote); -;; text region inserted with the last part of a double-quoted string but there is no string beginning (double quote); +Each test is constructed as three phases: -;; All of the same scenarios as the inserts above but with the text yanked rather than inserted. +* First phase empties the buffer from any previous test and then + prepares the text and sets the point. Hywiki-mode is activated in the + prepare phase in order to set any initial + highlighting. (`hywiki-test--set-buffer-text-with-point-and-highlight') -;; * Kill / Deletion -;; text region deleted that contains the first part or the last part of a wikiword reference; -;; text region deleted with the first part of a double-quoted string where there is a string end (double quote); -;; text region deleted with the last part of a double-quoted string and there is a string beginning (double quote); -;; text region deleted with the first part of a double-quoted string but there is no string end (double quote); -;; text region deleted with the last part of a double-quoted string but there is no string beginning (double quote). +* The second phase performs some action. It can be insertion, killing + or deletion. The action should call the pre- and post-command-hooks + in order for the highlighting overlays to be constructed. -(ert-deftest hywiki--verify-get-buffer-text-point-mark-highlight () +* The third phase does a verification. A representation of the + `buffer-string' as a string is constructed where chars are used for + point, and start and stop of the highlighted WikiWord. + (`hywiki-test--get-buffer-text-with-point-and-highlight'). That is + then compared to the expected string." (skip-unless (not noninteractive)) ; Only works in interactive mode for now (hywiki-tests--preserve-hywiki-mode (let* ((hywiki-directory (make-temp-file "hywiki" t)) @@ -1833,28 +1831,28 @@ Inserts tags for highlighted areas as well as point." (unwind-protect (with-temp-buffer (ert-info ("1" :prefix "Verify point, no highlighting: ") - (hywiki-test--set-buffer-text-point-and-highlight "hej^hopp") + (hywiki-test--set-buffer-text-with-point-and-highlight "hej^hopp") (should (string= "hej^hopp" - (hywiki-test--get-buffer-text-point-and-highlight-as-tags)))) + (hywiki-test--get-buffer-text-with-point-and-highlight)))) (ert-info ("2" :prefix "Verify point, no highlighting: ") - (hywiki-test--set-buffer-text-point-and-highlight "hej^hopp") + (hywiki-test--set-buffer-text-with-point-and-highlight "hej^hopp") (forward-char 1) (should (string= "hejh^opp" - (hywiki-test--get-buffer-text-point-and-highlight-as-tags)))) + (hywiki-test--get-buffer-text-with-point-and-highlight)))) (ert-info ("3" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-point-and-highlight "^Hi") + (hywiki-test--set-buffer-text-with-point-and-highlight "^Hi") (should (string= "^" - (hywiki-test--get-buffer-text-point-and-highlight-as-tags)))) + (hywiki-test--get-buffer-text-with-point-and-highlight)))) (ert-info ("4" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-point-and-highlight "Hi^Ho") + (hywiki-test--set-buffer-text-with-point-and-highlight "Hi^Ho") (hywiki-test--insert "\"text\"") (should (string= "Hi\"text\"^" - (hywiki-test--get-buffer-text-point-and-highlight-as-tags)))) + (hywiki-test--get-buffer-text-with-point-and-highlight)))) (ert-info ("5" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-point-and-highlight "Hi^Ho") + (hywiki-test--set-buffer-text-with-point-and-highlight "Hi^Ho") (hywiki-test--insert " \"text\"") (should (string= " \"text\"^" - (hywiki-test--get-buffer-text-point-and-highlight-as-tags))))) + (hywiki-test--get-buffer-text-with-point-and-highlight))))) (hy-delete-files-and-buffers (list wikiHi wikiHo)) (hy-delete-dir-and-buffer hywiki-directory))))) From d2efbfa2c9dc5db2ced65c301deae1ccc7f039d0 Mon Sep 17 00:00:00 2001 From: Mats Lidell Date: Sun, 13 Jul 2025 20:08:40 +0200 Subject: [PATCH 3/6] Move the yank, kill and insert test to a separate file --- test/hywiki-tests.el | 165 -------------------------------- test/hywiki-yki-tests.el | 198 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+), 165 deletions(-) create mode 100644 test/hywiki-yki-tests.el diff --git a/test/hywiki-tests.el b/test/hywiki-tests.el index 2c95308c..cff84643 100644 --- a/test/hywiki-tests.el +++ b/test/hywiki-tests.el @@ -1691,171 +1691,6 @@ Insert test in the middle of other text." (hy-delete-files-and-buffers (list wikiHi wikiHo)) (hy-delete-dir-and-buffer hywiki-directory))))) -;; Based on functions in simple-test.el add support for also marking -;; highlighted words in buffers using the same idea with point-tag and -;; mark-tag also for start and end of highlighted regions. Note that -;; for setting the highlighted areas hywiki-mode is used. The tag -;; notation is not used for that. It is only used for the -;; verification. -(defconst hywiki-test--point-char "^") -(defconst hywiki-test--highlight-start-char "<") -(defconst hywiki-test--highlight-end-char ">") - -(defun hywiki-test--insert-chars (positions) - "Insert in string representation of current buffer start, end and point chars. -POSITIONS is a list of cons cells (START . END) with beginning of string -at index 1. For every highlighted word in the buffer a -`hywiki-test--highlight-start-char' and -`hywiki-test--highlight-end-char' surrounding the highlighted word is -inserted. Finally a `hywiki-test--point-char' is inserted where point is." - (let* ((buffer-string (buffer-substring-no-properties (point-min) (point-max))) - (current-point (point)) - ;; Convert 1-based positions to 0-based for string operations - ;; Note: Emacs ranges are (start . end) where end is exclusive - (zero-based-positions (mapcar (lambda (pos) - (cons (1- (car pos)) (1- (cdr pos)))) - (or positions '()))) - ;; Sort ranges in reverse order for right-to-left processing - (sorted-positions (sort zero-based-positions - (lambda (a b) (> (car a) (car b))))) - (result buffer-string)) - - ;; First, process all ranges from right to left - (dolist (pos sorted-positions) - (let* ((start (car pos)) - (end (cdr pos)) - (before (substring result 0 start)) - (middle (substring result start end)) - (after (substring result end))) - - (setq result (concat before hywiki-test--highlight-start-char middle hywiki-test--highlight-end-char after)))) - - ;; Then insert point char adjusting for inserted chars. - (let* ((point-pos (1- current-point)) - (tags-before-point 0) - (point-in-range nil)) - - ;; Count characters and check if point is within ranges - (dolist (pos zero-based-positions) - (let ((range-start (car pos)) - (range-end (cdr pos))) - (cond - ((and (> point-pos range-start) (< point-pos range-end)) - (setq point-in-range t) - (setq tags-before-point (+ tags-before-point (length hywiki-test--highlight-start-char)))) - ((<= range-end point-pos) - (setq tags-before-point (+ tags-before-point - (length hywiki-test--highlight-start-char) - (length hywiki-test--highlight-end-char))))))) - - ;; Insert point char at adjusted position - (let* ((adjusted-point (+ point-pos tags-before-point)) - (before (substring result 0 adjusted-point)) - (after (substring result adjusted-point))) - (setq result (concat before hywiki-test--point-char after)))) - - result)) - -(ert-deftest hywiki-test--insert--test () - "Verify `hywiki-test--insert-chars'." - (with-temp-buffer - (insert "Hello World\n") - (goto-char 6) - (should (string= (hywiki-test--insert-chars nil) - "Hello^ World\n")) - (should (string= (hywiki-test--insert-chars '((1 . 6) (7 . 12))) - "^ \n")) - (goto-char 5) - (should (string= (hywiki-test--insert-chars '((1 . 6) (7 . 12))) - " \n")))) - -(defun hywiki-test--set-buffer-text-with-point-and-highlight (description) - "Set the current buffer's text, point and mark according to DESCRIPTION. - -Erase current buffer and insert DESCRIPTION. Set point to the first -occurrence of `hywiki-test--point-char' in the buffer, removing it. If -there is no `hywiki-test--point-char', set point to the beginning of the -buffer. - -End the insertion of text by turning on hywiki-mode and perform a dummy -command to get the pre- and post-hooks executed. This creates the -highlighting overlays we want to test." - (erase-buffer) - (hywiki-mode 0) ; Deactivate hywiki-mode, disable highlighting - (insert description) - (goto-char (point-min)) - (when (search-forward hywiki-test--point-char nil t) - (delete-char (- (length hywiki-test--point-char)))) - (hywiki-mode 1) ; Activate hywiki-mode activates highlighting - (save-excursion ; Force pre- and post-hooks. - (end-of-buffer) - (hywiki-tests--command-execute #'self-insert-command 1 ? ) - (hywiki-tests--command-execute #'delete-char -1))) - -(defun hywiki-test--get-buffer-text-with-point-and-highlight () - "An inverse of `hywiki-test--set-buffer-text-with-point-and-highlight'. -Inserts tags for highlighted areas as well as point." - (hywiki-test--insert-chars (hywiki-get-reference-positions))) - -(defun hywiki-test--insert (string) - "Command to insert a STRING at point." - (interactive "s: ") - (dolist (c (string-to-list string)) - (hywiki-tests--command-execute #'self-insert-command 1 c))) - -(ert-deftest hywiki--verify-get-buffer-text-with-point-and-highlight () - "Verify proper highlighting after different actions of insertion, killing and deletion. - -Each test is constructed as three phases: - -* First phase empties the buffer from any previous test and then - prepares the text and sets the point. Hywiki-mode is activated in the - prepare phase in order to set any initial - highlighting. (`hywiki-test--set-buffer-text-with-point-and-highlight') - -* The second phase performs some action. It can be insertion, killing - or deletion. The action should call the pre- and post-command-hooks - in order for the highlighting overlays to be constructed. - -* The third phase does a verification. A representation of the - `buffer-string' as a string is constructed where chars are used for - point, and start and stop of the highlighted WikiWord. - (`hywiki-test--get-buffer-text-with-point-and-highlight'). That is - then compared to the expected string." - (skip-unless (not noninteractive)) ; Only works in interactive mode for now - (hywiki-tests--preserve-hywiki-mode - (let* ((hywiki-directory (make-temp-file "hywiki" t)) - (wikiHi (cdr (hywiki-add-page "Hi"))) - (wikiHo (cdr (hywiki-add-page "Ho"))) - (hywiki-tests--with-face-test t)) - (unwind-protect - (with-temp-buffer - (ert-info ("1" :prefix "Verify point, no highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "hej^hopp") - (should (string= "hej^hopp" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - (ert-info ("2" :prefix "Verify point, no highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "hej^hopp") - (forward-char 1) - (should (string= "hejh^opp" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - (ert-info ("3" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "^Hi") - (should (string= "^" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - (ert-info ("4" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "Hi^Ho") - (hywiki-test--insert "\"text\"") - (should (string= "Hi\"text\"^" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - (ert-info ("5" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "Hi^Ho") - (hywiki-test--insert " \"text\"") - (should (string= " \"text\"^" - (hywiki-test--get-buffer-text-with-point-and-highlight))))) - (hy-delete-files-and-buffers (list wikiHi wikiHo)) - (hy-delete-dir-and-buffer hywiki-directory))))) - (provide 'hywiki-tests) ;; This file can't be byte-compiled without the `el-mock' package diff --git a/test/hywiki-yki-tests.el b/test/hywiki-yki-tests.el new file mode 100644 index 00000000..03d29ca8 --- /dev/null +++ b/test/hywiki-yki-tests.el @@ -0,0 +1,198 @@ +;;; hywiki-yki.tests --- Yank, kill and insert tests for hywiki -*- lexical-binding: t; -*- +;; +;; Author: Mats Lidell +;; +;; Orig-Date: 13-Jul-25 at 19:50:37 +;; Last-Mod: 13-Jul-25 at 20:07:36 by Mats Lidell +;; +;; SPDX-License-Identifier: GPL-3.0-or-later +;; +;; Copyright (C) 2025 Free Software Foundation, Inc. +;; See the "../HY-COPY" file for license information. +;; +;; This file is part of GNU Hyperbole. + +;;; Commentary: +;; +;; Development area for new hywiki-tests and their support functions. +;; Will eventually, when finished, be moved into "hywiki-test.el" or +;; maybe stay as here in a separate file!? + +;;; Code: + +(require 'ert) +(require 'el-mock) +(require 'ert-x) +(require 'hy-test-helpers) +(require 'hywiki) + +(require 'hywiki-tests) ;; For functions and variables in hywiki-tests + +;; The testing idea here is based on functions in simple-test.el add +;; support for also marking highlighted words in buffers using the +;; same idea with point-tag and mark-tag also for start and end of +;; highlighted regions. Note that for setting the highlighted areas +;; hywiki-mode is used. The tag notation is not used for that. It is +;; only used for the verification. + +(defconst hywiki-test--point-char "^") +(defconst hywiki-test--highlight-start-char "<") +(defconst hywiki-test--highlight-end-char ">") + +(defun hywiki-test--insert-chars (positions) + "Insert in string representation of current buffer start, end and point chars. +POSITIONS is a list of cons cells (START . END) with beginning of string +at index 1. For every highlighted word in the buffer a +`hywiki-test--highlight-start-char' and +`hywiki-test--highlight-end-char' surrounding the highlighted word is +inserted. Finally a `hywiki-test--point-char' is inserted where point is." + (let* ((buffer-string (buffer-substring-no-properties (point-min) (point-max))) + (current-point (point)) + ;; Convert 1-based positions to 0-based for string operations + ;; Note: Emacs ranges are (start . end) where end is exclusive + (zero-based-positions (mapcar (lambda (pos) + (cons (1- (car pos)) (1- (cdr pos)))) + (or positions '()))) + ;; Sort ranges in reverse order for right-to-left processing + (sorted-positions (sort zero-based-positions + (lambda (a b) (> (car a) (car b))))) + (result buffer-string)) + + ;; First, process all ranges from right to left + (dolist (pos sorted-positions) + (let* ((start (car pos)) + (end (cdr pos)) + (before (substring result 0 start)) + (middle (substring result start end)) + (after (substring result end))) + + (setq result (concat before hywiki-test--highlight-start-char middle hywiki-test--highlight-end-char after)))) + + ;; Then insert point char adjusting for inserted chars. + (let* ((point-pos (1- current-point)) + (tags-before-point 0)) + + ;; Count characters and check if point is within ranges + (dolist (pos zero-based-positions) + (let ((range-start (car pos)) + (range-end (cdr pos))) + (cond + ((and (> point-pos range-start) (< point-pos range-end)) + (setq tags-before-point (+ tags-before-point (length hywiki-test--highlight-start-char)))) + ((<= range-end point-pos) + (setq tags-before-point (+ tags-before-point + (length hywiki-test--highlight-start-char) + (length hywiki-test--highlight-end-char))))))) + + ;; Insert point char at adjusted position + (let* ((adjusted-point (+ point-pos tags-before-point)) + (before (substring result 0 adjusted-point)) + (after (substring result adjusted-point))) + (setq result (concat before hywiki-test--point-char after)))) + + result)) + +(ert-deftest hywiki-test--insert--test () + "Verify `hywiki-test--insert-chars'." + (with-temp-buffer + (insert "Hello World\n") + (goto-char 6) + (should (string= (hywiki-test--insert-chars nil) + "Hello^ World\n")) + (should (string= (hywiki-test--insert-chars '((1 . 6) (7 . 12))) + "^ \n")) + (goto-char 5) + (should (string= (hywiki-test--insert-chars '((1 . 6) (7 . 12))) + " \n")))) + +(defun hywiki-test--set-buffer-text-with-point-and-highlight (description) + "Set the current buffer's text, point and mark according to DESCRIPTION. + +Erase current buffer and insert DESCRIPTION. Set point to the first +occurrence of `hywiki-test--point-char' in the buffer, removing it. If +there is no `hywiki-test--point-char', set point to the beginning of the +buffer. + +End the insertion of text by turning on hywiki-mode and perform a dummy +command to get the pre- and post-hooks executed. This creates the +highlighting overlays we want to test." + (erase-buffer) + (hywiki-mode 0) ; Deactivate hywiki-mode, disable highlighting + (insert description) + (goto-char (point-min)) + (when (search-forward hywiki-test--point-char nil t) + (delete-char (- (length hywiki-test--point-char)))) + (hywiki-mode 1) ; Activate hywiki-mode activates highlighting + (save-excursion ; Force pre- and post-hooks. + (goto-char (point-max)) + (hywiki-tests--command-execute #'self-insert-command 1 ? ) + (hywiki-tests--command-execute #'delete-char -1))) + +(defun hywiki-test--get-buffer-text-with-point-and-highlight () + "An inverse of `hywiki-test--set-buffer-text-with-point-and-highlight'. +Inserts tags for highlighted areas as well as point." + (hywiki-test--insert-chars (hywiki-get-reference-positions))) + +(defun hywiki-test--insert (string) + "Command to insert a STRING at point." + (interactive "s: ") + (dolist (c (string-to-list string)) + (hywiki-tests--command-execute #'self-insert-command 1 c))) + +(ert-deftest hywiki--verify-get-buffer-text-with-point-and-highlight () + "Verify proper highlighting after different editing actions. +Actions can be insertion, killing and deletion. + +Each test is constructed as three phases: + +* First phase empties the buffer from any previous test and then + prepares the text and sets the point. Hywiki-mode is activated in the + prepare phase in order to set any initial + highlighting. (`hywiki-test--set-buffer-text-with-point-and-highlight') + +* The second phase performs some action. It can be insertion, killing + or deletion. The action should call the pre- and post-command-hooks + in order for the highlighting overlays to be constructed. + +* The third phase does a verification. A representation of the + `buffer-string' as a string is constructed where chars are used for + point, and start and stop of the highlighted WikiWord. + (`hywiki-test--get-buffer-text-with-point-and-highlight'). That is + then compared to the expected string." + (skip-unless (not noninteractive)) ; Only works in interactive mode for now + (hywiki-tests--preserve-hywiki-mode + (let* ((hywiki-directory (make-temp-file "hywiki" t)) + (wikiHi (cdr (hywiki-add-page "Hi"))) + (wikiHo (cdr (hywiki-add-page "Ho"))) + (hywiki-tests--with-face-test t)) + (unwind-protect + (with-temp-buffer + (ert-info ("1" :prefix "Verify point, no highlighting: ") + (hywiki-test--set-buffer-text-with-point-and-highlight "hej^hopp") + (should (string= "hej^hopp" + (hywiki-test--get-buffer-text-with-point-and-highlight)))) + (ert-info ("2" :prefix "Verify point, no highlighting: ") + (hywiki-test--set-buffer-text-with-point-and-highlight "hej^hopp") + (forward-char 1) + (should (string= "hejh^opp" + (hywiki-test--get-buffer-text-with-point-and-highlight)))) + (ert-info ("3" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-with-point-and-highlight "^Hi") + (should (string= "^" + (hywiki-test--get-buffer-text-with-point-and-highlight)))) + (ert-info ("4" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-with-point-and-highlight "Hi^Ho") + (hywiki-test--insert "\"text\"") + (should (string= "Hi\"text\"^" + (hywiki-test--get-buffer-text-with-point-and-highlight)))) + (ert-info ("5" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-with-point-and-highlight "Hi^Ho") + (hywiki-test--insert " \"text\"") + (should (string= " \"text\"^" + (hywiki-test--get-buffer-text-with-point-and-highlight))))) + (hy-delete-files-and-buffers (list wikiHi wikiHo)) + (hy-delete-dir-and-buffer hywiki-directory))))) + +(provide 'hywiki-yki-tests) + +;;; hywiki-yki-tests.el ends here From 781af100d5374163d0df1afde30eee4521851185 Mon Sep 17 00:00:00 2001 From: Mats Lidell Date: Tue, 15 Jul 2025 11:10:10 +0200 Subject: [PATCH 4/6] Add some tests from examples --- test/hywiki-yki-tests.el | 81 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/test/hywiki-yki-tests.el b/test/hywiki-yki-tests.el index 03d29ca8..c5f94ff1 100644 --- a/test/hywiki-yki-tests.el +++ b/test/hywiki-yki-tests.el @@ -3,7 +3,7 @@ ;; Author: Mats Lidell ;; ;; Orig-Date: 13-Jul-25 at 19:50:37 -;; Last-Mod: 13-Jul-25 at 20:07:36 by Mats Lidell +;; Last-Mod: 15-Jul-25 at 11:07:32 by Mats Lidell ;; ;; SPDX-License-Identifier: GPL-3.0-or-later ;; @@ -164,6 +164,7 @@ Each test is constructed as three phases: (let* ((hywiki-directory (make-temp-file "hywiki" t)) (wikiHi (cdr (hywiki-add-page "Hi"))) (wikiHo (cdr (hywiki-add-page "Ho"))) + (wikiWord (cdr (hywiki-add-page "WikiWord"))) (hywiki-tests--with-face-test t)) (unwind-protect (with-temp-buffer @@ -189,8 +190,84 @@ Each test is constructed as three phases: (hywiki-test--set-buffer-text-with-point-and-highlight "Hi^Ho") (hywiki-test--insert " \"text\"") (should (string= " \"text\"^" + (hywiki-test--get-buffer-text-with-point-and-highlight)))) + + ;; PASS: WikiWord -> highlight {WikiWord} after delete + (ert-info ("6" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-with-point-and-highlight "Wiki^delete-regionWord") + (hywiki-tests--command-execute #'delete-region (point) + (+ (point) (length "delete-region"))) + (should (string= "" + (hywiki-test--get-buffer-text-with-point-and-highlight)))) + + ;; PASS: Wiki#secWord -> no highlight after adding "tion" + (ert-info ("7" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-with-point-and-highlight "Wiki#sec^tionWord") + (hywiki-tests--command-execute #'delete-region (point) + (+ (point) (length "tion"))) + (should (string= "Wiki#sec^Word" + (hywiki-test--get-buffer-text-with-point-and-highlight)))) + + ;; PASS: Wiki<#section>Word -> highlight {WikiWord} after delete of "#section" + (ert-info ("8" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-with-point-and-highlight "Wiki^#sectionWord") + (hywiki-tests--command-execute #'delete-region (point) + (+ (point) (length "#section"))) + (should (string= "" + (hywiki-test--get-buffer-text-with-point-and-highlight)))) + + ;; PASS: WikiWord -> dehighlight "WikiWord" + (ert-info ("8" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-with-point-and-highlight "WikiWo^kill-wordrd") + (hywiki-tests--command-execute #'delete-region (point) + (+ (point) (length "kill-word"))) + (should (string= "" + (hywiki-test--get-buffer-text-with-point-and-highlight)))) + + ;; PASS: "WikiWord#section with spaces" -> shrink highlight + ;; to {WikiWord#section} with this operation: + ;; "WikiWord#section with spaces" + (ert-info ("9" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-with-point-and-highlight "^\"WikiWord#section with spaces\"") + (hywiki-tests--command-execute #'delete-char 1) + (should (string= "^ with spaces\"" + (hywiki-test--get-buffer-text-with-point-and-highlight)))) + + ;; PASS: "WikiWord#section" -> no + ;; highlight change "{WikiWord#section} + (ert-info ("10" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-with-point-and-highlight "\"WikiWord#section\"^") + (hywiki-tests--command-execute #'backward-delete-char-untabify 1) + (should (string= "\"^" + (hywiki-test--get-buffer-text-with-point-and-highlight)))) + + ;; FAIL: "WikiWord#section with + ;; spaces" -> shrink highlight to + ;; "{WikiWord#section} with spaces + (ert-info ("11" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-with-point-and-highlight "\"WikiWord#section with spaces\"^") + (hywiki-tests--command-execute #'backward-delete-char-untabify 1) + (should (string= "\"^" ;;; <= FAIL: Not correct highlight + (hywiki-test--get-buffer-text-with-point-and-highlight)))) + + ;; FAIL: WikiWord abc WikiWord -> improperly + ;; dehighlights *SECOND* WikiWord + (ert-info ("12" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-with-point-and-highlight "WikiWord ^ abc WikiWord") + (hywiki-tests--command-execute #'delete-region (point) + (+ (point) (length " abc "))) ; Delete trailing space. + (should (string= " ^WikiWord" ;;; <= FAIL: Not correct highlight + (hywiki-test--get-buffer-text-with-point-and-highlight)))) + + ;; PASS: WikiWord abc WikiWord -> *BOTH + ;; WikiWord's are highlighted + (ert-info ("13" :prefix "Verify highlighting: ") + (hywiki-test--set-buffer-text-with-point-and-highlight "WikiWord ^ abc WikiWord") + (hywiki-tests--command-execute #'delete-region (point) + (+ (point) (length " abc"))) + (should (string= " ^ " (hywiki-test--get-buffer-text-with-point-and-highlight))))) - (hy-delete-files-and-buffers (list wikiHi wikiHo)) + (hy-delete-files-and-buffers (list wikiHi wikiHo wikiWord)) (hy-delete-dir-and-buffer hywiki-directory))))) (provide 'hywiki-yki-tests) From d844d8986b7ea62f462a4d37763d6211da79eb0d Mon Sep 17 00:00:00 2001 From: Mats Lidell Date: Sun, 27 Jul 2025 00:38:37 +0200 Subject: [PATCH 5/6] Use a compact notation for pre, post and exec --- test/hywiki-yki-tests.el | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/test/hywiki-yki-tests.el b/test/hywiki-yki-tests.el index c5f94ff1..2af00b29 100644 --- a/test/hywiki-yki-tests.el +++ b/test/hywiki-yki-tests.el @@ -3,7 +3,7 @@ ;; Author: Mats Lidell ;; ;; Orig-Date: 13-Jul-25 at 19:50:37 -;; Last-Mod: 15-Jul-25 at 11:07:32 by Mats Lidell +;; Last-Mod: 15-Jul-25 at 22:55:07 by Mats Lidell ;; ;; SPDX-License-Identifier: GPL-3.0-or-later ;; @@ -270,6 +270,31 @@ Each test is constructed as three phases: (hy-delete-files-and-buffers (list wikiHi wikiHo wikiWord)) (hy-delete-dir-and-buffer hywiki-directory))))) +(ert-deftest hywiki--verify-get-buffer-text-with-point-and-highlight-compact () + "Example test with more compact notation using `cl-flet'. +Can be expanded with alternatives for insert, delete and yank instead if +exec which does not cover all cases." + (skip-unless (not noninteractive)) ; Only works in interactive mode for now + (hywiki-tests--preserve-hywiki-mode + (let* ((hywiki-directory (make-temp-file "hywiki" t)) + (wikiHi (cdr (hywiki-add-page "Hi"))) + (wikiHo (cdr (hywiki-add-page "Ho"))) + (wikiWord (cdr (hywiki-add-page "WikiWord"))) + (hywiki-tests--with-face-test t)) + (cl-flet ((pre: (start) + (hywiki-test--set-buffer-text-with-point-and-highlight start)) + (exec: (cmd &rest args) + (apply #'hywiki-tests--command-execute cmd args)) + (post: (stop) + (should (string= stop (hywiki-test--get-buffer-text-with-point-and-highlight))))) + (unwind-protect + (ert-info ("1" :prefix "Verify highlighting: ") + (pre: "WikiWord ^ abc WikiWord") + (exec: #'delete-region (point) (+ (point) (length " abc"))) + (post: " ^ ")) + (hy-delete-files-and-buffers (list wikiHi wikiHo wikiWord)) + (hy-delete-dir-and-buffer hywiki-directory)))))) + (provide 'hywiki-yki-tests) ;;; hywiki-yki-tests.el ends here From 50414e78f71bfce93cb80196c1d594e49596ea3d Mon Sep 17 00:00:00 2001 From: Mats Lidell Date: Sun, 27 Jul 2025 22:17:10 +0200 Subject: [PATCH 6/6] Further simplify test and helper methods --- test/hywiki-yki-tests.el | 269 ++++++++++++++++++--------------------- 1 file changed, 122 insertions(+), 147 deletions(-) diff --git a/test/hywiki-yki-tests.el b/test/hywiki-yki-tests.el index 2af00b29..511f483a 100644 --- a/test/hywiki-yki-tests.el +++ b/test/hywiki-yki-tests.el @@ -105,6 +105,22 @@ inserted. Finally a `hywiki-test--point-char' is inserted where point is." (should (string= (hywiki-test--insert-chars '((1 . 6) (7 . 12))) " \n")))) +(defun hywiki-test--insert (string) + "Command to insert a STRING at point." + (interactive "s: ") + (dolist (c (string-to-list string)) + (hywiki-tests--command-execute #'self-insert-command 1 c))) + +(defun hywiki-test--insert-with-point (string) + "Insert STRING and return new POINT pos given by `hywiki-test--point-char'. +The point char is not inserted." + (interactive "s: ") + (let ((pos (point))) + (dolist (c (string-to-list string) pos) + (if (equal (char-to-string c) hywiki-test--point-char) + (setq pos (point)) + (hywiki-tests--command-execute #'self-insert-command 1 c))))) + (defun hywiki-test--set-buffer-text-with-point-and-highlight (description) "Set the current buffer's text, point and mark according to DESCRIPTION. @@ -117,48 +133,33 @@ End the insertion of text by turning on hywiki-mode and perform a dummy command to get the pre- and post-hooks executed. This creates the highlighting overlays we want to test." (erase-buffer) - (hywiki-mode 0) ; Deactivate hywiki-mode, disable highlighting - (insert description) - (goto-char (point-min)) - (when (search-forward hywiki-test--point-char nil t) - (delete-char (- (length hywiki-test--point-char)))) - (hywiki-mode 1) ; Activate hywiki-mode activates highlighting - (save-excursion ; Force pre- and post-hooks. - (goto-char (point-max)) - (hywiki-tests--command-execute #'self-insert-command 1 ? ) - (hywiki-tests--command-execute #'delete-char -1))) + (hywiki-mode 1) + (goto-char (hywiki-test--insert-with-point description))) (defun hywiki-test--get-buffer-text-with-point-and-highlight () "An inverse of `hywiki-test--set-buffer-text-with-point-and-highlight'. Inserts tags for highlighted areas as well as point." (hywiki-test--insert-chars (hywiki-get-reference-positions))) -(defun hywiki-test--insert (string) - "Command to insert a STRING at point." - (interactive "s: ") - (dolist (c (string-to-list string)) - (hywiki-tests--command-execute #'self-insert-command 1 c))) - -(ert-deftest hywiki--verify-get-buffer-text-with-point-and-highlight () +(ert-deftest hywiki--verify-get-buffer-text-with-point-and-highlight-compact () "Verify proper highlighting after different editing actions. -Actions can be insertion, killing and deletion. +Actions can be move, insertion, killing and deletion. Each test is constructed as three phases: -* First phase empties the buffer from any previous test and then +* First phase, pre:, empties the buffer from any previous test and then prepares the text and sets the point. Hywiki-mode is activated in the prepare phase in order to set any initial - highlighting. (`hywiki-test--set-buffer-text-with-point-and-highlight') + highlighting. * The second phase performs some action. It can be insertion, killing or deletion. The action should call the pre- and post-command-hooks in order for the highlighting overlays to be constructed. -* The third phase does a verification. A representation of the +* The third phase, post:, does a verification. A representation of the `buffer-string' as a string is constructed where chars are used for - point, and start and stop of the highlighted WikiWord. - (`hywiki-test--get-buffer-text-with-point-and-highlight'). That is - then compared to the expected string." + point, and start and stop of the highlighting with angle brackets. + That is then compared to the expected string." (skip-unless (not noninteractive)) ; Only works in interactive mode for now (hywiki-tests--preserve-hywiki-mode (let* ((hywiki-directory (make-temp-file "hywiki" t)) @@ -166,134 +167,108 @@ Each test is constructed as three phases: (wikiHo (cdr (hywiki-add-page "Ho"))) (wikiWord (cdr (hywiki-add-page "WikiWord"))) (hywiki-tests--with-face-test t)) - (unwind-protect - (with-temp-buffer - (ert-info ("1" :prefix "Verify point, no highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "hej^hopp") - (should (string= "hej^hopp" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - (ert-info ("2" :prefix "Verify point, no highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "hej^hopp") - (forward-char 1) - (should (string= "hejh^opp" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - (ert-info ("3" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "^Hi") - (should (string= "^" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - (ert-info ("4" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "Hi^Ho") - (hywiki-test--insert "\"text\"") - (should (string= "Hi\"text\"^" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - (ert-info ("5" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "Hi^Ho") - (hywiki-test--insert " \"text\"") - (should (string= " \"text\"^" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - - ;; PASS: WikiWord -> highlight {WikiWord} after delete - (ert-info ("6" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "Wiki^delete-regionWord") - (hywiki-tests--command-execute #'delete-region (point) - (+ (point) (length "delete-region"))) - (should (string= "" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - - ;; PASS: Wiki#secWord -> no highlight after adding "tion" - (ert-info ("7" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "Wiki#sec^tionWord") - (hywiki-tests--command-execute #'delete-region (point) - (+ (point) (length "tion"))) - (should (string= "Wiki#sec^Word" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - - ;; PASS: Wiki<#section>Word -> highlight {WikiWord} after delete of "#section" - (ert-info ("8" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "Wiki^#sectionWord") - (hywiki-tests--command-execute #'delete-region (point) - (+ (point) (length "#section"))) - (should (string= "" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - - ;; PASS: WikiWord -> dehighlight "WikiWord" - (ert-info ("8" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "WikiWo^kill-wordrd") - (hywiki-tests--command-execute #'delete-region (point) - (+ (point) (length "kill-word"))) - (should (string= "" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - - ;; PASS: "WikiWord#section with spaces" -> shrink highlight - ;; to {WikiWord#section} with this operation: - ;; "WikiWord#section with spaces" - (ert-info ("9" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "^\"WikiWord#section with spaces\"") - (hywiki-tests--command-execute #'delete-char 1) - (should (string= "^ with spaces\"" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - - ;; PASS: "WikiWord#section" -> no - ;; highlight change "{WikiWord#section} - (ert-info ("10" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "\"WikiWord#section\"^") - (hywiki-tests--command-execute #'backward-delete-char-untabify 1) - (should (string= "\"^" - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - - ;; FAIL: "WikiWord#section with - ;; spaces" -> shrink highlight to - ;; "{WikiWord#section} with spaces - (ert-info ("11" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "\"WikiWord#section with spaces\"^") - (hywiki-tests--command-execute #'backward-delete-char-untabify 1) - (should (string= "\"^" ;;; <= FAIL: Not correct highlight - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - - ;; FAIL: WikiWord abc WikiWord -> improperly - ;; dehighlights *SECOND* WikiWord - (ert-info ("12" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "WikiWord ^ abc WikiWord") - (hywiki-tests--command-execute #'delete-region (point) - (+ (point) (length " abc "))) ; Delete trailing space. - (should (string= " ^WikiWord" ;;; <= FAIL: Not correct highlight - (hywiki-test--get-buffer-text-with-point-and-highlight)))) - - ;; PASS: WikiWord abc WikiWord -> *BOTH - ;; WikiWord's are highlighted - (ert-info ("13" :prefix "Verify highlighting: ") - (hywiki-test--set-buffer-text-with-point-and-highlight "WikiWord ^ abc WikiWord") - (hywiki-tests--command-execute #'delete-region (point) - (+ (point) (length " abc"))) - (should (string= " ^ " - (hywiki-test--get-buffer-text-with-point-and-highlight))))) - (hy-delete-files-and-buffers (list wikiHi wikiHo wikiWord)) - (hy-delete-dir-and-buffer hywiki-directory))))) - -(ert-deftest hywiki--verify-get-buffer-text-with-point-and-highlight-compact () - "Example test with more compact notation using `cl-flet'. -Can be expanded with alternatives for insert, delete and yank instead if -exec which does not cover all cases." - (skip-unless (not noninteractive)) ; Only works in interactive mode for now - (hywiki-tests--preserve-hywiki-mode - (let* ((hywiki-directory (make-temp-file "hywiki" t)) - (wikiHi (cdr (hywiki-add-page "Hi"))) - (wikiHo (cdr (hywiki-add-page "Ho"))) - (wikiWord (cdr (hywiki-add-page "WikiWord"))) - (hywiki-tests--with-face-test t)) - (cl-flet ((pre: (start) + (cl-flet* ((pre: (start) (hywiki-test--set-buffer-text-with-point-and-highlight start)) (exec: (cmd &rest args) (apply #'hywiki-tests--command-execute cmd args)) + (del: (str) + (exec: #'delete-region (point) (+ (point) (length str)))) (post: (stop) (should (string= stop (hywiki-test--get-buffer-text-with-point-and-highlight))))) - (unwind-protect - (ert-info ("1" :prefix "Verify highlighting: ") - (pre: "WikiWord ^ abc WikiWord") - (exec: #'delete-region (point) (+ (point) (length " abc"))) - (post: " ^ ")) - (hy-delete-files-and-buffers (list wikiHi wikiHo wikiWord)) - (hy-delete-dir-and-buffer hywiki-directory)))))) + (unwind-protect + (progn + (ert-info ("1" :prefix "Verify point, no highlighting:") + (pre: "hej^hopp") + (post: "hej^hopp")) + (ert-info ("2" :prefix "Verify point, no highlighting: ") + (pre: "hej^hopp") + (forward-char 1) + (post: "hejh^opp")) + (ert-info ("3" :prefix "Verify highlighting: ") + (pre: "^Hi") + (post: "^")) + (ert-info ("4" :prefix "Verify highlighting: ") + (pre: "Hi^Ho") + (hywiki-test--insert "\"text\"") + (post: "Hi\"text\"^")) + (ert-info ("5" :prefix "Verify highlighting: ") + (pre: "Hi^Ho") + (hywiki-test--insert " \"text\"") + (post: " \"text\"^")) + + ;; PASS: WikiWord -> highlight {WikiWord} after delete + (ert-info ("6" :prefix "Verify highlighting: ") + (pre: "Wiki^delete-regionWord") + (del: "delete-region") + (post: "")) + + ;; PASS: Wiki#secWord -> no highlight after adding "tion" + (ert-info ("7" :prefix "Verify highlighting: ") + (pre: "Wiki#sec^tionWord") + (del: "tion") + (post: "Wiki#sec^Word")) + + ;; PASS: Wiki<#section>Word -> highlight {WikiWord} after delete of "#section" + (ert-info ("8" :prefix "Verify highlighting: ") + (pre: "Wiki^#sectionWord") + (del: "#section") + (post: "")) + + ;; PASS: WikiWord -> dehighlight "WikiWord" + (ert-info ("8" :prefix "Verify highlighting: ") + (pre: "WikiWo^kill-wordrd") + (del: "kill-word") + (post: "")) + + ;; PASS: "WikiWord#section with spaces" -> shrink highlight + ;; to {WikiWord#section} with this operation: + ;; "WikiWord#section with spaces" + (ert-info ("9" :prefix "Verify highlighting: ") + (pre: "^\"WikiWord#section with spaces\"") + (exec: #'delete-char 1) + (post: "^ with spaces\"")) + + ;; PASS: "WikiWord#section" -> no + ;; highlight change "{WikiWord#section} + (ert-info ("10" :prefix "Verify highlighting: ") + (pre: "\"WikiWord#section\"^") + (exec: #'backward-delete-char-untabify 1) + (post: "\"^")) + + ;; FAIL: "WikiWord#section with + ;; spaces" -> shrink highlight to + ;; "{WikiWord#section} with spaces + (ert-info ("11" :prefix "Verify highlighting: ") + (pre: "\"WikiWord#section with spaces\"^") + (exec: #'backward-delete-char-untabify 1) + (post: "\"^")) ;;; <= FAIL: Not correct highlight + + ;; PASS: WikiWord abc WikiWord + (ert-info ("12" :prefix "Verify highlighting: ") + (pre: "WikiWord ^abc WikiWord") + (del: "abc ") + (post: " ^")) + + ;; PASS: WikiWord abc WikiWord + (ert-info ("13" :prefix "Verify highlighting: ") + (pre: "WikiWord ^abc WikiWord") + (del: "abc") + (post: " ^ ")) ;;; <= FAIL: Not correct highlight + + ;; FAIL: WikiWord abc WikiWord -> does not highlight *SECOND* WikiWord + (ert-info ("14" :prefix "Verify highlighting: ") + (pre: "WikiWord ^ abc WikiWord") + (del: " abc ") + (post: " ^WikiWord")) ;;; <= FAIL: Not correct highlight + + ;; PASS: WikiWord abc WikiWord + (ert-info ("15" :prefix "Verify highlighting: ") + (pre: "WikiWord ^ abc WikiWord") + (del: " abc") + (post: " ^ "))) + + (hy-delete-files-and-buffers (list wikiHi wikiHo wikiWord)) + (hy-delete-dir-and-buffer hywiki-directory)))))) (provide 'hywiki-yki-tests)