From 26759b2439ce37838e1d585bae746544c18f5cfa Mon Sep 17 00:00:00 2001 From: Sebastian Miele Date: Wed, 19 Jun 2024 15:58:24 +0200 Subject: [PATCH] Add tests for `kill-whole-line' (bug#65734) * test/lisp/simple-tests.el (simple-test-point-tag): (simple-test-mark-tag): (simple-test--set-buffer-text-point-mark): (simple-test--get-buffer-text-point-mark): Add helper functions used by the tests. (kill-whole-line-invisible): (kill-whole-line-read-only): (kill-whole-line-after-other-kill): (kill-whole-line-buffer-boundaries): (kill-whole-line-line-boundaries): Add tests for `kill-whole-line'. (cherry picked from commit aa10d0c5ac1a4d706b8562671f2a22a82f9e733b) --- test/lisp/simple-tests.el | 227 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el index afd75786804..9e3e71bd69b 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,49 @@ ,@body (with-no-warnings (simple-test--buffer-substrings)))) +(defconst simple-test-point-tag "") +(defconst simple-test-mark-tag "") + +(defun simple-test--set-buffer-text-point-mark (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 `simple-test-point-tag' (\"\") in the +buffer, removing it. If there is no `simple-test-point-tag', set +point to the beginning of the buffer. If there is a +`simple-test-mark-tag' (\"\"), remove it, and set an active +mark there." + (erase-buffer) + (insert description) + (goto-char (point-min)) + (when (search-forward simple-test-mark-tag nil t) + (delete-char (- (length simple-test-mark-tag))) + (push-mark (point) nil 'activate)) + (goto-char (point-min)) + (when (search-forward simple-test-point-tag nil t) + (delete-char (- (length simple-test-point-tag))))) + +(defun simple-test--get-buffer-text-point-mark () + "Inverse of `simple-test--set-buffer-text-point-mark'." + (cond + ((not mark-active) + (concat (buffer-substring-no-properties (point-min) (point)) + simple-test-point-tag + (buffer-substring-no-properties (point) (point-max)))) + ((< (mark) (point)) + (concat (buffer-substring-no-properties (point-min) (mark)) + simple-test-mark-tag + (buffer-substring-no-properties (mark) (point)) + simple-test-point-tag + (buffer-substring-no-properties (point) (point-max)))) + (t + (concat (buffer-substring-no-properties (point-min) (point)) + simple-test-point-tag + (buffer-substring-no-properties (point) (mark)) + simple-test-mark-tag + (buffer-substring-no-properties (mark) (point-max)))))) + ;;; `count-words' (ert-deftest simple-test-count-words-bug-41761 () @@ -1046,5 +1090,188 @@ See Bug#21722." (with-zap-to-char-test "abcdeCXYZ" "XYZ" (zap-to-char 1 ?C 'interactive)))) + +;;; Tests for `kill-whole-line' + +(ert-deftest kill-whole-line-invisible () + (cl-flet ((test (kill-whole-line-arg &rest expected-lines) + (ert-info ((format "%s" kill-whole-line-arg) :prefix "Subtest: ") + (ert-with-test-buffer-selected nil + (simple-test--set-buffer-text-point-mark + (string-join + '("* -2" "hidden" + "* -1" "hidden" + "* AB" "hidden" + "* 1" "hidden" + "* 2" "hidden" + "") + "\n")) + (org-mode) + (org-fold-hide-sublevels 1) + (kill-whole-line kill-whole-line-arg) + (should + (equal (string-join expected-lines "\n") + (simple-test--get-buffer-text-point-mark))))))) + (test 0 + "* -2" "hidden" + "* -1" "hidden" + "" + "* 1" "hidden" + "* 2" "hidden" + "") + (test 1 + "* -2" "hidden" + "* -1" "hidden" + "* 1" "hidden" + "* 2" "hidden" + "") + (test 2 + "* -2" "hidden" + "* -1" "hidden" + "* 2" "hidden" + "") + (test 3 + "* -2" "hidden" + "* -1" "hidden" + "") + (test 9 + "* -2" "hidden" + "* -1" "hidden" + "") + (test -1 + "* -2" "hidden" + "* -1" "hidden" + "* 1" "hidden" + "* 2" "hidden" + "") + (test -2 + "* -2" "hidden" + "* 1" "hidden" + "* 2" "hidden" + "") + (test -3 + "" + "* 1" "hidden" + "* 2" "hidden" + "") + (test -9 + "" + "* 1" "hidden" + "* 2" "hidden" + ""))) + +(ert-deftest kill-whole-line-read-only () + (cl-flet + ((test (kill-whole-line-arg expected-kill-lines expected-buffer-lines) + (ert-info ((format "%s" kill-whole-line-arg) :prefix "Subtest: ") + (ert-with-test-buffer-selected nil + (simple-test--set-buffer-text-point-mark + (string-join '("-2" "-1" "AB" "1" "2" "") "\n")) + (read-only-mode 1) + (setq last-command #'ignore) + (should-error (kill-whole-line kill-whole-line-arg) + :type 'buffer-read-only) + (should (equal (string-join expected-kill-lines "\n") + (car kill-ring))) + (should (equal (string-join expected-buffer-lines "\n") + (simple-test--get-buffer-text-point-mark))))))) + (test 0 '("AB") '("-2" "-1" "AB" "1" "2" "")) + (test 1 '("AB" "") '("-2" "-1" "AB" "1" "2" "")) + (test 2 '("AB" "1" "") '("-2" "-1" "AB" "1" "2" "")) + (test 3 '("AB" "1" "2" "") '("-2" "-1" "AB" "1" "2" "")) + (test 9 '("AB" "1" "2" "") '("-2" "-1" "AB" "1" "2" "")) + (test -1 '("" "AB") '("-2" "-1" "AB" "1" "2" "")) + (test -2 '("" "-1" "AB") '("-2" "-1" "AB" "1" "2" "")) + (test -3 '("-2" "-1" "AB") '("-2" "-1" "AB" "1" "2" "")) + (test -9 '("-2" "-1" "AB") '("-2" "-1" "AB" "1" "2" "")))) + +(ert-deftest kill-whole-line-after-other-kill () + (ert-with-test-buffer-selected nil + (simple-test--set-buffer-text-point-mark "AXB") + (setq last-command #'ignore) + (kill-region (point) (mark)) + (deactivate-mark 'force) + (setq last-command #'kill-region) + (kill-whole-line) + (should (equal "AXB" (car kill-ring))) + (should (equal "" + (simple-test--get-buffer-text-point-mark))))) + +(ert-deftest kill-whole-line-buffer-boundaries () + (ert-with-test-buffer-selected nil + (ert-info ("0" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "") + (should-error (kill-whole-line -1) + :type 'beginning-of-buffer) + (should-error (kill-whole-line 1) + :type 'end-of-buffer)) + (ert-info ("1a" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\n") + (should-error (kill-whole-line 1) + :type 'end-of-buffer)) + (ert-info ("1b" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\nA") + (setq last-command #'ignore) + (kill-whole-line 1) + (should (equal "-1\n" + (simple-test--get-buffer-text-point-mark))) + (should (equal "A" (car kill-ring)))) + (ert-info ("2" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "\n1") + (should-error (kill-whole-line -1) + :type 'beginning-of-buffer)) + (ert-info ("2b" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "A\n1") + (setq last-command #'ignore) + (kill-whole-line 1) + (should (equal "1" + (simple-test--get-buffer-text-point-mark))) + (should (equal "A\n" (car kill-ring)))))) + +(ert-deftest kill-whole-line-line-boundaries () + (ert-with-test-buffer-selected nil + (ert-info ("1a" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\n\n1\n") + (setq last-command #'ignore) + (kill-whole-line 1) + (should (equal "-1\n1\n" + (simple-test--get-buffer-text-point-mark))) + (should (equal "\n" (car kill-ring)))) + (ert-info ("1b" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\n\n1\n") + (setq last-command #'ignore) + (kill-whole-line -1) + (should (equal "-1\n1\n" + (simple-test--get-buffer-text-point-mark))) + (should (equal "\n" (car kill-ring)))) + (ert-info ("2a" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\nA\n1\n") + (setq last-command #'ignore) + (kill-whole-line 1) + (should (equal "-1\n1\n" + (simple-test--get-buffer-text-point-mark))) + (should (equal "A\n" (car kill-ring)))) + (ert-info ("2b" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\nA\n1\n") + (setq last-command #'ignore) + (kill-whole-line -1) + (should (equal "-1\n1\n" + (simple-test--get-buffer-text-point-mark))) + (should (equal "\nA" (car kill-ring)))) + (ert-info ("3a" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\nA\n1\n") + (setq last-command #'ignore) + (kill-whole-line 1) + (should (equal "-1\n1\n" + (simple-test--get-buffer-text-point-mark))) + (should (equal "A\n" (car kill-ring)))) + (ert-info ("3b" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\nA\n1\n") + (setq last-command #'ignore) + (kill-whole-line -1) + (should (equal "-1\n1\n" + (simple-test--get-buffer-text-point-mark))) + (should (equal "\nA" (car kill-ring)))))) + (provide 'simple-test) ;;; simple-tests.el ends here -- 2.39.2