]> code.delx.au - gnu-emacs/blob - test/lisp/emulation/viper-tests.el
Merge from origin/emacs-25
[gnu-emacs] / test / lisp / emulation / viper-tests.el
1 ;;; viper-tests.el --- tests for viper.
2
3 ;; Copyright (C) 2016 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
19
20 ;;; Commentary:
21
22 ;;; Code:
23
24
25 (require 'viper)
26
27 (defun viper-test-undo-kmacro (kmacro)
28 "In a clean viper buffer, run KMACRO and return `buffer-string'.
29
30 This function makes as many attempts as possible to clean up
31 after itself, although it will leave a buffer called
32 *viper-test-buffer* if it fails (this is deliberate!)."
33 (let (
34 ;; Viper just turns itself off during batch use.
35 (noninteractive nil)
36 ;; Switch off start up message or it will chew the key presses.
37 (viper-inhibit-startup-message 't)
38 ;; Select an expert-level for the same reason.
39 (viper-expert-level 5)
40 ;; viper loads this even with -q so make sure it's empty!
41 (viper-custom-file-name (make-temp-file "viper-tests"))
42 (before-buffer (current-buffer)))
43 (unwind-protect
44 (progn
45 ;; viper-mode is essentially global, so set it here.
46 (viper-mode)
47 ;; We must switch to buffer because we are using a keyboard macro
48 ;; which appears to not go to the current-buffer but what ever is
49 ;; currently taking keyboard events. We use a named buffer because
50 ;; then we can see what it in it if it all goes wrong.
51 (switch-to-buffer
52 (get-buffer-create
53 "*viper-test-buffer*"))
54 (erase-buffer)
55 ;; The new buffer fails to enter vi state so set it.
56 (viper-change-state-to-vi)
57 ;; Run the macro.
58 (execute-kbd-macro kmacro)
59 (let ((rtn
60 (buffer-substring-no-properties
61 (point-min)
62 (point-max))))
63 ;; Kill the buffer iff the macro succeeds.
64 (kill-buffer)
65 rtn))
66 ;; Switch everything off and restore the buffer.
67 (toggle-viper-mode)
68 (delete-file viper-custom-file-name)
69 (switch-to-buffer before-buffer))))
70
71 (ert-deftest viper-test-go ()
72 "Test that this file is running."
73 (should t))
74
75 (ert-deftest viper-test-fix ()
76 "Test that the viper kmacro fixture is working."
77 (should
78 (viper-test-undo-kmacro [])))
79
80 (ert-deftest viper-test-undo-1 ()
81 "Test for VI like undo behaviour.
82
83 Insert 1, then 2 on consecutive lines, followed by undo. This
84 should leave just 1 in the buffer.
85
86 Test for Bug #22295"
87 (should
88 (equal
89 "1\n"
90 (viper-test-undo-kmacro
91 [
92 ?a
93 ?1
94 escape
95 ?o
96 ?2
97 escape
98 ?u
99 ]
100 ))))
101
102 (ert-deftest viper-test-undo-2 ()
103 "Test for VI like undo behaviour.
104
105 Insert \"1 2 3 4 5\" then delete the 2, then the 4, and undo.
106 Should restore the 4, but leave the 2 deleted.
107
108 Test for Bug #22295"
109 (should
110 (equal
111 "1 3 4 5\n"
112 (viper-test-undo-kmacro
113 [
114 ?i
115 ?1 ? ?2 ? ?3 ? ?4 ? ?5
116 escape
117 ?F ?2 ?d ?w
118 ?w ?d ?w
119 ?u
120 ]))))
121
122 (ert-deftest viper-test-undo-3 ()
123 "Test for VI like undo behaviour.
124
125 Insert \"1 2 3 4 5 6\", delete the 2, then the 3 4 and 5.
126 Should restore the 3 4 and 5 but not the 2.
127
128 Test for Bug #22295"
129 (should
130 (equal
131 "1 3 4 5 6\n"
132 (viper-test-undo-kmacro
133 [
134 ;; Insert this lot.
135 ?i ?1 ? ?2 ? ?3 ? ?4 ? ?5 ? ?6
136 escape
137 ;; Start of line.
138 ?0
139 ;; Move to 2, delete
140 ?w ?d ?w
141 ;; Delete 3 4 5
142 ?. ?. ?.
143 ;; Undo del 5, then
144 ?u ?. ?.
145 ]))))
146
147
148 (ert-deftest viper-test-undo-4()
149 (should
150 (equal
151 ""
152 (viper-test-undo-kmacro
153 [
154 ?i ?1 escape
155 ?o ?2 escape
156 ?o ?3 escape
157 ?u ?. ?.
158 ])
159 )))
160
161 ;;; viper-tests.el ends here