blob: 41996bd451599abe1b41923a7f5e281a8cd703f5 [file] [log] [blame]
Bram Moolenaar08243d22017-01-10 16:12:29 +01001" Tests for various functions.
2
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +01003" Must be done first, since the alternate buffer must be unset.
4func Test_00_bufexists()
5 call assert_equal(0, bufexists('does_not_exist'))
6 call assert_equal(1, bufexists(bufnr('%')))
7 call assert_equal(0, bufexists(0))
8 new Xfoo
9 let bn = bufnr('%')
10 call assert_equal(1, bufexists(bn))
11 call assert_equal(1, bufexists('Xfoo'))
12 call assert_equal(1, bufexists(getcwd() . '/Xfoo'))
13 call assert_equal(1, bufexists(0))
14 bw
15 call assert_equal(0, bufexists(bn))
16 call assert_equal(0, bufexists('Xfoo'))
17endfunc
18
Bram Moolenaar24c2e482017-01-29 15:45:12 +010019func Test_empty()
20 call assert_equal(1, empty(''))
21 call assert_equal(0, empty('a'))
22
23 call assert_equal(1, empty(0))
24 call assert_equal(1, empty(-0))
25 call assert_equal(0, empty(1))
26 call assert_equal(0, empty(-1))
27
28 call assert_equal(1, empty(0.0))
29 call assert_equal(1, empty(-0.0))
30 call assert_equal(0, empty(1.0))
31 call assert_equal(0, empty(-1.0))
32 call assert_equal(0, empty(1.0/0.0))
33 call assert_equal(0, empty(0.0/0.0))
34
35 call assert_equal(1, empty([]))
36 call assert_equal(0, empty(['a']))
37
38 call assert_equal(1, empty({}))
39 call assert_equal(0, empty({'a':1}))
40
41 call assert_equal(1, empty(v:null))
42 call assert_equal(1, empty(v:none))
43 call assert_equal(1, empty(v:false))
44 call assert_equal(0, empty(v:true))
45
Bram Moolenaar41042f32017-03-09 12:09:32 +010046 if has('channel')
47 call assert_equal(1, empty(test_null_channel()))
48 endif
49 if has('job')
50 call assert_equal(1, empty(test_null_job()))
51 endif
52
Bram Moolenaar24c2e482017-01-29 15:45:12 +010053 call assert_equal(0, empty(function('Test_empty')))
54endfunc
55
56func Test_len()
57 call assert_equal(1, len(0))
58 call assert_equal(2, len(12))
59
60 call assert_equal(0, len(''))
61 call assert_equal(2, len('ab'))
62
63 call assert_equal(0, len([]))
64 call assert_equal(2, len([2, 1]))
65
66 call assert_equal(0, len({}))
67 call assert_equal(2, len({'a': 1, 'b': 2}))
68
69 call assert_fails('call len(v:none)', 'E701:')
70 call assert_fails('call len({-> 0})', 'E701:')
71endfunc
72
73func Test_max()
74 call assert_equal(0, max([]))
75 call assert_equal(2, max([2]))
76 call assert_equal(2, max([1, 2]))
77 call assert_equal(2, max([1, 2, v:null]))
78
79 call assert_equal(0, max({}))
80 call assert_equal(2, max({'a':1, 'b':2}))
81
82 call assert_fails('call max(1)', 'E712:')
83 call assert_fails('call max(v:none)', 'E712:')
84endfunc
85
86func Test_min()
87 call assert_equal(0, min([]))
88 call assert_equal(2, min([2]))
89 call assert_equal(1, min([1, 2]))
90 call assert_equal(0, min([1, 2, v:null]))
91
92 call assert_equal(0, min({}))
93 call assert_equal(1, min({'a':1, 'b':2}))
94
95 call assert_fails('call min(1)', 'E712:')
96 call assert_fails('call min(v:none)', 'E712:')
97endfunc
98
Bram Moolenaar42ab17b2018-05-20 14:11:10 +020099func Test_strwidth()
100 for aw in ['single', 'double']
101 exe 'set ambiwidth=' . aw
102 call assert_equal(0, strwidth(''))
103 call assert_equal(1, strwidth("\t"))
104 call assert_equal(3, strwidth('Vim'))
105 call assert_equal(4, strwidth(1234))
106 call assert_equal(5, strwidth(-1234))
107
108 if has('multi_byte')
109 call assert_equal(2, strwidth('😉'))
110 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
111 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
112 endif
113
114 call assert_fails('call strwidth({->0})', 'E729:')
115 call assert_fails('call strwidth([])', 'E730:')
116 call assert_fails('call strwidth({})', 'E731:')
117 call assert_fails('call strwidth(1.2)', 'E806:')
118 endfor
119
120 set ambiwidth&
121endfunc
122
Bram Moolenaar08243d22017-01-10 16:12:29 +0100123func Test_str2nr()
124 call assert_equal(0, str2nr(''))
125 call assert_equal(1, str2nr('1'))
126 call assert_equal(1, str2nr(' 1 '))
127
128 call assert_equal(1, str2nr('+1'))
129 call assert_equal(1, str2nr('+ 1'))
130 call assert_equal(1, str2nr(' + 1 '))
131
132 call assert_equal(-1, str2nr('-1'))
133 call assert_equal(-1, str2nr('- 1'))
134 call assert_equal(-1, str2nr(' - 1 '))
135
136 call assert_equal(123456789, str2nr('123456789'))
137 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100138
139 call assert_equal(5, str2nr('101', 2))
140 call assert_equal(5, str2nr('0b101', 2))
141 call assert_equal(5, str2nr('0B101', 2))
142 call assert_equal(-5, str2nr('-101', 2))
143 call assert_equal(-5, str2nr('-0b101', 2))
144 call assert_equal(-5, str2nr('-0B101', 2))
145
146 call assert_equal(65, str2nr('101', 8))
147 call assert_equal(65, str2nr('0101', 8))
148 call assert_equal(-65, str2nr('-101', 8))
149 call assert_equal(-65, str2nr('-0101', 8))
150
151 call assert_equal(11259375, str2nr('abcdef', 16))
152 call assert_equal(11259375, str2nr('ABCDEF', 16))
153 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
154 call assert_equal(11259375, str2nr('0xabcdef', 16))
155 call assert_equal(11259375, str2nr('0Xabcdef', 16))
156 call assert_equal(11259375, str2nr('0XABCDEF', 16))
157 call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
158
159 call assert_equal(0, str2nr('0x10'))
160 call assert_equal(0, str2nr('0b10'))
161 call assert_equal(1, str2nr('12', 2))
162 call assert_equal(1, str2nr('18', 8))
163 call assert_equal(1, str2nr('1g', 16))
164
165 call assert_equal(0, str2nr(v:null))
166 call assert_equal(0, str2nr(v:none))
167
168 call assert_fails('call str2nr([])', 'E730:')
169 call assert_fails('call str2nr({->2})', 'E729:')
170 call assert_fails('call str2nr(1.2)', 'E806:')
171 call assert_fails('call str2nr(10, [])', 'E474:')
172endfunc
173
174func Test_strftime()
175 if !exists('*strftime')
176 return
177 endif
178 " Format of strftime() depends on system. We assume
179 " that basic formats tested here are available and
180 " identical on all systems which support strftime().
181 "
182 " The 2nd parameter of strftime() is a local time, so the output day
183 " of strftime() can be 17 or 18, depending on timezone.
184 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
185 "
186 call assert_match('^\d\d\d\d-\(0\d\|1[012]\)-\([012]\d\|3[01]\) \([01]\d\|2[0-3]\):[0-5]\d:\([0-5]\d\|60\)$', strftime('%Y-%m-%d %H:%M:%S'))
187
188 call assert_fails('call strftime([])', 'E730:')
189 call assert_fails('call strftime("%Y", [])', 'E745:')
190endfunc
191
192func Test_simplify()
193 call assert_equal('', simplify(''))
194 call assert_equal('/', simplify('/'))
195 call assert_equal('/', simplify('/.'))
196 call assert_equal('/', simplify('/..'))
197 call assert_equal('/...', simplify('/...'))
198 call assert_equal('./dir/file', simplify('./dir/file'))
199 call assert_equal('./dir/file', simplify('.///dir//file'))
200 call assert_equal('./dir/file', simplify('./dir/./file'))
201 call assert_equal('./file', simplify('./dir/../file'))
202 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
203 call assert_equal('./file', simplify('dir/.././file'))
204
205 call assert_fails('call simplify({->0})', 'E729:')
206 call assert_fails('call simplify([])', 'E730:')
207 call assert_fails('call simplify({})', 'E731:')
208 call assert_fails('call simplify(1.2)', 'E806:')
Bram Moolenaar08243d22017-01-10 16:12:29 +0100209endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100210
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100211func Test_strpart()
212 call assert_equal('de', strpart('abcdefg', 3, 2))
213 call assert_equal('ab', strpart('abcdefg', -2, 4))
214 call assert_equal('abcdefg', strpart('abcdefg', -2))
215 call assert_equal('fg', strpart('abcdefg', 5, 4))
216 call assert_equal('defg', strpart('abcdefg', 3))
217
218 if has('multi_byte')
219 call assert_equal('lép', strpart('éléphant', 2, 4))
220 call assert_equal('léphant', strpart('éléphant', 2))
221 endif
222endfunc
223
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100224func Test_tolower()
225 call assert_equal("", tolower(""))
226
227 " Test with all printable ASCII characters.
228 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
229 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
230
231 if !has('multi_byte')
232 return
233 endif
234
235 " Test with a few uppercase diacritics.
236 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
237 call assert_equal("bḃḇ", tolower("BḂḆ"))
238 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
239 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
240 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
241 call assert_equal("f ", tolower("F "))
242 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
243 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
244 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
245 call assert_equal("jĵ", tolower("JĴ"))
246 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
247 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
248 call assert_equal("mḿṁ", tolower("MḾṀ"))
249 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
250 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
251 call assert_equal("pṕṗ", tolower("PṔṖ"))
252 call assert_equal("q", tolower("Q"))
253 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
254 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
255 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
256 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
257 call assert_equal("v", tolower("V"))
258 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
259 call assert_equal("xẋẍ", tolower("XẊẌ"))
260 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
261 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
262
263 " Test with a few lowercase diacritics, which should remain unchanged.
264 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
265 call assert_equal("bḃḇ", tolower("bḃḇ"))
266 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
267 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
268 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
269 call assert_equal("fḟ", tolower("fḟ"))
270 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
271 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
272 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
273 call assert_equal("jĵǰ", tolower("jĵǰ"))
274 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
275 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
276 call assert_equal("mḿṁ ", tolower("mḿṁ "))
277 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
278 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
279 call assert_equal("pṕṗ", tolower("pṕṗ"))
280 call assert_equal("q", tolower("q"))
281 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
282 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
283 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
284 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
285 call assert_equal("vṽ", tolower("vṽ"))
286 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
287 call assert_equal("ẋẍ", tolower("ẋẍ"))
288 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
289 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
290
291 " According to https://twitter.com/jifa/status/625776454479970304
292 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
293 " in length (2 to 3 bytes) when lowercased. So let's test them.
294 call assert_equal(" ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100295
296 " This call to tolower with invalid utf8 sequence used to cause access to
297 " invalid memory.
298 call tolower("\xC0\x80\xC0")
299 call tolower("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100300endfunc
301
302func Test_toupper()
303 call assert_equal("", toupper(""))
304
305 " Test with all printable ASCII characters.
306 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
307 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
308
309 if !has('multi_byte')
310 return
311 endif
312
313 " Test with a few lowercase diacritics.
314 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("aàáâãäåāăąǎǟǡả"))
315 call assert_equal("BḂḆ", toupper("bḃḇ"))
316 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
317 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
318 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
319 call assert_equal("F", toupper("f"))
320 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
321 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
322 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
323 call assert_equal("JĴǰ", toupper("jĵǰ"))
324 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
325 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
326 call assert_equal("MḾṀ ", toupper("mḿṁ "))
327 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
328 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
329 call assert_equal("PṔṖ", toupper("pṕṗ"))
330 call assert_equal("Q", toupper("q"))
331 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
332 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
333 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
334 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
335 call assert_equal("V", toupper("v"))
336 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
337 call assert_equal("ẊẌ", toupper("ẋẍ"))
338 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
339 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
340
341 " Test that uppercase diacritics, which should remain unchanged.
342 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
343 call assert_equal("BḂḆ", toupper("BḂḆ"))
344 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
345 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
346 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
347 call assert_equal("FḞ ", toupper("FḞ "))
348 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
349 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
350 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
351 call assert_equal("JĴ", toupper("JĴ"))
352 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
353 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
354 call assert_equal("MḾṀ", toupper("MḾṀ"))
355 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
356 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
357 call assert_equal("PṔṖ", toupper("PṔṖ"))
358 call assert_equal("Q", toupper("Q"))
359 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
360 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
361 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
362 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
363 call assert_equal("VṼ", toupper("VṼ"))
364 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
365 call assert_equal("XẊẌ", toupper("XẊẌ"))
366 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
367 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
368
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100369 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100370
371 " This call to toupper with invalid utf8 sequence used to cause access to
372 " invalid memory.
373 call toupper("\xC0\x80\xC0")
374 call toupper("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100375endfunc
376
Bram Moolenaare90858d2017-02-01 17:24:34 +0100377" Tests for the mode() function
378let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100379func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100380 let g:current_modes = mode(0) . '-' . mode(1)
381 return ''
382endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100383
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100384func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100385 new
386 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
387
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100388 " Only complete from the current buffer.
389 set complete=.
390
Bram Moolenaare90858d2017-02-01 17:24:34 +0100391 inoremap <F2> <C-R>=Save_mode()<CR>
392
393 normal! 3G
394 exe "normal i\<F2>\<Esc>"
395 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100396 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100397 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
398 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100399 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100400 exe "normal iBro\<C-P>\<F2>\<Esc>u"
401 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100402 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100403 exe "normal iBa\<C-X>\<F2>\<Esc>u"
404 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100405 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100406 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
407 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100408 " i_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100409 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
410 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100411 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100412 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
413 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100414 " i_CTRL-X CTRL-L: Multiple matches
415 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
416 call assert_equal('i-ic', g:current_modes)
417 " i_CTRL-X CTRL-L: Single match
418 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
419 call assert_equal('i-ic', g:current_modes)
420 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100421 exe "normal iCom\<C-P>\<F2>\<Esc>u"
422 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100423 " i_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100424 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
425 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100426 " i_CTRL-X CTRL-L: No match
427 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
428 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100429
Bram Moolenaare971df32017-02-05 14:15:29 +0100430 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100431 exe "normal RBa\<C-P>\<F2>\<Esc>u"
432 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100433 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100434 exe "normal RBro\<C-P>\<F2>\<Esc>u"
435 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100436 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100437 exe "normal RBa\<C-X>\<F2>\<Esc>u"
438 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100439 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100440 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
441 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100442 " R_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100443 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
444 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100445 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100446 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
447 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100448 " R_CTRL-X CTRL-L: Multiple matches
449 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
450 call assert_equal('R-Rc', g:current_modes)
451 " R_CTRL-X CTRL-L: Single match
452 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
453 call assert_equal('R-Rc', g:current_modes)
454 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100455 exe "normal RCom\<C-P>\<F2>\<Esc>u"
456 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100457 " R_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100458 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
459 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100460 " R_CTRL-X CTRL-L: No match
461 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
462 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100463
464 call assert_equal('n', mode(0))
465 call assert_equal('n', mode(1))
466
Bram Moolenaar612cc382018-07-29 15:34:26 +0200467 " i_CTRL-O
468 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
469 call assert_equal("n-niI", g:current_modes)
470
471 " R_CTRL-O
472 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
473 call assert_equal("n-niR", g:current_modes)
474
475 " gR_CTRL-O
476 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
477 call assert_equal("n-niV", g:current_modes)
478
Bram Moolenaare90858d2017-02-01 17:24:34 +0100479 " How to test operator-pending mode?
480
481 call feedkeys("v", 'xt')
482 call assert_equal('v', mode())
483 call assert_equal('v', mode(1))
484 call feedkeys("\<Esc>V", 'xt')
485 call assert_equal('V', mode())
486 call assert_equal('V', mode(1))
487 call feedkeys("\<Esc>\<C-V>", 'xt')
488 call assert_equal("\<C-V>", mode())
489 call assert_equal("\<C-V>", mode(1))
490 call feedkeys("\<Esc>", 'xt')
491
492 call feedkeys("gh", 'xt')
493 call assert_equal('s', mode())
494 call assert_equal('s', mode(1))
495 call feedkeys("\<Esc>gH", 'xt')
496 call assert_equal('S', mode())
497 call assert_equal('S', mode(1))
498 call feedkeys("\<Esc>g\<C-H>", 'xt')
499 call assert_equal("\<C-S>", mode())
500 call assert_equal("\<C-S>", mode(1))
501 call feedkeys("\<Esc>", 'xt')
502
503 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
504 call assert_equal('c-c', g:current_modes)
505 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
506 call assert_equal('c-cv', g:current_modes)
507 " How to test Ex mode?
508
509 bwipe!
510 iunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100511 set complete&
Bram Moolenaare90858d2017-02-01 17:24:34 +0100512endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100513
514func Test_getbufvar()
515 let bnr = bufnr('%')
516 let b:var_num = '1234'
517 let def_num = '5678'
518 call assert_equal('1234', getbufvar(bnr, 'var_num'))
519 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
520
521 let bd = getbufvar(bnr, '')
522 call assert_equal('1234', bd['var_num'])
523 call assert_true(exists("bd['changedtick']"))
524 call assert_equal(2, len(bd))
525
526 let bd2 = getbufvar(bnr, '', def_num)
527 call assert_equal(bd, bd2)
528
529 unlet b:var_num
530 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
531 call assert_equal('', getbufvar(bnr, 'var_num'))
532
533 let bd = getbufvar(bnr, '')
534 call assert_equal(1, len(bd))
535 let bd = getbufvar(bnr, '',def_num)
536 call assert_equal(1, len(bd))
537
Bram Moolenaar4520d442017-03-19 16:09:46 +0100538 call assert_equal('', getbufvar(9999, ''))
539 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100540 unlet def_num
541
Bram Moolenaar507647d2017-02-17 16:43:49 +0100542 call assert_equal(0, getbufvar(bnr, '&autoindent'))
543 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100544
545 " Open new window with forced option values
546 set fileformats=unix,dos
547 new ++ff=dos ++bin ++enc=iso-8859-2
548 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
549 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
550 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
551 close
552
553 set fileformats&
554endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100555
Bram Moolenaar41042f32017-03-09 12:09:32 +0100556func Test_last_buffer_nr()
557 call assert_equal(bufnr('$'), last_buffer_nr())
558endfunc
559
560func Test_stridx()
561 call assert_equal(-1, stridx('', 'l'))
562 call assert_equal(0, stridx('', ''))
563 call assert_equal(0, stridx('hello', ''))
564 call assert_equal(-1, stridx('hello', 'L'))
565 call assert_equal(2, stridx('hello', 'l', -1))
566 call assert_equal(2, stridx('hello', 'l', 0))
567 call assert_equal(2, stridx('hello', 'l', 1))
568 call assert_equal(3, stridx('hello', 'l', 3))
569 call assert_equal(-1, stridx('hello', 'l', 4))
570 call assert_equal(-1, stridx('hello', 'l', 10))
571 call assert_equal(2, stridx('hello', 'll'))
572 call assert_equal(-1, stridx('hello', 'hello world'))
573endfunc
574
575func Test_strridx()
576 call assert_equal(-1, strridx('', 'l'))
577 call assert_equal(0, strridx('', ''))
578 call assert_equal(5, strridx('hello', ''))
579 call assert_equal(-1, strridx('hello', 'L'))
580 call assert_equal(3, strridx('hello', 'l'))
581 call assert_equal(3, strridx('hello', 'l', 10))
582 call assert_equal(3, strridx('hello', 'l', 3))
583 call assert_equal(2, strridx('hello', 'l', 2))
584 call assert_equal(-1, strridx('hello', 'l', 1))
585 call assert_equal(-1, strridx('hello', 'l', 0))
586 call assert_equal(-1, strridx('hello', 'l', -1))
587 call assert_equal(2, strridx('hello', 'll'))
588 call assert_equal(-1, strridx('hello', 'hello world'))
589endfunc
590
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200591func Test_match_func()
592 call assert_equal(4, match('testing', 'ing'))
593 call assert_equal(4, match('testing', 'ing', 2))
594 call assert_equal(-1, match('testing', 'ing', 5))
595 call assert_equal(-1, match('testing', 'ing', 8))
596 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
597 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
598endfunc
599
Bram Moolenaar41042f32017-03-09 12:09:32 +0100600func Test_matchend()
601 call assert_equal(7, matchend('testing', 'ing'))
602 call assert_equal(7, matchend('testing', 'ing', 2))
603 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200604 call assert_equal(-1, matchend('testing', 'ing', 8))
605 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
606 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
607endfunc
608
609func Test_matchlist()
610 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
611 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
612 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
613endfunc
614
615func Test_matchstr()
616 call assert_equal('ing', matchstr('testing', 'ing'))
617 call assert_equal('ing', matchstr('testing', 'ing', 2))
618 call assert_equal('', matchstr('testing', 'ing', 5))
619 call assert_equal('', matchstr('testing', 'ing', 8))
620 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
621 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
622endfunc
623
624func Test_matchstrpos()
625 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
626 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing', 2))
627 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
628 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
629 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
630 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100631endfunc
632
633func Test_nextnonblank_prevnonblank()
634 new
635insert
636This
637
638
639is
640
641a
642Test
643.
644 call assert_equal(0, nextnonblank(-1))
645 call assert_equal(0, nextnonblank(0))
646 call assert_equal(1, nextnonblank(1))
647 call assert_equal(4, nextnonblank(2))
648 call assert_equal(4, nextnonblank(3))
649 call assert_equal(4, nextnonblank(4))
650 call assert_equal(6, nextnonblank(5))
651 call assert_equal(6, nextnonblank(6))
652 call assert_equal(7, nextnonblank(7))
653 call assert_equal(0, nextnonblank(8))
654
655 call assert_equal(0, prevnonblank(-1))
656 call assert_equal(0, prevnonblank(0))
657 call assert_equal(1, prevnonblank(1))
658 call assert_equal(1, prevnonblank(2))
659 call assert_equal(1, prevnonblank(3))
660 call assert_equal(4, prevnonblank(4))
661 call assert_equal(4, prevnonblank(5))
662 call assert_equal(6, prevnonblank(6))
663 call assert_equal(7, prevnonblank(7))
664 call assert_equal(0, prevnonblank(8))
665 bw!
666endfunc
667
668func Test_byte2line_line2byte()
669 new
670 call setline(1, ['a', 'bc', 'd'])
671
672 set fileformat=unix
673 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
674 \ map(range(-1, 8), 'byte2line(v:val)'))
675 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
676 \ map(range(-1, 5), 'line2byte(v:val)'))
677
678 set fileformat=mac
679 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
680 \ map(range(-1, 8), 'byte2line(v:val)'))
681 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
682 \ map(range(-1, 5), 'line2byte(v:val)'))
683
684 set fileformat=dos
685 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
686 \ map(range(-1, 11), 'byte2line(v:val)'))
687 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
688 \ map(range(-1, 5), 'line2byte(v:val)'))
689
690 set fileformat&
691 bw!
692endfunc
693
694func Test_count()
695 let l = ['a', 'a', 'A', 'b']
696 call assert_equal(2, count(l, 'a'))
697 call assert_equal(1, count(l, 'A'))
698 call assert_equal(1, count(l, 'b'))
699 call assert_equal(0, count(l, 'B'))
700
701 call assert_equal(2, count(l, 'a', 0))
702 call assert_equal(1, count(l, 'A', 0))
703 call assert_equal(1, count(l, 'b', 0))
704 call assert_equal(0, count(l, 'B', 0))
705
706 call assert_equal(3, count(l, 'a', 1))
707 call assert_equal(3, count(l, 'A', 1))
708 call assert_equal(1, count(l, 'b', 1))
709 call assert_equal(1, count(l, 'B', 1))
710 call assert_equal(0, count(l, 'c', 1))
711
712 call assert_equal(1, count(l, 'a', 0, 1))
713 call assert_equal(2, count(l, 'a', 1, 1))
714 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
715
716 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
717 call assert_equal(2, count(d, 'a'))
718 call assert_equal(1, count(d, 'A'))
719 call assert_equal(1, count(d, 'b'))
720 call assert_equal(0, count(d, 'B'))
721
722 call assert_equal(2, count(d, 'a', 0))
723 call assert_equal(1, count(d, 'A', 0))
724 call assert_equal(1, count(d, 'b', 0))
725 call assert_equal(0, count(d, 'B', 0))
726
727 call assert_equal(3, count(d, 'a', 1))
728 call assert_equal(3, count(d, 'A', 1))
729 call assert_equal(1, count(d, 'b', 1))
730 call assert_equal(1, count(d, 'B', 1))
731 call assert_equal(0, count(d, 'c', 1))
732
733 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +0200734
735 call assert_equal(0, count("foo", "bar"))
736 call assert_equal(1, count("foo", "oo"))
737 call assert_equal(2, count("foo", "o"))
738 call assert_equal(0, count("foo", "O"))
739 call assert_equal(2, count("foo", "O", 1))
740 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +0100741 call assert_equal(0, count("foo", ""))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100742endfunc
743
744func Test_changenr()
745 new Xchangenr
746 call assert_equal(0, changenr())
747 norm ifoo
748 call assert_equal(1, changenr())
749 set undolevels=10
750 norm Sbar
751 call assert_equal(2, changenr())
752 undo
753 call assert_equal(1, changenr())
754 redo
755 call assert_equal(2, changenr())
756 bw!
757 set undolevels&
758endfunc
759
760func Test_filewritable()
761 new Xfilewritable
762 write!
763 call assert_equal(1, filewritable('Xfilewritable'))
764
765 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
766 call assert_equal(0, filewritable('Xfilewritable'))
767
768 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
769 call assert_equal(1, filewritable('Xfilewritable'))
770
771 call assert_equal(0, filewritable('doesnotexist'))
772
773 call delete('Xfilewritable')
774 bw!
775endfunc
776
777func Test_hostname()
778 let hostname_vim = hostname()
779 if has('unix')
780 let hostname_system = systemlist('uname -n')[0]
781 call assert_equal(hostname_vim, hostname_system)
782 endif
783endfunc
784
785func Test_getpid()
786 " getpid() always returns the same value within a vim instance.
787 call assert_equal(getpid(), getpid())
788 if has('unix')
789 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
790 endif
791endfunc
792
793func Test_hlexists()
794 call assert_equal(0, hlexists('does_not_exist'))
795 call assert_equal(0, hlexists('Number'))
796 call assert_equal(0, highlight_exists('does_not_exist'))
797 call assert_equal(0, highlight_exists('Number'))
798 syntax on
799 call assert_equal(0, hlexists('does_not_exist'))
800 call assert_equal(1, hlexists('Number'))
801 call assert_equal(0, highlight_exists('does_not_exist'))
802 call assert_equal(1, highlight_exists('Number'))
803 syntax off
804endfunc
805
806func Test_col()
807 new
808 call setline(1, 'abcdef')
809 norm gg4|mx6|mY2|
810 call assert_equal(2, col('.'))
811 call assert_equal(7, col('$'))
812 call assert_equal(4, col("'x"))
813 call assert_equal(6, col("'Y"))
814 call assert_equal(2, col([1, 2]))
815 call assert_equal(7, col([1, '$']))
816
817 call assert_equal(0, col(''))
818 call assert_equal(0, col('x'))
819 call assert_equal(0, col([2, '$']))
820 call assert_equal(0, col([1, 100]))
821 call assert_equal(0, col([1]))
822 bw!
823endfunc
824
Bram Moolenaar947b39e2018-07-22 19:36:37 +0200825func Test_inputlist()
826 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
827 call assert_equal(1, c)
828 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>2\<cr>", 'tx')
829 call assert_equal(2, c)
830 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
831 call assert_equal(3, c)
832
833 call assert_fails('call inputlist("")', 'E686:')
834endfunc
835
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100836func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +0100837 if has('balloon_eval')
838 " This won't do anything but must not crash either.
839 call balloon_show('hi!')
840 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100841endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +0100842
843func Test_setbufvar_options()
844 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
845 " window layout.
846 call assert_equal(1, winnr('$'))
847 split dummy_preview
848 resize 2
849 set winfixheight winfixwidth
850 let prev_id = win_getid()
851
852 wincmd j
853 let wh = winheight('.')
854 let dummy_buf = bufnr('dummy_buf1', v:true)
855 call setbufvar(dummy_buf, '&buftype', 'nofile')
856 execute 'belowright vertical split #' . dummy_buf
857 call assert_equal(wh, winheight('.'))
858 let dum1_id = win_getid()
859
860 wincmd h
861 let wh = winheight('.')
862 let dummy_buf = bufnr('dummy_buf2', v:true)
863 call setbufvar(dummy_buf, '&buftype', 'nofile')
864 execute 'belowright vertical split #' . dummy_buf
865 call assert_equal(wh, winheight('.'))
866
867 bwipe!
868 call win_gotoid(prev_id)
869 bwipe!
870 call win_gotoid(dum1_id)
871 bwipe!
872endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200873
874func Test_redo_in_nested_functions()
875 nnoremap g. :set opfunc=Operator<CR>g@
876 function Operator( type, ... )
877 let @x = 'XXX'
878 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
879 endfunction
880
881 function! Apply()
882 5,6normal! .
883 endfunction
884
885 new
886 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
887 1normal g.i"
888 call assert_equal('some "XXX" text', getline(1))
889 3,4normal .
890 call assert_equal('some "XXX" text', getline(3))
891 call assert_equal('more "XXX" text', getline(4))
892 call Apply()
893 call assert_equal('some "XXX" text', getline(5))
894 call assert_equal('more "XXX" text', getline(6))
895 bwipe!
896
897 nunmap g.
898 delfunc Operator
899 delfunc Apply
900endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +0200901
902func Test_shellescape()
903 let save_shell = &shell
904 set shell=bash
905 call assert_equal("'text'", shellescape('text'))
906 call assert_equal("'te\"xt'", shellescape('te"xt'))
907 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
908
909 call assert_equal("'te%xt'", shellescape("te%xt"))
910 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
911 call assert_equal("'te#xt'", shellescape("te#xt"))
912 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
913 call assert_equal("'te!xt'", shellescape("te!xt"))
914 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
915
916 call assert_equal("'te\nxt'", shellescape("te\nxt"))
917 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
918 set shell=tcsh
919 call assert_equal("'te\\!xt'", shellescape("te!xt"))
920 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
921 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
922 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
923
924 let &shell = save_shell
925endfunc
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100926
927func Test_trim()
928 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
929 call assert_equal("Testing", trim(" \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B"))
930 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
931 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
932 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
933 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
934 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
935 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
936 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
937 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
938 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
939 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
940 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
941 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
942 call assert_equal("", trim("", ""))
943 call assert_equal("a", trim("a", ""))
944 call assert_equal("", trim("", "a"))
945
946 let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '')
947 call assert_equal("x", trim(chars . "x" . chars))
948endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200949
950" Test for reg_recording() and reg_executing()
951func Test_reg_executing_and_recording()
952 let s:reg_stat = ''
953 func s:save_reg_stat()
954 let s:reg_stat = reg_recording() . ':' . reg_executing()
955 return ''
956 endfunc
957
958 new
959 call s:save_reg_stat()
960 call assert_equal(':', s:reg_stat)
961 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
962 call assert_equal('a:', s:reg_stat)
963 call feedkeys("@a", 'xt')
964 call assert_equal(':a', s:reg_stat)
965 call feedkeys("qb@aq", 'xt')
966 call assert_equal('b:a', s:reg_stat)
967 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
968 call assert_equal('":', s:reg_stat)
969
970 bwipe!
971 delfunc s:save_reg_stat
972 unlet s:reg_stat
973endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +0200974
975func Test_libcall_libcallnr()
976 if !has('libcall')
977 return
978 endif
979
980 if has('win32')
981 let libc = 'msvcrt.dll'
982 elseif has('mac')
983 let libc = 'libSystem.B.dylib'
984 else
985 " On Unix, libc.so can be in various places.
986 " Interestingly, using an empty string for the 1st argument of libcall
987 " allows to call functions from libc which is not documented.
988 let libc = ''
989 endif
990
991 if has('win32')
992 call assert_equal($USERPROFILE, libcall(libc, 'getenv', 'USERPROFILE'))
993 else
994 call assert_equal($HOME, libcall(libc, 'getenv', 'HOME'))
995 endif
996
997 " If function returns NULL, libcall() should return an empty string.
998 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
999
1000 " Test libcallnr() with string and integer argument.
1001 call assert_equal(4, libcallnr(libc, 'strlen', 'abcd'))
1002 call assert_equal(char2nr('A'), libcallnr(libc, 'toupper', char2nr('a')))
1003
1004 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1005 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1006
1007 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1008 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1009endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001010
1011sandbox function Fsandbox()
1012 normal ix
1013endfunc
1014
1015func Test_func_sandbox()
1016 sandbox let F = {-> 'hello'}
1017 call assert_equal('hello', F())
1018
1019 sandbox let F = {-> execute("normal ix\<Esc>")}
1020 call assert_fails('call F()', 'E48:')
1021 unlet F
1022
1023 call assert_fails('call Fsandbox()', 'E48:')
1024 delfunc Fsandbox
1025endfunc