blob: 13db0041e7036ef9f8b25b55c1f9aa93b769900e [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
685 call setline(1, ['a', 'bc', 'd'])
686
687 set fileformat=unix
688 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
689 \ map(range(-1, 8), 'byte2line(v:val)'))
690 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
691 \ map(range(-1, 5), 'line2byte(v:val)'))
692
693 set fileformat=mac
694 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
695 \ map(range(-1, 8), 'byte2line(v:val)'))
696 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
697 \ map(range(-1, 5), 'line2byte(v:val)'))
698
699 set fileformat=dos
700 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
701 \ map(range(-1, 11), 'byte2line(v:val)'))
702 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
703 \ map(range(-1, 5), 'line2byte(v:val)'))
704
705 set fileformat&
706 bw!
707endfunc
708
709func Test_count()
710 let l = ['a', 'a', 'A', 'b']
711 call assert_equal(2, count(l, 'a'))
712 call assert_equal(1, count(l, 'A'))
713 call assert_equal(1, count(l, 'b'))
714 call assert_equal(0, count(l, 'B'))
715
716 call assert_equal(2, count(l, 'a', 0))
717 call assert_equal(1, count(l, 'A', 0))
718 call assert_equal(1, count(l, 'b', 0))
719 call assert_equal(0, count(l, 'B', 0))
720
721 call assert_equal(3, count(l, 'a', 1))
722 call assert_equal(3, count(l, 'A', 1))
723 call assert_equal(1, count(l, 'b', 1))
724 call assert_equal(1, count(l, 'B', 1))
725 call assert_equal(0, count(l, 'c', 1))
726
727 call assert_equal(1, count(l, 'a', 0, 1))
728 call assert_equal(2, count(l, 'a', 1, 1))
729 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
730
731 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
732 call assert_equal(2, count(d, 'a'))
733 call assert_equal(1, count(d, 'A'))
734 call assert_equal(1, count(d, 'b'))
735 call assert_equal(0, count(d, 'B'))
736
737 call assert_equal(2, count(d, 'a', 0))
738 call assert_equal(1, count(d, 'A', 0))
739 call assert_equal(1, count(d, 'b', 0))
740 call assert_equal(0, count(d, 'B', 0))
741
742 call assert_equal(3, count(d, 'a', 1))
743 call assert_equal(3, count(d, 'A', 1))
744 call assert_equal(1, count(d, 'b', 1))
745 call assert_equal(1, count(d, 'B', 1))
746 call assert_equal(0, count(d, 'c', 1))
747
748 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +0200749
750 call assert_equal(0, count("foo", "bar"))
751 call assert_equal(1, count("foo", "oo"))
752 call assert_equal(2, count("foo", "o"))
753 call assert_equal(0, count("foo", "O"))
754 call assert_equal(2, count("foo", "O", 1))
755 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +0100756 call assert_equal(0, count("foo", ""))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100757endfunc
758
759func Test_changenr()
760 new Xchangenr
761 call assert_equal(0, changenr())
762 norm ifoo
763 call assert_equal(1, changenr())
764 set undolevels=10
765 norm Sbar
766 call assert_equal(2, changenr())
767 undo
768 call assert_equal(1, changenr())
769 redo
770 call assert_equal(2, changenr())
771 bw!
772 set undolevels&
773endfunc
774
775func Test_filewritable()
776 new Xfilewritable
777 write!
778 call assert_equal(1, filewritable('Xfilewritable'))
779
780 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
781 call assert_equal(0, filewritable('Xfilewritable'))
782
783 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
784 call assert_equal(1, filewritable('Xfilewritable'))
785
786 call assert_equal(0, filewritable('doesnotexist'))
787
788 call delete('Xfilewritable')
789 bw!
790endfunc
791
792func Test_hostname()
793 let hostname_vim = hostname()
794 if has('unix')
795 let hostname_system = systemlist('uname -n')[0]
796 call assert_equal(hostname_vim, hostname_system)
797 endif
798endfunc
799
800func Test_getpid()
801 " getpid() always returns the same value within a vim instance.
802 call assert_equal(getpid(), getpid())
803 if has('unix')
804 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
805 endif
806endfunc
807
808func Test_hlexists()
809 call assert_equal(0, hlexists('does_not_exist'))
810 call assert_equal(0, hlexists('Number'))
811 call assert_equal(0, highlight_exists('does_not_exist'))
812 call assert_equal(0, highlight_exists('Number'))
813 syntax on
814 call assert_equal(0, hlexists('does_not_exist'))
815 call assert_equal(1, hlexists('Number'))
816 call assert_equal(0, highlight_exists('does_not_exist'))
817 call assert_equal(1, highlight_exists('Number'))
818 syntax off
819endfunc
820
821func Test_col()
822 new
823 call setline(1, 'abcdef')
824 norm gg4|mx6|mY2|
825 call assert_equal(2, col('.'))
826 call assert_equal(7, col('$'))
827 call assert_equal(4, col("'x"))
828 call assert_equal(6, col("'Y"))
829 call assert_equal(2, col([1, 2]))
830 call assert_equal(7, col([1, '$']))
831
832 call assert_equal(0, col(''))
833 call assert_equal(0, col('x'))
834 call assert_equal(0, col([2, '$']))
835 call assert_equal(0, col([1, 100]))
836 call assert_equal(0, col([1]))
837 bw!
838endfunc
839
Bram Moolenaar947b39e2018-07-22 19:36:37 +0200840func Test_inputlist()
841 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
842 call assert_equal(1, c)
843 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>2\<cr>", 'tx')
844 call assert_equal(2, c)
845 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
846 call assert_equal(3, c)
847
848 call assert_fails('call inputlist("")', 'E686:')
849endfunc
850
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100851func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +0100852 if has('balloon_eval')
853 " This won't do anything but must not crash either.
854 call balloon_show('hi!')
855 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100856endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +0100857
858func Test_setbufvar_options()
859 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
860 " window layout.
861 call assert_equal(1, winnr('$'))
862 split dummy_preview
863 resize 2
864 set winfixheight winfixwidth
865 let prev_id = win_getid()
866
867 wincmd j
868 let wh = winheight('.')
869 let dummy_buf = bufnr('dummy_buf1', v:true)
870 call setbufvar(dummy_buf, '&buftype', 'nofile')
871 execute 'belowright vertical split #' . dummy_buf
872 call assert_equal(wh, winheight('.'))
873 let dum1_id = win_getid()
874
875 wincmd h
876 let wh = winheight('.')
877 let dummy_buf = bufnr('dummy_buf2', v:true)
878 call setbufvar(dummy_buf, '&buftype', 'nofile')
879 execute 'belowright vertical split #' . dummy_buf
880 call assert_equal(wh, winheight('.'))
881
882 bwipe!
883 call win_gotoid(prev_id)
884 bwipe!
885 call win_gotoid(dum1_id)
886 bwipe!
887endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200888
889func Test_redo_in_nested_functions()
890 nnoremap g. :set opfunc=Operator<CR>g@
891 function Operator( type, ... )
892 let @x = 'XXX'
893 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
894 endfunction
895
896 function! Apply()
897 5,6normal! .
898 endfunction
899
900 new
901 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
902 1normal g.i"
903 call assert_equal('some "XXX" text', getline(1))
904 3,4normal .
905 call assert_equal('some "XXX" text', getline(3))
906 call assert_equal('more "XXX" text', getline(4))
907 call Apply()
908 call assert_equal('some "XXX" text', getline(5))
909 call assert_equal('more "XXX" text', getline(6))
910 bwipe!
911
912 nunmap g.
913 delfunc Operator
914 delfunc Apply
915endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +0200916
917func Test_shellescape()
918 let save_shell = &shell
919 set shell=bash
920 call assert_equal("'text'", shellescape('text'))
921 call assert_equal("'te\"xt'", shellescape('te"xt'))
922 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
923
924 call assert_equal("'te%xt'", shellescape("te%xt"))
925 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
926 call assert_equal("'te#xt'", shellescape("te#xt"))
927 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
928 call assert_equal("'te!xt'", shellescape("te!xt"))
929 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
930
931 call assert_equal("'te\nxt'", shellescape("te\nxt"))
932 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
933 set shell=tcsh
934 call assert_equal("'te\\!xt'", shellescape("te!xt"))
935 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
936 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
937 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
938
939 let &shell = save_shell
940endfunc
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100941
942func Test_trim()
943 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
944 call assert_equal("Testing", trim(" \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B"))
945 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
946 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
947 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
948 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
949 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
950 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
951 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
952 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
953 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
954 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
955 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
956 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
957 call assert_equal("", trim("", ""))
958 call assert_equal("a", trim("a", ""))
959 call assert_equal("", trim("", "a"))
960
961 let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '')
962 call assert_equal("x", trim(chars . "x" . chars))
963endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200964
965" Test for reg_recording() and reg_executing()
966func Test_reg_executing_and_recording()
967 let s:reg_stat = ''
968 func s:save_reg_stat()
969 let s:reg_stat = reg_recording() . ':' . reg_executing()
970 return ''
971 endfunc
972
973 new
974 call s:save_reg_stat()
975 call assert_equal(':', s:reg_stat)
976 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
977 call assert_equal('a:', s:reg_stat)
978 call feedkeys("@a", 'xt')
979 call assert_equal(':a', s:reg_stat)
980 call feedkeys("qb@aq", 'xt')
981 call assert_equal('b:a', s:reg_stat)
982 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
983 call assert_equal('":', s:reg_stat)
984
985 bwipe!
986 delfunc s:save_reg_stat
987 unlet s:reg_stat
988endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +0200989
990func Test_libcall_libcallnr()
991 if !has('libcall')
992 return
993 endif
994
995 if has('win32')
996 let libc = 'msvcrt.dll'
997 elseif has('mac')
998 let libc = 'libSystem.B.dylib'
999 else
1000 " On Unix, libc.so can be in various places.
1001 " Interestingly, using an empty string for the 1st argument of libcall
1002 " allows to call functions from libc which is not documented.
1003 let libc = ''
1004 endif
1005
1006 if has('win32')
1007 call assert_equal($USERPROFILE, libcall(libc, 'getenv', 'USERPROFILE'))
1008 else
1009 call assert_equal($HOME, libcall(libc, 'getenv', 'HOME'))
1010 endif
1011
1012 " If function returns NULL, libcall() should return an empty string.
1013 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1014
1015 " Test libcallnr() with string and integer argument.
1016 call assert_equal(4, libcallnr(libc, 'strlen', 'abcd'))
1017 call assert_equal(char2nr('A'), libcallnr(libc, 'toupper', char2nr('a')))
1018
1019 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1020 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1021
1022 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1023 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1024endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001025
1026sandbox function Fsandbox()
1027 normal ix
1028endfunc
1029
1030func Test_func_sandbox()
1031 sandbox let F = {-> 'hello'}
1032 call assert_equal('hello', F())
1033
1034 sandbox let F = {-> execute("normal ix\<Esc>")}
1035 call assert_fails('call F()', 'E48:')
1036 unlet F
1037
1038 call assert_fails('call Fsandbox()', 'E48:')
1039 delfunc Fsandbox
1040endfunc