Subject: Re: bug#65734: [BUG] kill-whole-line on folded subtrees [9.6.8 (release_9.6.8-3-g21171d @ /home/w/usr/emacs/0/29/0/lisp/org/)]

From: Sebastian Miele <[email-masked]>
Date: Sun, 10 Sep 2023 18:31:20 +0200

I removed [email-masked] from CC.

> From: Sebastian Miele <[email-masked]>
> Date: Wed, 2023-09-06 15:30 +0200
>
> I will write the tests.  And I will probably come up with an updated
> version of the original patch.  There is at least one cosmetic change.
> And something else that I want to have tried.  May take some time.

Please have a look at the following patch.  For now it contains three
tests, two of them with :expected-result :failed.  (They do not fail on
the bug-fixed version of `kill-whole-line'.)

There probably will be more tests and further questions.  But for now, I
would like to basically have a statement of whether the style of writing
the tests goes in an acceptable direction.

diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el
index 28d8120f143..c15b0059536 100644
--- a/test/lisp/simple-tests.el
+++ b/test/lisp/simple-tests.el
@@ -22,6 +22,7 @@
 ;;; Code:
 
 (require 'ert)
+(require 'ert-x)
 (eval-when-compile (require 'cl-lib))
 
 (defun simple-test--buffer-substrings ()
@@ -40,6 +41,112 @@ simple-test--dummy-buffer
      ,@body
      (with-no-warnings (simple-test--buffer-substrings))))
 
+(defconst simple-tests-point-tag "<POINT>")
+(defconst simple-tests-mark-tag "<MARK>")
+
+(defun simple-tests--set-buffer-text-point-mark (description)
+  "Set the current buffers text, point and mark according to DESCRIPTION.
+
+Erase current buffer and insert DESCRIPTION.  Set point to the
+first occurrence of `simple-tests-point-tag' (\"<POINT>\") in the
+buffer, removing it.  If there is no `simple-tests-point-tag',
+set point to the beginning of the buffer.  Similar for the active
+mark (`simple-tests-mark-tag', \"<MARK>\")."
+  (erase-buffer)
+  (insert description)
+  (goto-char (point-min))
+  (when (search-forward simple-tests-mark-tag nil t)
+    (delete-char (- (length simple-tests-mark-tag)))
+    (push-mark (point) nil 'activate))
+  (goto-char (point-min))
+  (when (search-forward simple-tests-point-tag nil t)
+    (delete-char (- (length simple-tests-point-tag)))))
+
+(defun simple-tests--get-buffer-text-point-mark ()
+  "Inverse of `simple-tests--set-buffer-text-point-mark'."
+  (if (not mark-active)
+      (concat (buffer-substring-no-properties (point-min) (point))
+              simple-tests-point-tag
+              (buffer-substring-no-properties (point) (point-max)))
+    (if (< (mark) (point))
+        (concat (buffer-substring-no-properties (point-min) (mark))
+                simple-tests-mark-tag
+                (buffer-substring-no-properties (mark) (point))
+                simple-tests-point-tag
+                (buffer-substring-no-properties (point) (point-max)))
+      (concat (buffer-substring-no-properties (point-min) (point))
+              simple-tests-point-tag
+              (buffer-substring-no-properties (point) (mark))
+              simple-tests-mark-tag
+              (buffer-substring-no-properties (mark) (point-max))))))
+
+(ert-deftest simple-tests--buffer-text-point-mark-helpers ()
+  (ert-with-test-buffer-selected nil
+    (simple-tests--set-buffer-text-point-mark "")
+    (should (equal "" (buffer-substring-no-properties
+                       (point-min) (point-max))))
+    (should-not mark-active)
+    (should (equal 1 (point)))
+    (should (equal "<POINT>" (simple-tests--get-buffer-text-point-mark))))
+
+  (ert-with-test-buffer-selected nil
+    (simple-tests--set-buffer-text-point-mark "<POINT><MARK>")
+    (should (equal "" (buffer-substring-no-properties
+                       (point-min) (point-max))))
+    (should mark-active)
+    (should (equal 1 (point)))
+    (should (equal 1 (mark)))
+    (should (equal "<POINT><MARK>" (simple-tests--get-buffer-text-point-mark))))
+
+  (ert-with-test-buffer-selected nil
+    (simple-tests--set-buffer-text-point-mark "<MARK><POINT>")
+    (should (equal "" (buffer-substring-no-properties
+                       (point-min) (point-max))))
+    (should mark-active)
+    (should (equal 1 (point)))
+    (should (equal 1 (mark)))
+    (should (equal "<POINT><MARK>" (simple-tests--get-buffer-text-point-mark))))
+
+  (ert-with-test-buffer-selected nil
+    (simple-tests--set-buffer-text-point-mark "A<POINT><MARK>B")
+    (should (equal "AB" (buffer-substring-no-properties
+                         (point-min) (point-max))))
+    (should mark-active)
+    (should (equal 2 (point)))
+    (should (equal 2 (mark)))
+    (should (equal "A<POINT><MARK>B"
+                   (simple-tests--get-buffer-text-point-mark))))
+
+  (ert-with-test-buffer-selected nil
+    (simple-tests--set-buffer-text-point-mark "A<MARK><POINT>B")
+    (should (equal "AB" (buffer-substring-no-properties
+                         (point-min) (point-max))))
+    (should mark-active)
+    (should (equal 2 (point)))
+    (should (equal 2 (mark)))
+    (should (equal "A<POINT><MARK>B"
+                   (simple-tests--get-buffer-text-point-mark))))
+
+  (ert-with-test-buffer-selected nil
+    (simple-tests--set-buffer-text-point-mark "A<POINT>X<MARK>B")
+    (should (equal "AXB" (buffer-substring-no-properties
+                          (point-min) (point-max))))
+    (should mark-active)
+    (should (equal 2 (point)))
+    (should (equal 3 (mark)))
+    (should (equal "A<POINT>X<MARK>B"
+                   (simple-tests--get-buffer-text-point-mark))))
+
+  (ert-with-test-buffer-selected nil
+    (simple-tests--set-buffer-text-point-mark "A<MARK>X<POINT>B")
+    (should (equal "AXB" (buffer-substring-no-properties
+                          (point-min) (point-max))))
+    (should mark-active)
+    (should (equal 3 (point)))
+    (should (equal 2 (mark)))
+    (should (equal "A<MARK>X<POINT>B"
+                   (simple-tests--get-buffer-text-point-mark)))))
+
 
 ;;; `count-words'
 (ert-deftest simple-test-count-words-bug-41761 ()
@@ -1046,5 +1153,109 @@ simple-tests-zap-to-char
     (with-zap-to-char-test "abcdeCXYZ" "XYZ"
       (zap-to-char 1 ?C 'interactive))))
 
+
+;;; Tests for `kill-whole-line'
+
+(ert-deftest kill-whole-line-invisible ()
+  :expected-result :failed
+  (cl-macrolet ((test (kill-whole-line-arg &rest expected-lines)
+                  `(ert-with-test-buffer-selected nil
+                     (simple-tests--set-buffer-text-point-mark
+                      (string-join
+                       '("* -2" "hidden"
+                         "* -1" "hidden"
+                         "* A<POINT>B" "hidden"
+                         "* 1" "hidden"
+                         "* 2" "hidden"
+                         "")
+                       "\n"))
+                     (ert-simulate-command '(org-mode))
+                     (ert-simulate-command '(org-fold-hide-sublevels 1))
+                     (ert-simulate-command
+                      '(kill-whole-line ,kill-whole-line-arg))
+                     (should
+                      (equal (string-join ',expected-lines "\n")
+                             (simple-tests--get-buffer-text-point-mark))))))
+    (test 0
+          "* -2" "hidden"
+          "* -1" "hidden"
+          "<POINT>"
+          "* 1" "hidden"
+          "* 2" "hidden"
+          "")
+    (test 1
+          "* -2" "hidden"
+          "* -1" "hidden"
+          "<POINT>* 1" "hidden"
+          "* 2" "hidden"
+          "")
+    (test 2
+          "* -2" "hidden"
+          "* -1" "hidden"
+          "<POINT>* 2" "hidden"
+          "")
+    (test 3
+          "* -2" "hidden"
+          "* -1" "hidden"
+          "<POINT>")
+    (test 9
+          "* -2" "hidden"
+          "* -1" "hidden"
+          "<POINT>")
+    (test -1
+          "* -2" "hidden"
+          "* -1" "hidden<POINT>"
+          "* 1" "hidden"
+          "* 2" "hidden"
+          "")
+    (test -2
+          "* -2" "hidden<POINT>"
+          "* 1" "hidden"
+          "* 2" "hidden"
+          "")
+    (test -3
+          "<POINT>"
+          "* 1" "hidden"
+          "* 2" "hidden"
+          "")
+    (test -9
+          "<POINT>"
+          "* 1" "hidden"
+          "* 2" "hidden"
+          "")))
+
+(ert-deftest kill-whole-line-read-only ()
+  :expected-result :failed
+  (cl-macrolet
+      ((test (kill-whole-line-arg expected-kill-lines expected-buffer-lines)
+         `(ert-with-test-buffer-selected nil
+            (simple-tests--set-buffer-text-point-mark
+             (string-join '("-2" "-1" "A<POINT>B" "1" "2" "") "\n"))
+            (ert-simulate-command '(read-only-mode 1))
+            (should-error (ert-simulate-command
+                           '(kill-whole-line ,kill-whole-line-arg)))
+            (should (equal (string-join ,expected-kill-lines "\n")
+                           (car kill-ring)))
+            (should (equal (string-join ,expected-buffer-lines "\n")
+                           (simple-tests--get-buffer-text-point-mark))))))
+    (test 0 '("AB") '("-2" "-1" "AB<POINT>" "1" "2" ""))
+    (test 1 '("AB" "") '("-2" "-1" "AB" "<POINT>1" "2" ""))
+    (test 2 '("AB" "1" "") '("-2" "-1" "AB" "1" "<POINT>2" ""))
+    (test 3 '("AB" "1" "2" "") '("-2" "-1" "AB" "1" "2" "<POINT>"))
+    (test 9 '("AB" "1" "2" "") '("-2" "-1" "AB" "1" "2" "<POINT>"))
+    (test -1 '("" "AB") '("-2" "-1<POINT>" "AB" "1" "2" ""))
+    (test -2 '("" "-1" "AB") '("-2<POINT>" "-1" "AB" "1" "2" ""))
+    (test -3 '("-2" "-1" "AB") '("<POINT>-2" "-1" "AB" "1" "2" ""))
+    (test -9 '("-2" "-1" "AB") '("<POINT>-2" "-1" "AB" "1" "2" ""))))
+
+(ert-deftest kill-whole-line-after-other-kill ()
+  (ert-with-test-buffer-selected nil
+    (simple-tests--set-buffer-text-point-mark "A<POINT>X<MARK>B")
+    (ert-simulate-command '(kill-region (mark) (point) 'region))
+    (ert-simulate-command '(kill-whole-line))
+    (should (equal "AXB" (car kill-ring)))
+    (should (equal "<POINT>"
+                   (simple-tests--get-buffer-text-point-mark)))))
+
 (provide 'simple-test)
 ;;; simple-tests.el ends here