-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
smartparens-wrapping-test.el
258 lines (222 loc) · 10.9 KB
/
smartparens-wrapping-test.el
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
(defun sp-test-wrapping (initial keys result)
(sp-test-with-temp-elisp-buffer initial
(-each (-list keys) 'execute-kbd-macro)
(sp-buffer-equals result)))
(ert-deftest sp-test-wrap-basic nil
(let ((sp-pairs sp--test-basic-pairs))
(sp-test-wrapping "|aM" "(" "(|aM)")
(sp-test-wrapping "Ma|" "(" "M(a)|")
(sp-test-wrapping "|aM" "[" "[|a]")
(sp-test-wrapping "Ma|" "[" "[a]|")
(sp-test-wrapping "|aM" "\\{" "\\{|a\\}")
(sp-test-wrapping "Ma|" "\\{" "\\{a\\}|")
(sp-test-wrapping "|aM" "\\\"" "\\\"|a\\\"")
(sp-test-wrapping "Ma|" "\\\"" "\\\"a\\\"|")
(sp-test-wrapping "|aM" "\\langle" "\\langle|a\\rangle")
(sp-test-wrapping "Ma|" "\\langle" "\\langlea\\rangle|")))
(ert-deftest sp-test-wrap-basic-respect-direction nil
(let ((sp-pairs sp--test-basic-pairs)
(sp-wrap-respect-direction t))
(sp-test-wrapping "|aM" "(" "|(a)M")
(sp-test-wrapping "Ma|" "(" "|(a)M")))
(ert-deftest sp-test-wrap-basic-with-quotes nil
(let ((sp-pairs '((t (:open "\"" :close "\""
:actions (insert wrap autoskip navigate escape)
:post-handlers (sp-escape-wrapped-region
sp-escape-quotes-after-insert))))))
;; #212
(sp-test-wrapping "\"C-M[|\"" "\"" "\"C-\\\"[\\\"|\"")
(sp-test-wrapping "\"C-|[M\"" "\"" "\"C-\\\"|[\\\"\"")))
(ert-deftest sp-test-wrap-with-trigger nil
(let ((sp-pairs '((t (:open "\\(" :close "\\)" :trigger-wrap "$"
:actions (insert wrap autoskip navigate escape))
(:open "$" :close "$"
:actions (insert wrap autoskip navigate escape))
(:open "[" :close "]"
:actions (insert wrap autoskip navigate escape))))))
;; #663
(sp-test-wrapping "|foobarM" "$" "\\(|foobar\\)")
(sp-test-wrapping "Mfoobar|" "$" "\\(foobar\\)|")))
(ert-deftest sp-test-wrap-unbalanced-region nil
(shut-up
(let ((sp-pairs sp--test-basic-pairs))
(sp-test-wrapping "[a b |c] dM e" "(" "[a b |c] d e")
(sp-test-wrapping "[a b Mc] d| e" "(" "[a b c] d| e")
(sp-test-wrapping "[a b |c] d [e Mf]" "(" "[a b |c] d [e f]")
(sp-test-wrapping "[a b Mc] d [e |f]" "(" "[a b c] d [e |f]")
;; wrapping with quotes
(sp-test-wrapping "[a b |c] dM e" "\"" "[a b |c] d e")
(sp-test-wrapping "[a b Mc] d| e" "\"" "[a b c] d| e")
(sp-test-wrapping "\"a b |c\" dM e" "\"" "\"a b |c\" d e")
(sp-test-wrapping "\"a b Mc\" d| e" "\"" "\"a b c\" d| e")
(sp-test-wrapping "[foo |bar] baz \"quMx\"" "\"" "[foo |bar] baz \"qux\"")
(sp-test-wrapping "[foo Mbar] baz \"qu|x\"" "\"" "[foo bar] baz \"qu|x\"")
(sp-test-wrapping "\"fo|o\" asd \"baMz\"" "[" "\"fo|o\" asd \"baz\"")
(sp-test-wrapping "\"foMo\" asd \"ba|z\"" "[" "\"foo\" asd \"ba|z\""))))
(ert-deftest sp-test-wrap-with-closing nil
(let ((sp-pairs sp--test-basic-pairs))
(sp-test-wrapping "|aM" "]" "M[a]|")
(sp-test-wrapping "Ma|" "]" "M[a]|")
(sp-test-wrapping "|aM" "\\}" "\\{a\\}|")))
(ert-deftest sp-test-wrap-repeated nil
(let ((sp-pairs sp--test-basic-pairs))
(sp-test-wrapping "|aM" "[[" "[[|a]]")
(sp-test-wrapping "Ma|" "[[" "[[a]]|")
(sp-test-wrapping "Ma|" "[ [" "[a] [|]")))
(ert-deftest sp-test-wrap-repeated-wrap-only-pair nil
(let ((sp-pairs
'((t
(:open "*" :close "*" :actions (wrap))
(:open "[" :close "]" :actions (wrap))))))
(sp-test-wrapping "|aM" "**" "**|a**")
(sp-test-wrapping "Ma|" "**" "**a**|")
(sp-test-wrapping "|aM" "[[" "[[|a]]")
(sp-test-wrapping "Ma|" "[[" "[[a]]|")))
(ert-deftest sp-test-wrap-in-delete-selection-mode nil
(sp-test-with-delete-selection-mode
(sp-test-with-temp-elisp-buffer "|fooM"
;; Inserting a character that pairs should wrap instead of
;; replacing the selection.
(execute-kbd-macro "(")
(sp-buffer-equals "(|fooM)"))))
(ert-deftest sp-test-delete-selection-mode-still-works nil
"Test that `delete-selection-pre-hook' still works despite our advice on it."
(sp-test-with-delete-selection-mode
(sp-test-with-temp-elisp-buffer "|fooM"
;; Inserting a character that does not pair should replace the
;; selection when delete-selection-mode is on.
(execute-kbd-macro "x")
(sp-buffer-equals "x|"))))
;; #763
(ert-deftest sp-test-delete-selection-mode-after-turning-sp-off nil
"Don't inhibit `delete-selection-mode' after smartparens is turned off."
(sp-test-with-delete-selection-mode
(sp-test-with-temp-elisp-buffer "|fooM"
(smartparens-mode -1)
(execute-kbd-macro "(")
(sp-buffer-equals "(|"))))
(defun sp-test-wrapping-latex (initial keys result)
(sp-test-with-temp-buffer initial
(latex-mode)
(-each (-list keys) 'execute-kbd-macro)
(should (equal (buffer-string) (replace-regexp-in-string "[|]" "" result)))
(should (= (1+ (string-match-p "|" result)) (point)))))
(ert-deftest sp-test-wrap-basic-latex nil
(let ((sp-pairs '((latex-mode
;; This following pair is a hack to let
;; `sp-wrap--can-wrap-p' pass the test. It
;; should probably be replaced with a check if a
;; wrappable pair exists.
(:open "\"" :close "\"" :actions (insert wrap autoskip navigate))
(:open "$" :close "$" :actions (insert wrap autoskip navigate))
(:open "\\[" :close "\\]" :actions (insert wrap autoskip navigate))
(:open "\\bigl(" :close "\\bigr)" :actions (insert wrap autoskip navigate))
(:open "[" :close "]" :actions (insert wrap autoskip navigate))
(:open "``" :close "''" :trigger "\"" :actions (insert wrap autoskip navigate))
(:open "`" :close "'" :actions (insert wrap autoskip navigate))))))
(sp-test-wrapping-latex "|fooM" "`" "`|foo'")
(sp-test-wrapping-latex "|fooM" "``" "``|foo''")
(sp-test-wrapping-latex "Mfoo|" "`" "`foo'|")
(sp-test-wrapping-latex "Mfoo|" "``" "``foo''|")
(sp-test-wrapping-latex "Mfoo|" "\"" "``foo''|")
(sp-test-wrapping-latex "|fooM" "\"" "``|foo''")))
(defun sp-test-wrapping-python (initial keys result)
(sp-test-with-temp-buffer initial
(shut-up (python-mode))
(-each (-list keys) 'execute-kbd-macro)
(should (equal (buffer-string) (replace-regexp-in-string "[|]" "" result)))
(should (= (1+ (string-match-p "|" result)) (point)))))
(defvar sp-test-wrap-python-pairs
'((python-mode
(:open "\"" :close "\"" :actions (insert wrap autoskip navigate escape) :post-handlers (sp-escape-wrapped-region))
(:open "'" :close "'" :actions (insert wrap autoskip navigate escape) :post-handlers (sp-escape-wrapped-region))
(:open "(" :close ")" :actions (insert wrap autoskip navigate))
(:open "[" :close "]" :actions (insert wrap autoskip navigate)))))
(ert-deftest sp-test-wrap-python-same nil
(let ((sp-pairs sp-test-wrap-python-pairs))
;; wrap word with the same quote as enclosing
(sp-test-wrapping-python "'foo |barM baz'"
"'"
"'foo \\'|bar\\' baz'")
(sp-test-wrapping-python "'foo Mbar| baz'"
"'"
"'foo \\'bar\\'| baz'")))
(ert-deftest sp-test-wrap-python-opposite nil
(let ((sp-pairs sp-test-wrap-python-pairs))
;; wrap word with opposing quote as enclosing
(sp-test-wrapping-python "'foo |barM baz'"
"\""
"'foo \"|bar\" baz'")
(sp-test-wrapping-python "'foo Mbar| baz'"
"\""
"'foo \"bar\"| baz'")))
(ert-deftest sp-test-wrap-python-same-same nil
(let ((sp-pairs sp-test-wrap-python-pairs))
;; wrap same quote with same as enclosing
(sp-test-wrapping-python "'foo |bar \\' bazM qux'"
"'"
"'foo \\'|bar \\\\\\' baz\\' qux'")
(sp-test-wrapping-python "'foo Mbar \\' baz| qux'"
"'"
"'foo \\'bar \\\\\\' baz\\'| qux'")))
(ert-deftest sp-test-wrap-python-same-opposite nil
(let ((sp-pairs sp-test-wrap-python-pairs))
;; wrap same quote with opposing as enclosing
(sp-test-wrapping-python "'foo |bar \\' bazM qux'"
"\""
"'foo \"|bar \\' baz\" qux'")
(sp-test-wrapping-python "'foo Mbar \\' baz| qux'"
"\""
"'foo \"bar \\' baz\"| qux'")))
(ert-deftest sp-test-wrap-python-opposite-same nil
(let ((sp-pairs sp-test-wrap-python-pairs))
;; wrap opposing quote with same as enclosing
(sp-test-wrapping-python "'foo |bar \" bazM qux'"
"'"
"'foo \\'|bar \" baz\\' qux'")
(sp-test-wrapping-python "'foo Mbar \" baz| qux'"
"'"
"'foo \\'bar \" baz\\'| qux'")))
(ert-deftest sp-test-wrap-python-opposite-opposite nil
(let ((sp-pairs sp-test-wrap-python-pairs))
;; wrap opposing quote with opposing as enclosing
(sp-test-wrapping-python "'foo |bar \" bazM qux'"
"\""
"'foo \"|bar \\\" baz\" qux'")
(sp-test-wrapping-python "'foo Mbar \" baz| qux'"
"\""
"'foo \"bar \\\" baz\"| qux'")
))
(ert-deftest sp-test-wrap-python-escaped-opposite-same nil
(let ((sp-pairs sp-test-wrap-python-pairs))
;; wrap escaped opposing quote with same as enclosing
(sp-test-wrapping-python "'foo |bar \\\" bazM qux'"
"'"
"'foo \\'|bar \\\\\" baz\\' qux'")
(sp-test-wrapping-python "'foo Mbar \\\" baz| qux'"
"'"
"'foo \\'bar \\\\\" baz\\'| qux'")))
(ert-deftest sp-test-wrap-python-escaped-opposite-opposite nil
(let ((sp-pairs sp-test-wrap-python-pairs))
;; wrap escaped opposing quote with opposing as enclosing
(sp-test-wrapping-python "'foo |bar \\\" bazM qux'"
"\""
"'foo \"|bar \\\\\" baz\" qux'")
(sp-test-wrapping-python "'foo Mbar \\\" baz| qux'"
"\""
"'foo \"bar \\\\\" baz\"| qux'")))
(ert-deftest sp-test-wrap-autodelete-wrap nil
"Delete the last wrapping pair if DEL is pressed immediately
after wrapping."
(let ((sp-autodelete-wrap t))
(sp-test-with-temp-elisp-buffer "|fooM"
(execute-kbd-macro (kbd "( DEL"))
(sp-buffer-equals "|foo"))))
(ert-deftest sp-test-wrap-autodelete-wrap-strict-mode nil
"Delete the last wrapping pair if DEL is pressed immediately
after wrapping."
(let ((sp-autodelete-wrap t))
(sp-test-with-temp-elisp-buffer "|fooM"
(smartparens-strict-mode 1)
(execute-kbd-macro (kbd "( DEL"))
(sp-buffer-equals "|foo"))))