summaryrefslogtreecommitdiff
path: root/test/evil-surround-test.el
blob: df8a03f2bbd60ccd736a5ecc88a912ca01af9354 (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

(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 ("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 ("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 ("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 ("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 ("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)")))