blob: 848e7986c30607f6ad9323532e43939835c1a99a [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 Moolenaarbfde0b42018-08-08 22:27:31 +0200211func Test_pathshorten()
212 call assert_equal('', pathshorten(''))
213 call assert_equal('foo', pathshorten('foo'))
214 call assert_equal('/foo', pathshorten('/foo'))
215 call assert_equal('f/', pathshorten('foo/'))
216 call assert_equal('f/bar', pathshorten('foo/bar'))
217 call assert_equal('f/b/foobar', pathshorten('foo/bar/foobar'))
218 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
219 call assert_equal('.f/bar', pathshorten('.foo/bar'))
220 call assert_equal('~f/bar', pathshorten('~foo/bar'))
221 call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
222 call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
223 call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
224endfunc
225
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100226func Test_strpart()
227 call assert_equal('de', strpart('abcdefg', 3, 2))
228 call assert_equal('ab', strpart('abcdefg', -2, 4))
229 call assert_equal('abcdefg', strpart('abcdefg', -2))
230 call assert_equal('fg', strpart('abcdefg', 5, 4))
231 call assert_equal('defg', strpart('abcdefg', 3))
232
233 if has('multi_byte')
234 call assert_equal('lép', strpart('éléphant', 2, 4))
235 call assert_equal('léphant', strpart('éléphant', 2))
236 endif
237endfunc
238
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100239func Test_tolower()
240 call assert_equal("", tolower(""))
241
242 " Test with all printable ASCII characters.
243 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
244 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
245
246 if !has('multi_byte')
247 return
248 endif
249
250 " Test with a few uppercase diacritics.
251 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
252 call assert_equal("bḃḇ", tolower("BḂḆ"))
253 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
254 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
255 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
256 call assert_equal("f ", tolower("F "))
257 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
258 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
259 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
260 call assert_equal("jĵ", tolower("JĴ"))
261 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
262 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
263 call assert_equal("mḿṁ", tolower("MḾṀ"))
264 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
265 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
266 call assert_equal("pṕṗ", tolower("PṔṖ"))
267 call assert_equal("q", tolower("Q"))
268 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
269 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
270 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
271 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
272 call assert_equal("v", tolower("V"))
273 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
274 call assert_equal("xẋẍ", tolower("XẊẌ"))
275 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
276 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
277
278 " Test with a few lowercase diacritics, which should remain unchanged.
279 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
280 call assert_equal("bḃḇ", tolower("bḃḇ"))
281 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
282 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
283 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
284 call assert_equal("fḟ", tolower("fḟ"))
285 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
286 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
287 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
288 call assert_equal("jĵǰ", tolower("jĵǰ"))
289 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
290 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
291 call assert_equal("mḿṁ ", tolower("mḿṁ "))
292 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
293 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
294 call assert_equal("pṕṗ", tolower("pṕṗ"))
295 call assert_equal("q", tolower("q"))
296 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
297 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
298 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
299 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
300 call assert_equal("vṽ", tolower("vṽ"))
301 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
302 call assert_equal("ẋẍ", tolower("ẋẍ"))
303 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
304 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
305
306 " According to https://twitter.com/jifa/status/625776454479970304
307 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
308 " in length (2 to 3 bytes) when lowercased. So let's test them.
309 call assert_equal(" ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100310
311 " This call to tolower with invalid utf8 sequence used to cause access to
312 " invalid memory.
313 call tolower("\xC0\x80\xC0")
314 call tolower("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100315endfunc
316
317func Test_toupper()
318 call assert_equal("", toupper(""))
319
320 " Test with all printable ASCII characters.
321 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
322 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
323
324 if !has('multi_byte')
325 return
326 endif
327
328 " Test with a few lowercase diacritics.
329 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("aàáâãäåāăąǎǟǡả"))
330 call assert_equal("BḂḆ", toupper("bḃḇ"))
331 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
332 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
333 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
334 call assert_equal("F", toupper("f"))
335 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
336 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
337 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
338 call assert_equal("JĴǰ", toupper("jĵǰ"))
339 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
340 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
341 call assert_equal("MḾṀ ", toupper("mḿṁ "))
342 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
343 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
344 call assert_equal("PṔṖ", toupper("pṕṗ"))
345 call assert_equal("Q", toupper("q"))
346 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
347 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
348 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
349 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
350 call assert_equal("V", toupper("v"))
351 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
352 call assert_equal("ẊẌ", toupper("ẋẍ"))
353 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
354 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
355
356 " Test that uppercase diacritics, which should remain unchanged.
357 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
358 call assert_equal("BḂḆ", toupper("BḂḆ"))
359 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
360 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
361 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
362 call assert_equal("FḞ ", toupper("FḞ "))
363 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
364 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
365 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
366 call assert_equal("JĴ", toupper("JĴ"))
367 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
368 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
369 call assert_equal("MḾṀ", toupper("MḾṀ"))
370 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
371 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
372 call assert_equal("PṔṖ", toupper("PṔṖ"))
373 call assert_equal("Q", toupper("Q"))
374 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
375 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
376 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
377 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
378 call assert_equal("VṼ", toupper("VṼ"))
379 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
380 call assert_equal("XẊẌ", toupper("XẊẌ"))
381 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
382 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
383
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100384 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100385
386 " This call to toupper with invalid utf8 sequence used to cause access to
387 " invalid memory.
388 call toupper("\xC0\x80\xC0")
389 call toupper("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100390endfunc
391
Bram Moolenaare90858d2017-02-01 17:24:34 +0100392" Tests for the mode() function
393let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100394func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100395 let g:current_modes = mode(0) . '-' . mode(1)
396 return ''
397endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100398
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100399func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100400 new
401 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
402
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100403 " Only complete from the current buffer.
404 set complete=.
405
Bram Moolenaare90858d2017-02-01 17:24:34 +0100406 inoremap <F2> <C-R>=Save_mode()<CR>
407
408 normal! 3G
409 exe "normal i\<F2>\<Esc>"
410 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100411 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100412 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
413 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100414 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100415 exe "normal iBro\<C-P>\<F2>\<Esc>u"
416 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100417 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100418 exe "normal iBa\<C-X>\<F2>\<Esc>u"
419 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100420 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100421 exe "normal iBa\<C-X>\<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: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100424 exe "normal iBro\<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-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100427 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
428 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100429 " i_CTRL-X CTRL-L: Multiple matches
430 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
431 call assert_equal('i-ic', g:current_modes)
432 " i_CTRL-X CTRL-L: Single match
433 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
434 call assert_equal('i-ic', g:current_modes)
435 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100436 exe "normal iCom\<C-P>\<F2>\<Esc>u"
437 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100438 " i_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100439 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
440 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100441 " i_CTRL-X CTRL-L: No match
442 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
443 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100444
Bram Moolenaare971df32017-02-05 14:15:29 +0100445 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100446 exe "normal RBa\<C-P>\<F2>\<Esc>u"
447 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100448 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100449 exe "normal RBro\<C-P>\<F2>\<Esc>u"
450 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100451 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100452 exe "normal RBa\<C-X>\<F2>\<Esc>u"
453 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100454 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100455 exe "normal RBa\<C-X>\<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: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100458 exe "normal RBro\<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-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100461 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
462 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100463 " R_CTRL-X CTRL-L: Multiple matches
464 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
465 call assert_equal('R-Rc', g:current_modes)
466 " R_CTRL-X CTRL-L: Single match
467 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
468 call assert_equal('R-Rc', g:current_modes)
469 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100470 exe "normal RCom\<C-P>\<F2>\<Esc>u"
471 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100472 " R_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100473 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
474 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100475 " R_CTRL-X CTRL-L: No match
476 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
477 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100478
479 call assert_equal('n', mode(0))
480 call assert_equal('n', mode(1))
481
Bram Moolenaar612cc382018-07-29 15:34:26 +0200482 " i_CTRL-O
483 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
484 call assert_equal("n-niI", g:current_modes)
485
486 " R_CTRL-O
487 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
488 call assert_equal("n-niR", g:current_modes)
489
490 " gR_CTRL-O
491 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
492 call assert_equal("n-niV", g:current_modes)
493
Bram Moolenaare90858d2017-02-01 17:24:34 +0100494 " How to test operator-pending mode?
495
496 call feedkeys("v", 'xt')
497 call assert_equal('v', mode())
498 call assert_equal('v', mode(1))
499 call feedkeys("\<Esc>V", 'xt')
500 call assert_equal('V', mode())
501 call assert_equal('V', mode(1))
502 call feedkeys("\<Esc>\<C-V>", 'xt')
503 call assert_equal("\<C-V>", mode())
504 call assert_equal("\<C-V>", mode(1))
505 call feedkeys("\<Esc>", 'xt')
506
507 call feedkeys("gh", 'xt')
508 call assert_equal('s', mode())
509 call assert_equal('s', mode(1))
510 call feedkeys("\<Esc>gH", 'xt')
511 call assert_equal('S', mode())
512 call assert_equal('S', mode(1))
513 call feedkeys("\<Esc>g\<C-H>", 'xt')
514 call assert_equal("\<C-S>", mode())
515 call assert_equal("\<C-S>", mode(1))
516 call feedkeys("\<Esc>", 'xt')
517
518 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
519 call assert_equal('c-c', g:current_modes)
520 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
521 call assert_equal('c-cv', g:current_modes)
522 " How to test Ex mode?
523
524 bwipe!
525 iunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100526 set complete&
Bram Moolenaare90858d2017-02-01 17:24:34 +0100527endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100528
529func Test_getbufvar()
530 let bnr = bufnr('%')
531 let b:var_num = '1234'
532 let def_num = '5678'
533 call assert_equal('1234', getbufvar(bnr, 'var_num'))
534 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
535
536 let bd = getbufvar(bnr, '')
537 call assert_equal('1234', bd['var_num'])
538 call assert_true(exists("bd['changedtick']"))
539 call assert_equal(2, len(bd))
540
541 let bd2 = getbufvar(bnr, '', def_num)
542 call assert_equal(bd, bd2)
543
544 unlet b:var_num
545 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
546 call assert_equal('', getbufvar(bnr, 'var_num'))
547
548 let bd = getbufvar(bnr, '')
549 call assert_equal(1, len(bd))
550 let bd = getbufvar(bnr, '',def_num)
551 call assert_equal(1, len(bd))
552
Bram Moolenaar4520d442017-03-19 16:09:46 +0100553 call assert_equal('', getbufvar(9999, ''))
554 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100555 unlet def_num
556
Bram Moolenaar507647d2017-02-17 16:43:49 +0100557 call assert_equal(0, getbufvar(bnr, '&autoindent'))
558 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100559
560 " Open new window with forced option values
561 set fileformats=unix,dos
562 new ++ff=dos ++bin ++enc=iso-8859-2
563 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
564 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
565 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
566 close
567
568 set fileformats&
569endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100570
Bram Moolenaar41042f32017-03-09 12:09:32 +0100571func Test_last_buffer_nr()
572 call assert_equal(bufnr('$'), last_buffer_nr())
573endfunc
574
575func Test_stridx()
576 call assert_equal(-1, stridx('', 'l'))
577 call assert_equal(0, stridx('', ''))
578 call assert_equal(0, stridx('hello', ''))
579 call assert_equal(-1, stridx('hello', 'L'))
580 call assert_equal(2, stridx('hello', 'l', -1))
581 call assert_equal(2, stridx('hello', 'l', 0))
582 call assert_equal(2, stridx('hello', 'l', 1))
583 call assert_equal(3, stridx('hello', 'l', 3))
584 call assert_equal(-1, stridx('hello', 'l', 4))
585 call assert_equal(-1, stridx('hello', 'l', 10))
586 call assert_equal(2, stridx('hello', 'll'))
587 call assert_equal(-1, stridx('hello', 'hello world'))
588endfunc
589
590func Test_strridx()
591 call assert_equal(-1, strridx('', 'l'))
592 call assert_equal(0, strridx('', ''))
593 call assert_equal(5, strridx('hello', ''))
594 call assert_equal(-1, strridx('hello', 'L'))
595 call assert_equal(3, strridx('hello', 'l'))
596 call assert_equal(3, strridx('hello', 'l', 10))
597 call assert_equal(3, strridx('hello', 'l', 3))
598 call assert_equal(2, strridx('hello', 'l', 2))
599 call assert_equal(-1, strridx('hello', 'l', 1))
600 call assert_equal(-1, strridx('hello', 'l', 0))
601 call assert_equal(-1, strridx('hello', 'l', -1))
602 call assert_equal(2, strridx('hello', 'll'))
603 call assert_equal(-1, strridx('hello', 'hello world'))
604endfunc
605
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200606func Test_match_func()
607 call assert_equal(4, match('testing', 'ing'))
608 call assert_equal(4, match('testing', 'ing', 2))
609 call assert_equal(-1, match('testing', 'ing', 5))
610 call assert_equal(-1, match('testing', 'ing', 8))
611 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
612 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
613endfunc
614
Bram Moolenaar41042f32017-03-09 12:09:32 +0100615func Test_matchend()
616 call assert_equal(7, matchend('testing', 'ing'))
617 call assert_equal(7, matchend('testing', 'ing', 2))
618 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200619 call assert_equal(-1, matchend('testing', 'ing', 8))
620 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
621 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
622endfunc
623
624func Test_matchlist()
625 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
626 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
627 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
628endfunc
629
630func Test_matchstr()
631 call assert_equal('ing', matchstr('testing', 'ing'))
632 call assert_equal('ing', matchstr('testing', 'ing', 2))
633 call assert_equal('', matchstr('testing', 'ing', 5))
634 call assert_equal('', matchstr('testing', 'ing', 8))
635 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
636 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
637endfunc
638
639func Test_matchstrpos()
640 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
641 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing', 2))
642 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
643 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
644 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
645 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100646endfunc
647
648func Test_nextnonblank_prevnonblank()
649 new
650insert
651This
652
653
654is
655
656a
657Test
658.
659 call assert_equal(0, nextnonblank(-1))
660 call assert_equal(0, nextnonblank(0))
661 call assert_equal(1, nextnonblank(1))
662 call assert_equal(4, nextnonblank(2))
663 call assert_equal(4, nextnonblank(3))
664 call assert_equal(4, nextnonblank(4))
665 call assert_equal(6, nextnonblank(5))
666 call assert_equal(6, nextnonblank(6))
667 call assert_equal(7, nextnonblank(7))
668 call assert_equal(0, nextnonblank(8))
669
670 call assert_equal(0, prevnonblank(-1))
671 call assert_equal(0, prevnonblank(0))
672 call assert_equal(1, prevnonblank(1))
673 call assert_equal(1, prevnonblank(2))
674 call assert_equal(1, prevnonblank(3))
675 call assert_equal(4, prevnonblank(4))
676 call assert_equal(4, prevnonblank(5))
677 call assert_equal(6, prevnonblank(6))
678 call assert_equal(7, prevnonblank(7))
679 call assert_equal(0, prevnonblank(8))
680 bw!
681endfunc
682
683func Test_byte2line_line2byte()
684 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200685 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +0100686 call setline(1, ['a', 'bc', 'd'])
687
688 set fileformat=unix
689 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
690 \ map(range(-1, 8), 'byte2line(v:val)'))
691 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
692 \ map(range(-1, 5), 'line2byte(v:val)'))
693
694 set fileformat=mac
695 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
696 \ map(range(-1, 8), 'byte2line(v:val)'))
697 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
698 \ map(range(-1, 5), 'line2byte(v:val)'))
699
700 set fileformat=dos
701 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
702 \ map(range(-1, 11), 'byte2line(v:val)'))
703 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
704 \ map(range(-1, 5), 'line2byte(v:val)'))
705
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200706 bw!
707 set noendofline nofixendofline
708 normal a-
709 for ff in ["unix", "mac", "dos"]
710 let &fileformat = ff
711 call assert_equal(1, line2byte(1))
712 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
713 endfor
714
715 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +0100716 bw!
717endfunc
718
719func Test_count()
720 let l = ['a', 'a', 'A', 'b']
721 call assert_equal(2, count(l, 'a'))
722 call assert_equal(1, count(l, 'A'))
723 call assert_equal(1, count(l, 'b'))
724 call assert_equal(0, count(l, 'B'))
725
726 call assert_equal(2, count(l, 'a', 0))
727 call assert_equal(1, count(l, 'A', 0))
728 call assert_equal(1, count(l, 'b', 0))
729 call assert_equal(0, count(l, 'B', 0))
730
731 call assert_equal(3, count(l, 'a', 1))
732 call assert_equal(3, count(l, 'A', 1))
733 call assert_equal(1, count(l, 'b', 1))
734 call assert_equal(1, count(l, 'B', 1))
735 call assert_equal(0, count(l, 'c', 1))
736
737 call assert_equal(1, count(l, 'a', 0, 1))
738 call assert_equal(2, count(l, 'a', 1, 1))
739 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
740
741 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
742 call assert_equal(2, count(d, 'a'))
743 call assert_equal(1, count(d, 'A'))
744 call assert_equal(1, count(d, 'b'))
745 call assert_equal(0, count(d, 'B'))
746
747 call assert_equal(2, count(d, 'a', 0))
748 call assert_equal(1, count(d, 'A', 0))
749 call assert_equal(1, count(d, 'b', 0))
750 call assert_equal(0, count(d, 'B', 0))
751
752 call assert_equal(3, count(d, 'a', 1))
753 call assert_equal(3, count(d, 'A', 1))
754 call assert_equal(1, count(d, 'b', 1))
755 call assert_equal(1, count(d, 'B', 1))
756 call assert_equal(0, count(d, 'c', 1))
757
758 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +0200759
760 call assert_equal(0, count("foo", "bar"))
761 call assert_equal(1, count("foo", "oo"))
762 call assert_equal(2, count("foo", "o"))
763 call assert_equal(0, count("foo", "O"))
764 call assert_equal(2, count("foo", "O", 1))
765 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +0100766 call assert_equal(0, count("foo", ""))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100767endfunc
768
769func Test_changenr()
770 new Xchangenr
771 call assert_equal(0, changenr())
772 norm ifoo
773 call assert_equal(1, changenr())
774 set undolevels=10
775 norm Sbar
776 call assert_equal(2, changenr())
777 undo
778 call assert_equal(1, changenr())
779 redo
780 call assert_equal(2, changenr())
781 bw!
782 set undolevels&
783endfunc
784
785func Test_filewritable()
786 new Xfilewritable
787 write!
788 call assert_equal(1, filewritable('Xfilewritable'))
789
790 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
791 call assert_equal(0, filewritable('Xfilewritable'))
792
793 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
794 call assert_equal(1, filewritable('Xfilewritable'))
795
796 call assert_equal(0, filewritable('doesnotexist'))
797
798 call delete('Xfilewritable')
799 bw!
800endfunc
801
802func Test_hostname()
803 let hostname_vim = hostname()
804 if has('unix')
805 let hostname_system = systemlist('uname -n')[0]
806 call assert_equal(hostname_vim, hostname_system)
807 endif
808endfunc
809
810func Test_getpid()
811 " getpid() always returns the same value within a vim instance.
812 call assert_equal(getpid(), getpid())
813 if has('unix')
814 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
815 endif
816endfunc
817
818func Test_hlexists()
819 call assert_equal(0, hlexists('does_not_exist'))
820 call assert_equal(0, hlexists('Number'))
821 call assert_equal(0, highlight_exists('does_not_exist'))
822 call assert_equal(0, highlight_exists('Number'))
823 syntax on
824 call assert_equal(0, hlexists('does_not_exist'))
825 call assert_equal(1, hlexists('Number'))
826 call assert_equal(0, highlight_exists('does_not_exist'))
827 call assert_equal(1, highlight_exists('Number'))
828 syntax off
829endfunc
830
831func Test_col()
832 new
833 call setline(1, 'abcdef')
834 norm gg4|mx6|mY2|
835 call assert_equal(2, col('.'))
836 call assert_equal(7, col('$'))
837 call assert_equal(4, col("'x"))
838 call assert_equal(6, col("'Y"))
839 call assert_equal(2, col([1, 2]))
840 call assert_equal(7, col([1, '$']))
841
842 call assert_equal(0, col(''))
843 call assert_equal(0, col('x'))
844 call assert_equal(0, col([2, '$']))
845 call assert_equal(0, col([1, 100]))
846 call assert_equal(0, col([1]))
847 bw!
848endfunc
849
Bram Moolenaar947b39e2018-07-22 19:36:37 +0200850func Test_inputlist()
851 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
852 call assert_equal(1, c)
853 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>2\<cr>", 'tx')
854 call assert_equal(2, c)
855 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
856 call assert_equal(3, c)
857
858 call assert_fails('call inputlist("")', 'E686:')
859endfunc
860
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100861func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +0100862 if has('balloon_eval')
863 " This won't do anything but must not crash either.
864 call balloon_show('hi!')
865 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100866endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +0100867
868func Test_setbufvar_options()
869 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
870 " window layout.
871 call assert_equal(1, winnr('$'))
872 split dummy_preview
873 resize 2
874 set winfixheight winfixwidth
875 let prev_id = win_getid()
876
877 wincmd j
878 let wh = winheight('.')
879 let dummy_buf = bufnr('dummy_buf1', v:true)
880 call setbufvar(dummy_buf, '&buftype', 'nofile')
881 execute 'belowright vertical split #' . dummy_buf
882 call assert_equal(wh, winheight('.'))
883 let dum1_id = win_getid()
884
885 wincmd h
886 let wh = winheight('.')
887 let dummy_buf = bufnr('dummy_buf2', v:true)
888 call setbufvar(dummy_buf, '&buftype', 'nofile')
889 execute 'belowright vertical split #' . dummy_buf
890 call assert_equal(wh, winheight('.'))
891
892 bwipe!
893 call win_gotoid(prev_id)
894 bwipe!
895 call win_gotoid(dum1_id)
896 bwipe!
897endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200898
899func Test_redo_in_nested_functions()
900 nnoremap g. :set opfunc=Operator<CR>g@
901 function Operator( type, ... )
902 let @x = 'XXX'
903 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
904 endfunction
905
906 function! Apply()
907 5,6normal! .
908 endfunction
909
910 new
911 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
912 1normal g.i"
913 call assert_equal('some "XXX" text', getline(1))
914 3,4normal .
915 call assert_equal('some "XXX" text', getline(3))
916 call assert_equal('more "XXX" text', getline(4))
917 call Apply()
918 call assert_equal('some "XXX" text', getline(5))
919 call assert_equal('more "XXX" text', getline(6))
920 bwipe!
921
922 nunmap g.
923 delfunc Operator
924 delfunc Apply
925endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +0200926
927func Test_shellescape()
928 let save_shell = &shell
929 set shell=bash
930 call assert_equal("'text'", shellescape('text'))
931 call assert_equal("'te\"xt'", shellescape('te"xt'))
932 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
933
934 call assert_equal("'te%xt'", shellescape("te%xt"))
935 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
936 call assert_equal("'te#xt'", shellescape("te#xt"))
937 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
938 call assert_equal("'te!xt'", shellescape("te!xt"))
939 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
940
941 call assert_equal("'te\nxt'", shellescape("te\nxt"))
942 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
943 set shell=tcsh
944 call assert_equal("'te\\!xt'", shellescape("te!xt"))
945 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
946 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
947 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
948
949 let &shell = save_shell
950endfunc
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100951
952func Test_trim()
953 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
954 call assert_equal("Testing", trim(" \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B"))
955 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
956 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
957 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
958 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
959 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
960 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
961 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
962 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
963 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
964 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
965 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
966 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
967 call assert_equal("", trim("", ""))
968 call assert_equal("a", trim("a", ""))
969 call assert_equal("", trim("", "a"))
970
971 let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '')
972 call assert_equal("x", trim(chars . "x" . chars))
973endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200974
975" Test for reg_recording() and reg_executing()
976func Test_reg_executing_and_recording()
977 let s:reg_stat = ''
978 func s:save_reg_stat()
979 let s:reg_stat = reg_recording() . ':' . reg_executing()
980 return ''
981 endfunc
982
983 new
984 call s:save_reg_stat()
985 call assert_equal(':', s:reg_stat)
986 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
987 call assert_equal('a:', s:reg_stat)
988 call feedkeys("@a", 'xt')
989 call assert_equal(':a', s:reg_stat)
990 call feedkeys("qb@aq", 'xt')
991 call assert_equal('b:a', s:reg_stat)
992 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
993 call assert_equal('":', s:reg_stat)
994
995 bwipe!
996 delfunc s:save_reg_stat
997 unlet s:reg_stat
998endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +0200999
1000func Test_libcall_libcallnr()
1001 if !has('libcall')
1002 return
1003 endif
1004
1005 if has('win32')
1006 let libc = 'msvcrt.dll'
1007 elseif has('mac')
1008 let libc = 'libSystem.B.dylib'
1009 else
1010 " On Unix, libc.so can be in various places.
1011 " Interestingly, using an empty string for the 1st argument of libcall
1012 " allows to call functions from libc which is not documented.
1013 let libc = ''
1014 endif
1015
1016 if has('win32')
1017 call assert_equal($USERPROFILE, libcall(libc, 'getenv', 'USERPROFILE'))
1018 else
1019 call assert_equal($HOME, libcall(libc, 'getenv', 'HOME'))
1020 endif
1021
1022 " If function returns NULL, libcall() should return an empty string.
1023 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1024
1025 " Test libcallnr() with string and integer argument.
1026 call assert_equal(4, libcallnr(libc, 'strlen', 'abcd'))
1027 call assert_equal(char2nr('A'), libcallnr(libc, 'toupper', char2nr('a')))
1028
1029 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1030 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1031
1032 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1033 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1034endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001035
1036sandbox function Fsandbox()
1037 normal ix
1038endfunc
1039
1040func Test_func_sandbox()
1041 sandbox let F = {-> 'hello'}
1042 call assert_equal('hello', F())
1043
1044 sandbox let F = {-> execute("normal ix\<Esc>")}
1045 call assert_fails('call F()', 'E48:')
1046 unlet F
1047
1048 call assert_fails('call Fsandbox()', 'E48:')
1049 delfunc Fsandbox
1050endfunc