blob: 0ebd4c46ba7126714524cad9f9fa8843a0938abb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
;;; -*- lexical-binding: t -*-
(require 'ert)
(require 'evil)
(require 'evil-surround)
(require 'evil-test-helpers)
;; interactive test helper (this is the standard configuration)
(evil-add-to-alist
'evil-surround-pairs-alist
?\) '("(" . ")")
?\] '("[" . "]")
?\} '("{" . "}")
?\( '("( " . " )")
?\[ '("[ " . " ]")
?\{ '("{ " . " }"))
(defmacro test-widened-buffer (start cmds exp)
(declare (indent 0))
`(let (widened)
(evil-test-buffer
,start
(turn-on-evil-surround-mode)
(cl-letf (((symbol-function #'widen)
(lambda () (setq widened t))))
(execute-kbd-macro ,(car cmds)))
,exp)
(should widened)))
(ert-deftest evil-surround-test ()
(ert-info ("basic surrounding")
(evil-test-buffer
"one [t]wo three"
(turn-on-evil-surround-mode)
("ysiwb")
"one (two) three"
("csb'")
"one 'two' three"
("ds'")
"one two three"))
(ert-info ("shortcut surrounding")
(evil-test-buffer
"One, and two. Also three.\n\n"
(turn-on-evil-surround-mode)
("csw)")
"(One), and two. Also three.\n\n"
("csW'")
"'(One),' and two. Also three.\n\n"
("css.")
".'(One),' and two. Also three..\n\n"
("csp0")
"0.'(One),' and two. Also three..0\n\n"))
(ert-info ("examples from readme")
(evil-test-buffer
:visual-start nil
:visual-end nil
"\"Hello world!\""
(turn-on-evil-surround-mode)
("cs\"'")
"'Hello world!'"
("cs'<q>")
"<q>Hello world!</q>"
("cst\"")
"\"Hello world!\""
("ds\"")
"Hello world!"
("ysiw]")
"[Hello] world!"
("cs[{")
"{ Hello } world!"
("yssb")
"({ Hello } world!)"
("lds{ds)") ;; 'l' to move the cursor right, inside brackets
"Hello world!"
("ysiw<em>")
"<em>Hello</em> world!"))
(ert-info ("tests for dots and caps support in tags")
(evil-test-buffer
:visual-start nil
:visual-end nil
"\"Hello world!\""
(turn-on-evil-surround-mode)
("cs\"'")
"'Hello world!'"
("cs'<Table.Hi>")
"<Table.Hi>Hello world!</Table.Hi>"
("dst")
"Hello world!"
("ysiw<div.Test attr=\"true\">")
"<div.Test attr=\"true\">Hello</div.Test> world!"
("cst<Three.Separate.Components>")
"<Three.Separate.Components>Hello</Three.Separate.Components> world!"))
(ert-info ("more examples from readme: function surrounding with dot repeat")
(evil-test-buffer
:visual-start nil
:visual-end nil
"argument1 argument2"
(turn-on-evil-surround-mode)
("ysiwffunction" [return])
"function(argument1) argument2"
("W.")
"function(argument1) function(argument2)"))
(ert-info ("prefix-function surrounding with dot repeat")
(evil-test-buffer
:visual-start nil
:visual-end nil
"argument1 argument2"
(turn-on-evil-surround-mode)
("ysiw\C-ffunction" [return])
"(function argument1) argument2"
("WW.")
"(function argument1) (function argument2)"))
(ert-info ("even more examples from readme: tag surrounding with dot repeat")
(evil-test-buffer
:visual-start nil
:visual-end nil
"tag1 tag2"
(turn-on-evil-surround-mode)
("ysiw<a>")
"<a>tag1</a> tag2"
("W.")
"<a>tag1</a> <a>tag2</a>"))
(ert-info ("optionally keep xml attributes")
(evil-test-buffer
:visual-start nil
:visual-end nil
"<div class=\"foo\">Bar</div>"
(turn-on-evil-surround-mode)
("cst<span")
"<span class=\"foo\">Bar</span>"
("cst<p>")
"<p>Bar</p>"))
(ert-info ("optionally keep xml attributes: more complicated cases")
(evil-test-buffer
:visual-start nil
:visual-end nil
"<div ngModel class=\"foo\" randomAngularDirective #anchor1>Bar</div>"
(turn-on-evil-surround-mode)
("cst<span")
"<span ngModel class=\"foo\" randomAngularDirective #anchor1>Bar</span>"
("cst<p>")
"<p>Bar</p>"))
(ert-info ("optionally keep xml attributes: repeating")
(evil-test-buffer
:visual-start nil
:visual-end nil
"<div attr=\"foo\">Foo</div><div attr=\"bar\">Bar</div>"
(turn-on-evil-surround-mode)
("cst<span")
"<span attr=\"foo\">Foo</span><div attr=\"bar\">Bar</div>"
("fB.")
"<span attr=\"foo\">Foo</span><span attr=\"bar\">Bar</span>"))
(ert-info ("repeat surrounding")
(evil-test-buffer
"[o]ne two three"
(turn-on-evil-surround-mode)
;; surround and repeat it
("ysiwb")
"(one) two three"
("W.") ;; repeat surround region
"(one) (two) three"
("W.") ;; repeat surround region
"(one) (two) (three)"
;; change surround and repeat it
("0csb'")
"'one' (two) (three)"
("W.") ;; repeat change surround
"'one' 'two' (three)"
("W.") ;; repeat change surround
"'one' 'two' 'three'"
;; delete surround and repeat it
("0ds'")
"one 'two' 'three'"
("W.") ;; repeat delete surround
"one two 'three'"
("W.") ;; repeat delete surround
"one two three"))
(ert-info ("repeat surrounding with count")
(evil-test-buffer
"[o]ne two three\none two three"
(turn-on-evil-surround-mode)
("ys2wb")
"(one two) three\none two three"
("j.")
"(one two) three\n(one two) three"))
(ert-info ("visual surrounding")
(evil-test-buffer
"<one two> three\nfour\n"
(turn-on-evil-surround-mode)
("Sb")
"(one two) three\nfour\n")
(evil-test-buffer
"<one two three>\nfour\n"
(turn-on-evil-surround-mode)
("Sb")
"(one two three)\nfour\n")
(evil-test-buffer
"<one two three\nfo>ur\n"
(turn-on-evil-surround-mode)
("Sb")
"(one two three\nfo)ur\n"))
(ert-info ("visual line surrounding")
(evil-test-buffer
"[o]ne two three\nfour\n"
(turn-on-evil-surround-mode)
("yssb")
"(one two three)\nfour\n"
("dsb")
"one two three\nfour\n"
("VSb")
"(\none two three\n)\nfour\n")
(evil-test-buffer
"111 222 333\n[1]11 222 333\n111 222 333\n111 222 333\n"
(turn-on-evil-surround-mode)
("ysjb")
"111 222 333\n(\n111 222 333\n111 222 333\n)\n111 222 333\n"))
(ert-info ("test with evil-want-change-word-to-end")
(evil-test-buffer
"[o]ne two three"
(setq evil-want-change-word-to-end nil)
(turn-on-evil-surround-mode)
("yswb")
"[(]one )two three"
("dsb")
"[o]ne two three"
("ys2wb")
"[(]one two )three"
("dsb")
"[o]ne two three"
("ys3wb")
"[(]one two three)")
(evil-test-buffer
"[o]ne two three"
(setq evil-want-change-word-to-end t)
(turn-on-evil-surround-mode)
("yswb")
"[(]one) two three"
("dsb")
"[o]ne two three"
("ys2wb")
"[(]one two) three"
("dsb")
"[o]ne two three"
("ys3wb")
"[(]one two three)"))
(ert-info ("yS test")
(evil-test-buffer
"some_word"
(turn-on-evil-surround-mode)
("ySiW\"")
"\"\nsome_word\n\""
))
(ert-info ("ensure backquote delimiters work")
(evil-test-buffer
"`this_is_a_[b]acktick_surrounded_word`"
(turn-on-evil-surround-mode)
("cs`)")
"[(]this_is_a_backtick_surrounded_word)"
))
(ert-info ("buffer is widened before reading char")
(test-widened-buffer
"`[w]ord`"
("cs`)")
"[(]word)")
(test-widened-buffer
"`[w]ord`"
("ds`")
"[w]ord")
(test-widened-buffer
"[w]ord"
("ysiwb")
"[(]word)"
))
(ert-info ("create buffer local text object and surround binding")
(evil-test-buffer
"QQw[o]rdQQ soup"
(turn-on-evil-surround-mode)
(evil-define-text-object evil-surround-test-inner-QQ (count &optional beg end type)
"Test text object"
:extend-selection nil
(evil-select-paren "QQ" "QQ"
beg end type count nil))
(evil-define-text-object evil-surround-test-a-QQ (count &optional beg end type)
:extend-selection nil
(evil-select-paren "QQ" "QQ"
beg end type count t))
(evil-define-text-object evil-surround-test-inner-NN (count &optional beg end type)
"Test text object"
:extend-selection nil
(evil-select-paren "NN" "NN"
beg end type count nil))
(evil-define-text-object evil-surround-test-a-NN (count &optional beg end type)
:extend-selection nil
(evil-select-paren "NN" "NN"
beg end type count t))
(defvar evil-surround-test-inner-text-objects-map (make-sparse-keymap)
"Inner text object test keymap")
(defvar evil-surround-test-outer-text-objects-map (make-sparse-keymap)
"Outer text object keymap")
(define-key evil-surround-test-inner-text-objects-map "Q" #'evil-surround-test-inner-QQ)
(define-key evil-surround-test-outer-text-objects-map "Q" #'evil-surround-test-a-QQ)
(define-key evil-visual-state-local-map "iQ" #'evil-surround-test-inner-QQ)
(define-key evil-operator-state-local-map "iQ" #'evil-surround-test-inner-QQ)
(define-key evil-visual-state-local-map "aQ" #'evil-surround-test-a-QQ)
(define-key evil-operator-state-local-map "aQ" #'evil-surround-test-a-QQ)
(define-key evil-visual-state-local-map "iN" #'evil-surround-test-inner-NN)
(define-key evil-operator-state-local-map "iN" #'evil-surround-test-inner-NN)
(define-key evil-visual-state-local-map "aN" #'evil-surround-test-a-NN)
(define-key evil-operator-state-local-map "aN" #'evil-surround-test-a-NN)
(setq evil-surround-local-inner-text-object-map-list (list evil-surround-test-inner-text-objects-map ))
(setq evil-surround-local-outer-text-object-map-list (list evil-surround-test-outer-text-objects-map ))
(setq evil-surround-local-inner-text-object-map-list (list (lookup-key evil-operator-state-local-map "i")))
(setq evil-surround-local-outer-text-object-map-list (list (lookup-key evil-operator-state-local-map "a")))
(setq-local evil-surround-pairs-alist (append '((?Q "QQ" . "QQ") (?N "NN" . "NN")) evil-surround-pairs-alist))
("dsQ")
"word soup"
("cswQ")
"QQwordQQ soup"
("daQ")
" soup"
("ysawQ")
"QQ soupQQ"
("csQb")
"( soup)"
("csbN")
"NN soupNN"
))
(ert-info ("check that the previous binding was in fact local")
(evil-test-buffer
"QQw[o]rdQQ soup"
(turn-on-evil-surround-mode)
("dsQ")
"QQwordQQ soup"
("cswQ")
"QQQwordQQQ soup")))
(ert-deftest evil-surround-tag-from-macro ()
(ert-info ("tag surround in macro")
(save-window-excursion
(with-current-buffer (get-buffer-create "*evil-surround-tag-from-macro*")
(switch-to-buffer-other-window (current-buffer))
(insert "foo\nbar\nbaz\n")
(goto-char (point-min))
(evil-mode 1)
(turn-on-evil-surround-mode)
(execute-kbd-macro
"yse<div>f>lysit$j_")
(should
(string= "<div>$foo$</div>\nbar\nbaz\n"
(buffer-substring-no-properties (point-min) (point-max))))))))
|