branch: externals/hyperbole
commit c4325885a0cda7671520ea5e72946e3938917d6f
Author: Mats Lidell <[email protected]>
Commit: GitHub <[email protected]>
Add single file match tests for some hyrolo hide and show commands (#458)
Includes helper method for turning a buffer with invisible regions
into a plain string with the ellipses indicating where the invisible
portions for the file are.
---
ChangeLog | 9 +++
test/hyrolo-tests.el | 190 ++++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 198 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index 6b76a994c7..6d7b775e4c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2024-01-30 Mats Lidell <[email protected]>
+
+* test/hyrolo-tests.el (hyrolo-tests--outline-as-string): Add helper that
+ returns how the buffer looks to the eye.
+ (hyrolo-tests--outline-hide-other)
+ (hyrolo-tests--outline-hide-sublevels)
+ (hyrolo-tests--hyrolo-outline-show-subtree): Add tests for hyrolo
+ outline show and hide commands.
+
2024-01-28 Bob Weiner <[email protected]>
* hyrolo.el (hyrolo-org-outline-level): Fix to not move point; was causing
errors
diff --git a/test/hyrolo-tests.el b/test/hyrolo-tests.el
index 064dff8708..fcf692f4fd 100644
--- a/test/hyrolo-tests.el
+++ b/test/hyrolo-tests.el
@@ -3,7 +3,7 @@
;; Author: Mats Lidell <[email protected]>
;;
;; Orig-Date: 19-Jun-21 at 22:42:00
-;; Last-Mod: 28-Jan-24 at 15:51:04 by Bob Weiner
+;; Last-Mod: 30-Jan-24 at 14:24:16 by Mats Lidell
;;
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
@@ -1067,5 +1067,193 @@ body
(kill-buffer hyrolo-display-buffer)
(hy-delete-files-and-buffers hyrolo-file-list))))
+(defun hyrolo-tests--outline-as-string (&optional begin end)
+ "Return buffer content as a string with hidden text replaced by ellipses.
+The string contains what the outline actually looks like. This
+enables `string-match' tests for verifying text is hidden. With
+optional BEGIN and END only return that part of the buffer."
+ (if (not begin) (setq begin (point-min)))
+ (if (not end) (setq end (point-max)))
+ (save-excursion
+ (let ((result "")
+ in-invisible-section)
+ (goto-char begin)
+ (while (< (point) end)
+ (setq result
+ (concat result
+ (if (get-char-property (point) 'invisible)
+ (cond ((not in-invisible-section)
+ (setq in-invisible-section t)
+ "...")
+ (t nil))
+ (progn
+ (if in-invisible-section
+ (setq in-invisible-section nil))
+ (char-to-string (char-after))))))
+ (goto-char (1+ (point))))
+ result)))
+
+(ert-deftest hyrolo-tests--outline-hide-other ()
+ "Verify `hyrolo-outline-hide-other' hides except current body, parent and
top-level headings."
+ (let* ((org-file1 (make-temp-file "hypb" nil ".org"
hyrolo-tests--outline-content-org))
+ (hyrolo-file-list (list org-file1)))
+ (unwind-protect
+ (progn
+ (hyrolo-grep "body")
+
+ ;; First line
+ (should (= (point) 1))
+ (hyrolo-outline-hide-other)
+ (should (string-match-p
+ (concat "^===+" (regexp-quote "...\n* h-org 1...\n* h-org
2...\n") "$")
+ (hyrolo-tests--outline-as-string)))
+
+ ;; On first header
+ (goto-char (point-min))
+ (hyrolo-outline-show-all)
+ (search-forward "* h-org 1")
+ (beginning-of-line)
+ (hyrolo-outline-hide-other)
+ (should (string= "...\n* h-org 1\nbody...\n* h-org 2...\n"
+ (hyrolo-tests--outline-as-string)))
+ (should (string= "* h-org 1\nbody...\n* h-org 2...\n"
+ (hyrolo-tests--outline-as-string (point))))
+
+ ;; On second header
+ (goto-char (point-min))
+ (hyrolo-outline-show-all)
+ (search-forward "** h-org 1.1")
+ (beginning-of-line)
+ (hyrolo-outline-hide-other)
+ (should (string= "...\n* h-org 1\n...\n** h-org 1.1\nbody...\n*
h-org 2...\n"
+ (hyrolo-tests--outline-as-string)))
+ (should (string= "** h-org 1.1\nbody...\n* h-org 2...\n"
+ (hyrolo-tests--outline-as-string (point)))))
+ (kill-buffer hyrolo-display-buffer)
+ (hy-delete-files-and-buffers hyrolo-file-list))))
+
+
+(ert-deftest hyrolo-tests--outline-hide-sublevels ()
+ "Verify `hyrolo-outline-hide-sublevels' hides everything but the top levels."
+ (let* ((org-file1 (make-temp-file "hypb" nil ".org"
hyrolo-tests--outline-content-org))
+ (hyrolo-file-list (list org-file1)))
+ (unwind-protect
+ (progn
+ (hyrolo-grep "body")
+
+ ;; First line
+ (should (= (point) 1))
+ (hyrolo-outline-hide-sublevels 1)
+ (should (= (point) 1))
+ (should (string= "...\n* h-org 1...\n* h-org 2...\n"
+ (hyrolo-tests--outline-as-string)))
+
+ ;; On first header
+ (goto-char (point-min))
+ (hyrolo-outline-show-all)
+ (search-forward "* h-org 1")
+ (beginning-of-line)
+ (hyrolo-outline-hide-sublevels 1)
+ (should (string= "...\n* h-org 1...\n* h-org 2...\n"
+ (hyrolo-tests--outline-as-string)))
+ (should (string= "* h-org 1...\n* h-org 2...\n"
+ (hyrolo-tests--outline-as-string (point))))
+
+ ;; On second header
+ (goto-char (point-min))
+ (hyrolo-outline-show-all)
+ (search-forward "** h-org 1.1")
+ (beginning-of-line)
+ (hyrolo-outline-hide-sublevels 1)
+ (should (string= "...\n* h-org 1...\n* h-org 2...\n"
+ (hyrolo-tests--outline-as-string)))
+ (should (string= "1...\n* h-org 2...\n"
+ (hyrolo-tests--outline-as-string (point))))
+
+ ;; First line - 2 levels
+ (goto-char (point-min))
+ (should (= (point) 1))
+ (hyrolo-outline-hide-sublevels 2)
+ (should (= (point) 1))
+ (should (string= "...\n* h-org 1...\n** h-org 1.1...\n** h-org
1.2...\n* h-org 2...\n** h-org-2.1...\n"
+ (hyrolo-tests--outline-as-string)))
+
+ ;; On first header - 2 levels
+ (goto-char (point-min))
+ (hyrolo-outline-show-all)
+ (search-forward "* h-org 1")
+ (beginning-of-line)
+ (hyrolo-outline-hide-sublevels 2)
+ (should (string= "...\n* h-org 1...\n** h-org 1.1...\n** h-org
1.2...\n* h-org 2...\n** h-org-2.1...\n"
+ (hyrolo-tests--outline-as-string)))
+ (should (string= "* h-org 1...\n** h-org 1.1...\n** h-org 1.2...\n*
h-org 2...\n** h-org-2.1...\n"
+ (hyrolo-tests--outline-as-string (point))))
+
+ ;; On second header - 2 levels
+ (goto-char (point-min))
+ (hyrolo-outline-show-all)
+ (search-forward "** h-org 1.1")
+ (beginning-of-line)
+ (hyrolo-outline-hide-sublevels 2)
+ (should (string=
+ "...\n* h-org 1...\n** h-org 1.1...\n** h-org 1.2...\n*
h-org 2...\n** h-org-2.1...\n"
+ (hyrolo-tests--outline-as-string)))
+ (should (string= "** h-org 1.1...\n** h-org 1.2...\n* h-org 2...\n**
h-org-2.1...\n"
+ (hyrolo-tests--outline-as-string (point)))))
+ (kill-buffer hyrolo-display-buffer)
+ (hy-delete-files-and-buffers hyrolo-file-list))))
+
+(ert-deftest hyrolo-tests--hyrolo-outline-show-subtree ()
+ "Verify `hyrolo-hyrolo-outline-show-subtree' shows everything after heading
at deeper levels."
+ (let* ((org-file1 (make-temp-file "hypb" nil ".org"
hyrolo-tests--outline-content-org))
+ (hyrolo-file-list (list org-file1)))
+ (unwind-protect
+ (progn
+ (hyrolo-grep "body")
+
+ ;; First line - show all
+ (should (= (point) 1))
+ (let ((original-look (hyrolo-tests--outline-as-string)))
+ (hyrolo-outline-hide-subtree)
+ (should (string-match-p
+ (concat "^===+" (regexp-quote "...") "\n$")
+ (hyrolo-tests--outline-as-string)))
+ (hyrolo-outline-show-subtree)
+ (should (string= original-look (hyrolo-tests--outline-as-string))))
+
+ ;; On first header
+ (goto-char (point-min))
+ (hyrolo-outline-show-all)
+ (hyrolo-outline-hide-sublevels 1)
+ (search-forward "* h-org 1")
+ (beginning-of-line)
+ (let ((original-look (hyrolo-tests--outline-as-string)))
+ (hyrolo-outline-show-subtree)
+ (should (string= "...\n* h-org 1\nbody\n** h-org 1.1\nbody\n**
h-org 1.2\nbody\n*** h-org 1.2.1\nbody\n* h-org 2...\n"
+ (hyrolo-tests--outline-as-string)))
+ (should (string= "* h-org 1\nbody\n** h-org 1.1\nbody\n** h-org
1.2\nbody\n*** h-org 1.2.1\nbody\n* h-org 2...\n"
+ (hyrolo-tests--outline-as-string (point))))
+ ;; Hide it again
+ (hyrolo-outline-hide-subtree)
+ (should (string= original-look (hyrolo-tests--outline-as-string))))
+
+ ;; On second level header
+ (goto-char (point-min))
+ (hyrolo-outline-show-all)
+ (hyrolo-outline-hide-sublevels 2)
+ (search-forward "** h-org 1.2")
+ (beginning-of-line)
+ (let ((original-look (hyrolo-tests--outline-as-string)))
+ (hyrolo-outline-show-subtree)
+ (should (string= "...\n* h-org 1...\n** h-org 1.1...\n** h-org
1.2\nbody\n*** h-org 1.2.1\nbody\n* h-org 2...\n** h-org-2.1...\n"
+ (hyrolo-tests--outline-as-string)))
+ (should (string= "** h-org 1.2\nbody\n*** h-org 1.2.1\nbody\n*
h-org 2...\n** h-org-2.1...\n"
+ (hyrolo-tests--outline-as-string (point))))
+ ;; Hide it again
+ (hyrolo-outline-hide-subtree)
+ (should (string= original-look
(hyrolo-tests--outline-as-string)))))
+ (kill-buffer hyrolo-display-buffer)
+ (hy-delete-files-and-buffers hyrolo-file-list))))
+
(provide 'hyrolo-tests)
;;; hyrolo-tests.el ends here