diff options
Diffstat (limited to 'test/lisp/simple-tests.el')
-rw-r--r-- | test/lisp/simple-tests.el | 136 |
1 files changed, 134 insertions, 2 deletions
diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el index b4007a6c3f3..3ece61290bc 100644 --- a/test/lisp/simple-tests.el +++ b/test/lisp/simple-tests.el @@ -1,4 +1,4 @@ -;;; simple-test.el --- Tests for simple.el -*- lexical-binding: t; -*- +;;; simple-tests.el --- Tests for simple.el -*- lexical-binding: t; -*- ;; Copyright (C) 2015-2021 Free Software Foundation, Inc. @@ -67,6 +67,11 @@ (insert (propertize "\nbar\nbaz\nzut" 'invisible t)) (should (= (count-lines (point-min) (point-max) t) 2)))) +(ert-deftest simple-text-count-lines-non-ascii () + (with-temp-buffer + (insert "あ\nい\nう\nえ\nお\n") + (should (= (count-lines (point) (point)) 0)))) + ;;; `transpose-sexps' (defmacro simple-test--transpositions (&rest body) @@ -460,8 +465,124 @@ See bug#35036." (simple-tests--exec '(backward-char undo-redo undo-redo)) (should (equal (buffer-string) "abc")) (simple-tests--exec '(backward-char undo-redo undo-redo)) + (should (equal (buffer-string) "abcde")))) + +(ert-deftest simple-tests--undo-in-region () + ;; Test undo/redo in region. + (with-temp-buffer + ;; Enable `transient-mark-mode' so `region-active-p' works as + ;; expected. `region-active-p' is used to determine whether to + ;; perform regional undo in `undo'. + (transient-mark-mode) + (buffer-enable-undo) + (dolist (x '("a" "b" "c" "d" "e")) + (insert x) + (undo-boundary)) + (should (equal (buffer-string) "abcde")) + ;; The test does this: activate region, `undo', break the undo + ;; chain (by deactivating and reactivating the region), then + ;; `undo-only'. There used to be a bug in + ;; `undo-make-selective-list' that makes `undo-only' error out in + ;; that case, which is fixed by in the same commit as this change. + (simple-tests--exec '(move-beginning-of-line + push-mark-command + forward-char + forward-char + undo)) + (should (equal (buffer-string) "acde")) + (simple-tests--exec '(move-beginning-of-line + push-mark-command + forward-char + forward-char + undo-only)) (should (equal (buffer-string) "abcde")) - )) + ;; Rest are simple redo in region tests. + (simple-tests--exec '(undo-redo)) + (should (equal (buffer-string) "acde")) + (simple-tests--exec '(undo-redo)) + (should (equal (buffer-string) "abcde")))) + +(defun simple-tests--sans-leading-nil (lst) + "Return LST sans the leading nils." + (while (and (consp lst) (null (car lst))) + (setq lst (cdr lst))) + lst) + +(ert-deftest simple-tests--undo-equiv-table () + (with-temp-buffer + (buffer-enable-undo) + (transient-mark-mode) + (let ((ul-hash-table (make-hash-table :test #'equal))) + (dolist (x '("a" "b" "c")) + (insert x) + (puthash x (simple-tests--sans-leading-nil buffer-undo-list) + ul-hash-table) + (undo-boundary)) + (should (equal (buffer-string) "abc")) + ;; Tests mappings in `undo-equiv-table'. + (simple-tests--exec '(undo)) + (should (equal (buffer-string) "ab")) + (should (eq (gethash (simple-tests--sans-leading-nil + buffer-undo-list) + undo-equiv-table) + (gethash "b" ul-hash-table))) + (simple-tests--exec '(backward-char undo)) + (should (equal (buffer-string) "abc")) + (should (eq (gethash (simple-tests--sans-leading-nil + buffer-undo-list) + undo-equiv-table) + (gethash "c" ul-hash-table))) + ;; Undo in region should map to 'undo-in-region. + (simple-tests--exec '(backward-char + push-mark-command + move-end-of-line + undo)) + (should (equal (buffer-string) "ab")) + (should (eq (gethash (simple-tests--sans-leading-nil + buffer-undo-list) + undo-equiv-table) + 'undo-in-region)) + ;; The undo that undoes to the beginning should map to t. + (deactivate-mark 'force) + (simple-tests--exec '(backward-char + undo undo undo + undo undo undo)) + (should (equal (buffer-string) "")) + (should (eq (gethash (simple-tests--sans-leading-nil + buffer-undo-list) + undo-equiv-table) + t)) + ;; Erroneous nil undo should map to 'empty. + (insert "a") + (undo-boundary) + (push nil buffer-undo-list) + (simple-tests--exec '(backward-char undo)) + (should (equal (buffer-string) "a")) + (should (eq (gethash (simple-tests--sans-leading-nil + buffer-undo-list) + undo-equiv-table) + 'empty)) + ;; But if the previous record is a redo record, its mapping + ;; shouldn't change. + (insert "e") + (undo-boundary) + (should (equal (buffer-string) "ea")) + (puthash "e" (simple-tests--sans-leading-nil buffer-undo-list) + ul-hash-table) + (insert "a") + (undo-boundary) + (simple-tests--exec '(backward-char undo)) + (should (equal (buffer-string) "ea")) + (push nil buffer-undo-list) + (simple-tests--exec '(forward-char undo)) + ;; Buffer content should change since we just undid a nil + ;; record. + (should (equal (buffer-string) "ea")) + ;; The previous redo record shouldn't map to empty. + (should (equal (gethash (simple-tests--sans-leading-nil + buffer-undo-list) + undo-equiv-table) + (gethash "e" ul-hash-table)))))) ;;; undo auto-boundary tests (ert-deftest undo-auto-boundary-timer () @@ -838,6 +959,17 @@ See Bug#21722." (with-shell-command-dont-erase-buffer str output-buffer-is-current (should (= (point) (alist-get shell-command-dont-erase-buffer expected-point))))))) +(ert-deftest test-undo-region () + (with-temp-buffer + (insert "This is a test\n") + (goto-char (point-min)) + (setq buffer-undo-list nil) + (downcase-word 1) + (should (= (length (delq nil (undo-make-selective-list 1 9))) 2)) + (should (= (length (delq nil (undo-make-selective-list 4 9))) 1)) + ;; FIXME this is the off-by-one error case. + ;;(should (= (length (delq nil (undo-make-selective-list 5 9))) 0)) + (should (= (length (delq nil (undo-make-selective-list 6 9))) 0)))) (provide 'simple-test) ;;; simple-test.el ends here |